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