e863f7e34023e92b1ecef8514cbd3c356e66d7df
[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, 2006, 2007, 2008, 2009 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 3 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
24 /* This file is based on a preliminary PowerPC ELF ABI.  The
25    information may not match the final PowerPC ELF ABI.  It includes
26    suggestions from the in-progress Embedded PowerPC ABI, and that
27    information may also not match.  */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc.h"
36 #include "elf32-ppc.h"
37 #include "elf-vxworks.h"
38
39 /* RELA relocations are used here.  */
40
41 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc_elf_unhandled_reloc
44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45
46 /* Branch prediction bit for branch taken relocs.  */
47 #define BRANCH_PREDICT_BIT 0x200000
48 /* Mask to set RA in memory instructions.  */
49 #define RA_REGISTER_MASK 0x001f0000
50 /* Value to shift register by to insert RA.  */
51 #define RA_REGISTER_SHIFT 16
52
53 /* The name of the dynamic interpreter.  This is put in the .interp
54    section.  */
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
56
57 /* For old-style PLT.  */
58 /* The number of single-slot PLT entries (the rest use two slots).  */
59 #define PLT_NUM_SINGLE_ENTRIES 8192
60
61 /* For new-style .glink and .plt.  */
62 #define GLINK_PLTRESOLVE 16*4
63 #define GLINK_ENTRY_SIZE 4*4
64 #define TLS_GET_ADDR_GLINK_SIZE 12*4
65
66 /* VxWorks uses its own plt layout, filled in by the static linker.  */
67
68 /* The standard VxWorks PLT entry.  */
69 #define VXWORKS_PLT_ENTRY_SIZE 32
70 static const bfd_vma ppc_elf_vxworks_plt_entry
71     [VXWORKS_PLT_ENTRY_SIZE / 4] =
72   {
73     0x3d800000, /* lis     r12,0                 */
74     0x818c0000, /* lwz     r12,0(r12)            */
75     0x7d8903a6, /* mtctr   r12                   */
76     0x4e800420, /* bctr                          */
77     0x39600000, /* li      r11,0                 */
78     0x48000000, /* b       14 <.PLT0resolve+0x4> */
79     0x60000000, /* nop                           */
80     0x60000000, /* nop                           */
81   };
82 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
83     [VXWORKS_PLT_ENTRY_SIZE / 4] =
84   {
85     0x3d9e0000, /* addis r12,r30,0 */
86     0x818c0000, /* lwz   r12,0(r12) */
87     0x7d8903a6, /* mtctr r12 */
88     0x4e800420, /* bctr */
89     0x39600000, /* li    r11,0 */
90     0x48000000, /* b     14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
91     0x60000000, /* nop */
92     0x60000000, /* nop */
93   };
94
95 /* The initial VxWorks PLT entry.  */
96 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
97 static const bfd_vma ppc_elf_vxworks_plt0_entry
98     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
99   {
100     0x3d800000, /* lis     r12,0        */
101     0x398c0000, /* addi    r12,r12,0    */
102     0x800c0008, /* lwz     r0,8(r12)    */
103     0x7c0903a6, /* mtctr   r0           */
104     0x818c0004, /* lwz     r12,4(r12)   */
105     0x4e800420, /* bctr                 */
106     0x60000000, /* nop                  */
107     0x60000000, /* nop                  */
108   };
109 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
110     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
111   {
112     0x819e0008, /* lwz   r12,8(r30) */
113     0x7d8903a6, /* mtctr r12        */
114     0x819e0004, /* lwz   r12,4(r30) */
115     0x4e800420, /* bctr             */
116     0x60000000, /* nop              */
117     0x60000000, /* nop              */
118     0x60000000, /* nop              */
119     0x60000000, /* nop              */
120   };
121
122 /* For executables, we have some additional relocations in
123    .rela.plt.unloaded, for the kernel loader.  */
124
125 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
126 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
127 /* The number of relocations in the PLTResolve slot. */
128 #define VXWORKS_PLTRESOLVE_RELOCS 2
129 /* The number of relocations in the PLTResolve slot when when creating
130    a shared library. */
131 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
132
133 /* Some instructions.  */
134 #define ADDIS_11_11     0x3d6b0000
135 #define ADDIS_11_30     0x3d7e0000
136 #define ADDIS_12_12     0x3d8c0000
137 #define ADDI_11_11      0x396b0000
138 #define ADD_0_11_11     0x7c0b5a14
139 #define ADD_3_12_2      0x7c6c1214
140 #define ADD_11_0_11     0x7d605a14
141 #define B               0x48000000
142 #define BCL_20_31       0x429f0005
143 #define BCTR            0x4e800420
144 #define BEQLR           0x4d820020
145 #define CMPWI_11_0      0x2c0b0000
146 #define LIS_11          0x3d600000
147 #define LIS_12          0x3d800000
148 #define LWZU_0_12       0x840c0000
149 #define LWZ_0_12        0x800c0000
150 #define LWZ_11_3        0x81630000
151 #define LWZ_11_11       0x816b0000
152 #define LWZ_11_30       0x817e0000
153 #define LWZ_12_3        0x81830000
154 #define LWZ_12_12       0x818c0000
155 #define MR_0_3          0x7c601b78
156 #define MR_3_0          0x7c030378
157 #define MFLR_0          0x7c0802a6
158 #define MFLR_12         0x7d8802a6
159 #define MTCTR_0         0x7c0903a6
160 #define MTCTR_11        0x7d6903a6
161 #define MTLR_0          0x7c0803a6
162 #define NOP             0x60000000
163 #define SUB_11_11_12    0x7d6c5850
164
165 /* Offset of tp and dtp pointers from start of TLS block.  */
166 #define TP_OFFSET       0x7000
167 #define DTP_OFFSET      0x8000
168
169 /* The value of a defined global symbol.  */
170 #define SYM_VAL(SYM) \
171   ((SYM)->root.u.def.section->output_section->vma       \
172    + (SYM)->root.u.def.section->output_offset           \
173    + (SYM)->root.u.def.value)
174 \f
175 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
176
177 static reloc_howto_type ppc_elf_howto_raw[] = {
178   /* This reloc does nothing.  */
179   HOWTO (R_PPC_NONE,            /* type */
180          0,                     /* rightshift */
181          2,                     /* size (0 = byte, 1 = short, 2 = long) */
182          32,                    /* bitsize */
183          FALSE,                 /* pc_relative */
184          0,                     /* bitpos */
185          complain_overflow_bitfield, /* complain_on_overflow */
186          bfd_elf_generic_reloc, /* special_function */
187          "R_PPC_NONE",          /* name */
188          FALSE,                 /* partial_inplace */
189          0,                     /* src_mask */
190          0,                     /* dst_mask */
191          FALSE),                /* pcrel_offset */
192
193   /* A standard 32 bit relocation.  */
194   HOWTO (R_PPC_ADDR32,          /* type */
195          0,                     /* rightshift */
196          2,                     /* size (0 = byte, 1 = short, 2 = long) */
197          32,                    /* bitsize */
198          FALSE,                 /* pc_relative */
199          0,                     /* bitpos */
200          complain_overflow_bitfield, /* complain_on_overflow */
201          bfd_elf_generic_reloc, /* special_function */
202          "R_PPC_ADDR32",        /* name */
203          FALSE,                 /* partial_inplace */
204          0,                     /* src_mask */
205          0xffffffff,            /* dst_mask */
206          FALSE),                /* pcrel_offset */
207
208   /* An absolute 26 bit branch; the lower two bits must be zero.
209      FIXME: we don't check that, we just clear them.  */
210   HOWTO (R_PPC_ADDR24,          /* type */
211          0,                     /* rightshift */
212          2,                     /* size (0 = byte, 1 = short, 2 = long) */
213          26,                    /* bitsize */
214          FALSE,                 /* pc_relative */
215          0,                     /* bitpos */
216          complain_overflow_bitfield, /* complain_on_overflow */
217          bfd_elf_generic_reloc, /* special_function */
218          "R_PPC_ADDR24",        /* name */
219          FALSE,                 /* partial_inplace */
220          0,                     /* src_mask */
221          0x3fffffc,             /* dst_mask */
222          FALSE),                /* pcrel_offset */
223
224   /* A standard 16 bit relocation.  */
225   HOWTO (R_PPC_ADDR16,          /* type */
226          0,                     /* rightshift */
227          1,                     /* size (0 = byte, 1 = short, 2 = long) */
228          16,                    /* bitsize */
229          FALSE,                 /* pc_relative */
230          0,                     /* bitpos */
231          complain_overflow_bitfield, /* complain_on_overflow */
232          bfd_elf_generic_reloc, /* special_function */
233          "R_PPC_ADDR16",        /* name */
234          FALSE,                 /* partial_inplace */
235          0,                     /* src_mask */
236          0xffff,                /* dst_mask */
237          FALSE),                /* pcrel_offset */
238
239   /* A 16 bit relocation without overflow.  */
240   HOWTO (R_PPC_ADDR16_LO,       /* type */
241          0,                     /* rightshift */
242          1,                     /* size (0 = byte, 1 = short, 2 = long) */
243          16,                    /* bitsize */
244          FALSE,                 /* pc_relative */
245          0,                     /* bitpos */
246          complain_overflow_dont,/* complain_on_overflow */
247          bfd_elf_generic_reloc, /* special_function */
248          "R_PPC_ADDR16_LO",     /* name */
249          FALSE,                 /* partial_inplace */
250          0,                     /* src_mask */
251          0xffff,                /* dst_mask */
252          FALSE),                /* pcrel_offset */
253
254   /* The high order 16 bits of an address.  */
255   HOWTO (R_PPC_ADDR16_HI,       /* type */
256          16,                    /* rightshift */
257          1,                     /* size (0 = byte, 1 = short, 2 = long) */
258          16,                    /* bitsize */
259          FALSE,                 /* pc_relative */
260          0,                     /* bitpos */
261          complain_overflow_dont, /* complain_on_overflow */
262          bfd_elf_generic_reloc, /* special_function */
263          "R_PPC_ADDR16_HI",     /* name */
264          FALSE,                 /* partial_inplace */
265          0,                     /* src_mask */
266          0xffff,                /* dst_mask */
267          FALSE),                /* pcrel_offset */
268
269   /* The high order 16 bits of an address, plus 1 if the contents of
270      the low 16 bits, treated as a signed number, is negative.  */
271   HOWTO (R_PPC_ADDR16_HA,       /* type */
272          16,                    /* rightshift */
273          1,                     /* size (0 = byte, 1 = short, 2 = long) */
274          16,                    /* bitsize */
275          FALSE,                 /* pc_relative */
276          0,                     /* bitpos */
277          complain_overflow_dont, /* complain_on_overflow */
278          ppc_elf_addr16_ha_reloc, /* special_function */
279          "R_PPC_ADDR16_HA",     /* name */
280          FALSE,                 /* partial_inplace */
281          0,                     /* src_mask */
282          0xffff,                /* dst_mask */
283          FALSE),                /* pcrel_offset */
284
285   /* An absolute 16 bit branch; the lower two bits must be zero.
286      FIXME: we don't check that, we just clear them.  */
287   HOWTO (R_PPC_ADDR14,          /* type */
288          0,                     /* rightshift */
289          2,                     /* size (0 = byte, 1 = short, 2 = long) */
290          16,                    /* bitsize */
291          FALSE,                 /* pc_relative */
292          0,                     /* bitpos */
293          complain_overflow_bitfield, /* complain_on_overflow */
294          bfd_elf_generic_reloc, /* special_function */
295          "R_PPC_ADDR14",        /* name */
296          FALSE,                 /* partial_inplace */
297          0,                     /* src_mask */
298          0xfffc,                /* dst_mask */
299          FALSE),                /* pcrel_offset */
300
301   /* An absolute 16 bit branch, for which bit 10 should be set to
302      indicate that the branch is expected to be taken.  The lower two
303      bits must be zero.  */
304   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* type */
305          0,                     /* rightshift */
306          2,                     /* size (0 = byte, 1 = short, 2 = long) */
307          16,                    /* bitsize */
308          FALSE,                 /* pc_relative */
309          0,                     /* bitpos */
310          complain_overflow_bitfield, /* complain_on_overflow */
311          bfd_elf_generic_reloc, /* special_function */
312          "R_PPC_ADDR14_BRTAKEN",/* name */
313          FALSE,                 /* partial_inplace */
314          0,                     /* src_mask */
315          0xfffc,                /* dst_mask */
316          FALSE),                /* pcrel_offset */
317
318   /* An absolute 16 bit branch, for which bit 10 should be set to
319      indicate that the branch is not expected to be taken.  The lower
320      two bits must be zero.  */
321   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
322          0,                     /* rightshift */
323          2,                     /* size (0 = byte, 1 = short, 2 = long) */
324          16,                    /* bitsize */
325          FALSE,                 /* pc_relative */
326          0,                     /* bitpos */
327          complain_overflow_bitfield, /* complain_on_overflow */
328          bfd_elf_generic_reloc, /* special_function */
329          "R_PPC_ADDR14_BRNTAKEN",/* name */
330          FALSE,                 /* partial_inplace */
331          0,                     /* src_mask */
332          0xfffc,                /* dst_mask */
333          FALSE),                /* pcrel_offset */
334
335   /* A relative 26 bit branch; the lower two bits must be zero.  */
336   HOWTO (R_PPC_REL24,           /* type */
337          0,                     /* rightshift */
338          2,                     /* size (0 = byte, 1 = short, 2 = long) */
339          26,                    /* bitsize */
340          TRUE,                  /* pc_relative */
341          0,                     /* bitpos */
342          complain_overflow_signed, /* complain_on_overflow */
343          bfd_elf_generic_reloc, /* special_function */
344          "R_PPC_REL24",         /* name */
345          FALSE,                 /* partial_inplace */
346          0,                     /* src_mask */
347          0x3fffffc,             /* dst_mask */
348          TRUE),                 /* pcrel_offset */
349
350   /* A relative 16 bit branch; the lower two bits must be zero.  */
351   HOWTO (R_PPC_REL14,           /* type */
352          0,                     /* rightshift */
353          2,                     /* size (0 = byte, 1 = short, 2 = long) */
354          16,                    /* bitsize */
355          TRUE,                  /* pc_relative */
356          0,                     /* bitpos */
357          complain_overflow_signed, /* complain_on_overflow */
358          bfd_elf_generic_reloc, /* special_function */
359          "R_PPC_REL14",         /* name */
360          FALSE,                 /* partial_inplace */
361          0,                     /* src_mask */
362          0xfffc,                /* dst_mask */
363          TRUE),                 /* pcrel_offset */
364
365   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
366      the branch is expected to be taken.  The lower two bits must be
367      zero.  */
368   HOWTO (R_PPC_REL14_BRTAKEN,   /* type */
369          0,                     /* rightshift */
370          2,                     /* size (0 = byte, 1 = short, 2 = long) */
371          16,                    /* bitsize */
372          TRUE,                  /* pc_relative */
373          0,                     /* bitpos */
374          complain_overflow_signed, /* complain_on_overflow */
375          bfd_elf_generic_reloc, /* special_function */
376          "R_PPC_REL14_BRTAKEN", /* name */
377          FALSE,                 /* partial_inplace */
378          0,                     /* src_mask */
379          0xfffc,                /* dst_mask */
380          TRUE),                 /* pcrel_offset */
381
382   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
383      the branch is not expected to be taken.  The lower two bits must
384      be zero.  */
385   HOWTO (R_PPC_REL14_BRNTAKEN,  /* type */
386          0,                     /* rightshift */
387          2,                     /* size (0 = byte, 1 = short, 2 = long) */
388          16,                    /* bitsize */
389          TRUE,                  /* pc_relative */
390          0,                     /* bitpos */
391          complain_overflow_signed, /* complain_on_overflow */
392          bfd_elf_generic_reloc, /* special_function */
393          "R_PPC_REL14_BRNTAKEN",/* name */
394          FALSE,                 /* partial_inplace */
395          0,                     /* src_mask */
396          0xfffc,                /* dst_mask */
397          TRUE),                 /* pcrel_offset */
398
399   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
400      symbol.  */
401   HOWTO (R_PPC_GOT16,           /* type */
402          0,                     /* rightshift */
403          1,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          FALSE,                 /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          bfd_elf_generic_reloc, /* special_function */
409          "R_PPC_GOT16",         /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0xffff,                /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
416      the symbol.  */
417   HOWTO (R_PPC_GOT16_LO,        /* type */
418          0,                     /* rightshift */
419          1,                     /* size (0 = byte, 1 = short, 2 = long) */
420          16,                    /* bitsize */
421          FALSE,                 /* pc_relative */
422          0,                     /* bitpos */
423          complain_overflow_dont, /* complain_on_overflow */
424          bfd_elf_generic_reloc, /* special_function */
425          "R_PPC_GOT16_LO",      /* name */
426          FALSE,                 /* partial_inplace */
427          0,                     /* src_mask */
428          0xffff,                /* dst_mask */
429          FALSE),                /* pcrel_offset */
430
431   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
432      the symbol.  */
433   HOWTO (R_PPC_GOT16_HI,        /* type */
434          16,                    /* rightshift */
435          1,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          FALSE,                 /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_bitfield, /* complain_on_overflow */
440          bfd_elf_generic_reloc, /* special_function */
441          "R_PPC_GOT16_HI",      /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0xffff,                /* dst_mask */
445          FALSE),                 /* pcrel_offset */
446
447   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
448      the symbol.  */
449   HOWTO (R_PPC_GOT16_HA,        /* type */
450          16,                    /* rightshift */
451          1,                     /* size (0 = byte, 1 = short, 2 = long) */
452          16,                    /* bitsize */
453          FALSE,                 /* pc_relative */
454          0,                     /* bitpos */
455          complain_overflow_bitfield, /* complain_on_overflow */
456          ppc_elf_addr16_ha_reloc, /* special_function */
457          "R_PPC_GOT16_HA",      /* name */
458          FALSE,                 /* partial_inplace */
459          0,                     /* src_mask */
460          0xffff,                /* dst_mask */
461          FALSE),                /* pcrel_offset */
462
463   /* Like R_PPC_REL24, but referring to the procedure linkage table
464      entry for the symbol.  */
465   HOWTO (R_PPC_PLTREL24,        /* type */
466          0,                     /* rightshift */
467          2,                     /* size (0 = byte, 1 = short, 2 = long) */
468          26,                    /* bitsize */
469          TRUE,                  /* pc_relative */
470          0,                     /* bitpos */
471          complain_overflow_signed,  /* complain_on_overflow */
472          bfd_elf_generic_reloc, /* special_function */
473          "R_PPC_PLTREL24",      /* name */
474          FALSE,                 /* partial_inplace */
475          0,                     /* src_mask */
476          0x3fffffc,             /* dst_mask */
477          TRUE),                 /* pcrel_offset */
478
479   /* This is used only by the dynamic linker.  The symbol should exist
480      both in the object being run and in some shared library.  The
481      dynamic linker copies the data addressed by the symbol from the
482      shared library into the object, because the object being
483      run has to have the data at some particular address.  */
484   HOWTO (R_PPC_COPY,            /* type */
485          0,                     /* rightshift */
486          2,                     /* size (0 = byte, 1 = short, 2 = long) */
487          32,                    /* bitsize */
488          FALSE,                 /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_bitfield, /* complain_on_overflow */
491          bfd_elf_generic_reloc,  /* special_function */
492          "R_PPC_COPY",          /* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0,                     /* dst_mask */
496          FALSE),                /* pcrel_offset */
497
498   /* Like R_PPC_ADDR32, but used when setting global offset table
499      entries.  */
500   HOWTO (R_PPC_GLOB_DAT,        /* type */
501          0,                     /* rightshift */
502          2,                     /* size (0 = byte, 1 = short, 2 = long) */
503          32,                    /* bitsize */
504          FALSE,                 /* pc_relative */
505          0,                     /* bitpos */
506          complain_overflow_bitfield, /* complain_on_overflow */
507          bfd_elf_generic_reloc,  /* special_function */
508          "R_PPC_GLOB_DAT",      /* name */
509          FALSE,                 /* partial_inplace */
510          0,                     /* src_mask */
511          0xffffffff,            /* dst_mask */
512          FALSE),                /* pcrel_offset */
513
514   /* Marks a procedure linkage table entry for a symbol.  */
515   HOWTO (R_PPC_JMP_SLOT,        /* type */
516          0,                     /* rightshift */
517          2,                     /* size (0 = byte, 1 = short, 2 = long) */
518          32,                    /* bitsize */
519          FALSE,                 /* pc_relative */
520          0,                     /* bitpos */
521          complain_overflow_bitfield, /* complain_on_overflow */
522          bfd_elf_generic_reloc,  /* special_function */
523          "R_PPC_JMP_SLOT",      /* name */
524          FALSE,                 /* partial_inplace */
525          0,                     /* src_mask */
526          0,                     /* dst_mask */
527          FALSE),                /* pcrel_offset */
528
529   /* Used only by the dynamic linker.  When the object is run, this
530      longword is set to the load address of the object, plus the
531      addend.  */
532   HOWTO (R_PPC_RELATIVE,        /* 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_RELATIVE",      /* name */
541          FALSE,                 /* partial_inplace */
542          0,                     /* src_mask */
543          0xffffffff,            /* dst_mask */
544          FALSE),                /* pcrel_offset */
545
546   /* Like R_PPC_REL24, but uses the value of the symbol within the
547      object rather than the final value.  Normally used for
548      _GLOBAL_OFFSET_TABLE_.  */
549   HOWTO (R_PPC_LOCAL24PC,       /* type */
550          0,                     /* rightshift */
551          2,                     /* size (0 = byte, 1 = short, 2 = long) */
552          26,                    /* bitsize */
553          TRUE,                  /* pc_relative */
554          0,                     /* bitpos */
555          complain_overflow_signed, /* complain_on_overflow */
556          bfd_elf_generic_reloc, /* special_function */
557          "R_PPC_LOCAL24PC",     /* name */
558          FALSE,                 /* partial_inplace */
559          0,                     /* src_mask */
560          0x3fffffc,             /* dst_mask */
561          TRUE),                 /* pcrel_offset */
562
563   /* Like R_PPC_ADDR32, but may be unaligned.  */
564   HOWTO (R_PPC_UADDR32,         /* type */
565          0,                     /* rightshift */
566          2,                     /* size (0 = byte, 1 = short, 2 = long) */
567          32,                    /* bitsize */
568          FALSE,                 /* pc_relative */
569          0,                     /* bitpos */
570          complain_overflow_bitfield, /* complain_on_overflow */
571          bfd_elf_generic_reloc, /* special_function */
572          "R_PPC_UADDR32",       /* name */
573          FALSE,                 /* partial_inplace */
574          0,                     /* src_mask */
575          0xffffffff,            /* dst_mask */
576          FALSE),                /* pcrel_offset */
577
578   /* Like R_PPC_ADDR16, but may be unaligned.  */
579   HOWTO (R_PPC_UADDR16,         /* type */
580          0,                     /* rightshift */
581          1,                     /* size (0 = byte, 1 = short, 2 = long) */
582          16,                    /* bitsize */
583          FALSE,                 /* pc_relative */
584          0,                     /* bitpos */
585          complain_overflow_bitfield, /* complain_on_overflow */
586          bfd_elf_generic_reloc, /* special_function */
587          "R_PPC_UADDR16",       /* name */
588          FALSE,                 /* partial_inplace */
589          0,                     /* src_mask */
590          0xffff,                /* dst_mask */
591          FALSE),                /* pcrel_offset */
592
593   /* 32-bit PC relative */
594   HOWTO (R_PPC_REL32,           /* type */
595          0,                     /* rightshift */
596          2,                     /* size (0 = byte, 1 = short, 2 = long) */
597          32,                    /* bitsize */
598          TRUE,                  /* pc_relative */
599          0,                     /* bitpos */
600          complain_overflow_bitfield, /* complain_on_overflow */
601          bfd_elf_generic_reloc, /* special_function */
602          "R_PPC_REL32",         /* name */
603          FALSE,                 /* partial_inplace */
604          0,                     /* src_mask */
605          0xffffffff,            /* dst_mask */
606          TRUE),                 /* pcrel_offset */
607
608   /* 32-bit relocation to the symbol's procedure linkage table.
609      FIXME: not supported.  */
610   HOWTO (R_PPC_PLT32,           /* type */
611          0,                     /* rightshift */
612          2,                     /* size (0 = byte, 1 = short, 2 = long) */
613          32,                    /* bitsize */
614          FALSE,                 /* pc_relative */
615          0,                     /* bitpos */
616          complain_overflow_bitfield, /* complain_on_overflow */
617          bfd_elf_generic_reloc, /* special_function */
618          "R_PPC_PLT32",         /* name */
619          FALSE,                 /* partial_inplace */
620          0,                     /* src_mask */
621          0,                     /* dst_mask */
622          FALSE),                /* pcrel_offset */
623
624   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
625      FIXME: not supported.  */
626   HOWTO (R_PPC_PLTREL32,        /* type */
627          0,                     /* rightshift */
628          2,                     /* size (0 = byte, 1 = short, 2 = long) */
629          32,                    /* bitsize */
630          TRUE,                  /* pc_relative */
631          0,                     /* bitpos */
632          complain_overflow_bitfield, /* complain_on_overflow */
633          bfd_elf_generic_reloc, /* special_function */
634          "R_PPC_PLTREL32",      /* name */
635          FALSE,                 /* partial_inplace */
636          0,                     /* src_mask */
637          0,                     /* dst_mask */
638          TRUE),                 /* pcrel_offset */
639
640   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
641      the symbol.  */
642   HOWTO (R_PPC_PLT16_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_PLT16_LO",      /* name */
651          FALSE,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0xffff,                /* dst_mask */
654          FALSE),                /* pcrel_offset */
655
656   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
657      the symbol.  */
658   HOWTO (R_PPC_PLT16_HI,        /* type */
659          16,                    /* rightshift */
660          1,                     /* size (0 = byte, 1 = short, 2 = long) */
661          16,                    /* bitsize */
662          FALSE,                 /* pc_relative */
663          0,                     /* bitpos */
664          complain_overflow_bitfield, /* complain_on_overflow */
665          bfd_elf_generic_reloc, /* special_function */
666          "R_PPC_PLT16_HI",      /* name */
667          FALSE,                 /* partial_inplace */
668          0,                     /* src_mask */
669          0xffff,                /* dst_mask */
670          FALSE),                 /* pcrel_offset */
671
672   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
673      the symbol.  */
674   HOWTO (R_PPC_PLT16_HA,        /* type */
675          16,                    /* rightshift */
676          1,                     /* size (0 = byte, 1 = short, 2 = long) */
677          16,                    /* bitsize */
678          FALSE,                 /* pc_relative */
679          0,                     /* bitpos */
680          complain_overflow_bitfield, /* complain_on_overflow */
681          ppc_elf_addr16_ha_reloc, /* special_function */
682          "R_PPC_PLT16_HA",      /* name */
683          FALSE,                 /* partial_inplace */
684          0,                     /* src_mask */
685          0xffff,                /* dst_mask */
686          FALSE),                /* pcrel_offset */
687
688   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
689      small data items.  */
690   HOWTO (R_PPC_SDAREL16,        /* type */
691          0,                     /* rightshift */
692          1,                     /* size (0 = byte, 1 = short, 2 = long) */
693          16,                    /* bitsize */
694          FALSE,                 /* pc_relative */
695          0,                     /* bitpos */
696          complain_overflow_signed, /* complain_on_overflow */
697          bfd_elf_generic_reloc, /* special_function */
698          "R_PPC_SDAREL16",      /* name */
699          FALSE,                 /* partial_inplace */
700          0,                     /* src_mask */
701          0xffff,                /* dst_mask */
702          FALSE),                /* pcrel_offset */
703
704   /* 16-bit section relative relocation.  */
705   HOWTO (R_PPC_SECTOFF,         /* type */
706          0,                     /* rightshift */
707          1,                     /* size (0 = byte, 1 = short, 2 = long) */
708          16,                    /* bitsize */
709          FALSE,                 /* pc_relative */
710          0,                     /* bitpos */
711          complain_overflow_bitfield, /* complain_on_overflow */
712          bfd_elf_generic_reloc, /* special_function */
713          "R_PPC_SECTOFF",       /* name */
714          FALSE,                 /* partial_inplace */
715          0,                     /* src_mask */
716          0xffff,                /* dst_mask */
717          FALSE),                /* pcrel_offset */
718
719   /* 16-bit lower half section relative relocation.  */
720   HOWTO (R_PPC_SECTOFF_LO,        /* type */
721          0,                     /* rightshift */
722          1,                     /* size (0 = byte, 1 = short, 2 = long) */
723          16,                    /* bitsize */
724          FALSE,                 /* pc_relative */
725          0,                     /* bitpos */
726          complain_overflow_dont, /* complain_on_overflow */
727          bfd_elf_generic_reloc, /* special_function */
728          "R_PPC_SECTOFF_LO",    /* name */
729          FALSE,                 /* partial_inplace */
730          0,                     /* src_mask */
731          0xffff,                /* dst_mask */
732          FALSE),                /* pcrel_offset */
733
734   /* 16-bit upper half section relative relocation.  */
735   HOWTO (R_PPC_SECTOFF_HI,      /* type */
736          16,                    /* rightshift */
737          1,                     /* size (0 = byte, 1 = short, 2 = long) */
738          16,                    /* bitsize */
739          FALSE,                 /* pc_relative */
740          0,                     /* bitpos */
741          complain_overflow_bitfield, /* complain_on_overflow */
742          bfd_elf_generic_reloc, /* special_function */
743          "R_PPC_SECTOFF_HI",    /* name */
744          FALSE,                 /* partial_inplace */
745          0,                     /* src_mask */
746          0xffff,                /* dst_mask */
747          FALSE),                 /* pcrel_offset */
748
749   /* 16-bit upper half adjusted section relative relocation.  */
750   HOWTO (R_PPC_SECTOFF_HA,      /* type */
751          16,                    /* rightshift */
752          1,                     /* size (0 = byte, 1 = short, 2 = long) */
753          16,                    /* bitsize */
754          FALSE,                 /* pc_relative */
755          0,                     /* bitpos */
756          complain_overflow_bitfield, /* complain_on_overflow */
757          ppc_elf_addr16_ha_reloc, /* special_function */
758          "R_PPC_SECTOFF_HA",    /* name */
759          FALSE,                 /* partial_inplace */
760          0,                     /* src_mask */
761          0xffff,                /* dst_mask */
762          FALSE),                /* pcrel_offset */
763
764   /* Marker relocs for TLS.  */
765   HOWTO (R_PPC_TLS,
766          0,                     /* rightshift */
767          2,                     /* size (0 = byte, 1 = short, 2 = long) */
768          32,                    /* bitsize */
769          FALSE,                 /* pc_relative */
770          0,                     /* bitpos */
771          complain_overflow_dont, /* complain_on_overflow */
772          bfd_elf_generic_reloc, /* special_function */
773          "R_PPC_TLS",           /* name */
774          FALSE,                 /* partial_inplace */
775          0,                     /* src_mask */
776          0,                     /* dst_mask */
777          FALSE),                /* pcrel_offset */
778
779   HOWTO (R_PPC_TLSGD,
780          0,                     /* rightshift */
781          2,                     /* size (0 = byte, 1 = short, 2 = long) */
782          32,                    /* bitsize */
783          FALSE,                 /* pc_relative */
784          0,                     /* bitpos */
785          complain_overflow_dont, /* complain_on_overflow */
786          bfd_elf_generic_reloc, /* special_function */
787          "R_PPC_TLSGD",         /* name */
788          FALSE,                 /* partial_inplace */
789          0,                     /* src_mask */
790          0,                     /* dst_mask */
791          FALSE),                /* pcrel_offset */
792
793   HOWTO (R_PPC_TLSLD,
794          0,                     /* rightshift */
795          2,                     /* size (0 = byte, 1 = short, 2 = long) */
796          32,                    /* bitsize */
797          FALSE,                 /* pc_relative */
798          0,                     /* bitpos */
799          complain_overflow_dont, /* complain_on_overflow */
800          bfd_elf_generic_reloc, /* special_function */
801          "R_PPC_TLSLD",         /* name */
802          FALSE,                 /* partial_inplace */
803          0,                     /* src_mask */
804          0,                     /* dst_mask */
805          FALSE),                /* pcrel_offset */
806
807   /* Computes the load module index of the load module that contains the
808      definition of its TLS sym.  */
809   HOWTO (R_PPC_DTPMOD32,
810          0,                     /* rightshift */
811          2,                     /* size (0 = byte, 1 = short, 2 = long) */
812          32,                    /* bitsize */
813          FALSE,                 /* pc_relative */
814          0,                     /* bitpos */
815          complain_overflow_dont, /* complain_on_overflow */
816          ppc_elf_unhandled_reloc, /* special_function */
817          "R_PPC_DTPMOD32",      /* name */
818          FALSE,                 /* partial_inplace */
819          0,                     /* src_mask */
820          0xffffffff,            /* dst_mask */
821          FALSE),                /* pcrel_offset */
822
823   /* Computes a dtv-relative displacement, the difference between the value
824      of sym+add and the base address of the thread-local storage block that
825      contains the definition of sym, minus 0x8000.  */
826   HOWTO (R_PPC_DTPREL32,
827          0,                     /* rightshift */
828          2,                     /* size (0 = byte, 1 = short, 2 = long) */
829          32,                    /* 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_DTPREL32",      /* name */
835          FALSE,                 /* partial_inplace */
836          0,                     /* src_mask */
837          0xffffffff,            /* dst_mask */
838          FALSE),                /* pcrel_offset */
839
840   /* A 16 bit dtprel reloc.  */
841   HOWTO (R_PPC_DTPREL16,
842          0,                     /* rightshift */
843          1,                     /* size (0 = byte, 1 = short, 2 = long) */
844          16,                    /* bitsize */
845          FALSE,                 /* pc_relative */
846          0,                     /* bitpos */
847          complain_overflow_signed, /* complain_on_overflow */
848          ppc_elf_unhandled_reloc, /* special_function */
849          "R_PPC_DTPREL16",      /* name */
850          FALSE,                 /* partial_inplace */
851          0,                     /* src_mask */
852          0xffff,                /* dst_mask */
853          FALSE),                /* pcrel_offset */
854
855   /* Like DTPREL16, but no overflow.  */
856   HOWTO (R_PPC_DTPREL16_LO,
857          0,                     /* 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_DTPREL16_LO",   /* name */
865          FALSE,                 /* partial_inplace */
866          0,                     /* src_mask */
867          0xffff,                /* dst_mask */
868          FALSE),                /* pcrel_offset */
869
870   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
871   HOWTO (R_PPC_DTPREL16_HI,
872          16,                    /* rightshift */
873          1,                     /* size (0 = byte, 1 = short, 2 = long) */
874          16,                    /* bitsize */
875          FALSE,                 /* pc_relative */
876          0,                     /* bitpos */
877          complain_overflow_dont, /* complain_on_overflow */
878          ppc_elf_unhandled_reloc, /* special_function */
879          "R_PPC_DTPREL16_HI",   /* name */
880          FALSE,                 /* partial_inplace */
881          0,                     /* src_mask */
882          0xffff,                /* dst_mask */
883          FALSE),                /* pcrel_offset */
884
885   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
886   HOWTO (R_PPC_DTPREL16_HA,
887          16,                    /* rightshift */
888          1,                     /* size (0 = byte, 1 = short, 2 = long) */
889          16,                    /* bitsize */
890          FALSE,                 /* pc_relative */
891          0,                     /* bitpos */
892          complain_overflow_dont, /* complain_on_overflow */
893          ppc_elf_unhandled_reloc, /* special_function */
894          "R_PPC_DTPREL16_HA",   /* name */
895          FALSE,                 /* partial_inplace */
896          0,                     /* src_mask */
897          0xffff,                /* dst_mask */
898          FALSE),                /* pcrel_offset */
899
900   /* Computes a tp-relative displacement, the difference between the value of
901      sym+add and the value of the thread pointer (r13).  */
902   HOWTO (R_PPC_TPREL32,
903          0,                     /* rightshift */
904          2,                     /* size (0 = byte, 1 = short, 2 = long) */
905          32,                    /* bitsize */
906          FALSE,                 /* pc_relative */
907          0,                     /* bitpos */
908          complain_overflow_dont, /* complain_on_overflow */
909          ppc_elf_unhandled_reloc, /* special_function */
910          "R_PPC_TPREL32",       /* name */
911          FALSE,                 /* partial_inplace */
912          0,                     /* src_mask */
913          0xffffffff,            /* dst_mask */
914          FALSE),                /* pcrel_offset */
915
916   /* A 16 bit tprel reloc.  */
917   HOWTO (R_PPC_TPREL16,
918          0,                     /* rightshift */
919          1,                     /* size (0 = byte, 1 = short, 2 = long) */
920          16,                    /* bitsize */
921          FALSE,                 /* pc_relative */
922          0,                     /* bitpos */
923          complain_overflow_signed, /* complain_on_overflow */
924          ppc_elf_unhandled_reloc, /* special_function */
925          "R_PPC_TPREL16",       /* name */
926          FALSE,                 /* partial_inplace */
927          0,                     /* src_mask */
928          0xffff,                /* dst_mask */
929          FALSE),                /* pcrel_offset */
930
931   /* Like TPREL16, but no overflow.  */
932   HOWTO (R_PPC_TPREL16_LO,
933          0,                     /* rightshift */
934          1,                     /* size (0 = byte, 1 = short, 2 = long) */
935          16,                    /* bitsize */
936          FALSE,                 /* pc_relative */
937          0,                     /* bitpos */
938          complain_overflow_dont, /* complain_on_overflow */
939          ppc_elf_unhandled_reloc, /* special_function */
940          "R_PPC_TPREL16_LO",    /* name */
941          FALSE,                 /* partial_inplace */
942          0,                     /* src_mask */
943          0xffff,                /* dst_mask */
944          FALSE),                /* pcrel_offset */
945
946   /* Like TPREL16_LO, but next higher group of 16 bits.  */
947   HOWTO (R_PPC_TPREL16_HI,
948          16,                    /* rightshift */
949          1,                     /* size (0 = byte, 1 = short, 2 = long) */
950          16,                    /* bitsize */
951          FALSE,                 /* pc_relative */
952          0,                     /* bitpos */
953          complain_overflow_dont, /* complain_on_overflow */
954          ppc_elf_unhandled_reloc, /* special_function */
955          "R_PPC_TPREL16_HI",    /* name */
956          FALSE,                 /* partial_inplace */
957          0,                     /* src_mask */
958          0xffff,                /* dst_mask */
959          FALSE),                /* pcrel_offset */
960
961   /* Like TPREL16_HI, but adjust for low 16 bits.  */
962   HOWTO (R_PPC_TPREL16_HA,
963          16,                    /* rightshift */
964          1,                     /* size (0 = byte, 1 = short, 2 = long) */
965          16,                    /* bitsize */
966          FALSE,                 /* pc_relative */
967          0,                     /* bitpos */
968          complain_overflow_dont, /* complain_on_overflow */
969          ppc_elf_unhandled_reloc, /* special_function */
970          "R_PPC_TPREL16_HA",    /* name */
971          FALSE,                 /* partial_inplace */
972          0,                     /* src_mask */
973          0xffff,                /* dst_mask */
974          FALSE),                /* pcrel_offset */
975
976   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
977      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
978      to the first entry.  */
979   HOWTO (R_PPC_GOT_TLSGD16,
980          0,                     /* rightshift */
981          1,                     /* size (0 = byte, 1 = short, 2 = long) */
982          16,                    /* bitsize */
983          FALSE,                 /* pc_relative */
984          0,                     /* bitpos */
985          complain_overflow_signed, /* complain_on_overflow */
986          ppc_elf_unhandled_reloc, /* special_function */
987          "R_PPC_GOT_TLSGD16",   /* name */
988          FALSE,                 /* partial_inplace */
989          0,                     /* src_mask */
990          0xffff,                /* dst_mask */
991          FALSE),                /* pcrel_offset */
992
993   /* Like GOT_TLSGD16, but no overflow.  */
994   HOWTO (R_PPC_GOT_TLSGD16_LO,
995          0,                     /* rightshift */
996          1,                     /* size (0 = byte, 1 = short, 2 = long) */
997          16,                    /* bitsize */
998          FALSE,                 /* pc_relative */
999          0,                     /* bitpos */
1000          complain_overflow_dont, /* complain_on_overflow */
1001          ppc_elf_unhandled_reloc, /* special_function */
1002          "R_PPC_GOT_TLSGD16_LO", /* name */
1003          FALSE,                 /* partial_inplace */
1004          0,                     /* src_mask */
1005          0xffff,                /* dst_mask */
1006          FALSE),                /* pcrel_offset */
1007
1008   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1009   HOWTO (R_PPC_GOT_TLSGD16_HI,
1010          16,                    /* rightshift */
1011          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1012          16,                    /* bitsize */
1013          FALSE,                 /* pc_relative */
1014          0,                     /* bitpos */
1015          complain_overflow_dont, /* complain_on_overflow */
1016          ppc_elf_unhandled_reloc, /* special_function */
1017          "R_PPC_GOT_TLSGD16_HI", /* name */
1018          FALSE,                 /* partial_inplace */
1019          0,                     /* src_mask */
1020          0xffff,                /* dst_mask */
1021          FALSE),                /* pcrel_offset */
1022
1023   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1024   HOWTO (R_PPC_GOT_TLSGD16_HA,
1025          16,                    /* rightshift */
1026          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1027          16,                    /* bitsize */
1028          FALSE,                 /* pc_relative */
1029          0,                     /* bitpos */
1030          complain_overflow_dont, /* complain_on_overflow */
1031          ppc_elf_unhandled_reloc, /* special_function */
1032          "R_PPC_GOT_TLSGD16_HA", /* name */
1033          FALSE,                 /* partial_inplace */
1034          0,                     /* src_mask */
1035          0xffff,                /* dst_mask */
1036          FALSE),                /* pcrel_offset */
1037
1038   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1039      with values (sym+add)@dtpmod and zero, and computes the offset to the
1040      first entry.  */
1041   HOWTO (R_PPC_GOT_TLSLD16,
1042          0,                     /* rightshift */
1043          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1044          16,                    /* bitsize */
1045          FALSE,                 /* pc_relative */
1046          0,                     /* bitpos */
1047          complain_overflow_signed, /* complain_on_overflow */
1048          ppc_elf_unhandled_reloc, /* special_function */
1049          "R_PPC_GOT_TLSLD16",   /* name */
1050          FALSE,                 /* partial_inplace */
1051          0,                     /* src_mask */
1052          0xffff,                /* dst_mask */
1053          FALSE),                /* pcrel_offset */
1054
1055   /* Like GOT_TLSLD16, but no overflow.  */
1056   HOWTO (R_PPC_GOT_TLSLD16_LO,
1057          0,                     /* rightshift */
1058          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1059          16,                    /* bitsize */
1060          FALSE,                 /* pc_relative */
1061          0,                     /* bitpos */
1062          complain_overflow_dont, /* complain_on_overflow */
1063          ppc_elf_unhandled_reloc, /* special_function */
1064          "R_PPC_GOT_TLSLD16_LO", /* name */
1065          FALSE,                 /* partial_inplace */
1066          0,                     /* src_mask */
1067          0xffff,                /* dst_mask */
1068          FALSE),                /* pcrel_offset */
1069
1070   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1071   HOWTO (R_PPC_GOT_TLSLD16_HI,
1072          16,                    /* rightshift */
1073          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1074          16,                    /* bitsize */
1075          FALSE,                 /* pc_relative */
1076          0,                     /* bitpos */
1077          complain_overflow_dont, /* complain_on_overflow */
1078          ppc_elf_unhandled_reloc, /* special_function */
1079          "R_PPC_GOT_TLSLD16_HI", /* name */
1080          FALSE,                 /* partial_inplace */
1081          0,                     /* src_mask */
1082          0xffff,                /* dst_mask */
1083          FALSE),                /* pcrel_offset */
1084
1085   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1086   HOWTO (R_PPC_GOT_TLSLD16_HA,
1087          16,                    /* rightshift */
1088          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1089          16,                    /* bitsize */
1090          FALSE,                 /* pc_relative */
1091          0,                     /* bitpos */
1092          complain_overflow_dont, /* complain_on_overflow */
1093          ppc_elf_unhandled_reloc, /* special_function */
1094          "R_PPC_GOT_TLSLD16_HA", /* name */
1095          FALSE,                 /* partial_inplace */
1096          0,                     /* src_mask */
1097          0xffff,                /* dst_mask */
1098          FALSE),                /* pcrel_offset */
1099
1100   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1101      the offset to the entry.  */
1102   HOWTO (R_PPC_GOT_DTPREL16,
1103          0,                     /* rightshift */
1104          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1105          16,                    /* bitsize */
1106          FALSE,                 /* pc_relative */
1107          0,                     /* bitpos */
1108          complain_overflow_signed, /* complain_on_overflow */
1109          ppc_elf_unhandled_reloc, /* special_function */
1110          "R_PPC_GOT_DTPREL16",  /* name */
1111          FALSE,                 /* partial_inplace */
1112          0,                     /* src_mask */
1113          0xffff,                /* dst_mask */
1114          FALSE),                /* pcrel_offset */
1115
1116   /* Like GOT_DTPREL16, but no overflow.  */
1117   HOWTO (R_PPC_GOT_DTPREL16_LO,
1118          0,                     /* rightshift */
1119          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1120          16,                    /* bitsize */
1121          FALSE,                 /* pc_relative */
1122          0,                     /* bitpos */
1123          complain_overflow_dont, /* complain_on_overflow */
1124          ppc_elf_unhandled_reloc, /* special_function */
1125          "R_PPC_GOT_DTPREL16_LO", /* name */
1126          FALSE,                 /* partial_inplace */
1127          0,                     /* src_mask */
1128          0xffff,                /* dst_mask */
1129          FALSE),                /* pcrel_offset */
1130
1131   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
1132   HOWTO (R_PPC_GOT_DTPREL16_HI,
1133          16,                    /* rightshift */
1134          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1135          16,                    /* bitsize */
1136          FALSE,                 /* pc_relative */
1137          0,                     /* bitpos */
1138          complain_overflow_dont, /* complain_on_overflow */
1139          ppc_elf_unhandled_reloc, /* special_function */
1140          "R_PPC_GOT_DTPREL16_HI", /* name */
1141          FALSE,                 /* partial_inplace */
1142          0,                     /* src_mask */
1143          0xffff,                /* dst_mask */
1144          FALSE),                /* pcrel_offset */
1145
1146   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1147   HOWTO (R_PPC_GOT_DTPREL16_HA,
1148          16,                    /* rightshift */
1149          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1150          16,                    /* bitsize */
1151          FALSE,                 /* pc_relative */
1152          0,                     /* bitpos */
1153          complain_overflow_dont, /* complain_on_overflow */
1154          ppc_elf_unhandled_reloc, /* special_function */
1155          "R_PPC_GOT_DTPREL16_HA", /* name */
1156          FALSE,                 /* partial_inplace */
1157          0,                     /* src_mask */
1158          0xffff,                /* dst_mask */
1159          FALSE),                /* pcrel_offset */
1160
1161   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1162      offset to the entry.  */
1163   HOWTO (R_PPC_GOT_TPREL16,
1164          0,                     /* rightshift */
1165          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1166          16,                    /* bitsize */
1167          FALSE,                 /* pc_relative */
1168          0,                     /* bitpos */
1169          complain_overflow_signed, /* complain_on_overflow */
1170          ppc_elf_unhandled_reloc, /* special_function */
1171          "R_PPC_GOT_TPREL16",   /* name */
1172          FALSE,                 /* partial_inplace */
1173          0,                     /* src_mask */
1174          0xffff,                /* dst_mask */
1175          FALSE),                /* pcrel_offset */
1176
1177   /* Like GOT_TPREL16, but no overflow.  */
1178   HOWTO (R_PPC_GOT_TPREL16_LO,
1179          0,                     /* rightshift */
1180          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1181          16,                    /* bitsize */
1182          FALSE,                 /* pc_relative */
1183          0,                     /* bitpos */
1184          complain_overflow_dont, /* complain_on_overflow */
1185          ppc_elf_unhandled_reloc, /* special_function */
1186          "R_PPC_GOT_TPREL16_LO", /* name */
1187          FALSE,                 /* partial_inplace */
1188          0,                     /* src_mask */
1189          0xffff,                /* dst_mask */
1190          FALSE),                /* pcrel_offset */
1191
1192   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
1193   HOWTO (R_PPC_GOT_TPREL16_HI,
1194          16,                    /* rightshift */
1195          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1196          16,                    /* bitsize */
1197          FALSE,                 /* pc_relative */
1198          0,                     /* bitpos */
1199          complain_overflow_dont, /* complain_on_overflow */
1200          ppc_elf_unhandled_reloc, /* special_function */
1201          "R_PPC_GOT_TPREL16_HI", /* name */
1202          FALSE,                 /* partial_inplace */
1203          0,                     /* src_mask */
1204          0xffff,                /* dst_mask */
1205          FALSE),                /* pcrel_offset */
1206
1207   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1208   HOWTO (R_PPC_GOT_TPREL16_HA,
1209          16,                    /* rightshift */
1210          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1211          16,                    /* bitsize */
1212          FALSE,                 /* pc_relative */
1213          0,                     /* bitpos */
1214          complain_overflow_dont, /* complain_on_overflow */
1215          ppc_elf_unhandled_reloc, /* special_function */
1216          "R_PPC_GOT_TPREL16_HA", /* name */
1217          FALSE,                 /* partial_inplace */
1218          0,                     /* src_mask */
1219          0xffff,                /* dst_mask */
1220          FALSE),                /* pcrel_offset */
1221
1222   /* The remaining relocs are from the Embedded ELF ABI, and are not
1223      in the SVR4 ELF ABI.  */
1224
1225   /* 32 bit value resulting from the addend minus the symbol.  */
1226   HOWTO (R_PPC_EMB_NADDR32,     /* type */
1227          0,                     /* rightshift */
1228          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1229          32,                    /* bitsize */
1230          FALSE,                 /* pc_relative */
1231          0,                     /* bitpos */
1232          complain_overflow_bitfield, /* complain_on_overflow */
1233          bfd_elf_generic_reloc, /* special_function */
1234          "R_PPC_EMB_NADDR32",   /* name */
1235          FALSE,                 /* partial_inplace */
1236          0,                     /* src_mask */
1237          0xffffffff,            /* dst_mask */
1238          FALSE),                /* pcrel_offset */
1239
1240   /* 16 bit value resulting from the addend minus the symbol.  */
1241   HOWTO (R_PPC_EMB_NADDR16,     /* type */
1242          0,                     /* rightshift */
1243          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1244          16,                    /* bitsize */
1245          FALSE,                 /* pc_relative */
1246          0,                     /* bitpos */
1247          complain_overflow_bitfield, /* complain_on_overflow */
1248          bfd_elf_generic_reloc, /* special_function */
1249          "R_PPC_EMB_NADDR16",   /* name */
1250          FALSE,                 /* partial_inplace */
1251          0,                     /* src_mask */
1252          0xffff,                /* dst_mask */
1253          FALSE),                /* pcrel_offset */
1254
1255   /* 16 bit value resulting from the addend minus the symbol.  */
1256   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
1257          0,                     /* rightshift */
1258          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1259          16,                    /* bitsize */
1260          FALSE,                 /* pc_relative */
1261          0,                     /* bitpos */
1262          complain_overflow_dont,/* complain_on_overflow */
1263          bfd_elf_generic_reloc, /* special_function */
1264          "R_PPC_EMB_ADDR16_LO", /* name */
1265          FALSE,                 /* partial_inplace */
1266          0,                     /* src_mask */
1267          0xffff,                /* dst_mask */
1268          FALSE),                /* pcrel_offset */
1269
1270   /* The high order 16 bits of the addend minus the symbol.  */
1271   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
1272          16,                    /* rightshift */
1273          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1274          16,                    /* bitsize */
1275          FALSE,                 /* pc_relative */
1276          0,                     /* bitpos */
1277          complain_overflow_dont, /* complain_on_overflow */
1278          bfd_elf_generic_reloc, /* special_function */
1279          "R_PPC_EMB_NADDR16_HI", /* name */
1280          FALSE,                 /* partial_inplace */
1281          0,                     /* src_mask */
1282          0xffff,                /* dst_mask */
1283          FALSE),                /* pcrel_offset */
1284
1285   /* The high order 16 bits of the result of the addend minus the address,
1286      plus 1 if the contents of the low 16 bits, treated as a signed number,
1287      is negative.  */
1288   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
1289          16,                    /* rightshift */
1290          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1291          16,                    /* bitsize */
1292          FALSE,                 /* pc_relative */
1293          0,                     /* bitpos */
1294          complain_overflow_dont, /* complain_on_overflow */
1295          ppc_elf_addr16_ha_reloc, /* special_function */
1296          "R_PPC_EMB_NADDR16_HA", /* name */
1297          FALSE,                 /* partial_inplace */
1298          0,                     /* src_mask */
1299          0xffff,                /* dst_mask */
1300          FALSE),                /* pcrel_offset */
1301
1302   /* 16 bit value resulting from allocating a 4 byte word to hold an
1303      address in the .sdata section, and returning the offset from
1304      _SDA_BASE_ for that relocation.  */
1305   HOWTO (R_PPC_EMB_SDAI16,      /* type */
1306          0,                     /* rightshift */
1307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1308          16,                    /* bitsize */
1309          FALSE,                 /* pc_relative */
1310          0,                     /* bitpos */
1311          complain_overflow_signed, /* complain_on_overflow */
1312          bfd_elf_generic_reloc, /* special_function */
1313          "R_PPC_EMB_SDAI16",    /* name */
1314          FALSE,                 /* partial_inplace */
1315          0,                     /* src_mask */
1316          0xffff,                /* dst_mask */
1317          FALSE),                /* pcrel_offset */
1318
1319   /* 16 bit value resulting from allocating a 4 byte word to hold an
1320      address in the .sdata2 section, and returning the offset from
1321      _SDA2_BASE_ for that relocation.  */
1322   HOWTO (R_PPC_EMB_SDA2I16,     /* type */
1323          0,                     /* rightshift */
1324          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1325          16,                    /* bitsize */
1326          FALSE,                 /* pc_relative */
1327          0,                     /* bitpos */
1328          complain_overflow_signed, /* complain_on_overflow */
1329          bfd_elf_generic_reloc, /* special_function */
1330          "R_PPC_EMB_SDA2I16",   /* name */
1331          FALSE,                 /* partial_inplace */
1332          0,                     /* src_mask */
1333          0xffff,                /* dst_mask */
1334          FALSE),                /* pcrel_offset */
1335
1336   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1337      small data items.   */
1338   HOWTO (R_PPC_EMB_SDA2REL,     /* type */
1339          0,                     /* rightshift */
1340          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1341          16,                    /* bitsize */
1342          FALSE,                 /* pc_relative */
1343          0,                     /* bitpos */
1344          complain_overflow_signed, /* complain_on_overflow */
1345          bfd_elf_generic_reloc, /* special_function */
1346          "R_PPC_EMB_SDA2REL",   /* name */
1347          FALSE,                 /* partial_inplace */
1348          0,                     /* src_mask */
1349          0xffff,                /* dst_mask */
1350          FALSE),                /* pcrel_offset */
1351
1352   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1353      signed offset from the appropriate base, and filling in the register
1354      field with the appropriate register (0, 2, or 13).  */
1355   HOWTO (R_PPC_EMB_SDA21,       /* type */
1356          0,                     /* rightshift */
1357          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1358          16,                    /* bitsize */
1359          FALSE,                 /* pc_relative */
1360          0,                     /* bitpos */
1361          complain_overflow_signed, /* complain_on_overflow */
1362          bfd_elf_generic_reloc, /* special_function */
1363          "R_PPC_EMB_SDA21",     /* name */
1364          FALSE,                 /* partial_inplace */
1365          0,                     /* src_mask */
1366          0xffff,                /* dst_mask */
1367          FALSE),                /* pcrel_offset */
1368
1369   /* Relocation not handled: R_PPC_EMB_MRKREF */
1370   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1371   /* Relocation not handled: R_PPC_EMB_RELST_LO */
1372   /* Relocation not handled: R_PPC_EMB_RELST_HI */
1373   /* Relocation not handled: R_PPC_EMB_RELST_HA */
1374   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1375
1376   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1377      in the 16 bit signed offset from the appropriate base, and filling in the
1378      register field with the appropriate register (0, 2, or 13).  */
1379   HOWTO (R_PPC_EMB_RELSDA,      /* type */
1380          0,                     /* rightshift */
1381          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1382          16,                    /* bitsize */
1383          TRUE,                  /* pc_relative */
1384          0,                     /* bitpos */
1385          complain_overflow_signed, /* complain_on_overflow */
1386          bfd_elf_generic_reloc, /* special_function */
1387          "R_PPC_EMB_RELSDA",    /* name */
1388          FALSE,                 /* partial_inplace */
1389          0,                     /* src_mask */
1390          0xffff,                /* dst_mask */
1391          FALSE),                /* pcrel_offset */
1392
1393   HOWTO (R_PPC_IRELATIVE,       /* type */
1394          0,                     /* rightshift */
1395          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1396          32,                    /* bitsize */
1397          FALSE,                 /* pc_relative */
1398          0,                     /* bitpos */
1399          complain_overflow_bitfield, /* complain_on_overflow */
1400          bfd_elf_generic_reloc,  /* special_function */
1401          "R_PPC_IRELATIVE",     /* name */
1402          FALSE,                 /* partial_inplace */
1403          0,                     /* src_mask */
1404          0xffffffff,            /* dst_mask */
1405          FALSE),                /* pcrel_offset */
1406
1407   /* A 16 bit relative relocation.  */
1408   HOWTO (R_PPC_REL16,           /* type */
1409          0,                     /* rightshift */
1410          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1411          16,                    /* bitsize */
1412          TRUE,                  /* pc_relative */
1413          0,                     /* bitpos */
1414          complain_overflow_bitfield, /* complain_on_overflow */
1415          bfd_elf_generic_reloc, /* special_function */
1416          "R_PPC_REL16",         /* name */
1417          FALSE,                 /* partial_inplace */
1418          0,                     /* src_mask */
1419          0xffff,                /* dst_mask */
1420          TRUE),                 /* pcrel_offset */
1421
1422   /* A 16 bit relative relocation without overflow.  */
1423   HOWTO (R_PPC_REL16_LO,        /* type */
1424          0,                     /* rightshift */
1425          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1426          16,                    /* bitsize */
1427          TRUE,                  /* pc_relative */
1428          0,                     /* bitpos */
1429          complain_overflow_dont,/* complain_on_overflow */
1430          bfd_elf_generic_reloc, /* special_function */
1431          "R_PPC_REL16_LO",      /* name */
1432          FALSE,                 /* partial_inplace */
1433          0,                     /* src_mask */
1434          0xffff,                /* dst_mask */
1435          TRUE),                 /* pcrel_offset */
1436
1437   /* The high order 16 bits of a relative address.  */
1438   HOWTO (R_PPC_REL16_HI,        /* type */
1439          16,                    /* rightshift */
1440          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1441          16,                    /* bitsize */
1442          TRUE,                  /* pc_relative */
1443          0,                     /* bitpos */
1444          complain_overflow_dont, /* complain_on_overflow */
1445          bfd_elf_generic_reloc, /* special_function */
1446          "R_PPC_REL16_HI",      /* name */
1447          FALSE,                 /* partial_inplace */
1448          0,                     /* src_mask */
1449          0xffff,                /* dst_mask */
1450          TRUE),                 /* pcrel_offset */
1451
1452   /* The high order 16 bits of a relative address, plus 1 if the contents of
1453      the low 16 bits, treated as a signed number, is negative.  */
1454   HOWTO (R_PPC_REL16_HA,        /* type */
1455          16,                    /* rightshift */
1456          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1457          16,                    /* bitsize */
1458          TRUE,                  /* pc_relative */
1459          0,                     /* bitpos */
1460          complain_overflow_dont, /* complain_on_overflow */
1461          ppc_elf_addr16_ha_reloc, /* special_function */
1462          "R_PPC_REL16_HA",      /* name */
1463          FALSE,                 /* partial_inplace */
1464          0,                     /* src_mask */
1465          0xffff,                /* dst_mask */
1466          TRUE),                 /* pcrel_offset */
1467
1468   /* GNU extension to record C++ vtable hierarchy.  */
1469   HOWTO (R_PPC_GNU_VTINHERIT,   /* type */
1470          0,                     /* rightshift */
1471          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1472          0,                     /* bitsize */
1473          FALSE,                 /* pc_relative */
1474          0,                     /* bitpos */
1475          complain_overflow_dont, /* complain_on_overflow */
1476          NULL,                  /* special_function */
1477          "R_PPC_GNU_VTINHERIT", /* name */
1478          FALSE,                 /* partial_inplace */
1479          0,                     /* src_mask */
1480          0,                     /* dst_mask */
1481          FALSE),                /* pcrel_offset */
1482
1483   /* GNU extension to record C++ vtable member usage.  */
1484   HOWTO (R_PPC_GNU_VTENTRY,     /* type */
1485          0,                     /* rightshift */
1486          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1487          0,                     /* bitsize */
1488          FALSE,                 /* pc_relative */
1489          0,                     /* bitpos */
1490          complain_overflow_dont, /* complain_on_overflow */
1491          NULL,                  /* special_function */
1492          "R_PPC_GNU_VTENTRY",   /* name */
1493          FALSE,                 /* partial_inplace */
1494          0,                     /* src_mask */
1495          0,                     /* dst_mask */
1496          FALSE),                /* pcrel_offset */
1497
1498   /* Phony reloc to handle AIX style TOC entries.  */
1499   HOWTO (R_PPC_TOC16,           /* type */
1500          0,                     /* rightshift */
1501          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1502          16,                    /* bitsize */
1503          FALSE,                 /* pc_relative */
1504          0,                     /* bitpos */
1505          complain_overflow_signed, /* complain_on_overflow */
1506          bfd_elf_generic_reloc, /* special_function */
1507          "R_PPC_TOC16",         /* name */
1508          FALSE,                 /* partial_inplace */
1509          0,                     /* src_mask */
1510          0xffff,                /* dst_mask */
1511          FALSE),                /* pcrel_offset */
1512 };
1513 \f
1514 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1515
1516 static void
1517 ppc_elf_howto_init (void)
1518 {
1519   unsigned int i, type;
1520
1521   for (i = 0;
1522        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1523        i++)
1524     {
1525       type = ppc_elf_howto_raw[i].type;
1526       if (type >= (sizeof (ppc_elf_howto_table)
1527                    / sizeof (ppc_elf_howto_table[0])))
1528         abort ();
1529       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1530     }
1531 }
1532
1533 static reloc_howto_type *
1534 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1535                            bfd_reloc_code_real_type code)
1536 {
1537   enum elf_ppc_reloc_type r;
1538
1539   /* Initialize howto table if not already done.  */
1540   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1541     ppc_elf_howto_init ();
1542
1543   switch (code)
1544     {
1545     default:
1546       return NULL;
1547
1548     case BFD_RELOC_NONE:                r = R_PPC_NONE;                 break;
1549     case BFD_RELOC_32:                  r = R_PPC_ADDR32;               break;
1550     case BFD_RELOC_PPC_BA26:            r = R_PPC_ADDR24;               break;
1551     case BFD_RELOC_16:                  r = R_PPC_ADDR16;               break;
1552     case BFD_RELOC_LO16:                r = R_PPC_ADDR16_LO;            break;
1553     case BFD_RELOC_HI16:                r = R_PPC_ADDR16_HI;            break;
1554     case BFD_RELOC_HI16_S:              r = R_PPC_ADDR16_HA;            break;
1555     case BFD_RELOC_PPC_BA16:            r = R_PPC_ADDR14;               break;
1556     case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC_ADDR14_BRTAKEN;       break;
1557     case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC_ADDR14_BRNTAKEN;      break;
1558     case BFD_RELOC_PPC_B26:             r = R_PPC_REL24;                break;
1559     case BFD_RELOC_PPC_B16:             r = R_PPC_REL14;                break;
1560     case BFD_RELOC_PPC_B16_BRTAKEN:     r = R_PPC_REL14_BRTAKEN;        break;
1561     case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC_REL14_BRNTAKEN;       break;
1562     case BFD_RELOC_16_GOTOFF:           r = R_PPC_GOT16;                break;
1563     case BFD_RELOC_LO16_GOTOFF:         r = R_PPC_GOT16_LO;             break;
1564     case BFD_RELOC_HI16_GOTOFF:         r = R_PPC_GOT16_HI;             break;
1565     case BFD_RELOC_HI16_S_GOTOFF:       r = R_PPC_GOT16_HA;             break;
1566     case BFD_RELOC_24_PLT_PCREL:        r = R_PPC_PLTREL24;             break;
1567     case BFD_RELOC_PPC_COPY:            r = R_PPC_COPY;                 break;
1568     case BFD_RELOC_PPC_GLOB_DAT:        r = R_PPC_GLOB_DAT;             break;
1569     case BFD_RELOC_PPC_LOCAL24PC:       r = R_PPC_LOCAL24PC;            break;
1570     case BFD_RELOC_32_PCREL:            r = R_PPC_REL32;                break;
1571     case BFD_RELOC_32_PLTOFF:           r = R_PPC_PLT32;                break;
1572     case BFD_RELOC_32_PLT_PCREL:        r = R_PPC_PLTREL32;             break;
1573     case BFD_RELOC_LO16_PLTOFF:         r = R_PPC_PLT16_LO;             break;
1574     case BFD_RELOC_HI16_PLTOFF:         r = R_PPC_PLT16_HI;             break;
1575     case BFD_RELOC_HI16_S_PLTOFF:       r = R_PPC_PLT16_HA;             break;
1576     case BFD_RELOC_GPREL16:             r = R_PPC_SDAREL16;             break;
1577     case BFD_RELOC_16_BASEREL:          r = R_PPC_SECTOFF;              break;
1578     case BFD_RELOC_LO16_BASEREL:        r = R_PPC_SECTOFF_LO;           break;
1579     case BFD_RELOC_HI16_BASEREL:        r = R_PPC_SECTOFF_HI;           break;
1580     case BFD_RELOC_HI16_S_BASEREL:      r = R_PPC_SECTOFF_HA;           break;
1581     case BFD_RELOC_CTOR:                r = R_PPC_ADDR32;               break;
1582     case BFD_RELOC_PPC_TOC16:           r = R_PPC_TOC16;                break;
1583     case BFD_RELOC_PPC_TLS:             r = R_PPC_TLS;                  break;
1584     case BFD_RELOC_PPC_TLSGD:           r = R_PPC_TLSGD;                break;
1585     case BFD_RELOC_PPC_TLSLD:           r = R_PPC_TLSLD;                break;
1586     case BFD_RELOC_PPC_DTPMOD:          r = R_PPC_DTPMOD32;             break;
1587     case BFD_RELOC_PPC_TPREL16:         r = R_PPC_TPREL16;              break;
1588     case BFD_RELOC_PPC_TPREL16_LO:      r = R_PPC_TPREL16_LO;           break;
1589     case BFD_RELOC_PPC_TPREL16_HI:      r = R_PPC_TPREL16_HI;           break;
1590     case BFD_RELOC_PPC_TPREL16_HA:      r = R_PPC_TPREL16_HA;           break;
1591     case BFD_RELOC_PPC_TPREL:           r = R_PPC_TPREL32;              break;
1592     case BFD_RELOC_PPC_DTPREL16:        r = R_PPC_DTPREL16;             break;
1593     case BFD_RELOC_PPC_DTPREL16_LO:     r = R_PPC_DTPREL16_LO;          break;
1594     case BFD_RELOC_PPC_DTPREL16_HI:     r = R_PPC_DTPREL16_HI;          break;
1595     case BFD_RELOC_PPC_DTPREL16_HA:     r = R_PPC_DTPREL16_HA;          break;
1596     case BFD_RELOC_PPC_DTPREL:          r = R_PPC_DTPREL32;             break;
1597     case BFD_RELOC_PPC_GOT_TLSGD16:     r = R_PPC_GOT_TLSGD16;          break;
1598     case BFD_RELOC_PPC_GOT_TLSGD16_LO:  r = R_PPC_GOT_TLSGD16_LO;       break;
1599     case BFD_RELOC_PPC_GOT_TLSGD16_HI:  r = R_PPC_GOT_TLSGD16_HI;       break;
1600     case BFD_RELOC_PPC_GOT_TLSGD16_HA:  r = R_PPC_GOT_TLSGD16_HA;       break;
1601     case BFD_RELOC_PPC_GOT_TLSLD16:     r = R_PPC_GOT_TLSLD16;          break;
1602     case BFD_RELOC_PPC_GOT_TLSLD16_LO:  r = R_PPC_GOT_TLSLD16_LO;       break;
1603     case BFD_RELOC_PPC_GOT_TLSLD16_HI:  r = R_PPC_GOT_TLSLD16_HI;       break;
1604     case BFD_RELOC_PPC_GOT_TLSLD16_HA:  r = R_PPC_GOT_TLSLD16_HA;       break;
1605     case BFD_RELOC_PPC_GOT_TPREL16:     r = R_PPC_GOT_TPREL16;          break;
1606     case BFD_RELOC_PPC_GOT_TPREL16_LO:  r = R_PPC_GOT_TPREL16_LO;       break;
1607     case BFD_RELOC_PPC_GOT_TPREL16_HI:  r = R_PPC_GOT_TPREL16_HI;       break;
1608     case BFD_RELOC_PPC_GOT_TPREL16_HA:  r = R_PPC_GOT_TPREL16_HA;       break;
1609     case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC_GOT_DTPREL16;         break;
1610     case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO;      break;
1611     case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI;      break;
1612     case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA;      break;
1613     case BFD_RELOC_PPC_EMB_NADDR32:     r = R_PPC_EMB_NADDR32;          break;
1614     case BFD_RELOC_PPC_EMB_NADDR16:     r = R_PPC_EMB_NADDR16;          break;
1615     case BFD_RELOC_PPC_EMB_NADDR16_LO:  r = R_PPC_EMB_NADDR16_LO;       break;
1616     case BFD_RELOC_PPC_EMB_NADDR16_HI:  r = R_PPC_EMB_NADDR16_HI;       break;
1617     case BFD_RELOC_PPC_EMB_NADDR16_HA:  r = R_PPC_EMB_NADDR16_HA;       break;
1618     case BFD_RELOC_PPC_EMB_SDAI16:      r = R_PPC_EMB_SDAI16;           break;
1619     case BFD_RELOC_PPC_EMB_SDA2I16:     r = R_PPC_EMB_SDA2I16;          break;
1620     case BFD_RELOC_PPC_EMB_SDA2REL:     r = R_PPC_EMB_SDA2REL;          break;
1621     case BFD_RELOC_PPC_EMB_SDA21:       r = R_PPC_EMB_SDA21;            break;
1622     case BFD_RELOC_PPC_EMB_MRKREF:      r = R_PPC_EMB_MRKREF;           break;
1623     case BFD_RELOC_PPC_EMB_RELSEC16:    r = R_PPC_EMB_RELSEC16;         break;
1624     case BFD_RELOC_PPC_EMB_RELST_LO:    r = R_PPC_EMB_RELST_LO;         break;
1625     case BFD_RELOC_PPC_EMB_RELST_HI:    r = R_PPC_EMB_RELST_HI;         break;
1626     case BFD_RELOC_PPC_EMB_RELST_HA:    r = R_PPC_EMB_RELST_HA;         break;
1627     case BFD_RELOC_PPC_EMB_BIT_FLD:     r = R_PPC_EMB_BIT_FLD;          break;
1628     case BFD_RELOC_PPC_EMB_RELSDA:      r = R_PPC_EMB_RELSDA;           break;
1629     case BFD_RELOC_16_PCREL:            r = R_PPC_REL16;                break;
1630     case BFD_RELOC_LO16_PCREL:          r = R_PPC_REL16_LO;             break;
1631     case BFD_RELOC_HI16_PCREL:          r = R_PPC_REL16_HI;             break;
1632     case BFD_RELOC_HI16_S_PCREL:        r = R_PPC_REL16_HA;             break;
1633     case BFD_RELOC_VTABLE_INHERIT:      r = R_PPC_GNU_VTINHERIT;        break;
1634     case BFD_RELOC_VTABLE_ENTRY:        r = R_PPC_GNU_VTENTRY;          break;
1635     }
1636
1637   return ppc_elf_howto_table[r];
1638 };
1639
1640 static reloc_howto_type *
1641 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1642                            const char *r_name)
1643 {
1644   unsigned int i;
1645
1646   for (i = 0;
1647        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1648        i++)
1649     if (ppc_elf_howto_raw[i].name != NULL
1650         && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
1651       return &ppc_elf_howto_raw[i];
1652
1653   return NULL;
1654 }
1655
1656 /* Set the howto pointer for a PowerPC ELF reloc.  */
1657
1658 static void
1659 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1660                        arelent *cache_ptr,
1661                        Elf_Internal_Rela *dst)
1662 {
1663   /* Initialize howto table if not already done.  */
1664   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1665     ppc_elf_howto_init ();
1666
1667   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1668   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1669
1670   /* Just because the above assert didn't trigger doesn't mean that
1671      ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation.  */
1672   if (!cache_ptr->howto)
1673     {
1674       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
1675                              abfd, ELF32_R_TYPE (dst->r_info));
1676       bfd_set_error (bfd_error_bad_value);
1677
1678       cache_ptr->howto = ppc_elf_howto_table[R_PPC_NONE];
1679     }
1680 }
1681
1682 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
1683
1684 static bfd_reloc_status_type
1685 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1686                          arelent *reloc_entry,
1687                          asymbol *symbol,
1688                          void *data ATTRIBUTE_UNUSED,
1689                          asection *input_section,
1690                          bfd *output_bfd,
1691                          char **error_message ATTRIBUTE_UNUSED)
1692 {
1693   bfd_vma relocation;
1694
1695   if (output_bfd != NULL)
1696     {
1697       reloc_entry->address += input_section->output_offset;
1698       return bfd_reloc_ok;
1699     }
1700
1701   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1702     return bfd_reloc_outofrange;
1703
1704   if (bfd_is_com_section (symbol->section))
1705     relocation = 0;
1706   else
1707     relocation = symbol->value;
1708
1709   relocation += symbol->section->output_section->vma;
1710   relocation += symbol->section->output_offset;
1711   relocation += reloc_entry->addend;
1712   if (reloc_entry->howto->pc_relative)
1713     relocation -= reloc_entry->address;
1714
1715   reloc_entry->addend += (relocation & 0x8000) << 1;
1716
1717   return bfd_reloc_continue;
1718 }
1719
1720 static bfd_reloc_status_type
1721 ppc_elf_unhandled_reloc (bfd *abfd,
1722                          arelent *reloc_entry,
1723                          asymbol *symbol,
1724                          void *data,
1725                          asection *input_section,
1726                          bfd *output_bfd,
1727                          char **error_message)
1728 {
1729   /* If this is a relocatable link (output_bfd test tells us), just
1730      call the generic function.  Any adjustment will be done at final
1731      link time.  */
1732   if (output_bfd != NULL)
1733     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1734                                   input_section, output_bfd, error_message);
1735
1736   if (error_message != NULL)
1737     {
1738       static char buf[60];
1739       sprintf (buf, _("generic linker can't handle %s"),
1740                reloc_entry->howto->name);
1741       *error_message = buf;
1742     }
1743   return bfd_reloc_dangerous;
1744 }
1745 \f
1746 /* Sections created by the linker.  */
1747
1748 typedef struct elf_linker_section
1749 {
1750   /* Pointer to the bfd section.  */
1751   asection *section;
1752   /* Section name.  */
1753   const char *name;
1754   /* Associated bss section name.  */
1755   const char *bss_name;
1756   /* Associated symbol name.  */
1757   const char *sym_name;
1758   /* Associated symbol.  */
1759   struct elf_link_hash_entry *sym;
1760 } elf_linker_section_t;
1761
1762 /* Linked list of allocated pointer entries.  This hangs off of the
1763    symbol lists, and provides allows us to return different pointers,
1764    based on different addend's.  */
1765
1766 typedef struct elf_linker_section_pointers
1767 {
1768   /* next allocated pointer for this symbol */
1769   struct elf_linker_section_pointers *next;
1770   /* offset of pointer from beginning of section */
1771   bfd_vma offset;
1772   /* addend used */
1773   bfd_vma addend;
1774   /* which linker section this is */
1775   elf_linker_section_t *lsect;
1776 } elf_linker_section_pointers_t;
1777
1778 struct ppc_elf_obj_tdata
1779 {
1780   struct elf_obj_tdata elf;
1781
1782   /* A mapping from local symbols to offsets into the various linker
1783      sections added.  This is index by the symbol index.  */
1784   elf_linker_section_pointers_t **linker_section_pointers;
1785
1786   /* Flags used to auto-detect plt type.  */
1787   unsigned int makes_plt_call : 1;
1788   unsigned int has_rel16 : 1;
1789 };
1790
1791 #define ppc_elf_tdata(bfd) \
1792   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1793
1794 #define elf_local_ptr_offsets(bfd) \
1795   (ppc_elf_tdata (bfd)->linker_section_pointers)
1796
1797 #define is_ppc_elf(bfd) \
1798   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1799    && elf_object_id (bfd) == PPC32_ELF_TDATA)
1800
1801 /* Override the generic function because we store some extras.  */
1802
1803 static bfd_boolean
1804 ppc_elf_mkobject (bfd *abfd)
1805 {
1806   return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
1807                                   PPC32_ELF_TDATA);
1808 }
1809
1810 /* Fix bad default arch selected for a 32 bit input bfd when the
1811    default is 64 bit.  */
1812
1813 static bfd_boolean
1814 ppc_elf_object_p (bfd *abfd)
1815 {
1816   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1817     {
1818       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1821         {
1822           /* Relies on arch after 64 bit default being 32 bit default.  */
1823           abfd->arch_info = abfd->arch_info->next;
1824           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1825         }
1826     }
1827   return TRUE;
1828 }
1829
1830 /* Function to set whether a module needs the -mrelocatable bit set.  */
1831
1832 static bfd_boolean
1833 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1834 {
1835   BFD_ASSERT (!elf_flags_init (abfd)
1836               || elf_elfheader (abfd)->e_flags == flags);
1837
1838   elf_elfheader (abfd)->e_flags = flags;
1839   elf_flags_init (abfd) = TRUE;
1840   return TRUE;
1841 }
1842
1843 /* Support for core dump NOTE sections.  */
1844
1845 static bfd_boolean
1846 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1847 {
1848   int offset;
1849   unsigned int size;
1850
1851   switch (note->descsz)
1852     {
1853     default:
1854       return FALSE;
1855
1856     case 268:           /* Linux/PPC.  */
1857       /* pr_cursig */
1858       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1859
1860       /* pr_pid */
1861       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1862
1863       /* pr_reg */
1864       offset = 72;
1865       size = 192;
1866
1867       break;
1868     }
1869
1870   /* Make a ".reg/999" section.  */
1871   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1872                                           size, note->descpos + offset);
1873 }
1874
1875 static bfd_boolean
1876 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1877 {
1878   switch (note->descsz)
1879     {
1880     default:
1881       return FALSE;
1882
1883     case 128:           /* Linux/PPC elf_prpsinfo.  */
1884       elf_tdata (abfd)->core_program
1885         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1886       elf_tdata (abfd)->core_command
1887         = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1888     }
1889
1890   /* Note that for some reason, a spurious space is tacked
1891      onto the end of the args in some (at least one anyway)
1892      implementations, so strip it off if it exists.  */
1893
1894   {
1895     char *command = elf_tdata (abfd)->core_command;
1896     int n = strlen (command);
1897
1898     if (0 < n && command[n - 1] == ' ')
1899       command[n - 1] = '\0';
1900   }
1901
1902   return TRUE;
1903 }
1904
1905 static char *
1906 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
1907 {
1908   switch (note_type)
1909     {
1910     default:
1911       return NULL;
1912
1913     case NT_PRPSINFO:
1914       {
1915         char data[128];
1916         va_list ap;
1917
1918         va_start (ap, note_type);
1919         memset (data, 0, 32);
1920         strncpy (data + 32, va_arg (ap, const char *), 16);
1921         strncpy (data + 48, va_arg (ap, const char *), 80);
1922         va_end (ap);
1923         return elfcore_write_note (abfd, buf, bufsiz,
1924                                    "CORE", note_type, data, sizeof (data));
1925       }
1926
1927     case NT_PRSTATUS:
1928       {
1929         char data[268];
1930         va_list ap;
1931         long pid;
1932         int cursig;
1933         const void *greg;
1934
1935         va_start (ap, note_type);
1936         memset (data, 0, 72);
1937         pid = va_arg (ap, long);
1938         bfd_put_32 (abfd, pid, data + 24);
1939         cursig = va_arg (ap, int);
1940         bfd_put_16 (abfd, cursig, data + 12);
1941         greg = va_arg (ap, const void *);
1942         memcpy (data + 72, greg, 192);
1943         memset (data + 264, 0, 4);
1944         va_end (ap);
1945         return elfcore_write_note (abfd, buf, bufsiz,
1946                                    "CORE", note_type, data, sizeof (data));
1947       }
1948     }
1949 }
1950
1951 /* Return address for Ith PLT stub in section PLT, for relocation REL
1952    or (bfd_vma) -1 if it should not be included.  */
1953
1954 static bfd_vma
1955 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1956                      const asection *plt ATTRIBUTE_UNUSED,
1957                      const arelent *rel)
1958 {
1959   return rel->address;
1960 }
1961
1962 /* Handle a PowerPC specific section when reading an object file.  This
1963    is called when bfd_section_from_shdr finds a section with an unknown
1964    type.  */
1965
1966 static bfd_boolean
1967 ppc_elf_section_from_shdr (bfd *abfd,
1968                            Elf_Internal_Shdr *hdr,
1969                            const char *name,
1970                            int shindex)
1971 {
1972   asection *newsect;
1973   flagword flags;
1974
1975   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1976     return FALSE;
1977
1978   newsect = hdr->bfd_section;
1979   flags = bfd_get_section_flags (abfd, newsect);
1980   if (hdr->sh_flags & SHF_EXCLUDE)
1981     flags |= SEC_EXCLUDE;
1982
1983   if (hdr->sh_type == SHT_ORDERED)
1984     flags |= SEC_SORT_ENTRIES;
1985
1986   bfd_set_section_flags (abfd, newsect, flags);
1987   return TRUE;
1988 }
1989
1990 /* Set up any other section flags and such that may be necessary.  */
1991
1992 static bfd_boolean
1993 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1994                        Elf_Internal_Shdr *shdr,
1995                        asection *asect)
1996 {
1997   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
1998     shdr->sh_flags |= SHF_EXCLUDE;
1999
2000   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2001     shdr->sh_type = SHT_ORDERED;
2002
2003   return TRUE;
2004 }
2005
2006 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
2007    need to bump up the number of section headers.  */
2008
2009 static int
2010 ppc_elf_additional_program_headers (bfd *abfd,
2011                                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
2012 {
2013   asection *s;
2014   int ret = 0;
2015
2016   s = bfd_get_section_by_name (abfd, ".sbss2");
2017   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2018     ++ret;
2019
2020   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2021   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2022     ++ret;
2023
2024   return ret;
2025 }
2026
2027 /* Add extra PPC sections -- Note, for now, make .sbss2 and
2028    .PPC.EMB.sbss0 a normal section, and not a bss section so
2029    that the linker doesn't crater when trying to make more than
2030    2 sections.  */
2031
2032 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
2033 {
2034   { STRING_COMMA_LEN (".plt"),             0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
2035   { STRING_COMMA_LEN (".sbss"),           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2036   { STRING_COMMA_LEN (".sbss2"),          -2, SHT_PROGBITS, SHF_ALLOC },
2037   { STRING_COMMA_LEN (".sdata"),          -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2038   { STRING_COMMA_LEN (".sdata2"),         -2, SHT_PROGBITS, SHF_ALLOC },
2039   { STRING_COMMA_LEN (".tags"),            0, SHT_ORDERED,  SHF_ALLOC },
2040   { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE,     0 },
2041   { STRING_COMMA_LEN (".PPC.EMB.sbss0"),   0, SHT_PROGBITS, SHF_ALLOC },
2042   { STRING_COMMA_LEN (".PPC.EMB.sdata0"),  0, SHT_PROGBITS, SHF_ALLOC },
2043   { NULL,                              0,  0, 0,            0 }
2044 };
2045
2046 /* This is what we want for new plt/got.  */
2047 static struct bfd_elf_special_section ppc_alt_plt =
2048   { STRING_COMMA_LEN (".plt"),             0, SHT_PROGBITS, SHF_ALLOC };
2049
2050 static const struct bfd_elf_special_section *
2051 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
2052 {
2053   const struct bfd_elf_special_section *ssect;
2054
2055   /* See if this is one of the special sections.  */
2056   if (sec->name == NULL)
2057     return NULL;
2058
2059   ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2060                                         sec->use_rela_p);
2061   if (ssect != NULL)
2062     {
2063       if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2064         ssect = &ppc_alt_plt;
2065       return ssect;
2066     }
2067
2068   return _bfd_elf_get_sec_type_attr (abfd, sec);
2069 }
2070 \f
2071 /* Very simple linked list structure for recording apuinfo values.  */
2072 typedef struct apuinfo_list
2073 {
2074   struct apuinfo_list *next;
2075   unsigned long value;
2076 }
2077 apuinfo_list;
2078
2079 static apuinfo_list *head;
2080
2081
2082 static void
2083 apuinfo_list_init (void)
2084 {
2085   head = NULL;
2086 }
2087
2088 static void
2089 apuinfo_list_add (unsigned long value)
2090 {
2091   apuinfo_list *entry = head;
2092
2093   while (entry != NULL)
2094     {
2095       if (entry->value == value)
2096         return;
2097       entry = entry->next;
2098     }
2099
2100   entry = bfd_malloc (sizeof (* entry));
2101   if (entry == NULL)
2102     return;
2103
2104   entry->value = value;
2105   entry->next  = head;
2106   head = entry;
2107 }
2108
2109 static unsigned
2110 apuinfo_list_length (void)
2111 {
2112   apuinfo_list *entry;
2113   unsigned long count;
2114
2115   for (entry = head, count = 0;
2116        entry;
2117        entry = entry->next)
2118     ++ count;
2119
2120   return count;
2121 }
2122
2123 static inline unsigned long
2124 apuinfo_list_element (unsigned long number)
2125 {
2126   apuinfo_list * entry;
2127
2128   for (entry = head;
2129        entry && number --;
2130        entry = entry->next)
2131     ;
2132
2133   return entry ? entry->value : 0;
2134 }
2135
2136 static void
2137 apuinfo_list_finish (void)
2138 {
2139   apuinfo_list *entry;
2140
2141   for (entry = head; entry;)
2142     {
2143       apuinfo_list *next = entry->next;
2144       free (entry);
2145       entry = next;
2146     }
2147
2148   head = NULL;
2149 }
2150
2151 #define APUINFO_SECTION_NAME    ".PPC.EMB.apuinfo"
2152 #define APUINFO_LABEL           "APUinfo"
2153
2154 /* Scan the input BFDs and create a linked list of
2155    the APUinfo values that will need to be emitted.  */
2156
2157 static void
2158 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2159 {
2160   bfd *ibfd;
2161   asection *asec;
2162   char *buffer;
2163   unsigned num_input_sections;
2164   bfd_size_type output_section_size;
2165   unsigned i;
2166   unsigned num_entries;
2167   unsigned long offset;
2168   unsigned long length;
2169   const char *error_message = NULL;
2170
2171   if (link_info == NULL)
2172     return;
2173
2174   /* Scan the input bfds, looking for apuinfo sections.  */
2175   num_input_sections = 0;
2176   output_section_size = 0;
2177
2178   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2179     {
2180       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2181       if (asec)
2182         {
2183           ++ num_input_sections;
2184           output_section_size += asec->size;
2185         }
2186     }
2187
2188   /* We need at least one input sections
2189      in order to make merging worthwhile.  */
2190   if (num_input_sections < 1)
2191     return;
2192
2193   /* Just make sure that the output section exists as well.  */
2194   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2195   if (asec == NULL)
2196     return;
2197
2198   /* Allocate a buffer for the contents of the input sections.  */
2199   buffer = bfd_malloc (output_section_size);
2200   if (buffer == NULL)
2201     return;
2202
2203   offset = 0;
2204   apuinfo_list_init ();
2205
2206   /* Read in the input sections contents.  */
2207   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2208     {
2209       unsigned long datum;
2210       char *ptr;
2211
2212       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2213       if (asec == NULL)
2214         continue;
2215
2216       length = asec->size;
2217       if (length < 24)
2218         {
2219           error_message = _("corrupt or empty %s section in %B");
2220           goto fail;
2221         }
2222
2223       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2224           || (bfd_bread (buffer + offset, length, ibfd) != length))
2225         {
2226           error_message = _("unable to read in %s section from %B");
2227           goto fail;
2228         }
2229
2230       /* Process the contents of the section.  */
2231       ptr = buffer + offset;
2232       error_message = _("corrupt %s section in %B");
2233
2234       /* Verify the contents of the header.  Note - we have to
2235          extract the values this way in order to allow for a
2236          host whose endian-ness is different from the target.  */
2237       datum = bfd_get_32 (ibfd, ptr);
2238       if (datum != sizeof APUINFO_LABEL)
2239         goto fail;
2240
2241       datum = bfd_get_32 (ibfd, ptr + 8);
2242       if (datum != 0x2)
2243         goto fail;
2244
2245       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2246         goto fail;
2247
2248       /* Get the number of bytes used for apuinfo entries.  */
2249       datum = bfd_get_32 (ibfd, ptr + 4);
2250       if (datum + 20 != length)
2251         goto fail;
2252
2253       /* Make sure that we do not run off the end of the section.  */
2254       if (offset + length > output_section_size)
2255         goto fail;
2256
2257       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
2258       for (i = 0; i < datum; i += 4)
2259         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2260
2261       /* Update the offset.  */
2262       offset += length;
2263     }
2264
2265   error_message = NULL;
2266
2267   /* Compute the size of the output section.  */
2268   num_entries = apuinfo_list_length ();
2269   output_section_size = 20 + num_entries * 4;
2270
2271   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2272
2273   if (! bfd_set_section_size (abfd, asec, output_section_size))
2274     ibfd = abfd,
2275       error_message = _("warning: unable to set size of %s section in %B");
2276
2277  fail:
2278   free (buffer);
2279
2280   if (error_message)
2281     (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2282 }
2283
2284 /* Prevent the output section from accumulating the input sections'
2285    contents.  We have already stored this in our linked list structure.  */
2286
2287 static bfd_boolean
2288 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2289                        struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2290                        asection *asec,
2291                        bfd_byte *contents ATTRIBUTE_UNUSED)
2292 {
2293   return (apuinfo_list_length ()
2294           && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2295 }
2296
2297 /* Finally we can generate the output section.  */
2298
2299 static void
2300 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2301 {
2302   bfd_byte *buffer;
2303   asection *asec;
2304   unsigned i;
2305   unsigned num_entries;
2306   bfd_size_type length;
2307
2308   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2309   if (asec == NULL)
2310     return;
2311
2312   if (apuinfo_list_length () == 0)
2313     return;
2314
2315   length = asec->size;
2316   if (length < 20)
2317     return;
2318
2319   buffer = bfd_malloc (length);
2320   if (buffer == NULL)
2321     {
2322       (*_bfd_error_handler)
2323         (_("failed to allocate space for new APUinfo section."));
2324       return;
2325     }
2326
2327   /* Create the apuinfo header.  */
2328   num_entries = apuinfo_list_length ();
2329   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2330   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2331   bfd_put_32 (abfd, 0x2, buffer + 8);
2332   strcpy ((char *) buffer + 12, APUINFO_LABEL);
2333
2334   length = 20;
2335   for (i = 0; i < num_entries; i++)
2336     {
2337       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2338       length += 4;
2339     }
2340
2341   if (length != asec->size)
2342     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2343
2344   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2345     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2346
2347   free (buffer);
2348
2349   apuinfo_list_finish ();
2350 }
2351 \f
2352 static bfd_boolean
2353 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
2354 {
2355   bfd_byte buf[GLINK_ENTRY_SIZE];
2356
2357   if (!bfd_get_section_contents (abfd, glink, buf, off, GLINK_ENTRY_SIZE))
2358     return FALSE;
2359
2360   return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
2361           && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
2362           && bfd_get_32 (abfd, buf + 8) == MTCTR_11
2363           && bfd_get_32 (abfd, buf + 12) == BCTR);
2364 }
2365
2366 static bfd_boolean
2367 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2368 {
2369   bfd_vma vma = *(bfd_vma *) ptr;
2370   return ((section->flags & SEC_ALLOC) != 0
2371           && section->vma <= vma
2372           && vma < section->vma + section->size);
2373 }
2374
2375 static long
2376 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
2377                               long dynsymcount, asymbol **dynsyms,
2378                               asymbol **ret)
2379 {
2380   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2381   asection *plt, *relplt, *dynamic, *glink;
2382   bfd_vma glink_vma = 0;
2383   bfd_vma resolv_vma = 0;
2384   bfd_vma stub_vma;
2385   asymbol *s;
2386   arelent *p;
2387   long count, i;
2388   size_t size;
2389   char *names;
2390   bfd_byte buf[4];
2391
2392   *ret = NULL;
2393
2394   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
2395     return 0;
2396
2397   if (dynsymcount <= 0)
2398     return 0;
2399
2400   relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2401   if (relplt == NULL)
2402     return 0;
2403
2404   plt = bfd_get_section_by_name (abfd, ".plt");
2405   if (plt == NULL)
2406     return 0;
2407
2408   /* Call common code to handle old-style executable PLTs.  */
2409   if (elf_section_flags (plt) & SHF_EXECINSTR)
2410     return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
2411                                           dynsymcount, dynsyms, ret);
2412
2413   /* If this object was prelinked, the prelinker stored the address
2414      of .glink at got[1].  If it wasn't prelinked, got[1] will be zero.  */
2415   dynamic = bfd_get_section_by_name (abfd, ".dynamic");
2416   if (dynamic != NULL)
2417     {
2418       bfd_byte *dynbuf, *extdyn, *extdynend;
2419       size_t extdynsize;
2420       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2421
2422       if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2423         return -1;
2424
2425       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2426       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2427
2428       extdyn = dynbuf;
2429       extdynend = extdyn + dynamic->size;
2430       for (; extdyn < extdynend; extdyn += extdynsize)
2431         {
2432           Elf_Internal_Dyn dyn;
2433           (*swap_dyn_in) (abfd, extdyn, &dyn);
2434
2435           if (dyn.d_tag == DT_NULL)
2436             break;
2437
2438           if (dyn.d_tag == DT_PPC_GOT)
2439             {
2440               unsigned int g_o_t = dyn.d_un.d_val;
2441               asection *got = bfd_get_section_by_name (abfd, ".got");
2442               if (got != NULL
2443                   && bfd_get_section_contents (abfd, got, buf,
2444                                                g_o_t - got->vma + 4, 4))
2445                 glink_vma = bfd_get_32 (abfd, buf);
2446               break;
2447             }
2448         }
2449       free (dynbuf);
2450     }
2451
2452   /* Otherwise we read the first plt entry.  */
2453   if (glink_vma == 0)
2454     {
2455       if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
2456         glink_vma = bfd_get_32 (abfd, buf);
2457     }
2458
2459   if (glink_vma == 0)
2460     return 0;
2461
2462   /* The .glink section usually does not survive the final
2463      link; search for the section (usually .text) where the
2464      glink stubs now reside.  */
2465   glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
2466   if (glink == NULL)
2467     return 0;
2468
2469   /* Determine glink PLT resolver by reading the relative branch
2470      from the first glink stub.  */
2471   if (bfd_get_section_contents (abfd, glink, buf,
2472                                 glink_vma - glink->vma, 4))
2473     {
2474       unsigned int insn = bfd_get_32 (abfd, buf);
2475
2476       /* The first glink stub may either branch to the resolver ...  */
2477       insn ^= B;
2478       if ((insn & ~0x3fffffc) == 0)
2479         resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
2480
2481       /* ... or fall through a bunch of NOPs.  */
2482       else if ((insn ^ B ^ NOP) == 0)
2483         for (i = 4;
2484              bfd_get_section_contents (abfd, glink, buf,
2485                                        glink_vma - glink->vma + i, 4);
2486              i += 4)
2487           if (bfd_get_32 (abfd, buf) != NOP)
2488             {
2489               resolv_vma = glink_vma + i;
2490               break;
2491             }
2492     }
2493
2494   count = relplt->size / sizeof (Elf32_External_Rela);
2495   stub_vma = glink_vma - (bfd_vma) count * 16;
2496   /* If the stubs are those for -shared/-pie then we might have
2497      multiple stubs for each plt entry.  If that is the case then
2498      there is no way to associate stubs with their plt entries short
2499      of figuring out the GOT pointer value used in the stub.  */
2500   if (!is_nonpic_glink_stub (abfd, glink,
2501                              glink_vma - GLINK_ENTRY_SIZE - glink->vma))
2502     return 0;
2503
2504   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2505   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
2506     return -1;
2507
2508   size = count * sizeof (asymbol);
2509   p = relplt->relocation;
2510   for (i = 0; i < count; i++, p++)
2511     {
2512       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2513       if (p->addend != 0)
2514         size += sizeof ("+0x") - 1 + 8;
2515     }
2516
2517   size += sizeof (asymbol) + sizeof ("__glink");
2518
2519   if (resolv_vma)
2520     size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2521
2522   s = *ret = bfd_malloc (size);
2523   if (s == NULL)
2524     return -1;
2525
2526   names = (char *) (s + count + 1 + (resolv_vma != 0));
2527   p = relplt->relocation;
2528   for (i = 0; i < count; i++, p++)
2529     {
2530       size_t len;
2531
2532       *s = **p->sym_ptr_ptr;
2533       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2534          we are defining a symbol, ensure one of them is set.  */
2535       if ((s->flags & BSF_LOCAL) == 0)
2536         s->flags |= BSF_GLOBAL;
2537       s->flags |= BSF_SYNTHETIC;
2538       s->section = glink;
2539       s->value = stub_vma - glink->vma;
2540       s->name = names;
2541       s->udata.p = NULL;
2542       len = strlen ((*p->sym_ptr_ptr)->name);
2543       memcpy (names, (*p->sym_ptr_ptr)->name, len);
2544       names += len;
2545       if (p->addend != 0)
2546         {
2547           memcpy (names, "+0x", sizeof ("+0x") - 1);
2548           names += sizeof ("+0x") - 1;
2549           bfd_sprintf_vma (abfd, names, p->addend);
2550           names += strlen (names);
2551         }
2552       memcpy (names, "@plt", sizeof ("@plt"));
2553       names += sizeof ("@plt");
2554       ++s;
2555       stub_vma += 16;
2556     }
2557
2558   /* Add a symbol at the start of the glink branch table.  */
2559   memset (s, 0, sizeof *s);
2560   s->the_bfd = abfd;
2561   s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2562   s->section = glink;
2563   s->value = glink_vma - glink->vma;
2564   s->name = names;
2565   memcpy (names, "__glink", sizeof ("__glink"));
2566   names += sizeof ("__glink");
2567   s++;
2568   count++;
2569
2570   if (resolv_vma)
2571     {
2572       /* Add a symbol for the glink PLT resolver.  */
2573       memset (s, 0, sizeof *s);
2574       s->the_bfd = abfd;
2575       s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2576       s->section = glink;
2577       s->value = resolv_vma - glink->vma;
2578       s->name = names;
2579       memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2580       names += sizeof ("__glink_PLTresolve");
2581       s++;
2582       count++;
2583     }
2584
2585   return count;
2586 }
2587 \f
2588 /* The following functions are specific to the ELF linker, while
2589    functions above are used generally.  They appear in this file more
2590    or less in the order in which they are called.  eg.
2591    ppc_elf_check_relocs is called early in the link process,
2592    ppc_elf_finish_dynamic_sections is one of the last functions
2593    called.  */
2594
2595 /* The PPC linker needs to keep track of the number of relocs that it
2596    decides to copy as dynamic relocs in check_relocs for each symbol.
2597    This is so that it can later discard them if they are found to be
2598    unnecessary.  We store the information in a field extending the
2599    regular ELF linker hash table.  */
2600
2601 struct ppc_elf_dyn_relocs
2602 {
2603   struct ppc_elf_dyn_relocs *next;
2604
2605   /* The input section of the reloc.  */
2606   asection *sec;
2607
2608   /* Total number of relocs copied for the input section.  */
2609   bfd_size_type count;
2610
2611   /* Number of pc-relative relocs copied for the input section.  */
2612   bfd_size_type pc_count;
2613 };
2614
2615 /* Track PLT entries needed for a given symbol.  We might need more
2616    than one glink entry per symbol when generating a pic binary.  */
2617 struct plt_entry
2618 {
2619   struct plt_entry *next;
2620
2621   /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2622      This field stores the offset into .got2 used to initialise the
2623      GOT pointer reg.  It will always be at least 32768.  (Current
2624      gcc always uses an offset of 32768, but ld -r will pack .got2
2625      sections together resulting in larger offsets).  */
2626   bfd_vma addend;
2627
2628   /* The .got2 section.  */
2629   asection *sec;
2630
2631   /* PLT refcount or offset.  */
2632   union
2633     {
2634       bfd_signed_vma refcount;
2635       bfd_vma offset;
2636     } plt;
2637
2638   /* .glink stub offset.  */
2639   bfd_vma glink_offset;
2640 };
2641
2642 /* Of those relocs that might be copied as dynamic relocs, this function
2643    selects those that must be copied when linking a shared library,
2644    even when the symbol is local.  */
2645
2646 static int
2647 must_be_dyn_reloc (struct bfd_link_info *info,
2648                    enum elf_ppc_reloc_type r_type)
2649 {
2650   switch (r_type)
2651     {
2652     default:
2653       return 1;
2654
2655     case R_PPC_REL24:
2656     case R_PPC_REL14:
2657     case R_PPC_REL14_BRTAKEN:
2658     case R_PPC_REL14_BRNTAKEN:
2659     case R_PPC_REL32:
2660       return 0;
2661
2662     case R_PPC_TPREL32:
2663     case R_PPC_TPREL16:
2664     case R_PPC_TPREL16_LO:
2665     case R_PPC_TPREL16_HI:
2666     case R_PPC_TPREL16_HA:
2667       return !info->executable;
2668     }
2669 }
2670
2671 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2672    copying dynamic variables from a shared lib into an app's dynbss
2673    section, and instead use a dynamic relocation to point into the
2674    shared lib.  */
2675 #define ELIMINATE_COPY_RELOCS 1
2676
2677 /* PPC ELF linker hash entry.  */
2678
2679 struct ppc_elf_link_hash_entry
2680 {
2681   struct elf_link_hash_entry elf;
2682
2683   /* If this symbol is used in the linker created sections, the processor
2684      specific backend uses this field to map the field into the offset
2685      from the beginning of the section.  */
2686   elf_linker_section_pointers_t *linker_section_pointer;
2687
2688   /* Track dynamic relocs copied for this symbol.  */
2689   struct ppc_elf_dyn_relocs *dyn_relocs;
2690
2691   /* Contexts in which symbol is used in the GOT (or TOC).
2692      TLS_GD .. TLS_TLS bits are or'd into the mask as the
2693      corresponding relocs are encountered during check_relocs.
2694      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2695      indicate the corresponding GOT entry type is not needed.  */
2696 #define TLS_GD           1      /* GD reloc. */
2697 #define TLS_LD           2      /* LD reloc. */
2698 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2699 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2700 #define TLS_TLS         16      /* Any TLS reloc.  */
2701 #define TLS_TPRELGD     32      /* TPREL reloc resulting from GD->IE. */
2702 #define PLT_IFUNC       64      /* STT_GNU_IFUNC.  */
2703   char tls_mask;
2704
2705   /* Nonzero if we have seen a small data relocation referring to this
2706      symbol.  */
2707   unsigned char has_sda_refs;
2708 };
2709
2710 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2711
2712 /* PPC ELF linker hash table.  */
2713
2714 struct ppc_elf_link_hash_table
2715 {
2716   struct elf_link_hash_table elf;
2717
2718   /* Short-cuts to get to dynamic linker sections.  */
2719   asection *got;
2720   asection *relgot;
2721   asection *glink;
2722   asection *plt;
2723   asection *relplt;
2724   asection *iplt;
2725   asection *reliplt;
2726   asection *dynbss;
2727   asection *relbss;
2728   asection *dynsbss;
2729   asection *relsbss;
2730   elf_linker_section_t sdata[2];
2731   asection *sbss;
2732
2733   /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
2734   asection *srelplt2;
2735
2736   /* The .got.plt section (VxWorks only)*/
2737   asection *sgotplt;
2738
2739   /* Shortcut to __tls_get_addr.  */
2740   struct elf_link_hash_entry *tls_get_addr;
2741
2742   /* The bfd that forced an old-style PLT.  */
2743   bfd *old_bfd;
2744  
2745   /* TLS local dynamic got entry handling.  */
2746   union {
2747     bfd_signed_vma refcount;
2748     bfd_vma offset;
2749   } tlsld_got;
2750
2751   /* Offset of branch table to PltResolve function in glink.  */
2752   bfd_vma glink_pltresolve;
2753
2754   /* Size of reserved GOT entries.  */
2755   unsigned int got_header_size;
2756   /* Non-zero if allocating the header left a gap.  */
2757   unsigned int got_gap;
2758
2759   /* The type of PLT we have chosen to use.  */
2760   enum ppc_elf_plt_type plt_type;
2761
2762   /* Set if we should emit symbols for stubs.  */
2763   unsigned int emit_stub_syms:1;
2764
2765   /* Set if __tls_get_addr optimization should not be done.  */
2766   unsigned int no_tls_get_addr_opt:1;
2767
2768   /* True if the target system is VxWorks.  */
2769   unsigned int is_vxworks:1;
2770
2771   /* The size of PLT entries.  */
2772   int plt_entry_size;
2773   /* The distance between adjacent PLT slots.  */
2774   int plt_slot_size;
2775   /* The size of the first PLT entry.  */
2776   int plt_initial_entry_size;
2777
2778   /* Small local sym cache.  */
2779   struct sym_cache sym_cache;
2780 };
2781
2782 /* Get the PPC ELF linker hash table from a link_info structure.  */
2783
2784 #define ppc_elf_hash_table(p) \
2785   ((struct ppc_elf_link_hash_table *) (p)->hash)
2786
2787 /* Create an entry in a PPC ELF linker hash table.  */
2788
2789 static struct bfd_hash_entry *
2790 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2791                            struct bfd_hash_table *table,
2792                            const char *string)
2793 {
2794   /* Allocate the structure if it has not already been allocated by a
2795      subclass.  */
2796   if (entry == NULL)
2797     {
2798       entry = bfd_hash_allocate (table,
2799                                  sizeof (struct ppc_elf_link_hash_entry));
2800       if (entry == NULL)
2801         return entry;
2802     }
2803
2804   /* Call the allocation method of the superclass.  */
2805   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2806   if (entry != NULL)
2807     {
2808       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2809       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2810       ppc_elf_hash_entry (entry)->tls_mask = 0;
2811     }
2812
2813   return entry;
2814 }
2815
2816 /* Create a PPC ELF linker hash table.  */
2817
2818 static struct bfd_link_hash_table *
2819 ppc_elf_link_hash_table_create (bfd *abfd)
2820 {
2821   struct ppc_elf_link_hash_table *ret;
2822
2823   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2824   if (ret == NULL)
2825     return NULL;
2826
2827   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2828                                       ppc_elf_link_hash_newfunc,
2829                                       sizeof (struct ppc_elf_link_hash_entry)))
2830     {
2831       free (ret);
2832       return NULL;
2833     }
2834
2835   ret->elf.init_plt_refcount.refcount = 0;
2836   ret->elf.init_plt_refcount.glist = NULL;
2837   ret->elf.init_plt_offset.offset = 0;
2838   ret->elf.init_plt_offset.glist = NULL;
2839
2840   ret->sdata[0].name = ".sdata";
2841   ret->sdata[0].sym_name = "_SDA_BASE_";
2842   ret->sdata[0].bss_name = ".sbss";
2843
2844   ret->sdata[1].name = ".sdata2";
2845   ret->sdata[1].sym_name = "_SDA2_BASE_";
2846   ret->sdata[1].bss_name = ".sbss2";
2847
2848   ret->plt_entry_size = 12;
2849   ret->plt_slot_size = 8;
2850   ret->plt_initial_entry_size = 72;
2851
2852   return &ret->elf.root;
2853 }
2854
2855 /* Create .got and the related sections.  */
2856
2857 static bfd_boolean
2858 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2859 {
2860   struct ppc_elf_link_hash_table *htab;
2861   asection *s;
2862   flagword flags;
2863
2864   if (!_bfd_elf_create_got_section (abfd, info))
2865     return FALSE;
2866
2867   htab = ppc_elf_hash_table (info);
2868   htab->got = s = bfd_get_section_by_name (abfd, ".got");
2869   if (s == NULL)
2870     abort ();
2871
2872   if (htab->is_vxworks)
2873     {
2874       htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2875       if (!htab->sgotplt)
2876         abort ();
2877     }
2878   else
2879     {
2880       /* The powerpc .got has a blrl instruction in it.  Mark it
2881          executable.  */
2882       flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2883                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2884       if (!bfd_set_section_flags (abfd, s, flags))
2885         return FALSE;
2886     }
2887
2888   htab->relgot = bfd_get_section_by_name (abfd, ".rela.got");
2889   if (!htab->relgot)
2890     abort ();
2891
2892   return TRUE;
2893 }
2894
2895 static bfd_boolean
2896 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2897 {
2898   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2899   asection *s;
2900   flagword flags;
2901
2902   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2903            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2904   s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2905   htab->glink = s;
2906   if (s == NULL
2907       || !bfd_set_section_alignment (abfd, s, 4))
2908     return FALSE;
2909
2910   flags = SEC_ALLOC | SEC_LINKER_CREATED;
2911   s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2912   htab->iplt = s;
2913   if (s == NULL
2914       || !bfd_set_section_alignment (abfd, s, 4))
2915     return FALSE;
2916
2917   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2918            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2919   s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags);
2920   htab->reliplt = s;
2921   if (s == NULL
2922       || ! bfd_set_section_alignment (abfd, s, 2))
2923     return FALSE;
2924   return TRUE;
2925 }
2926
2927 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2928    to output sections (just like _bfd_elf_create_dynamic_sections has
2929    to create .dynbss and .rela.bss).  */
2930
2931 static bfd_boolean
2932 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2933 {
2934   struct ppc_elf_link_hash_table *htab;
2935   asection *s;
2936   flagword flags;
2937
2938   htab = ppc_elf_hash_table (info);
2939
2940   if (htab->got == NULL
2941       && !ppc_elf_create_got (abfd, info))
2942     return FALSE;
2943
2944   if (!_bfd_elf_create_dynamic_sections (abfd, info))
2945     return FALSE;
2946
2947   if (htab->glink == NULL
2948       && !ppc_elf_create_glink (abfd, info))
2949     return FALSE;
2950
2951   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2952   s = bfd_make_section_with_flags (abfd, ".dynsbss",
2953                                    SEC_ALLOC | SEC_LINKER_CREATED);
2954   htab->dynsbss = s;
2955   if (s == NULL)
2956     return FALSE;
2957
2958   if (! info->shared)
2959     {
2960       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2961       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2962                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2963       s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2964       htab->relsbss = s;
2965       if (s == NULL
2966           || ! bfd_set_section_alignment (abfd, s, 2))
2967         return FALSE;
2968     }
2969
2970   if (htab->is_vxworks
2971       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2972     return FALSE;
2973
2974   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2975   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2976   if (s == NULL)
2977     abort ();
2978
2979   flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2980   if (htab->plt_type == PLT_VXWORKS)
2981     /* The VxWorks PLT is a loaded section with contents.  */
2982     flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2983   return bfd_set_section_flags (abfd, s, flags);
2984 }
2985
2986 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2987
2988 static void
2989 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2990                               struct elf_link_hash_entry *dir,
2991                               struct elf_link_hash_entry *ind)
2992 {
2993   struct ppc_elf_link_hash_entry *edir, *eind;
2994
2995   edir = (struct ppc_elf_link_hash_entry *) dir;
2996   eind = (struct ppc_elf_link_hash_entry *) ind;
2997
2998   if (eind->dyn_relocs != NULL)
2999     {
3000       if (edir->dyn_relocs != NULL)
3001         {
3002           struct ppc_elf_dyn_relocs **pp;
3003           struct ppc_elf_dyn_relocs *p;
3004
3005           /* Add reloc counts against the indirect sym to the direct sym
3006              list.  Merge any entries against the same section.  */
3007           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3008             {
3009               struct ppc_elf_dyn_relocs *q;
3010
3011               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3012                 if (q->sec == p->sec)
3013                   {
3014                     q->pc_count += p->pc_count;
3015                     q->count += p->count;
3016                     *pp = p->next;
3017                     break;
3018                   }
3019               if (q == NULL)
3020                 pp = &p->next;
3021             }
3022           *pp = edir->dyn_relocs;
3023         }
3024
3025       edir->dyn_relocs = eind->dyn_relocs;
3026       eind->dyn_relocs = NULL;
3027     }
3028
3029   edir->tls_mask |= eind->tls_mask;
3030   edir->has_sda_refs |= eind->has_sda_refs;
3031
3032   /* If called to transfer flags for a weakdef during processing
3033      of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3034      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
3035   if (!(ELIMINATE_COPY_RELOCS
3036         && eind->elf.root.type != bfd_link_hash_indirect
3037         && edir->elf.dynamic_adjusted))
3038     edir->elf.non_got_ref |= eind->elf.non_got_ref;
3039
3040   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3041   edir->elf.ref_regular |= eind->elf.ref_regular;
3042   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3043   edir->elf.needs_plt |= eind->elf.needs_plt;
3044   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3045
3046   /* If we were called to copy over info for a weak sym, that's all.  */
3047   if (eind->elf.root.type != bfd_link_hash_indirect)
3048     return;
3049
3050   /* Copy over the GOT refcount entries that we may have already seen to
3051      the symbol which just became indirect.  */
3052   edir->elf.got.refcount += eind->elf.got.refcount;
3053   eind->elf.got.refcount = 0;
3054
3055   /* And plt entries.  */
3056   if (eind->elf.plt.plist != NULL)
3057     {
3058       if (edir->elf.plt.plist != NULL)
3059         {
3060           struct plt_entry **entp;
3061           struct plt_entry *ent;
3062
3063           for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3064             {
3065               struct plt_entry *dent;
3066
3067               for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3068                 if (dent->sec == ent->sec && dent->addend == ent->addend)
3069                   {
3070                     dent->plt.refcount += ent->plt.refcount;
3071                     *entp = ent->next;
3072                     break;
3073                   }
3074               if (dent == NULL)
3075                 entp = &ent->next;
3076             }
3077           *entp = edir->elf.plt.plist;
3078         }
3079
3080       edir->elf.plt.plist = eind->elf.plt.plist;
3081       eind->elf.plt.plist = NULL;
3082     }
3083
3084   if (eind->elf.dynindx != -1)
3085     {
3086       if (edir->elf.dynindx != -1)
3087         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3088                                 edir->elf.dynstr_index);
3089       edir->elf.dynindx = eind->elf.dynindx;
3090       edir->elf.dynstr_index = eind->elf.dynstr_index;
3091       eind->elf.dynindx = -1;
3092       eind->elf.dynstr_index = 0;
3093     }
3094 }
3095
3096 /* Hook called by the linker routine which adds symbols from an object
3097    file.  We use it to put .comm items in .sbss, and not .bss.  */
3098
3099 static bfd_boolean
3100 ppc_elf_add_symbol_hook (bfd *abfd,
3101                          struct bfd_link_info *info,
3102                          Elf_Internal_Sym *sym,
3103                          const char **namep ATTRIBUTE_UNUSED,
3104                          flagword *flagsp ATTRIBUTE_UNUSED,
3105                          asection **secp,
3106                          bfd_vma *valp)
3107 {
3108   if (sym->st_shndx == SHN_COMMON
3109       && !info->relocatable
3110       && is_ppc_elf (info->output_bfd)
3111       && sym->st_size <= elf_gp_size (abfd))
3112     {
3113       /* Common symbols less than or equal to -G nn bytes are automatically
3114          put into .sbss.  */
3115       struct ppc_elf_link_hash_table *htab;
3116
3117       htab = ppc_elf_hash_table (info);
3118       if (htab->sbss == NULL)
3119         {
3120           flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3121
3122           if (!htab->elf.dynobj)
3123             htab->elf.dynobj = abfd;
3124
3125           htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3126                                                            ".sbss",
3127                                                            flags);
3128           if (htab->sbss == NULL)
3129             return FALSE;
3130         }
3131
3132       *secp = htab->sbss;
3133       *valp = sym->st_size;
3134     }
3135
3136   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3137     elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3138
3139   return TRUE;
3140 }
3141 \f
3142 static bfd_boolean
3143 create_sdata_sym (struct bfd_link_info *info, elf_linker_section_t *lsect)
3144 {
3145   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3146
3147   lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
3148                                      TRUE, FALSE, TRUE);
3149   if (lsect->sym == NULL)
3150     return FALSE;
3151   if (lsect->sym->root.type == bfd_link_hash_new)
3152     lsect->sym->non_elf = 0;
3153   lsect->sym->ref_regular = 1;
3154   _bfd_elf_link_hash_hide_symbol (info, lsect->sym, TRUE);
3155   return TRUE;
3156 }
3157
3158 /* Create a special linker section.  */
3159
3160 static bfd_boolean
3161 ppc_elf_create_linker_section (bfd *abfd,
3162                                struct bfd_link_info *info,
3163                                flagword flags,
3164                                elf_linker_section_t *lsect)
3165 {
3166   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3167   asection *s;
3168
3169   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3170             | SEC_LINKER_CREATED);
3171
3172   /* Record the first bfd that needs the special sections.  */
3173   if (!htab->elf.dynobj)
3174     htab->elf.dynobj = abfd;
3175
3176   s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3177                                           lsect->name,
3178                                           flags);
3179   if (s == NULL
3180       || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
3181     return FALSE;
3182   lsect->section = s;
3183
3184   return create_sdata_sym (info, lsect);
3185 }
3186
3187 /* Find a linker generated pointer with a given addend and type.  */
3188
3189 static elf_linker_section_pointers_t *
3190 elf_find_pointer_linker_section
3191   (elf_linker_section_pointers_t *linker_pointers,
3192    bfd_vma addend,
3193    elf_linker_section_t *lsect)
3194 {
3195   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
3196     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
3197       return linker_pointers;
3198
3199   return NULL;
3200 }
3201
3202 /* Allocate a pointer to live in a linker created section.  */
3203
3204 static bfd_boolean
3205 elf_create_pointer_linker_section (bfd *abfd,
3206                                    elf_linker_section_t *lsect,
3207                                    struct elf_link_hash_entry *h,
3208                                    const Elf_Internal_Rela *rel)
3209 {
3210   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
3211   elf_linker_section_pointers_t *linker_section_ptr;
3212   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3213   bfd_size_type amt;
3214
3215   BFD_ASSERT (lsect != NULL);
3216
3217   /* Is this a global symbol?  */
3218   if (h != NULL)
3219     {
3220       struct ppc_elf_link_hash_entry *eh;
3221
3222       /* Has this symbol already been allocated?  If so, our work is done.  */
3223       eh = (struct ppc_elf_link_hash_entry *) h;
3224       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
3225                                            rel->r_addend,
3226                                            lsect))
3227         return TRUE;
3228
3229       ptr_linker_section_ptr = &eh->linker_section_pointer;
3230     }
3231   else
3232     {
3233       BFD_ASSERT (is_ppc_elf (abfd));
3234
3235       /* Allocation of a pointer to a local symbol.  */
3236       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
3237
3238       /* Allocate a table to hold the local symbols if first time.  */
3239       if (!ptr)
3240         {
3241           unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
3242
3243           amt = num_symbols;
3244           amt *= sizeof (elf_linker_section_pointers_t *);
3245           ptr = bfd_zalloc (abfd, amt);
3246
3247           if (!ptr)
3248             return FALSE;
3249
3250           elf_local_ptr_offsets (abfd) = ptr;
3251         }
3252
3253       /* Has this symbol already been allocated?  If so, our work is done.  */
3254       if (elf_find_pointer_linker_section (ptr[r_symndx],
3255                                            rel->r_addend,
3256                                            lsect))
3257         return TRUE;
3258
3259       ptr_linker_section_ptr = &ptr[r_symndx];
3260     }
3261
3262   /* Allocate space for a pointer in the linker section, and allocate
3263      a new pointer record from internal memory.  */
3264   BFD_ASSERT (ptr_linker_section_ptr != NULL);
3265   amt = sizeof (elf_linker_section_pointers_t);
3266   linker_section_ptr = bfd_alloc (abfd, amt);
3267
3268   if (!linker_section_ptr)
3269     return FALSE;
3270
3271   linker_section_ptr->next = *ptr_linker_section_ptr;
3272   linker_section_ptr->addend = rel->r_addend;
3273   linker_section_ptr->lsect = lsect;
3274   *ptr_linker_section_ptr = linker_section_ptr;
3275
3276   linker_section_ptr->offset = lsect->section->size;
3277   lsect->section->size += 4;
3278
3279 #ifdef DEBUG
3280   fprintf (stderr,
3281            "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
3282            lsect->name, (long) linker_section_ptr->offset,
3283            (long) lsect->section->size);
3284 #endif
3285
3286   return TRUE;
3287 }
3288
3289 static struct plt_entry **
3290 update_local_sym_info (bfd *abfd,
3291                        Elf_Internal_Shdr *symtab_hdr,
3292                        unsigned long r_symndx,
3293                        int tls_type)
3294 {
3295   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3296   struct plt_entry **local_plt;
3297   char *local_got_tls_masks;
3298
3299   if (local_got_refcounts == NULL)
3300     {
3301       bfd_size_type size = symtab_hdr->sh_info;
3302
3303       size *= (sizeof (*local_got_refcounts)
3304                + sizeof (*local_plt)
3305                + sizeof (*local_got_tls_masks));
3306       local_got_refcounts = bfd_zalloc (abfd, size);
3307       if (local_got_refcounts == NULL)
3308         return NULL;
3309       elf_local_got_refcounts (abfd) = local_got_refcounts;
3310     }
3311
3312   local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
3313   local_got_tls_masks = (char *) (local_plt + symtab_hdr->sh_info);
3314   local_got_tls_masks[r_symndx] |= tls_type;
3315   if (tls_type != PLT_IFUNC)
3316     local_got_refcounts[r_symndx] += 1;
3317   return local_plt + r_symndx;
3318 }
3319
3320 static bfd_boolean
3321 update_plt_info (bfd *abfd, struct plt_entry **plist,
3322                  asection *sec, bfd_vma addend)
3323 {
3324   struct plt_entry *ent;
3325
3326   for (ent = *plist; ent != NULL; ent = ent->next)
3327     if (ent->sec == sec && ent->addend == addend)
3328       break;
3329   if (ent == NULL)
3330     {
3331       bfd_size_type amt = sizeof (*ent);
3332       ent = bfd_alloc (abfd, amt);
3333       if (ent == NULL)
3334         return FALSE;
3335       ent->next = *plist;
3336       ent->sec = sec;
3337       ent->addend = addend;
3338       ent->plt.refcount = 0;
3339       *plist = ent;
3340     }
3341   ent->plt.refcount += 1;
3342   return TRUE;
3343 }
3344
3345 static struct plt_entry *
3346 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
3347 {
3348   struct plt_entry *ent;
3349
3350   if (addend < 32768)
3351     sec = NULL;
3352   for (ent = *plist; ent != NULL; ent = ent->next)
3353     if (ent->sec == sec && ent->addend == addend)
3354       break;
3355   return ent;
3356 }
3357
3358 static bfd_boolean
3359 is_branch_reloc (enum elf_ppc_reloc_type r_type)
3360 {
3361   return (r_type == R_PPC_PLTREL24
3362           || r_type == R_PPC_LOCAL24PC
3363           || r_type == R_PPC_REL24
3364           || r_type == R_PPC_REL14
3365           || r_type == R_PPC_REL14_BRTAKEN
3366           || r_type == R_PPC_REL14_BRNTAKEN
3367           || r_type == R_PPC_ADDR24
3368           || r_type == R_PPC_ADDR14
3369           || r_type == R_PPC_ADDR14_BRTAKEN
3370           || r_type == R_PPC_ADDR14_BRNTAKEN);
3371 }
3372
3373 static void
3374 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3375 {
3376   (*_bfd_error_handler)
3377     (_("%B: relocation %s cannot be used when making a shared object"),
3378      abfd,
3379      ppc_elf_howto_table[r_type]->name);
3380   bfd_set_error (bfd_error_bad_value);
3381 }
3382
3383 /* Look through the relocs for a section during the first phase, and
3384    allocate space in the global offset table or procedure linkage
3385    table.  */
3386
3387 static bfd_boolean
3388 ppc_elf_check_relocs (bfd *abfd,
3389                       struct bfd_link_info *info,
3390                       asection *sec,
3391                       const Elf_Internal_Rela *relocs)
3392 {
3393   struct ppc_elf_link_hash_table *htab;
3394   Elf_Internal_Shdr *symtab_hdr;
3395   struct elf_link_hash_entry **sym_hashes;
3396   const Elf_Internal_Rela *rel;
3397   const Elf_Internal_Rela *rel_end;
3398   asection *got2, *sreloc;
3399   struct elf_link_hash_entry *tga;
3400
3401   if (info->relocatable)
3402     return TRUE;
3403
3404   /* Don't do anything special with non-loaded, non-alloced sections.
3405      In particular, any relocs in such sections should not affect GOT
3406      and PLT reference counting (ie. we don't allow them to create GOT
3407      or PLT entries), there's no possibility or desire to optimize TLS
3408      relocs, and there's not much point in propagating relocs to shared
3409      libs that the dynamic linker won't relocate.  */
3410   if ((sec->flags & SEC_ALLOC) == 0)
3411     return TRUE;
3412
3413 #ifdef DEBUG
3414   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3415                       sec, abfd);
3416 #endif
3417
3418   BFD_ASSERT (is_ppc_elf (abfd));
3419
3420   /* Initialize howto table if not already done.  */
3421   if (!ppc_elf_howto_table[R_PPC_ADDR32])
3422     ppc_elf_howto_init ();
3423
3424   htab = ppc_elf_hash_table (info);
3425   if (htab->glink == NULL)
3426     {
3427       if (htab->elf.dynobj == NULL)
3428         htab->elf.dynobj = abfd;
3429       if (!ppc_elf_create_glink (htab->elf.dynobj, info))
3430         return FALSE;
3431     }
3432   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3433                               FALSE, FALSE, TRUE);
3434   symtab_hdr = &elf_symtab_hdr (abfd);
3435   sym_hashes = elf_sym_hashes (abfd);
3436   got2 = bfd_get_section_by_name (abfd, ".got2");
3437   sreloc = NULL;
3438
3439   rel_end = relocs + sec->reloc_count;
3440   for (rel = relocs; rel < rel_end; rel++)
3441     {
3442       unsigned long r_symndx;
3443       enum elf_ppc_reloc_type r_type;
3444       struct elf_link_hash_entry *h;
3445       int tls_type;
3446       struct plt_entry **ifunc;
3447
3448       r_symndx = ELF32_R_SYM (rel->r_info);
3449       if (r_symndx < symtab_hdr->sh_info)
3450         h = NULL;
3451       else
3452         {
3453           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3454           while (h->root.type == bfd_link_hash_indirect
3455                  || h->root.type == bfd_link_hash_warning)
3456             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3457         }
3458
3459       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3460          This shows up in particular in an R_PPC_ADDR32 in the eabi
3461          startup code.  */
3462       if (h != NULL
3463           && htab->got == NULL
3464           && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3465         {
3466           if (htab->elf.dynobj == NULL)
3467             htab->elf.dynobj = abfd;
3468           if (!ppc_elf_create_got (htab->elf.dynobj, info))
3469             return FALSE;
3470           BFD_ASSERT (h == htab->elf.hgot);
3471         }
3472
3473       tls_type = 0;
3474       ifunc = NULL;
3475       r_type = ELF32_R_TYPE (rel->r_info);
3476       if (!htab->is_vxworks)
3477         {
3478           if (h != NULL)
3479             {
3480               if (h->type == STT_GNU_IFUNC)
3481                 ifunc = &h->plt.plist;
3482             }
3483           else
3484             {
3485               Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3486                                                               abfd, r_symndx);
3487               if (isym == NULL)
3488                 return FALSE;
3489
3490               if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3491                   && (!info->shared
3492                       || is_branch_reloc (r_type)))
3493                 {
3494                   bfd_vma addend;
3495
3496                   ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3497                                                  PLT_IFUNC);
3498                   if (ifunc == NULL)
3499                     return FALSE;
3500
3501                   /* STT_GNU_IFUNC symbols must have a PLT entry;
3502                      In a non-pie executable even when there are
3503                      no plt calls.  */
3504                   addend = 0;
3505                   if (r_type == R_PPC_PLTREL24)
3506                     {
3507                       ppc_elf_tdata (abfd)->makes_plt_call = 1;
3508                       if (info->shared)
3509                         addend = rel->r_addend;
3510                     }
3511                   if (!update_plt_info (abfd, ifunc,
3512                                         addend < 32768 ? NULL : got2, addend))
3513                     return FALSE;
3514                 }
3515             }
3516         }
3517
3518       if (!htab->is_vxworks
3519           && is_branch_reloc (r_type)
3520           && h != NULL
3521           && h == tga)
3522         {
3523           if (rel != relocs
3524               && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3525                   || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3526             /* We have a new-style __tls_get_addr call with a marker
3527                reloc.  */
3528             ;
3529           else
3530             /* Mark this section as having an old-style call.  */
3531             sec->has_tls_get_addr_call = 1;
3532         }
3533
3534       switch (r_type)
3535         {
3536         case R_PPC_TLSGD:
3537         case R_PPC_TLSLD:
3538           /* These special tls relocs tie a call to __tls_get_addr with
3539              its parameter symbol.  */
3540           break;
3541
3542         case R_PPC_GOT_TLSLD16:
3543         case R_PPC_GOT_TLSLD16_LO:
3544         case R_PPC_GOT_TLSLD16_HI:
3545         case R_PPC_GOT_TLSLD16_HA:
3546           tls_type = TLS_TLS | TLS_LD;
3547           goto dogottls;
3548
3549         case R_PPC_GOT_TLSGD16:
3550         case R_PPC_GOT_TLSGD16_LO:
3551         case R_PPC_GOT_TLSGD16_HI:
3552         case R_PPC_GOT_TLSGD16_HA:
3553           tls_type = TLS_TLS | TLS_GD;
3554           goto dogottls;
3555
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           if (!info->executable)
3561             info->flags |= DF_STATIC_TLS;
3562           tls_type = TLS_TLS | TLS_TPREL;
3563           goto dogottls;
3564
3565         case R_PPC_GOT_DTPREL16:
3566         case R_PPC_GOT_DTPREL16_LO:
3567         case R_PPC_GOT_DTPREL16_HI:
3568         case R_PPC_GOT_DTPREL16_HA:
3569           tls_type = TLS_TLS | TLS_DTPREL;
3570         dogottls:
3571           sec->has_tls_reloc = 1;
3572           /* Fall thru */
3573
3574           /* GOT16 relocations */
3575         case R_PPC_GOT16:
3576         case R_PPC_GOT16_LO:
3577         case R_PPC_GOT16_HI:
3578         case R_PPC_GOT16_HA:
3579           /* This symbol requires a global offset table entry.  */
3580           if (htab->got == NULL)
3581             {
3582               if (htab->elf.dynobj == NULL)
3583                 htab->elf.dynobj = abfd;
3584               if (!ppc_elf_create_got (htab->elf.dynobj, info))
3585                 return FALSE;
3586             }
3587           if (h != NULL)
3588             {
3589               h->got.refcount += 1;
3590               ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3591             }
3592           else
3593             /* This is a global offset table entry for a local symbol.  */
3594             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3595               return FALSE;
3596
3597           /* We may also need a plt entry if the symbol turns out to be
3598              an ifunc.  */
3599           if (h != NULL && !info->shared)
3600             {
3601               if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3602                 return FALSE;
3603             }
3604           break;
3605
3606           /* Indirect .sdata relocation.  */
3607         case R_PPC_EMB_SDAI16:
3608           if (info->shared)
3609             {
3610               bad_shared_reloc (abfd, r_type);
3611               return FALSE;
3612             }
3613           if (htab->sdata[0].section == NULL
3614               && !ppc_elf_create_linker_section (abfd, info, 0,
3615                                                  &htab->sdata[0]))
3616             return FALSE;
3617           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3618                                                   h, rel))
3619             return FALSE;
3620           if (h != NULL)
3621             {
3622               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3623               h->non_got_ref = TRUE;
3624             }
3625           break;
3626
3627           /* Indirect .sdata2 relocation.  */
3628         case R_PPC_EMB_SDA2I16:
3629           if (info->shared)
3630             {
3631               bad_shared_reloc (abfd, r_type);
3632               return FALSE;
3633             }
3634           if (htab->sdata[1].section == NULL
3635               && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3636                                                  &htab->sdata[1]))
3637             return FALSE;
3638           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3639                                                   h, rel))
3640             return FALSE;
3641           if (h != NULL)
3642             {
3643               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3644               h->non_got_ref = TRUE;
3645             }
3646           break;
3647
3648         case R_PPC_SDAREL16:
3649           if (htab->sdata[0].sym == NULL
3650               && !create_sdata_sym (info, &htab->sdata[0]))
3651             return FALSE;
3652           if (h != NULL)
3653             {
3654               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3655               h->non_got_ref = TRUE;
3656             }
3657           break;
3658
3659         case R_PPC_EMB_SDA2REL:
3660           if (info->shared)
3661             {
3662               bad_shared_reloc (abfd, r_type);
3663               return FALSE;
3664             }
3665           if (htab->sdata[1].sym == NULL
3666               && !create_sdata_sym (info, &htab->sdata[1]))
3667             return FALSE;
3668           if (h != NULL)
3669             {
3670               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3671               h->non_got_ref = TRUE;
3672             }
3673           break;
3674
3675         case R_PPC_EMB_SDA21:
3676         case R_PPC_EMB_RELSDA:
3677           if (info->shared)
3678             {
3679               bad_shared_reloc (abfd, r_type);
3680               return FALSE;
3681             }
3682           if (htab->sdata[0].sym == NULL
3683               && !create_sdata_sym (info, &htab->sdata[0]))
3684             return FALSE;
3685           if (htab->sdata[1].sym == NULL
3686               && !create_sdata_sym (info, &htab->sdata[1]))
3687             return FALSE;
3688           if (h != NULL)
3689             {
3690               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3691               h->non_got_ref = TRUE;
3692             }
3693           break;
3694
3695         case R_PPC_EMB_NADDR32:
3696         case R_PPC_EMB_NADDR16:
3697         case R_PPC_EMB_NADDR16_LO:
3698         case R_PPC_EMB_NADDR16_HI:
3699         case R_PPC_EMB_NADDR16_HA:
3700           if (info->shared)
3701             {
3702               bad_shared_reloc (abfd, r_type);
3703               return FALSE;
3704             }
3705           if (h != NULL)
3706             h->non_got_ref = TRUE;
3707           break;
3708
3709         case R_PPC_PLTREL24:
3710           if (h == NULL)
3711             break;
3712           /* Fall through */
3713         case R_PPC_PLT32:
3714         case R_PPC_PLTREL32:
3715         case R_PPC_PLT16_LO:
3716         case R_PPC_PLT16_HI:
3717         case R_PPC_PLT16_HA:
3718 #ifdef DEBUG
3719           fprintf (stderr, "Reloc requires a PLT entry\n");
3720 #endif
3721           /* This symbol requires a procedure linkage table entry.  We
3722              actually build the entry in finish_dynamic_symbol,
3723              because this might be a case of linking PIC code without
3724              linking in any dynamic objects, in which case we don't
3725              need to generate a procedure linkage table after all.  */
3726
3727           if (h == NULL)
3728             {
3729               /* It does not make sense to have a procedure linkage
3730                  table entry for a local symbol.  */
3731               (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3732                                        "local symbol"),
3733                                      abfd,
3734                                      sec,
3735                                      (long) rel->r_offset,
3736                                      ppc_elf_howto_table[r_type]->name);
3737               bfd_set_error (bfd_error_bad_value);
3738               return FALSE;
3739             }
3740           else
3741             {
3742               bfd_vma addend = 0;
3743
3744               if (r_type == R_PPC_PLTREL24)
3745                 {
3746                   ppc_elf_tdata (abfd)->makes_plt_call = 1;
3747                   if (info->shared)
3748                     addend = rel->r_addend;
3749                 }
3750               h->needs_plt = 1;
3751               if (!update_plt_info (abfd, &h->plt.plist,
3752                                     addend < 32768 ? NULL : got2, addend))
3753                 return FALSE;
3754             }
3755           break;
3756
3757           /* The following relocations don't need to propagate the
3758              relocation if linking a shared object since they are
3759              section relative.  */
3760         case R_PPC_SECTOFF:
3761         case R_PPC_SECTOFF_LO:
3762         case R_PPC_SECTOFF_HI:
3763         case R_PPC_SECTOFF_HA:
3764         case R_PPC_DTPREL16:
3765         case R_PPC_DTPREL16_LO:
3766         case R_PPC_DTPREL16_HI:
3767         case R_PPC_DTPREL16_HA:
3768         case R_PPC_TOC16:
3769           break;
3770
3771         case R_PPC_REL16:
3772         case R_PPC_REL16_LO:
3773         case R_PPC_REL16_HI:
3774         case R_PPC_REL16_HA:
3775           ppc_elf_tdata (abfd)->has_rel16 = 1;
3776           break;
3777
3778           /* These are just markers.  */
3779         case R_PPC_TLS:
3780         case R_PPC_EMB_MRKREF:
3781         case R_PPC_NONE:
3782         case R_PPC_max:
3783         case R_PPC_RELAX32:
3784         case R_PPC_RELAX32PC:
3785         case R_PPC_RELAX32_PLT:
3786         case R_PPC_RELAX32PC_PLT:
3787           break;
3788
3789           /* These should only appear in dynamic objects.  */
3790         case R_PPC_COPY:
3791         case R_PPC_GLOB_DAT:
3792         case R_PPC_JMP_SLOT:
3793         case R_PPC_RELATIVE:
3794         case R_PPC_IRELATIVE:
3795           break;
3796
3797           /* These aren't handled yet.  We'll report an error later.  */
3798         case R_PPC_ADDR30:
3799         case R_PPC_EMB_RELSEC16:
3800         case R_PPC_EMB_RELST_LO:
3801         case R_PPC_EMB_RELST_HI:
3802         case R_PPC_EMB_RELST_HA:
3803         case R_PPC_EMB_BIT_FLD:
3804           break;
3805
3806           /* This refers only to functions defined in the shared library.  */
3807         case R_PPC_LOCAL24PC:
3808           if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3809             {
3810               htab->plt_type = PLT_OLD;
3811               htab->old_bfd = abfd;
3812             }
3813           break;
3814
3815           /* This relocation describes the C++ object vtable hierarchy.
3816              Reconstruct it for later use during GC.  */
3817         case R_PPC_GNU_VTINHERIT:
3818           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3819             return FALSE;
3820           break;
3821
3822           /* This relocation describes which C++ vtable entries are actually
3823              used.  Record for later use during GC.  */
3824         case R_PPC_GNU_VTENTRY:
3825           BFD_ASSERT (h != NULL);
3826           if (h != NULL
3827               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3828             return FALSE;
3829           break;
3830
3831           /* We shouldn't really be seeing these.  */
3832         case R_PPC_TPREL32:
3833         case R_PPC_TPREL16:
3834         case R_PPC_TPREL16_LO:
3835         case R_PPC_TPREL16_HI:
3836         case R_PPC_TPREL16_HA:
3837           if (!info->executable)
3838             info->flags |= DF_STATIC_TLS;
3839           goto dodyn;
3840
3841           /* Nor these.  */
3842         case R_PPC_DTPMOD32:
3843         case R_PPC_DTPREL32:
3844           goto dodyn;
3845
3846         case R_PPC_REL32:
3847           if (h == NULL
3848               && got2 != NULL
3849               && (sec->flags & SEC_CODE) != 0
3850               && info->shared
3851               && htab->plt_type == PLT_UNSET)
3852             {
3853               /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3854                  the start of a function, which assembles to a REL32
3855                  reference to .got2.  If we detect one of these, then
3856                  force the old PLT layout because the linker cannot
3857                  reliably deduce the GOT pointer value needed for
3858                  PLT call stubs.  */
3859               asection *s;
3860               Elf_Internal_Sym *isym;
3861
3862               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3863                                             abfd, r_symndx);
3864               if (isym == NULL)
3865                 return FALSE;
3866
3867               s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3868               if (s == got2)
3869                 {
3870                   htab->plt_type = PLT_OLD;
3871                   htab->old_bfd = abfd;
3872                 }
3873             }
3874           if (h == NULL || h == htab->elf.hgot)
3875             break;
3876           /* fall through */
3877
3878         case R_PPC_ADDR32:
3879         case R_PPC_ADDR16:
3880         case R_PPC_ADDR16_LO:
3881         case R_PPC_ADDR16_HI:
3882         case R_PPC_ADDR16_HA:
3883         case R_PPC_UADDR32:
3884         case R_PPC_UADDR16:
3885           if (h != NULL && !info->shared)
3886             {
3887               /* We may need a plt entry if the symbol turns out to be
3888                  a function defined in a dynamic object.  */
3889               if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3890                 return FALSE;
3891
3892               /* We may need a copy reloc too.  */
3893               h->non_got_ref = 1;
3894               h->pointer_equality_needed = 1;
3895             }
3896           goto dodyn;
3897
3898         case R_PPC_REL24:
3899         case R_PPC_REL14:
3900         case R_PPC_REL14_BRTAKEN:
3901         case R_PPC_REL14_BRNTAKEN:
3902           if (h == NULL)
3903             break;
3904           if (h == htab->elf.hgot)
3905             {
3906               if (htab->plt_type == PLT_UNSET)
3907                 {
3908                   htab->plt_type = PLT_OLD;
3909                   htab->old_bfd = abfd;
3910                 }
3911               break;
3912             }
3913           /* fall through */
3914
3915         case R_PPC_ADDR24:
3916         case R_PPC_ADDR14:
3917         case R_PPC_ADDR14_BRTAKEN:
3918         case R_PPC_ADDR14_BRNTAKEN:
3919           if (h != NULL && !info->shared)
3920             {
3921               /* We may need a plt entry if the symbol turns out to be
3922                  a function defined in a dynamic object.  */
3923               h->needs_plt = 1;
3924               if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3925                 return FALSE;
3926               break;
3927             }
3928
3929         dodyn:
3930           /* If we are creating a shared library, and this is a reloc
3931              against a global symbol, or a non PC relative reloc
3932              against a local symbol, then we need to copy the reloc
3933              into the shared library.  However, if we are linking with
3934              -Bsymbolic, we do not need to copy a reloc against a
3935              global symbol which is defined in an object we are
3936              including in the link (i.e., DEF_REGULAR is set).  At
3937              this point we have not seen all the input files, so it is
3938              possible that DEF_REGULAR is not set now but will be set
3939              later (it is never cleared).  In case of a weak definition,
3940              DEF_REGULAR may be cleared later by a strong definition in
3941              a shared library.  We account for that possibility below by
3942              storing information in the dyn_relocs field of the hash
3943              table entry.  A similar situation occurs when creating
3944              shared libraries and symbol visibility changes render the
3945              symbol local.
3946
3947              If on the other hand, we are creating an executable, we
3948              may need to keep relocations for symbols satisfied by a
3949              dynamic library if we manage to avoid copy relocs for the
3950              symbol.  */
3951           if ((info->shared
3952                && (must_be_dyn_reloc (info, r_type)
3953                    || (h != NULL
3954                        && (! info->symbolic
3955                            || h->root.type == bfd_link_hash_defweak
3956                            || !h->def_regular))))
3957               || (ELIMINATE_COPY_RELOCS
3958                   && !info->shared
3959                   && h != NULL
3960                   && (h->root.type == bfd_link_hash_defweak
3961                       || !h->def_regular)))
3962             {
3963               struct ppc_elf_dyn_relocs *p;
3964               struct ppc_elf_dyn_relocs **rel_head;
3965
3966 #ifdef DEBUG
3967               fprintf (stderr,
3968                        "ppc_elf_check_relocs needs to "
3969                        "create relocation for %s\n",
3970                        (h && h->root.root.string
3971                         ? h->root.root.string : "<unknown>"));
3972 #endif
3973               if (sreloc == NULL)
3974                 {
3975                   if (htab->elf.dynobj == NULL)
3976                     htab->elf.dynobj = abfd;
3977
3978                   sreloc = _bfd_elf_make_dynamic_reloc_section
3979                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3980
3981                   if (sreloc == NULL)
3982                     return FALSE;
3983                 }
3984
3985               /* If this is a global symbol, we count the number of
3986                  relocations we need for this symbol.  */
3987               if (h != NULL)
3988                 {
3989                   rel_head = &ppc_elf_hash_entry (h)->dyn_relocs;
3990                 }
3991               else
3992                 {
3993                   /* Track dynamic relocs needed for local syms too.
3994                      We really need local syms available to do this
3995                      easily.  Oh well.  */
3996                   asection *s;
3997                   void *vpp;
3998                   Elf_Internal_Sym *isym;
3999
4000                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4001                                                 abfd, r_symndx);
4002                   if (isym == NULL)
4003                     return FALSE;
4004
4005                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4006                   if (s == NULL)
4007                     s = sec;
4008
4009                   vpp = &elf_section_data (s)->local_dynrel;
4010                   rel_head = (struct ppc_elf_dyn_relocs **) vpp;
4011                 }
4012
4013               p = *rel_head;
4014               if (p == NULL || p->sec != sec)
4015                 {
4016                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4017                   if (p == NULL)
4018                     return FALSE;
4019                   p->next = *rel_head;
4020                   *rel_head = p;
4021                   p->sec = sec;
4022                   p->count = 0;
4023                   p->pc_count = 0;
4024                 }
4025
4026               p->count += 1;
4027               if (!must_be_dyn_reloc (info, r_type))
4028                 p->pc_count += 1;
4029             }
4030
4031           break;
4032         }
4033     }
4034
4035   return TRUE;
4036 }
4037 \f
4038
4039 /* Merge object attributes from IBFD into OBFD.  Raise an error if
4040    there are conflicting attributes.  */
4041 static bfd_boolean
4042 ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
4043 {
4044   obj_attribute *in_attr, *in_attrs;
4045   obj_attribute *out_attr, *out_attrs;
4046
4047   if (!elf_known_obj_attributes_proc (obfd)[0].i)
4048     {
4049       /* This is the first object.  Copy the attributes.  */
4050       _bfd_elf_copy_obj_attributes (ibfd, obfd);
4051
4052       /* Use the Tag_null value to indicate the attributes have been
4053          initialized.  */
4054       elf_known_obj_attributes_proc (obfd)[0].i = 1;
4055
4056       return TRUE;
4057     }
4058
4059   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4060   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4061
4062   /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
4063      non-conflicting ones.  */
4064   in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
4065   out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
4066   if (in_attr->i != out_attr->i)
4067     {
4068       out_attr->type = 1;
4069       if (out_attr->i == 0)
4070         out_attr->i = in_attr->i;
4071       else if (in_attr->i == 0)
4072         ;
4073       else if (out_attr->i == 1 && in_attr->i == 2)
4074         _bfd_error_handler
4075           (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
4076       else if (out_attr->i == 1 && in_attr->i == 3)
4077         _bfd_error_handler
4078           (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"), 
4079           obfd, ibfd);
4080       else if (out_attr->i == 3 && in_attr->i == 1)
4081         _bfd_error_handler
4082           (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"), 
4083           ibfd, obfd);
4084       else if (out_attr->i == 3 && in_attr->i == 2)
4085         _bfd_error_handler
4086           (_("Warning: %B uses soft float, %B uses single-precision hard float"), 
4087           ibfd, obfd);
4088       else if (out_attr->i == 2 && (in_attr->i == 1 || in_attr->i == 3))
4089         _bfd_error_handler
4090           (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
4091       else if (in_attr->i > 3)
4092         _bfd_error_handler
4093           (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
4094            in_attr->i);
4095       else
4096         _bfd_error_handler
4097           (_("Warning: %B uses unknown floating point ABI %d"), obfd,
4098            out_attr->i);
4099     }
4100
4101   /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
4102      merge non-conflicting ones.  */
4103   in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
4104   out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
4105   if (in_attr->i != out_attr->i)
4106     {
4107       const char *in_abi = NULL, *out_abi = NULL;
4108
4109       switch (in_attr->i)
4110         {
4111         case 1: in_abi = "generic"; break;
4112         case 2: in_abi = "AltiVec"; break;
4113         case 3: in_abi = "SPE"; break;
4114         }
4115
4116       switch (out_attr->i)
4117         {
4118         case 1: out_abi = "generic"; break;
4119         case 2: out_abi = "AltiVec"; break;
4120         case 3: out_abi = "SPE"; break;
4121         }
4122
4123       out_attr->type = 1;
4124       if (out_attr->i == 0)
4125         out_attr->i = in_attr->i;
4126       else if (in_attr->i == 0)
4127         ;
4128       /* For now, allow generic to transition to AltiVec or SPE
4129          without a warning.  If GCC marked files with their stack
4130          alignment and used don't-care markings for files which are
4131          not affected by the vector ABI, we could warn about this
4132          case too.  */
4133       else if (out_attr->i == 1)
4134         out_attr->i = in_attr->i;
4135       else if (in_attr->i == 1)
4136         ;
4137       else if (in_abi == NULL)
4138         _bfd_error_handler
4139           (_("Warning: %B uses unknown vector ABI %d"), ibfd,
4140            in_attr->i);
4141       else if (out_abi == NULL)
4142         _bfd_error_handler
4143           (_("Warning: %B uses unknown vector ABI %d"), obfd,
4144            in_attr->i);
4145       else
4146         _bfd_error_handler
4147           (_("Warning: %B uses vector ABI \"%s\", %B uses \"%s\""),
4148            ibfd, obfd, in_abi, out_abi);
4149     }
4150
4151   /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
4152      and merge non-conflicting ones.  */
4153   in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
4154   out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
4155   if (in_attr->i != out_attr->i)
4156     {
4157       out_attr->type = 1;
4158       if (out_attr->i == 0)
4159        out_attr->i = in_attr->i;
4160       else if (in_attr->i == 0)
4161        ;
4162       else if (out_attr->i == 1 && in_attr->i == 2)
4163        _bfd_error_handler
4164          (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), obfd, ibfd);
4165       else if (out_attr->i == 2 && in_attr->i == 1)
4166        _bfd_error_handler
4167          (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), ibfd, obfd);
4168       else if (in_attr->i > 2)
4169        _bfd_error_handler
4170          (_("Warning: %B uses unknown small structure return convention %d"), ibfd,
4171           in_attr->i);
4172       else
4173        _bfd_error_handler
4174          (_("Warning: %B uses unknown small structure return convention %d"), obfd,
4175           out_attr->i);
4176     }
4177
4178   /* Merge Tag_compatibility attributes and any common GNU ones.  */
4179   _bfd_elf_merge_object_attributes (ibfd, obfd);
4180
4181   return TRUE;
4182 }
4183
4184 /* Merge backend specific data from an object file to the output
4185    object file when linking.  */
4186
4187 static bfd_boolean
4188 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4189 {
4190   flagword old_flags;
4191   flagword new_flags;
4192   bfd_boolean error;
4193
4194   if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
4195     return TRUE;
4196
4197   /* Check if we have the same endianess.  */
4198   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4199     return FALSE;
4200
4201   if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
4202     return FALSE;
4203
4204   new_flags = elf_elfheader (ibfd)->e_flags;
4205   old_flags = elf_elfheader (obfd)->e_flags;
4206   if (!elf_flags_init (obfd))
4207     {
4208       /* First call, no flags set.  */
4209       elf_flags_init (obfd) = TRUE;
4210       elf_elfheader (obfd)->e_flags = new_flags;
4211     }
4212
4213   /* Compatible flags are ok.  */
4214   else if (new_flags == old_flags)
4215     ;
4216
4217   /* Incompatible flags.  */
4218   else
4219     {
4220       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
4221          to be linked with either.  */
4222       error = FALSE;
4223       if ((new_flags & EF_PPC_RELOCATABLE) != 0
4224           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
4225         {
4226           error = TRUE;
4227           (*_bfd_error_handler)
4228             (_("%B: compiled with -mrelocatable and linked with "
4229                "modules compiled normally"), ibfd);
4230         }
4231       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
4232                && (old_flags & EF_PPC_RELOCATABLE) != 0)
4233         {
4234           error = TRUE;
4235           (*_bfd_error_handler)
4236             (_("%B: compiled normally and linked with "
4237                "modules compiled with -mrelocatable"), ibfd);
4238         }
4239
4240       /* The output is -mrelocatable-lib iff both the input files are.  */
4241       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
4242         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
4243
4244       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
4245          but each input file is either -mrelocatable or -mrelocatable-lib.  */
4246       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
4247           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
4248           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
4249         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
4250
4251       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
4252          any module uses it.  */
4253       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
4254
4255       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4256       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4257
4258       /* Warn about any other mismatches.  */
4259       if (new_flags != old_flags)
4260         {
4261           error = TRUE;
4262           (*_bfd_error_handler)
4263             (_("%B: uses different e_flags (0x%lx) fields "
4264                "than previous modules (0x%lx)"),
4265              ibfd, (long) new_flags, (long) old_flags);
4266         }
4267
4268       if (error)
4269         {
4270           bfd_set_error (bfd_error_bad_value);
4271           return FALSE;
4272         }
4273     }
4274
4275   return TRUE;
4276 }
4277 \f
4278 /* Choose which PLT scheme to use, and set .plt flags appropriately.
4279    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
4280 int
4281 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
4282                            struct bfd_link_info *info,
4283                            enum ppc_elf_plt_type plt_style,
4284                            int emit_stub_syms)
4285 {
4286   struct ppc_elf_link_hash_table *htab;
4287   flagword flags;
4288
4289   htab = ppc_elf_hash_table (info);
4290
4291   htab->emit_stub_syms = emit_stub_syms;
4292
4293   if (htab->plt_type == PLT_UNSET)
4294     {
4295       if (plt_style == PLT_OLD)
4296         htab->plt_type = PLT_OLD;
4297       else
4298         {
4299           bfd *ibfd;
4300           enum ppc_elf_plt_type plt_type = plt_style;
4301
4302           /* Look through the reloc flags left by ppc_elf_check_relocs.
4303              Use the old style bss plt if a file makes plt calls
4304              without using the new relocs, and if ld isn't given
4305              --secure-plt and we never see REL16 relocs.  */
4306           if (plt_type == PLT_UNSET)
4307             plt_type = PLT_OLD;
4308           for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4309             if (is_ppc_elf (ibfd))
4310               {
4311                 if (ppc_elf_tdata (ibfd)->has_rel16)
4312                   plt_type = PLT_NEW;
4313                 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4314                   {
4315                     plt_type = PLT_OLD;
4316                     htab->old_bfd = ibfd;
4317                     break;
4318                   }
4319               }
4320           htab->plt_type = plt_type;
4321         }
4322     }
4323   if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
4324     info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
4325
4326   BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
4327
4328   if (htab->plt_type == PLT_NEW)
4329     {
4330       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4331                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4332
4333       /* The new PLT is a loaded section.  */
4334       if (htab->plt != NULL
4335           && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
4336         return -1;
4337
4338       /* The new GOT is not executable.  */
4339       if (htab->got != NULL
4340           && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
4341         return -1;
4342     }
4343   else
4344     {
4345       /* Stop an unused .glink section from affecting .text alignment.  */
4346       if (htab->glink != NULL
4347           && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
4348         return -1;
4349     }
4350   return htab->plt_type == PLT_NEW;
4351 }
4352 \f
4353 /* Return the section that should be marked against GC for a given
4354    relocation.  */
4355
4356 static asection *
4357 ppc_elf_gc_mark_hook (asection *sec,
4358                       struct bfd_link_info *info,
4359                       Elf_Internal_Rela *rel,
4360                       struct elf_link_hash_entry *h,
4361                       Elf_Internal_Sym *sym)
4362 {
4363   if (h != NULL)
4364     switch (ELF32_R_TYPE (rel->r_info))
4365       {
4366       case R_PPC_GNU_VTINHERIT:
4367       case R_PPC_GNU_VTENTRY:
4368         return NULL;
4369       }
4370
4371   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4372 }
4373
4374 /* Update the got, plt and dynamic reloc reference counts for the
4375    section being removed.  */
4376
4377 static bfd_boolean
4378 ppc_elf_gc_sweep_hook (bfd *abfd,
4379                        struct bfd_link_info *info,
4380                        asection *sec,
4381                        const Elf_Internal_Rela *relocs)
4382 {
4383   struct ppc_elf_link_hash_table *htab;
4384   Elf_Internal_Shdr *symtab_hdr;
4385   struct elf_link_hash_entry **sym_hashes;
4386   bfd_signed_vma *local_got_refcounts;
4387   const Elf_Internal_Rela *rel, *relend;
4388   asection *got2;
4389
4390   if (info->relocatable)
4391     return TRUE;
4392
4393   if ((sec->flags & SEC_ALLOC) == 0)
4394     return TRUE;
4395
4396   elf_section_data (sec)->local_dynrel = NULL;
4397
4398   htab = ppc_elf_hash_table (info);
4399   symtab_hdr = &elf_symtab_hdr (abfd);
4400   sym_hashes = elf_sym_hashes (abfd);
4401   local_got_refcounts = elf_local_got_refcounts (abfd);
4402   got2 = bfd_get_section_by_name (abfd, ".got2");
4403
4404   relend = relocs + sec->reloc_count;
4405   for (rel = relocs; rel < relend; rel++)
4406     {
4407       unsigned long r_symndx;
4408       enum elf_ppc_reloc_type r_type;
4409       struct elf_link_hash_entry *h = NULL;
4410
4411       r_symndx = ELF32_R_SYM (rel->r_info);
4412       if (r_symndx >= symtab_hdr->sh_info)
4413         {
4414           struct ppc_elf_dyn_relocs **pp, *p;
4415           struct ppc_elf_link_hash_entry *eh;
4416
4417           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4418           while (h->root.type == bfd_link_hash_indirect
4419                  || h->root.type == bfd_link_hash_warning)
4420             h = (struct elf_link_hash_entry *) h->root.u.i.link;
4421           eh = (struct ppc_elf_link_hash_entry *) h;
4422
4423           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4424             if (p->sec == sec)
4425               {
4426                 /* Everything must go for SEC.  */
4427                 *pp = p->next;
4428                 break;
4429               }
4430         }
4431
4432       r_type = ELF32_R_TYPE (rel->r_info);
4433       if (!htab->is_vxworks
4434           && h == NULL
4435           && local_got_refcounts != NULL
4436           && (!info->shared
4437               || is_branch_reloc (r_type)))
4438         {
4439           struct plt_entry **local_plt = (struct plt_entry **)
4440             (local_got_refcounts + symtab_hdr->sh_info);
4441           char *local_got_tls_masks = (char *)
4442             (local_plt + symtab_hdr->sh_info);
4443           if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
4444             {
4445               struct plt_entry **ifunc = local_plt + r_symndx;
4446               bfd_vma addend = 0;
4447               struct plt_entry *ent;
4448
4449               if (r_type == R_PPC_PLTREL24 && info->shared)
4450                 addend = rel->r_addend;
4451               ent = find_plt_ent (ifunc, got2, addend);
4452               if (ent->plt.refcount > 0)
4453                 ent->plt.refcount -= 1;
4454               continue;
4455             }
4456         }
4457
4458       switch (r_type)
4459         {
4460         case R_PPC_GOT_TLSLD16:
4461         case R_PPC_GOT_TLSLD16_LO:
4462         case R_PPC_GOT_TLSLD16_HI:
4463         case R_PPC_GOT_TLSLD16_HA:
4464         case R_PPC_GOT_TLSGD16:
4465         case R_PPC_GOT_TLSGD16_LO:
4466         case R_PPC_GOT_TLSGD16_HI:
4467         case R_PPC_GOT_TLSGD16_HA:
4468         case R_PPC_GOT_TPREL16:
4469         case R_PPC_GOT_TPREL16_LO:
4470         case R_PPC_GOT_TPREL16_HI:
4471         case R_PPC_GOT_TPREL16_HA:
4472         case R_PPC_GOT_DTPREL16:
4473         case R_PPC_GOT_DTPREL16_LO:
4474         case R_PPC_GOT_DTPREL16_HI:
4475         case R_PPC_GOT_DTPREL16_HA:
4476         case R_PPC_GOT16:
4477         case R_PPC_GOT16_LO:
4478         case R_PPC_GOT16_HI:
4479         case R_PPC_GOT16_HA:
4480           if (h != NULL)
4481             {
4482               if (h->got.refcount > 0)
4483                 h->got.refcount--;
4484               if (!info->shared)
4485                 {
4486                   struct plt_entry *ent;
4487
4488                   ent = find_plt_ent (&h->plt.plist, NULL, 0);
4489                   if (ent->plt.refcount > 0)
4490                     ent->plt.refcount -= 1;
4491                 }
4492             }
4493           else if (local_got_refcounts != NULL)
4494             {
4495               if (local_got_refcounts[r_symndx] > 0)
4496                 local_got_refcounts[r_symndx]--;
4497             }
4498           break;
4499
4500         case R_PPC_REL24:
4501         case R_PPC_REL14:
4502         case R_PPC_REL14_BRTAKEN:
4503         case R_PPC_REL14_BRNTAKEN:
4504         case R_PPC_REL32:
4505           if (h == NULL || h == htab->elf.hgot)
4506             break;
4507           /* Fall thru */
4508
4509         case R_PPC_ADDR32:
4510         case R_PPC_ADDR24:
4511         case R_PPC_ADDR16:
4512         case R_PPC_ADDR16_LO:
4513         case R_PPC_ADDR16_HI:
4514         case R_PPC_ADDR16_HA:
4515         case R_PPC_ADDR14:
4516         case R_PPC_ADDR14_BRTAKEN:
4517         case R_PPC_ADDR14_BRNTAKEN:
4518         case R_PPC_UADDR32:
4519         case R_PPC_UADDR16:
4520           if (info->shared)
4521             break;
4522
4523         case R_PPC_PLT32:
4524         case R_PPC_PLTREL24:
4525         case R_PPC_PLTREL32:
4526         case R_PPC_PLT16_LO:
4527         case R_PPC_PLT16_HI:
4528         case R_PPC_PLT16_HA:
4529           if (h != NULL)
4530             {
4531               bfd_vma addend = 0;
4532               struct plt_entry *ent;
4533
4534               if (r_type == R_PPC_PLTREL24 && info->shared)
4535                 addend = rel->r_addend;
4536               ent = find_plt_ent (&h->plt.plist, got2, addend);
4537               if (ent->plt.refcount > 0)
4538                 ent->plt.refcount -= 1;
4539             }
4540           break;
4541
4542         default:
4543           break;
4544         }
4545     }
4546   return TRUE;
4547 }
4548 \f
4549 /* Set plt output section type, htab->tls_get_addr, and call the
4550    generic ELF tls_setup function.  */
4551
4552 asection *
4553 ppc_elf_tls_setup (bfd *obfd,
4554                    struct bfd_link_info *info,
4555                    int no_tls_get_addr_opt)
4556 {
4557   struct ppc_elf_link_hash_table *htab;
4558
4559   htab = ppc_elf_hash_table (info);
4560   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4561                                              FALSE, FALSE, TRUE);
4562   if (!no_tls_get_addr_opt)
4563     {
4564       struct elf_link_hash_entry *opt, *tga;
4565       opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4566                                   FALSE, FALSE, TRUE);
4567       if (opt != NULL
4568           && (opt->root.type == bfd_link_hash_defined
4569               || opt->root.type == bfd_link_hash_defweak))
4570         {
4571           /* If glibc supports an optimized __tls_get_addr call stub,
4572              signalled by the presence of __tls_get_addr_opt, and we'll
4573              be calling __tls_get_addr via a plt call stub, then
4574              make __tls_get_addr point to __tls_get_addr_opt.  */
4575           tga = htab->tls_get_addr;
4576           if (htab->elf.dynamic_sections_created
4577               && tga != NULL
4578               && (tga->type == STT_FUNC
4579                   || tga->needs_plt)
4580               && !(SYMBOL_CALLS_LOCAL (info, tga)
4581                    || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
4582                        && tga->root.type == bfd_link_hash_undefweak)))
4583             {
4584               struct plt_entry *ent;
4585               ent = find_plt_ent (&tga->plt.plist, NULL, 0);
4586               if (ent != NULL
4587                   && ent->plt.refcount > 0)
4588                 {
4589                   tga->root.type = bfd_link_hash_indirect;
4590                   tga->root.u.i.link = &opt->root;
4591                   ppc_elf_copy_indirect_symbol (info, opt, tga);
4592                   if (opt->dynindx != -1)
4593                     {
4594                       /* Use __tls_get_addr_opt in dynamic relocations.  */
4595                       opt->dynindx = -1;
4596                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4597                                               opt->dynstr_index);
4598                       if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4599                         return FALSE;
4600                     }
4601                   htab->tls_get_addr = opt;
4602                 }
4603             }
4604         }
4605       else
4606         no_tls_get_addr_opt = TRUE;
4607     }
4608   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
4609   if (htab->plt_type == PLT_NEW
4610       && htab->plt != NULL
4611       && htab->plt->output_section != NULL)
4612     {
4613       elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4614       elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4615     }
4616
4617   return _bfd_elf_tls_setup (obfd, info);
4618 }
4619
4620 /* Return TRUE iff REL is a branch reloc with a global symbol matching
4621    HASH.  */
4622
4623 static bfd_boolean
4624 branch_reloc_hash_match (const bfd *ibfd,
4625                          const Elf_Internal_Rela *rel,
4626                          const struct elf_link_hash_entry *hash)
4627 {
4628   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4629   enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4630   unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4631
4632   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
4633     {
4634       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4635       struct elf_link_hash_entry *h;
4636
4637       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4638       while (h->root.type == bfd_link_hash_indirect
4639              || h->root.type == bfd_link_hash_warning)
4640         h = (struct elf_link_hash_entry *) h->root.u.i.link;
4641       if (h == hash)
4642         return TRUE;
4643     }
4644   return FALSE;
4645 }
4646
4647 /* Run through all the TLS relocs looking for optimization
4648    opportunities.  */
4649
4650 bfd_boolean
4651 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4652                       struct bfd_link_info *info)
4653 {
4654   bfd *ibfd;
4655   asection *sec;
4656   struct ppc_elf_link_hash_table *htab;
4657   int pass;
4658
4659   if (info->relocatable || !info->executable)
4660     return TRUE;
4661
4662   htab = ppc_elf_hash_table (info);
4663   /* Make two passes through the relocs.  First time check that tls
4664      relocs involved in setting up a tls_get_addr call are indeed
4665      followed by such a call.  If they are not, exclude them from
4666      the optimizations done on the second pass.  */
4667   for (pass = 0; pass < 2; ++pass)
4668     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4669       {
4670         Elf_Internal_Sym *locsyms = NULL;
4671         Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4672
4673         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4674           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4675             {
4676               Elf_Internal_Rela *relstart, *rel, *relend;
4677
4678               /* Read the relocations.  */
4679               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4680                                                     info->keep_memory);
4681               if (relstart == NULL)
4682                 return FALSE;
4683
4684               relend = relstart + sec->reloc_count;
4685               for (rel = relstart; rel < relend; rel++)
4686                 {
4687                   enum elf_ppc_reloc_type r_type;
4688                   unsigned long r_symndx;
4689                   struct elf_link_hash_entry *h = NULL;
4690                   char *tls_mask;
4691                   char tls_set, tls_clear;
4692                   bfd_boolean is_local;
4693                   int expecting_tls_get_addr;
4694                   bfd_signed_vma *got_count;
4695
4696                   r_symndx = ELF32_R_SYM (rel->r_info);
4697                   if (r_symndx >= symtab_hdr->sh_info)
4698                     {
4699                       struct elf_link_hash_entry **sym_hashes;
4700
4701                       sym_hashes = elf_sym_hashes (ibfd);
4702                       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4703                       while (h->root.type == bfd_link_hash_indirect
4704                              || h->root.type == bfd_link_hash_warning)
4705                         h = (struct elf_link_hash_entry *) h->root.u.i.link;
4706                     }
4707
4708                   expecting_tls_get_addr = 0;
4709                   is_local = FALSE;
4710                   if (h == NULL
4711                       || !h->def_dynamic)
4712                     is_local = TRUE;
4713
4714                   r_type = ELF32_R_TYPE (rel->r_info);
4715                   switch (r_type)
4716                     {
4717                     case R_PPC_GOT_TLSLD16:
4718                     case R_PPC_GOT_TLSLD16_LO:
4719                       expecting_tls_get_addr = 1;
4720                       /* Fall thru */
4721
4722                     case R_PPC_GOT_TLSLD16_HI:
4723                     case R_PPC_GOT_TLSLD16_HA:
4724                       /* These relocs should never be against a symbol
4725                          defined in a shared lib.  Leave them alone if
4726                          that turns out to be the case.  */
4727                       if (!is_local)
4728                         continue;
4729
4730                       /* LD -> LE */
4731                       tls_set = 0;
4732                       tls_clear = TLS_LD;
4733                       break;
4734
4735                     case R_PPC_GOT_TLSGD16:
4736                     case R_PPC_GOT_TLSGD16_LO:
4737                       expecting_tls_get_addr = 1;
4738                       /* Fall thru */
4739
4740                     case R_PPC_GOT_TLSGD16_HI:
4741                     case R_PPC_GOT_TLSGD16_HA:
4742                       if (is_local)
4743                         /* GD -> LE */
4744                         tls_set = 0;
4745                       else
4746                         /* GD -> IE */
4747                         tls_set = TLS_TLS | TLS_TPRELGD;
4748                       tls_clear = TLS_GD;
4749                       break;
4750
4751                     case R_PPC_GOT_TPREL16:
4752                     case R_PPC_GOT_TPREL16_LO:
4753                     case R_PPC_GOT_TPREL16_HI:
4754                     case R_PPC_GOT_TPREL16_HA:
4755                       if (is_local)
4756                         {
4757                           /* IE -> LE */
4758                           tls_set = 0;
4759                           tls_clear = TLS_TPREL;
4760                           break;
4761                         }
4762                       else
4763                         continue;
4764
4765                     default:
4766                       continue;
4767                     }
4768
4769                   if (pass == 0)
4770                     {
4771                       if (!expecting_tls_get_addr
4772                           || !sec->has_tls_get_addr_call)
4773                         continue;
4774
4775                       if (rel + 1 < relend
4776                           && branch_reloc_hash_match (ibfd, rel + 1,
4777                                                       htab->tls_get_addr))
4778                         continue;
4779
4780                       /* Uh oh, we didn't find the expected call.  We
4781                          could just mark this symbol to exclude it
4782                          from tls optimization but it's safer to skip
4783                          the entire section.  */
4784                       sec->has_tls_reloc = 0;
4785                       break;
4786                     }
4787
4788                   if (h != NULL)
4789                     {
4790                       tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4791                       got_count = &h->got.refcount;
4792                     }
4793                   else
4794                     {
4795                       Elf_Internal_Sym *sym;
4796                       bfd_signed_vma *lgot_refs;
4797                       struct plt_entry **local_plt;
4798                       char *lgot_masks;
4799
4800                       if (locsyms == NULL)
4801                         {
4802                           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4803                           if (locsyms == NULL)
4804                             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4805                                                             symtab_hdr->sh_info,
4806                                                             0, NULL, NULL, NULL);
4807                           if (locsyms == NULL)
4808                             {
4809                               if (elf_section_data (sec)->relocs != relstart)
4810                                 free (relstart);
4811                               return FALSE;
4812                             }
4813                         }
4814                       sym = locsyms + r_symndx;
4815                       lgot_refs = elf_local_got_refcounts (ibfd);
4816                       if (lgot_refs == NULL)
4817                         abort ();
4818                       local_plt = (struct plt_entry **)
4819                         (lgot_refs + symtab_hdr->sh_info);
4820                       lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
4821                       tls_mask = &lgot_masks[r_symndx];
4822                       got_count = &lgot_refs[r_symndx];
4823                     }
4824
4825                   if (tls_set == 0)
4826                     {
4827                       /* We managed to get rid of a got entry.  */
4828                       if (*got_count > 0)
4829                         *got_count -= 1;
4830                     }
4831
4832                   if (expecting_tls_get_addr)
4833                     {
4834                       struct plt_entry *ent;
4835
4836                       ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4837                                           NULL, 0);
4838                       if (ent != NULL && ent->plt.refcount > 0)
4839                         ent->plt.refcount -= 1;
4840                     }
4841
4842                   *tls_mask |= tls_set;
4843                   *tls_mask &= ~tls_clear;
4844                 }
4845
4846               if (elf_section_data (sec)->relocs != relstart)
4847                 free (relstart);
4848             }
4849
4850         if (locsyms != NULL
4851             && (symtab_hdr->contents != (unsigned char *) locsyms))
4852           {
4853             if (!info->keep_memory)
4854               free (locsyms);
4855             else
4856               symtab_hdr->contents = (unsigned char *) locsyms;
4857           }
4858       }
4859   return TRUE;
4860 }
4861 \f
4862 /* Return true if we have dynamic relocs that apply to read-only sections.  */
4863
4864 static bfd_boolean
4865 readonly_dynrelocs (struct elf_link_hash_entry *h)
4866 {
4867   struct ppc_elf_dyn_relocs *p;
4868
4869   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4870     {
4871       asection *s = p->sec->output_section;
4872
4873       if (s != NULL
4874           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4875               == (SEC_READONLY | SEC_ALLOC)))
4876         return TRUE;
4877     }
4878   return FALSE;
4879 }
4880
4881 /* Adjust a symbol defined by a dynamic object and referenced by a
4882    regular object.  The current definition is in some section of the
4883    dynamic object, but we're not including those sections.  We have to
4884    change the definition to something the rest of the link can
4885    understand.  */
4886
4887 static bfd_boolean
4888 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4889                                struct elf_link_hash_entry *h)
4890 {
4891   struct ppc_elf_link_hash_table *htab;
4892   asection *s;
4893
4894 #ifdef DEBUG
4895   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4896            h->root.root.string);
4897 #endif
4898
4899   /* Make sure we know what is going on here.  */
4900   htab = ppc_elf_hash_table (info);
4901   BFD_ASSERT (htab->elf.dynobj != NULL
4902               && (h->needs_plt
4903                   || h->type == STT_GNU_IFUNC
4904                   || h->u.weakdef != NULL
4905                   || (h->def_dynamic
4906                       && h->ref_regular
4907                       && !h->def_regular)));
4908
4909   /* Deal with function syms.  */
4910   if (h->type == STT_FUNC
4911       || h->type == STT_GNU_IFUNC
4912       || h->needs_plt)
4913     {
4914       /* Clear procedure linkage table information for any symbol that
4915          won't need a .plt entry.  */
4916       struct plt_entry *ent;
4917       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4918         if (ent->plt.refcount > 0)
4919           break;
4920       if (ent == NULL
4921           || (h->type != STT_GNU_IFUNC
4922               && (SYMBOL_CALLS_LOCAL (info, h)
4923                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4924                       && h->root.type == bfd_link_hash_undefweak))))
4925         {
4926           /* A PLT entry is not required/allowed when:
4927
4928              1. We are not using ld.so; because then the PLT entry
4929              can't be set up, so we can't use one.  In this case,
4930              ppc_elf_adjust_dynamic_symbol won't even be called.
4931
4932              2. GC has rendered the entry unused.
4933
4934              3. We know for certain that a call to this symbol
4935              will go to this object, or will remain undefined.  */
4936           h->plt.plist = NULL;
4937           h->needs_plt = 0;
4938         }
4939       else
4940         {
4941           /* After adjust_dynamic_symbol, non_got_ref set in the
4942              non-shared case means that we have allocated space in
4943              .dynbss for the symbol and thus dyn_relocs for this
4944              symbol should be discarded.
4945              If we get here we know we are making a PLT entry for this
4946              symbol, and in an executable we'd normally resolve
4947              relocations against this symbol to the PLT entry.  Allow
4948              dynamic relocs if the reference is weak, and the dynamic
4949              relocs will not cause text relocation.  */
4950           if (!h->ref_regular_nonweak
4951               && h->non_got_ref
4952               && h->type != STT_GNU_IFUNC
4953               && !htab->is_vxworks
4954               && !ppc_elf_hash_entry (h)->has_sda_refs
4955               && !readonly_dynrelocs (h))
4956             h->non_got_ref = 0;
4957         }
4958       return TRUE;
4959     }
4960   else
4961     h->plt.plist = NULL;
4962
4963   /* If this is a weak symbol, and there is a real definition, the
4964      processor independent code will have arranged for us to see the
4965      real definition first, and we can just use the same value.  */
4966   if (h->u.weakdef != NULL)
4967     {
4968       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4969                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
4970       h->root.u.def.section = h->u.weakdef->root.u.def.section;
4971       h->root.u.def.value = h->u.weakdef->root.u.def.value;
4972       if (ELIMINATE_COPY_RELOCS)
4973         h->non_got_ref = h->u.weakdef->non_got_ref;
4974       return TRUE;
4975     }
4976
4977   /* This is a reference to a symbol defined by a dynamic object which
4978      is not a function.  */
4979
4980   /* If we are creating a shared library, we must presume that the
4981      only references to the symbol are via the global offset table.
4982      For such cases we need not do anything here; the relocations will
4983      be handled correctly by relocate_section.  */
4984   if (info->shared)
4985     return TRUE;
4986
4987   /* If there are no references to this symbol that do not use the
4988      GOT, we don't need to generate a copy reloc.  */
4989   if (!h->non_got_ref)
4990     return TRUE;
4991
4992    /* If we didn't find any dynamic relocs in read-only sections, then
4993       we'll be keeping the dynamic relocs and avoiding the copy reloc.
4994       We can't do this if there are any small data relocations.  This
4995       doesn't work on VxWorks, where we can not have dynamic
4996       relocations (other than copy and jump slot relocations) in an
4997       executable.  */
4998   if (ELIMINATE_COPY_RELOCS
4999       && !ppc_elf_hash_entry (h)->has_sda_refs
5000       && !htab->is_vxworks
5001       && !h->def_regular
5002       && !readonly_dynrelocs (h))
5003     {
5004       h->non_got_ref = 0;
5005       return TRUE;
5006     }
5007
5008   if (h->size == 0)
5009     {
5010       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5011                              h->root.root.string);
5012       return TRUE;
5013     }
5014
5015   /* We must allocate the symbol in our .dynbss section, which will
5016      become part of the .bss section of the executable.  There will be
5017      an entry for this symbol in the .dynsym section.  The dynamic
5018      object will contain position independent code, so all references
5019      from the dynamic object to this symbol will go through the global
5020      offset table.  The dynamic linker will use the .dynsym entry to
5021      determine the address it must put in the global offset table, so
5022      both the dynamic object and the regular object will refer to the
5023      same memory location for the variable.
5024
5025      Of course, if the symbol is referenced using SDAREL relocs, we
5026      must instead allocate it in .sbss.  */
5027
5028   if (ppc_elf_hash_entry (h)->has_sda_refs)
5029     s = htab->dynsbss;
5030   else
5031     s = htab->dynbss;
5032   BFD_ASSERT (s != NULL);
5033
5034   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
5035      copy the initial value out of the dynamic object and into the
5036      runtime process image.  We need to remember the offset into the
5037      .rela.bss section we are going to use.  */
5038   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5039     {
5040       asection *srel;
5041
5042       if (ppc_elf_hash_entry (h)->has_sda_refs)
5043         srel = htab->relsbss;
5044       else
5045         srel = htab->relbss;
5046       BFD_ASSERT (srel != NULL);
5047       srel->size += sizeof (Elf32_External_Rela);
5048       h->needs_copy = 1;
5049     }
5050
5051   return _bfd_elf_adjust_dynamic_copy (h, s);
5052 }
5053 \f
5054 /* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
5055    xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
5056    specifying the addend on the plt relocation.  For -fpic code, the sym
5057    is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
5058    xxxxxxxx.got2.plt_pic32.<callee>.  */
5059
5060 static bfd_boolean
5061 add_stub_sym (struct plt_entry *ent,
5062               struct elf_link_hash_entry *h,
5063               struct bfd_link_info *info)
5064 {
5065   struct elf_link_hash_entry *sh;
5066   size_t len1, len2, len3;
5067   char *name;
5068   const char *stub;
5069   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
5070
5071   if (info->shared)
5072     stub = ".plt_pic32.";
5073   else
5074     stub = ".plt_call32.";
5075
5076   len1 = strlen (h->root.root.string);
5077   len2 = strlen (stub);
5078   len3 = 0;
5079   if (ent->sec)
5080     len3 = strlen (ent->sec->name);
5081   name = bfd_malloc (len1 + len2 + len3 + 9);
5082   if (name == NULL)
5083     return FALSE;
5084   sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5085   if (ent->sec)
5086     memcpy (name + 8, ent->sec->name, len3);
5087   memcpy (name + 8 + len3, stub, len2);
5088   memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
5089   sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
5090   if (sh == NULL)
5091     return FALSE;
5092   if (sh->root.type == bfd_link_hash_new)
5093     {
5094       sh->root.type = bfd_link_hash_defined;
5095       sh->root.u.def.section = htab->glink;
5096       sh->root.u.def.value = ent->glink_offset;
5097       sh->ref_regular = 1;
5098       sh->def_regular = 1;
5099       sh->ref_regular_nonweak = 1;
5100       sh->forced_local = 1;
5101       sh->non_elf = 0;
5102     }
5103   return TRUE;
5104 }
5105
5106 /* Allocate NEED contiguous space in .got, and return the offset.
5107    Handles allocation of the got header when crossing 32k.  */
5108
5109 static bfd_vma
5110 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5111 {
5112   bfd_vma where;
5113   unsigned int max_before_header;
5114
5115   if (htab->plt_type == PLT_VXWORKS)
5116     {
5117       where = htab->got->size;
5118       htab->got->size += need;
5119     }
5120   else
5121     {
5122       max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5123       if (need <= htab->got_gap)
5124         {
5125           where = max_before_header - htab->got_gap;
5126           htab->got_gap -= need;
5127         }
5128       else
5129         {
5130           if (htab->got->size + need > max_before_header
5131               && htab->got->size <= max_before_header)
5132             {
5133               htab->got_gap = max_before_header - htab->got->size;
5134               htab->got->size = max_before_header + htab->got_header_size;
5135             }
5136           where = htab->got->size;
5137           htab->got->size += need;
5138         }
5139     }
5140   return where;
5141 }
5142
5143 /* Allocate space in associated reloc sections for dynamic relocs.  */
5144
5145 static bfd_boolean
5146 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5147 {
5148   struct bfd_link_info *info = inf;
5149   struct ppc_elf_link_hash_entry *eh;
5150   struct ppc_elf_link_hash_table *htab;
5151   struct ppc_elf_dyn_relocs *p;
5152
5153   if (h->root.type == bfd_link_hash_indirect)
5154     return TRUE;
5155
5156   if (h->root.type == bfd_link_hash_warning)
5157     /* When warning symbols are created, they **replace** the "real"
5158        entry in the hash table, thus we never get to see the real
5159        symbol in a hash traversal.  So look at it now.  */
5160     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5161
5162   htab = ppc_elf_hash_table (info);
5163   if (htab->elf.dynamic_sections_created
5164       || h->type == STT_GNU_IFUNC)
5165     {
5166       struct plt_entry *ent;
5167       bfd_boolean doneone = FALSE;
5168       bfd_vma plt_offset = 0, glink_offset = 0;
5169       bfd_boolean dyn;
5170
5171       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5172         if (ent->plt.refcount > 0)
5173           {
5174             /* Make sure this symbol is output as a dynamic symbol.  */
5175             if (h->dynindx == -1
5176                 && !h->forced_local
5177                 && !h->def_regular
5178                 && htab->elf.dynamic_sections_created)
5179               {
5180                 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5181                   return FALSE;
5182               }
5183
5184             dyn = htab->elf.dynamic_sections_created;
5185             if (info->shared
5186                 || h->type == STT_GNU_IFUNC
5187                 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5188               {
5189                 asection *s = htab->plt;
5190                 if (!dyn || h->dynindx == -1)
5191                   s = htab->iplt;
5192
5193                 if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
5194                   {
5195                     if (!doneone)
5196                       {
5197                         plt_offset = s->size;
5198                         s->size += 4;
5199                       }
5200                     ent->plt.offset = plt_offset;
5201
5202                     s = htab->glink;
5203                     if (!doneone || info->shared)
5204                       {
5205                         glink_offset = s->size;
5206                         s->size += GLINK_ENTRY_SIZE;
5207                         if (h == htab->tls_get_addr
5208                             && !htab->no_tls_get_addr_opt)
5209                           s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
5210                       }
5211                     if (!doneone
5212                         && !info->shared
5213                         && h->def_dynamic
5214                         && !h->def_regular)
5215                       {
5216                         h->root.u.def.section = s;
5217                         h->root.u.def.value = glink_offset;
5218                       }
5219                     ent->glink_offset = glink_offset;
5220
5221                     if (htab->emit_stub_syms
5222                         && !add_stub_sym (ent, h, info))
5223                       return FALSE;
5224                   }
5225                 else
5226                   {
5227                     if (!doneone)
5228                       {
5229                         /* If this is the first .plt entry, make room
5230                            for the special first entry.  */
5231                         if (s->size == 0)
5232                           s->size += htab->plt_initial_entry_size;
5233
5234                         /* The PowerPC PLT is actually composed of two
5235                            parts, the first part is 2 words (for a load
5236                            and a jump), and then there is a remaining
5237                            word available at the end.  */
5238                         plt_offset = (htab->plt_initial_entry_size
5239                                       + (htab->plt_slot_size
5240                                          * ((s->size
5241                                              - htab->plt_initial_entry_size)
5242                                             / htab->plt_entry_size)));
5243
5244                         /* If this symbol is not defined in a regular
5245                            file, and we are not generating a shared
5246                            library, then set the symbol to this location 
5247                            in the .plt.  This is to avoid text
5248                            relocations, and is required to make
5249                            function pointers compare as equal between
5250                            the normal executable and the shared library.  */
5251                         if (! info->shared
5252                             && h->def_dynamic
5253                             && !h->def_regular)
5254                           {
5255                             h->root.u.def.section = s;
5256                             h->root.u.def.value = plt_offset;
5257                           }
5258
5259                         /* Make room for this entry.  */
5260                         s->size += htab->plt_entry_size;
5261                         /* After the 8192nd entry, room for two entries
5262                            is allocated.  */
5263                         if (htab->plt_type == PLT_OLD
5264                             && (s->size - htab->plt_initial_entry_size)
5265                                 / htab->plt_entry_size
5266                                > PLT_NUM_SINGLE_ENTRIES)
5267                           s->size += htab->plt_entry_size;
5268                       }
5269                     ent->plt.offset = plt_offset;
5270                   }
5271
5272                 /* We also need to make an entry in the .rela.plt section.  */
5273                 if (!doneone)
5274                   {
5275                     if (!htab->elf.dynamic_sections_created
5276                         || h->dynindx == -1)
5277                       htab->reliplt->size += sizeof (Elf32_External_Rela);
5278                     else
5279                       {
5280                         htab->relplt->size += sizeof (Elf32_External_Rela);
5281
5282                         if (htab->plt_type == PLT_VXWORKS)
5283                           {
5284                             /* Allocate space for the unloaded relocations.  */
5285                             if (!info->shared
5286                                 && htab->elf.dynamic_sections_created)
5287                               {
5288                                 if (ent->plt.offset
5289                                     == (bfd_vma) htab->plt_initial_entry_size)
5290                                   {
5291                                     htab->srelplt2->size
5292                                       += (sizeof (Elf32_External_Rela)
5293                                           * VXWORKS_PLTRESOLVE_RELOCS);
5294                                   }
5295
5296                                 htab->srelplt2->size
5297                                   += (sizeof (Elf32_External_Rela)
5298                                       * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
5299                               }
5300
5301                             /* Every PLT entry has an associated GOT entry in
5302                                .got.plt.  */
5303                             htab->sgotplt->size += 4;
5304                           }
5305                       }
5306                     doneone = TRUE;
5307                   }
5308               }
5309             else
5310               ent->plt.offset = (bfd_vma) -1;
5311           }
5312         else
5313           ent->plt.offset = (bfd_vma) -1;
5314
5315       if (!doneone)
5316         {
5317           h->plt.plist = NULL;
5318           h->needs_plt = 0;
5319         }
5320     }
5321   else
5322     {
5323       h->plt.plist = NULL;
5324       h->needs_plt = 0;
5325     }
5326
5327   eh = (struct ppc_elf_link_hash_entry *) h;
5328   if (eh->elf.got.refcount > 0)
5329     {
5330       bfd_boolean dyn;
5331       unsigned int need;
5332
5333       /* Make sure this symbol is output as a dynamic symbol.  */
5334       if (eh->elf.dynindx == -1
5335           && !eh->elf.forced_local
5336           && eh->elf.type != STT_GNU_IFUNC
5337           && htab->elf.dynamic_sections_created)
5338         {
5339           if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
5340             return FALSE;
5341         }
5342
5343       need = 0;
5344       if ((eh->tls_mask & TLS_TLS) != 0)
5345         {
5346           if ((eh->tls_mask & TLS_LD) != 0)
5347             {
5348               if (!eh->elf.def_dynamic)
5349                 /* We'll just use htab->tlsld_got.offset.  This should
5350                    always be the case.  It's a little odd if we have
5351                    a local dynamic reloc against a non-local symbol.  */
5352                 htab->tlsld_got.refcount += 1;
5353               else
5354                 need += 8;
5355             }
5356           if ((eh->tls_mask & TLS_GD) != 0)
5357             need += 8;
5358           if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
5359             need += 4;
5360           if ((eh->tls_mask & TLS_DTPREL) != 0)
5361             need += 4;
5362         }
5363       else
5364         need += 4;
5365       if (need == 0)
5366         eh->elf.got.offset = (bfd_vma) -1;
5367       else
5368         {
5369           eh->elf.got.offset = allocate_got (htab, need);
5370           dyn = htab->elf.dynamic_sections_created;
5371           if ((info->shared
5372                || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
5373               && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
5374                   || eh->elf.root.type != bfd_link_hash_undefweak))
5375             {
5376               asection *rsec = htab->relgot;
5377               /* All the entries we allocated need relocs.
5378                  Except LD only needs one.  */
5379               if ((eh->tls_mask & TLS_LD) != 0
5380                   && eh->elf.def_dynamic)
5381                 need -= 4;
5382               rsec->size += need * (sizeof (Elf32_External_Rela) / 4);
5383             }
5384         }
5385     }
5386   else
5387     eh->elf.got.offset = (bfd_vma) -1;
5388
5389   if (eh->dyn_relocs == NULL
5390       || !htab->elf.dynamic_sections_created)
5391     return TRUE;
5392
5393   /* In the shared -Bsymbolic case, discard space allocated for
5394      dynamic pc-relative relocs against symbols which turn out to be
5395      defined in regular objects.  For the normal shared case, discard
5396      space for relocs that have become local due to symbol visibility
5397      changes.  */
5398
5399   if (info->shared)
5400     {
5401       /* Relocs that use pc_count are those that appear on a call insn,
5402          or certain REL relocs (see must_be_dyn_reloc) that can be
5403          generated via assembly.  We want calls to protected symbols to
5404          resolve directly to the function rather than going via the plt.
5405          If people want function pointer comparisons to work as expected
5406          then they should avoid writing weird assembly.  */
5407       if (SYMBOL_CALLS_LOCAL (info, h))
5408         {
5409           struct ppc_elf_dyn_relocs **pp;
5410
5411           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5412             {
5413               p->count -= p->pc_count;
5414               p->pc_count = 0;
5415               if (p->count == 0)
5416                 *pp = p->next;
5417               else
5418                 pp = &p->next;
5419             }
5420         }
5421
5422       if (htab->is_vxworks)
5423         {
5424           struct ppc_elf_dyn_relocs **pp;
5425
5426           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5427             {
5428               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5429                 *pp = p->next;
5430               else
5431                 pp = &p->next;
5432             }
5433         }
5434
5435       /* Discard relocs on undefined symbols that must be local.  */
5436       if (eh->dyn_relocs != NULL
5437           && h->root.type == bfd_link_hash_undefined
5438           && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
5439               || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
5440         eh->dyn_relocs = NULL;
5441
5442       /* Also discard relocs on undefined weak syms with non-default
5443          visibility.  */
5444       if (eh->dyn_relocs != NULL
5445           && h->root.type == bfd_link_hash_undefweak)
5446         {
5447           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5448             eh->dyn_relocs = NULL;
5449
5450           /* Make sure undefined weak symbols are output as a dynamic
5451              symbol in PIEs.  */
5452           else if (h->dynindx == -1
5453                    && !h->forced_local
5454                    && !h->def_regular)
5455             {
5456               if (! bfd_elf_link_record_dynamic_symbol (info, h))
5457                 return FALSE;
5458             }
5459         }
5460     }
5461   else if (ELIMINATE_COPY_RELOCS)
5462     {
5463       /* For the non-shared case, discard space for relocs against
5464          symbols which turn out to need copy relocs or are not
5465          dynamic.  */
5466
5467       if (!h->non_got_ref
5468           && !h->def_regular)
5469         {
5470           /* Make sure this symbol is output as a dynamic symbol.
5471              Undefined weak syms won't yet be marked as dynamic.  */
5472           if (h->dynindx == -1
5473               && !h->forced_local)
5474             {
5475               if (! bfd_elf_link_record_dynamic_symbol (info, h))
5476                 return FALSE;
5477             }
5478
5479           /* If that succeeded, we know we'll be keeping all the
5480              relocs.  */
5481           if (h->dynindx != -1)
5482             goto keep;
5483         }
5484
5485       eh->dyn_relocs = NULL;
5486
5487     keep: ;
5488     }
5489
5490   /* Finally, allocate space.  */
5491   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5492     {
5493       asection *sreloc = elf_section_data (p->sec)->sreloc;
5494       if (!htab->elf.dynamic_sections_created)
5495         sreloc = htab->reliplt;
5496       sreloc->size += p->count * sizeof (Elf32_External_Rela);
5497     }
5498
5499   return TRUE;
5500 }
5501
5502 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
5503    read-only sections.  */
5504
5505 static bfd_boolean
5506 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
5507 {
5508   if (h->root.type == bfd_link_hash_indirect)
5509     return TRUE;
5510
5511   if (h->root.type == bfd_link_hash_warning)
5512     h = (struct elf_link_hash_entry *) h->root.u.i.link;
5513
5514   if (readonly_dynrelocs (h))
5515     {
5516       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
5517
5518       /* Not an error, just cut short the traversal.  */
5519       return FALSE;
5520     }
5521   return TRUE;
5522 }
5523
5524 /* Set the sizes of the dynamic sections.  */
5525
5526 static bfd_boolean
5527 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5528                                struct bfd_link_info *info)
5529 {
5530   struct ppc_elf_link_hash_table *htab;
5531   asection *s;
5532   bfd_boolean relocs;
5533   bfd *ibfd;
5534
5535 #ifdef DEBUG
5536   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
5537 #endif
5538
5539   htab = ppc_elf_hash_table (info);
5540   BFD_ASSERT (htab->elf.dynobj != NULL);
5541
5542   if (elf_hash_table (info)->dynamic_sections_created)
5543     {
5544       /* Set the contents of the .interp section to the interpreter.  */
5545       if (info->executable)
5546         {
5547           s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
5548           BFD_ASSERT (s != NULL);
5549           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5550           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5551         }
5552     }
5553
5554   if (htab->plt_type == PLT_OLD)
5555     htab->got_header_size = 16;
5556   else if (htab->plt_type == PLT_NEW)
5557     htab->got_header_size = 12;
5558
5559   /* Set up .got offsets for local syms, and space for local dynamic
5560      relocs.  */
5561   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5562     {
5563       bfd_signed_vma *local_got;
5564       bfd_signed_vma *end_local_got;
5565       struct plt_entry **local_plt;
5566       struct plt_entry **end_local_plt;
5567       char *lgot_masks;
5568       bfd_size_type locsymcount;
5569       Elf_Internal_Shdr *symtab_hdr;
5570
5571       if (!is_ppc_elf (ibfd))
5572         continue;
5573
5574       for (s = ibfd->sections; s != NULL; s = s->next)
5575         {
5576           struct ppc_elf_dyn_relocs *p;
5577
5578           for (p = ((struct ppc_elf_dyn_relocs *)
5579                     elf_section_data (s)->local_dynrel);
5580                p != NULL;
5581                p = p->next)
5582             {
5583               if (!bfd_is_abs_section (p->sec)
5584                   && bfd_is_abs_section (p->sec->output_section))
5585                 {
5586                   /* Input section has been discarded, either because
5587                      it is a copy of a linkonce section or due to
5588                      linker script /DISCARD/, so we'll be discarding
5589                      the relocs too.  */
5590                 }
5591               else if (htab->is_vxworks
5592                        && strcmp (p->sec->output_section->name,
5593                                   ".tls_vars") == 0)
5594                 {
5595                   /* Relocations in vxworks .tls_vars sections are
5596                      handled specially by the loader.  */
5597                 }
5598               else if (p->count != 0)
5599                 {
5600                   asection *sreloc = elf_section_data (p->sec)->sreloc;
5601                   if (!htab->elf.dynamic_sections_created)
5602                     sreloc = htab->reliplt;
5603                   sreloc->size += p->count * sizeof (Elf32_External_Rela);
5604                   if ((p->sec->output_section->flags
5605                        & (SEC_READONLY | SEC_ALLOC))
5606                       == (SEC_READONLY | SEC_ALLOC))
5607                     info->flags |= DF_TEXTREL;
5608                 }
5609             }
5610         }
5611
5612       local_got = elf_local_got_refcounts (ibfd);
5613       if (!local_got)
5614         continue;
5615
5616       symtab_hdr = &elf_symtab_hdr (ibfd);
5617       locsymcount = symtab_hdr->sh_info;
5618       end_local_got = local_got + locsymcount;
5619       local_plt = (struct plt_entry **) end_local_got;
5620       end_local_plt = local_plt + locsymcount;
5621       lgot_masks = (char *) end_local_plt;
5622
5623       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5624         if (*local_got > 0)
5625           {
5626             unsigned int need = 0;
5627             if ((*lgot_masks & TLS_TLS) != 0)
5628               {
5629                 if ((*lgot_masks & TLS_GD) != 0)
5630                   need += 8;
5631                 if ((*lgot_masks & TLS_LD) != 0)
5632                   htab->tlsld_got.refcount += 1;
5633                 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5634                   need += 4;
5635                 if ((*lgot_masks & TLS_DTPREL) != 0)
5636                   need += 4;
5637               }
5638             else
5639               need += 4;
5640             if (need == 0)
5641               *local_got = (bfd_vma) -1;
5642             else
5643               {
5644                 *local_got = allocate_got (htab, need);
5645                 if (info->shared)
5646                   htab->relgot->size += (need
5647                                          * (sizeof (Elf32_External_Rela) / 4));
5648               }
5649           }
5650         else
5651           *local_got = (bfd_vma) -1;
5652
5653       if (htab->is_vxworks)
5654         continue;
5655
5656       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
5657       for (; local_plt < end_local_plt; ++local_plt)
5658         {
5659           struct plt_entry *ent;
5660           bfd_boolean doneone = FALSE;
5661           bfd_vma plt_offset = 0, glink_offset = 0;
5662
5663           for (ent = *local_plt; ent != NULL; ent = ent->next)
5664             if (ent->plt.refcount > 0)
5665               {
5666                 s = htab->iplt;
5667
5668                 if (!doneone)
5669                   {
5670                     plt_offset = s->size;
5671                     s->size += 4;
5672                   }
5673                 ent->plt.offset = plt_offset;
5674
5675                 s = htab->glink;
5676                 if (!doneone || info->shared)
5677                   {
5678                     glink_offset = s->size;
5679                     s->size += GLINK_ENTRY_SIZE;
5680                   }
5681                 ent->glink_offset = glink_offset;
5682
5683                 if (!doneone)
5684                   {
5685                     htab->reliplt->size += sizeof (Elf32_External_Rela);
5686                     doneone = TRUE;
5687                   }
5688               }
5689             else
5690               ent->plt.offset = (bfd_vma) -1;
5691         }
5692     }
5693
5694   /* Allocate space for global sym dynamic relocs.  */
5695   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5696
5697   if (htab->tlsld_got.refcount > 0)
5698     {
5699       htab->tlsld_got.offset = allocate_got (htab, 8);
5700       if (info->shared)
5701         htab->relgot->size += sizeof (Elf32_External_Rela);
5702     }
5703   else
5704     htab->tlsld_got.offset = (bfd_vma) -1;
5705
5706   if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
5707     {
5708       unsigned int g_o_t = 32768;
5709
5710       /* If we haven't allocated the header, do so now.  When we get here,
5711          for old plt/got the got size will be 0 to 32764 (not allocated),
5712          or 32780 to 65536 (header allocated).  For new plt/got, the
5713          corresponding ranges are 0 to 32768 and 32780 to 65536.  */
5714       if (htab->got->size <= 32768)
5715         {
5716           g_o_t = htab->got->size;
5717           if (htab->plt_type == PLT_OLD)
5718             g_o_t += 4;
5719           htab->got->size += htab->got_header_size;
5720         }
5721
5722       htab->elf.hgot->root.u.def.value = g_o_t;
5723     }
5724   if (info->shared)
5725     {
5726       struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5727       if (sda != NULL
5728           && !(sda->root.type == bfd_link_hash_defined
5729                || sda->root.type == bfd_link_hash_defweak))
5730         {
5731           sda->root.type = bfd_link_hash_defined;
5732           sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5733           sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5734         }
5735     }
5736
5737   if (htab->glink != NULL
5738       && htab->glink->size != 0
5739       && htab->elf.dynamic_sections_created)
5740     {
5741       htab->glink_pltresolve = htab->glink->size;
5742       /* Space for the branch table.  */
5743       htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5744       /* Pad out to align the start of PLTresolve.  */
5745       htab->glink->size += -htab->glink->size & 15;
5746       htab->glink->size += GLINK_PLTRESOLVE;
5747
5748       if (htab->emit_stub_syms)
5749         {
5750           struct elf_link_hash_entry *sh;
5751           sh = elf_link_hash_lookup (&htab->elf, "__glink",
5752                                      TRUE, FALSE, FALSE);
5753           if (sh == NULL)
5754             return FALSE;
5755           if (sh->root.type == bfd_link_hash_new)
5756             {
5757               sh->root.type = bfd_link_hash_defined;
5758               sh->root.u.def.section = htab->glink;
5759               sh->root.u.def.value = htab->glink_pltresolve;
5760               sh->ref_regular = 1;
5761               sh->def_regular = 1;
5762               sh->ref_regular_nonweak = 1;
5763               sh->forced_local = 1;
5764               sh->non_elf = 0;
5765             }
5766           sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5767                                      TRUE, FALSE, FALSE);
5768           if (sh == NULL)
5769             return FALSE;
5770           if (sh->root.type == bfd_link_hash_new)
5771             {
5772               sh->root.type = bfd_link_hash_defined;
5773               sh->root.u.def.section = htab->glink;
5774               sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5775               sh->ref_regular = 1;
5776               sh->def_regular = 1;
5777               sh->ref_regular_nonweak = 1;
5778               sh->forced_local = 1;
5779               sh->non_elf = 0;
5780             }
5781         }
5782     }
5783
5784   /* We've now determined the sizes of the various dynamic sections.
5785      Allocate memory for them.  */
5786   relocs = FALSE;
5787   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5788     {
5789       bfd_boolean strip_section = TRUE;
5790
5791       if ((s->flags & SEC_LINKER_CREATED) == 0)
5792         continue;
5793
5794       if (s == htab->plt
5795           || s == htab->got)
5796         {
5797           /* We'd like to strip these sections if they aren't needed, but if
5798              we've exported dynamic symbols from them we must leave them.
5799              It's too late to tell BFD to get rid of the symbols.  */
5800           if (htab->elf.hplt != NULL)
5801             strip_section = FALSE;
5802           /* Strip this section if we don't need it; see the
5803              comment below.  */
5804         }
5805       else if (s == htab->iplt
5806                || s == htab->glink
5807                || s == htab->sgotplt
5808                || s == htab->sbss
5809                || s == htab->dynbss
5810                || s == htab->dynsbss
5811                || s == htab->sdata[0].section
5812                || s == htab->sdata[1].section)
5813         {
5814           /* Strip these too.  */
5815         }
5816       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5817         {
5818           if (s->size != 0)
5819             {
5820               /* Remember whether there are any relocation sections.  */
5821               relocs = TRUE;
5822
5823               /* We use the reloc_count field as a counter if we need
5824                  to copy relocs into the output file.  */
5825               s->reloc_count = 0;
5826             }
5827         }
5828       else
5829         {
5830           /* It's not one of our sections, so don't allocate space.  */
5831           continue;
5832         }
5833
5834       if (s->size == 0 && strip_section)
5835         {
5836           /* If we don't need this section, strip it from the
5837              output file.  This is mostly to handle .rela.bss and
5838              .rela.plt.  We must create both sections in
5839              create_dynamic_sections, because they must be created
5840              before the linker maps input sections to output
5841              sections.  The linker does that before
5842              adjust_dynamic_symbol is called, and it is that
5843              function which decides whether anything needs to go
5844              into these sections.  */
5845           s->flags |= SEC_EXCLUDE;
5846           continue;
5847         }
5848
5849       if ((s->flags & SEC_HAS_CONTENTS) == 0)
5850         continue;
5851
5852       /* Allocate memory for the section contents.  */
5853       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5854       if (s->contents == NULL)
5855         return FALSE;
5856     }
5857
5858   if (htab->elf.dynamic_sections_created)
5859     {
5860       /* Add some entries to the .dynamic section.  We fill in the
5861          values later, in ppc_elf_finish_dynamic_sections, but we
5862          must add the entries now so that we get the correct size for
5863          the .dynamic section.  The DT_DEBUG entry is filled in by the
5864          dynamic linker and used by the debugger.  */
5865 #define add_dynamic_entry(TAG, VAL) \
5866   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5867
5868       if (info->executable)
5869         {
5870           if (!add_dynamic_entry (DT_DEBUG, 0))
5871             return FALSE;
5872         }
5873
5874       if (htab->plt != NULL && htab->plt->size != 0)
5875         {
5876           if (!add_dynamic_entry (DT_PLTGOT, 0)
5877               || !add_dynamic_entry (DT_PLTRELSZ, 0)
5878               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5879               || !add_dynamic_entry (DT_JMPREL, 0))
5880             return FALSE;
5881         }
5882
5883       if (htab->glink != NULL && htab->glink->size != 0)
5884         {
5885           if (!add_dynamic_entry (DT_PPC_GOT, 0))
5886             return FALSE;
5887           if (!htab->no_tls_get_addr_opt
5888               && htab->tls_get_addr != NULL
5889               && htab->tls_get_addr->plt.plist != NULL
5890               && !add_dynamic_entry (DT_PPC_TLSOPT, 0))
5891             return FALSE;
5892         }
5893
5894       if (relocs)
5895         {
5896           if (!add_dynamic_entry (DT_RELA, 0)
5897               || !add_dynamic_entry (DT_RELASZ, 0)
5898               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5899             return FALSE;
5900         }
5901
5902       /* If any dynamic relocs apply to a read-only section, then we
5903          need a DT_TEXTREL entry.  */
5904       if ((info->flags & DF_TEXTREL) == 0)
5905         elf_link_hash_traverse (elf_hash_table (info), maybe_set_textrel,
5906                                 info);
5907
5908       if ((info->flags & DF_TEXTREL) != 0)
5909         {
5910           if (!add_dynamic_entry (DT_TEXTREL, 0))
5911             return FALSE;
5912         }
5913       if (htab->is_vxworks
5914           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
5915         return FALSE;
5916    }
5917 #undef add_dynamic_entry
5918
5919   return TRUE;
5920 }
5921
5922 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
5923
5924 static bfd_boolean
5925 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
5926 {
5927   if (h->plt.plist != NULL
5928       && !h->def_regular
5929       && (!h->pointer_equality_needed
5930           || !h->ref_regular_nonweak))
5931     return FALSE;
5932
5933   return _bfd_elf_hash_symbol (h);
5934 }
5935 \f
5936 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5937
5938 /* Relaxation trampolines.  r12 is available for clobbering (r11, is
5939    used for some functions that are allowed to break the ABI).  */
5940 static const int shared_stub_entry[] =
5941   {
5942     0x7c0802a6, /* mflr 0 */
5943     0x429f0005, /* bcl 20, 31, .Lxxx */
5944     0x7d8802a6, /* mflr 12 */
5945     0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
5946     0x398c0008, /* addi 12, 12, (xxx-.Lxxx)@l */
5947     0x7c0803a6, /* mtlr 0 */
5948     0x7d8903a6, /* mtctr 12 */
5949     0x4e800420, /* bctr */
5950   };
5951
5952 static const int stub_entry[] =
5953   {
5954     0x3d800000, /* lis 12,xxx@ha */
5955     0x398c0000, /* addi 12,12,xxx@l */
5956     0x7d8903a6, /* mtctr 12 */
5957     0x4e800420, /* bctr */
5958   };
5959
5960 static bfd_boolean
5961 ppc_elf_relax_section (bfd *abfd,
5962                        asection *isec,
5963                        struct bfd_link_info *link_info,
5964                        bfd_boolean *again)
5965 {
5966   struct one_fixup
5967   {
5968     struct one_fixup *next;
5969     asection *tsec;
5970     /* Final link, can use the symbol offset.  For a
5971        relocatable link we use the symbol's index.  */
5972     bfd_vma toff;
5973     bfd_vma trampoff;
5974   };
5975
5976   Elf_Internal_Shdr *symtab_hdr;
5977   bfd_byte *contents = NULL;
5978   Elf_Internal_Sym *isymbuf = NULL;
5979   Elf_Internal_Rela *internal_relocs = NULL;
5980   Elf_Internal_Rela *irel, *irelend;
5981   struct one_fixup *fixups = NULL;
5982   unsigned changes = 0;
5983   struct ppc_elf_link_hash_table *htab;
5984   bfd_size_type trampoff;
5985   asection *got2;
5986
5987   *again = FALSE;
5988
5989   /* Nothing to do if there are no relocations, and no need to do
5990      anything with non-alloc or non-code sections.  */
5991   if ((isec->flags & SEC_ALLOC) == 0
5992       || (isec->flags & SEC_CODE) == 0
5993       || (isec->flags & SEC_RELOC) == 0
5994       || isec->reloc_count == 0)
5995     return TRUE;
5996
5997   /* We cannot represent the required PIC relocs in the output, so don't
5998      do anything.  The linker doesn't support mixing -shared and -r
5999      anyway.  */
6000   if (link_info->relocatable && link_info->shared)
6001      return TRUE;
6002   
6003   trampoff = (isec->size + 3) & (bfd_vma) -4;
6004   /* Space for a branch around any trampolines.  */
6005   trampoff += 4;
6006
6007   symtab_hdr = &elf_symtab_hdr (abfd);
6008
6009   /* Get a copy of the native relocations.  */
6010   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
6011                                                link_info->keep_memory);
6012   if (internal_relocs == NULL)
6013     goto error_return;
6014
6015   htab = ppc_elf_hash_table (link_info);
6016   got2 = bfd_get_section_by_name (abfd, ".got2");
6017
6018   irelend = internal_relocs + isec->reloc_count;
6019   for (irel = internal_relocs; irel < irelend; irel++)
6020     {
6021       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6022       bfd_vma toff, roff;
6023       asection *tsec;
6024       struct one_fixup *f;
6025       size_t insn_offset = 0;
6026       bfd_vma max_branch_offset, val;
6027       bfd_byte *hit_addr;
6028       unsigned long t0;
6029       struct elf_link_hash_entry *h;
6030       struct plt_entry **plist;
6031       unsigned char sym_type;
6032
6033       switch (r_type)
6034         {
6035         case R_PPC_REL24:
6036         case R_PPC_LOCAL24PC:
6037         case R_PPC_PLTREL24:
6038           max_branch_offset = 1 << 25;
6039           break;
6040
6041         case R_PPC_REL14:
6042         case R_PPC_REL14_BRTAKEN:
6043         case R_PPC_REL14_BRNTAKEN:
6044           max_branch_offset = 1 << 15;
6045           break;
6046
6047         default:
6048           continue;
6049         }
6050
6051       /* Get the value of the symbol referred to by the reloc.  */
6052       h = NULL;
6053       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6054         {
6055           /* A local symbol.  */
6056           Elf_Internal_Sym *isym;
6057
6058           /* Read this BFD's local symbols.  */
6059           if (isymbuf == NULL)
6060             {
6061               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6062               if (isymbuf == NULL)
6063                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6064                                                 symtab_hdr->sh_info, 0,
6065                                                 NULL, NULL, NULL);
6066               if (isymbuf == 0)
6067                 goto error_return;
6068             }
6069           isym = isymbuf + ELF32_R_SYM (irel->r_info);
6070           if (isym->st_shndx == SHN_UNDEF)
6071             tsec = bfd_und_section_ptr;
6072           else if (isym->st_shndx == SHN_ABS)
6073             tsec = bfd_abs_section_ptr;
6074           else if (isym->st_shndx == SHN_COMMON)
6075             tsec = bfd_com_section_ptr;
6076           else
6077             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6078
6079           toff = isym->st_value;
6080           sym_type = ELF_ST_TYPE (isym->st_info);
6081         }
6082       else
6083         {
6084           /* Global symbol handling.  */
6085           unsigned long indx;
6086
6087           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6088           h = elf_sym_hashes (abfd)[indx];
6089
6090           while (h->root.type == bfd_link_hash_indirect
6091                  || h->root.type == bfd_link_hash_warning)
6092             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6093
6094           if (h->root.type == bfd_link_hash_defined
6095               || h->root.type == bfd_link_hash_defweak)
6096             {
6097               tsec = h->root.u.def.section;
6098               toff = h->root.u.def.value;
6099             }
6100           else if (h->root.type == bfd_link_hash_undefined
6101                    || h->root.type == bfd_link_hash_undefweak)
6102             {
6103               tsec = bfd_und_section_ptr;
6104               toff = link_info->relocatable ? indx : 0;
6105             }
6106           else
6107             continue;
6108
6109           sym_type = h->type;
6110         }
6111
6112       /* The condition here under which we call find_plt_ent must
6113          match that in relocate_section.  If we call find_plt_ent here
6114          but not in relocate_section, or vice versa, then the branch
6115          destination used here may be incorrect.  */
6116       plist = NULL;
6117       if (h != NULL)
6118         {
6119           /* We know is_branch_reloc (r_type) is true.  */
6120           if (h->type == STT_GNU_IFUNC
6121               || r_type == R_PPC_PLTREL24)
6122             plist = &h->plt.plist;
6123         }
6124       else if (sym_type == STT_GNU_IFUNC
6125                && elf_local_got_offsets (abfd) != NULL)
6126         {
6127           bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6128           struct plt_entry **local_plt = (struct plt_entry **)
6129             (local_got_offsets + symtab_hdr->sh_info);
6130           plist = local_plt + ELF32_R_SYM (irel->r_info);
6131         }
6132       if (plist != NULL)
6133         {
6134           bfd_vma addend = 0;
6135           struct plt_entry *ent;
6136
6137           if (r_type == R_PPC_PLTREL24 && link_info->shared)
6138             addend = irel->r_addend;
6139           ent = find_plt_ent (plist, got2, addend);
6140           if (ent != NULL)
6141             {
6142               if (htab->plt_type == PLT_NEW
6143                   || h == NULL
6144                   || !htab->elf.dynamic_sections_created
6145                   || h->dynindx == -1)
6146                 {
6147                   tsec = htab->glink;
6148                   toff = ent->glink_offset;
6149                 }
6150               else
6151                 {
6152                   tsec = htab->plt;
6153                   toff = ent->plt.offset;
6154                 }
6155             }
6156         }
6157
6158       /* If the branch and target are in the same section, you have
6159          no hope of adding stubs.  We'll error out later should the
6160          branch overflow.  */
6161       if (tsec == isec)
6162         continue;
6163
6164       /* There probably isn't any reason to handle symbols in
6165          SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
6166          attribute for a code section, and we are only looking at
6167          branches.  However, implement it correctly here as a
6168          reference for other target relax_section functions.  */
6169       if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
6170         {
6171           /* At this stage in linking, no SEC_MERGE symbol has been
6172              adjusted, so all references to such symbols need to be
6173              passed through _bfd_merged_section_offset.  (Later, in
6174              relocate_section, all SEC_MERGE symbols *except* for
6175              section symbols have been adjusted.)
6176
6177              gas may reduce relocations against symbols in SEC_MERGE
6178              sections to a relocation against the section symbol when
6179              the original addend was zero.  When the reloc is against
6180              a section symbol we should include the addend in the
6181              offset passed to _bfd_merged_section_offset, since the
6182              location of interest is the original symbol.  On the
6183              other hand, an access to "sym+addend" where "sym" is not
6184              a section symbol should not include the addend;  Such an
6185              access is presumed to be an offset from "sym";  The
6186              location of interest is just "sym".  */
6187           if (sym_type == STT_SECTION)
6188             toff += irel->r_addend;
6189
6190           toff = _bfd_merged_section_offset (abfd, &tsec,
6191                                              elf_section_data (tsec)->sec_info,
6192                                              toff);
6193
6194           if (sym_type != STT_SECTION)
6195             toff += irel->r_addend;
6196         }
6197       /* PLTREL24 addends are special.  */
6198       else if (r_type != R_PPC_PLTREL24)
6199         toff += irel->r_addend;
6200
6201       /* Attempted -shared link of non-pic code loses.  */
6202       if (tsec->output_section == NULL)
6203         continue;
6204
6205       roff = irel->r_offset;
6206
6207       /* If the branch is in range, no need to do anything.  */
6208       if (tsec != bfd_und_section_ptr
6209           && (!link_info->relocatable
6210               /* A relocatable link may have sections moved during
6211                  final link, so do not presume they remain in range.  */
6212               || tsec->output_section == isec->output_section))
6213         {
6214           bfd_vma symaddr, reladdr;
6215
6216           symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6217           reladdr = isec->output_section->vma + isec->output_offset + roff;
6218           if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
6219             continue;
6220         }
6221
6222       /* Look for an existing fixup to this address.  */
6223       for (f = fixups; f ; f = f->next)
6224         if (f->tsec == tsec && f->toff == toff)
6225           break;
6226
6227       if (f == NULL)
6228         {
6229           size_t size;
6230           unsigned long stub_rtype;
6231
6232           val = trampoff - roff;
6233           if (val >= max_branch_offset)
6234             /* Oh dear, we can't reach a trampoline.  Don't try to add
6235                one.  We'll report an error later.  */
6236             continue;
6237
6238           if (link_info->shared)
6239             {
6240               size = 4 * ARRAY_SIZE (shared_stub_entry);
6241               insn_offset = 12;
6242               stub_rtype = R_PPC_RELAX32PC;
6243             }
6244           else
6245             {
6246               size = 4 * ARRAY_SIZE (stub_entry);
6247               insn_offset = 0;
6248               stub_rtype = R_PPC_RELAX32;
6249             }
6250
6251           if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
6252               != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
6253             abort ();
6254           if (tsec == htab->plt
6255               || tsec == htab->glink)
6256             stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
6257
6258           /* Hijack the old relocation.  Since we need two
6259              relocations for this use a "composite" reloc.  */
6260           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6261                                        stub_rtype);
6262           irel->r_offset = trampoff + insn_offset;
6263           if (r_type == R_PPC_PLTREL24)
6264             irel->r_addend = 0;
6265
6266           /* Record the fixup so we don't do it again this section.  */
6267           f = bfd_malloc (sizeof (*f));
6268           f->next = fixups;
6269           f->tsec = tsec;
6270           f->toff = toff;
6271           f->trampoff = trampoff;
6272           fixups = f;
6273
6274           trampoff += size;
6275           changes++;
6276         }
6277       else
6278         {
6279           val = f->trampoff - roff;
6280           if (val >= max_branch_offset)
6281             continue;
6282
6283           /* Nop out the reloc, since we're finalizing things here.  */
6284           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6285         }
6286
6287       /* Get the section contents.  */
6288       if (contents == NULL)
6289         {
6290           /* Get cached copy if it exists.  */
6291           if (elf_section_data (isec)->this_hdr.contents != NULL)
6292             contents = elf_section_data (isec)->this_hdr.contents;
6293           else
6294             {
6295               /* Go get them off disk.  */
6296               if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6297                 goto error_return;
6298             }
6299         }
6300
6301       /* Fix up the existing branch to hit the trampoline.  */
6302       hit_addr = contents + roff;
6303       switch (r_type)
6304         {
6305         case R_PPC_REL24:
6306         case R_PPC_LOCAL24PC:
6307         case R_PPC_PLTREL24:
6308           t0 = bfd_get_32 (abfd, hit_addr);
6309           t0 &= ~0x3fffffc;
6310           t0 |= val & 0x3fffffc;
6311           bfd_put_32 (abfd, t0, hit_addr);
6312           break;
6313
6314         case R_PPC_REL14:
6315         case R_PPC_REL14_BRTAKEN:
6316         case R_PPC_REL14_BRNTAKEN:
6317           t0 = bfd_get_32 (abfd, hit_addr);
6318           t0 &= ~0xfffc;
6319           t0 |= val & 0xfffc;
6320           bfd_put_32 (abfd, t0, hit_addr);
6321           break;
6322         }
6323     }
6324
6325   /* Write out the trampolines.  */
6326   if (fixups != NULL)
6327     {
6328       const int *stub;
6329       bfd_byte *dest;
6330       bfd_vma val;
6331       int i, size;
6332
6333       do
6334         {
6335           struct one_fixup *f = fixups;
6336           fixups = fixups->next;
6337           free (f);
6338         }
6339       while (fixups);
6340
6341       contents = bfd_realloc_or_free (contents, trampoff);
6342       if (contents == NULL)
6343         goto error_return;
6344
6345       isec->size = (isec->size + 3) & (bfd_vma) -4;
6346       /* Branch around the trampolines.  */
6347       val = B + trampoff - isec->size;
6348       dest = contents + isec->size;
6349       isec->size = trampoff;
6350       bfd_put_32 (abfd, val, dest);
6351       dest += 4;
6352
6353       if (link_info->shared)
6354         {
6355           stub = shared_stub_entry;
6356           size = ARRAY_SIZE (shared_stub_entry);
6357         }
6358       else
6359         {
6360           stub = stub_entry;
6361           size = ARRAY_SIZE (stub_entry);
6362         }
6363
6364       i = 0;
6365       while (dest < contents + trampoff)
6366         {
6367           bfd_put_32 (abfd, stub[i], dest);
6368           i++;
6369           if (i == size)
6370             i = 0;
6371           dest += 4;
6372         }
6373       BFD_ASSERT (i == 0);
6374     }
6375
6376   if (isymbuf != NULL
6377       && symtab_hdr->contents != (unsigned char *) isymbuf)
6378     {
6379       if (! link_info->keep_memory)
6380         free (isymbuf);
6381       else
6382         {
6383           /* Cache the symbols for elf_link_input_bfd.  */
6384           symtab_hdr->contents = (unsigned char *) isymbuf;
6385         }
6386     }
6387
6388   if (contents != NULL
6389       && elf_section_data (isec)->this_hdr.contents != contents)
6390     {
6391       if (!changes && !link_info->keep_memory)
6392         free (contents);
6393       else
6394         {
6395           /* Cache the section contents for elf_link_input_bfd.  */
6396           elf_section_data (isec)->this_hdr.contents = contents;
6397         }
6398     }
6399
6400   if (changes != 0)
6401     {
6402       /* Append sufficient NOP relocs so we can write out relocation
6403          information for the trampolines.  */
6404       Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6405                                                   * sizeof (*new_relocs));
6406       unsigned ix;
6407       
6408       if (!new_relocs)
6409         goto error_return;
6410       memcpy (new_relocs, internal_relocs,
6411               isec->reloc_count * sizeof (*new_relocs));
6412       for (ix = changes; ix--;)
6413         {
6414           irel = new_relocs + ix + isec->reloc_count;
6415
6416           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6417         }
6418       if (internal_relocs != elf_section_data (isec)->relocs)
6419         free (internal_relocs);
6420       elf_section_data (isec)->relocs = new_relocs;
6421       isec->reloc_count += changes;
6422       elf_section_data (isec)->rel_hdr.sh_size
6423         += changes * elf_section_data (isec)->rel_hdr.sh_entsize;
6424     }
6425   else if (elf_section_data (isec)->relocs != internal_relocs)
6426     free (internal_relocs);
6427
6428   *again = changes != 0;
6429   if (!*again && link_info->relocatable)
6430     {
6431       /* Convert the internal relax relocs to external form.  */
6432       for (irel = internal_relocs; irel < irelend; irel++)
6433         if (ELF32_R_TYPE (irel->r_info) == R_PPC_RELAX32)
6434           {
6435             unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
6436
6437             /* Rewrite the reloc and convert one of the trailing nop
6438                relocs to describe this relocation.  */
6439             BFD_ASSERT (ELF32_R_TYPE (irelend[-1].r_info) == R_PPC_NONE);
6440             /* The relocs are at the bottom 2 bytes */
6441             irel[0].r_offset += 2;
6442             memmove (irel + 1, irel, (irelend - irel - 1) * sizeof (*irel));
6443             irel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
6444             irel[1].r_offset += 4;
6445             irel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
6446             irel++;
6447           }
6448     }
6449   
6450   return TRUE;
6451
6452  error_return:
6453   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
6454     free (isymbuf);
6455   if (contents != NULL
6456       && elf_section_data (isec)->this_hdr.contents != contents)
6457     free (contents);
6458   if (internal_relocs != NULL
6459       && elf_section_data (isec)->relocs != internal_relocs)
6460     free (internal_relocs);
6461   return FALSE;
6462 }
6463 \f
6464 /* What to do when ld finds relocations against symbols defined in
6465    discarded sections.  */
6466
6467 static unsigned int
6468 ppc_elf_action_discarded (asection *sec)
6469 {
6470   if (strcmp (".fixup", sec->name) == 0)
6471     return 0;
6472
6473   if (strcmp (".got2", sec->name) == 0)
6474     return 0;
6475
6476   return _bfd_elf_default_action_discarded (sec);
6477 }
6478 \f
6479 /* Fill in the address for a pointer generated in a linker section.  */
6480
6481 static bfd_vma
6482 elf_finish_pointer_linker_section (bfd *input_bfd,
6483                                    elf_linker_section_t *lsect,
6484                                    struct elf_link_hash_entry *h,
6485                                    bfd_vma relocation,
6486                                    const Elf_Internal_Rela *rel)
6487 {
6488   elf_linker_section_pointers_t *linker_section_ptr;
6489
6490   BFD_ASSERT (lsect != NULL);
6491
6492   if (h != NULL)
6493     {
6494       /* Handle global symbol.  */
6495       struct ppc_elf_link_hash_entry *eh;
6496
6497       eh = (struct ppc_elf_link_hash_entry *) h;
6498       BFD_ASSERT (eh->elf.def_regular);
6499       linker_section_ptr = eh->linker_section_pointer;
6500     }
6501   else
6502     {
6503       /* Handle local symbol.  */
6504       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6505
6506       BFD_ASSERT (is_ppc_elf (input_bfd));
6507       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6508       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6509     }
6510
6511   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6512                                                         rel->r_addend,
6513                                                         lsect);
6514   BFD_ASSERT (linker_section_ptr != NULL);
6515
6516   /* Offset will always be a multiple of four, so use the bottom bit
6517      as a "written" flag.  */
6518   if ((linker_section_ptr->offset & 1) == 0)
6519     {
6520       bfd_put_32 (lsect->section->owner,
6521                   relocation + linker_section_ptr->addend,
6522                   lsect->section->contents + linker_section_ptr->offset);
6523       linker_section_ptr->offset += 1;
6524     }
6525
6526   relocation = (lsect->section->output_section->vma
6527                 + lsect->section->output_offset
6528                 + linker_section_ptr->offset - 1
6529                 - SYM_VAL (lsect->sym));
6530
6531 #ifdef DEBUG
6532   fprintf (stderr,
6533            "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6534            lsect->name, (long) relocation, (long) relocation);
6535 #endif
6536
6537   return relocation;
6538 }
6539
6540 #define PPC_LO(v) ((v) & 0xffff)
6541 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6542 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6543
6544 static void
6545 write_glink_stub (struct plt_entry *ent, asection *plt_sec, unsigned char *p,
6546                   struct bfd_link_info *info)
6547 {
6548   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6549   bfd *output_bfd = info->output_bfd;
6550   bfd_vma plt;
6551
6552   plt = ((ent->plt.offset & ~1)
6553          + plt_sec->output_section->vma
6554          + plt_sec->output_offset);
6555
6556   if (info->shared)
6557     {
6558       bfd_vma got = 0;
6559
6560       if (ent->addend >= 32768)
6561         got = (ent->addend
6562                + ent->sec->output_section->vma
6563                + ent->sec->output_offset);
6564       else if (htab->elf.hgot != NULL)
6565         got = SYM_VAL (htab->elf.hgot);
6566
6567       plt -= got;
6568
6569       if (plt + 0x8000 < 0x10000)
6570         {
6571           bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6572           p += 4;
6573           bfd_put_32 (output_bfd, MTCTR_11, p);
6574           p += 4;
6575           bfd_put_32 (output_bfd, BCTR, p);
6576           p += 4;
6577           bfd_put_32 (output_bfd, NOP, p);
6578           p += 4;
6579         }
6580       else
6581         {
6582           bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6583           p += 4;
6584           bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6585           p += 4;
6586           bfd_put_32 (output_bfd, MTCTR_11, p);
6587           p += 4;
6588           bfd_put_32 (output_bfd, BCTR, p);
6589           p += 4;
6590         }
6591     }
6592   else
6593     {
6594       bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6595       p += 4;
6596       bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6597       p += 4;
6598       bfd_put_32 (output_bfd, MTCTR_11, p);
6599       p += 4;
6600       bfd_put_32 (output_bfd, BCTR, p);
6601       p += 4;
6602     }
6603 }
6604
6605 /* Return true if symbol is defined statically.  */
6606
6607 static bfd_boolean
6608 is_static_defined (struct elf_link_hash_entry *h)
6609 {
6610   return ((h->root.type == bfd_link_hash_defined
6611            || h->root.type == bfd_link_hash_defweak)
6612           && h->root.u.def.section != NULL
6613           && h->root.u.def.section->output_section != NULL);
6614 }
6615
6616 /* If INSN is an opcode that may be used with an @tls operand, return
6617    the transformed insn for TLS optimisation, otherwise return 0.  If
6618    REG is non-zero only match an insn with RB or RA equal to REG.  */
6619
6620 unsigned int
6621 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6622 {
6623   unsigned int rtra;
6624
6625   if ((insn & (0x3f << 26)) != 31 << 26)
6626     return 0;
6627
6628   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6629     rtra = insn & ((1 << 26) - (1 << 16));
6630   else if (((insn >> 16) & 0x1f) == reg)
6631     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6632   else
6633     return 0;
6634
6635   if ((insn & (0x3ff << 1)) == 266 << 1)
6636     /* add -> addi.  */
6637     insn = 14 << 26;
6638   else if ((insn & (0x1f << 1)) == 23 << 1
6639            && ((insn & (0x1f << 6)) < 14 << 6
6640                || ((insn & (0x1f << 6)) >= 16 << 6
6641                    && (insn & (0x1f << 6)) < 24 << 6)))
6642     /* load and store indexed -> dform.  */
6643     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
6644   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6645     /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
6646     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6647   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6648     /* lwax -> lwa.  */
6649     insn = (58 << 26) | 2;
6650   else
6651     return 0;
6652   insn |= rtra;
6653   return insn;
6654 }
6655
6656 /* The RELOCATE_SECTION function is called by the ELF backend linker
6657    to handle the relocations for a section.
6658
6659    The relocs are always passed as Rela structures; if the section
6660    actually uses Rel structures, the r_addend field will always be
6661    zero.
6662
6663    This function is responsible for adjust the section contents as
6664    necessary, and (if using Rela relocs and generating a
6665    relocatable output file) adjusting the reloc addend as
6666    necessary.
6667
6668    This function does not have to worry about setting the reloc
6669    address or the reloc symbol index.
6670
6671    LOCAL_SYMS is a pointer to the swapped in local symbols.
6672
6673    LOCAL_SECTIONS is an array giving the section in the input file
6674    corresponding to the st_shndx field of each local symbol.
6675
6676    The global hash table entry for the global symbols can be found
6677    via elf_sym_hashes (input_bfd).
6678
6679    When generating relocatable output, this function must handle
6680    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
6681    going to be the section symbol corresponding to the output
6682    section, which means that the addend must be adjusted
6683    accordingly.  */
6684
6685 static bfd_boolean
6686 ppc_elf_relocate_section (bfd *output_bfd,
6687                           struct bfd_link_info *info,
6688                           bfd *input_bfd,
6689                           asection *input_section,
6690                           bfd_byte *contents,
6691                           Elf_Internal_Rela *relocs,
6692                           Elf_Internal_Sym *local_syms,
6693                           asection **local_sections)
6694 {
6695   Elf_Internal_Shdr *symtab_hdr;
6696   struct elf_link_hash_entry **sym_hashes;
6697   struct ppc_elf_link_hash_table *htab;
6698   Elf_Internal_Rela *rel;
6699   Elf_Internal_Rela *relend;
6700   Elf_Internal_Rela outrel;
6701   asection *got2, *sreloc = NULL;
6702   bfd_vma *local_got_offsets;
6703   bfd_boolean ret = TRUE;
6704   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
6705   bfd_boolean is_vxworks_tls;
6706
6707 #ifdef DEBUG
6708   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
6709                       "%ld relocations%s",
6710                       input_bfd, input_section,
6711                       (long) input_section->reloc_count,
6712                       (info->relocatable) ? " (relocatable)" : "");
6713 #endif
6714
6715   got2 = bfd_get_section_by_name (input_bfd, ".got2");
6716
6717   /* Initialize howto table if not already done.  */
6718   if (!ppc_elf_howto_table[R_PPC_ADDR32])
6719     ppc_elf_howto_init ();
6720
6721   htab = ppc_elf_hash_table (info);
6722   local_got_offsets = elf_local_got_offsets (input_bfd);
6723   symtab_hdr = &elf_symtab_hdr (input_bfd);
6724   sym_hashes = elf_sym_hashes (input_bfd);
6725   /* We have to handle relocations in vxworks .tls_vars sections
6726      specially, because the dynamic loader is 'weird'.  */
6727   is_vxworks_tls = (htab->is_vxworks && info->shared
6728                     && !strcmp (input_section->output_section->name,
6729                                 ".tls_vars"));
6730   rel = relocs;
6731   relend = relocs + input_section->reloc_count;
6732   for (; rel < relend; rel++)
6733     {
6734       enum elf_ppc_reloc_type r_type;
6735       bfd_vma addend;
6736       bfd_reloc_status_type r;
6737       Elf_Internal_Sym *sym;
6738       asection *sec;
6739       struct elf_link_hash_entry *h;
6740       const char *sym_name;
6741       reloc_howto_type *howto;
6742       unsigned long r_symndx;
6743       bfd_vma relocation;
6744       bfd_vma branch_bit, from;
6745       bfd_boolean unresolved_reloc;
6746       bfd_boolean warned;
6747       unsigned int tls_type, tls_mask, tls_gd;
6748       struct plt_entry **ifunc;
6749
6750       r_type = ELF32_R_TYPE (rel->r_info);
6751       sym = NULL;
6752       sec = NULL;
6753       h = NULL;
6754       unresolved_reloc = FALSE;
6755       warned = FALSE;
6756       r_symndx = ELF32_R_SYM (rel->r_info);
6757
6758       if (r_symndx < symtab_hdr->sh_info)
6759         {
6760           sym = local_syms + r_symndx;
6761           sec = local_sections[r_symndx];
6762           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
6763
6764           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6765         }
6766       else
6767         {
6768           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6769                                    r_symndx, symtab_hdr, sym_hashes,
6770                                    h, sec, relocation,
6771                                    unresolved_reloc, warned);
6772
6773           sym_name = h->root.root.string;
6774         }
6775
6776       if (sec != NULL && elf_discarded_section (sec))
6777         {
6778           /* For relocs against symbols from removed linkonce sections,
6779              or sections discarded by a linker script, we just want the
6780              section contents zeroed.  Avoid any special processing.  */
6781           howto = NULL;
6782           if (r_type < R_PPC_max)
6783             howto = ppc_elf_howto_table[r_type];
6784           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
6785           rel->r_info = 0;
6786           rel->r_addend = 0;
6787           continue;
6788         }
6789
6790       if (info->relocatable)
6791         {
6792           if (got2 != NULL
6793               && r_type == R_PPC_PLTREL24
6794               && rel->r_addend >= 32768)
6795             {
6796               /* R_PPC_PLTREL24 is rather special.  If non-zero, the
6797                  addend specifies the GOT pointer offset within .got2.  */
6798               rel->r_addend += got2->output_offset;
6799             }
6800           continue;
6801         }
6802
6803       /* TLS optimizations.  Replace instruction sequences and relocs
6804          based on information we collected in tls_optimize.  We edit
6805          RELOCS so that --emit-relocs will output something sensible
6806          for the final instruction stream.  */
6807       tls_mask = 0;
6808       tls_gd = 0;
6809       if (h != NULL)
6810         tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
6811       else if (local_got_offsets != NULL)
6812         {
6813           struct plt_entry **local_plt;
6814           char *lgot_masks;
6815           local_plt
6816             = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
6817           lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
6818           tls_mask = lgot_masks[r_symndx];
6819         }
6820
6821       /* Ensure reloc mapping code below stays sane.  */
6822       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
6823           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
6824           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
6825           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
6826           || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
6827           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
6828           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
6829           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
6830         abort ();
6831       switch (r_type)
6832         {
6833         default:
6834           break;
6835
6836         case R_PPC_GOT_TPREL16:
6837         case R_PPC_GOT_TPREL16_LO:
6838           if ((tls_mask & TLS_TLS) != 0
6839               && (tls_mask & TLS_TPREL) == 0)
6840             {
6841               bfd_vma insn;
6842
6843               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
6844               insn &= 31 << 21;
6845               insn |= 0x3c020000;       /* addis 0,2,0 */
6846               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
6847               r_type = R_PPC_TPREL16_HA;
6848               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6849             }
6850           break;
6851
6852         case R_PPC_TLS:
6853           if ((tls_mask & TLS_TLS) != 0
6854               && (tls_mask & TLS_TPREL) == 0)
6855             {
6856               bfd_vma insn;
6857
6858               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6859               insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
6860               if (insn == 0)
6861                 abort ();
6862               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6863               r_type = R_PPC_TPREL16_LO;
6864               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6865
6866               /* Was PPC_TLS which sits on insn boundary, now
6867                  PPC_TPREL16_LO which is at low-order half-word.  */
6868               rel->r_offset += d_offset;
6869             }
6870           break;
6871
6872         case R_PPC_GOT_TLSGD16_HI:
6873         case R_PPC_GOT_TLSGD16_HA:
6874           tls_gd = TLS_TPRELGD;
6875           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6876             goto tls_gdld_hi;
6877           break;
6878
6879         case R_PPC_GOT_TLSLD16_HI:
6880         case R_PPC_GOT_TLSLD16_HA:
6881           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6882             {
6883             tls_gdld_hi:
6884               if ((tls_mask & tls_gd) != 0)
6885                 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6886                           + R_PPC_GOT_TPREL16);
6887               else
6888                 {
6889                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
6890                   rel->r_offset -= d_offset;
6891                   r_type = R_PPC_NONE;
6892                 }
6893               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6894             }
6895           break;
6896
6897         case R_PPC_GOT_TLSGD16:
6898         case R_PPC_GOT_TLSGD16_LO:
6899           tls_gd = TLS_TPRELGD;
6900           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6901             goto tls_ldgd_opt;
6902           break;
6903
6904         case R_PPC_GOT_TLSLD16:
6905         case R_PPC_GOT_TLSLD16_LO:
6906           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6907             {
6908               unsigned int insn1, insn2;
6909               bfd_vma offset;
6910
6911             tls_ldgd_opt:
6912               offset = (bfd_vma) -1;
6913               /* If not using the newer R_PPC_TLSGD/LD to mark
6914                  __tls_get_addr calls, we must trust that the call
6915                  stays with its arg setup insns, ie. that the next
6916                  reloc is the __tls_get_addr call associated with
6917                  the current reloc.  Edit both insns.  */
6918               if (input_section->has_tls_get_addr_call
6919                   && rel + 1 < relend
6920                   && branch_reloc_hash_match (input_bfd, rel + 1,
6921                                               htab->tls_get_addr))
6922                 offset = rel[1].r_offset;
6923               if ((tls_mask & tls_gd) != 0)
6924                 {
6925                   /* IE */
6926                   insn1 = bfd_get_32 (output_bfd,
6927                                       contents + rel->r_offset - d_offset);
6928                   insn1 &= (1 << 26) - 1;
6929                   insn1 |= 32 << 26;    /* lwz */
6930                   if (offset != (bfd_vma) -1)
6931                     {
6932                       rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
6933                       insn2 = 0x7c631214;       /* add 3,3,2 */
6934                       bfd_put_32 (output_bfd, insn2, contents + offset);
6935                     }
6936                   r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6937                             + R_PPC_GOT_TPREL16);
6938                   rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6939                 }
6940               else
6941                 {
6942                   /* LE */
6943                   insn1 = 0x3c620000;   /* addis 3,2,0 */
6944                   if (tls_gd == 0)
6945                     {
6946                       /* Was an LD reloc.  */
6947                       for (r_symndx = 0;
6948                            r_symndx < symtab_hdr->sh_info;
6949                            r_symndx++)
6950                         if (local_sections[r_symndx] == sec)
6951                           break;
6952                       if (r_symndx >= symtab_hdr->sh_info)
6953                         r_symndx = 0;
6954                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
6955                       if (r_symndx != 0)
6956                         rel->r_addend -= (local_syms[r_symndx].st_value
6957                                           + sec->output_offset
6958                                           + sec->output_section->vma);
6959                     }
6960                   r_type = R_PPC_TPREL16_HA;
6961                   rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6962                   if (offset != (bfd_vma) -1)
6963                     {
6964                       rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
6965                       rel[1].r_offset = offset + d_offset;
6966                       rel[1].r_addend = rel->r_addend;
6967                       insn2 = 0x38630000;       /* addi 3,3,0 */
6968                       bfd_put_32 (output_bfd, insn2, contents + offset);
6969                     }
6970                 }
6971               bfd_put_32 (output_bfd, insn1,
6972                           contents + rel->r_offset - d_offset);
6973               if (tls_gd == 0)
6974                 {
6975                   /* We changed the symbol on an LD reloc.  Start over
6976                      in order to get h, sym, sec etc. right.  */
6977                   rel--;
6978                   continue;
6979                 }
6980             }
6981           break;
6982
6983         case R_PPC_TLSGD:
6984           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6985             {
6986               unsigned int insn2;
6987               bfd_vma offset = rel->r_offset;
6988
6989               if ((tls_mask & TLS_TPRELGD) != 0)
6990                 {
6991                   /* IE */
6992                   r_type = R_PPC_NONE;
6993                   insn2 = 0x7c631214;   /* add 3,3,2 */
6994                 }
6995               else
6996                 {
6997                   /* LE */
6998                   r_type = R_PPC_TPREL16_LO;
6999                   rel->r_offset += d_offset;
7000                   insn2 = 0x38630000;   /* addi 3,3,0 */
7001                 }
7002               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7003               bfd_put_32 (output_bfd, insn2, contents + offset);
7004               /* Zap the reloc on the _tls_get_addr call too.  */
7005               BFD_ASSERT (offset == rel[1].r_offset);
7006               rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7007             }
7008           break;
7009
7010         case R_PPC_TLSLD:
7011           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7012             {
7013               unsigned int insn2;
7014
7015               for (r_symndx = 0;
7016                    r_symndx < symtab_hdr->sh_info;
7017                    r_symndx++)
7018                 if (local_sections[r_symndx] == sec)
7019                   break;
7020               if (r_symndx >= symtab_hdr->sh_info)
7021                 r_symndx = 0;
7022               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7023               if (r_symndx != 0)
7024                 rel->r_addend -= (local_syms[r_symndx].st_value
7025                                   + sec->output_offset
7026                                   + sec->output_section->vma);
7027
7028               rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7029               rel->r_offset += d_offset;
7030               insn2 = 0x38630000;       /* addi 3,3,0 */
7031               bfd_put_32 (output_bfd, insn2,
7032                           contents + rel->r_offset - d_offset);
7033               /* Zap the reloc on the _tls_get_addr call too.  */
7034               BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
7035               rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7036               rel--;
7037               continue;
7038             }
7039           break;
7040         }
7041
7042       /* Handle other relocations that tweak non-addend part of insn.  */
7043       branch_bit = 0;
7044       switch (r_type)
7045         {
7046         default:
7047           break;
7048
7049           /* Branch taken prediction relocations.  */
7050         case R_PPC_ADDR14_BRTAKEN:
7051         case R_PPC_REL14_BRTAKEN:
7052           branch_bit = BRANCH_PREDICT_BIT;
7053           /* Fall thru */
7054
7055           /* Branch not taken prediction relocations.  */
7056         case R_PPC_ADDR14_BRNTAKEN:
7057         case R_PPC_REL14_BRNTAKEN:
7058           {
7059             bfd_vma insn;
7060
7061             insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7062             insn &= ~BRANCH_PREDICT_BIT;
7063             insn |= branch_bit;
7064
7065             from = (rel->r_offset
7066                     + input_section->output_offset
7067                     + input_section->output_section->vma);
7068
7069             /* Invert 'y' bit if not the default.  */
7070             if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7071               insn ^= BRANCH_PREDICT_BIT;
7072
7073             bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7074             break;
7075           }
7076         }
7077
7078       ifunc = NULL;
7079       if (!htab->is_vxworks)
7080         {
7081           struct plt_entry *ent;
7082
7083           if (h != NULL)
7084             {
7085               if (h->type == STT_GNU_IFUNC)
7086                 ifunc = &h->plt.plist;
7087             }
7088           else if (local_got_offsets != NULL
7089                    && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7090             {
7091               struct plt_entry **local_plt;
7092
7093               local_plt = (struct plt_entry **) (local_got_offsets
7094                                                  + symtab_hdr->sh_info);
7095               ifunc = local_plt + r_symndx;
7096             }
7097
7098           ent = NULL;
7099           if (ifunc != NULL
7100               && (!info->shared
7101                   || is_branch_reloc (r_type)))
7102             {
7103               addend = 0;
7104               if (r_type == R_PPC_PLTREL24 && info->shared)
7105                 addend = rel->r_addend;
7106               ent = find_plt_ent (ifunc, got2, addend);
7107             }
7108           if (ent != NULL)
7109             {
7110               if (h == NULL && (ent->plt.offset & 1) == 0)
7111                 {
7112                   Elf_Internal_Rela rela;
7113                   bfd_byte *loc;
7114
7115                   rela.r_offset = (htab->iplt->output_section->vma
7116                                    + htab->iplt->output_offset
7117                                    + ent->plt.offset);
7118                   rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7119                   rela.r_addend = relocation;
7120                   loc = htab->reliplt->contents;
7121                   loc += (htab->reliplt->reloc_count++
7122                           * sizeof (Elf32_External_Rela));
7123                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7124
7125                   ent->plt.offset |= 1;
7126                 }
7127               if (h == NULL && (ent->glink_offset & 1) == 0)
7128                 {
7129                   unsigned char *p = ((unsigned char *) htab->glink->contents
7130                                       + ent->glink_offset);
7131                   write_glink_stub (ent, htab->iplt, p, info);
7132                   ent->glink_offset |= 1;
7133                 }
7134
7135               unresolved_reloc = FALSE;
7136               if (htab->plt_type == PLT_NEW
7137                   || !htab->elf.dynamic_sections_created
7138                   || h == NULL)
7139                 relocation = (htab->glink->output_section->vma
7140                               + htab->glink->output_offset
7141                               + (ent->glink_offset & ~1));
7142               else
7143                 relocation = (htab->plt->output_section->vma
7144                               + htab->plt->output_offset
7145                               + ent->plt.offset);
7146             }
7147         }
7148
7149       addend = rel->r_addend;
7150       tls_type = 0;
7151       howto = NULL;
7152       if (r_type < R_PPC_max)
7153         howto = ppc_elf_howto_table[r_type];
7154       switch (r_type)
7155         {
7156         default:
7157           (*_bfd_error_handler)
7158             (_("%B: unknown relocation type %d for symbol %s"),
7159              input_bfd, (int) r_type, sym_name);
7160
7161           bfd_set_error (bfd_error_bad_value);
7162           ret = FALSE;
7163           continue;
7164
7165         case R_PPC_NONE:
7166         case R_PPC_TLS:
7167         case R_PPC_TLSGD:
7168         case R_PPC_TLSLD:
7169         case R_PPC_EMB_MRKREF:
7170         case R_PPC_GNU_VTINHERIT:
7171         case R_PPC_GNU_VTENTRY:
7172           continue;
7173
7174           /* GOT16 relocations.  Like an ADDR16 using the symbol's
7175              address in the GOT as relocation value instead of the
7176              symbol's value itself.  Also, create a GOT entry for the
7177              symbol and put the symbol value there.  */
7178         case R_PPC_GOT_TLSGD16:
7179         case R_PPC_GOT_TLSGD16_LO:
7180         case R_PPC_GOT_TLSGD16_HI:
7181         case R_PPC_GOT_TLSGD16_HA:
7182           tls_type = TLS_TLS | TLS_GD;
7183           goto dogot;
7184
7185         case R_PPC_GOT_TLSLD16:
7186         case R_PPC_GOT_TLSLD16_LO:
7187         case R_PPC_GOT_TLSLD16_HI:
7188         case R_PPC_GOT_TLSLD16_HA:
7189           tls_type = TLS_TLS | TLS_LD;
7190           goto dogot;
7191
7192         case R_PPC_GOT_TPREL16:
7193         case R_PPC_GOT_TPREL16_LO:
7194         case R_PPC_GOT_TPREL16_HI:
7195         case R_PPC_GOT_TPREL16_HA:
7196           tls_type = TLS_TLS | TLS_TPREL;
7197           goto dogot;
7198
7199         case R_PPC_GOT_DTPREL16:
7200         case R_PPC_GOT_DTPREL16_LO:
7201         case R_PPC_GOT_DTPREL16_HI:
7202         case R_PPC_GOT_DTPREL16_HA:
7203           tls_type = TLS_TLS | TLS_DTPREL;
7204           goto dogot;
7205
7206         case R_PPC_GOT16:
7207         case R_PPC_GOT16_LO:
7208         case R_PPC_GOT16_HI:
7209         case R_PPC_GOT16_HA:
7210           tls_mask = 0;
7211         dogot:
7212           {
7213             /* Relocation is to the entry for this symbol in the global
7214                offset table.  */
7215             bfd_vma off;
7216             bfd_vma *offp;
7217             unsigned long indx;
7218
7219             if (htab->got == NULL)
7220               abort ();
7221
7222             indx = 0;
7223             if (tls_type == (TLS_TLS | TLS_LD)
7224                 && (h == NULL
7225                     || !h->def_dynamic))
7226               offp = &htab->tlsld_got.offset;
7227             else if (h != NULL)
7228               {
7229                 bfd_boolean dyn;
7230                 dyn = htab->elf.dynamic_sections_created;
7231                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7232                     || (info->shared
7233                         && SYMBOL_REFERENCES_LOCAL (info, h)))
7234                   /* This is actually a static link, or it is a
7235                      -Bsymbolic link and the symbol is defined
7236                      locally, or the symbol was forced to be local
7237                      because of a version file.  */
7238                   ;
7239                 else
7240                   {
7241                     indx = h->dynindx;
7242                     unresolved_reloc = FALSE;
7243                   }
7244                 offp = &h->got.offset;
7245               }
7246             else
7247               {
7248                 if (local_got_offsets == NULL)
7249                   abort ();
7250                 offp = &local_got_offsets[r_symndx];
7251               }
7252
7253             /* The offset must always be a multiple of 4.  We use the
7254                least significant bit to record whether we have already
7255                processed this entry.  */
7256             off = *offp;
7257             if ((off & 1) != 0)
7258               off &= ~1;
7259             else
7260               {
7261                 unsigned int tls_m = (tls_mask
7262                                       & (TLS_LD | TLS_GD | TLS_DTPREL
7263                                          | TLS_TPREL | TLS_TPRELGD));
7264
7265                 if (offp == &htab->tlsld_got.offset)
7266                   tls_m = TLS_LD;
7267                 else if (h == NULL
7268                          || !h->def_dynamic)
7269                   tls_m &= ~TLS_LD;
7270
7271                 /* We might have multiple got entries for this sym.
7272                    Initialize them all.  */
7273                 do
7274                   {
7275                     int tls_ty = 0;
7276
7277                     if ((tls_m & TLS_LD) != 0)
7278                       {
7279                         tls_ty = TLS_TLS | TLS_LD;
7280                         tls_m &= ~TLS_LD;
7281                       }
7282                     else if ((tls_m & TLS_GD) != 0)
7283                       {
7284                         tls_ty = TLS_TLS | TLS_GD;
7285                         tls_m &= ~TLS_GD;
7286                       }
7287                     else if ((tls_m & TLS_DTPREL) != 0)
7288                       {
7289                         tls_ty = TLS_TLS | TLS_DTPREL;
7290                         tls_m &= ~TLS_DTPREL;
7291                       }
7292                     else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
7293                       {
7294                         tls_ty = TLS_TLS | TLS_TPREL;
7295                         tls_m = 0;
7296                       }
7297
7298                     /* Generate relocs for the dynamic linker.  */
7299                     if ((info->shared || indx != 0)
7300                         && (offp == &htab->tlsld_got.offset
7301                             || h == NULL
7302                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7303                             || h->root.type != bfd_link_hash_undefweak))
7304                       {
7305                         asection *rsec = htab->relgot;
7306                         bfd_byte * loc;
7307
7308                         outrel.r_offset = (htab->got->output_section->vma
7309                                            + htab->got->output_offset
7310                                            + off);
7311                         outrel.r_addend = 0;
7312                         if (tls_ty & (TLS_LD | TLS_GD))
7313                           {
7314                             outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
7315                             if (tls_ty == (TLS_TLS | TLS_GD))
7316                               {
7317                                 loc = rsec->contents;
7318                                 loc += (rsec->reloc_count++
7319                                         * sizeof (Elf32_External_Rela));
7320                                 bfd_elf32_swap_reloca_out (output_bfd,
7321                                                            &outrel, loc);
7322                                 outrel.r_offset += 4;
7323                                 outrel.r_info
7324                                   = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7325                               }
7326                           }
7327                         else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7328                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7329                         else if (tls_ty == (TLS_TLS | TLS_TPREL))
7330                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
7331                         else if (indx != 0)
7332                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
7333                         else if (ifunc != NULL)
7334                           outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7335                         else
7336                           outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7337                         if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
7338                           {
7339                             outrel.r_addend += relocation;
7340                             if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7341                               outrel.r_addend -= htab->elf.tls_sec->vma;
7342                           }
7343                         loc = rsec->contents;
7344                         loc += (rsec->reloc_count++
7345                                 * sizeof (Elf32_External_Rela));
7346                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7347                       }
7348
7349                     /* Init the .got section contents if we're not
7350                        emitting a reloc.  */
7351                     else
7352                       {
7353                         bfd_vma value = relocation;
7354
7355                         if (tls_ty == (TLS_TLS | TLS_LD))
7356                           value = 1;
7357                         else if (tls_ty != 0)
7358                           {
7359                             value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7360                             if (tls_ty == (TLS_TLS | TLS_TPREL))
7361                               value += DTP_OFFSET - TP_OFFSET;
7362
7363                             if (tls_ty == (TLS_TLS | TLS_GD))
7364                               {
7365                                 bfd_put_32 (output_bfd, value,
7366                                             htab->got->contents + off + 4);
7367                                 value = 1;
7368                               }
7369                           }
7370                         bfd_put_32 (output_bfd, value,
7371                                     htab->got->contents + off);
7372                       }
7373
7374                     off += 4;
7375                     if (tls_ty & (TLS_LD | TLS_GD))
7376                       off += 4;
7377                   }
7378                 while (tls_m != 0);
7379
7380                 off = *offp;
7381                 *offp = off | 1;
7382               }
7383
7384             if (off >= (bfd_vma) -2)
7385               abort ();
7386
7387             if ((tls_type & TLS_TLS) != 0)
7388               {
7389                 if (tls_type != (TLS_TLS | TLS_LD))
7390                   {
7391                     if ((tls_mask & TLS_LD) != 0
7392                         && !(h == NULL
7393                              || !h->def_dynamic))
7394                       off += 8;
7395                     if (tls_type != (TLS_TLS | TLS_GD))
7396                       {
7397                         if ((tls_mask & TLS_GD) != 0)
7398                           off += 8;
7399                         if (tls_type != (TLS_TLS | TLS_DTPREL))
7400                           {
7401                             if ((tls_mask & TLS_DTPREL) != 0)
7402                               off += 4;
7403                           }
7404                       }
7405                   }
7406               }
7407
7408             relocation = (htab->got->output_section->vma
7409                           + htab->got->output_offset
7410                           + off
7411                           - SYM_VAL (htab->elf.hgot));
7412
7413             /* Addends on got relocations don't make much sense.
7414                x+off@got is actually x@got+off, and since the got is
7415                generated by a hash table traversal, the value in the
7416                got at entry m+n bears little relation to the entry m.  */
7417             if (addend != 0)
7418               (*_bfd_error_handler)
7419                 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
7420                  input_bfd,
7421                  input_section,
7422                  (long) rel->r_offset,
7423                  howto->name,
7424                  sym_name);
7425           }
7426         break;
7427
7428         /* Relocations that need no special processing.  */
7429         case R_PPC_LOCAL24PC:
7430           /* It makes no sense to point a local relocation
7431              at a symbol not in this object.  */
7432           if (unresolved_reloc)
7433             {
7434               if (! (*info->callbacks->undefined_symbol) (info,
7435                                                           h->root.root.string,
7436                                                           input_bfd,
7437                                                           input_section,
7438                                                           rel->r_offset,
7439                                                           TRUE))
7440                 return FALSE;
7441               continue;
7442             }
7443           break;
7444
7445         case R_PPC_DTPREL16:
7446         case R_PPC_DTPREL16_LO:
7447         case R_PPC_DTPREL16_HI:
7448         case R_PPC_DTPREL16_HA:
7449           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7450           break;
7451
7452           /* Relocations that may need to be propagated if this is a shared
7453              object.  */
7454         case R_PPC_TPREL16:
7455         case R_PPC_TPREL16_LO:
7456         case R_PPC_TPREL16_HI:
7457         case R_PPC_TPREL16_HA:
7458           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7459           /* The TPREL16 relocs shouldn't really be used in shared
7460              libs as they will result in DT_TEXTREL being set, but
7461              support them anyway.  */
7462           goto dodyn;
7463
7464         case R_PPC_TPREL32:
7465           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7466           goto dodyn;
7467
7468         case R_PPC_DTPREL32:
7469           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7470           goto dodyn;
7471
7472         case R_PPC_DTPMOD32:
7473           relocation = 1;
7474           addend = 0;
7475           goto dodyn;
7476
7477         case R_PPC_REL16:
7478         case R_PPC_REL16_LO:
7479         case R_PPC_REL16_HI:
7480         case R_PPC_REL16_HA:
7481           break;
7482
7483         case R_PPC_REL32:
7484           if (h == NULL || h == htab->elf.hgot)
7485             break;
7486           /* fall through */
7487
7488         case R_PPC_ADDR32:
7489         case R_PPC_ADDR16:
7490         case R_PPC_ADDR16_LO:
7491         case R_PPC_ADDR16_HI:
7492         case R_PPC_ADDR16_HA:
7493         case R_PPC_UADDR32:
7494         case R_PPC_UADDR16:
7495           goto dodyn;
7496
7497         case R_PPC_REL24:
7498         case R_PPC_REL14:
7499         case R_PPC_REL14_BRTAKEN:
7500         case R_PPC_REL14_BRNTAKEN:
7501           /* If these relocations are not to a named symbol, they can be
7502              handled right here, no need to bother the dynamic linker.  */
7503           if (SYMBOL_CALLS_LOCAL (info, h)
7504               || h == htab->elf.hgot)
7505             break;
7506           /* fall through */
7507
7508         case R_PPC_ADDR24:
7509         case R_PPC_ADDR14:
7510         case R_PPC_ADDR14_BRTAKEN:
7511         case R_PPC_ADDR14_BRNTAKEN:
7512           if (h != NULL && !info->shared)
7513             break;
7514           /* fall through */
7515
7516         dodyn:
7517           if ((input_section->flags & SEC_ALLOC) == 0
7518               || is_vxworks_tls)
7519             break;
7520
7521           if ((info->shared
7522                && !(h != NULL
7523                     && ((h->root.type == bfd_link_hash_undefined
7524                          && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7525                              || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
7526                         || (h->root.type == bfd_link_hash_undefweak
7527                             && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
7528                && (must_be_dyn_reloc (info, r_type)
7529                    || !SYMBOL_CALLS_LOCAL (info, h)))
7530               || (ELIMINATE_COPY_RELOCS
7531                   && !info->shared
7532                   && h != NULL
7533                   && h->dynindx != -1
7534                   && !h->non_got_ref
7535                   && !h->def_regular))
7536             {
7537               int skip;
7538               bfd_byte * loc;
7539 #ifdef DEBUG
7540               fprintf (stderr, "ppc_elf_relocate_section needs to "
7541                        "create relocation for %s\n",
7542                        (h && h->root.root.string
7543                         ? h->root.root.string : "<unknown>"));
7544 #endif
7545
7546               /* When generating a shared object, these relocations
7547                  are copied into the output file to be resolved at run
7548                  time.  */
7549               if (sreloc == NULL)
7550                 {
7551                   sreloc = elf_section_data (input_section)->sreloc;
7552                   if (!htab->elf.dynamic_sections_created)
7553                     sreloc = htab->reliplt;
7554                   if (sreloc == NULL)
7555                     return FALSE;
7556                 }
7557
7558               skip = 0;
7559               outrel.r_offset =
7560                 _bfd_elf_section_offset (output_bfd, info, input_section,
7561                                          rel->r_offset);
7562               if (outrel.r_offset == (bfd_vma) -1
7563                   || outrel.r_offset == (bfd_vma) -2)
7564                 skip = (int) outrel.r_offset;
7565               outrel.r_offset += (input_section->output_section->vma
7566                                   + input_section->output_offset);
7567
7568               if (skip)
7569                 memset (&outrel, 0, sizeof outrel);
7570               else if ((h != NULL
7571                         && (h->root.type == bfd_link_hash_undefined
7572                             || h->root.type == bfd_link_hash_undefweak))
7573                        || !SYMBOL_REFERENCES_LOCAL (info, h))
7574                 {
7575                   unresolved_reloc = FALSE;
7576                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
7577                   outrel.r_addend = rel->r_addend;
7578                 }
7579               else
7580                 {
7581                   outrel.r_addend = relocation + rel->r_addend;
7582
7583                   if (r_type != R_PPC_ADDR32)
7584                     {
7585                       long indx = 0;
7586
7587                       if (ifunc != NULL)
7588                         {
7589                           /* If we get here when building a static
7590                              executable, then the libc startup function
7591                              responsible for applying indirect function
7592                              relocations is going to complain about
7593                              the reloc type.
7594                              If we get here when building a dynamic
7595                              executable, it will be because we have
7596                              a text relocation.  The dynamic loader
7597                              will set the text segment writable and
7598                              non-executable to apply text relocations.
7599                              So we'll segfault when trying to run the
7600                              indirection function to resolve the reloc.  */
7601                           (*_bfd_error_handler)
7602                             (_("%B(%A+0x%lx): relocation %s for indirect "
7603                                "function %s unsupported"),
7604                              input_bfd,
7605                              input_section,
7606                              (long) rel->r_offset,
7607                              howto->name,
7608                              sym_name);
7609                           ret = FALSE;
7610                         }
7611                       else if (r_symndx == 0 || bfd_is_abs_section (sec))
7612                         ;
7613                       else if (sec == NULL || sec->owner == NULL)
7614                         {
7615                           bfd_set_error (bfd_error_bad_value);
7616                           ret = FALSE;
7617                         }
7618                       else
7619                         {
7620                           asection *osec;
7621
7622                           /* We are turning this relocation into one
7623                              against a section symbol.  It would be
7624                              proper to subtract the symbol's value,
7625                              osec->vma, from the emitted reloc addend,
7626                              but ld.so expects buggy relocs.
7627                              FIXME: Why not always use a zero index?  */
7628                           osec = sec->output_section;
7629                           indx = elf_section_data (osec)->dynindx;
7630                           if (indx == 0)
7631                             {
7632                               osec = htab->elf.text_index_section;
7633                               indx = elf_section_data (osec)->dynindx;
7634                             }
7635                           BFD_ASSERT (indx != 0);
7636 #ifdef DEBUG
7637                           if (indx == 0)
7638                             printf ("indx=%ld section=%s flags=%08x name=%s\n",
7639                                     indx, osec->name, osec->flags,
7640                                     h->root.root.string);
7641 #endif
7642                         }
7643
7644                       outrel.r_info = ELF32_R_INFO (indx, r_type);
7645                     }
7646                   else if (ifunc != NULL)
7647                     outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7648                   else
7649                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7650                 }
7651
7652               loc = sreloc->contents;
7653               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
7654               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7655
7656               if (skip == -1)
7657                 continue;
7658
7659               /* This reloc will be computed at runtime.  We clear the memory
7660                  so that it contains predictable value.  */
7661               if (! skip
7662                   && ((input_section->flags & SEC_ALLOC) != 0
7663                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
7664                 {
7665                   relocation = howto->pc_relative ? outrel.r_offset : 0;
7666                   addend = 0;
7667                   break;
7668                 }
7669             }
7670           break;
7671
7672         case R_PPC_RELAX32PC_PLT:
7673         case R_PPC_RELAX32_PLT:
7674           if (h != NULL)
7675             {
7676               struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7677                                                     info->shared ? addend : 0);
7678               if (htab->plt_type == PLT_NEW)
7679                 relocation = (htab->glink->output_section->vma
7680                               + htab->glink->output_offset
7681                               + ent->glink_offset);
7682               else
7683                 relocation = (htab->plt->output_section->vma
7684                               + htab->plt->output_offset
7685                               + ent->plt.offset);
7686             }
7687           if (r_type == R_PPC_RELAX32_PLT)
7688             goto relax32;
7689           /* Fall thru */
7690
7691         case R_PPC_RELAX32PC:
7692           relocation -= (input_section->output_section->vma
7693                          + input_section->output_offset
7694                          + rel->r_offset - 4);
7695           /* Fall thru */
7696
7697         case R_PPC_RELAX32:
7698         relax32:
7699           {
7700             unsigned long t0;
7701             unsigned long t1;
7702
7703             t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
7704             t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
7705
7706             /* We're clearing the bits for R_PPC_ADDR16_HA
7707                and R_PPC_ADDR16_LO here.  */
7708             t0 &= ~0xffff;
7709             t1 &= ~0xffff;
7710
7711             /* t0 is HA, t1 is LO */
7712             relocation += addend;
7713             t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
7714             t1 |= relocation & 0xffff;
7715
7716             bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
7717             bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
7718
7719             /* Rewrite the reloc and convert one of the trailing nop
7720                relocs to describe this relocation.  */
7721             BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
7722             /* The relocs are at the bottom 2 bytes */
7723             rel[0].r_offset += 2;
7724             memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7725             rel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
7726             rel[1].r_offset += 4;
7727             rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
7728             rel++;
7729           }
7730           continue;
7731
7732           /* Indirect .sdata relocation.  */
7733         case R_PPC_EMB_SDAI16:
7734           BFD_ASSERT (htab->sdata[0].section != NULL);
7735           if (!is_static_defined (htab->sdata[0].sym))
7736             {
7737               unresolved_reloc = TRUE;
7738               break;
7739             }
7740           relocation
7741             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
7742                                                  h, relocation, rel);
7743           addend = 0;
7744           break;
7745
7746           /* Indirect .sdata2 relocation.  */
7747         case R_PPC_EMB_SDA2I16:
7748           BFD_ASSERT (htab->sdata[1].section != NULL);
7749           if (!is_static_defined (htab->sdata[1].sym))
7750             {
7751               unresolved_reloc = TRUE;
7752               break;
7753             }
7754           relocation
7755             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
7756                                                  h, relocation, rel);
7757           addend = 0;
7758           break;
7759
7760           /* Handle the TOC16 reloc.  We want to use the offset within the .got
7761              section, not the actual VMA.  This is appropriate when generating
7762              an embedded ELF object, for which the .got section acts like the
7763              AIX .toc section.  */
7764         case R_PPC_TOC16:                       /* phony GOT16 relocations */
7765           if (sec == NULL || sec->output_section == NULL)
7766             {
7767               unresolved_reloc = TRUE;
7768               break;
7769             }
7770           BFD_ASSERT (strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
7771                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
7772
7773           addend -= sec->output_section->vma + sec->output_offset + 0x8000;
7774           break;
7775
7776         case R_PPC_PLTREL24:
7777           if (h == NULL || ifunc != NULL)
7778             break;
7779           /* Relocation is to the entry for this symbol in the
7780              procedure linkage table.  */
7781           {
7782             struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7783                                                   info->shared ? addend : 0);
7784             addend = 0;
7785             if (ent == NULL
7786                 || htab->plt == NULL)
7787               {
7788                 /* We didn't make a PLT entry for this symbol.  This
7789                    happens when statically linking PIC code, or when
7790                    using -Bsymbolic.  */
7791                 break;
7792               }
7793
7794             unresolved_reloc = FALSE;
7795             if (htab->plt_type == PLT_NEW)
7796               relocation = (htab->glink->output_section->vma
7797                             + htab->glink->output_offset
7798                             + ent->glink_offset);
7799             else
7800               relocation = (htab->plt->output_section->vma
7801                             + htab->plt->output_offset
7802                             + ent->plt.offset);
7803           }
7804           break;
7805
7806           /* Relocate against _SDA_BASE_.  */
7807         case R_PPC_SDAREL16:
7808           {
7809             const char *name;
7810             struct elf_link_hash_entry *sda = htab->sdata[0].sym;
7811
7812             if (sec == NULL
7813                 || sec->output_section == NULL
7814                 || !is_static_defined (sda))
7815               {
7816                 unresolved_reloc = TRUE;
7817                 break;
7818               }
7819             addend -= SYM_VAL (sda);
7820
7821             name = bfd_get_section_name (abfd, sec->output_section);
7822             if (! ((CONST_STRNEQ (name, ".sdata")
7823                     && (name[6] == 0 || name[6] == '.'))
7824                    || (CONST_STRNEQ (name, ".sbss")
7825                        && (name[5] == 0 || name[5] == '.'))))
7826               {
7827                 (*_bfd_error_handler)
7828                   (_("%B: the target (%s) of a %s relocation is "
7829                      "in the wrong output section (%s)"),
7830                    input_bfd,
7831                    sym_name,
7832                    howto->name,
7833                    name);
7834               }
7835           }
7836           break;
7837
7838           /* Relocate against _SDA2_BASE_.  */
7839         case R_PPC_EMB_SDA2REL:
7840           {
7841             const char *name;
7842             struct elf_link_hash_entry *sda = htab->sdata[1].sym;
7843
7844             if (sec == NULL
7845                 || sec->output_section == NULL
7846                 || !is_static_defined (sda))
7847               {
7848                 unresolved_reloc = TRUE;
7849                 break;
7850               }
7851             addend -= SYM_VAL (sda);
7852
7853             name = bfd_get_section_name (abfd, sec->output_section);
7854             if (! (CONST_STRNEQ (name, ".sdata2")
7855                    || CONST_STRNEQ (name, ".sbss2")))
7856               {
7857                 (*_bfd_error_handler)
7858                   (_("%B: the target (%s) of a %s relocation is "
7859                      "in the wrong output section (%s)"),
7860                    input_bfd,
7861                    sym_name,
7862                    howto->name,
7863                    name);
7864               }
7865           }
7866           break;
7867
7868           /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
7869         case R_PPC_EMB_SDA21:
7870         case R_PPC_EMB_RELSDA:
7871           {
7872             const char *name;
7873             int reg;
7874             struct elf_link_hash_entry *sda = NULL;
7875
7876             if (sec == NULL || sec->output_section == NULL)
7877               {
7878                 unresolved_reloc = TRUE;
7879                 break;
7880               }
7881
7882             name = bfd_get_section_name (abfd, sec->output_section);
7883             if (((CONST_STRNEQ (name, ".sdata")
7884                   && (name[6] == 0 || name[6] == '.'))
7885                  || (CONST_STRNEQ (name, ".sbss")
7886                      && (name[5] == 0 || name[5] == '.'))))
7887               {
7888                 reg = 13;
7889                 sda = htab->sdata[0].sym;
7890               }
7891             else if (CONST_STRNEQ (name, ".sdata2")
7892                      || CONST_STRNEQ (name, ".sbss2"))
7893               {
7894                 reg = 2;
7895                 sda = htab->sdata[1].sym;
7896               }
7897             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
7898                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
7899               {
7900                 reg = 0;
7901               }
7902             else
7903               {
7904                 (*_bfd_error_handler)
7905                   (_("%B: the target (%s) of a %s relocation is "
7906                      "in the wrong output section (%s)"),
7907                    input_bfd,
7908                    sym_name,
7909                    howto->name,
7910                    name);
7911
7912                 bfd_set_error (bfd_error_bad_value);
7913                 ret = FALSE;
7914                 continue;
7915               }
7916
7917             if (sda != NULL)
7918               {
7919                 if (!is_static_defined (sda))
7920                   {
7921                     unresolved_reloc = TRUE;
7922                     break;
7923                   }
7924                 addend -= SYM_VAL (sda);
7925               }
7926
7927             if (r_type == R_PPC_EMB_SDA21)
7928               {
7929                 bfd_vma insn;  /* Fill in register field.  */
7930
7931                 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7932                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7933                 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7934               }
7935           }
7936           break;
7937
7938           /* Relocate against the beginning of the section.  */
7939         case R_PPC_SECTOFF:
7940         case R_PPC_SECTOFF_LO:
7941         case R_PPC_SECTOFF_HI:
7942         case R_PPC_SECTOFF_HA:
7943           if (sec == NULL || sec->output_section == NULL)
7944             {
7945               unresolved_reloc = TRUE;
7946               break;
7947             }
7948           addend -= sec->output_section->vma;
7949           break;
7950
7951           /* Negative relocations.  */
7952         case R_PPC_EMB_NADDR32:
7953         case R_PPC_EMB_NADDR16:
7954         case R_PPC_EMB_NADDR16_LO:
7955         case R_PPC_EMB_NADDR16_HI:
7956         case R_PPC_EMB_NADDR16_HA:
7957           addend -= 2 * relocation;
7958           break;
7959
7960         case R_PPC_COPY:
7961         case R_PPC_GLOB_DAT:
7962         case R_PPC_JMP_SLOT:
7963         case R_PPC_RELATIVE:
7964         case R_PPC_IRELATIVE:
7965         case R_PPC_PLT32:
7966         case R_PPC_PLTREL32:
7967         case R_PPC_PLT16_LO:
7968         case R_PPC_PLT16_HI:
7969         case R_PPC_PLT16_HA:
7970         case R_PPC_ADDR30:
7971         case R_PPC_EMB_RELSEC16:
7972         case R_PPC_EMB_RELST_LO:
7973         case R_PPC_EMB_RELST_HI:
7974         case R_PPC_EMB_RELST_HA:
7975         case R_PPC_EMB_BIT_FLD:
7976           (*_bfd_error_handler)
7977             (_("%B: relocation %s is not yet supported for symbol %s."),
7978              input_bfd,
7979              howto->name,
7980              sym_name);
7981
7982           bfd_set_error (bfd_error_invalid_operation);
7983           ret = FALSE;
7984           continue;
7985         }
7986
7987       /* Do any further special processing.  */
7988       switch (r_type)
7989         {
7990         default:
7991           break;
7992
7993         case R_PPC_ADDR16_HA:
7994         case R_PPC_REL16_HA:
7995         case R_PPC_SECTOFF_HA:
7996         case R_PPC_TPREL16_HA:
7997         case R_PPC_DTPREL16_HA:
7998         case R_PPC_EMB_NADDR16_HA:
7999         case R_PPC_EMB_RELST_HA:
8000           /* It's just possible that this symbol is a weak symbol
8001              that's not actually defined anywhere.  In that case,
8002              'sec' would be NULL, and we should leave the symbol
8003              alone (it will be set to zero elsewhere in the link).  */
8004           if (sec == NULL)
8005             break;
8006           /* Fall thru */
8007
8008         case R_PPC_PLT16_HA:
8009         case R_PPC_GOT16_HA:
8010         case R_PPC_GOT_TLSGD16_HA:
8011         case R_PPC_GOT_TLSLD16_HA:
8012         case R_PPC_GOT_TPREL16_HA:
8013         case R_PPC_GOT_DTPREL16_HA:
8014           /* Add 0x10000 if sign bit in 0:15 is set.
8015              Bits 0:15 are not used.  */
8016           addend += 0x8000;
8017           break;
8018         }
8019
8020 #ifdef DEBUG
8021       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8022                "offset = %ld, addend = %ld\n",
8023                howto->name,
8024                (int) r_type,
8025                sym_name,
8026                r_symndx,
8027                (long) rel->r_offset,
8028                (long) addend);
8029 #endif
8030
8031       if (unresolved_reloc
8032           && !((input_section->flags & SEC_DEBUGGING) != 0
8033                && h->def_dynamic))
8034         {
8035           (*_bfd_error_handler)
8036             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8037              input_bfd,
8038              input_section,
8039              (long) rel->r_offset,
8040              howto->name,
8041              sym_name);
8042           ret = FALSE;
8043         }
8044
8045       r = _bfd_final_link_relocate (howto,
8046                                     input_bfd,
8047                                     input_section,
8048                                     contents,
8049                                     rel->r_offset,
8050                                     relocation,
8051                                     addend);
8052
8053       if (r != bfd_reloc_ok)
8054         {
8055           if (r == bfd_reloc_overflow)
8056             {
8057               if (warned)
8058                 continue;
8059               if (h != NULL
8060                   && h->root.type == bfd_link_hash_undefweak
8061                   && howto->pc_relative)
8062                 {
8063                   /* Assume this is a call protected by other code that
8064                      detect the symbol is undefined.  If this is the case,
8065                      we can safely ignore the overflow.  If not, the
8066                      program is hosed anyway, and a little warning isn't
8067                      going to help.  */
8068
8069                   continue;
8070                 }
8071
8072               if (! (*info->callbacks->reloc_overflow) (info,
8073                                                         (h ? &h->root : NULL),
8074                                                         sym_name,
8075                                                         howto->name,
8076                                                         rel->r_addend,
8077                                                         input_bfd,
8078                                                         input_section,
8079                                                         rel->r_offset))
8080                 return FALSE;
8081             }
8082           else
8083             {
8084               (*_bfd_error_handler)
8085                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
8086                  input_bfd, input_section,
8087                  (long) rel->r_offset, howto->name, sym_name, (int) r);
8088               ret = FALSE;
8089             }
8090         }
8091     }
8092
8093 #ifdef DEBUG
8094   fprintf (stderr, "\n");
8095 #endif
8096
8097   return ret;
8098 }
8099 \f
8100 /* Finish up dynamic symbol handling.  We set the contents of various
8101    dynamic sections here.  */
8102
8103 static bfd_boolean
8104 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
8105                                struct bfd_link_info *info,
8106                                struct elf_link_hash_entry *h,
8107                                Elf_Internal_Sym *sym)
8108 {
8109   struct ppc_elf_link_hash_table *htab;
8110   struct plt_entry *ent;
8111   bfd_boolean doneone;
8112
8113 #ifdef DEBUG
8114   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
8115            h->root.root.string);
8116 #endif
8117
8118   htab = ppc_elf_hash_table (info);
8119   BFD_ASSERT (htab->elf.dynobj != NULL);
8120
8121   doneone = FALSE;
8122   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8123     if (ent->plt.offset != (bfd_vma) -1)
8124       {
8125         if (!doneone)
8126           {
8127             Elf_Internal_Rela rela;
8128             bfd_byte *loc;
8129             bfd_vma reloc_index;
8130
8131             if (htab->plt_type == PLT_NEW
8132                 || !htab->elf.dynamic_sections_created
8133                 || h->dynindx == -1)
8134               reloc_index = ent->plt.offset / 4;
8135             else
8136               {
8137                 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
8138                                / htab->plt_slot_size);
8139                 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
8140                     && htab->plt_type == PLT_OLD)
8141                   reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
8142               }
8143
8144             /* This symbol has an entry in the procedure linkage table.
8145                Set it up.  */
8146             if (htab->plt_type == PLT_VXWORKS
8147                 && htab->elf.dynamic_sections_created
8148                 && h->dynindx != -1)
8149               {
8150                 bfd_vma got_offset;
8151                 const bfd_vma *plt_entry;
8152                 
8153                 /* The first three entries in .got.plt are reserved.  */
8154                 got_offset = (reloc_index + 3) * 4;
8155
8156                 /* Use the right PLT. */
8157                 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
8158                             : ppc_elf_vxworks_plt_entry;
8159
8160                 /* Fill in the .plt on VxWorks.  */
8161                 if (info->shared)
8162                   {
8163                     bfd_put_32 (output_bfd,
8164                                 plt_entry[0] | PPC_HA (got_offset),
8165                                 htab->plt->contents + ent->plt.offset + 0);
8166                     bfd_put_32 (output_bfd,
8167                                 plt_entry[1] | PPC_LO (got_offset),
8168                                 htab->plt->contents + ent->plt.offset + 4);
8169                   }
8170                 else
8171                   {
8172                     bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
8173
8174                     bfd_put_32 (output_bfd,
8175                                 plt_entry[0] | PPC_HA (got_loc),
8176                                 htab->plt->contents + ent->plt.offset + 0);
8177                     bfd_put_32 (output_bfd,
8178                                 plt_entry[1] | PPC_LO (got_loc),
8179                                 htab->plt->contents + ent->plt.offset + 4);
8180                   }
8181
8182                 bfd_put_32 (output_bfd, plt_entry[2],
8183                             htab->plt->contents + ent->plt.offset + 8);
8184                 bfd_put_32 (output_bfd, plt_entry[3],
8185                             htab->plt->contents + ent->plt.offset + 12);
8186
8187                 /* This instruction is an immediate load.  The value loaded is
8188                    the byte offset of the R_PPC_JMP_SLOT relocation from the
8189                    start of the .rela.plt section.  The value is stored in the
8190                    low-order 16 bits of the load instruction.  */
8191                 /* NOTE: It appears that this is now an index rather than a
8192                    prescaled offset.  */
8193                 bfd_put_32 (output_bfd, 
8194                             plt_entry[4] | reloc_index,
8195                             htab->plt->contents + ent->plt.offset + 16);
8196                 /* This instruction is a PC-relative branch whose target is
8197                    the start of the PLT section.  The address of this branch
8198                    instruction is 20 bytes beyond the start of this PLT entry.
8199                    The address is encoded in bits 6-29, inclusive.  The value
8200                    stored is right-shifted by two bits, permitting a 26-bit
8201                    offset.  */
8202                 bfd_put_32 (output_bfd, 
8203                             (plt_entry[5] 
8204                              | (-(ent->plt.offset + 20) & 0x03fffffc)),
8205                             htab->plt->contents + ent->plt.offset + 20);
8206                 bfd_put_32 (output_bfd, plt_entry[6],
8207                             htab->plt->contents + ent->plt.offset + 24);
8208                 bfd_put_32 (output_bfd, plt_entry[7],
8209                             htab->plt->contents + ent->plt.offset + 28);
8210
8211                 /* Fill in the GOT entry corresponding to this PLT slot with
8212                    the address immediately after the the "bctr" instruction
8213                    in this PLT entry.  */
8214                 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
8215                                          + htab->plt->output_offset
8216                                          + ent->plt.offset + 16),
8217                             htab->sgotplt->contents + got_offset);
8218
8219                 if (!info->shared)
8220                   {
8221                     /* Fill in a couple of entries in .rela.plt.unloaded.  */
8222                     loc = htab->srelplt2->contents
8223                       + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
8224                           * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
8225                          * sizeof (Elf32_External_Rela));
8226
8227                     /* Provide the @ha relocation for the first instruction.  */
8228                     rela.r_offset = (htab->plt->output_section->vma
8229                                      + htab->plt->output_offset
8230                                      + ent->plt.offset + 2);
8231                     rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8232                                                 R_PPC_ADDR16_HA);
8233                     rela.r_addend = got_offset;
8234                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8235                     loc += sizeof (Elf32_External_Rela);
8236
8237                     /* Provide the @l relocation for the second instruction.  */
8238                     rela.r_offset = (htab->plt->output_section->vma
8239                                      + htab->plt->output_offset
8240                                      + ent->plt.offset + 6);
8241                     rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8242                                                 R_PPC_ADDR16_LO);
8243                     rela.r_addend = got_offset;
8244                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8245                     loc += sizeof (Elf32_External_Rela);
8246
8247                     /* Provide a relocation for the GOT entry corresponding to this
8248                        PLT slot.  Point it at the middle of the .plt entry.  */
8249                     rela.r_offset = (htab->sgotplt->output_section->vma
8250                                      + htab->sgotplt->output_offset
8251                                      + got_offset);
8252                     rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
8253                                                 R_PPC_ADDR32);
8254                     rela.r_addend = ent->plt.offset + 16;
8255                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8256                   }
8257
8258                 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
8259                    In particular, the offset for the relocation is not the
8260                    address of the PLT entry for this function, as specified
8261                    by the ABI.  Instead, the offset is set to the address of
8262                    the GOT slot for this function.  See EABI 4.4.4.1.  */
8263                 rela.r_offset = (htab->sgotplt->output_section->vma
8264                                  + htab->sgotplt->output_offset
8265                                  + got_offset);
8266
8267               }
8268             else
8269               {
8270                 asection *splt = htab->plt;
8271                 if (!htab->elf.dynamic_sections_created
8272                     || h->dynindx == -1)
8273                   splt = htab->iplt;
8274
8275                 rela.r_offset = (splt->output_section->vma
8276                                  + splt->output_offset
8277                                  + ent->plt.offset);
8278                 if (htab->plt_type == PLT_OLD
8279                     || !htab->elf.dynamic_sections_created
8280                     || h->dynindx == -1)
8281                   {
8282                     /* We don't need to fill in the .plt.  The ppc dynamic
8283                        linker will fill it in.  */
8284                   }
8285                 else
8286                   {
8287                     bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
8288                                    + htab->glink->output_section->vma
8289                                    + htab->glink->output_offset);
8290                     bfd_put_32 (output_bfd, val,
8291                                 splt->contents + ent->plt.offset);
8292                   }
8293               }
8294
8295             /* Fill in the entry in the .rela.plt section.  */
8296             rela.r_addend = 0;
8297             if (!htab->elf.dynamic_sections_created
8298                 || h->dynindx == -1)
8299               {
8300                 BFD_ASSERT (h->type == STT_GNU_IFUNC
8301                             && h->def_regular
8302                             && (h->root.type == bfd_link_hash_defined
8303                                 || h->root.type == bfd_link_hash_defweak));
8304                 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8305                 rela.r_addend = SYM_VAL (h);
8306               }
8307             else
8308               rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
8309
8310             if (!htab->elf.dynamic_sections_created
8311                 || h->dynindx == -1)
8312               loc = (htab->reliplt->contents
8313                      + (htab->reliplt->reloc_count++
8314                         * sizeof (Elf32_External_Rela)));
8315             else
8316               loc = (htab->relplt->contents
8317                      + reloc_index * sizeof (Elf32_External_Rela));
8318             bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8319
8320             if (!h->def_regular)
8321               {
8322                 /* Mark the symbol as undefined, rather than as
8323                    defined in the .plt section.  Leave the value if
8324                    there were any relocations where pointer equality
8325                    matters (this is a clue for the dynamic linker, to
8326                    make function pointer comparisons work between an
8327                    application and shared library), otherwise set it
8328                    to zero.  */
8329                 sym->st_shndx = SHN_UNDEF;
8330                 if (!h->pointer_equality_needed)
8331                   sym->st_value = 0;
8332                 else if (!h->ref_regular_nonweak)
8333                   {
8334                     /* This breaks function pointer comparisons, but
8335                        that is better than breaking tests for a NULL
8336                        function pointer.  */
8337                     sym->st_value = 0;
8338                   }
8339               }
8340             else if (h->type == STT_GNU_IFUNC
8341                      && !info->shared)
8342               {
8343                 /* Set the value of ifunc symbols in a non-pie
8344                    executable to the glink entry.  This is to avoid
8345                    text relocations.  We can't do this for ifunc in
8346                    allocate_dynrelocs, as we do for normal dynamic
8347                    function symbols with plt entries, because we need
8348                    to keep the original value around for the ifunc
8349                    relocation.  */
8350                 sym->st_shndx = (_bfd_elf_section_from_bfd_section
8351                                  (output_bfd, htab->glink->output_section));
8352                 sym->st_value = (ent->glink_offset +
8353                                  htab->glink->output_offset
8354                                  + htab->glink->output_section->vma);
8355               }
8356             doneone = TRUE;
8357           }
8358
8359         if (htab->plt_type == PLT_NEW
8360             || !htab->elf.dynamic_sections_created
8361             || h->dynindx == -1)
8362           {
8363             unsigned char *p;
8364             asection *splt = htab->plt;
8365             if (!htab->elf.dynamic_sections_created
8366                 || h->dynindx == -1)
8367               splt = htab->iplt;
8368
8369             p = (unsigned char *) htab->glink->contents + ent->glink_offset;
8370
8371             if (h == htab->tls_get_addr && !htab->no_tls_get_addr_opt)
8372               {
8373                 bfd_put_32 (output_bfd, LWZ_11_3, p);
8374                 p += 4;
8375                 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
8376                 p += 4;
8377                 bfd_put_32 (output_bfd, MR_0_3, p);
8378                 p += 4;
8379                 bfd_put_32 (output_bfd, CMPWI_11_0, p);
8380                 p += 4;
8381                 bfd_put_32 (output_bfd, ADD_3_12_2, p);
8382                 p += 4;
8383                 bfd_put_32 (output_bfd, BEQLR, p);
8384                 p += 4;
8385                 bfd_put_32 (output_bfd, MR_3_0, p);
8386                 p += 4;
8387                 bfd_put_32 (output_bfd, NOP, p);
8388                 p += 4;
8389               }
8390
8391             write_glink_stub (ent, splt, p, info);
8392
8393             if (!info->shared)
8394               /* We only need one non-PIC glink stub.  */
8395               break;
8396           }
8397         else
8398           break;
8399       }
8400
8401   if (h->needs_copy)
8402     {
8403       asection *s;
8404       Elf_Internal_Rela rela;
8405       bfd_byte *loc;
8406
8407       /* This symbols needs a copy reloc.  Set it up.  */
8408
8409 #ifdef DEBUG
8410       fprintf (stderr, ", copy");
8411 #endif
8412
8413       BFD_ASSERT (h->dynindx != -1);
8414
8415       if (ppc_elf_hash_entry (h)->has_sda_refs)
8416         s = htab->relsbss;
8417       else
8418         s = htab->relbss;
8419       BFD_ASSERT (s != NULL);
8420
8421       rela.r_offset = SYM_VAL (h);
8422       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
8423       rela.r_addend = 0;
8424       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
8425       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8426     }
8427
8428 #ifdef DEBUG
8429   fprintf (stderr, "\n");
8430 #endif
8431
8432   /* Mark some specially defined symbols as absolute.  */
8433   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8434       || (!htab->is_vxworks
8435           && (h == htab->elf.hgot
8436               || strcmp (h->root.root.string,
8437                          "_PROCEDURE_LINKAGE_TABLE_") == 0)))
8438     sym->st_shndx = SHN_ABS;
8439
8440   return TRUE;
8441 }
8442 \f
8443 static enum elf_reloc_type_class
8444 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8445 {
8446   switch (ELF32_R_TYPE (rela->r_info))
8447     {
8448     case R_PPC_RELATIVE:
8449       return reloc_class_relative;
8450     case R_PPC_REL24:
8451     case R_PPC_ADDR24:
8452     case R_PPC_JMP_SLOT:
8453       return reloc_class_plt;
8454     case R_PPC_COPY:
8455       return reloc_class_copy;
8456     default:
8457       return reloc_class_normal;
8458     }
8459 }
8460 \f
8461 /* Finish up the dynamic sections.  */
8462
8463 static bfd_boolean
8464 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
8465                                  struct bfd_link_info *info)
8466 {
8467   asection *sdyn;
8468   asection *splt;
8469   struct ppc_elf_link_hash_table *htab;
8470   bfd_vma got;
8471   bfd *dynobj;
8472   bfd_boolean ret = TRUE;
8473
8474 #ifdef DEBUG
8475   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
8476 #endif
8477
8478   htab = ppc_elf_hash_table (info);
8479   dynobj = elf_hash_table (info)->dynobj;
8480   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8481   if (htab->is_vxworks)
8482     splt = bfd_get_section_by_name (dynobj, ".plt");  
8483   else
8484     splt = NULL;
8485
8486   got = 0;
8487   if (htab->elf.hgot != NULL)
8488     got = SYM_VAL (htab->elf.hgot);
8489
8490   if (htab->elf.dynamic_sections_created)
8491     {
8492       Elf32_External_Dyn *dyncon, *dynconend;
8493
8494       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
8495
8496       dyncon = (Elf32_External_Dyn *) sdyn->contents;
8497       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8498       for (; dyncon < dynconend; dyncon++)
8499         {
8500           Elf_Internal_Dyn dyn;
8501           asection *s;
8502
8503           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
8504
8505           switch (dyn.d_tag)
8506             {
8507             case DT_PLTGOT:
8508               if (htab->is_vxworks)
8509                 s = htab->sgotplt;
8510               else
8511                 s = htab->plt;
8512               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8513               break;
8514
8515             case DT_PLTRELSZ:
8516               dyn.d_un.d_val = htab->relplt->size;
8517               break;
8518
8519             case DT_JMPREL:
8520               s = htab->relplt;
8521               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8522               break;
8523
8524             case DT_PPC_GOT:
8525               dyn.d_un.d_ptr = got;
8526               break;
8527
8528             case DT_RELASZ:
8529               if (htab->is_vxworks)
8530                 {
8531                   if (htab->relplt)
8532                     dyn.d_un.d_ptr -= htab->relplt->size;
8533                   break;
8534                 }
8535               continue;
8536
8537             default:
8538               if (htab->is_vxworks
8539                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
8540                 break;
8541               continue;
8542             }
8543
8544           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8545         }
8546     }
8547
8548   if (htab->got != NULL)
8549     {
8550       if (htab->elf.hgot->root.u.def.section == htab->got
8551           || htab->elf.hgot->root.u.def.section == htab->sgotplt)
8552         {
8553           unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
8554
8555           p += htab->elf.hgot->root.u.def.value;
8556           if (htab->plt_type == PLT_OLD)
8557             {
8558               /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
8559                  so that a function can easily find the address of
8560                  _GLOBAL_OFFSET_TABLE_.  */
8561               BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
8562                           < htab->elf.hgot->root.u.def.section->size);
8563               bfd_put_32 (output_bfd, 0x4e800021, p - 4);
8564             }
8565
8566           if (sdyn != NULL)
8567             {
8568               bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
8569               BFD_ASSERT (htab->elf.hgot->root.u.def.value
8570                           < htab->elf.hgot->root.u.def.section->size);
8571               bfd_put_32 (output_bfd, val, p);
8572             }
8573         }
8574       else
8575         {
8576           (*_bfd_error_handler) (_("%s not defined in linker created %s"),
8577                                  htab->elf.hgot->root.root.string,
8578                                  (htab->sgotplt != NULL
8579                                   ? htab->sgotplt->name : htab->got->name));
8580           bfd_set_error (bfd_error_bad_value);
8581           ret = FALSE;
8582         }
8583
8584       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
8585     }
8586
8587   /* Fill in the first entry in the VxWorks procedure linkage table.  */
8588   if (splt && splt->size > 0)
8589     {
8590       /* Use the right PLT. */
8591       static const bfd_vma *plt_entry = NULL;
8592       plt_entry = info->shared ? 
8593         ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
8594
8595       if (!info->shared)
8596         {
8597           bfd_vma got_value = SYM_VAL (htab->elf.hgot);
8598
8599           bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
8600                       splt->contents +  0);
8601           bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
8602                       splt->contents +  4);
8603         }
8604       else
8605         {
8606           bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
8607           bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
8608         }
8609       bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
8610       bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
8611       bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
8612       bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
8613       bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
8614       bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
8615
8616       if (! info->shared)
8617         {
8618           Elf_Internal_Rela rela;
8619           bfd_byte *loc;
8620
8621           loc = htab->srelplt2->contents;
8622
8623           /* Output the @ha relocation for the first instruction.  */
8624           rela.r_offset = (htab->plt->output_section->vma
8625                            + htab->plt->output_offset
8626                            + 2);
8627           rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8628           rela.r_addend = 0;
8629           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8630           loc += sizeof (Elf32_External_Rela);
8631           
8632           /* Output the @l relocation for the second instruction.  */
8633           rela.r_offset = (htab->plt->output_section->vma
8634                            + htab->plt->output_offset
8635                            + 6);
8636           rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8637           rela.r_addend = 0;
8638           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8639           loc += sizeof (Elf32_External_Rela);
8640
8641           /* Fix up the remaining relocations.  They may have the wrong
8642              symbol index for _G_O_T_ or _P_L_T_ depending on the order
8643              in which symbols were output.  */
8644           while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8645             {
8646               Elf_Internal_Rela rel;
8647
8648               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8649               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8650               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8651               loc += sizeof (Elf32_External_Rela);
8652
8653               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8654               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8655               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8656               loc += sizeof (Elf32_External_Rela);
8657
8658               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8659               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
8660               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8661               loc += sizeof (Elf32_External_Rela);
8662             }
8663         }
8664     }
8665
8666   if (htab->glink != NULL
8667       && htab->glink->contents != NULL
8668       && htab->elf.dynamic_sections_created)
8669     {
8670       unsigned char *p;
8671       unsigned char *endp;
8672       bfd_vma res0;
8673       unsigned int i;
8674
8675       /*
8676        * PIC glink code is the following:
8677        *
8678        * # ith PLT code stub.
8679        *   addis 11,30,(plt+(i-1)*4-got)@ha
8680        *   lwz 11,(plt+(i-1)*4-got)@l(11)
8681        *   mtctr 11
8682        *   bctr
8683        *
8684        * # A table of branches, one for each plt entry.
8685        * # The idea is that the plt call stub loads ctr and r11 with these
8686        * # addresses, so (r11 - res_0) gives the plt index * 4.
8687        * res_0: b PLTresolve
8688        * res_1: b PLTresolve
8689        * .
8690        * # Some number of entries towards the end can be nops
8691        * res_n_m3: nop
8692        * res_n_m2: nop
8693        * res_n_m1:
8694        *
8695        * PLTresolve:
8696        *    addis 11,11,(1f-res_0)@ha
8697        *    mflr 0
8698        *    bcl 20,31,1f
8699        * 1: addi 11,11,(1b-res_0)@l
8700        *    mflr 12
8701        *    mtlr 0
8702        *    sub 11,11,12                # r11 = index * 4
8703        *    addis 12,12,(got+4-1b)@ha
8704        *    lwz 0,(got+4-1b)@l(12)      # got[1] address of dl_runtime_resolve
8705        *    lwz 12,(got+8-1b)@l(12)     # got[2] contains the map address
8706        *    mtctr 0
8707        *    add 0,11,11
8708        *    add 11,0,11                 # r11 = index * 12 = reloc offset.
8709        *    bctr
8710        */
8711       static const unsigned int pic_plt_resolve[] =
8712         {
8713           ADDIS_11_11,
8714           MFLR_0,
8715           BCL_20_31,
8716           ADDI_11_11,
8717           MFLR_12,
8718           MTLR_0,
8719           SUB_11_11_12,
8720           ADDIS_12_12,
8721           LWZ_0_12,
8722           LWZ_12_12,
8723           MTCTR_0,
8724           ADD_0_11_11,
8725           ADD_11_0_11,
8726           BCTR,
8727           NOP,
8728           NOP
8729         };
8730
8731       /*
8732        * Non-PIC glink code is a little simpler.
8733        *
8734        * # ith PLT code stub.
8735        *   lis 11,(plt+(i-1)*4)@ha
8736        *   lwz 11,(plt+(i-1)*4)@l(11)
8737        *   mtctr 11
8738        *   bctr
8739        *
8740        * The branch table is the same, then comes
8741        *
8742        * PLTresolve:
8743        *    lis 12,(got+4)@ha
8744        *    addis 11,11,(-res_0)@ha
8745        *    lwz 0,(got+4)@l(12)         # got[1] address of dl_runtime_resolve
8746        *    addi 11,11,(-res_0)@l       # r11 = index * 4
8747        *    mtctr 0
8748        *    add 0,11,11
8749        *    lwz 12,(got+8)@l(12)        # got[2] contains the map address
8750        *    add 11,0,11                 # r11 = index * 12 = reloc offset.
8751        *    bctr
8752        */
8753       static const unsigned int plt_resolve[] =
8754         {
8755           LIS_12,
8756           ADDIS_11_11,
8757           LWZ_0_12,
8758           ADDI_11_11,
8759           MTCTR_0,
8760           ADD_0_11_11,
8761           LWZ_12_12,
8762           ADD_11_0_11,
8763           BCTR,
8764           NOP,
8765           NOP,
8766           NOP,
8767           NOP,
8768           NOP,
8769           NOP,
8770           NOP
8771         };
8772
8773       if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
8774         abort ();
8775       if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
8776         abort ();
8777
8778       /* Build the branch table, one for each plt entry (less one),
8779          and perhaps some padding.  */
8780       p = htab->glink->contents;
8781       p += htab->glink_pltresolve;
8782       endp = htab->glink->contents;
8783       endp += htab->glink->size - GLINK_PLTRESOLVE;
8784       while (p < endp - 8 * 4)
8785         {
8786           bfd_put_32 (output_bfd, B + endp - p, p);
8787           p += 4;
8788         }
8789       while (p < endp)
8790         {
8791           bfd_put_32 (output_bfd, NOP, p);
8792           p += 4;
8793         }
8794
8795       res0 = (htab->glink_pltresolve
8796               + htab->glink->output_section->vma
8797               + htab->glink->output_offset);
8798
8799       /* Last comes the PLTresolve stub.  */
8800       if (info->shared)
8801         {
8802           bfd_vma bcl;
8803
8804           for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
8805             {
8806               bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
8807               p += 4;
8808             }
8809           p -= 4 * ARRAY_SIZE (pic_plt_resolve);
8810
8811           bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
8812                  + htab->glink->output_section->vma
8813                  + htab->glink->output_offset);
8814
8815           bfd_put_32 (output_bfd,
8816                       ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
8817           bfd_put_32 (output_bfd,
8818                       ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
8819           bfd_put_32 (output_bfd,
8820                       ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
8821           if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
8822             {
8823               bfd_put_32 (output_bfd,
8824                           LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8825               bfd_put_32 (output_bfd,
8826                           LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
8827             }
8828           else
8829             {
8830               bfd_put_32 (output_bfd,
8831                           LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8832               bfd_put_32 (output_bfd,
8833                           LWZ_12_12 + 4, p + 9*4);
8834             }
8835         }
8836       else
8837         {
8838           for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
8839             {
8840               bfd_put_32 (output_bfd, plt_resolve[i], p);
8841               p += 4;
8842             }
8843           p -= 4 * ARRAY_SIZE (plt_resolve);
8844
8845           bfd_put_32 (output_bfd,
8846                       LIS_12 + PPC_HA (got + 4), p + 0*4);
8847           bfd_put_32 (output_bfd,
8848                       ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
8849           bfd_put_32 (output_bfd,
8850                       ADDI_11_11 + PPC_LO (-res0), p + 3*4);
8851           if (PPC_HA (got + 4) == PPC_HA (got + 8))
8852             {
8853               bfd_put_32 (output_bfd,
8854                           LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
8855               bfd_put_32 (output_bfd,
8856                           LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
8857             }
8858           else
8859             {
8860               bfd_put_32 (output_bfd,
8861                           LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
8862               bfd_put_32 (output_bfd,
8863                           LWZ_12_12 + 4, p + 6*4);
8864             }
8865         }
8866     }
8867
8868   return ret;
8869 }
8870 \f
8871 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
8872 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
8873 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
8874 #define TARGET_BIG_NAME         "elf32-powerpc"
8875 #define ELF_ARCH                bfd_arch_powerpc
8876 #define ELF_MACHINE_CODE        EM_PPC
8877 #ifdef __QNXTARGET__
8878 #define ELF_MAXPAGESIZE         0x1000
8879 #else
8880 #define ELF_MAXPAGESIZE         0x10000
8881 #endif
8882 #define ELF_MINPAGESIZE         0x1000
8883 #define ELF_COMMONPAGESIZE      0x1000
8884 #define elf_info_to_howto       ppc_elf_info_to_howto
8885
8886 #ifdef  EM_CYGNUS_POWERPC
8887 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
8888 #endif
8889
8890 #ifdef EM_PPC_OLD
8891 #define ELF_MACHINE_ALT2        EM_PPC_OLD
8892 #endif
8893
8894 #define elf_backend_plt_not_loaded      1
8895 #define elf_backend_can_gc_sections     1
8896 #define elf_backend_can_refcount        1
8897 #define elf_backend_rela_normal         1
8898
8899 #define bfd_elf32_mkobject                      ppc_elf_mkobject
8900 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
8901 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
8902 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
8903 #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
8904 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
8905 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
8906 #define bfd_elf32_get_synthetic_symtab          ppc_elf_get_synthetic_symtab
8907
8908 #define elf_backend_object_p                    ppc_elf_object_p
8909 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
8910 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
8911 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
8912 #define elf_backend_relocate_section            ppc_elf_relocate_section
8913 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
8914 #define elf_backend_check_relocs                ppc_elf_check_relocs
8915 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
8916 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
8917 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
8918 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
8919 #define elf_backend_hash_symbol                 ppc_elf_hash_symbol
8920 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
8921 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
8922 #define elf_backend_fake_sections               ppc_elf_fake_sections
8923 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
8924 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
8925 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
8926 #define elf_backend_write_core_note             ppc_elf_write_core_note
8927 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
8928 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
8929 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
8930 #define elf_backend_write_section               ppc_elf_write_section
8931 #define elf_backend_get_sec_type_attr           ppc_elf_get_sec_type_attr
8932 #define elf_backend_plt_sym_val                 ppc_elf_plt_sym_val
8933 #define elf_backend_action_discarded            ppc_elf_action_discarded
8934 #define elf_backend_init_index_section          _bfd_elf_init_1_index_section
8935 #define elf_backend_post_process_headers        _bfd_elf_set_osabi
8936
8937 #include "elf32-target.h"
8938
8939 /* VxWorks Target */
8940
8941 #undef TARGET_LITTLE_SYM
8942 #undef TARGET_LITTLE_NAME
8943
8944 #undef TARGET_BIG_SYM
8945 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vxworks_vec
8946 #undef TARGET_BIG_NAME
8947 #define TARGET_BIG_NAME         "elf32-powerpc-vxworks"
8948
8949 /* VxWorks uses the elf default section flags for .plt.  */
8950 static const struct bfd_elf_special_section *
8951 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
8952 {
8953   if (sec->name == NULL)
8954     return NULL;
8955
8956   if (strcmp (sec->name, ".plt") == 0)
8957     return _bfd_elf_get_sec_type_attr (abfd, sec);
8958
8959   return ppc_elf_get_sec_type_attr (abfd, sec);
8960 }
8961
8962 /* Like ppc_elf_link_hash_table_create, but overrides
8963    appropriately for VxWorks.  */
8964 static struct bfd_link_hash_table *
8965 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
8966 {
8967   struct bfd_link_hash_table *ret;
8968
8969   ret = ppc_elf_link_hash_table_create (abfd);
8970   if (ret)
8971     {
8972       struct ppc_elf_link_hash_table *htab
8973         = (struct ppc_elf_link_hash_table *)ret;
8974       htab->is_vxworks = 1;
8975       htab->plt_type = PLT_VXWORKS;
8976       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
8977       htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
8978       htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
8979     }
8980   return ret;
8981 }
8982
8983 /* Tweak magic VxWorks symbols as they are loaded.  */
8984 static bfd_boolean
8985 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
8986                                  struct bfd_link_info *info,
8987                                  Elf_Internal_Sym *sym,
8988                                  const char **namep ATTRIBUTE_UNUSED,
8989                                  flagword *flagsp ATTRIBUTE_UNUSED,
8990                                  asection **secp,
8991                                  bfd_vma *valp)
8992 {
8993   if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
8994                                    valp))
8995     return FALSE;
8996
8997   return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
8998 }
8999
9000 static void
9001 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9002 {
9003   ppc_elf_final_write_processing(abfd, linker);
9004   elf_vxworks_final_write_processing(abfd, linker);
9005 }
9006
9007 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
9008    define it.  */
9009 #undef elf_backend_want_plt_sym
9010 #define elf_backend_want_plt_sym                1
9011 #undef elf_backend_want_got_plt
9012 #define elf_backend_want_got_plt                1
9013 #undef elf_backend_got_symbol_offset
9014 #define elf_backend_got_symbol_offset           0
9015 #undef elf_backend_plt_not_loaded
9016 #define elf_backend_plt_not_loaded              0
9017 #undef elf_backend_plt_readonly
9018 #define elf_backend_plt_readonly                1
9019 #undef elf_backend_got_header_size
9020 #define elf_backend_got_header_size             12
9021
9022 #undef bfd_elf32_get_synthetic_symtab
9023
9024 #undef bfd_elf32_bfd_link_hash_table_create
9025 #define bfd_elf32_bfd_link_hash_table_create \
9026   ppc_elf_vxworks_link_hash_table_create
9027 #undef elf_backend_add_symbol_hook
9028 #define elf_backend_add_symbol_hook \
9029   ppc_elf_vxworks_add_symbol_hook
9030 #undef elf_backend_link_output_symbol_hook
9031 #define elf_backend_link_output_symbol_hook \
9032   elf_vxworks_link_output_symbol_hook
9033 #undef elf_backend_final_write_processing
9034 #define elf_backend_final_write_processing \
9035   ppc_elf_vxworks_final_write_processing
9036 #undef elf_backend_get_sec_type_attr
9037 #define elf_backend_get_sec_type_attr \
9038   ppc_elf_vxworks_get_sec_type_attr
9039 #undef elf_backend_emit_relocs
9040 #define elf_backend_emit_relocs \
9041   elf_vxworks_emit_relocs
9042
9043 #undef elf32_bed
9044 #define elf32_bed                               ppc_elf_vxworks_bed
9045 #undef elf_backend_post_process_headers
9046
9047 #include "elf32-target.h"