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