elf64-ppc.c revision 218822
1/* PowerPC64-specific support for 64-bit ELF.
2   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3   Free Software Foundation, Inc.
4   Written by Linus Nordberg, Swox AB <info@swox.com>,
5   based on elf32-ppc.c by Ian Lance Taylor.
6   Largely rewritten by Alan Modra <amodra@bigpond.net.au>
7
8   This file is part of BFD, the Binary File Descriptor library.
9
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; either version 2 of the License, or
13   (at your option) any later version.
14
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19
20   You should have received a copy of the GNU General Public License along
21   with this program; if not, write to the Free Software Foundation, Inc.,
22   51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23
24/* The 64-bit PowerPC ELF ABI may be found at
25   http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26   http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28#include "sysdep.h"
29#include <stdarg.h>
30#include "bfd.h"
31#include "bfdlink.h"
32#include "libbfd.h"
33#include "elf-bfd.h"
34#include "elf/ppc64.h"
35#include "elf64-ppc.h"
36
37static bfd_reloc_status_type ppc64_elf_ha_reloc
38  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
39static bfd_reloc_status_type ppc64_elf_branch_reloc
40  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41static bfd_reloc_status_type ppc64_elf_brtaken_reloc
42  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43static bfd_reloc_status_type ppc64_elf_sectoff_reloc
44  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
46  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47static bfd_reloc_status_type ppc64_elf_toc_reloc
48  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
50  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51static bfd_reloc_status_type ppc64_elf_toc64_reloc
52  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53static bfd_reloc_status_type ppc64_elf_unhandled_reloc
54  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55static bfd_vma opd_entry_value
56  (asection *, bfd_vma, asection **, bfd_vma *);
57
58#define TARGET_LITTLE_SYM	bfd_elf64_powerpcle_vec
59#define TARGET_LITTLE_NAME	"elf64-powerpcle"
60#define TARGET_BIG_SYM		bfd_elf64_powerpc_vec
61#define TARGET_BIG_NAME		"elf64-powerpc"
62#define ELF_ARCH		bfd_arch_powerpc
63#define ELF_MACHINE_CODE	EM_PPC64
64#define ELF_MAXPAGESIZE		0x10000
65#define ELF_COMMONPAGESIZE	0x1000
66#define elf_info_to_howto	ppc64_elf_info_to_howto
67
68#define elf_backend_want_got_sym 0
69#define elf_backend_want_plt_sym 0
70#define elf_backend_plt_alignment 3
71#define elf_backend_plt_not_loaded 1
72#define elf_backend_got_header_size 8
73#define elf_backend_can_gc_sections 1
74#define elf_backend_can_refcount 1
75#define elf_backend_rela_normal 1
76#define elf_backend_default_execstack 0
77
78#define bfd_elf64_mkobject		      ppc64_elf_mkobject
79#define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
80#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
81#define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
82#define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
83#define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
84#define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
85#define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
86
87#define elf_backend_object_p		      ppc64_elf_object_p
88#define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
89#define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
90#define elf_backend_write_core_note	      ppc64_elf_write_core_note
91#define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
92#define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
93#define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
94#define elf_backend_check_directives	      ppc64_elf_check_directives
95#define elf_backend_as_needed_cleanup	      ppc64_elf_as_needed_cleanup
96#define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
97#define elf_backend_check_relocs	      ppc64_elf_check_relocs
98#define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
99#define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
100#define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
101#define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
102#define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
103#define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
104#define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
105#define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
106#define elf_backend_action_discarded	      ppc64_elf_action_discarded
107#define elf_backend_relocate_section	      ppc64_elf_relocate_section
108#define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
109#define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
110#define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
111#define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
112#define elf_backend_special_sections	      ppc64_elf_special_sections
113
114/* The name of the dynamic interpreter.  This is put in the .interp
115   section.  */
116#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
117
118/* The size in bytes of an entry in the procedure linkage table.  */
119#define PLT_ENTRY_SIZE 24
120
121/* The initial size of the plt reserved for the dynamic linker.  */
122#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
123
124/* TOC base pointers offset from start of TOC.  */
125#define TOC_BASE_OFF	0x8000
126
127/* Offset of tp and dtp pointers from start of TLS block.  */
128#define TP_OFFSET	0x7000
129#define DTP_OFFSET	0x8000
130
131/* .plt call stub instructions.  The normal stub is like this, but
132   sometimes the .plt entry crosses a 64k boundary and we need to
133   insert an addis to adjust r12.  */
134#define PLT_CALL_STUB_SIZE (7*4)
135#define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
136#define STD_R2_40R1	0xf8410028	/* std	 %r2,40(%r1)	     */
137#define LD_R11_0R12	0xe96c0000	/* ld	 %r11,xxx+0@l(%r12)  */
138#define LD_R2_0R12	0xe84c0000	/* ld	 %r2,xxx+8@l(%r12)   */
139#define MTCTR_R11	0x7d6903a6	/* mtctr %r11		     */
140					/* ld	 %r11,xxx+16@l(%r12) */
141#define BCTR		0x4e800420	/* bctr			     */
142
143
144#define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,off@ha  */
145#define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
146#define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
147
148#define LD_R2_40R1	0xe8410028	/* ld    %r2,40(%r1)     */
149
150/* glink call stub instructions.  We enter with the index in R0.  */
151#define GLINK_CALL_STUB_SIZE (16*4)
152					/* 0:				*/
153					/*  .quad plt0-1f		*/
154					/* __glink:			*/
155#define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
156#define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
157					/* 1:				*/
158#define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
159#define LD_R2_M16R11	0xe84bfff0	/*  ld %2,(0b-1b)(%11)		*/
160#define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
161#define ADD_R12_R2_R11	0x7d825a14	/*  add %12,%2,%11		*/
162					/*  ld %11,0(%12)		*/
163					/*  ld %2,8(%12)		*/
164					/*  mtctr %11			*/
165					/*  ld %11,16(%12)		*/
166					/*  bctr			*/
167
168/* Pad with this.  */
169#define NOP		0x60000000
170
171/* Some other nops.  */
172#define CROR_151515	0x4def7b82
173#define CROR_313131	0x4ffffb82
174
175/* .glink entries for the first 32k functions are two instructions.  */
176#define LI_R0_0		0x38000000	/* li    %r0,0		*/
177#define B_DOT		0x48000000	/* b     .		*/
178
179/* After that, we need two instructions to load the index, followed by
180   a branch.  */
181#define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
182#define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
183
184/* Instructions used by the save and restore reg functions.  */
185#define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
186#define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
187#define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
188#define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
189#define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
190#define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
191#define LI_R12_0	0x39800000	/* li    %r12,0		*/
192#define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
193#define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
194#define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
195#define BLR		0x4e800020	/* blr			*/
196
197/* Since .opd is an array of descriptors and each entry will end up
198   with identical R_PPC64_RELATIVE relocs, there is really no need to
199   propagate .opd relocs;  The dynamic linker should be taught to
200   relocate .opd without reloc entries.  */
201#ifndef NO_OPD_RELOCS
202#define NO_OPD_RELOCS 0
203#endif
204
205#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
206
207/* Relocation HOWTO's.  */
208static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
209
210static reloc_howto_type ppc64_elf_howto_raw[] = {
211  /* This reloc does nothing.  */
212  HOWTO (R_PPC64_NONE,		/* type */
213	 0,			/* rightshift */
214	 2,			/* size (0 = byte, 1 = short, 2 = long) */
215	 32,			/* bitsize */
216	 FALSE,			/* pc_relative */
217	 0,			/* bitpos */
218	 complain_overflow_dont, /* complain_on_overflow */
219	 bfd_elf_generic_reloc,	/* special_function */
220	 "R_PPC64_NONE",	/* name */
221	 FALSE,			/* partial_inplace */
222	 0,			/* src_mask */
223	 0,			/* dst_mask */
224	 FALSE),		/* pcrel_offset */
225
226  /* A standard 32 bit relocation.  */
227  HOWTO (R_PPC64_ADDR32,	/* type */
228	 0,			/* rightshift */
229	 2,			/* size (0 = byte, 1 = short, 2 = long) */
230	 32,			/* bitsize */
231	 FALSE,			/* pc_relative */
232	 0,			/* bitpos */
233	 complain_overflow_bitfield, /* complain_on_overflow */
234	 bfd_elf_generic_reloc,	/* special_function */
235	 "R_PPC64_ADDR32",	/* name */
236	 FALSE,			/* partial_inplace */
237	 0,			/* src_mask */
238	 0xffffffff,		/* dst_mask */
239	 FALSE),		/* pcrel_offset */
240
241  /* An absolute 26 bit branch; the lower two bits must be zero.
242     FIXME: we don't check that, we just clear them.  */
243  HOWTO (R_PPC64_ADDR24,	/* type */
244	 0,			/* rightshift */
245	 2,			/* size (0 = byte, 1 = short, 2 = long) */
246	 26,			/* bitsize */
247	 FALSE,			/* pc_relative */
248	 0,			/* bitpos */
249	 complain_overflow_bitfield, /* complain_on_overflow */
250	 bfd_elf_generic_reloc,	/* special_function */
251	 "R_PPC64_ADDR24",	/* name */
252	 FALSE,			/* partial_inplace */
253	 0,			/* src_mask */
254	 0x03fffffc,		/* dst_mask */
255	 FALSE),		/* pcrel_offset */
256
257  /* A standard 16 bit relocation.  */
258  HOWTO (R_PPC64_ADDR16,	/* type */
259	 0,			/* rightshift */
260	 1,			/* size (0 = byte, 1 = short, 2 = long) */
261	 16,			/* bitsize */
262	 FALSE,			/* pc_relative */
263	 0,			/* bitpos */
264	 complain_overflow_bitfield, /* complain_on_overflow */
265	 bfd_elf_generic_reloc,	/* special_function */
266	 "R_PPC64_ADDR16",	/* name */
267	 FALSE,			/* partial_inplace */
268	 0,			/* src_mask */
269	 0xffff,		/* dst_mask */
270	 FALSE),		/* pcrel_offset */
271
272  /* A 16 bit relocation without overflow.  */
273  HOWTO (R_PPC64_ADDR16_LO,	/* type */
274	 0,			/* rightshift */
275	 1,			/* size (0 = byte, 1 = short, 2 = long) */
276	 16,			/* bitsize */
277	 FALSE,			/* pc_relative */
278	 0,			/* bitpos */
279	 complain_overflow_dont,/* complain_on_overflow */
280	 bfd_elf_generic_reloc,	/* special_function */
281	 "R_PPC64_ADDR16_LO",	/* name */
282	 FALSE,			/* partial_inplace */
283	 0,			/* src_mask */
284	 0xffff,		/* dst_mask */
285	 FALSE),		/* pcrel_offset */
286
287  /* Bits 16-31 of an address.  */
288  HOWTO (R_PPC64_ADDR16_HI,	/* type */
289	 16,			/* rightshift */
290	 1,			/* size (0 = byte, 1 = short, 2 = long) */
291	 16,			/* bitsize */
292	 FALSE,			/* pc_relative */
293	 0,			/* bitpos */
294	 complain_overflow_dont, /* complain_on_overflow */
295	 bfd_elf_generic_reloc,	/* special_function */
296	 "R_PPC64_ADDR16_HI",	/* name */
297	 FALSE,			/* partial_inplace */
298	 0,			/* src_mask */
299	 0xffff,		/* dst_mask */
300	 FALSE),		/* pcrel_offset */
301
302  /* Bits 16-31 of an address, plus 1 if the contents of the low 16
303     bits, treated as a signed number, is negative.  */
304  HOWTO (R_PPC64_ADDR16_HA,	/* type */
305	 16,			/* rightshift */
306	 1,			/* size (0 = byte, 1 = short, 2 = long) */
307	 16,			/* bitsize */
308	 FALSE,			/* pc_relative */
309	 0,			/* bitpos */
310	 complain_overflow_dont, /* complain_on_overflow */
311	 ppc64_elf_ha_reloc,	/* special_function */
312	 "R_PPC64_ADDR16_HA",	/* name */
313	 FALSE,			/* partial_inplace */
314	 0,			/* src_mask */
315	 0xffff,		/* dst_mask */
316	 FALSE),		/* pcrel_offset */
317
318  /* An absolute 16 bit branch; the lower two bits must be zero.
319     FIXME: we don't check that, we just clear them.  */
320  HOWTO (R_PPC64_ADDR14,	/* type */
321	 0,			/* rightshift */
322	 2,			/* size (0 = byte, 1 = short, 2 = long) */
323	 16,			/* bitsize */
324	 FALSE,			/* pc_relative */
325	 0,			/* bitpos */
326	 complain_overflow_bitfield, /* complain_on_overflow */
327	 ppc64_elf_branch_reloc, /* special_function */
328	 "R_PPC64_ADDR14",	/* name */
329	 FALSE,			/* partial_inplace */
330	 0,			/* src_mask */
331	 0x0000fffc,		/* dst_mask */
332	 FALSE),		/* pcrel_offset */
333
334  /* An absolute 16 bit branch, for which bit 10 should be set to
335     indicate that the branch is expected to be taken.  The lower two
336     bits must be zero.  */
337  HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
338	 0,			/* rightshift */
339	 2,			/* size (0 = byte, 1 = short, 2 = long) */
340	 16,			/* bitsize */
341	 FALSE,			/* pc_relative */
342	 0,			/* bitpos */
343	 complain_overflow_bitfield, /* complain_on_overflow */
344	 ppc64_elf_brtaken_reloc, /* special_function */
345	 "R_PPC64_ADDR14_BRTAKEN",/* name */
346	 FALSE,			/* partial_inplace */
347	 0,			/* src_mask */
348	 0x0000fffc,		/* dst_mask */
349	 FALSE),		/* pcrel_offset */
350
351  /* An absolute 16 bit branch, for which bit 10 should be set to
352     indicate that the branch is not expected to be taken.  The lower
353     two bits must be zero.  */
354  HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
355	 0,			/* rightshift */
356	 2,			/* size (0 = byte, 1 = short, 2 = long) */
357	 16,			/* bitsize */
358	 FALSE,			/* pc_relative */
359	 0,			/* bitpos */
360	 complain_overflow_bitfield, /* complain_on_overflow */
361	 ppc64_elf_brtaken_reloc, /* special_function */
362	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
363	 FALSE,			/* partial_inplace */
364	 0,			/* src_mask */
365	 0x0000fffc,		/* dst_mask */
366	 FALSE),		/* pcrel_offset */
367
368  /* A relative 26 bit branch; the lower two bits must be zero.  */
369  HOWTO (R_PPC64_REL24,		/* type */
370	 0,			/* rightshift */
371	 2,			/* size (0 = byte, 1 = short, 2 = long) */
372	 26,			/* bitsize */
373	 TRUE,			/* pc_relative */
374	 0,			/* bitpos */
375	 complain_overflow_signed, /* complain_on_overflow */
376	 ppc64_elf_branch_reloc, /* special_function */
377	 "R_PPC64_REL24",	/* name */
378	 FALSE,			/* partial_inplace */
379	 0,			/* src_mask */
380	 0x03fffffc,		/* dst_mask */
381	 TRUE),			/* pcrel_offset */
382
383  /* A relative 16 bit branch; the lower two bits must be zero.  */
384  HOWTO (R_PPC64_REL14,		/* type */
385	 0,			/* rightshift */
386	 2,			/* size (0 = byte, 1 = short, 2 = long) */
387	 16,			/* bitsize */
388	 TRUE,			/* pc_relative */
389	 0,			/* bitpos */
390	 complain_overflow_signed, /* complain_on_overflow */
391	 ppc64_elf_branch_reloc, /* special_function */
392	 "R_PPC64_REL14",	/* name */
393	 FALSE,			/* partial_inplace */
394	 0,			/* src_mask */
395	 0x0000fffc,		/* dst_mask */
396	 TRUE),			/* pcrel_offset */
397
398  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
399     the branch is expected to be taken.  The lower two bits must be
400     zero.  */
401  HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
402	 0,			/* rightshift */
403	 2,			/* size (0 = byte, 1 = short, 2 = long) */
404	 16,			/* bitsize */
405	 TRUE,			/* pc_relative */
406	 0,			/* bitpos */
407	 complain_overflow_signed, /* complain_on_overflow */
408	 ppc64_elf_brtaken_reloc, /* special_function */
409	 "R_PPC64_REL14_BRTAKEN", /* name */
410	 FALSE,			/* partial_inplace */
411	 0,			/* src_mask */
412	 0x0000fffc,		/* dst_mask */
413	 TRUE),			/* pcrel_offset */
414
415  /* A relative 16 bit branch.  Bit 10 should be set to indicate that
416     the branch is not expected to be taken.  The lower two bits must
417     be zero.  */
418  HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
419	 0,			/* rightshift */
420	 2,			/* size (0 = byte, 1 = short, 2 = long) */
421	 16,			/* bitsize */
422	 TRUE,			/* pc_relative */
423	 0,			/* bitpos */
424	 complain_overflow_signed, /* complain_on_overflow */
425	 ppc64_elf_brtaken_reloc, /* special_function */
426	 "R_PPC64_REL14_BRNTAKEN",/* name */
427	 FALSE,			/* partial_inplace */
428	 0,			/* src_mask */
429	 0x0000fffc,		/* dst_mask */
430	 TRUE),			/* pcrel_offset */
431
432  /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
433     symbol.  */
434  HOWTO (R_PPC64_GOT16,		/* type */
435	 0,			/* rightshift */
436	 1,			/* size (0 = byte, 1 = short, 2 = long) */
437	 16,			/* bitsize */
438	 FALSE,			/* pc_relative */
439	 0,			/* bitpos */
440	 complain_overflow_signed, /* complain_on_overflow */
441	 ppc64_elf_unhandled_reloc, /* special_function */
442	 "R_PPC64_GOT16",	/* name */
443	 FALSE,			/* partial_inplace */
444	 0,			/* src_mask */
445	 0xffff,		/* dst_mask */
446	 FALSE),		/* pcrel_offset */
447
448  /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
449     the symbol.  */
450  HOWTO (R_PPC64_GOT16_LO,	/* type */
451	 0,			/* rightshift */
452	 1,			/* size (0 = byte, 1 = short, 2 = long) */
453	 16,			/* bitsize */
454	 FALSE,			/* pc_relative */
455	 0,			/* bitpos */
456	 complain_overflow_dont, /* complain_on_overflow */
457	 ppc64_elf_unhandled_reloc, /* special_function */
458	 "R_PPC64_GOT16_LO",	/* name */
459	 FALSE,			/* partial_inplace */
460	 0,			/* src_mask */
461	 0xffff,		/* dst_mask */
462	 FALSE),		/* pcrel_offset */
463
464  /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
465     the symbol.  */
466  HOWTO (R_PPC64_GOT16_HI,	/* type */
467	 16,			/* rightshift */
468	 1,			/* size (0 = byte, 1 = short, 2 = long) */
469	 16,			/* bitsize */
470	 FALSE,			/* pc_relative */
471	 0,			/* bitpos */
472	 complain_overflow_dont,/* complain_on_overflow */
473	 ppc64_elf_unhandled_reloc, /* special_function */
474	 "R_PPC64_GOT16_HI",	/* name */
475	 FALSE,			/* partial_inplace */
476	 0,			/* src_mask */
477	 0xffff,		/* dst_mask */
478	 FALSE),		/* pcrel_offset */
479
480  /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
481     the symbol.  */
482  HOWTO (R_PPC64_GOT16_HA,	/* type */
483	 16,			/* rightshift */
484	 1,			/* size (0 = byte, 1 = short, 2 = long) */
485	 16,			/* bitsize */
486	 FALSE,			/* pc_relative */
487	 0,			/* bitpos */
488	 complain_overflow_dont,/* complain_on_overflow */
489	 ppc64_elf_unhandled_reloc, /* special_function */
490	 "R_PPC64_GOT16_HA",	/* name */
491	 FALSE,			/* partial_inplace */
492	 0,			/* src_mask */
493	 0xffff,		/* dst_mask */
494	 FALSE),		/* pcrel_offset */
495
496  /* This is used only by the dynamic linker.  The symbol should exist
497     both in the object being run and in some shared library.  The
498     dynamic linker copies the data addressed by the symbol from the
499     shared library into the object, because the object being
500     run has to have the data at some particular address.  */
501  HOWTO (R_PPC64_COPY,		/* type */
502	 0,			/* rightshift */
503	 0,			/* this one is variable size */
504	 0,			/* bitsize */
505	 FALSE,			/* pc_relative */
506	 0,			/* bitpos */
507	 complain_overflow_dont, /* complain_on_overflow */
508	 ppc64_elf_unhandled_reloc, /* special_function */
509	 "R_PPC64_COPY",	/* name */
510	 FALSE,			/* partial_inplace */
511	 0,			/* src_mask */
512	 0,			/* dst_mask */
513	 FALSE),		/* pcrel_offset */
514
515  /* Like R_PPC64_ADDR64, but used when setting global offset table
516     entries.  */
517  HOWTO (R_PPC64_GLOB_DAT,	/* type */
518	 0,			/* rightshift */
519	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
520	 64,			/* bitsize */
521	 FALSE,			/* pc_relative */
522	 0,			/* bitpos */
523	 complain_overflow_dont, /* complain_on_overflow */
524	 ppc64_elf_unhandled_reloc,  /* special_function */
525	 "R_PPC64_GLOB_DAT",	/* name */
526	 FALSE,			/* partial_inplace */
527	 0,			/* src_mask */
528	 ONES (64),		/* dst_mask */
529	 FALSE),		/* pcrel_offset */
530
531  /* Created by the link editor.  Marks a procedure linkage table
532     entry for a symbol.  */
533  HOWTO (R_PPC64_JMP_SLOT,	/* type */
534	 0,			/* rightshift */
535	 0,			/* size (0 = byte, 1 = short, 2 = long) */
536	 0,			/* bitsize */
537	 FALSE,			/* pc_relative */
538	 0,			/* bitpos */
539	 complain_overflow_dont, /* complain_on_overflow */
540	 ppc64_elf_unhandled_reloc, /* special_function */
541	 "R_PPC64_JMP_SLOT",	/* name */
542	 FALSE,			/* partial_inplace */
543	 0,			/* src_mask */
544	 0,			/* dst_mask */
545	 FALSE),		/* pcrel_offset */
546
547  /* Used only by the dynamic linker.  When the object is run, this
548     doubleword64 is set to the load address of the object, plus the
549     addend.  */
550  HOWTO (R_PPC64_RELATIVE,	/* type */
551	 0,			/* rightshift */
552	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
553	 64,			/* bitsize */
554	 FALSE,			/* pc_relative */
555	 0,			/* bitpos */
556	 complain_overflow_dont, /* complain_on_overflow */
557	 bfd_elf_generic_reloc,	/* special_function */
558	 "R_PPC64_RELATIVE",	/* name */
559	 FALSE,			/* partial_inplace */
560	 0,			/* src_mask */
561	 ONES (64),		/* dst_mask */
562	 FALSE),		/* pcrel_offset */
563
564  /* Like R_PPC64_ADDR32, but may be unaligned.  */
565  HOWTO (R_PPC64_UADDR32,	/* type */
566	 0,			/* rightshift */
567	 2,			/* size (0 = byte, 1 = short, 2 = long) */
568	 32,			/* bitsize */
569	 FALSE,			/* pc_relative */
570	 0,			/* bitpos */
571	 complain_overflow_bitfield, /* complain_on_overflow */
572	 bfd_elf_generic_reloc,	/* special_function */
573	 "R_PPC64_UADDR32",	/* name */
574	 FALSE,			/* partial_inplace */
575	 0,			/* src_mask */
576	 0xffffffff,		/* dst_mask */
577	 FALSE),		/* pcrel_offset */
578
579  /* Like R_PPC64_ADDR16, but may be unaligned.  */
580  HOWTO (R_PPC64_UADDR16,	/* type */
581	 0,			/* rightshift */
582	 1,			/* size (0 = byte, 1 = short, 2 = long) */
583	 16,			/* bitsize */
584	 FALSE,			/* pc_relative */
585	 0,			/* bitpos */
586	 complain_overflow_bitfield, /* complain_on_overflow */
587	 bfd_elf_generic_reloc,	/* special_function */
588	 "R_PPC64_UADDR16",	/* name */
589	 FALSE,			/* partial_inplace */
590	 0,			/* src_mask */
591	 0xffff,		/* dst_mask */
592	 FALSE),		/* pcrel_offset */
593
594  /* 32-bit PC relative.  */
595  HOWTO (R_PPC64_REL32,		/* type */
596	 0,			/* rightshift */
597	 2,			/* size (0 = byte, 1 = short, 2 = long) */
598	 32,			/* bitsize */
599	 TRUE,			/* pc_relative */
600	 0,			/* bitpos */
601	 /* FIXME: Verify.  Was complain_overflow_bitfield.  */
602	 complain_overflow_signed, /* complain_on_overflow */
603	 bfd_elf_generic_reloc,	/* special_function */
604	 "R_PPC64_REL32",	/* name */
605	 FALSE,			/* partial_inplace */
606	 0,			/* src_mask */
607	 0xffffffff,		/* dst_mask */
608	 TRUE),			/* pcrel_offset */
609
610  /* 32-bit relocation to the symbol's procedure linkage table.  */
611  HOWTO (R_PPC64_PLT32,		/* type */
612	 0,			/* rightshift */
613	 2,			/* size (0 = byte, 1 = short, 2 = long) */
614	 32,			/* bitsize */
615	 FALSE,			/* pc_relative */
616	 0,			/* bitpos */
617	 complain_overflow_bitfield, /* complain_on_overflow */
618	 ppc64_elf_unhandled_reloc, /* special_function */
619	 "R_PPC64_PLT32",	/* name */
620	 FALSE,			/* partial_inplace */
621	 0,			/* src_mask */
622	 0xffffffff,		/* dst_mask */
623	 FALSE),		/* pcrel_offset */
624
625  /* 32-bit PC relative relocation to the symbol's procedure linkage table.
626     FIXME: R_PPC64_PLTREL32 not supported.  */
627  HOWTO (R_PPC64_PLTREL32,	/* type */
628	 0,			/* rightshift */
629	 2,			/* size (0 = byte, 1 = short, 2 = long) */
630	 32,			/* bitsize */
631	 TRUE,			/* pc_relative */
632	 0,			/* bitpos */
633	 complain_overflow_signed, /* complain_on_overflow */
634	 bfd_elf_generic_reloc,	/* special_function */
635	 "R_PPC64_PLTREL32",	/* name */
636	 FALSE,			/* partial_inplace */
637	 0,			/* src_mask */
638	 0xffffffff,		/* dst_mask */
639	 TRUE),			/* pcrel_offset */
640
641  /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
642     the symbol.  */
643  HOWTO (R_PPC64_PLT16_LO,	/* type */
644	 0,			/* rightshift */
645	 1,			/* size (0 = byte, 1 = short, 2 = long) */
646	 16,			/* bitsize */
647	 FALSE,			/* pc_relative */
648	 0,			/* bitpos */
649	 complain_overflow_dont, /* complain_on_overflow */
650	 ppc64_elf_unhandled_reloc, /* special_function */
651	 "R_PPC64_PLT16_LO",	/* name */
652	 FALSE,			/* partial_inplace */
653	 0,			/* src_mask */
654	 0xffff,		/* dst_mask */
655	 FALSE),		/* pcrel_offset */
656
657  /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
658     the symbol.  */
659  HOWTO (R_PPC64_PLT16_HI,	/* type */
660	 16,			/* rightshift */
661	 1,			/* size (0 = byte, 1 = short, 2 = long) */
662	 16,			/* bitsize */
663	 FALSE,			/* pc_relative */
664	 0,			/* bitpos */
665	 complain_overflow_dont, /* complain_on_overflow */
666	 ppc64_elf_unhandled_reloc, /* special_function */
667	 "R_PPC64_PLT16_HI",	/* name */
668	 FALSE,			/* partial_inplace */
669	 0,			/* src_mask */
670	 0xffff,		/* dst_mask */
671	 FALSE),		/* pcrel_offset */
672
673  /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
674     the symbol.  */
675  HOWTO (R_PPC64_PLT16_HA,	/* type */
676	 16,			/* rightshift */
677	 1,			/* size (0 = byte, 1 = short, 2 = long) */
678	 16,			/* bitsize */
679	 FALSE,			/* pc_relative */
680	 0,			/* bitpos */
681	 complain_overflow_dont, /* complain_on_overflow */
682	 ppc64_elf_unhandled_reloc, /* special_function */
683	 "R_PPC64_PLT16_HA",	/* name */
684	 FALSE,			/* partial_inplace */
685	 0,			/* src_mask */
686	 0xffff,		/* dst_mask */
687	 FALSE),		/* pcrel_offset */
688
689  /* 16-bit section relative relocation.  */
690  HOWTO (R_PPC64_SECTOFF,	/* type */
691	 0,			/* rightshift */
692	 1,			/* size (0 = byte, 1 = short, 2 = long) */
693	 16,			/* bitsize */
694	 FALSE,			/* pc_relative */
695	 0,			/* bitpos */
696	 complain_overflow_bitfield, /* complain_on_overflow */
697	 ppc64_elf_sectoff_reloc, /* special_function */
698	 "R_PPC64_SECTOFF",	/* name */
699	 FALSE,			/* partial_inplace */
700	 0,			/* src_mask */
701	 0xffff,		/* dst_mask */
702	 FALSE),		/* pcrel_offset */
703
704  /* Like R_PPC64_SECTOFF, but no overflow warning.  */
705  HOWTO (R_PPC64_SECTOFF_LO,	/* type */
706	 0,			/* rightshift */
707	 1,			/* size (0 = byte, 1 = short, 2 = long) */
708	 16,			/* bitsize */
709	 FALSE,			/* pc_relative */
710	 0,			/* bitpos */
711	 complain_overflow_dont, /* complain_on_overflow */
712	 ppc64_elf_sectoff_reloc, /* special_function */
713	 "R_PPC64_SECTOFF_LO",	/* name */
714	 FALSE,			/* partial_inplace */
715	 0,			/* src_mask */
716	 0xffff,		/* dst_mask */
717	 FALSE),		/* pcrel_offset */
718
719  /* 16-bit upper half section relative relocation.  */
720  HOWTO (R_PPC64_SECTOFF_HI,	/* type */
721	 16,			/* rightshift */
722	 1,			/* size (0 = byte, 1 = short, 2 = long) */
723	 16,			/* bitsize */
724	 FALSE,			/* pc_relative */
725	 0,			/* bitpos */
726	 complain_overflow_dont, /* complain_on_overflow */
727	 ppc64_elf_sectoff_reloc, /* special_function */
728	 "R_PPC64_SECTOFF_HI",	/* name */
729	 FALSE,			/* partial_inplace */
730	 0,			/* src_mask */
731	 0xffff,		/* dst_mask */
732	 FALSE),		/* pcrel_offset */
733
734  /* 16-bit upper half adjusted section relative relocation.  */
735  HOWTO (R_PPC64_SECTOFF_HA,	/* type */
736	 16,			/* rightshift */
737	 1,			/* size (0 = byte, 1 = short, 2 = long) */
738	 16,			/* bitsize */
739	 FALSE,			/* pc_relative */
740	 0,			/* bitpos */
741	 complain_overflow_dont, /* complain_on_overflow */
742	 ppc64_elf_sectoff_ha_reloc, /* special_function */
743	 "R_PPC64_SECTOFF_HA",	/* name */
744	 FALSE,			/* partial_inplace */
745	 0,			/* src_mask */
746	 0xffff,		/* dst_mask */
747	 FALSE),		/* pcrel_offset */
748
749  /* Like R_PPC64_REL24 without touching the two least significant bits.  */
750  HOWTO (R_PPC64_REL30,		/* type */
751	 2,			/* rightshift */
752	 2,			/* size (0 = byte, 1 = short, 2 = long) */
753	 30,			/* bitsize */
754	 TRUE,			/* pc_relative */
755	 0,			/* bitpos */
756	 complain_overflow_dont, /* complain_on_overflow */
757	 bfd_elf_generic_reloc, /* special_function */
758	 "R_PPC64_REL30",	/* name */
759	 FALSE,			/* partial_inplace */
760	 0,			/* src_mask */
761	 0xfffffffc,		/* dst_mask */
762	 TRUE),			/* pcrel_offset */
763
764  /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
765
766  /* A standard 64-bit relocation.  */
767  HOWTO (R_PPC64_ADDR64,	/* type */
768	 0,			/* rightshift */
769	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
770	 64,			/* bitsize */
771	 FALSE,			/* pc_relative */
772	 0,			/* bitpos */
773	 complain_overflow_dont, /* complain_on_overflow */
774	 bfd_elf_generic_reloc,	/* special_function */
775	 "R_PPC64_ADDR64",	/* name */
776	 FALSE,			/* partial_inplace */
777	 0,			/* src_mask */
778	 ONES (64),		/* dst_mask */
779	 FALSE),		/* pcrel_offset */
780
781  /* The bits 32-47 of an address.  */
782  HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
783	 32,			/* rightshift */
784	 1,			/* size (0 = byte, 1 = short, 2 = long) */
785	 16,			/* bitsize */
786	 FALSE,			/* pc_relative */
787	 0,			/* bitpos */
788	 complain_overflow_dont, /* complain_on_overflow */
789	 bfd_elf_generic_reloc,	/* special_function */
790	 "R_PPC64_ADDR16_HIGHER", /* name */
791	 FALSE,			/* partial_inplace */
792	 0,			/* src_mask */
793	 0xffff,		/* dst_mask */
794	 FALSE),		/* pcrel_offset */
795
796  /* The bits 32-47 of an address, plus 1 if the contents of the low
797     16 bits, treated as a signed number, is negative.  */
798  HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
799	 32,			/* rightshift */
800	 1,			/* size (0 = byte, 1 = short, 2 = long) */
801	 16,			/* bitsize */
802	 FALSE,			/* pc_relative */
803	 0,			/* bitpos */
804	 complain_overflow_dont, /* complain_on_overflow */
805	 ppc64_elf_ha_reloc,	/* special_function */
806	 "R_PPC64_ADDR16_HIGHERA", /* name */
807	 FALSE,			/* partial_inplace */
808	 0,			/* src_mask */
809	 0xffff,		/* dst_mask */
810	 FALSE),		/* pcrel_offset */
811
812  /* The bits 48-63 of an address.  */
813  HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
814	 48,			/* rightshift */
815	 1,			/* size (0 = byte, 1 = short, 2 = long) */
816	 16,			/* bitsize */
817	 FALSE,			/* pc_relative */
818	 0,			/* bitpos */
819	 complain_overflow_dont, /* complain_on_overflow */
820	 bfd_elf_generic_reloc,	/* special_function */
821	 "R_PPC64_ADDR16_HIGHEST", /* name */
822	 FALSE,			/* partial_inplace */
823	 0,			/* src_mask */
824	 0xffff,		/* dst_mask */
825	 FALSE),		/* pcrel_offset */
826
827  /* The bits 48-63 of an address, plus 1 if the contents of the low
828     16 bits, treated as a signed number, is negative.  */
829  HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
830	 48,			/* rightshift */
831	 1,			/* size (0 = byte, 1 = short, 2 = long) */
832	 16,			/* bitsize */
833	 FALSE,			/* pc_relative */
834	 0,			/* bitpos */
835	 complain_overflow_dont, /* complain_on_overflow */
836	 ppc64_elf_ha_reloc,	/* special_function */
837	 "R_PPC64_ADDR16_HIGHESTA", /* name */
838	 FALSE,			/* partial_inplace */
839	 0,			/* src_mask */
840	 0xffff,		/* dst_mask */
841	 FALSE),		/* pcrel_offset */
842
843  /* Like ADDR64, but may be unaligned.  */
844  HOWTO (R_PPC64_UADDR64,	/* type */
845	 0,			/* rightshift */
846	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
847	 64,			/* bitsize */
848	 FALSE,			/* pc_relative */
849	 0,			/* bitpos */
850	 complain_overflow_dont, /* complain_on_overflow */
851	 bfd_elf_generic_reloc,	/* special_function */
852	 "R_PPC64_UADDR64",	/* name */
853	 FALSE,			/* partial_inplace */
854	 0,			/* src_mask */
855	 ONES (64),		/* dst_mask */
856	 FALSE),		/* pcrel_offset */
857
858  /* 64-bit relative relocation.  */
859  HOWTO (R_PPC64_REL64,		/* type */
860	 0,			/* rightshift */
861	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
862	 64,			/* bitsize */
863	 TRUE,			/* pc_relative */
864	 0,			/* bitpos */
865	 complain_overflow_dont, /* complain_on_overflow */
866	 bfd_elf_generic_reloc,	/* special_function */
867	 "R_PPC64_REL64",	/* name */
868	 FALSE,			/* partial_inplace */
869	 0,			/* src_mask */
870	 ONES (64),		/* dst_mask */
871	 TRUE),			/* pcrel_offset */
872
873  /* 64-bit relocation to the symbol's procedure linkage table.  */
874  HOWTO (R_PPC64_PLT64,		/* type */
875	 0,			/* rightshift */
876	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
877	 64,			/* bitsize */
878	 FALSE,			/* pc_relative */
879	 0,			/* bitpos */
880	 complain_overflow_dont, /* complain_on_overflow */
881	 ppc64_elf_unhandled_reloc, /* special_function */
882	 "R_PPC64_PLT64",	/* name */
883	 FALSE,			/* partial_inplace */
884	 0,			/* src_mask */
885	 ONES (64),		/* dst_mask */
886	 FALSE),		/* pcrel_offset */
887
888  /* 64-bit PC relative relocation to the symbol's procedure linkage
889     table.  */
890  /* FIXME: R_PPC64_PLTREL64 not supported.  */
891  HOWTO (R_PPC64_PLTREL64,	/* type */
892	 0,			/* rightshift */
893	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
894	 64,			/* bitsize */
895	 TRUE,			/* pc_relative */
896	 0,			/* bitpos */
897	 complain_overflow_dont, /* complain_on_overflow */
898	 ppc64_elf_unhandled_reloc, /* special_function */
899	 "R_PPC64_PLTREL64",	/* name */
900	 FALSE,			/* partial_inplace */
901	 0,			/* src_mask */
902	 ONES (64),		/* dst_mask */
903	 TRUE),			/* pcrel_offset */
904
905  /* 16 bit TOC-relative relocation.  */
906
907  /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
908  HOWTO (R_PPC64_TOC16,		/* type */
909	 0,			/* rightshift */
910	 1,			/* size (0 = byte, 1 = short, 2 = long) */
911	 16,			/* bitsize */
912	 FALSE,			/* pc_relative */
913	 0,			/* bitpos */
914	 complain_overflow_signed, /* complain_on_overflow */
915	 ppc64_elf_toc_reloc,	/* special_function */
916	 "R_PPC64_TOC16",	/* name */
917	 FALSE,			/* partial_inplace */
918	 0,			/* src_mask */
919	 0xffff,		/* dst_mask */
920	 FALSE),		/* pcrel_offset */
921
922  /* 16 bit TOC-relative relocation without overflow.  */
923
924  /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
925  HOWTO (R_PPC64_TOC16_LO,	/* type */
926	 0,			/* rightshift */
927	 1,			/* size (0 = byte, 1 = short, 2 = long) */
928	 16,			/* bitsize */
929	 FALSE,			/* pc_relative */
930	 0,			/* bitpos */
931	 complain_overflow_dont, /* complain_on_overflow */
932	 ppc64_elf_toc_reloc,	/* special_function */
933	 "R_PPC64_TOC16_LO",	/* name */
934	 FALSE,			/* partial_inplace */
935	 0,			/* src_mask */
936	 0xffff,		/* dst_mask */
937	 FALSE),		/* pcrel_offset */
938
939  /* 16 bit TOC-relative relocation, high 16 bits.  */
940
941  /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
942  HOWTO (R_PPC64_TOC16_HI,	/* type */
943	 16,			/* rightshift */
944	 1,			/* size (0 = byte, 1 = short, 2 = long) */
945	 16,			/* bitsize */
946	 FALSE,			/* pc_relative */
947	 0,			/* bitpos */
948	 complain_overflow_dont, /* complain_on_overflow */
949	 ppc64_elf_toc_reloc,	/* special_function */
950	 "R_PPC64_TOC16_HI",	/* name */
951	 FALSE,			/* partial_inplace */
952	 0,			/* src_mask */
953	 0xffff,		/* dst_mask */
954	 FALSE),		/* pcrel_offset */
955
956  /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
957     contents of the low 16 bits, treated as a signed number, is
958     negative.  */
959
960  /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
961  HOWTO (R_PPC64_TOC16_HA,	/* type */
962	 16,			/* rightshift */
963	 1,			/* size (0 = byte, 1 = short, 2 = long) */
964	 16,			/* bitsize */
965	 FALSE,			/* pc_relative */
966	 0,			/* bitpos */
967	 complain_overflow_dont, /* complain_on_overflow */
968	 ppc64_elf_toc_ha_reloc, /* special_function */
969	 "R_PPC64_TOC16_HA",	/* name */
970	 FALSE,			/* partial_inplace */
971	 0,			/* src_mask */
972	 0xffff,		/* dst_mask */
973	 FALSE),		/* pcrel_offset */
974
975  /* 64-bit relocation; insert value of TOC base (.TOC.).  */
976
977  /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
978  HOWTO (R_PPC64_TOC,		/* type */
979	 0,			/* rightshift */
980	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
981	 64,			/* bitsize */
982	 FALSE,			/* pc_relative */
983	 0,			/* bitpos */
984	 complain_overflow_bitfield, /* complain_on_overflow */
985	 ppc64_elf_toc64_reloc,	/* special_function */
986	 "R_PPC64_TOC",		/* name */
987	 FALSE,			/* partial_inplace */
988	 0,			/* src_mask */
989	 ONES (64),		/* dst_mask */
990	 FALSE),		/* pcrel_offset */
991
992  /* Like R_PPC64_GOT16, but also informs the link editor that the
993     value to relocate may (!) refer to a PLT entry which the link
994     editor (a) may replace with the symbol value.  If the link editor
995     is unable to fully resolve the symbol, it may (b) create a PLT
996     entry and store the address to the new PLT entry in the GOT.
997     This permits lazy resolution of function symbols at run time.
998     The link editor may also skip all of this and just (c) emit a
999     R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1000  /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1001    HOWTO (R_PPC64_PLTGOT16,	/* type */
1002	 0,			/* rightshift */
1003	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1004	 16,			/* bitsize */
1005	 FALSE,			/* pc_relative */
1006	 0,			/* bitpos */
1007	 complain_overflow_signed, /* complain_on_overflow */
1008	 ppc64_elf_unhandled_reloc, /* special_function */
1009	 "R_PPC64_PLTGOT16",	/* name */
1010	 FALSE,			/* partial_inplace */
1011	 0,			/* src_mask */
1012	 0xffff,		/* dst_mask */
1013	 FALSE),		/* pcrel_offset */
1014
1015  /* Like R_PPC64_PLTGOT16, but without overflow.  */
1016  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1017  HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
1018	 0,			/* rightshift */
1019	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1020	 16,			/* bitsize */
1021	 FALSE,			/* pc_relative */
1022	 0,			/* bitpos */
1023	 complain_overflow_dont, /* complain_on_overflow */
1024	 ppc64_elf_unhandled_reloc, /* special_function */
1025	 "R_PPC64_PLTGOT16_LO",	/* name */
1026	 FALSE,			/* partial_inplace */
1027	 0,			/* src_mask */
1028	 0xffff,		/* dst_mask */
1029	 FALSE),		/* pcrel_offset */
1030
1031  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1032  /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1033  HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
1034	 16,			/* rightshift */
1035	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1036	 16,			/* bitsize */
1037	 FALSE,			/* pc_relative */
1038	 0,			/* bitpos */
1039	 complain_overflow_dont, /* complain_on_overflow */
1040	 ppc64_elf_unhandled_reloc, /* special_function */
1041	 "R_PPC64_PLTGOT16_HI",	/* name */
1042	 FALSE,			/* partial_inplace */
1043	 0,			/* src_mask */
1044	 0xffff,		/* dst_mask */
1045	 FALSE),		/* pcrel_offset */
1046
1047  /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1048     1 if the contents of the low 16 bits, treated as a signed number,
1049     is negative.  */
1050  /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1051  HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
1052	 16,			/* rightshift */
1053	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1054	 16,			/* bitsize */
1055	 FALSE,			/* pc_relative */
1056	 0,			/* bitpos */
1057	 complain_overflow_dont,/* complain_on_overflow */
1058	 ppc64_elf_unhandled_reloc, /* special_function */
1059	 "R_PPC64_PLTGOT16_HA",	/* name */
1060	 FALSE,			/* partial_inplace */
1061	 0,			/* src_mask */
1062	 0xffff,		/* dst_mask */
1063	 FALSE),		/* pcrel_offset */
1064
1065  /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1066  HOWTO (R_PPC64_ADDR16_DS,	/* type */
1067	 0,			/* rightshift */
1068	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1069	 16,			/* bitsize */
1070	 FALSE,			/* pc_relative */
1071	 0,			/* bitpos */
1072	 complain_overflow_bitfield, /* complain_on_overflow */
1073	 bfd_elf_generic_reloc,	/* special_function */
1074	 "R_PPC64_ADDR16_DS",	/* name */
1075	 FALSE,			/* partial_inplace */
1076	 0,			/* src_mask */
1077	 0xfffc,		/* dst_mask */
1078	 FALSE),		/* pcrel_offset */
1079
1080  /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1081  HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
1082	 0,			/* rightshift */
1083	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1084	 16,			/* bitsize */
1085	 FALSE,			/* pc_relative */
1086	 0,			/* bitpos */
1087	 complain_overflow_dont,/* complain_on_overflow */
1088	 bfd_elf_generic_reloc,	/* special_function */
1089	 "R_PPC64_ADDR16_LO_DS",/* name */
1090	 FALSE,			/* partial_inplace */
1091	 0,			/* src_mask */
1092	 0xfffc,		/* dst_mask */
1093	 FALSE),		/* pcrel_offset */
1094
1095  /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1096  HOWTO (R_PPC64_GOT16_DS,	/* type */
1097	 0,			/* rightshift */
1098	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1099	 16,			/* bitsize */
1100	 FALSE,			/* pc_relative */
1101	 0,			/* bitpos */
1102	 complain_overflow_signed, /* complain_on_overflow */
1103	 ppc64_elf_unhandled_reloc, /* special_function */
1104	 "R_PPC64_GOT16_DS",	/* name */
1105	 FALSE,			/* partial_inplace */
1106	 0,			/* src_mask */
1107	 0xfffc,		/* dst_mask */
1108	 FALSE),		/* pcrel_offset */
1109
1110  /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1111  HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
1112	 0,			/* rightshift */
1113	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1114	 16,			/* bitsize */
1115	 FALSE,			/* pc_relative */
1116	 0,			/* bitpos */
1117	 complain_overflow_dont, /* complain_on_overflow */
1118	 ppc64_elf_unhandled_reloc, /* special_function */
1119	 "R_PPC64_GOT16_LO_DS",	/* name */
1120	 FALSE,			/* partial_inplace */
1121	 0,			/* src_mask */
1122	 0xfffc,		/* dst_mask */
1123	 FALSE),		/* pcrel_offset */
1124
1125  /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1126  HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
1127	 0,			/* rightshift */
1128	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1129	 16,			/* bitsize */
1130	 FALSE,			/* pc_relative */
1131	 0,			/* bitpos */
1132	 complain_overflow_dont, /* complain_on_overflow */
1133	 ppc64_elf_unhandled_reloc, /* special_function */
1134	 "R_PPC64_PLT16_LO_DS",	/* name */
1135	 FALSE,			/* partial_inplace */
1136	 0,			/* src_mask */
1137	 0xfffc,		/* dst_mask */
1138	 FALSE),		/* pcrel_offset */
1139
1140  /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1141  HOWTO (R_PPC64_SECTOFF_DS,	/* type */
1142	 0,			/* rightshift */
1143	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1144	 16,			/* bitsize */
1145	 FALSE,			/* pc_relative */
1146	 0,			/* bitpos */
1147	 complain_overflow_bitfield, /* complain_on_overflow */
1148	 ppc64_elf_sectoff_reloc, /* special_function */
1149	 "R_PPC64_SECTOFF_DS",	/* name */
1150	 FALSE,			/* partial_inplace */
1151	 0,			/* src_mask */
1152	 0xfffc,		/* dst_mask */
1153	 FALSE),		/* pcrel_offset */
1154
1155  /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1156  HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1157	 0,			/* rightshift */
1158	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1159	 16,			/* bitsize */
1160	 FALSE,			/* pc_relative */
1161	 0,			/* bitpos */
1162	 complain_overflow_dont, /* complain_on_overflow */
1163	 ppc64_elf_sectoff_reloc, /* special_function */
1164	 "R_PPC64_SECTOFF_LO_DS",/* name */
1165	 FALSE,			/* partial_inplace */
1166	 0,			/* src_mask */
1167	 0xfffc,		/* dst_mask */
1168	 FALSE),		/* pcrel_offset */
1169
1170  /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1171  HOWTO (R_PPC64_TOC16_DS,	/* type */
1172	 0,			/* rightshift */
1173	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1174	 16,			/* bitsize */
1175	 FALSE,			/* pc_relative */
1176	 0,			/* bitpos */
1177	 complain_overflow_signed, /* complain_on_overflow */
1178	 ppc64_elf_toc_reloc,	/* special_function */
1179	 "R_PPC64_TOC16_DS",	/* name */
1180	 FALSE,			/* partial_inplace */
1181	 0,			/* src_mask */
1182	 0xfffc,		/* dst_mask */
1183	 FALSE),		/* pcrel_offset */
1184
1185  /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1186  HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
1187	 0,			/* rightshift */
1188	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1189	 16,			/* bitsize */
1190	 FALSE,			/* pc_relative */
1191	 0,			/* bitpos */
1192	 complain_overflow_dont, /* complain_on_overflow */
1193	 ppc64_elf_toc_reloc,	/* special_function */
1194	 "R_PPC64_TOC16_LO_DS",	/* name */
1195	 FALSE,			/* partial_inplace */
1196	 0,			/* src_mask */
1197	 0xfffc,		/* dst_mask */
1198	 FALSE),		/* pcrel_offset */
1199
1200  /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1201  /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1202  HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
1203	 0,			/* rightshift */
1204	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1205	 16,			/* bitsize */
1206	 FALSE,			/* pc_relative */
1207	 0,			/* bitpos */
1208	 complain_overflow_signed, /* complain_on_overflow */
1209	 ppc64_elf_unhandled_reloc, /* special_function */
1210	 "R_PPC64_PLTGOT16_DS",	/* name */
1211	 FALSE,			/* partial_inplace */
1212	 0,			/* src_mask */
1213	 0xfffc,		/* dst_mask */
1214	 FALSE),		/* pcrel_offset */
1215
1216  /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1217  /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1218  HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1219	 0,			/* rightshift */
1220	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1221	 16,			/* bitsize */
1222	 FALSE,			/* pc_relative */
1223	 0,			/* bitpos */
1224	 complain_overflow_dont, /* complain_on_overflow */
1225	 ppc64_elf_unhandled_reloc, /* special_function */
1226	 "R_PPC64_PLTGOT16_LO_DS",/* name */
1227	 FALSE,			/* partial_inplace */
1228	 0,			/* src_mask */
1229	 0xfffc,		/* dst_mask */
1230	 FALSE),		/* pcrel_offset */
1231
1232  /* Marker reloc for TLS.  */
1233  HOWTO (R_PPC64_TLS,
1234	 0,			/* rightshift */
1235	 2,			/* size (0 = byte, 1 = short, 2 = long) */
1236	 32,			/* bitsize */
1237	 FALSE,			/* pc_relative */
1238	 0,			/* bitpos */
1239	 complain_overflow_dont, /* complain_on_overflow */
1240	 bfd_elf_generic_reloc,	/* special_function */
1241	 "R_PPC64_TLS",		/* name */
1242	 FALSE,			/* partial_inplace */
1243	 0,			/* src_mask */
1244	 0,			/* dst_mask */
1245	 FALSE),		/* pcrel_offset */
1246
1247  /* Computes the load module index of the load module that contains the
1248     definition of its TLS sym.  */
1249  HOWTO (R_PPC64_DTPMOD64,
1250	 0,			/* rightshift */
1251	 4,			/* size (0 = byte, 1 = short, 2 = long) */
1252	 64,			/* bitsize */
1253	 FALSE,			/* pc_relative */
1254	 0,			/* bitpos */
1255	 complain_overflow_dont, /* complain_on_overflow */
1256	 ppc64_elf_unhandled_reloc, /* special_function */
1257	 "R_PPC64_DTPMOD64",	/* name */
1258	 FALSE,			/* partial_inplace */
1259	 0,			/* src_mask */
1260	 ONES (64),		/* dst_mask */
1261	 FALSE),		/* pcrel_offset */
1262
1263  /* Computes a dtv-relative displacement, the difference between the value
1264     of sym+add and the base address of the thread-local storage block that
1265     contains the definition of sym, minus 0x8000.  */
1266  HOWTO (R_PPC64_DTPREL64,
1267	 0,			/* rightshift */
1268	 4,			/* size (0 = byte, 1 = short, 2 = long) */
1269	 64,			/* bitsize */
1270	 FALSE,			/* pc_relative */
1271	 0,			/* bitpos */
1272	 complain_overflow_dont, /* complain_on_overflow */
1273	 ppc64_elf_unhandled_reloc, /* special_function */
1274	 "R_PPC64_DTPREL64",	/* name */
1275	 FALSE,			/* partial_inplace */
1276	 0,			/* src_mask */
1277	 ONES (64),		/* dst_mask */
1278	 FALSE),		/* pcrel_offset */
1279
1280  /* A 16 bit dtprel reloc.  */
1281  HOWTO (R_PPC64_DTPREL16,
1282	 0,			/* rightshift */
1283	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1284	 16,			/* bitsize */
1285	 FALSE,			/* pc_relative */
1286	 0,			/* bitpos */
1287	 complain_overflow_signed, /* complain_on_overflow */
1288	 ppc64_elf_unhandled_reloc, /* special_function */
1289	 "R_PPC64_DTPREL16",	/* name */
1290	 FALSE,			/* partial_inplace */
1291	 0,			/* src_mask */
1292	 0xffff,		/* dst_mask */
1293	 FALSE),		/* pcrel_offset */
1294
1295  /* Like DTPREL16, but no overflow.  */
1296  HOWTO (R_PPC64_DTPREL16_LO,
1297	 0,			/* rightshift */
1298	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1299	 16,			/* bitsize */
1300	 FALSE,			/* pc_relative */
1301	 0,			/* bitpos */
1302	 complain_overflow_dont, /* complain_on_overflow */
1303	 ppc64_elf_unhandled_reloc, /* special_function */
1304	 "R_PPC64_DTPREL16_LO",	/* name */
1305	 FALSE,			/* partial_inplace */
1306	 0,			/* src_mask */
1307	 0xffff,		/* dst_mask */
1308	 FALSE),		/* pcrel_offset */
1309
1310  /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1311  HOWTO (R_PPC64_DTPREL16_HI,
1312	 16,			/* rightshift */
1313	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1314	 16,			/* bitsize */
1315	 FALSE,			/* pc_relative */
1316	 0,			/* bitpos */
1317	 complain_overflow_dont, /* complain_on_overflow */
1318	 ppc64_elf_unhandled_reloc, /* special_function */
1319	 "R_PPC64_DTPREL16_HI",	/* name */
1320	 FALSE,			/* partial_inplace */
1321	 0,			/* src_mask */
1322	 0xffff,		/* dst_mask */
1323	 FALSE),		/* pcrel_offset */
1324
1325  /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1326  HOWTO (R_PPC64_DTPREL16_HA,
1327	 16,			/* rightshift */
1328	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1329	 16,			/* bitsize */
1330	 FALSE,			/* pc_relative */
1331	 0,			/* bitpos */
1332	 complain_overflow_dont, /* complain_on_overflow */
1333	 ppc64_elf_unhandled_reloc, /* special_function */
1334	 "R_PPC64_DTPREL16_HA",	/* name */
1335	 FALSE,			/* partial_inplace */
1336	 0,			/* src_mask */
1337	 0xffff,		/* dst_mask */
1338	 FALSE),		/* pcrel_offset */
1339
1340  /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1341  HOWTO (R_PPC64_DTPREL16_HIGHER,
1342	 32,			/* rightshift */
1343	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1344	 16,			/* bitsize */
1345	 FALSE,			/* pc_relative */
1346	 0,			/* bitpos */
1347	 complain_overflow_dont, /* complain_on_overflow */
1348	 ppc64_elf_unhandled_reloc, /* special_function */
1349	 "R_PPC64_DTPREL16_HIGHER", /* name */
1350	 FALSE,			/* partial_inplace */
1351	 0,			/* src_mask */
1352	 0xffff,		/* dst_mask */
1353	 FALSE),		/* pcrel_offset */
1354
1355  /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1356  HOWTO (R_PPC64_DTPREL16_HIGHERA,
1357	 32,			/* rightshift */
1358	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1359	 16,			/* bitsize */
1360	 FALSE,			/* pc_relative */
1361	 0,			/* bitpos */
1362	 complain_overflow_dont, /* complain_on_overflow */
1363	 ppc64_elf_unhandled_reloc, /* special_function */
1364	 "R_PPC64_DTPREL16_HIGHERA", /* name */
1365	 FALSE,			/* partial_inplace */
1366	 0,			/* src_mask */
1367	 0xffff,		/* dst_mask */
1368	 FALSE),		/* pcrel_offset */
1369
1370  /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1371  HOWTO (R_PPC64_DTPREL16_HIGHEST,
1372	 48,			/* rightshift */
1373	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1374	 16,			/* bitsize */
1375	 FALSE,			/* pc_relative */
1376	 0,			/* bitpos */
1377	 complain_overflow_dont, /* complain_on_overflow */
1378	 ppc64_elf_unhandled_reloc, /* special_function */
1379	 "R_PPC64_DTPREL16_HIGHEST", /* name */
1380	 FALSE,			/* partial_inplace */
1381	 0,			/* src_mask */
1382	 0xffff,		/* dst_mask */
1383	 FALSE),		/* pcrel_offset */
1384
1385  /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1386  HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1387	 48,			/* rightshift */
1388	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1389	 16,			/* bitsize */
1390	 FALSE,			/* pc_relative */
1391	 0,			/* bitpos */
1392	 complain_overflow_dont, /* complain_on_overflow */
1393	 ppc64_elf_unhandled_reloc, /* special_function */
1394	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1395	 FALSE,			/* partial_inplace */
1396	 0,			/* src_mask */
1397	 0xffff,		/* dst_mask */
1398	 FALSE),		/* pcrel_offset */
1399
1400  /* Like DTPREL16, but for insns with a DS field.  */
1401  HOWTO (R_PPC64_DTPREL16_DS,
1402	 0,			/* rightshift */
1403	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1404	 16,			/* bitsize */
1405	 FALSE,			/* pc_relative */
1406	 0,			/* bitpos */
1407	 complain_overflow_signed, /* complain_on_overflow */
1408	 ppc64_elf_unhandled_reloc, /* special_function */
1409	 "R_PPC64_DTPREL16_DS",	/* name */
1410	 FALSE,			/* partial_inplace */
1411	 0,			/* src_mask */
1412	 0xfffc,		/* dst_mask */
1413	 FALSE),		/* pcrel_offset */
1414
1415  /* Like DTPREL16_DS, but no overflow.  */
1416  HOWTO (R_PPC64_DTPREL16_LO_DS,
1417	 0,			/* rightshift */
1418	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1419	 16,			/* bitsize */
1420	 FALSE,			/* pc_relative */
1421	 0,			/* bitpos */
1422	 complain_overflow_dont, /* complain_on_overflow */
1423	 ppc64_elf_unhandled_reloc, /* special_function */
1424	 "R_PPC64_DTPREL16_LO_DS", /* name */
1425	 FALSE,			/* partial_inplace */
1426	 0,			/* src_mask */
1427	 0xfffc,		/* dst_mask */
1428	 FALSE),		/* pcrel_offset */
1429
1430  /* Computes a tp-relative displacement, the difference between the value of
1431     sym+add and the value of the thread pointer (r13).  */
1432  HOWTO (R_PPC64_TPREL64,
1433	 0,			/* rightshift */
1434	 4,			/* size (0 = byte, 1 = short, 2 = long) */
1435	 64,			/* bitsize */
1436	 FALSE,			/* pc_relative */
1437	 0,			/* bitpos */
1438	 complain_overflow_dont, /* complain_on_overflow */
1439	 ppc64_elf_unhandled_reloc, /* special_function */
1440	 "R_PPC64_TPREL64",	/* name */
1441	 FALSE,			/* partial_inplace */
1442	 0,			/* src_mask */
1443	 ONES (64),		/* dst_mask */
1444	 FALSE),		/* pcrel_offset */
1445
1446  /* A 16 bit tprel reloc.  */
1447  HOWTO (R_PPC64_TPREL16,
1448	 0,			/* rightshift */
1449	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1450	 16,			/* bitsize */
1451	 FALSE,			/* pc_relative */
1452	 0,			/* bitpos */
1453	 complain_overflow_signed, /* complain_on_overflow */
1454	 ppc64_elf_unhandled_reloc, /* special_function */
1455	 "R_PPC64_TPREL16",	/* name */
1456	 FALSE,			/* partial_inplace */
1457	 0,			/* src_mask */
1458	 0xffff,		/* dst_mask */
1459	 FALSE),		/* pcrel_offset */
1460
1461  /* Like TPREL16, but no overflow.  */
1462  HOWTO (R_PPC64_TPREL16_LO,
1463	 0,			/* rightshift */
1464	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1465	 16,			/* bitsize */
1466	 FALSE,			/* pc_relative */
1467	 0,			/* bitpos */
1468	 complain_overflow_dont, /* complain_on_overflow */
1469	 ppc64_elf_unhandled_reloc, /* special_function */
1470	 "R_PPC64_TPREL16_LO",	/* name */
1471	 FALSE,			/* partial_inplace */
1472	 0,			/* src_mask */
1473	 0xffff,		/* dst_mask */
1474	 FALSE),		/* pcrel_offset */
1475
1476  /* Like TPREL16_LO, but next higher group of 16 bits.  */
1477  HOWTO (R_PPC64_TPREL16_HI,
1478	 16,			/* rightshift */
1479	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1480	 16,			/* bitsize */
1481	 FALSE,			/* pc_relative */
1482	 0,			/* bitpos */
1483	 complain_overflow_dont, /* complain_on_overflow */
1484	 ppc64_elf_unhandled_reloc, /* special_function */
1485	 "R_PPC64_TPREL16_HI",	/* name */
1486	 FALSE,			/* partial_inplace */
1487	 0,			/* src_mask */
1488	 0xffff,		/* dst_mask */
1489	 FALSE),		/* pcrel_offset */
1490
1491  /* Like TPREL16_HI, but adjust for low 16 bits.  */
1492  HOWTO (R_PPC64_TPREL16_HA,
1493	 16,			/* rightshift */
1494	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1495	 16,			/* bitsize */
1496	 FALSE,			/* pc_relative */
1497	 0,			/* bitpos */
1498	 complain_overflow_dont, /* complain_on_overflow */
1499	 ppc64_elf_unhandled_reloc, /* special_function */
1500	 "R_PPC64_TPREL16_HA",	/* name */
1501	 FALSE,			/* partial_inplace */
1502	 0,			/* src_mask */
1503	 0xffff,		/* dst_mask */
1504	 FALSE),		/* pcrel_offset */
1505
1506  /* Like TPREL16_HI, but next higher group of 16 bits.  */
1507  HOWTO (R_PPC64_TPREL16_HIGHER,
1508	 32,			/* rightshift */
1509	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1510	 16,			/* bitsize */
1511	 FALSE,			/* pc_relative */
1512	 0,			/* bitpos */
1513	 complain_overflow_dont, /* complain_on_overflow */
1514	 ppc64_elf_unhandled_reloc, /* special_function */
1515	 "R_PPC64_TPREL16_HIGHER",	/* name */
1516	 FALSE,			/* partial_inplace */
1517	 0,			/* src_mask */
1518	 0xffff,		/* dst_mask */
1519	 FALSE),		/* pcrel_offset */
1520
1521  /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1522  HOWTO (R_PPC64_TPREL16_HIGHERA,
1523	 32,			/* rightshift */
1524	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1525	 16,			/* bitsize */
1526	 FALSE,			/* pc_relative */
1527	 0,			/* bitpos */
1528	 complain_overflow_dont, /* complain_on_overflow */
1529	 ppc64_elf_unhandled_reloc, /* special_function */
1530	 "R_PPC64_TPREL16_HIGHERA", /* name */
1531	 FALSE,			/* partial_inplace */
1532	 0,			/* src_mask */
1533	 0xffff,		/* dst_mask */
1534	 FALSE),		/* pcrel_offset */
1535
1536  /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1537  HOWTO (R_PPC64_TPREL16_HIGHEST,
1538	 48,			/* rightshift */
1539	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1540	 16,			/* bitsize */
1541	 FALSE,			/* pc_relative */
1542	 0,			/* bitpos */
1543	 complain_overflow_dont, /* complain_on_overflow */
1544	 ppc64_elf_unhandled_reloc, /* special_function */
1545	 "R_PPC64_TPREL16_HIGHEST", /* name */
1546	 FALSE,			/* partial_inplace */
1547	 0,			/* src_mask */
1548	 0xffff,		/* dst_mask */
1549	 FALSE),		/* pcrel_offset */
1550
1551  /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1552  HOWTO (R_PPC64_TPREL16_HIGHESTA,
1553	 48,			/* rightshift */
1554	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1555	 16,			/* bitsize */
1556	 FALSE,			/* pc_relative */
1557	 0,			/* bitpos */
1558	 complain_overflow_dont, /* complain_on_overflow */
1559	 ppc64_elf_unhandled_reloc, /* special_function */
1560	 "R_PPC64_TPREL16_HIGHESTA", /* name */
1561	 FALSE,			/* partial_inplace */
1562	 0,			/* src_mask */
1563	 0xffff,		/* dst_mask */
1564	 FALSE),		/* pcrel_offset */
1565
1566  /* Like TPREL16, but for insns with a DS field.  */
1567  HOWTO (R_PPC64_TPREL16_DS,
1568	 0,			/* rightshift */
1569	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1570	 16,			/* bitsize */
1571	 FALSE,			/* pc_relative */
1572	 0,			/* bitpos */
1573	 complain_overflow_signed, /* complain_on_overflow */
1574	 ppc64_elf_unhandled_reloc, /* special_function */
1575	 "R_PPC64_TPREL16_DS",	/* name */
1576	 FALSE,			/* partial_inplace */
1577	 0,			/* src_mask */
1578	 0xfffc,		/* dst_mask */
1579	 FALSE),		/* pcrel_offset */
1580
1581  /* Like TPREL16_DS, but no overflow.  */
1582  HOWTO (R_PPC64_TPREL16_LO_DS,
1583	 0,			/* rightshift */
1584	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1585	 16,			/* bitsize */
1586	 FALSE,			/* pc_relative */
1587	 0,			/* bitpos */
1588	 complain_overflow_dont, /* complain_on_overflow */
1589	 ppc64_elf_unhandled_reloc, /* special_function */
1590	 "R_PPC64_TPREL16_LO_DS", /* name */
1591	 FALSE,			/* partial_inplace */
1592	 0,			/* src_mask */
1593	 0xfffc,		/* dst_mask */
1594	 FALSE),		/* pcrel_offset */
1595
1596  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1597     with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1598     to the first entry relative to the TOC base (r2).  */
1599  HOWTO (R_PPC64_GOT_TLSGD16,
1600	 0,			/* rightshift */
1601	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1602	 16,			/* bitsize */
1603	 FALSE,			/* pc_relative */
1604	 0,			/* bitpos */
1605	 complain_overflow_signed, /* complain_on_overflow */
1606	 ppc64_elf_unhandled_reloc, /* special_function */
1607	 "R_PPC64_GOT_TLSGD16",	/* name */
1608	 FALSE,			/* partial_inplace */
1609	 0,			/* src_mask */
1610	 0xffff,		/* dst_mask */
1611	 FALSE),		/* pcrel_offset */
1612
1613  /* Like GOT_TLSGD16, but no overflow.  */
1614  HOWTO (R_PPC64_GOT_TLSGD16_LO,
1615	 0,			/* rightshift */
1616	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1617	 16,			/* bitsize */
1618	 FALSE,			/* pc_relative */
1619	 0,			/* bitpos */
1620	 complain_overflow_dont, /* complain_on_overflow */
1621	 ppc64_elf_unhandled_reloc, /* special_function */
1622	 "R_PPC64_GOT_TLSGD16_LO", /* name */
1623	 FALSE,			/* partial_inplace */
1624	 0,			/* src_mask */
1625	 0xffff,		/* dst_mask */
1626	 FALSE),		/* pcrel_offset */
1627
1628  /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1629  HOWTO (R_PPC64_GOT_TLSGD16_HI,
1630	 16,			/* rightshift */
1631	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1632	 16,			/* bitsize */
1633	 FALSE,			/* pc_relative */
1634	 0,			/* bitpos */
1635	 complain_overflow_dont, /* complain_on_overflow */
1636	 ppc64_elf_unhandled_reloc, /* special_function */
1637	 "R_PPC64_GOT_TLSGD16_HI", /* name */
1638	 FALSE,			/* partial_inplace */
1639	 0,			/* src_mask */
1640	 0xffff,		/* dst_mask */
1641	 FALSE),		/* pcrel_offset */
1642
1643  /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1644  HOWTO (R_PPC64_GOT_TLSGD16_HA,
1645	 16,			/* rightshift */
1646	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1647	 16,			/* bitsize */
1648	 FALSE,			/* pc_relative */
1649	 0,			/* bitpos */
1650	 complain_overflow_dont, /* complain_on_overflow */
1651	 ppc64_elf_unhandled_reloc, /* special_function */
1652	 "R_PPC64_GOT_TLSGD16_HA", /* name */
1653	 FALSE,			/* partial_inplace */
1654	 0,			/* src_mask */
1655	 0xffff,		/* dst_mask */
1656	 FALSE),		/* pcrel_offset */
1657
1658  /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1659     with values (sym+add)@dtpmod and zero, and computes the offset to the
1660     first entry relative to the TOC base (r2).  */
1661  HOWTO (R_PPC64_GOT_TLSLD16,
1662	 0,			/* rightshift */
1663	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1664	 16,			/* bitsize */
1665	 FALSE,			/* pc_relative */
1666	 0,			/* bitpos */
1667	 complain_overflow_signed, /* complain_on_overflow */
1668	 ppc64_elf_unhandled_reloc, /* special_function */
1669	 "R_PPC64_GOT_TLSLD16",	/* name */
1670	 FALSE,			/* partial_inplace */
1671	 0,			/* src_mask */
1672	 0xffff,		/* dst_mask */
1673	 FALSE),		/* pcrel_offset */
1674
1675  /* Like GOT_TLSLD16, but no overflow.  */
1676  HOWTO (R_PPC64_GOT_TLSLD16_LO,
1677	 0,			/* rightshift */
1678	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1679	 16,			/* bitsize */
1680	 FALSE,			/* pc_relative */
1681	 0,			/* bitpos */
1682	 complain_overflow_dont, /* complain_on_overflow */
1683	 ppc64_elf_unhandled_reloc, /* special_function */
1684	 "R_PPC64_GOT_TLSLD16_LO", /* name */
1685	 FALSE,			/* partial_inplace */
1686	 0,			/* src_mask */
1687	 0xffff,		/* dst_mask */
1688	 FALSE),		/* pcrel_offset */
1689
1690  /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1691  HOWTO (R_PPC64_GOT_TLSLD16_HI,
1692	 16,			/* rightshift */
1693	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1694	 16,			/* bitsize */
1695	 FALSE,			/* pc_relative */
1696	 0,			/* bitpos */
1697	 complain_overflow_dont, /* complain_on_overflow */
1698	 ppc64_elf_unhandled_reloc, /* special_function */
1699	 "R_PPC64_GOT_TLSLD16_HI", /* name */
1700	 FALSE,			/* partial_inplace */
1701	 0,			/* src_mask */
1702	 0xffff,		/* dst_mask */
1703	 FALSE),		/* pcrel_offset */
1704
1705  /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1706  HOWTO (R_PPC64_GOT_TLSLD16_HA,
1707	 16,			/* rightshift */
1708	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1709	 16,			/* bitsize */
1710	 FALSE,			/* pc_relative */
1711	 0,			/* bitpos */
1712	 complain_overflow_dont, /* complain_on_overflow */
1713	 ppc64_elf_unhandled_reloc, /* special_function */
1714	 "R_PPC64_GOT_TLSLD16_HA", /* name */
1715	 FALSE,			/* partial_inplace */
1716	 0,			/* src_mask */
1717	 0xffff,		/* dst_mask */
1718	 FALSE),		/* pcrel_offset */
1719
1720  /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1721     the offset to the entry relative to the TOC base (r2).  */
1722  HOWTO (R_PPC64_GOT_DTPREL16_DS,
1723	 0,			/* rightshift */
1724	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1725	 16,			/* bitsize */
1726	 FALSE,			/* pc_relative */
1727	 0,			/* bitpos */
1728	 complain_overflow_signed, /* complain_on_overflow */
1729	 ppc64_elf_unhandled_reloc, /* special_function */
1730	 "R_PPC64_GOT_DTPREL16_DS", /* name */
1731	 FALSE,			/* partial_inplace */
1732	 0,			/* src_mask */
1733	 0xfffc,		/* dst_mask */
1734	 FALSE),		/* pcrel_offset */
1735
1736  /* Like GOT_DTPREL16_DS, but no overflow.  */
1737  HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1738	 0,			/* rightshift */
1739	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1740	 16,			/* bitsize */
1741	 FALSE,			/* pc_relative */
1742	 0,			/* bitpos */
1743	 complain_overflow_dont, /* complain_on_overflow */
1744	 ppc64_elf_unhandled_reloc, /* special_function */
1745	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1746	 FALSE,			/* partial_inplace */
1747	 0,			/* src_mask */
1748	 0xfffc,		/* dst_mask */
1749	 FALSE),		/* pcrel_offset */
1750
1751  /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1752  HOWTO (R_PPC64_GOT_DTPREL16_HI,
1753	 16,			/* rightshift */
1754	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1755	 16,			/* bitsize */
1756	 FALSE,			/* pc_relative */
1757	 0,			/* bitpos */
1758	 complain_overflow_dont, /* complain_on_overflow */
1759	 ppc64_elf_unhandled_reloc, /* special_function */
1760	 "R_PPC64_GOT_DTPREL16_HI", /* name */
1761	 FALSE,			/* partial_inplace */
1762	 0,			/* src_mask */
1763	 0xffff,		/* dst_mask */
1764	 FALSE),		/* pcrel_offset */
1765
1766  /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1767  HOWTO (R_PPC64_GOT_DTPREL16_HA,
1768	 16,			/* rightshift */
1769	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1770	 16,			/* bitsize */
1771	 FALSE,			/* pc_relative */
1772	 0,			/* bitpos */
1773	 complain_overflow_dont, /* complain_on_overflow */
1774	 ppc64_elf_unhandled_reloc, /* special_function */
1775	 "R_PPC64_GOT_DTPREL16_HA", /* name */
1776	 FALSE,			/* partial_inplace */
1777	 0,			/* src_mask */
1778	 0xffff,		/* dst_mask */
1779	 FALSE),		/* pcrel_offset */
1780
1781  /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1782     offset to the entry relative to the TOC base (r2).  */
1783  HOWTO (R_PPC64_GOT_TPREL16_DS,
1784	 0,			/* rightshift */
1785	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1786	 16,			/* bitsize */
1787	 FALSE,			/* pc_relative */
1788	 0,			/* bitpos */
1789	 complain_overflow_signed, /* complain_on_overflow */
1790	 ppc64_elf_unhandled_reloc, /* special_function */
1791	 "R_PPC64_GOT_TPREL16_DS", /* name */
1792	 FALSE,			/* partial_inplace */
1793	 0,			/* src_mask */
1794	 0xfffc,		/* dst_mask */
1795	 FALSE),		/* pcrel_offset */
1796
1797  /* Like GOT_TPREL16_DS, but no overflow.  */
1798  HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1799	 0,			/* rightshift */
1800	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1801	 16,			/* bitsize */
1802	 FALSE,			/* pc_relative */
1803	 0,			/* bitpos */
1804	 complain_overflow_dont, /* complain_on_overflow */
1805	 ppc64_elf_unhandled_reloc, /* special_function */
1806	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1807	 FALSE,			/* partial_inplace */
1808	 0,			/* src_mask */
1809	 0xfffc,		/* dst_mask */
1810	 FALSE),		/* pcrel_offset */
1811
1812  /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1813  HOWTO (R_PPC64_GOT_TPREL16_HI,
1814	 16,			/* rightshift */
1815	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1816	 16,			/* bitsize */
1817	 FALSE,			/* pc_relative */
1818	 0,			/* bitpos */
1819	 complain_overflow_dont, /* complain_on_overflow */
1820	 ppc64_elf_unhandled_reloc, /* special_function */
1821	 "R_PPC64_GOT_TPREL16_HI", /* name */
1822	 FALSE,			/* partial_inplace */
1823	 0,			/* src_mask */
1824	 0xffff,		/* dst_mask */
1825	 FALSE),		/* pcrel_offset */
1826
1827  /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1828  HOWTO (R_PPC64_GOT_TPREL16_HA,
1829	 16,			/* rightshift */
1830	 1,			/* size (0 = byte, 1 = short, 2 = long) */
1831	 16,			/* bitsize */
1832	 FALSE,			/* pc_relative */
1833	 0,			/* bitpos */
1834	 complain_overflow_dont, /* complain_on_overflow */
1835	 ppc64_elf_unhandled_reloc, /* special_function */
1836	 "R_PPC64_GOT_TPREL16_HA", /* name */
1837	 FALSE,			/* partial_inplace */
1838	 0,			/* src_mask */
1839	 0xffff,		/* dst_mask */
1840	 FALSE),		/* pcrel_offset */
1841
1842  /* GNU extension to record C++ vtable hierarchy.  */
1843  HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
1844	 0,			/* rightshift */
1845	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1846	 0,			/* bitsize */
1847	 FALSE,			/* pc_relative */
1848	 0,			/* bitpos */
1849	 complain_overflow_dont, /* complain_on_overflow */
1850	 NULL,			/* special_function */
1851	 "R_PPC64_GNU_VTINHERIT", /* name */
1852	 FALSE,			/* partial_inplace */
1853	 0,			/* src_mask */
1854	 0,			/* dst_mask */
1855	 FALSE),		/* pcrel_offset */
1856
1857  /* GNU extension to record C++ vtable member usage.  */
1858  HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
1859	 0,			/* rightshift */
1860	 0,			/* size (0 = byte, 1 = short, 2 = long) */
1861	 0,			/* bitsize */
1862	 FALSE,			/* pc_relative */
1863	 0,			/* bitpos */
1864	 complain_overflow_dont, /* complain_on_overflow */
1865	 NULL,			/* special_function */
1866	 "R_PPC64_GNU_VTENTRY",	/* name */
1867	 FALSE,			/* partial_inplace */
1868	 0,			/* src_mask */
1869	 0,			/* dst_mask */
1870	 FALSE),		/* pcrel_offset */
1871};
1872
1873
1874/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1875   be done.  */
1876
1877static void
1878ppc_howto_init (void)
1879{
1880  unsigned int i, type;
1881
1882  for (i = 0;
1883       i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1884       i++)
1885    {
1886      type = ppc64_elf_howto_raw[i].type;
1887      BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1888			  / sizeof (ppc64_elf_howto_table[0])));
1889      ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1890    }
1891}
1892
1893static reloc_howto_type *
1894ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1895			     bfd_reloc_code_real_type code)
1896{
1897  enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1898
1899  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1900    /* Initialize howto table if needed.  */
1901    ppc_howto_init ();
1902
1903  switch (code)
1904    {
1905    default:
1906      return NULL;
1907
1908    case BFD_RELOC_NONE:			r = R_PPC64_NONE;
1909      break;
1910    case BFD_RELOC_32:				r = R_PPC64_ADDR32;
1911      break;
1912    case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
1913      break;
1914    case BFD_RELOC_16:				r = R_PPC64_ADDR16;
1915      break;
1916    case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
1917      break;
1918    case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
1919      break;
1920    case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
1921      break;
1922    case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
1923      break;
1924    case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
1925      break;
1926    case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
1927      break;
1928    case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
1929      break;
1930    case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
1931      break;
1932    case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
1933      break;
1934    case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
1935      break;
1936    case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
1937      break;
1938    case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
1939      break;
1940    case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
1941      break;
1942    case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
1943      break;
1944    case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
1945      break;
1946    case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
1947      break;
1948    case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
1949      break;
1950    case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
1951      break;
1952    case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
1953      break;
1954    case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
1955      break;
1956    case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
1957      break;
1958    case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
1959      break;
1960    case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
1961      break;
1962    case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
1963      break;
1964    case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
1965      break;
1966    case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
1967      break;
1968    case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
1969      break;
1970    case BFD_RELOC_64:				r = R_PPC64_ADDR64;
1971      break;
1972    case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
1973      break;
1974    case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
1975      break;
1976    case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
1977      break;
1978    case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
1979      break;
1980    case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
1981      break;
1982    case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
1983      break;
1984    case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
1985      break;
1986    case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
1987      break;
1988    case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
1989      break;
1990    case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
1991      break;
1992    case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
1993      break;
1994    case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
1995      break;
1996    case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
1997      break;
1998    case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
1999      break;
2000    case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
2001      break;
2002    case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
2003      break;
2004    case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
2005      break;
2006    case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
2007      break;
2008    case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
2009      break;
2010    case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
2011      break;
2012    case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
2013      break;
2014    case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
2015      break;
2016    case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
2017      break;
2018    case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
2019      break;
2020    case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
2021      break;
2022    case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
2023      break;
2024    case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
2025      break;
2026    case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
2027      break;
2028    case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
2029      break;
2030    case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
2031      break;
2032    case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
2033      break;
2034    case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
2035      break;
2036    case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
2037      break;
2038    case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
2039      break;
2040    case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
2041      break;
2042    case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
2043      break;
2044    case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
2045      break;
2046    case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
2047      break;
2048    case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
2049      break;
2050    case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
2051      break;
2052    case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
2053      break;
2054    case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
2055      break;
2056    case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
2057      break;
2058    case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
2059      break;
2060    case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
2061      break;
2062    case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
2063      break;
2064    case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
2065      break;
2066    case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
2067      break;
2068    case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
2069      break;
2070    case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
2071      break;
2072    case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
2073      break;
2074    case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
2075      break;
2076    case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
2077      break;
2078    case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
2079      break;
2080    case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
2081      break;
2082    case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
2083      break;
2084    case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
2085      break;
2086    case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
2087      break;
2088    case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
2089      break;
2090    case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
2091      break;
2092    case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
2093      break;
2094    case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
2095      break;
2096    case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
2097      break;
2098    case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
2099      break;
2100    case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
2101      break;
2102    case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
2103      break;
2104    case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
2105      break;
2106    case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
2107      break;
2108    case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
2109      break;
2110    }
2111
2112  return ppc64_elf_howto_table[r];
2113};
2114
2115static reloc_howto_type *
2116ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2117			     const char *r_name)
2118{
2119  unsigned int i;
2120
2121  for (i = 0;
2122       i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2123       i++)
2124    if (ppc64_elf_howto_raw[i].name != NULL
2125	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2126      return &ppc64_elf_howto_raw[i];
2127
2128  return NULL;
2129}
2130
2131/* Set the howto pointer for a PowerPC ELF reloc.  */
2132
2133static void
2134ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2135			 Elf_Internal_Rela *dst)
2136{
2137  unsigned int type;
2138
2139  /* Initialize howto table if needed.  */
2140  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2141    ppc_howto_init ();
2142
2143  type = ELF64_R_TYPE (dst->r_info);
2144  if (type >= (sizeof (ppc64_elf_howto_table)
2145	       / sizeof (ppc64_elf_howto_table[0])))
2146    {
2147      (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2148			     abfd, (int) type);
2149      type = R_PPC64_NONE;
2150    }
2151  cache_ptr->howto = ppc64_elf_howto_table[type];
2152}
2153
2154/* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2155
2156static bfd_reloc_status_type
2157ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2158		    void *data, asection *input_section,
2159		    bfd *output_bfd, char **error_message)
2160{
2161  /* If this is a relocatable link (output_bfd test tells us), just
2162     call the generic function.  Any adjustment will be done at final
2163     link time.  */
2164  if (output_bfd != NULL)
2165    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2166				  input_section, output_bfd, error_message);
2167
2168  /* Adjust the addend for sign extension of the low 16 bits.
2169     We won't actually be using the low 16 bits, so trashing them
2170     doesn't matter.  */
2171  reloc_entry->addend += 0x8000;
2172  return bfd_reloc_continue;
2173}
2174
2175static bfd_reloc_status_type
2176ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2177			void *data, asection *input_section,
2178			bfd *output_bfd, char **error_message)
2179{
2180  if (output_bfd != NULL)
2181    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2182				  input_section, output_bfd, error_message);
2183
2184  if (strcmp (symbol->section->name, ".opd") == 0
2185      && (symbol->section->owner->flags & DYNAMIC) == 0)
2186    {
2187      bfd_vma dest = opd_entry_value (symbol->section,
2188				      symbol->value + reloc_entry->addend,
2189				      NULL, NULL);
2190      if (dest != (bfd_vma) -1)
2191	reloc_entry->addend = dest - (symbol->value
2192				      + symbol->section->output_section->vma
2193				      + symbol->section->output_offset);
2194    }
2195  return bfd_reloc_continue;
2196}
2197
2198static bfd_reloc_status_type
2199ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2200			 void *data, asection *input_section,
2201			 bfd *output_bfd, char **error_message)
2202{
2203  long insn;
2204  enum elf_ppc64_reloc_type r_type;
2205  bfd_size_type octets;
2206  /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
2207  bfd_boolean is_power4 = FALSE;
2208
2209  /* If this is a relocatable link (output_bfd test tells us), just
2210     call the generic function.  Any adjustment will be done at final
2211     link time.  */
2212  if (output_bfd != NULL)
2213    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2214				  input_section, output_bfd, error_message);
2215
2216  octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2217  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2218  insn &= ~(0x01 << 21);
2219  r_type = reloc_entry->howto->type;
2220  if (r_type == R_PPC64_ADDR14_BRTAKEN
2221      || r_type == R_PPC64_REL14_BRTAKEN)
2222    insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2223
2224  if (is_power4)
2225    {
2226      /* Set 'a' bit.  This is 0b00010 in BO field for branch
2227	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2228	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
2229      if ((insn & (0x14 << 21)) == (0x04 << 21))
2230	insn |= 0x02 << 21;
2231      else if ((insn & (0x14 << 21)) == (0x10 << 21))
2232	insn |= 0x08 << 21;
2233      else
2234	goto out;
2235    }
2236  else
2237    {
2238      bfd_vma target = 0;
2239      bfd_vma from;
2240
2241      if (!bfd_is_com_section (symbol->section))
2242	target = symbol->value;
2243      target += symbol->section->output_section->vma;
2244      target += symbol->section->output_offset;
2245      target += reloc_entry->addend;
2246
2247      from = (reloc_entry->address
2248	      + input_section->output_offset
2249	      + input_section->output_section->vma);
2250
2251      /* Invert 'y' bit if not the default.  */
2252      if ((bfd_signed_vma) (target - from) < 0)
2253	insn ^= 0x01 << 21;
2254    }
2255  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2256 out:
2257  return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2258				 input_section, output_bfd, error_message);
2259}
2260
2261static bfd_reloc_status_type
2262ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2263			 void *data, asection *input_section,
2264			 bfd *output_bfd, char **error_message)
2265{
2266  /* If this is a relocatable link (output_bfd test tells us), just
2267     call the generic function.  Any adjustment will be done at final
2268     link time.  */
2269  if (output_bfd != NULL)
2270    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2271				  input_section, output_bfd, error_message);
2272
2273  /* Subtract the symbol section base address.  */
2274  reloc_entry->addend -= symbol->section->output_section->vma;
2275  return bfd_reloc_continue;
2276}
2277
2278static bfd_reloc_status_type
2279ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2280			    void *data, asection *input_section,
2281			    bfd *output_bfd, char **error_message)
2282{
2283  /* If this is a relocatable link (output_bfd test tells us), just
2284     call the generic function.  Any adjustment will be done at final
2285     link time.  */
2286  if (output_bfd != NULL)
2287    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2288				  input_section, output_bfd, error_message);
2289
2290  /* Subtract the symbol section base address.  */
2291  reloc_entry->addend -= symbol->section->output_section->vma;
2292
2293  /* Adjust the addend for sign extension of the low 16 bits.  */
2294  reloc_entry->addend += 0x8000;
2295  return bfd_reloc_continue;
2296}
2297
2298static bfd_reloc_status_type
2299ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2300		     void *data, asection *input_section,
2301		     bfd *output_bfd, char **error_message)
2302{
2303  bfd_vma TOCstart;
2304
2305  /* If this is a relocatable link (output_bfd test tells us), just
2306     call the generic function.  Any adjustment will be done at final
2307     link time.  */
2308  if (output_bfd != NULL)
2309    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2310				  input_section, output_bfd, error_message);
2311
2312  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2313  if (TOCstart == 0)
2314    TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2315
2316  /* Subtract the TOC base address.  */
2317  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2318  return bfd_reloc_continue;
2319}
2320
2321static bfd_reloc_status_type
2322ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2323			void *data, asection *input_section,
2324			bfd *output_bfd, char **error_message)
2325{
2326  bfd_vma TOCstart;
2327
2328  /* If this is a relocatable link (output_bfd test tells us), just
2329     call the generic function.  Any adjustment will be done at final
2330     link time.  */
2331  if (output_bfd != NULL)
2332    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2333				  input_section, output_bfd, error_message);
2334
2335  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2336  if (TOCstart == 0)
2337    TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2338
2339  /* Subtract the TOC base address.  */
2340  reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2341
2342  /* Adjust the addend for sign extension of the low 16 bits.  */
2343  reloc_entry->addend += 0x8000;
2344  return bfd_reloc_continue;
2345}
2346
2347static bfd_reloc_status_type
2348ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2349		       void *data, asection *input_section,
2350		       bfd *output_bfd, char **error_message)
2351{
2352  bfd_vma TOCstart;
2353  bfd_size_type octets;
2354
2355  /* If this is a relocatable link (output_bfd test tells us), just
2356     call the generic function.  Any adjustment will be done at final
2357     link time.  */
2358  if (output_bfd != NULL)
2359    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2360				  input_section, output_bfd, error_message);
2361
2362  TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2363  if (TOCstart == 0)
2364    TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2365
2366  octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2367  bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2368  return bfd_reloc_ok;
2369}
2370
2371static bfd_reloc_status_type
2372ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2373			   void *data, asection *input_section,
2374			   bfd *output_bfd, char **error_message)
2375{
2376  /* If this is a relocatable link (output_bfd test tells us), just
2377     call the generic function.  Any adjustment will be done at final
2378     link time.  */
2379  if (output_bfd != NULL)
2380    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2381				  input_section, output_bfd, error_message);
2382
2383  if (error_message != NULL)
2384    {
2385      static char buf[60];
2386      sprintf (buf, "generic linker can't handle %s",
2387	       reloc_entry->howto->name);
2388      *error_message = buf;
2389    }
2390  return bfd_reloc_dangerous;
2391}
2392
2393struct ppc64_elf_obj_tdata
2394{
2395  struct elf_obj_tdata elf;
2396
2397  /* Shortcuts to dynamic linker sections.  */
2398  asection *got;
2399  asection *relgot;
2400
2401  /* Used during garbage collection.  We attach global symbols defined
2402     on removed .opd entries to this section so that the sym is removed.  */
2403  asection *deleted_section;
2404
2405  /* TLS local dynamic got entry handling.  Suppose for multiple GOT
2406     sections means we potentially need one of these for each input bfd.  */
2407  union {
2408    bfd_signed_vma refcount;
2409    bfd_vma offset;
2410  } tlsld_got;
2411
2412  /* A copy of relocs before they are modified for --emit-relocs.  */
2413  Elf_Internal_Rela *opd_relocs;
2414};
2415
2416#define ppc64_elf_tdata(bfd) \
2417  ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2418
2419#define ppc64_tlsld_got(bfd) \
2420  (&ppc64_elf_tdata (bfd)->tlsld_got)
2421
2422/* Override the generic function because we store some extras.  */
2423
2424static bfd_boolean
2425ppc64_elf_mkobject (bfd *abfd)
2426{
2427  if (abfd->tdata.any == NULL)
2428    {
2429      bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2430      abfd->tdata.any = bfd_zalloc (abfd, amt);
2431      if (abfd->tdata.any == NULL)
2432	return FALSE;
2433    }
2434  return bfd_elf_mkobject (abfd);
2435}
2436
2437/* Return 1 if target is one of ours.  */
2438
2439static bfd_boolean
2440is_ppc64_elf_target (const struct bfd_target *targ)
2441{
2442  extern const bfd_target bfd_elf64_powerpc_vec;
2443  extern const bfd_target bfd_elf64_powerpcle_vec;
2444
2445  return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2446}
2447
2448/* Fix bad default arch selected for a 64 bit input bfd when the
2449   default is 32 bit.  */
2450
2451static bfd_boolean
2452ppc64_elf_object_p (bfd *abfd)
2453{
2454  if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2455    {
2456      Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2457
2458      if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2459	{
2460	  /* Relies on arch after 32 bit default being 64 bit default.  */
2461	  abfd->arch_info = abfd->arch_info->next;
2462	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2463	}
2464    }
2465  return TRUE;
2466}
2467
2468/* Support for core dump NOTE sections.  */
2469
2470static bfd_boolean
2471ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2472{
2473  size_t offset, size;
2474
2475  if (note->descsz != 504)
2476    return FALSE;
2477
2478  /* pr_cursig */
2479  elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2480
2481  /* pr_pid */
2482  elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2483
2484  /* pr_reg */
2485  offset = 112;
2486  size = 384;
2487
2488  /* Make a ".reg/999" section.  */
2489  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2490					  size, note->descpos + offset);
2491}
2492
2493static bfd_boolean
2494ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2495{
2496  if (note->descsz != 136)
2497    return FALSE;
2498
2499  elf_tdata (abfd)->core_program
2500    = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2501  elf_tdata (abfd)->core_command
2502    = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2503
2504  return TRUE;
2505}
2506
2507static char *
2508ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2509			   ...)
2510{
2511  switch (note_type)
2512    {
2513    default:
2514      return NULL;
2515
2516    case NT_PRPSINFO:
2517      {
2518	char data[136];
2519	va_list ap;
2520
2521	va_start (ap, note_type);
2522	memset (data, 0, 40);
2523	strncpy (data + 40, va_arg (ap, const char *), 16);
2524	strncpy (data + 56, va_arg (ap, const char *), 80);
2525	va_end (ap);
2526	return elfcore_write_note (abfd, buf, bufsiz,
2527				   "CORE", note_type, data, sizeof (data));
2528      }
2529
2530    case NT_PRSTATUS:
2531      {
2532	char data[504];
2533	va_list ap;
2534	long pid;
2535	int cursig;
2536	const void *greg;
2537
2538	va_start (ap, note_type);
2539	memset (data, 0, 112);
2540	pid = va_arg (ap, long);
2541	bfd_put_32 (abfd, pid, data + 32);
2542	cursig = va_arg (ap, int);
2543	bfd_put_16 (abfd, cursig, data + 12);
2544	greg = va_arg (ap, const void *);
2545	memcpy (data + 112, greg, 384);
2546	memset (data + 496, 0, 8);
2547	va_end (ap);
2548	return elfcore_write_note (abfd, buf, bufsiz,
2549				   "CORE", note_type, data, sizeof (data));
2550      }
2551    }
2552}
2553
2554/* Merge backend specific data from an object file to the output
2555   object file when linking.  */
2556
2557static bfd_boolean
2558ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2559{
2560  /* Check if we have the same endianess.  */
2561  if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2562      && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2563      && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2564    {
2565      const char *msg;
2566
2567      if (bfd_big_endian (ibfd))
2568	msg = _("%B: compiled for a big endian system "
2569		"and target is little endian");
2570      else
2571	msg = _("%B: compiled for a little endian system "
2572		"and target is big endian");
2573
2574      (*_bfd_error_handler) (msg, ibfd);
2575
2576      bfd_set_error (bfd_error_wrong_format);
2577      return FALSE;
2578    }
2579
2580  return TRUE;
2581}
2582
2583/* Add extra PPC sections.  */
2584
2585static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2586{
2587  { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
2588  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2589  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2590  { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2591  { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2592  { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2593  { NULL,                     0,  0, 0,            0 }
2594};
2595
2596enum _ppc64_sec_type {
2597  sec_normal = 0,
2598  sec_opd = 1,
2599  sec_toc = 2
2600};
2601
2602struct _ppc64_elf_section_data
2603{
2604  struct bfd_elf_section_data elf;
2605
2606  /* An array with one entry for each opd function descriptor.  */
2607  union
2608  {
2609    /* Points to the function code section for local opd entries.  */
2610    asection **opd_func_sec;
2611    /* After editing .opd, adjust references to opd local syms.  */
2612    long *opd_adjust;
2613
2614    /* An array for toc sections, indexed by offset/8.
2615       Specifies the relocation symbol index used at a given toc offset.  */
2616    unsigned *t_symndx;
2617  } u;
2618
2619  enum _ppc64_sec_type sec_type:2;
2620
2621  /* Flag set when small branches are detected.  Used to
2622     select suitable defaults for the stub group size.  */
2623  unsigned int has_14bit_branch:1;
2624};
2625
2626#define ppc64_elf_section_data(sec) \
2627  ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2628
2629static bfd_boolean
2630ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2631{
2632  if (!sec->used_by_bfd)
2633    {
2634      struct _ppc64_elf_section_data *sdata;
2635      bfd_size_type amt = sizeof (*sdata);
2636
2637      sdata = bfd_zalloc (abfd, amt);
2638      if (sdata == NULL)
2639	return FALSE;
2640      sec->used_by_bfd = sdata;
2641    }
2642
2643  return _bfd_elf_new_section_hook (abfd, sec);
2644}
2645
2646static void *
2647get_opd_info (asection * sec)
2648{
2649  if (sec != NULL
2650      && ppc64_elf_section_data (sec) != NULL
2651      && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2652    return ppc64_elf_section_data (sec)->u.opd_adjust;
2653  return NULL;
2654}
2655
2656/* Parameters for the qsort hook.  */
2657static asection *synthetic_opd;
2658static bfd_boolean synthetic_relocatable;
2659
2660/* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2661
2662static int
2663compare_symbols (const void *ap, const void *bp)
2664{
2665  const asymbol *a = * (const asymbol **) ap;
2666  const asymbol *b = * (const asymbol **) bp;
2667
2668  /* Section symbols first.  */
2669  if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2670    return -1;
2671  if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2672    return 1;
2673
2674  /* then .opd symbols.  */
2675  if (a->section == synthetic_opd && b->section != synthetic_opd)
2676    return -1;
2677  if (a->section != synthetic_opd && b->section == synthetic_opd)
2678    return 1;
2679
2680  /* then other code symbols.  */
2681  if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2682      == (SEC_CODE | SEC_ALLOC)
2683      && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2684	 != (SEC_CODE | SEC_ALLOC))
2685    return -1;
2686
2687  if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2688      != (SEC_CODE | SEC_ALLOC)
2689      && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2690	 == (SEC_CODE | SEC_ALLOC))
2691    return 1;
2692
2693  if (synthetic_relocatable)
2694    {
2695      if (a->section->id < b->section->id)
2696	return -1;
2697
2698      if (a->section->id > b->section->id)
2699	return 1;
2700    }
2701
2702  if (a->value + a->section->vma < b->value + b->section->vma)
2703    return -1;
2704
2705  if (a->value + a->section->vma > b->value + b->section->vma)
2706    return 1;
2707
2708  /* For syms with the same value, prefer strong dynamic global function
2709     syms over other syms.  */
2710  if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2711    return -1;
2712
2713  if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2714    return 1;
2715
2716  if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2717    return -1;
2718
2719  if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2720    return 1;
2721
2722  if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2723    return -1;
2724
2725  if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2726    return 1;
2727
2728  if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2729    return -1;
2730
2731  if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2732    return 1;
2733
2734  return 0;
2735}
2736
2737/* Search SYMS for a symbol of the given VALUE.  */
2738
2739static asymbol *
2740sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2741{
2742  long mid;
2743
2744  if (id == -1)
2745    {
2746      while (lo < hi)
2747	{
2748	  mid = (lo + hi) >> 1;
2749	  if (syms[mid]->value + syms[mid]->section->vma < value)
2750	    lo = mid + 1;
2751	  else if (syms[mid]->value + syms[mid]->section->vma > value)
2752	    hi = mid;
2753	  else
2754	    return syms[mid];
2755	}
2756    }
2757  else
2758    {
2759      while (lo < hi)
2760	{
2761	  mid = (lo + hi) >> 1;
2762	  if (syms[mid]->section->id < id)
2763	    lo = mid + 1;
2764	  else if (syms[mid]->section->id > id)
2765	    hi = mid;
2766	  else if (syms[mid]->value < value)
2767	    lo = mid + 1;
2768	  else if (syms[mid]->value > value)
2769	    hi = mid;
2770	  else
2771	    return syms[mid];
2772	}
2773    }
2774  return NULL;
2775}
2776
2777/* Create synthetic symbols, effectively restoring "dot-symbol" function
2778   entry syms.  */
2779
2780static long
2781ppc64_elf_get_synthetic_symtab (bfd *abfd,
2782				long static_count, asymbol **static_syms,
2783				long dyn_count, asymbol **dyn_syms,
2784				asymbol **ret)
2785{
2786  asymbol *s;
2787  long i;
2788  long count;
2789  char *names;
2790  long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2791  asection *opd;
2792  bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2793  asymbol **syms;
2794
2795  *ret = NULL;
2796
2797  opd = bfd_get_section_by_name (abfd, ".opd");
2798  if (opd == NULL)
2799    return 0;
2800
2801  symcount = static_count;
2802  if (!relocatable)
2803    symcount += dyn_count;
2804  if (symcount == 0)
2805    return 0;
2806
2807  syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2808  if (syms == NULL)
2809    return -1;
2810
2811  if (!relocatable && static_count != 0 && dyn_count != 0)
2812    {
2813      /* Use both symbol tables.  */
2814      memcpy (syms, static_syms, static_count * sizeof (*syms));
2815      memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2816    }
2817  else if (!relocatable && static_count == 0)
2818    memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2819  else
2820    memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2821
2822  synthetic_opd = opd;
2823  synthetic_relocatable = relocatable;
2824  qsort (syms, symcount, sizeof (*syms), compare_symbols);
2825
2826  if (!relocatable && symcount > 1)
2827    {
2828      long j;
2829      /* Trim duplicate syms, since we may have merged the normal and
2830	 dynamic symbols.  Actually, we only care about syms that have
2831	 different values, so trim any with the same value.  */
2832      for (i = 1, j = 1; i < symcount; ++i)
2833	if (syms[i - 1]->value + syms[i - 1]->section->vma
2834	    != syms[i]->value + syms[i]->section->vma)
2835	  syms[j++] = syms[i];
2836      symcount = j;
2837    }
2838
2839  i = 0;
2840  if (syms[i]->section == opd)
2841    ++i;
2842  codesecsym = i;
2843
2844  for (; i < symcount; ++i)
2845    if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2846	 != (SEC_CODE | SEC_ALLOC))
2847	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
2848      break;
2849  codesecsymend = i;
2850
2851  for (; i < symcount; ++i)
2852    if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2853      break;
2854  secsymend = i;
2855
2856  for (; i < symcount; ++i)
2857    if (syms[i]->section != opd)
2858      break;
2859  opdsymend = i;
2860
2861  for (; i < symcount; ++i)
2862    if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2863	!= (SEC_CODE | SEC_ALLOC))
2864      break;
2865  symcount = i;
2866
2867  count = 0;
2868  if (opdsymend == secsymend)
2869    goto done;
2870
2871  if (relocatable)
2872    {
2873      bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2874      arelent *r;
2875      size_t size;
2876      long relcount;
2877
2878      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2879      relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2880      if (relcount == 0)
2881	goto done;
2882
2883      if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2884	{
2885	  count = -1;
2886	  goto done;
2887	}
2888
2889      size = 0;
2890      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2891	{
2892	  asymbol *sym;
2893
2894	  while (r < opd->relocation + relcount
2895		 && r->address < syms[i]->value + opd->vma)
2896	    ++r;
2897
2898	  if (r == opd->relocation + relcount)
2899	    break;
2900
2901	  if (r->address != syms[i]->value + opd->vma)
2902	    continue;
2903
2904	  if (r->howto->type != R_PPC64_ADDR64)
2905	    continue;
2906
2907	  sym = *r->sym_ptr_ptr;
2908	  if (!sym_exists_at (syms, opdsymend, symcount,
2909			      sym->section->id, sym->value + r->addend))
2910	    {
2911	      ++count;
2912	      size += sizeof (asymbol);
2913	      size += strlen (syms[i]->name) + 2;
2914	    }
2915	}
2916
2917      s = *ret = bfd_malloc (size);
2918      if (s == NULL)
2919	{
2920	  count = -1;
2921	  goto done;
2922	}
2923
2924      names = (char *) (s + count);
2925
2926      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2927	{
2928	  asymbol *sym;
2929
2930	  while (r < opd->relocation + relcount
2931		 && r->address < syms[i]->value + opd->vma)
2932	    ++r;
2933
2934	  if (r == opd->relocation + relcount)
2935	    break;
2936
2937	  if (r->address != syms[i]->value + opd->vma)
2938	    continue;
2939
2940	  if (r->howto->type != R_PPC64_ADDR64)
2941	    continue;
2942
2943	  sym = *r->sym_ptr_ptr;
2944	  if (!sym_exists_at (syms, opdsymend, symcount,
2945			      sym->section->id, sym->value + r->addend))
2946	    {
2947	      size_t len;
2948
2949	      *s = *syms[i];
2950	      s->section = sym->section;
2951	      s->value = sym->value + r->addend;
2952	      s->name = names;
2953	      *names++ = '.';
2954	      len = strlen (syms[i]->name);
2955	      memcpy (names, syms[i]->name, len + 1);
2956	      names += len + 1;
2957	      s++;
2958	    }
2959	}
2960    }
2961  else
2962    {
2963      bfd_byte *contents;
2964      size_t size;
2965
2966      if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2967	{
2968	  if (contents)
2969	    {
2970	    free_contents_and_exit:
2971	      free (contents);
2972	    }
2973	  count = -1;
2974	  goto done;
2975	}
2976
2977      size = 0;
2978      for (i = secsymend; i < opdsymend; ++i)
2979	{
2980	  bfd_vma ent;
2981
2982	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
2983	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2984	    {
2985	      ++count;
2986	      size += sizeof (asymbol);
2987	      size += strlen (syms[i]->name) + 2;
2988	    }
2989	}
2990
2991      s = *ret = bfd_malloc (size);
2992      if (s == NULL)
2993	goto free_contents_and_exit;
2994
2995      names = (char *) (s + count);
2996
2997      for (i = secsymend; i < opdsymend; ++i)
2998	{
2999	  bfd_vma ent;
3000
3001	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
3002	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3003	    {
3004	      long lo, hi;
3005	      size_t len;
3006	      asection *sec = abfd->sections;
3007
3008	      *s = *syms[i];
3009	      lo = codesecsym;
3010	      hi = codesecsymend;
3011	      while (lo < hi)
3012		{
3013		  long mid = (lo + hi) >> 1;
3014		  if (syms[mid]->section->vma < ent)
3015		    lo = mid + 1;
3016		  else if (syms[mid]->section->vma > ent)
3017		    hi = mid;
3018		  else
3019		    {
3020		      sec = syms[mid]->section;
3021		      break;
3022		    }
3023		}
3024
3025	      if (lo >= hi && lo > codesecsym)
3026		sec = syms[lo - 1]->section;
3027
3028	      for (; sec != NULL; sec = sec->next)
3029		{
3030		  if (sec->vma > ent)
3031		    break;
3032		  if ((sec->flags & SEC_ALLOC) == 0
3033		      || (sec->flags & SEC_LOAD) == 0)
3034		    break;
3035		  if ((sec->flags & SEC_CODE) != 0)
3036		    s->section = sec;
3037		}
3038	      s->value = ent - s->section->vma;
3039	      s->name = names;
3040	      *names++ = '.';
3041	      len = strlen (syms[i]->name);
3042	      memcpy (names, syms[i]->name, len + 1);
3043	      names += len + 1;
3044	      s++;
3045	    }
3046	}
3047      free (contents);
3048    }
3049
3050 done:
3051  free (syms);
3052  return count;
3053}
3054
3055/* The following functions are specific to the ELF linker, while
3056   functions above are used generally.  Those named ppc64_elf_* are
3057   called by the main ELF linker code.  They appear in this file more
3058   or less in the order in which they are called.  eg.
3059   ppc64_elf_check_relocs is called early in the link process,
3060   ppc64_elf_finish_dynamic_sections is one of the last functions
3061   called.
3062
3063   PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3064   functions have both a function code symbol and a function descriptor
3065   symbol.  A call to foo in a relocatable object file looks like:
3066
3067   .		.text
3068   .	x:
3069   .		bl	.foo
3070   .		nop
3071
3072   The function definition in another object file might be:
3073
3074   .		.section .opd
3075   .	foo:	.quad	.foo
3076   .		.quad	.TOC.@tocbase
3077   .		.quad	0
3078   .
3079   .		.text
3080   .	.foo:	blr
3081
3082   When the linker resolves the call during a static link, the branch
3083   unsurprisingly just goes to .foo and the .opd information is unused.
3084   If the function definition is in a shared library, things are a little
3085   different:  The call goes via a plt call stub, the opd information gets
3086   copied to the plt, and the linker patches the nop.
3087
3088   .	x:
3089   .		bl	.foo_stub
3090   .		ld	2,40(1)
3091   .
3092   .
3093   .	.foo_stub:
3094   .		addis	12,2,Lfoo@toc@ha	# in practice, the call stub
3095   .		addi	12,12,Lfoo@toc@l	# is slightly optimized, but
3096   .		std	2,40(1)			# this is the general idea
3097   .		ld	11,0(12)
3098   .		ld	2,8(12)
3099   .		mtctr	11
3100   .		ld	11,16(12)
3101   .		bctr
3102   .
3103   .		.section .plt
3104   .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
3105
3106   The "reloc ()" notation is supposed to indicate that the linker emits
3107   an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3108   copying.
3109
3110   What are the difficulties here?  Well, firstly, the relocations
3111   examined by the linker in check_relocs are against the function code
3112   sym .foo, while the dynamic relocation in the plt is emitted against
3113   the function descriptor symbol, foo.  Somewhere along the line, we need
3114   to carefully copy dynamic link information from one symbol to the other.
3115   Secondly, the generic part of the elf linker will make .foo a dynamic
3116   symbol as is normal for most other backends.  We need foo dynamic
3117   instead, at least for an application final link.  However, when
3118   creating a shared library containing foo, we need to have both symbols
3119   dynamic so that references to .foo are satisfied during the early
3120   stages of linking.  Otherwise the linker might decide to pull in a
3121   definition from some other object, eg. a static library.
3122
3123   Update: As of August 2004, we support a new convention.  Function
3124   calls may use the function descriptor symbol, ie. "bl foo".  This
3125   behaves exactly as "bl .foo".  */
3126
3127/* The linker needs to keep track of the number of relocs that it
3128   decides to copy as dynamic relocs in check_relocs for each symbol.
3129   This is so that it can later discard them if they are found to be
3130   unnecessary.  We store the information in a field extending the
3131   regular ELF linker hash table.  */
3132
3133struct ppc_dyn_relocs
3134{
3135  struct ppc_dyn_relocs *next;
3136
3137  /* The input section of the reloc.  */
3138  asection *sec;
3139
3140  /* Total number of relocs copied for the input section.  */
3141  bfd_size_type count;
3142
3143  /* Number of pc-relative relocs copied for the input section.  */
3144  bfd_size_type pc_count;
3145};
3146
3147/* Track GOT entries needed for a given symbol.  We might need more
3148   than one got entry per symbol.  */
3149struct got_entry
3150{
3151  struct got_entry *next;
3152
3153  /* The symbol addend that we'll be placing in the GOT.  */
3154  bfd_vma addend;
3155
3156  /* Unlike other ELF targets, we use separate GOT entries for the same
3157     symbol referenced from different input files.  This is to support
3158     automatic multiple TOC/GOT sections, where the TOC base can vary
3159     from one input file to another.
3160
3161     Point to the BFD owning this GOT entry.  */
3162  bfd *owner;
3163
3164  /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3165     TLS_TPREL or TLS_DTPREL for tls entries.  */
3166  char tls_type;
3167
3168  /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
3169  union
3170    {
3171      bfd_signed_vma refcount;
3172      bfd_vma offset;
3173    } got;
3174};
3175
3176/* The same for PLT.  */
3177struct plt_entry
3178{
3179  struct plt_entry *next;
3180
3181  bfd_vma addend;
3182
3183  union
3184    {
3185      bfd_signed_vma refcount;
3186      bfd_vma offset;
3187    } plt;
3188};
3189
3190/* Of those relocs that might be copied as dynamic relocs, this macro
3191   selects those that must be copied when linking a shared library,
3192   even when the symbol is local.  */
3193
3194#define MUST_BE_DYN_RELOC(RTYPE)		\
3195  ((RTYPE) != R_PPC64_REL32			\
3196   && (RTYPE) != R_PPC64_REL64			\
3197   && (RTYPE) != R_PPC64_REL30)
3198
3199/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3200   copying dynamic variables from a shared lib into an app's dynbss
3201   section, and instead use a dynamic relocation to point into the
3202   shared lib.  With code that gcc generates, it's vital that this be
3203   enabled;  In the PowerPC64 ABI, the address of a function is actually
3204   the address of a function descriptor, which resides in the .opd
3205   section.  gcc uses the descriptor directly rather than going via the
3206   GOT as some other ABI's do, which means that initialized function
3207   pointers must reference the descriptor.  Thus, a function pointer
3208   initialized to the address of a function in a shared library will
3209   either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3210   redefines the function descriptor symbol to point to the copy.  This
3211   presents a problem as a plt entry for that function is also
3212   initialized from the function descriptor symbol and the copy reloc
3213   may not be initialized first.  */
3214#define ELIMINATE_COPY_RELOCS 1
3215
3216/* Section name for stubs is the associated section name plus this
3217   string.  */
3218#define STUB_SUFFIX ".stub"
3219
3220/* Linker stubs.
3221   ppc_stub_long_branch:
3222   Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3223   destination, but a 24 bit branch in a stub section will reach.
3224   .	b	dest
3225
3226   ppc_stub_plt_branch:
3227   Similar to the above, but a 24 bit branch in the stub section won't
3228   reach its destination.
3229   .	addis	%r12,%r2,xxx@toc@ha
3230   .	ld	%r11,xxx@toc@l(%r12)
3231   .	mtctr	%r11
3232   .	bctr
3233
3234   ppc_stub_plt_call:
3235   Used to call a function in a shared library.  If it so happens that
3236   the plt entry referenced crosses a 64k boundary, then an extra
3237   "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3238   xxx+16 as appropriate.
3239   .	addis	%r12,%r2,xxx@toc@ha
3240   .	std	%r2,40(%r1)
3241   .	ld	%r11,xxx+0@toc@l(%r12)
3242   .	ld	%r2,xxx+8@toc@l(%r12)
3243   .	mtctr	%r11
3244   .	ld	%r11,xxx+16@toc@l(%r12)
3245   .	bctr
3246
3247   ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3248   code to adjust the value and save r2 to support multiple toc sections.
3249   A ppc_stub_long_branch with an r2 offset looks like:
3250   .	std	%r2,40(%r1)
3251   .	addis	%r2,%r2,off@ha
3252   .	addi	%r2,%r2,off@l
3253   .	b	dest
3254
3255   A ppc_stub_plt_branch with an r2 offset looks like:
3256   .	std	%r2,40(%r1)
3257   .	addis	%r12,%r2,xxx@toc@ha
3258   .	ld	%r11,xxx@toc@l(%r12)
3259   .	addis	%r2,%r2,off@ha
3260   .	addi	%r2,%r2,off@l
3261   .	mtctr	%r11
3262   .	bctr
3263*/
3264
3265enum ppc_stub_type {
3266  ppc_stub_none,
3267  ppc_stub_long_branch,
3268  ppc_stub_long_branch_r2off,
3269  ppc_stub_plt_branch,
3270  ppc_stub_plt_branch_r2off,
3271  ppc_stub_plt_call
3272};
3273
3274struct ppc_stub_hash_entry {
3275
3276  /* Base hash table entry structure.  */
3277  struct bfd_hash_entry root;
3278
3279  enum ppc_stub_type stub_type;
3280
3281  /* The stub section.  */
3282  asection *stub_sec;
3283
3284  /* Offset within stub_sec of the beginning of this stub.  */
3285  bfd_vma stub_offset;
3286
3287  /* Given the symbol's value and its section we can determine its final
3288     value when building the stubs (so the stub knows where to jump.  */
3289  bfd_vma target_value;
3290  asection *target_section;
3291
3292  /* The symbol table entry, if any, that this was derived from.  */
3293  struct ppc_link_hash_entry *h;
3294
3295  /* And the reloc addend that this was derived from.  */
3296  bfd_vma addend;
3297
3298  /* Where this stub is being called from, or, in the case of combined
3299     stub sections, the first input section in the group.  */
3300  asection *id_sec;
3301};
3302
3303struct ppc_branch_hash_entry {
3304
3305  /* Base hash table entry structure.  */
3306  struct bfd_hash_entry root;
3307
3308  /* Offset within branch lookup table.  */
3309  unsigned int offset;
3310
3311  /* Generation marker.  */
3312  unsigned int iter;
3313};
3314
3315struct ppc_link_hash_entry
3316{
3317  struct elf_link_hash_entry elf;
3318
3319  union {
3320    /* A pointer to the most recently used stub hash entry against this
3321       symbol.  */
3322    struct ppc_stub_hash_entry *stub_cache;
3323
3324    /* A pointer to the next symbol starting with a '.'  */
3325    struct ppc_link_hash_entry *next_dot_sym;
3326  } u;
3327
3328  /* Track dynamic relocs copied for this symbol.  */
3329  struct ppc_dyn_relocs *dyn_relocs;
3330
3331  /* Link between function code and descriptor symbols.  */
3332  struct ppc_link_hash_entry *oh;
3333
3334  /* Flag function code and descriptor symbols.  */
3335  unsigned int is_func:1;
3336  unsigned int is_func_descriptor:1;
3337  unsigned int fake:1;
3338
3339  /* Whether global opd/toc sym has been adjusted or not.
3340     After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3341     should be set for all globals defined in any opd/toc section.  */
3342  unsigned int adjust_done:1;
3343
3344  /* Set if we twiddled this symbol to weak at some stage.  */
3345  unsigned int was_undefined:1;
3346
3347  /* Contexts in which symbol is used in the GOT (or TOC).
3348     TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3349     corresponding relocs are encountered during check_relocs.
3350     tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3351     indicate the corresponding GOT entry type is not needed.
3352     tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3353     a TPREL one.  We use a separate flag rather than setting TPREL
3354     just for convenience in distinguishing the two cases.  */
3355#define TLS_GD		 1	/* GD reloc. */
3356#define TLS_LD		 2	/* LD reloc. */
3357#define TLS_TPREL	 4	/* TPREL reloc, => IE. */
3358#define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
3359#define TLS_TLS		16	/* Any TLS reloc.  */
3360#define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
3361#define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
3362  char tls_mask;
3363};
3364
3365/* ppc64 ELF linker hash table.  */
3366
3367struct ppc_link_hash_table
3368{
3369  struct elf_link_hash_table elf;
3370
3371  /* The stub hash table.  */
3372  struct bfd_hash_table stub_hash_table;
3373
3374  /* Another hash table for plt_branch stubs.  */
3375  struct bfd_hash_table branch_hash_table;
3376
3377  /* Linker stub bfd.  */
3378  bfd *stub_bfd;
3379
3380  /* Linker call-backs.  */
3381  asection * (*add_stub_section) (const char *, asection *);
3382  void (*layout_sections_again) (void);
3383
3384  /* Array to keep track of which stub sections have been created, and
3385     information on stub grouping.  */
3386  struct map_stub {
3387    /* This is the section to which stubs in the group will be attached.  */
3388    asection *link_sec;
3389    /* The stub section.  */
3390    asection *stub_sec;
3391    /* Along with elf_gp, specifies the TOC pointer used in this group.  */
3392    bfd_vma toc_off;
3393  } *stub_group;
3394
3395  /* Temp used when calculating TOC pointers.  */
3396  bfd_vma toc_curr;
3397
3398  /* Highest input section id.  */
3399  int top_id;
3400
3401  /* Highest output section index.  */
3402  int top_index;
3403
3404  /* Used when adding symbols.  */
3405  struct ppc_link_hash_entry *dot_syms;
3406
3407  /* List of input sections for each output section.  */
3408  asection **input_list;
3409
3410  /* Short-cuts to get to dynamic linker sections.  */
3411  asection *got;
3412  asection *plt;
3413  asection *relplt;
3414  asection *dynbss;
3415  asection *relbss;
3416  asection *glink;
3417  asection *sfpr;
3418  asection *brlt;
3419  asection *relbrlt;
3420
3421  /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3422  struct ppc_link_hash_entry *tls_get_addr;
3423  struct ppc_link_hash_entry *tls_get_addr_fd;
3424
3425  /* Statistics.  */
3426  unsigned long stub_count[ppc_stub_plt_call];
3427
3428  /* Number of stubs against global syms.  */
3429  unsigned long stub_globals;
3430
3431  /* Set if we should emit symbols for stubs.  */
3432  unsigned int emit_stub_syms:1;
3433
3434  /* Support for multiple toc sections.  */
3435  unsigned int no_multi_toc:1;
3436  unsigned int multi_toc_needed:1;
3437
3438  /* Set on error.  */
3439  unsigned int stub_error:1;
3440
3441  /* Temp used by ppc64_elf_check_directives.  */
3442  unsigned int twiddled_syms:1;
3443
3444  /* Incremented every time we size stubs.  */
3445  unsigned int stub_iteration;
3446
3447  /* Small local sym to section mapping cache.  */
3448  struct sym_sec_cache sym_sec;
3449};
3450
3451/* Rename some of the generic section flags to better document how they
3452   are used here.  */
3453#define has_toc_reloc has_gp_reloc
3454#define makes_toc_func_call need_finalize_relax
3455#define call_check_in_progress reloc_done
3456
3457/* Get the ppc64 ELF linker hash table from a link_info structure.  */
3458
3459#define ppc_hash_table(p) \
3460  ((struct ppc_link_hash_table *) ((p)->hash))
3461
3462#define ppc_stub_hash_lookup(table, string, create, copy) \
3463  ((struct ppc_stub_hash_entry *) \
3464   bfd_hash_lookup ((table), (string), (create), (copy)))
3465
3466#define ppc_branch_hash_lookup(table, string, create, copy) \
3467  ((struct ppc_branch_hash_entry *) \
3468   bfd_hash_lookup ((table), (string), (create), (copy)))
3469
3470/* Create an entry in the stub hash table.  */
3471
3472static struct bfd_hash_entry *
3473stub_hash_newfunc (struct bfd_hash_entry *entry,
3474		   struct bfd_hash_table *table,
3475		   const char *string)
3476{
3477  /* Allocate the structure if it has not already been allocated by a
3478     subclass.  */
3479  if (entry == NULL)
3480    {
3481      entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3482      if (entry == NULL)
3483	return entry;
3484    }
3485
3486  /* Call the allocation method of the superclass.  */
3487  entry = bfd_hash_newfunc (entry, table, string);
3488  if (entry != NULL)
3489    {
3490      struct ppc_stub_hash_entry *eh;
3491
3492      /* Initialize the local fields.  */
3493      eh = (struct ppc_stub_hash_entry *) entry;
3494      eh->stub_type = ppc_stub_none;
3495      eh->stub_sec = NULL;
3496      eh->stub_offset = 0;
3497      eh->target_value = 0;
3498      eh->target_section = NULL;
3499      eh->h = NULL;
3500      eh->id_sec = NULL;
3501    }
3502
3503  return entry;
3504}
3505
3506/* Create an entry in the branch hash table.  */
3507
3508static struct bfd_hash_entry *
3509branch_hash_newfunc (struct bfd_hash_entry *entry,
3510		     struct bfd_hash_table *table,
3511		     const char *string)
3512{
3513  /* Allocate the structure if it has not already been allocated by a
3514     subclass.  */
3515  if (entry == NULL)
3516    {
3517      entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3518      if (entry == NULL)
3519	return entry;
3520    }
3521
3522  /* Call the allocation method of the superclass.  */
3523  entry = bfd_hash_newfunc (entry, table, string);
3524  if (entry != NULL)
3525    {
3526      struct ppc_branch_hash_entry *eh;
3527
3528      /* Initialize the local fields.  */
3529      eh = (struct ppc_branch_hash_entry *) entry;
3530      eh->offset = 0;
3531      eh->iter = 0;
3532    }
3533
3534  return entry;
3535}
3536
3537/* Create an entry in a ppc64 ELF linker hash table.  */
3538
3539static struct bfd_hash_entry *
3540link_hash_newfunc (struct bfd_hash_entry *entry,
3541		   struct bfd_hash_table *table,
3542		   const char *string)
3543{
3544  /* Allocate the structure if it has not already been allocated by a
3545     subclass.  */
3546  if (entry == NULL)
3547    {
3548      entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3549      if (entry == NULL)
3550	return entry;
3551    }
3552
3553  /* Call the allocation method of the superclass.  */
3554  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3555  if (entry != NULL)
3556    {
3557      struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3558
3559      memset (&eh->u.stub_cache, 0,
3560	      (sizeof (struct ppc_link_hash_entry)
3561	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3562
3563      /* When making function calls, old ABI code references function entry
3564	 points (dot symbols), while new ABI code references the function
3565	 descriptor symbol.  We need to make any combination of reference and
3566	 definition work together, without breaking archive linking.
3567
3568	 For a defined function "foo" and an undefined call to "bar":
3569	 An old object defines "foo" and ".foo", references ".bar" (possibly
3570	 "bar" too).
3571	 A new object defines "foo" and references "bar".
3572
3573	 A new object thus has no problem with its undefined symbols being
3574	 satisfied by definitions in an old object.  On the other hand, the
3575	 old object won't have ".bar" satisfied by a new object.
3576
3577	 Keep a list of newly added dot-symbols.  */
3578
3579      if (string[0] == '.')
3580	{
3581	  struct ppc_link_hash_table *htab;
3582
3583	  htab = (struct ppc_link_hash_table *) table;
3584	  eh->u.next_dot_sym = htab->dot_syms;
3585	  htab->dot_syms = eh;
3586	}
3587    }
3588
3589  return entry;
3590}
3591
3592/* Create a ppc64 ELF linker hash table.  */
3593
3594static struct bfd_link_hash_table *
3595ppc64_elf_link_hash_table_create (bfd *abfd)
3596{
3597  struct ppc_link_hash_table *htab;
3598  bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3599
3600  htab = bfd_zmalloc (amt);
3601  if (htab == NULL)
3602    return NULL;
3603
3604  if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3605				      sizeof (struct ppc_link_hash_entry)))
3606    {
3607      free (htab);
3608      return NULL;
3609    }
3610
3611  /* Init the stub hash table too.  */
3612  if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3613			    sizeof (struct ppc_stub_hash_entry)))
3614    return NULL;
3615
3616  /* And the branch hash table.  */
3617  if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3618			    sizeof (struct ppc_branch_hash_entry)))
3619    return NULL;
3620
3621  /* Initializing two fields of the union is just cosmetic.  We really
3622     only care about glist, but when compiled on a 32-bit host the
3623     bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3624     debugger inspection of these fields look nicer.  */
3625  htab->elf.init_got_refcount.refcount = 0;
3626  htab->elf.init_got_refcount.glist = NULL;
3627  htab->elf.init_plt_refcount.refcount = 0;
3628  htab->elf.init_plt_refcount.glist = NULL;
3629  htab->elf.init_got_offset.offset = 0;
3630  htab->elf.init_got_offset.glist = NULL;
3631  htab->elf.init_plt_offset.offset = 0;
3632  htab->elf.init_plt_offset.glist = NULL;
3633
3634  return &htab->elf.root;
3635}
3636
3637/* Free the derived linker hash table.  */
3638
3639static void
3640ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3641{
3642  struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3643
3644  bfd_hash_table_free (&ret->stub_hash_table);
3645  bfd_hash_table_free (&ret->branch_hash_table);
3646  _bfd_generic_link_hash_table_free (hash);
3647}
3648
3649/* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3650
3651void
3652ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3653{
3654  struct ppc_link_hash_table *htab;
3655
3656  elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3657
3658/* Always hook our dynamic sections into the first bfd, which is the
3659   linker created stub bfd.  This ensures that the GOT header is at
3660   the start of the output TOC section.  */
3661  htab = ppc_hash_table (info);
3662  htab->stub_bfd = abfd;
3663  htab->elf.dynobj = abfd;
3664}
3665
3666/* Build a name for an entry in the stub hash table.  */
3667
3668static char *
3669ppc_stub_name (const asection *input_section,
3670	       const asection *sym_sec,
3671	       const struct ppc_link_hash_entry *h,
3672	       const Elf_Internal_Rela *rel)
3673{
3674  char *stub_name;
3675  bfd_size_type len;
3676
3677  /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3678     offsets from a sym as a branch target?  In fact, we could
3679     probably assume the addend is always zero.  */
3680  BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3681
3682  if (h)
3683    {
3684      len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3685      stub_name = bfd_malloc (len);
3686      if (stub_name == NULL)
3687	return stub_name;
3688
3689      sprintf (stub_name, "%08x.%s+%x",
3690	       input_section->id & 0xffffffff,
3691	       h->elf.root.root.string,
3692	       (int) rel->r_addend & 0xffffffff);
3693    }
3694  else
3695    {
3696      len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3697      stub_name = bfd_malloc (len);
3698      if (stub_name == NULL)
3699	return stub_name;
3700
3701      sprintf (stub_name, "%08x.%x:%x+%x",
3702	       input_section->id & 0xffffffff,
3703	       sym_sec->id & 0xffffffff,
3704	       (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3705	       (int) rel->r_addend & 0xffffffff);
3706    }
3707  if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3708    stub_name[len - 2] = 0;
3709  return stub_name;
3710}
3711
3712/* Look up an entry in the stub hash.  Stub entries are cached because
3713   creating the stub name takes a bit of time.  */
3714
3715static struct ppc_stub_hash_entry *
3716ppc_get_stub_entry (const asection *input_section,
3717		    const asection *sym_sec,
3718		    struct ppc_link_hash_entry *h,
3719		    const Elf_Internal_Rela *rel,
3720		    struct ppc_link_hash_table *htab)
3721{
3722  struct ppc_stub_hash_entry *stub_entry;
3723  const asection *id_sec;
3724
3725  /* If this input section is part of a group of sections sharing one
3726     stub section, then use the id of the first section in the group.
3727     Stub names need to include a section id, as there may well be
3728     more than one stub used to reach say, printf, and we need to
3729     distinguish between them.  */
3730  id_sec = htab->stub_group[input_section->id].link_sec;
3731
3732  if (h != NULL && h->u.stub_cache != NULL
3733      && h->u.stub_cache->h == h
3734      && h->u.stub_cache->id_sec == id_sec)
3735    {
3736      stub_entry = h->u.stub_cache;
3737    }
3738  else
3739    {
3740      char *stub_name;
3741
3742      stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3743      if (stub_name == NULL)
3744	return NULL;
3745
3746      stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3747					 stub_name, FALSE, FALSE);
3748      if (h != NULL)
3749	h->u.stub_cache = stub_entry;
3750
3751      free (stub_name);
3752    }
3753
3754  return stub_entry;
3755}
3756
3757/* Add a new stub entry to the stub hash.  Not all fields of the new
3758   stub entry are initialised.  */
3759
3760static struct ppc_stub_hash_entry *
3761ppc_add_stub (const char *stub_name,
3762	      asection *section,
3763	      struct ppc_link_hash_table *htab)
3764{
3765  asection *link_sec;
3766  asection *stub_sec;
3767  struct ppc_stub_hash_entry *stub_entry;
3768
3769  link_sec = htab->stub_group[section->id].link_sec;
3770  stub_sec = htab->stub_group[section->id].stub_sec;
3771  if (stub_sec == NULL)
3772    {
3773      stub_sec = htab->stub_group[link_sec->id].stub_sec;
3774      if (stub_sec == NULL)
3775	{
3776	  size_t namelen;
3777	  bfd_size_type len;
3778	  char *s_name;
3779
3780	  namelen = strlen (link_sec->name);
3781	  len = namelen + sizeof (STUB_SUFFIX);
3782	  s_name = bfd_alloc (htab->stub_bfd, len);
3783	  if (s_name == NULL)
3784	    return NULL;
3785
3786	  memcpy (s_name, link_sec->name, namelen);
3787	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3788	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3789	  if (stub_sec == NULL)
3790	    return NULL;
3791	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
3792	}
3793      htab->stub_group[section->id].stub_sec = stub_sec;
3794    }
3795
3796  /* Enter this entry into the linker stub hash table.  */
3797  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3798				     TRUE, FALSE);
3799  if (stub_entry == NULL)
3800    {
3801      (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3802			     section->owner, stub_name);
3803      return NULL;
3804    }
3805
3806  stub_entry->stub_sec = stub_sec;
3807  stub_entry->stub_offset = 0;
3808  stub_entry->id_sec = link_sec;
3809  return stub_entry;
3810}
3811
3812/* Create sections for linker generated code.  */
3813
3814static bfd_boolean
3815create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3816{
3817  struct ppc_link_hash_table *htab;
3818  flagword flags;
3819
3820  htab = ppc_hash_table (info);
3821
3822  /* Create .sfpr for code to save and restore fp regs.  */
3823  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3824	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3825  htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3826						   flags);
3827  if (htab->sfpr == NULL
3828      || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3829    return FALSE;
3830
3831  /* Create .glink for lazy dynamic linking support.  */
3832  htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3833						    flags);
3834  if (htab->glink == NULL
3835      || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
3836    return FALSE;
3837
3838  /* Create branch lookup table for plt_branch stubs.  */
3839  flags = (SEC_ALLOC | SEC_LOAD
3840	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3841  htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3842						   flags);
3843  if (htab->brlt == NULL
3844      || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3845    return FALSE;
3846
3847  if (!info->shared)
3848    return TRUE;
3849
3850  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3851	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3852  htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
3853						      ".rela.branch_lt",
3854						      flags);
3855  if (!htab->relbrlt
3856      || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3857    return FALSE;
3858
3859  return TRUE;
3860}
3861
3862/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3863   not already done.  */
3864
3865static bfd_boolean
3866create_got_section (bfd *abfd, struct bfd_link_info *info)
3867{
3868  asection *got, *relgot;
3869  flagword flags;
3870  struct ppc_link_hash_table *htab = ppc_hash_table (info);
3871
3872  if (!htab->got)
3873    {
3874      if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3875	return FALSE;
3876
3877      htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3878      if (!htab->got)
3879	abort ();
3880    }
3881
3882  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3883	   | SEC_LINKER_CREATED);
3884
3885  got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3886  if (!got
3887      || !bfd_set_section_alignment (abfd, got, 3))
3888    return FALSE;
3889
3890  relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3891					       flags | SEC_READONLY);
3892  if (!relgot
3893      || ! bfd_set_section_alignment (abfd, relgot, 3))
3894    return FALSE;
3895
3896  ppc64_elf_tdata (abfd)->got = got;
3897  ppc64_elf_tdata (abfd)->relgot = relgot;
3898  return TRUE;
3899}
3900
3901/* Create the dynamic sections, and set up shortcuts.  */
3902
3903static bfd_boolean
3904ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3905{
3906  struct ppc_link_hash_table *htab;
3907
3908  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3909    return FALSE;
3910
3911  htab = ppc_hash_table (info);
3912  if (!htab->got)
3913    htab->got = bfd_get_section_by_name (dynobj, ".got");
3914  htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3915  htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3916  htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3917  if (!info->shared)
3918    htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3919
3920  if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3921      || (!info->shared && !htab->relbss))
3922    abort ();
3923
3924  return TRUE;
3925}
3926
3927/* Merge PLT info on FROM with that on TO.  */
3928
3929static void
3930move_plt_plist (struct ppc_link_hash_entry *from,
3931		struct ppc_link_hash_entry *to)
3932{
3933  if (from->elf.plt.plist != NULL)
3934    {
3935      if (to->elf.plt.plist != NULL)
3936	{
3937	  struct plt_entry **entp;
3938	  struct plt_entry *ent;
3939
3940	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3941	    {
3942	      struct plt_entry *dent;
3943
3944	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3945		if (dent->addend == ent->addend)
3946		  {
3947		    dent->plt.refcount += ent->plt.refcount;
3948		    *entp = ent->next;
3949		    break;
3950		  }
3951	      if (dent == NULL)
3952		entp = &ent->next;
3953	    }
3954	  *entp = to->elf.plt.plist;
3955	}
3956
3957      to->elf.plt.plist = from->elf.plt.plist;
3958      from->elf.plt.plist = NULL;
3959    }
3960}
3961
3962/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3963
3964static void
3965ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3966				struct elf_link_hash_entry *dir,
3967				struct elf_link_hash_entry *ind)
3968{
3969  struct ppc_link_hash_entry *edir, *eind;
3970
3971  edir = (struct ppc_link_hash_entry *) dir;
3972  eind = (struct ppc_link_hash_entry *) ind;
3973
3974  /* Copy over any dynamic relocs we may have on the indirect sym.  */
3975  if (eind->dyn_relocs != NULL)
3976    {
3977      if (edir->dyn_relocs != NULL)
3978	{
3979	  struct ppc_dyn_relocs **pp;
3980	  struct ppc_dyn_relocs *p;
3981
3982	  /* Add reloc counts against the indirect sym to the direct sym
3983	     list.  Merge any entries against the same section.  */
3984	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3985	    {
3986	      struct ppc_dyn_relocs *q;
3987
3988	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
3989		if (q->sec == p->sec)
3990		  {
3991		    q->pc_count += p->pc_count;
3992		    q->count += p->count;
3993		    *pp = p->next;
3994		    break;
3995		  }
3996	      if (q == NULL)
3997		pp = &p->next;
3998	    }
3999	  *pp = edir->dyn_relocs;
4000	}
4001
4002      edir->dyn_relocs = eind->dyn_relocs;
4003      eind->dyn_relocs = NULL;
4004    }
4005
4006  edir->is_func |= eind->is_func;
4007  edir->is_func_descriptor |= eind->is_func_descriptor;
4008  edir->tls_mask |= eind->tls_mask;
4009
4010  /* If called to transfer flags for a weakdef during processing
4011     of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4012     We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4013  if (!(ELIMINATE_COPY_RELOCS
4014	&& eind->elf.root.type != bfd_link_hash_indirect
4015	&& edir->elf.dynamic_adjusted))
4016    edir->elf.non_got_ref |= eind->elf.non_got_ref;
4017
4018  edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4019  edir->elf.ref_regular |= eind->elf.ref_regular;
4020  edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4021  edir->elf.needs_plt |= eind->elf.needs_plt;
4022
4023  /* If we were called to copy over info for a weak sym, that's all.  */
4024  if (eind->elf.root.type != bfd_link_hash_indirect)
4025    return;
4026
4027  /* Copy over got entries that we may have already seen to the
4028     symbol which just became indirect.  */
4029  if (eind->elf.got.glist != NULL)
4030    {
4031      if (edir->elf.got.glist != NULL)
4032	{
4033	  struct got_entry **entp;
4034	  struct got_entry *ent;
4035
4036	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4037	    {
4038	      struct got_entry *dent;
4039
4040	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4041		if (dent->addend == ent->addend
4042		    && dent->owner == ent->owner
4043		    && dent->tls_type == ent->tls_type)
4044		  {
4045		    dent->got.refcount += ent->got.refcount;
4046		    *entp = ent->next;
4047		    break;
4048		  }
4049	      if (dent == NULL)
4050		entp = &ent->next;
4051	    }
4052	  *entp = edir->elf.got.glist;
4053	}
4054
4055      edir->elf.got.glist = eind->elf.got.glist;
4056      eind->elf.got.glist = NULL;
4057    }
4058
4059  /* And plt entries.  */
4060  move_plt_plist (eind, edir);
4061
4062  if (eind->elf.dynindx != -1)
4063    {
4064      if (edir->elf.dynindx != -1)
4065	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4066				edir->elf.dynstr_index);
4067      edir->elf.dynindx = eind->elf.dynindx;
4068      edir->elf.dynstr_index = eind->elf.dynstr_index;
4069      eind->elf.dynindx = -1;
4070      eind->elf.dynstr_index = 0;
4071    }
4072}
4073
4074/* Find the function descriptor hash entry from the given function code
4075   hash entry FH.  Link the entries via their OH fields.  */
4076
4077static struct ppc_link_hash_entry *
4078get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4079{
4080  struct ppc_link_hash_entry *fdh = fh->oh;
4081
4082  if (fdh == NULL)
4083    {
4084      const char *fd_name = fh->elf.root.root.string + 1;
4085
4086      fdh = (struct ppc_link_hash_entry *)
4087	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4088      if (fdh != NULL)
4089	{
4090	  fdh->is_func_descriptor = 1;
4091	  fdh->oh = fh;
4092	  fh->is_func = 1;
4093	  fh->oh = fdh;
4094	}
4095    }
4096
4097  return fdh;
4098}
4099
4100/* Make a fake function descriptor sym for the code sym FH.  */
4101
4102static struct ppc_link_hash_entry *
4103make_fdh (struct bfd_link_info *info,
4104	  struct ppc_link_hash_entry *fh)
4105{
4106  bfd *abfd;
4107  asymbol *newsym;
4108  struct bfd_link_hash_entry *bh;
4109  struct ppc_link_hash_entry *fdh;
4110
4111  abfd = fh->elf.root.u.undef.abfd;
4112  newsym = bfd_make_empty_symbol (abfd);
4113  newsym->name = fh->elf.root.root.string + 1;
4114  newsym->section = bfd_und_section_ptr;
4115  newsym->value = 0;
4116  newsym->flags = BSF_WEAK;
4117
4118  bh = NULL;
4119  if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4120					 newsym->flags, newsym->section,
4121					 newsym->value, NULL, FALSE, FALSE,
4122					 &bh))
4123    return NULL;
4124
4125  fdh = (struct ppc_link_hash_entry *) bh;
4126  fdh->elf.non_elf = 0;
4127  fdh->fake = 1;
4128  fdh->is_func_descriptor = 1;
4129  fdh->oh = fh;
4130  fh->is_func = 1;
4131  fh->oh = fdh;
4132  return fdh;
4133}
4134
4135/* Fix function descriptor symbols defined in .opd sections to be
4136   function type.  */
4137
4138static bfd_boolean
4139ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
4140			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
4141			   Elf_Internal_Sym *isym,
4142			   const char **name ATTRIBUTE_UNUSED,
4143			   flagword *flags ATTRIBUTE_UNUSED,
4144			   asection **sec,
4145			   bfd_vma *value ATTRIBUTE_UNUSED)
4146{
4147  if (*sec != NULL
4148      && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4149    isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4150
4151  return TRUE;
4152}
4153
4154/* This function makes an old ABI object reference to ".bar" cause the
4155   inclusion of a new ABI object archive that defines "bar".
4156   NAME is a symbol defined in an archive.  Return a symbol in the hash
4157   table that might be satisfied by the archive symbols.  */
4158
4159static struct elf_link_hash_entry *
4160ppc64_elf_archive_symbol_lookup (bfd *abfd,
4161				 struct bfd_link_info *info,
4162				 const char *name)
4163{
4164  struct elf_link_hash_entry *h;
4165  char *dot_name;
4166  size_t len;
4167
4168  h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4169  if (h != NULL
4170      /* Don't return this sym if it is a fake function descriptor
4171	 created by add_symbol_adjust.  */
4172      && !(h->root.type == bfd_link_hash_undefweak
4173	   && ((struct ppc_link_hash_entry *) h)->fake))
4174    return h;
4175
4176  if (name[0] == '.')
4177    return h;
4178
4179  len = strlen (name);
4180  dot_name = bfd_alloc (abfd, len + 2);
4181  if (dot_name == NULL)
4182    return (struct elf_link_hash_entry *) 0 - 1;
4183  dot_name[0] = '.';
4184  memcpy (dot_name + 1, name, len + 1);
4185  h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4186  bfd_release (abfd, dot_name);
4187  return h;
4188}
4189
4190/* This function satisfies all old ABI object references to ".bar" if a
4191   new ABI object defines "bar".  Well, at least, undefined dot symbols
4192   are made weak.  This stops later archive searches from including an
4193   object if we already have a function descriptor definition.  It also
4194   prevents the linker complaining about undefined symbols.
4195   We also check and correct mismatched symbol visibility here.  The
4196   most restrictive visibility of the function descriptor and the
4197   function entry symbol is used.  */
4198
4199static bfd_boolean
4200add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4201{
4202  struct ppc_link_hash_table *htab;
4203  struct ppc_link_hash_entry *fdh;
4204
4205  if (eh->elf.root.type == bfd_link_hash_indirect)
4206    return TRUE;
4207
4208  if (eh->elf.root.type == bfd_link_hash_warning)
4209    eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4210
4211  if (eh->elf.root.root.string[0] != '.')
4212    abort ();
4213
4214  htab = ppc_hash_table (info);
4215  fdh = get_fdh (eh, htab);
4216  if (fdh == NULL
4217      && !info->relocatable
4218      && (eh->elf.root.type == bfd_link_hash_undefined
4219	  || eh->elf.root.type == bfd_link_hash_undefweak)
4220      && eh->elf.ref_regular)
4221    {
4222      /* Make an undefweak function descriptor sym, which is enough to
4223	 pull in an --as-needed shared lib, but won't cause link
4224	 errors.  Archives are handled elsewhere.  */
4225      fdh = make_fdh (info, eh);
4226      if (fdh == NULL)
4227	return FALSE;
4228      else
4229	fdh->elf.ref_regular = 1;
4230    }
4231  else if (fdh != NULL)
4232    {
4233      unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4234      unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4235      if (entry_vis < descr_vis)
4236	fdh->elf.other += entry_vis - descr_vis;
4237      else if (entry_vis > descr_vis)
4238	eh->elf.other += descr_vis - entry_vis;
4239
4240      if ((fdh->elf.root.type == bfd_link_hash_defined
4241	   || fdh->elf.root.type == bfd_link_hash_defweak)
4242	  && eh->elf.root.type == bfd_link_hash_undefined)
4243	{
4244	  eh->elf.root.type = bfd_link_hash_undefweak;
4245	  eh->was_undefined = 1;
4246	  htab->twiddled_syms = 1;
4247	}
4248    }
4249
4250  return TRUE;
4251}
4252
4253/* Process list of dot-symbols we made in link_hash_newfunc.  */
4254
4255static bfd_boolean
4256ppc64_elf_check_directives (bfd *ibfd, struct bfd_link_info *info)
4257{
4258  struct ppc_link_hash_table *htab;
4259  struct ppc_link_hash_entry **p, *eh;
4260
4261  htab = ppc_hash_table (info);
4262  if (!is_ppc64_elf_target (htab->elf.root.creator))
4263    return TRUE;
4264
4265  if (is_ppc64_elf_target (ibfd->xvec))
4266    {
4267      p = &htab->dot_syms;
4268      while ((eh = *p) != NULL)
4269	{
4270	  *p = NULL;
4271	  if (!add_symbol_adjust (eh, info))
4272	    return FALSE;
4273	  p = &eh->u.next_dot_sym;
4274	}
4275    }
4276
4277  /* Clear the list for non-ppc64 input files.  */
4278  p = &htab->dot_syms;
4279  while ((eh = *p) != NULL)
4280    {
4281      *p = NULL;
4282      p = &eh->u.next_dot_sym;
4283    }
4284
4285  /* We need to fix the undefs list for any syms we have twiddled to
4286     undef_weak.  */
4287  if (htab->twiddled_syms)
4288    {
4289      bfd_link_repair_undef_list (&htab->elf.root);
4290      htab->twiddled_syms = 0;
4291    }
4292  return TRUE;
4293}
4294
4295/* Undo hash table changes when an --as-needed input file is determined
4296   not to be needed.  */
4297
4298static bfd_boolean
4299ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4300			     struct bfd_link_info *info)
4301{
4302  ppc_hash_table (info)->dot_syms = NULL;
4303  return TRUE;
4304}
4305
4306static bfd_boolean
4307update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4308		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4309{
4310  struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4311  char *local_got_tls_masks;
4312
4313  if (local_got_ents == NULL)
4314    {
4315      bfd_size_type size = symtab_hdr->sh_info;
4316
4317      size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4318      local_got_ents = bfd_zalloc (abfd, size);
4319      if (local_got_ents == NULL)
4320	return FALSE;
4321      elf_local_got_ents (abfd) = local_got_ents;
4322    }
4323
4324  if ((tls_type & TLS_EXPLICIT) == 0)
4325    {
4326      struct got_entry *ent;
4327
4328      for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4329	if (ent->addend == r_addend
4330	    && ent->owner == abfd
4331	    && ent->tls_type == tls_type)
4332	  break;
4333      if (ent == NULL)
4334	{
4335	  bfd_size_type amt = sizeof (*ent);
4336	  ent = bfd_alloc (abfd, amt);
4337	  if (ent == NULL)
4338	    return FALSE;
4339	  ent->next = local_got_ents[r_symndx];
4340	  ent->addend = r_addend;
4341	  ent->owner = abfd;
4342	  ent->tls_type = tls_type;
4343	  ent->got.refcount = 0;
4344	  local_got_ents[r_symndx] = ent;
4345	}
4346      ent->got.refcount += 1;
4347    }
4348
4349  local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4350  local_got_tls_masks[r_symndx] |= tls_type;
4351  return TRUE;
4352}
4353
4354static bfd_boolean
4355update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4356{
4357  struct plt_entry *ent;
4358
4359  for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4360    if (ent->addend == addend)
4361      break;
4362  if (ent == NULL)
4363    {
4364      bfd_size_type amt = sizeof (*ent);
4365      ent = bfd_alloc (abfd, amt);
4366      if (ent == NULL)
4367	return FALSE;
4368      ent->next = eh->elf.plt.plist;
4369      ent->addend = addend;
4370      ent->plt.refcount = 0;
4371      eh->elf.plt.plist = ent;
4372    }
4373  ent->plt.refcount += 1;
4374  eh->elf.needs_plt = 1;
4375  if (eh->elf.root.root.string[0] == '.'
4376      && eh->elf.root.root.string[1] != '\0')
4377    eh->is_func = 1;
4378  return TRUE;
4379}
4380
4381/* Look through the relocs for a section during the first phase, and
4382   calculate needed space in the global offset table, procedure
4383   linkage table, and dynamic reloc sections.  */
4384
4385static bfd_boolean
4386ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4387			asection *sec, const Elf_Internal_Rela *relocs)
4388{
4389  struct ppc_link_hash_table *htab;
4390  Elf_Internal_Shdr *symtab_hdr;
4391  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4392  const Elf_Internal_Rela *rel;
4393  const Elf_Internal_Rela *rel_end;
4394  asection *sreloc;
4395  asection **opd_sym_map;
4396
4397  if (info->relocatable)
4398    return TRUE;
4399
4400  /* Don't do anything special with non-loaded, non-alloced sections.
4401     In particular, any relocs in such sections should not affect GOT
4402     and PLT reference counting (ie. we don't allow them to create GOT
4403     or PLT entries), there's no possibility or desire to optimize TLS
4404     relocs, and there's not much point in propagating relocs to shared
4405     libs that the dynamic linker won't relocate.  */
4406  if ((sec->flags & SEC_ALLOC) == 0)
4407    return TRUE;
4408
4409  htab = ppc_hash_table (info);
4410  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4411
4412  sym_hashes = elf_sym_hashes (abfd);
4413  sym_hashes_end = (sym_hashes
4414		    + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4415		    - symtab_hdr->sh_info);
4416
4417  sreloc = NULL;
4418  opd_sym_map = NULL;
4419  if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4420    {
4421      /* Garbage collection needs some extra help with .opd sections.
4422	 We don't want to necessarily keep everything referenced by
4423	 relocs in .opd, as that would keep all functions.  Instead,
4424	 if we reference an .opd symbol (a function descriptor), we
4425	 want to keep the function code symbol's section.  This is
4426	 easy for global symbols, but for local syms we need to keep
4427	 information about the associated function section.  Later, if
4428	 edit_opd deletes entries, we'll use this array to adjust
4429	 local syms in .opd.  */
4430      union opd_info {
4431	asection *func_section;
4432	long entry_adjust;
4433      };
4434      bfd_size_type amt;
4435
4436      amt = sec->size * sizeof (union opd_info) / 8;
4437      opd_sym_map = bfd_zalloc (abfd, amt);
4438      if (opd_sym_map == NULL)
4439	return FALSE;
4440      ppc64_elf_section_data (sec)->u.opd_func_sec = opd_sym_map;
4441      BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4442      ppc64_elf_section_data (sec)->sec_type = sec_opd;
4443    }
4444
4445  if (htab->sfpr == NULL
4446      && !create_linkage_sections (htab->elf.dynobj, info))
4447    return FALSE;
4448
4449  rel_end = relocs + sec->reloc_count;
4450  for (rel = relocs; rel < rel_end; rel++)
4451    {
4452      unsigned long r_symndx;
4453      struct elf_link_hash_entry *h;
4454      enum elf_ppc64_reloc_type r_type;
4455      int tls_type = 0;
4456      struct _ppc64_elf_section_data *ppc64_sec;
4457
4458      r_symndx = ELF64_R_SYM (rel->r_info);
4459      if (r_symndx < symtab_hdr->sh_info)
4460	h = NULL;
4461      else
4462	{
4463	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4464	  while (h->root.type == bfd_link_hash_indirect
4465		 || h->root.type == bfd_link_hash_warning)
4466	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4467	}
4468
4469      r_type = ELF64_R_TYPE (rel->r_info);
4470      switch (r_type)
4471	{
4472	case R_PPC64_GOT_TLSLD16:
4473	case R_PPC64_GOT_TLSLD16_LO:
4474	case R_PPC64_GOT_TLSLD16_HI:
4475	case R_PPC64_GOT_TLSLD16_HA:
4476	  ppc64_tlsld_got (abfd)->refcount += 1;
4477	  tls_type = TLS_TLS | TLS_LD;
4478	  goto dogottls;
4479
4480	case R_PPC64_GOT_TLSGD16:
4481	case R_PPC64_GOT_TLSGD16_LO:
4482	case R_PPC64_GOT_TLSGD16_HI:
4483	case R_PPC64_GOT_TLSGD16_HA:
4484	  tls_type = TLS_TLS | TLS_GD;
4485	  goto dogottls;
4486
4487	case R_PPC64_GOT_TPREL16_DS:
4488	case R_PPC64_GOT_TPREL16_LO_DS:
4489	case R_PPC64_GOT_TPREL16_HI:
4490	case R_PPC64_GOT_TPREL16_HA:
4491	  if (info->shared)
4492	    info->flags |= DF_STATIC_TLS;
4493	  tls_type = TLS_TLS | TLS_TPREL;
4494	  goto dogottls;
4495
4496	case R_PPC64_GOT_DTPREL16_DS:
4497	case R_PPC64_GOT_DTPREL16_LO_DS:
4498	case R_PPC64_GOT_DTPREL16_HI:
4499	case R_PPC64_GOT_DTPREL16_HA:
4500	  tls_type = TLS_TLS | TLS_DTPREL;
4501	dogottls:
4502	  sec->has_tls_reloc = 1;
4503	  /* Fall thru */
4504
4505	case R_PPC64_GOT16:
4506	case R_PPC64_GOT16_DS:
4507	case R_PPC64_GOT16_HA:
4508	case R_PPC64_GOT16_HI:
4509	case R_PPC64_GOT16_LO:
4510	case R_PPC64_GOT16_LO_DS:
4511	  /* This symbol requires a global offset table entry.  */
4512	  sec->has_toc_reloc = 1;
4513	  if (ppc64_elf_tdata (abfd)->got == NULL
4514	      && !create_got_section (abfd, info))
4515	    return FALSE;
4516
4517	  if (h != NULL)
4518	    {
4519	      struct ppc_link_hash_entry *eh;
4520	      struct got_entry *ent;
4521
4522	      eh = (struct ppc_link_hash_entry *) h;
4523	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4524		if (ent->addend == rel->r_addend
4525		    && ent->owner == abfd
4526		    && ent->tls_type == tls_type)
4527		  break;
4528	      if (ent == NULL)
4529		{
4530		  bfd_size_type amt = sizeof (*ent);
4531		  ent = bfd_alloc (abfd, amt);
4532		  if (ent == NULL)
4533		    return FALSE;
4534		  ent->next = eh->elf.got.glist;
4535		  ent->addend = rel->r_addend;
4536		  ent->owner = abfd;
4537		  ent->tls_type = tls_type;
4538		  ent->got.refcount = 0;
4539		  eh->elf.got.glist = ent;
4540		}
4541	      ent->got.refcount += 1;
4542	      eh->tls_mask |= tls_type;
4543	    }
4544	  else
4545	    /* This is a global offset table entry for a local symbol.  */
4546	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4547					rel->r_addend, tls_type))
4548	      return FALSE;
4549	  break;
4550
4551	case R_PPC64_PLT16_HA:
4552	case R_PPC64_PLT16_HI:
4553	case R_PPC64_PLT16_LO:
4554	case R_PPC64_PLT32:
4555	case R_PPC64_PLT64:
4556	  /* This symbol requires a procedure linkage table entry.  We
4557	     actually build the entry in adjust_dynamic_symbol,
4558	     because this might be a case of linking PIC code without
4559	     linking in any dynamic objects, in which case we don't
4560	     need to generate a procedure linkage table after all.  */
4561	  if (h == NULL)
4562	    {
4563	      /* It does not make sense to have a procedure linkage
4564		 table entry for a local symbol.  */
4565	      bfd_set_error (bfd_error_bad_value);
4566	      return FALSE;
4567	    }
4568	  else
4569	    if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4570				  rel->r_addend))
4571	      return FALSE;
4572	  break;
4573
4574	  /* The following relocations don't need to propagate the
4575	     relocation if linking a shared object since they are
4576	     section relative.  */
4577	case R_PPC64_SECTOFF:
4578	case R_PPC64_SECTOFF_LO:
4579	case R_PPC64_SECTOFF_HI:
4580	case R_PPC64_SECTOFF_HA:
4581	case R_PPC64_SECTOFF_DS:
4582	case R_PPC64_SECTOFF_LO_DS:
4583	case R_PPC64_DTPREL16:
4584	case R_PPC64_DTPREL16_LO:
4585	case R_PPC64_DTPREL16_HI:
4586	case R_PPC64_DTPREL16_HA:
4587	case R_PPC64_DTPREL16_DS:
4588	case R_PPC64_DTPREL16_LO_DS:
4589	case R_PPC64_DTPREL16_HIGHER:
4590	case R_PPC64_DTPREL16_HIGHERA:
4591	case R_PPC64_DTPREL16_HIGHEST:
4592	case R_PPC64_DTPREL16_HIGHESTA:
4593	  break;
4594
4595	  /* Nor do these.  */
4596	case R_PPC64_TOC16:
4597	case R_PPC64_TOC16_LO:
4598	case R_PPC64_TOC16_HI:
4599	case R_PPC64_TOC16_HA:
4600	case R_PPC64_TOC16_DS:
4601	case R_PPC64_TOC16_LO_DS:
4602	  sec->has_toc_reloc = 1;
4603	  break;
4604
4605	  /* This relocation describes the C++ object vtable hierarchy.
4606	     Reconstruct it for later use during GC.  */
4607	case R_PPC64_GNU_VTINHERIT:
4608	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4609	    return FALSE;
4610	  break;
4611
4612	  /* This relocation describes which C++ vtable entries are actually
4613	     used.  Record for later use during GC.  */
4614	case R_PPC64_GNU_VTENTRY:
4615	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4616	    return FALSE;
4617	  break;
4618
4619	case R_PPC64_REL14:
4620	case R_PPC64_REL14_BRTAKEN:
4621	case R_PPC64_REL14_BRNTAKEN:
4622	  {
4623	    asection *dest = NULL;
4624
4625	    /* Heuristic: If jumping outside our section, chances are
4626	       we are going to need a stub.  */
4627	    if (h != NULL)
4628	      {
4629		/* If the sym is weak it may be overridden later, so
4630		   don't assume we know where a weak sym lives.  */
4631		if (h->root.type == bfd_link_hash_defined)
4632		  dest = h->root.u.def.section;
4633	      }
4634	    else
4635	      dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4636						sec, r_symndx);
4637	    if (dest != sec)
4638	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4639	  }
4640	  /* Fall through.  */
4641
4642	case R_PPC64_REL24:
4643	  if (h != NULL)
4644	    {
4645	      /* We may need a .plt entry if the function this reloc
4646		 refers to is in a shared lib.  */
4647	      if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4648				    rel->r_addend))
4649		return FALSE;
4650	      if (h == &htab->tls_get_addr->elf
4651		  || h == &htab->tls_get_addr_fd->elf)
4652		sec->has_tls_reloc = 1;
4653	      else if (htab->tls_get_addr == NULL
4654		       && CONST_STRNEQ (h->root.root.string, ".__tls_get_addr")
4655		       && (h->root.root.string[15] == 0
4656			   || h->root.root.string[15] == '@'))
4657		{
4658		  htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4659		  sec->has_tls_reloc = 1;
4660		}
4661	      else if (htab->tls_get_addr_fd == NULL
4662		       && CONST_STRNEQ (h->root.root.string, "__tls_get_addr")
4663		       && (h->root.root.string[14] == 0
4664			   || h->root.root.string[14] == '@'))
4665		{
4666		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4667		  sec->has_tls_reloc = 1;
4668		}
4669	    }
4670	  break;
4671
4672	case R_PPC64_TPREL64:
4673	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4674	  if (info->shared)
4675	    info->flags |= DF_STATIC_TLS;
4676	  goto dotlstoc;
4677
4678	case R_PPC64_DTPMOD64:
4679	  if (rel + 1 < rel_end
4680	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4681	      && rel[1].r_offset == rel->r_offset + 8)
4682	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4683	  else
4684	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4685	  goto dotlstoc;
4686
4687	case R_PPC64_DTPREL64:
4688	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4689	  if (rel != relocs
4690	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4691	      && rel[-1].r_offset == rel->r_offset - 8)
4692	    /* This is the second reloc of a dtpmod, dtprel pair.
4693	       Don't mark with TLS_DTPREL.  */
4694	    goto dodyn;
4695
4696	dotlstoc:
4697	  sec->has_tls_reloc = 1;
4698	  if (h != NULL)
4699	    {
4700	      struct ppc_link_hash_entry *eh;
4701	      eh = (struct ppc_link_hash_entry *) h;
4702	      eh->tls_mask |= tls_type;
4703	    }
4704	  else
4705	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4706					rel->r_addend, tls_type))
4707	      return FALSE;
4708
4709	  ppc64_sec = ppc64_elf_section_data (sec);
4710	  if (ppc64_sec->sec_type != sec_toc)
4711	    {
4712	      /* One extra to simplify get_tls_mask.  */
4713	      bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4714	      ppc64_sec->u.t_symndx = bfd_zalloc (abfd, amt);
4715	      if (ppc64_sec->u.t_symndx == NULL)
4716		return FALSE;
4717	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4718	      ppc64_sec->sec_type = sec_toc;
4719	    }
4720	  BFD_ASSERT (rel->r_offset % 8 == 0);
4721	  ppc64_sec->u.t_symndx[rel->r_offset / 8] = r_symndx;
4722
4723	  /* Mark the second slot of a GD or LD entry.
4724	     -1 to indicate GD and -2 to indicate LD.  */
4725	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4726	    ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -1;
4727	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4728	    ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -2;
4729	  goto dodyn;
4730
4731	case R_PPC64_TPREL16:
4732	case R_PPC64_TPREL16_LO:
4733	case R_PPC64_TPREL16_HI:
4734	case R_PPC64_TPREL16_HA:
4735	case R_PPC64_TPREL16_DS:
4736	case R_PPC64_TPREL16_LO_DS:
4737	case R_PPC64_TPREL16_HIGHER:
4738	case R_PPC64_TPREL16_HIGHERA:
4739	case R_PPC64_TPREL16_HIGHEST:
4740	case R_PPC64_TPREL16_HIGHESTA:
4741	  if (info->shared)
4742	    {
4743	      info->flags |= DF_STATIC_TLS;
4744	      goto dodyn;
4745	    }
4746	  break;
4747
4748	case R_PPC64_ADDR64:
4749	  if (opd_sym_map != NULL
4750	      && rel + 1 < rel_end
4751	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4752	    {
4753	      if (h != NULL)
4754		{
4755		  if (h->root.root.string[0] == '.'
4756		      && h->root.root.string[1] != 0
4757		      && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4758		    ;
4759		  else
4760		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
4761		}
4762	      else
4763		{
4764		  asection *s;
4765
4766		  s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4767						 r_symndx);
4768		  if (s == NULL)
4769		    return FALSE;
4770		  else if (s != sec)
4771		    opd_sym_map[rel->r_offset / 8] = s;
4772		}
4773	    }
4774	  /* Fall through.  */
4775
4776	case R_PPC64_REL30:
4777	case R_PPC64_REL32:
4778	case R_PPC64_REL64:
4779	case R_PPC64_ADDR14:
4780	case R_PPC64_ADDR14_BRNTAKEN:
4781	case R_PPC64_ADDR14_BRTAKEN:
4782	case R_PPC64_ADDR16:
4783	case R_PPC64_ADDR16_DS:
4784	case R_PPC64_ADDR16_HA:
4785	case R_PPC64_ADDR16_HI:
4786	case R_PPC64_ADDR16_HIGHER:
4787	case R_PPC64_ADDR16_HIGHERA:
4788	case R_PPC64_ADDR16_HIGHEST:
4789	case R_PPC64_ADDR16_HIGHESTA:
4790	case R_PPC64_ADDR16_LO:
4791	case R_PPC64_ADDR16_LO_DS:
4792	case R_PPC64_ADDR24:
4793	case R_PPC64_ADDR32:
4794	case R_PPC64_UADDR16:
4795	case R_PPC64_UADDR32:
4796	case R_PPC64_UADDR64:
4797	case R_PPC64_TOC:
4798	  if (h != NULL && !info->shared)
4799	    /* We may need a copy reloc.  */
4800	    h->non_got_ref = 1;
4801
4802	  /* Don't propagate .opd relocs.  */
4803	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
4804	    break;
4805
4806	  /* If we are creating a shared library, and this is a reloc
4807	     against a global symbol, or a non PC relative reloc
4808	     against a local symbol, then we need to copy the reloc
4809	     into the shared library.  However, if we are linking with
4810	     -Bsymbolic, we do not need to copy a reloc against a
4811	     global symbol which is defined in an object we are
4812	     including in the link (i.e., DEF_REGULAR is set).  At
4813	     this point we have not seen all the input files, so it is
4814	     possible that DEF_REGULAR is not set now but will be set
4815	     later (it is never cleared).  In case of a weak definition,
4816	     DEF_REGULAR may be cleared later by a strong definition in
4817	     a shared library.  We account for that possibility below by
4818	     storing information in the dyn_relocs field of the hash
4819	     table entry.  A similar situation occurs when creating
4820	     shared libraries and symbol visibility changes render the
4821	     symbol local.
4822
4823	     If on the other hand, we are creating an executable, we
4824	     may need to keep relocations for symbols satisfied by a
4825	     dynamic library if we manage to avoid copy relocs for the
4826	     symbol.  */
4827	dodyn:
4828	  if ((info->shared
4829	       && (MUST_BE_DYN_RELOC (r_type)
4830		   || (h != NULL
4831		       && (! info->symbolic
4832			   || h->root.type == bfd_link_hash_defweak
4833			   || !h->def_regular))))
4834	      || (ELIMINATE_COPY_RELOCS
4835		  && !info->shared
4836		  && h != NULL
4837		  && (h->root.type == bfd_link_hash_defweak
4838		      || !h->def_regular)))
4839	    {
4840	      struct ppc_dyn_relocs *p;
4841	      struct ppc_dyn_relocs **head;
4842
4843	      /* We must copy these reloc types into the output file.
4844		 Create a reloc section in dynobj and make room for
4845		 this reloc.  */
4846	      if (sreloc == NULL)
4847		{
4848		  const char *name;
4849		  bfd *dynobj;
4850
4851		  name = (bfd_elf_string_from_elf_section
4852			  (abfd,
4853			   elf_elfheader (abfd)->e_shstrndx,
4854			   elf_section_data (sec)->rel_hdr.sh_name));
4855		  if (name == NULL)
4856		    return FALSE;
4857
4858		  if (! CONST_STRNEQ (name, ".rela")
4859		      || strcmp (bfd_get_section_name (abfd, sec),
4860				 name + 5) != 0)
4861		    {
4862		      (*_bfd_error_handler)
4863			(_("%B: bad relocation section name `%s\'"),
4864			 abfd, name);
4865		      bfd_set_error (bfd_error_bad_value);
4866		    }
4867
4868		  dynobj = htab->elf.dynobj;
4869		  sreloc = bfd_get_section_by_name (dynobj, name);
4870		  if (sreloc == NULL)
4871		    {
4872		      flagword flags;
4873
4874		      flags = (SEC_HAS_CONTENTS | SEC_READONLY
4875			       | SEC_IN_MEMORY | SEC_LINKER_CREATED
4876			       | SEC_ALLOC | SEC_LOAD);
4877		      sreloc = bfd_make_section_with_flags (dynobj,
4878							    name,
4879							    flags);
4880		      if (sreloc == NULL
4881			  || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4882			return FALSE;
4883		    }
4884		  elf_section_data (sec)->sreloc = sreloc;
4885		}
4886
4887	      /* If this is a global symbol, we count the number of
4888		 relocations we need for this symbol.  */
4889	      if (h != NULL)
4890		{
4891		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4892		}
4893	      else
4894		{
4895		  /* Track dynamic relocs needed for local syms too.
4896		     We really need local syms available to do this
4897		     easily.  Oh well.  */
4898
4899		  asection *s;
4900		  void *vpp;
4901
4902		  s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4903						 sec, r_symndx);
4904		  if (s == NULL)
4905		    return FALSE;
4906
4907		  vpp = &elf_section_data (s)->local_dynrel;
4908		  head = (struct ppc_dyn_relocs **) vpp;
4909		}
4910
4911	      p = *head;
4912	      if (p == NULL || p->sec != sec)
4913		{
4914		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4915		  if (p == NULL)
4916		    return FALSE;
4917		  p->next = *head;
4918		  *head = p;
4919		  p->sec = sec;
4920		  p->count = 0;
4921		  p->pc_count = 0;
4922		}
4923
4924	      p->count += 1;
4925	      if (!MUST_BE_DYN_RELOC (r_type))
4926		p->pc_count += 1;
4927	    }
4928	  break;
4929
4930	default:
4931	  break;
4932	}
4933    }
4934
4935  return TRUE;
4936}
4937
4938/* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
4939   of the code entry point, and its section.  */
4940
4941static bfd_vma
4942opd_entry_value (asection *opd_sec,
4943		 bfd_vma offset,
4944		 asection **code_sec,
4945		 bfd_vma *code_off)
4946{
4947  bfd *opd_bfd = opd_sec->owner;
4948  Elf_Internal_Rela *relocs;
4949  Elf_Internal_Rela *lo, *hi, *look;
4950  bfd_vma val;
4951
4952  /* No relocs implies we are linking a --just-symbols object.  */
4953  if (opd_sec->reloc_count == 0)
4954    {
4955      bfd_vma val;
4956
4957      if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4958	return (bfd_vma) -1;
4959
4960      if (code_sec != NULL)
4961	{
4962	  asection *sec, *likely = NULL;
4963	  for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4964	    if (sec->vma <= val
4965		&& (sec->flags & SEC_LOAD) != 0
4966		&& (sec->flags & SEC_ALLOC) != 0)
4967	      likely = sec;
4968	  if (likely != NULL)
4969	    {
4970	      *code_sec = likely;
4971	      if (code_off != NULL)
4972		*code_off = val - likely->vma;
4973	    }
4974	}
4975      return val;
4976    }
4977
4978  relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4979  if (relocs == NULL)
4980    relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4981
4982  /* Go find the opd reloc at the sym address.  */
4983  lo = relocs;
4984  BFD_ASSERT (lo != NULL);
4985  hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4986  val = (bfd_vma) -1;
4987  while (lo < hi)
4988    {
4989      look = lo + (hi - lo) / 2;
4990      if (look->r_offset < offset)
4991	lo = look + 1;
4992      else if (look->r_offset > offset)
4993	hi = look;
4994      else
4995	{
4996	  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4997	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4998	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4999	    {
5000	      unsigned long symndx = ELF64_R_SYM (look->r_info);
5001	      asection *sec;
5002
5003	      if (symndx < symtab_hdr->sh_info)
5004		{
5005		  Elf_Internal_Sym *sym;
5006
5007		  sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5008		  if (sym == NULL)
5009		    {
5010		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5011						  symtab_hdr->sh_info,
5012						  0, NULL, NULL, NULL);
5013		      if (sym == NULL)
5014			break;
5015		      symtab_hdr->contents = (bfd_byte *) sym;
5016		    }
5017
5018		  sym += symndx;
5019		  val = sym->st_value;
5020		  sec = NULL;
5021		  if ((sym->st_shndx != SHN_UNDEF
5022		       && sym->st_shndx < SHN_LORESERVE)
5023		      || sym->st_shndx > SHN_HIRESERVE)
5024		    sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5025		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5026		}
5027	      else
5028		{
5029		  struct elf_link_hash_entry **sym_hashes;
5030		  struct elf_link_hash_entry *rh;
5031
5032		  sym_hashes = elf_sym_hashes (opd_bfd);
5033		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
5034		  while (rh->root.type == bfd_link_hash_indirect
5035			 || rh->root.type == bfd_link_hash_warning)
5036		    rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
5037		  BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5038			      || rh->root.type == bfd_link_hash_defweak);
5039		  val = rh->root.u.def.value;
5040		  sec = rh->root.u.def.section;
5041		}
5042	      val += look->r_addend;
5043	      if (code_off != NULL)
5044		*code_off = val;
5045	      if (code_sec != NULL)
5046		*code_sec = sec;
5047	      if (sec != NULL && sec->output_section != NULL)
5048		val += sec->output_section->vma + sec->output_offset;
5049	    }
5050	  break;
5051	}
5052    }
5053
5054  return val;
5055}
5056
5057/* Mark sections containing dynamically referenced symbols.  When
5058   building shared libraries, we must assume that any visible symbol is
5059   referenced.  */
5060
5061static bfd_boolean
5062ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5063{
5064  struct bfd_link_info *info = (struct bfd_link_info *) inf;
5065  struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5066
5067  if (eh->elf.root.type == bfd_link_hash_warning)
5068    eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5069
5070  /* Dynamic linking info is on the func descriptor sym.  */
5071  if (eh->oh != NULL
5072      && eh->oh->is_func_descriptor
5073      && (eh->oh->elf.root.type == bfd_link_hash_defined
5074	  || eh->oh->elf.root.type == bfd_link_hash_defweak))
5075    eh = eh->oh;
5076
5077  if ((eh->elf.root.type == bfd_link_hash_defined
5078       || eh->elf.root.type == bfd_link_hash_defweak)
5079      && (eh->elf.ref_dynamic
5080	  || (!info->executable
5081	      && eh->elf.def_regular
5082	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5083	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5084    {
5085      asection *code_sec;
5086
5087      eh->elf.root.u.def.section->flags |= SEC_KEEP;
5088
5089      /* Function descriptor syms cause the associated
5090	 function code sym section to be marked.  */
5091      if (eh->is_func_descriptor
5092	  && (eh->oh->elf.root.type == bfd_link_hash_defined
5093	      || eh->oh->elf.root.type == bfd_link_hash_defweak))
5094	eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
5095      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5096	       && opd_entry_value (eh->elf.root.u.def.section,
5097				   eh->elf.root.u.def.value,
5098				   &code_sec, NULL) != (bfd_vma) -1)
5099	code_sec->flags |= SEC_KEEP;
5100    }
5101
5102  return TRUE;
5103}
5104
5105/* Return the section that should be marked against GC for a given
5106   relocation.  */
5107
5108static asection *
5109ppc64_elf_gc_mark_hook (asection *sec,
5110			struct bfd_link_info *info,
5111			Elf_Internal_Rela *rel,
5112			struct elf_link_hash_entry *h,
5113			Elf_Internal_Sym *sym)
5114{
5115  asection *rsec;
5116
5117  /* First mark all our entry sym sections.  */
5118  if (info->gc_sym_list != NULL)
5119    {
5120      struct ppc_link_hash_table *htab = ppc_hash_table (info);
5121      struct bfd_sym_chain *sym = info->gc_sym_list;
5122
5123      info->gc_sym_list = NULL;
5124      for (; sym != NULL; sym = sym->next)
5125	{
5126	  struct ppc_link_hash_entry *eh;
5127
5128	  eh = (struct ppc_link_hash_entry *)
5129	    elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5130	  if (eh == NULL)
5131	    continue;
5132	  if (eh->elf.root.type != bfd_link_hash_defined
5133	      && eh->elf.root.type != bfd_link_hash_defweak)
5134	    continue;
5135
5136	  if (eh->is_func_descriptor
5137	      && (eh->oh->elf.root.type == bfd_link_hash_defined
5138		  || eh->oh->elf.root.type == bfd_link_hash_defweak))
5139	    rsec = eh->oh->elf.root.u.def.section;
5140	  else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5141		   && opd_entry_value (eh->elf.root.u.def.section,
5142				       eh->elf.root.u.def.value,
5143				       &rsec, NULL) != (bfd_vma) -1)
5144	    ;
5145	  else
5146	    continue;
5147
5148	  if (!rsec->gc_mark)
5149	    _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5150
5151	  rsec = eh->elf.root.u.def.section;
5152	  if (!rsec->gc_mark)
5153	    _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5154	}
5155    }
5156
5157  /* Syms return NULL if we're marking .opd, so we avoid marking all
5158     function sections, as all functions are referenced in .opd.  */
5159  rsec = NULL;
5160  if (get_opd_info (sec) != NULL)
5161    return rsec;
5162
5163  if (h != NULL)
5164    {
5165      enum elf_ppc64_reloc_type r_type;
5166      struct ppc_link_hash_entry *eh;
5167
5168      r_type = ELF64_R_TYPE (rel->r_info);
5169      switch (r_type)
5170	{
5171	case R_PPC64_GNU_VTINHERIT:
5172	case R_PPC64_GNU_VTENTRY:
5173	  break;
5174
5175	default:
5176	  switch (h->root.type)
5177	    {
5178	    case bfd_link_hash_defined:
5179	    case bfd_link_hash_defweak:
5180	      eh = (struct ppc_link_hash_entry *) h;
5181	      if (eh->oh != NULL
5182		  && eh->oh->is_func_descriptor
5183		  && (eh->oh->elf.root.type == bfd_link_hash_defined
5184		      || eh->oh->elf.root.type == bfd_link_hash_defweak))
5185		eh = eh->oh;
5186
5187	      /* Function descriptor syms cause the associated
5188		 function code sym section to be marked.  */
5189	      if (eh->is_func_descriptor
5190		  && (eh->oh->elf.root.type == bfd_link_hash_defined
5191		      || eh->oh->elf.root.type == bfd_link_hash_defweak))
5192		{
5193		  /* They also mark their opd section.  */
5194		  if (!eh->elf.root.u.def.section->gc_mark)
5195		    _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5196				      ppc64_elf_gc_mark_hook);
5197
5198		  rsec = eh->oh->elf.root.u.def.section;
5199		}
5200	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5201		       && opd_entry_value (eh->elf.root.u.def.section,
5202					   eh->elf.root.u.def.value,
5203					   &rsec, NULL) != (bfd_vma) -1)
5204		{
5205		  if (!eh->elf.root.u.def.section->gc_mark)
5206		    _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5207				      ppc64_elf_gc_mark_hook);
5208		}
5209	      else
5210		rsec = h->root.u.def.section;
5211	      break;
5212
5213	    case bfd_link_hash_common:
5214	      rsec = h->root.u.c.p->section;
5215	      break;
5216
5217	    default:
5218	      break;
5219	    }
5220	}
5221    }
5222  else
5223    {
5224      asection **opd_sym_section;
5225
5226      rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5227      opd_sym_section = get_opd_info (rsec);
5228      if (opd_sym_section != NULL)
5229	{
5230	  if (!rsec->gc_mark)
5231	    _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5232
5233	  rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5234	}
5235    }
5236
5237  return rsec;
5238}
5239
5240/* Update the .got, .plt. and dynamic reloc reference counts for the
5241   section being removed.  */
5242
5243static bfd_boolean
5244ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5245			 asection *sec, const Elf_Internal_Rela *relocs)
5246{
5247  struct ppc_link_hash_table *htab;
5248  Elf_Internal_Shdr *symtab_hdr;
5249  struct elf_link_hash_entry **sym_hashes;
5250  struct got_entry **local_got_ents;
5251  const Elf_Internal_Rela *rel, *relend;
5252
5253  if ((sec->flags & SEC_ALLOC) == 0)
5254    return TRUE;
5255
5256  elf_section_data (sec)->local_dynrel = NULL;
5257
5258  htab = ppc_hash_table (info);
5259  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5260  sym_hashes = elf_sym_hashes (abfd);
5261  local_got_ents = elf_local_got_ents (abfd);
5262
5263  relend = relocs + sec->reloc_count;
5264  for (rel = relocs; rel < relend; rel++)
5265    {
5266      unsigned long r_symndx;
5267      enum elf_ppc64_reloc_type r_type;
5268      struct elf_link_hash_entry *h = NULL;
5269      char tls_type = 0;
5270
5271      r_symndx = ELF64_R_SYM (rel->r_info);
5272      r_type = ELF64_R_TYPE (rel->r_info);
5273      if (r_symndx >= symtab_hdr->sh_info)
5274	{
5275	  struct ppc_link_hash_entry *eh;
5276	  struct ppc_dyn_relocs **pp;
5277	  struct ppc_dyn_relocs *p;
5278
5279	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5280	  while (h->root.type == bfd_link_hash_indirect
5281		 || h->root.type == bfd_link_hash_warning)
5282	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
5283	  eh = (struct ppc_link_hash_entry *) h;
5284
5285	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5286	    if (p->sec == sec)
5287	      {
5288		/* Everything must go for SEC.  */
5289		*pp = p->next;
5290		break;
5291	      }
5292	}
5293
5294      switch (r_type)
5295	{
5296	case R_PPC64_GOT_TLSLD16:
5297	case R_PPC64_GOT_TLSLD16_LO:
5298	case R_PPC64_GOT_TLSLD16_HI:
5299	case R_PPC64_GOT_TLSLD16_HA:
5300	  ppc64_tlsld_got (abfd)->refcount -= 1;
5301	  tls_type = TLS_TLS | TLS_LD;
5302	  goto dogot;
5303
5304	case R_PPC64_GOT_TLSGD16:
5305	case R_PPC64_GOT_TLSGD16_LO:
5306	case R_PPC64_GOT_TLSGD16_HI:
5307	case R_PPC64_GOT_TLSGD16_HA:
5308	  tls_type = TLS_TLS | TLS_GD;
5309	  goto dogot;
5310
5311	case R_PPC64_GOT_TPREL16_DS:
5312	case R_PPC64_GOT_TPREL16_LO_DS:
5313	case R_PPC64_GOT_TPREL16_HI:
5314	case R_PPC64_GOT_TPREL16_HA:
5315	  tls_type = TLS_TLS | TLS_TPREL;
5316	  goto dogot;
5317
5318	case R_PPC64_GOT_DTPREL16_DS:
5319	case R_PPC64_GOT_DTPREL16_LO_DS:
5320	case R_PPC64_GOT_DTPREL16_HI:
5321	case R_PPC64_GOT_DTPREL16_HA:
5322	  tls_type = TLS_TLS | TLS_DTPREL;
5323	  goto dogot;
5324
5325	case R_PPC64_GOT16:
5326	case R_PPC64_GOT16_DS:
5327	case R_PPC64_GOT16_HA:
5328	case R_PPC64_GOT16_HI:
5329	case R_PPC64_GOT16_LO:
5330	case R_PPC64_GOT16_LO_DS:
5331	dogot:
5332	  {
5333	    struct got_entry *ent;
5334
5335	    if (h != NULL)
5336	      ent = h->got.glist;
5337	    else
5338	      ent = local_got_ents[r_symndx];
5339
5340	    for (; ent != NULL; ent = ent->next)
5341	      if (ent->addend == rel->r_addend
5342		  && ent->owner == abfd
5343		  && ent->tls_type == tls_type)
5344		break;
5345	    if (ent == NULL)
5346	      abort ();
5347	    if (ent->got.refcount > 0)
5348	      ent->got.refcount -= 1;
5349	  }
5350	  break;
5351
5352	case R_PPC64_PLT16_HA:
5353	case R_PPC64_PLT16_HI:
5354	case R_PPC64_PLT16_LO:
5355	case R_PPC64_PLT32:
5356	case R_PPC64_PLT64:
5357	case R_PPC64_REL14:
5358	case R_PPC64_REL14_BRNTAKEN:
5359	case R_PPC64_REL14_BRTAKEN:
5360	case R_PPC64_REL24:
5361	  if (h != NULL)
5362	    {
5363	      struct plt_entry *ent;
5364
5365	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5366		if (ent->addend == rel->r_addend)
5367		  break;
5368	      if (ent != NULL && ent->plt.refcount > 0)
5369		ent->plt.refcount -= 1;
5370	    }
5371	  break;
5372
5373	default:
5374	  break;
5375	}
5376    }
5377  return TRUE;
5378}
5379
5380/* The maximum size of .sfpr.  */
5381#define SFPR_MAX (218*4)
5382
5383struct sfpr_def_parms
5384{
5385  const char name[12];
5386  unsigned char lo, hi;
5387  bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5388  bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5389};
5390
5391/* Auto-generate _save*, _rest* functions in .sfpr.  */
5392
5393static unsigned int
5394sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5395{
5396  struct ppc_link_hash_table *htab = ppc_hash_table (info);
5397  unsigned int i;
5398  size_t len = strlen (parm->name);
5399  bfd_boolean writing = FALSE;
5400  char sym[16];
5401
5402  memcpy (sym, parm->name, len);
5403  sym[len + 2] = 0;
5404
5405  for (i = parm->lo; i <= parm->hi; i++)
5406    {
5407      struct elf_link_hash_entry *h;
5408
5409      sym[len + 0] = i / 10 + '0';
5410      sym[len + 1] = i % 10 + '0';
5411      h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5412      if (h != NULL
5413	  && !h->def_regular)
5414	{
5415	  h->root.type = bfd_link_hash_defined;
5416	  h->root.u.def.section = htab->sfpr;
5417	  h->root.u.def.value = htab->sfpr->size;
5418	  h->type = STT_FUNC;
5419	  h->def_regular = 1;
5420	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5421	  writing = TRUE;
5422	  if (htab->sfpr->contents == NULL)
5423	    {
5424	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5425	      if (htab->sfpr->contents == NULL)
5426		return FALSE;
5427	    }
5428	}
5429      if (writing)
5430	{
5431	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5432	  if (i != parm->hi)
5433	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5434	  else
5435	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5436	  htab->sfpr->size = p - htab->sfpr->contents;
5437	}
5438    }
5439
5440  return TRUE;
5441}
5442
5443static bfd_byte *
5444savegpr0 (bfd *abfd, bfd_byte *p, int r)
5445{
5446  bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5447  return p + 4;
5448}
5449
5450static bfd_byte *
5451savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5452{
5453  p = savegpr0 (abfd, p, r);
5454  bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5455  p = p + 4;
5456  bfd_put_32 (abfd, BLR, p);
5457  return p + 4;
5458}
5459
5460static bfd_byte *
5461restgpr0 (bfd *abfd, bfd_byte *p, int r)
5462{
5463  bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5464  return p + 4;
5465}
5466
5467static bfd_byte *
5468restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5469{
5470  bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5471  p = p + 4;
5472  p = restgpr0 (abfd, p, r);
5473  bfd_put_32 (abfd, MTLR_R0, p);
5474  p = p + 4;
5475  if (r == 29)
5476    {
5477      p = restgpr0 (abfd, p, 30);
5478      p = restgpr0 (abfd, p, 31);
5479    }
5480  bfd_put_32 (abfd, BLR, p);
5481  return p + 4;
5482}
5483
5484static bfd_byte *
5485savegpr1 (bfd *abfd, bfd_byte *p, int r)
5486{
5487  bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5488  return p + 4;
5489}
5490
5491static bfd_byte *
5492savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5493{
5494  p = savegpr1 (abfd, p, r);
5495  bfd_put_32 (abfd, BLR, p);
5496  return p + 4;
5497}
5498
5499static bfd_byte *
5500restgpr1 (bfd *abfd, bfd_byte *p, int r)
5501{
5502  bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5503  return p + 4;
5504}
5505
5506static bfd_byte *
5507restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5508{
5509  p = restgpr1 (abfd, p, r);
5510  bfd_put_32 (abfd, BLR, p);
5511  return p + 4;
5512}
5513
5514static bfd_byte *
5515savefpr (bfd *abfd, bfd_byte *p, int r)
5516{
5517  bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5518  return p + 4;
5519}
5520
5521static bfd_byte *
5522savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5523{
5524  p = savefpr (abfd, p, r);
5525  bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5526  p = p + 4;
5527  bfd_put_32 (abfd, BLR, p);
5528  return p + 4;
5529}
5530
5531static bfd_byte *
5532restfpr (bfd *abfd, bfd_byte *p, int r)
5533{
5534  bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5535  return p + 4;
5536}
5537
5538static bfd_byte *
5539restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5540{
5541  bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5542  p = p + 4;
5543  p = restfpr (abfd, p, r);
5544  bfd_put_32 (abfd, MTLR_R0, p);
5545  p = p + 4;
5546  if (r == 29)
5547    {
5548      p = restfpr (abfd, p, 30);
5549      p = restfpr (abfd, p, 31);
5550    }
5551  bfd_put_32 (abfd, BLR, p);
5552  return p + 4;
5553}
5554
5555static bfd_byte *
5556savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5557{
5558  p = savefpr (abfd, p, r);
5559  bfd_put_32 (abfd, BLR, p);
5560  return p + 4;
5561}
5562
5563static bfd_byte *
5564restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5565{
5566  p = restfpr (abfd, p, r);
5567  bfd_put_32 (abfd, BLR, p);
5568  return p + 4;
5569}
5570
5571static bfd_byte *
5572savevr (bfd *abfd, bfd_byte *p, int r)
5573{
5574  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5575  p = p + 4;
5576  bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5577  return p + 4;
5578}
5579
5580static bfd_byte *
5581savevr_tail (bfd *abfd, bfd_byte *p, int r)
5582{
5583  p = savevr (abfd, p, r);
5584  bfd_put_32 (abfd, BLR, p);
5585  return p + 4;
5586}
5587
5588static bfd_byte *
5589restvr (bfd *abfd, bfd_byte *p, int r)
5590{
5591  bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5592  p = p + 4;
5593  bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5594  return p + 4;
5595}
5596
5597static bfd_byte *
5598restvr_tail (bfd *abfd, bfd_byte *p, int r)
5599{
5600  p = restvr (abfd, p, r);
5601  bfd_put_32 (abfd, BLR, p);
5602  return p + 4;
5603}
5604
5605/* Called via elf_link_hash_traverse to transfer dynamic linking
5606   information on function code symbol entries to their corresponding
5607   function descriptor symbol entries.  */
5608
5609static bfd_boolean
5610func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5611{
5612  struct bfd_link_info *info;
5613  struct ppc_link_hash_table *htab;
5614  struct plt_entry *ent;
5615  struct ppc_link_hash_entry *fh;
5616  struct ppc_link_hash_entry *fdh;
5617  bfd_boolean force_local;
5618
5619  fh = (struct ppc_link_hash_entry *) h;
5620  if (fh->elf.root.type == bfd_link_hash_indirect)
5621    return TRUE;
5622
5623  if (fh->elf.root.type == bfd_link_hash_warning)
5624    fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5625
5626  info = inf;
5627  htab = ppc_hash_table (info);
5628
5629  /* Resolve undefined references to dot-symbols as the value
5630     in the function descriptor, if we have one in a regular object.
5631     This is to satisfy cases like ".quad .foo".  Calls to functions
5632     in dynamic objects are handled elsewhere.  */
5633  if (fh->elf.root.type == bfd_link_hash_undefweak
5634      && fh->was_undefined
5635      && (fh->oh->elf.root.type == bfd_link_hash_defined
5636	  || fh->oh->elf.root.type == bfd_link_hash_defweak)
5637      && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5638      && opd_entry_value (fh->oh->elf.root.u.def.section,
5639			  fh->oh->elf.root.u.def.value,
5640			  &fh->elf.root.u.def.section,
5641			  &fh->elf.root.u.def.value) != (bfd_vma) -1)
5642    {
5643      fh->elf.root.type = fh->oh->elf.root.type;
5644      fh->elf.forced_local = 1;
5645    }
5646
5647  /* If this is a function code symbol, transfer dynamic linking
5648     information to the function descriptor symbol.  */
5649  if (!fh->is_func)
5650    return TRUE;
5651
5652  for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5653    if (ent->plt.refcount > 0)
5654      break;
5655  if (ent == NULL
5656      || fh->elf.root.root.string[0] != '.'
5657      || fh->elf.root.root.string[1] == '\0')
5658    return TRUE;
5659
5660  /* Find the corresponding function descriptor symbol.  Create it
5661     as undefined if necessary.  */
5662
5663  fdh = get_fdh (fh, htab);
5664  if (fdh != NULL)
5665    while (fdh->elf.root.type == bfd_link_hash_indirect
5666	   || fdh->elf.root.type == bfd_link_hash_warning)
5667      fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5668
5669  if (fdh == NULL
5670      && info->shared
5671      && (fh->elf.root.type == bfd_link_hash_undefined
5672	  || fh->elf.root.type == bfd_link_hash_undefweak))
5673    {
5674      fdh = make_fdh (info, fh);
5675      if (fdh == NULL)
5676	return FALSE;
5677    }
5678
5679  /* Fake function descriptors are made undefweak.  If the function
5680     code symbol is strong undefined, make the fake sym the same.
5681     If the function code symbol is defined, then force the fake
5682     descriptor local;  We can't support overriding of symbols in a
5683     shared library on a fake descriptor.  */
5684
5685  if (fdh != NULL
5686      && fdh->fake
5687      && fdh->elf.root.type == bfd_link_hash_undefweak)
5688    {
5689      if (fh->elf.root.type == bfd_link_hash_undefined)
5690	{
5691	  fdh->elf.root.type = bfd_link_hash_undefined;
5692	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5693	}
5694      else if (fh->elf.root.type == bfd_link_hash_defined
5695	       || fh->elf.root.type == bfd_link_hash_defweak)
5696	{
5697	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5698	}
5699    }
5700
5701  if (fdh != NULL
5702      && !fdh->elf.forced_local
5703      && (info->shared
5704	  || fdh->elf.def_dynamic
5705	  || fdh->elf.ref_dynamic
5706	  || (fdh->elf.root.type == bfd_link_hash_undefweak
5707	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5708    {
5709      if (fdh->elf.dynindx == -1)
5710	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5711	  return FALSE;
5712      fdh->elf.ref_regular |= fh->elf.ref_regular;
5713      fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5714      fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5715      fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5716      if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5717	{
5718	  move_plt_plist (fh, fdh);
5719	  fdh->elf.needs_plt = 1;
5720	}
5721      fdh->is_func_descriptor = 1;
5722      fdh->oh = fh;
5723      fh->oh = fdh;
5724    }
5725
5726  /* Now that the info is on the function descriptor, clear the
5727     function code sym info.  Any function code syms for which we
5728     don't have a definition in a regular file, we force local.
5729     This prevents a shared library from exporting syms that have
5730     been imported from another library.  Function code syms that
5731     are really in the library we must leave global to prevent the
5732     linker dragging in a definition from a static library.  */
5733  force_local = (!fh->elf.def_regular
5734		 || fdh == NULL
5735		 || !fdh->elf.def_regular
5736		 || fdh->elf.forced_local);
5737  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5738
5739  return TRUE;
5740}
5741
5742/* Called near the start of bfd_elf_size_dynamic_sections.  We use
5743   this hook to a) provide some gcc support functions, and b) transfer
5744   dynamic linking information gathered so far on function code symbol
5745   entries, to their corresponding function descriptor symbol entries.  */
5746
5747static bfd_boolean
5748ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5749			    struct bfd_link_info *info)
5750{
5751  struct ppc_link_hash_table *htab;
5752  unsigned int i;
5753  const struct sfpr_def_parms funcs[] =
5754    {
5755      { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5756      { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5757      { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5758      { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5759      { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5760      { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5761      { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5762      { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5763      { "._savef", 14, 31, savefpr, savefpr1_tail },
5764      { "._restf", 14, 31, restfpr, restfpr1_tail },
5765      { "_savevr_", 20, 31, savevr, savevr_tail },
5766      { "_restvr_", 20, 31, restvr, restvr_tail }
5767    };
5768
5769  htab = ppc_hash_table (info);
5770  if (htab->sfpr == NULL)
5771    /* We don't have any relocs.  */
5772    return TRUE;
5773
5774  /* Provide any missing _save* and _rest* functions.  */
5775  htab->sfpr->size = 0;
5776  for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5777    if (!sfpr_define (info, &funcs[i]))
5778      return FALSE;
5779
5780  elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5781
5782  if (htab->sfpr->size == 0)
5783    htab->sfpr->flags |= SEC_EXCLUDE;
5784
5785  return TRUE;
5786}
5787
5788/* Adjust a symbol defined by a dynamic object and referenced by a
5789   regular object.  The current definition is in some section of the
5790   dynamic object, but we're not including those sections.  We have to
5791   change the definition to something the rest of the link can
5792   understand.  */
5793
5794static bfd_boolean
5795ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5796				 struct elf_link_hash_entry *h)
5797{
5798  struct ppc_link_hash_table *htab;
5799  asection *s;
5800
5801  htab = ppc_hash_table (info);
5802
5803  /* Deal with function syms.  */
5804  if (h->type == STT_FUNC
5805      || h->needs_plt)
5806    {
5807      /* Clear procedure linkage table information for any symbol that
5808	 won't need a .plt entry.  */
5809      struct plt_entry *ent;
5810      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5811	if (ent->plt.refcount > 0)
5812	  break;
5813      if (ent == NULL
5814	  || SYMBOL_CALLS_LOCAL (info, h)
5815	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5816	      && h->root.type == bfd_link_hash_undefweak))
5817	{
5818	  h->plt.plist = NULL;
5819	  h->needs_plt = 0;
5820	}
5821    }
5822  else
5823    h->plt.plist = NULL;
5824
5825  /* If this is a weak symbol, and there is a real definition, the
5826     processor independent code will have arranged for us to see the
5827     real definition first, and we can just use the same value.  */
5828  if (h->u.weakdef != NULL)
5829    {
5830      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5831		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
5832      h->root.u.def.section = h->u.weakdef->root.u.def.section;
5833      h->root.u.def.value = h->u.weakdef->root.u.def.value;
5834      if (ELIMINATE_COPY_RELOCS)
5835	h->non_got_ref = h->u.weakdef->non_got_ref;
5836      return TRUE;
5837    }
5838
5839  /* If we are creating a shared library, we must presume that the
5840     only references to the symbol are via the global offset table.
5841     For such cases we need not do anything here; the relocations will
5842     be handled correctly by relocate_section.  */
5843  if (info->shared)
5844    return TRUE;
5845
5846  /* If there are no references to this symbol that do not use the
5847     GOT, we don't need to generate a copy reloc.  */
5848  if (!h->non_got_ref)
5849    return TRUE;
5850
5851  /* Don't generate a copy reloc for symbols defined in the executable.  */
5852  if (!h->def_dynamic || !h->ref_regular || h->def_regular)
5853    return TRUE;
5854
5855  if (ELIMINATE_COPY_RELOCS)
5856    {
5857      struct ppc_link_hash_entry * eh;
5858      struct ppc_dyn_relocs *p;
5859
5860      eh = (struct ppc_link_hash_entry *) h;
5861      for (p = eh->dyn_relocs; p != NULL; p = p->next)
5862	{
5863	  s = p->sec->output_section;
5864	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
5865	    break;
5866	}
5867
5868      /* If we didn't find any dynamic relocs in read-only sections, then
5869	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
5870      if (p == NULL)
5871	{
5872	  h->non_got_ref = 0;
5873	  return TRUE;
5874	}
5875    }
5876
5877  if (h->plt.plist != NULL)
5878    {
5879      /* We should never get here, but unfortunately there are versions
5880	 of gcc out there that improperly (for this ABI) put initialized
5881	 function pointers, vtable refs and suchlike in read-only
5882	 sections.  Allow them to proceed, but warn that this might
5883	 break at runtime.  */
5884      (*_bfd_error_handler)
5885	(_("copy reloc against `%s' requires lazy plt linking; "
5886	   "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5887	 h->root.root.string);
5888    }
5889
5890  /* This is a reference to a symbol defined by a dynamic object which
5891     is not a function.  */
5892
5893  if (h->size == 0)
5894    {
5895      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5896			     h->root.root.string);
5897      return TRUE;
5898    }
5899
5900  /* We must allocate the symbol in our .dynbss section, which will
5901     become part of the .bss section of the executable.  There will be
5902     an entry for this symbol in the .dynsym section.  The dynamic
5903     object will contain position independent code, so all references
5904     from the dynamic object to this symbol will go through the global
5905     offset table.  The dynamic linker will use the .dynsym entry to
5906     determine the address it must put in the global offset table, so
5907     both the dynamic object and the regular object will refer to the
5908     same memory location for the variable.  */
5909
5910  /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5911     to copy the initial value out of the dynamic object and into the
5912     runtime process image.  We need to remember the offset into the
5913     .rela.bss section we are going to use.  */
5914  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5915    {
5916      htab->relbss->size += sizeof (Elf64_External_Rela);
5917      h->needs_copy = 1;
5918    }
5919
5920  s = htab->dynbss;
5921
5922  return _bfd_elf_adjust_dynamic_copy (h, s);
5923}
5924
5925/* If given a function descriptor symbol, hide both the function code
5926   sym and the descriptor.  */
5927static void
5928ppc64_elf_hide_symbol (struct bfd_link_info *info,
5929		       struct elf_link_hash_entry *h,
5930		       bfd_boolean force_local)
5931{
5932  struct ppc_link_hash_entry *eh;
5933  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5934
5935  eh = (struct ppc_link_hash_entry *) h;
5936  if (eh->is_func_descriptor)
5937    {
5938      struct ppc_link_hash_entry *fh = eh->oh;
5939
5940      if (fh == NULL)
5941	{
5942	  const char *p, *q;
5943	  struct ppc_link_hash_table *htab;
5944	  char save;
5945
5946	  /* We aren't supposed to use alloca in BFD because on
5947	     systems which do not have alloca the version in libiberty
5948	     calls xmalloc, which might cause the program to crash
5949	     when it runs out of memory.  This function doesn't have a
5950	     return status, so there's no way to gracefully return an
5951	     error.  So cheat.  We know that string[-1] can be safely
5952	     accessed;  It's either a string in an ELF string table,
5953	     or allocated in an objalloc structure.  */
5954
5955	  p = eh->elf.root.root.string - 1;
5956	  save = *p;
5957	  *(char *) p = '.';
5958	  htab = ppc_hash_table (info);
5959	  fh = (struct ppc_link_hash_entry *)
5960	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5961	  *(char *) p = save;
5962
5963	  /* Unfortunately, if it so happens that the string we were
5964	     looking for was allocated immediately before this string,
5965	     then we overwrote the string terminator.  That's the only
5966	     reason the lookup should fail.  */
5967	  if (fh == NULL)
5968	    {
5969	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5970	      while (q >= eh->elf.root.root.string && *q == *p)
5971		--q, --p;
5972	      if (q < eh->elf.root.root.string && *p == '.')
5973		fh = (struct ppc_link_hash_entry *)
5974		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5975	    }
5976	  if (fh != NULL)
5977	    {
5978	      eh->oh = fh;
5979	      fh->oh = eh;
5980	    }
5981	}
5982      if (fh != NULL)
5983	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5984    }
5985}
5986
5987static bfd_boolean
5988get_sym_h (struct elf_link_hash_entry **hp,
5989	   Elf_Internal_Sym **symp,
5990	   asection **symsecp,
5991	   char **tls_maskp,
5992	   Elf_Internal_Sym **locsymsp,
5993	   unsigned long r_symndx,
5994	   bfd *ibfd)
5995{
5996  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5997
5998  if (r_symndx >= symtab_hdr->sh_info)
5999    {
6000      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6001      struct elf_link_hash_entry *h;
6002
6003      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6004      while (h->root.type == bfd_link_hash_indirect
6005	     || h->root.type == bfd_link_hash_warning)
6006	h = (struct elf_link_hash_entry *) h->root.u.i.link;
6007
6008      if (hp != NULL)
6009	*hp = h;
6010
6011      if (symp != NULL)
6012	*symp = NULL;
6013
6014      if (symsecp != NULL)
6015	{
6016	  asection *symsec = NULL;
6017	  if (h->root.type == bfd_link_hash_defined
6018	      || h->root.type == bfd_link_hash_defweak)
6019	    symsec = h->root.u.def.section;
6020	  *symsecp = symsec;
6021	}
6022
6023      if (tls_maskp != NULL)
6024	{
6025	  struct ppc_link_hash_entry *eh;
6026
6027	  eh = (struct ppc_link_hash_entry *) h;
6028	  *tls_maskp = &eh->tls_mask;
6029	}
6030    }
6031  else
6032    {
6033      Elf_Internal_Sym *sym;
6034      Elf_Internal_Sym *locsyms = *locsymsp;
6035
6036      if (locsyms == NULL)
6037	{
6038	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6039	  if (locsyms == NULL)
6040	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6041					    symtab_hdr->sh_info,
6042					    0, NULL, NULL, NULL);
6043	  if (locsyms == NULL)
6044	    return FALSE;
6045	  *locsymsp = locsyms;
6046	}
6047      sym = locsyms + r_symndx;
6048
6049      if (hp != NULL)
6050	*hp = NULL;
6051
6052      if (symp != NULL)
6053	*symp = sym;
6054
6055      if (symsecp != NULL)
6056	{
6057	  asection *symsec = NULL;
6058	  if ((sym->st_shndx != SHN_UNDEF
6059	       && sym->st_shndx < SHN_LORESERVE)
6060	      || sym->st_shndx > SHN_HIRESERVE)
6061	    symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6062	  *symsecp = symsec;
6063	}
6064
6065      if (tls_maskp != NULL)
6066	{
6067	  struct got_entry **lgot_ents;
6068	  char *tls_mask;
6069
6070	  tls_mask = NULL;
6071	  lgot_ents = elf_local_got_ents (ibfd);
6072	  if (lgot_ents != NULL)
6073	    {
6074	      char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
6075	      tls_mask = &lgot_masks[r_symndx];
6076	    }
6077	  *tls_maskp = tls_mask;
6078	}
6079    }
6080  return TRUE;
6081}
6082
6083/* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6084   error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6085   type suitable for optimization, and 1 otherwise.  */
6086
6087static int
6088get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6089	      Elf_Internal_Sym **locsymsp,
6090	      const Elf_Internal_Rela *rel, bfd *ibfd)
6091{
6092  unsigned long r_symndx;
6093  int next_r;
6094  struct elf_link_hash_entry *h;
6095  Elf_Internal_Sym *sym;
6096  asection *sec;
6097  bfd_vma off;
6098
6099  r_symndx = ELF64_R_SYM (rel->r_info);
6100  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6101    return 0;
6102
6103  if ((*tls_maskp != NULL && **tls_maskp != 0)
6104      || sec == NULL
6105      || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6106    return 1;
6107
6108  /* Look inside a TOC section too.  */
6109  if (h != NULL)
6110    {
6111      BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6112      off = h->root.u.def.value;
6113    }
6114  else
6115    off = sym->st_value;
6116  off += rel->r_addend;
6117  BFD_ASSERT (off % 8 == 0);
6118  r_symndx = ppc64_elf_section_data (sec)->u.t_symndx[off / 8];
6119  next_r = ppc64_elf_section_data (sec)->u.t_symndx[off / 8 + 1];
6120  if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6121    return 0;
6122  if (toc_symndx != NULL)
6123    *toc_symndx = r_symndx;
6124  if ((h == NULL
6125       || ((h->root.type == bfd_link_hash_defined
6126	    || h->root.type == bfd_link_hash_defweak)
6127	   && !h->def_dynamic))
6128      && (next_r == -1 || next_r == -2))
6129    return 1 - next_r;
6130  return 1;
6131}
6132
6133/* Adjust all global syms defined in opd sections.  In gcc generated
6134   code for the old ABI, these will already have been done.  */
6135
6136static bfd_boolean
6137adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6138{
6139  struct ppc_link_hash_entry *eh;
6140  asection *sym_sec;
6141  long *opd_adjust;
6142
6143  if (h->root.type == bfd_link_hash_indirect)
6144    return TRUE;
6145
6146  if (h->root.type == bfd_link_hash_warning)
6147    h = (struct elf_link_hash_entry *) h->root.u.i.link;
6148
6149  if (h->root.type != bfd_link_hash_defined
6150      && h->root.type != bfd_link_hash_defweak)
6151    return TRUE;
6152
6153  eh = (struct ppc_link_hash_entry *) h;
6154  if (eh->adjust_done)
6155    return TRUE;
6156
6157  sym_sec = eh->elf.root.u.def.section;
6158  opd_adjust = get_opd_info (sym_sec);
6159  if (opd_adjust != NULL)
6160    {
6161      long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6162      if (adjust == -1)
6163	{
6164	  /* This entry has been deleted.  */
6165	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6166	  if (dsec == NULL)
6167	    {
6168	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6169		if (elf_discarded_section (dsec))
6170		  {
6171		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6172		    break;
6173		  }
6174	    }
6175	  eh->elf.root.u.def.value = 0;
6176	  eh->elf.root.u.def.section = dsec;
6177	}
6178      else
6179	eh->elf.root.u.def.value += adjust;
6180      eh->adjust_done = 1;
6181    }
6182  return TRUE;
6183}
6184
6185/* Handles decrementing dynamic reloc counts for the reloc specified by
6186   R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
6187   have already been determined.  */
6188
6189static bfd_boolean
6190dec_dynrel_count (bfd_vma r_info,
6191		  asection *sec,
6192		  struct bfd_link_info *info,
6193		  Elf_Internal_Sym **local_syms,
6194		  struct elf_link_hash_entry *h,
6195		  asection *sym_sec)
6196{
6197  enum elf_ppc64_reloc_type r_type;
6198  struct ppc_dyn_relocs *p;
6199  struct ppc_dyn_relocs **pp;
6200
6201  /* Can this reloc be dynamic?  This switch, and later tests here
6202     should be kept in sync with the code in check_relocs.  */
6203  r_type = ELF64_R_TYPE (r_info);
6204  switch (r_type)
6205    {
6206    default:
6207      return TRUE;
6208
6209    case R_PPC64_TPREL16:
6210    case R_PPC64_TPREL16_LO:
6211    case R_PPC64_TPREL16_HI:
6212    case R_PPC64_TPREL16_HA:
6213    case R_PPC64_TPREL16_DS:
6214    case R_PPC64_TPREL16_LO_DS:
6215    case R_PPC64_TPREL16_HIGHER:
6216    case R_PPC64_TPREL16_HIGHERA:
6217    case R_PPC64_TPREL16_HIGHEST:
6218    case R_PPC64_TPREL16_HIGHESTA:
6219      if (!info->shared)
6220	return TRUE;
6221
6222    case R_PPC64_TPREL64:
6223    case R_PPC64_DTPMOD64:
6224    case R_PPC64_DTPREL64:
6225    case R_PPC64_ADDR64:
6226    case R_PPC64_REL30:
6227    case R_PPC64_REL32:
6228    case R_PPC64_REL64:
6229    case R_PPC64_ADDR14:
6230    case R_PPC64_ADDR14_BRNTAKEN:
6231    case R_PPC64_ADDR14_BRTAKEN:
6232    case R_PPC64_ADDR16:
6233    case R_PPC64_ADDR16_DS:
6234    case R_PPC64_ADDR16_HA:
6235    case R_PPC64_ADDR16_HI:
6236    case R_PPC64_ADDR16_HIGHER:
6237    case R_PPC64_ADDR16_HIGHERA:
6238    case R_PPC64_ADDR16_HIGHEST:
6239    case R_PPC64_ADDR16_HIGHESTA:
6240    case R_PPC64_ADDR16_LO:
6241    case R_PPC64_ADDR16_LO_DS:
6242    case R_PPC64_ADDR24:
6243    case R_PPC64_ADDR32:
6244    case R_PPC64_UADDR16:
6245    case R_PPC64_UADDR32:
6246    case R_PPC64_UADDR64:
6247    case R_PPC64_TOC:
6248      break;
6249    }
6250
6251  if (local_syms != NULL)
6252    {
6253      unsigned long r_symndx;
6254      Elf_Internal_Sym *sym;
6255      bfd *ibfd = sec->owner;
6256
6257      r_symndx = ELF64_R_SYM (r_info);
6258      if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6259	return FALSE;
6260    }
6261
6262  if ((info->shared
6263       && (MUST_BE_DYN_RELOC (r_type)
6264	   || (h != NULL
6265	       && (!info->symbolic
6266		   || h->root.type == bfd_link_hash_defweak
6267		   || !h->def_regular))))
6268      || (ELIMINATE_COPY_RELOCS
6269	  && !info->shared
6270	  && h != NULL
6271	  && (h->root.type == bfd_link_hash_defweak
6272	      || !h->def_regular)))
6273    ;
6274  else
6275    return TRUE;
6276
6277  if (h != NULL)
6278    pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6279  else
6280    {
6281      if (sym_sec != NULL)
6282	{
6283	  void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6284	  pp = (struct ppc_dyn_relocs **) vpp;
6285	}
6286      else
6287	{
6288	  void *vpp = &elf_section_data (sec)->local_dynrel;
6289	  pp = (struct ppc_dyn_relocs **) vpp;
6290	}
6291
6292      /* elf_gc_sweep may have already removed all dyn relocs associated
6293	 with local syms for a given section.  Don't report a dynreloc
6294	 miscount.  */
6295      if (*pp == NULL)
6296	return TRUE;
6297    }
6298
6299  while ((p = *pp) != NULL)
6300    {
6301      if (p->sec == sec)
6302	{
6303	  if (!MUST_BE_DYN_RELOC (r_type))
6304	    p->pc_count -= 1;
6305	  p->count -= 1;
6306	  if (p->count == 0)
6307	    *pp = p->next;
6308	  return TRUE;
6309	}
6310      pp = &p->next;
6311    }
6312
6313  (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6314			   sec->owner, sec);
6315  bfd_set_error (bfd_error_bad_value);
6316  return FALSE;
6317}
6318
6319/* Remove unused Official Procedure Descriptor entries.  Currently we
6320   only remove those associated with functions in discarded link-once
6321   sections, or weakly defined functions that have been overridden.  It
6322   would be possible to remove many more entries for statically linked
6323   applications.  */
6324
6325bfd_boolean
6326ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6327		    bfd_boolean no_opd_opt,
6328		    bfd_boolean non_overlapping)
6329{
6330  bfd *ibfd;
6331  bfd_boolean some_edited = FALSE;
6332  asection *need_pad = NULL;
6333
6334  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6335    {
6336      asection *sec;
6337      Elf_Internal_Rela *relstart, *rel, *relend;
6338      Elf_Internal_Shdr *symtab_hdr;
6339      Elf_Internal_Sym *local_syms;
6340      struct elf_link_hash_entry **sym_hashes;
6341      bfd_vma offset;
6342      bfd_size_type amt;
6343      long *opd_adjust;
6344      bfd_boolean need_edit, add_aux_fields;
6345      bfd_size_type cnt_16b = 0;
6346
6347      sec = bfd_get_section_by_name (ibfd, ".opd");
6348      if (sec == NULL || sec->size == 0)
6349	continue;
6350
6351      amt = sec->size * sizeof (long) / 8;
6352      opd_adjust = get_opd_info (sec);
6353      if (opd_adjust == NULL)
6354	{
6355	  /* check_relocs hasn't been called.  Must be a ld -r link
6356	     or --just-symbols object.   */
6357	  opd_adjust = bfd_alloc (obfd, amt);
6358	  if (opd_adjust == NULL)
6359	    return FALSE;
6360	  ppc64_elf_section_data (sec)->u.opd_adjust = opd_adjust;
6361	  BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
6362	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
6363	}
6364      memset (opd_adjust, 0, amt);
6365
6366      if (no_opd_opt)
6367	continue;
6368
6369      if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6370	continue;
6371
6372      if (sec->output_section == bfd_abs_section_ptr)
6373	continue;
6374
6375      /* Look through the section relocs.  */
6376      if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6377	continue;
6378
6379      local_syms = NULL;
6380      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6381      sym_hashes = elf_sym_hashes (ibfd);
6382
6383      /* Read the relocations.  */
6384      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6385					    info->keep_memory);
6386      if (relstart == NULL)
6387	return FALSE;
6388
6389      /* First run through the relocs to check they are sane, and to
6390	 determine whether we need to edit this opd section.  */
6391      need_edit = FALSE;
6392      need_pad = sec;
6393      offset = 0;
6394      relend = relstart + sec->reloc_count;
6395      for (rel = relstart; rel < relend; )
6396	{
6397	  enum elf_ppc64_reloc_type r_type;
6398	  unsigned long r_symndx;
6399	  asection *sym_sec;
6400	  struct elf_link_hash_entry *h;
6401	  Elf_Internal_Sym *sym;
6402
6403	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
6404	     only interested in the reloc pointing to a function entry
6405	     point.  */
6406	  if (rel->r_offset != offset
6407	      || rel + 1 >= relend
6408	      || (rel + 1)->r_offset != offset + 8)
6409	    {
6410	      /* If someone messes with .opd alignment then after a
6411		 "ld -r" we might have padding in the middle of .opd.
6412		 Also, there's nothing to prevent someone putting
6413		 something silly in .opd with the assembler.  No .opd
6414		 optimization for them!  */
6415	    broken_opd:
6416	      (*_bfd_error_handler)
6417		(_("%B: .opd is not a regular array of opd entries"), ibfd);
6418	      need_edit = FALSE;
6419	      break;
6420	    }
6421
6422	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6423	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6424	    {
6425	      (*_bfd_error_handler)
6426		(_("%B: unexpected reloc type %u in .opd section"),
6427		 ibfd, r_type);
6428	      need_edit = FALSE;
6429	      break;
6430	    }
6431
6432	  r_symndx = ELF64_R_SYM (rel->r_info);
6433	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6434			  r_symndx, ibfd))
6435	    goto error_ret;
6436
6437	  if (sym_sec == NULL || sym_sec->owner == NULL)
6438	    {
6439	      const char *sym_name;
6440	      if (h != NULL)
6441		sym_name = h->root.root.string;
6442	      else
6443		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6444					     sym_sec);
6445
6446	      (*_bfd_error_handler)
6447		(_("%B: undefined sym `%s' in .opd section"),
6448		 ibfd, sym_name);
6449	      need_edit = FALSE;
6450	      break;
6451	    }
6452
6453	  /* opd entries are always for functions defined in the
6454	     current input bfd.  If the symbol isn't defined in the
6455	     input bfd, then we won't be using the function in this
6456	     bfd;  It must be defined in a linkonce section in another
6457	     bfd, or is weak.  It's also possible that we are
6458	     discarding the function due to a linker script /DISCARD/,
6459	     which we test for via the output_section.  */
6460	  if (sym_sec->owner != ibfd
6461	      || sym_sec->output_section == bfd_abs_section_ptr)
6462	    need_edit = TRUE;
6463
6464	  rel += 2;
6465	  if (rel == relend
6466	      || (rel + 1 == relend && rel->r_offset == offset + 16))
6467	    {
6468	      if (sec->size == offset + 24)
6469		{
6470		  need_pad = NULL;
6471		  break;
6472		}
6473	      if (rel == relend && sec->size == offset + 16)
6474		{
6475		  cnt_16b++;
6476		  break;
6477		}
6478	      goto broken_opd;
6479	    }
6480
6481	  if (rel->r_offset == offset + 24)
6482	    offset += 24;
6483	  else if (rel->r_offset != offset + 16)
6484	    goto broken_opd;
6485	  else if (rel + 1 < relend
6486		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6487		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6488	    {
6489	      offset += 16;
6490	      cnt_16b++;
6491	    }
6492	  else if (rel + 2 < relend
6493		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6494		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6495	    {
6496	      offset += 24;
6497	      rel += 1;
6498	    }
6499	  else
6500	    goto broken_opd;
6501	}
6502
6503      add_aux_fields = non_overlapping && cnt_16b > 0;
6504
6505      if (need_edit || add_aux_fields)
6506	{
6507	  Elf_Internal_Rela *write_rel;
6508	  bfd_byte *rptr, *wptr;
6509	  bfd_byte *new_contents = NULL;
6510	  bfd_boolean skip;
6511	  long opd_ent_size;
6512
6513	  /* This seems a waste of time as input .opd sections are all
6514	     zeros as generated by gcc, but I suppose there's no reason
6515	     this will always be so.  We might start putting something in
6516	     the third word of .opd entries.  */
6517	  if ((sec->flags & SEC_IN_MEMORY) == 0)
6518	    {
6519	      bfd_byte *loc;
6520	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6521		{
6522		  if (loc != NULL)
6523		    free (loc);
6524		error_ret:
6525		  if (local_syms != NULL
6526		      && symtab_hdr->contents != (unsigned char *) local_syms)
6527		    free (local_syms);
6528		  if (elf_section_data (sec)->relocs != relstart)
6529		    free (relstart);
6530		  return FALSE;
6531		}
6532	      sec->contents = loc;
6533	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6534	    }
6535
6536	  elf_section_data (sec)->relocs = relstart;
6537
6538	  new_contents = sec->contents;
6539	  if (add_aux_fields)
6540	    {
6541	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6542	      if (new_contents == NULL)
6543		return FALSE;
6544	      need_pad = FALSE;
6545	    }
6546	  wptr = new_contents;
6547	  rptr = sec->contents;
6548
6549	  write_rel = relstart;
6550	  skip = FALSE;
6551	  offset = 0;
6552	  opd_ent_size = 0;
6553	  for (rel = relstart; rel < relend; rel++)
6554	    {
6555	      unsigned long r_symndx;
6556	      asection *sym_sec;
6557	      struct elf_link_hash_entry *h;
6558	      Elf_Internal_Sym *sym;
6559
6560	      r_symndx = ELF64_R_SYM (rel->r_info);
6561	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6562			      r_symndx, ibfd))
6563		goto error_ret;
6564
6565	      if (rel->r_offset == offset)
6566		{
6567		  struct ppc_link_hash_entry *fdh = NULL;
6568
6569		  /* See if the .opd entry is full 24 byte or
6570		     16 byte (with fd_aux entry overlapped with next
6571		     fd_func).  */
6572		  opd_ent_size = 24;
6573		  if ((rel + 2 == relend && sec->size == offset + 16)
6574		      || (rel + 3 < relend
6575			  && rel[2].r_offset == offset + 16
6576			  && rel[3].r_offset == offset + 24
6577			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6578			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6579		    opd_ent_size = 16;
6580
6581		  if (h != NULL
6582		      && h->root.root.string[0] == '.')
6583		    {
6584		      fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6585				     ppc_hash_table (info));
6586		      if (fdh != NULL
6587			  && fdh->elf.root.type != bfd_link_hash_defined
6588			  && fdh->elf.root.type != bfd_link_hash_defweak)
6589			fdh = NULL;
6590		    }
6591
6592		  skip = (sym_sec->owner != ibfd
6593			  || sym_sec->output_section == bfd_abs_section_ptr);
6594		  if (skip)
6595		    {
6596		      if (fdh != NULL && sym_sec->owner == ibfd)
6597			{
6598			  /* Arrange for the function descriptor sym
6599			     to be dropped.  */
6600			  fdh->elf.root.u.def.value = 0;
6601			  fdh->elf.root.u.def.section = sym_sec;
6602			}
6603		      opd_adjust[rel->r_offset / 8] = -1;
6604		    }
6605		  else
6606		    {
6607		      /* We'll be keeping this opd entry.  */
6608
6609		      if (fdh != NULL)
6610			{
6611			  /* Redefine the function descriptor symbol to
6612			     this location in the opd section.  It is
6613			     necessary to update the value here rather
6614			     than using an array of adjustments as we do
6615			     for local symbols, because various places
6616			     in the generic ELF code use the value
6617			     stored in u.def.value.  */
6618			  fdh->elf.root.u.def.value = wptr - new_contents;
6619			  fdh->adjust_done = 1;
6620			}
6621
6622		      /* Local syms are a bit tricky.  We could
6623			 tweak them as they can be cached, but
6624			 we'd need to look through the local syms
6625			 for the function descriptor sym which we
6626			 don't have at the moment.  So keep an
6627			 array of adjustments.  */
6628		      opd_adjust[rel->r_offset / 8]
6629			= (wptr - new_contents) - (rptr - sec->contents);
6630
6631		      if (wptr != rptr)
6632			memcpy (wptr, rptr, opd_ent_size);
6633		      wptr += opd_ent_size;
6634		      if (add_aux_fields && opd_ent_size == 16)
6635			{
6636			  memset (wptr, '\0', 8);
6637			  wptr += 8;
6638			}
6639		    }
6640		  rptr += opd_ent_size;
6641		  offset += opd_ent_size;
6642		}
6643
6644	      if (skip)
6645		{
6646		  if (!NO_OPD_RELOCS
6647		      && !info->relocatable
6648		      && !dec_dynrel_count (rel->r_info, sec, info,
6649					    NULL, h, sym_sec))
6650		    goto error_ret;
6651		}
6652	      else
6653		{
6654		  /* We need to adjust any reloc offsets to point to the
6655		     new opd entries.  While we're at it, we may as well
6656		     remove redundant relocs.  */
6657		  rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6658		  if (write_rel != rel)
6659		    memcpy (write_rel, rel, sizeof (*rel));
6660		  ++write_rel;
6661		}
6662	    }
6663
6664	  sec->size = wptr - new_contents;
6665	  sec->reloc_count = write_rel - relstart;
6666	  if (add_aux_fields)
6667	    {
6668	      free (sec->contents);
6669	      sec->contents = new_contents;
6670	    }
6671
6672	  /* Fudge the header size too, as this is used later in
6673	     elf_bfd_final_link if we are emitting relocs.  */
6674	  elf_section_data (sec)->rel_hdr.sh_size
6675	    = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6676	  BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6677	  some_edited = TRUE;
6678	}
6679      else if (elf_section_data (sec)->relocs != relstart)
6680	free (relstart);
6681
6682      if (local_syms != NULL
6683	  && symtab_hdr->contents != (unsigned char *) local_syms)
6684	{
6685	  if (!info->keep_memory)
6686	    free (local_syms);
6687	  else
6688	    symtab_hdr->contents = (unsigned char *) local_syms;
6689	}
6690    }
6691
6692  if (some_edited)
6693    elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6694
6695  /* If we are doing a final link and the last .opd entry is just 16 byte
6696     long, add a 8 byte padding after it.  */
6697  if (need_pad != NULL && !info->relocatable)
6698    {
6699      bfd_byte *p;
6700
6701      if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6702	{
6703	  BFD_ASSERT (need_pad->size > 0);
6704
6705	  p = bfd_malloc (need_pad->size + 8);
6706	  if (p == NULL)
6707	    return FALSE;
6708
6709	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
6710					  p, 0, need_pad->size))
6711	    return FALSE;
6712
6713	  need_pad->contents = p;
6714	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6715	}
6716      else
6717	{
6718	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6719	  if (p == NULL)
6720	    return FALSE;
6721
6722	  need_pad->contents = p;
6723	}
6724
6725      memset (need_pad->contents + need_pad->size, 0, 8);
6726      need_pad->size += 8;
6727    }
6728
6729  return TRUE;
6730}
6731
6732/* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
6733
6734asection *
6735ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6736{
6737  struct ppc_link_hash_table *htab;
6738
6739  htab = ppc_hash_table (info);
6740  if (htab->tls_get_addr != NULL)
6741    {
6742      struct ppc_link_hash_entry *h = htab->tls_get_addr;
6743
6744      while (h->elf.root.type == bfd_link_hash_indirect
6745	     || h->elf.root.type == bfd_link_hash_warning)
6746	h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6747
6748      htab->tls_get_addr = h;
6749
6750      if (htab->tls_get_addr_fd == NULL
6751	  && h->oh != NULL
6752	  && h->oh->is_func_descriptor
6753	  && (h->oh->elf.root.type == bfd_link_hash_defined
6754	      || h->oh->elf.root.type == bfd_link_hash_defweak))
6755	htab->tls_get_addr_fd = h->oh;
6756    }
6757
6758  if (htab->tls_get_addr_fd != NULL)
6759    {
6760      struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6761
6762      while (h->elf.root.type == bfd_link_hash_indirect
6763	     || h->elf.root.type == bfd_link_hash_warning)
6764	h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6765
6766      htab->tls_get_addr_fd = h;
6767    }
6768
6769  return _bfd_elf_tls_setup (obfd, info);
6770}
6771
6772/* Run through all the TLS relocs looking for optimization
6773   opportunities.  The linker has been hacked (see ppc64elf.em) to do
6774   a preliminary section layout so that we know the TLS segment
6775   offsets.  We can't optimize earlier because some optimizations need
6776   to know the tp offset, and we need to optimize before allocating
6777   dynamic relocations.  */
6778
6779bfd_boolean
6780ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6781{
6782  bfd *ibfd;
6783  asection *sec;
6784  struct ppc_link_hash_table *htab;
6785
6786  if (info->relocatable || info->shared)
6787    return TRUE;
6788
6789  htab = ppc_hash_table (info);
6790  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6791    {
6792      Elf_Internal_Sym *locsyms = NULL;
6793      asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6794      unsigned char *toc_ref = NULL;
6795
6796      /* Look at all the sections for this file, with TOC last.  */
6797      for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6798		  : ibfd->sections);
6799	   sec != NULL;
6800	   sec = (sec == toc ? NULL
6801		  : sec->next == NULL ? toc
6802		  : sec->next == toc && toc->next ? toc->next
6803		  : sec->next))
6804	if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6805	  {
6806	    Elf_Internal_Rela *relstart, *rel, *relend;
6807	    int expecting_tls_get_addr;
6808	    long toc_ref_index = 0;
6809
6810	    /* Read the relocations.  */
6811	    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6812						  info->keep_memory);
6813	    if (relstart == NULL)
6814	      return FALSE;
6815
6816	    expecting_tls_get_addr = 0;
6817	    relend = relstart + sec->reloc_count;
6818	    for (rel = relstart; rel < relend; rel++)
6819	      {
6820		enum elf_ppc64_reloc_type r_type;
6821		unsigned long r_symndx;
6822		struct elf_link_hash_entry *h;
6823		Elf_Internal_Sym *sym;
6824		asection *sym_sec;
6825		char *tls_mask;
6826		char tls_set, tls_clear, tls_type = 0;
6827		bfd_vma value;
6828		bfd_boolean ok_tprel, is_local;
6829
6830		r_symndx = ELF64_R_SYM (rel->r_info);
6831		if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6832				r_symndx, ibfd))
6833		  {
6834		  err_free_rel:
6835		    if (elf_section_data (sec)->relocs != relstart)
6836		      free (relstart);
6837		    if (toc_ref != NULL)
6838		      free (toc_ref);
6839		    if (locsyms != NULL
6840			&& (elf_tdata (ibfd)->symtab_hdr.contents
6841			    != (unsigned char *) locsyms))
6842		      free (locsyms);
6843		    return FALSE;
6844		  }
6845
6846		if (h != NULL)
6847		  {
6848		    if (h->root.type != bfd_link_hash_defined
6849			&& h->root.type != bfd_link_hash_defweak)
6850		      continue;
6851		    value = h->root.u.def.value;
6852		  }
6853		else
6854		  /* Symbols referenced by TLS relocs must be of type
6855		     STT_TLS.  So no need for .opd local sym adjust.  */
6856		  value = sym->st_value;
6857
6858		ok_tprel = FALSE;
6859		is_local = FALSE;
6860		if (h == NULL
6861		    || !h->def_dynamic)
6862		  {
6863		    is_local = TRUE;
6864		    value += sym_sec->output_offset;
6865		    value += sym_sec->output_section->vma;
6866		    value -= htab->elf.tls_sec->vma;
6867		    ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6868				< (bfd_vma) 1 << 32);
6869		  }
6870
6871		r_type = ELF64_R_TYPE (rel->r_info);
6872		switch (r_type)
6873		  {
6874		  case R_PPC64_GOT_TLSLD16:
6875		  case R_PPC64_GOT_TLSLD16_LO:
6876		  case R_PPC64_GOT_TLSLD16_HI:
6877		  case R_PPC64_GOT_TLSLD16_HA:
6878		    /* These relocs should never be against a symbol
6879		       defined in a shared lib.  Leave them alone if
6880		       that turns out to be the case.  */
6881		    ppc64_tlsld_got (ibfd)->refcount -= 1;
6882		    if (!is_local)
6883		      continue;
6884
6885		    /* LD -> LE */
6886		    tls_set = 0;
6887		    tls_clear = TLS_LD;
6888		    tls_type = TLS_TLS | TLS_LD;
6889		    expecting_tls_get_addr = 1;
6890		    break;
6891
6892		  case R_PPC64_GOT_TLSGD16:
6893		  case R_PPC64_GOT_TLSGD16_LO:
6894		  case R_PPC64_GOT_TLSGD16_HI:
6895		  case R_PPC64_GOT_TLSGD16_HA:
6896		    if (ok_tprel)
6897		      /* GD -> LE */
6898		      tls_set = 0;
6899		    else
6900		      /* GD -> IE */
6901		      tls_set = TLS_TLS | TLS_TPRELGD;
6902		    tls_clear = TLS_GD;
6903		    tls_type = TLS_TLS | TLS_GD;
6904		    expecting_tls_get_addr = 1;
6905		    break;
6906
6907		  case R_PPC64_GOT_TPREL16_DS:
6908		  case R_PPC64_GOT_TPREL16_LO_DS:
6909		  case R_PPC64_GOT_TPREL16_HI:
6910		  case R_PPC64_GOT_TPREL16_HA:
6911		    expecting_tls_get_addr = 0;
6912		    if (ok_tprel)
6913		      {
6914			/* IE -> LE */
6915			tls_set = 0;
6916			tls_clear = TLS_TPREL;
6917			tls_type = TLS_TLS | TLS_TPREL;
6918			break;
6919		      }
6920		    else
6921		      continue;
6922
6923		  case R_PPC64_REL14:
6924		  case R_PPC64_REL14_BRTAKEN:
6925		  case R_PPC64_REL14_BRNTAKEN:
6926		  case R_PPC64_REL24:
6927		    if (h != NULL
6928			&& (h == &htab->tls_get_addr->elf
6929			    || h == &htab->tls_get_addr_fd->elf))
6930		      {
6931			if (!expecting_tls_get_addr
6932			    && rel != relstart
6933			    && ((ELF64_R_TYPE (rel[-1].r_info)
6934				 == R_PPC64_TOC16)
6935				|| (ELF64_R_TYPE (rel[-1].r_info)
6936				    == R_PPC64_TOC16_LO)))
6937			  {
6938			    /* Check for toc tls entries.  */
6939			    char *toc_tls;
6940			    int retval;
6941
6942			    retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6943						   rel - 1, ibfd);
6944			    if (retval == 0)
6945			      goto err_free_rel;
6946			    if (retval > 1 && toc_tls != NULL)
6947			      {
6948				expecting_tls_get_addr = 1;
6949				if (toc_ref != NULL)
6950				  toc_ref[toc_ref_index] = 1;
6951			      }
6952			  }
6953
6954			if (expecting_tls_get_addr)
6955			  {
6956			    struct plt_entry *ent;
6957			    for (ent = h->plt.plist; ent; ent = ent->next)
6958			      if (ent->addend == 0)
6959				{
6960				  if (ent->plt.refcount > 0)
6961				    ent->plt.refcount -= 1;
6962				  break;
6963				}
6964			  }
6965		      }
6966		    expecting_tls_get_addr = 0;
6967		    continue;
6968
6969		  case R_PPC64_TOC16:
6970		  case R_PPC64_TOC16_LO:
6971		  case R_PPC64_TLS:
6972		    expecting_tls_get_addr = 0;
6973		    if (sym_sec == toc && toc != NULL)
6974		      {
6975			/* Mark this toc entry as referenced by a TLS
6976			   code sequence.  We can do that now in the
6977			   case of R_PPC64_TLS, and after checking for
6978			   tls_get_addr for the TOC16 relocs.  */
6979			if (toc_ref == NULL)
6980			  {
6981			    toc_ref = bfd_zmalloc (toc->size / 8);
6982			    if (toc_ref == NULL)
6983			      goto err_free_rel;
6984			  }
6985			if (h != NULL)
6986			  value = h->root.u.def.value;
6987			else
6988			  value = sym->st_value;
6989			value += rel->r_addend;
6990			BFD_ASSERT (value < toc->size && value % 8 == 0);
6991			toc_ref_index = value / 8;
6992			if (r_type == R_PPC64_TLS)
6993			  toc_ref[toc_ref_index] = 1;
6994		      }
6995		    continue;
6996
6997		  case R_PPC64_TPREL64:
6998		    expecting_tls_get_addr = 0;
6999		    if (sec != toc
7000			|| toc_ref == NULL
7001			|| !toc_ref[rel->r_offset / 8])
7002		      continue;
7003		    if (ok_tprel)
7004		      {
7005			/* IE -> LE */
7006			tls_set = TLS_EXPLICIT;
7007			tls_clear = TLS_TPREL;
7008			break;
7009		      }
7010		    else
7011		      continue;
7012
7013		  case R_PPC64_DTPMOD64:
7014		    expecting_tls_get_addr = 0;
7015		    if (sec != toc
7016			|| toc_ref == NULL
7017			|| !toc_ref[rel->r_offset / 8])
7018		      continue;
7019		    if (rel + 1 < relend
7020			&& (rel[1].r_info
7021			    == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7022			&& rel[1].r_offset == rel->r_offset + 8)
7023		      {
7024			if (ok_tprel)
7025			  /* GD -> LE */
7026			  tls_set = TLS_EXPLICIT | TLS_GD;
7027			else
7028			  /* GD -> IE */
7029			  tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7030			tls_clear = TLS_GD;
7031		      }
7032		    else
7033		      {
7034			if (!is_local)
7035			  continue;
7036
7037			/* LD -> LE */
7038			tls_set = TLS_EXPLICIT;
7039			tls_clear = TLS_LD;
7040		      }
7041		    break;
7042
7043		  default:
7044		    expecting_tls_get_addr = 0;
7045		    continue;
7046		  }
7047
7048		if ((tls_set & TLS_EXPLICIT) == 0)
7049		  {
7050		    struct got_entry *ent;
7051
7052		    /* Adjust got entry for this reloc.  */
7053		    if (h != NULL)
7054		      ent = h->got.glist;
7055		    else
7056		      ent = elf_local_got_ents (ibfd)[r_symndx];
7057
7058		    for (; ent != NULL; ent = ent->next)
7059		      if (ent->addend == rel->r_addend
7060			  && ent->owner == ibfd
7061			  && ent->tls_type == tls_type)
7062			break;
7063		    if (ent == NULL)
7064		      abort ();
7065
7066		    if (tls_set == 0)
7067		      {
7068			/* We managed to get rid of a got entry.  */
7069			if (ent->got.refcount > 0)
7070			  ent->got.refcount -= 1;
7071		      }
7072		  }
7073		else
7074		  {
7075		    /* If we got rid of a DTPMOD/DTPREL reloc pair then
7076		       we'll lose one or two dyn relocs.  */
7077		    if (!dec_dynrel_count (rel->r_info, sec, info,
7078					   NULL, h, sym_sec))
7079		      return FALSE;
7080
7081		    if (tls_set == (TLS_EXPLICIT | TLS_GD))
7082		      {
7083			if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7084					       NULL, h, sym_sec))
7085			  return FALSE;
7086		      }
7087		  }
7088
7089		*tls_mask |= tls_set;
7090		*tls_mask &= ~tls_clear;
7091	      }
7092
7093	    if (elf_section_data (sec)->relocs != relstart)
7094	      free (relstart);
7095	  }
7096
7097      if (toc_ref != NULL)
7098	free (toc_ref);
7099
7100      if (locsyms != NULL
7101	  && (elf_tdata (ibfd)->symtab_hdr.contents
7102	      != (unsigned char *) locsyms))
7103	{
7104	  if (!info->keep_memory)
7105	    free (locsyms);
7106	  else
7107	    elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
7108	}
7109    }
7110  return TRUE;
7111}
7112
7113/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7114   the values of any global symbols in a toc section that has been
7115   edited.  Globals in toc sections should be a rarity, so this function
7116   sets a flag if any are found in toc sections other than the one just
7117   edited, so that futher hash table traversals can be avoided.  */
7118
7119struct adjust_toc_info
7120{
7121  asection *toc;
7122  unsigned long *skip;
7123  bfd_boolean global_toc_syms;
7124};
7125
7126static bfd_boolean
7127adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7128{
7129  struct ppc_link_hash_entry *eh;
7130  struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7131
7132  if (h->root.type == bfd_link_hash_indirect)
7133    return TRUE;
7134
7135  if (h->root.type == bfd_link_hash_warning)
7136    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7137
7138  if (h->root.type != bfd_link_hash_defined
7139      && h->root.type != bfd_link_hash_defweak)
7140    return TRUE;
7141
7142  eh = (struct ppc_link_hash_entry *) h;
7143  if (eh->adjust_done)
7144    return TRUE;
7145
7146  if (eh->elf.root.u.def.section == toc_inf->toc)
7147    {
7148      unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7149      if (skip != (unsigned long) -1)
7150	eh->elf.root.u.def.value -= skip;
7151      else
7152	{
7153	  (*_bfd_error_handler)
7154	    (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7155	  eh->elf.root.u.def.section = &bfd_abs_section;
7156	  eh->elf.root.u.def.value = 0;
7157	}
7158      eh->adjust_done = 1;
7159    }
7160  else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7161    toc_inf->global_toc_syms = TRUE;
7162
7163  return TRUE;
7164}
7165
7166/* Examine all relocs referencing .toc sections in order to remove
7167   unused .toc entries.  */
7168
7169bfd_boolean
7170ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7171{
7172  bfd *ibfd;
7173  struct adjust_toc_info toc_inf;
7174
7175  toc_inf.global_toc_syms = TRUE;
7176  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7177    {
7178      asection *toc, *sec;
7179      Elf_Internal_Shdr *symtab_hdr;
7180      Elf_Internal_Sym *local_syms;
7181      struct elf_link_hash_entry **sym_hashes;
7182      Elf_Internal_Rela *relstart, *rel;
7183      unsigned long *skip, *drop;
7184      unsigned char *used;
7185      unsigned char *keep, last, some_unused;
7186
7187      toc = bfd_get_section_by_name (ibfd, ".toc");
7188      if (toc == NULL
7189	  || toc->size == 0
7190	  || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7191	  || elf_discarded_section (toc))
7192	continue;
7193
7194      local_syms = NULL;
7195      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7196      sym_hashes = elf_sym_hashes (ibfd);
7197
7198      /* Look at sections dropped from the final link.  */
7199      skip = NULL;
7200      relstart = NULL;
7201      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7202	{
7203	  if (sec->reloc_count == 0
7204	      || !elf_discarded_section (sec)
7205	      || get_opd_info (sec)
7206	      || (sec->flags & SEC_ALLOC) == 0
7207	      || (sec->flags & SEC_DEBUGGING) != 0)
7208	    continue;
7209
7210	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7211	  if (relstart == NULL)
7212	    goto error_ret;
7213
7214	  /* Run through the relocs to see which toc entries might be
7215	     unused.  */
7216	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7217	    {
7218	      enum elf_ppc64_reloc_type r_type;
7219	      unsigned long r_symndx;
7220	      asection *sym_sec;
7221	      struct elf_link_hash_entry *h;
7222	      Elf_Internal_Sym *sym;
7223	      bfd_vma val;
7224
7225	      r_type = ELF64_R_TYPE (rel->r_info);
7226	      switch (r_type)
7227		{
7228		default:
7229		  continue;
7230
7231		case R_PPC64_TOC16:
7232		case R_PPC64_TOC16_LO:
7233		case R_PPC64_TOC16_HI:
7234		case R_PPC64_TOC16_HA:
7235		case R_PPC64_TOC16_DS:
7236		case R_PPC64_TOC16_LO_DS:
7237		  break;
7238		}
7239
7240	      r_symndx = ELF64_R_SYM (rel->r_info);
7241	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7242			      r_symndx, ibfd))
7243		goto error_ret;
7244
7245	      if (sym_sec != toc)
7246		continue;
7247
7248	      if (h != NULL)
7249		val = h->root.u.def.value;
7250	      else
7251		val = sym->st_value;
7252	      val += rel->r_addend;
7253
7254	      if (val >= toc->size)
7255		continue;
7256
7257	      /* Anything in the toc ought to be aligned to 8 bytes.
7258		 If not, don't mark as unused.  */
7259	      if (val & 7)
7260		continue;
7261
7262	      if (skip == NULL)
7263		{
7264		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7265		  if (skip == NULL)
7266		    goto error_ret;
7267		}
7268
7269	      skip[val >> 3] = 1;
7270	    }
7271
7272	  if (elf_section_data (sec)->relocs != relstart)
7273	    free (relstart);
7274	}
7275
7276      if (skip == NULL)
7277	continue;
7278
7279      used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7280      if (used == NULL)
7281	{
7282	error_ret:
7283	  if (local_syms != NULL
7284	      && symtab_hdr->contents != (unsigned char *) local_syms)
7285	    free (local_syms);
7286	  if (sec != NULL
7287	      && relstart != NULL
7288	      && elf_section_data (sec)->relocs != relstart)
7289	    free (relstart);
7290	  if (skip != NULL)
7291	    free (skip);
7292	  return FALSE;
7293	}
7294
7295      /* Now check all kept sections that might reference the toc.
7296	 Check the toc itself last.  */
7297      for (sec = (ibfd->sections == toc && toc->next ? toc->next
7298		  : ibfd->sections);
7299	   sec != NULL;
7300	   sec = (sec == toc ? NULL
7301		  : sec->next == NULL ? toc
7302		  : sec->next == toc && toc->next ? toc->next
7303		  : sec->next))
7304	{
7305	  int repeat;
7306
7307	  if (sec->reloc_count == 0
7308	      || elf_discarded_section (sec)
7309	      || get_opd_info (sec)
7310	      || (sec->flags & SEC_ALLOC) == 0
7311	      || (sec->flags & SEC_DEBUGGING) != 0)
7312	    continue;
7313
7314	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7315	  if (relstart == NULL)
7316	    goto error_ret;
7317
7318	  /* Mark toc entries referenced as used.  */
7319	  repeat = 0;
7320	  do
7321	    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7322	      {
7323		enum elf_ppc64_reloc_type r_type;
7324		unsigned long r_symndx;
7325		asection *sym_sec;
7326		struct elf_link_hash_entry *h;
7327		Elf_Internal_Sym *sym;
7328		bfd_vma val;
7329
7330		r_type = ELF64_R_TYPE (rel->r_info);
7331		switch (r_type)
7332		  {
7333		  case R_PPC64_TOC16:
7334		  case R_PPC64_TOC16_LO:
7335		  case R_PPC64_TOC16_HI:
7336		  case R_PPC64_TOC16_HA:
7337		  case R_PPC64_TOC16_DS:
7338		  case R_PPC64_TOC16_LO_DS:
7339		    /* In case we're taking addresses of toc entries.  */
7340		  case R_PPC64_ADDR64:
7341		    break;
7342
7343		  default:
7344		    continue;
7345		  }
7346
7347		r_symndx = ELF64_R_SYM (rel->r_info);
7348		if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7349				r_symndx, ibfd))
7350		  {
7351		    free (used);
7352		    goto error_ret;
7353		  }
7354
7355		if (sym_sec != toc)
7356		  continue;
7357
7358		if (h != NULL)
7359		  val = h->root.u.def.value;
7360		else
7361		  val = sym->st_value;
7362		val += rel->r_addend;
7363
7364		if (val >= toc->size)
7365		  continue;
7366
7367		/* For the toc section, we only mark as used if
7368		   this entry itself isn't unused.  */
7369		if (sec == toc
7370		    && !used[val >> 3]
7371		    && (used[rel->r_offset >> 3]
7372			|| !skip[rel->r_offset >> 3]))
7373		  /* Do all the relocs again, to catch reference
7374		     chains.  */
7375		  repeat = 1;
7376
7377		used[val >> 3] = 1;
7378	      }
7379	  while (repeat);
7380	}
7381
7382      /* Merge the used and skip arrays.  Assume that TOC
7383	 doublewords not appearing as either used or unused belong
7384	 to to an entry more than one doubleword in size.  */
7385      for (drop = skip, keep = used, last = 0, some_unused = 0;
7386	   drop < skip + (toc->size + 7) / 8;
7387	   ++drop, ++keep)
7388	{
7389	  if (*keep)
7390	    {
7391	      *drop = 0;
7392	      last = 0;
7393	    }
7394	  else if (*drop)
7395	    {
7396	      some_unused = 1;
7397	      last = 1;
7398	    }
7399	  else
7400	    *drop = last;
7401	}
7402
7403      free (used);
7404
7405      if (some_unused)
7406	{
7407	  bfd_byte *contents, *src;
7408	  unsigned long off;
7409
7410	  /* Shuffle the toc contents, and at the same time convert the
7411	     skip array from booleans into offsets.  */
7412	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7413	    goto error_ret;
7414
7415	  elf_section_data (toc)->this_hdr.contents = contents;
7416
7417	  for (src = contents, off = 0, drop = skip;
7418	       src < contents + toc->size;
7419	       src += 8, ++drop)
7420	    {
7421	      if (*drop)
7422		{
7423		  *drop = (unsigned long) -1;
7424		  off += 8;
7425		}
7426	      else if (off != 0)
7427		{
7428		  *drop = off;
7429		  memcpy (src - off, src, 8);
7430		}
7431	    }
7432	  toc->rawsize = toc->size;
7433	  toc->size = src - contents - off;
7434
7435	  if (toc->reloc_count != 0)
7436	    {
7437	      Elf_Internal_Rela *wrel;
7438	      bfd_size_type sz;
7439
7440	      /* Read toc relocs.  */
7441	      relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7442						    TRUE);
7443	      if (relstart == NULL)
7444		goto error_ret;
7445
7446	      /* Remove unused toc relocs, and adjust those we keep.  */
7447	      wrel = relstart;
7448	      for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7449		if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7450		  {
7451		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7452		    wrel->r_info = rel->r_info;
7453		    wrel->r_addend = rel->r_addend;
7454		    ++wrel;
7455		  }
7456		else if (!dec_dynrel_count (rel->r_info, toc, info,
7457					    &local_syms, NULL, NULL))
7458		  goto error_ret;
7459
7460	      toc->reloc_count = wrel - relstart;
7461	      sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7462	      elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7463	      BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7464	    }
7465
7466	  /* Adjust addends for relocs against the toc section sym.  */
7467	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7468	    {
7469	      if (sec->reloc_count == 0
7470		  || elf_discarded_section (sec))
7471		continue;
7472
7473	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7474						    TRUE);
7475	      if (relstart == NULL)
7476		goto error_ret;
7477
7478	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7479		{
7480		  enum elf_ppc64_reloc_type r_type;
7481		  unsigned long r_symndx;
7482		  asection *sym_sec;
7483		  struct elf_link_hash_entry *h;
7484		  Elf_Internal_Sym *sym;
7485
7486		  r_type = ELF64_R_TYPE (rel->r_info);
7487		  switch (r_type)
7488		    {
7489		    default:
7490		      continue;
7491
7492		    case R_PPC64_TOC16:
7493		    case R_PPC64_TOC16_LO:
7494		    case R_PPC64_TOC16_HI:
7495		    case R_PPC64_TOC16_HA:
7496		    case R_PPC64_TOC16_DS:
7497		    case R_PPC64_TOC16_LO_DS:
7498		    case R_PPC64_ADDR64:
7499		      break;
7500		    }
7501
7502		  r_symndx = ELF64_R_SYM (rel->r_info);
7503		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7504				  r_symndx, ibfd))
7505		    goto error_ret;
7506
7507		  if (sym_sec != toc || h != NULL || sym->st_value != 0)
7508		    continue;
7509
7510		  rel->r_addend -= skip[rel->r_addend >> 3];
7511		}
7512	    }
7513
7514	  /* We shouldn't have local or global symbols defined in the TOC,
7515	     but handle them anyway.  */
7516	  if (local_syms != NULL)
7517	    {
7518	      Elf_Internal_Sym *sym;
7519
7520	      for (sym = local_syms;
7521		   sym < local_syms + symtab_hdr->sh_info;
7522		   ++sym)
7523		if (sym->st_shndx != SHN_UNDEF
7524		    && (sym->st_shndx < SHN_LORESERVE
7525			|| sym->st_shndx > SHN_HIRESERVE)
7526		    && sym->st_value != 0
7527		    && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7528		  {
7529		    if (skip[sym->st_value >> 3] != (unsigned long) -1)
7530		      sym->st_value -= skip[sym->st_value >> 3];
7531		    else
7532		      {
7533			(*_bfd_error_handler)
7534			  (_("%s defined in removed toc entry"),
7535			   bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7536					     NULL));
7537			sym->st_value = 0;
7538			sym->st_shndx = SHN_ABS;
7539		      }
7540		    symtab_hdr->contents = (unsigned char *) local_syms;
7541		  }
7542	    }
7543
7544	  /* Finally, adjust any global syms defined in the toc.  */
7545	  if (toc_inf.global_toc_syms)
7546	    {
7547	      toc_inf.toc = toc;
7548	      toc_inf.skip = skip;
7549	      toc_inf.global_toc_syms = FALSE;
7550	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7551				      &toc_inf);
7552	    }
7553	}
7554
7555      if (local_syms != NULL
7556	  && symtab_hdr->contents != (unsigned char *) local_syms)
7557	{
7558	  if (!info->keep_memory)
7559	    free (local_syms);
7560	  else
7561	    symtab_hdr->contents = (unsigned char *) local_syms;
7562	}
7563      free (skip);
7564    }
7565
7566  return TRUE;
7567}
7568
7569/* Allocate space in .plt, .got and associated reloc sections for
7570   dynamic relocs.  */
7571
7572static bfd_boolean
7573allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7574{
7575  struct bfd_link_info *info;
7576  struct ppc_link_hash_table *htab;
7577  asection *s;
7578  struct ppc_link_hash_entry *eh;
7579  struct ppc_dyn_relocs *p;
7580  struct got_entry *gent;
7581
7582  if (h->root.type == bfd_link_hash_indirect)
7583    return TRUE;
7584
7585  if (h->root.type == bfd_link_hash_warning)
7586    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7587
7588  info = (struct bfd_link_info *) inf;
7589  htab = ppc_hash_table (info);
7590
7591  if (htab->elf.dynamic_sections_created
7592      && h->dynindx != -1
7593      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7594    {
7595      struct plt_entry *pent;
7596      bfd_boolean doneone = FALSE;
7597      for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7598	if (pent->plt.refcount > 0)
7599	  {
7600	    /* If this is the first .plt entry, make room for the special
7601	       first entry.  */
7602	    s = htab->plt;
7603	    if (s->size == 0)
7604	      s->size += PLT_INITIAL_ENTRY_SIZE;
7605
7606	    pent->plt.offset = s->size;
7607
7608	    /* Make room for this entry.  */
7609	    s->size += PLT_ENTRY_SIZE;
7610
7611	    /* Make room for the .glink code.  */
7612	    s = htab->glink;
7613	    if (s->size == 0)
7614	      s->size += GLINK_CALL_STUB_SIZE;
7615	    /* We need bigger stubs past index 32767.  */
7616	    if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7617	      s->size += 4;
7618	    s->size += 2*4;
7619
7620	    /* We also need to make an entry in the .rela.plt section.  */
7621	    s = htab->relplt;
7622	    s->size += sizeof (Elf64_External_Rela);
7623	    doneone = TRUE;
7624	  }
7625	else
7626	  pent->plt.offset = (bfd_vma) -1;
7627      if (!doneone)
7628	{
7629	  h->plt.plist = NULL;
7630	  h->needs_plt = 0;
7631	}
7632    }
7633  else
7634    {
7635      h->plt.plist = NULL;
7636      h->needs_plt = 0;
7637    }
7638
7639  eh = (struct ppc_link_hash_entry *) h;
7640  /* Run through the TLS GD got entries first if we're changing them
7641     to TPREL.  */
7642  if ((eh->tls_mask & TLS_TPRELGD) != 0)
7643    for (gent = h->got.glist; gent != NULL; gent = gent->next)
7644      if (gent->got.refcount > 0
7645	  && (gent->tls_type & TLS_GD) != 0)
7646	{
7647	  /* This was a GD entry that has been converted to TPREL.  If
7648	     there happens to be a TPREL entry we can use that one.  */
7649	  struct got_entry *ent;
7650	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
7651	    if (ent->got.refcount > 0
7652		&& (ent->tls_type & TLS_TPREL) != 0
7653		&& ent->addend == gent->addend
7654		&& ent->owner == gent->owner)
7655	      {
7656		gent->got.refcount = 0;
7657		break;
7658	      }
7659
7660	  /* If not, then we'll be using our own TPREL entry.  */
7661	  if (gent->got.refcount != 0)
7662	    gent->tls_type = TLS_TLS | TLS_TPREL;
7663	}
7664
7665  for (gent = h->got.glist; gent != NULL; gent = gent->next)
7666    if (gent->got.refcount > 0)
7667      {
7668	bfd_boolean dyn;
7669
7670	/* Make sure this symbol is output as a dynamic symbol.
7671	   Undefined weak syms won't yet be marked as dynamic,
7672	   nor will all TLS symbols.  */
7673	if (h->dynindx == -1
7674	    && !h->forced_local)
7675	  {
7676	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
7677	      return FALSE;
7678	  }
7679
7680	if ((gent->tls_type & TLS_LD) != 0
7681	    && !h->def_dynamic)
7682	  {
7683	    gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7684	    continue;
7685	  }
7686
7687	s = ppc64_elf_tdata (gent->owner)->got;
7688	gent->got.offset = s->size;
7689	s->size
7690	  += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7691	dyn = htab->elf.dynamic_sections_created;
7692	if ((info->shared
7693	     || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7694	    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7695		|| h->root.type != bfd_link_hash_undefweak))
7696	  ppc64_elf_tdata (gent->owner)->relgot->size
7697	    += (gent->tls_type & eh->tls_mask & TLS_GD
7698		? 2 * sizeof (Elf64_External_Rela)
7699		: sizeof (Elf64_External_Rela));
7700      }
7701    else
7702      gent->got.offset = (bfd_vma) -1;
7703
7704  if (eh->dyn_relocs == NULL)
7705    return TRUE;
7706
7707  /* In the shared -Bsymbolic case, discard space allocated for
7708     dynamic pc-relative relocs against symbols which turn out to be
7709     defined in regular objects.  For the normal shared case, discard
7710     space for relocs that have become local due to symbol visibility
7711     changes.  */
7712
7713  if (info->shared)
7714    {
7715      /* Relocs that use pc_count are those that appear on a call insn,
7716	 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7717	 generated via assembly.  We want calls to protected symbols to
7718	 resolve directly to the function rather than going via the plt.
7719	 If people want function pointer comparisons to work as expected
7720	 then they should avoid writing weird assembly.  */
7721      if (SYMBOL_CALLS_LOCAL (info, h))
7722	{
7723	  struct ppc_dyn_relocs **pp;
7724
7725	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7726	    {
7727	      p->count -= p->pc_count;
7728	      p->pc_count = 0;
7729	      if (p->count == 0)
7730		*pp = p->next;
7731	      else
7732		pp = &p->next;
7733	    }
7734	}
7735
7736      /* Also discard relocs on undefined weak syms with non-default
7737	 visibility.  */
7738      if (eh->dyn_relocs != NULL
7739	  && h->root.type == bfd_link_hash_undefweak)
7740	{
7741	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7742	    eh->dyn_relocs = NULL;
7743
7744	  /* Make sure this symbol is output as a dynamic symbol.
7745	     Undefined weak syms won't yet be marked as dynamic.  */
7746	  else if (h->dynindx == -1
7747		   && !h->forced_local)
7748	    {
7749	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
7750		return FALSE;
7751	    }
7752	}
7753    }
7754  else if (ELIMINATE_COPY_RELOCS)
7755    {
7756      /* For the non-shared case, discard space for relocs against
7757	 symbols which turn out to need copy relocs or are not
7758	 dynamic.  */
7759
7760      if (!h->non_got_ref
7761	  && h->def_dynamic
7762	  && !h->def_regular)
7763	{
7764	  /* Make sure this symbol is output as a dynamic symbol.
7765	     Undefined weak syms won't yet be marked as dynamic.  */
7766	  if (h->dynindx == -1
7767	      && !h->forced_local)
7768	    {
7769	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
7770		return FALSE;
7771	    }
7772
7773	  /* If that succeeded, we know we'll be keeping all the
7774	     relocs.  */
7775	  if (h->dynindx != -1)
7776	    goto keep;
7777	}
7778
7779      eh->dyn_relocs = NULL;
7780
7781    keep: ;
7782    }
7783
7784  /* Finally, allocate space.  */
7785  for (p = eh->dyn_relocs; p != NULL; p = p->next)
7786    {
7787      asection *sreloc = elf_section_data (p->sec)->sreloc;
7788      sreloc->size += p->count * sizeof (Elf64_External_Rela);
7789    }
7790
7791  return TRUE;
7792}
7793
7794/* Find any dynamic relocs that apply to read-only sections.  */
7795
7796static bfd_boolean
7797readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7798{
7799  struct ppc_link_hash_entry *eh;
7800  struct ppc_dyn_relocs *p;
7801
7802  if (h->root.type == bfd_link_hash_warning)
7803    h = (struct elf_link_hash_entry *) h->root.u.i.link;
7804
7805  eh = (struct ppc_link_hash_entry *) h;
7806  for (p = eh->dyn_relocs; p != NULL; p = p->next)
7807    {
7808      asection *s = p->sec->output_section;
7809
7810      if (s != NULL && (s->flags & SEC_READONLY) != 0)
7811	{
7812	  struct bfd_link_info *info = inf;
7813
7814	  info->flags |= DF_TEXTREL;
7815
7816	  /* Not an error, just cut short the traversal.  */
7817	  return FALSE;
7818	}
7819    }
7820  return TRUE;
7821}
7822
7823/* Set the sizes of the dynamic sections.  */
7824
7825static bfd_boolean
7826ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7827				 struct bfd_link_info *info)
7828{
7829  struct ppc_link_hash_table *htab;
7830  bfd *dynobj;
7831  asection *s;
7832  bfd_boolean relocs;
7833  bfd *ibfd;
7834
7835  htab = ppc_hash_table (info);
7836  dynobj = htab->elf.dynobj;
7837  if (dynobj == NULL)
7838    abort ();
7839
7840  if (htab->elf.dynamic_sections_created)
7841    {
7842      /* Set the contents of the .interp section to the interpreter.  */
7843      if (info->executable)
7844	{
7845	  s = bfd_get_section_by_name (dynobj, ".interp");
7846	  if (s == NULL)
7847	    abort ();
7848	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7849	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7850	}
7851    }
7852
7853  /* Set up .got offsets for local syms, and space for local dynamic
7854     relocs.  */
7855  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7856    {
7857      struct got_entry **lgot_ents;
7858      struct got_entry **end_lgot_ents;
7859      char *lgot_masks;
7860      bfd_size_type locsymcount;
7861      Elf_Internal_Shdr *symtab_hdr;
7862      asection *srel;
7863
7864      if (!is_ppc64_elf_target (ibfd->xvec))
7865	continue;
7866
7867      if (ppc64_tlsld_got (ibfd)->refcount > 0)
7868	{
7869	  s = ppc64_elf_tdata (ibfd)->got;
7870	  ppc64_tlsld_got (ibfd)->offset = s->size;
7871	  s->size += 16;
7872	  if (info->shared)
7873	    {
7874	      srel = ppc64_elf_tdata (ibfd)->relgot;
7875	      srel->size += sizeof (Elf64_External_Rela);
7876	    }
7877	}
7878      else
7879	ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7880
7881      for (s = ibfd->sections; s != NULL; s = s->next)
7882	{
7883	  struct ppc_dyn_relocs *p;
7884
7885	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7886	    {
7887	      if (!bfd_is_abs_section (p->sec)
7888		  && bfd_is_abs_section (p->sec->output_section))
7889		{
7890		  /* Input section has been discarded, either because
7891		     it is a copy of a linkonce section or due to
7892		     linker script /DISCARD/, so we'll be discarding
7893		     the relocs too.  */
7894		}
7895	      else if (p->count != 0)
7896		{
7897		  srel = elf_section_data (p->sec)->sreloc;
7898		  srel->size += p->count * sizeof (Elf64_External_Rela);
7899		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7900		    info->flags |= DF_TEXTREL;
7901		}
7902	    }
7903	}
7904
7905      lgot_ents = elf_local_got_ents (ibfd);
7906      if (!lgot_ents)
7907	continue;
7908
7909      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7910      locsymcount = symtab_hdr->sh_info;
7911      end_lgot_ents = lgot_ents + locsymcount;
7912      lgot_masks = (char *) end_lgot_ents;
7913      s = ppc64_elf_tdata (ibfd)->got;
7914      srel = ppc64_elf_tdata (ibfd)->relgot;
7915      for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7916	{
7917	  struct got_entry *ent;
7918
7919	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7920	    if (ent->got.refcount > 0)
7921	      {
7922		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7923		  {
7924		    if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7925		      {
7926			ppc64_tlsld_got (ibfd)->offset = s->size;
7927			s->size += 16;
7928			if (info->shared)
7929			  srel->size += sizeof (Elf64_External_Rela);
7930		      }
7931		    ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7932		  }
7933		else
7934		  {
7935		    ent->got.offset = s->size;
7936		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7937		      {
7938			s->size += 16;
7939			if (info->shared)
7940			  srel->size += 2 * sizeof (Elf64_External_Rela);
7941		      }
7942		    else
7943		      {
7944			s->size += 8;
7945			if (info->shared)
7946			  srel->size += sizeof (Elf64_External_Rela);
7947		      }
7948		  }
7949	      }
7950	    else
7951	      ent->got.offset = (bfd_vma) -1;
7952	}
7953    }
7954
7955  /* Allocate global sym .plt and .got entries, and space for global
7956     sym dynamic relocs.  */
7957  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7958
7959  /* We now have determined the sizes of the various dynamic sections.
7960     Allocate memory for them.  */
7961  relocs = FALSE;
7962  for (s = dynobj->sections; s != NULL; s = s->next)
7963    {
7964      if ((s->flags & SEC_LINKER_CREATED) == 0)
7965	continue;
7966
7967      if (s == htab->brlt || s == htab->relbrlt)
7968	/* These haven't been allocated yet;  don't strip.  */
7969	continue;
7970      else if (s == htab->got
7971	       || s == htab->plt
7972	       || s == htab->glink
7973	       || s == htab->dynbss)
7974	{
7975	  /* Strip this section if we don't need it; see the
7976	     comment below.  */
7977	}
7978      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
7979	{
7980	  if (s->size != 0)
7981	    {
7982	      if (s != htab->relplt)
7983		relocs = TRUE;
7984
7985	      /* We use the reloc_count field as a counter if we need
7986		 to copy relocs into the output file.  */
7987	      s->reloc_count = 0;
7988	    }
7989	}
7990      else
7991	{
7992	  /* It's not one of our sections, so don't allocate space.  */
7993	  continue;
7994	}
7995
7996      if (s->size == 0)
7997	{
7998	  /* If we don't need this section, strip it from the
7999	     output file.  This is mostly to handle .rela.bss and
8000	     .rela.plt.  We must create both sections in
8001	     create_dynamic_sections, because they must be created
8002	     before the linker maps input sections to output
8003	     sections.  The linker does that before
8004	     adjust_dynamic_symbol is called, and it is that
8005	     function which decides whether anything needs to go
8006	     into these sections.  */
8007	  s->flags |= SEC_EXCLUDE;
8008	  continue;
8009	}
8010
8011      if ((s->flags & SEC_HAS_CONTENTS) == 0)
8012	continue;
8013
8014      /* Allocate memory for the section contents.  We use bfd_zalloc
8015	 here in case unused entries are not reclaimed before the
8016	 section's contents are written out.  This should not happen,
8017	 but this way if it does we get a R_PPC64_NONE reloc in .rela
8018	 sections instead of garbage.
8019	 We also rely on the section contents being zero when writing
8020	 the GOT.  */
8021      s->contents = bfd_zalloc (dynobj, s->size);
8022      if (s->contents == NULL)
8023	return FALSE;
8024    }
8025
8026  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8027    {
8028      if (!is_ppc64_elf_target (ibfd->xvec))
8029	continue;
8030
8031      s = ppc64_elf_tdata (ibfd)->got;
8032      if (s != NULL && s != htab->got)
8033	{
8034	  if (s->size == 0)
8035	    s->flags |= SEC_EXCLUDE;
8036	  else
8037	    {
8038	      s->contents = bfd_zalloc (ibfd, s->size);
8039	      if (s->contents == NULL)
8040		return FALSE;
8041	    }
8042	}
8043      s = ppc64_elf_tdata (ibfd)->relgot;
8044      if (s != NULL)
8045	{
8046	  if (s->size == 0)
8047	    s->flags |= SEC_EXCLUDE;
8048	  else
8049	    {
8050	      s->contents = bfd_zalloc (ibfd, s->size);
8051	      if (s->contents == NULL)
8052		return FALSE;
8053	      relocs = TRUE;
8054	      s->reloc_count = 0;
8055	    }
8056	}
8057    }
8058
8059  if (htab->elf.dynamic_sections_created)
8060    {
8061      /* Add some entries to the .dynamic section.  We fill in the
8062	 values later, in ppc64_elf_finish_dynamic_sections, but we
8063	 must add the entries now so that we get the correct size for
8064	 the .dynamic section.  The DT_DEBUG entry is filled in by the
8065	 dynamic linker and used by the debugger.  */
8066#define add_dynamic_entry(TAG, VAL) \
8067  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8068
8069      if (info->executable)
8070	{
8071	  if (!add_dynamic_entry (DT_DEBUG, 0))
8072	    return FALSE;
8073	}
8074
8075      if (htab->plt != NULL && htab->plt->size != 0)
8076	{
8077	  if (!add_dynamic_entry (DT_PLTGOT, 0)
8078	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
8079	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8080	      || !add_dynamic_entry (DT_JMPREL, 0)
8081	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
8082	    return FALSE;
8083	}
8084
8085      if (NO_OPD_RELOCS)
8086	{
8087	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
8088	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
8089	    return FALSE;
8090	}
8091
8092      if (relocs)
8093	{
8094	  if (!add_dynamic_entry (DT_RELA, 0)
8095	      || !add_dynamic_entry (DT_RELASZ, 0)
8096	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
8097	    return FALSE;
8098
8099	  /* If any dynamic relocs apply to a read-only section,
8100	     then we need a DT_TEXTREL entry.  */
8101	  if ((info->flags & DF_TEXTREL) == 0)
8102	    elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
8103
8104	  if ((info->flags & DF_TEXTREL) != 0)
8105	    {
8106	      if (!add_dynamic_entry (DT_TEXTREL, 0))
8107		return FALSE;
8108	    }
8109	}
8110    }
8111#undef add_dynamic_entry
8112
8113  return TRUE;
8114}
8115
8116/* Determine the type of stub needed, if any, for a call.  */
8117
8118static inline enum ppc_stub_type
8119ppc_type_of_stub (asection *input_sec,
8120		  const Elf_Internal_Rela *rel,
8121		  struct ppc_link_hash_entry **hash,
8122		  bfd_vma destination)
8123{
8124  struct ppc_link_hash_entry *h = *hash;
8125  bfd_vma location;
8126  bfd_vma branch_offset;
8127  bfd_vma max_branch_offset;
8128  enum elf_ppc64_reloc_type r_type;
8129
8130  if (h != NULL)
8131    {
8132      struct ppc_link_hash_entry *fdh = h;
8133      if (fdh->oh != NULL
8134	  && fdh->oh->is_func_descriptor)
8135	fdh = fdh->oh;
8136
8137      if (fdh->elf.dynindx != -1)
8138	{
8139	  struct plt_entry *ent;
8140
8141	  for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
8142	    if (ent->addend == rel->r_addend
8143		&& ent->plt.offset != (bfd_vma) -1)
8144	      {
8145		*hash = fdh;
8146		return ppc_stub_plt_call;
8147	      }
8148	}
8149
8150      /* Here, we know we don't have a plt entry.  If we don't have a
8151	 either a defined function descriptor or a defined entry symbol
8152	 in a regular object file, then it is pointless trying to make
8153	 any other type of stub.  */
8154      if (!((fdh->elf.root.type == bfd_link_hash_defined
8155	    || fdh->elf.root.type == bfd_link_hash_defweak)
8156	    && fdh->elf.root.u.def.section->output_section != NULL)
8157	  && !((h->elf.root.type == bfd_link_hash_defined
8158		|| h->elf.root.type == bfd_link_hash_defweak)
8159	       && h->elf.root.u.def.section->output_section != NULL))
8160	return ppc_stub_none;
8161    }
8162
8163  /* Determine where the call point is.  */
8164  location = (input_sec->output_offset
8165	      + input_sec->output_section->vma
8166	      + rel->r_offset);
8167
8168  branch_offset = destination - location;
8169  r_type = ELF64_R_TYPE (rel->r_info);
8170
8171  /* Determine if a long branch stub is needed.  */
8172  max_branch_offset = 1 << 25;
8173  if (r_type != R_PPC64_REL24)
8174    max_branch_offset = 1 << 15;
8175
8176  if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8177    /* We need a stub.  Figure out whether a long_branch or plt_branch
8178       is needed later.  */
8179    return ppc_stub_long_branch;
8180
8181  return ppc_stub_none;
8182}
8183
8184/* Build a .plt call stub.  */
8185
8186static inline bfd_byte *
8187build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8188{
8189#define PPC_LO(v) ((v) & 0xffff)
8190#define PPC_HI(v) (((v) >> 16) & 0xffff)
8191#define PPC_HA(v) PPC_HI ((v) + 0x8000)
8192
8193  bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
8194  bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
8195  bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
8196  if (PPC_HA (offset + 8) != PPC_HA (offset))
8197    bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),		p += 4;
8198  offset += 8;
8199  bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p),	p += 4;
8200  if (PPC_HA (offset + 8) != PPC_HA (offset))
8201    bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p),		p += 4;
8202  offset += 8;
8203  bfd_put_32 (obfd, MTCTR_R11, p),			p += 4;
8204  bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
8205  bfd_put_32 (obfd, BCTR, p),				p += 4;
8206  return p;
8207}
8208
8209static bfd_boolean
8210ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8211{
8212  struct ppc_stub_hash_entry *stub_entry;
8213  struct ppc_branch_hash_entry *br_entry;
8214  struct bfd_link_info *info;
8215  struct ppc_link_hash_table *htab;
8216  bfd_byte *loc;
8217  bfd_byte *p;
8218  unsigned int indx;
8219  struct plt_entry *ent;
8220  bfd_vma dest, off;
8221  int size;
8222
8223  /* Massage our args to the form they really have.  */
8224  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8225  info = in_arg;
8226
8227  htab = ppc_hash_table (info);
8228
8229  /* Make a note of the offset within the stubs for this entry.  */
8230  stub_entry->stub_offset = stub_entry->stub_sec->size;
8231  loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8232
8233  htab->stub_count[stub_entry->stub_type - 1] += 1;
8234  switch (stub_entry->stub_type)
8235    {
8236    case ppc_stub_long_branch:
8237    case ppc_stub_long_branch_r2off:
8238      /* Branches are relative.  This is where we are going to.  */
8239      off = dest = (stub_entry->target_value
8240		    + stub_entry->target_section->output_offset
8241		    + stub_entry->target_section->output_section->vma);
8242
8243      /* And this is where we are coming from.  */
8244      off -= (stub_entry->stub_offset
8245	      + stub_entry->stub_sec->output_offset
8246	      + stub_entry->stub_sec->output_section->vma);
8247
8248      if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8249	size = 4;
8250      else
8251	{
8252	  bfd_vma r2off;
8253
8254	  r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8255		   - htab->stub_group[stub_entry->id_sec->id].toc_off);
8256	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8257	  loc += 4;
8258	  bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8259	  loc += 4;
8260	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8261	  loc += 4;
8262	  off -= 12;
8263	  size = 16;
8264	}
8265      bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8266
8267      if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8268	{
8269	  (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8270				 stub_entry->root.string);
8271	  htab->stub_error = TRUE;
8272	  return FALSE;
8273	}
8274
8275      if (info->emitrelocations)
8276	{
8277	  Elf_Internal_Rela *relocs, *r;
8278	  struct bfd_elf_section_data *elfsec_data;
8279
8280	  elfsec_data = elf_section_data (stub_entry->stub_sec);
8281	  relocs = elfsec_data->relocs;
8282	  if (relocs == NULL)
8283	    {
8284	      bfd_size_type relsize;
8285	      relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8286	      relocs = bfd_alloc (htab->stub_bfd, relsize);
8287	      if (relocs == NULL)
8288		return FALSE;
8289	      elfsec_data->relocs = relocs;
8290	      elfsec_data->rel_hdr.sh_size = relsize;
8291	      elfsec_data->rel_hdr.sh_entsize = 24;
8292	      stub_entry->stub_sec->reloc_count = 0;
8293	    }
8294	  r = relocs + stub_entry->stub_sec->reloc_count;
8295	  stub_entry->stub_sec->reloc_count += 1;
8296	  r->r_offset = loc - stub_entry->stub_sec->contents;
8297	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8298	  r->r_addend = dest;
8299	  if (stub_entry->h != NULL)
8300	    {
8301	      struct elf_link_hash_entry **hashes;
8302	      unsigned long symndx;
8303	      struct ppc_link_hash_entry *h;
8304
8305	      hashes = elf_sym_hashes (htab->stub_bfd);
8306	      if (hashes == NULL)
8307		{
8308		  bfd_size_type hsize;
8309
8310		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8311		  hashes = bfd_zalloc (htab->stub_bfd, hsize);
8312		  if (hashes == NULL)
8313		    return FALSE;
8314		  elf_sym_hashes (htab->stub_bfd) = hashes;
8315		  htab->stub_globals = 1;
8316		}
8317	      symndx = htab->stub_globals++;
8318	      h = stub_entry->h;
8319	      hashes[symndx] = &h->elf;
8320	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8321	      if (h->oh != NULL && h->oh->is_func)
8322		h = h->oh;
8323	      if (h->elf.root.u.def.section != stub_entry->target_section)
8324		/* H is an opd symbol.  The addend must be zero.  */
8325		r->r_addend = 0;
8326	      else
8327		{
8328		  off = (h->elf.root.u.def.value
8329			 + h->elf.root.u.def.section->output_offset
8330			 + h->elf.root.u.def.section->output_section->vma);
8331		  r->r_addend -= off;
8332		}
8333	    }
8334	}
8335      break;
8336
8337    case ppc_stub_plt_branch:
8338    case ppc_stub_plt_branch_r2off:
8339      br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8340					 stub_entry->root.string + 9,
8341					 FALSE, FALSE);
8342      if (br_entry == NULL)
8343	{
8344	  (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8345				 stub_entry->root.string);
8346	  htab->stub_error = TRUE;
8347	  return FALSE;
8348	}
8349
8350      off = (stub_entry->target_value
8351	     + stub_entry->target_section->output_offset
8352	     + stub_entry->target_section->output_section->vma);
8353
8354      bfd_put_64 (htab->brlt->owner, off,
8355		  htab->brlt->contents + br_entry->offset);
8356
8357      if (htab->relbrlt != NULL)
8358	{
8359	  /* Create a reloc for the branch lookup table entry.  */
8360	  Elf_Internal_Rela rela;
8361	  bfd_byte *rl;
8362
8363	  rela.r_offset = (br_entry->offset
8364			   + htab->brlt->output_offset
8365			   + htab->brlt->output_section->vma);
8366	  rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8367	  rela.r_addend = off;
8368
8369	  rl = htab->relbrlt->contents;
8370	  rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8371	  bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8372	}
8373      else if (info->emitrelocations)
8374	{
8375	  Elf_Internal_Rela *relocs, *r;
8376	  struct bfd_elf_section_data *elfsec_data;
8377
8378	  elfsec_data = elf_section_data (htab->brlt);
8379	  relocs = elfsec_data->relocs;
8380	  if (relocs == NULL)
8381	    {
8382	      bfd_size_type relsize;
8383	      relsize = htab->brlt->reloc_count * sizeof (*relocs);
8384	      relocs = bfd_alloc (htab->brlt->owner, relsize);
8385	      if (relocs == NULL)
8386		return FALSE;
8387	      elfsec_data->relocs = relocs;
8388	      elfsec_data->rel_hdr.sh_size = relsize;
8389	      elfsec_data->rel_hdr.sh_entsize = 24;
8390	      htab->brlt->reloc_count = 0;
8391	    }
8392	  r = relocs + htab->brlt->reloc_count;
8393	  htab->brlt->reloc_count += 1;
8394	  r->r_offset = (br_entry->offset
8395			 + htab->brlt->output_offset
8396			 + htab->brlt->output_section->vma);
8397	  r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8398	  r->r_addend = off;
8399	}
8400
8401      off = (br_entry->offset
8402	     + htab->brlt->output_offset
8403	     + htab->brlt->output_section->vma
8404	     - elf_gp (htab->brlt->output_section->owner)
8405	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
8406
8407      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8408	{
8409	  (*_bfd_error_handler)
8410	    (_("linkage table error against `%s'"),
8411	     stub_entry->root.string);
8412	  bfd_set_error (bfd_error_bad_value);
8413	  htab->stub_error = TRUE;
8414	  return FALSE;
8415	}
8416
8417      indx = off;
8418      if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8419	{
8420	  bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8421	  loc += 4;
8422	  bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8423	  size = 16;
8424	}
8425      else
8426	{
8427	  bfd_vma r2off;
8428
8429	  r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8430		   - htab->stub_group[stub_entry->id_sec->id].toc_off);
8431	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8432	  loc += 4;
8433	  bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8434	  loc += 4;
8435	  bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8436	  loc += 4;
8437	  bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8438	  loc += 4;
8439	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8440	  size = 28;
8441	}
8442      loc += 4;
8443      bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8444      loc += 4;
8445      bfd_put_32 (htab->stub_bfd, BCTR, loc);
8446      break;
8447
8448    case ppc_stub_plt_call:
8449      /* Do the best we can for shared libraries built without
8450	 exporting ".foo" for each "foo".  This can happen when symbol
8451	 versioning scripts strip all bar a subset of symbols.  */
8452      if (stub_entry->h->oh != NULL
8453	  && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8454	  && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8455	{
8456	  /* Point the symbol at the stub.  There may be multiple stubs,
8457	     we don't really care;  The main thing is to make this sym
8458	     defined somewhere.  Maybe defining the symbol in the stub
8459	     section is a silly idea.  If we didn't do this, htab->top_id
8460	     could disappear.  */
8461	  stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8462	  stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8463	  stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8464	}
8465
8466      /* Now build the stub.  */
8467      off = (bfd_vma) -1;
8468      for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8469	if (ent->addend == stub_entry->addend)
8470	  {
8471	    off = ent->plt.offset;
8472	    break;
8473	  }
8474      if (off >= (bfd_vma) -2)
8475	abort ();
8476
8477      off &= ~ (bfd_vma) 1;
8478      off += (htab->plt->output_offset
8479	      + htab->plt->output_section->vma
8480	      - elf_gp (htab->plt->output_section->owner)
8481	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
8482
8483      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8484	{
8485	  (*_bfd_error_handler)
8486	    (_("linkage table error against `%s'"),
8487	     stub_entry->h->elf.root.root.string);
8488	  bfd_set_error (bfd_error_bad_value);
8489	  htab->stub_error = TRUE;
8490	  return FALSE;
8491	}
8492
8493      p = build_plt_stub (htab->stub_bfd, loc, off);
8494      size = p - loc;
8495      break;
8496
8497    default:
8498      BFD_FAIL ();
8499      return FALSE;
8500    }
8501
8502  stub_entry->stub_sec->size += size;
8503
8504  if (htab->emit_stub_syms)
8505    {
8506      struct elf_link_hash_entry *h;
8507      size_t len1, len2;
8508      char *name;
8509      const char *const stub_str[] = { "long_branch",
8510				       "long_branch_r2off",
8511				       "plt_branch",
8512				       "plt_branch_r2off",
8513				       "plt_call" };
8514
8515      len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8516      len2 = strlen (stub_entry->root.string);
8517      name = bfd_malloc (len1 + len2 + 2);
8518      if (name == NULL)
8519	return FALSE;
8520      memcpy (name, stub_entry->root.string, 9);
8521      memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8522      memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8523      h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8524      if (h == NULL)
8525	return FALSE;
8526      if (h->root.type == bfd_link_hash_new)
8527	{
8528	  h->root.type = bfd_link_hash_defined;
8529	  h->root.u.def.section = stub_entry->stub_sec;
8530	  h->root.u.def.value = stub_entry->stub_offset;
8531	  h->ref_regular = 1;
8532	  h->def_regular = 1;
8533	  h->ref_regular_nonweak = 1;
8534	  h->forced_local = 1;
8535	  h->non_elf = 0;
8536	}
8537    }
8538
8539  return TRUE;
8540}
8541
8542/* As above, but don't actually build the stub.  Just bump offset so
8543   we know stub section sizes, and select plt_branch stubs where
8544   long_branch stubs won't do.  */
8545
8546static bfd_boolean
8547ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8548{
8549  struct ppc_stub_hash_entry *stub_entry;
8550  struct bfd_link_info *info;
8551  struct ppc_link_hash_table *htab;
8552  bfd_vma off;
8553  int size;
8554
8555  /* Massage our args to the form they really have.  */
8556  stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8557  info = in_arg;
8558
8559  htab = ppc_hash_table (info);
8560
8561  if (stub_entry->stub_type == ppc_stub_plt_call)
8562    {
8563      struct plt_entry *ent;
8564      off = (bfd_vma) -1;
8565      for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8566	if (ent->addend == stub_entry->addend)
8567	  {
8568	    off = ent->plt.offset & ~(bfd_vma) 1;
8569	    break;
8570	  }
8571      if (off >= (bfd_vma) -2)
8572	abort ();
8573      off += (htab->plt->output_offset
8574	      + htab->plt->output_section->vma
8575	      - elf_gp (htab->plt->output_section->owner)
8576	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
8577
8578      size = PLT_CALL_STUB_SIZE;
8579      if (PPC_HA (off + 16) != PPC_HA (off))
8580	size += 4;
8581    }
8582  else
8583    {
8584      /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8585	 variants.  */
8586      off = (stub_entry->target_value
8587	     + stub_entry->target_section->output_offset
8588	     + stub_entry->target_section->output_section->vma);
8589      off -= (stub_entry->stub_sec->size
8590	      + stub_entry->stub_sec->output_offset
8591	      + stub_entry->stub_sec->output_section->vma);
8592
8593      /* Reset the stub type from the plt variant in case we now
8594	 can reach with a shorter stub.  */
8595      if (stub_entry->stub_type >= ppc_stub_plt_branch)
8596	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8597
8598      size = 4;
8599      if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8600	{
8601	  off -= 12;
8602	  size = 16;
8603	}
8604
8605      /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
8606      if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8607	{
8608	  struct ppc_branch_hash_entry *br_entry;
8609
8610	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8611					     stub_entry->root.string + 9,
8612					     TRUE, FALSE);
8613	  if (br_entry == NULL)
8614	    {
8615	      (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8616				     stub_entry->root.string);
8617	      htab->stub_error = TRUE;
8618	      return FALSE;
8619	    }
8620
8621	  if (br_entry->iter != htab->stub_iteration)
8622	    {
8623	      br_entry->iter = htab->stub_iteration;
8624	      br_entry->offset = htab->brlt->size;
8625	      htab->brlt->size += 8;
8626
8627	      if (htab->relbrlt != NULL)
8628		htab->relbrlt->size += sizeof (Elf64_External_Rela);
8629	      else if (info->emitrelocations)
8630		{
8631		  htab->brlt->reloc_count += 1;
8632		  htab->brlt->flags |= SEC_RELOC;
8633		}
8634	    }
8635
8636	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8637	  size = 16;
8638	  if (stub_entry->stub_type != ppc_stub_plt_branch)
8639	    size = 28;
8640	}
8641      else if (info->emitrelocations)
8642	{
8643	  stub_entry->stub_sec->reloc_count += 1;
8644	  stub_entry->stub_sec->flags |= SEC_RELOC;
8645	}
8646    }
8647
8648  stub_entry->stub_sec->size += size;
8649  return TRUE;
8650}
8651
8652/* Set up various things so that we can make a list of input sections
8653   for each output section included in the link.  Returns -1 on error,
8654   0 when no stubs will be needed, and 1 on success.  */
8655
8656int
8657ppc64_elf_setup_section_lists (bfd *output_bfd,
8658			       struct bfd_link_info *info,
8659			       int no_multi_toc)
8660{
8661  bfd *input_bfd;
8662  int top_id, top_index, id;
8663  asection *section;
8664  asection **input_list;
8665  bfd_size_type amt;
8666  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8667
8668  htab->no_multi_toc = no_multi_toc;
8669
8670  if (htab->brlt == NULL)
8671    return 0;
8672
8673  /* Find the top input section id.  */
8674  for (input_bfd = info->input_bfds, top_id = 3;
8675       input_bfd != NULL;
8676       input_bfd = input_bfd->link_next)
8677    {
8678      for (section = input_bfd->sections;
8679	   section != NULL;
8680	   section = section->next)
8681	{
8682	  if (top_id < section->id)
8683	    top_id = section->id;
8684	}
8685    }
8686
8687  htab->top_id = top_id;
8688  amt = sizeof (struct map_stub) * (top_id + 1);
8689  htab->stub_group = bfd_zmalloc (amt);
8690  if (htab->stub_group == NULL)
8691    return -1;
8692
8693  /* Set toc_off for com, und, abs and ind sections.  */
8694  for (id = 0; id < 3; id++)
8695    htab->stub_group[id].toc_off = TOC_BASE_OFF;
8696
8697  elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8698
8699  /* We can't use output_bfd->section_count here to find the top output
8700     section index as some sections may have been removed, and
8701     strip_excluded_output_sections doesn't renumber the indices.  */
8702  for (section = output_bfd->sections, top_index = 0;
8703       section != NULL;
8704       section = section->next)
8705    {
8706      if (top_index < section->index)
8707	top_index = section->index;
8708    }
8709
8710  htab->top_index = top_index;
8711  amt = sizeof (asection *) * (top_index + 1);
8712  input_list = bfd_zmalloc (amt);
8713  htab->input_list = input_list;
8714  if (input_list == NULL)
8715    return -1;
8716
8717  return 1;
8718}
8719
8720/* The linker repeatedly calls this function for each TOC input section
8721   and linker generated GOT section.  Group input bfds such that the toc
8722   within a group is less than 64k in size.  Will break with cute linker
8723   scripts that play games with dot in the output toc section.  */
8724
8725void
8726ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8727{
8728  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8729
8730  if (!htab->no_multi_toc)
8731    {
8732      bfd_vma addr = isec->output_offset + isec->output_section->vma;
8733      bfd_vma off = addr - htab->toc_curr;
8734
8735      if (off + isec->size > 0x10000)
8736	htab->toc_curr = addr;
8737
8738      elf_gp (isec->owner) = (htab->toc_curr
8739			      - elf_gp (isec->output_section->owner)
8740			      + TOC_BASE_OFF);
8741    }
8742}
8743
8744/* Called after the last call to the above function.  */
8745
8746void
8747ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8748{
8749  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8750
8751  htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8752
8753  /* toc_curr tracks the TOC offset used for code sections below in
8754     ppc64_elf_next_input_section.  Start off at 0x8000.  */
8755  htab->toc_curr = TOC_BASE_OFF;
8756}
8757
8758/* No toc references were found in ISEC.  If the code in ISEC makes no
8759   calls, then there's no need to use toc adjusting stubs when branching
8760   into ISEC.  Actually, indirect calls from ISEC are OK as they will
8761   load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
8762   needed, and 2 if a cyclical call-graph was found but no other reason
8763   for a stub was detected.  If called from the top level, a return of
8764   2 means the same as a return of 0.  */
8765
8766static int
8767toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8768{
8769  Elf_Internal_Rela *relstart, *rel;
8770  Elf_Internal_Sym *local_syms;
8771  int ret;
8772  struct ppc_link_hash_table *htab;
8773
8774  /* We know none of our code bearing sections will need toc stubs.  */
8775  if ((isec->flags & SEC_LINKER_CREATED) != 0)
8776    return 0;
8777
8778  if (isec->size == 0)
8779    return 0;
8780
8781  if (isec->output_section == NULL)
8782    return 0;
8783
8784  /* Hack for linux kernel.  .fixup contains branches, but only back to
8785     the function that hit an exception.  */
8786  if (strcmp (isec->name, ".fixup") == 0)
8787    return 0;
8788
8789  if (isec->reloc_count == 0)
8790    return 0;
8791
8792  relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8793					info->keep_memory);
8794  if (relstart == NULL)
8795    return -1;
8796
8797  /* Look for branches to outside of this section.  */
8798  local_syms = NULL;
8799  ret = 0;
8800  htab = ppc_hash_table (info);
8801  for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8802    {
8803      enum elf_ppc64_reloc_type r_type;
8804      unsigned long r_symndx;
8805      struct elf_link_hash_entry *h;
8806      Elf_Internal_Sym *sym;
8807      asection *sym_sec;
8808      long *opd_adjust;
8809      bfd_vma sym_value;
8810      bfd_vma dest;
8811
8812      r_type = ELF64_R_TYPE (rel->r_info);
8813      if (r_type != R_PPC64_REL24
8814	  && r_type != R_PPC64_REL14
8815	  && r_type != R_PPC64_REL14_BRTAKEN
8816	  && r_type != R_PPC64_REL14_BRNTAKEN)
8817	continue;
8818
8819      r_symndx = ELF64_R_SYM (rel->r_info);
8820      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8821		      isec->owner))
8822	{
8823	  ret = -1;
8824	  break;
8825	}
8826
8827      /* Calls to dynamic lib functions go through a plt call stub
8828	 that uses r2.  Branches to undefined symbols might be a call
8829	 using old-style dot symbols that can be satisfied by a plt
8830	 call into a new-style dynamic library.  */
8831      if (sym_sec == NULL)
8832	{
8833	  struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8834	  if (eh != NULL
8835	      && eh->oh != NULL
8836	      && eh->oh->elf.plt.plist != NULL)
8837	    {
8838	      ret = 1;
8839	      break;
8840	    }
8841
8842	  /* Ignore other undefined symbols.  */
8843	  continue;
8844	}
8845
8846      /* Assume branches to other sections not included in the link need
8847	 stubs too, to cover -R and absolute syms.  */
8848      if (sym_sec->output_section == NULL)
8849	{
8850	  ret = 1;
8851	  break;
8852	}
8853
8854      if (h == NULL)
8855	sym_value = sym->st_value;
8856      else
8857	{
8858	  if (h->root.type != bfd_link_hash_defined
8859	      && h->root.type != bfd_link_hash_defweak)
8860	    abort ();
8861	  sym_value = h->root.u.def.value;
8862	}
8863      sym_value += rel->r_addend;
8864
8865      /* If this branch reloc uses an opd sym, find the code section.  */
8866      opd_adjust = get_opd_info (sym_sec);
8867      if (opd_adjust != NULL)
8868	{
8869	  if (h == NULL)
8870	    {
8871	      long adjust;
8872
8873	      adjust = opd_adjust[sym->st_value / 8];
8874	      if (adjust == -1)
8875		/* Assume deleted functions won't ever be called.  */
8876		continue;
8877	      sym_value += adjust;
8878	    }
8879
8880	  dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8881	  if (dest == (bfd_vma) -1)
8882	    continue;
8883	}
8884      else
8885	dest = (sym_value
8886		+ sym_sec->output_offset
8887		+ sym_sec->output_section->vma);
8888
8889      /* Ignore branch to self.  */
8890      if (sym_sec == isec)
8891	continue;
8892
8893      /* If the called function uses the toc, we need a stub.  */
8894      if (sym_sec->has_toc_reloc
8895	  || sym_sec->makes_toc_func_call)
8896	{
8897	  ret = 1;
8898	  break;
8899	}
8900
8901      /* Assume any branch that needs a long branch stub might in fact
8902	 need a plt_branch stub.  A plt_branch stub uses r2.  */
8903      else if (dest - (isec->output_offset
8904		       + isec->output_section->vma
8905		       + rel->r_offset) + (1 << 25) >= (2 << 25))
8906	{
8907	  ret = 1;
8908	  break;
8909	}
8910
8911      /* If calling back to a section in the process of being tested, we
8912	 can't say for sure that no toc adjusting stubs are needed, so
8913	 don't return zero.  */
8914      else if (sym_sec->call_check_in_progress)
8915	ret = 2;
8916
8917      /* Branches to another section that itself doesn't have any TOC
8918	 references are OK.  Recursively call ourselves to check.  */
8919      else if (sym_sec->id <= htab->top_id
8920	       && htab->stub_group[sym_sec->id].toc_off == 0)
8921	{
8922	  int recur;
8923
8924	  /* Mark current section as indeterminate, so that other
8925	     sections that call back to current won't be marked as
8926	     known.  */
8927	  isec->call_check_in_progress = 1;
8928	  recur = toc_adjusting_stub_needed (info, sym_sec);
8929	  isec->call_check_in_progress = 0;
8930
8931	  if (recur < 0)
8932	    {
8933	      /* An error.  Exit.  */
8934	      ret = -1;
8935	      break;
8936	    }
8937	  else if (recur <= 1)
8938	    {
8939	      /* Known result.  Mark as checked and set section flag.  */
8940	      htab->stub_group[sym_sec->id].toc_off = 1;
8941	      if (recur != 0)
8942		{
8943		  sym_sec->makes_toc_func_call = 1;
8944		  ret = 1;
8945		  break;
8946		}
8947	    }
8948	  else
8949	    {
8950	      /* Unknown result.  Continue checking.  */
8951	      ret = 2;
8952	    }
8953	}
8954    }
8955
8956  if (local_syms != NULL
8957      && (elf_tdata (isec->owner)->symtab_hdr.contents
8958	  != (unsigned char *) local_syms))
8959    free (local_syms);
8960  if (elf_section_data (isec)->relocs != relstart)
8961    free (relstart);
8962
8963  return ret;
8964}
8965
8966/* The linker repeatedly calls this function for each input section,
8967   in the order that input sections are linked into output sections.
8968   Build lists of input sections to determine groupings between which
8969   we may insert linker stubs.  */
8970
8971bfd_boolean
8972ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8973{
8974  struct ppc_link_hash_table *htab = ppc_hash_table (info);
8975
8976  if ((isec->output_section->flags & SEC_CODE) != 0
8977      && isec->output_section->index <= htab->top_index)
8978    {
8979      asection **list = htab->input_list + isec->output_section->index;
8980      /* Steal the link_sec pointer for our list.  */
8981#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8982      /* This happens to make the list in reverse order,
8983	 which is what we want.  */
8984      PREV_SEC (isec) = *list;
8985      *list = isec;
8986    }
8987
8988  if (htab->multi_toc_needed)
8989    {
8990      /* If a code section has a function that uses the TOC then we need
8991	 to use the right TOC (obviously).  Also, make sure that .opd gets
8992	 the correct TOC value for R_PPC64_TOC relocs that don't have or
8993	 can't find their function symbol (shouldn't ever happen now).  */
8994      if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8995	{
8996	  if (elf_gp (isec->owner) != 0)
8997	    htab->toc_curr = elf_gp (isec->owner);
8998	}
8999      else if (htab->stub_group[isec->id].toc_off == 0)
9000	{
9001	  int ret = toc_adjusting_stub_needed (info, isec);
9002	  if (ret < 0)
9003	    return FALSE;
9004	  else
9005	    isec->makes_toc_func_call = ret & 1;
9006	}
9007    }
9008
9009  /* Functions that don't use the TOC can belong in any TOC group.
9010     Use the last TOC base.  This happens to make _init and _fini
9011     pasting work.  */
9012  htab->stub_group[isec->id].toc_off = htab->toc_curr;
9013  return TRUE;
9014}
9015
9016/* See whether we can group stub sections together.  Grouping stub
9017   sections may result in fewer stubs.  More importantly, we need to
9018   put all .init* and .fini* stubs at the beginning of the .init or
9019   .fini output sections respectively, because glibc splits the
9020   _init and _fini functions into multiple parts.  Putting a stub in
9021   the middle of a function is not a good idea.  */
9022
9023static void
9024group_sections (struct ppc_link_hash_table *htab,
9025		bfd_size_type stub_group_size,
9026		bfd_boolean stubs_always_before_branch)
9027{
9028  asection **list;
9029  bfd_size_type stub14_group_size;
9030  bfd_boolean suppress_size_errors;
9031
9032  suppress_size_errors = FALSE;
9033  stub14_group_size = stub_group_size;
9034  if (stub_group_size == 1)
9035    {
9036      /* Default values.  */
9037      if (stubs_always_before_branch)
9038	{
9039	  stub_group_size = 0x1e00000;
9040	  stub14_group_size = 0x7800;
9041	}
9042      else
9043	{
9044	  stub_group_size = 0x1c00000;
9045	  stub14_group_size = 0x7000;
9046	}
9047      suppress_size_errors = TRUE;
9048    }
9049
9050  list = htab->input_list + htab->top_index;
9051  do
9052    {
9053      asection *tail = *list;
9054      while (tail != NULL)
9055	{
9056	  asection *curr;
9057	  asection *prev;
9058	  bfd_size_type total;
9059	  bfd_boolean big_sec;
9060	  bfd_vma curr_toc;
9061
9062	  curr = tail;
9063	  total = tail->size;
9064	  big_sec = total > (ppc64_elf_section_data (tail)->has_14bit_branch
9065			     ? stub14_group_size : stub_group_size);
9066	  if (big_sec && !suppress_size_errors)
9067	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
9068				     tail->owner, tail);
9069	  curr_toc = htab->stub_group[tail->id].toc_off;
9070
9071	  while ((prev = PREV_SEC (curr)) != NULL
9072		 && ((total += curr->output_offset - prev->output_offset)
9073		     < (ppc64_elf_section_data (prev)->has_14bit_branch
9074			? stub14_group_size : stub_group_size))
9075		 && htab->stub_group[prev->id].toc_off == curr_toc)
9076	    curr = prev;
9077
9078	  /* OK, the size from the start of CURR to the end is less
9079	     than stub_group_size and thus can be handled by one stub
9080	     section.  (or the tail section is itself larger than
9081	     stub_group_size, in which case we may be toast.)  We
9082	     should really be keeping track of the total size of stubs
9083	     added here, as stubs contribute to the final output
9084	     section size.  That's a little tricky, and this way will
9085	     only break if stubs added make the total size more than
9086	     2^25, ie. for the default stub_group_size, if stubs total
9087	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
9088	  do
9089	    {
9090	      prev = PREV_SEC (tail);
9091	      /* Set up this stub group.  */
9092	      htab->stub_group[tail->id].link_sec = curr;
9093	    }
9094	  while (tail != curr && (tail = prev) != NULL);
9095
9096	  /* But wait, there's more!  Input sections up to stub_group_size
9097	     bytes before the stub section can be handled by it too.
9098	     Don't do this if we have a really large section after the
9099	     stubs, as adding more stubs increases the chance that
9100	     branches may not reach into the stub section.  */
9101	  if (!stubs_always_before_branch && !big_sec)
9102	    {
9103	      total = 0;
9104	      while (prev != NULL
9105		     && ((total += tail->output_offset - prev->output_offset)
9106			 < (ppc64_elf_section_data (prev)->has_14bit_branch
9107			    ? stub14_group_size : stub_group_size))
9108		     && htab->stub_group[prev->id].toc_off == curr_toc)
9109		{
9110		  tail = prev;
9111		  prev = PREV_SEC (tail);
9112		  htab->stub_group[tail->id].link_sec = curr;
9113		}
9114	    }
9115	  tail = prev;
9116	}
9117    }
9118  while (list-- != htab->input_list);
9119  free (htab->input_list);
9120#undef PREV_SEC
9121}
9122
9123/* Determine and set the size of the stub section for a final link.
9124
9125   The basic idea here is to examine all the relocations looking for
9126   PC-relative calls to a target that is unreachable with a "bl"
9127   instruction.  */
9128
9129bfd_boolean
9130ppc64_elf_size_stubs (bfd *output_bfd,
9131		      struct bfd_link_info *info,
9132		      bfd_signed_vma group_size,
9133		      asection *(*add_stub_section) (const char *, asection *),
9134		      void (*layout_sections_again) (void))
9135{
9136  bfd_size_type stub_group_size;
9137  bfd_boolean stubs_always_before_branch;
9138  struct ppc_link_hash_table *htab = ppc_hash_table (info);
9139
9140  /* Stash our params away.  */
9141  htab->add_stub_section = add_stub_section;
9142  htab->layout_sections_again = layout_sections_again;
9143  stubs_always_before_branch = group_size < 0;
9144  if (group_size < 0)
9145    stub_group_size = -group_size;
9146  else
9147    stub_group_size = group_size;
9148
9149  group_sections (htab, stub_group_size, stubs_always_before_branch);
9150
9151  while (1)
9152    {
9153      bfd *input_bfd;
9154      unsigned int bfd_indx;
9155      asection *stub_sec;
9156
9157      htab->stub_iteration += 1;
9158
9159      for (input_bfd = info->input_bfds, bfd_indx = 0;
9160	   input_bfd != NULL;
9161	   input_bfd = input_bfd->link_next, bfd_indx++)
9162	{
9163	  Elf_Internal_Shdr *symtab_hdr;
9164	  asection *section;
9165	  Elf_Internal_Sym *local_syms = NULL;
9166
9167	  if (!is_ppc64_elf_target (input_bfd->xvec))
9168	    continue;
9169
9170	  /* We'll need the symbol table in a second.  */
9171	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9172	  if (symtab_hdr->sh_info == 0)
9173	    continue;
9174
9175	  /* Walk over each section attached to the input bfd.  */
9176	  for (section = input_bfd->sections;
9177	       section != NULL;
9178	       section = section->next)
9179	    {
9180	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
9181
9182	      /* If there aren't any relocs, then there's nothing more
9183		 to do.  */
9184	      if ((section->flags & SEC_RELOC) == 0
9185		  || (section->flags & SEC_ALLOC) == 0
9186		  || (section->flags & SEC_LOAD) == 0
9187		  || (section->flags & SEC_CODE) == 0
9188		  || section->reloc_count == 0)
9189		continue;
9190
9191	      /* If this section is a link-once section that will be
9192		 discarded, then don't create any stubs.  */
9193	      if (section->output_section == NULL
9194		  || section->output_section->owner != output_bfd)
9195		continue;
9196
9197	      /* Get the relocs.  */
9198	      internal_relocs
9199		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
9200					     info->keep_memory);
9201	      if (internal_relocs == NULL)
9202		goto error_ret_free_local;
9203
9204	      /* Now examine each relocation.  */
9205	      irela = internal_relocs;
9206	      irelaend = irela + section->reloc_count;
9207	      for (; irela < irelaend; irela++)
9208		{
9209		  enum elf_ppc64_reloc_type r_type;
9210		  unsigned int r_indx;
9211		  enum ppc_stub_type stub_type;
9212		  struct ppc_stub_hash_entry *stub_entry;
9213		  asection *sym_sec, *code_sec;
9214		  bfd_vma sym_value;
9215		  bfd_vma destination;
9216		  bfd_boolean ok_dest;
9217		  struct ppc_link_hash_entry *hash;
9218		  struct ppc_link_hash_entry *fdh;
9219		  struct elf_link_hash_entry *h;
9220		  Elf_Internal_Sym *sym;
9221		  char *stub_name;
9222		  const asection *id_sec;
9223		  long *opd_adjust;
9224
9225		  r_type = ELF64_R_TYPE (irela->r_info);
9226		  r_indx = ELF64_R_SYM (irela->r_info);
9227
9228		  if (r_type >= R_PPC64_max)
9229		    {
9230		      bfd_set_error (bfd_error_bad_value);
9231		      goto error_ret_free_internal;
9232		    }
9233
9234		  /* Only look for stubs on branch instructions.  */
9235		  if (r_type != R_PPC64_REL24
9236		      && r_type != R_PPC64_REL14
9237		      && r_type != R_PPC64_REL14_BRTAKEN
9238		      && r_type != R_PPC64_REL14_BRNTAKEN)
9239		    continue;
9240
9241		  /* Now determine the call target, its name, value,
9242		     section.  */
9243		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9244				  r_indx, input_bfd))
9245		    goto error_ret_free_internal;
9246		  hash = (struct ppc_link_hash_entry *) h;
9247
9248		  ok_dest = FALSE;
9249		  fdh = NULL;
9250		  sym_value = 0;
9251		  if (hash == NULL)
9252		    {
9253		      sym_value = sym->st_value;
9254		      ok_dest = TRUE;
9255		    }
9256		  else if (hash->elf.root.type == bfd_link_hash_defined
9257			   || hash->elf.root.type == bfd_link_hash_defweak)
9258		    {
9259		      sym_value = hash->elf.root.u.def.value;
9260		      if (sym_sec->output_section != NULL)
9261			ok_dest = TRUE;
9262		    }
9263		  else if (hash->elf.root.type == bfd_link_hash_undefweak
9264			   || hash->elf.root.type == bfd_link_hash_undefined)
9265		    {
9266		      /* Recognise an old ABI func code entry sym, and
9267			 use the func descriptor sym instead if it is
9268			 defined.  */
9269		      if (hash->elf.root.root.string[0] == '.'
9270			  && (fdh = get_fdh (hash, htab)) != NULL)
9271			{
9272			  if (fdh->elf.root.type == bfd_link_hash_defined
9273			      || fdh->elf.root.type == bfd_link_hash_defweak)
9274			    {
9275			      sym_sec = fdh->elf.root.u.def.section;
9276			      sym_value = fdh->elf.root.u.def.value;
9277			      if (sym_sec->output_section != NULL)
9278				ok_dest = TRUE;
9279			    }
9280			  else
9281			    fdh = NULL;
9282			}
9283		    }
9284		  else
9285		    {
9286		      bfd_set_error (bfd_error_bad_value);
9287		      goto error_ret_free_internal;
9288		    }
9289
9290		  destination = 0;
9291		  if (ok_dest)
9292		    {
9293		      sym_value += irela->r_addend;
9294		      destination = (sym_value
9295				     + sym_sec->output_offset
9296				     + sym_sec->output_section->vma);
9297		    }
9298
9299		  code_sec = sym_sec;
9300		  opd_adjust = get_opd_info (sym_sec);
9301		  if (opd_adjust != NULL)
9302		    {
9303		      bfd_vma dest;
9304
9305		      if (hash == NULL)
9306			{
9307			  long adjust = opd_adjust[sym_value / 8];
9308			  if (adjust == -1)
9309			    continue;
9310			  sym_value += adjust;
9311			}
9312		      dest = opd_entry_value (sym_sec, sym_value,
9313					      &code_sec, &sym_value);
9314		      if (dest != (bfd_vma) -1)
9315			{
9316			  destination = dest;
9317			  if (fdh != NULL)
9318			    {
9319			      /* Fixup old ABI sym to point at code
9320				 entry.  */
9321			      hash->elf.root.type = bfd_link_hash_defweak;
9322			      hash->elf.root.u.def.section = code_sec;
9323			      hash->elf.root.u.def.value = sym_value;
9324			    }
9325			}
9326		    }
9327
9328		  /* Determine what (if any) linker stub is needed.  */
9329		  stub_type = ppc_type_of_stub (section, irela, &hash,
9330						destination);
9331
9332		  if (stub_type != ppc_stub_plt_call)
9333		    {
9334		      /* Check whether we need a TOC adjusting stub.
9335			 Since the linker pastes together pieces from
9336			 different object files when creating the
9337			 _init and _fini functions, it may be that a
9338			 call to what looks like a local sym is in
9339			 fact a call needing a TOC adjustment.  */
9340		      if (code_sec != NULL
9341			  && code_sec->output_section != NULL
9342			  && (htab->stub_group[code_sec->id].toc_off
9343			      != htab->stub_group[section->id].toc_off)
9344			  && (code_sec->has_toc_reloc
9345			      || code_sec->makes_toc_func_call))
9346			stub_type = ppc_stub_long_branch_r2off;
9347		    }
9348
9349		  if (stub_type == ppc_stub_none)
9350		    continue;
9351
9352		  /* __tls_get_addr calls might be eliminated.  */
9353		  if (stub_type != ppc_stub_plt_call
9354		      && hash != NULL
9355		      && (hash == htab->tls_get_addr
9356			  || hash == htab->tls_get_addr_fd)
9357		      && section->has_tls_reloc
9358		      && irela != internal_relocs)
9359		    {
9360		      /* Get tls info.  */
9361		      char *tls_mask;
9362
9363		      if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9364					 irela - 1, input_bfd))
9365			goto error_ret_free_internal;
9366		      if (*tls_mask != 0)
9367			continue;
9368		    }
9369
9370		  /* Support for grouping stub sections.  */
9371		  id_sec = htab->stub_group[section->id].link_sec;
9372
9373		  /* Get the name of this stub.  */
9374		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9375		  if (!stub_name)
9376		    goto error_ret_free_internal;
9377
9378		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9379						     stub_name, FALSE, FALSE);
9380		  if (stub_entry != NULL)
9381		    {
9382		      /* The proper stub has already been created.  */
9383		      free (stub_name);
9384		      continue;
9385		    }
9386
9387		  stub_entry = ppc_add_stub (stub_name, section, htab);
9388		  if (stub_entry == NULL)
9389		    {
9390		      free (stub_name);
9391		    error_ret_free_internal:
9392		      if (elf_section_data (section)->relocs == NULL)
9393			free (internal_relocs);
9394		    error_ret_free_local:
9395		      if (local_syms != NULL
9396			  && (symtab_hdr->contents
9397			      != (unsigned char *) local_syms))
9398			free (local_syms);
9399		      return FALSE;
9400		    }
9401
9402		  stub_entry->stub_type = stub_type;
9403		  stub_entry->target_value = sym_value;
9404		  stub_entry->target_section = code_sec;
9405		  stub_entry->h = hash;
9406		  stub_entry->addend = irela->r_addend;
9407
9408		  if (stub_entry->h != NULL)
9409		    htab->stub_globals += 1;
9410		}
9411
9412	      /* We're done with the internal relocs, free them.  */
9413	      if (elf_section_data (section)->relocs != internal_relocs)
9414		free (internal_relocs);
9415	    }
9416
9417	  if (local_syms != NULL
9418	      && symtab_hdr->contents != (unsigned char *) local_syms)
9419	    {
9420	      if (!info->keep_memory)
9421		free (local_syms);
9422	      else
9423		symtab_hdr->contents = (unsigned char *) local_syms;
9424	    }
9425	}
9426
9427      /* We may have added some stubs.  Find out the new size of the
9428	 stub sections.  */
9429      for (stub_sec = htab->stub_bfd->sections;
9430	   stub_sec != NULL;
9431	   stub_sec = stub_sec->next)
9432	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9433	  {
9434	    stub_sec->rawsize = stub_sec->size;
9435	    stub_sec->size = 0;
9436	    stub_sec->reloc_count = 0;
9437	    stub_sec->flags &= ~SEC_RELOC;
9438	  }
9439
9440      htab->brlt->size = 0;
9441      htab->brlt->reloc_count = 0;
9442      htab->brlt->flags &= ~SEC_RELOC;
9443      if (htab->relbrlt != NULL)
9444	htab->relbrlt->size = 0;
9445
9446      bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9447
9448      for (stub_sec = htab->stub_bfd->sections;
9449	   stub_sec != NULL;
9450	   stub_sec = stub_sec->next)
9451	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9452	    && stub_sec->rawsize != stub_sec->size)
9453	  break;
9454
9455      /* Exit from this loop when no stubs have been added, and no stubs
9456	 have changed size.  */
9457      if (stub_sec == NULL)
9458	break;
9459
9460      /* Ask the linker to do its stuff.  */
9461      (*htab->layout_sections_again) ();
9462    }
9463
9464  /* It would be nice to strip htab->brlt from the output if the
9465     section is empty, but it's too late.  If we strip sections here,
9466     the dynamic symbol table is corrupted since the section symbol
9467     for the stripped section isn't written.  */
9468
9469  return TRUE;
9470}
9471
9472/* Called after we have determined section placement.  If sections
9473   move, we'll be called again.  Provide a value for TOCstart.  */
9474
9475bfd_vma
9476ppc64_elf_toc (bfd *obfd)
9477{
9478  asection *s;
9479  bfd_vma TOCstart;
9480
9481  /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9482     order.  The TOC starts where the first of these sections starts.  */
9483  s = bfd_get_section_by_name (obfd, ".got");
9484  if (s == NULL)
9485    s = bfd_get_section_by_name (obfd, ".toc");
9486  if (s == NULL)
9487    s = bfd_get_section_by_name (obfd, ".tocbss");
9488  if (s == NULL)
9489    s = bfd_get_section_by_name (obfd, ".plt");
9490  if (s == NULL)
9491    {
9492      /* This may happen for
9493	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
9494	 .toc directive
9495	 o  bad linker script
9496	 o --gc-sections and empty TOC sections
9497
9498	 FIXME: Warn user?  */
9499
9500      /* Look for a likely section.  We probably won't even be
9501	 using TOCstart.  */
9502      for (s = obfd->sections; s != NULL; s = s->next)
9503	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9504	    == (SEC_ALLOC | SEC_SMALL_DATA))
9505	  break;
9506      if (s == NULL)
9507	for (s = obfd->sections; s != NULL; s = s->next)
9508	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9509	      == (SEC_ALLOC | SEC_SMALL_DATA))
9510	    break;
9511      if (s == NULL)
9512	for (s = obfd->sections; s != NULL; s = s->next)
9513	  if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9514	    break;
9515      if (s == NULL)
9516	for (s = obfd->sections; s != NULL; s = s->next)
9517	  if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9518	    break;
9519    }
9520
9521  TOCstart = 0;
9522  if (s != NULL)
9523    TOCstart = s->output_section->vma + s->output_offset;
9524
9525  return TOCstart;
9526}
9527
9528/* Build all the stubs associated with the current output file.
9529   The stubs are kept in a hash table attached to the main linker
9530   hash table.  This function is called via gldelf64ppc_finish.  */
9531
9532bfd_boolean
9533ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9534		       struct bfd_link_info *info,
9535		       char **stats)
9536{
9537  struct ppc_link_hash_table *htab = ppc_hash_table (info);
9538  asection *stub_sec;
9539  bfd_byte *p;
9540  int stub_sec_count = 0;
9541
9542  htab->emit_stub_syms = emit_stub_syms;
9543
9544  /* Allocate memory to hold the linker stubs.  */
9545  for (stub_sec = htab->stub_bfd->sections;
9546       stub_sec != NULL;
9547       stub_sec = stub_sec->next)
9548    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9549	&& stub_sec->size != 0)
9550      {
9551	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9552	if (stub_sec->contents == NULL)
9553	  return FALSE;
9554	/* We want to check that built size is the same as calculated
9555	   size.  rawsize is a convenient location to use.  */
9556	stub_sec->rawsize = stub_sec->size;
9557	stub_sec->size = 0;
9558      }
9559
9560  if (htab->glink != NULL && htab->glink->size != 0)
9561    {
9562      unsigned int indx;
9563      bfd_vma plt0;
9564
9565      /* Build the .glink plt call stub.  */
9566      if (htab->emit_stub_syms)
9567	{
9568	  struct elf_link_hash_entry *h;
9569	  h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9570	  if (h == NULL)
9571	    return FALSE;
9572	  if (h->root.type == bfd_link_hash_new)
9573	    {
9574	      h->root.type = bfd_link_hash_defined;
9575	      h->root.u.def.section = htab->glink;
9576	      h->root.u.def.value = 8;
9577	      h->ref_regular = 1;
9578	      h->def_regular = 1;
9579	      h->ref_regular_nonweak = 1;
9580	      h->forced_local = 1;
9581	      h->non_elf = 0;
9582	    }
9583	}
9584      p = htab->glink->contents;
9585      plt0 = (htab->plt->output_section->vma
9586	      + htab->plt->output_offset
9587	      - (htab->glink->output_section->vma
9588		 + htab->glink->output_offset
9589		 + 16));
9590      bfd_put_64 (htab->glink->owner, plt0, p);
9591      p += 8;
9592      bfd_put_32 (htab->glink->owner, MFLR_R12, p);
9593      p += 4;
9594      bfd_put_32 (htab->glink->owner, BCL_20_31, p);
9595      p += 4;
9596      bfd_put_32 (htab->glink->owner, MFLR_R11, p);
9597      p += 4;
9598      bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
9599      p += 4;
9600      bfd_put_32 (htab->glink->owner, MTLR_R12, p);
9601      p += 4;
9602      bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
9603      p += 4;
9604      bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
9605      p += 4;
9606      bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9607      p += 4;
9608      bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9609      p += 4;
9610      bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9611      p += 4;
9612      bfd_put_32 (htab->glink->owner, BCTR, p);
9613      p += 4;
9614      while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
9615	{
9616	  bfd_put_32 (htab->glink->owner, NOP, p);
9617	  p += 4;
9618	}
9619
9620      /* Build the .glink lazy link call stubs.  */
9621      indx = 0;
9622      while (p < htab->glink->contents + htab->glink->size)
9623	{
9624	  if (indx < 0x8000)
9625	    {
9626	      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9627	      p += 4;
9628	    }
9629	  else
9630	    {
9631	      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9632	      p += 4;
9633	      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9634	      p += 4;
9635	    }
9636	  bfd_put_32 (htab->glink->owner,
9637		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
9638	  indx++;
9639	  p += 4;
9640	}
9641      htab->glink->rawsize = p - htab->glink->contents;
9642    }
9643
9644  if (htab->brlt->size != 0)
9645    {
9646      htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9647					 htab->brlt->size);
9648      if (htab->brlt->contents == NULL)
9649	return FALSE;
9650    }
9651  if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9652    {
9653      htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9654					    htab->relbrlt->size);
9655      if (htab->relbrlt->contents == NULL)
9656	return FALSE;
9657    }
9658
9659  /* Build the stubs as directed by the stub hash table.  */
9660  bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9661
9662  if (htab->relbrlt != NULL)
9663    htab->relbrlt->reloc_count = 0;
9664
9665  for (stub_sec = htab->stub_bfd->sections;
9666       stub_sec != NULL;
9667       stub_sec = stub_sec->next)
9668    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9669      {
9670	stub_sec_count += 1;
9671	if (stub_sec->rawsize != stub_sec->size)
9672	  break;
9673      }
9674
9675  if (stub_sec != NULL
9676      || htab->glink->rawsize != htab->glink->size)
9677    {
9678      htab->stub_error = TRUE;
9679      (*_bfd_error_handler) (_("stubs don't match calculated size"));
9680    }
9681
9682  if (htab->stub_error)
9683    return FALSE;
9684
9685  if (stats != NULL)
9686    {
9687      *stats = bfd_malloc (500);
9688      if (*stats == NULL)
9689	return FALSE;
9690
9691      sprintf (*stats, _("linker stubs in %u group%s\n"
9692			 "  branch       %lu\n"
9693			 "  toc adjust   %lu\n"
9694			 "  long branch  %lu\n"
9695			 "  long toc adj %lu\n"
9696			 "  plt call     %lu"),
9697	       stub_sec_count,
9698	       stub_sec_count == 1 ? "" : "s",
9699	       htab->stub_count[ppc_stub_long_branch - 1],
9700	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
9701	       htab->stub_count[ppc_stub_plt_branch - 1],
9702	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9703	       htab->stub_count[ppc_stub_plt_call - 1]);
9704    }
9705  return TRUE;
9706}
9707
9708/* This function undoes the changes made by add_symbol_adjust.  */
9709
9710static bfd_boolean
9711undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9712{
9713  struct ppc_link_hash_entry *eh;
9714
9715  if (h->root.type == bfd_link_hash_indirect)
9716    return TRUE;
9717
9718  if (h->root.type == bfd_link_hash_warning)
9719    h = (struct elf_link_hash_entry *) h->root.u.i.link;
9720
9721  eh = (struct ppc_link_hash_entry *) h;
9722  if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9723    return TRUE;
9724
9725  eh->elf.root.type = bfd_link_hash_undefined;
9726  return TRUE;
9727}
9728
9729void
9730ppc64_elf_restore_symbols (struct bfd_link_info *info)
9731{
9732  struct ppc_link_hash_table *htab = ppc_hash_table (info);
9733  elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9734}
9735
9736/* What to do when ld finds relocations against symbols defined in
9737   discarded sections.  */
9738
9739static unsigned int
9740ppc64_elf_action_discarded (asection *sec)
9741{
9742  if (strcmp (".opd", sec->name) == 0)
9743    return 0;
9744
9745  if (strcmp (".toc", sec->name) == 0)
9746    return 0;
9747
9748  if (strcmp (".toc1", sec->name) == 0)
9749    return 0;
9750
9751  if (sec->flags & SEC_RELOC)
9752    return 0;
9753
9754  return _bfd_elf_default_action_discarded (sec);
9755}
9756
9757/* The RELOCATE_SECTION function is called by the ELF backend linker
9758   to handle the relocations for a section.
9759
9760   The relocs are always passed as Rela structures; if the section
9761   actually uses Rel structures, the r_addend field will always be
9762   zero.
9763
9764   This function is responsible for adjust the section contents as
9765   necessary, and (if using Rela relocs and generating a
9766   relocatable output file) adjusting the reloc addend as
9767   necessary.
9768
9769   This function does not have to worry about setting the reloc
9770   address or the reloc symbol index.
9771
9772   LOCAL_SYMS is a pointer to the swapped in local symbols.
9773
9774   LOCAL_SECTIONS is an array giving the section in the input file
9775   corresponding to the st_shndx field of each local symbol.
9776
9777   The global hash table entry for the global symbols can be found
9778   via elf_sym_hashes (input_bfd).
9779
9780   When generating relocatable output, this function must handle
9781   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
9782   going to be the section symbol corresponding to the output
9783   section, which means that the addend must be adjusted
9784   accordingly.  */
9785
9786static bfd_boolean
9787ppc64_elf_relocate_section (bfd *output_bfd,
9788			    struct bfd_link_info *info,
9789			    bfd *input_bfd,
9790			    asection *input_section,
9791			    bfd_byte *contents,
9792			    Elf_Internal_Rela *relocs,
9793			    Elf_Internal_Sym *local_syms,
9794			    asection **local_sections)
9795{
9796  struct ppc_link_hash_table *htab;
9797  Elf_Internal_Shdr *symtab_hdr;
9798  struct elf_link_hash_entry **sym_hashes;
9799  Elf_Internal_Rela *rel;
9800  Elf_Internal_Rela *relend;
9801  Elf_Internal_Rela outrel;
9802  bfd_byte *loc;
9803  struct got_entry **local_got_ents;
9804  bfd_vma TOCstart;
9805  bfd_boolean ret = TRUE;
9806  bfd_boolean is_opd;
9807  /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
9808  bfd_boolean is_power4 = FALSE;
9809  bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
9810
9811  /* Initialize howto table if needed.  */
9812  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9813    ppc_howto_init ();
9814
9815  htab = ppc_hash_table (info);
9816
9817  /* Don't relocate stub sections.  */
9818  if (input_section->owner == htab->stub_bfd)
9819    return TRUE;
9820
9821  local_got_ents = elf_local_got_ents (input_bfd);
9822  TOCstart = elf_gp (output_bfd);
9823  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9824  sym_hashes = elf_sym_hashes (input_bfd);
9825  is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
9826
9827  rel = relocs;
9828  relend = relocs + input_section->reloc_count;
9829  for (; rel < relend; rel++)
9830    {
9831      enum elf_ppc64_reloc_type r_type;
9832      bfd_vma addend, orig_addend;
9833      bfd_reloc_status_type r;
9834      Elf_Internal_Sym *sym;
9835      asection *sec;
9836      struct elf_link_hash_entry *h_elf;
9837      struct ppc_link_hash_entry *h;
9838      struct ppc_link_hash_entry *fdh;
9839      const char *sym_name;
9840      unsigned long r_symndx, toc_symndx;
9841      char tls_mask, tls_gd, tls_type;
9842      char sym_type;
9843      bfd_vma relocation;
9844      bfd_boolean unresolved_reloc;
9845      bfd_boolean warned;
9846      unsigned long insn, mask;
9847      struct ppc_stub_hash_entry *stub_entry;
9848      bfd_vma max_br_offset;
9849      bfd_vma from;
9850
9851      r_type = ELF64_R_TYPE (rel->r_info);
9852      r_symndx = ELF64_R_SYM (rel->r_info);
9853
9854      /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9855	 symbol of the previous ADDR64 reloc.  The symbol gives us the
9856	 proper TOC base to use.  */
9857      if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9858	  && rel != relocs
9859	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9860	  && is_opd)
9861	r_symndx = ELF64_R_SYM (rel[-1].r_info);
9862
9863      sym = NULL;
9864      sec = NULL;
9865      h_elf = NULL;
9866      sym_name = NULL;
9867      unresolved_reloc = FALSE;
9868      warned = FALSE;
9869      orig_addend = rel->r_addend;
9870
9871      if (r_symndx < symtab_hdr->sh_info)
9872	{
9873	  /* It's a local symbol.  */
9874	  long *opd_adjust;
9875
9876	  sym = local_syms + r_symndx;
9877	  sec = local_sections[r_symndx];
9878	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9879	  sym_type = ELF64_ST_TYPE (sym->st_info);
9880	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9881	  opd_adjust = get_opd_info (sec);
9882	  if (opd_adjust != NULL)
9883	    {
9884	      long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9885	      if (adjust == -1)
9886		relocation = 0;
9887	      else
9888		{
9889		  /* If this is a relocation against the opd section sym
9890		     and we have edited .opd, adjust the reloc addend so
9891		     that ld -r and ld --emit-relocs output is correct.
9892		     If it is a reloc against some other .opd symbol,
9893		     then the symbol value will be adjusted later.  */
9894		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9895		    rel->r_addend += adjust;
9896		  else
9897		    relocation += adjust;
9898		}
9899	    }
9900	}
9901      else
9902	{
9903	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9904				   r_symndx, symtab_hdr, sym_hashes,
9905				   h_elf, sec, relocation,
9906				   unresolved_reloc, warned);
9907	  sym_name = h_elf->root.root.string;
9908	  sym_type = h_elf->type;
9909	}
9910      h = (struct ppc_link_hash_entry *) h_elf;
9911
9912      if (sec != NULL && elf_discarded_section (sec))
9913	{
9914	  /* For relocs against symbols from removed linkonce sections,
9915	     or sections discarded by a linker script, we just want the
9916	     section contents zeroed.  Avoid any special processing.  */
9917	  _bfd_clear_contents (ppc64_elf_howto_table[r_type], input_bfd,
9918			       contents + rel->r_offset);
9919	  rel->r_info = 0;
9920	  rel->r_addend = 0;
9921	  continue;
9922	}
9923
9924      if (info->relocatable)
9925	continue;
9926
9927      /* TLS optimizations.  Replace instruction sequences and relocs
9928	 based on information we collected in tls_optimize.  We edit
9929	 RELOCS so that --emit-relocs will output something sensible
9930	 for the final instruction stream.  */
9931      tls_mask = 0;
9932      tls_gd = 0;
9933      toc_symndx = 0;
9934      if (IS_PPC64_TLS_RELOC (r_type))
9935	{
9936	  if (h != NULL)
9937	    tls_mask = h->tls_mask;
9938	  else if (local_got_ents != NULL)
9939	    {
9940	      char *lgot_masks;
9941	      lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9942	      tls_mask = lgot_masks[r_symndx];
9943	    }
9944	  if (tls_mask == 0 && r_type == R_PPC64_TLS)
9945	    {
9946	      /* Check for toc tls entries.  */
9947	      char *toc_tls;
9948
9949	      if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9950				 rel, input_bfd))
9951		return FALSE;
9952
9953	      if (toc_tls)
9954		tls_mask = *toc_tls;
9955	    }
9956	}
9957
9958      /* Check that tls relocs are used with tls syms, and non-tls
9959	 relocs are used with non-tls syms.  */
9960      if (r_symndx != 0
9961	  && r_type != R_PPC64_NONE
9962	  && (h == NULL
9963	      || h->elf.root.type == bfd_link_hash_defined
9964	      || h->elf.root.type == bfd_link_hash_defweak)
9965	  && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9966	{
9967	  if (r_type == R_PPC64_TLS && tls_mask != 0)
9968	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
9969	    ;
9970	  else
9971	    (*_bfd_error_handler)
9972	      (sym_type == STT_TLS
9973	       ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9974	       : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9975	       input_bfd,
9976	       input_section,
9977	       (long) rel->r_offset,
9978	       ppc64_elf_howto_table[r_type]->name,
9979	       sym_name);
9980	}
9981
9982      /* Ensure reloc mapping code below stays sane.  */
9983      if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9984	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9985	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
9986	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9987	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9988	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9989	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
9990	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9991	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9992	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9993	abort ();
9994
9995      switch (r_type)
9996	{
9997	default:
9998	  break;
9999
10000	case R_PPC64_TOC16:
10001	case R_PPC64_TOC16_LO:
10002	case R_PPC64_TOC16_DS:
10003	case R_PPC64_TOC16_LO_DS:
10004	  {
10005	    /* Check for toc tls entries.  */
10006	    char *toc_tls;
10007	    int retval;
10008
10009	    retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
10010				   rel, input_bfd);
10011	    if (retval == 0)
10012	      return FALSE;
10013
10014	    if (toc_tls)
10015	      {
10016		tls_mask = *toc_tls;
10017		if (r_type == R_PPC64_TOC16_DS
10018		    || r_type == R_PPC64_TOC16_LO_DS)
10019		  {
10020		    if (tls_mask != 0
10021			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
10022		      goto toctprel;
10023		  }
10024		else
10025		  {
10026		    /* If we found a GD reloc pair, then we might be
10027		       doing a GD->IE transition.  */
10028		    if (retval == 2)
10029		      {
10030			tls_gd = TLS_TPRELGD;
10031			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10032			  goto tls_get_addr_check;
10033		      }
10034		    else if (retval == 3)
10035		      {
10036			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10037			  goto tls_get_addr_check;
10038		      }
10039		  }
10040	      }
10041	  }
10042	  break;
10043
10044	case R_PPC64_GOT_TPREL16_DS:
10045	case R_PPC64_GOT_TPREL16_LO_DS:
10046	  if (tls_mask != 0
10047	      && (tls_mask & TLS_TPREL) == 0)
10048	    {
10049	    toctprel:
10050	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
10051	      insn &= 31 << 21;
10052	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
10053	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
10054	      r_type = R_PPC64_TPREL16_HA;
10055	      if (toc_symndx != 0)
10056		{
10057		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10058		  /* We changed the symbol.  Start over in order to
10059		     get h, sym, sec etc. right.  */
10060		  rel--;
10061		  continue;
10062		}
10063	      else
10064		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10065	    }
10066	  break;
10067
10068	case R_PPC64_TLS:
10069	  if (tls_mask != 0
10070	      && (tls_mask & TLS_TPREL) == 0)
10071	    {
10072	      bfd_vma rtra;
10073	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
10074	      if ((insn & ((0x3f << 26) | (31 << 11)))
10075		  == ((31 << 26) | (13 << 11)))
10076		rtra = insn & ((1 << 26) - (1 << 16));
10077	      else if ((insn & ((0x3f << 26) | (31 << 16)))
10078		       == ((31 << 26) | (13 << 16)))
10079		rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
10080	      else
10081		abort ();
10082	      if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
10083		/* add -> addi.  */
10084		insn = 14 << 26;
10085	      else if ((insn & (31 << 1)) == 23 << 1
10086		       && ((insn & (31 << 6)) < 14 << 6
10087			   || ((insn & (31 << 6)) >= 16 << 6
10088			       && (insn & (31 << 6)) < 24 << 6)))
10089		/* load and store indexed -> dform.  */
10090		insn = (32 | ((insn >> 6) & 31)) << 26;
10091	      else if ((insn & (31 << 1)) == 21 << 1
10092		       && (insn & (0x1a << 6)) == 0)
10093		/* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
10094		insn = (((58 | ((insn >> 6) & 4)) << 26)
10095			| ((insn >> 6) & 1));
10096	      else if ((insn & (31 << 1)) == 21 << 1
10097		       && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
10098		/* lwax -> lwa.  */
10099		insn = (58 << 26) | 2;
10100	      else
10101		abort ();
10102	      insn |= rtra;
10103	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10104	      /* Was PPC64_TLS which sits on insn boundary, now
10105		 PPC64_TPREL16_LO which is at low-order half-word.  */
10106	      rel->r_offset += d_offset;
10107	      r_type = R_PPC64_TPREL16_LO;
10108	      if (toc_symndx != 0)
10109		{
10110		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10111		  /* We changed the symbol.  Start over in order to
10112		     get h, sym, sec etc. right.  */
10113		  rel--;
10114		  continue;
10115		}
10116	      else
10117		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10118	    }
10119	  break;
10120
10121	case R_PPC64_GOT_TLSGD16_HI:
10122	case R_PPC64_GOT_TLSGD16_HA:
10123	  tls_gd = TLS_TPRELGD;
10124	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10125	    goto tls_gdld_hi;
10126	  break;
10127
10128	case R_PPC64_GOT_TLSLD16_HI:
10129	case R_PPC64_GOT_TLSLD16_HA:
10130	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10131	    {
10132	    tls_gdld_hi:
10133	      if ((tls_mask & tls_gd) != 0)
10134		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10135			  + R_PPC64_GOT_TPREL16_DS);
10136	      else
10137		{
10138		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10139		  rel->r_offset -= d_offset;
10140		  r_type = R_PPC64_NONE;
10141		}
10142	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10143	    }
10144	  break;
10145
10146	case R_PPC64_GOT_TLSGD16:
10147	case R_PPC64_GOT_TLSGD16_LO:
10148	  tls_gd = TLS_TPRELGD;
10149	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10150	    goto tls_get_addr_check;
10151	  break;
10152
10153	case R_PPC64_GOT_TLSLD16:
10154	case R_PPC64_GOT_TLSLD16_LO:
10155	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10156	    {
10157	    tls_get_addr_check:
10158	      if (rel + 1 < relend)
10159		{
10160		  enum elf_ppc64_reloc_type r_type2;
10161		  unsigned long r_symndx2;
10162		  struct elf_link_hash_entry *h2;
10163		  bfd_vma insn1, insn2, insn3;
10164		  bfd_vma offset;
10165
10166		  /* The next instruction should be a call to
10167		     __tls_get_addr.  Peek at the reloc to be sure.  */
10168		  r_type2 = ELF64_R_TYPE (rel[1].r_info);
10169		  r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10170		  if (r_symndx2 < symtab_hdr->sh_info
10171		      || (r_type2 != R_PPC64_REL14
10172			  && r_type2 != R_PPC64_REL14_BRTAKEN
10173			  && r_type2 != R_PPC64_REL14_BRNTAKEN
10174			  && r_type2 != R_PPC64_REL24))
10175		    break;
10176
10177		  h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10178		  while (h2->root.type == bfd_link_hash_indirect
10179			 || h2->root.type == bfd_link_hash_warning)
10180		    h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
10181		  if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10182				     && h2 != &htab->tls_get_addr_fd->elf))
10183		    break;
10184
10185		  /* OK, it checks out.  Replace the call.  */
10186		  offset = rel[1].r_offset;
10187		  insn1 = bfd_get_32 (output_bfd,
10188				      contents + rel->r_offset - d_offset);
10189		  insn3 = bfd_get_32 (output_bfd,
10190				      contents + offset + 4);
10191		  if ((tls_mask & tls_gd) != 0)
10192		    {
10193		      /* IE */
10194		      insn1 &= (1 << 26) - (1 << 2);
10195		      insn1 |= 58 << 26;	/* ld */
10196		      insn2 = 0x7c636a14;	/* add 3,3,13 */
10197		      rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10198		      if ((tls_mask & TLS_EXPLICIT) == 0)
10199			r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10200				  + R_PPC64_GOT_TPREL16_DS);
10201		      else
10202			r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10203		      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10204		    }
10205		  else
10206		    {
10207		      /* LE */
10208		      insn1 = 0x3c6d0000;	/* addis 3,13,0 */
10209		      insn2 = 0x38630000;	/* addi 3,3,0 */
10210		      if (tls_gd == 0)
10211			{
10212			  /* Was an LD reloc.  */
10213			  r_symndx = 0;
10214			  rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10215			  rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10216			}
10217		      else if (toc_symndx != 0)
10218			r_symndx = toc_symndx;
10219		      r_type = R_PPC64_TPREL16_HA;
10220		      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10221		      rel[1].r_info = ELF64_R_INFO (r_symndx,
10222						    R_PPC64_TPREL16_LO);
10223		      rel[1].r_offset += d_offset;
10224		    }
10225		  if (insn3 == NOP
10226		      || insn3 == CROR_151515 || insn3 == CROR_313131)
10227		    {
10228		      insn3 = insn2;
10229		      insn2 = NOP;
10230		      rel[1].r_offset += 4;
10231		    }
10232		  bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
10233		  bfd_put_32 (output_bfd, insn2, contents + offset);
10234		  bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10235		  if (tls_gd == 0 || toc_symndx != 0)
10236		    {
10237		      /* We changed the symbol.  Start over in order
10238			 to get h, sym, sec etc. right.  */
10239		      rel--;
10240		      continue;
10241		    }
10242		}
10243	    }
10244	  break;
10245
10246	case R_PPC64_DTPMOD64:
10247	  if (rel + 1 < relend
10248	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10249	      && rel[1].r_offset == rel->r_offset + 8)
10250	    {
10251	      if ((tls_mask & TLS_GD) == 0)
10252		{
10253		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10254		  if ((tls_mask & TLS_TPRELGD) != 0)
10255		    r_type = R_PPC64_TPREL64;
10256		  else
10257		    {
10258		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10259		      r_type = R_PPC64_NONE;
10260		    }
10261		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10262		}
10263	    }
10264	  else
10265	    {
10266	      if ((tls_mask & TLS_LD) == 0)
10267		{
10268		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10269		  r_type = R_PPC64_NONE;
10270		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10271		}
10272	    }
10273	  break;
10274
10275	case R_PPC64_TPREL64:
10276	  if ((tls_mask & TLS_TPREL) == 0)
10277	    {
10278	      r_type = R_PPC64_NONE;
10279	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10280	    }
10281	  break;
10282	}
10283
10284      /* Handle other relocations that tweak non-addend part of insn.  */
10285      insn = 0;
10286      max_br_offset = 1 << 25;
10287      addend = rel->r_addend;
10288      switch (r_type)
10289	{
10290	default:
10291	  break;
10292
10293	  /* Branch taken prediction relocations.  */
10294	case R_PPC64_ADDR14_BRTAKEN:
10295	case R_PPC64_REL14_BRTAKEN:
10296	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
10297	  /* Fall thru.  */
10298
10299	  /* Branch not taken prediction relocations.  */
10300	case R_PPC64_ADDR14_BRNTAKEN:
10301	case R_PPC64_REL14_BRNTAKEN:
10302	  insn |= bfd_get_32 (output_bfd,
10303			      contents + rel->r_offset) & ~(0x01 << 21);
10304	  /* Fall thru.  */
10305
10306	case R_PPC64_REL14:
10307	  max_br_offset = 1 << 15;
10308	  /* Fall thru.  */
10309
10310	case R_PPC64_REL24:
10311	  /* Calls to functions with a different TOC, such as calls to
10312	     shared objects, need to alter the TOC pointer.  This is
10313	     done using a linkage stub.  A REL24 branching to these
10314	     linkage stubs needs to be followed by a nop, as the nop
10315	     will be replaced with an instruction to restore the TOC
10316	     base pointer.  */
10317	  stub_entry = NULL;
10318	  fdh = h;
10319	  if (((h != NULL
10320		&& (((fdh = h->oh) != NULL
10321		     && fdh->elf.plt.plist != NULL)
10322		    || (fdh = h)->elf.plt.plist != NULL))
10323	       || (sec != NULL
10324		   && sec->output_section != NULL
10325		   && sec->id <= htab->top_id
10326		   && (htab->stub_group[sec->id].toc_off
10327		       != htab->stub_group[input_section->id].toc_off)))
10328	      && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10329						   rel, htab)) != NULL
10330	      && (stub_entry->stub_type == ppc_stub_plt_call
10331		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10332		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10333	    {
10334	      bfd_boolean can_plt_call = FALSE;
10335
10336	      if (rel->r_offset + 8 <= input_section->size)
10337		{
10338		  unsigned long nop;
10339		  nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10340		  if (nop == NOP
10341		      || nop == CROR_151515 || nop == CROR_313131)
10342		    {
10343		      bfd_put_32 (input_bfd, LD_R2_40R1,
10344				  contents + rel->r_offset + 4);
10345		      can_plt_call = TRUE;
10346		    }
10347		}
10348
10349	      if (!can_plt_call)
10350		{
10351		  if (stub_entry->stub_type == ppc_stub_plt_call)
10352		    {
10353		      /* If this is a plain branch rather than a branch
10354			 and link, don't require a nop.  However, don't
10355			 allow tail calls in a shared library as they
10356			 will result in r2 being corrupted.  */
10357		      unsigned long br;
10358		      br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10359		      if (info->executable && (br & 1) == 0)
10360			can_plt_call = TRUE;
10361		      else
10362			stub_entry = NULL;
10363		    }
10364		  else if (h != NULL
10365			   && strcmp (h->elf.root.root.string,
10366				      ".__libc_start_main") == 0)
10367		    {
10368		      /* Allow crt1 branch to go via a toc adjusting stub.  */
10369		      can_plt_call = TRUE;
10370		    }
10371		  else
10372		    {
10373		      if (strcmp (input_section->output_section->name,
10374				  ".init") == 0
10375			  || strcmp (input_section->output_section->name,
10376				     ".fini") == 0)
10377			(*_bfd_error_handler)
10378			  (_("%B(%A+0x%lx): automatic multiple TOCs "
10379			     "not supported using your crt files; "
10380			     "recompile with -mminimal-toc or upgrade gcc"),
10381			   input_bfd,
10382			   input_section,
10383			   (long) rel->r_offset);
10384		      else
10385			(*_bfd_error_handler)
10386			  (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10387			     "does not allow automatic multiple TOCs; "
10388			     "recompile with -mminimal-toc or "
10389			     "-fno-optimize-sibling-calls, "
10390			     "or make `%s' extern"),
10391			   input_bfd,
10392			   input_section,
10393			   (long) rel->r_offset,
10394			   sym_name,
10395			   sym_name);
10396		      bfd_set_error (bfd_error_bad_value);
10397		      ret = FALSE;
10398		    }
10399		}
10400
10401	      if (can_plt_call
10402		  && stub_entry->stub_type == ppc_stub_plt_call)
10403		unresolved_reloc = FALSE;
10404	    }
10405
10406	  if (stub_entry == NULL
10407	      && get_opd_info (sec) != NULL)
10408	    {
10409	      /* The branch destination is the value of the opd entry. */
10410	      bfd_vma off = (relocation + addend
10411			     - sec->output_section->vma
10412			     - sec->output_offset);
10413	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10414	      if (dest != (bfd_vma) -1)
10415		{
10416		  relocation = dest;
10417		  addend = 0;
10418		}
10419	    }
10420
10421	  /* If the branch is out of reach we ought to have a long
10422	     branch stub.  */
10423	  from = (rel->r_offset
10424		  + input_section->output_offset
10425		  + input_section->output_section->vma);
10426
10427	  if (stub_entry == NULL
10428	      && (relocation + addend - from + max_br_offset
10429		  >= 2 * max_br_offset)
10430	      && r_type != R_PPC64_ADDR14_BRTAKEN
10431	      && r_type != R_PPC64_ADDR14_BRNTAKEN)
10432	    stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10433					     htab);
10434
10435	  if (stub_entry != NULL)
10436	    {
10437	      /* Munge up the value and addend so that we call the stub
10438		 rather than the procedure directly.  */
10439	      relocation = (stub_entry->stub_offset
10440			    + stub_entry->stub_sec->output_offset
10441			    + stub_entry->stub_sec->output_section->vma);
10442	      addend = 0;
10443	    }
10444
10445	  if (insn != 0)
10446	    {
10447	      if (is_power4)
10448		{
10449		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
10450		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
10451		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
10452		  if ((insn & (0x14 << 21)) == (0x04 << 21))
10453		    insn |= 0x02 << 21;
10454		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
10455		    insn |= 0x08 << 21;
10456		  else
10457		    break;
10458		}
10459	      else
10460		{
10461		  /* Invert 'y' bit if not the default.  */
10462		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
10463		    insn ^= 0x01 << 21;
10464		}
10465
10466	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10467	    }
10468
10469	  /* NOP out calls to undefined weak functions.
10470	     We can thus call a weak function without first
10471	     checking whether the function is defined.  */
10472	  else if (h != NULL
10473		   && h->elf.root.type == bfd_link_hash_undefweak
10474		   && r_type == R_PPC64_REL24
10475		   && relocation == 0
10476		   && addend == 0)
10477	    {
10478	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10479	      continue;
10480	    }
10481	  break;
10482	}
10483
10484      /* Set `addend'.  */
10485      tls_type = 0;
10486      switch (r_type)
10487	{
10488	default:
10489	  (*_bfd_error_handler)
10490	    (_("%B: unknown relocation type %d for symbol %s"),
10491	     input_bfd, (int) r_type, sym_name);
10492
10493	  bfd_set_error (bfd_error_bad_value);
10494	  ret = FALSE;
10495	  continue;
10496
10497	case R_PPC64_NONE:
10498	case R_PPC64_TLS:
10499	case R_PPC64_GNU_VTINHERIT:
10500	case R_PPC64_GNU_VTENTRY:
10501	  continue;
10502
10503	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
10504	     address in the GOT as relocation value instead of the
10505	     symbol's value itself.  Also, create a GOT entry for the
10506	     symbol and put the symbol value there.  */
10507	case R_PPC64_GOT_TLSGD16:
10508	case R_PPC64_GOT_TLSGD16_LO:
10509	case R_PPC64_GOT_TLSGD16_HI:
10510	case R_PPC64_GOT_TLSGD16_HA:
10511	  tls_type = TLS_TLS | TLS_GD;
10512	  goto dogot;
10513
10514	case R_PPC64_GOT_TLSLD16:
10515	case R_PPC64_GOT_TLSLD16_LO:
10516	case R_PPC64_GOT_TLSLD16_HI:
10517	case R_PPC64_GOT_TLSLD16_HA:
10518	  tls_type = TLS_TLS | TLS_LD;
10519	  goto dogot;
10520
10521	case R_PPC64_GOT_TPREL16_DS:
10522	case R_PPC64_GOT_TPREL16_LO_DS:
10523	case R_PPC64_GOT_TPREL16_HI:
10524	case R_PPC64_GOT_TPREL16_HA:
10525	  tls_type = TLS_TLS | TLS_TPREL;
10526	  goto dogot;
10527
10528	case R_PPC64_GOT_DTPREL16_DS:
10529	case R_PPC64_GOT_DTPREL16_LO_DS:
10530	case R_PPC64_GOT_DTPREL16_HI:
10531	case R_PPC64_GOT_DTPREL16_HA:
10532	  tls_type = TLS_TLS | TLS_DTPREL;
10533	  goto dogot;
10534
10535	case R_PPC64_GOT16:
10536	case R_PPC64_GOT16_LO:
10537	case R_PPC64_GOT16_HI:
10538	case R_PPC64_GOT16_HA:
10539	case R_PPC64_GOT16_DS:
10540	case R_PPC64_GOT16_LO_DS:
10541	dogot:
10542	  {
10543	    /* Relocation is to the entry for this symbol in the global
10544	       offset table.  */
10545	    asection *got;
10546	    bfd_vma *offp;
10547	    bfd_vma off;
10548	    unsigned long indx = 0;
10549
10550	    if (tls_type == (TLS_TLS | TLS_LD)
10551		&& (h == NULL
10552		    || !h->elf.def_dynamic))
10553	      offp = &ppc64_tlsld_got (input_bfd)->offset;
10554	    else
10555	      {
10556		struct got_entry *ent;
10557
10558		if (h != NULL)
10559		  {
10560		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
10561		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10562							  &h->elf)
10563			|| (info->shared
10564			    && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10565		      /* This is actually a static link, or it is a
10566			 -Bsymbolic link and the symbol is defined
10567			 locally, or the symbol was forced to be local
10568			 because of a version file.  */
10569		      ;
10570		    else
10571		      {
10572			indx = h->elf.dynindx;
10573			unresolved_reloc = FALSE;
10574		      }
10575		    ent = h->elf.got.glist;
10576		  }
10577		else
10578		  {
10579		    if (local_got_ents == NULL)
10580		      abort ();
10581		    ent = local_got_ents[r_symndx];
10582		  }
10583
10584		for (; ent != NULL; ent = ent->next)
10585		  if (ent->addend == orig_addend
10586		      && ent->owner == input_bfd
10587		      && ent->tls_type == tls_type)
10588		    break;
10589		if (ent == NULL)
10590		  abort ();
10591		offp = &ent->got.offset;
10592	      }
10593
10594	    got = ppc64_elf_tdata (input_bfd)->got;
10595	    if (got == NULL)
10596	      abort ();
10597
10598	    /* The offset must always be a multiple of 8.  We use the
10599	       least significant bit to record whether we have already
10600	       processed this entry.  */
10601	    off = *offp;
10602	    if ((off & 1) != 0)
10603	      off &= ~1;
10604	    else
10605	      {
10606		/* Generate relocs for the dynamic linker, except in
10607		   the case of TLSLD where we'll use one entry per
10608		   module.  */
10609		asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10610
10611		*offp = off | 1;
10612		if ((info->shared || indx != 0)
10613		    && (h == NULL
10614			|| ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10615			|| h->elf.root.type != bfd_link_hash_undefweak))
10616		  {
10617		    outrel.r_offset = (got->output_section->vma
10618				       + got->output_offset
10619				       + off);
10620		    outrel.r_addend = addend;
10621		    if (tls_type & (TLS_LD | TLS_GD))
10622		      {
10623			outrel.r_addend = 0;
10624			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10625			if (tls_type == (TLS_TLS | TLS_GD))
10626			  {
10627			    loc = relgot->contents;
10628			    loc += (relgot->reloc_count++
10629				    * sizeof (Elf64_External_Rela));
10630			    bfd_elf64_swap_reloca_out (output_bfd,
10631						       &outrel, loc);
10632			    outrel.r_offset += 8;
10633			    outrel.r_addend = addend;
10634			    outrel.r_info
10635			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10636			  }
10637		      }
10638		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
10639		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10640		    else if (tls_type == (TLS_TLS | TLS_TPREL))
10641		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10642		    else if (indx == 0)
10643		      {
10644			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10645
10646			/* Write the .got section contents for the sake
10647			   of prelink.  */
10648			loc = got->contents + off;
10649			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10650				    loc);
10651		      }
10652		    else
10653		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10654
10655		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10656		      {
10657			outrel.r_addend += relocation;
10658			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10659			  outrel.r_addend -= htab->elf.tls_sec->vma;
10660		      }
10661		    loc = relgot->contents;
10662		    loc += (relgot->reloc_count++
10663			    * sizeof (Elf64_External_Rela));
10664		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10665		  }
10666
10667		/* Init the .got section contents here if we're not
10668		   emitting a reloc.  */
10669		else
10670		  {
10671		    relocation += addend;
10672		    if (tls_type == (TLS_TLS | TLS_LD))
10673		      relocation = 1;
10674		    else if (tls_type != 0)
10675		      {
10676			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10677			if (tls_type == (TLS_TLS | TLS_TPREL))
10678			  relocation += DTP_OFFSET - TP_OFFSET;
10679
10680			if (tls_type == (TLS_TLS | TLS_GD))
10681			  {
10682			    bfd_put_64 (output_bfd, relocation,
10683					got->contents + off + 8);
10684			    relocation = 1;
10685			  }
10686		      }
10687
10688		    bfd_put_64 (output_bfd, relocation,
10689				got->contents + off);
10690		  }
10691	      }
10692
10693	    if (off >= (bfd_vma) -2)
10694	      abort ();
10695
10696	    relocation = got->output_offset + off;
10697
10698	    /* TOC base (r2) is TOC start plus 0x8000.  */
10699	    addend = -TOC_BASE_OFF;
10700	  }
10701	  break;
10702
10703	case R_PPC64_PLT16_HA:
10704	case R_PPC64_PLT16_HI:
10705	case R_PPC64_PLT16_LO:
10706	case R_PPC64_PLT32:
10707	case R_PPC64_PLT64:
10708	  /* Relocation is to the entry for this symbol in the
10709	     procedure linkage table.  */
10710
10711	  /* Resolve a PLT reloc against a local symbol directly,
10712	     without using the procedure linkage table.  */
10713	  if (h == NULL)
10714	    break;
10715
10716	  /* It's possible that we didn't make a PLT entry for this
10717	     symbol.  This happens when statically linking PIC code,
10718	     or when using -Bsymbolic.  Go find a match if there is a
10719	     PLT entry.  */
10720	  if (htab->plt != NULL)
10721	    {
10722	      struct plt_entry *ent;
10723	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10724		if (ent->addend == orig_addend
10725		    && ent->plt.offset != (bfd_vma) -1)
10726		  {
10727		    relocation = (htab->plt->output_section->vma
10728				  + htab->plt->output_offset
10729				  + ent->plt.offset);
10730		    unresolved_reloc = FALSE;
10731		  }
10732	    }
10733	  break;
10734
10735	case R_PPC64_TOC:
10736	  /* Relocation value is TOC base.  */
10737	  relocation = TOCstart;
10738	  if (r_symndx == 0)
10739	    relocation += htab->stub_group[input_section->id].toc_off;
10740	  else if (unresolved_reloc)
10741	    ;
10742	  else if (sec != NULL && sec->id <= htab->top_id)
10743	    relocation += htab->stub_group[sec->id].toc_off;
10744	  else
10745	    unresolved_reloc = TRUE;
10746	  goto dodyn;
10747
10748	  /* TOC16 relocs.  We want the offset relative to the TOC base,
10749	     which is the address of the start of the TOC plus 0x8000.
10750	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
10751	     in this order.  */
10752	case R_PPC64_TOC16:
10753	case R_PPC64_TOC16_LO:
10754	case R_PPC64_TOC16_HI:
10755	case R_PPC64_TOC16_DS:
10756	case R_PPC64_TOC16_LO_DS:
10757	case R_PPC64_TOC16_HA:
10758	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10759	  break;
10760
10761	  /* Relocate against the beginning of the section.  */
10762	case R_PPC64_SECTOFF:
10763	case R_PPC64_SECTOFF_LO:
10764	case R_PPC64_SECTOFF_HI:
10765	case R_PPC64_SECTOFF_DS:
10766	case R_PPC64_SECTOFF_LO_DS:
10767	case R_PPC64_SECTOFF_HA:
10768	  if (sec != NULL)
10769	    addend -= sec->output_section->vma;
10770	  break;
10771
10772	case R_PPC64_REL14:
10773	case R_PPC64_REL14_BRNTAKEN:
10774	case R_PPC64_REL14_BRTAKEN:
10775	case R_PPC64_REL24:
10776	  break;
10777
10778	case R_PPC64_TPREL16:
10779	case R_PPC64_TPREL16_LO:
10780	case R_PPC64_TPREL16_HI:
10781	case R_PPC64_TPREL16_HA:
10782	case R_PPC64_TPREL16_DS:
10783	case R_PPC64_TPREL16_LO_DS:
10784	case R_PPC64_TPREL16_HIGHER:
10785	case R_PPC64_TPREL16_HIGHERA:
10786	case R_PPC64_TPREL16_HIGHEST:
10787	case R_PPC64_TPREL16_HIGHESTA:
10788	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10789	  if (info->shared)
10790	    /* The TPREL16 relocs shouldn't really be used in shared
10791	       libs as they will result in DT_TEXTREL being set, but
10792	       support them anyway.  */
10793	    goto dodyn;
10794	  break;
10795
10796	case R_PPC64_DTPREL16:
10797	case R_PPC64_DTPREL16_LO:
10798	case R_PPC64_DTPREL16_HI:
10799	case R_PPC64_DTPREL16_HA:
10800	case R_PPC64_DTPREL16_DS:
10801	case R_PPC64_DTPREL16_LO_DS:
10802	case R_PPC64_DTPREL16_HIGHER:
10803	case R_PPC64_DTPREL16_HIGHERA:
10804	case R_PPC64_DTPREL16_HIGHEST:
10805	case R_PPC64_DTPREL16_HIGHESTA:
10806	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10807	  break;
10808
10809	case R_PPC64_DTPMOD64:
10810	  relocation = 1;
10811	  addend = 0;
10812	  goto dodyn;
10813
10814	case R_PPC64_TPREL64:
10815	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10816	  goto dodyn;
10817
10818	case R_PPC64_DTPREL64:
10819	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10820	  /* Fall thru */
10821
10822	  /* Relocations that may need to be propagated if this is a
10823	     dynamic object.  */
10824	case R_PPC64_REL30:
10825	case R_PPC64_REL32:
10826	case R_PPC64_REL64:
10827	case R_PPC64_ADDR14:
10828	case R_PPC64_ADDR14_BRNTAKEN:
10829	case R_PPC64_ADDR14_BRTAKEN:
10830	case R_PPC64_ADDR16:
10831	case R_PPC64_ADDR16_DS:
10832	case R_PPC64_ADDR16_HA:
10833	case R_PPC64_ADDR16_HI:
10834	case R_PPC64_ADDR16_HIGHER:
10835	case R_PPC64_ADDR16_HIGHERA:
10836	case R_PPC64_ADDR16_HIGHEST:
10837	case R_PPC64_ADDR16_HIGHESTA:
10838	case R_PPC64_ADDR16_LO:
10839	case R_PPC64_ADDR16_LO_DS:
10840	case R_PPC64_ADDR24:
10841	case R_PPC64_ADDR32:
10842	case R_PPC64_ADDR64:
10843	case R_PPC64_UADDR16:
10844	case R_PPC64_UADDR32:
10845	case R_PPC64_UADDR64:
10846	dodyn:
10847	  if ((input_section->flags & SEC_ALLOC) == 0)
10848	    break;
10849
10850	  if (NO_OPD_RELOCS && is_opd)
10851	    break;
10852
10853	  if ((info->shared
10854	       && (h == NULL
10855		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10856		   || h->elf.root.type != bfd_link_hash_undefweak)
10857	       && (MUST_BE_DYN_RELOC (r_type)
10858		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10859	      || (ELIMINATE_COPY_RELOCS
10860		  && !info->shared
10861		  && h != NULL
10862		  && h->elf.dynindx != -1
10863		  && !h->elf.non_got_ref
10864		  && h->elf.def_dynamic
10865		  && !h->elf.def_regular))
10866	    {
10867	      Elf_Internal_Rela outrel;
10868	      bfd_boolean skip, relocate;
10869	      asection *sreloc;
10870	      bfd_byte *loc;
10871	      bfd_vma out_off;
10872
10873	      /* When generating a dynamic object, these relocations
10874		 are copied into the output file to be resolved at run
10875		 time.  */
10876
10877	      skip = FALSE;
10878	      relocate = FALSE;
10879
10880	      out_off = _bfd_elf_section_offset (output_bfd, info,
10881						 input_section, rel->r_offset);
10882	      if (out_off == (bfd_vma) -1)
10883		skip = TRUE;
10884	      else if (out_off == (bfd_vma) -2)
10885		skip = TRUE, relocate = TRUE;
10886	      out_off += (input_section->output_section->vma
10887			  + input_section->output_offset);
10888	      outrel.r_offset = out_off;
10889	      outrel.r_addend = rel->r_addend;
10890
10891	      /* Optimize unaligned reloc use.  */
10892	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10893		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10894		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10895	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10896		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10897		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10898	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10899		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10900		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10901
10902	      if (skip)
10903		memset (&outrel, 0, sizeof outrel);
10904	      else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10905		       && !is_opd
10906		       && r_type != R_PPC64_TOC)
10907		outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10908	      else
10909		{
10910		  /* This symbol is local, or marked to become local,
10911		     or this is an opd section reloc which must point
10912		     at a local function.  */
10913		  outrel.r_addend += relocation;
10914		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10915		    {
10916		      if (is_opd && h != NULL)
10917			{
10918			  /* Lie about opd entries.  This case occurs
10919			     when building shared libraries and we
10920			     reference a function in another shared
10921			     lib.  The same thing happens for a weak
10922			     definition in an application that's
10923			     overridden by a strong definition in a
10924			     shared lib.  (I believe this is a generic
10925			     bug in binutils handling of weak syms.)
10926			     In these cases we won't use the opd
10927			     entry in this lib.  */
10928			  unresolved_reloc = FALSE;
10929			}
10930		      outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10931
10932		      /* We need to relocate .opd contents for ld.so.
10933			 Prelink also wants simple and consistent rules
10934			 for relocs.  This make all RELATIVE relocs have
10935			 *r_offset equal to r_addend.  */
10936		      relocate = TRUE;
10937		    }
10938		  else
10939		    {
10940		      long indx = 0;
10941
10942		      if (bfd_is_abs_section (sec))
10943			;
10944		      else if (sec == NULL || sec->owner == NULL)
10945			{
10946			  bfd_set_error (bfd_error_bad_value);
10947			  return FALSE;
10948			}
10949		      else
10950			{
10951			  asection *osec;
10952
10953			  osec = sec->output_section;
10954			  indx = elf_section_data (osec)->dynindx;
10955
10956			  if (indx == 0)
10957			    {
10958			      if ((osec->flags & SEC_READONLY) == 0
10959				  && htab->elf.data_index_section != NULL)
10960				osec = htab->elf.data_index_section;
10961			      else
10962				osec = htab->elf.text_index_section;
10963			      indx = elf_section_data (osec)->dynindx;
10964			    }
10965			  BFD_ASSERT (indx != 0);
10966
10967			  /* We are turning this relocation into one
10968			     against a section symbol, so subtract out
10969			     the output section's address but not the
10970			     offset of the input section in the output
10971			     section.  */
10972			  outrel.r_addend -= osec->vma;
10973			}
10974
10975		      outrel.r_info = ELF64_R_INFO (indx, r_type);
10976		    }
10977		}
10978
10979	      sreloc = elf_section_data (input_section)->sreloc;
10980	      if (sreloc == NULL)
10981		abort ();
10982
10983	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10984		  >= sreloc->size)
10985		abort ();
10986	      loc = sreloc->contents;
10987	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10988	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10989
10990	      /* If this reloc is against an external symbol, it will
10991		 be computed at runtime, so there's no need to do
10992		 anything now.  However, for the sake of prelink ensure
10993		 that the section contents are a known value.  */
10994	      if (! relocate)
10995		{
10996		  unresolved_reloc = FALSE;
10997		  /* The value chosen here is quite arbitrary as ld.so
10998		     ignores section contents except for the special
10999		     case of .opd where the contents might be accessed
11000		     before relocation.  Choose zero, as that won't
11001		     cause reloc overflow.  */
11002		  relocation = 0;
11003		  addend = 0;
11004		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
11005		     to improve backward compatibility with older
11006		     versions of ld.  */
11007		  if (r_type == R_PPC64_ADDR64)
11008		    addend = outrel.r_addend;
11009		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
11010		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
11011		    addend = (input_section->output_section->vma
11012			      + input_section->output_offset
11013			      + rel->r_offset);
11014		}
11015	    }
11016	  break;
11017
11018	case R_PPC64_COPY:
11019	case R_PPC64_GLOB_DAT:
11020	case R_PPC64_JMP_SLOT:
11021	case R_PPC64_RELATIVE:
11022	  /* We shouldn't ever see these dynamic relocs in relocatable
11023	     files.  */
11024	  /* Fall through.  */
11025
11026	case R_PPC64_PLTGOT16:
11027	case R_PPC64_PLTGOT16_DS:
11028	case R_PPC64_PLTGOT16_HA:
11029	case R_PPC64_PLTGOT16_HI:
11030	case R_PPC64_PLTGOT16_LO:
11031	case R_PPC64_PLTGOT16_LO_DS:
11032	case R_PPC64_PLTREL32:
11033	case R_PPC64_PLTREL64:
11034	  /* These ones haven't been implemented yet.  */
11035
11036	  (*_bfd_error_handler)
11037	    (_("%B: relocation %s is not supported for symbol %s."),
11038	     input_bfd,
11039	     ppc64_elf_howto_table[r_type]->name, sym_name);
11040
11041	  bfd_set_error (bfd_error_invalid_operation);
11042	  ret = FALSE;
11043	  continue;
11044	}
11045
11046      /* Do any further special processing.  */
11047      switch (r_type)
11048	{
11049	default:
11050	  break;
11051
11052	case R_PPC64_ADDR16_HA:
11053	case R_PPC64_ADDR16_HIGHERA:
11054	case R_PPC64_ADDR16_HIGHESTA:
11055	case R_PPC64_TOC16_HA:
11056	case R_PPC64_SECTOFF_HA:
11057	case R_PPC64_TPREL16_HA:
11058	case R_PPC64_DTPREL16_HA:
11059	case R_PPC64_TPREL16_HIGHER:
11060	case R_PPC64_TPREL16_HIGHERA:
11061	case R_PPC64_TPREL16_HIGHEST:
11062	case R_PPC64_TPREL16_HIGHESTA:
11063	case R_PPC64_DTPREL16_HIGHER:
11064	case R_PPC64_DTPREL16_HIGHERA:
11065	case R_PPC64_DTPREL16_HIGHEST:
11066	case R_PPC64_DTPREL16_HIGHESTA:
11067	  /* It's just possible that this symbol is a weak symbol
11068	     that's not actually defined anywhere. In that case,
11069	     'sec' would be NULL, and we should leave the symbol
11070	     alone (it will be set to zero elsewhere in the link).  */
11071	  if (sec == NULL)
11072	    break;
11073	  /* Fall thru */
11074
11075	case R_PPC64_GOT16_HA:
11076	case R_PPC64_PLTGOT16_HA:
11077	case R_PPC64_PLT16_HA:
11078	case R_PPC64_GOT_TLSGD16_HA:
11079	case R_PPC64_GOT_TLSLD16_HA:
11080	case R_PPC64_GOT_TPREL16_HA:
11081	case R_PPC64_GOT_DTPREL16_HA:
11082	  /* Add 0x10000 if sign bit in 0:15 is set.
11083	     Bits 0:15 are not used.  */
11084	  addend += 0x8000;
11085	  break;
11086
11087	case R_PPC64_ADDR16_DS:
11088	case R_PPC64_ADDR16_LO_DS:
11089	case R_PPC64_GOT16_DS:
11090	case R_PPC64_GOT16_LO_DS:
11091	case R_PPC64_PLT16_LO_DS:
11092	case R_PPC64_SECTOFF_DS:
11093	case R_PPC64_SECTOFF_LO_DS:
11094	case R_PPC64_TOC16_DS:
11095	case R_PPC64_TOC16_LO_DS:
11096	case R_PPC64_PLTGOT16_DS:
11097	case R_PPC64_PLTGOT16_LO_DS:
11098	case R_PPC64_GOT_TPREL16_DS:
11099	case R_PPC64_GOT_TPREL16_LO_DS:
11100	case R_PPC64_GOT_DTPREL16_DS:
11101	case R_PPC64_GOT_DTPREL16_LO_DS:
11102	case R_PPC64_TPREL16_DS:
11103	case R_PPC64_TPREL16_LO_DS:
11104	case R_PPC64_DTPREL16_DS:
11105	case R_PPC64_DTPREL16_LO_DS:
11106	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
11107	  mask = 3;
11108	  /* If this reloc is against an lq insn, then the value must be
11109	     a multiple of 16.  This is somewhat of a hack, but the
11110	     "correct" way to do this by defining _DQ forms of all the
11111	     _DS relocs bloats all reloc switches in this file.  It
11112	     doesn't seem to make much sense to use any of these relocs
11113	     in data, so testing the insn should be safe.  */
11114	  if ((insn & (0x3f << 26)) == (56u << 26))
11115	    mask = 15;
11116	  if (((relocation + addend) & mask) != 0)
11117	    {
11118	      (*_bfd_error_handler)
11119		(_("%B: error: relocation %s not a multiple of %d"),
11120		 input_bfd,
11121		 ppc64_elf_howto_table[r_type]->name,
11122		 mask + 1);
11123	      bfd_set_error (bfd_error_bad_value);
11124	      ret = FALSE;
11125	      continue;
11126	    }
11127	  break;
11128	}
11129
11130      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11131	 because such sections are not SEC_ALLOC and thus ld.so will
11132	 not process them.  */
11133      if (unresolved_reloc
11134	  && !((input_section->flags & SEC_DEBUGGING) != 0
11135	       && h->elf.def_dynamic))
11136	{
11137	  (*_bfd_error_handler)
11138	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11139	     input_bfd,
11140	     input_section,
11141	     (long) rel->r_offset,
11142	     ppc64_elf_howto_table[(int) r_type]->name,
11143	     h->elf.root.root.string);
11144	  ret = FALSE;
11145	}
11146
11147      r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
11148				    input_bfd,
11149				    input_section,
11150				    contents,
11151				    rel->r_offset,
11152				    relocation,
11153				    addend);
11154
11155      if (r != bfd_reloc_ok)
11156	{
11157	  if (sym_name == NULL)
11158	    sym_name = "(null)";
11159	  if (r == bfd_reloc_overflow)
11160	    {
11161	      if (warned)
11162		continue;
11163	      if (h != NULL
11164		  && h->elf.root.type == bfd_link_hash_undefweak
11165		  && ppc64_elf_howto_table[r_type]->pc_relative)
11166		{
11167		  /* Assume this is a call protected by other code that
11168		     detects the symbol is undefined.  If this is the case,
11169		     we can safely ignore the overflow.  If not, the
11170		     program is hosed anyway, and a little warning isn't
11171		     going to help.  */
11172
11173		  continue;
11174		}
11175
11176	      if (!((*info->callbacks->reloc_overflow)
11177		    (info, (h ? &h->elf.root : NULL), sym_name,
11178		     ppc64_elf_howto_table[r_type]->name,
11179		     orig_addend, input_bfd, input_section, rel->r_offset)))
11180		return FALSE;
11181	    }
11182	  else
11183	    {
11184	      (*_bfd_error_handler)
11185		(_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11186		 input_bfd,
11187		 input_section,
11188		 (long) rel->r_offset,
11189		 ppc64_elf_howto_table[r_type]->name,
11190		 sym_name,
11191		 (int) r);
11192	      ret = FALSE;
11193	    }
11194	}
11195    }
11196
11197  /* If we're emitting relocations, then shortly after this function
11198     returns, reloc offsets and addends for this section will be
11199     adjusted.  Worse, reloc symbol indices will be for the output
11200     file rather than the input.  Save a copy of the relocs for
11201     opd_entry_value.  */
11202  if (is_opd && (info->emitrelocations || info->relocatable))
11203    {
11204      bfd_size_type amt;
11205      amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11206      rel = bfd_alloc (input_bfd, amt);
11207      BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11208      ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11209      if (rel == NULL)
11210	return FALSE;
11211      memcpy (rel, relocs, amt);
11212    }
11213  return ret;
11214}
11215
11216/* Adjust the value of any local symbols in opd sections.  */
11217
11218static bfd_boolean
11219ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11220			      const char *name ATTRIBUTE_UNUSED,
11221			      Elf_Internal_Sym *elfsym,
11222			      asection *input_sec,
11223			      struct elf_link_hash_entry *h)
11224{
11225  long *opd_adjust, adjust;
11226  bfd_vma value;
11227
11228  if (h != NULL)
11229    return TRUE;
11230
11231  opd_adjust = get_opd_info (input_sec);
11232  if (opd_adjust == NULL)
11233    return TRUE;
11234
11235  value = elfsym->st_value - input_sec->output_offset;
11236  if (!info->relocatable)
11237    value -= input_sec->output_section->vma;
11238
11239  adjust = opd_adjust[value / 8];
11240  if (adjust == -1)
11241    elfsym->st_value = 0;
11242  else
11243    elfsym->st_value += adjust;
11244  return TRUE;
11245}
11246
11247/* Finish up dynamic symbol handling.  We set the contents of various
11248   dynamic sections here.  */
11249
11250static bfd_boolean
11251ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11252				 struct bfd_link_info *info,
11253				 struct elf_link_hash_entry *h,
11254				 Elf_Internal_Sym *sym)
11255{
11256  struct ppc_link_hash_table *htab;
11257  struct plt_entry *ent;
11258  Elf_Internal_Rela rela;
11259  bfd_byte *loc;
11260
11261  htab = ppc_hash_table (info);
11262
11263  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11264    if (ent->plt.offset != (bfd_vma) -1)
11265      {
11266	/* This symbol has an entry in the procedure linkage
11267	   table.  Set it up.  */
11268
11269	if (htab->plt == NULL
11270	    || htab->relplt == NULL
11271	    || htab->glink == NULL)
11272	  abort ();
11273
11274	/* Create a JMP_SLOT reloc to inform the dynamic linker to
11275	   fill in the PLT entry.  */
11276	rela.r_offset = (htab->plt->output_section->vma
11277			 + htab->plt->output_offset
11278			 + ent->plt.offset);
11279	rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11280	rela.r_addend = ent->addend;
11281
11282	loc = htab->relplt->contents;
11283	loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11284		* sizeof (Elf64_External_Rela));
11285	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11286      }
11287
11288  if (h->needs_copy)
11289    {
11290      Elf_Internal_Rela rela;
11291      bfd_byte *loc;
11292
11293      /* This symbol needs a copy reloc.  Set it up.  */
11294
11295      if (h->dynindx == -1
11296	  || (h->root.type != bfd_link_hash_defined
11297	      && h->root.type != bfd_link_hash_defweak)
11298	  || htab->relbss == NULL)
11299	abort ();
11300
11301      rela.r_offset = (h->root.u.def.value
11302		       + h->root.u.def.section->output_section->vma
11303		       + h->root.u.def.section->output_offset);
11304      rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11305      rela.r_addend = 0;
11306      loc = htab->relbss->contents;
11307      loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11308      bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11309    }
11310
11311  /* Mark some specially defined symbols as absolute.  */
11312  if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11313    sym->st_shndx = SHN_ABS;
11314
11315  return TRUE;
11316}
11317
11318/* Used to decide how to sort relocs in an optimal manner for the
11319   dynamic linker, before writing them out.  */
11320
11321static enum elf_reloc_type_class
11322ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11323{
11324  enum elf_ppc64_reloc_type r_type;
11325
11326  r_type = ELF64_R_TYPE (rela->r_info);
11327  switch (r_type)
11328    {
11329    case R_PPC64_RELATIVE:
11330      return reloc_class_relative;
11331    case R_PPC64_JMP_SLOT:
11332      return reloc_class_plt;
11333    case R_PPC64_COPY:
11334      return reloc_class_copy;
11335    default:
11336      return reloc_class_normal;
11337    }
11338}
11339
11340/* Finish up the dynamic sections.  */
11341
11342static bfd_boolean
11343ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11344				   struct bfd_link_info *info)
11345{
11346  struct ppc_link_hash_table *htab;
11347  bfd *dynobj;
11348  asection *sdyn;
11349
11350  htab = ppc_hash_table (info);
11351  dynobj = htab->elf.dynobj;
11352  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11353
11354  if (htab->elf.dynamic_sections_created)
11355    {
11356      Elf64_External_Dyn *dyncon, *dynconend;
11357
11358      if (sdyn == NULL || htab->got == NULL)
11359	abort ();
11360
11361      dyncon = (Elf64_External_Dyn *) sdyn->contents;
11362      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11363      for (; dyncon < dynconend; dyncon++)
11364	{
11365	  Elf_Internal_Dyn dyn;
11366	  asection *s;
11367
11368	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11369
11370	  switch (dyn.d_tag)
11371	    {
11372	    default:
11373	      continue;
11374
11375	    case DT_PPC64_GLINK:
11376	      s = htab->glink;
11377	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11378	      /* We stupidly defined DT_PPC64_GLINK to be the start
11379		 of glink rather than the first entry point, which is
11380		 what ld.so needs, and now have a bigger stub to
11381		 support automatic multiple TOCs.  */
11382	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11383	      break;
11384
11385	    case DT_PPC64_OPD:
11386	      s = bfd_get_section_by_name (output_bfd, ".opd");
11387	      if (s == NULL)
11388		continue;
11389	      dyn.d_un.d_ptr = s->vma;
11390	      break;
11391
11392	    case DT_PPC64_OPDSZ:
11393	      s = bfd_get_section_by_name (output_bfd, ".opd");
11394	      if (s == NULL)
11395		continue;
11396	      dyn.d_un.d_val = s->size;
11397	      break;
11398
11399	    case DT_PLTGOT:
11400	      s = htab->plt;
11401	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11402	      break;
11403
11404	    case DT_JMPREL:
11405	      s = htab->relplt;
11406	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11407	      break;
11408
11409	    case DT_PLTRELSZ:
11410	      dyn.d_un.d_val = htab->relplt->size;
11411	      break;
11412
11413	    case DT_RELASZ:
11414	      /* Don't count procedure linkage table relocs in the
11415		 overall reloc count.  */
11416	      s = htab->relplt;
11417	      if (s == NULL)
11418		continue;
11419	      dyn.d_un.d_val -= s->size;
11420	      break;
11421
11422	    case DT_RELA:
11423	      /* We may not be using the standard ELF linker script.
11424		 If .rela.plt is the first .rela section, we adjust
11425		 DT_RELA to not include it.  */
11426	      s = htab->relplt;
11427	      if (s == NULL)
11428		continue;
11429	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11430		continue;
11431	      dyn.d_un.d_ptr += s->size;
11432	      break;
11433	    }
11434
11435	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11436	}
11437    }
11438
11439  if (htab->got != NULL && htab->got->size != 0)
11440    {
11441      /* Fill in the first entry in the global offset table.
11442	 We use it to hold the link-time TOCbase.  */
11443      bfd_put_64 (output_bfd,
11444		  elf_gp (output_bfd) + TOC_BASE_OFF,
11445		  htab->got->contents);
11446
11447      /* Set .got entry size.  */
11448      elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11449    }
11450
11451  if (htab->plt != NULL && htab->plt->size != 0)
11452    {
11453      /* Set .plt entry size.  */
11454      elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11455	= PLT_ENTRY_SIZE;
11456    }
11457
11458  /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
11459     brlt ourselves if emitrelocations.  */
11460  if (htab->brlt != NULL
11461      && htab->brlt->reloc_count != 0
11462      && !_bfd_elf_link_output_relocs (output_bfd,
11463				       htab->brlt,
11464				       &elf_section_data (htab->brlt)->rel_hdr,
11465				       elf_section_data (htab->brlt)->relocs,
11466				       NULL))
11467    return FALSE;
11468
11469  /* We need to handle writing out multiple GOT sections ourselves,
11470     since we didn't add them to DYNOBJ.  We know dynobj is the first
11471     bfd.  */
11472  while ((dynobj = dynobj->link_next) != NULL)
11473    {
11474      asection *s;
11475
11476      if (!is_ppc64_elf_target (dynobj->xvec))
11477	continue;
11478
11479      s = ppc64_elf_tdata (dynobj)->got;
11480      if (s != NULL
11481	  && s->size != 0
11482	  && s->output_section != bfd_abs_section_ptr
11483	  && !bfd_set_section_contents (output_bfd, s->output_section,
11484					s->contents, s->output_offset,
11485					s->size))
11486	return FALSE;
11487      s = ppc64_elf_tdata (dynobj)->relgot;
11488      if (s != NULL
11489	  && s->size != 0
11490	  && s->output_section != bfd_abs_section_ptr
11491	  && !bfd_set_section_contents (output_bfd, s->output_section,
11492					s->contents, s->output_offset,
11493					s->size))
11494	return FALSE;
11495    }
11496
11497  return TRUE;
11498}
11499
11500#include "elf64-target.h"
11501