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