1/* PowerPC-specific support for 32-bit ELF
2   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4   Written by Ian Lance Taylor, Cygnus Support.
5
6   This file is part of BFD, the Binary File Descriptor library.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the
20   Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21   Boston, MA 02110-1301, USA.  */
22
23/* 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
38/* RELA relocations are used here.  */
39
40static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
41  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42static bfd_reloc_status_type ppc_elf_unhandled_reloc
43  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44
45/* Branch prediction bit for branch taken relocs.  */
46#define BRANCH_PREDICT_BIT 0x200000
47/* Mask to set RA in memory instructions.  */
48#define RA_REGISTER_MASK 0x001f0000
49/* Value to shift register by to insert RA.  */
50#define RA_REGISTER_SHIFT 16
51
52/* The name of the dynamic interpreter.  This is put in the .interp
53   section.  */
54#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
55
56/* For old-style PLT.  */
57/* The number of single-slot PLT entries (the rest use two slots).  */
58#define PLT_NUM_SINGLE_ENTRIES 8192
59
60/* For new-style .glink and .plt.  */
61#define GLINK_PLTRESOLVE 16*4
62#define GLINK_ENTRY_SIZE 4*4
63
64/* VxWorks uses its own plt layout, filled in by the static linker.  */
65
66/* The standard VxWorks PLT entry.  */
67#define VXWORKS_PLT_ENTRY_SIZE 32
68static const bfd_vma ppc_elf_vxworks_plt_entry
69    [VXWORKS_PLT_ENTRY_SIZE / 4] =
70  {
71    0x3d800000, /* lis     r12,0                 */
72    0x818c0000, /* lwz     r12,0(r12)            */
73    0x7d8903a6, /* mtctr   r12                   */
74    0x4e800420, /* bctr                          */
75    0x39600000, /* li      r11,0                 */
76    0x48000000, /* b       14 <.PLT0resolve+0x4> */
77    0x60000000, /* nop                           */
78    0x60000000, /* nop                           */
79  };
80static const bfd_vma ppc_elf_vxworks_pic_plt_entry
81    [VXWORKS_PLT_ENTRY_SIZE / 4] =
82  {
83    0x3d9e0000, /* addis r12,r30,0 */
84    0x818c0000, /* lwz	 r12,0(r12) */
85    0x7d8903a6, /* mtctr r12 */
86    0x4e800420, /* bctr */
87    0x39600000, /* li	 r11,0 */
88    0x48000000, /* b	 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
89    0x60000000, /* nop */
90    0x60000000, /* nop */
91  };
92
93/* The initial VxWorks PLT entry.  */
94#define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
95static const bfd_vma ppc_elf_vxworks_plt0_entry
96    [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
97  {
98    0x3d800000, /* lis     r12,0        */
99    0x398c0000, /* addi    r12,r12,0    */
100    0x800c0008, /* lwz     r0,8(r12)    */
101    0x7c0903a6, /* mtctr   r0           */
102    0x818c0004, /* lwz     r12,4(r12)   */
103    0x4e800420, /* bctr                 */
104    0x60000000, /* nop                  */
105    0x60000000, /* nop                  */
106  };
107static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
108    [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
109  {
110    0x819e0008, /* lwz	 r12,8(r30) */
111    0x7d8903a6, /* mtctr r12        */
112    0x819e0004, /* lwz	 r12,4(r30) */
113    0x4e800420, /* bctr             */
114    0x60000000, /* nop              */
115    0x60000000, /* nop              */
116    0x60000000, /* nop              */
117    0x60000000, /* nop              */
118  };
119
120/* For executables, we have some additional relocations in
121   .rela.plt.unloaded, for the kernel loader.  */
122
123/* The number of non-JMP_SLOT relocations per PLT0 slot. */
124#define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
125/* The number of relocations in the PLTResolve slot. */
126#define VXWORKS_PLTRESOLVE_RELOCS 2
127/* The number of relocations in the PLTResolve slot when when creating
128   a shared library. */
129#define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
130
131/* Some instructions.  */
132#define ADDIS_11_11	0x3d6b0000
133#define ADDIS_11_30	0x3d7e0000
134#define ADDIS_12_12	0x3d8c0000
135#define ADDI_11_11	0x396b0000
136#define ADD_0_11_11	0x7c0b5a14
137#define ADD_11_0_11	0x7d605a14
138#define B		0x48000000
139#define BCL_20_31	0x429f0005
140#define BCTR		0x4e800420
141#define LIS_11		0x3d600000
142#define LIS_12		0x3d800000
143#define LWZU_0_12	0x840c0000
144#define LWZ_0_12	0x800c0000
145#define LWZ_11_11	0x816b0000
146#define LWZ_11_30	0x817e0000
147#define LWZ_12_12	0x818c0000
148#define MFLR_0		0x7c0802a6
149#define MFLR_12		0x7d8802a6
150#define MTCTR_0		0x7c0903a6
151#define MTCTR_11	0x7d6903a6
152#define MTLR_0		0x7c0803a6
153#define NOP		0x60000000
154#define SUB_11_11_12	0x7d6c5850
155
156/* Offset of tp and dtp pointers from start of TLS block.  */
157#define TP_OFFSET	0x7000
158#define DTP_OFFSET	0x8000
159
160static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
161
162static reloc_howto_type ppc_elf_howto_raw[] = {
163  /* This reloc does nothing.  */
164  HOWTO (R_PPC_NONE,		/* type */
165	 0,			/* rightshift */
166	 2,			/* size (0 = byte, 1 = short, 2 = long) */
167	 32,			/* bitsize */
168	 FALSE,			/* pc_relative */
169	 0,			/* bitpos */
170	 complain_overflow_bitfield, /* complain_on_overflow */
171	 bfd_elf_generic_reloc,	/* special_function */
172	 "R_PPC_NONE",		/* name */
173	 FALSE,			/* partial_inplace */
174	 0,			/* src_mask */
175	 0,			/* dst_mask */
176	 FALSE),		/* pcrel_offset */
177
178  /* A standard 32 bit relocation.  */
179  HOWTO (R_PPC_ADDR32,		/* type */
180	 0,			/* rightshift */
181	 2,			/* size (0 = byte, 1 = short, 2 = long) */
182	 32,			/* bitsize */
183	 FALSE,			/* pc_relative */
184	 0,			/* bitpos */
185	 complain_overflow_bitfield, /* complain_on_overflow */
186	 bfd_elf_generic_reloc,	/* special_function */
187	 "R_PPC_ADDR32",	/* name */
188	 FALSE,			/* partial_inplace */
189	 0,			/* src_mask */
190	 0xffffffff,		/* dst_mask */
191	 FALSE),		/* pcrel_offset */
192
193  /* An absolute 26 bit branch; the lower two bits must be zero.
194     FIXME: we don't check that, we just clear them.  */
195  HOWTO (R_PPC_ADDR24,		/* type */
196	 0,			/* rightshift */
197	 2,			/* size (0 = byte, 1 = short, 2 = long) */
198	 26,			/* bitsize */
199	 FALSE,			/* pc_relative */
200	 0,			/* bitpos */
201	 complain_overflow_bitfield, /* complain_on_overflow */
202	 bfd_elf_generic_reloc,	/* special_function */
203	 "R_PPC_ADDR24",	/* name */
204	 FALSE,			/* partial_inplace */
205	 0,			/* src_mask */
206	 0x3fffffc,		/* dst_mask */
207	 FALSE),		/* pcrel_offset */
208
209  /* A standard 16 bit relocation.  */
210  HOWTO (R_PPC_ADDR16,		/* type */
211	 0,			/* rightshift */
212	 1,			/* size (0 = byte, 1 = short, 2 = long) */
213	 16,			/* bitsize */
214	 FALSE,			/* pc_relative */
215	 0,			/* bitpos */
216	 complain_overflow_bitfield, /* complain_on_overflow */
217	 bfd_elf_generic_reloc,	/* special_function */
218	 "R_PPC_ADDR16",	/* name */
219	 FALSE,			/* partial_inplace */
220	 0,			/* src_mask */
221	 0xffff,		/* dst_mask */
222	 FALSE),		/* pcrel_offset */
223
224  /* A 16 bit relocation without overflow.  */
225  HOWTO (R_PPC_ADDR16_LO,	/* type */
226	 0,			/* rightshift */
227	 1,			/* size (0 = byte, 1 = short, 2 = long) */
228	 16,			/* bitsize */
229	 FALSE,			/* pc_relative */
230	 0,			/* bitpos */
231	 complain_overflow_dont,/* complain_on_overflow */
232	 bfd_elf_generic_reloc,	/* special_function */
233	 "R_PPC_ADDR16_LO",	/* name */
234	 FALSE,			/* partial_inplace */
235	 0,			/* src_mask */
236	 0xffff,		/* dst_mask */
237	 FALSE),		/* pcrel_offset */
238
239  /* The high order 16 bits of an address.  */
240  HOWTO (R_PPC_ADDR16_HI,	/* type */
241	 16,			/* rightshift */
242	 1,			/* size (0 = byte, 1 = short, 2 = long) */
243	 16,			/* bitsize */
244	 FALSE,			/* pc_relative */
245	 0,			/* bitpos */
246	 complain_overflow_dont, /* complain_on_overflow */
247	 bfd_elf_generic_reloc,	/* special_function */
248	 "R_PPC_ADDR16_HI",	/* name */
249	 FALSE,			/* partial_inplace */
250	 0,			/* src_mask */
251	 0xffff,		/* dst_mask */
252	 FALSE),		/* pcrel_offset */
253
254  /* The high order 16 bits of an address, plus 1 if the contents of
255     the low 16 bits, treated as a signed number, is negative.  */
256  HOWTO (R_PPC_ADDR16_HA,	/* type */
257	 16,			/* rightshift */
258	 1,			/* size (0 = byte, 1 = short, 2 = long) */
259	 16,			/* bitsize */
260	 FALSE,			/* pc_relative */
261	 0,			/* bitpos */
262	 complain_overflow_dont, /* complain_on_overflow */
263	 ppc_elf_addr16_ha_reloc, /* special_function */
264	 "R_PPC_ADDR16_HA",	/* name */
265	 FALSE,			/* partial_inplace */
266	 0,			/* src_mask */
267	 0xffff,		/* dst_mask */
268	 FALSE),		/* pcrel_offset */
269
270  /* An absolute 16 bit branch; the lower two bits must be zero.
271     FIXME: we don't check that, we just clear them.  */
272  HOWTO (R_PPC_ADDR14,		/* type */
273	 0,			/* rightshift */
274	 2,			/* size (0 = byte, 1 = short, 2 = long) */
275	 16,			/* bitsize */
276	 FALSE,			/* pc_relative */
277	 0,			/* bitpos */
278	 complain_overflow_bitfield, /* complain_on_overflow */
279	 bfd_elf_generic_reloc,	/* special_function */
280	 "R_PPC_ADDR14",	/* name */
281	 FALSE,			/* partial_inplace */
282	 0,			/* src_mask */
283	 0xfffc,		/* dst_mask */
284	 FALSE),		/* pcrel_offset */
285
286  /* An absolute 16 bit branch, for which bit 10 should be set to
287     indicate that the branch is expected to be taken.	The lower two
288     bits must be zero.  */
289  HOWTO (R_PPC_ADDR14_BRTAKEN,	/* type */
290	 0,			/* rightshift */
291	 2,			/* size (0 = byte, 1 = short, 2 = long) */
292	 16,			/* bitsize */
293	 FALSE,			/* pc_relative */
294	 0,			/* bitpos */
295	 complain_overflow_bitfield, /* complain_on_overflow */
296	 bfd_elf_generic_reloc,	/* special_function */
297	 "R_PPC_ADDR14_BRTAKEN",/* name */
298	 FALSE,			/* partial_inplace */
299	 0,			/* src_mask */
300	 0xfffc,		/* dst_mask */
301	 FALSE),		/* pcrel_offset */
302
303  /* An absolute 16 bit branch, for which bit 10 should be set to
304     indicate that the branch is not expected to be taken.  The lower
305     two bits must be zero.  */
306  HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
307	 0,			/* rightshift */
308	 2,			/* size (0 = byte, 1 = short, 2 = long) */
309	 16,			/* bitsize */
310	 FALSE,			/* pc_relative */
311	 0,			/* bitpos */
312	 complain_overflow_bitfield, /* complain_on_overflow */
313	 bfd_elf_generic_reloc,	/* special_function */
314	 "R_PPC_ADDR14_BRNTAKEN",/* name */
315	 FALSE,			/* partial_inplace */
316	 0,			/* src_mask */
317	 0xfffc,		/* dst_mask */
318	 FALSE),		/* pcrel_offset */
319
320  /* A relative 26 bit branch; the lower two bits must be zero.  */
321  HOWTO (R_PPC_REL24,		/* type */
322	 0,			/* rightshift */
323	 2,			/* size (0 = byte, 1 = short, 2 = long) */
324	 26,			/* bitsize */
325	 TRUE,			/* pc_relative */
326	 0,			/* bitpos */
327	 complain_overflow_signed, /* complain_on_overflow */
328	 bfd_elf_generic_reloc,	/* special_function */
329	 "R_PPC_REL24",		/* name */
330	 FALSE,			/* partial_inplace */
331	 0,			/* src_mask */
332	 0x3fffffc,		/* dst_mask */
333	 TRUE),			/* pcrel_offset */
334
335  /* A relative 16 bit branch; the lower two bits must be zero.  */
336  HOWTO (R_PPC_REL14,		/* type */
337	 0,			/* rightshift */
338	 2,			/* size (0 = byte, 1 = short, 2 = long) */
339	 16,			/* bitsize */
340	 TRUE,			/* pc_relative */
341	 0,			/* bitpos */
342	 complain_overflow_signed, /* complain_on_overflow */
343	 bfd_elf_generic_reloc,	/* special_function */
344	 "R_PPC_REL14",		/* name */
345	 FALSE,			/* partial_inplace */
346	 0,			/* src_mask */
347	 0xfffc,		/* dst_mask */
348	 TRUE),			/* pcrel_offset */
349
350  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
351     the branch is expected to be taken.  The lower two bits must be
352     zero.  */
353  HOWTO (R_PPC_REL14_BRTAKEN,	/* type */
354	 0,			/* rightshift */
355	 2,			/* size (0 = byte, 1 = short, 2 = long) */
356	 16,			/* bitsize */
357	 TRUE,			/* pc_relative */
358	 0,			/* bitpos */
359	 complain_overflow_signed, /* complain_on_overflow */
360	 bfd_elf_generic_reloc,	/* special_function */
361	 "R_PPC_REL14_BRTAKEN",	/* name */
362	 FALSE,			/* partial_inplace */
363	 0,			/* src_mask */
364	 0xfffc,		/* dst_mask */
365	 TRUE),			/* pcrel_offset */
366
367  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
368     the branch is not expected to be taken.  The lower two bits must
369     be zero.  */
370  HOWTO (R_PPC_REL14_BRNTAKEN,	/* type */
371	 0,			/* rightshift */
372	 2,			/* size (0 = byte, 1 = short, 2 = long) */
373	 16,			/* bitsize */
374	 TRUE,			/* pc_relative */
375	 0,			/* bitpos */
376	 complain_overflow_signed, /* complain_on_overflow */
377	 bfd_elf_generic_reloc,	/* special_function */
378	 "R_PPC_REL14_BRNTAKEN",/* name */
379	 FALSE,			/* partial_inplace */
380	 0,			/* src_mask */
381	 0xfffc,		/* dst_mask */
382	 TRUE),			/* pcrel_offset */
383
384  /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
385     symbol.  */
386  HOWTO (R_PPC_GOT16,		/* type */
387	 0,			/* rightshift */
388	 1,			/* size (0 = byte, 1 = short, 2 = long) */
389	 16,			/* bitsize */
390	 FALSE,			/* pc_relative */
391	 0,			/* bitpos */
392	 complain_overflow_signed, /* complain_on_overflow */
393	 bfd_elf_generic_reloc,	/* special_function */
394	 "R_PPC_GOT16",		/* name */
395	 FALSE,			/* partial_inplace */
396	 0,			/* src_mask */
397	 0xffff,		/* dst_mask */
398	 FALSE),		/* pcrel_offset */
399
400  /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
401     the symbol.  */
402  HOWTO (R_PPC_GOT16_LO,	/* type */
403	 0,			/* rightshift */
404	 1,			/* size (0 = byte, 1 = short, 2 = long) */
405	 16,			/* bitsize */
406	 FALSE,			/* pc_relative */
407	 0,			/* bitpos */
408	 complain_overflow_dont, /* complain_on_overflow */
409	 bfd_elf_generic_reloc,	/* special_function */
410	 "R_PPC_GOT16_LO",	/* name */
411	 FALSE,			/* partial_inplace */
412	 0,			/* src_mask */
413	 0xffff,		/* dst_mask */
414	 FALSE),		/* pcrel_offset */
415
416  /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
417     the symbol.  */
418  HOWTO (R_PPC_GOT16_HI,	/* type */
419	 16,			/* rightshift */
420	 1,			/* size (0 = byte, 1 = short, 2 = long) */
421	 16,			/* bitsize */
422	 FALSE,			/* pc_relative */
423	 0,			/* bitpos */
424	 complain_overflow_bitfield, /* complain_on_overflow */
425	 bfd_elf_generic_reloc,	/* special_function */
426	 "R_PPC_GOT16_HI",	/* name */
427	 FALSE,			/* partial_inplace */
428	 0,			/* src_mask */
429	 0xffff,		/* dst_mask */
430	 FALSE),		 /* pcrel_offset */
431
432  /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
433     the symbol.  */
434  HOWTO (R_PPC_GOT16_HA,	/* type */
435	 16,			/* rightshift */
436	 1,			/* size (0 = byte, 1 = short, 2 = long) */
437	 16,			/* bitsize */
438	 FALSE,			/* pc_relative */
439	 0,			/* bitpos */
440	 complain_overflow_bitfield, /* complain_on_overflow */
441	 ppc_elf_addr16_ha_reloc, /* special_function */
442	 "R_PPC_GOT16_HA",	/* name */
443	 FALSE,			/* partial_inplace */
444	 0,			/* src_mask */
445	 0xffff,		/* dst_mask */
446	 FALSE),		/* pcrel_offset */
447
448  /* Like R_PPC_REL24, but referring to the procedure linkage table
449     entry for the symbol.  */
450  HOWTO (R_PPC_PLTREL24,	/* type */
451	 0,			/* rightshift */
452	 2,			/* size (0 = byte, 1 = short, 2 = long) */
453	 26,			/* bitsize */
454	 TRUE,			/* pc_relative */
455	 0,			/* bitpos */
456	 complain_overflow_signed,  /* complain_on_overflow */
457	 bfd_elf_generic_reloc,	/* special_function */
458	 "R_PPC_PLTREL24",	/* name */
459	 FALSE,			/* partial_inplace */
460	 0,			/* src_mask */
461	 0x3fffffc,		/* dst_mask */
462	 TRUE),			/* pcrel_offset */
463
464  /* This is used only by the dynamic linker.  The symbol should exist
465     both in the object being run and in some shared library.  The
466     dynamic linker copies the data addressed by the symbol from the
467     shared library into the object, because the object being
468     run has to have the data at some particular address.  */
469  HOWTO (R_PPC_COPY,		/* type */
470	 0,			/* rightshift */
471	 2,			/* size (0 = byte, 1 = short, 2 = long) */
472	 32,			/* bitsize */
473	 FALSE,			/* pc_relative */
474	 0,			/* bitpos */
475	 complain_overflow_bitfield, /* complain_on_overflow */
476	 bfd_elf_generic_reloc,	 /* special_function */
477	 "R_PPC_COPY",		/* name */
478	 FALSE,			/* partial_inplace */
479	 0,			/* src_mask */
480	 0,			/* dst_mask */
481	 FALSE),		/* pcrel_offset */
482
483  /* Like R_PPC_ADDR32, but used when setting global offset table
484     entries.  */
485  HOWTO (R_PPC_GLOB_DAT,	/* type */
486	 0,			/* rightshift */
487	 2,			/* size (0 = byte, 1 = short, 2 = long) */
488	 32,			/* bitsize */
489	 FALSE,			/* pc_relative */
490	 0,			/* bitpos */
491	 complain_overflow_bitfield, /* complain_on_overflow */
492	 bfd_elf_generic_reloc,	 /* special_function */
493	 "R_PPC_GLOB_DAT",	/* name */
494	 FALSE,			/* partial_inplace */
495	 0,			/* src_mask */
496	 0xffffffff,		/* dst_mask */
497	 FALSE),		/* pcrel_offset */
498
499  /* Marks a procedure linkage table entry for a symbol.  */
500  HOWTO (R_PPC_JMP_SLOT,	/* type */
501	 0,			/* rightshift */
502	 2,			/* size (0 = byte, 1 = short, 2 = long) */
503	 32,			/* bitsize */
504	 FALSE,			/* pc_relative */
505	 0,			/* bitpos */
506	 complain_overflow_bitfield, /* complain_on_overflow */
507	 bfd_elf_generic_reloc,	 /* special_function */
508	 "R_PPC_JMP_SLOT",	/* name */
509	 FALSE,			/* partial_inplace */
510	 0,			/* src_mask */
511	 0,			/* dst_mask */
512	 FALSE),		/* pcrel_offset */
513
514  /* Used only by the dynamic linker.  When the object is run, this
515     longword is set to the load address of the object, plus the
516     addend.  */
517  HOWTO (R_PPC_RELATIVE,	/* type */
518	 0,			/* rightshift */
519	 2,			/* size (0 = byte, 1 = short, 2 = long) */
520	 32,			/* bitsize */
521	 FALSE,			/* pc_relative */
522	 0,			/* bitpos */
523	 complain_overflow_bitfield, /* complain_on_overflow */
524	 bfd_elf_generic_reloc,	 /* special_function */
525	 "R_PPC_RELATIVE",	/* name */
526	 FALSE,			/* partial_inplace */
527	 0,			/* src_mask */
528	 0xffffffff,		/* dst_mask */
529	 FALSE),		/* pcrel_offset */
530
531  /* Like R_PPC_REL24, but uses the value of the symbol within the
532     object rather than the final value.  Normally used for
533     _GLOBAL_OFFSET_TABLE_.  */
534  HOWTO (R_PPC_LOCAL24PC,	/* type */
535	 0,			/* rightshift */
536	 2,			/* size (0 = byte, 1 = short, 2 = long) */
537	 26,			/* bitsize */
538	 TRUE,			/* pc_relative */
539	 0,			/* bitpos */
540	 complain_overflow_signed, /* complain_on_overflow */
541	 bfd_elf_generic_reloc,	/* special_function */
542	 "R_PPC_LOCAL24PC",	/* name */
543	 FALSE,			/* partial_inplace */
544	 0,			/* src_mask */
545	 0x3fffffc,		/* dst_mask */
546	 TRUE),			/* pcrel_offset */
547
548  /* Like R_PPC_ADDR32, but may be unaligned.  */
549  HOWTO (R_PPC_UADDR32,		/* type */
550	 0,			/* rightshift */
551	 2,			/* size (0 = byte, 1 = short, 2 = long) */
552	 32,			/* bitsize */
553	 FALSE,			/* pc_relative */
554	 0,			/* bitpos */
555	 complain_overflow_bitfield, /* complain_on_overflow */
556	 bfd_elf_generic_reloc,	/* special_function */
557	 "R_PPC_UADDR32",	/* name */
558	 FALSE,			/* partial_inplace */
559	 0,			/* src_mask */
560	 0xffffffff,		/* dst_mask */
561	 FALSE),		/* pcrel_offset */
562
563  /* Like R_PPC_ADDR16, but may be unaligned.  */
564  HOWTO (R_PPC_UADDR16,		/* type */
565	 0,			/* rightshift */
566	 1,			/* size (0 = byte, 1 = short, 2 = long) */
567	 16,			/* bitsize */
568	 FALSE,			/* pc_relative */
569	 0,			/* bitpos */
570	 complain_overflow_bitfield, /* complain_on_overflow */
571	 bfd_elf_generic_reloc,	/* special_function */
572	 "R_PPC_UADDR16",	/* name */
573	 FALSE,			/* partial_inplace */
574	 0,			/* src_mask */
575	 0xffff,		/* dst_mask */
576	 FALSE),		/* pcrel_offset */
577
578  /* 32-bit PC relative */
579  HOWTO (R_PPC_REL32,		/* type */
580	 0,			/* rightshift */
581	 2,			/* size (0 = byte, 1 = short, 2 = long) */
582	 32,			/* bitsize */
583	 TRUE,			/* pc_relative */
584	 0,			/* bitpos */
585	 complain_overflow_bitfield, /* complain_on_overflow */
586	 bfd_elf_generic_reloc,	/* special_function */
587	 "R_PPC_REL32",		/* name */
588	 FALSE,			/* partial_inplace */
589	 0,			/* src_mask */
590	 0xffffffff,		/* dst_mask */
591	 TRUE),			/* pcrel_offset */
592
593  /* 32-bit relocation to the symbol's procedure linkage table.
594     FIXME: not supported.  */
595  HOWTO (R_PPC_PLT32,		/* type */
596	 0,			/* rightshift */
597	 2,			/* size (0 = byte, 1 = short, 2 = long) */
598	 32,			/* bitsize */
599	 FALSE,			/* pc_relative */
600	 0,			/* bitpos */
601	 complain_overflow_bitfield, /* complain_on_overflow */
602	 bfd_elf_generic_reloc,	/* special_function */
603	 "R_PPC_PLT32",		/* name */
604	 FALSE,			/* partial_inplace */
605	 0,			/* src_mask */
606	 0,			/* dst_mask */
607	 FALSE),		/* pcrel_offset */
608
609  /* 32-bit PC relative relocation to the symbol's procedure linkage table.
610     FIXME: not supported.  */
611  HOWTO (R_PPC_PLTREL32,	/* type */
612	 0,			/* rightshift */
613	 2,			/* size (0 = byte, 1 = short, 2 = long) */
614	 32,			/* bitsize */
615	 TRUE,			/* pc_relative */
616	 0,			/* bitpos */
617	 complain_overflow_bitfield, /* complain_on_overflow */
618	 bfd_elf_generic_reloc,	/* special_function */
619	 "R_PPC_PLTREL32",	/* name */
620	 FALSE,			/* partial_inplace */
621	 0,			/* src_mask */
622	 0,			/* dst_mask */
623	 TRUE),			/* pcrel_offset */
624
625  /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
626     the symbol.  */
627  HOWTO (R_PPC_PLT16_LO,	/* type */
628	 0,			/* rightshift */
629	 1,			/* size (0 = byte, 1 = short, 2 = long) */
630	 16,			/* bitsize */
631	 FALSE,			/* pc_relative */
632	 0,			/* bitpos */
633	 complain_overflow_dont, /* complain_on_overflow */
634	 bfd_elf_generic_reloc,	/* special_function */
635	 "R_PPC_PLT16_LO",	/* name */
636	 FALSE,			/* partial_inplace */
637	 0,			/* src_mask */
638	 0xffff,		/* dst_mask */
639	 FALSE),		/* pcrel_offset */
640
641  /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
642     the symbol.  */
643  HOWTO (R_PPC_PLT16_HI,	/* type */
644	 16,			/* rightshift */
645	 1,			/* size (0 = byte, 1 = short, 2 = long) */
646	 16,			/* bitsize */
647	 FALSE,			/* pc_relative */
648	 0,			/* bitpos */
649	 complain_overflow_bitfield, /* complain_on_overflow */
650	 bfd_elf_generic_reloc,	/* special_function */
651	 "R_PPC_PLT16_HI",	/* name */
652	 FALSE,			/* partial_inplace */
653	 0,			/* src_mask */
654	 0xffff,		/* dst_mask */
655	 FALSE),		 /* pcrel_offset */
656
657  /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
658     the symbol.  */
659  HOWTO (R_PPC_PLT16_HA,	/* type */
660	 16,			/* rightshift */
661	 1,			/* size (0 = byte, 1 = short, 2 = long) */
662	 16,			/* bitsize */
663	 FALSE,			/* pc_relative */
664	 0,			/* bitpos */
665	 complain_overflow_bitfield, /* complain_on_overflow */
666	 ppc_elf_addr16_ha_reloc, /* special_function */
667	 "R_PPC_PLT16_HA",	/* name */
668	 FALSE,			/* partial_inplace */
669	 0,			/* src_mask */
670	 0xffff,		/* dst_mask */
671	 FALSE),		/* pcrel_offset */
672
673  /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
674     small data items.  */
675  HOWTO (R_PPC_SDAREL16,	/* type */
676	 0,			/* rightshift */
677	 1,			/* size (0 = byte, 1 = short, 2 = long) */
678	 16,			/* bitsize */
679	 FALSE,			/* pc_relative */
680	 0,			/* bitpos */
681	 complain_overflow_signed, /* complain_on_overflow */
682	 bfd_elf_generic_reloc,	/* special_function */
683	 "R_PPC_SDAREL16",	/* name */
684	 FALSE,			/* partial_inplace */
685	 0,			/* src_mask */
686	 0xffff,		/* dst_mask */
687	 FALSE),		/* pcrel_offset */
688
689  /* 16-bit section relative relocation.  */
690  HOWTO (R_PPC_SECTOFF,		/* type */
691	 0,			/* rightshift */
692	 1,			/* size (0 = byte, 1 = short, 2 = long) */
693	 16,			/* bitsize */
694	 FALSE,			/* pc_relative */
695	 0,			/* bitpos */
696	 complain_overflow_bitfield, /* complain_on_overflow */
697	 bfd_elf_generic_reloc,	/* special_function */
698	 "R_PPC_SECTOFF",	/* name */
699	 FALSE,			/* partial_inplace */
700	 0,			/* src_mask */
701	 0xffff,		/* dst_mask */
702	 FALSE),		/* pcrel_offset */
703
704  /* 16-bit lower half section relative relocation.  */
705  HOWTO (R_PPC_SECTOFF_LO,	  /* type */
706	 0,			/* rightshift */
707	 1,			/* size (0 = byte, 1 = short, 2 = long) */
708	 16,			/* bitsize */
709	 FALSE,			/* pc_relative */
710	 0,			/* bitpos */
711	 complain_overflow_dont, /* complain_on_overflow */
712	 bfd_elf_generic_reloc,	/* special_function */
713	 "R_PPC_SECTOFF_LO",	/* name */
714	 FALSE,			/* partial_inplace */
715	 0,			/* src_mask */
716	 0xffff,		/* dst_mask */
717	 FALSE),		/* pcrel_offset */
718
719  /* 16-bit upper half section relative relocation.  */
720  HOWTO (R_PPC_SECTOFF_HI,	/* type */
721	 16,			/* rightshift */
722	 1,			/* size (0 = byte, 1 = short, 2 = long) */
723	 16,			/* bitsize */
724	 FALSE,			/* pc_relative */
725	 0,			/* bitpos */
726	 complain_overflow_bitfield, /* complain_on_overflow */
727	 bfd_elf_generic_reloc,	/* special_function */
728	 "R_PPC_SECTOFF_HI",	/* name */
729	 FALSE,			/* partial_inplace */
730	 0,			/* src_mask */
731	 0xffff,		/* dst_mask */
732	 FALSE),		 /* pcrel_offset */
733
734  /* 16-bit upper half adjusted section relative relocation.  */
735  HOWTO (R_PPC_SECTOFF_HA,	/* type */
736	 16,			/* rightshift */
737	 1,			/* size (0 = byte, 1 = short, 2 = long) */
738	 16,			/* bitsize */
739	 FALSE,			/* pc_relative */
740	 0,			/* bitpos */
741	 complain_overflow_bitfield, /* complain_on_overflow */
742	 ppc_elf_addr16_ha_reloc, /* special_function */
743	 "R_PPC_SECTOFF_HA",	/* name */
744	 FALSE,			/* partial_inplace */
745	 0,			/* src_mask */
746	 0xffff,		/* dst_mask */
747	 FALSE),		/* pcrel_offset */
748
749  /* Marker relocs for TLS.  */
750  HOWTO (R_PPC_TLS,
751	 0,			/* rightshift */
752	 2,			/* size (0 = byte, 1 = short, 2 = long) */
753	 32,			/* bitsize */
754	 FALSE,			/* pc_relative */
755	 0,			/* bitpos */
756	 complain_overflow_dont, /* complain_on_overflow */
757	 bfd_elf_generic_reloc,	/* special_function */
758	 "R_PPC_TLS",		/* name */
759	 FALSE,			/* partial_inplace */
760	 0,			/* src_mask */
761	 0,			/* dst_mask */
762	 FALSE),		/* pcrel_offset */
763
764  HOWTO (R_PPC_TLSGD,
765	 0,			/* rightshift */
766	 2,			/* size (0 = byte, 1 = short, 2 = long) */
767	 32,			/* bitsize */
768	 FALSE,			/* pc_relative */
769	 0,			/* bitpos */
770	 complain_overflow_dont, /* complain_on_overflow */
771	 bfd_elf_generic_reloc, /* special_function */
772	 "R_PPC_TLSGD",		/* name */
773	 FALSE,			/* partial_inplace */
774	 0,			/* src_mask */
775	 0,			/* dst_mask */
776	 FALSE),		/* pcrel_offset */
777
778  HOWTO (R_PPC_TLSLD,
779	 0,			/* rightshift */
780	 2,			/* size (0 = byte, 1 = short, 2 = long) */
781	 32,			/* bitsize */
782	 FALSE,			/* pc_relative */
783	 0,			/* bitpos */
784	 complain_overflow_dont, /* complain_on_overflow */
785	 bfd_elf_generic_reloc, /* special_function */
786	 "R_PPC_TLSLD",		/* name */
787	 FALSE,			/* partial_inplace */
788	 0,			/* src_mask */
789	 0,			/* dst_mask */
790	 FALSE),		/* pcrel_offset */
791
792  /* Computes the load module index of the load module that contains the
793     definition of its TLS sym.  */
794  HOWTO (R_PPC_DTPMOD32,
795	 0,			/* rightshift */
796	 2,			/* size (0 = byte, 1 = short, 2 = long) */
797	 32,			/* bitsize */
798	 FALSE,			/* pc_relative */
799	 0,			/* bitpos */
800	 complain_overflow_dont, /* complain_on_overflow */
801	 ppc_elf_unhandled_reloc, /* special_function */
802	 "R_PPC_DTPMOD32",	/* name */
803	 FALSE,			/* partial_inplace */
804	 0,			/* src_mask */
805	 0xffffffff,		/* dst_mask */
806	 FALSE),		/* pcrel_offset */
807
808  /* Computes a dtv-relative displacement, the difference between the value
809     of sym+add and the base address of the thread-local storage block that
810     contains the definition of sym, minus 0x8000.  */
811  HOWTO (R_PPC_DTPREL32,
812	 0,			/* rightshift */
813	 2,			/* size (0 = byte, 1 = short, 2 = long) */
814	 32,			/* bitsize */
815	 FALSE,			/* pc_relative */
816	 0,			/* bitpos */
817	 complain_overflow_dont, /* complain_on_overflow */
818	 ppc_elf_unhandled_reloc, /* special_function */
819	 "R_PPC_DTPREL32",	/* name */
820	 FALSE,			/* partial_inplace */
821	 0,			/* src_mask */
822	 0xffffffff,		/* dst_mask */
823	 FALSE),		/* pcrel_offset */
824
825  /* A 16 bit dtprel reloc.  */
826  HOWTO (R_PPC_DTPREL16,
827	 0,			/* rightshift */
828	 1,			/* size (0 = byte, 1 = short, 2 = long) */
829	 16,			/* bitsize */
830	 FALSE,			/* pc_relative */
831	 0,			/* bitpos */
832	 complain_overflow_signed, /* complain_on_overflow */
833	 ppc_elf_unhandled_reloc, /* special_function */
834	 "R_PPC_DTPREL16",	/* name */
835	 FALSE,			/* partial_inplace */
836	 0,			/* src_mask */
837	 0xffff,		/* dst_mask */
838	 FALSE),		/* pcrel_offset */
839
840  /* Like DTPREL16, but no overflow.  */
841  HOWTO (R_PPC_DTPREL16_LO,
842	 0,			/* rightshift */
843	 1,			/* size (0 = byte, 1 = short, 2 = long) */
844	 16,			/* bitsize */
845	 FALSE,			/* pc_relative */
846	 0,			/* bitpos */
847	 complain_overflow_dont, /* complain_on_overflow */
848	 ppc_elf_unhandled_reloc, /* special_function */
849	 "R_PPC_DTPREL16_LO",	/* name */
850	 FALSE,			/* partial_inplace */
851	 0,			/* src_mask */
852	 0xffff,		/* dst_mask */
853	 FALSE),		/* pcrel_offset */
854
855  /* Like DTPREL16_LO, but next higher group of 16 bits.  */
856  HOWTO (R_PPC_DTPREL16_HI,
857	 16,			/* rightshift */
858	 1,			/* size (0 = byte, 1 = short, 2 = long) */
859	 16,			/* bitsize */
860	 FALSE,			/* pc_relative */
861	 0,			/* bitpos */
862	 complain_overflow_dont, /* complain_on_overflow */
863	 ppc_elf_unhandled_reloc, /* special_function */
864	 "R_PPC_DTPREL16_HI",	/* name */
865	 FALSE,			/* partial_inplace */
866	 0,			/* src_mask */
867	 0xffff,		/* dst_mask */
868	 FALSE),		/* pcrel_offset */
869
870  /* Like DTPREL16_HI, but adjust for low 16 bits.  */
871  HOWTO (R_PPC_DTPREL16_HA,
872	 16,			/* rightshift */
873	 1,			/* size (0 = byte, 1 = short, 2 = long) */
874	 16,			/* bitsize */
875	 FALSE,			/* pc_relative */
876	 0,			/* bitpos */
877	 complain_overflow_dont, /* complain_on_overflow */
878	 ppc_elf_unhandled_reloc, /* special_function */
879	 "R_PPC_DTPREL16_HA",	/* name */
880	 FALSE,			/* partial_inplace */
881	 0,			/* src_mask */
882	 0xffff,		/* dst_mask */
883	 FALSE),		/* pcrel_offset */
884
885  /* Computes a tp-relative displacement, the difference between the value of
886     sym+add and the value of the thread pointer (r13).  */
887  HOWTO (R_PPC_TPREL32,
888	 0,			/* rightshift */
889	 2,			/* size (0 = byte, 1 = short, 2 = long) */
890	 32,			/* bitsize */
891	 FALSE,			/* pc_relative */
892	 0,			/* bitpos */
893	 complain_overflow_dont, /* complain_on_overflow */
894	 ppc_elf_unhandled_reloc, /* special_function */
895	 "R_PPC_TPREL32",	/* name */
896	 FALSE,			/* partial_inplace */
897	 0,			/* src_mask */
898	 0xffffffff,		/* dst_mask */
899	 FALSE),		/* pcrel_offset */
900
901  /* A 16 bit tprel reloc.  */
902  HOWTO (R_PPC_TPREL16,
903	 0,			/* rightshift */
904	 1,			/* size (0 = byte, 1 = short, 2 = long) */
905	 16,			/* bitsize */
906	 FALSE,			/* pc_relative */
907	 0,			/* bitpos */
908	 complain_overflow_signed, /* complain_on_overflow */
909	 ppc_elf_unhandled_reloc, /* special_function */
910	 "R_PPC_TPREL16",	/* name */
911	 FALSE,			/* partial_inplace */
912	 0,			/* src_mask */
913	 0xffff,		/* dst_mask */
914	 FALSE),		/* pcrel_offset */
915
916  /* Like TPREL16, but no overflow.  */
917  HOWTO (R_PPC_TPREL16_LO,
918	 0,			/* rightshift */
919	 1,			/* size (0 = byte, 1 = short, 2 = long) */
920	 16,			/* bitsize */
921	 FALSE,			/* pc_relative */
922	 0,			/* bitpos */
923	 complain_overflow_dont, /* complain_on_overflow */
924	 ppc_elf_unhandled_reloc, /* special_function */
925	 "R_PPC_TPREL16_LO",	/* name */
926	 FALSE,			/* partial_inplace */
927	 0,			/* src_mask */
928	 0xffff,		/* dst_mask */
929	 FALSE),		/* pcrel_offset */
930
931  /* Like TPREL16_LO, but next higher group of 16 bits.  */
932  HOWTO (R_PPC_TPREL16_HI,
933	 16,			/* rightshift */
934	 1,			/* size (0 = byte, 1 = short, 2 = long) */
935	 16,			/* bitsize */
936	 FALSE,			/* pc_relative */
937	 0,			/* bitpos */
938	 complain_overflow_dont, /* complain_on_overflow */
939	 ppc_elf_unhandled_reloc, /* special_function */
940	 "R_PPC_TPREL16_HI",	/* name */
941	 FALSE,			/* partial_inplace */
942	 0,			/* src_mask */
943	 0xffff,		/* dst_mask */
944	 FALSE),		/* pcrel_offset */
945
946  /* Like TPREL16_HI, but adjust for low 16 bits.  */
947  HOWTO (R_PPC_TPREL16_HA,
948	 16,			/* rightshift */
949	 1,			/* size (0 = byte, 1 = short, 2 = long) */
950	 16,			/* bitsize */
951	 FALSE,			/* pc_relative */
952	 0,			/* bitpos */
953	 complain_overflow_dont, /* complain_on_overflow */
954	 ppc_elf_unhandled_reloc, /* special_function */
955	 "R_PPC_TPREL16_HA",	/* name */
956	 FALSE,			/* partial_inplace */
957	 0,			/* src_mask */
958	 0xffff,		/* dst_mask */
959	 FALSE),		/* pcrel_offset */
960
961  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
962     with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
963     to the first entry.  */
964  HOWTO (R_PPC_GOT_TLSGD16,
965	 0,			/* rightshift */
966	 1,			/* size (0 = byte, 1 = short, 2 = long) */
967	 16,			/* bitsize */
968	 FALSE,			/* pc_relative */
969	 0,			/* bitpos */
970	 complain_overflow_signed, /* complain_on_overflow */
971	 ppc_elf_unhandled_reloc, /* special_function */
972	 "R_PPC_GOT_TLSGD16",	/* name */
973	 FALSE,			/* partial_inplace */
974	 0,			/* src_mask */
975	 0xffff,		/* dst_mask */
976	 FALSE),		/* pcrel_offset */
977
978  /* Like GOT_TLSGD16, but no overflow.  */
979  HOWTO (R_PPC_GOT_TLSGD16_LO,
980	 0,			/* rightshift */
981	 1,			/* size (0 = byte, 1 = short, 2 = long) */
982	 16,			/* bitsize */
983	 FALSE,			/* pc_relative */
984	 0,			/* bitpos */
985	 complain_overflow_dont, /* complain_on_overflow */
986	 ppc_elf_unhandled_reloc, /* special_function */
987	 "R_PPC_GOT_TLSGD16_LO", /* name */
988	 FALSE,			/* partial_inplace */
989	 0,			/* src_mask */
990	 0xffff,		/* dst_mask */
991	 FALSE),		/* pcrel_offset */
992
993  /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
994  HOWTO (R_PPC_GOT_TLSGD16_HI,
995	 16,			/* rightshift */
996	 1,			/* size (0 = byte, 1 = short, 2 = long) */
997	 16,			/* bitsize */
998	 FALSE,			/* pc_relative */
999	 0,			/* bitpos */
1000	 complain_overflow_dont, /* complain_on_overflow */
1001	 ppc_elf_unhandled_reloc, /* special_function */
1002	 "R_PPC_GOT_TLSGD16_HI", /* name */
1003	 FALSE,			/* partial_inplace */
1004	 0,			/* src_mask */
1005	 0xffff,		/* dst_mask */
1006	 FALSE),		/* pcrel_offset */
1007
1008  /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1009  HOWTO (R_PPC_GOT_TLSGD16_HA,
1010	 16,			/* rightshift */
1011	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1012	 16,			/* bitsize */
1013	 FALSE,			/* pc_relative */
1014	 0,			/* bitpos */
1015	 complain_overflow_dont, /* complain_on_overflow */
1016	 ppc_elf_unhandled_reloc, /* special_function */
1017	 "R_PPC_GOT_TLSGD16_HA", /* name */
1018	 FALSE,			/* partial_inplace */
1019	 0,			/* src_mask */
1020	 0xffff,		/* dst_mask */
1021	 FALSE),		/* pcrel_offset */
1022
1023  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1024     with values (sym+add)@dtpmod and zero, and computes the offset to the
1025     first entry.  */
1026  HOWTO (R_PPC_GOT_TLSLD16,
1027	 0,			/* rightshift */
1028	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1029	 16,			/* bitsize */
1030	 FALSE,			/* pc_relative */
1031	 0,			/* bitpos */
1032	 complain_overflow_signed, /* complain_on_overflow */
1033	 ppc_elf_unhandled_reloc, /* special_function */
1034	 "R_PPC_GOT_TLSLD16",	/* name */
1035	 FALSE,			/* partial_inplace */
1036	 0,			/* src_mask */
1037	 0xffff,		/* dst_mask */
1038	 FALSE),		/* pcrel_offset */
1039
1040  /* Like GOT_TLSLD16, but no overflow.  */
1041  HOWTO (R_PPC_GOT_TLSLD16_LO,
1042	 0,			/* rightshift */
1043	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1044	 16,			/* bitsize */
1045	 FALSE,			/* pc_relative */
1046	 0,			/* bitpos */
1047	 complain_overflow_dont, /* complain_on_overflow */
1048	 ppc_elf_unhandled_reloc, /* special_function */
1049	 "R_PPC_GOT_TLSLD16_LO", /* name */
1050	 FALSE,			/* partial_inplace */
1051	 0,			/* src_mask */
1052	 0xffff,		/* dst_mask */
1053	 FALSE),		/* pcrel_offset */
1054
1055  /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1056  HOWTO (R_PPC_GOT_TLSLD16_HI,
1057	 16,			/* rightshift */
1058	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1059	 16,			/* bitsize */
1060	 FALSE,			/* pc_relative */
1061	 0,			/* bitpos */
1062	 complain_overflow_dont, /* complain_on_overflow */
1063	 ppc_elf_unhandled_reloc, /* special_function */
1064	 "R_PPC_GOT_TLSLD16_HI", /* name */
1065	 FALSE,			/* partial_inplace */
1066	 0,			/* src_mask */
1067	 0xffff,		/* dst_mask */
1068	 FALSE),		/* pcrel_offset */
1069
1070  /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1071  HOWTO (R_PPC_GOT_TLSLD16_HA,
1072	 16,			/* rightshift */
1073	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1074	 16,			/* bitsize */
1075	 FALSE,			/* pc_relative */
1076	 0,			/* bitpos */
1077	 complain_overflow_dont, /* complain_on_overflow */
1078	 ppc_elf_unhandled_reloc, /* special_function */
1079	 "R_PPC_GOT_TLSLD16_HA", /* name */
1080	 FALSE,			/* partial_inplace */
1081	 0,			/* src_mask */
1082	 0xffff,		/* dst_mask */
1083	 FALSE),		/* pcrel_offset */
1084
1085  /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1086     the offset to the entry.  */
1087  HOWTO (R_PPC_GOT_DTPREL16,
1088	 0,			/* rightshift */
1089	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1090	 16,			/* bitsize */
1091	 FALSE,			/* pc_relative */
1092	 0,			/* bitpos */
1093	 complain_overflow_signed, /* complain_on_overflow */
1094	 ppc_elf_unhandled_reloc, /* special_function */
1095	 "R_PPC_GOT_DTPREL16",	/* name */
1096	 FALSE,			/* partial_inplace */
1097	 0,			/* src_mask */
1098	 0xffff,		/* dst_mask */
1099	 FALSE),		/* pcrel_offset */
1100
1101  /* Like GOT_DTPREL16, but no overflow.  */
1102  HOWTO (R_PPC_GOT_DTPREL16_LO,
1103	 0,			/* rightshift */
1104	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1105	 16,			/* bitsize */
1106	 FALSE,			/* pc_relative */
1107	 0,			/* bitpos */
1108	 complain_overflow_dont, /* complain_on_overflow */
1109	 ppc_elf_unhandled_reloc, /* special_function */
1110	 "R_PPC_GOT_DTPREL16_LO", /* name */
1111	 FALSE,			/* partial_inplace */
1112	 0,			/* src_mask */
1113	 0xffff,		/* dst_mask */
1114	 FALSE),		/* pcrel_offset */
1115
1116  /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
1117  HOWTO (R_PPC_GOT_DTPREL16_HI,
1118	 16,			/* rightshift */
1119	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1120	 16,			/* bitsize */
1121	 FALSE,			/* pc_relative */
1122	 0,			/* bitpos */
1123	 complain_overflow_dont, /* complain_on_overflow */
1124	 ppc_elf_unhandled_reloc, /* special_function */
1125	 "R_PPC_GOT_DTPREL16_HI", /* name */
1126	 FALSE,			/* partial_inplace */
1127	 0,			/* src_mask */
1128	 0xffff,		/* dst_mask */
1129	 FALSE),		/* pcrel_offset */
1130
1131  /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1132  HOWTO (R_PPC_GOT_DTPREL16_HA,
1133	 16,			/* rightshift */
1134	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1135	 16,			/* bitsize */
1136	 FALSE,			/* pc_relative */
1137	 0,			/* bitpos */
1138	 complain_overflow_dont, /* complain_on_overflow */
1139	 ppc_elf_unhandled_reloc, /* special_function */
1140	 "R_PPC_GOT_DTPREL16_HA", /* name */
1141	 FALSE,			/* partial_inplace */
1142	 0,			/* src_mask */
1143	 0xffff,		/* dst_mask */
1144	 FALSE),		/* pcrel_offset */
1145
1146  /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1147     offset to the entry.  */
1148  HOWTO (R_PPC_GOT_TPREL16,
1149	 0,			/* rightshift */
1150	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1151	 16,			/* bitsize */
1152	 FALSE,			/* pc_relative */
1153	 0,			/* bitpos */
1154	 complain_overflow_signed, /* complain_on_overflow */
1155	 ppc_elf_unhandled_reloc, /* special_function */
1156	 "R_PPC_GOT_TPREL16",	/* name */
1157	 FALSE,			/* partial_inplace */
1158	 0,			/* src_mask */
1159	 0xffff,		/* dst_mask */
1160	 FALSE),		/* pcrel_offset */
1161
1162  /* Like GOT_TPREL16, but no overflow.  */
1163  HOWTO (R_PPC_GOT_TPREL16_LO,
1164	 0,			/* rightshift */
1165	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1166	 16,			/* bitsize */
1167	 FALSE,			/* pc_relative */
1168	 0,			/* bitpos */
1169	 complain_overflow_dont, /* complain_on_overflow */
1170	 ppc_elf_unhandled_reloc, /* special_function */
1171	 "R_PPC_GOT_TPREL16_LO", /* name */
1172	 FALSE,			/* partial_inplace */
1173	 0,			/* src_mask */
1174	 0xffff,		/* dst_mask */
1175	 FALSE),		/* pcrel_offset */
1176
1177  /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
1178  HOWTO (R_PPC_GOT_TPREL16_HI,
1179	 16,			/* rightshift */
1180	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1181	 16,			/* bitsize */
1182	 FALSE,			/* pc_relative */
1183	 0,			/* bitpos */
1184	 complain_overflow_dont, /* complain_on_overflow */
1185	 ppc_elf_unhandled_reloc, /* special_function */
1186	 "R_PPC_GOT_TPREL16_HI", /* name */
1187	 FALSE,			/* partial_inplace */
1188	 0,			/* src_mask */
1189	 0xffff,		/* dst_mask */
1190	 FALSE),		/* pcrel_offset */
1191
1192  /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1193  HOWTO (R_PPC_GOT_TPREL16_HA,
1194	 16,			/* rightshift */
1195	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1196	 16,			/* bitsize */
1197	 FALSE,			/* pc_relative */
1198	 0,			/* bitpos */
1199	 complain_overflow_dont, /* complain_on_overflow */
1200	 ppc_elf_unhandled_reloc, /* special_function */
1201	 "R_PPC_GOT_TPREL16_HA", /* name */
1202	 FALSE,			/* partial_inplace */
1203	 0,			/* src_mask */
1204	 0xffff,		/* dst_mask */
1205	 FALSE),		/* pcrel_offset */
1206
1207  /* The remaining relocs are from the Embedded ELF ABI, and are not
1208     in the SVR4 ELF ABI.  */
1209
1210  /* 32 bit value resulting from the addend minus the symbol.  */
1211  HOWTO (R_PPC_EMB_NADDR32,	/* type */
1212	 0,			/* rightshift */
1213	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1214	 32,			/* bitsize */
1215	 FALSE,			/* pc_relative */
1216	 0,			/* bitpos */
1217	 complain_overflow_bitfield, /* complain_on_overflow */
1218	 bfd_elf_generic_reloc,	/* special_function */
1219	 "R_PPC_EMB_NADDR32",	/* name */
1220	 FALSE,			/* partial_inplace */
1221	 0,			/* src_mask */
1222	 0xffffffff,		/* dst_mask */
1223	 FALSE),		/* pcrel_offset */
1224
1225  /* 16 bit value resulting from the addend minus the symbol.  */
1226  HOWTO (R_PPC_EMB_NADDR16,	/* type */
1227	 0,			/* rightshift */
1228	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1229	 16,			/* bitsize */
1230	 FALSE,			/* pc_relative */
1231	 0,			/* bitpos */
1232	 complain_overflow_bitfield, /* complain_on_overflow */
1233	 bfd_elf_generic_reloc,	/* special_function */
1234	 "R_PPC_EMB_NADDR16",	/* name */
1235	 FALSE,			/* partial_inplace */
1236	 0,			/* src_mask */
1237	 0xffff,		/* dst_mask */
1238	 FALSE),		/* pcrel_offset */
1239
1240  /* 16 bit value resulting from the addend minus the symbol.  */
1241  HOWTO (R_PPC_EMB_NADDR16_LO,	/* type */
1242	 0,			/* rightshift */
1243	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1244	 16,			/* bitsize */
1245	 FALSE,			/* pc_relative */
1246	 0,			/* bitpos */
1247	 complain_overflow_dont,/* complain_on_overflow */
1248	 bfd_elf_generic_reloc,	/* special_function */
1249	 "R_PPC_EMB_ADDR16_LO",	/* name */
1250	 FALSE,			/* partial_inplace */
1251	 0,			/* src_mask */
1252	 0xffff,		/* dst_mask */
1253	 FALSE),		/* pcrel_offset */
1254
1255  /* The high order 16 bits of the addend minus the symbol.  */
1256  HOWTO (R_PPC_EMB_NADDR16_HI,	/* type */
1257	 16,			/* rightshift */
1258	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1259	 16,			/* bitsize */
1260	 FALSE,			/* pc_relative */
1261	 0,			/* bitpos */
1262	 complain_overflow_dont, /* complain_on_overflow */
1263	 bfd_elf_generic_reloc,	/* special_function */
1264	 "R_PPC_EMB_NADDR16_HI", /* name */
1265	 FALSE,			/* partial_inplace */
1266	 0,			/* src_mask */
1267	 0xffff,		/* dst_mask */
1268	 FALSE),		/* pcrel_offset */
1269
1270  /* The high order 16 bits of the result of the addend minus the address,
1271     plus 1 if the contents of the low 16 bits, treated as a signed number,
1272     is negative.  */
1273  HOWTO (R_PPC_EMB_NADDR16_HA,	/* type */
1274	 16,			/* rightshift */
1275	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1276	 16,			/* bitsize */
1277	 FALSE,			/* pc_relative */
1278	 0,			/* bitpos */
1279	 complain_overflow_dont, /* complain_on_overflow */
1280	 ppc_elf_addr16_ha_reloc, /* special_function */
1281	 "R_PPC_EMB_NADDR16_HA", /* name */
1282	 FALSE,			/* partial_inplace */
1283	 0,			/* src_mask */
1284	 0xffff,		/* dst_mask */
1285	 FALSE),		/* pcrel_offset */
1286
1287  /* 16 bit value resulting from allocating a 4 byte word to hold an
1288     address in the .sdata section, and returning the offset from
1289     _SDA_BASE_ for that relocation.  */
1290  HOWTO (R_PPC_EMB_SDAI16,	/* type */
1291	 0,			/* rightshift */
1292	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1293	 16,			/* bitsize */
1294	 FALSE,			/* pc_relative */
1295	 0,			/* bitpos */
1296	 complain_overflow_bitfield, /* complain_on_overflow */
1297	 bfd_elf_generic_reloc,	/* special_function */
1298	 "R_PPC_EMB_SDAI16",	/* name */
1299	 FALSE,			/* partial_inplace */
1300	 0,			/* src_mask */
1301	 0xffff,		/* dst_mask */
1302	 FALSE),		/* pcrel_offset */
1303
1304  /* 16 bit value resulting from allocating a 4 byte word to hold an
1305     address in the .sdata2 section, and returning the offset from
1306     _SDA2_BASE_ for that relocation.  */
1307  HOWTO (R_PPC_EMB_SDA2I16,	/* type */
1308	 0,			/* rightshift */
1309	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1310	 16,			/* bitsize */
1311	 FALSE,			/* pc_relative */
1312	 0,			/* bitpos */
1313	 complain_overflow_bitfield, /* complain_on_overflow */
1314	 bfd_elf_generic_reloc,	/* special_function */
1315	 "R_PPC_EMB_SDA2I16",	/* name */
1316	 FALSE,			/* partial_inplace */
1317	 0,			/* src_mask */
1318	 0xffff,		/* dst_mask */
1319	 FALSE),		/* pcrel_offset */
1320
1321  /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1322     small data items.	 */
1323  HOWTO (R_PPC_EMB_SDA2REL,	/* type */
1324	 0,			/* rightshift */
1325	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1326	 16,			/* bitsize */
1327	 FALSE,			/* pc_relative */
1328	 0,			/* bitpos */
1329	 complain_overflow_signed, /* complain_on_overflow */
1330	 bfd_elf_generic_reloc,	/* special_function */
1331	 "R_PPC_EMB_SDA2REL",	/* name */
1332	 FALSE,			/* partial_inplace */
1333	 0,			/* src_mask */
1334	 0xffff,		/* dst_mask */
1335	 FALSE),		/* pcrel_offset */
1336
1337  /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1338     signed offset from the appropriate base, and filling in the register
1339     field with the appropriate register (0, 2, or 13).  */
1340  HOWTO (R_PPC_EMB_SDA21,	/* type */
1341	 0,			/* rightshift */
1342	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1343	 16,			/* bitsize */
1344	 FALSE,			/* pc_relative */
1345	 0,			/* bitpos */
1346	 complain_overflow_signed, /* complain_on_overflow */
1347	 bfd_elf_generic_reloc,	/* special_function */
1348	 "R_PPC_EMB_SDA21",	/* name */
1349	 FALSE,			/* partial_inplace */
1350	 0,			/* src_mask */
1351	 0xffff,		/* dst_mask */
1352	 FALSE),		/* pcrel_offset */
1353
1354  /* Relocation not handled: R_PPC_EMB_MRKREF */
1355  /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1356  /* Relocation not handled: R_PPC_EMB_RELST_LO */
1357  /* Relocation not handled: R_PPC_EMB_RELST_HI */
1358  /* Relocation not handled: R_PPC_EMB_RELST_HA */
1359  /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1360
1361  /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1362     in the 16 bit signed offset from the appropriate base, and filling in the
1363     register field with the appropriate register (0, 2, or 13).  */
1364  HOWTO (R_PPC_EMB_RELSDA,	/* type */
1365	 0,			/* rightshift */
1366	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1367	 16,			/* bitsize */
1368	 TRUE,			/* pc_relative */
1369	 0,			/* bitpos */
1370	 complain_overflow_signed, /* complain_on_overflow */
1371	 bfd_elf_generic_reloc,	/* special_function */
1372	 "R_PPC_EMB_RELSDA",	/* name */
1373	 FALSE,			/* partial_inplace */
1374	 0,			/* src_mask */
1375	 0xffff,		/* dst_mask */
1376	 FALSE),		/* pcrel_offset */
1377
1378  /* A 16 bit relative relocation.  */
1379  HOWTO (R_PPC_REL16,		/* type */
1380	 0,			/* rightshift */
1381	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1382	 16,			/* bitsize */
1383	 TRUE,			/* pc_relative */
1384	 0,			/* bitpos */
1385	 complain_overflow_bitfield, /* complain_on_overflow */
1386	 bfd_elf_generic_reloc,	/* special_function */
1387	 "R_PPC_REL16",		/* name */
1388	 FALSE,			/* partial_inplace */
1389	 0,			/* src_mask */
1390	 0xffff,		/* dst_mask */
1391	 TRUE),			/* pcrel_offset */
1392
1393  /* A 16 bit relative relocation without overflow.  */
1394  HOWTO (R_PPC_REL16_LO,	/* type */
1395	 0,			/* rightshift */
1396	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1397	 16,			/* bitsize */
1398	 TRUE,			/* pc_relative */
1399	 0,			/* bitpos */
1400	 complain_overflow_dont,/* complain_on_overflow */
1401	 bfd_elf_generic_reloc,	/* special_function */
1402	 "R_PPC_REL16_LO",	/* name */
1403	 FALSE,			/* partial_inplace */
1404	 0,			/* src_mask */
1405	 0xffff,		/* dst_mask */
1406	 TRUE),			/* pcrel_offset */
1407
1408  /* The high order 16 bits of a relative address.  */
1409  HOWTO (R_PPC_REL16_HI,	/* type */
1410	 16,			/* rightshift */
1411	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1412	 16,			/* bitsize */
1413	 TRUE,			/* pc_relative */
1414	 0,			/* bitpos */
1415	 complain_overflow_dont, /* complain_on_overflow */
1416	 bfd_elf_generic_reloc,	/* special_function */
1417	 "R_PPC_REL16_HI",	/* name */
1418	 FALSE,			/* partial_inplace */
1419	 0,			/* src_mask */
1420	 0xffff,		/* dst_mask */
1421	 TRUE),			/* pcrel_offset */
1422
1423  /* The high order 16 bits of a relative address, plus 1 if the contents of
1424     the low 16 bits, treated as a signed number, is negative.  */
1425  HOWTO (R_PPC_REL16_HA,	/* type */
1426	 16,			/* rightshift */
1427	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1428	 16,			/* bitsize */
1429	 TRUE,			/* pc_relative */
1430	 0,			/* bitpos */
1431	 complain_overflow_dont, /* complain_on_overflow */
1432	 ppc_elf_addr16_ha_reloc, /* special_function */
1433	 "R_PPC_REL16_HA",	/* name */
1434	 FALSE,			/* partial_inplace */
1435	 0,			/* src_mask */
1436	 0xffff,		/* dst_mask */
1437	 TRUE),			/* pcrel_offset */
1438
1439  /* GNU extension to record C++ vtable hierarchy.  */
1440  HOWTO (R_PPC_GNU_VTINHERIT,	/* type */
1441	 0,			/* rightshift */
1442	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1443	 0,			/* bitsize */
1444	 FALSE,			/* pc_relative */
1445	 0,			/* bitpos */
1446	 complain_overflow_dont, /* complain_on_overflow */
1447	 NULL,			/* special_function */
1448	 "R_PPC_GNU_VTINHERIT",	/* name */
1449	 FALSE,			/* partial_inplace */
1450	 0,			/* src_mask */
1451	 0,			/* dst_mask */
1452	 FALSE),		/* pcrel_offset */
1453
1454  /* GNU extension to record C++ vtable member usage.  */
1455  HOWTO (R_PPC_GNU_VTENTRY,	/* type */
1456	 0,			/* rightshift */
1457	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1458	 0,			/* bitsize */
1459	 FALSE,			/* pc_relative */
1460	 0,			/* bitpos */
1461	 complain_overflow_dont, /* complain_on_overflow */
1462	 NULL,			/* special_function */
1463	 "R_PPC_GNU_VTENTRY",	/* name */
1464	 FALSE,			/* partial_inplace */
1465	 0,			/* src_mask */
1466	 0,			/* dst_mask */
1467	 FALSE),		/* pcrel_offset */
1468
1469  /* Phony reloc to handle AIX style TOC entries.  */
1470  HOWTO (R_PPC_TOC16,		/* type */
1471	 0,			/* rightshift */
1472	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1473	 16,			/* bitsize */
1474	 FALSE,			/* pc_relative */
1475	 0,			/* bitpos */
1476	 complain_overflow_signed, /* complain_on_overflow */
1477	 bfd_elf_generic_reloc,	/* special_function */
1478	 "R_PPC_TOC16",		/* name */
1479	 FALSE,			/* partial_inplace */
1480	 0,			/* src_mask */
1481	 0xffff,		/* dst_mask */
1482	 FALSE),		/* pcrel_offset */
1483};
1484
1485/* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1486
1487static void
1488ppc_elf_howto_init (void)
1489{
1490  unsigned int i, type;
1491
1492  for (i = 0;
1493       i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1494       i++)
1495    {
1496      type = ppc_elf_howto_raw[i].type;
1497      if (type >= (sizeof (ppc_elf_howto_table)
1498		   / sizeof (ppc_elf_howto_table[0])))
1499	abort ();
1500      ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1501    }
1502}
1503
1504static reloc_howto_type *
1505ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1506			   bfd_reloc_code_real_type code)
1507{
1508  enum elf_ppc_reloc_type r;
1509
1510  /* Initialize howto table if not already done.  */
1511  if (!ppc_elf_howto_table[R_PPC_ADDR32])
1512    ppc_elf_howto_init ();
1513
1514  switch (code)
1515    {
1516    default:
1517      return NULL;
1518
1519    case BFD_RELOC_NONE:		r = R_PPC_NONE;			break;
1520    case BFD_RELOC_32:			r = R_PPC_ADDR32;		break;
1521    case BFD_RELOC_PPC_BA26:		r = R_PPC_ADDR24;		break;
1522    case BFD_RELOC_16:			r = R_PPC_ADDR16;		break;
1523    case BFD_RELOC_LO16:		r = R_PPC_ADDR16_LO;		break;
1524    case BFD_RELOC_HI16:		r = R_PPC_ADDR16_HI;		break;
1525    case BFD_RELOC_HI16_S:		r = R_PPC_ADDR16_HA;		break;
1526    case BFD_RELOC_PPC_BA16:		r = R_PPC_ADDR14;		break;
1527    case BFD_RELOC_PPC_BA16_BRTAKEN:	r = R_PPC_ADDR14_BRTAKEN;	break;
1528    case BFD_RELOC_PPC_BA16_BRNTAKEN:	r = R_PPC_ADDR14_BRNTAKEN;	break;
1529    case BFD_RELOC_PPC_B26:		r = R_PPC_REL24;		break;
1530    case BFD_RELOC_PPC_B16:		r = R_PPC_REL14;		break;
1531    case BFD_RELOC_PPC_B16_BRTAKEN:	r = R_PPC_REL14_BRTAKEN;	break;
1532    case BFD_RELOC_PPC_B16_BRNTAKEN:	r = R_PPC_REL14_BRNTAKEN;	break;
1533    case BFD_RELOC_16_GOTOFF:		r = R_PPC_GOT16;		break;
1534    case BFD_RELOC_LO16_GOTOFF:		r = R_PPC_GOT16_LO;		break;
1535    case BFD_RELOC_HI16_GOTOFF:		r = R_PPC_GOT16_HI;		break;
1536    case BFD_RELOC_HI16_S_GOTOFF:	r = R_PPC_GOT16_HA;		break;
1537    case BFD_RELOC_24_PLT_PCREL:	r = R_PPC_PLTREL24;		break;
1538    case BFD_RELOC_PPC_COPY:		r = R_PPC_COPY;			break;
1539    case BFD_RELOC_PPC_GLOB_DAT:	r = R_PPC_GLOB_DAT;		break;
1540    case BFD_RELOC_PPC_LOCAL24PC:	r = R_PPC_LOCAL24PC;		break;
1541    case BFD_RELOC_32_PCREL:		r = R_PPC_REL32;		break;
1542    case BFD_RELOC_32_PLTOFF:		r = R_PPC_PLT32;		break;
1543    case BFD_RELOC_32_PLT_PCREL:	r = R_PPC_PLTREL32;		break;
1544    case BFD_RELOC_LO16_PLTOFF:		r = R_PPC_PLT16_LO;		break;
1545    case BFD_RELOC_HI16_PLTOFF:		r = R_PPC_PLT16_HI;		break;
1546    case BFD_RELOC_HI16_S_PLTOFF:	r = R_PPC_PLT16_HA;		break;
1547    case BFD_RELOC_GPREL16:		r = R_PPC_SDAREL16;		break;
1548    case BFD_RELOC_16_BASEREL:		r = R_PPC_SECTOFF;		break;
1549    case BFD_RELOC_LO16_BASEREL:	r = R_PPC_SECTOFF_LO;		break;
1550    case BFD_RELOC_HI16_BASEREL:	r = R_PPC_SECTOFF_HI;		break;
1551    case BFD_RELOC_HI16_S_BASEREL:	r = R_PPC_SECTOFF_HA;		break;
1552    case BFD_RELOC_CTOR:		r = R_PPC_ADDR32;		break;
1553    case BFD_RELOC_PPC_TOC16:		r = R_PPC_TOC16;		break;
1554    case BFD_RELOC_PPC_TLS:		r = R_PPC_TLS;			break;
1555    case BFD_RELOC_PPC_TLSGD:		r = R_PPC_TLSGD;		break;
1556    case BFD_RELOC_PPC_TLSLD:		r = R_PPC_TLSLD;		break;
1557    case BFD_RELOC_PPC_DTPMOD:		r = R_PPC_DTPMOD32;		break;
1558    case BFD_RELOC_PPC_TPREL16:		r = R_PPC_TPREL16;		break;
1559    case BFD_RELOC_PPC_TPREL16_LO:	r = R_PPC_TPREL16_LO;		break;
1560    case BFD_RELOC_PPC_TPREL16_HI:	r = R_PPC_TPREL16_HI;		break;
1561    case BFD_RELOC_PPC_TPREL16_HA:	r = R_PPC_TPREL16_HA;		break;
1562    case BFD_RELOC_PPC_TPREL:		r = R_PPC_TPREL32;		break;
1563    case BFD_RELOC_PPC_DTPREL16:	r = R_PPC_DTPREL16;		break;
1564    case BFD_RELOC_PPC_DTPREL16_LO:	r = R_PPC_DTPREL16_LO;		break;
1565    case BFD_RELOC_PPC_DTPREL16_HI:	r = R_PPC_DTPREL16_HI;		break;
1566    case BFD_RELOC_PPC_DTPREL16_HA:	r = R_PPC_DTPREL16_HA;		break;
1567    case BFD_RELOC_PPC_DTPREL:		r = R_PPC_DTPREL32;		break;
1568    case BFD_RELOC_PPC_GOT_TLSGD16:	r = R_PPC_GOT_TLSGD16;		break;
1569    case BFD_RELOC_PPC_GOT_TLSGD16_LO:	r = R_PPC_GOT_TLSGD16_LO;	break;
1570    case BFD_RELOC_PPC_GOT_TLSGD16_HI:	r = R_PPC_GOT_TLSGD16_HI;	break;
1571    case BFD_RELOC_PPC_GOT_TLSGD16_HA:	r = R_PPC_GOT_TLSGD16_HA;	break;
1572    case BFD_RELOC_PPC_GOT_TLSLD16:	r = R_PPC_GOT_TLSLD16;		break;
1573    case BFD_RELOC_PPC_GOT_TLSLD16_LO:	r = R_PPC_GOT_TLSLD16_LO;	break;
1574    case BFD_RELOC_PPC_GOT_TLSLD16_HI:	r = R_PPC_GOT_TLSLD16_HI;	break;
1575    case BFD_RELOC_PPC_GOT_TLSLD16_HA:	r = R_PPC_GOT_TLSLD16_HA;	break;
1576    case BFD_RELOC_PPC_GOT_TPREL16:	r = R_PPC_GOT_TPREL16;		break;
1577    case BFD_RELOC_PPC_GOT_TPREL16_LO:	r = R_PPC_GOT_TPREL16_LO;	break;
1578    case BFD_RELOC_PPC_GOT_TPREL16_HI:	r = R_PPC_GOT_TPREL16_HI;	break;
1579    case BFD_RELOC_PPC_GOT_TPREL16_HA:	r = R_PPC_GOT_TPREL16_HA;	break;
1580    case BFD_RELOC_PPC_GOT_DTPREL16:	r = R_PPC_GOT_DTPREL16;		break;
1581    case BFD_RELOC_PPC_GOT_DTPREL16_LO:	r = R_PPC_GOT_DTPREL16_LO;	break;
1582    case BFD_RELOC_PPC_GOT_DTPREL16_HI:	r = R_PPC_GOT_DTPREL16_HI;	break;
1583    case BFD_RELOC_PPC_GOT_DTPREL16_HA:	r = R_PPC_GOT_DTPREL16_HA;	break;
1584    case BFD_RELOC_PPC_EMB_NADDR32:	r = R_PPC_EMB_NADDR32;		break;
1585    case BFD_RELOC_PPC_EMB_NADDR16:	r = R_PPC_EMB_NADDR16;		break;
1586    case BFD_RELOC_PPC_EMB_NADDR16_LO:	r = R_PPC_EMB_NADDR16_LO;	break;
1587    case BFD_RELOC_PPC_EMB_NADDR16_HI:	r = R_PPC_EMB_NADDR16_HI;	break;
1588    case BFD_RELOC_PPC_EMB_NADDR16_HA:	r = R_PPC_EMB_NADDR16_HA;	break;
1589    case BFD_RELOC_PPC_EMB_SDAI16:	r = R_PPC_EMB_SDAI16;		break;
1590    case BFD_RELOC_PPC_EMB_SDA2I16:	r = R_PPC_EMB_SDA2I16;		break;
1591    case BFD_RELOC_PPC_EMB_SDA2REL:	r = R_PPC_EMB_SDA2REL;		break;
1592    case BFD_RELOC_PPC_EMB_SDA21:	r = R_PPC_EMB_SDA21;		break;
1593    case BFD_RELOC_PPC_EMB_MRKREF:	r = R_PPC_EMB_MRKREF;		break;
1594    case BFD_RELOC_PPC_EMB_RELSEC16:	r = R_PPC_EMB_RELSEC16;		break;
1595    case BFD_RELOC_PPC_EMB_RELST_LO:	r = R_PPC_EMB_RELST_LO;		break;
1596    case BFD_RELOC_PPC_EMB_RELST_HI:	r = R_PPC_EMB_RELST_HI;		break;
1597    case BFD_RELOC_PPC_EMB_RELST_HA:	r = R_PPC_EMB_RELST_HA;		break;
1598    case BFD_RELOC_PPC_EMB_BIT_FLD:	r = R_PPC_EMB_BIT_FLD;		break;
1599    case BFD_RELOC_PPC_EMB_RELSDA:	r = R_PPC_EMB_RELSDA;		break;
1600    case BFD_RELOC_16_PCREL:		r = R_PPC_REL16;		break;
1601    case BFD_RELOC_LO16_PCREL:		r = R_PPC_REL16_LO;		break;
1602    case BFD_RELOC_HI16_PCREL:		r = R_PPC_REL16_HI;		break;
1603    case BFD_RELOC_HI16_S_PCREL:	r = R_PPC_REL16_HA;		break;
1604    case BFD_RELOC_VTABLE_INHERIT:	r = R_PPC_GNU_VTINHERIT;	break;
1605    case BFD_RELOC_VTABLE_ENTRY:	r = R_PPC_GNU_VTENTRY;		break;
1606    }
1607
1608  return ppc_elf_howto_table[r];
1609};
1610
1611static reloc_howto_type *
1612ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1613			   const char *r_name)
1614{
1615  unsigned int i;
1616
1617  for (i = 0;
1618       i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1619       i++)
1620    if (ppc_elf_howto_raw[i].name != NULL
1621	&& strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
1622      return &ppc_elf_howto_raw[i];
1623
1624  return NULL;
1625}
1626
1627/* Set the howto pointer for a PowerPC ELF reloc.  */
1628
1629static void
1630ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1631		       arelent *cache_ptr,
1632		       Elf_Internal_Rela *dst)
1633{
1634  /* Initialize howto table if not already done.  */
1635  if (!ppc_elf_howto_table[R_PPC_ADDR32])
1636    ppc_elf_howto_init ();
1637
1638  BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1639  cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1640
1641  /* Just because the above assert didn't trigger doesn't mean that
1642     ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation.  */
1643  if (!cache_ptr->howto)
1644    {
1645      (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
1646                             abfd, ELF32_R_TYPE (dst->r_info));
1647      bfd_set_error (bfd_error_bad_value);
1648
1649      cache_ptr->howto = ppc_elf_howto_table[R_PPC_NONE];
1650    }
1651}
1652
1653/* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
1654
1655static bfd_reloc_status_type
1656ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1657			 arelent *reloc_entry,
1658			 asymbol *symbol,
1659			 void *data ATTRIBUTE_UNUSED,
1660			 asection *input_section,
1661			 bfd *output_bfd,
1662			 char **error_message ATTRIBUTE_UNUSED)
1663{
1664  bfd_vma relocation;
1665
1666  if (output_bfd != NULL)
1667    {
1668      reloc_entry->address += input_section->output_offset;
1669      return bfd_reloc_ok;
1670    }
1671
1672  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1673    return bfd_reloc_outofrange;
1674
1675  if (bfd_is_com_section (symbol->section))
1676    relocation = 0;
1677  else
1678    relocation = symbol->value;
1679
1680  relocation += symbol->section->output_section->vma;
1681  relocation += symbol->section->output_offset;
1682  relocation += reloc_entry->addend;
1683  if (reloc_entry->howto->pc_relative)
1684    relocation -= reloc_entry->address;
1685
1686  reloc_entry->addend += (relocation & 0x8000) << 1;
1687
1688  return bfd_reloc_continue;
1689}
1690
1691static bfd_reloc_status_type
1692ppc_elf_unhandled_reloc (bfd *abfd,
1693			 arelent *reloc_entry,
1694			 asymbol *symbol,
1695			 void *data,
1696			 asection *input_section,
1697			 bfd *output_bfd,
1698			 char **error_message)
1699{
1700  /* If this is a relocatable link (output_bfd test tells us), just
1701     call the generic function.  Any adjustment will be done at final
1702     link time.  */
1703  if (output_bfd != NULL)
1704    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1705				  input_section, output_bfd, error_message);
1706
1707  if (error_message != NULL)
1708    {
1709      static char buf[60];
1710      sprintf (buf, _("generic linker can't handle %s"),
1711	       reloc_entry->howto->name);
1712      *error_message = buf;
1713    }
1714  return bfd_reloc_dangerous;
1715}
1716
1717/* Sections created by the linker.  */
1718
1719typedef struct elf_linker_section
1720{
1721  /* Pointer to the bfd section.  */
1722  asection *section;
1723  /* Section name.  */
1724  const char *name;
1725  /* Associated bss section name.  */
1726  const char *bss_name;
1727  /* Associated symbol name.  */
1728  const char *sym_name;
1729  /* Associated symbol.  */
1730  struct elf_link_hash_entry *sym;
1731} elf_linker_section_t;
1732
1733/* Linked list of allocated pointer entries.  This hangs off of the
1734   symbol lists, and provides allows us to return different pointers,
1735   based on different addend's.  */
1736
1737typedef struct elf_linker_section_pointers
1738{
1739  /* next allocated pointer for this symbol */
1740  struct elf_linker_section_pointers *next;
1741  /* offset of pointer from beginning of section */
1742  bfd_vma offset;
1743  /* addend used */
1744  bfd_vma addend;
1745  /* which linker section this is */
1746  elf_linker_section_t *lsect;
1747} elf_linker_section_pointers_t;
1748
1749struct ppc_elf_obj_tdata
1750{
1751  struct elf_obj_tdata elf;
1752
1753  /* A mapping from local symbols to offsets into the various linker
1754     sections added.  This is index by the symbol index.  */
1755  elf_linker_section_pointers_t **linker_section_pointers;
1756
1757  /* Flags used to auto-detect plt type.  */
1758  unsigned int makes_plt_call : 1;
1759  unsigned int has_rel16 : 1;
1760};
1761
1762#define ppc_elf_tdata(bfd) \
1763  ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1764
1765#define elf_local_ptr_offsets(bfd) \
1766  (ppc_elf_tdata (bfd)->linker_section_pointers)
1767
1768/* Override the generic function because we store some extras.  */
1769
1770static bfd_boolean
1771ppc_elf_mkobject (bfd *abfd)
1772{
1773  if (abfd->tdata.any == NULL)
1774    {
1775      bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1776      abfd->tdata.any = bfd_zalloc (abfd, amt);
1777      if (abfd->tdata.any == NULL)
1778	return FALSE;
1779    }
1780  return bfd_elf_mkobject (abfd);
1781}
1782
1783/* Fix bad default arch selected for a 32 bit input bfd when the
1784   default is 64 bit.  */
1785
1786static bfd_boolean
1787ppc_elf_object_p (bfd *abfd)
1788{
1789  if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1790    {
1791      Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1792
1793      if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1794	{
1795	  /* Relies on arch after 64 bit default being 32 bit default.  */
1796	  abfd->arch_info = abfd->arch_info->next;
1797	  BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1798	}
1799    }
1800  return TRUE;
1801}
1802
1803/* Function to set whether a module needs the -mrelocatable bit set.  */
1804
1805static bfd_boolean
1806ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1807{
1808  BFD_ASSERT (!elf_flags_init (abfd)
1809	      || elf_elfheader (abfd)->e_flags == flags);
1810
1811  elf_elfheader (abfd)->e_flags = flags;
1812  elf_flags_init (abfd) = TRUE;
1813  return TRUE;
1814}
1815
1816/* Support for core dump NOTE sections.  */
1817
1818static bfd_boolean
1819ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1820{
1821  int offset;
1822  unsigned int size;
1823
1824  switch (note->descsz)
1825    {
1826    default:
1827      return FALSE;
1828
1829    case 268:		/* Linux/PPC.  */
1830      /* pr_cursig */
1831      elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1832
1833      /* pr_pid */
1834      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1835
1836      /* pr_reg */
1837      offset = 72;
1838      size = 192;
1839
1840      break;
1841    }
1842
1843  /* Make a ".reg/999" section.  */
1844  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1845					  size, note->descpos + offset);
1846}
1847
1848static bfd_boolean
1849ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1850{
1851  switch (note->descsz)
1852    {
1853    default:
1854      return FALSE;
1855
1856    case 128:		/* Linux/PPC elf_prpsinfo.  */
1857      elf_tdata (abfd)->core_program
1858	= _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1859      elf_tdata (abfd)->core_command
1860	= _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1861    }
1862
1863  /* Note that for some reason, a spurious space is tacked
1864     onto the end of the args in some (at least one anyway)
1865     implementations, so strip it off if it exists.  */
1866
1867  {
1868    char *command = elf_tdata (abfd)->core_command;
1869    int n = strlen (command);
1870
1871    if (0 < n && command[n - 1] == ' ')
1872      command[n - 1] = '\0';
1873  }
1874
1875  return TRUE;
1876}
1877
1878static char *
1879ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
1880{
1881  switch (note_type)
1882    {
1883    default:
1884      return NULL;
1885
1886    case NT_PRPSINFO:
1887      {
1888	char data[128];
1889	va_list ap;
1890
1891	va_start (ap, note_type);
1892	memset (data, 0, 32);
1893	strncpy (data + 32, va_arg (ap, const char *), 16);
1894	strncpy (data + 48, va_arg (ap, const char *), 80);
1895	va_end (ap);
1896	return elfcore_write_note (abfd, buf, bufsiz,
1897				   "CORE", note_type, data, sizeof (data));
1898      }
1899
1900    case NT_PRSTATUS:
1901      {
1902	char data[268];
1903	va_list ap;
1904	long pid;
1905	int cursig;
1906	const void *greg;
1907
1908	va_start (ap, note_type);
1909	memset (data, 0, 72);
1910	pid = va_arg (ap, long);
1911	bfd_put_32 (abfd, pid, data + 24);
1912	cursig = va_arg (ap, int);
1913	bfd_put_16 (abfd, cursig, data + 12);
1914	greg = va_arg (ap, const void *);
1915	memcpy (data + 72, greg, 192);
1916	memset (data + 264, 0, 4);
1917	va_end (ap);
1918	return elfcore_write_note (abfd, buf, bufsiz,
1919				   "CORE", note_type, data, sizeof (data));
1920      }
1921    }
1922}
1923
1924/* Return address for Ith PLT stub in section PLT, for relocation REL
1925   or (bfd_vma) -1 if it should not be included.  */
1926
1927static bfd_vma
1928ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1929		     const asection *plt ATTRIBUTE_UNUSED,
1930		     const arelent *rel)
1931{
1932  return rel->address;
1933}
1934
1935/* Handle a PowerPC specific section when reading an object file.  This
1936   is called when bfd_section_from_shdr finds a section with an unknown
1937   type.  */
1938
1939static bfd_boolean
1940ppc_elf_section_from_shdr (bfd *abfd,
1941			   Elf_Internal_Shdr *hdr,
1942			   const char *name,
1943			   int shindex)
1944{
1945  asection *newsect;
1946  flagword flags;
1947
1948  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1949    return FALSE;
1950
1951  newsect = hdr->bfd_section;
1952  flags = bfd_get_section_flags (abfd, newsect);
1953  if (hdr->sh_flags & SHF_EXCLUDE)
1954    flags |= SEC_EXCLUDE;
1955
1956  if (hdr->sh_type == SHT_ORDERED)
1957    flags |= SEC_SORT_ENTRIES;
1958
1959  bfd_set_section_flags (abfd, newsect, flags);
1960  return TRUE;
1961}
1962
1963/* Set up any other section flags and such that may be necessary.  */
1964
1965static bfd_boolean
1966ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1967		       Elf_Internal_Shdr *shdr,
1968		       asection *asect)
1969{
1970  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
1971    shdr->sh_flags |= SHF_EXCLUDE;
1972
1973  if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1974    shdr->sh_type = SHT_ORDERED;
1975
1976  return TRUE;
1977}
1978
1979/* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1980   need to bump up the number of section headers.  */
1981
1982static int
1983ppc_elf_additional_program_headers (bfd *abfd,
1984				    struct bfd_link_info *info ATTRIBUTE_UNUSED)
1985{
1986  asection *s;
1987  int ret = 0;
1988
1989  s = bfd_get_section_by_name (abfd, ".sbss2");
1990  if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1991    ++ret;
1992
1993  s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1994  if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1995    ++ret;
1996
1997  return ret;
1998}
1999
2000/* Add extra PPC sections -- Note, for now, make .sbss2 and
2001   .PPC.EMB.sbss0 a normal section, and not a bss section so
2002   that the linker doesn't crater when trying to make more than
2003   2 sections.  */
2004
2005static const struct bfd_elf_special_section ppc_elf_special_sections[] =
2006{
2007  { STRING_COMMA_LEN (".plt"),             0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
2008  { STRING_COMMA_LEN (".sbss"),           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2009  { STRING_COMMA_LEN (".sbss2"),          -2, SHT_PROGBITS, SHF_ALLOC },
2010  { STRING_COMMA_LEN (".sdata"),          -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2011  { STRING_COMMA_LEN (".sdata2"),         -2, SHT_PROGBITS, SHF_ALLOC },
2012  { STRING_COMMA_LEN (".tags"),            0, SHT_ORDERED,  SHF_ALLOC },
2013  { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE,     0 },
2014  { STRING_COMMA_LEN (".PPC.EMB.sbss0"),   0, SHT_PROGBITS, SHF_ALLOC },
2015  { STRING_COMMA_LEN (".PPC.EMB.sdata0"),  0, SHT_PROGBITS, SHF_ALLOC },
2016  { NULL,                              0,  0, 0,            0 }
2017};
2018
2019/* This is what we want for new plt/got.  */
2020static struct bfd_elf_special_section ppc_alt_plt =
2021  { STRING_COMMA_LEN (".plt"),             0, SHT_PROGBITS, SHF_ALLOC };
2022
2023static const struct bfd_elf_special_section *
2024ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
2025{
2026  const struct bfd_elf_special_section *ssect;
2027
2028  /* See if this is one of the special sections.  */
2029  if (sec->name == NULL)
2030    return NULL;
2031
2032  ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2033					sec->use_rela_p);
2034  if (ssect != NULL)
2035    {
2036      if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2037	ssect = &ppc_alt_plt;
2038      return ssect;
2039    }
2040
2041  return _bfd_elf_get_sec_type_attr (abfd, sec);
2042}
2043
2044/* Very simple linked list structure for recording apuinfo values.  */
2045typedef struct apuinfo_list
2046{
2047  struct apuinfo_list *next;
2048  unsigned long value;
2049}
2050apuinfo_list;
2051
2052static apuinfo_list *head;
2053
2054
2055static void
2056apuinfo_list_init (void)
2057{
2058  head = NULL;
2059}
2060
2061static void
2062apuinfo_list_add (unsigned long value)
2063{
2064  apuinfo_list *entry = head;
2065
2066  while (entry != NULL)
2067    {
2068      if (entry->value == value)
2069	return;
2070      entry = entry->next;
2071    }
2072
2073  entry = bfd_malloc (sizeof (* entry));
2074  if (entry == NULL)
2075    return;
2076
2077  entry->value = value;
2078  entry->next  = head;
2079  head = entry;
2080}
2081
2082static unsigned
2083apuinfo_list_length (void)
2084{
2085  apuinfo_list *entry;
2086  unsigned long count;
2087
2088  for (entry = head, count = 0;
2089       entry;
2090       entry = entry->next)
2091    ++ count;
2092
2093  return count;
2094}
2095
2096static inline unsigned long
2097apuinfo_list_element (unsigned long number)
2098{
2099  apuinfo_list * entry;
2100
2101  for (entry = head;
2102       entry && number --;
2103       entry = entry->next)
2104    ;
2105
2106  return entry ? entry->value : 0;
2107}
2108
2109static void
2110apuinfo_list_finish (void)
2111{
2112  apuinfo_list *entry;
2113
2114  for (entry = head; entry;)
2115    {
2116      apuinfo_list *next = entry->next;
2117      free (entry);
2118      entry = next;
2119    }
2120
2121  head = NULL;
2122}
2123
2124#define APUINFO_SECTION_NAME	".PPC.EMB.apuinfo"
2125#define APUINFO_LABEL		"APUinfo"
2126
2127/* Scan the input BFDs and create a linked list of
2128   the APUinfo values that will need to be emitted.  */
2129
2130static void
2131ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2132{
2133  bfd *ibfd;
2134  asection *asec;
2135  char *buffer;
2136  unsigned num_input_sections;
2137  bfd_size_type	output_section_size;
2138  unsigned i;
2139  unsigned num_entries;
2140  unsigned long	offset;
2141  unsigned long length;
2142  const char *error_message = NULL;
2143
2144  if (link_info == NULL)
2145    return;
2146
2147  /* Scan the input bfds, looking for apuinfo sections.  */
2148  num_input_sections = 0;
2149  output_section_size = 0;
2150
2151  for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2152    {
2153      asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2154      if (asec)
2155	{
2156	  ++ num_input_sections;
2157	  output_section_size += asec->size;
2158	}
2159    }
2160
2161  /* We need at least one input sections
2162     in order to make merging worthwhile.  */
2163  if (num_input_sections < 1)
2164    return;
2165
2166  /* Just make sure that the output section exists as well.  */
2167  asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2168  if (asec == NULL)
2169    return;
2170
2171  /* Allocate a buffer for the contents of the input sections.  */
2172  buffer = bfd_malloc (output_section_size);
2173  if (buffer == NULL)
2174    return;
2175
2176  offset = 0;
2177  apuinfo_list_init ();
2178
2179  /* Read in the input sections contents.  */
2180  for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2181    {
2182      unsigned long datum;
2183      char *ptr;
2184
2185      asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2186      if (asec == NULL)
2187	continue;
2188
2189      length = asec->size;
2190      if (length < 24)
2191	{
2192	  error_message = _("corrupt or empty %s section in %B");
2193	  goto fail;
2194	}
2195
2196      if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2197	  || (bfd_bread (buffer + offset, length, ibfd) != length))
2198	{
2199	  error_message = _("unable to read in %s section from %B");
2200	  goto fail;
2201	}
2202
2203      /* Process the contents of the section.  */
2204      ptr = buffer + offset;
2205      error_message = _("corrupt %s section in %B");
2206
2207      /* Verify the contents of the header.  Note - we have to
2208	 extract the values this way in order to allow for a
2209	 host whose endian-ness is different from the target.  */
2210      datum = bfd_get_32 (ibfd, ptr);
2211      if (datum != sizeof APUINFO_LABEL)
2212	goto fail;
2213
2214      datum = bfd_get_32 (ibfd, ptr + 8);
2215      if (datum != 0x2)
2216	goto fail;
2217
2218      if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2219	goto fail;
2220
2221      /* Get the number of bytes used for apuinfo entries.  */
2222      datum = bfd_get_32 (ibfd, ptr + 4);
2223      if (datum + 20 != length)
2224	goto fail;
2225
2226      /* Make sure that we do not run off the end of the section.  */
2227      if (offset + length > output_section_size)
2228	goto fail;
2229
2230      /* Scan the apuinfo section, building a list of apuinfo numbers.  */
2231      for (i = 0; i < datum; i += 4)
2232	apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2233
2234      /* Update the offset.  */
2235      offset += length;
2236    }
2237
2238  error_message = NULL;
2239
2240  /* Compute the size of the output section.  */
2241  num_entries = apuinfo_list_length ();
2242  output_section_size = 20 + num_entries * 4;
2243
2244  asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2245
2246  if (! bfd_set_section_size (abfd, asec, output_section_size))
2247    ibfd = abfd,
2248      error_message = _("warning: unable to set size of %s section in %B");
2249
2250 fail:
2251  free (buffer);
2252
2253  if (error_message)
2254    (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2255}
2256
2257/* Prevent the output section from accumulating the input sections'
2258   contents.  We have already stored this in our linked list structure.  */
2259
2260static bfd_boolean
2261ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2262		       struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2263		       asection *asec,
2264		       bfd_byte *contents ATTRIBUTE_UNUSED)
2265{
2266  return (apuinfo_list_length ()
2267	  && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2268}
2269
2270/* Finally we can generate the output section.  */
2271
2272static void
2273ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2274{
2275  bfd_byte *buffer;
2276  asection *asec;
2277  unsigned i;
2278  unsigned num_entries;
2279  bfd_size_type length;
2280
2281  asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2282  if (asec == NULL)
2283    return;
2284
2285  if (apuinfo_list_length () == 0)
2286    return;
2287
2288  length = asec->size;
2289  if (length < 20)
2290    return;
2291
2292  buffer = bfd_malloc (length);
2293  if (buffer == NULL)
2294    {
2295      (*_bfd_error_handler)
2296	(_("failed to allocate space for new APUinfo section."));
2297      return;
2298    }
2299
2300  /* Create the apuinfo header.  */
2301  num_entries = apuinfo_list_length ();
2302  bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2303  bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2304  bfd_put_32 (abfd, 0x2, buffer + 8);
2305  strcpy ((char *) buffer + 12, APUINFO_LABEL);
2306
2307  length = 20;
2308  for (i = 0; i < num_entries; i++)
2309    {
2310      bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2311      length += 4;
2312    }
2313
2314  if (length != asec->size)
2315    (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2316
2317  if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2318    (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2319
2320  free (buffer);
2321
2322  apuinfo_list_finish ();
2323}
2324
2325/* The following functions are specific to the ELF linker, while
2326   functions above are used generally.  They appear in this file more
2327   or less in the order in which they are called.  eg.
2328   ppc_elf_check_relocs is called early in the link process,
2329   ppc_elf_finish_dynamic_sections is one of the last functions
2330   called.  */
2331
2332/* The PPC linker needs to keep track of the number of relocs that it
2333   decides to copy as dynamic relocs in check_relocs for each symbol.
2334   This is so that it can later discard them if they are found to be
2335   unnecessary.  We store the information in a field extending the
2336   regular ELF linker hash table.  */
2337
2338struct ppc_elf_dyn_relocs
2339{
2340  struct ppc_elf_dyn_relocs *next;
2341
2342  /* The input section of the reloc.  */
2343  asection *sec;
2344
2345  /* Total number of relocs copied for the input section.  */
2346  bfd_size_type count;
2347
2348  /* Number of pc-relative relocs copied for the input section.  */
2349  bfd_size_type pc_count;
2350};
2351
2352/* Track PLT entries needed for a given symbol.  We might need more
2353   than one glink entry per symbol.  */
2354struct plt_entry
2355{
2356  struct plt_entry *next;
2357
2358  /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2359     This field stores the offset into .got2 used to initialise the
2360     GOT pointer reg.  It will always be at least 32768 (and for
2361     current gcc this is the only offset used).  */
2362  bfd_vma addend;
2363
2364  /* The .got2 section.  */
2365  asection *sec;
2366
2367  /* PLT refcount or offset.  */
2368  union
2369    {
2370      bfd_signed_vma refcount;
2371      bfd_vma offset;
2372    } plt;
2373
2374  /* .glink stub offset.  */
2375  bfd_vma glink_offset;
2376};
2377
2378/* Of those relocs that might be copied as dynamic relocs, this function
2379   selects those that must be copied when linking a shared library,
2380   even when the symbol is local.  */
2381
2382static int
2383must_be_dyn_reloc (struct bfd_link_info *info,
2384		   enum elf_ppc_reloc_type r_type)
2385{
2386  switch (r_type)
2387    {
2388    default:
2389      return 1;
2390
2391    case R_PPC_REL24:
2392    case R_PPC_REL14:
2393    case R_PPC_REL14_BRTAKEN:
2394    case R_PPC_REL14_BRNTAKEN:
2395    case R_PPC_REL32:
2396      return 0;
2397
2398    case R_PPC_TPREL32:
2399    case R_PPC_TPREL16:
2400    case R_PPC_TPREL16_LO:
2401    case R_PPC_TPREL16_HI:
2402    case R_PPC_TPREL16_HA:
2403      return !info->executable;
2404    }
2405}
2406
2407/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2408   copying dynamic variables from a shared lib into an app's dynbss
2409   section, and instead use a dynamic relocation to point into the
2410   shared lib.  */
2411#define ELIMINATE_COPY_RELOCS 1
2412
2413/* PPC ELF linker hash entry.  */
2414
2415struct ppc_elf_link_hash_entry
2416{
2417  struct elf_link_hash_entry elf;
2418
2419  /* If this symbol is used in the linker created sections, the processor
2420     specific backend uses this field to map the field into the offset
2421     from the beginning of the section.  */
2422  elf_linker_section_pointers_t *linker_section_pointer;
2423
2424  /* Track dynamic relocs copied for this symbol.  */
2425  struct ppc_elf_dyn_relocs *dyn_relocs;
2426
2427  /* Contexts in which symbol is used in the GOT (or TOC).
2428     TLS_GD .. TLS_TLS bits are or'd into the mask as the
2429     corresponding relocs are encountered during check_relocs.
2430     tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2431     indicate the corresponding GOT entry type is not needed.  */
2432#define TLS_GD		 1	/* GD reloc. */
2433#define TLS_LD		 2	/* LD reloc. */
2434#define TLS_TPREL	 4	/* TPREL reloc, => IE. */
2435#define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
2436#define TLS_TLS		16	/* Any TLS reloc.  */
2437#define TLS_TPRELGD	32	/* TPREL reloc resulting from GD->IE. */
2438  char tls_mask;
2439
2440  /* Nonzero if we have seen a small data relocation referring to this
2441     symbol.  */
2442  unsigned char has_sda_refs;
2443};
2444
2445#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2446
2447/* PPC ELF linker hash table.  */
2448
2449struct ppc_elf_link_hash_table
2450{
2451  struct elf_link_hash_table elf;
2452
2453  /* Short-cuts to get to dynamic linker sections.  */
2454  asection *got;
2455  asection *relgot;
2456  asection *glink;
2457  asection *plt;
2458  asection *relplt;
2459  asection *dynbss;
2460  asection *relbss;
2461  asection *dynsbss;
2462  asection *relsbss;
2463  elf_linker_section_t sdata[2];
2464  asection *sbss;
2465
2466  /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
2467  asection *srelplt2;
2468
2469  /* The .got.plt section (VxWorks only)*/
2470  asection *sgotplt;
2471
2472  /* Shortcut to __tls_get_addr.  */
2473  struct elf_link_hash_entry *tls_get_addr;
2474
2475  /* The bfd that forced an old-style PLT.  */
2476  bfd *old_bfd;
2477
2478  /* TLS local dynamic got entry handling.  */
2479  union {
2480    bfd_signed_vma refcount;
2481    bfd_vma offset;
2482  } tlsld_got;
2483
2484  /* Offset of PltResolve function in glink.  */
2485  bfd_vma glink_pltresolve;
2486
2487  /* Size of reserved GOT entries.  */
2488  unsigned int got_header_size;
2489  /* Non-zero if allocating the header left a gap.  */
2490  unsigned int got_gap;
2491
2492  /* The type of PLT we have chosen to use.  */
2493  enum ppc_elf_plt_type plt_type;
2494
2495  /* Set if we should emit symbols for stubs.  */
2496  unsigned int emit_stub_syms:1;
2497
2498  /* True if the target system is VxWorks.  */
2499  unsigned int is_vxworks:1;
2500
2501  /* The size of PLT entries.  */
2502  int plt_entry_size;
2503  /* The distance between adjacent PLT slots.  */
2504  int plt_slot_size;
2505  /* The size of the first PLT entry.  */
2506  int plt_initial_entry_size;
2507
2508  /* Small local sym to section mapping cache.  */
2509  struct sym_sec_cache sym_sec;
2510};
2511
2512/* Get the PPC ELF linker hash table from a link_info structure.  */
2513
2514#define ppc_elf_hash_table(p) \
2515  ((struct ppc_elf_link_hash_table *) (p)->hash)
2516
2517/* Create an entry in a PPC ELF linker hash table.  */
2518
2519static struct bfd_hash_entry *
2520ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2521			   struct bfd_hash_table *table,
2522			   const char *string)
2523{
2524  /* Allocate the structure if it has not already been allocated by a
2525     subclass.  */
2526  if (entry == NULL)
2527    {
2528      entry = bfd_hash_allocate (table,
2529				 sizeof (struct ppc_elf_link_hash_entry));
2530      if (entry == NULL)
2531	return entry;
2532    }
2533
2534  /* Call the allocation method of the superclass.  */
2535  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2536  if (entry != NULL)
2537    {
2538      ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2539      ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2540      ppc_elf_hash_entry (entry)->tls_mask = 0;
2541    }
2542
2543  return entry;
2544}
2545
2546/* Create a PPC ELF linker hash table.  */
2547
2548static struct bfd_link_hash_table *
2549ppc_elf_link_hash_table_create (bfd *abfd)
2550{
2551  struct ppc_elf_link_hash_table *ret;
2552
2553  ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2554  if (ret == NULL)
2555    return NULL;
2556
2557  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2558				      ppc_elf_link_hash_newfunc,
2559				      sizeof (struct ppc_elf_link_hash_entry)))
2560    {
2561      free (ret);
2562      return NULL;
2563    }
2564
2565  ret->elf.init_plt_refcount.refcount = 0;
2566  ret->elf.init_plt_refcount.glist = NULL;
2567  ret->elf.init_plt_offset.offset = 0;
2568  ret->elf.init_plt_offset.glist = NULL;
2569
2570  ret->sdata[0].name = ".sdata";
2571  ret->sdata[0].sym_name = "_SDA_BASE_";
2572  ret->sdata[0].bss_name = ".sbss";
2573
2574  ret->sdata[1].name = ".sdata2";
2575  ret->sdata[1].sym_name = "_SDA2_BASE_";
2576  ret->sdata[1].bss_name = ".sbss2";
2577
2578  ret->plt_entry_size = 12;
2579  ret->plt_slot_size = 8;
2580  ret->plt_initial_entry_size = 72;
2581
2582  return &ret->elf.root;
2583}
2584
2585/* Create .got and the related sections.  */
2586
2587static bfd_boolean
2588ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2589{
2590  struct ppc_elf_link_hash_table *htab;
2591  asection *s;
2592  flagword flags;
2593
2594  if (!_bfd_elf_create_got_section (abfd, info))
2595    return FALSE;
2596
2597  htab = ppc_elf_hash_table (info);
2598  htab->got = s = bfd_get_section_by_name (abfd, ".got");
2599  if (s == NULL)
2600    abort ();
2601
2602  if (htab->is_vxworks)
2603    {
2604      htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2605      if (!htab->sgotplt)
2606	abort ();
2607    }
2608  else
2609    {
2610      /* The powerpc .got has a blrl instruction in it.  Mark it
2611	 executable.  */
2612      flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2613	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2614      if (!bfd_set_section_flags (abfd, s, flags))
2615	return FALSE;
2616    }
2617
2618  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2619	   | SEC_LINKER_CREATED | SEC_READONLY);
2620  htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got", flags);
2621  if (!htab->relgot
2622      || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2623    return FALSE;
2624
2625  return TRUE;
2626}
2627
2628/* We have to create .dynsbss and .rela.sbss here so that they get mapped
2629   to output sections (just like _bfd_elf_create_dynamic_sections has
2630   to create .dynbss and .rela.bss).  */
2631
2632static bfd_boolean
2633ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2634{
2635  struct ppc_elf_link_hash_table *htab;
2636  asection *s;
2637  flagword flags;
2638
2639  htab = ppc_elf_hash_table (info);
2640
2641  if (htab->got == NULL
2642      && !ppc_elf_create_got (abfd, info))
2643    return FALSE;
2644
2645  if (!_bfd_elf_create_dynamic_sections (abfd, info))
2646    return FALSE;
2647
2648  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2649	   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2650
2651  s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags | SEC_CODE);
2652  htab->glink = s;
2653  if (s == NULL
2654      || !bfd_set_section_alignment (abfd, s, 4))
2655    return FALSE;
2656
2657  htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2658  s = bfd_make_section_with_flags (abfd, ".dynsbss",
2659				   SEC_ALLOC | SEC_LINKER_CREATED);
2660  htab->dynsbss = s;
2661  if (s == NULL)
2662    return FALSE;
2663
2664  if (! info->shared)
2665    {
2666      htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2667      s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2668      htab->relsbss = s;
2669      if (s == NULL
2670	  || ! bfd_set_section_alignment (abfd, s, 2))
2671	return FALSE;
2672    }
2673
2674  if (htab->is_vxworks
2675      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2676    return FALSE;
2677
2678  htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2679  htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2680  if (s == NULL)
2681    abort ();
2682
2683  flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2684  if (htab->plt_type == PLT_VXWORKS)
2685    /* The VxWorks PLT is a loaded section with contents.  */
2686    flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2687  return bfd_set_section_flags (abfd, s, flags);
2688}
2689
2690/* Copy the extra info we tack onto an elf_link_hash_entry.  */
2691
2692static void
2693ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2694			      struct elf_link_hash_entry *dir,
2695			      struct elf_link_hash_entry *ind)
2696{
2697  struct ppc_elf_link_hash_entry *edir, *eind;
2698
2699  edir = (struct ppc_elf_link_hash_entry *) dir;
2700  eind = (struct ppc_elf_link_hash_entry *) ind;
2701
2702  if (eind->dyn_relocs != NULL)
2703    {
2704      if (edir->dyn_relocs != NULL)
2705	{
2706	  struct ppc_elf_dyn_relocs **pp;
2707	  struct ppc_elf_dyn_relocs *p;
2708
2709	  /* Add reloc counts against the indirect sym to the direct sym
2710	     list.  Merge any entries against the same section.  */
2711	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2712	    {
2713	      struct ppc_elf_dyn_relocs *q;
2714
2715	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
2716		if (q->sec == p->sec)
2717		  {
2718		    q->pc_count += p->pc_count;
2719		    q->count += p->count;
2720		    *pp = p->next;
2721		    break;
2722		  }
2723	      if (q == NULL)
2724		pp = &p->next;
2725	    }
2726	  *pp = edir->dyn_relocs;
2727	}
2728
2729      edir->dyn_relocs = eind->dyn_relocs;
2730      eind->dyn_relocs = NULL;
2731    }
2732
2733  edir->tls_mask |= eind->tls_mask;
2734  edir->has_sda_refs |= eind->has_sda_refs;
2735
2736  /* If called to transfer flags for a weakdef during processing
2737     of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2738     We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
2739  if (!(ELIMINATE_COPY_RELOCS
2740	&& eind->elf.root.type != bfd_link_hash_indirect
2741	&& edir->elf.dynamic_adjusted))
2742    edir->elf.non_got_ref |= eind->elf.non_got_ref;
2743
2744  edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
2745  edir->elf.ref_regular |= eind->elf.ref_regular;
2746  edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
2747  edir->elf.needs_plt |= eind->elf.needs_plt;
2748
2749  /* If we were called to copy over info for a weak sym, that's all.  */
2750  if (eind->elf.root.type != bfd_link_hash_indirect)
2751    return;
2752
2753  /* Copy over the GOT refcount entries that we may have already seen to
2754     the symbol which just became indirect.  */
2755  edir->elf.got.refcount += eind->elf.got.refcount;
2756  eind->elf.got.refcount = 0;
2757
2758  /* And plt entries.  */
2759  if (eind->elf.plt.plist != NULL)
2760    {
2761      if (edir->elf.plt.plist != NULL)
2762	{
2763	  struct plt_entry **entp;
2764	  struct plt_entry *ent;
2765
2766	  for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
2767	    {
2768	      struct plt_entry *dent;
2769
2770	      for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
2771		if (dent->sec == ent->sec && dent->addend == ent->addend)
2772		  {
2773		    dent->plt.refcount += ent->plt.refcount;
2774		    *entp = ent->next;
2775		    break;
2776		  }
2777	      if (dent == NULL)
2778		entp = &ent->next;
2779	    }
2780	  *entp = edir->elf.plt.plist;
2781	}
2782
2783      edir->elf.plt.plist = eind->elf.plt.plist;
2784      eind->elf.plt.plist = NULL;
2785    }
2786
2787  if (eind->elf.dynindx != -1)
2788    {
2789      if (edir->elf.dynindx != -1)
2790	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2791				edir->elf.dynstr_index);
2792      edir->elf.dynindx = eind->elf.dynindx;
2793      edir->elf.dynstr_index = eind->elf.dynstr_index;
2794      eind->elf.dynindx = -1;
2795      eind->elf.dynstr_index = 0;
2796    }
2797}
2798
2799/* Return 1 if target is one of ours.  */
2800
2801static bfd_boolean
2802is_ppc_elf_target (const struct bfd_target *targ)
2803{
2804  extern const bfd_target bfd_elf32_powerpc_vec;
2805  extern const bfd_target bfd_elf32_powerpc_vxworks_vec;
2806  extern const bfd_target bfd_elf32_powerpcle_vec;
2807
2808  return (targ == &bfd_elf32_powerpc_vec
2809	  || targ == &bfd_elf32_powerpc_vxworks_vec
2810	  || targ == &bfd_elf32_powerpcle_vec);
2811}
2812
2813/* Hook called by the linker routine which adds symbols from an object
2814   file.  We use it to put .comm items in .sbss, and not .bss.  */
2815
2816static bfd_boolean
2817ppc_elf_add_symbol_hook (bfd *abfd,
2818			 struct bfd_link_info *info,
2819			 Elf_Internal_Sym *sym,
2820			 const char **namep ATTRIBUTE_UNUSED,
2821			 flagword *flagsp ATTRIBUTE_UNUSED,
2822			 asection **secp,
2823			 bfd_vma *valp)
2824{
2825  if (sym->st_shndx == SHN_COMMON
2826      && !info->relocatable
2827      && sym->st_size <= elf_gp_size (abfd)
2828      && is_ppc_elf_target (info->hash->creator))
2829    {
2830      /* Common symbols less than or equal to -G nn bytes are automatically
2831	 put into .sbss.  */
2832      struct ppc_elf_link_hash_table *htab;
2833
2834      htab = ppc_elf_hash_table (info);
2835      if (htab->sbss == NULL)
2836	{
2837	  flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
2838
2839	  if (!htab->elf.dynobj)
2840	    htab->elf.dynobj = abfd;
2841
2842	  htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2843							   ".sbss",
2844							   flags);
2845	  if (htab->sbss == NULL)
2846	    return FALSE;
2847	}
2848
2849      *secp = htab->sbss;
2850      *valp = sym->st_size;
2851    }
2852
2853  return TRUE;
2854}
2855
2856static bfd_boolean
2857create_sdata_sym (struct ppc_elf_link_hash_table *htab,
2858		  elf_linker_section_t *lsect)
2859{
2860  lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
2861				     TRUE, FALSE, TRUE);
2862  if (lsect->sym == NULL)
2863    return FALSE;
2864  if (lsect->sym->root.type == bfd_link_hash_new)
2865    lsect->sym->non_elf = 0;
2866  lsect->sym->ref_regular = 1;
2867  return TRUE;
2868}
2869
2870/* Create a special linker section.  */
2871
2872static bfd_boolean
2873ppc_elf_create_linker_section (bfd *abfd,
2874			       struct bfd_link_info *info,
2875			       flagword flags,
2876			       elf_linker_section_t *lsect)
2877{
2878  struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2879  asection *s;
2880
2881  flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2882	    | SEC_LINKER_CREATED);
2883
2884  /* Record the first bfd that needs the special sections.  */
2885  if (!htab->elf.dynobj)
2886    htab->elf.dynobj = abfd;
2887
2888  s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2889					  lsect->name,
2890					  flags);
2891  if (s == NULL
2892      || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2893    return FALSE;
2894  lsect->section = s;
2895
2896  return create_sdata_sym (htab, lsect);
2897}
2898
2899/* Find a linker generated pointer with a given addend and type.  */
2900
2901static elf_linker_section_pointers_t *
2902elf_find_pointer_linker_section
2903  (elf_linker_section_pointers_t *linker_pointers,
2904   bfd_vma addend,
2905   elf_linker_section_t *lsect)
2906{
2907  for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2908    if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2909      return linker_pointers;
2910
2911  return NULL;
2912}
2913
2914/* Allocate a pointer to live in a linker created section.  */
2915
2916static bfd_boolean
2917elf_create_pointer_linker_section (bfd *abfd,
2918				   elf_linker_section_t *lsect,
2919				   struct elf_link_hash_entry *h,
2920				   const Elf_Internal_Rela *rel)
2921{
2922  elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2923  elf_linker_section_pointers_t *linker_section_ptr;
2924  unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2925  bfd_size_type amt;
2926
2927  BFD_ASSERT (lsect != NULL);
2928
2929  /* Is this a global symbol?  */
2930  if (h != NULL)
2931    {
2932      struct ppc_elf_link_hash_entry *eh;
2933
2934      /* Has this symbol already been allocated?  If so, our work is done.  */
2935      eh = (struct ppc_elf_link_hash_entry *) h;
2936      if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2937					   rel->r_addend,
2938					   lsect))
2939	return TRUE;
2940
2941      ptr_linker_section_ptr = &eh->linker_section_pointer;
2942    }
2943  else
2944    {
2945      /* Allocation of a pointer to a local symbol.  */
2946      elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2947
2948      /* Allocate a table to hold the local symbols if first time.  */
2949      if (!ptr)
2950	{
2951	  unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2952
2953	  amt = num_symbols;
2954	  amt *= sizeof (elf_linker_section_pointers_t *);
2955	  ptr = bfd_zalloc (abfd, amt);
2956
2957	  if (!ptr)
2958	    return FALSE;
2959
2960	  elf_local_ptr_offsets (abfd) = ptr;
2961	}
2962
2963      /* Has this symbol already been allocated?  If so, our work is done.  */
2964      if (elf_find_pointer_linker_section (ptr[r_symndx],
2965					   rel->r_addend,
2966					   lsect))
2967	return TRUE;
2968
2969      ptr_linker_section_ptr = &ptr[r_symndx];
2970    }
2971
2972  /* Allocate space for a pointer in the linker section, and allocate
2973     a new pointer record from internal memory.  */
2974  BFD_ASSERT (ptr_linker_section_ptr != NULL);
2975  amt = sizeof (elf_linker_section_pointers_t);
2976  linker_section_ptr = bfd_alloc (abfd, amt);
2977
2978  if (!linker_section_ptr)
2979    return FALSE;
2980
2981  linker_section_ptr->next = *ptr_linker_section_ptr;
2982  linker_section_ptr->addend = rel->r_addend;
2983  linker_section_ptr->lsect = lsect;
2984  *ptr_linker_section_ptr = linker_section_ptr;
2985
2986  linker_section_ptr->offset = lsect->section->size;
2987  lsect->section->size += 4;
2988
2989#ifdef DEBUG
2990  fprintf (stderr,
2991	   "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2992	   lsect->name, (long) linker_section_ptr->offset,
2993	   (long) lsect->section->size);
2994#endif
2995
2996  return TRUE;
2997}
2998
2999static bfd_boolean
3000update_local_sym_info (bfd *abfd,
3001		       Elf_Internal_Shdr *symtab_hdr,
3002		       unsigned long r_symndx,
3003		       int tls_type)
3004{
3005  bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3006  char *local_got_tls_masks;
3007
3008  if (local_got_refcounts == NULL)
3009    {
3010      bfd_size_type size = symtab_hdr->sh_info;
3011
3012      size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3013      local_got_refcounts = bfd_zalloc (abfd, size);
3014      if (local_got_refcounts == NULL)
3015	return FALSE;
3016      elf_local_got_refcounts (abfd) = local_got_refcounts;
3017    }
3018
3019  local_got_refcounts[r_symndx] += 1;
3020  local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3021  local_got_tls_masks[r_symndx] |= tls_type;
3022  return TRUE;
3023}
3024
3025static bfd_boolean
3026update_plt_info (bfd *abfd, struct elf_link_hash_entry *h,
3027		 asection *sec, bfd_vma addend)
3028{
3029  struct plt_entry *ent;
3030
3031  if (addend < 32768)
3032    sec = NULL;
3033  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
3034    if (ent->sec == sec && ent->addend == addend)
3035      break;
3036  if (ent == NULL)
3037    {
3038      bfd_size_type amt = sizeof (*ent);
3039      ent = bfd_alloc (abfd, amt);
3040      if (ent == NULL)
3041	return FALSE;
3042      ent->next = h->plt.plist;
3043      ent->sec = sec;
3044      ent->addend = addend;
3045      ent->plt.refcount = 0;
3046      h->plt.plist = ent;
3047    }
3048  ent->plt.refcount += 1;
3049  return TRUE;
3050}
3051
3052static struct plt_entry *
3053find_plt_ent (struct elf_link_hash_entry *h, asection *sec, bfd_vma addend)
3054{
3055  struct plt_entry *ent;
3056
3057  if (addend < 32768)
3058    sec = NULL;
3059  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
3060    if (ent->sec == sec && ent->addend == addend)
3061      break;
3062  return ent;
3063}
3064
3065static void
3066bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3067{
3068  (*_bfd_error_handler)
3069    (_("%B: relocation %s cannot be used when making a shared object"),
3070     abfd,
3071     ppc_elf_howto_table[r_type]->name);
3072  bfd_set_error (bfd_error_bad_value);
3073}
3074
3075/* Look through the relocs for a section during the first phase, and
3076   allocate space in the global offset table or procedure linkage
3077   table.  */
3078
3079static bfd_boolean
3080ppc_elf_check_relocs (bfd *abfd,
3081		      struct bfd_link_info *info,
3082		      asection *sec,
3083		      const Elf_Internal_Rela *relocs)
3084{
3085  struct ppc_elf_link_hash_table *htab;
3086  Elf_Internal_Shdr *symtab_hdr;
3087  struct elf_link_hash_entry **sym_hashes;
3088  const Elf_Internal_Rela *rel;
3089  const Elf_Internal_Rela *rel_end;
3090  asection *got2, *sreloc;
3091  struct elf_link_hash_entry *tga;
3092
3093  if (info->relocatable)
3094    return TRUE;
3095
3096  /* Don't do anything special with non-loaded, non-alloced sections.
3097     In particular, any relocs in such sections should not affect GOT
3098     and PLT reference counting (ie. we don't allow them to create GOT
3099     or PLT entries), there's no possibility or desire to optimize TLS
3100     relocs, and there's not much point in propagating relocs to shared
3101     libs that the dynamic linker won't relocate.  */
3102  if ((sec->flags & SEC_ALLOC) == 0)
3103    return TRUE;
3104
3105#ifdef DEBUG
3106  _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3107		      sec, abfd);
3108#endif
3109
3110  /* Initialize howto table if not already done.  */
3111  if (!ppc_elf_howto_table[R_PPC_ADDR32])
3112    ppc_elf_howto_init ();
3113
3114  htab = ppc_elf_hash_table (info);
3115  tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3116			      FALSE, FALSE, TRUE);
3117  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3118  sym_hashes = elf_sym_hashes (abfd);
3119  got2 = bfd_get_section_by_name (abfd, ".got2");
3120  sreloc = NULL;
3121
3122  rel_end = relocs + sec->reloc_count;
3123  for (rel = relocs; rel < rel_end; rel++)
3124    {
3125      unsigned long r_symndx;
3126      enum elf_ppc_reloc_type r_type;
3127      struct elf_link_hash_entry *h;
3128      int tls_type;
3129
3130      r_symndx = ELF32_R_SYM (rel->r_info);
3131      if (r_symndx < symtab_hdr->sh_info)
3132	h = NULL;
3133      else
3134	{
3135	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3136	  while (h->root.type == bfd_link_hash_indirect
3137		 || h->root.type == bfd_link_hash_warning)
3138	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3139	}
3140
3141      /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3142	 This shows up in particular in an R_PPC_ADDR32 in the eabi
3143	 startup code.  */
3144      if (h != NULL
3145	  && htab->got == NULL
3146	  && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3147	{
3148	  if (htab->elf.dynobj == NULL)
3149	    htab->elf.dynobj = abfd;
3150	  if (!ppc_elf_create_got (htab->elf.dynobj, info))
3151	    return FALSE;
3152	  BFD_ASSERT (h == htab->elf.hgot);
3153	}
3154
3155      tls_type = 0;
3156      r_type = ELF32_R_TYPE (rel->r_info);
3157      if (h != NULL && h == tga)
3158	switch (r_type)
3159	  {
3160	  default:
3161	    break;
3162
3163	  case R_PPC_PLTREL24:
3164	  case R_PPC_LOCAL24PC:
3165	  case R_PPC_REL24:
3166	  case R_PPC_REL14:
3167	  case R_PPC_REL14_BRTAKEN:
3168	  case R_PPC_REL14_BRNTAKEN:
3169	  case R_PPC_ADDR24:
3170	  case R_PPC_ADDR14:
3171	  case R_PPC_ADDR14_BRTAKEN:
3172	  case R_PPC_ADDR14_BRNTAKEN:
3173	    if (rel != relocs
3174		&& (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3175		    || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3176	      /* We have a new-style __tls_get_addr call with a marker
3177		 reloc.  */
3178	      ;
3179	    else
3180	      /* Mark this section as having an old-style call.  */
3181	      sec->has_tls_get_addr_call = 1;
3182	    break;
3183	  }
3184
3185      switch (r_type)
3186	{
3187	case R_PPC_TLSGD:
3188	case R_PPC_TLSLD:
3189	  /* These special tls relocs tie a call to __tls_get_addr with
3190	     its parameter symbol.  */
3191	  break;
3192
3193	case R_PPC_GOT_TLSLD16:
3194	case R_PPC_GOT_TLSLD16_LO:
3195	case R_PPC_GOT_TLSLD16_HI:
3196	case R_PPC_GOT_TLSLD16_HA:
3197	  tls_type = TLS_TLS | TLS_LD;
3198	  goto dogottls;
3199
3200	case R_PPC_GOT_TLSGD16:
3201	case R_PPC_GOT_TLSGD16_LO:
3202	case R_PPC_GOT_TLSGD16_HI:
3203	case R_PPC_GOT_TLSGD16_HA:
3204	  tls_type = TLS_TLS | TLS_GD;
3205	  goto dogottls;
3206
3207	case R_PPC_GOT_TPREL16:
3208	case R_PPC_GOT_TPREL16_LO:
3209	case R_PPC_GOT_TPREL16_HI:
3210	case R_PPC_GOT_TPREL16_HA:
3211	  if (!info->executable)
3212	    info->flags |= DF_STATIC_TLS;
3213	  tls_type = TLS_TLS | TLS_TPREL;
3214	  goto dogottls;
3215
3216	case R_PPC_GOT_DTPREL16:
3217	case R_PPC_GOT_DTPREL16_LO:
3218	case R_PPC_GOT_DTPREL16_HI:
3219	case R_PPC_GOT_DTPREL16_HA:
3220	  tls_type = TLS_TLS | TLS_DTPREL;
3221	dogottls:
3222	  sec->has_tls_reloc = 1;
3223	  /* Fall thru */
3224
3225	  /* GOT16 relocations */
3226	case R_PPC_GOT16:
3227	case R_PPC_GOT16_LO:
3228	case R_PPC_GOT16_HI:
3229	case R_PPC_GOT16_HA:
3230	  /* This symbol requires a global offset table entry.  */
3231	  if (htab->got == NULL)
3232	    {
3233	      if (htab->elf.dynobj == NULL)
3234		htab->elf.dynobj = abfd;
3235	      if (!ppc_elf_create_got (htab->elf.dynobj, info))
3236		return FALSE;
3237	    }
3238	  if (h != NULL)
3239	    {
3240	      h->got.refcount += 1;
3241	      ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3242	    }
3243	  else
3244	    /* This is a global offset table entry for a local symbol.  */
3245	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3246	      return FALSE;
3247	  break;
3248
3249	  /* Indirect .sdata relocation.  */
3250	case R_PPC_EMB_SDAI16:
3251	  if (info->shared)
3252	    {
3253	      bad_shared_reloc (abfd, r_type);
3254	      return FALSE;
3255	    }
3256	  if (htab->sdata[0].section == NULL
3257	      && !ppc_elf_create_linker_section (abfd, info, 0,
3258						 &htab->sdata[0]))
3259	    return FALSE;
3260	  if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3261						  h, rel))
3262	    return FALSE;
3263	  if (h != NULL)
3264	    {
3265	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3266	      h->non_got_ref = TRUE;
3267	    }
3268	  break;
3269
3270	  /* Indirect .sdata2 relocation.  */
3271	case R_PPC_EMB_SDA2I16:
3272	  if (info->shared)
3273	    {
3274	      bad_shared_reloc (abfd, r_type);
3275	      return FALSE;
3276	    }
3277	  if (htab->sdata[1].section == NULL
3278	      && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3279						 &htab->sdata[1]))
3280	    return FALSE;
3281	  if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3282						  h, rel))
3283	    return FALSE;
3284	  if (h != NULL)
3285	    {
3286	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3287	      h->non_got_ref = TRUE;
3288	    }
3289	  break;
3290
3291	case R_PPC_SDAREL16:
3292	  if (info->shared)
3293	    {
3294	      bad_shared_reloc (abfd, r_type);
3295	      return FALSE;
3296	    }
3297	  if (htab->sdata[0].sym == NULL
3298	      && !create_sdata_sym (htab, &htab->sdata[0]))
3299	    return FALSE;
3300	  if (h != NULL)
3301	    {
3302	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3303	      h->non_got_ref = TRUE;
3304	    }
3305	  break;
3306
3307	case R_PPC_EMB_SDA2REL:
3308	  if (info->shared)
3309	    {
3310	      bad_shared_reloc (abfd, r_type);
3311	      return FALSE;
3312	    }
3313	  if (htab->sdata[1].sym == NULL
3314	      && !create_sdata_sym (htab, &htab->sdata[1]))
3315	    return FALSE;
3316	  if (h != NULL)
3317	    {
3318	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3319	      h->non_got_ref = TRUE;
3320	    }
3321	  break;
3322
3323	case R_PPC_EMB_SDA21:
3324	case R_PPC_EMB_RELSDA:
3325	  if (info->shared)
3326	    {
3327	      bad_shared_reloc (abfd, r_type);
3328	      return FALSE;
3329	    }
3330	  if (htab->sdata[0].sym == NULL
3331	      && !create_sdata_sym (htab, &htab->sdata[0]))
3332	    return FALSE;
3333	  if (htab->sdata[1].sym == NULL
3334	      && !create_sdata_sym (htab, &htab->sdata[1]))
3335	    return FALSE;
3336	  if (h != NULL)
3337	    {
3338	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3339	      h->non_got_ref = TRUE;
3340	    }
3341	  break;
3342
3343	case R_PPC_EMB_NADDR32:
3344	case R_PPC_EMB_NADDR16:
3345	case R_PPC_EMB_NADDR16_LO:
3346	case R_PPC_EMB_NADDR16_HI:
3347	case R_PPC_EMB_NADDR16_HA:
3348	  if (info->shared)
3349	    {
3350	      bad_shared_reloc (abfd, r_type);
3351	      return FALSE;
3352	    }
3353	  if (h != NULL)
3354	    h->non_got_ref = TRUE;
3355	  break;
3356
3357	case R_PPC_PLT32:
3358	case R_PPC_PLTREL24:
3359	case R_PPC_PLTREL32:
3360	case R_PPC_PLT16_LO:
3361	case R_PPC_PLT16_HI:
3362	case R_PPC_PLT16_HA:
3363#ifdef DEBUG
3364	  fprintf (stderr, "Reloc requires a PLT entry\n");
3365#endif
3366	  /* This symbol requires a procedure linkage table entry.  We
3367	     actually build the entry in finish_dynamic_symbol,
3368	     because this might be a case of linking PIC code without
3369	     linking in any dynamic objects, in which case we don't
3370	     need to generate a procedure linkage table after all.  */
3371
3372	  if (h == NULL)
3373	    {
3374	      /* It does not make sense to have a procedure linkage
3375		 table entry for a local symbol.  */
3376	      (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3377				       "local symbol"),
3378				     abfd,
3379				     sec,
3380				     (long) rel->r_offset,
3381				     ppc_elf_howto_table[r_type]->name);
3382	      bfd_set_error (bfd_error_bad_value);
3383	      return FALSE;
3384	    }
3385	  else
3386	    {
3387	      bfd_vma addend = 0;
3388
3389	      if (r_type == R_PPC_PLTREL24)
3390		{
3391		  ppc_elf_tdata (abfd)->makes_plt_call = 1;
3392		  addend = rel->r_addend;
3393		}
3394	      h->needs_plt = 1;
3395	      if (!update_plt_info (abfd, h, got2, addend))
3396		return FALSE;
3397	    }
3398	  break;
3399
3400	  /* The following relocations don't need to propagate the
3401	     relocation if linking a shared object since they are
3402	     section relative.  */
3403	case R_PPC_SECTOFF:
3404	case R_PPC_SECTOFF_LO:
3405	case R_PPC_SECTOFF_HI:
3406	case R_PPC_SECTOFF_HA:
3407	case R_PPC_DTPREL16:
3408	case R_PPC_DTPREL16_LO:
3409	case R_PPC_DTPREL16_HI:
3410	case R_PPC_DTPREL16_HA:
3411	case R_PPC_TOC16:
3412	  break;
3413
3414	case R_PPC_REL16:
3415	case R_PPC_REL16_LO:
3416	case R_PPC_REL16_HI:
3417	case R_PPC_REL16_HA:
3418	  ppc_elf_tdata (abfd)->has_rel16 = 1;
3419	  break;
3420
3421	  /* These are just markers.  */
3422	case R_PPC_TLS:
3423	case R_PPC_EMB_MRKREF:
3424	case R_PPC_NONE:
3425	case R_PPC_max:
3426	  break;
3427
3428	  /* These should only appear in dynamic objects.  */
3429	case R_PPC_COPY:
3430	case R_PPC_GLOB_DAT:
3431	case R_PPC_JMP_SLOT:
3432	case R_PPC_RELATIVE:
3433	  break;
3434
3435	  /* These aren't handled yet.  We'll report an error later.  */
3436	case R_PPC_ADDR30:
3437	case R_PPC_EMB_RELSEC16:
3438	case R_PPC_EMB_RELST_LO:
3439	case R_PPC_EMB_RELST_HI:
3440	case R_PPC_EMB_RELST_HA:
3441	case R_PPC_EMB_BIT_FLD:
3442	  break;
3443
3444	  /* This refers only to functions defined in the shared library.  */
3445	case R_PPC_LOCAL24PC:
3446	  if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3447	    {
3448	      htab->plt_type = PLT_OLD;
3449	      htab->old_bfd = abfd;
3450	    }
3451	  break;
3452
3453	  /* This relocation describes the C++ object vtable hierarchy.
3454	     Reconstruct it for later use during GC.  */
3455	case R_PPC_GNU_VTINHERIT:
3456	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3457	    return FALSE;
3458	  break;
3459
3460	  /* This relocation describes which C++ vtable entries are actually
3461	     used.  Record for later use during GC.  */
3462	case R_PPC_GNU_VTENTRY:
3463	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3464	    return FALSE;
3465	  break;
3466
3467	  /* We shouldn't really be seeing these.  */
3468	case R_PPC_TPREL32:
3469	case R_PPC_TPREL16:
3470	case R_PPC_TPREL16_LO:
3471	case R_PPC_TPREL16_HI:
3472	case R_PPC_TPREL16_HA:
3473	  if (!info->executable)
3474	    info->flags |= DF_STATIC_TLS;
3475	  goto dodyn;
3476
3477	  /* Nor these.  */
3478	case R_PPC_DTPMOD32:
3479	case R_PPC_DTPREL32:
3480	  goto dodyn;
3481
3482	case R_PPC_REL32:
3483	  if (h == NULL
3484	      && got2 != NULL
3485	      && (sec->flags & SEC_CODE) != 0
3486	      && (info->shared || info->pie)
3487	      && htab->plt_type == PLT_UNSET)
3488	    {
3489	      /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3490		 the start of a function, which assembles to a REL32
3491		 reference to .got2.  If we detect one of these, then
3492		 force the old PLT layout because the linker cannot
3493		 reliably deduce the GOT pointer value needed for
3494		 PLT call stubs.  */
3495	      asection *s;
3496
3497	      s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3498					     r_symndx);
3499	      if (s == got2)
3500		{
3501		  htab->plt_type = PLT_OLD;
3502		  htab->old_bfd = abfd;
3503		}
3504	    }
3505	  if (h == NULL || h == htab->elf.hgot)
3506	    break;
3507	  goto dodyn1;
3508
3509	case R_PPC_REL24:
3510	case R_PPC_REL14:
3511	case R_PPC_REL14_BRTAKEN:
3512	case R_PPC_REL14_BRNTAKEN:
3513	  if (h == NULL)
3514	    break;
3515	  if (h == htab->elf.hgot)
3516	    {
3517	      if (htab->plt_type == PLT_UNSET)
3518		{
3519		  htab->plt_type = PLT_OLD;
3520		  htab->old_bfd = abfd;
3521		}
3522	      break;
3523	    }
3524	  /* fall through */
3525
3526	case R_PPC_ADDR32:
3527	case R_PPC_ADDR24:
3528	case R_PPC_ADDR16:
3529	case R_PPC_ADDR16_LO:
3530	case R_PPC_ADDR16_HI:
3531	case R_PPC_ADDR16_HA:
3532	case R_PPC_ADDR14:
3533	case R_PPC_ADDR14_BRTAKEN:
3534	case R_PPC_ADDR14_BRNTAKEN:
3535	case R_PPC_UADDR32:
3536	case R_PPC_UADDR16:
3537	dodyn1:
3538	  if (h != NULL && !info->shared)
3539	    {
3540	      /* We may need a plt entry if the symbol turns out to be
3541		 a function defined in a dynamic object.  */
3542	      if (!update_plt_info (abfd, h, NULL, 0))
3543		return FALSE;
3544
3545	      /* We may need a copy reloc too.  */
3546	      h->non_got_ref = 1;
3547	    }
3548
3549	dodyn:
3550	  /* If we are creating a shared library, and this is a reloc
3551	     against a global symbol, or a non PC relative reloc
3552	     against a local symbol, then we need to copy the reloc
3553	     into the shared library.  However, if we are linking with
3554	     -Bsymbolic, we do not need to copy a reloc against a
3555	     global symbol which is defined in an object we are
3556	     including in the link (i.e., DEF_REGULAR is set).  At
3557	     this point we have not seen all the input files, so it is
3558	     possible that DEF_REGULAR is not set now but will be set
3559	     later (it is never cleared).  In case of a weak definition,
3560	     DEF_REGULAR may be cleared later by a strong definition in
3561	     a shared library.  We account for that possibility below by
3562	     storing information in the dyn_relocs field of the hash
3563	     table entry.  A similar situation occurs when creating
3564	     shared libraries and symbol visibility changes render the
3565	     symbol local.
3566
3567	     If on the other hand, we are creating an executable, we
3568	     may need to keep relocations for symbols satisfied by a
3569	     dynamic library if we manage to avoid copy relocs for the
3570	     symbol.  */
3571	  if ((info->shared
3572	       && (must_be_dyn_reloc (info, r_type)
3573		   || (h != NULL
3574		       && (! info->symbolic
3575			   || h->root.type == bfd_link_hash_defweak
3576			   || !h->def_regular))))
3577	      || (ELIMINATE_COPY_RELOCS
3578		  && !info->shared
3579		  && h != NULL
3580		  && (h->root.type == bfd_link_hash_defweak
3581		      || !h->def_regular)))
3582	    {
3583	      struct ppc_elf_dyn_relocs *p;
3584	      struct ppc_elf_dyn_relocs **head;
3585
3586#ifdef DEBUG
3587	      fprintf (stderr,
3588		       "ppc_elf_check_relocs needs to "
3589		       "create relocation for %s\n",
3590		       (h && h->root.root.string
3591			? h->root.root.string : "<unknown>"));
3592#endif
3593	      if (sreloc == NULL)
3594		{
3595		  const char *name;
3596
3597		  name = (bfd_elf_string_from_elf_section
3598			  (abfd,
3599			   elf_elfheader (abfd)->e_shstrndx,
3600			   elf_section_data (sec)->rel_hdr.sh_name));
3601		  if (name == NULL)
3602		    return FALSE;
3603
3604		  BFD_ASSERT (CONST_STRNEQ (name, ".rela")
3605			      && strcmp (bfd_get_section_name (abfd, sec),
3606					 name + 5) == 0);
3607
3608		  if (htab->elf.dynobj == NULL)
3609		    htab->elf.dynobj = abfd;
3610		  sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3611		  if (sreloc == NULL)
3612		    {
3613		      flagword flags;
3614
3615		      flags = (SEC_HAS_CONTENTS | SEC_READONLY
3616			       | SEC_IN_MEMORY | SEC_LINKER_CREATED
3617			       | SEC_ALLOC | SEC_LOAD);
3618		      sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
3619							    name,
3620							    flags);
3621		      if (sreloc == NULL
3622			  || ! bfd_set_section_alignment (htab->elf.dynobj,
3623							  sreloc, 2))
3624			return FALSE;
3625		    }
3626		  elf_section_data (sec)->sreloc = sreloc;
3627		}
3628
3629	      /* If this is a global symbol, we count the number of
3630		 relocations we need for this symbol.  */
3631	      if (h != NULL)
3632		{
3633		  head = &ppc_elf_hash_entry (h)->dyn_relocs;
3634		}
3635	      else
3636		{
3637		  /* Track dynamic relocs needed for local syms too.
3638		     We really need local syms available to do this
3639		     easily.  Oh well.  */
3640
3641		  asection *s;
3642		  void *vpp;
3643
3644		  s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3645						 sec, r_symndx);
3646		  if (s == NULL)
3647		    return FALSE;
3648
3649		  vpp = &elf_section_data (s)->local_dynrel;
3650		  head = (struct ppc_elf_dyn_relocs **) vpp;
3651		}
3652
3653	      p = *head;
3654	      if (p == NULL || p->sec != sec)
3655		{
3656		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3657		  if (p == NULL)
3658		    return FALSE;
3659		  p->next = *head;
3660		  *head = p;
3661		  p->sec = sec;
3662		  p->count = 0;
3663		  p->pc_count = 0;
3664		}
3665
3666	      p->count += 1;
3667	      if (!must_be_dyn_reloc (info, r_type))
3668		p->pc_count += 1;
3669	    }
3670
3671	  break;
3672	}
3673    }
3674
3675  return TRUE;
3676}
3677
3678
3679/* Merge object attributes from IBFD into OBFD.  Raise an error if
3680   there are conflicting attributes.  */
3681static bfd_boolean
3682ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
3683{
3684  obj_attribute *in_attr;
3685  obj_attribute *out_attr;
3686
3687  if (!elf_known_obj_attributes_proc (obfd)[0].i)
3688    {
3689      /* This is the first object.  Copy the attributes.  */
3690      _bfd_elf_copy_obj_attributes (ibfd, obfd);
3691
3692      /* Use the Tag_null value to indicate the attributes have been
3693	 initialized.  */
3694      elf_known_obj_attributes_proc (obfd)[0].i = 1;
3695
3696      return TRUE;
3697    }
3698
3699  /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
3700     non-conflicting ones.  */
3701  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
3702  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
3703  if (in_attr[Tag_GNU_Power_ABI_FP].i != out_attr[Tag_GNU_Power_ABI_FP].i)
3704    {
3705      out_attr[Tag_GNU_Power_ABI_FP].type = 1;
3706      if (out_attr[Tag_GNU_Power_ABI_FP].i == 0)
3707	out_attr[Tag_GNU_Power_ABI_FP].i = in_attr[Tag_GNU_Power_ABI_FP].i;
3708      else if (in_attr[Tag_GNU_Power_ABI_FP].i == 0)
3709	;
3710      else if (out_attr[Tag_GNU_Power_ABI_FP].i == 1
3711	       && in_attr[Tag_GNU_Power_ABI_FP].i == 2)
3712	_bfd_error_handler
3713	  (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
3714      else if (out_attr[Tag_GNU_Power_ABI_FP].i == 2
3715	       && in_attr[Tag_GNU_Power_ABI_FP].i == 1)
3716	_bfd_error_handler
3717	  (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
3718      else if (in_attr[Tag_GNU_Power_ABI_FP].i > 2)
3719	_bfd_error_handler
3720	  (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
3721	   in_attr[Tag_GNU_Power_ABI_FP].i);
3722      else
3723	_bfd_error_handler
3724	  (_("Warning: %B uses unknown floating point ABI %d"), obfd,
3725	   out_attr[Tag_GNU_Power_ABI_FP].i);
3726    }
3727
3728  /* Merge Tag_compatibility attributes and any common GNU ones.  */
3729  _bfd_elf_merge_object_attributes (ibfd, obfd);
3730
3731  return TRUE;
3732}
3733
3734/* Merge backend specific data from an object file to the output
3735   object file when linking.  */
3736
3737static bfd_boolean
3738ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3739{
3740  flagword old_flags;
3741  flagword new_flags;
3742  bfd_boolean error;
3743
3744  if (!is_ppc_elf_target (ibfd->xvec)
3745      || !is_ppc_elf_target (obfd->xvec))
3746    return TRUE;
3747
3748  /* Check if we have the same endianess.  */
3749  if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3750    return FALSE;
3751
3752  if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
3753    return FALSE;
3754
3755  new_flags = elf_elfheader (ibfd)->e_flags;
3756  old_flags = elf_elfheader (obfd)->e_flags;
3757  if (!elf_flags_init (obfd))
3758    {
3759      /* First call, no flags set.  */
3760      elf_flags_init (obfd) = TRUE;
3761      elf_elfheader (obfd)->e_flags = new_flags;
3762    }
3763
3764  /* Compatible flags are ok.  */
3765  else if (new_flags == old_flags)
3766    ;
3767
3768  /* Incompatible flags.  */
3769  else
3770    {
3771      /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
3772	 to be linked with either.  */
3773      error = FALSE;
3774      if ((new_flags & EF_PPC_RELOCATABLE) != 0
3775	  && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3776	{
3777	  error = TRUE;
3778	  (*_bfd_error_handler)
3779	    (_("%B: compiled with -mrelocatable and linked with "
3780	       "modules compiled normally"), ibfd);
3781	}
3782      else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3783	       && (old_flags & EF_PPC_RELOCATABLE) != 0)
3784	{
3785	  error = TRUE;
3786	  (*_bfd_error_handler)
3787	    (_("%B: compiled normally and linked with "
3788	       "modules compiled with -mrelocatable"), ibfd);
3789	}
3790
3791      /* The output is -mrelocatable-lib iff both the input files are.  */
3792      if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3793	elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3794
3795      /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3796	 but each input file is either -mrelocatable or -mrelocatable-lib.  */
3797      if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3798	  && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3799	  && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3800	elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3801
3802      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3803	 any module uses it.  */
3804      elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3805
3806      new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3807      old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3808
3809      /* Warn about any other mismatches.  */
3810      if (new_flags != old_flags)
3811	{
3812	  error = TRUE;
3813	  (*_bfd_error_handler)
3814	    (_("%B: uses different e_flags (0x%lx) fields "
3815	       "than previous modules (0x%lx)"),
3816	     ibfd, (long) new_flags, (long) old_flags);
3817	}
3818
3819      if (error)
3820	{
3821	  bfd_set_error (bfd_error_bad_value);
3822	  return FALSE;
3823	}
3824    }
3825
3826  return TRUE;
3827}
3828
3829/* Choose which PLT scheme to use, and set .plt flags appropriately.
3830   Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
3831int
3832ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
3833			   struct bfd_link_info *info,
3834			   enum ppc_elf_plt_type plt_style,
3835			   int emit_stub_syms)
3836{
3837  struct ppc_elf_link_hash_table *htab;
3838  flagword flags;
3839
3840  htab = ppc_elf_hash_table (info);
3841
3842  if (htab->plt_type == PLT_UNSET)
3843    {
3844      if (plt_style == PLT_OLD)
3845	htab->plt_type = PLT_OLD;
3846      else
3847	{
3848	  bfd *ibfd;
3849	  enum ppc_elf_plt_type plt_type = plt_style;
3850
3851	  /* Look through the reloc flags left by ppc_elf_check_relocs.
3852	     Use the old style bss plt if a file makes plt calls
3853	     without using the new relocs, and if ld isn't given
3854	     --secure-plt and we never see REL16 relocs.  */
3855	  if (plt_type == PLT_UNSET)
3856	    plt_type = PLT_OLD;
3857	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
3858	    if (is_ppc_elf_target (ibfd->xvec))
3859	      {
3860		if (ppc_elf_tdata (ibfd)->has_rel16)
3861		  plt_type = PLT_NEW;
3862		else if (ppc_elf_tdata (ibfd)->makes_plt_call)
3863		  {
3864		    plt_type = PLT_OLD;
3865		    htab->old_bfd = ibfd;
3866		    break;
3867		  }
3868	      }
3869	  htab->plt_type = plt_type;
3870	}
3871    }
3872  if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
3873    info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
3874
3875  htab->emit_stub_syms = emit_stub_syms;
3876
3877  BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
3878
3879  if (htab->plt_type == PLT_NEW)
3880    {
3881      flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3882	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3883
3884      /* The new PLT is a loaded section.  */
3885      if (htab->plt != NULL
3886	  && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
3887	return -1;
3888
3889      /* The new GOT is not executable.  */
3890      if (htab->got != NULL
3891	  && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
3892	return -1;
3893    }
3894  else
3895    {
3896      /* Stop an unused .glink section from affecting .text alignment.  */
3897      if (htab->glink != NULL
3898	  && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
3899	return -1;
3900    }
3901  return htab->plt_type == PLT_NEW;
3902}
3903
3904/* Return the section that should be marked against GC for a given
3905   relocation.  */
3906
3907static asection *
3908ppc_elf_gc_mark_hook (asection *sec,
3909		      struct bfd_link_info *info,
3910		      Elf_Internal_Rela *rel,
3911		      struct elf_link_hash_entry *h,
3912		      Elf_Internal_Sym *sym)
3913{
3914  if (h != NULL)
3915    switch (ELF32_R_TYPE (rel->r_info))
3916      {
3917      case R_PPC_GNU_VTINHERIT:
3918      case R_PPC_GNU_VTENTRY:
3919	return NULL;
3920      }
3921
3922  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
3923}
3924
3925/* Update the got, plt and dynamic reloc reference counts for the
3926   section being removed.  */
3927
3928static bfd_boolean
3929ppc_elf_gc_sweep_hook (bfd *abfd,
3930		       struct bfd_link_info *info,
3931		       asection *sec,
3932		       const Elf_Internal_Rela *relocs)
3933{
3934  struct ppc_elf_link_hash_table *htab;
3935  Elf_Internal_Shdr *symtab_hdr;
3936  struct elf_link_hash_entry **sym_hashes;
3937  bfd_signed_vma *local_got_refcounts;
3938  const Elf_Internal_Rela *rel, *relend;
3939  asection *got2;
3940
3941  if ((sec->flags & SEC_ALLOC) == 0)
3942    return TRUE;
3943
3944  elf_section_data (sec)->local_dynrel = NULL;
3945
3946  htab = ppc_elf_hash_table (info);
3947  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3948  sym_hashes = elf_sym_hashes (abfd);
3949  local_got_refcounts = elf_local_got_refcounts (abfd);
3950  got2 = bfd_get_section_by_name (abfd, ".got2");
3951
3952  relend = relocs + sec->reloc_count;
3953  for (rel = relocs; rel < relend; rel++)
3954    {
3955      unsigned long r_symndx;
3956      enum elf_ppc_reloc_type r_type;
3957      struct elf_link_hash_entry *h = NULL;
3958
3959      r_symndx = ELF32_R_SYM (rel->r_info);
3960      if (r_symndx >= symtab_hdr->sh_info)
3961	{
3962	  struct ppc_elf_dyn_relocs **pp, *p;
3963	  struct ppc_elf_link_hash_entry *eh;
3964
3965	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3966	  while (h->root.type == bfd_link_hash_indirect
3967		 || h->root.type == bfd_link_hash_warning)
3968	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3969	  eh = (struct ppc_elf_link_hash_entry *) h;
3970
3971	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3972	    if (p->sec == sec)
3973	      {
3974		/* Everything must go for SEC.  */
3975		*pp = p->next;
3976		break;
3977	      }
3978	}
3979
3980      r_type = ELF32_R_TYPE (rel->r_info);
3981      switch (r_type)
3982	{
3983	case R_PPC_GOT_TLSLD16:
3984	case R_PPC_GOT_TLSLD16_LO:
3985	case R_PPC_GOT_TLSLD16_HI:
3986	case R_PPC_GOT_TLSLD16_HA:
3987	case R_PPC_GOT_TLSGD16:
3988	case R_PPC_GOT_TLSGD16_LO:
3989	case R_PPC_GOT_TLSGD16_HI:
3990	case R_PPC_GOT_TLSGD16_HA:
3991	case R_PPC_GOT_TPREL16:
3992	case R_PPC_GOT_TPREL16_LO:
3993	case R_PPC_GOT_TPREL16_HI:
3994	case R_PPC_GOT_TPREL16_HA:
3995	case R_PPC_GOT_DTPREL16:
3996	case R_PPC_GOT_DTPREL16_LO:
3997	case R_PPC_GOT_DTPREL16_HI:
3998	case R_PPC_GOT_DTPREL16_HA:
3999	case R_PPC_GOT16:
4000	case R_PPC_GOT16_LO:
4001	case R_PPC_GOT16_HI:
4002	case R_PPC_GOT16_HA:
4003	  if (h != NULL)
4004	    {
4005	      if (h->got.refcount > 0)
4006		h->got.refcount--;
4007	    }
4008	  else if (local_got_refcounts != NULL)
4009	    {
4010	      if (local_got_refcounts[r_symndx] > 0)
4011		local_got_refcounts[r_symndx]--;
4012	    }
4013	  break;
4014
4015	case R_PPC_REL24:
4016	case R_PPC_REL14:
4017	case R_PPC_REL14_BRTAKEN:
4018	case R_PPC_REL14_BRNTAKEN:
4019	case R_PPC_REL32:
4020	  if (h == NULL || h == htab->elf.hgot)
4021	    break;
4022	  /* Fall thru */
4023
4024	case R_PPC_ADDR32:
4025	case R_PPC_ADDR24:
4026	case R_PPC_ADDR16:
4027	case R_PPC_ADDR16_LO:
4028	case R_PPC_ADDR16_HI:
4029	case R_PPC_ADDR16_HA:
4030	case R_PPC_ADDR14:
4031	case R_PPC_ADDR14_BRTAKEN:
4032	case R_PPC_ADDR14_BRNTAKEN:
4033	case R_PPC_UADDR32:
4034	case R_PPC_UADDR16:
4035	  if (info->shared)
4036	    break;
4037
4038	case R_PPC_PLT32:
4039	case R_PPC_PLTREL24:
4040	case R_PPC_PLTREL32:
4041	case R_PPC_PLT16_LO:
4042	case R_PPC_PLT16_HI:
4043	case R_PPC_PLT16_HA:
4044	  if (h != NULL)
4045	    {
4046	      bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
4047	      struct plt_entry *ent = find_plt_ent (h, got2, addend);
4048	      if (ent->plt.refcount > 0)
4049		ent->plt.refcount -= 1;
4050	    }
4051	  break;
4052
4053	default:
4054	  break;
4055	}
4056    }
4057  return TRUE;
4058}
4059
4060/* Set plt output section type, htab->tls_get_addr, and call the
4061   generic ELF tls_setup function.  */
4062
4063asection *
4064ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4065{
4066  struct ppc_elf_link_hash_table *htab;
4067
4068  htab = ppc_elf_hash_table (info);
4069  if (htab->plt_type == PLT_NEW
4070      && htab->plt != NULL
4071      && htab->plt->output_section != NULL)
4072    {
4073      elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4074      elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4075    }
4076
4077  htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4078					     FALSE, FALSE, TRUE);
4079  return _bfd_elf_tls_setup (obfd, info);
4080}
4081
4082/* Return TRUE iff REL is a branch reloc with a global symbol matching
4083   HASH.  */
4084
4085static bfd_boolean
4086branch_reloc_hash_match (const bfd *ibfd,
4087			 const Elf_Internal_Rela *rel,
4088			 const struct elf_link_hash_entry *hash)
4089{
4090  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4091  enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4092  unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4093
4094  if (r_symndx >= symtab_hdr->sh_info
4095      && (r_type == R_PPC_PLTREL24
4096	  || r_type == R_PPC_LOCAL24PC
4097	  || r_type == R_PPC_REL14
4098	  || r_type == R_PPC_REL14_BRTAKEN
4099	  || r_type == R_PPC_REL14_BRNTAKEN
4100	  || r_type == R_PPC_REL24
4101	  || r_type == R_PPC_ADDR24
4102	  || r_type == R_PPC_ADDR14
4103	  || r_type == R_PPC_ADDR14_BRTAKEN
4104	  || r_type == R_PPC_ADDR14_BRNTAKEN))
4105    {
4106      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4107      struct elf_link_hash_entry *h;
4108
4109      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4110      while (h->root.type == bfd_link_hash_indirect
4111	     || h->root.type == bfd_link_hash_warning)
4112	h = (struct elf_link_hash_entry *) h->root.u.i.link;
4113      if (h == hash)
4114	return TRUE;
4115    }
4116  return FALSE;
4117}
4118
4119/* Run through all the TLS relocs looking for optimization
4120   opportunities.  */
4121
4122bfd_boolean
4123ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4124		      struct bfd_link_info *info)
4125{
4126  bfd *ibfd;
4127  asection *sec;
4128  struct ppc_elf_link_hash_table *htab;
4129  int pass;
4130
4131  if (info->relocatable || !info->executable)
4132    return TRUE;
4133
4134  htab = ppc_elf_hash_table (info);
4135  /* Make two passes through the relocs.  First time check that tls
4136     relocs involved in setting up a tls_get_addr call are indeed
4137     followed by such a call.  If they are not, exclude them from
4138     the optimizations done on the second pass.  */
4139  for (pass = 0; pass < 2; ++pass)
4140    for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4141      {
4142	Elf_Internal_Sym *locsyms = NULL;
4143	Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4144
4145	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4146	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4147	    {
4148	      Elf_Internal_Rela *relstart, *rel, *relend;
4149
4150	      /* Read the relocations.  */
4151	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4152						    info->keep_memory);
4153	      if (relstart == NULL)
4154		return FALSE;
4155
4156	      relend = relstart + sec->reloc_count;
4157	      for (rel = relstart; rel < relend; rel++)
4158		{
4159		  enum elf_ppc_reloc_type r_type;
4160		  unsigned long r_symndx;
4161		  struct elf_link_hash_entry *h = NULL;
4162		  char *tls_mask;
4163		  char tls_set, tls_clear;
4164		  bfd_boolean is_local;
4165		  int expecting_tls_get_addr;
4166		  bfd_signed_vma *got_count;
4167
4168		  r_symndx = ELF32_R_SYM (rel->r_info);
4169		  if (r_symndx >= symtab_hdr->sh_info)
4170		    {
4171		      struct elf_link_hash_entry **sym_hashes;
4172
4173		      sym_hashes = elf_sym_hashes (ibfd);
4174		      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4175		      while (h->root.type == bfd_link_hash_indirect
4176			     || h->root.type == bfd_link_hash_warning)
4177			h = (struct elf_link_hash_entry *) h->root.u.i.link;
4178		    }
4179
4180		  expecting_tls_get_addr = 0;
4181		  is_local = FALSE;
4182		  if (h == NULL
4183		      || !h->def_dynamic)
4184		    is_local = TRUE;
4185
4186		  r_type = ELF32_R_TYPE (rel->r_info);
4187		  switch (r_type)
4188		    {
4189		    case R_PPC_GOT_TLSLD16:
4190		    case R_PPC_GOT_TLSLD16_LO:
4191		      expecting_tls_get_addr = 1;
4192		      /* Fall thru */
4193
4194		    case R_PPC_GOT_TLSLD16_HI:
4195		    case R_PPC_GOT_TLSLD16_HA:
4196		      /* These relocs should never be against a symbol
4197			 defined in a shared lib.  Leave them alone if
4198			 that turns out to be the case.  */
4199		      if (!is_local)
4200			continue;
4201
4202		      /* LD -> LE */
4203		      tls_set = 0;
4204		      tls_clear = TLS_LD;
4205		      break;
4206
4207		    case R_PPC_GOT_TLSGD16:
4208		    case R_PPC_GOT_TLSGD16_LO:
4209		      expecting_tls_get_addr = 1;
4210		      /* Fall thru */
4211
4212		    case R_PPC_GOT_TLSGD16_HI:
4213		    case R_PPC_GOT_TLSGD16_HA:
4214		      if (is_local)
4215			/* GD -> LE */
4216			tls_set = 0;
4217		      else
4218			/* GD -> IE */
4219			tls_set = TLS_TLS | TLS_TPRELGD;
4220		      tls_clear = TLS_GD;
4221		      break;
4222
4223		    case R_PPC_GOT_TPREL16:
4224		    case R_PPC_GOT_TPREL16_LO:
4225		    case R_PPC_GOT_TPREL16_HI:
4226		    case R_PPC_GOT_TPREL16_HA:
4227		      if (is_local)
4228			{
4229			  /* IE -> LE */
4230			  tls_set = 0;
4231			  tls_clear = TLS_TPREL;
4232			  break;
4233			}
4234		      else
4235			continue;
4236
4237		    default:
4238		      continue;
4239		    }
4240
4241		  if (pass == 0)
4242		    {
4243		      if (!expecting_tls_get_addr
4244			  || !sec->has_tls_get_addr_call)
4245			continue;
4246
4247		      if (rel + 1 < relend
4248			  && branch_reloc_hash_match (ibfd, rel + 1,
4249						      htab->tls_get_addr))
4250			continue;
4251
4252		      /* Uh oh, we didn't find the expected call.  We
4253			 could just mark this symbol to exclude it
4254			 from tls optimization but it's safer to skip
4255			 the entire section.  */
4256		      sec->has_tls_reloc = 0;
4257		      break;
4258		    }
4259
4260		  if (h != NULL)
4261		    {
4262		      tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4263		      got_count = &h->got.refcount;
4264		    }
4265		  else
4266		    {
4267		      Elf_Internal_Sym *sym;
4268		      bfd_signed_vma *lgot_refs;
4269		      char *lgot_masks;
4270
4271		      if (locsyms == NULL)
4272			{
4273			  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4274			  if (locsyms == NULL)
4275			    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4276							    symtab_hdr->sh_info,
4277							    0, NULL, NULL, NULL);
4278			  if (locsyms == NULL)
4279			    {
4280			      if (elf_section_data (sec)->relocs != relstart)
4281				free (relstart);
4282			      return FALSE;
4283			    }
4284			}
4285		      sym = locsyms + r_symndx;
4286		      lgot_refs = elf_local_got_refcounts (ibfd);
4287		      if (lgot_refs == NULL)
4288			abort ();
4289		      lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4290		      tls_mask = &lgot_masks[r_symndx];
4291		      got_count = &lgot_refs[r_symndx];
4292		    }
4293
4294		  if (tls_set == 0)
4295		    {
4296		      /* We managed to get rid of a got entry.  */
4297		      if (*got_count > 0)
4298			*got_count -= 1;
4299		    }
4300
4301		  if (expecting_tls_get_addr)
4302		    {
4303		      struct plt_entry *ent;
4304
4305		      ent = find_plt_ent (htab->tls_get_addr, NULL, 0);
4306		      if (ent != NULL && ent->plt.refcount > 0)
4307			ent->plt.refcount -= 1;
4308		    }
4309
4310		  *tls_mask |= tls_set;
4311		  *tls_mask &= ~tls_clear;
4312		}
4313
4314	      if (elf_section_data (sec)->relocs != relstart)
4315		free (relstart);
4316	    }
4317
4318	if (locsyms != NULL
4319	    && (symtab_hdr->contents != (unsigned char *) locsyms))
4320	  {
4321	    if (!info->keep_memory)
4322	      free (locsyms);
4323	    else
4324	      symtab_hdr->contents = (unsigned char *) locsyms;
4325	  }
4326      }
4327  return TRUE;
4328}
4329
4330/* Adjust a symbol defined by a dynamic object and referenced by a
4331   regular object.  The current definition is in some section of the
4332   dynamic object, but we're not including those sections.  We have to
4333   change the definition to something the rest of the link can
4334   understand.  */
4335
4336static bfd_boolean
4337ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4338			       struct elf_link_hash_entry *h)
4339{
4340  struct ppc_elf_link_hash_table *htab;
4341  asection *s;
4342
4343#ifdef DEBUG
4344  fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4345	   h->root.root.string);
4346#endif
4347
4348  /* Make sure we know what is going on here.  */
4349  htab = ppc_elf_hash_table (info);
4350  BFD_ASSERT (htab->elf.dynobj != NULL
4351	      && (h->needs_plt
4352		  || h->u.weakdef != NULL
4353		  || (h->def_dynamic
4354		      && h->ref_regular
4355		      && !h->def_regular)));
4356
4357  /* Deal with function syms.  */
4358  if (h->type == STT_FUNC
4359      || h->needs_plt)
4360    {
4361      /* Clear procedure linkage table information for any symbol that
4362	 won't need a .plt entry.  */
4363      struct plt_entry *ent;
4364      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4365	if (ent->plt.refcount > 0)
4366	  break;
4367      if (ent == NULL
4368	  || SYMBOL_CALLS_LOCAL (info, h)
4369	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4370	      && h->root.type == bfd_link_hash_undefweak))
4371	{
4372	  /* A PLT entry is not required/allowed when:
4373
4374	     1. We are not using ld.so; because then the PLT entry
4375	     can't be set up, so we can't use one.  In this case,
4376	     ppc_elf_adjust_dynamic_symbol won't even be called.
4377
4378	     2. GC has rendered the entry unused.
4379
4380	     3. We know for certain that a call to this symbol
4381	     will go to this object, or will remain undefined.  */
4382	  h->plt.plist = NULL;
4383	  h->needs_plt = 0;
4384	}
4385      return TRUE;
4386    }
4387  else
4388    h->plt.plist = NULL;
4389
4390  /* If this is a weak symbol, and there is a real definition, the
4391     processor independent code will have arranged for us to see the
4392     real definition first, and we can just use the same value.  */
4393  if (h->u.weakdef != NULL)
4394    {
4395      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4396		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
4397      h->root.u.def.section = h->u.weakdef->root.u.def.section;
4398      h->root.u.def.value = h->u.weakdef->root.u.def.value;
4399      if (ELIMINATE_COPY_RELOCS)
4400	h->non_got_ref = h->u.weakdef->non_got_ref;
4401      return TRUE;
4402    }
4403
4404  /* This is a reference to a symbol defined by a dynamic object which
4405     is not a function.  */
4406
4407  /* If we are creating a shared library, we must presume that the
4408     only references to the symbol are via the global offset table.
4409     For such cases we need not do anything here; the relocations will
4410     be handled correctly by relocate_section.  */
4411  if (info->shared)
4412    return TRUE;
4413
4414  /* If there are no references to this symbol that do not use the
4415     GOT, we don't need to generate a copy reloc.  */
4416  if (!h->non_got_ref)
4417    return TRUE;
4418
4419   /* If we didn't find any dynamic relocs in read-only sections, then we'll
4420      be keeping the dynamic relocs and avoiding the copy reloc.  We can't
4421      do this if there are any small data relocations.  */
4422  if (ELIMINATE_COPY_RELOCS
4423      && !ppc_elf_hash_entry (h)->has_sda_refs)
4424    {
4425      struct ppc_elf_dyn_relocs *p;
4426      for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4427	{
4428	  s = p->sec->output_section;
4429	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
4430	    break;
4431	}
4432
4433      if (p == NULL)
4434	{
4435	  h->non_got_ref = 0;
4436	  return TRUE;
4437	}
4438    }
4439
4440  if (h->size == 0)
4441    {
4442      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4443			     h->root.root.string);
4444      return TRUE;
4445    }
4446
4447  /* We must allocate the symbol in our .dynbss section, which will
4448     become part of the .bss section of the executable.  There will be
4449     an entry for this symbol in the .dynsym section.  The dynamic
4450     object will contain position independent code, so all references
4451     from the dynamic object to this symbol will go through the global
4452     offset table.  The dynamic linker will use the .dynsym entry to
4453     determine the address it must put in the global offset table, so
4454     both the dynamic object and the regular object will refer to the
4455     same memory location for the variable.
4456
4457     Of course, if the symbol is referenced using SDAREL relocs, we
4458     must instead allocate it in .sbss.  */
4459
4460  if (ppc_elf_hash_entry (h)->has_sda_refs)
4461    s = htab->dynsbss;
4462  else
4463    s = htab->dynbss;
4464  BFD_ASSERT (s != NULL);
4465
4466  /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
4467     copy the initial value out of the dynamic object and into the
4468     runtime process image.  We need to remember the offset into the
4469     .rela.bss section we are going to use.  */
4470  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4471    {
4472      asection *srel;
4473
4474      if (ppc_elf_hash_entry (h)->has_sda_refs)
4475	srel = htab->relsbss;
4476      else
4477	srel = htab->relbss;
4478      BFD_ASSERT (srel != NULL);
4479      srel->size += sizeof (Elf32_External_Rela);
4480      h->needs_copy = 1;
4481    }
4482
4483  return _bfd_elf_adjust_dynamic_copy (h, s);
4484}
4485
4486/* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
4487   xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
4488   specifying the addend on the plt relocation.  For -fpic code, the sym
4489   is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
4490   xxxxxxxx.got2.plt_pic32.<callee>.  */
4491
4492static bfd_boolean
4493add_stub_sym (struct plt_entry *ent,
4494	      struct elf_link_hash_entry *h,
4495	      struct bfd_link_info *info)
4496{
4497  struct elf_link_hash_entry *sh;
4498  size_t len1, len2, len3;
4499  char *name;
4500  const char *stub;
4501  struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
4502
4503  if (info->shared || info->pie)
4504    stub = ".plt_pic32.";
4505  else
4506    stub = ".plt_call32.";
4507
4508  len1 = strlen (h->root.root.string);
4509  len2 = strlen (stub);
4510  len3 = 0;
4511  if (ent->sec)
4512    len3 = strlen (ent->sec->name);
4513  name = bfd_malloc (len1 + len2 + len3 + 9);
4514  if (name == NULL)
4515    return FALSE;
4516  sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
4517  if (ent->sec)
4518    memcpy (name + 8, ent->sec->name, len3);
4519  memcpy (name + 8 + len3, stub, len2);
4520  memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
4521  sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
4522  if (sh == NULL)
4523    return FALSE;
4524  if (sh->root.type == bfd_link_hash_new)
4525    {
4526      sh->root.type = bfd_link_hash_defined;
4527      sh->root.u.def.section = htab->glink;
4528      sh->root.u.def.value = ent->glink_offset;
4529      sh->ref_regular = 1;
4530      sh->def_regular = 1;
4531      sh->ref_regular_nonweak = 1;
4532      sh->forced_local = 1;
4533      sh->non_elf = 0;
4534    }
4535  return TRUE;
4536}
4537
4538/* Allocate NEED contiguous space in .got, and return the offset.
4539   Handles allocation of the got header when crossing 32k.  */
4540
4541static bfd_vma
4542allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
4543{
4544  bfd_vma where;
4545  unsigned int max_before_header;
4546
4547  if (htab->plt_type == PLT_VXWORKS)
4548    {
4549      where = htab->got->size;
4550      htab->got->size += need;
4551    }
4552  else
4553    {
4554      max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
4555      if (need <= htab->got_gap)
4556	{
4557	  where = max_before_header - htab->got_gap;
4558	  htab->got_gap -= need;
4559	}
4560      else
4561	{
4562	  if (htab->got->size + need > max_before_header
4563	      && htab->got->size <= max_before_header)
4564	    {
4565	      htab->got_gap = max_before_header - htab->got->size;
4566	      htab->got->size = max_before_header + htab->got_header_size;
4567	    }
4568	  where = htab->got->size;
4569	  htab->got->size += need;
4570	}
4571    }
4572  return where;
4573}
4574
4575/* Allocate space in associated reloc sections for dynamic relocs.  */
4576
4577static bfd_boolean
4578allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4579{
4580  struct bfd_link_info *info = inf;
4581  struct ppc_elf_link_hash_entry *eh;
4582  struct ppc_elf_link_hash_table *htab;
4583  struct ppc_elf_dyn_relocs *p;
4584
4585  if (h->root.type == bfd_link_hash_indirect)
4586    return TRUE;
4587
4588  if (h->root.type == bfd_link_hash_warning)
4589    /* When warning symbols are created, they **replace** the "real"
4590       entry in the hash table, thus we never get to see the real
4591       symbol in a hash traversal.  So look at it now.  */
4592    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4593
4594  htab = ppc_elf_hash_table (info);
4595  if (htab->elf.dynamic_sections_created)
4596    {
4597      struct plt_entry *ent;
4598      bfd_boolean doneone = FALSE;
4599      bfd_vma plt_offset = 0, glink_offset = 0;
4600
4601      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4602	if (ent->plt.refcount > 0)
4603	  {
4604	    /* Make sure this symbol is output as a dynamic symbol.  */
4605	    if (h->dynindx == -1
4606		&& !h->forced_local)
4607	      {
4608		if (! bfd_elf_link_record_dynamic_symbol (info, h))
4609		  return FALSE;
4610	      }
4611
4612	    if (info->shared
4613		|| WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
4614	      {
4615		asection *s = htab->plt;
4616
4617		if (htab->plt_type == PLT_NEW)
4618		  {
4619		    if (!doneone)
4620		      {
4621			plt_offset = s->size;
4622			s->size += 4;
4623		      }
4624		    ent->plt.offset = plt_offset;
4625
4626		    s = htab->glink;
4627		    if (!doneone || info->shared || info->pie)
4628		      {
4629			glink_offset = s->size;
4630			s->size += GLINK_ENTRY_SIZE;
4631		      }
4632		    if (!doneone
4633			&& !info->shared
4634			&& !h->def_regular)
4635		      {
4636			h->root.u.def.section = s;
4637			h->root.u.def.value = glink_offset;
4638		      }
4639		    ent->glink_offset = glink_offset;
4640
4641		    if (htab->emit_stub_syms
4642			&& !add_stub_sym (ent, h, info))
4643		      return FALSE;
4644		  }
4645		else
4646		  {
4647		    if (!doneone)
4648		      {
4649			/* If this is the first .plt entry, make room
4650			   for the special first entry.  */
4651			if (s->size == 0)
4652			  s->size += htab->plt_initial_entry_size;
4653
4654			/* The PowerPC PLT is actually composed of two
4655			   parts, the first part is 2 words (for a load
4656			   and a jump), and then there is a remaining
4657			   word available at the end.  */
4658			plt_offset = (htab->plt_initial_entry_size
4659				      + (htab->plt_slot_size
4660					 * ((s->size
4661					     - htab->plt_initial_entry_size)
4662					    / htab->plt_entry_size)));
4663
4664			/* If this symbol is not defined in a regular
4665			   file, and we are not generating a shared
4666			   library, then set the symbol to this location
4667			   in the .plt.  This is required to make
4668			   function pointers compare as equal between
4669			   the normal executable and the shared library.  */
4670			if (! info->shared
4671			    && !h->def_regular)
4672			  {
4673			    h->root.u.def.section = s;
4674			    h->root.u.def.value = plt_offset;
4675			  }
4676
4677			/* Make room for this entry.  */
4678			s->size += htab->plt_entry_size;
4679			/* After the 8192nd entry, room for two entries
4680			   is allocated.  */
4681			if (htab->plt_type == PLT_OLD
4682			    && (s->size - htab->plt_initial_entry_size)
4683				/ htab->plt_entry_size
4684			       > PLT_NUM_SINGLE_ENTRIES)
4685			  s->size += htab->plt_entry_size;
4686		      }
4687		    ent->plt.offset = plt_offset;
4688		  }
4689
4690		/* We also need to make an entry in the .rela.plt section.  */
4691		if (!doneone)
4692		  {
4693		    htab->relplt->size += sizeof (Elf32_External_Rela);
4694
4695		    if (htab->plt_type == PLT_VXWORKS)
4696		      {
4697			/* Allocate space for the unloaded relocations.  */
4698			if (!info->shared)
4699			  {
4700			    if (ent->plt.offset
4701				== (bfd_vma) htab->plt_initial_entry_size)
4702			      {
4703				htab->srelplt2->size
4704				  += sizeof (Elf32_External_Rela)
4705				      * VXWORKS_PLTRESOLVE_RELOCS;
4706			      }
4707
4708			    htab->srelplt2->size
4709			      += sizeof (Elf32_External_Rela)
4710				  * VXWORKS_PLT_NON_JMP_SLOT_RELOCS;
4711			  }
4712
4713			/* Every PLT entry has an associated GOT entry in
4714			   .got.plt.  */
4715			htab->sgotplt->size += 4;
4716		      }
4717		    doneone = TRUE;
4718		  }
4719	      }
4720	    else
4721	      ent->plt.offset = (bfd_vma) -1;
4722	  }
4723	else
4724	  ent->plt.offset = (bfd_vma) -1;
4725
4726      if (!doneone)
4727	{
4728	  h->plt.plist = NULL;
4729	  h->needs_plt = 0;
4730	}
4731    }
4732  else
4733    {
4734      h->plt.plist = NULL;
4735      h->needs_plt = 0;
4736    }
4737
4738  eh = (struct ppc_elf_link_hash_entry *) h;
4739  if (eh->elf.got.refcount > 0)
4740    {
4741      /* Make sure this symbol is output as a dynamic symbol.  */
4742      if (eh->elf.dynindx == -1
4743	  && !eh->elf.forced_local)
4744	{
4745	  if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
4746	    return FALSE;
4747	}
4748
4749      if (eh->tls_mask == (TLS_TLS | TLS_LD)
4750	  && !eh->elf.def_dynamic)
4751	{
4752	  /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
4753	  htab->tlsld_got.refcount += 1;
4754	  eh->elf.got.offset = (bfd_vma) -1;
4755	}
4756      else
4757	{
4758	  bfd_boolean dyn;
4759	  unsigned int need = 0;
4760	  if ((eh->tls_mask & TLS_TLS) != 0)
4761	    {
4762	      if ((eh->tls_mask & TLS_LD) != 0)
4763		need += 8;
4764	      if ((eh->tls_mask & TLS_GD) != 0)
4765		need += 8;
4766	      if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
4767		need += 4;
4768	      if ((eh->tls_mask & TLS_DTPREL) != 0)
4769		need += 4;
4770	    }
4771	  else
4772	    need += 4;
4773	  eh->elf.got.offset = allocate_got (htab, need);
4774	  dyn = htab->elf.dynamic_sections_created;
4775	  if ((info->shared
4776	       || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
4777	      && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
4778		  || eh->elf.root.type != bfd_link_hash_undefweak))
4779	    {
4780	      /* All the entries we allocated need relocs.
4781		 Except LD only needs one.  */
4782	      if ((eh->tls_mask & TLS_LD) != 0)
4783		need -= 4;
4784	      htab->relgot->size += need * (sizeof (Elf32_External_Rela) / 4);
4785	    }
4786	}
4787    }
4788  else
4789    eh->elf.got.offset = (bfd_vma) -1;
4790
4791  if (eh->dyn_relocs == NULL)
4792    return TRUE;
4793
4794  /* In the shared -Bsymbolic case, discard space allocated for
4795     dynamic pc-relative relocs against symbols which turn out to be
4796     defined in regular objects.  For the normal shared case, discard
4797     space for relocs that have become local due to symbol visibility
4798     changes.  */
4799
4800  if (info->shared)
4801    {
4802      /* Relocs that use pc_count are those that appear on a call insn,
4803	 or certain REL relocs (see must_be_dyn_reloc) that can be
4804	 generated via assembly.  We want calls to protected symbols to
4805	 resolve directly to the function rather than going via the plt.
4806	 If people want function pointer comparisons to work as expected
4807	 then they should avoid writing weird assembly.  */
4808      if (SYMBOL_CALLS_LOCAL (info, h))
4809	{
4810	  struct ppc_elf_dyn_relocs **pp;
4811
4812	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4813	    {
4814	      p->count -= p->pc_count;
4815	      p->pc_count = 0;
4816	      if (p->count == 0)
4817		*pp = p->next;
4818	      else
4819		pp = &p->next;
4820	    }
4821	}
4822
4823      /* Also discard relocs on undefined weak syms with non-default
4824	 visibility.  */
4825      if (eh->dyn_relocs != NULL
4826	  && h->root.type == bfd_link_hash_undefweak)
4827	{
4828	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4829	    eh->dyn_relocs = NULL;
4830
4831	  /* Make sure undefined weak symbols are output as a dynamic
4832	     symbol in PIEs.  */
4833	  else if (h->dynindx == -1
4834		   && !h->forced_local)
4835	    {
4836	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4837		return FALSE;
4838	    }
4839	}
4840    }
4841  else if (ELIMINATE_COPY_RELOCS)
4842    {
4843      /* For the non-shared case, discard space for relocs against
4844	 symbols which turn out to need copy relocs or are not
4845	 dynamic.  */
4846
4847      if (!h->non_got_ref
4848	  && h->def_dynamic
4849	  && !h->def_regular)
4850	{
4851	  /* Make sure this symbol is output as a dynamic symbol.
4852	     Undefined weak syms won't yet be marked as dynamic.  */
4853	  if (h->dynindx == -1
4854	      && !h->forced_local)
4855	    {
4856	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4857		return FALSE;
4858	    }
4859
4860	  /* If that succeeded, we know we'll be keeping all the
4861	     relocs.  */
4862	  if (h->dynindx != -1)
4863	    goto keep;
4864	}
4865
4866      eh->dyn_relocs = NULL;
4867
4868    keep: ;
4869    }
4870
4871  /* Finally, allocate space.  */
4872  for (p = eh->dyn_relocs; p != NULL; p = p->next)
4873    {
4874      asection *sreloc = elf_section_data (p->sec)->sreloc;
4875      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4876    }
4877
4878  return TRUE;
4879}
4880
4881/* Find any dynamic relocs that apply to read-only sections.  */
4882
4883static bfd_boolean
4884readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
4885{
4886  struct ppc_elf_dyn_relocs *p;
4887
4888  if (h->root.type == bfd_link_hash_indirect)
4889    return TRUE;
4890
4891  if (h->root.type == bfd_link_hash_warning)
4892    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4893
4894  for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4895    {
4896      asection *s = p->sec->output_section;
4897
4898      if (s != NULL
4899	  && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4900	      == (SEC_READONLY | SEC_ALLOC)))
4901	{
4902	  ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
4903
4904	  /* Not an error, just cut short the traversal.  */
4905	  return FALSE;
4906	}
4907    }
4908  return TRUE;
4909}
4910
4911/* Set the sizes of the dynamic sections.  */
4912
4913static bfd_boolean
4914ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4915			       struct bfd_link_info *info)
4916{
4917  struct ppc_elf_link_hash_table *htab;
4918  asection *s;
4919  bfd_boolean relocs;
4920  bfd *ibfd;
4921
4922#ifdef DEBUG
4923  fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
4924#endif
4925
4926  htab = ppc_elf_hash_table (info);
4927  BFD_ASSERT (htab->elf.dynobj != NULL);
4928
4929  if (elf_hash_table (info)->dynamic_sections_created)
4930    {
4931      /* Set the contents of the .interp section to the interpreter.  */
4932      if (info->executable)
4933	{
4934	  s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
4935	  BFD_ASSERT (s != NULL);
4936	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4937	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4938	}
4939    }
4940
4941  if (htab->plt_type == PLT_OLD)
4942    htab->got_header_size = 16;
4943  else if (htab->plt_type == PLT_NEW)
4944    htab->got_header_size = 12;
4945
4946  /* Set up .got offsets for local syms, and space for local dynamic
4947     relocs.  */
4948  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4949    {
4950      bfd_signed_vma *local_got;
4951      bfd_signed_vma *end_local_got;
4952      char *lgot_masks;
4953      bfd_size_type locsymcount;
4954      Elf_Internal_Shdr *symtab_hdr;
4955
4956      if (!is_ppc_elf_target (ibfd->xvec))
4957	continue;
4958
4959      for (s = ibfd->sections; s != NULL; s = s->next)
4960	{
4961	  struct ppc_elf_dyn_relocs *p;
4962
4963	  for (p = ((struct ppc_elf_dyn_relocs *)
4964		    elf_section_data (s)->local_dynrel);
4965	       p != NULL;
4966	       p = p->next)
4967	    {
4968	      if (!bfd_is_abs_section (p->sec)
4969		  && bfd_is_abs_section (p->sec->output_section))
4970		{
4971		  /* Input section has been discarded, either because
4972		     it is a copy of a linkonce section or due to
4973		     linker script /DISCARD/, so we'll be discarding
4974		     the relocs too.  */
4975		}
4976	      else if (p->count != 0)
4977		{
4978		  elf_section_data (p->sec)->sreloc->size
4979		    += p->count * sizeof (Elf32_External_Rela);
4980		  if ((p->sec->output_section->flags
4981		       & (SEC_READONLY | SEC_ALLOC))
4982		      == (SEC_READONLY | SEC_ALLOC))
4983		    info->flags |= DF_TEXTREL;
4984		}
4985	    }
4986	}
4987
4988      local_got = elf_local_got_refcounts (ibfd);
4989      if (!local_got)
4990	continue;
4991
4992      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4993      locsymcount = symtab_hdr->sh_info;
4994      end_local_got = local_got + locsymcount;
4995      lgot_masks = (char *) end_local_got;
4996      for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4997	if (*local_got > 0)
4998	  {
4999	    if (*lgot_masks == (TLS_TLS | TLS_LD))
5000	      {
5001		/* If just an LD reloc, we'll just use
5002		   htab->tlsld_got.offset.  */
5003		htab->tlsld_got.refcount += 1;
5004		*local_got = (bfd_vma) -1;
5005	      }
5006	    else
5007	      {
5008		unsigned int need = 0;
5009		if ((*lgot_masks & TLS_TLS) != 0)
5010		  {
5011		    if ((*lgot_masks & TLS_GD) != 0)
5012		      need += 8;
5013		    if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5014		      need += 4;
5015		    if ((*lgot_masks & TLS_DTPREL) != 0)
5016		      need += 4;
5017		  }
5018		else
5019		  need += 4;
5020		*local_got = allocate_got (htab, need);
5021		if (info->shared)
5022		  htab->relgot->size += (need
5023					 * (sizeof (Elf32_External_Rela) / 4));
5024	      }
5025	  }
5026	else
5027	  *local_got = (bfd_vma) -1;
5028    }
5029
5030  /* Allocate space for global sym dynamic relocs.  */
5031  elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5032
5033  if (htab->tlsld_got.refcount > 0)
5034    {
5035      htab->tlsld_got.offset = allocate_got (htab, 8);
5036      if (info->shared)
5037	htab->relgot->size += sizeof (Elf32_External_Rela);
5038    }
5039  else
5040    htab->tlsld_got.offset = (bfd_vma) -1;
5041
5042  if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
5043    {
5044      unsigned int g_o_t = 32768;
5045
5046      /* If we haven't allocated the header, do so now.  When we get here,
5047	 for old plt/got the got size will be 0 to 32764 (not allocated),
5048	 or 32780 to 65536 (header allocated).  For new plt/got, the
5049	 corresponding ranges are 0 to 32768 and 32780 to 65536.  */
5050      if (htab->got->size <= 32768)
5051	{
5052	  g_o_t = htab->got->size;
5053	  if (htab->plt_type == PLT_OLD)
5054	    g_o_t += 4;
5055	  htab->got->size += htab->got_header_size;
5056	}
5057
5058      htab->elf.hgot->root.u.def.value = g_o_t;
5059    }
5060
5061  if (htab->glink != NULL && htab->glink->size != 0)
5062    {
5063      htab->glink_pltresolve = htab->glink->size;
5064      /* Space for the branch table.  */
5065      htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5066      /* Pad out to align the start of PLTresolve.  */
5067      htab->glink->size += -htab->glink->size & 15;
5068      htab->glink->size += GLINK_PLTRESOLVE;
5069
5070      if (htab->emit_stub_syms)
5071	{
5072	  struct elf_link_hash_entry *sh;
5073	  sh = elf_link_hash_lookup (&htab->elf, "__glink",
5074				     TRUE, FALSE, FALSE);
5075	  if (sh == NULL)
5076	    return FALSE;
5077	  if (sh->root.type == bfd_link_hash_new)
5078	    {
5079	      sh->root.type = bfd_link_hash_defined;
5080	      sh->root.u.def.section = htab->glink;
5081	      sh->root.u.def.value = htab->glink_pltresolve;
5082	      sh->ref_regular = 1;
5083	      sh->def_regular = 1;
5084	      sh->ref_regular_nonweak = 1;
5085	      sh->forced_local = 1;
5086	      sh->non_elf = 0;
5087	    }
5088	  sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5089				     TRUE, FALSE, FALSE);
5090	  if (sh == NULL)
5091	    return FALSE;
5092	  if (sh->root.type == bfd_link_hash_new)
5093	    {
5094	      sh->root.type = bfd_link_hash_defined;
5095	      sh->root.u.def.section = htab->glink;
5096	      sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5097	      sh->ref_regular = 1;
5098	      sh->def_regular = 1;
5099	      sh->ref_regular_nonweak = 1;
5100	      sh->forced_local = 1;
5101	      sh->non_elf = 0;
5102	    }
5103	}
5104    }
5105
5106  /* We've now determined the sizes of the various dynamic sections.
5107     Allocate memory for them.  */
5108  relocs = FALSE;
5109  for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5110    {
5111      bfd_boolean strip_section = TRUE;
5112
5113      if ((s->flags & SEC_LINKER_CREATED) == 0)
5114	continue;
5115
5116      if (s == htab->plt
5117	  || s == htab->glink
5118	  || s == htab->got
5119	  || s == htab->sgotplt
5120	  || s == htab->sbss
5121	  || s == htab->dynbss
5122	  || s == htab->dynsbss)
5123	{
5124	  /* We'd like to strip these sections if they aren't needed, but if
5125	     we've exported dynamic symbols from them we must leave them.
5126	     It's too late to tell BFD to get rid of the symbols.  */
5127	  if ((s == htab->plt || s == htab->got) && htab->elf.hplt != NULL)
5128	    strip_section = FALSE;
5129	  /* Strip this section if we don't need it; see the
5130	     comment below.  */
5131	}
5132      else if (s == htab->sdata[0].section
5133	       || s == htab->sdata[1].section)
5134	{
5135	  /* Strip these too.  */
5136	}
5137      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5138	{
5139	  if (s->size != 0)
5140	    {
5141	      /* Remember whether there are any relocation sections.  */
5142	      relocs = TRUE;
5143
5144	      /* We use the reloc_count field as a counter if we need
5145		 to copy relocs into the output file.  */
5146	      s->reloc_count = 0;
5147	    }
5148	}
5149      else
5150	{
5151	  /* It's not one of our sections, so don't allocate space.  */
5152	  continue;
5153	}
5154
5155      if (s->size == 0 && strip_section)
5156	{
5157	  /* If we don't need this section, strip it from the
5158	     output file.  This is mostly to handle .rela.bss and
5159	     .rela.plt.  We must create both sections in
5160	     create_dynamic_sections, because they must be created
5161	     before the linker maps input sections to output
5162	     sections.  The linker does that before
5163	     adjust_dynamic_symbol is called, and it is that
5164	     function which decides whether anything needs to go
5165	     into these sections.  */
5166	  s->flags |= SEC_EXCLUDE;
5167	  continue;
5168	}
5169
5170      if ((s->flags & SEC_HAS_CONTENTS) == 0)
5171	continue;
5172
5173      /* Allocate memory for the section contents.  */
5174      s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5175      if (s->contents == NULL)
5176	return FALSE;
5177    }
5178
5179  if (htab->elf.dynamic_sections_created)
5180    {
5181      /* Add some entries to the .dynamic section.  We fill in the
5182	 values later, in ppc_elf_finish_dynamic_sections, but we
5183	 must add the entries now so that we get the correct size for
5184	 the .dynamic section.  The DT_DEBUG entry is filled in by the
5185	 dynamic linker and used by the debugger.  */
5186#define add_dynamic_entry(TAG, VAL) \
5187  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5188
5189      if (info->executable)
5190	{
5191	  if (!add_dynamic_entry (DT_DEBUG, 0))
5192	    return FALSE;
5193	}
5194
5195      if (htab->plt != NULL && htab->plt->size != 0)
5196	{
5197	  if (!add_dynamic_entry (DT_PLTGOT, 0)
5198	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
5199	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5200	      || !add_dynamic_entry (DT_JMPREL, 0))
5201	    return FALSE;
5202	}
5203
5204      if (htab->glink != NULL && htab->glink->size != 0)
5205	{
5206	  if (!add_dynamic_entry (DT_PPC_GOT, 0))
5207	    return FALSE;
5208	}
5209
5210      if (relocs)
5211	{
5212	  if (!add_dynamic_entry (DT_RELA, 0)
5213	      || !add_dynamic_entry (DT_RELASZ, 0)
5214	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5215	    return FALSE;
5216	}
5217
5218      /* If any dynamic relocs apply to a read-only section, then we
5219	 need a DT_TEXTREL entry.  */
5220      if ((info->flags & DF_TEXTREL) == 0)
5221	elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
5222				info);
5223
5224      if ((info->flags & DF_TEXTREL) != 0)
5225	{
5226	  if (!add_dynamic_entry (DT_TEXTREL, 0))
5227	    return FALSE;
5228	}
5229    }
5230#undef add_dynamic_entry
5231
5232  return TRUE;
5233}
5234
5235#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5236
5237static const int shared_stub_entry[] =
5238  {
5239    0x7c0802a6, /* mflr 0 */
5240    0x429f0005, /* bcl 20, 31, .Lxxx */
5241    0x7d6802a6, /* mflr 11 */
5242    0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
5243    0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
5244    0x7c0803a6, /* mtlr 0 */
5245    0x7d6903a6, /* mtctr 11 */
5246    0x4e800420, /* bctr */
5247  };
5248
5249static const int stub_entry[] =
5250  {
5251    0x3d600000, /* lis 11,xxx@ha */
5252    0x396b0000, /* addi 11,11,xxx@l */
5253    0x7d6903a6, /* mtctr 11 */
5254    0x4e800420, /* bctr */
5255  };
5256
5257static bfd_boolean
5258ppc_elf_relax_section (bfd *abfd,
5259		       asection *isec,
5260		       struct bfd_link_info *link_info,
5261		       bfd_boolean *again)
5262{
5263  struct one_fixup
5264  {
5265    struct one_fixup *next;
5266    asection *tsec;
5267    bfd_vma toff;
5268    bfd_vma trampoff;
5269  };
5270
5271  Elf_Internal_Shdr *symtab_hdr;
5272  bfd_byte *contents = NULL;
5273  Elf_Internal_Sym *isymbuf = NULL;
5274  Elf_Internal_Rela *internal_relocs = NULL;
5275  Elf_Internal_Rela *irel, *irelend;
5276  struct one_fixup *fixups = NULL;
5277  bfd_boolean changed;
5278  struct ppc_elf_link_hash_table *htab;
5279  bfd_size_type trampoff;
5280  asection *got2;
5281
5282  *again = FALSE;
5283
5284  /* Nothing to do if there are no relocations, and no need to do
5285     anything with non-alloc sections.  */
5286  if ((isec->flags & SEC_ALLOC) == 0
5287      || (isec->flags & SEC_RELOC) == 0
5288      || isec->reloc_count == 0)
5289    return TRUE;
5290
5291  trampoff = (isec->size + 3) & (bfd_vma) -4;
5292  /* Space for a branch around any trampolines.  */
5293  trampoff += 4;
5294
5295  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5296
5297  /* Get a copy of the native relocations.  */
5298  internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
5299					       link_info->keep_memory);
5300  if (internal_relocs == NULL)
5301    goto error_return;
5302
5303  htab = ppc_elf_hash_table (link_info);
5304  got2 = bfd_get_section_by_name (abfd, ".got2");
5305
5306  irelend = internal_relocs + isec->reloc_count;
5307  for (irel = internal_relocs; irel < irelend; irel++)
5308    {
5309      unsigned long r_type = ELF32_R_TYPE (irel->r_info);
5310      bfd_vma symaddr, reladdr, toff, roff;
5311      asection *tsec;
5312      struct one_fixup *f;
5313      size_t insn_offset = 0;
5314      bfd_vma max_branch_offset, val;
5315      bfd_byte *hit_addr;
5316      unsigned long t0;
5317      unsigned char sym_type;
5318
5319      switch (r_type)
5320	{
5321	case R_PPC_REL24:
5322	case R_PPC_LOCAL24PC:
5323	case R_PPC_PLTREL24:
5324	  max_branch_offset = 1 << 25;
5325	  break;
5326
5327	case R_PPC_REL14:
5328	case R_PPC_REL14_BRTAKEN:
5329	case R_PPC_REL14_BRNTAKEN:
5330	  max_branch_offset = 1 << 15;
5331	  break;
5332
5333	default:
5334	  continue;
5335	}
5336
5337      /* Get the value of the symbol referred to by the reloc.  */
5338      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
5339	{
5340	  /* A local symbol.  */
5341	  Elf_Internal_Sym *isym;
5342
5343	  /* Read this BFD's local symbols.  */
5344	  if (isymbuf == NULL)
5345	    {
5346	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5347	      if (isymbuf == NULL)
5348		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5349						symtab_hdr->sh_info, 0,
5350						NULL, NULL, NULL);
5351	      if (isymbuf == 0)
5352		goto error_return;
5353	    }
5354	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
5355	  if (isym->st_shndx == SHN_UNDEF)
5356	    continue;	/* We can't do anything with undefined symbols.  */
5357	  else if (isym->st_shndx == SHN_ABS)
5358	    tsec = bfd_abs_section_ptr;
5359	  else if (isym->st_shndx == SHN_COMMON)
5360	    tsec = bfd_com_section_ptr;
5361	  else
5362	    tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5363
5364	  toff = isym->st_value;
5365	  sym_type = ELF_ST_TYPE (isym->st_info);
5366	}
5367      else
5368	{
5369	  /* Global symbol handling.  */
5370	  unsigned long indx;
5371	  struct elf_link_hash_entry *h;
5372
5373	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
5374	  h = elf_sym_hashes (abfd)[indx];
5375
5376	  while (h->root.type == bfd_link_hash_indirect
5377		 || h->root.type == bfd_link_hash_warning)
5378	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5379
5380	  tsec = NULL;
5381	  toff = 0;
5382	  if (r_type == R_PPC_PLTREL24
5383	      && htab->plt != NULL)
5384	    {
5385	      struct plt_entry *ent = find_plt_ent (h, got2, irel->r_addend);
5386
5387	      if (ent != NULL)
5388		{
5389		  if (htab->plt_type == PLT_NEW)
5390		    {
5391		      tsec = htab->glink;
5392		      toff = ent->glink_offset;
5393		    }
5394		  else
5395		    {
5396		      tsec = htab->plt;
5397		      toff = ent->plt.offset;
5398		    }
5399		}
5400	    }
5401	  if (tsec != NULL)
5402	    ;
5403	  else if (h->root.type == bfd_link_hash_defined
5404		   || h->root.type == bfd_link_hash_defweak)
5405	    {
5406	      tsec = h->root.u.def.section;
5407	      toff = h->root.u.def.value;
5408	    }
5409	  else
5410	    continue;
5411
5412	  sym_type = h->type;
5413	}
5414
5415      /* If the branch and target are in the same section, you have
5416	 no hope of adding stubs.  We'll error out later should the
5417	 branch overflow.  */
5418      if (tsec == isec)
5419	continue;
5420
5421      /* There probably isn't any reason to handle symbols in
5422	 SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
5423	 attribute for a code section, and we are only looking at
5424	 branches.  However, implement it correctly here as a
5425	 reference for other target relax_section functions.  */
5426      if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
5427	{
5428	  /* At this stage in linking, no SEC_MERGE symbol has been
5429	     adjusted, so all references to such symbols need to be
5430	     passed through _bfd_merged_section_offset.  (Later, in
5431	     relocate_section, all SEC_MERGE symbols *except* for
5432	     section symbols have been adjusted.)
5433
5434	     gas may reduce relocations against symbols in SEC_MERGE
5435	     sections to a relocation against the section symbol when
5436	     the original addend was zero.  When the reloc is against
5437	     a section symbol we should include the addend in the
5438	     offset passed to _bfd_merged_section_offset, since the
5439	     location of interest is the original symbol.  On the
5440	     other hand, an access to "sym+addend" where "sym" is not
5441	     a section symbol should not include the addend;  Such an
5442	     access is presumed to be an offset from "sym";  The
5443	     location of interest is just "sym".  */
5444	  if (sym_type == STT_SECTION)
5445	    toff += irel->r_addend;
5446
5447	  toff = _bfd_merged_section_offset (abfd, &tsec,
5448					     elf_section_data (tsec)->sec_info,
5449					     toff);
5450
5451	  if (sym_type != STT_SECTION)
5452	    toff += irel->r_addend;
5453	}
5454      /* PLTREL24 addends are special.  */
5455      else if (r_type != R_PPC_PLTREL24)
5456	toff += irel->r_addend;
5457
5458      /* Attempted -shared link of non-pic code loses.  */
5459      if (tsec->output_section == NULL)
5460	continue;
5461
5462      symaddr = tsec->output_section->vma + tsec->output_offset + toff;
5463
5464      roff = irel->r_offset;
5465      reladdr = isec->output_section->vma + isec->output_offset + roff;
5466
5467      /* If the branch is in range, no need to do anything.  */
5468      if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
5469	continue;
5470
5471      /* Look for an existing fixup to this address.  */
5472      for (f = fixups; f ; f = f->next)
5473	if (f->tsec == tsec && f->toff == toff)
5474	  break;
5475
5476      if (f == NULL)
5477	{
5478	  size_t size;
5479	  unsigned long stub_rtype;
5480
5481	  val = trampoff - roff;
5482	  if (val >= max_branch_offset)
5483	    /* Oh dear, we can't reach a trampoline.  Don't try to add
5484	       one.  We'll report an error later.  */
5485	    continue;
5486
5487	  if (link_info->shared)
5488	    {
5489	      size = 4 * ARRAY_SIZE (shared_stub_entry);
5490	      insn_offset = 12;
5491	      stub_rtype = R_PPC_RELAX32PC;
5492	    }
5493	  else
5494	    {
5495	      size = 4 * ARRAY_SIZE (stub_entry);
5496	      insn_offset = 0;
5497	      stub_rtype = R_PPC_RELAX32;
5498	    }
5499
5500	  if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
5501	      != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
5502	    abort ();
5503	  if (tsec == htab->plt
5504	      || tsec == htab->glink)
5505	    stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
5506
5507	  /* Hijack the old relocation.  Since we need two
5508	     relocations for this use a "composite" reloc.  */
5509	  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5510				       stub_rtype);
5511	  irel->r_offset = trampoff + insn_offset;
5512
5513	  /* Record the fixup so we don't do it again this section.  */
5514	  f = bfd_malloc (sizeof (*f));
5515	  f->next = fixups;
5516	  f->tsec = tsec;
5517	  f->toff = toff;
5518	  f->trampoff = trampoff;
5519	  fixups = f;
5520
5521	  trampoff += size;
5522	}
5523      else
5524	{
5525	  val = f->trampoff - roff;
5526	  if (val >= max_branch_offset)
5527	    continue;
5528
5529	  /* Nop out the reloc, since we're finalizing things here.  */
5530	  irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
5531	}
5532
5533      /* Get the section contents.  */
5534      if (contents == NULL)
5535	{
5536	  /* Get cached copy if it exists.  */
5537	  if (elf_section_data (isec)->this_hdr.contents != NULL)
5538	    contents = elf_section_data (isec)->this_hdr.contents;
5539	  else
5540	    {
5541	      /* Go get them off disk.  */
5542	      if (!bfd_malloc_and_get_section (abfd, isec, &contents))
5543		goto error_return;
5544	    }
5545	}
5546
5547      /* Fix up the existing branch to hit the trampoline.  */
5548      hit_addr = contents + roff;
5549      switch (r_type)
5550	{
5551	case R_PPC_REL24:
5552	case R_PPC_LOCAL24PC:
5553	case R_PPC_PLTREL24:
5554	  t0 = bfd_get_32 (abfd, hit_addr);
5555	  t0 &= ~0x3fffffc;
5556	  t0 |= val & 0x3fffffc;
5557	  bfd_put_32 (abfd, t0, hit_addr);
5558	  break;
5559
5560	case R_PPC_REL14:
5561	case R_PPC_REL14_BRTAKEN:
5562	case R_PPC_REL14_BRNTAKEN:
5563	  t0 = bfd_get_32 (abfd, hit_addr);
5564	  t0 &= ~0xfffc;
5565	  t0 |= val & 0xfffc;
5566	  bfd_put_32 (abfd, t0, hit_addr);
5567	  break;
5568	}
5569    }
5570
5571  /* Write out the trampolines.  */
5572  changed = fixups != NULL;
5573  if (fixups != NULL)
5574    {
5575      const int *stub;
5576      bfd_byte *dest;
5577      bfd_vma val;
5578      int i, size;
5579
5580      do
5581	{
5582	  struct one_fixup *f = fixups;
5583	  fixups = fixups->next;
5584	  free (f);
5585	}
5586      while (fixups);
5587
5588      contents = bfd_realloc (contents, trampoff);
5589      if (contents == NULL)
5590	goto error_return;
5591
5592      isec->size = (isec->size + 3) & (bfd_vma) -4;
5593      /* Branch around the trampolines.  */
5594      val = trampoff - isec->size + 0x48000000;
5595      dest = contents + isec->size;
5596      isec->size = trampoff;
5597      bfd_put_32 (abfd, val, dest);
5598      dest += 4;
5599
5600      if (link_info->shared)
5601	{
5602	  stub = shared_stub_entry;
5603	  size = ARRAY_SIZE (shared_stub_entry);
5604	}
5605      else
5606	{
5607	  stub = stub_entry;
5608	  size = ARRAY_SIZE (stub_entry);
5609	}
5610
5611      i = 0;
5612      while (dest < contents + trampoff)
5613	{
5614	  bfd_put_32 (abfd, stub[i], dest);
5615	  i++;
5616	  if (i == size)
5617	    i = 0;
5618	  dest += 4;
5619	}
5620      BFD_ASSERT (i == 0);
5621    }
5622
5623  if (isymbuf != NULL
5624      && symtab_hdr->contents != (unsigned char *) isymbuf)
5625    {
5626      if (! link_info->keep_memory)
5627	free (isymbuf);
5628      else
5629	{
5630	  /* Cache the symbols for elf_link_input_bfd.  */
5631	  symtab_hdr->contents = (unsigned char *) isymbuf;
5632	}
5633    }
5634
5635  if (contents != NULL
5636      && elf_section_data (isec)->this_hdr.contents != contents)
5637    {
5638      if (!changed && !link_info->keep_memory)
5639	free (contents);
5640      else
5641	{
5642	  /* Cache the section contents for elf_link_input_bfd.  */
5643	  elf_section_data (isec)->this_hdr.contents = contents;
5644	}
5645    }
5646
5647  if (elf_section_data (isec)->relocs != internal_relocs)
5648    {
5649      if (!changed)
5650	free (internal_relocs);
5651      else
5652	elf_section_data (isec)->relocs = internal_relocs;
5653    }
5654
5655  *again = changed;
5656  return TRUE;
5657
5658 error_return:
5659  if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
5660    free (isymbuf);
5661  if (contents != NULL
5662      && elf_section_data (isec)->this_hdr.contents != contents)
5663    free (contents);
5664  if (internal_relocs != NULL
5665      && elf_section_data (isec)->relocs != internal_relocs)
5666    free (internal_relocs);
5667  return FALSE;
5668}
5669
5670/* What to do when ld finds relocations against symbols defined in
5671   discarded sections.  */
5672
5673static unsigned int
5674ppc_elf_action_discarded (asection *sec)
5675{
5676  if (strcmp (".fixup", sec->name) == 0)
5677    return 0;
5678
5679  if (strcmp (".got2", sec->name) == 0)
5680    return 0;
5681
5682  return _bfd_elf_default_action_discarded (sec);
5683}
5684
5685/* Fill in the address for a pointer generated in a linker section.  */
5686
5687static bfd_vma
5688elf_finish_pointer_linker_section (bfd *input_bfd,
5689				   elf_linker_section_t *lsect,
5690				   struct elf_link_hash_entry *h,
5691				   bfd_vma relocation,
5692				   const Elf_Internal_Rela *rel)
5693{
5694  elf_linker_section_pointers_t *linker_section_ptr;
5695
5696  BFD_ASSERT (lsect != NULL);
5697
5698  if (h != NULL)
5699    {
5700      /* Handle global symbol.  */
5701      struct ppc_elf_link_hash_entry *eh;
5702
5703      eh = (struct ppc_elf_link_hash_entry *) h;
5704      BFD_ASSERT (eh->elf.def_regular);
5705      linker_section_ptr = eh->linker_section_pointer;
5706    }
5707  else
5708    {
5709      /* Handle local symbol.  */
5710      unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
5711
5712      BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
5713      linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
5714    }
5715
5716  linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
5717							rel->r_addend,
5718							lsect);
5719  BFD_ASSERT (linker_section_ptr != NULL);
5720
5721  /* Offset will always be a multiple of four, so use the bottom bit
5722     as a "written" flag.  */
5723  if ((linker_section_ptr->offset & 1) == 0)
5724    {
5725      bfd_put_32 (lsect->section->owner,
5726		  relocation + linker_section_ptr->addend,
5727		  lsect->section->contents + linker_section_ptr->offset);
5728      linker_section_ptr->offset += 1;
5729    }
5730
5731  relocation = (lsect->section->output_offset
5732		+ linker_section_ptr->offset - 1
5733		- 0x8000);
5734
5735#ifdef DEBUG
5736  fprintf (stderr,
5737	   "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5738	   lsect->name, (long) relocation, (long) relocation);
5739#endif
5740
5741  /* Subtract out the addend, because it will get added back in by the normal
5742     processing.  */
5743  return relocation - linker_section_ptr->addend;
5744}
5745
5746/* The RELOCATE_SECTION function is called by the ELF backend linker
5747   to handle the relocations for a section.
5748
5749   The relocs are always passed as Rela structures; if the section
5750   actually uses Rel structures, the r_addend field will always be
5751   zero.
5752
5753   This function is responsible for adjust the section contents as
5754   necessary, and (if using Rela relocs and generating a
5755   relocatable output file) adjusting the reloc addend as
5756   necessary.
5757
5758   This function does not have to worry about setting the reloc
5759   address or the reloc symbol index.
5760
5761   LOCAL_SYMS is a pointer to the swapped in local symbols.
5762
5763   LOCAL_SECTIONS is an array giving the section in the input file
5764   corresponding to the st_shndx field of each local symbol.
5765
5766   The global hash table entry for the global symbols can be found
5767   via elf_sym_hashes (input_bfd).
5768
5769   When generating relocatable output, this function must handle
5770   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
5771   going to be the section symbol corresponding to the output
5772   section, which means that the addend must be adjusted
5773   accordingly.  */
5774
5775static bfd_boolean
5776ppc_elf_relocate_section (bfd *output_bfd,
5777			  struct bfd_link_info *info,
5778			  bfd *input_bfd,
5779			  asection *input_section,
5780			  bfd_byte *contents,
5781			  Elf_Internal_Rela *relocs,
5782			  Elf_Internal_Sym *local_syms,
5783			  asection **local_sections)
5784{
5785  Elf_Internal_Shdr *symtab_hdr;
5786  struct elf_link_hash_entry **sym_hashes;
5787  struct ppc_elf_link_hash_table *htab;
5788  Elf_Internal_Rela *rel;
5789  Elf_Internal_Rela *relend;
5790  Elf_Internal_Rela outrel;
5791  bfd_byte *loc;
5792  asection *got2, *sreloc = NULL;
5793  bfd_vma *local_got_offsets;
5794  bfd_boolean ret = TRUE;
5795  bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5796
5797#ifdef DEBUG
5798  _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
5799		      "%ld relocations%s",
5800		      input_bfd, input_section,
5801		      (long) input_section->reloc_count,
5802		      (info->relocatable) ? " (relocatable)" : "");
5803#endif
5804
5805  got2 = bfd_get_section_by_name (input_bfd, ".got2");
5806
5807  /* Initialize howto table if not already done.  */
5808  if (!ppc_elf_howto_table[R_PPC_ADDR32])
5809    ppc_elf_howto_init ();
5810
5811  htab = ppc_elf_hash_table (info);
5812  local_got_offsets = elf_local_got_offsets (input_bfd);
5813  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5814  sym_hashes = elf_sym_hashes (input_bfd);
5815  rel = relocs;
5816  relend = relocs + input_section->reloc_count;
5817  for (; rel < relend; rel++)
5818    {
5819      enum elf_ppc_reloc_type r_type;
5820      bfd_vma addend;
5821      bfd_reloc_status_type r;
5822      Elf_Internal_Sym *sym;
5823      asection *sec;
5824      struct elf_link_hash_entry *h;
5825      const char *sym_name;
5826      reloc_howto_type *howto;
5827      unsigned long r_symndx;
5828      bfd_vma relocation;
5829      bfd_vma branch_bit, insn, from;
5830      bfd_boolean unresolved_reloc;
5831      bfd_boolean warned;
5832      unsigned int tls_type, tls_mask, tls_gd;
5833
5834      r_type = ELF32_R_TYPE (rel->r_info);
5835      sym = NULL;
5836      sec = NULL;
5837      h = NULL;
5838      unresolved_reloc = FALSE;
5839      warned = FALSE;
5840      r_symndx = ELF32_R_SYM (rel->r_info);
5841
5842      if (r_symndx < symtab_hdr->sh_info)
5843	{
5844	  sym = local_syms + r_symndx;
5845	  sec = local_sections[r_symndx];
5846	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
5847
5848	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5849	}
5850      else
5851	{
5852	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5853				   r_symndx, symtab_hdr, sym_hashes,
5854				   h, sec, relocation,
5855				   unresolved_reloc, warned);
5856
5857	  sym_name = h->root.root.string;
5858	}
5859
5860      if (sec != NULL && elf_discarded_section (sec))
5861	{
5862	  /* For relocs against symbols from removed linkonce sections,
5863	     or sections discarded by a linker script, we just want the
5864	     section contents zeroed.  Avoid any special processing.  */
5865	  howto = NULL;
5866	  if (r_type < R_PPC_max)
5867	    howto = ppc_elf_howto_table[r_type];
5868	  _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
5869	  rel->r_info = 0;
5870	  rel->r_addend = 0;
5871	  continue;
5872	}
5873
5874      if (info->relocatable)
5875	{
5876	  if (got2 != NULL
5877	      && r_type == R_PPC_PLTREL24
5878	      && rel->r_addend >= 32768)
5879	    {
5880	      /* R_PPC_PLTREL24 is rather special.  If non-zero, the
5881		 addend specifies the GOT pointer offset within .got2.  */
5882	      rel->r_addend += got2->output_offset;
5883	    }
5884	  continue;
5885	}
5886
5887      /* TLS optimizations.  Replace instruction sequences and relocs
5888	 based on information we collected in tls_optimize.  We edit
5889	 RELOCS so that --emit-relocs will output something sensible
5890	 for the final instruction stream.  */
5891      tls_mask = 0;
5892      tls_gd = 0;
5893      if (h != NULL)
5894	tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
5895      else if (local_got_offsets != NULL)
5896	{
5897	  char *lgot_masks;
5898	  lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
5899	  tls_mask = lgot_masks[r_symndx];
5900	}
5901
5902      /* Ensure reloc mapping code below stays sane.  */
5903      if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
5904	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
5905	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
5906	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
5907	  || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
5908	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
5909	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
5910	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
5911	abort ();
5912      switch (r_type)
5913	{
5914	default:
5915	  break;
5916
5917	case R_PPC_GOT_TPREL16:
5918	case R_PPC_GOT_TPREL16_LO:
5919	  if (tls_mask != 0
5920	      && (tls_mask & TLS_TPREL) == 0)
5921	    {
5922	      bfd_vma insn;
5923	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
5924	      insn &= 31 << 21;
5925	      insn |= 0x3c020000;	/* addis 0,2,0 */
5926	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
5927	      r_type = R_PPC_TPREL16_HA;
5928	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5929	    }
5930	  break;
5931
5932	case R_PPC_TLS:
5933	  if (tls_mask != 0
5934	      && (tls_mask & TLS_TPREL) == 0)
5935	    {
5936	      bfd_vma insn, rtra;
5937	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5938	      if ((insn & ((31 << 26) | (31 << 11)))
5939		  == ((31 << 26) | (2 << 11)))
5940		rtra = insn & ((1 << 26) - (1 << 16));
5941	      else if ((insn & ((31 << 26) | (31 << 16)))
5942		       == ((31 << 26) | (2 << 16)))
5943		rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
5944	      else
5945		abort ();
5946	      if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
5947		/* add -> addi.  */
5948		insn = 14 << 26;
5949	      else if ((insn & (31 << 1)) == 23 << 1
5950		       && ((insn & (31 << 6)) < 14 << 6
5951			   || ((insn & (31 << 6)) >= 16 << 6
5952			       && (insn & (31 << 6)) < 24 << 6)))
5953		/* load and store indexed -> dform.  */
5954		insn = (32 | ((insn >> 6) & 31)) << 26;
5955	      else if ((insn & (31 << 1)) == 21 << 1
5956		       && (insn & (0x1a << 6)) == 0)
5957		/* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
5958		insn = (((58 | ((insn >> 6) & 4)) << 26)
5959			| ((insn >> 6) & 1));
5960	      else if ((insn & (31 << 1)) == 21 << 1
5961		       && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
5962		/* lwax -> lwa.  */
5963		insn = (58 << 26) | 2;
5964	      else
5965		abort ();
5966	      insn |= rtra;
5967	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5968	      r_type = R_PPC_TPREL16_LO;
5969	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5970
5971	      /* Was PPC_TLS which sits on insn boundary, now
5972		 PPC_TPREL16_LO which is at low-order half-word.  */
5973	      rel->r_offset += d_offset;
5974	    }
5975	  break;
5976
5977	case R_PPC_GOT_TLSGD16_HI:
5978	case R_PPC_GOT_TLSGD16_HA:
5979	  tls_gd = TLS_TPRELGD;
5980	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5981	    goto tls_gdld_hi;
5982	  break;
5983
5984	case R_PPC_GOT_TLSLD16_HI:
5985	case R_PPC_GOT_TLSLD16_HA:
5986	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5987	    {
5988	    tls_gdld_hi:
5989	      if ((tls_mask & tls_gd) != 0)
5990		r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5991			  + R_PPC_GOT_TPREL16);
5992	      else
5993		{
5994		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
5995		  rel->r_offset -= d_offset;
5996		  r_type = R_PPC_NONE;
5997		}
5998	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5999	    }
6000	  break;
6001
6002	case R_PPC_GOT_TLSGD16:
6003	case R_PPC_GOT_TLSGD16_LO:
6004	  tls_gd = TLS_TPRELGD;
6005	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
6006	    goto tls_ldgd_opt;
6007	  break;
6008
6009	case R_PPC_GOT_TLSLD16:
6010	case R_PPC_GOT_TLSLD16_LO:
6011	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
6012	    {
6013	      unsigned int insn1, insn2;
6014	      bfd_vma offset;
6015
6016	    tls_ldgd_opt:
6017	      offset = (bfd_vma) -1;
6018	      /* If not using the newer R_PPC_TLSGD/LD to mark
6019		 __tls_get_addr calls, we must trust that the call
6020		 stays with its arg setup insns, ie. that the next
6021		 reloc is the __tls_get_addr call associated with
6022		 the current reloc.  Edit both insns.  */
6023	      if (input_section->has_tls_get_addr_call
6024		  && rel + 1 < relend
6025		  && branch_reloc_hash_match (input_bfd, rel + 1,
6026					      htab->tls_get_addr))
6027		offset = rel[1].r_offset;
6028	      if ((tls_mask & tls_gd) != 0)
6029		{
6030		  /* IE */
6031		  insn1 = bfd_get_32 (output_bfd,
6032				      contents + rel->r_offset - d_offset);
6033		  insn1 &= (1 << 26) - 1;
6034		  insn1 |= 32 << 26;	/* lwz */
6035		  if (offset != (bfd_vma) -1)
6036		    {
6037		      rel[1].r_info
6038			= ELF32_R_INFO (ELF32_R_SYM (rel[1].r_info),
6039					R_PPC_NONE);
6040		      insn2 = 0x7c631214;	/* add 3,3,2 */
6041		      bfd_put_32 (output_bfd, insn2, contents + offset);
6042		    }
6043		  r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6044			    + R_PPC_GOT_TPREL16);
6045		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6046		}
6047	      else
6048		{
6049		  /* LE */
6050		  insn1 = 0x3c620000;	/* addis 3,2,0 */
6051		  if (tls_gd == 0)
6052		    {
6053		      /* Was an LD reloc.  */
6054		      for (r_symndx = 0;
6055			   r_symndx < symtab_hdr->sh_info;
6056			   r_symndx++)
6057			if (local_sections[r_symndx] == sec)
6058			  break;
6059		      if (r_symndx >= symtab_hdr->sh_info)
6060			r_symndx = 0;
6061		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
6062		      if (r_symndx != 0)
6063			rel->r_addend -= (local_syms[r_symndx].st_value
6064					  + sec->output_offset
6065					  + sec->output_section->vma);
6066		    }
6067		  r_type = R_PPC_TPREL16_HA;
6068		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6069		  if (offset != (bfd_vma) -1)
6070		    {
6071		      rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
6072		      rel[1].r_offset = offset + d_offset;
6073		      rel[1].r_addend = rel->r_addend;
6074		      insn2 = 0x38630000;	/* addi 3,3,0 */
6075		      bfd_put_32 (output_bfd, insn2, contents + offset);
6076		    }
6077		}
6078	      bfd_put_32 (output_bfd, insn1,
6079			  contents + rel->r_offset - d_offset);
6080	      if (tls_gd == 0)
6081		{
6082		  /* We changed the symbol on an LD reloc.  Start over
6083		     in order to get h, sym, sec etc. right.  */
6084		  rel--;
6085		  continue;
6086		}
6087	    }
6088	  break;
6089
6090	case R_PPC_TLSGD:
6091	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
6092	    {
6093	      unsigned int insn2;
6094	      bfd_vma offset = rel->r_offset;
6095
6096	      if ((tls_mask & TLS_TPRELGD) != 0)
6097		{
6098		  /* IE */
6099		  r_type = R_PPC_NONE;
6100		  insn2 = 0x7c631214;	/* add 3,3,2 */
6101		}
6102	      else
6103		{
6104		  /* LE */
6105		  r_type = R_PPC_TPREL16_LO;
6106		  rel->r_offset += d_offset;
6107		  insn2 = 0x38630000;	/* addi 3,3,0 */
6108		}
6109	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6110	      bfd_put_32 (output_bfd, insn2, contents + offset);
6111	      /* Zap the reloc on the _tls_get_addr call too.  */
6112	      BFD_ASSERT (offset == rel[1].r_offset);
6113	      rel[1].r_info = ELF32_R_INFO (ELF32_R_SYM (rel[1].r_info),
6114					    R_PPC_NONE);
6115	    }
6116	  break;
6117
6118	case R_PPC_TLSLD:
6119	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
6120	    {
6121	      unsigned int insn2;
6122
6123	      for (r_symndx = 0;
6124		   r_symndx < symtab_hdr->sh_info;
6125		   r_symndx++)
6126		if (local_sections[r_symndx] == sec)
6127		  break;
6128	      if (r_symndx >= symtab_hdr->sh_info)
6129		r_symndx = 0;
6130	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
6131	      if (r_symndx != 0)
6132		rel->r_addend -= (local_syms[r_symndx].st_value
6133				  + sec->output_offset
6134				  + sec->output_section->vma);
6135
6136	      rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
6137	      rel->r_offset += d_offset;
6138	      insn2 = 0x38630000;	/* addi 3,3,0 */
6139	      bfd_put_32 (output_bfd, insn2,
6140			  contents + rel->r_offset - d_offset);
6141	      /* Zap the reloc on the _tls_get_addr call too.  */
6142	      BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
6143	      rel[1].r_info = ELF32_R_INFO (ELF32_R_SYM (rel[1].r_info),
6144					    R_PPC_NONE);
6145	      rel--;
6146	      continue;
6147	    }
6148	  break;
6149	}
6150
6151      /* Handle other relocations that tweak non-addend part of insn.  */
6152      branch_bit = 0;
6153      switch (r_type)
6154	{
6155	default:
6156	  break;
6157
6158	  /* Branch taken prediction relocations.  */
6159	case R_PPC_ADDR14_BRTAKEN:
6160	case R_PPC_REL14_BRTAKEN:
6161	  branch_bit = BRANCH_PREDICT_BIT;
6162	  /* Fall thru */
6163
6164	  /* Branch not taken prediction relocations.  */
6165	case R_PPC_ADDR14_BRNTAKEN:
6166	case R_PPC_REL14_BRNTAKEN:
6167	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6168	  insn &= ~BRANCH_PREDICT_BIT;
6169	  insn |= branch_bit;
6170
6171	  from = (rel->r_offset
6172		  + input_section->output_offset
6173		  + input_section->output_section->vma);
6174
6175	  /* Invert 'y' bit if not the default.  */
6176	  if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
6177	    insn ^= BRANCH_PREDICT_BIT;
6178
6179	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6180	  break;
6181	}
6182
6183      addend = rel->r_addend;
6184      tls_type = 0;
6185      howto = NULL;
6186      if (r_type < R_PPC_max)
6187	howto = ppc_elf_howto_table[r_type];
6188      switch ((int) r_type)
6189	{
6190	default:
6191	  (*_bfd_error_handler)
6192	    (_("%B: unknown relocation type %d for symbol %s"),
6193	     input_bfd, (int) r_type, sym_name);
6194
6195	  bfd_set_error (bfd_error_bad_value);
6196	  ret = FALSE;
6197	  continue;
6198
6199	case R_PPC_NONE:
6200	case R_PPC_TLS:
6201	case R_PPC_TLSGD:
6202	case R_PPC_TLSLD:
6203	case R_PPC_EMB_MRKREF:
6204	case R_PPC_GNU_VTINHERIT:
6205	case R_PPC_GNU_VTENTRY:
6206	  continue;
6207
6208	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
6209	     address in the GOT as relocation value instead of the
6210	     symbol's value itself.  Also, create a GOT entry for the
6211	     symbol and put the symbol value there.  */
6212	case R_PPC_GOT_TLSGD16:
6213	case R_PPC_GOT_TLSGD16_LO:
6214	case R_PPC_GOT_TLSGD16_HI:
6215	case R_PPC_GOT_TLSGD16_HA:
6216	  tls_type = TLS_TLS | TLS_GD;
6217	  goto dogot;
6218
6219	case R_PPC_GOT_TLSLD16:
6220	case R_PPC_GOT_TLSLD16_LO:
6221	case R_PPC_GOT_TLSLD16_HI:
6222	case R_PPC_GOT_TLSLD16_HA:
6223	  tls_type = TLS_TLS | TLS_LD;
6224	  goto dogot;
6225
6226	case R_PPC_GOT_TPREL16:
6227	case R_PPC_GOT_TPREL16_LO:
6228	case R_PPC_GOT_TPREL16_HI:
6229	case R_PPC_GOT_TPREL16_HA:
6230	  tls_type = TLS_TLS | TLS_TPREL;
6231	  goto dogot;
6232
6233	case R_PPC_GOT_DTPREL16:
6234	case R_PPC_GOT_DTPREL16_LO:
6235	case R_PPC_GOT_DTPREL16_HI:
6236	case R_PPC_GOT_DTPREL16_HA:
6237	  tls_type = TLS_TLS | TLS_DTPREL;
6238	  goto dogot;
6239
6240	case R_PPC_GOT16:
6241	case R_PPC_GOT16_LO:
6242	case R_PPC_GOT16_HI:
6243	case R_PPC_GOT16_HA:
6244	  tls_mask = 0;
6245	dogot:
6246	  {
6247	    /* Relocation is to the entry for this symbol in the global
6248	       offset table.  */
6249	    bfd_vma off;
6250	    bfd_vma *offp;
6251	    unsigned long indx;
6252
6253	    if (htab->got == NULL)
6254	      abort ();
6255
6256	    indx = 0;
6257	    if (tls_type == (TLS_TLS | TLS_LD)
6258		&& (h == NULL
6259		    || !h->def_dynamic))
6260	      offp = &htab->tlsld_got.offset;
6261	    else if (h != NULL)
6262	      {
6263		bfd_boolean dyn;
6264		dyn = htab->elf.dynamic_sections_created;
6265		if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6266		    || (info->shared
6267			&& SYMBOL_REFERENCES_LOCAL (info, h)))
6268		  /* This is actually a static link, or it is a
6269		     -Bsymbolic link and the symbol is defined
6270		     locally, or the symbol was forced to be local
6271		     because of a version file.  */
6272		  ;
6273		else
6274		  {
6275		    indx = h->dynindx;
6276		    unresolved_reloc = FALSE;
6277		  }
6278		offp = &h->got.offset;
6279	      }
6280	    else
6281	      {
6282		if (local_got_offsets == NULL)
6283		  abort ();
6284		offp = &local_got_offsets[r_symndx];
6285	      }
6286
6287	    /* The offset must always be a multiple of 4.  We use the
6288	       least significant bit to record whether we have already
6289	       processed this entry.  */
6290	    off = *offp;
6291	    if ((off & 1) != 0)
6292	      off &= ~1;
6293	    else
6294	      {
6295		unsigned int tls_m = (tls_mask
6296				      & (TLS_LD | TLS_GD | TLS_DTPREL
6297					 | TLS_TPREL | TLS_TPRELGD));
6298
6299		if (offp == &htab->tlsld_got.offset)
6300		  tls_m = TLS_LD;
6301		else if (h == NULL
6302			 || !h->def_dynamic)
6303		  tls_m &= ~TLS_LD;
6304
6305		/* We might have multiple got entries for this sym.
6306		   Initialize them all.  */
6307		do
6308		  {
6309		    int tls_ty = 0;
6310
6311		    if ((tls_m & TLS_LD) != 0)
6312		      {
6313			tls_ty = TLS_TLS | TLS_LD;
6314			tls_m &= ~TLS_LD;
6315		      }
6316		    else if ((tls_m & TLS_GD) != 0)
6317		      {
6318			tls_ty = TLS_TLS | TLS_GD;
6319			tls_m &= ~TLS_GD;
6320		      }
6321		    else if ((tls_m & TLS_DTPREL) != 0)
6322		      {
6323			tls_ty = TLS_TLS | TLS_DTPREL;
6324			tls_m &= ~TLS_DTPREL;
6325		      }
6326		    else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
6327		      {
6328			tls_ty = TLS_TLS | TLS_TPREL;
6329			tls_m = 0;
6330		      }
6331
6332		    /* Generate relocs for the dynamic linker.  */
6333		    if ((info->shared || indx != 0)
6334			&& (h == NULL
6335			    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6336			    || h->root.type != bfd_link_hash_undefweak))
6337		      {
6338			outrel.r_offset = (htab->got->output_section->vma
6339					   + htab->got->output_offset
6340					   + off);
6341			outrel.r_addend = 0;
6342			if (tls_ty & (TLS_LD | TLS_GD))
6343			  {
6344			    outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
6345			    if (tls_ty == (TLS_TLS | TLS_GD))
6346			      {
6347				loc = htab->relgot->contents;
6348				loc += (htab->relgot->reloc_count++
6349					* sizeof (Elf32_External_Rela));
6350				bfd_elf32_swap_reloca_out (output_bfd,
6351							   &outrel, loc);
6352				outrel.r_offset += 4;
6353				outrel.r_info
6354				  = ELF32_R_INFO (indx, R_PPC_DTPREL32);
6355			      }
6356			  }
6357			else if (tls_ty == (TLS_TLS | TLS_DTPREL))
6358			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
6359			else if (tls_ty == (TLS_TLS | TLS_TPREL))
6360			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
6361			else if (indx == 0)
6362			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
6363			else
6364			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
6365			if (indx == 0)
6366			  {
6367			    outrel.r_addend += relocation;
6368			    if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
6369			      outrel.r_addend -= htab->elf.tls_sec->vma;
6370			  }
6371			loc = htab->relgot->contents;
6372			loc += (htab->relgot->reloc_count++
6373				* sizeof (Elf32_External_Rela));
6374			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6375		      }
6376
6377		    /* Init the .got section contents if we're not
6378		       emitting a reloc.  */
6379		    else
6380		      {
6381			bfd_vma value = relocation;
6382
6383			if (tls_ty == (TLS_TLS | TLS_LD))
6384			  value = 1;
6385			else if (tls_ty != 0)
6386			  {
6387			    value -= htab->elf.tls_sec->vma + DTP_OFFSET;
6388			    if (tls_ty == (TLS_TLS | TLS_TPREL))
6389			      value += DTP_OFFSET - TP_OFFSET;
6390
6391			    if (tls_ty == (TLS_TLS | TLS_GD))
6392			      {
6393				bfd_put_32 (output_bfd, value,
6394					    htab->got->contents + off + 4);
6395				value = 1;
6396			      }
6397			  }
6398			bfd_put_32 (output_bfd, value,
6399				    htab->got->contents + off);
6400		      }
6401
6402		    off += 4;
6403		    if (tls_ty & (TLS_LD | TLS_GD))
6404		      off += 4;
6405		  }
6406		while (tls_m != 0);
6407
6408		off = *offp;
6409		*offp = off | 1;
6410	      }
6411
6412	    if (off >= (bfd_vma) -2)
6413	      abort ();
6414
6415	    if ((tls_type & TLS_TLS) != 0)
6416	      {
6417		if (tls_type != (TLS_TLS | TLS_LD))
6418		  {
6419		    if ((tls_mask & TLS_LD) != 0
6420			&& !(h == NULL
6421			     || !h->def_dynamic))
6422		      off += 8;
6423		    if (tls_type != (TLS_TLS | TLS_GD))
6424		      {
6425			if ((tls_mask & TLS_GD) != 0)
6426			  off += 8;
6427			if (tls_type != (TLS_TLS | TLS_DTPREL))
6428			  {
6429			    if ((tls_mask & TLS_DTPREL) != 0)
6430			      off += 4;
6431			  }
6432		      }
6433		  }
6434	      }
6435
6436	    relocation = htab->got->output_offset + off;
6437	    relocation -= htab->elf.hgot->root.u.def.value;
6438
6439	    /* Addends on got relocations don't make much sense.
6440	       x+off@got is actually x@got+off, and since the got is
6441	       generated by a hash table traversal, the value in the
6442	       got at entry m+n bears little relation to the entry m.  */
6443	    if (addend != 0)
6444	      (*_bfd_error_handler)
6445		(_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
6446		 input_bfd,
6447		 input_section,
6448		 (long) rel->r_offset,
6449		 howto->name,
6450		 sym_name);
6451	  }
6452	break;
6453
6454	/* Relocations that need no special processing.  */
6455	case R_PPC_LOCAL24PC:
6456	  /* It makes no sense to point a local relocation
6457	     at a symbol not in this object.  */
6458	  if (unresolved_reloc)
6459	    {
6460	      if (! (*info->callbacks->undefined_symbol) (info,
6461							  h->root.root.string,
6462							  input_bfd,
6463							  input_section,
6464							  rel->r_offset,
6465							  TRUE))
6466		return FALSE;
6467	      continue;
6468	    }
6469	  break;
6470
6471	case R_PPC_DTPREL16:
6472	case R_PPC_DTPREL16_LO:
6473	case R_PPC_DTPREL16_HI:
6474	case R_PPC_DTPREL16_HA:
6475	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
6476	  break;
6477
6478	  /* Relocations that may need to be propagated if this is a shared
6479	     object.  */
6480	case R_PPC_TPREL16:
6481	case R_PPC_TPREL16_LO:
6482	case R_PPC_TPREL16_HI:
6483	case R_PPC_TPREL16_HA:
6484	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
6485	  /* The TPREL16 relocs shouldn't really be used in shared
6486	     libs as they will result in DT_TEXTREL being set, but
6487	     support them anyway.  */
6488	  goto dodyn;
6489
6490	case R_PPC_TPREL32:
6491	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
6492	  goto dodyn;
6493
6494	case R_PPC_DTPREL32:
6495	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
6496	  goto dodyn;
6497
6498	case R_PPC_DTPMOD32:
6499	  relocation = 1;
6500	  addend = 0;
6501	  goto dodyn;
6502
6503	case R_PPC_REL16:
6504	case R_PPC_REL16_LO:
6505	case R_PPC_REL16_HI:
6506	case R_PPC_REL16_HA:
6507	  break;
6508
6509	case R_PPC_REL24:
6510	case R_PPC_REL32:
6511	case R_PPC_REL14:
6512	case R_PPC_REL14_BRTAKEN:
6513	case R_PPC_REL14_BRNTAKEN:
6514	  /* If these relocations are not to a named symbol, they can be
6515	     handled right here, no need to bother the dynamic linker.  */
6516	  if (SYMBOL_REFERENCES_LOCAL (info, h)
6517	      || h == htab->elf.hgot)
6518	    break;
6519	  /* fall through */
6520
6521	  /* Relocations that always need to be propagated if this is a shared
6522	     object.  */
6523	case R_PPC_ADDR32:
6524	case R_PPC_ADDR24:
6525	case R_PPC_ADDR16:
6526	case R_PPC_ADDR16_LO:
6527	case R_PPC_ADDR16_HI:
6528	case R_PPC_ADDR16_HA:
6529	case R_PPC_ADDR14:
6530	case R_PPC_ADDR14_BRTAKEN:
6531	case R_PPC_ADDR14_BRNTAKEN:
6532	case R_PPC_UADDR32:
6533	case R_PPC_UADDR16:
6534	dodyn:
6535	  if ((input_section->flags & SEC_ALLOC) == 0)
6536	    break;
6537	  /* Fall thru.  */
6538
6539	  if ((info->shared
6540	       && (h == NULL
6541		   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6542		   || h->root.type != bfd_link_hash_undefweak)
6543	       && (must_be_dyn_reloc (info, r_type)
6544		   || !SYMBOL_CALLS_LOCAL (info, h)))
6545	      || (ELIMINATE_COPY_RELOCS
6546		  && !info->shared
6547		  && h != NULL
6548		  && h->dynindx != -1
6549		  && !h->non_got_ref
6550		  && h->def_dynamic
6551		  && !h->def_regular))
6552	    {
6553	      int skip;
6554
6555#ifdef DEBUG
6556	      fprintf (stderr, "ppc_elf_relocate_section needs to "
6557		       "create relocation for %s\n",
6558		       (h && h->root.root.string
6559			? h->root.root.string : "<unknown>"));
6560#endif
6561
6562	      /* When generating a shared object, these relocations
6563		 are copied into the output file to be resolved at run
6564		 time.  */
6565	      if (sreloc == NULL)
6566		{
6567		  const char *name;
6568
6569		  name = (bfd_elf_string_from_elf_section
6570			  (input_bfd,
6571			   elf_elfheader (input_bfd)->e_shstrndx,
6572			   elf_section_data (input_section)->rel_hdr.sh_name));
6573		  if (name == NULL)
6574		    return FALSE;
6575
6576		  BFD_ASSERT (CONST_STRNEQ (name, ".rela")
6577			      && strcmp (bfd_get_section_name (input_bfd,
6578							       input_section),
6579					 name + 5) == 0);
6580
6581		  sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
6582		  BFD_ASSERT (sreloc != NULL);
6583		}
6584
6585	      skip = 0;
6586
6587	      outrel.r_offset =
6588		_bfd_elf_section_offset (output_bfd, info, input_section,
6589					 rel->r_offset);
6590	      if (outrel.r_offset == (bfd_vma) -1
6591		  || outrel.r_offset == (bfd_vma) -2)
6592		skip = (int) outrel.r_offset;
6593	      outrel.r_offset += (input_section->output_section->vma
6594				  + input_section->output_offset);
6595
6596	      if (skip)
6597		memset (&outrel, 0, sizeof outrel);
6598	      else if (!SYMBOL_REFERENCES_LOCAL (info, h))
6599		{
6600		  unresolved_reloc = FALSE;
6601		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
6602		  outrel.r_addend = rel->r_addend;
6603		}
6604	      else
6605		{
6606		  outrel.r_addend = relocation + rel->r_addend;
6607
6608		  if (r_type == R_PPC_ADDR32)
6609		    outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
6610		  else
6611		    {
6612		      long indx = 0;
6613
6614		      if (r_symndx == 0 || bfd_is_abs_section (sec))
6615			;
6616		      else if (sec == NULL || sec->owner == NULL)
6617			{
6618			  bfd_set_error (bfd_error_bad_value);
6619			  return FALSE;
6620			}
6621		      else
6622			{
6623			  asection *osec;
6624
6625			  /* We are turning this relocation into one
6626			     against a section symbol.  It would be
6627			     proper to subtract the symbol's value,
6628			     osec->vma, from the emitted reloc addend,
6629			     but ld.so expects buggy relocs.  */
6630			  osec = sec->output_section;
6631			  indx = elf_section_data (osec)->dynindx;
6632			  if (indx == 0)
6633			    {
6634			      osec = htab->elf.text_index_section;
6635			      indx = elf_section_data (osec)->dynindx;
6636			    }
6637			  BFD_ASSERT (indx != 0);
6638#ifdef DEBUG
6639			  if (indx == 0)
6640			    printf ("indx=%ld section=%s flags=%08x name=%s\n",
6641				    indx, osec->name, osec->flags,
6642				    h->root.root.string);
6643#endif
6644			}
6645
6646		      outrel.r_info = ELF32_R_INFO (indx, r_type);
6647		    }
6648		}
6649
6650	      loc = sreloc->contents;
6651	      loc += sreloc->reloc_count++ * sizeof (