elf32-ppc.c revision 99461
1/* PowerPC-specific support for 32-bit ELF
2   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3   Free Software Foundation, Inc.
4   Written by Ian Lance Taylor, Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22/* This file is based on a preliminary PowerPC ELF ABI.  The
23   information may not match the final PowerPC ELF ABI.  It includes
24   suggestions from the in-progress Embedded PowerPC ABI, and that
25   information may also not match.  */
26
27#include "bfd.h"
28#include "sysdep.h"
29#include "bfdlink.h"
30#include "libbfd.h"
31#include "elf-bfd.h"
32#include "elf/ppc.h"
33
34#define USE_RELA		/* we want RELA relocations, not REL */
35
36static reloc_howto_type *ppc_elf_reloc_type_lookup
37  PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
38static void ppc_elf_info_to_howto
39  PARAMS ((bfd *abfd, arelent *cache_ptr, Elf32_Internal_Rela *dst));
40static void ppc_elf_howto_init PARAMS ((void));
41static int ppc_elf_sort_rela PARAMS ((const PTR, const PTR));
42static boolean ppc_elf_relax_section
43  PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
44static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
45  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46static boolean ppc_elf_set_private_flags PARAMS ((bfd *, flagword));
47static boolean ppc_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
48
49static int ppc_elf_additional_program_headers PARAMS ((bfd *));
50static boolean ppc_elf_modify_segment_map PARAMS ((bfd *));
51
52static asection *ppc_elf_create_got
53  PARAMS ((bfd *, struct bfd_link_info *));
54static boolean ppc_elf_create_dynamic_sections
55  PARAMS ((bfd *, struct bfd_link_info *));
56
57static boolean ppc_elf_section_from_shdr PARAMS ((bfd *,
58						  Elf32_Internal_Shdr *,
59						  char *));
60static boolean ppc_elf_fake_sections
61  PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
62
63static elf_linker_section_t *ppc_elf_create_linker_section
64  PARAMS ((bfd *abfd,
65	   struct bfd_link_info *info,
66	   enum elf_linker_section_enum));
67
68static boolean ppc_elf_check_relocs PARAMS ((bfd *,
69					     struct bfd_link_info *,
70					     asection *,
71					     const Elf_Internal_Rela *));
72
73static asection * ppc_elf_gc_mark_hook PARAMS ((bfd *abfd,
74						struct bfd_link_info *info,
75						Elf_Internal_Rela *rel,
76						struct elf_link_hash_entry *h,
77						Elf_Internal_Sym *sym));
78
79static boolean ppc_elf_gc_sweep_hook PARAMS ((bfd *abfd,
80					      struct bfd_link_info *info,
81					      asection *sec,
82					      const Elf_Internal_Rela *relocs));
83
84static boolean ppc_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
85						      struct elf_link_hash_entry *));
86
87static boolean ppc_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
88
89static boolean ppc_elf_relocate_section PARAMS ((bfd *,
90						 struct bfd_link_info *info,
91						 bfd *,
92						 asection *,
93						 bfd_byte *,
94						 Elf_Internal_Rela *relocs,
95						 Elf_Internal_Sym *local_syms,
96						 asection **));
97
98static boolean ppc_elf_add_symbol_hook  PARAMS ((bfd *,
99						 struct bfd_link_info *,
100						 const Elf_Internal_Sym *,
101						 const char **,
102						 flagword *,
103						 asection **,
104						 bfd_vma *));
105
106static boolean ppc_elf_finish_dynamic_symbol PARAMS ((bfd *,
107						      struct bfd_link_info *,
108						      struct elf_link_hash_entry *,
109						      Elf_Internal_Sym *));
110
111static boolean ppc_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
112static enum elf_reloc_type_class ppc_elf_reloc_type_class
113  PARAMS ((const Elf_Internal_Rela *));
114static boolean ppc_elf_grok_prstatus
115  PARAMS ((bfd *abfd, Elf_Internal_Note *note));
116static boolean ppc_elf_grok_psinfo
117  PARAMS ((bfd *abfd, Elf_Internal_Note *note));
118
119#define BRANCH_PREDICT_BIT 0x200000		/* branch prediction bit for branch taken relocs */
120#define RA_REGISTER_MASK 0x001f0000		/* mask to set RA in memory instructions */
121#define RA_REGISTER_SHIFT 16			/* value to shift register by to insert RA */
122
123/* The name of the dynamic interpreter.  This is put in the .interp
124   section.  */
125
126#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
127
128/* The size in bytes of an entry in the procedure linkage table.  */
129#define PLT_ENTRY_SIZE 12
130/* The initial size of the plt reserved for the dynamic linker.  */
131#define PLT_INITIAL_ENTRY_SIZE 72
132/* The size of the gap between entries in the PLT.  */
133#define PLT_SLOT_SIZE 8
134/* The number of single-slot PLT entries (the rest use two slots).  */
135#define PLT_NUM_SINGLE_ENTRIES 8192
136
137/* Will references to this symbol always reference the symbol
138   in this object?  */
139#define SYMBOL_REFERENCES_LOCAL(INFO, H)				\
140  ((! INFO->shared							\
141    || INFO->symbolic							\
142    || H->dynindx == -1							\
143    || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL			\
144    || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN)			\
145   && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
146
147/* Will _calls_ to this symbol always call the version in this object?  */
148#define SYMBOL_CALLS_LOCAL(INFO, H)				\
149  ((! INFO->shared							\
150    || INFO->symbolic							\
151    || H->dynindx == -1							\
152    || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT)			\
153   && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
154
155static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
156
157static reloc_howto_type ppc_elf_howto_raw[] = {
158  /* This reloc does nothing.  */
159  HOWTO (R_PPC_NONE,		/* type */
160	 0,			/* rightshift */
161	 2,			/* size (0 = byte, 1 = short, 2 = long) */
162	 32,			/* bitsize */
163	 false,			/* pc_relative */
164	 0,			/* bitpos */
165	 complain_overflow_bitfield, /* complain_on_overflow */
166	 bfd_elf_generic_reloc,	/* special_function */
167	 "R_PPC_NONE",		/* name */
168	 false,			/* partial_inplace */
169	 0,			/* src_mask */
170	 0,			/* dst_mask */
171	 false),		/* pcrel_offset */
172
173  /* A standard 32 bit relocation.  */
174  HOWTO (R_PPC_ADDR32,		/* type */
175	 0,			/* rightshift */
176	 2,			/* size (0 = byte, 1 = short, 2 = long) */
177	 32,			/* bitsize */
178	 false,			/* pc_relative */
179	 0,			/* bitpos */
180	 complain_overflow_bitfield, /* complain_on_overflow */
181	 bfd_elf_generic_reloc,	/* special_function */
182	 "R_PPC_ADDR32",	/* name */
183	 false,			/* partial_inplace */
184	 0,			/* src_mask */
185	 0xffffffff,		/* dst_mask */
186	 false),		/* pcrel_offset */
187
188  /* An absolute 26 bit branch; the lower two bits must be zero.
189     FIXME: we don't check that, we just clear them.  */
190  HOWTO (R_PPC_ADDR24,		/* type */
191	 0,			/* rightshift */
192	 2,			/* size (0 = byte, 1 = short, 2 = long) */
193	 26,			/* bitsize */
194	 false,			/* pc_relative */
195	 0,			/* bitpos */
196	 complain_overflow_bitfield, /* complain_on_overflow */
197	 bfd_elf_generic_reloc,	/* special_function */
198	 "R_PPC_ADDR24",	/* name */
199	 false,			/* partial_inplace */
200	 0,			/* src_mask */
201	 0x3fffffc,		/* dst_mask */
202	 false),		/* pcrel_offset */
203
204  /* A standard 16 bit relocation.  */
205  HOWTO (R_PPC_ADDR16,		/* type */
206	 0,			/* rightshift */
207	 1,			/* size (0 = byte, 1 = short, 2 = long) */
208	 16,			/* bitsize */
209	 false,			/* pc_relative */
210	 0,			/* bitpos */
211	 complain_overflow_bitfield, /* complain_on_overflow */
212	 bfd_elf_generic_reloc,	/* special_function */
213	 "R_PPC_ADDR16",	/* name */
214	 false,			/* partial_inplace */
215	 0,			/* src_mask */
216	 0xffff,		/* dst_mask */
217	 false),		/* pcrel_offset */
218
219  /* A 16 bit relocation without overflow.  */
220  HOWTO (R_PPC_ADDR16_LO,	/* type */
221	 0,			/* rightshift */
222	 1,			/* size (0 = byte, 1 = short, 2 = long) */
223	 16,			/* bitsize */
224	 false,			/* pc_relative */
225	 0,			/* bitpos */
226	 complain_overflow_dont,/* complain_on_overflow */
227	 bfd_elf_generic_reloc,	/* special_function */
228	 "R_PPC_ADDR16_LO",	/* name */
229	 false,			/* partial_inplace */
230	 0,			/* src_mask */
231	 0xffff,		/* dst_mask */
232	 false),		/* pcrel_offset */
233
234  /* The high order 16 bits of an address.  */
235  HOWTO (R_PPC_ADDR16_HI,	/* type */
236	 16,			/* rightshift */
237	 1,			/* size (0 = byte, 1 = short, 2 = long) */
238	 16,			/* bitsize */
239	 false,			/* pc_relative */
240	 0,			/* bitpos */
241	 complain_overflow_dont, /* complain_on_overflow */
242	 bfd_elf_generic_reloc,	/* special_function */
243	 "R_PPC_ADDR16_HI",	/* name */
244	 false,			/* partial_inplace */
245	 0,			/* src_mask */
246	 0xffff,		/* dst_mask */
247	 false),		/* pcrel_offset */
248
249  /* The high order 16 bits of an address, plus 1 if the contents of
250     the low 16 bits, treated as a signed number, is negative.  */
251  HOWTO (R_PPC_ADDR16_HA,	/* type */
252	 16,			/* rightshift */
253	 1,			/* size (0 = byte, 1 = short, 2 = long) */
254	 16,			/* bitsize */
255	 false,			/* pc_relative */
256	 0,			/* bitpos */
257	 complain_overflow_dont, /* complain_on_overflow */
258	 ppc_elf_addr16_ha_reloc, /* special_function */
259	 "R_PPC_ADDR16_HA",	/* name */
260	 false,			/* partial_inplace */
261	 0,			/* src_mask */
262	 0xffff,		/* dst_mask */
263	 false),		/* pcrel_offset */
264
265  /* An absolute 16 bit branch; the lower two bits must be zero.
266     FIXME: we don't check that, we just clear them.  */
267  HOWTO (R_PPC_ADDR14,		/* type */
268	 0,			/* rightshift */
269	 2,			/* size (0 = byte, 1 = short, 2 = long) */
270	 16,			/* bitsize */
271	 false,			/* pc_relative */
272	 0,			/* bitpos */
273	 complain_overflow_bitfield, /* complain_on_overflow */
274	 bfd_elf_generic_reloc,	/* special_function */
275	 "R_PPC_ADDR14",	/* name */
276	 false,			/* partial_inplace */
277	 0,			/* src_mask */
278	 0xfffc,		/* dst_mask */
279	 false),		/* pcrel_offset */
280
281  /* An absolute 16 bit branch, for which bit 10 should be set to
282     indicate that the branch is expected to be taken.	The lower two
283     bits must be zero.  */
284  HOWTO (R_PPC_ADDR14_BRTAKEN,	/* type */
285	 0,			/* rightshift */
286	 2,			/* size (0 = byte, 1 = short, 2 = long) */
287	 16,			/* bitsize */
288	 false,			/* pc_relative */
289	 0,			/* bitpos */
290	 complain_overflow_bitfield, /* complain_on_overflow */
291	 bfd_elf_generic_reloc,	/* special_function */
292	 "R_PPC_ADDR14_BRTAKEN",/* name */
293	 false,			/* partial_inplace */
294	 0,			/* src_mask */
295	 0xfffc,		/* dst_mask */
296	 false),		/* pcrel_offset */
297
298  /* An absolute 16 bit branch, for which bit 10 should be set to
299     indicate that the branch is not expected to be taken.  The lower
300     two bits must be zero.  */
301  HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
302	 0,			/* rightshift */
303	 2,			/* size (0 = byte, 1 = short, 2 = long) */
304	 16,			/* bitsize */
305	 false,			/* pc_relative */
306	 0,			/* bitpos */
307	 complain_overflow_bitfield, /* complain_on_overflow */
308	 bfd_elf_generic_reloc,	/* special_function */
309	 "R_PPC_ADDR14_BRNTAKEN",/* name */
310	 false,			/* partial_inplace */
311	 0,			/* src_mask */
312	 0xfffc,		/* dst_mask */
313	 false),		/* pcrel_offset */
314
315  /* A relative 26 bit branch; the lower two bits must be zero.  */
316  HOWTO (R_PPC_REL24,		/* type */
317	 0,			/* rightshift */
318	 2,			/* size (0 = byte, 1 = short, 2 = long) */
319	 26,			/* bitsize */
320	 true,			/* pc_relative */
321	 0,			/* bitpos */
322	 complain_overflow_signed, /* complain_on_overflow */
323	 bfd_elf_generic_reloc,	/* special_function */
324	 "R_PPC_REL24",		/* name */
325	 false,			/* partial_inplace */
326	 0,			/* src_mask */
327	 0x3fffffc,		/* dst_mask */
328	 true),			/* pcrel_offset */
329
330  /* A relative 16 bit branch; the lower two bits must be zero.  */
331  HOWTO (R_PPC_REL14,		/* type */
332	 0,			/* rightshift */
333	 2,			/* size (0 = byte, 1 = short, 2 = long) */
334	 16,			/* bitsize */
335	 true,			/* pc_relative */
336	 0,			/* bitpos */
337	 complain_overflow_signed, /* complain_on_overflow */
338	 bfd_elf_generic_reloc,	/* special_function */
339	 "R_PPC_REL14",		/* name */
340	 false,			/* partial_inplace */
341	 0,			/* src_mask */
342	 0xfffc,		/* dst_mask */
343	 true),			/* pcrel_offset */
344
345  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
346     the branch is expected to be taken.  The lower two bits must be
347     zero.  */
348  HOWTO (R_PPC_REL14_BRTAKEN,	/* type */
349	 0,			/* rightshift */
350	 2,			/* size (0 = byte, 1 = short, 2 = long) */
351	 16,			/* bitsize */
352	 true,			/* pc_relative */
353	 0,			/* bitpos */
354	 complain_overflow_signed, /* complain_on_overflow */
355	 bfd_elf_generic_reloc,	/* special_function */
356	 "R_PPC_REL14_BRTAKEN",	/* name */
357	 false,			/* partial_inplace */
358	 0,			/* src_mask */
359	 0xfffc,		/* dst_mask */
360	 true),			/* pcrel_offset */
361
362  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
363     the branch is not expected to be taken.  The lower two bits must
364     be zero.  */
365  HOWTO (R_PPC_REL14_BRNTAKEN,	/* type */
366	 0,			/* rightshift */
367	 2,			/* size (0 = byte, 1 = short, 2 = long) */
368	 16,			/* bitsize */
369	 true,			/* pc_relative */
370	 0,			/* bitpos */
371	 complain_overflow_signed, /* complain_on_overflow */
372	 bfd_elf_generic_reloc,	/* special_function */
373	 "R_PPC_REL14_BRNTAKEN",/* name */
374	 false,			/* partial_inplace */
375	 0,			/* src_mask */
376	 0xfffc,		/* dst_mask */
377	 true),			/* pcrel_offset */
378
379  /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
380     symbol.  */
381  HOWTO (R_PPC_GOT16,		/* type */
382	 0,			/* rightshift */
383	 1,			/* size (0 = byte, 1 = short, 2 = long) */
384	 16,			/* bitsize */
385	 false,			/* pc_relative */
386	 0,			/* bitpos */
387	 complain_overflow_signed, /* complain_on_overflow */
388	 bfd_elf_generic_reloc,	/* special_function */
389	 "R_PPC_GOT16",		/* name */
390	 false,			/* partial_inplace */
391	 0,			/* src_mask */
392	 0xffff,		/* dst_mask */
393	 false),		/* pcrel_offset */
394
395  /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
396     the symbol.  */
397  HOWTO (R_PPC_GOT16_LO,	/* type */
398	 0,			/* rightshift */
399	 1,			/* size (0 = byte, 1 = short, 2 = long) */
400	 16,			/* bitsize */
401	 false,			/* pc_relative */
402	 0,			/* bitpos */
403	 complain_overflow_dont, /* complain_on_overflow */
404	 bfd_elf_generic_reloc,	/* special_function */
405	 "R_PPC_GOT16_LO",	/* name */
406	 false,			/* partial_inplace */
407	 0,			/* src_mask */
408	 0xffff,		/* dst_mask */
409	 false),		/* pcrel_offset */
410
411  /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
412     the symbol.  */
413  HOWTO (R_PPC_GOT16_HI,	/* type */
414	 16,			/* rightshift */
415	 1,			/* size (0 = byte, 1 = short, 2 = long) */
416	 16,			/* bitsize */
417	 false,			/* pc_relative */
418	 0,			/* bitpos */
419	 complain_overflow_bitfield, /* complain_on_overflow */
420	 bfd_elf_generic_reloc,	/* special_function */
421	 "R_PPC_GOT16_HI",	/* name */
422	 false,			/* partial_inplace */
423	 0,			/* src_mask */
424	 0xffff,		/* dst_mask */
425	 false),		 /* pcrel_offset */
426
427  /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
428     the symbol.  */
429  HOWTO (R_PPC_GOT16_HA,	/* type */
430	 16,			/* rightshift */
431	 1,			/* size (0 = byte, 1 = short, 2 = long) */
432	 16,			/* bitsize */
433	 false,			/* pc_relative */
434	 0,			/* bitpos */
435	 complain_overflow_bitfield, /* complain_on_overflow */
436	 ppc_elf_addr16_ha_reloc, /* special_function */
437	 "R_PPC_GOT16_HA",	/* name */
438	 false,			/* partial_inplace */
439	 0,			/* src_mask */
440	 0xffff,		/* dst_mask */
441	 false),		/* pcrel_offset */
442
443  /* Like R_PPC_REL24, but referring to the procedure linkage table
444     entry for the symbol.  */
445  HOWTO (R_PPC_PLTREL24,	/* type */
446	 0,			/* rightshift */
447	 2,			/* size (0 = byte, 1 = short, 2 = long) */
448	 26,			/* bitsize */
449	 true,			/* pc_relative */
450	 0,			/* bitpos */
451	 complain_overflow_signed,  /* complain_on_overflow */
452	 bfd_elf_generic_reloc,	/* special_function */
453	 "R_PPC_PLTREL24",	/* name */
454	 false,			/* partial_inplace */
455	 0,			/* src_mask */
456	 0x3fffffc,		/* dst_mask */
457	 true),			/* pcrel_offset */
458
459  /* This is used only by the dynamic linker.  The symbol should exist
460     both in the object being run and in some shared library.  The
461     dynamic linker copies the data addressed by the symbol from the
462     shared library into the object, because the object being
463     run has to have the data at some particular address.  */
464  HOWTO (R_PPC_COPY,		/* type */
465	 0,			/* rightshift */
466	 2,			/* size (0 = byte, 1 = short, 2 = long) */
467	 32,			/* bitsize */
468	 false,			/* pc_relative */
469	 0,			/* bitpos */
470	 complain_overflow_bitfield, /* complain_on_overflow */
471	 bfd_elf_generic_reloc,	 /* special_function */
472	 "R_PPC_COPY",		/* name */
473	 false,			/* partial_inplace */
474	 0,			/* src_mask */
475	 0,			/* dst_mask */
476	 false),		/* pcrel_offset */
477
478  /* Like R_PPC_ADDR32, but used when setting global offset table
479     entries.  */
480  HOWTO (R_PPC_GLOB_DAT,	/* type */
481	 0,			/* rightshift */
482	 2,			/* size (0 = byte, 1 = short, 2 = long) */
483	 32,			/* bitsize */
484	 false,			/* pc_relative */
485	 0,			/* bitpos */
486	 complain_overflow_bitfield, /* complain_on_overflow */
487	 bfd_elf_generic_reloc,	 /* special_function */
488	 "R_PPC_GLOB_DAT",	/* name */
489	 false,			/* partial_inplace */
490	 0,			/* src_mask */
491	 0xffffffff,		/* dst_mask */
492	 false),		/* pcrel_offset */
493
494  /* Marks a procedure linkage table entry for a symbol.  */
495  HOWTO (R_PPC_JMP_SLOT,	/* type */
496	 0,			/* rightshift */
497	 2,			/* size (0 = byte, 1 = short, 2 = long) */
498	 32,			/* bitsize */
499	 false,			/* pc_relative */
500	 0,			/* bitpos */
501	 complain_overflow_bitfield, /* complain_on_overflow */
502	 bfd_elf_generic_reloc,	 /* special_function */
503	 "R_PPC_JMP_SLOT",	/* name */
504	 false,			/* partial_inplace */
505	 0,			/* src_mask */
506	 0,			/* dst_mask */
507	 false),		/* pcrel_offset */
508
509  /* Used only by the dynamic linker.  When the object is run, this
510     longword is set to the load address of the object, plus the
511     addend.  */
512  HOWTO (R_PPC_RELATIVE,	/* type */
513	 0,			/* rightshift */
514	 2,			/* size (0 = byte, 1 = short, 2 = long) */
515	 32,			/* bitsize */
516	 false,			/* pc_relative */
517	 0,			/* bitpos */
518	 complain_overflow_bitfield, /* complain_on_overflow */
519	 bfd_elf_generic_reloc,	 /* special_function */
520	 "R_PPC_RELATIVE",	/* name */
521	 false,			/* partial_inplace */
522	 0,			/* src_mask */
523	 0xffffffff,		/* dst_mask */
524	 false),		/* pcrel_offset */
525
526  /* Like R_PPC_REL24, but uses the value of the symbol within the
527     object rather than the final value.  Normally used for
528     _GLOBAL_OFFSET_TABLE_.  */
529  HOWTO (R_PPC_LOCAL24PC,	/* type */
530	 0,			/* rightshift */
531	 2,			/* size (0 = byte, 1 = short, 2 = long) */
532	 26,			/* bitsize */
533	 true,			/* pc_relative */
534	 0,			/* bitpos */
535	 complain_overflow_signed, /* complain_on_overflow */
536	 bfd_elf_generic_reloc,	/* special_function */
537	 "R_PPC_LOCAL24PC",	/* name */
538	 false,			/* partial_inplace */
539	 0,			/* src_mask */
540	 0x3fffffc,		/* dst_mask */
541	 true),			/* pcrel_offset */
542
543  /* Like R_PPC_ADDR32, but may be unaligned.  */
544  HOWTO (R_PPC_UADDR32,		/* type */
545	 0,			/* rightshift */
546	 2,			/* size (0 = byte, 1 = short, 2 = long) */
547	 32,			/* bitsize */
548	 false,			/* pc_relative */
549	 0,			/* bitpos */
550	 complain_overflow_bitfield, /* complain_on_overflow */
551	 bfd_elf_generic_reloc,	/* special_function */
552	 "R_PPC_UADDR32",	/* name */
553	 false,			/* partial_inplace */
554	 0,			/* src_mask */
555	 0xffffffff,		/* dst_mask */
556	 false),		/* pcrel_offset */
557
558  /* Like R_PPC_ADDR16, but may be unaligned.  */
559  HOWTO (R_PPC_UADDR16,		/* type */
560	 0,			/* rightshift */
561	 1,			/* size (0 = byte, 1 = short, 2 = long) */
562	 16,			/* bitsize */
563	 false,			/* pc_relative */
564	 0,			/* bitpos */
565	 complain_overflow_bitfield, /* complain_on_overflow */
566	 bfd_elf_generic_reloc,	/* special_function */
567	 "R_PPC_UADDR16",	/* name */
568	 false,			/* partial_inplace */
569	 0,			/* src_mask */
570	 0xffff,		/* dst_mask */
571	 false),		/* pcrel_offset */
572
573  /* 32-bit PC relative */
574  HOWTO (R_PPC_REL32,		/* type */
575	 0,			/* rightshift */
576	 2,			/* size (0 = byte, 1 = short, 2 = long) */
577	 32,			/* bitsize */
578	 true,			/* pc_relative */
579	 0,			/* bitpos */
580	 complain_overflow_bitfield, /* complain_on_overflow */
581	 bfd_elf_generic_reloc,	/* special_function */
582	 "R_PPC_REL32",		/* name */
583	 false,			/* partial_inplace */
584	 0,			/* src_mask */
585	 0xffffffff,		/* dst_mask */
586	 true),			/* pcrel_offset */
587
588  /* 32-bit relocation to the symbol's procedure linkage table.
589     FIXME: not supported.  */
590  HOWTO (R_PPC_PLT32,		/* type */
591	 0,			/* rightshift */
592	 2,			/* size (0 = byte, 1 = short, 2 = long) */
593	 32,			/* bitsize */
594	 false,			/* pc_relative */
595	 0,			/* bitpos */
596	 complain_overflow_bitfield, /* complain_on_overflow */
597	 bfd_elf_generic_reloc,	/* special_function */
598	 "R_PPC_PLT32",		/* name */
599	 false,			/* partial_inplace */
600	 0,			/* src_mask */
601	 0,			/* dst_mask */
602	 false),		/* pcrel_offset */
603
604  /* 32-bit PC relative relocation to the symbol's procedure linkage table.
605     FIXME: not supported.  */
606  HOWTO (R_PPC_PLTREL32,	/* type */
607	 0,			/* rightshift */
608	 2,			/* size (0 = byte, 1 = short, 2 = long) */
609	 32,			/* bitsize */
610	 true,			/* pc_relative */
611	 0,			/* bitpos */
612	 complain_overflow_bitfield, /* complain_on_overflow */
613	 bfd_elf_generic_reloc,	/* special_function */
614	 "R_PPC_PLTREL32",	/* name */
615	 false,			/* partial_inplace */
616	 0,			/* src_mask */
617	 0,			/* dst_mask */
618	 true),			/* pcrel_offset */
619
620  /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
621     the symbol.  */
622  HOWTO (R_PPC_PLT16_LO,	/* type */
623	 0,			/* rightshift */
624	 1,			/* size (0 = byte, 1 = short, 2 = long) */
625	 16,			/* bitsize */
626	 false,			/* pc_relative */
627	 0,			/* bitpos */
628	 complain_overflow_dont, /* complain_on_overflow */
629	 bfd_elf_generic_reloc,	/* special_function */
630	 "R_PPC_PLT16_LO",	/* name */
631	 false,			/* partial_inplace */
632	 0,			/* src_mask */
633	 0xffff,		/* dst_mask */
634	 false),		/* pcrel_offset */
635
636  /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
637     the symbol.  */
638  HOWTO (R_PPC_PLT16_HI,	/* type */
639	 16,			/* rightshift */
640	 1,			/* size (0 = byte, 1 = short, 2 = long) */
641	 16,			/* bitsize */
642	 false,			/* pc_relative */
643	 0,			/* bitpos */
644	 complain_overflow_bitfield, /* complain_on_overflow */
645	 bfd_elf_generic_reloc,	/* special_function */
646	 "R_PPC_PLT16_HI",	/* name */
647	 false,			/* partial_inplace */
648	 0,			/* src_mask */
649	 0xffff,		/* dst_mask */
650	 false),		 /* pcrel_offset */
651
652  /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
653     the symbol.  */
654  HOWTO (R_PPC_PLT16_HA,	/* type */
655	 16,			/* rightshift */
656	 1,			/* size (0 = byte, 1 = short, 2 = long) */
657	 16,			/* bitsize */
658	 false,			/* pc_relative */
659	 0,			/* bitpos */
660	 complain_overflow_bitfield, /* complain_on_overflow */
661	 ppc_elf_addr16_ha_reloc, /* special_function */
662	 "R_PPC_PLT16_HA",	/* name */
663	 false,			/* partial_inplace */
664	 0,			/* src_mask */
665	 0xffff,		/* dst_mask */
666	 false),		/* pcrel_offset */
667
668  /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
669     small data items.  */
670  HOWTO (R_PPC_SDAREL16,	/* type */
671	 0,			/* rightshift */
672	 1,			/* size (0 = byte, 1 = short, 2 = long) */
673	 16,			/* bitsize */
674	 false,			/* pc_relative */
675	 0,			/* bitpos */
676	 complain_overflow_signed, /* complain_on_overflow */
677	 bfd_elf_generic_reloc,	/* special_function */
678	 "R_PPC_SDAREL16",	/* name */
679	 false,			/* partial_inplace */
680	 0,			/* src_mask */
681	 0xffff,		/* dst_mask */
682	 false),		/* pcrel_offset */
683
684  /* 16-bit section relative relocation.  */
685  HOWTO (R_PPC_SECTOFF,		/* type */
686	 0,			/* rightshift */
687	 1,			/* size (0 = byte, 1 = short, 2 = long) */
688	 16,			/* bitsize */
689	 false,			/* pc_relative */
690	 0,			/* bitpos */
691	 complain_overflow_bitfield, /* complain_on_overflow */
692	 bfd_elf_generic_reloc,	/* special_function */
693	 "R_PPC_SECTOFF",	/* name */
694	 false,			/* partial_inplace */
695	 0,			/* src_mask */
696	 0xffff,		/* dst_mask */
697	 false),		/* pcrel_offset */
698
699  /* 16-bit lower half section relative relocation.  */
700  HOWTO (R_PPC_SECTOFF_LO,	  /* type */
701	 0,			/* rightshift */
702	 1,			/* size (0 = byte, 1 = short, 2 = long) */
703	 16,			/* bitsize */
704	 false,			/* pc_relative */
705	 0,			/* bitpos */
706	 complain_overflow_dont, /* complain_on_overflow */
707	 bfd_elf_generic_reloc,	/* special_function */
708	 "R_PPC_SECTOFF_LO",	/* name */
709	 false,			/* partial_inplace */
710	 0,			/* src_mask */
711	 0xffff,		/* dst_mask */
712	 false),		/* pcrel_offset */
713
714  /* 16-bit upper half section relative relocation.  */
715  HOWTO (R_PPC_SECTOFF_HI,	/* type */
716	 16,			/* rightshift */
717	 1,			/* size (0 = byte, 1 = short, 2 = long) */
718	 16,			/* bitsize */
719	 false,			/* pc_relative */
720	 0,			/* bitpos */
721	 complain_overflow_bitfield, /* complain_on_overflow */
722	 bfd_elf_generic_reloc,	/* special_function */
723	 "R_PPC_SECTOFF_HI",	/* name */
724	 false,			/* partial_inplace */
725	 0,			/* src_mask */
726	 0xffff,		/* dst_mask */
727	 false),		 /* pcrel_offset */
728
729  /* 16-bit upper half adjusted section relative relocation.  */
730  HOWTO (R_PPC_SECTOFF_HA,	/* type */
731	 16,			/* rightshift */
732	 1,			/* size (0 = byte, 1 = short, 2 = long) */
733	 16,			/* bitsize */
734	 false,			/* pc_relative */
735	 0,			/* bitpos */
736	 complain_overflow_bitfield, /* complain_on_overflow */
737	 ppc_elf_addr16_ha_reloc, /* special_function */
738	 "R_PPC_SECTOFF_HA",	/* name */
739	 false,			/* partial_inplace */
740	 0,			/* src_mask */
741	 0xffff,		/* dst_mask */
742	 false),		/* pcrel_offset */
743
744  /* The remaining relocs are from the Embedded ELF ABI, and are not
745     in the SVR4 ELF ABI.  */
746
747  /* 32 bit value resulting from the addend minus the symbol */
748  HOWTO (R_PPC_EMB_NADDR32,	/* type */
749	 0,			/* rightshift */
750	 2,			/* size (0 = byte, 1 = short, 2 = long) */
751	 32,			/* bitsize */
752	 false,			/* pc_relative */
753	 0,			/* bitpos */
754	 complain_overflow_bitfield, /* complain_on_overflow */
755	 bfd_elf_generic_reloc,	/* special_function */
756	 "R_PPC_EMB_NADDR32",	/* name */
757	 false,			/* partial_inplace */
758	 0,			/* src_mask */
759	 0xffffffff,		/* dst_mask */
760	 false),		/* pcrel_offset */
761
762  /* 16 bit value resulting from the addend minus the symbol */
763  HOWTO (R_PPC_EMB_NADDR16,	/* type */
764	 0,			/* rightshift */
765	 1,			/* size (0 = byte, 1 = short, 2 = long) */
766	 16,			/* bitsize */
767	 false,			/* pc_relative */
768	 0,			/* bitpos */
769	 complain_overflow_bitfield, /* complain_on_overflow */
770	 bfd_elf_generic_reloc,	/* special_function */
771	 "R_PPC_EMB_NADDR16",	/* name */
772	 false,			/* partial_inplace */
773	 0,			/* src_mask */
774	 0xffff,		/* dst_mask */
775	 false),		/* pcrel_offset */
776
777  /* 16 bit value resulting from the addend minus the symbol */
778  HOWTO (R_PPC_EMB_NADDR16_LO,	/* type */
779	 0,			/* rightshift */
780	 1,			/* size (0 = byte, 1 = short, 2 = long) */
781	 16,			/* 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_EMB_ADDR16_LO",	/* name */
787	 false,			/* partial_inplace */
788	 0,			/* src_mask */
789	 0xffff,		/* dst_mask */
790	 false),		/* pcrel_offset */
791
792  /* The high order 16 bits of the addend minus the symbol */
793  HOWTO (R_PPC_EMB_NADDR16_HI,	/* type */
794	 16,			/* rightshift */
795	 1,			/* size (0 = byte, 1 = short, 2 = long) */
796	 16,			/* bitsize */
797	 false,			/* pc_relative */
798	 0,			/* bitpos */
799	 complain_overflow_dont, /* complain_on_overflow */
800	 bfd_elf_generic_reloc,	/* special_function */
801	 "R_PPC_EMB_NADDR16_HI", /* name */
802	 false,			/* partial_inplace */
803	 0,			/* src_mask */
804	 0xffff,		/* dst_mask */
805	 false),		/* pcrel_offset */
806
807  /* The high order 16 bits of the result of the addend minus the address,
808     plus 1 if the contents of the low 16 bits, treated as a signed number,
809     is negative.  */
810  HOWTO (R_PPC_EMB_NADDR16_HA,	/* type */
811	 16,			/* rightshift */
812	 1,			/* size (0 = byte, 1 = short, 2 = long) */
813	 16,			/* bitsize */
814	 false,			/* pc_relative */
815	 0,			/* bitpos */
816	 complain_overflow_dont, /* complain_on_overflow */
817	 ppc_elf_addr16_ha_reloc, /* special_function */
818	 "R_PPC_EMB_NADDR16_HA", /* name */
819	 false,			/* partial_inplace */
820	 0,			/* src_mask */
821	 0xffff,		/* dst_mask */
822	 false),		/* pcrel_offset */
823
824  /* 16 bit value resulting from allocating a 4 byte word to hold an
825     address in the .sdata section, and returning the offset from
826     _SDA_BASE_ for that relocation */
827  HOWTO (R_PPC_EMB_SDAI16,	/* type */
828	 0,			/* rightshift */
829	 1,			/* size (0 = byte, 1 = short, 2 = long) */
830	 16,			/* bitsize */
831	 false,			/* pc_relative */
832	 0,			/* bitpos */
833	 complain_overflow_bitfield, /* complain_on_overflow */
834	 bfd_elf_generic_reloc,	/* special_function */
835	 "R_PPC_EMB_SDAI16",	/* name */
836	 false,			/* partial_inplace */
837	 0,			/* src_mask */
838	 0xffff,		/* dst_mask */
839	 false),		/* pcrel_offset */
840
841  /* 16 bit value resulting from allocating a 4 byte word to hold an
842     address in the .sdata2 section, and returning the offset from
843     _SDA2_BASE_ for that relocation */
844  HOWTO (R_PPC_EMB_SDA2I16,	/* type */
845	 0,			/* rightshift */
846	 1,			/* size (0 = byte, 1 = short, 2 = long) */
847	 16,			/* bitsize */
848	 false,			/* pc_relative */
849	 0,			/* bitpos */
850	 complain_overflow_bitfield, /* complain_on_overflow */
851	 bfd_elf_generic_reloc,	/* special_function */
852	 "R_PPC_EMB_SDA2I16",	/* name */
853	 false,			/* partial_inplace */
854	 0,			/* src_mask */
855	 0xffff,		/* dst_mask */
856	 false),		/* pcrel_offset */
857
858  /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
859     small data items.	 */
860  HOWTO (R_PPC_EMB_SDA2REL,	/* type */
861	 0,			/* rightshift */
862	 1,			/* size (0 = byte, 1 = short, 2 = long) */
863	 16,			/* bitsize */
864	 false,			/* pc_relative */
865	 0,			/* bitpos */
866	 complain_overflow_signed, /* complain_on_overflow */
867	 bfd_elf_generic_reloc,	/* special_function */
868	 "R_PPC_EMB_SDA2REL",	/* name */
869	 false,			/* partial_inplace */
870	 0,			/* src_mask */
871	 0xffff,		/* dst_mask */
872	 false),		/* pcrel_offset */
873
874  /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
875     signed offset from the appropriate base, and filling in the register
876     field with the appropriate register (0, 2, or 13).  */
877  HOWTO (R_PPC_EMB_SDA21,	/* type */
878	 0,			/* rightshift */
879	 2,			/* size (0 = byte, 1 = short, 2 = long) */
880	 16,			/* bitsize */
881	 false,			/* pc_relative */
882	 0,			/* bitpos */
883	 complain_overflow_signed, /* complain_on_overflow */
884	 bfd_elf_generic_reloc,	/* special_function */
885	 "R_PPC_EMB_SDA21",	/* name */
886	 false,			/* partial_inplace */
887	 0,			/* src_mask */
888	 0xffff,		/* dst_mask */
889	 false),		/* pcrel_offset */
890
891  /* Relocation not handled: R_PPC_EMB_MRKREF */
892  /* Relocation not handled: R_PPC_EMB_RELSEC16 */
893  /* Relocation not handled: R_PPC_EMB_RELST_LO */
894  /* Relocation not handled: R_PPC_EMB_RELST_HI */
895  /* Relocation not handled: R_PPC_EMB_RELST_HA */
896  /* Relocation not handled: R_PPC_EMB_BIT_FLD */
897
898  /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
899     in the 16 bit signed offset from the appropriate base, and filling in the
900     register field with the appropriate register (0, 2, or 13).  */
901  HOWTO (R_PPC_EMB_RELSDA,	/* type */
902	 0,			/* rightshift */
903	 1,			/* size (0 = byte, 1 = short, 2 = long) */
904	 16,			/* bitsize */
905	 true,			/* pc_relative */
906	 0,			/* bitpos */
907	 complain_overflow_signed, /* complain_on_overflow */
908	 bfd_elf_generic_reloc,	/* special_function */
909	 "R_PPC_EMB_RELSDA",	/* name */
910	 false,			/* partial_inplace */
911	 0,			/* src_mask */
912	 0xffff,		/* dst_mask */
913	 false),		/* pcrel_offset */
914
915  /* GNU extension to record C++ vtable hierarchy */
916  HOWTO (R_PPC_GNU_VTINHERIT,	/* type */
917	 0,			/* rightshift */
918	 0,			/* size (0 = byte, 1 = short, 2 = long) */
919	 0,			/* bitsize */
920	 false,			/* pc_relative */
921	 0,			/* bitpos */
922	 complain_overflow_dont, /* complain_on_overflow */
923	 NULL,			/* special_function */
924	 "R_PPC_GNU_VTINHERIT",	/* name */
925	 false,			/* partial_inplace */
926	 0,			/* src_mask */
927	 0,			/* dst_mask */
928	 false),		/* pcrel_offset */
929
930  /* GNU extension to record C++ vtable member usage */
931  HOWTO (R_PPC_GNU_VTENTRY,	/* type */
932	 0,			/* rightshift */
933	 0,			/* size (0 = byte, 1 = short, 2 = long) */
934	 0,			/* bitsize */
935	 false,			/* pc_relative */
936	 0,			/* bitpos */
937	 complain_overflow_dont, /* complain_on_overflow */
938	 NULL,			/* special_function */
939	 "R_PPC_GNU_VTENTRY",	/* name */
940	 false,			/* partial_inplace */
941	 0,			/* src_mask */
942	 0,			/* dst_mask */
943	 false),		/* pcrel_offset */
944
945  /* Phony reloc to handle AIX style TOC entries */
946  HOWTO (R_PPC_TOC16,		/* type */
947	 0,			/* rightshift */
948	 1,			/* size (0 = byte, 1 = short, 2 = long) */
949	 16,			/* bitsize */
950	 false,			/* pc_relative */
951	 0,			/* bitpos */
952	 complain_overflow_signed, /* complain_on_overflow */
953	 bfd_elf_generic_reloc,	/* special_function */
954	 "R_PPC_TOC16",		/* name */
955	 false,			/* partial_inplace */
956	 0,			/* src_mask */
957	 0xffff,		/* dst_mask */
958	 false),		/* pcrel_offset */
959};
960
961/* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
962
963static void
964ppc_elf_howto_init ()
965{
966  unsigned int i, type;
967
968  for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
969    {
970      type = ppc_elf_howto_raw[i].type;
971      BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
972      ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
973    }
974}
975
976/* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
977
978   The MPC860, revision C0 or earlier contains a bug in the die.
979   If all of the following conditions are true, the next instruction
980   to be executed *may* be treated as a no-op.
981   1/ A forward branch is executed.
982   2/ The branch is predicted as not taken.
983   3/ The branch is taken.
984   4/ The branch is located in the last 5 words of a page.
985      (The EOP limit is 5 by default but may be specified as any value from 1-10.)
986
987   Our software solution is to detect these problematic branches in a
988   linker pass and modify them as follows:
989   1/ Unconditional branches - Since these are always predicted taken,
990      there is no problem and no action is required.
991   2/ Conditional backward branches - No problem, no action required.
992   3/ Conditional forward branches - Ensure that the "inverse prediction
993      bit" is set (ensure it is predicted taken).
994   4/ Conditional register branches - Ensure that the "y bit" is set
995      (ensure it is predicted taken).
996*/
997
998/* Sort sections by address.  */
999
1000static int
1001ppc_elf_sort_rela (arg1, arg2)
1002     const PTR arg1;
1003     const PTR arg2;
1004{
1005  const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1006  const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1007
1008  /* Sort by offset.  */
1009  return ((*rela1)->r_offset - (*rela2)->r_offset);
1010}
1011
1012static boolean
1013ppc_elf_relax_section (abfd, isec, link_info, again)
1014     bfd *abfd;
1015     asection *isec;
1016     struct bfd_link_info *link_info;
1017     boolean *again;
1018{
1019#define PAGESIZE 0x1000
1020
1021  bfd_byte *contents = NULL;
1022  bfd_byte *free_contents = NULL;
1023  Elf_Internal_Rela *internal_relocs = NULL;
1024  Elf_Internal_Rela *free_relocs = NULL;
1025  Elf_Internal_Rela **rela_comb = NULL;
1026  int comb_curr, comb_count;
1027
1028  /* We never have to do this more than once per input section.  */
1029  *again = false;
1030
1031  /* If needed, initialize this section's cooked size.  */
1032  if (isec->_cooked_size == 0)
1033      isec->_cooked_size = isec->_raw_size;
1034
1035  /* We're only interested in text sections which overlap the
1036     troublesome area at the end of a page.  */
1037  if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1038    {
1039      bfd_vma dot, end_page, end_section;
1040      boolean section_modified;
1041
1042      /* Get the section contents.  */
1043      /* Get cached copy if it exists.  */
1044      if (elf_section_data (isec)->this_hdr.contents != NULL)
1045	  contents = elf_section_data (isec)->this_hdr.contents;
1046      else
1047	{
1048	  /* Go get them off disk.  */
1049	  contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1050	  if (contents == NULL)
1051	    goto error_return;
1052	  free_contents = contents;
1053
1054	  if (! bfd_get_section_contents (abfd, isec, contents,
1055					  (file_ptr) 0, isec->_raw_size))
1056	    goto error_return;
1057	}
1058
1059      comb_curr = 0;
1060      comb_count = 0;
1061      if (isec->reloc_count)
1062	{
1063          unsigned n;
1064	  bfd_size_type amt;
1065
1066          /* Get a copy of the native relocations.  */
1067          internal_relocs = _bfd_elf32_link_read_relocs (
1068    	    abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1069    	    link_info->keep_memory);
1070          if (internal_relocs == NULL)
1071    	      goto error_return;
1072          if (! link_info->keep_memory)
1073    	      free_relocs = internal_relocs;
1074
1075          /* Setup a faster access method for the reloc info we need.  */
1076	  amt = isec->reloc_count;
1077	  amt *= sizeof (Elf_Internal_Rela*);
1078          rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1079          if (rela_comb == NULL)
1080              goto error_return;
1081          for (n = 0; n < isec->reloc_count; ++n)
1082            {
1083              long r_type;
1084
1085              r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1086              if (r_type < 0 || r_type >= (int) R_PPC_max)
1087                  goto error_return;
1088
1089              /* Prologue constants are sometimes present in the ".text"
1090              sections and they can be identified by their associated relocation.
1091              We don't want to process those words and some others which
1092              can also be identified by their relocations.  However, not all
1093              conditional branches will have a relocation so we will
1094              only ignore words that 1) have a reloc, and 2) the reloc
1095              is not applicable to a conditional branch.
1096              The array rela_comb is built here for use in the EOP scan loop.  */
1097              switch (r_type)
1098                {
1099                case R_PPC_ADDR14_BRNTAKEN:     /* absolute, predicted not taken */
1100                case R_PPC_REL14:               /* relative cond. br.  */
1101                case R_PPC_REL14_BRNTAKEN:      /* rel. cond. br., predicted not taken */
1102                  /* We should check the instruction.  */
1103                  break;
1104                default:
1105                  /* The word is not a conditional branch - ignore it.  */
1106                  rela_comb[comb_count++] = &internal_relocs[n];
1107                  break;
1108                }
1109            }
1110          if (comb_count > 1)
1111	    qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1112	}
1113
1114      /* Enumerate each EOP region that overlaps this section.  */
1115      end_section = isec->vma + isec->_cooked_size;
1116      dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1117      dot -= link_info->mpc860c0;
1118      section_modified = false;
1119      if (dot < isec->vma)      /* Increment the start position if this section */
1120          dot = isec->vma;      /* begins in the middle of its first EOP region.  */
1121      for (;
1122           dot < end_section;
1123           dot += PAGESIZE, end_page += PAGESIZE)
1124        {
1125
1126          /* Check each word in this EOP region.  */
1127          for (; dot < end_page; dot += 4)
1128            {
1129              bfd_vma isec_offset;
1130              unsigned long insn;
1131              boolean skip, modified;
1132
1133              /* Don't process this word if there is a relocation for it and
1134              the relocation indicates the word is not a conditional branch.  */
1135              skip = false;
1136              isec_offset = dot - isec->vma;
1137              for (; comb_curr<comb_count; ++comb_curr)
1138                {
1139                  bfd_vma r_offset;
1140
1141                  r_offset = rela_comb[comb_curr]->r_offset;
1142                  if (r_offset >= isec_offset)
1143                    {
1144                      if (r_offset == isec_offset) skip = true;
1145                      break;
1146                    }
1147                }
1148              if (skip) continue;
1149
1150              /* Check the current word for a problematic conditional branch.  */
1151#define BO0(insn) ((insn) & 0x02000000)
1152#define BO2(insn) ((insn) & 0x00800000)
1153#define BO4(insn) ((insn) & 0x00200000)
1154              insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1155              modified = false;
1156              if ((insn & 0xFc000000) == 0x40000000)
1157                {
1158                  /* Instruction is BCx */
1159                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1160                    {
1161                      bfd_vma target;
1162                      /* This branch is predicted as "normal".
1163                      If this is a forward branch, it is problematic.  */
1164
1165                      target = insn & 0x0000Fffc;               /*extract*/
1166                      target = (target ^ 0x8000) - 0x8000;      /*sign extend*/
1167                      if ((insn & 0x00000002) == 0)
1168                          target += dot;                        /*convert to abs*/
1169                      if (target > dot)
1170                        {
1171                          insn |= 0x00200000;   /* set the prediction bit */
1172                          modified = true;
1173                        }
1174                    }
1175                }
1176              else if ((insn & 0xFc00Fffe) == 0x4c000420)
1177                {
1178                  /* Instruction is BCCTRx */
1179                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1180		    {
1181		      /* This branch is predicted as not-taken.
1182		      If this is a forward branch, it is problematic.
1183                      Since we can't tell statically if it will branch forward,
1184                      always set the prediction bit.  */
1185                      insn |= 0x00200000;   /* set the prediction bit */
1186                      modified = true;
1187		    }
1188                }
1189              else if ((insn & 0xFc00Fffe) == 0x4c000020)
1190                {
1191                  /* Instruction is BCLRx */
1192                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1193		    {
1194		      /* This branch is predicted as not-taken.
1195		      If this is a forward branch, it is problematic.
1196                      Since we can't tell statically if it will branch forward,
1197                      always set the prediction bit.  */
1198                      insn |= 0x00200000;   /* set the prediction bit */
1199                      modified = true;
1200		    }
1201                }
1202#undef BO0
1203#undef BO2
1204#undef BO4
1205              if (modified)
1206	        {
1207                  bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1208		  section_modified = true;
1209	        }
1210            }
1211        }
1212      if (section_modified)
1213	{
1214	  elf_section_data (isec)->this_hdr.contents = contents;
1215	  free_contents = NULL;
1216	}
1217    }
1218
1219  if (rela_comb != NULL)
1220    {
1221      free (rela_comb);
1222      rela_comb = NULL;
1223    }
1224
1225  if (free_relocs != NULL)
1226    {
1227      free (free_relocs);
1228      free_relocs = NULL;
1229    }
1230
1231  if (free_contents != NULL)
1232    {
1233      if (! link_info->keep_memory)
1234	free (free_contents);
1235      else
1236	{
1237	  /* Cache the section contents for elf_link_input_bfd.  */
1238	  elf_section_data (isec)->this_hdr.contents = contents;
1239	}
1240      free_contents = NULL;
1241    }
1242
1243  return true;
1244
1245error_return:
1246  if (rela_comb != NULL)
1247    free (rela_comb);
1248  if (free_relocs != NULL)
1249    free (free_relocs);
1250  if (free_contents != NULL)
1251    free (free_contents);
1252  return false;
1253}
1254
1255static reloc_howto_type *
1256ppc_elf_reloc_type_lookup (abfd, code)
1257     bfd *abfd ATTRIBUTE_UNUSED;
1258     bfd_reloc_code_real_type code;
1259{
1260  enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1261
1262  if (!ppc_elf_howto_table[R_PPC_ADDR32])
1263    /* Initialize howto table if needed.  */
1264    ppc_elf_howto_init ();
1265
1266  switch ((int) code)
1267    {
1268    default:
1269      return (reloc_howto_type *) NULL;
1270
1271    case BFD_RELOC_NONE:		ppc_reloc = R_PPC_NONE;			break;
1272    case BFD_RELOC_32:			ppc_reloc = R_PPC_ADDR32;		break;
1273    case BFD_RELOC_PPC_BA26:		ppc_reloc = R_PPC_ADDR24;		break;
1274    case BFD_RELOC_16:			ppc_reloc = R_PPC_ADDR16;		break;
1275    case BFD_RELOC_LO16:		ppc_reloc = R_PPC_ADDR16_LO;		break;
1276    case BFD_RELOC_HI16:		ppc_reloc = R_PPC_ADDR16_HI;		break;
1277    case BFD_RELOC_HI16_S:		ppc_reloc = R_PPC_ADDR16_HA;		break;
1278    case BFD_RELOC_PPC_BA16:		ppc_reloc = R_PPC_ADDR14;		break;
1279    case BFD_RELOC_PPC_BA16_BRTAKEN:	ppc_reloc = R_PPC_ADDR14_BRTAKEN;	break;
1280    case BFD_RELOC_PPC_BA16_BRNTAKEN:	ppc_reloc = R_PPC_ADDR14_BRNTAKEN;	break;
1281    case BFD_RELOC_PPC_B26:		ppc_reloc = R_PPC_REL24;		break;
1282    case BFD_RELOC_PPC_B16:		ppc_reloc = R_PPC_REL14;		break;
1283    case BFD_RELOC_PPC_B16_BRTAKEN:	ppc_reloc = R_PPC_REL14_BRTAKEN;	break;
1284    case BFD_RELOC_PPC_B16_BRNTAKEN:	ppc_reloc = R_PPC_REL14_BRNTAKEN;	break;
1285    case BFD_RELOC_16_GOTOFF:		ppc_reloc = R_PPC_GOT16;		break;
1286    case BFD_RELOC_LO16_GOTOFF:		ppc_reloc = R_PPC_GOT16_LO;		break;
1287    case BFD_RELOC_HI16_GOTOFF:		ppc_reloc = R_PPC_GOT16_HI;		break;
1288    case BFD_RELOC_HI16_S_GOTOFF:	ppc_reloc = R_PPC_GOT16_HA;		break;
1289    case BFD_RELOC_24_PLT_PCREL:	ppc_reloc = R_PPC_PLTREL24;		break;
1290    case BFD_RELOC_PPC_COPY:		ppc_reloc = R_PPC_COPY;			break;
1291    case BFD_RELOC_PPC_GLOB_DAT:	ppc_reloc = R_PPC_GLOB_DAT;		break;
1292    case BFD_RELOC_PPC_LOCAL24PC:	ppc_reloc = R_PPC_LOCAL24PC;		break;
1293    case BFD_RELOC_32_PCREL:		ppc_reloc = R_PPC_REL32;		break;
1294    case BFD_RELOC_32_PLTOFF:		ppc_reloc = R_PPC_PLT32;		break;
1295    case BFD_RELOC_32_PLT_PCREL:	ppc_reloc = R_PPC_PLTREL32;		break;
1296    case BFD_RELOC_LO16_PLTOFF:		ppc_reloc = R_PPC_PLT16_LO;		break;
1297    case BFD_RELOC_HI16_PLTOFF:		ppc_reloc = R_PPC_PLT16_HI;		break;
1298    case BFD_RELOC_HI16_S_PLTOFF:	ppc_reloc = R_PPC_PLT16_HA;		break;
1299    case BFD_RELOC_GPREL16:		ppc_reloc = R_PPC_SDAREL16;		break;
1300    case BFD_RELOC_16_BASEREL:		ppc_reloc = R_PPC_SECTOFF;		break;
1301    case BFD_RELOC_LO16_BASEREL:	ppc_reloc = R_PPC_SECTOFF_LO;		break;
1302    case BFD_RELOC_HI16_BASEREL:	ppc_reloc = R_PPC_SECTOFF_HI;		break;
1303    case BFD_RELOC_HI16_S_BASEREL:	ppc_reloc = R_PPC_SECTOFF_HA;		break;
1304    case BFD_RELOC_CTOR:		ppc_reloc = R_PPC_ADDR32;		break;
1305    case BFD_RELOC_PPC_TOC16:		ppc_reloc = R_PPC_TOC16;		break;
1306    case BFD_RELOC_PPC_EMB_NADDR32:	ppc_reloc = R_PPC_EMB_NADDR32;		break;
1307    case BFD_RELOC_PPC_EMB_NADDR16:	ppc_reloc = R_PPC_EMB_NADDR16;		break;
1308    case BFD_RELOC_PPC_EMB_NADDR16_LO:	ppc_reloc = R_PPC_EMB_NADDR16_LO;	break;
1309    case BFD_RELOC_PPC_EMB_NADDR16_HI:	ppc_reloc = R_PPC_EMB_NADDR16_HI;	break;
1310    case BFD_RELOC_PPC_EMB_NADDR16_HA:	ppc_reloc = R_PPC_EMB_NADDR16_HA;	break;
1311    case BFD_RELOC_PPC_EMB_SDAI16:	ppc_reloc = R_PPC_EMB_SDAI16;		break;
1312    case BFD_RELOC_PPC_EMB_SDA2I16:	ppc_reloc = R_PPC_EMB_SDA2I16;		break;
1313    case BFD_RELOC_PPC_EMB_SDA2REL:	ppc_reloc = R_PPC_EMB_SDA2REL;		break;
1314    case BFD_RELOC_PPC_EMB_SDA21:	ppc_reloc = R_PPC_EMB_SDA21;		break;
1315    case BFD_RELOC_PPC_EMB_MRKREF:	ppc_reloc = R_PPC_EMB_MRKREF;		break;
1316    case BFD_RELOC_PPC_EMB_RELSEC16:	ppc_reloc = R_PPC_EMB_RELSEC16;		break;
1317    case BFD_RELOC_PPC_EMB_RELST_LO:	ppc_reloc = R_PPC_EMB_RELST_LO;		break;
1318    case BFD_RELOC_PPC_EMB_RELST_HI:	ppc_reloc = R_PPC_EMB_RELST_HI;		break;
1319    case BFD_RELOC_PPC_EMB_RELST_HA:	ppc_reloc = R_PPC_EMB_RELST_HA;		break;
1320    case BFD_RELOC_PPC_EMB_BIT_FLD:	ppc_reloc = R_PPC_EMB_BIT_FLD;		break;
1321    case BFD_RELOC_PPC_EMB_RELSDA:	ppc_reloc = R_PPC_EMB_RELSDA;		break;
1322    case BFD_RELOC_VTABLE_INHERIT:	ppc_reloc = R_PPC_GNU_VTINHERIT;	break;
1323    case BFD_RELOC_VTABLE_ENTRY:	ppc_reloc = R_PPC_GNU_VTENTRY;		break;
1324    }
1325
1326  return ppc_elf_howto_table[(int) ppc_reloc];
1327};
1328
1329/* Set the howto pointer for a PowerPC ELF reloc.  */
1330
1331static void
1332ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1333     bfd *abfd ATTRIBUTE_UNUSED;
1334     arelent *cache_ptr;
1335     Elf32_Internal_Rela *dst;
1336{
1337  if (!ppc_elf_howto_table[R_PPC_ADDR32])
1338    /* Initialize howto table if needed.  */
1339    ppc_elf_howto_init ();
1340
1341  BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1342  cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1343}
1344
1345/* Handle the R_PPC_ADDR16_HA reloc.  */
1346
1347static bfd_reloc_status_type
1348ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1349			 output_bfd, error_message)
1350     bfd *abfd ATTRIBUTE_UNUSED;
1351     arelent *reloc_entry;
1352     asymbol *symbol;
1353     PTR data ATTRIBUTE_UNUSED;
1354     asection *input_section;
1355     bfd *output_bfd;
1356     char **error_message ATTRIBUTE_UNUSED;
1357{
1358  bfd_vma relocation;
1359
1360  if (output_bfd != NULL)
1361    {
1362      reloc_entry->address += input_section->output_offset;
1363      return bfd_reloc_ok;
1364    }
1365
1366  if (reloc_entry->address > input_section->_cooked_size)
1367    return bfd_reloc_outofrange;
1368
1369  if (bfd_is_com_section (symbol->section))
1370    relocation = 0;
1371  else
1372    relocation = symbol->value;
1373
1374  relocation += symbol->section->output_section->vma;
1375  relocation += symbol->section->output_offset;
1376  relocation += reloc_entry->addend;
1377
1378  reloc_entry->addend += (relocation & 0x8000) << 1;
1379
1380  return bfd_reloc_continue;
1381}
1382
1383/* Function to set whether a module needs the -mrelocatable bit set.  */
1384
1385static boolean
1386ppc_elf_set_private_flags (abfd, flags)
1387     bfd *abfd;
1388     flagword flags;
1389{
1390  BFD_ASSERT (!elf_flags_init (abfd)
1391	      || elf_elfheader (abfd)->e_flags == flags);
1392
1393  elf_elfheader (abfd)->e_flags = flags;
1394  elf_flags_init (abfd) = true;
1395  return true;
1396}
1397
1398/* Merge backend specific data from an object file to the output
1399   object file when linking */
1400static boolean
1401ppc_elf_merge_private_bfd_data (ibfd, obfd)
1402     bfd *ibfd;
1403     bfd *obfd;
1404{
1405  flagword old_flags;
1406  flagword new_flags;
1407  boolean error;
1408
1409  /* Check if we have the same endianess */
1410  if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
1411    return false;
1412
1413  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1414      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1415    return true;
1416
1417  new_flags = elf_elfheader (ibfd)->e_flags;
1418  old_flags = elf_elfheader (obfd)->e_flags;
1419  if (!elf_flags_init (obfd))	/* First call, no flags set */
1420    {
1421      elf_flags_init (obfd) = true;
1422      elf_elfheader (obfd)->e_flags = new_flags;
1423    }
1424
1425  else if (new_flags == old_flags)	/* Compatible flags are ok */
1426    ;
1427
1428  else					/* Incompatible flags */
1429    {
1430      /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
1431         with either.  */
1432      error = false;
1433      if ((new_flags & EF_PPC_RELOCATABLE) != 0
1434	  && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1435	{
1436	  error = true;
1437	  (*_bfd_error_handler)
1438	    (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1439	     bfd_archive_filename (ibfd));
1440	}
1441      else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1442	       && (old_flags & EF_PPC_RELOCATABLE) != 0)
1443	{
1444	  error = true;
1445	  (*_bfd_error_handler)
1446	    (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1447	     bfd_archive_filename (ibfd));
1448	}
1449
1450      /* The output is -mrelocatable-lib iff both the input files are.  */
1451      if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1452	elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1453
1454      /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1455         but each input file is either -mrelocatable or -mrelocatable-lib.  */
1456      if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1457	  && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1458	  && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1459	elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1460
1461      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
1462      elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1463
1464      new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1465      old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1466
1467      /* Warn about any other mismatches */
1468      if (new_flags != old_flags)
1469	{
1470	  error = true;
1471	  (*_bfd_error_handler)
1472	    (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1473	     bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1474	}
1475
1476      if (error)
1477	{
1478	  bfd_set_error (bfd_error_bad_value);
1479	  return false;
1480	}
1481    }
1482
1483  return true;
1484}
1485
1486/* Handle a PowerPC specific section when reading an object file.  This
1487   is called when elfcode.h finds a section with an unknown type.  */
1488
1489static boolean
1490ppc_elf_section_from_shdr (abfd, hdr, name)
1491     bfd *abfd;
1492     Elf32_Internal_Shdr *hdr;
1493     char *name;
1494{
1495  asection *newsect;
1496  flagword flags;
1497
1498  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1499    return false;
1500
1501  newsect = hdr->bfd_section;
1502  flags = bfd_get_section_flags (abfd, newsect);
1503  if (hdr->sh_flags & SHF_EXCLUDE)
1504    flags |= SEC_EXCLUDE;
1505
1506  if (hdr->sh_type == SHT_ORDERED)
1507    flags |= SEC_SORT_ENTRIES;
1508
1509  bfd_set_section_flags (abfd, newsect, flags);
1510  return true;
1511}
1512
1513/* Set up any other section flags and such that may be necessary.  */
1514
1515static boolean
1516ppc_elf_fake_sections (abfd, shdr, asect)
1517     bfd *abfd ATTRIBUTE_UNUSED;
1518     Elf32_Internal_Shdr *shdr;
1519     asection *asect;
1520{
1521  if ((asect->flags & SEC_EXCLUDE) != 0)
1522    shdr->sh_flags |= SHF_EXCLUDE;
1523
1524  if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1525    shdr->sh_type = SHT_ORDERED;
1526
1527  return true;
1528}
1529
1530/* Create a special linker section */
1531static elf_linker_section_t *
1532ppc_elf_create_linker_section (abfd, info, which)
1533     bfd *abfd;
1534     struct bfd_link_info *info;
1535     enum elf_linker_section_enum which;
1536{
1537  bfd *dynobj = elf_hash_table (info)->dynobj;
1538  elf_linker_section_t *lsect;
1539
1540  /* Record the first bfd section that needs the special section */
1541  if (!dynobj)
1542    dynobj = elf_hash_table (info)->dynobj = abfd;
1543
1544  /* If this is the first time, create the section */
1545  lsect = elf_linker_section (dynobj, which);
1546  if (!lsect)
1547    {
1548      elf_linker_section_t defaults;
1549      static elf_linker_section_t zero_section;
1550
1551      defaults = zero_section;
1552      defaults.which = which;
1553      defaults.hole_written_p = false;
1554      defaults.alignment = 2;
1555
1556      /* Both of these sections are (technically) created by the user
1557	 putting data in them, so they shouldn't be marked
1558	 SEC_LINKER_CREATED.
1559
1560	 The linker creates them so it has somewhere to attach their
1561	 respective symbols. In fact, if they were empty it would
1562	 be OK to leave the symbol set to 0 (or any random number), because
1563	 the appropriate register should never be used.  */
1564      defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1565			| SEC_IN_MEMORY);
1566
1567      switch (which)
1568	{
1569	default:
1570	  (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1571				 bfd_get_filename (abfd),
1572				 (int) which);
1573
1574	  bfd_set_error (bfd_error_bad_value);
1575	  return (elf_linker_section_t *) 0;
1576
1577	case LINKER_SECTION_SDATA:	/* .sdata/.sbss section */
1578	  defaults.name		  = ".sdata";
1579	  defaults.rel_name	  = ".rela.sdata";
1580	  defaults.bss_name	  = ".sbss";
1581	  defaults.sym_name	  = "_SDA_BASE_";
1582	  defaults.sym_offset	  = 32768;
1583	  break;
1584
1585	case LINKER_SECTION_SDATA2:	/* .sdata2/.sbss2 section */
1586	  defaults.name		  = ".sdata2";
1587	  defaults.rel_name	  = ".rela.sdata2";
1588	  defaults.bss_name	  = ".sbss2";
1589	  defaults.sym_name	  = "_SDA2_BASE_";
1590	  defaults.sym_offset	  = 32768;
1591	  defaults.flags	 |= SEC_READONLY;
1592	  break;
1593	}
1594
1595      lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1596    }
1597
1598  return lsect;
1599}
1600
1601/* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
1602   need to bump up the number of section headers.  */
1603
1604static int
1605ppc_elf_additional_program_headers (abfd)
1606     bfd *abfd;
1607{
1608  asection *s;
1609  int ret;
1610
1611  ret = 0;
1612
1613  s = bfd_get_section_by_name (abfd, ".interp");
1614  if (s != NULL)
1615    ++ret;
1616
1617  s = bfd_get_section_by_name (abfd, ".sbss2");
1618  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1619    ++ret;
1620
1621  s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1622  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1623    ++ret;
1624
1625  return ret;
1626}
1627
1628/* Modify the segment map if needed.  */
1629
1630static boolean
1631ppc_elf_modify_segment_map (abfd)
1632     bfd *abfd ATTRIBUTE_UNUSED;
1633{
1634  return true;
1635}
1636
1637/* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
1638
1639static asection *
1640ppc_elf_create_got (abfd, info)
1641     bfd *abfd;
1642     struct bfd_link_info *info;
1643{
1644  register asection *s;
1645  flagword flags;
1646
1647  if (!_bfd_elf_create_got_section (abfd, info))
1648    return NULL;
1649
1650  s = bfd_get_section_by_name (abfd, ".got");
1651  if (s == NULL)
1652    abort ();
1653
1654  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1655	   | SEC_LINKER_CREATED);
1656  if (!bfd_set_section_flags (abfd, s, flags))
1657    return NULL;
1658  return s;
1659}
1660
1661/* We have to create .dynsbss and .rela.sbss here so that they get mapped
1662   to output sections (just like _bfd_elf_create_dynamic_sections has
1663   to create .dynbss and .rela.bss).  */
1664
1665static boolean
1666ppc_elf_create_dynamic_sections (abfd, info)
1667     bfd *abfd;
1668     struct bfd_link_info *info;
1669{
1670  register asection *s;
1671  flagword flags;
1672
1673  if (!ppc_elf_create_got (abfd, info))
1674    return false;
1675
1676  if (!_bfd_elf_create_dynamic_sections (abfd, info))
1677    return false;
1678
1679  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1680	   | SEC_LINKER_CREATED);
1681
1682  s = bfd_make_section (abfd, ".dynsbss");
1683  if (s == NULL
1684      || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1685    return false;
1686
1687  if (! info->shared)
1688    {
1689      s = bfd_make_section (abfd, ".rela.sbss");
1690      if (s == NULL
1691	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1692	  || ! bfd_set_section_alignment (abfd, s, 2))
1693	return false;
1694    }
1695
1696  s = bfd_get_section_by_name (abfd, ".plt");
1697  if (s == NULL)
1698    abort ();
1699
1700  flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
1701  return bfd_set_section_flags (abfd, s, flags);
1702}
1703
1704/* Adjust a symbol defined by a dynamic object and referenced by a
1705   regular object.  The current definition is in some section of the
1706   dynamic object, but we're not including those sections.  We have to
1707   change the definition to something the rest of the link can
1708   understand.  */
1709
1710static boolean
1711ppc_elf_adjust_dynamic_symbol (info, h)
1712     struct bfd_link_info *info;
1713     struct elf_link_hash_entry *h;
1714{
1715  bfd *dynobj = elf_hash_table (info)->dynobj;
1716  asection *s;
1717  unsigned int power_of_two;
1718  bfd_vma plt_offset;
1719
1720#ifdef DEBUG
1721  fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1722#endif
1723
1724  /* Make sure we know what is going on here.  */
1725  BFD_ASSERT (dynobj != NULL
1726	      && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1727		  || h->weakdef != NULL
1728		  || ((h->elf_link_hash_flags
1729		       & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1730		      && (h->elf_link_hash_flags
1731			  & ELF_LINK_HASH_REF_REGULAR) != 0
1732		      && (h->elf_link_hash_flags
1733			  & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1734
1735  /* If this is a function, put it in the procedure linkage table.  We
1736     will fill in the contents of the procedure linkage table later,
1737     when we know the address of the .got section.  */
1738  if (h->type == STT_FUNC
1739      || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1740    {
1741      if (! elf_hash_table (info)->dynamic_sections_created
1742 	  || SYMBOL_CALLS_LOCAL (info, h)
1743	  || (info->shared && h->plt.refcount <= 0))
1744	{
1745	  /* A PLT entry is not required/allowed when:
1746
1747	     1. We are not using ld.so; because then the PLT entry
1748	     can't be set up, so we can't use one.
1749
1750	     2. We know for certain that a call to this symbol
1751	     will go to this object.
1752
1753	     3. GC has rendered the entry unused.
1754	     Note, however, that in an executable all references to the
1755	     symbol go to the PLT, so we can't turn it off in that case.
1756	     ??? The correct thing to do here is to reference count
1757	     all uses of the symbol, not just those to the GOT or PLT.  */
1758	  h->plt.offset = (bfd_vma) -1;
1759	  h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1760	  return true;
1761	}
1762
1763      /* Make sure this symbol is output as a dynamic symbol.  */
1764      if (h->dynindx == -1)
1765	{
1766	  if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1767	    return false;
1768	}
1769      BFD_ASSERT (h->dynindx != -1);
1770
1771      s = bfd_get_section_by_name (dynobj, ".plt");
1772      BFD_ASSERT (s != NULL);
1773
1774      /* If this is the first .plt entry, make room for the special
1775	 first entry.  */
1776      if (s->_raw_size == 0)
1777	s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1778
1779      /* The PowerPC PLT is actually composed of two parts, the first part
1780	 is 2 words (for a load and a jump), and then there is a remaining
1781	 word available at the end.  */
1782      plt_offset = (PLT_INITIAL_ENTRY_SIZE
1783		    + (PLT_SLOT_SIZE
1784		       * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1785			  / PLT_ENTRY_SIZE)));
1786
1787      /* If this symbol is not defined in a regular file, and we are
1788	 not generating a shared library, then set the symbol to this
1789	 location in the .plt.  This is required to make function
1790	 pointers compare as equal between the normal executable and
1791	 the shared library.  */
1792      if (! info->shared
1793	  && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1794	{
1795	  h->root.u.def.section = s;
1796	  h->root.u.def.value = plt_offset;
1797	}
1798
1799      h->plt.offset = plt_offset;
1800
1801      /* Make room for this entry.  After the 8192nd entry, room
1802         for two entries is allocated.  */
1803      if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1804	  >= PLT_NUM_SINGLE_ENTRIES)
1805	s->_raw_size += 2 * PLT_ENTRY_SIZE;
1806      else
1807	s->_raw_size += PLT_ENTRY_SIZE;
1808
1809      /* We also need to make an entry in the .rela.plt section.  */
1810      s = bfd_get_section_by_name (dynobj, ".rela.plt");
1811      BFD_ASSERT (s != NULL);
1812      s->_raw_size += sizeof (Elf32_External_Rela);
1813
1814      return true;
1815    }
1816  else
1817    h->plt.offset = (bfd_vma) -1;
1818
1819  /* If this is a weak symbol, and there is a real definition, the
1820     processor independent code will have arranged for us to see the
1821     real definition first, and we can just use the same value.  */
1822  if (h->weakdef != NULL)
1823    {
1824      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1825		  || h->weakdef->root.type == bfd_link_hash_defweak);
1826      h->root.u.def.section = h->weakdef->root.u.def.section;
1827      h->root.u.def.value = h->weakdef->root.u.def.value;
1828      return true;
1829    }
1830
1831  /* This is a reference to a symbol defined by a dynamic object which
1832     is not a function.  */
1833
1834  /* If we are creating a shared library, we must presume that the
1835     only references to the symbol are via the global offset table.
1836     For such cases we need not do anything here; the relocations will
1837     be handled correctly by relocate_section.  */
1838  if (info->shared)
1839    return true;
1840
1841  /* We must allocate the symbol in our .dynbss section, which will
1842     become part of the .bss section of the executable.  There will be
1843     an entry for this symbol in the .dynsym section.  The dynamic
1844     object will contain position independent code, so all references
1845     from the dynamic object to this symbol will go through the global
1846     offset table.  The dynamic linker will use the .dynsym entry to
1847     determine the address it must put in the global offset table, so
1848     both the dynamic object and the regular object will refer to the
1849     same memory location for the variable.
1850
1851     Of course, if the symbol is sufficiently small, we must instead
1852     allocate it in .sbss.  FIXME: It would be better to do this if and
1853     only if there were actually SDAREL relocs for that symbol.  */
1854
1855  if (h->size <= elf_gp_size (dynobj))
1856    s = bfd_get_section_by_name (dynobj, ".dynsbss");
1857  else
1858    s = bfd_get_section_by_name (dynobj, ".dynbss");
1859  BFD_ASSERT (s != NULL);
1860
1861  /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
1862     copy the initial value out of the dynamic object and into the
1863     runtime process image.  We need to remember the offset into the
1864     .rela.bss section we are going to use.  */
1865  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1866    {
1867      asection *srel;
1868
1869      if (h->size <= elf_gp_size (dynobj))
1870	srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
1871      else
1872	srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1873      BFD_ASSERT (srel != NULL);
1874      srel->_raw_size += sizeof (Elf32_External_Rela);
1875      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1876    }
1877
1878  /* We need to figure out the alignment required for this symbol.  I
1879     have no idea how ELF linkers handle this.  */
1880  power_of_two = bfd_log2 (h->size);
1881  if (power_of_two > 4)
1882    power_of_two = 4;
1883
1884  /* Apply the required alignment.  */
1885  s->_raw_size = BFD_ALIGN (s->_raw_size,
1886			    (bfd_size_type) (1 << power_of_two));
1887  if (power_of_two > bfd_get_section_alignment (dynobj, s))
1888    {
1889      if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1890	return false;
1891    }
1892
1893  /* Define the symbol as being at this point in the section.  */
1894  h->root.u.def.section = s;
1895  h->root.u.def.value = s->_raw_size;
1896
1897  /* Increment the section size to make room for the symbol.  */
1898  s->_raw_size += h->size;
1899
1900  return true;
1901}
1902
1903/* Set the sizes of the dynamic sections.  */
1904
1905static boolean
1906ppc_elf_size_dynamic_sections (output_bfd, info)
1907     bfd *output_bfd ATTRIBUTE_UNUSED;
1908     struct bfd_link_info *info;
1909{
1910  bfd *dynobj;
1911  asection *s;
1912  boolean plt;
1913  boolean relocs;
1914
1915#ifdef DEBUG
1916  fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
1917#endif
1918
1919  dynobj = elf_hash_table (info)->dynobj;
1920  BFD_ASSERT (dynobj != NULL);
1921
1922  if (elf_hash_table (info)->dynamic_sections_created)
1923    {
1924      /* Set the contents of the .interp section to the interpreter.  */
1925      if (! info->shared)
1926	{
1927	  s = bfd_get_section_by_name (dynobj, ".interp");
1928	  BFD_ASSERT (s != NULL);
1929	  s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1930	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1931	}
1932    }
1933  else
1934    {
1935      /* We may have created entries in the .rela.got, .rela.sdata, and
1936	 .rela.sdata2 sections.  However, if we are not creating the
1937	 dynamic sections, we will not actually use these entries.  Reset
1938	 the size of .rela.got, et al, which will cause it to get
1939	 stripped from the output file below.  */
1940      static char *rela_sections[] = { ".rela.got", ".rela.sdata",
1941				       ".rela.sdata2", ".rela.sbss",
1942				       (char *) 0 };
1943      char **p;
1944
1945      for (p = rela_sections; *p != (char *) 0; p++)
1946	{
1947	  s = bfd_get_section_by_name (dynobj, *p);
1948	  if (s != NULL)
1949	    s->_raw_size = 0;
1950	}
1951    }
1952
1953  /* The check_relocs and adjust_dynamic_symbol entry points have
1954     determined the sizes of the various dynamic sections.  Allocate
1955     memory for them.  */
1956  plt = false;
1957  relocs = false;
1958  for (s = dynobj->sections; s != NULL; s = s->next)
1959    {
1960      const char *name;
1961      boolean strip;
1962
1963      if ((s->flags & SEC_LINKER_CREATED) == 0)
1964	continue;
1965
1966      /* It's OK to base decisions on the section name, because none
1967	 of the dynobj section names depend upon the input files.  */
1968      name = bfd_get_section_name (dynobj, s);
1969
1970      strip = false;
1971
1972      if (strcmp (name, ".plt") == 0)
1973	{
1974	  if (s->_raw_size == 0)
1975	    {
1976	      /* Strip this section if we don't need it; see the
1977                 comment below.  */
1978	      strip = true;
1979	    }
1980	  else
1981	    {
1982	      /* Remember whether there is a PLT.  */
1983	      plt = true;
1984	    }
1985	}
1986      else if (strncmp (name, ".rela", 5) == 0)
1987	{
1988	  if (s->_raw_size == 0)
1989	    {
1990	      /* If we don't need this section, strip it from the
1991		 output file.  This is mostly to handle .rela.bss and
1992		 .rela.plt.  We must create both sections in
1993		 create_dynamic_sections, because they must be created
1994		 before the linker maps input sections to output
1995		 sections.  The linker does that before
1996		 adjust_dynamic_symbol is called, and it is that
1997		 function which decides whether anything needs to go
1998		 into these sections.  */
1999	      strip = true;
2000	    }
2001	  else
2002	    {
2003	      /* Remember whether there are any relocation sections.  */
2004	      relocs = true;
2005
2006	      /* We use the reloc_count field as a counter if we need
2007		 to copy relocs into the output file.  */
2008	      s->reloc_count = 0;
2009	    }
2010	}
2011      else if (strcmp (name, ".got") != 0
2012	       && strcmp (name, ".sdata") != 0
2013	       && strcmp (name, ".sdata2") != 0)
2014	{
2015	  /* It's not one of our sections, so don't allocate space.  */
2016	  continue;
2017	}
2018
2019      if (strip)
2020	{
2021	  _bfd_strip_section_from_output (info, s);
2022	  continue;
2023	}
2024
2025      /* Allocate memory for the section contents.  */
2026      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2027      if (s->contents == NULL && s->_raw_size != 0)
2028	return false;
2029    }
2030
2031  if (elf_hash_table (info)->dynamic_sections_created)
2032    {
2033      /* Add some entries to the .dynamic section.  We fill in the
2034	 values later, in ppc_elf_finish_dynamic_sections, but we
2035	 must add the entries now so that we get the correct size for
2036	 the .dynamic section.  The DT_DEBUG entry is filled in by the
2037	 dynamic linker and used by the debugger.  */
2038#define add_dynamic_entry(TAG, VAL) \
2039  bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2040
2041      if (!info->shared)
2042	{
2043	  if (!add_dynamic_entry (DT_DEBUG, 0))
2044	    return false;
2045	}
2046
2047      if (plt)
2048	{
2049	  if (!add_dynamic_entry (DT_PLTGOT, 0)
2050	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
2051	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2052	      || !add_dynamic_entry (DT_JMPREL, 0))
2053	    return false;
2054	}
2055
2056      if (relocs)
2057	{
2058	  if (!add_dynamic_entry (DT_RELA, 0)
2059	      || !add_dynamic_entry (DT_RELASZ, 0)
2060	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2061	    return false;
2062	}
2063
2064      if ((info->flags & DF_TEXTREL) != 0)
2065	{
2066	  if (!add_dynamic_entry (DT_TEXTREL, 0))
2067	    return false;
2068	  info->flags |= DF_TEXTREL;
2069	}
2070    }
2071#undef add_dynamic_entry
2072
2073  return true;
2074}
2075
2076/* Look through the relocs for a section during the first phase, and
2077   allocate space in the global offset table or procedure linkage
2078   table.  */
2079
2080static boolean
2081ppc_elf_check_relocs (abfd, info, sec, relocs)
2082     bfd *abfd;
2083     struct bfd_link_info *info;
2084     asection *sec;
2085     const Elf_Internal_Rela *relocs;
2086{
2087  bfd *dynobj;
2088  Elf_Internal_Shdr *symtab_hdr;
2089  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2090  const Elf_Internal_Rela *rel;
2091  const Elf_Internal_Rela *rel_end;
2092  bfd_signed_vma *local_got_refcounts;
2093  elf_linker_section_t *sdata;
2094  elf_linker_section_t *sdata2;
2095  asection *sreloc;
2096  asection *sgot = NULL;
2097  asection *srelgot = NULL;
2098
2099  if (info->relocateable)
2100    return true;
2101
2102#ifdef DEBUG
2103  fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2104	   bfd_get_section_name (abfd, sec),
2105	   bfd_archive_filename (abfd));
2106#endif
2107
2108  /* Create the linker generated sections all the time so that the
2109     special symbols are created.  */
2110
2111  if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2112    {
2113      sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2114      if (!sdata)
2115	return false;
2116    }
2117
2118  if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2119    {
2120      sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2121      if (!sdata2)
2122	return false;
2123    }
2124
2125  dynobj = elf_hash_table (info)->dynobj;
2126  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2127  local_got_refcounts = elf_local_got_refcounts (abfd);
2128
2129  sym_hashes = elf_sym_hashes (abfd);
2130  sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2131  if (!elf_bad_symtab (abfd))
2132    sym_hashes_end -= symtab_hdr->sh_info;
2133
2134  sreloc = NULL;
2135
2136  rel_end = relocs + sec->reloc_count;
2137  for (rel = relocs; rel < rel_end; rel++)
2138    {
2139      unsigned long r_symndx;
2140      struct elf_link_hash_entry *h;
2141
2142      r_symndx = ELF32_R_SYM (rel->r_info);
2143      if (r_symndx < symtab_hdr->sh_info)
2144	h = NULL;
2145      else
2146	h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2147
2148      /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2149	 This shows up in particular in an R_PPC_ADDR32 in the eabi
2150	 startup code.  */
2151      if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2152	{
2153	  if (sgot == NULL)
2154	    {
2155	      if (dynobj == NULL)
2156		elf_hash_table (info)->dynobj = dynobj = abfd;
2157	      sgot = ppc_elf_create_got (dynobj, info);
2158	      if (sgot == NULL)
2159		return false;
2160	    }
2161	}
2162
2163      switch (ELF32_R_TYPE (rel->r_info))
2164	{
2165	/* GOT16 relocations */
2166	case R_PPC_GOT16:
2167	case R_PPC_GOT16_LO:
2168	case R_PPC_GOT16_HI:
2169	case R_PPC_GOT16_HA:
2170	  /* This symbol requires a global offset table entry.  */
2171
2172	  if (sgot == NULL)
2173	    {
2174	      if (dynobj == NULL)
2175		elf_hash_table (info)->dynobj = dynobj = abfd;
2176	      sgot = ppc_elf_create_got (dynobj, info);
2177	      if (sgot == NULL)
2178		return false;
2179	    }
2180
2181	  if (srelgot == NULL
2182	      && (h != NULL || info->shared))
2183	    {
2184	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2185	      if (srelgot == NULL)
2186		{
2187		  srelgot = bfd_make_section (dynobj, ".rela.got");
2188		  if (srelgot == NULL
2189		      || ! bfd_set_section_flags (dynobj, srelgot,
2190						  (SEC_ALLOC
2191						   | SEC_LOAD
2192						   | SEC_HAS_CONTENTS
2193						   | SEC_IN_MEMORY
2194						   | SEC_LINKER_CREATED
2195						   | SEC_READONLY))
2196		      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2197		    return false;
2198		}
2199	    }
2200
2201	  if (h != NULL)
2202	    {
2203	      if (h->got.refcount == 0)
2204		{
2205		  /* Make sure this symbol is output as a dynamic symbol.  */
2206		  if (h->dynindx == -1)
2207		    if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2208		      return false;
2209
2210		  /* Allocate space in the .got.  */
2211		  sgot->_raw_size += 4;
2212		  /* Allocate relocation space.  */
2213		  srelgot->_raw_size += sizeof (Elf32_External_Rela);
2214		}
2215	      h->got.refcount++;
2216	    }
2217	  else
2218	    {
2219	      /* This is a global offset table entry for a local symbol.  */
2220	      if (local_got_refcounts == NULL)
2221		{
2222		  bfd_size_type size;
2223
2224		  size = symtab_hdr->sh_info;
2225		  size *= sizeof (bfd_signed_vma);
2226		  local_got_refcounts
2227		    = (bfd_signed_vma *) bfd_zalloc (abfd, size);
2228		  if (local_got_refcounts == NULL)
2229		    return false;
2230		  elf_local_got_refcounts (abfd) = local_got_refcounts;
2231		}
2232	      if (local_got_refcounts[r_symndx] == 0)
2233		{
2234		  sgot->_raw_size += 4;
2235
2236		  /* If we are generating a shared object, we need to
2237                     output a R_PPC_RELATIVE reloc so that the
2238                     dynamic linker can adjust this GOT entry.  */
2239		  if (info->shared)
2240		    srelgot->_raw_size += sizeof (Elf32_External_Rela);
2241		}
2242	      local_got_refcounts[r_symndx]++;
2243	    }
2244	  break;
2245
2246	/* Indirect .sdata relocation */
2247	case R_PPC_EMB_SDAI16:
2248	  if (info->shared)
2249	    {
2250	      ((*_bfd_error_handler)
2251	       (_("%s: relocation %s cannot be used when making a shared object"),
2252		bfd_archive_filename (abfd), "R_PPC_EMB_SDAI16"));
2253	      return false;
2254	    }
2255
2256	  if (srelgot == NULL && (h != NULL || info->shared))
2257	    {
2258	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2259	      if (srelgot == NULL)
2260		{
2261		  srelgot = bfd_make_section (dynobj, ".rela.got");
2262		  if (srelgot == NULL
2263		      || ! bfd_set_section_flags (dynobj, srelgot,
2264						  (SEC_ALLOC
2265						   | SEC_LOAD
2266						   | SEC_HAS_CONTENTS
2267						   | SEC_IN_MEMORY
2268						   | SEC_LINKER_CREATED
2269						   | SEC_READONLY))
2270		      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2271		    return false;
2272		}
2273	    }
2274
2275	  if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2276	    return false;
2277
2278	  break;
2279
2280	/* Indirect .sdata2 relocation */
2281	case R_PPC_EMB_SDA2I16:
2282	  if (info->shared)
2283	    {
2284	      ((*_bfd_error_handler)
2285	       (_("%s: relocation %s cannot be used when making a shared object"),
2286		bfd_archive_filename (abfd), "R_PPC_EMB_SDA2I16"));
2287	      return false;
2288	    }
2289
2290	  if (srelgot == NULL && (h != NULL || info->shared))
2291	    {
2292	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2293	      if (srelgot == NULL)
2294		{
2295		  srelgot = bfd_make_section (dynobj, ".rela.got");
2296		  if (srelgot == NULL
2297		      || ! bfd_set_section_flags (dynobj, srelgot,
2298						  (SEC_ALLOC
2299						   | SEC_LOAD
2300						   | SEC_HAS_CONTENTS
2301						   | SEC_IN_MEMORY
2302						   | SEC_LINKER_CREATED
2303						   | SEC_READONLY))
2304		      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2305		    return false;
2306		}
2307	    }
2308
2309	  if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2310	    return false;
2311
2312	  break;
2313
2314	case R_PPC_SDAREL16:
2315	case R_PPC_EMB_SDA2REL:
2316	case R_PPC_EMB_SDA21:
2317	  if (info->shared)
2318	    {
2319	      ((*_bfd_error_handler)
2320	       (_("%s: relocation %s cannot be used when making a shared object"),
2321		bfd_archive_filename (abfd),
2322		ppc_elf_howto_table[(int) ELF32_R_TYPE (rel->r_info)]->name));
2323	      return false;
2324	    }
2325	  break;
2326
2327	case R_PPC_PLT32:
2328	case R_PPC_PLTREL24:
2329	case R_PPC_PLT16_LO:
2330	case R_PPC_PLT16_HI:
2331	case R_PPC_PLT16_HA:
2332#ifdef DEBUG
2333	  fprintf (stderr, "Reloc requires a PLT entry\n");
2334#endif
2335	  /* This symbol requires a procedure linkage table entry.  We
2336             actually build the entry in adjust_dynamic_symbol,
2337             because this might be a case of linking PIC code without
2338             linking in any dynamic objects, in which case we don't
2339             need to generate a procedure linkage table after all.  */
2340
2341	  if (h == NULL)
2342	    {
2343	      /* It does not make sense to have a procedure linkage
2344                 table entry for a local symbol.  */
2345	      bfd_set_error (bfd_error_bad_value);
2346	      return false;
2347	    }
2348
2349	  /* Make sure this symbol is output as a dynamic symbol.  */
2350	  if (h->dynindx == -1)
2351	    {
2352	      if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2353		return false;
2354	    }
2355	  h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2356	  h->plt.refcount++;
2357	  break;
2358
2359	  /* The following relocations don't need to propagate the
2360	     relocation if linking a shared object since they are
2361	     section relative.  */
2362	case R_PPC_SECTOFF:
2363	case R_PPC_SECTOFF_LO:
2364	case R_PPC_SECTOFF_HI:
2365	case R_PPC_SECTOFF_HA:
2366	  break;
2367
2368	  /* This refers only to functions defined in the shared library */
2369	case R_PPC_LOCAL24PC:
2370	  break;
2371
2372	  /* This relocation describes the C++ object vtable hierarchy.
2373	     Reconstruct it for later use during GC.  */
2374	case R_PPC_GNU_VTINHERIT:
2375	  if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2376	    return false;
2377	  break;
2378
2379	  /* This relocation describes which C++ vtable entries are actually
2380	     used.  Record for later use during GC.  */
2381	case R_PPC_GNU_VTENTRY:
2382	  if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2383	    return false;
2384	  break;
2385
2386	  /* When creating a shared object, we must copy these
2387	     relocs into the output file.  We create a reloc
2388	     section in dynobj and make room for the reloc.  */
2389	case R_PPC_REL24:
2390	case R_PPC_REL14:
2391	case R_PPC_REL14_BRTAKEN:
2392	case R_PPC_REL14_BRNTAKEN:
2393	case R_PPC_REL32:
2394	  if (h == NULL
2395	      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2396	      || SYMBOL_REFERENCES_LOCAL (info, h))
2397	    break;
2398	  /* fall through */
2399
2400	default:
2401	  if (info->shared)
2402	    {
2403#ifdef DEBUG
2404	      fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2405		       (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2406#endif
2407	      if (sreloc == NULL)
2408		{
2409		  const char *name;
2410
2411		  name = (bfd_elf_string_from_elf_section
2412			  (abfd,
2413			   elf_elfheader (abfd)->e_shstrndx,
2414			   elf_section_data (sec)->rel_hdr.sh_name));
2415		  if (name == NULL)
2416		    return false;
2417
2418		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2419			      && strcmp (bfd_get_section_name (abfd, sec),
2420					 name + 5) == 0);
2421
2422		  sreloc = bfd_get_section_by_name (dynobj, name);
2423		  if (sreloc == NULL)
2424		    {
2425		      flagword flags;
2426
2427		      sreloc = bfd_make_section (dynobj, name);
2428		      flags = (SEC_HAS_CONTENTS | SEC_READONLY
2429			       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2430		      if ((sec->flags & SEC_ALLOC) != 0)
2431			flags |= SEC_ALLOC | SEC_LOAD;
2432		      if (sreloc == NULL
2433			  || ! bfd_set_section_flags (dynobj, sreloc, flags)
2434			  || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2435			return false;
2436		    }
2437		  if (sec->flags & SEC_READONLY)
2438		    info->flags |= DF_TEXTREL;
2439		}
2440
2441	      sreloc->_raw_size += sizeof (Elf32_External_Rela);
2442
2443	      /* FIXME: We should here do what the m68k and i386
2444		 backends do: if the reloc is pc-relative, record it
2445		 in case it turns out that the reloc is unnecessary
2446		 because the symbol is forced local by versioning or
2447		 we are linking with -Bdynamic.  Fortunately this
2448		 case is not frequent.  */
2449	    }
2450
2451	  break;
2452	}
2453    }
2454
2455  return true;
2456}
2457
2458/* Return the section that should be marked against GC for a given
2459   relocation.  */
2460
2461static asection *
2462ppc_elf_gc_mark_hook (abfd, info, rel, h, sym)
2463     bfd *abfd;
2464     struct bfd_link_info *info ATTRIBUTE_UNUSED;
2465     Elf_Internal_Rela *rel;
2466     struct elf_link_hash_entry *h;
2467     Elf_Internal_Sym *sym;
2468{
2469  if (h != NULL)
2470    {
2471      switch (ELF32_R_TYPE (rel->r_info))
2472	{
2473	case R_PPC_GNU_VTINHERIT:
2474	case R_PPC_GNU_VTENTRY:
2475	  break;
2476
2477	default:
2478	  switch (h->root.type)
2479	    {
2480	    case bfd_link_hash_defined:
2481	    case bfd_link_hash_defweak:
2482	      return h->root.u.def.section;
2483
2484	    case bfd_link_hash_common:
2485	      return h->root.u.c.p->section;
2486
2487	    default:
2488	      break;
2489	    }
2490	}
2491    }
2492  else
2493    {
2494      return bfd_section_from_elf_index (abfd, sym->st_shndx);
2495    }
2496
2497  return NULL;
2498}
2499
2500/* Update the got entry reference counts for the section being removed.  */
2501
2502static boolean
2503ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2504     bfd *abfd;
2505     struct bfd_link_info *info ATTRIBUTE_UNUSED;
2506     asection *sec;
2507     const Elf_Internal_Rela *relocs;
2508{
2509  Elf_Internal_Shdr *symtab_hdr;
2510  struct elf_link_hash_entry **sym_hashes;
2511  bfd_signed_vma *local_got_refcounts;
2512  const Elf_Internal_Rela *rel, *relend;
2513  unsigned long r_symndx;
2514  struct elf_link_hash_entry *h;
2515
2516  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2517  sym_hashes = elf_sym_hashes (abfd);
2518  local_got_refcounts = elf_local_got_refcounts (abfd);
2519
2520  relend = relocs + sec->reloc_count;
2521  for (rel = relocs; rel < relend; rel++)
2522    switch (ELF32_R_TYPE (rel->r_info))
2523      {
2524      case R_PPC_GOT16:
2525      case R_PPC_GOT16_LO:
2526      case R_PPC_GOT16_HI:
2527      case R_PPC_GOT16_HA:
2528	r_symndx = ELF32_R_SYM (rel->r_info);
2529	if (r_symndx >= symtab_hdr->sh_info)
2530	  {
2531	    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2532	    if (h->got.refcount > 0)
2533	      h->got.refcount--;
2534	  }
2535	else if (local_got_refcounts != NULL)
2536	  {
2537	    if (local_got_refcounts[r_symndx] > 0)
2538	      local_got_refcounts[r_symndx]--;
2539	  }
2540        break;
2541
2542      case R_PPC_PLT32:
2543      case R_PPC_PLTREL24:
2544      case R_PPC_PLT16_LO:
2545      case R_PPC_PLT16_HI:
2546      case R_PPC_PLT16_HA:
2547	r_symndx = ELF32_R_SYM (rel->r_info);
2548	if (r_symndx >= symtab_hdr->sh_info)
2549	  {
2550	    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2551	    if (h->plt.refcount > 0)
2552	      h->plt.refcount--;
2553	  }
2554	break;
2555
2556      default:
2557	break;
2558      }
2559
2560  return true;
2561}
2562
2563/* Hook called by the linker routine which adds symbols from an object
2564   file.  We use it to put .comm items in .sbss, and not .bss.  */
2565
2566static boolean
2567ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2568     bfd *abfd;
2569     struct bfd_link_info *info;
2570     const Elf_Internal_Sym *sym;
2571     const char **namep ATTRIBUTE_UNUSED;
2572     flagword *flagsp ATTRIBUTE_UNUSED;
2573     asection **secp;
2574     bfd_vma *valp;
2575{
2576  if (sym->st_shndx == SHN_COMMON
2577      && !info->relocateable
2578      && sym->st_size <= elf_gp_size (abfd)
2579      && info->hash->creator->flavour == bfd_target_elf_flavour)
2580    {
2581      /* Common symbols less than or equal to -G nn bytes are automatically
2582	 put into .sdata.  */
2583      elf_linker_section_t *sdata
2584	= ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2585
2586      if (!sdata->bss_section)
2587	{
2588	  bfd_size_type amt;
2589
2590	  /* We don't go through bfd_make_section, because we don't
2591             want to attach this common section to DYNOBJ.  The linker
2592             will move the symbols to the appropriate output section
2593             when it defines common symbols.  */
2594	  amt = sizeof (asection);
2595	  sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
2596	  if (sdata->bss_section == NULL)
2597	    return false;
2598	  sdata->bss_section->name = sdata->bss_name;
2599	  sdata->bss_section->flags = SEC_IS_COMMON;
2600	  sdata->bss_section->output_section = sdata->bss_section;
2601	  amt = sizeof (asymbol);
2602	  sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
2603	  amt = sizeof (asymbol *);
2604	  sdata->bss_section->symbol_ptr_ptr =
2605	    (asymbol **) bfd_zalloc (abfd, amt);
2606	  if (sdata->bss_section->symbol == NULL
2607	      || sdata->bss_section->symbol_ptr_ptr == NULL)
2608	    return false;
2609	  sdata->bss_section->symbol->name = sdata->bss_name;
2610	  sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2611	  sdata->bss_section->symbol->section = sdata->bss_section;
2612	  *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2613	}
2614
2615      *secp = sdata->bss_section;
2616      *valp = sym->st_size;
2617    }
2618
2619  return true;
2620}
2621
2622/* Finish up dynamic symbol handling.  We set the contents of various
2623   dynamic sections here.  */
2624
2625static boolean
2626ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2627     bfd *output_bfd;
2628     struct bfd_link_info *info;
2629     struct elf_link_hash_entry *h;
2630     Elf_Internal_Sym *sym;
2631{
2632  bfd *dynobj;
2633
2634#ifdef DEBUG
2635  fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2636	   h->root.root.string);
2637#endif
2638
2639  dynobj = elf_hash_table (info)->dynobj;
2640  BFD_ASSERT (dynobj != NULL);
2641
2642  if (h->plt.offset != (bfd_vma) -1)
2643    {
2644      asection *splt;
2645      asection *srela;
2646      Elf_Internal_Rela rela;
2647      bfd_vma reloc_index;
2648
2649#ifdef DEBUG
2650      fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2651#endif
2652
2653      /* This symbol has an entry in the procedure linkage table.  Set
2654         it up.  */
2655
2656      BFD_ASSERT (h->dynindx != -1);
2657
2658      splt = bfd_get_section_by_name (dynobj, ".plt");
2659      srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2660      BFD_ASSERT (splt != NULL && srela != NULL);
2661
2662      /* We don't need to fill in the .plt.  The ppc dynamic linker
2663	 will fill it in.  */
2664
2665      /* Fill in the entry in the .rela.plt section.  */
2666      rela.r_offset = (splt->output_section->vma
2667		       + splt->output_offset
2668		       + h->plt.offset);
2669      rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2670      rela.r_addend = 0;
2671
2672      reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2673      if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2674	reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2675      bfd_elf32_swap_reloca_out (output_bfd, &rela,
2676				 ((Elf32_External_Rela *) srela->contents
2677				  + reloc_index));
2678
2679      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2680	{
2681	  /* Mark the symbol as undefined, rather than as defined in
2682	     the .plt section.  Leave the value alone.  */
2683	  sym->st_shndx = SHN_UNDEF;
2684	  /* If the symbol is weak, we do need to clear the value.
2685	     Otherwise, the PLT entry would provide a definition for
2686	     the symbol even if the symbol wasn't defined anywhere,
2687	     and so the symbol would never be NULL.  */
2688	  if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
2689	      == 0)
2690	    sym->st_value = 0;
2691	}
2692    }
2693
2694  if (h->got.offset != (bfd_vma) -1)
2695    {
2696      asection *sgot;
2697      asection *srela;
2698      Elf_Internal_Rela rela;
2699
2700      /* This symbol has an entry in the global offset table.  Set it
2701         up.  */
2702
2703      sgot = bfd_get_section_by_name (dynobj, ".got");
2704      srela = bfd_get_section_by_name (dynobj, ".rela.got");
2705      BFD_ASSERT (sgot != NULL && srela != NULL);
2706
2707      rela.r_offset = (sgot->output_section->vma
2708		       + sgot->output_offset
2709		       + (h->got.offset &~ (bfd_vma) 1));
2710
2711      /* If this is a -Bsymbolic link, and the symbol is defined
2712	 locally, we just want to emit a RELATIVE reloc.  The entry in
2713	 the global offset table will already have been initialized in
2714	 the relocate_section function.  */
2715      if (info->shared
2716	  && SYMBOL_REFERENCES_LOCAL (info, h))
2717	{
2718	  rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
2719	  rela.r_addend = (h->root.u.def.value
2720			   + h->root.u.def.section->output_section->vma
2721			   + h->root.u.def.section->output_offset);
2722	}
2723      else
2724	{
2725	  BFD_ASSERT ((h->got.offset & 1) == 0);
2726	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2727	  rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
2728	  rela.r_addend = 0;
2729	}
2730
2731      bfd_elf32_swap_reloca_out (output_bfd, &rela,
2732				 ((Elf32_External_Rela *) srela->contents
2733				  + srela->reloc_count));
2734      ++srela->reloc_count;
2735    }
2736
2737  if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2738    {
2739      asection *s;
2740      Elf_Internal_Rela rela;
2741
2742      /* This symbols needs a copy reloc.  Set it up.  */
2743
2744#ifdef DEBUG
2745      fprintf (stderr, ", copy");
2746#endif
2747
2748      BFD_ASSERT (h->dynindx != -1);
2749
2750      if (h->size <= elf_gp_size (dynobj))
2751	s = bfd_get_section_by_name (h->root.u.def.section->owner,
2752				     ".rela.sbss");
2753      else
2754	s = bfd_get_section_by_name (h->root.u.def.section->owner,
2755				     ".rela.bss");
2756      BFD_ASSERT (s != NULL);
2757
2758      rela.r_offset = (h->root.u.def.value
2759		       + h->root.u.def.section->output_section->vma
2760		       + h->root.u.def.section->output_offset);
2761      rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
2762      rela.r_addend = 0;
2763      bfd_elf32_swap_reloca_out (output_bfd, &rela,
2764				 ((Elf32_External_Rela *) s->contents
2765				  + s->reloc_count));
2766      ++s->reloc_count;
2767    }
2768
2769#ifdef DEBUG
2770  fprintf (stderr, "\n");
2771#endif
2772
2773  /* Mark some specially defined symbols as absolute.  */
2774  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2775      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2776      || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2777    sym->st_shndx = SHN_ABS;
2778
2779  return true;
2780}
2781
2782/* Finish up the dynamic sections.  */
2783
2784static boolean
2785ppc_elf_finish_dynamic_sections (output_bfd, info)
2786     bfd *output_bfd;
2787     struct bfd_link_info *info;
2788{
2789  asection *sdyn;
2790  bfd *dynobj = elf_hash_table (info)->dynobj;
2791  asection *sgot = bfd_get_section_by_name (dynobj, ".got");
2792
2793#ifdef DEBUG
2794  fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
2795#endif
2796
2797  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2798
2799  if (elf_hash_table (info)->dynamic_sections_created)
2800    {
2801      asection *splt;
2802      Elf32_External_Dyn *dyncon, *dynconend;
2803
2804      splt = bfd_get_section_by_name (dynobj, ".plt");
2805      BFD_ASSERT (splt != NULL && sdyn != NULL);
2806
2807      dyncon = (Elf32_External_Dyn *) sdyn->contents;
2808      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2809      for (; dyncon < dynconend; dyncon++)
2810	{
2811	  Elf_Internal_Dyn dyn;
2812	  const char *name;
2813	  boolean size;
2814
2815	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2816
2817	  switch (dyn.d_tag)
2818	    {
2819	    case DT_PLTGOT:   name = ".plt";	  size = false; break;
2820	    case DT_PLTRELSZ: name = ".rela.plt"; size = true;  break;
2821	    case DT_JMPREL:   name = ".rela.plt"; size = false; break;
2822	    default:	      name = NULL;	  size = false; break;
2823	    }
2824
2825	  if (name != NULL)
2826	    {
2827	      asection *s;
2828
2829	      s = bfd_get_section_by_name (output_bfd, name);
2830	      if (s == NULL)
2831		dyn.d_un.d_val = 0;
2832	      else
2833		{
2834		  if (! size)
2835		    dyn.d_un.d_ptr = s->vma;
2836		  else
2837		    {
2838		      if (s->_cooked_size != 0)
2839			dyn.d_un.d_val = s->_cooked_size;
2840		      else
2841			dyn.d_un.d_val = s->_raw_size;
2842		    }
2843		}
2844	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2845	    }
2846	}
2847    }
2848
2849  /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
2850     easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
2851  if (sgot)
2852    {
2853      unsigned char *contents = sgot->contents;
2854      bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
2855
2856      if (sdyn == NULL)
2857	bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
2858      else
2859	bfd_put_32 (output_bfd,
2860		    sdyn->output_section->vma + sdyn->output_offset,
2861		    contents+4);
2862
2863      elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2864    }
2865
2866  return true;
2867}
2868
2869/* The RELOCATE_SECTION function is called by the ELF backend linker
2870   to handle the relocations for a section.
2871
2872   The relocs are always passed as Rela structures; if the section
2873   actually uses Rel structures, the r_addend field will always be
2874   zero.
2875
2876   This function is responsible for adjust the section contents as
2877   necessary, and (if using Rela relocs and generating a
2878   relocateable output file) adjusting the reloc addend as
2879   necessary.
2880
2881   This function does not have to worry about setting the reloc
2882   address or the reloc symbol index.
2883
2884   LOCAL_SYMS is a pointer to the swapped in local symbols.
2885
2886   LOCAL_SECTIONS is an array giving the section in the input file
2887   corresponding to the st_shndx field of each local symbol.
2888
2889   The global hash table entry for the global symbols can be found
2890   via elf_sym_hashes (input_bfd).
2891
2892   When generating relocateable output, this function must handle
2893   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2894   going to be the section symbol corresponding to the output
2895   section, which means that the addend must be adjusted
2896   accordingly.  */
2897
2898static boolean
2899ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2900			  contents, relocs, local_syms, local_sections)
2901     bfd *output_bfd;
2902     struct bfd_link_info *info;
2903     bfd *input_bfd;
2904     asection *input_section;
2905     bfd_byte *contents;
2906     Elf_Internal_Rela *relocs;
2907     Elf_Internal_Sym *local_syms;
2908     asection **local_sections;
2909{
2910  Elf_Internal_Shdr *symtab_hdr		  = &elf_tdata (input_bfd)->symtab_hdr;
2911  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2912  bfd *dynobj				  = elf_hash_table (info)->dynobj;
2913  elf_linker_section_t *sdata		  = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA)  : NULL;
2914  elf_linker_section_t *sdata2		  = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA2) : NULL;
2915  Elf_Internal_Rela *rel		  = relocs;
2916  Elf_Internal_Rela *relend		  = relocs + input_section->reloc_count;
2917  asection *sreloc			  = NULL;
2918  asection *splt;
2919  asection *sgot;
2920  bfd_vma *local_got_offsets;
2921  boolean ret				  = true;
2922  long insn;
2923
2924#ifdef DEBUG
2925  fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
2926	   bfd_archive_filename (input_bfd),
2927	   bfd_section_name(input_bfd, input_section),
2928	   (long) input_section->reloc_count,
2929	   (info->relocateable) ? " (relocatable)" : "");
2930#endif
2931
2932  if (info->relocateable)
2933    return true;
2934
2935  if (!ppc_elf_howto_table[R_PPC_ADDR32])
2936    /* Initialize howto table if needed.  */
2937    ppc_elf_howto_init ();
2938
2939  local_got_offsets = elf_local_got_offsets (input_bfd);
2940
2941  splt = sgot = NULL;
2942  if (dynobj != NULL)
2943    {
2944      splt = bfd_get_section_by_name (dynobj, ".plt");
2945      sgot = bfd_get_section_by_name (dynobj, ".got");
2946    }
2947
2948  for (; rel < relend; rel++)
2949    {
2950      enum elf_ppc_reloc_type r_type	= (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
2951      bfd_vma offset			= rel->r_offset;
2952      bfd_vma addend			= rel->r_addend;
2953      bfd_reloc_status_type r		= bfd_reloc_other;
2954      Elf_Internal_Sym *sym		= (Elf_Internal_Sym *) 0;
2955      asection *sec			= (asection *) 0;
2956      struct elf_link_hash_entry *h	= (struct elf_link_hash_entry *) 0;
2957      const char *sym_name		= (const char *) 0;
2958      reloc_howto_type *howto;
2959      unsigned long r_symndx;
2960      bfd_vma relocation;
2961      int will_become_local;
2962
2963      /* Unknown relocation handling */
2964      if ((unsigned) r_type >= (unsigned) R_PPC_max
2965	  || !ppc_elf_howto_table[(int) r_type])
2966	{
2967	  (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2968				 bfd_archive_filename (input_bfd),
2969				 (int) r_type);
2970
2971	  bfd_set_error (bfd_error_bad_value);
2972	  ret = false;
2973	  continue;
2974	}
2975
2976      howto = ppc_elf_howto_table[(int) r_type];
2977      r_symndx = ELF32_R_SYM (rel->r_info);
2978
2979      if (r_symndx < symtab_hdr->sh_info)
2980	{
2981	  sym = local_syms + r_symndx;
2982	  sec = local_sections[r_symndx];
2983	  sym_name = "<local symbol>";
2984
2985	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
2986	  addend = rel->r_addend;
2987	  /* Relocs to local symbols are always resolved.  */
2988	  will_become_local = 1;
2989	}
2990      else
2991	{
2992	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2993	  while (h->root.type == bfd_link_hash_indirect
2994		 || h->root.type == bfd_link_hash_warning)
2995	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2996	  sym_name = h->root.root.string;
2997
2998	  /* Can this relocation be resolved immediately?  */
2999	  will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
3000
3001	  if (h->root.type == bfd_link_hash_defined
3002	      || h->root.type == bfd_link_hash_defweak)
3003	    {
3004	      sec = h->root.u.def.section;
3005	      if (((r_type == R_PPC_PLT32
3006		    || r_type == R_PPC_PLTREL24)
3007		   && splt != NULL
3008		   && h->plt.offset != (bfd_vma) -1)
3009		  || (r_type == R_PPC_LOCAL24PC
3010		      && sec->output_section == NULL)
3011		  || ((r_type == R_PPC_GOT16
3012		       || r_type == R_PPC_GOT16_LO
3013		       || r_type == R_PPC_GOT16_HI
3014		       || r_type == R_PPC_GOT16_HA)
3015		      && elf_hash_table (info)->dynamic_sections_created
3016		      && (! info->shared || ! will_become_local))
3017		  || (info->shared
3018 		      && ! will_become_local
3019		      && ((input_section->flags & SEC_ALLOC) != 0
3020			  /* Testing SEC_DEBUGGING here may be wrong.
3021                             It's here to avoid a crash when
3022                             generating a shared library with DWARF
3023                             debugging information.  */
3024			  || ((input_section->flags & SEC_DEBUGGING) != 0
3025			      && (h->elf_link_hash_flags
3026				  & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3027		      && (r_type == R_PPC_ADDR32
3028			  || r_type == R_PPC_ADDR24
3029			  || r_type == R_PPC_ADDR16
3030			  || r_type == R_PPC_ADDR16_LO
3031			  || r_type == R_PPC_ADDR16_HI
3032			  || r_type == R_PPC_ADDR16_HA
3033			  || r_type == R_PPC_ADDR14
3034			  || r_type == R_PPC_ADDR14_BRTAKEN
3035			  || r_type == R_PPC_ADDR14_BRNTAKEN
3036			  || r_type == R_PPC_COPY
3037			  || r_type == R_PPC_GLOB_DAT
3038			  || r_type == R_PPC_JMP_SLOT
3039			  || r_type == R_PPC_UADDR32
3040			  || r_type == R_PPC_UADDR16
3041			  || r_type == R_PPC_SDAREL16
3042			  || r_type == R_PPC_EMB_NADDR32
3043			  || r_type == R_PPC_EMB_NADDR16
3044			  || r_type == R_PPC_EMB_NADDR16_LO
3045			  || r_type == R_PPC_EMB_NADDR16_HI
3046			  || r_type == R_PPC_EMB_NADDR16_HA
3047			  || r_type == R_PPC_EMB_SDAI16
3048			  || r_type == R_PPC_EMB_SDA2I16
3049			  || r_type == R_PPC_EMB_SDA2REL
3050			  || r_type == R_PPC_EMB_SDA21
3051			  || r_type == R_PPC_EMB_MRKREF
3052			  || r_type == R_PPC_EMB_BIT_FLD
3053			  || r_type == R_PPC_EMB_RELSDA
3054			  || ((r_type == R_PPC_REL24
3055			       || r_type == R_PPC_REL32
3056			       || r_type == R_PPC_REL14
3057			       || r_type == R_PPC_REL14_BRTAKEN
3058			       || r_type == R_PPC_REL14_BRNTAKEN
3059			       || r_type == R_PPC_RELATIVE)
3060			      && strcmp (h->root.root.string,
3061					 "_GLOBAL_OFFSET_TABLE_") != 0))))
3062		{
3063		  /* In these cases, we don't need the relocation
3064                     value.  We check specially because in some
3065                     obscure cases sec->output_section will be NULL.  */
3066		  relocation = 0;
3067		}
3068	      else if (sec->output_section == NULL)
3069		{
3070                  (*_bfd_error_handler)
3071                    (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3072                     bfd_archive_filename (input_bfd), h->root.root.string,
3073                     bfd_get_section_name (input_bfd, input_section));
3074		  relocation = 0;
3075		}
3076	      else
3077		relocation = (h->root.u.def.value
3078			      + sec->output_section->vma
3079			      + sec->output_offset);
3080	    }
3081	  else if (h->root.type == bfd_link_hash_undefweak)
3082	    relocation = 0;
3083	  else if (info->shared
3084		   && (!info->symbolic || info->allow_shlib_undefined)
3085		   && !info->no_undefined
3086		   && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3087	    relocation = 0;
3088	  else
3089	    {
3090	      if (! (*info->callbacks->undefined_symbol) (info,
3091							 h->root.root.string,
3092							 input_bfd,
3093							 input_section,
3094							 rel->r_offset,
3095							 (!info->shared
3096							  || info->no_undefined
3097							  || ELF_ST_VISIBILITY (h->other))))
3098		return false;
3099	      relocation = 0;
3100	    }
3101	}
3102
3103      switch ((int) r_type)
3104	{
3105	default:
3106	  (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3107				 bfd_archive_filename (input_bfd),
3108				 (int) r_type, sym_name);
3109
3110	  bfd_set_error (bfd_error_bad_value);
3111	  ret = false;
3112	  continue;
3113
3114	case (int) R_PPC_NONE:
3115	  continue;
3116
3117	/* Relocations that need no special processing.  */
3118	case (int) R_PPC_LOCAL24PC:
3119	  /* It makes no sense to point a local relocation
3120	     at a symbol not in this object.  */
3121	  if (h != NULL
3122	      && (h->root.type == bfd_link_hash_defined
3123		  || h->root.type == bfd_link_hash_defweak)
3124	      && sec->output_section == NULL)
3125	    {
3126	      if (! (*info->callbacks->undefined_symbol) (info,
3127							  h->root.root.string,
3128							  input_bfd,
3129							  input_section,
3130							  rel->r_offset,
3131							  true))
3132		return false;
3133	      continue;
3134	    }
3135	  break;
3136
3137	/* Relocations that may need to be propagated if this is a shared
3138           object.  */
3139	case (int) R_PPC_REL24:
3140	case (int) R_PPC_REL32:
3141	case (int) R_PPC_REL14:
3142	  /* If these relocations are not to a named symbol, they can be
3143	     handled right here, no need to bother the dynamic linker.  */
3144	  if (h == NULL
3145	      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3146	      || SYMBOL_REFERENCES_LOCAL (info, h))
3147	    break;
3148	/* fall through */
3149
3150	/* Relocations that always need to be propagated if this is a shared
3151           object.  */
3152	case (int) R_PPC_ADDR32:
3153	case (int) R_PPC_ADDR24:
3154	case (int) R_PPC_ADDR16:
3155	case (int) R_PPC_ADDR16_LO:
3156	case (int) R_PPC_ADDR16_HI:
3157	case (int) R_PPC_ADDR16_HA:
3158	case (int) R_PPC_ADDR14:
3159	case (int) R_PPC_UADDR32:
3160	case (int) R_PPC_UADDR16:
3161	  if (info->shared && r_symndx != 0)
3162	    {
3163	      Elf_Internal_Rela outrel;
3164	      int skip;
3165
3166#ifdef DEBUG
3167	      fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3168		       (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3169#endif
3170
3171	      /* When generating a shared object, these relocations
3172                 are copied into the output file to be resolved at run
3173                 time.  */
3174
3175	      if (sreloc == NULL)
3176		{
3177		  const char *name;
3178
3179		  name = (bfd_elf_string_from_elf_section
3180			  (input_bfd,
3181			   elf_elfheader (input_bfd)->e_shstrndx,
3182			   elf_section_data (input_section)->rel_hdr.sh_name));
3183		  if (name == NULL)
3184		    return false;
3185
3186		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3187			      && strcmp (bfd_get_section_name (input_bfd,
3188							       input_section),
3189					 name + 5) == 0);
3190
3191		  sreloc = bfd_get_section_by_name (dynobj, name);
3192		  BFD_ASSERT (sreloc != NULL);
3193		}
3194
3195	      skip = 0;
3196
3197	      outrel.r_offset =
3198		_bfd_elf_section_offset (output_bfd, info, input_section,
3199					 rel->r_offset);
3200	      if (outrel.r_offset == (bfd_vma) -1
3201		  || outrel.r_offset == (bfd_vma) -2)
3202		skip = (int) outrel.r_offset;
3203	      outrel.r_offset += (input_section->output_section->vma
3204				  + input_section->output_offset);
3205
3206	      if (skip)
3207		memset (&outrel, 0, sizeof outrel);
3208	      /* h->dynindx may be -1 if this symbol was marked to
3209                 become local.  */
3210	      else if (! will_become_local)
3211		{
3212		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3213		  outrel.r_addend = rel->r_addend;
3214		}
3215	      else
3216		{
3217		  if (r_type == R_PPC_ADDR32)
3218		    {
3219		      outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3220		      outrel.r_addend = relocation + rel->r_addend;
3221		    }
3222		  else
3223		    {
3224		      long indx;
3225
3226		      if (h == NULL)
3227			sec = local_sections[r_symndx];
3228		      else
3229			{
3230			  BFD_ASSERT (h->root.type == bfd_link_hash_defined
3231				      || (h->root.type
3232					  == bfd_link_hash_defweak));
3233			  sec = h->root.u.def.section;
3234			}
3235		      if (sec != NULL && bfd_is_abs_section (sec))
3236			indx = 0;
3237		      else if (sec == NULL || sec->owner == NULL)
3238			{
3239			  bfd_set_error (bfd_error_bad_value);
3240			  return false;
3241			}
3242		      else
3243			{
3244			  asection *osec;
3245
3246			  osec = sec->output_section;
3247			  indx = elf_section_data (osec)->dynindx;
3248			  BFD_ASSERT (indx > 0);
3249#ifdef DEBUG
3250			  if (indx <= 0)
3251			    {
3252			      printf ("indx=%d section=%s flags=%08x name=%s\n",
3253				     indx, osec->name, osec->flags,
3254				     h->root.root.string);
3255			    }
3256#endif
3257			}
3258
3259		      outrel.r_info = ELF32_R_INFO (indx, r_type);
3260		      outrel.r_addend = relocation + rel->r_addend;
3261		    }
3262		}
3263
3264	      bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3265					 (((Elf32_External_Rela *)
3266					   sreloc->contents)
3267					  + sreloc->reloc_count));
3268	      ++sreloc->reloc_count;
3269
3270	      /* This reloc will be computed at runtime, so there's no
3271                 need to do anything now, unless this is a RELATIVE
3272                 reloc in an unallocated section.  */
3273	      if (skip != -1
3274		  || (input_section->flags & SEC_ALLOC) != 0
3275		  || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE)
3276		continue;
3277	    }
3278
3279	  /* Arithmetic adjust relocations that aren't going into a
3280	     shared object.  */
3281	  if (r_type == R_PPC_ADDR16_HA
3282	      /* It's just possible that this symbol is a weak symbol
3283		 that's not actually defined anywhere. In that case,
3284		 'sec' would be NULL, and we should leave the symbol
3285		 alone (it will be set to zero elsewhere in the link).  */
3286	      && sec != NULL)
3287	    {
3288	      addend += ((relocation + addend) & 0x8000) << 1;
3289	    }
3290	  break;
3291
3292	/* branch taken prediction relocations */
3293	case (int) R_PPC_ADDR14_BRTAKEN:
3294	case (int) R_PPC_REL14_BRTAKEN:
3295	  insn = bfd_get_32 (output_bfd, contents + offset);
3296	  if ((relocation - offset) & 0x8000)
3297	    insn &= ~BRANCH_PREDICT_BIT;
3298	  else
3299	    insn |= BRANCH_PREDICT_BIT;
3300	  bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3301	  break;
3302
3303	/* branch not taken predicition relocations */
3304	case (int) R_PPC_ADDR14_BRNTAKEN:
3305	case (int) R_PPC_REL14_BRNTAKEN:
3306	  insn = bfd_get_32 (output_bfd, contents + offset);
3307	  if ((relocation - offset) & 0x8000)
3308	    insn |= BRANCH_PREDICT_BIT;
3309	  else
3310	    insn &= ~BRANCH_PREDICT_BIT;
3311	  bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3312	  break;
3313
3314	/* GOT16 relocations */
3315	case (int) R_PPC_GOT16:
3316	case (int) R_PPC_GOT16_LO:
3317	case (int) R_PPC_GOT16_HI:
3318	case (int) R_PPC_GOT16_HA:
3319	  /* Relocation is to the entry for this symbol in the global
3320             offset table.  */
3321	  BFD_ASSERT (sgot != NULL);
3322
3323	  if (h != NULL)
3324	    {
3325	      bfd_vma off;
3326
3327	      off = h->got.offset;
3328	      BFD_ASSERT (off != (bfd_vma) -1);
3329
3330	      if (! elf_hash_table (info)->dynamic_sections_created
3331		  || (info->shared
3332		      && SYMBOL_REFERENCES_LOCAL (info, h)))
3333		{
3334		  /* This is actually a static link, or it is a
3335                     -Bsymbolic link and the symbol is defined
3336                     locally.  We must initialize this entry in the
3337                     global offset table.  Since the offset must
3338                     always be a multiple of 4, we use the least
3339                     significant bit to record whether we have
3340                     initialized it already.
3341
3342		     When doing a dynamic link, we create a .rela.got
3343		     relocation entry to initialize the value.  This
3344		     is done in the finish_dynamic_symbol routine.  */
3345		  if ((off & 1) != 0)
3346		    off &= ~1;
3347		  else
3348		    {
3349		      bfd_put_32 (output_bfd, relocation,
3350				  sgot->contents + off);
3351		      h->got.offset |= 1;
3352		    }
3353		}
3354
3355	      relocation = sgot->output_offset + off - 4;
3356	    }
3357	  else
3358	    {
3359	      bfd_vma off;
3360
3361	      BFD_ASSERT (local_got_offsets != NULL
3362			  && local_got_offsets[r_symndx] != (bfd_vma) -1);
3363
3364	      off = local_got_offsets[r_symndx];
3365
3366	      /* The offset must always be a multiple of 4.  We use
3367		 the least significant bit to record whether we have
3368		 already processed this entry.  */
3369	      if ((off & 1) != 0)
3370		off &= ~1;
3371	      else
3372		{
3373		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3374
3375		  if (info->shared)
3376		    {
3377		      asection *srelgot;
3378		      Elf_Internal_Rela outrel;
3379
3380		      /* We need to generate a R_PPC_RELATIVE reloc
3381			 for the dynamic linker.  */
3382		      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3383		      BFD_ASSERT (srelgot != NULL);
3384
3385		      outrel.r_offset = (sgot->output_section->vma
3386					 + sgot->output_offset
3387					 + off);
3388		      outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3389		      outrel.r_addend = relocation;
3390		      bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3391						 (((Elf32_External_Rela *)
3392						   srelgot->contents)
3393						  + srelgot->reloc_count));
3394		      ++srelgot->reloc_count;
3395		    }
3396
3397		  local_got_offsets[r_symndx] |= 1;
3398		}
3399
3400	      relocation = sgot->output_offset + off - 4;
3401	    }
3402	  break;
3403
3404	/* Indirect .sdata relocation */
3405	case (int) R_PPC_EMB_SDAI16:
3406	  BFD_ASSERT (sdata != NULL);
3407	  relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3408								sdata, h, relocation, rel,
3409								R_PPC_RELATIVE);
3410	  break;
3411
3412	/* Indirect .sdata2 relocation */
3413	case (int) R_PPC_EMB_SDA2I16:
3414	  BFD_ASSERT (sdata2 != NULL);
3415	  relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3416								sdata2, h, relocation, rel,
3417								R_PPC_RELATIVE);
3418	  break;
3419
3420	/* Handle the TOC16 reloc.  We want to use the offset within the .got
3421	   section, not the actual VMA.  This is appropriate when generating
3422	   an embedded ELF object, for which the .got section acts like the
3423	   AIX .toc section.  */
3424	case (int) R_PPC_TOC16:			/* phony GOT16 relocations */
3425	  BFD_ASSERT (sec != (asection *) 0);
3426	  BFD_ASSERT (bfd_is_und_section (sec)
3427		      || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3428		      || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3429
3430	  addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3431	  break;
3432
3433	case (int) R_PPC_PLTREL24:
3434	  /* Relocation is to the entry for this symbol in the
3435             procedure linkage table.  */
3436	  BFD_ASSERT (h != NULL);
3437
3438	  if (h->plt.offset == (bfd_vma) -1
3439	      || splt == NULL)
3440	    {
3441	      /* We didn't make a PLT entry for this symbol.  This
3442                 happens when statically linking PIC code, or when
3443                 using -Bsymbolic.  */
3444	      break;
3445	    }
3446
3447	  relocation = (splt->output_section->vma
3448			+ splt->output_offset
3449			+ h->plt.offset);
3450	  break;
3451
3452	/* relocate against _SDA_BASE_ */
3453	case (int) R_PPC_SDAREL16:
3454	  {
3455	    const char *name;
3456
3457	    BFD_ASSERT (sec != (asection *) 0);
3458	    name = bfd_get_section_name (abfd, sec->output_section);
3459	    if (strcmp (name, ".sdata") != 0
3460		&& strcmp (name, ".sbss") != 0)
3461	      {
3462		(*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3463				       bfd_archive_filename (input_bfd),
3464				       sym_name,
3465				       ppc_elf_howto_table[(int) r_type]->name,
3466				       name);
3467	      }
3468	    addend -= (sdata->sym_hash->root.u.def.value
3469		       + sdata->sym_hash->root.u.def.section->output_section->vma
3470		       + sdata->sym_hash->root.u.def.section->output_offset);
3471	  }
3472	  break;
3473
3474	/* relocate against _SDA2_BASE_ */
3475	case (int) R_PPC_EMB_SDA2REL:
3476	  {
3477	    const char *name;
3478
3479	    BFD_ASSERT (sec != (asection *) 0);
3480	    name = bfd_get_section_name (abfd, sec->output_section);
3481	    if (strcmp (name, ".sdata2") != 0 && strcmp (name, ".sbss2") != 0)
3482	      {
3483		(*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3484				       bfd_archive_filename (input_bfd),
3485				       sym_name,
3486				       ppc_elf_howto_table[(int) r_type]->name,
3487				       name);
3488
3489		bfd_set_error (bfd_error_bad_value);
3490		ret = false;
3491		continue;
3492	      }
3493	    addend -= (sdata2->sym_hash->root.u.def.value
3494		       + sdata2->sym_hash->root.u.def.section->output_section->vma
3495		       + sdata2->sym_hash->root.u.def.section->output_offset);
3496	  }
3497	  break;
3498
3499	/* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
3500	case (int) R_PPC_EMB_SDA21:
3501	case (int) R_PPC_EMB_RELSDA:
3502	  {
3503	    const char *name;
3504	    int reg;
3505
3506	    BFD_ASSERT (sec != (asection *) 0);
3507	    name = bfd_get_section_name (abfd, sec->output_section);
3508	    if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
3509	      {
3510		reg = 13;
3511		addend -= (sdata->sym_hash->root.u.def.value
3512			   + sdata->sym_hash->root.u.def.section->output_section->vma
3513			   + sdata->sym_hash->root.u.def.section->output_offset);
3514	      }
3515
3516	    else if (strcmp (name, ".sdata2") == 0
3517		     || strcmp (name, ".sbss2") == 0)
3518	      {
3519		reg = 2;
3520		addend -= (sdata2->sym_hash->root.u.def.value
3521			   + sdata2->sym_hash->root.u.def.section->output_section->vma
3522			   + sdata2->sym_hash->root.u.def.section->output_offset);
3523	      }
3524
3525	    else if (strcmp (name, ".PPC.EMB.sdata0") == 0
3526		     || strcmp (name, ".PPC.EMB.sbss0") == 0)
3527	      {
3528		reg = 0;
3529	      }
3530
3531	    else
3532	      {
3533		(*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3534				       bfd_archive_filename (input_bfd),
3535				       sym_name,
3536				       ppc_elf_howto_table[(int) r_type]->name,
3537				       name);
3538
3539		bfd_set_error (bfd_error_bad_value);
3540		ret = false;
3541		continue;
3542	      }
3543
3544	    if (r_type == R_PPC_EMB_SDA21)
3545	      {			/* fill in register field */
3546		insn = bfd_get_32 (output_bfd, contents + offset);
3547		insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3548		bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3549	      }
3550	  }
3551	  break;
3552
3553	/* Relocate against the beginning of the section */
3554	case (int) R_PPC_SECTOFF:
3555	case (int) R_PPC_SECTOFF_LO:
3556	case (int) R_PPC_SECTOFF_HI:
3557	  BFD_ASSERT (sec != (asection *) 0);
3558	  addend -= sec->output_section->vma;
3559	  break;
3560
3561	case (int) R_PPC_SECTOFF_HA:
3562	  BFD_ASSERT (sec != (asection *) 0);
3563	  addend -= sec->output_section->vma;
3564	  addend += ((relocation + addend) & 0x8000) << 1;
3565	  break;
3566
3567	/* Negative relocations */
3568	case (int) R_PPC_EMB_NADDR32:
3569	case (int) R_PPC_EMB_NADDR16:
3570	case (int) R_PPC_EMB_NADDR16_LO:
3571	case (int) R_PPC_EMB_NADDR16_HI:
3572	  addend -= 2 * relocation;
3573	  break;
3574
3575	case (int) R_PPC_EMB_NADDR16_HA:
3576	  addend -= 2 * relocation;
3577	  addend += ((relocation + addend) & 0x8000) << 1;
3578	  break;
3579
3580	/* NOP relocation that prevents garbage collecting linkers from omitting a
3581	   reference.  */
3582	case (int) R_PPC_EMB_MRKREF:
3583	  continue;
3584
3585	case (int) R_PPC_COPY:
3586	case (int) R_PPC_GLOB_DAT:
3587	case (int) R_PPC_JMP_SLOT:
3588	case (int) R_PPC_RELATIVE:
3589	case (int) R_PPC_PLT32:
3590	case (int) R_PPC_PLTREL32:
3591	case (int) R_PPC_PLT16_LO:
3592	case (int) R_PPC_PLT16_HI:
3593	case (int) R_PPC_PLT16_HA:
3594	case (int) R_PPC_EMB_RELSEC16:
3595	case (int) R_PPC_EMB_RELST_LO:
3596	case (int) R_PPC_EMB_RELST_HI:
3597	case (int) R_PPC_EMB_RELST_HA:
3598	case (int) R_PPC_EMB_BIT_FLD:
3599	  (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3600				 bfd_archive_filename (input_bfd),
3601				 ppc_elf_howto_table[(int) r_type]->name,
3602				 sym_name);
3603
3604	  bfd_set_error (bfd_error_invalid_operation);
3605	  ret = false;
3606	  continue;
3607
3608	case (int) R_PPC_GNU_VTINHERIT:
3609	case (int) R_PPC_GNU_VTENTRY:
3610	  /* These are no-ops in the end.  */
3611	  continue;
3612	}
3613
3614#ifdef DEBUG
3615      fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3616	       howto->name,
3617	       (int) r_type,
3618	       sym_name,
3619	       r_symndx,
3620	       (long) offset,
3621	       (long) addend);
3622#endif
3623
3624      r = _bfd_final_link_relocate (howto,
3625				    input_bfd,
3626				    input_section,
3627				    contents,
3628				    offset,
3629				    relocation,
3630				    addend);
3631
3632      if (r == bfd_reloc_ok)
3633	;
3634      else if (r == bfd_reloc_overflow)
3635	{
3636	  const char *name;
3637
3638	  if (h != NULL)
3639	    {
3640	      if (h->root.type == bfd_link_hash_undefweak
3641		  && howto->pc_relative)
3642		{
3643		  /* Assume this is a call protected by other code that
3644		     detect the symbol is undefined.  If this is the case,
3645		     we can safely ignore the overflow.  If not, the
3646		     program is hosed anyway, and a little warning isn't
3647		     going to help.  */
3648
3649		  continue;
3650		}
3651
3652	      name = h->root.root.string;
3653	    }
3654	  else
3655	    {
3656	      name = bfd_elf_string_from_elf_section (input_bfd,
3657						      symtab_hdr->sh_link,
3658						      sym->st_name);
3659	      if (name == NULL)
3660		continue;
3661	      if (*name == '\0')
3662		name = bfd_section_name (input_bfd, sec);
3663	    }
3664
3665	  if (! (*info->callbacks->reloc_overflow) (info,
3666						   name,
3667						   howto->name,
3668						   (bfd_vma) 0,
3669						   input_bfd,
3670						   input_section,
3671						   offset))
3672	    return false;
3673	}
3674      else
3675	ret = false;
3676    }
3677
3678#ifdef DEBUG
3679  fprintf (stderr, "\n");
3680#endif
3681
3682  return ret;
3683}
3684
3685static enum elf_reloc_type_class
3686ppc_elf_reloc_type_class (rela)
3687     const Elf_Internal_Rela *rela;
3688{
3689  switch ((int) ELF32_R_TYPE (rela->r_info))
3690    {
3691    case R_PPC_RELATIVE:
3692      return reloc_class_relative;
3693    case R_PPC_REL24:
3694    case R_PPC_ADDR24:
3695    case R_PPC_JMP_SLOT:
3696      return reloc_class_plt;
3697    case R_PPC_COPY:
3698      return reloc_class_copy;
3699    default:
3700      return reloc_class_normal;
3701    }
3702}
3703
3704/* Support for core dump NOTE sections */
3705static boolean
3706ppc_elf_grok_prstatus (abfd, note)
3707     bfd *abfd;
3708     Elf_Internal_Note *note;
3709{
3710  int offset;
3711  unsigned int raw_size;
3712
3713  switch (note->descsz)
3714    {
3715      default:
3716	return false;
3717
3718      case 268:		/* Linux/PPC */
3719	/* pr_cursig */
3720	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3721
3722	/* pr_pid */
3723	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3724
3725	/* pr_reg */
3726	offset = 72;
3727	raw_size = 192;
3728
3729	break;
3730    }
3731
3732  /* Make a ".reg/999" section.  */
3733  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3734					  raw_size, note->descpos + offset);
3735}
3736
3737static boolean
3738ppc_elf_grok_psinfo (abfd, note)
3739     bfd *abfd;
3740     Elf_Internal_Note *note;
3741{
3742  switch (note->descsz)
3743    {
3744      default:
3745	return false;
3746
3747      case 128:		/* Linux/PPC elf_prpsinfo */
3748	elf_tdata (abfd)->core_program
3749	 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3750	elf_tdata (abfd)->core_command
3751	 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3752    }
3753
3754  /* Note that for some reason, a spurious space is tacked
3755     onto the end of the args in some (at least one anyway)
3756     implementations, so strip it off if it exists.  */
3757
3758  {
3759    char *command = elf_tdata (abfd)->core_command;
3760    int n = strlen (command);
3761
3762    if (0 < n && command[n - 1] == ' ')
3763      command[n - 1] = '\0';
3764  }
3765
3766  return true;
3767}
3768
3769#define TARGET_LITTLE_SYM	bfd_elf32_powerpcle_vec
3770#define TARGET_LITTLE_NAME	"elf32-powerpcle"
3771#define TARGET_BIG_SYM		bfd_elf32_powerpc_vec
3772#define TARGET_BIG_NAME		"elf32-powerpc"
3773#define ELF_ARCH		bfd_arch_powerpc
3774#define ELF_MACHINE_CODE	EM_PPC
3775#define ELF_MAXPAGESIZE		0x10000
3776#define elf_info_to_howto	ppc_elf_info_to_howto
3777
3778#ifdef  EM_CYGNUS_POWERPC
3779#define ELF_MACHINE_ALT1	EM_CYGNUS_POWERPC
3780#endif
3781
3782#ifdef EM_PPC_OLD
3783#define ELF_MACHINE_ALT2	EM_PPC_OLD
3784#endif
3785
3786#define elf_backend_plt_not_loaded	1
3787#define elf_backend_got_symbol_offset	4
3788#define elf_backend_can_gc_sections	1
3789#define elf_backend_can_refcount	1
3790#define elf_backend_got_header_size	12
3791#define elf_backend_plt_header_size	PLT_INITIAL_ENTRY_SIZE
3792#define elf_backend_rela_normal		1
3793
3794#define bfd_elf32_bfd_merge_private_bfd_data	ppc_elf_merge_private_bfd_data
3795#define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
3796#define bfd_elf32_bfd_reloc_type_lookup		ppc_elf_reloc_type_lookup
3797#define bfd_elf32_bfd_set_private_flags		ppc_elf_set_private_flags
3798#define bfd_elf32_bfd_final_link		_bfd_elf32_gc_common_final_link
3799
3800#define elf_backend_gc_mark_hook		ppc_elf_gc_mark_hook
3801#define elf_backend_gc_sweep_hook		ppc_elf_gc_sweep_hook
3802#define elf_backend_section_from_shdr		ppc_elf_section_from_shdr
3803#define elf_backend_relocate_section		ppc_elf_relocate_section
3804#define elf_backend_create_dynamic_sections	ppc_elf_create_dynamic_sections
3805#define elf_backend_check_relocs		ppc_elf_check_relocs
3806#define elf_backend_adjust_dynamic_symbol	ppc_elf_adjust_dynamic_symbol
3807#define elf_backend_add_symbol_hook		ppc_elf_add_symbol_hook
3808#define elf_backend_size_dynamic_sections	ppc_elf_size_dynamic_sections
3809#define elf_backend_finish_dynamic_symbol	ppc_elf_finish_dynamic_symbol
3810#define elf_backend_finish_dynamic_sections	ppc_elf_finish_dynamic_sections
3811#define elf_backend_fake_sections		ppc_elf_fake_sections
3812#define elf_backend_additional_program_headers	ppc_elf_additional_program_headers
3813#define elf_backend_modify_segment_map		ppc_elf_modify_segment_map
3814#define elf_backend_grok_prstatus		ppc_elf_grok_prstatus
3815#define elf_backend_grok_psinfo			ppc_elf_grok_psinfo
3816#define elf_backend_reloc_type_class		ppc_elf_reloc_type_class
3817
3818#include "elf32-target.h"
3819