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