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