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