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