PowerPC relocations for prefix insns
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2019 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5    Largely rewritten by Alan Modra.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_prefix_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
57   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_vma opd_entry_value
59   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
60
61 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
62 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
63 #define TARGET_BIG_SYM          powerpc_elf64_vec
64 #define TARGET_BIG_NAME         "elf64-powerpc"
65 #define ELF_ARCH                bfd_arch_powerpc
66 #define ELF_TARGET_ID           PPC64_ELF_DATA
67 #define ELF_MACHINE_CODE        EM_PPC64
68 #define ELF_MAXPAGESIZE         0x10000
69 #define ELF_COMMONPAGESIZE      0x1000
70 #define ELF_RELROPAGESIZE       ELF_MAXPAGESIZE
71 #define elf_info_to_howto       ppc64_elf_info_to_howto
72
73 #define elf_backend_want_got_sym 0
74 #define elf_backend_want_plt_sym 0
75 #define elf_backend_plt_alignment 3
76 #define elf_backend_plt_not_loaded 1
77 #define elf_backend_got_header_size 8
78 #define elf_backend_want_dynrelro 1
79 #define elf_backend_can_gc_sections 1
80 #define elf_backend_can_refcount 1
81 #define elf_backend_rela_normal 1
82 #define elf_backend_dtrel_excludes_plt 1
83 #define elf_backend_default_execstack 0
84
85 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
86 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
87 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
88 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
89 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
90 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
91 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
92 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
93 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
94 #define bfd_elf64_bfd_gc_sections             ppc64_elf_gc_sections
95
96 #define elf_backend_object_p                  ppc64_elf_object_p
97 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
98 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
99 #define elf_backend_write_core_note           ppc64_elf_write_core_note
100 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
101 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
102 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
103 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
104 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
105 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
106 #define elf_backend_check_relocs              ppc64_elf_check_relocs
107 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
108 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
109 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
110 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
111 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
112 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
113 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
114 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
115 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
116 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
117 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
118 #define elf_backend_action_discarded          ppc64_elf_action_discarded
119 #define elf_backend_relocate_section          ppc64_elf_relocate_section
120 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
121 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
122 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
123 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
124 #define elf_backend_special_sections          ppc64_elf_special_sections
125 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
126 #define elf_backend_merge_symbol              ppc64_elf_merge_symbol
127 #define elf_backend_get_reloc_section         bfd_get_section_by_name
128
129 /* The name of the dynamic interpreter.  This is put in the .interp
130    section.  */
131 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132
133 /* The size in bytes of an entry in the procedure linkage table.  */
134 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
135 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
136
137 /* The initial size of the plt reserved for the dynamic linker.  */
138 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
139
140 /* Offsets to some stack save slots.  */
141 #define STK_LR 16
142 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
143 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
144    CR save slot.  Used only by optimised __tls_get_addr call stub,
145    relying on __tls_get_addr_opt not saving CR..  */
146 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
147
148 /* TOC base pointers offset from start of TOC.  */
149 #define TOC_BASE_OFF    0x8000
150 /* TOC base alignment.  */
151 #define TOC_BASE_ALIGN  256
152
153 /* Offset of tp and dtp pointers from start of TLS block.  */
154 #define TP_OFFSET       0x7000
155 #define DTP_OFFSET      0x8000
156
157 /* .plt call stub instructions.  The normal stub is like this, but
158    sometimes the .plt entry crosses a 64k boundary and we need to
159    insert an addi to adjust r11.  */
160 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
161 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
162 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
163 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
164 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
165 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
166 #define BCTR            0x4e800420      /* bctr                      */
167
168 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
169 #define ADDI_R12_R11    0x398b0000      /* addi %r12,%r11,off@l  */
170 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
171 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
172 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
173
174 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
175 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
176 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
177 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
178 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
179 #define BNECTR          0x4ca20420      /* bnectr+               */
180 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
181
182 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
183 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
184 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
185
186 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
187 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
188 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
189
190 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
191 #define LIS_R12         0x3d800000      /* lis %r12,xxx@ha        */
192 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
193 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
194 #define ADDIS_R12_R11   0x3d8b0000      /* addis %r12,%r11,xxx@ha */
195 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
196 #define ORIS_R12_R12_0  0x658c0000      /* oris  %r12,%r12,xxx@hi */
197 #define ORI_R12_R12_0   0x618c0000      /* ori   %r12,%r12,xxx@l  */
198 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
199 #define SLDI_R12_R12_32 0x799c07c6      /* sldi  %r12,%r12,32     */
200 #define LDX_R12_R11_R12 0x7d8b602a      /* ldx   %r12,%r11,%r12   */
201 #define ADD_R12_R11_R12 0x7d8b6214      /* add   %r12,%r11,%r12   */
202 #define PNOP            0x0700000000000000ULL
203
204 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
205 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
206   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
207                                         /* 0:                           */
208                                         /*  .quad plt0-1f               */
209                                         /* __glink:                     */
210 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
211 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
212                                         /* 1:                           */
213 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
214                                         /*  ld %2,(0b-1b)(%11)          */
215 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
216 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
217                                         /*  ld %12,0(%11)               */
218                                         /*  ld %2,8(%11)                */
219                                         /*  mtctr %12                   */
220                                         /*  ld %11,16(%11)              */
221                                         /*  bctr                        */
222 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
223 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
224 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
225 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
226 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
227
228 /* Pad with this.  */
229 #define NOP             0x60000000
230
231 /* Some other nops.  */
232 #define CROR_151515     0x4def7b82
233 #define CROR_313131     0x4ffffb82
234
235 /* .glink entries for the first 32k functions are two instructions.  */
236 #define LI_R0_0         0x38000000      /* li    %r0,0          */
237 #define B_DOT           0x48000000      /* b     .              */
238
239 /* After that, we need two instructions to load the index, followed by
240    a branch.  */
241 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
242 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
243
244 /* Instructions used by the save and restore reg functions.  */
245 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
246 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
247 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
248 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
249 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
250 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
251 #define LI_R12_0        0x39800000      /* li    %r12,0         */
252 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
253 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
254 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
255 #define BLR             0x4e800020      /* blr                  */
256
257 /* Since .opd is an array of descriptors and each entry will end up
258    with identical R_PPC64_RELATIVE relocs, there is really no need to
259    propagate .opd relocs;  The dynamic linker should be taught to
260    relocate .opd without reloc entries.  */
261 #ifndef NO_OPD_RELOCS
262 #define NO_OPD_RELOCS 0
263 #endif
264
265 #ifndef ARRAY_SIZE
266 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
267 #endif
268
269 static inline int
270 abiversion (bfd *abfd)
271 {
272   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
273 }
274
275 static inline void
276 set_abiversion (bfd *abfd, int ver)
277 {
278   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
279   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
280 }
281 \f
282 /* Relocation HOWTO's.  */
283 /* Like other ELF RELA targets that don't apply multiple
284    field-altering relocations to the same localation, src_mask is
285    always zero and pcrel_offset is the same as pc_relative.
286    PowerPC can always use a zero bitpos, even when the field is not at
287    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
288    and bitpos=2 which matches the ABI description, or as we do here,
289    rightshift=0, bitsize=26 and bitpos=0.  */
290 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
291             complain, special_func)                             \
292   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,       \
293          complain_overflow_ ## complain, special_func,          \
294          #type, FALSE, 0, mask, pc_relative)
295
296 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
297
298 static reloc_howto_type ppc64_elf_howto_raw[] =
299 {
300   /* This reloc does nothing.  */
301   HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
302        bfd_elf_generic_reloc),
303
304   /* A standard 32 bit relocation.  */
305   HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
306        bfd_elf_generic_reloc),
307
308   /* An absolute 26 bit branch; the lower two bits must be zero.
309      FIXME: we don't check that, we just clear them.  */
310   HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
311        bfd_elf_generic_reloc),
312
313   /* A standard 16 bit relocation.  */
314   HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
315        bfd_elf_generic_reloc),
316
317   /* A 16 bit relocation without overflow.  */
318   HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
319        bfd_elf_generic_reloc),
320
321   /* Bits 16-31 of an address.  */
322   HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
323        bfd_elf_generic_reloc),
324
325   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
326      bits, treated as a signed number, is negative.  */
327   HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
328        ppc64_elf_ha_reloc),
329
330   /* An absolute 16 bit branch; the lower two bits must be zero.
331      FIXME: we don't check that, we just clear them.  */
332   HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
333        ppc64_elf_branch_reloc),
334
335   /* An absolute 16 bit branch, for which bit 10 should be set to
336      indicate that the branch is expected to be taken.  The lower two
337      bits must be zero.  */
338   HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
339        ppc64_elf_brtaken_reloc),
340
341   /* An absolute 16 bit branch, for which bit 10 should be set to
342      indicate that the branch is not expected to be taken.  The lower
343      two bits must be zero.  */
344   HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345        ppc64_elf_brtaken_reloc),
346
347   /* A relative 26 bit branch; the lower two bits must be zero.  */
348   HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
349        ppc64_elf_branch_reloc),
350
351   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
352   HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
353        ppc64_elf_branch_reloc),
354
355   /* A relative 16 bit branch; the lower two bits must be zero.  */
356   HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
357        ppc64_elf_branch_reloc),
358
359   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
360      the branch is expected to be taken.  The lower two bits must be
361      zero.  */
362   HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
363        ppc64_elf_brtaken_reloc),
364
365   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
366      the branch is not expected to be taken.  The lower two bits must
367      be zero.  */
368   HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369        ppc64_elf_brtaken_reloc),
370
371   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
372      symbol.  */
373   HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
374        ppc64_elf_unhandled_reloc),
375
376   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
377      the symbol.  */
378   HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
379        ppc64_elf_unhandled_reloc),
380
381   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
382      the symbol.  */
383   HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
384        ppc64_elf_unhandled_reloc),
385
386   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
387      the symbol.  */
388   HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
389        ppc64_elf_unhandled_reloc),
390
391   /* This is used only by the dynamic linker.  The symbol should exist
392      both in the object being run and in some shared library.  The
393      dynamic linker copies the data addressed by the symbol from the
394      shared library into the object, because the object being
395      run has to have the data at some particular address.  */
396   HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
397        ppc64_elf_unhandled_reloc),
398
399   /* Like R_PPC64_ADDR64, but used when setting global offset table
400      entries.  */
401   HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
402        ppc64_elf_unhandled_reloc),
403
404   /* Created by the link editor.  Marks a procedure linkage table
405      entry for a symbol.  */
406   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
407        ppc64_elf_unhandled_reloc),
408
409   /* Used only by the dynamic linker.  When the object is run, this
410      doubleword64 is set to the load address of the object, plus the
411      addend.  */
412   HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
413        bfd_elf_generic_reloc),
414
415   /* Like R_PPC64_ADDR32, but may be unaligned.  */
416   HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
417        bfd_elf_generic_reloc),
418
419   /* Like R_PPC64_ADDR16, but may be unaligned.  */
420   HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
421        bfd_elf_generic_reloc),
422
423   /* 32-bit PC relative.  */
424   HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
425        bfd_elf_generic_reloc),
426
427   /* 32-bit relocation to the symbol's procedure linkage table.  */
428   HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
429        ppc64_elf_unhandled_reloc),
430
431   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
432      FIXME: R_PPC64_PLTREL32 not supported.  */
433   HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
434        ppc64_elf_unhandled_reloc),
435
436   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
437      the symbol.  */
438   HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
439        ppc64_elf_unhandled_reloc),
440
441   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
442      the symbol.  */
443   HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
444        ppc64_elf_unhandled_reloc),
445
446   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
447      the symbol.  */
448   HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
449        ppc64_elf_unhandled_reloc),
450
451   /* 16-bit section relative relocation.  */
452   HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
453        ppc64_elf_sectoff_reloc),
454
455   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
456   HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
457        ppc64_elf_sectoff_reloc),
458
459   /* 16-bit upper half section relative relocation.  */
460   HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
461        ppc64_elf_sectoff_reloc),
462
463   /* 16-bit upper half adjusted section relative relocation.  */
464   HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
465        ppc64_elf_sectoff_ha_reloc),
466
467   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
468   HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
469        bfd_elf_generic_reloc),
470
471   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
472
473   /* A standard 64-bit relocation.  */
474   HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
475        bfd_elf_generic_reloc),
476
477   /* The bits 32-47 of an address.  */
478   HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
479        bfd_elf_generic_reloc),
480
481   /* The bits 32-47 of an address, plus 1 if the contents of the low
482      16 bits, treated as a signed number, is negative.  */
483   HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
484        ppc64_elf_ha_reloc),
485
486   /* The bits 48-63 of an address.  */
487   HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
488        bfd_elf_generic_reloc),
489
490   /* The bits 48-63 of an address, plus 1 if the contents of the low
491      16 bits, treated as a signed number, is negative.  */
492   HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
493        ppc64_elf_ha_reloc),
494
495   /* Like ADDR64, but may be unaligned.  */
496   HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
497        bfd_elf_generic_reloc),
498
499   /* 64-bit relative relocation.  */
500   HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
501        bfd_elf_generic_reloc),
502
503   /* 64-bit relocation to the symbol's procedure linkage table.  */
504   HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
505        ppc64_elf_unhandled_reloc),
506
507   /* 64-bit PC relative relocation to the symbol's procedure linkage
508      table.  */
509   /* FIXME: R_PPC64_PLTREL64 not supported.  */
510   HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
511        ppc64_elf_unhandled_reloc),
512
513   /* 16 bit TOC-relative relocation.  */
514   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
515   HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
516        ppc64_elf_toc_reloc),
517
518   /* 16 bit TOC-relative relocation without overflow.  */
519   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
520   HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
521        ppc64_elf_toc_reloc),
522
523   /* 16 bit TOC-relative relocation, high 16 bits.  */
524   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
525   HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
526        ppc64_elf_toc_reloc),
527
528   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
529      contents of the low 16 bits, treated as a signed number, is
530      negative.  */
531   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
532   HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
533        ppc64_elf_toc_ha_reloc),
534
535   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
536   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
537   HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
538        ppc64_elf_toc64_reloc),
539
540   /* Like R_PPC64_GOT16, but also informs the link editor that the
541      value to relocate may (!) refer to a PLT entry which the link
542      editor (a) may replace with the symbol value.  If the link editor
543      is unable to fully resolve the symbol, it may (b) create a PLT
544      entry and store the address to the new PLT entry in the GOT.
545      This permits lazy resolution of function symbols at run time.
546      The link editor may also skip all of this and just (c) emit a
547      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
548   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
549     HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
550           ppc64_elf_unhandled_reloc),
551
552   /* Like R_PPC64_PLTGOT16, but without overflow.  */
553   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
554   HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
555        ppc64_elf_unhandled_reloc),
556
557   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
558   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
559   HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
560        ppc64_elf_unhandled_reloc),
561
562   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
563      1 if the contents of the low 16 bits, treated as a signed number,
564      is negative.  */
565   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
566   HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
567        ppc64_elf_unhandled_reloc),
568
569   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
570   HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
571        bfd_elf_generic_reloc),
572
573   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
574   HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
575        bfd_elf_generic_reloc),
576
577   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
578   HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
579        ppc64_elf_unhandled_reloc),
580
581   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
582   HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
583        ppc64_elf_unhandled_reloc),
584
585   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
586   HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
587        ppc64_elf_unhandled_reloc),
588
589   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
590   HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
591        ppc64_elf_sectoff_reloc),
592
593   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
594   HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
595        ppc64_elf_sectoff_reloc),
596
597   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
598   HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
599        ppc64_elf_toc_reloc),
600
601   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
602   HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
603        ppc64_elf_toc_reloc),
604
605   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
606   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
607   HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
608        ppc64_elf_unhandled_reloc),
609
610   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
611   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
612   HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
613        ppc64_elf_unhandled_reloc),
614
615   /* Marker relocs for TLS.  */
616   HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
617        bfd_elf_generic_reloc),
618
619   HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
620        bfd_elf_generic_reloc),
621
622   HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
623        bfd_elf_generic_reloc),
624
625   /* Marker reloc for optimizing r2 save in prologue rather than on
626      each plt call stub.  */
627   HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
628        bfd_elf_generic_reloc),
629
630   /* Marker relocs on inline plt call instructions.  */
631   HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
632        bfd_elf_generic_reloc),
633
634   HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
635        bfd_elf_generic_reloc),
636
637   /* Computes the load module index of the load module that contains the
638      definition of its TLS sym.  */
639   HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
640        ppc64_elf_unhandled_reloc),
641
642   /* Computes a dtv-relative displacement, the difference between the value
643      of sym+add and the base address of the thread-local storage block that
644      contains the definition of sym, minus 0x8000.  */
645   HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646        ppc64_elf_unhandled_reloc),
647
648   /* A 16 bit dtprel reloc.  */
649   HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
650        ppc64_elf_unhandled_reloc),
651
652   /* Like DTPREL16, but no overflow.  */
653   HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
654        ppc64_elf_unhandled_reloc),
655
656   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
657   HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
658        ppc64_elf_unhandled_reloc),
659
660   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
661   HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
662        ppc64_elf_unhandled_reloc),
663
664   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
665   HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
666        ppc64_elf_unhandled_reloc),
667
668   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
669   HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
670        ppc64_elf_unhandled_reloc),
671
672   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
673   HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
674        ppc64_elf_unhandled_reloc),
675
676   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
677   HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
678        ppc64_elf_unhandled_reloc),
679
680   /* Like DTPREL16, but for insns with a DS field.  */
681   HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
682        ppc64_elf_unhandled_reloc),
683
684   /* Like DTPREL16_DS, but no overflow.  */
685   HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
686        ppc64_elf_unhandled_reloc),
687
688   /* Computes a tp-relative displacement, the difference between the value of
689      sym+add and the value of the thread pointer (r13).  */
690   HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
691        ppc64_elf_unhandled_reloc),
692
693   /* A 16 bit tprel reloc.  */
694   HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
695        ppc64_elf_unhandled_reloc),
696
697   /* Like TPREL16, but no overflow.  */
698   HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
699        ppc64_elf_unhandled_reloc),
700
701   /* Like TPREL16_LO, but next higher group of 16 bits.  */
702   HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
703        ppc64_elf_unhandled_reloc),
704
705   /* Like TPREL16_HI, but adjust for low 16 bits.  */
706   HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
707        ppc64_elf_unhandled_reloc),
708
709   /* Like TPREL16_HI, but next higher group of 16 bits.  */
710   HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
711        ppc64_elf_unhandled_reloc),
712
713   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
714   HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
715        ppc64_elf_unhandled_reloc),
716
717   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
718   HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
719        ppc64_elf_unhandled_reloc),
720
721   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
722   HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
723        ppc64_elf_unhandled_reloc),
724
725   /* Like TPREL16, but for insns with a DS field.  */
726   HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
727        ppc64_elf_unhandled_reloc),
728
729   /* Like TPREL16_DS, but no overflow.  */
730   HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
731        ppc64_elf_unhandled_reloc),
732
733   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
734      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
735      to the first entry relative to the TOC base (r2).  */
736   HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
737        ppc64_elf_unhandled_reloc),
738
739   /* Like GOT_TLSGD16, but no overflow.  */
740   HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
741        ppc64_elf_unhandled_reloc),
742
743   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
744   HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
745        ppc64_elf_unhandled_reloc),
746
747   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
748   HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
749        ppc64_elf_unhandled_reloc),
750
751   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
752      with values (sym+add)@dtpmod and zero, and computes the offset to the
753      first entry relative to the TOC base (r2).  */
754   HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
755        ppc64_elf_unhandled_reloc),
756
757   /* Like GOT_TLSLD16, but no overflow.  */
758   HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
759        ppc64_elf_unhandled_reloc),
760
761   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
762   HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
763        ppc64_elf_unhandled_reloc),
764
765   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
766   HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
767        ppc64_elf_unhandled_reloc),
768
769   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
770      the offset to the entry relative to the TOC base (r2).  */
771   HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
772        ppc64_elf_unhandled_reloc),
773
774   /* Like GOT_DTPREL16_DS, but no overflow.  */
775   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
776        ppc64_elf_unhandled_reloc),
777
778   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
779   HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
780        ppc64_elf_unhandled_reloc),
781
782   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
783   HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
784        ppc64_elf_unhandled_reloc),
785
786   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
787      offset to the entry relative to the TOC base (r2).  */
788   HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
789        ppc64_elf_unhandled_reloc),
790
791   /* Like GOT_TPREL16_DS, but no overflow.  */
792   HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
793        ppc64_elf_unhandled_reloc),
794
795   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
796   HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
797        ppc64_elf_unhandled_reloc),
798
799   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
800   HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
801        ppc64_elf_unhandled_reloc),
802
803   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
804        ppc64_elf_unhandled_reloc),
805
806   HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
807        bfd_elf_generic_reloc),
808
809   /* A 16 bit relative relocation.  */
810   HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
811        bfd_elf_generic_reloc),
812
813   /* A 16 bit relative relocation without overflow.  */
814   HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
815        bfd_elf_generic_reloc),
816
817   /* The high order 16 bits of a relative address.  */
818   HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
819        bfd_elf_generic_reloc),
820
821   /* The high order 16 bits of a relative address, plus 1 if the contents of
822      the low 16 bits, treated as a signed number, is negative.  */
823   HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
824        ppc64_elf_ha_reloc),
825
826   HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
827        bfd_elf_generic_reloc),
828
829   HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
830        ppc64_elf_ha_reloc),
831
832   HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
833        bfd_elf_generic_reloc),
834
835   HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
836        ppc64_elf_ha_reloc),
837
838   HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
839        bfd_elf_generic_reloc),
840
841   HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
842        ppc64_elf_ha_reloc),
843
844   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
845   HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
846        ppc64_elf_ha_reloc),
847
848   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
849   HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
850        ppc64_elf_ha_reloc),
851
852   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
853   HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
854        bfd_elf_generic_reloc),
855
856   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
857   HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
858        ppc64_elf_ha_reloc),
859
860   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
861   HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
862        ppc64_elf_unhandled_reloc),
863
864   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
865   HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
866        ppc64_elf_unhandled_reloc),
867
868   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
869   HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
870        ppc64_elf_unhandled_reloc),
871
872   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
873   HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
874        ppc64_elf_unhandled_reloc),
875
876   /* Marker reloc on ELFv2 large-model function entry.  */
877   HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
878        bfd_elf_generic_reloc),
879
880   /* Like ADDR64, but use local entry point of function.  */
881   HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
882        bfd_elf_generic_reloc),
883
884   HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
885        bfd_elf_generic_reloc),
886
887   HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
888        bfd_elf_generic_reloc),
889
890   HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
891        bfd_elf_generic_reloc),
892
893   HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
894        ppc64_elf_prefix_reloc),
895
896   HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
897        ppc64_elf_prefix_reloc),
898
899   HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
900        ppc64_elf_prefix_reloc),
901
902   HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
903        ppc64_elf_prefix_reloc),
904
905   HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
906        ppc64_elf_prefix_reloc),
907
908   HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
909        ppc64_elf_unhandled_reloc),
910
911   HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912        ppc64_elf_unhandled_reloc),
913
914   HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915        ppc64_elf_unhandled_reloc),
916
917   HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
918        bfd_elf_generic_reloc),
919
920   HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
921        ppc64_elf_ha_reloc),
922
923   HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
924        bfd_elf_generic_reloc),
925
926   HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
927        ppc64_elf_ha_reloc),
928
929   HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
930        bfd_elf_generic_reloc),
931
932   HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
933        ppc64_elf_ha_reloc),
934
935   HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
936        bfd_elf_generic_reloc),
937
938   HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
939        ppc64_elf_ha_reloc),
940
941   HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
942        ppc64_elf_prefix_reloc),
943
944   HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
945        ppc64_elf_prefix_reloc),
946
947   /* GNU extension to record C++ vtable hierarchy.  */
948   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
949        NULL),
950
951   /* GNU extension to record C++ vtable member usage.  */
952   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
953        NULL),
954 };
955
956 \f
957 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
958    be done.  */
959
960 static void
961 ppc_howto_init (void)
962 {
963   unsigned int i, type;
964
965   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
966     {
967       type = ppc64_elf_howto_raw[i].type;
968       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
969       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
970     }
971 }
972
973 static reloc_howto_type *
974 ppc64_elf_reloc_type_lookup (bfd *abfd,
975                              bfd_reloc_code_real_type code)
976 {
977   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
978
979   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
980     /* Initialize howto table if needed.  */
981     ppc_howto_init ();
982
983   switch (code)
984     {
985     default:
986       /* xgettext:c-format */
987       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
988                           (int) code);
989       bfd_set_error (bfd_error_bad_value);
990       return NULL;
991
992     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
993       break;
994     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
995       break;
996     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
997       break;
998     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
999       break;
1000     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1001       break;
1002     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1003       break;
1004     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
1005       break;
1006     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1007       break;
1008     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
1009       break;
1010     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1011       break;
1012     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1013       break;
1014     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1015       break;
1016     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1017       break;
1018     case BFD_RELOC_PPC64_REL24_NOTOC:           r = R_PPC64_REL24_NOTOC;
1019       break;
1020     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1021       break;
1022     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1023       break;
1024     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1025       break;
1026     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1027       break;
1028     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1029       break;
1030     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1031       break;
1032     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1033       break;
1034     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1035       break;
1036     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1037       break;
1038     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1039       break;
1040     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1041       break;
1042     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1043       break;
1044     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1045       break;
1046     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1047       break;
1048     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1049       break;
1050     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1051       break;
1052     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1053       break;
1054     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1055       break;
1056     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1057       break;
1058     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1059       break;
1060     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1061       break;
1062     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1063       break;
1064     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1065       break;
1066     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1067       break;
1068     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1069       break;
1070     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1071       break;
1072     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1073       break;
1074     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1075       break;
1076     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1077       break;
1078     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1079       break;
1080     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1081       break;
1082     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1083       break;
1084     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1085       break;
1086     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1087       break;
1088     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1089       break;
1090     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1091       break;
1092     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1093       break;
1094     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1095       break;
1096     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1097       break;
1098     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1099       break;
1100     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1101       break;
1102     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1103       break;
1104     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1105       break;
1106     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
1107       break;
1108     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
1109       break;
1110     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
1111       break;
1112     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
1113       break;
1114     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
1115       break;
1116     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
1117       break;
1118     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
1119       break;
1120     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
1121       break;
1122     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
1123       break;
1124     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
1125       break;
1126     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
1127       break;
1128     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
1129       break;
1130     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
1131       break;
1132     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
1133       break;
1134     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
1135       break;
1136     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
1137       break;
1138     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
1139       break;
1140     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
1141       break;
1142     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
1143       break;
1144     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
1145       break;
1146     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
1147       break;
1148     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
1149       break;
1150     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
1151       break;
1152     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
1153       break;
1154     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
1155       break;
1156     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
1157       break;
1158     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
1159       break;
1160     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
1161       break;
1162     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
1163       break;
1164     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
1165       break;
1166     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
1167       break;
1168     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
1169       break;
1170     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
1171       break;
1172     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
1173       break;
1174     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
1175       break;
1176     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
1177       break;
1178     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
1179       break;
1180     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
1181       break;
1182     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
1183       break;
1184     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
1185       break;
1186     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
1187       break;
1188     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
1189       break;
1190     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
1191       break;
1192     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
1193       break;
1194     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
1195       break;
1196     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
1197       break;
1198     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
1199       break;
1200     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
1201       break;
1202     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
1203       break;
1204     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
1205       break;
1206     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
1207       break;
1208     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
1209       break;
1210     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
1211       break;
1212     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
1213       break;
1214     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
1215       break;
1216     case BFD_RELOC_PPC64_REL16_HIGH:            r = R_PPC64_REL16_HIGH;
1217       break;
1218     case BFD_RELOC_PPC64_REL16_HIGHA:           r = R_PPC64_REL16_HIGHA;
1219       break;
1220     case BFD_RELOC_PPC64_REL16_HIGHER:          r = R_PPC64_REL16_HIGHER;
1221       break;
1222     case BFD_RELOC_PPC64_REL16_HIGHERA:         r = R_PPC64_REL16_HIGHERA;
1223       break;
1224     case BFD_RELOC_PPC64_REL16_HIGHEST:         r = R_PPC64_REL16_HIGHEST;
1225       break;
1226     case BFD_RELOC_PPC64_REL16_HIGHESTA:        r = R_PPC64_REL16_HIGHESTA;
1227       break;
1228     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
1229       break;
1230     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
1231       break;
1232     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
1233       break;
1234     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
1235       break;
1236     case BFD_RELOC_PPC64_D34:                   r = R_PPC64_D34;
1237       break;
1238     case BFD_RELOC_PPC64_D34_LO:                r = R_PPC64_D34_LO;
1239       break;
1240     case BFD_RELOC_PPC64_D34_HI30:              r = R_PPC64_D34_HI30;
1241       break;
1242     case BFD_RELOC_PPC64_D34_HA30:              r = R_PPC64_D34_HA30;
1243       break;
1244     case BFD_RELOC_PPC64_PCREL34:               r = R_PPC64_PCREL34;
1245       break;
1246     case BFD_RELOC_PPC64_GOT_PCREL34:           r = R_PPC64_GOT_PCREL34;
1247       break;
1248     case BFD_RELOC_PPC64_PLT_PCREL34:           r = R_PPC64_PLT_PCREL34;
1249       break;
1250     case BFD_RELOC_PPC64_ADDR16_HIGHER34:       r = R_PPC64_ADDR16_HIGHER34;
1251       break;
1252     case BFD_RELOC_PPC64_ADDR16_HIGHERA34:      r = R_PPC64_ADDR16_HIGHERA34;
1253       break;
1254     case BFD_RELOC_PPC64_ADDR16_HIGHEST34:      r = R_PPC64_ADDR16_HIGHEST34;
1255       break;
1256     case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:     r = R_PPC64_ADDR16_HIGHESTA34;
1257       break;
1258     case BFD_RELOC_PPC64_REL16_HIGHER34:        r = R_PPC64_REL16_HIGHER34;
1259       break;
1260     case BFD_RELOC_PPC64_REL16_HIGHERA34:       r = R_PPC64_REL16_HIGHERA34;
1261       break;
1262     case BFD_RELOC_PPC64_REL16_HIGHEST34:       r = R_PPC64_REL16_HIGHEST34;
1263       break;
1264     case BFD_RELOC_PPC64_REL16_HIGHESTA34:      r = R_PPC64_REL16_HIGHESTA34;
1265       break;
1266     case BFD_RELOC_PPC64_D28:                   r = R_PPC64_D28;
1267       break;
1268     case BFD_RELOC_PPC64_PCREL28:               r = R_PPC64_PCREL28;
1269       break;
1270     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
1271       break;
1272     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
1273       break;
1274     }
1275
1276   return ppc64_elf_howto_table[r];
1277 };
1278
1279 static reloc_howto_type *
1280 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1281                              const char *r_name)
1282 {
1283   unsigned int i;
1284
1285   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1286     if (ppc64_elf_howto_raw[i].name != NULL
1287         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1288       return &ppc64_elf_howto_raw[i];
1289
1290   return NULL;
1291 }
1292
1293 /* Set the howto pointer for a PowerPC ELF reloc.  */
1294
1295 static bfd_boolean
1296 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1297                          Elf_Internal_Rela *dst)
1298 {
1299   unsigned int type;
1300
1301   /* Initialize howto table if needed.  */
1302   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1303     ppc_howto_init ();
1304
1305   type = ELF64_R_TYPE (dst->r_info);
1306   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1307     {
1308       /* xgettext:c-format */
1309       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1310                           abfd, type);
1311       bfd_set_error (bfd_error_bad_value);
1312       return FALSE;
1313     }
1314   cache_ptr->howto = ppc64_elf_howto_table[type];
1315   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1316     {
1317       /* xgettext:c-format */
1318       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1319                           abfd, type);
1320       bfd_set_error (bfd_error_bad_value);
1321       return FALSE;
1322     }
1323
1324   return TRUE;
1325 }
1326
1327 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1328
1329 static bfd_reloc_status_type
1330 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1331                     void *data, asection *input_section,
1332                     bfd *output_bfd, char **error_message)
1333 {
1334   enum elf_ppc64_reloc_type r_type;
1335   long insn;
1336   bfd_size_type octets;
1337   bfd_vma value;
1338
1339   /* If this is a relocatable link (output_bfd test tells us), just
1340      call the generic function.  Any adjustment will be done at final
1341      link time.  */
1342   if (output_bfd != NULL)
1343     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1344                                   input_section, output_bfd, error_message);
1345
1346   /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1347      We won't actually be using the low bits, so trashing them
1348      doesn't matter.  */
1349   r_type = reloc_entry->howto->type;
1350   if (r_type == R_PPC64_ADDR16_HIGHERA34
1351       || r_type == R_PPC64_ADDR16_HIGHESTA34
1352       || r_type == R_PPC64_REL16_HIGHERA34
1353       || r_type == R_PPC64_REL16_HIGHESTA34)
1354     reloc_entry->addend += 1ULL << 33;
1355   else
1356     reloc_entry->addend += 1U << 15;
1357   if (r_type != R_PPC64_REL16DX_HA)
1358     return bfd_reloc_continue;
1359
1360   value = 0;
1361   if (!bfd_is_com_section (symbol->section))
1362     value = symbol->value;
1363   value += (reloc_entry->addend
1364             + symbol->section->output_offset
1365             + symbol->section->output_section->vma);
1366   value -= (reloc_entry->address
1367             + input_section->output_offset
1368             + input_section->output_section->vma);
1369   value = (bfd_signed_vma) value >> 16;
1370
1371   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1372   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1373   insn &= ~0x1fffc1;
1374   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1375   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1376   if (value + 0x8000 > 0xffff)
1377     return bfd_reloc_overflow;
1378   return bfd_reloc_ok;
1379 }
1380
1381 static bfd_reloc_status_type
1382 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1383                         void *data, asection *input_section,
1384                         bfd *output_bfd, char **error_message)
1385 {
1386   if (output_bfd != NULL)
1387     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1388                                   input_section, output_bfd, error_message);
1389
1390   if (strcmp (symbol->section->name, ".opd") == 0
1391       && (symbol->section->owner->flags & DYNAMIC) == 0)
1392     {
1393       bfd_vma dest = opd_entry_value (symbol->section,
1394                                       symbol->value + reloc_entry->addend,
1395                                       NULL, NULL, FALSE);
1396       if (dest != (bfd_vma) -1)
1397         reloc_entry->addend = dest - (symbol->value
1398                                       + symbol->section->output_section->vma
1399                                       + symbol->section->output_offset);
1400     }
1401   else
1402     {
1403       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1404
1405       if (symbol->section->owner != abfd
1406           && symbol->section->owner != NULL
1407           && abiversion (symbol->section->owner) >= 2)
1408         {
1409           unsigned int i;
1410
1411           for (i = 0; i < symbol->section->owner->symcount; ++i)
1412             {
1413               asymbol *symdef = symbol->section->owner->outsymbols[i];
1414
1415               if (strcmp (symdef->name, symbol->name) == 0)
1416                 {
1417                   elfsym = (elf_symbol_type *) symdef;
1418                   break;
1419                 }
1420             }
1421         }
1422       reloc_entry->addend
1423         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1424     }
1425   return bfd_reloc_continue;
1426 }
1427
1428 static bfd_reloc_status_type
1429 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1430                          void *data, asection *input_section,
1431                          bfd *output_bfd, char **error_message)
1432 {
1433   long insn;
1434   enum elf_ppc64_reloc_type r_type;
1435   bfd_size_type octets;
1436   /* Assume 'at' branch hints.  */
1437   bfd_boolean is_isa_v2 = TRUE;
1438
1439   /* If this is a relocatable link (output_bfd test tells us), just
1440      call the generic function.  Any adjustment will be done at final
1441      link time.  */
1442   if (output_bfd != NULL)
1443     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1444                                   input_section, output_bfd, error_message);
1445
1446   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1447   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448   insn &= ~(0x01 << 21);
1449   r_type = reloc_entry->howto->type;
1450   if (r_type == R_PPC64_ADDR14_BRTAKEN
1451       || r_type == R_PPC64_REL14_BRTAKEN)
1452     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1453
1454   if (is_isa_v2)
1455     {
1456       /* Set 'a' bit.  This is 0b00010 in BO field for branch
1457          on CR(BI) insns (BO == 001at or 011at), and 0b01000
1458          for branch on CTR insns (BO == 1a00t or 1a01t).  */
1459       if ((insn & (0x14 << 21)) == (0x04 << 21))
1460         insn |= 0x02 << 21;
1461       else if ((insn & (0x14 << 21)) == (0x10 << 21))
1462         insn |= 0x08 << 21;
1463       else
1464         goto out;
1465     }
1466   else
1467     {
1468       bfd_vma target = 0;
1469       bfd_vma from;
1470
1471       if (!bfd_is_com_section (symbol->section))
1472         target = symbol->value;
1473       target += symbol->section->output_section->vma;
1474       target += symbol->section->output_offset;
1475       target += reloc_entry->addend;
1476
1477       from = (reloc_entry->address
1478               + input_section->output_offset
1479               + input_section->output_section->vma);
1480
1481       /* Invert 'y' bit if not the default.  */
1482       if ((bfd_signed_vma) (target - from) < 0)
1483         insn ^= 0x01 << 21;
1484     }
1485   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1486  out:
1487   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1488                                  input_section, output_bfd, error_message);
1489 }
1490
1491 static bfd_reloc_status_type
1492 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1493                          void *data, asection *input_section,
1494                          bfd *output_bfd, char **error_message)
1495 {
1496   /* If this is a relocatable link (output_bfd test tells us), just
1497      call the generic function.  Any adjustment will be done at final
1498      link time.  */
1499   if (output_bfd != NULL)
1500     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1501                                   input_section, output_bfd, error_message);
1502
1503   /* Subtract the symbol section base address.  */
1504   reloc_entry->addend -= symbol->section->output_section->vma;
1505   return bfd_reloc_continue;
1506 }
1507
1508 static bfd_reloc_status_type
1509 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1510                             void *data, asection *input_section,
1511                             bfd *output_bfd, char **error_message)
1512 {
1513   /* If this is a relocatable link (output_bfd test tells us), just
1514      call the generic function.  Any adjustment will be done at final
1515      link time.  */
1516   if (output_bfd != NULL)
1517     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1518                                   input_section, output_bfd, error_message);
1519
1520   /* Subtract the symbol section base address.  */
1521   reloc_entry->addend -= symbol->section->output_section->vma;
1522
1523   /* Adjust the addend for sign extension of the low 16 bits.  */
1524   reloc_entry->addend += 0x8000;
1525   return bfd_reloc_continue;
1526 }
1527
1528 static bfd_reloc_status_type
1529 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530                      void *data, asection *input_section,
1531                      bfd *output_bfd, char **error_message)
1532 {
1533   bfd_vma TOCstart;
1534
1535   /* If this is a relocatable link (output_bfd test tells us), just
1536      call the generic function.  Any adjustment will be done at final
1537      link time.  */
1538   if (output_bfd != NULL)
1539     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1540                                   input_section, output_bfd, error_message);
1541
1542   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1543   if (TOCstart == 0)
1544     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1545
1546   /* Subtract the TOC base address.  */
1547   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1548   return bfd_reloc_continue;
1549 }
1550
1551 static bfd_reloc_status_type
1552 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1553                         void *data, asection *input_section,
1554                         bfd *output_bfd, char **error_message)
1555 {
1556   bfd_vma TOCstart;
1557
1558   /* If this is a relocatable link (output_bfd test tells us), just
1559      call the generic function.  Any adjustment will be done at final
1560      link time.  */
1561   if (output_bfd != NULL)
1562     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1563                                   input_section, output_bfd, error_message);
1564
1565   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1566   if (TOCstart == 0)
1567     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1568
1569   /* Subtract the TOC base address.  */
1570   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1571
1572   /* Adjust the addend for sign extension of the low 16 bits.  */
1573   reloc_entry->addend += 0x8000;
1574   return bfd_reloc_continue;
1575 }
1576
1577 static bfd_reloc_status_type
1578 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1579                        void *data, asection *input_section,
1580                        bfd *output_bfd, char **error_message)
1581 {
1582   bfd_vma TOCstart;
1583   bfd_size_type octets;
1584
1585   /* If this is a relocatable link (output_bfd test tells us), just
1586      call the generic function.  Any adjustment will be done at final
1587      link time.  */
1588   if (output_bfd != NULL)
1589     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1590                                   input_section, output_bfd, error_message);
1591
1592   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1593   if (TOCstart == 0)
1594     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1595
1596   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1597   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1598   return bfd_reloc_ok;
1599 }
1600
1601 static bfd_reloc_status_type
1602 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1603                         void *data, asection *input_section,
1604                         bfd *output_bfd, char **error_message)
1605 {
1606   uint64_t insn;
1607   bfd_vma targ;
1608
1609   if (output_bfd != NULL)
1610     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1611                                   input_section, output_bfd, error_message);
1612
1613   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1614   insn <<= 32;
1615   insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1616
1617   targ = (symbol->section->output_section->vma
1618           + symbol->section->output_offset
1619           + reloc_entry->addend);
1620   if (!bfd_is_com_section (symbol->section))
1621     targ += symbol->value;
1622   if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1623     targ += 1ULL << 33;
1624   if (reloc_entry->howto->pc_relative)
1625     {
1626       bfd_vma from = (reloc_entry->address
1627                       + input_section->output_offset
1628                       + input_section->output_section->vma);
1629       targ -=from;
1630     }
1631   targ >>= reloc_entry->howto->rightshift;
1632   insn &= ~reloc_entry->howto->dst_mask;
1633   insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1634   bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1635   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1636   if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1637       && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1638           >= 1ULL << reloc_entry->howto->bitsize))
1639     return bfd_reloc_overflow;
1640   return bfd_reloc_ok;
1641 }
1642
1643 static bfd_reloc_status_type
1644 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1645                            void *data, asection *input_section,
1646                            bfd *output_bfd, char **error_message)
1647 {
1648   /* If this is a relocatable link (output_bfd test tells us), just
1649      call the generic function.  Any adjustment will be done at final
1650      link time.  */
1651   if (output_bfd != NULL)
1652     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1653                                   input_section, output_bfd, error_message);
1654
1655   if (error_message != NULL)
1656     {
1657       static char buf[60];
1658       sprintf (buf, "generic linker can't handle %s",
1659                reloc_entry->howto->name);
1660       *error_message = buf;
1661     }
1662   return bfd_reloc_dangerous;
1663 }
1664
1665 /* Track GOT entries needed for a given symbol.  We might need more
1666    than one got entry per symbol.  */
1667 struct got_entry
1668 {
1669   struct got_entry *next;
1670
1671   /* The symbol addend that we'll be placing in the GOT.  */
1672   bfd_vma addend;
1673
1674   /* Unlike other ELF targets, we use separate GOT entries for the same
1675      symbol referenced from different input files.  This is to support
1676      automatic multiple TOC/GOT sections, where the TOC base can vary
1677      from one input file to another.  After partitioning into TOC groups
1678      we merge entries within the group.
1679
1680      Point to the BFD owning this GOT entry.  */
1681   bfd *owner;
1682
1683   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1684      TLS_TPREL or TLS_DTPREL for tls entries.  */
1685   unsigned char tls_type;
1686
1687   /* Non-zero if got.ent points to real entry.  */
1688   unsigned char is_indirect;
1689
1690   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1691   union
1692   {
1693     bfd_signed_vma refcount;
1694     bfd_vma offset;
1695     struct got_entry *ent;
1696   } got;
1697 };
1698
1699 /* The same for PLT.  */
1700 struct plt_entry
1701 {
1702   struct plt_entry *next;
1703
1704   bfd_vma addend;
1705
1706   union
1707   {
1708     bfd_signed_vma refcount;
1709     bfd_vma offset;
1710   } plt;
1711 };
1712
1713 struct ppc64_elf_obj_tdata
1714 {
1715   struct elf_obj_tdata elf;
1716
1717   /* Shortcuts to dynamic linker sections.  */
1718   asection *got;
1719   asection *relgot;
1720
1721   /* Used during garbage collection.  We attach global symbols defined
1722      on removed .opd entries to this section so that the sym is removed.  */
1723   asection *deleted_section;
1724
1725   /* TLS local dynamic got entry handling.  Support for multiple GOT
1726      sections means we potentially need one of these for each input bfd.  */
1727   struct got_entry tlsld_got;
1728
1729   union
1730   {
1731     /* A copy of relocs before they are modified for --emit-relocs.  */
1732     Elf_Internal_Rela *relocs;
1733
1734     /* Section contents.  */
1735     bfd_byte *contents;
1736   } opd;
1737
1738   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1739      the reloc to be in the range -32768 to 32767.  */
1740   unsigned int has_small_toc_reloc : 1;
1741
1742   /* Set if toc/got ha relocs detected not using r2, or lo reloc
1743      instruction not one we handle.  */
1744   unsigned int unexpected_toc_insn : 1;
1745
1746   /* Set if got relocs that can be optimised are present in this file.  */
1747   unsigned int has_gotrel : 1;
1748 };
1749
1750 #define ppc64_elf_tdata(bfd) \
1751   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1752
1753 #define ppc64_tlsld_got(bfd) \
1754   (&ppc64_elf_tdata (bfd)->tlsld_got)
1755
1756 #define is_ppc64_elf(bfd) \
1757   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1758    && elf_object_id (bfd) == PPC64_ELF_DATA)
1759
1760 /* Override the generic function because we store some extras.  */
1761
1762 static bfd_boolean
1763 ppc64_elf_mkobject (bfd *abfd)
1764 {
1765   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1766                                   PPC64_ELF_DATA);
1767 }
1768
1769 /* Fix bad default arch selected for a 64 bit input bfd when the
1770    default is 32 bit.  Also select arch based on apuinfo.  */
1771
1772 static bfd_boolean
1773 ppc64_elf_object_p (bfd *abfd)
1774 {
1775   if (!abfd->arch_info->the_default)
1776     return TRUE;
1777
1778   if (abfd->arch_info->bits_per_word == 32)
1779     {
1780       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1781
1782       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1783         {
1784           /* Relies on arch after 32 bit default being 64 bit default.  */
1785           abfd->arch_info = abfd->arch_info->next;
1786           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1787         }
1788     }
1789   return _bfd_elf_ppc_set_arch (abfd);
1790 }
1791
1792 /* Support for core dump NOTE sections.  */
1793
1794 static bfd_boolean
1795 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1796 {
1797   size_t offset, size;
1798
1799   if (note->descsz != 504)
1800     return FALSE;
1801
1802   /* pr_cursig */
1803   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1804
1805   /* pr_pid */
1806   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1807
1808   /* pr_reg */
1809   offset = 112;
1810   size = 384;
1811
1812   /* Make a ".reg/999" section.  */
1813   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1814                                           size, note->descpos + offset);
1815 }
1816
1817 static bfd_boolean
1818 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1819 {
1820   if (note->descsz != 136)
1821     return FALSE;
1822
1823   elf_tdata (abfd)->core->pid
1824     = bfd_get_32 (abfd, note->descdata + 24);
1825   elf_tdata (abfd)->core->program
1826     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1827   elf_tdata (abfd)->core->command
1828     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1829
1830   return TRUE;
1831 }
1832
1833 static char *
1834 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1835                            ...)
1836 {
1837   switch (note_type)
1838     {
1839     default:
1840       return NULL;
1841
1842     case NT_PRPSINFO:
1843       {
1844         char data[136] ATTRIBUTE_NONSTRING;
1845         va_list ap;
1846
1847         va_start (ap, note_type);
1848         memset (data, 0, sizeof (data));
1849         strncpy (data + 40, va_arg (ap, const char *), 16);
1850 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1851         DIAGNOSTIC_PUSH;
1852         /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1853            -Wstringop-truncation:
1854            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1855          */
1856         DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1857 #endif
1858         strncpy (data + 56, va_arg (ap, const char *), 80);
1859 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1860         DIAGNOSTIC_POP;
1861 #endif
1862         va_end (ap);
1863         return elfcore_write_note (abfd, buf, bufsiz,
1864                                    "CORE", note_type, data, sizeof (data));
1865       }
1866
1867     case NT_PRSTATUS:
1868       {
1869         char data[504];
1870         va_list ap;
1871         long pid;
1872         int cursig;
1873         const void *greg;
1874
1875         va_start (ap, note_type);
1876         memset (data, 0, 112);
1877         pid = va_arg (ap, long);
1878         bfd_put_32 (abfd, pid, data + 32);
1879         cursig = va_arg (ap, int);
1880         bfd_put_16 (abfd, cursig, data + 12);
1881         greg = va_arg (ap, const void *);
1882         memcpy (data + 112, greg, 384);
1883         memset (data + 496, 0, 8);
1884         va_end (ap);
1885         return elfcore_write_note (abfd, buf, bufsiz,
1886                                    "CORE", note_type, data, sizeof (data));
1887       }
1888     }
1889 }
1890
1891 /* Add extra PPC sections.  */
1892
1893 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1894 {
1895   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1896   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1897   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1898   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1899   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1900   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1901   { NULL,                     0,  0, 0,            0 }
1902 };
1903
1904 enum _ppc64_sec_type {
1905   sec_normal = 0,
1906   sec_opd = 1,
1907   sec_toc = 2
1908 };
1909
1910 struct _ppc64_elf_section_data
1911 {
1912   struct bfd_elf_section_data elf;
1913
1914   union
1915   {
1916     /* An array with one entry for each opd function descriptor,
1917        and some spares since opd entries may be either 16 or 24 bytes.  */
1918 #define OPD_NDX(OFF) ((OFF) >> 4)
1919     struct _opd_sec_data
1920     {
1921       /* Points to the function code section for local opd entries.  */
1922       asection **func_sec;
1923
1924       /* After editing .opd, adjust references to opd local syms.  */
1925       long *adjust;
1926     } opd;
1927
1928     /* An array for toc sections, indexed by offset/8.  */
1929     struct _toc_sec_data
1930     {
1931       /* Specifies the relocation symbol index used at a given toc offset.  */
1932       unsigned *symndx;
1933
1934       /* And the relocation addend.  */
1935       bfd_vma *add;
1936     } toc;
1937   } u;
1938
1939   enum _ppc64_sec_type sec_type:2;
1940
1941   /* Flag set when small branches are detected.  Used to
1942      select suitable defaults for the stub group size.  */
1943   unsigned int has_14bit_branch:1;
1944
1945   /* Flag set when PLTCALL relocs are detected.  */
1946   unsigned int has_pltcall:1;
1947
1948   /* Flag set when section has GOT relocations that can be optimised.  */
1949   unsigned int has_gotrel:1;
1950 };
1951
1952 #define ppc64_elf_section_data(sec) \
1953   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1954
1955 static bfd_boolean
1956 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1957 {
1958   if (!sec->used_by_bfd)
1959     {
1960       struct _ppc64_elf_section_data *sdata;
1961       bfd_size_type amt = sizeof (*sdata);
1962
1963       sdata = bfd_zalloc (abfd, amt);
1964       if (sdata == NULL)
1965         return FALSE;
1966       sec->used_by_bfd = sdata;
1967     }
1968
1969   return _bfd_elf_new_section_hook (abfd, sec);
1970 }
1971
1972 static struct _opd_sec_data *
1973 get_opd_info (asection * sec)
1974 {
1975   if (sec != NULL
1976       && ppc64_elf_section_data (sec) != NULL
1977       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1978     return &ppc64_elf_section_data (sec)->u.opd;
1979   return NULL;
1980 }
1981 \f
1982 /* Parameters for the qsort hook.  */
1983 static bfd_boolean synthetic_relocatable;
1984 static asection *synthetic_opd;
1985
1986 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
1987
1988 static int
1989 compare_symbols (const void *ap, const void *bp)
1990 {
1991   const asymbol *a = *(const asymbol **) ap;
1992   const asymbol *b = *(const asymbol **) bp;
1993
1994   /* Section symbols first.  */
1995   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1996     return -1;
1997   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1998     return 1;
1999
2000   /* then .opd symbols.  */
2001   if (synthetic_opd != NULL)
2002     {
2003       if (strcmp (a->section->name, ".opd") == 0
2004           && strcmp (b->section->name, ".opd") != 0)
2005         return -1;
2006       if (strcmp (a->section->name, ".opd") != 0
2007           && strcmp (b->section->name, ".opd") == 0)
2008         return 1;
2009     }
2010
2011   /* then other code symbols.  */
2012   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2013        == (SEC_CODE | SEC_ALLOC))
2014       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2015           != (SEC_CODE | SEC_ALLOC)))
2016     return -1;
2017
2018   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2019        != (SEC_CODE | SEC_ALLOC))
2020       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2021           == (SEC_CODE | SEC_ALLOC)))
2022     return 1;
2023
2024   if (synthetic_relocatable)
2025     {
2026       if (a->section->id < b->section->id)
2027         return -1;
2028
2029       if (a->section->id > b->section->id)
2030         return 1;
2031     }
2032
2033   if (a->value + a->section->vma < b->value + b->section->vma)
2034     return -1;
2035
2036   if (a->value + a->section->vma > b->value + b->section->vma)
2037     return 1;
2038
2039   /* For syms with the same value, prefer strong dynamic global function
2040      syms over other syms.  */
2041   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2042     return -1;
2043
2044   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2045     return 1;
2046
2047   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2048     return -1;
2049
2050   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2051     return 1;
2052
2053   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2054     return -1;
2055
2056   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2057     return 1;
2058
2059   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2060     return -1;
2061
2062   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2063     return 1;
2064
2065   return a > b;
2066 }
2067
2068 /* Search SYMS for a symbol of the given VALUE.  */
2069
2070 static asymbol *
2071 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2072 {
2073   long mid;
2074
2075   if (id == (unsigned) -1)
2076     {
2077       while (lo < hi)
2078         {
2079           mid = (lo + hi) >> 1;
2080           if (syms[mid]->value + syms[mid]->section->vma < value)
2081             lo = mid + 1;
2082           else if (syms[mid]->value + syms[mid]->section->vma > value)
2083             hi = mid;
2084           else
2085             return syms[mid];
2086         }
2087     }
2088   else
2089     {
2090       while (lo < hi)
2091         {
2092           mid = (lo + hi) >> 1;
2093           if (syms[mid]->section->id < id)
2094             lo = mid + 1;
2095           else if (syms[mid]->section->id > id)
2096             hi = mid;
2097           else if (syms[mid]->value < value)
2098             lo = mid + 1;
2099           else if (syms[mid]->value > value)
2100             hi = mid;
2101           else
2102             return syms[mid];
2103         }
2104     }
2105   return NULL;
2106 }
2107
2108 static bfd_boolean
2109 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2110 {
2111   bfd_vma vma = *(bfd_vma *) ptr;
2112   return ((section->flags & SEC_ALLOC) != 0
2113           && section->vma <= vma
2114           && vma < section->vma + section->size);
2115 }
2116
2117 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2118    entry syms.  Also generate @plt symbols for the glink branch table.
2119    Returns count of synthetic symbols in RET or -1 on error.  */
2120
2121 static long
2122 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2123                                 long static_count, asymbol **static_syms,
2124                                 long dyn_count, asymbol **dyn_syms,
2125                                 asymbol **ret)
2126 {
2127   asymbol *s;
2128   size_t i, j, count;
2129   char *names;
2130   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2131   asection *opd = NULL;
2132   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2133   asymbol **syms;
2134   int abi = abiversion (abfd);
2135
2136   *ret = NULL;
2137
2138   if (abi < 2)
2139     {
2140       opd = bfd_get_section_by_name (abfd, ".opd");
2141       if (opd == NULL && abi == 1)
2142         return 0;
2143     }
2144
2145   syms = NULL;
2146   codesecsym = 0;
2147   codesecsymend = 0;
2148   secsymend = 0;
2149   opdsymend = 0;
2150   symcount = 0;
2151   if (opd != NULL)
2152     {
2153       symcount = static_count;
2154       if (!relocatable)
2155         symcount += dyn_count;
2156       if (symcount == 0)
2157         return 0;
2158
2159       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2160       if (syms == NULL)
2161         return -1;
2162
2163       if (!relocatable && static_count != 0 && dyn_count != 0)
2164         {
2165           /* Use both symbol tables.  */
2166           memcpy (syms, static_syms, static_count * sizeof (*syms));
2167           memcpy (syms + static_count, dyn_syms,
2168                   (dyn_count + 1) * sizeof (*syms));
2169         }
2170       else if (!relocatable && static_count == 0)
2171         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2172       else
2173         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2174
2175       /* Trim uninteresting symbols.  Interesting symbols are section,
2176          function, and notype symbols.  */
2177       for (i = 0, j = 0; i < symcount; ++i)
2178         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2179                                | BSF_RELC | BSF_SRELC)) == 0)
2180           syms[j++] = syms[i];
2181       symcount = j;
2182
2183       synthetic_relocatable = relocatable;
2184       synthetic_opd = opd;
2185       qsort (syms, symcount, sizeof (*syms), compare_symbols);
2186
2187       if (!relocatable && symcount > 1)
2188         {
2189           /* Trim duplicate syms, since we may have merged the normal
2190              and dynamic symbols.  Actually, we only care about syms
2191              that have different values, so trim any with the same
2192              value.  Don't consider ifunc and ifunc resolver symbols
2193              duplicates however, because GDB wants to know whether a
2194              text symbol is an ifunc resolver.  */
2195           for (i = 1, j = 1; i < symcount; ++i)
2196             {
2197               const asymbol *s0 = syms[i - 1];
2198               const asymbol *s1 = syms[i];
2199
2200               if ((s0->value + s0->section->vma
2201                    != s1->value + s1->section->vma)
2202                   || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2203                       != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2204                 syms[j++] = syms[i];
2205             }
2206           symcount = j;
2207         }
2208
2209       i = 0;
2210       /* Note that here and in compare_symbols we can't compare opd and
2211          sym->section directly.  With separate debug info files, the
2212          symbols will be extracted from the debug file while abfd passed
2213          to this function is the real binary.  */
2214       if (strcmp (syms[i]->section->name, ".opd") == 0)
2215         ++i;
2216       codesecsym = i;
2217
2218       for (; i < symcount; ++i)
2219         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2220                                          | SEC_THREAD_LOCAL))
2221              != (SEC_CODE | SEC_ALLOC))
2222             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2223           break;
2224       codesecsymend = i;
2225
2226       for (; i < symcount; ++i)
2227         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2228           break;
2229       secsymend = i;
2230
2231       for (; i < symcount; ++i)
2232         if (strcmp (syms[i]->section->name, ".opd") != 0)
2233           break;
2234       opdsymend = i;
2235
2236       for (; i < symcount; ++i)
2237         if (((syms[i]->section->flags
2238               & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2239             != (SEC_CODE | SEC_ALLOC))
2240           break;
2241       symcount = i;
2242     }
2243   count = 0;
2244
2245   if (relocatable)
2246     {
2247       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2248       arelent *r;
2249       size_t size;
2250       size_t relcount;
2251
2252       if (opdsymend == secsymend)
2253         goto done;
2254
2255       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2256       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2257       if (relcount == 0)
2258         goto done;
2259
2260       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2261         {
2262           count = -1;
2263           goto done;
2264         }
2265
2266       size = 0;
2267       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2268         {
2269           asymbol *sym;
2270
2271           while (r < opd->relocation + relcount
2272                  && r->address < syms[i]->value + opd->vma)
2273             ++r;
2274
2275           if (r == opd->relocation + relcount)
2276             break;
2277
2278           if (r->address != syms[i]->value + opd->vma)
2279             continue;
2280
2281           if (r->howto->type != R_PPC64_ADDR64)
2282             continue;
2283
2284           sym = *r->sym_ptr_ptr;
2285           if (!sym_exists_at (syms, opdsymend, symcount,
2286                               sym->section->id, sym->value + r->addend))
2287             {
2288               ++count;
2289               size += sizeof (asymbol);
2290               size += strlen (syms[i]->name) + 2;
2291             }
2292         }
2293
2294       if (size == 0)
2295         goto done;
2296       s = *ret = bfd_malloc (size);
2297       if (s == NULL)
2298         {
2299           count = -1;
2300           goto done;
2301         }
2302
2303       names = (char *) (s + count);
2304
2305       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2306         {
2307           asymbol *sym;
2308
2309           while (r < opd->relocation + relcount
2310                  && r->address < syms[i]->value + opd->vma)
2311             ++r;
2312
2313           if (r == opd->relocation + relcount)
2314             break;
2315
2316           if (r->address != syms[i]->value + opd->vma)
2317             continue;
2318
2319           if (r->howto->type != R_PPC64_ADDR64)
2320             continue;
2321
2322           sym = *r->sym_ptr_ptr;
2323           if (!sym_exists_at (syms, opdsymend, symcount,
2324                               sym->section->id, sym->value + r->addend))
2325             {
2326               size_t len;
2327
2328               *s = *syms[i];
2329               s->flags |= BSF_SYNTHETIC;
2330               s->section = sym->section;
2331               s->value = sym->value + r->addend;
2332               s->name = names;
2333               *names++ = '.';
2334               len = strlen (syms[i]->name);
2335               memcpy (names, syms[i]->name, len + 1);
2336               names += len + 1;
2337               /* Have udata.p point back to the original symbol this
2338                  synthetic symbol was derived from.  */
2339               s->udata.p = syms[i];
2340               s++;
2341             }
2342         }
2343     }
2344   else
2345     {
2346       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2347       bfd_byte *contents = NULL;
2348       size_t size;
2349       size_t plt_count = 0;
2350       bfd_vma glink_vma = 0, resolv_vma = 0;
2351       asection *dynamic, *glink = NULL, *relplt = NULL;
2352       arelent *p;
2353
2354       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2355         {
2356         free_contents_and_exit_err:
2357           count = -1;
2358         free_contents_and_exit:
2359           if (contents)
2360             free (contents);
2361           goto done;
2362         }
2363
2364       size = 0;
2365       for (i = secsymend; i < opdsymend; ++i)
2366         {
2367           bfd_vma ent;
2368
2369           /* Ignore bogus symbols.  */
2370           if (syms[i]->value > opd->size - 8)
2371             continue;
2372
2373           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2374           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2375             {
2376               ++count;
2377               size += sizeof (asymbol);
2378               size += strlen (syms[i]->name) + 2;
2379             }
2380         }
2381
2382       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2383       if (dyn_count != 0
2384           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2385         {
2386           bfd_byte *dynbuf, *extdyn, *extdynend;
2387           size_t extdynsize;
2388           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2389
2390           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2391             goto free_contents_and_exit_err;
2392
2393           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2394           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2395
2396           extdyn = dynbuf;
2397           extdynend = extdyn + dynamic->size;
2398           for (; extdyn < extdynend; extdyn += extdynsize)
2399             {
2400               Elf_Internal_Dyn dyn;
2401               (*swap_dyn_in) (abfd, extdyn, &dyn);
2402
2403               if (dyn.d_tag == DT_NULL)
2404                 break;
2405
2406               if (dyn.d_tag == DT_PPC64_GLINK)
2407                 {
2408                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2409                      See comment in ppc64_elf_finish_dynamic_sections. */
2410                   glink_vma = dyn.d_un.d_val + 8 * 4;
2411                   /* The .glink section usually does not survive the final
2412                      link; search for the section (usually .text) where the
2413                      glink stubs now reside.  */
2414                   glink = bfd_sections_find_if (abfd, section_covers_vma,
2415                                                 &glink_vma);
2416                   break;
2417                 }
2418             }
2419
2420           free (dynbuf);
2421         }
2422
2423       if (glink != NULL)
2424         {
2425           /* Determine __glink trampoline by reading the relative branch
2426              from the first glink stub.  */
2427           bfd_byte buf[4];
2428           unsigned int off = 0;
2429
2430           while (bfd_get_section_contents (abfd, glink, buf,
2431                                            glink_vma + off - glink->vma, 4))
2432             {
2433               unsigned int insn = bfd_get_32 (abfd, buf);
2434               insn ^= B_DOT;
2435               if ((insn & ~0x3fffffc) == 0)
2436                 {
2437                   resolv_vma
2438                     = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2439                   break;
2440                 }
2441               off += 4;
2442               if (off > 4)
2443                 break;
2444             }
2445
2446           if (resolv_vma)
2447             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2448
2449           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2450           if (relplt != NULL)
2451             {
2452               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2453               if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2454                 goto free_contents_and_exit_err;
2455
2456               plt_count = relplt->size / sizeof (Elf64_External_Rela);
2457               size += plt_count * sizeof (asymbol);
2458
2459               p = relplt->relocation;
2460               for (i = 0; i < plt_count; i++, p++)
2461                 {
2462                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2463                   if (p->addend != 0)
2464                     size += sizeof ("+0x") - 1 + 16;
2465                 }
2466             }
2467         }
2468
2469       if (size == 0)
2470         goto free_contents_and_exit;
2471       s = *ret = bfd_malloc (size);
2472       if (s == NULL)
2473         goto free_contents_and_exit_err;
2474
2475       names = (char *) (s + count + plt_count + (resolv_vma != 0));
2476
2477       for (i = secsymend; i < opdsymend; ++i)
2478         {
2479           bfd_vma ent;
2480
2481           if (syms[i]->value > opd->size - 8)
2482             continue;
2483
2484           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2485           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2486             {
2487               size_t lo, hi;
2488               size_t len;
2489               asection *sec = abfd->sections;
2490
2491               *s = *syms[i];
2492               lo = codesecsym;
2493               hi = codesecsymend;
2494               while (lo < hi)
2495                 {
2496                   size_t mid = (lo + hi) >> 1;
2497                   if (syms[mid]->section->vma < ent)
2498                     lo = mid + 1;
2499                   else if (syms[mid]->section->vma > ent)
2500                     hi = mid;
2501                   else
2502                     {
2503                       sec = syms[mid]->section;
2504                       break;
2505                     }
2506                 }
2507
2508               if (lo >= hi && lo > codesecsym)
2509                 sec = syms[lo - 1]->section;
2510
2511               for (; sec != NULL; sec = sec->next)
2512                 {
2513                   if (sec->vma > ent)
2514                     break;
2515                   /* SEC_LOAD may not be set if SEC is from a separate debug
2516                      info file.  */
2517                   if ((sec->flags & SEC_ALLOC) == 0)
2518                     break;
2519                   if ((sec->flags & SEC_CODE) != 0)
2520                     s->section = sec;
2521                 }
2522               s->flags |= BSF_SYNTHETIC;
2523               s->value = ent - s->section->vma;
2524               s->name = names;
2525               *names++ = '.';
2526               len = strlen (syms[i]->name);
2527               memcpy (names, syms[i]->name, len + 1);
2528               names += len + 1;
2529               /* Have udata.p point back to the original symbol this
2530                  synthetic symbol was derived from.  */
2531               s->udata.p = syms[i];
2532               s++;
2533             }
2534         }
2535       free (contents);
2536
2537       if (glink != NULL && relplt != NULL)
2538         {
2539           if (resolv_vma)
2540             {
2541               /* Add a symbol for the main glink trampoline.  */
2542               memset (s, 0, sizeof *s);
2543               s->the_bfd = abfd;
2544               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2545               s->section = glink;
2546               s->value = resolv_vma - glink->vma;
2547               s->name = names;
2548               memcpy (names, "__glink_PLTresolve",
2549                       sizeof ("__glink_PLTresolve"));
2550               names += sizeof ("__glink_PLTresolve");
2551               s++;
2552               count++;
2553             }
2554
2555           /* FIXME: It would be very much nicer to put sym@plt on the
2556              stub rather than on the glink branch table entry.  The
2557              objdump disassembler would then use a sensible symbol
2558              name on plt calls.  The difficulty in doing so is
2559              a) finding the stubs, and,
2560              b) matching stubs against plt entries, and,
2561              c) there can be multiple stubs for a given plt entry.
2562
2563              Solving (a) could be done by code scanning, but older
2564              ppc64 binaries used different stubs to current code.
2565              (b) is the tricky one since you need to known the toc
2566              pointer for at least one function that uses a pic stub to
2567              be able to calculate the plt address referenced.
2568              (c) means gdb would need to set multiple breakpoints (or
2569              find the glink branch itself) when setting breakpoints
2570              for pending shared library loads.  */
2571           p = relplt->relocation;
2572           for (i = 0; i < plt_count; i++, p++)
2573             {
2574               size_t len;
2575
2576               *s = **p->sym_ptr_ptr;
2577               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2578                  we are defining a symbol, ensure one of them is set.  */
2579               if ((s->flags & BSF_LOCAL) == 0)
2580                 s->flags |= BSF_GLOBAL;
2581               s->flags |= BSF_SYNTHETIC;
2582               s->section = glink;
2583               s->value = glink_vma - glink->vma;
2584               s->name = names;
2585               s->udata.p = NULL;
2586               len = strlen ((*p->sym_ptr_ptr)->name);
2587               memcpy (names, (*p->sym_ptr_ptr)->name, len);
2588               names += len;
2589               if (p->addend != 0)
2590                 {
2591                   memcpy (names, "+0x", sizeof ("+0x") - 1);
2592                   names += sizeof ("+0x") - 1;
2593                   bfd_sprintf_vma (abfd, names, p->addend);
2594                   names += strlen (names);
2595                 }
2596               memcpy (names, "@plt", sizeof ("@plt"));
2597               names += sizeof ("@plt");
2598               s++;
2599               if (abi < 2)
2600                 {
2601                   glink_vma += 8;
2602                   if (i >= 0x8000)
2603                     glink_vma += 4;
2604                 }
2605               else
2606                 glink_vma += 4;
2607             }
2608           count += plt_count;
2609         }
2610     }
2611
2612  done:
2613   free (syms);
2614   return count;
2615 }
2616 \f
2617 /* The following functions are specific to the ELF linker, while
2618    functions above are used generally.  Those named ppc64_elf_* are
2619    called by the main ELF linker code.  They appear in this file more
2620    or less in the order in which they are called.  eg.
2621    ppc64_elf_check_relocs is called early in the link process,
2622    ppc64_elf_finish_dynamic_sections is one of the last functions
2623    called.
2624
2625    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2626    functions have both a function code symbol and a function descriptor
2627    symbol.  A call to foo in a relocatable object file looks like:
2628
2629    .            .text
2630    .    x:
2631    .            bl      .foo
2632    .            nop
2633
2634    The function definition in another object file might be:
2635
2636    .            .section .opd
2637    .    foo:    .quad   .foo
2638    .            .quad   .TOC.@tocbase
2639    .            .quad   0
2640    .
2641    .            .text
2642    .    .foo:   blr
2643
2644    When the linker resolves the call during a static link, the branch
2645    unsurprisingly just goes to .foo and the .opd information is unused.
2646    If the function definition is in a shared library, things are a little
2647    different:  The call goes via a plt call stub, the opd information gets
2648    copied to the plt, and the linker patches the nop.
2649
2650    .    x:
2651    .            bl      .foo_stub
2652    .            ld      2,40(1)
2653    .
2654    .
2655    .    .foo_stub:
2656    .            std     2,40(1)                 # in practice, the call stub
2657    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
2658    .            addi    11,11,Lfoo@toc@l        # this is the general idea
2659    .            ld      12,0(11)
2660    .            ld      2,8(11)
2661    .            mtctr   12
2662    .            ld      11,16(11)
2663    .            bctr
2664    .
2665    .            .section .plt
2666    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2667
2668    The "reloc ()" notation is supposed to indicate that the linker emits
2669    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2670    copying.
2671
2672    What are the difficulties here?  Well, firstly, the relocations
2673    examined by the linker in check_relocs are against the function code
2674    sym .foo, while the dynamic relocation in the plt is emitted against
2675    the function descriptor symbol, foo.  Somewhere along the line, we need
2676    to carefully copy dynamic link information from one symbol to the other.
2677    Secondly, the generic part of the elf linker will make .foo a dynamic
2678    symbol as is normal for most other backends.  We need foo dynamic
2679    instead, at least for an application final link.  However, when
2680    creating a shared library containing foo, we need to have both symbols
2681    dynamic so that references to .foo are satisfied during the early
2682    stages of linking.  Otherwise the linker might decide to pull in a
2683    definition from some other object, eg. a static library.
2684
2685    Update: As of August 2004, we support a new convention.  Function
2686    calls may use the function descriptor symbol, ie. "bl foo".  This
2687    behaves exactly as "bl .foo".  */
2688
2689 /* Of those relocs that might be copied as dynamic relocs, this
2690    function selects those that must be copied when linking a shared
2691    library or PIE, even when the symbol is local.  */
2692
2693 static int
2694 must_be_dyn_reloc (struct bfd_link_info *info,
2695                    enum elf_ppc64_reloc_type r_type)
2696 {
2697   switch (r_type)
2698     {
2699     default:
2700       /* Only relative relocs can be resolved when the object load
2701          address isn't fixed.  DTPREL64 is excluded because the
2702          dynamic linker needs to differentiate global dynamic from
2703          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2704       return 1;
2705
2706     case R_PPC64_REL32:
2707     case R_PPC64_REL64:
2708     case R_PPC64_REL30:
2709       return 0;
2710
2711     case R_PPC64_TPREL16:
2712     case R_PPC64_TPREL16_LO:
2713     case R_PPC64_TPREL16_HI:
2714     case R_PPC64_TPREL16_HA:
2715     case R_PPC64_TPREL16_DS:
2716     case R_PPC64_TPREL16_LO_DS:
2717     case R_PPC64_TPREL16_HIGH:
2718     case R_PPC64_TPREL16_HIGHA:
2719     case R_PPC64_TPREL16_HIGHER:
2720     case R_PPC64_TPREL16_HIGHERA:
2721     case R_PPC64_TPREL16_HIGHEST:
2722     case R_PPC64_TPREL16_HIGHESTA:
2723     case R_PPC64_TPREL64:
2724       /* These relocations are relative but in a shared library the
2725          linker doesn't know the thread pointer base.  */
2726       return bfd_link_dll (info);
2727     }
2728 }
2729
2730 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2731    copying dynamic variables from a shared lib into an app's dynbss
2732    section, and instead use a dynamic relocation to point into the
2733    shared lib.  With code that gcc generates, it's vital that this be
2734    enabled;  In the PowerPC64 ABI, the address of a function is actually
2735    the address of a function descriptor, which resides in the .opd
2736    section.  gcc uses the descriptor directly rather than going via the
2737    GOT as some other ABI's do, which means that initialized function
2738    pointers must reference the descriptor.  Thus, a function pointer
2739    initialized to the address of a function in a shared library will
2740    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
2741    redefines the function descriptor symbol to point to the copy.  This
2742    presents a problem as a plt entry for that function is also
2743    initialized from the function descriptor symbol and the copy reloc
2744    may not be initialized first.  */
2745 #define ELIMINATE_COPY_RELOCS 1
2746
2747 /* Section name for stubs is the associated section name plus this
2748    string.  */
2749 #define STUB_SUFFIX ".stub"
2750
2751 /* Linker stubs.
2752    ppc_stub_long_branch:
2753    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2754    destination, but a 24 bit branch in a stub section will reach.
2755    .    b       dest
2756
2757    ppc_stub_plt_branch:
2758    Similar to the above, but a 24 bit branch in the stub section won't
2759    reach its destination.
2760    .    addis   %r11,%r2,xxx@toc@ha
2761    .    ld      %r12,xxx@toc@l(%r11)
2762    .    mtctr   %r12
2763    .    bctr
2764
2765    ppc_stub_plt_call:
2766    Used to call a function in a shared library.  If it so happens that
2767    the plt entry referenced crosses a 64k boundary, then an extra
2768    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2769    ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2770    .    addis   %r11,%r2,xxx@toc@ha
2771    .    ld      %r12,xxx+0@toc@l(%r11)
2772    .    mtctr   %r12
2773    .    ld      %r2,xxx+8@toc@l(%r11)
2774    .    ld      %r11,xxx+16@toc@l(%r11)
2775    .    bctr
2776
2777    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2778    code to adjust the value and save r2 to support multiple toc sections.
2779    A ppc_stub_long_branch with an r2 offset looks like:
2780    .    std     %r2,40(%r1)
2781    .    addis   %r2,%r2,off@ha
2782    .    addi    %r2,%r2,off@l
2783    .    b       dest
2784
2785    A ppc_stub_plt_branch with an r2 offset looks like:
2786    .    std     %r2,40(%r1)
2787    .    addis   %r11,%r2,xxx@toc@ha
2788    .    ld      %r12,xxx@toc@l(%r11)
2789    .    addis   %r2,%r2,off@ha
2790    .    addi    %r2,%r2,off@l
2791    .    mtctr   %r12
2792    .    bctr
2793
2794    All of the above stubs are shown as their ELFv1 variants.  ELFv2
2795    variants exist too, simpler for plt calls since a new toc pointer
2796    and static chain are not loaded by the stub.  In addition, ELFv2
2797    has some more complex stubs to handle calls marked with NOTOC
2798    relocs from functions where r2 is not a valid toc pointer.  These
2799    come in two flavours, the ones shown below, and _both variants that
2800    start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2801    one call is from a function where r2 is used as the toc pointer but
2802    needs a toc adjusting stub for small-model multi-toc, and another
2803    call is from a function where r2 is not valid.
2804    ppc_stub_long_branch_notoc:
2805    .    mflr    %r12
2806    .    bcl     20,31,1f
2807    .  1:
2808    .    mflr    %r11
2809    .    mtlr    %r12
2810    .    addis   %r12,%r11,dest-1b@ha
2811    .    addi    %r12,%r12,dest-1b@l
2812    .    b       dest
2813
2814    ppc_stub_plt_branch_notoc:
2815    .    mflr    %r12
2816    .    bcl     20,31,1f
2817    .  1:
2818    .    mflr    %r11
2819    .    mtlr    %r12
2820    .    lis     %r12,xxx-1b@highest
2821    .    ori     %r12,%r12,xxx-1b@higher
2822    .    sldi    %r12,%r12,32
2823    .    oris    %r12,%r12,xxx-1b@high
2824    .    ori     %r12,%r12,xxx-1b@l
2825    .    add     %r12,%r11,%r12
2826    .    mtctr   %r12
2827    .    bctr
2828
2829    ppc_stub_plt_call_notoc:
2830    .    mflr    %r12
2831    .    bcl     20,31,1f
2832    .  1:
2833    .    mflr    %r11
2834    .    mtlr    %r12
2835    .    lis     %r12,xxx-1b@highest
2836    .    ori     %r12,%r12,xxx-1b@higher
2837    .    sldi    %r12,%r12,32
2838    .    oris    %r12,%r12,xxx-1b@high
2839    .    ori     %r12,%r12,xxx-1b@l
2840    .    ldx     %r12,%r11,%r12
2841    .    mtctr   %r12
2842    .    bctr
2843
2844    In cases where the high instructions would add zero, they are
2845    omitted and following instructions modified in some cases.
2846
2847    For a given stub group (a set of sections all using the same toc
2848    pointer value) there will be just one stub type used for any
2849    particular function symbol.  For example, if printf is called from
2850    code with the tocsave optimization (ie. r2 saved in function
2851    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2852    and from other code without the tocsave optimization requiring a
2853    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2854    type will be created.  Calls with the tocsave optimization will
2855    enter this stub after the instruction saving r2.  A similar
2856    situation exists when calls are marked with R_PPC64_REL24_NOTOC
2857    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2858    to call an external function like printf.  If other calls to printf
2859    require a ppc_stub_plt_call linkage stub then a single
2860    ppc_stub_plt_call_notoc linkage stub will be used for both types of
2861    call.  If other calls to printf require a ppc_stub_plt_call_r2save
2862    linkage stub then a single ppc_stub_plt_call_both linkage stub will
2863    be created and calls not requiring r2 to be saved will enter the
2864    stub after the r2 save instruction.  There is an analogous
2865    hierarchy of long branch and plt branch stubs for local call
2866    linkage.  */
2867
2868 enum ppc_stub_type
2869 {
2870   ppc_stub_none,
2871   ppc_stub_long_branch,
2872   ppc_stub_long_branch_r2off,
2873   ppc_stub_long_branch_notoc,
2874   ppc_stub_long_branch_both, /* r2off and notoc variants both needed.  */
2875   ppc_stub_plt_branch,
2876   ppc_stub_plt_branch_r2off,
2877   ppc_stub_plt_branch_notoc,
2878   ppc_stub_plt_branch_both,
2879   ppc_stub_plt_call,
2880   ppc_stub_plt_call_r2save,
2881   ppc_stub_plt_call_notoc,
2882   ppc_stub_plt_call_both,
2883   ppc_stub_global_entry,
2884   ppc_stub_save_res
2885 };
2886
2887 /* Information on stub grouping.  */
2888 struct map_stub
2889 {
2890   /* The stub section.  */
2891   asection *stub_sec;
2892   /* This is the section to which stubs in the group will be attached.  */
2893   asection *link_sec;
2894   /* Next group.  */
2895   struct map_stub *next;
2896   /* Whether to emit a copy of register save/restore functions in this
2897      group.  */
2898   int needs_save_res;
2899   /* Current offset within stubs after the insn restoring lr in a
2900      _notoc or _both stub using bcl for pc-relative addressing, or
2901      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
2902   unsigned int lr_restore;
2903   /* Accumulated size of EH info emitted to describe return address
2904      if stubs modify lr.  Does not include 17 byte FDE header.  */
2905   unsigned int eh_size;
2906   /* Offset in glink_eh_frame to the start of EH info for this group.  */
2907   unsigned int eh_base;
2908 };
2909
2910 struct ppc_stub_hash_entry
2911 {
2912   /* Base hash table entry structure.  */
2913   struct bfd_hash_entry root;
2914
2915   enum ppc_stub_type stub_type;
2916
2917   /* Group information.  */
2918   struct map_stub *group;
2919
2920   /* Offset within stub_sec of the beginning of this stub.  */
2921   bfd_vma stub_offset;
2922
2923   /* Given the symbol's value and its section we can determine its final
2924      value when building the stubs (so the stub knows where to jump.  */
2925   bfd_vma target_value;
2926   asection *target_section;
2927
2928   /* The symbol table entry, if any, that this was derived from.  */
2929   struct ppc_link_hash_entry *h;
2930   struct plt_entry *plt_ent;
2931
2932   /* Symbol type.  */
2933   unsigned char symtype;
2934
2935   /* Symbol st_other.  */
2936   unsigned char other;
2937 };
2938
2939 struct ppc_branch_hash_entry
2940 {
2941   /* Base hash table entry structure.  */
2942   struct bfd_hash_entry root;
2943
2944   /* Offset within branch lookup table.  */
2945   unsigned int offset;
2946
2947   /* Generation marker.  */
2948   unsigned int iter;
2949 };
2950
2951 /* Used to track dynamic relocations for local symbols.  */
2952 struct ppc_dyn_relocs
2953 {
2954   struct ppc_dyn_relocs *next;
2955
2956   /* The input section of the reloc.  */
2957   asection *sec;
2958
2959   /* Total number of relocs copied for the input section.  */
2960   unsigned int count : 31;
2961
2962   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
2963   unsigned int ifunc : 1;
2964 };
2965
2966 struct ppc_link_hash_entry
2967 {
2968   struct elf_link_hash_entry elf;
2969
2970   union
2971   {
2972     /* A pointer to the most recently used stub hash entry against this
2973        symbol.  */
2974     struct ppc_stub_hash_entry *stub_cache;
2975
2976     /* A pointer to the next symbol starting with a '.'  */
2977     struct ppc_link_hash_entry *next_dot_sym;
2978   } u;
2979
2980   /* Track dynamic relocs copied for this symbol.  */
2981   struct elf_dyn_relocs *dyn_relocs;
2982
2983   /* Link between function code and descriptor symbols.  */
2984   struct ppc_link_hash_entry *oh;
2985
2986   /* Flag function code and descriptor symbols.  */
2987   unsigned int is_func:1;
2988   unsigned int is_func_descriptor:1;
2989   unsigned int fake:1;
2990
2991   /* Whether global opd/toc sym has been adjusted or not.
2992      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2993      should be set for all globals defined in any opd/toc section.  */
2994   unsigned int adjust_done:1;
2995
2996   /* Set if this is an out-of-line register save/restore function,
2997      with non-standard calling convention.  */
2998   unsigned int save_res:1;
2999
3000   /* Set if a duplicate symbol with non-zero localentry is detected,
3001      even when the duplicate symbol does not provide a definition.  */
3002   unsigned int non_zero_localentry:1;
3003
3004   /* Contexts in which symbol is used in the GOT (or TOC).
3005      Bits are or'd into the mask as the corresponding relocs are
3006      encountered during check_relocs, with TLS_TLS being set when any
3007      of the other TLS bits are set.  tls_optimize clears bits when
3008      optimizing to indicate the corresponding GOT entry type is not
3009      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
3010      set TLS_TPRELGD when a GD reloc turns into a TPREL one.  We use a
3011      separate flag rather than setting TPREL just for convenience in
3012      distinguishing the two cases.
3013      These flags are also kept for local symbols.  */
3014 #define TLS_TLS          1      /* Any TLS reloc.  */
3015 #define TLS_GD           2      /* GD reloc. */
3016 #define TLS_LD           4      /* LD reloc. */
3017 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
3018 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
3019 #define TLS_MARK        32      /* __tls_get_addr call marked. */
3020 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
3021 #define TLS_EXPLICIT   128      /* Marks TOC section TLS relocs. */
3022   unsigned char tls_mask;
3023
3024   /* The above field is also used to mark function symbols.  In which
3025      case TLS_TLS will be 0.  */
3026 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
3027 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
3028 #define NON_GOT        256      /* local symbol plt, not stored.  */
3029 };
3030
3031 /* ppc64 ELF linker hash table.  */
3032
3033 struct ppc_link_hash_table
3034 {
3035   struct elf_link_hash_table elf;
3036
3037   /* The stub hash table.  */
3038   struct bfd_hash_table stub_hash_table;
3039
3040   /* Another hash table for plt_branch stubs.  */
3041   struct bfd_hash_table branch_hash_table;
3042
3043   /* Hash table for function prologue tocsave.  */
3044   htab_t tocsave_htab;
3045
3046   /* Various options and other info passed from the linker.  */
3047   struct ppc64_elf_params *params;
3048
3049   /* The size of sec_info below.  */
3050   unsigned int sec_info_arr_size;
3051
3052   /* Per-section array of extra section info.  Done this way rather
3053      than as part of ppc64_elf_section_data so we have the info for
3054      non-ppc64 sections.  */
3055   struct
3056   {
3057     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3058     bfd_vma toc_off;
3059
3060     union
3061     {
3062       /* The section group that this section belongs to.  */
3063       struct map_stub *group;
3064       /* A temp section list pointer.  */
3065       asection *list;
3066     } u;
3067   } *sec_info;
3068
3069   /* Linked list of groups.  */
3070   struct map_stub *group;
3071
3072   /* Temp used when calculating TOC pointers.  */
3073   bfd_vma toc_curr;
3074   bfd *toc_bfd;
3075   asection *toc_first_sec;
3076
3077   /* Used when adding symbols.  */
3078   struct ppc_link_hash_entry *dot_syms;
3079
3080   /* Shortcuts to get to dynamic linker sections.  */
3081   asection *glink;
3082   asection *global_entry;
3083   asection *sfpr;
3084   asection *pltlocal;
3085   asection *relpltlocal;
3086   asection *brlt;
3087   asection *relbrlt;
3088   asection *glink_eh_frame;
3089
3090   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3091   struct ppc_link_hash_entry *tls_get_addr;
3092   struct ppc_link_hash_entry *tls_get_addr_fd;
3093
3094   /* The size of reliplt used by got entry relocs.  */
3095   bfd_size_type got_reli_size;
3096
3097   /* Statistics.  */
3098   unsigned long stub_count[ppc_stub_global_entry];
3099
3100   /* Number of stubs against global syms.  */
3101   unsigned long stub_globals;
3102
3103   /* Set if we're linking code with function descriptors.  */
3104   unsigned int opd_abi:1;
3105
3106   /* Support for multiple toc sections.  */
3107   unsigned int do_multi_toc:1;
3108   unsigned int multi_toc_needed:1;
3109   unsigned int second_toc_pass:1;
3110   unsigned int do_toc_opt:1;
3111
3112   /* Set if tls optimization is enabled.  */
3113   unsigned int do_tls_opt:1;
3114
3115   /* Set if inline plt calls should be converted to direct calls.  */
3116   unsigned int can_convert_all_inline_plt:1;
3117
3118   /* Set on error.  */
3119   unsigned int stub_error:1;
3120
3121   /* Whether func_desc_adjust needs to be run over symbols.  */
3122   unsigned int need_func_desc_adj:1;
3123
3124   /* Whether there exist local gnu indirect function resolvers,
3125      referenced by dynamic relocations.  */
3126   unsigned int local_ifunc_resolver:1;
3127   unsigned int maybe_local_ifunc_resolver:1;
3128
3129   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
3130   unsigned int has_plt_localentry0:1;
3131
3132   /* Whether calls are made via the PLT from NOTOC functions.  */
3133   unsigned int notoc_plt:1;
3134
3135   /* Incremented every time we size stubs.  */
3136   unsigned int stub_iteration;
3137
3138   /* Small local sym cache.  */
3139   struct sym_cache sym_cache;
3140 };
3141
3142 /* Rename some of the generic section flags to better document how they
3143    are used here.  */
3144
3145 /* Nonzero if this section has TLS related relocations.  */
3146 #define has_tls_reloc sec_flg0
3147
3148 /* Nonzero if this section has an old-style call to __tls_get_addr.  */
3149 #define has_tls_get_addr_call sec_flg1
3150
3151 /* Nonzero if this section has any toc or got relocs.  */
3152 #define has_toc_reloc sec_flg2
3153
3154 /* Nonzero if this section has a call to another section that uses
3155    the toc or got.  */
3156 #define makes_toc_func_call sec_flg3
3157
3158 /* Recursion protection when determining above flag.  */
3159 #define call_check_in_progress sec_flg4
3160 #define call_check_done sec_flg5
3161
3162 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3163
3164 #define ppc_hash_table(p) \
3165   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3166   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3167
3168 #define ppc_stub_hash_lookup(table, string, create, copy) \
3169   ((struct ppc_stub_hash_entry *) \
3170    bfd_hash_lookup ((table), (string), (create), (copy)))
3171
3172 #define ppc_branch_hash_lookup(table, string, create, copy) \
3173   ((struct ppc_branch_hash_entry *) \
3174    bfd_hash_lookup ((table), (string), (create), (copy)))
3175
3176 /* Create an entry in the stub hash table.  */
3177
3178 static struct bfd_hash_entry *
3179 stub_hash_newfunc (struct bfd_hash_entry *entry,
3180                    struct bfd_hash_table *table,
3181                    const char *string)
3182 {
3183   /* Allocate the structure if it has not already been allocated by a
3184      subclass.  */
3185   if (entry == NULL)
3186     {
3187       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3188       if (entry == NULL)
3189         return entry;
3190     }
3191
3192   /* Call the allocation method of the superclass.  */
3193   entry = bfd_hash_newfunc (entry, table, string);
3194   if (entry != NULL)
3195     {
3196       struct ppc_stub_hash_entry *eh;
3197
3198       /* Initialize the local fields.  */
3199       eh = (struct ppc_stub_hash_entry *) entry;
3200       eh->stub_type = ppc_stub_none;
3201       eh->group = NULL;
3202       eh->stub_offset = 0;
3203       eh->target_value = 0;
3204       eh->target_section = NULL;
3205       eh->h = NULL;
3206       eh->plt_ent = NULL;
3207       eh->other = 0;
3208     }
3209
3210   return entry;
3211 }
3212
3213 /* Create an entry in the branch hash table.  */
3214
3215 static struct bfd_hash_entry *
3216 branch_hash_newfunc (struct bfd_hash_entry *entry,
3217                      struct bfd_hash_table *table,
3218                      const char *string)
3219 {
3220   /* Allocate the structure if it has not already been allocated by a
3221      subclass.  */
3222   if (entry == NULL)
3223     {
3224       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3225       if (entry == NULL)
3226         return entry;
3227     }
3228
3229   /* Call the allocation method of the superclass.  */
3230   entry = bfd_hash_newfunc (entry, table, string);
3231   if (entry != NULL)
3232     {
3233       struct ppc_branch_hash_entry *eh;
3234
3235       /* Initialize the local fields.  */
3236       eh = (struct ppc_branch_hash_entry *) entry;
3237       eh->offset = 0;
3238       eh->iter = 0;
3239     }
3240
3241   return entry;
3242 }
3243
3244 /* Create an entry in a ppc64 ELF linker hash table.  */
3245
3246 static struct bfd_hash_entry *
3247 link_hash_newfunc (struct bfd_hash_entry *entry,
3248                    struct bfd_hash_table *table,
3249                    const char *string)
3250 {
3251   /* Allocate the structure if it has not already been allocated by a
3252      subclass.  */
3253   if (entry == NULL)
3254     {
3255       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3256       if (entry == NULL)
3257         return entry;
3258     }
3259
3260   /* Call the allocation method of the superclass.  */
3261   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3262   if (entry != NULL)
3263     {
3264       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3265
3266       memset (&eh->u.stub_cache, 0,
3267               (sizeof (struct ppc_link_hash_entry)
3268                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3269
3270       /* When making function calls, old ABI code references function entry
3271          points (dot symbols), while new ABI code references the function
3272          descriptor symbol.  We need to make any combination of reference and
3273          definition work together, without breaking archive linking.
3274
3275          For a defined function "foo" and an undefined call to "bar":
3276          An old object defines "foo" and ".foo", references ".bar" (possibly
3277          "bar" too).
3278          A new object defines "foo" and references "bar".
3279
3280          A new object thus has no problem with its undefined symbols being
3281          satisfied by definitions in an old object.  On the other hand, the
3282          old object won't have ".bar" satisfied by a new object.
3283
3284          Keep a list of newly added dot-symbols.  */
3285
3286       if (string[0] == '.')
3287         {
3288           struct ppc_link_hash_table *htab;
3289
3290           htab = (struct ppc_link_hash_table *) table;
3291           eh->u.next_dot_sym = htab->dot_syms;
3292           htab->dot_syms = eh;
3293         }
3294     }
3295
3296   return entry;
3297 }
3298
3299 struct tocsave_entry
3300 {
3301   asection *sec;
3302   bfd_vma offset;
3303 };
3304
3305 static hashval_t
3306 tocsave_htab_hash (const void *p)
3307 {
3308   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3309   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3310 }
3311
3312 static int
3313 tocsave_htab_eq (const void *p1, const void *p2)
3314 {
3315   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3316   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3317   return e1->sec == e2->sec && e1->offset == e2->offset;
3318 }
3319
3320 /* Destroy a ppc64 ELF linker hash table.  */
3321
3322 static void
3323 ppc64_elf_link_hash_table_free (bfd *obfd)
3324 {
3325   struct ppc_link_hash_table *htab;
3326
3327   htab = (struct ppc_link_hash_table *) obfd->link.hash;
3328   if (htab->tocsave_htab)
3329     htab_delete (htab->tocsave_htab);
3330   bfd_hash_table_free (&htab->branch_hash_table);
3331   bfd_hash_table_free (&htab->stub_hash_table);
3332   _bfd_elf_link_hash_table_free (obfd);
3333 }
3334
3335 /* Create a ppc64 ELF linker hash table.  */
3336
3337 static struct bfd_link_hash_table *
3338 ppc64_elf_link_hash_table_create (bfd *abfd)
3339 {
3340   struct ppc_link_hash_table *htab;
3341   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3342
3343   htab = bfd_zmalloc (amt);
3344   if (htab == NULL)
3345     return NULL;
3346
3347   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3348                                       sizeof (struct ppc_link_hash_entry),
3349                                       PPC64_ELF_DATA))
3350     {
3351       free (htab);
3352       return NULL;
3353     }
3354
3355   /* Init the stub hash table too.  */
3356   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3357                             sizeof (struct ppc_stub_hash_entry)))
3358     {
3359       _bfd_elf_link_hash_table_free (abfd);
3360       return NULL;
3361     }
3362
3363   /* And the branch hash table.  */
3364   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3365                             sizeof (struct ppc_branch_hash_entry)))
3366     {
3367       bfd_hash_table_free (&htab->stub_hash_table);
3368       _bfd_elf_link_hash_table_free (abfd);
3369       return NULL;
3370     }
3371
3372   htab->tocsave_htab = htab_try_create (1024,
3373                                         tocsave_htab_hash,
3374                                         tocsave_htab_eq,
3375                                         NULL);
3376   if (htab->tocsave_htab == NULL)
3377     {
3378       ppc64_elf_link_hash_table_free (abfd);
3379       return NULL;
3380     }
3381   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3382
3383   /* Initializing two fields of the union is just cosmetic.  We really
3384      only care about glist, but when compiled on a 32-bit host the
3385      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3386      debugger inspection of these fields look nicer.  */
3387   htab->elf.init_got_refcount.refcount = 0;
3388   htab->elf.init_got_refcount.glist = NULL;
3389   htab->elf.init_plt_refcount.refcount = 0;
3390   htab->elf.init_plt_refcount.glist = NULL;
3391   htab->elf.init_got_offset.offset = 0;
3392   htab->elf.init_got_offset.glist = NULL;
3393   htab->elf.init_plt_offset.offset = 0;
3394   htab->elf.init_plt_offset.glist = NULL;
3395
3396   return &htab->elf.root;
3397 }
3398
3399 /* Create sections for linker generated code.  */
3400
3401 static bfd_boolean
3402 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3403 {
3404   struct ppc_link_hash_table *htab;
3405   flagword flags;
3406
3407   htab = ppc_hash_table (info);
3408
3409   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3410            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3411   if (htab->params->save_restore_funcs)
3412     {
3413       /* Create .sfpr for code to save and restore fp regs.  */
3414       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3415                                                        flags);
3416       if (htab->sfpr == NULL
3417           || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3418         return FALSE;
3419     }
3420
3421   if (bfd_link_relocatable (info))
3422     return TRUE;
3423
3424   /* Create .glink for lazy dynamic linking support.  */
3425   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3426                                                     flags);
3427   if (htab->glink == NULL
3428       || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3429     return FALSE;
3430
3431   /* The part of .glink used by global entry stubs, separate so that
3432      it can be aligned appropriately without affecting htab->glink.  */
3433   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3434                                                            flags);
3435   if (htab->global_entry == NULL
3436       || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3437     return FALSE;
3438
3439   if (!info->no_ld_generated_unwind_info)
3440     {
3441       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3442                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3443       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3444                                                                  ".eh_frame",
3445                                                                  flags);
3446       if (htab->glink_eh_frame == NULL
3447           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3448         return FALSE;
3449     }
3450
3451   flags = SEC_ALLOC | SEC_LINKER_CREATED;
3452   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3453   if (htab->elf.iplt == NULL
3454       || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3455     return FALSE;
3456
3457   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3458            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3459   htab->elf.irelplt
3460     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3461   if (htab->elf.irelplt == NULL
3462       || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3463     return FALSE;
3464
3465   /* Create branch lookup table for plt_branch stubs.  */
3466   flags = (SEC_ALLOC | SEC_LOAD
3467            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3468   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3469                                                    flags);
3470   if (htab->brlt == NULL
3471       || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3472     return FALSE;
3473
3474   /* Local plt entries, put in .branch_lt but a separate section for
3475      convenience.  */
3476   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3477                                                        flags);
3478   if (htab->pltlocal == NULL
3479       || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3480     return FALSE;
3481
3482   if (!bfd_link_pic (info))
3483     return TRUE;
3484
3485   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3486            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3487   htab->relbrlt
3488     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3489   if (htab->relbrlt == NULL
3490       || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3491     return FALSE;
3492
3493   htab->relpltlocal
3494     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3495   if (htab->relpltlocal == NULL
3496       || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3497     return FALSE;
3498
3499   return TRUE;
3500 }
3501
3502 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3503
3504 bfd_boolean
3505 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3506                          struct ppc64_elf_params *params)
3507 {
3508   struct ppc_link_hash_table *htab;
3509
3510   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3511
3512 /* Always hook our dynamic sections into the first bfd, which is the
3513    linker created stub bfd.  This ensures that the GOT header is at
3514    the start of the output TOC section.  */
3515   htab = ppc_hash_table (info);
3516   htab->elf.dynobj = params->stub_bfd;
3517   htab->params = params;
3518
3519   return create_linkage_sections (htab->elf.dynobj, info);
3520 }
3521
3522 /* Build a name for an entry in the stub hash table.  */
3523
3524 static char *
3525 ppc_stub_name (const asection *input_section,
3526                const asection *sym_sec,
3527                const struct ppc_link_hash_entry *h,
3528                const Elf_Internal_Rela *rel)
3529 {
3530   char *stub_name;
3531   ssize_t len;
3532
3533   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3534      offsets from a sym as a branch target?  In fact, we could
3535      probably assume the addend is always zero.  */
3536   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3537
3538   if (h)
3539     {
3540       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3541       stub_name = bfd_malloc (len);
3542       if (stub_name == NULL)
3543         return stub_name;
3544
3545       len = sprintf (stub_name, "%08x.%s+%x",
3546                      input_section->id & 0xffffffff,
3547                      h->elf.root.root.string,
3548                      (int) rel->r_addend & 0xffffffff);
3549     }
3550   else
3551     {
3552       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3553       stub_name = bfd_malloc (len);
3554       if (stub_name == NULL)
3555         return stub_name;
3556
3557       len = sprintf (stub_name, "%08x.%x:%x+%x",
3558                      input_section->id & 0xffffffff,
3559                      sym_sec->id & 0xffffffff,
3560                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3561                      (int) rel->r_addend & 0xffffffff);
3562     }
3563   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3564     stub_name[len - 2] = 0;
3565   return stub_name;
3566 }
3567
3568 /* Look up an entry in the stub hash.  Stub entries are cached because
3569    creating the stub name takes a bit of time.  */
3570
3571 static struct ppc_stub_hash_entry *
3572 ppc_get_stub_entry (const asection *input_section,
3573                     const asection *sym_sec,
3574                     struct ppc_link_hash_entry *h,
3575                     const Elf_Internal_Rela *rel,
3576                     struct ppc_link_hash_table *htab)
3577 {
3578   struct ppc_stub_hash_entry *stub_entry;
3579   struct map_stub *group;
3580
3581   /* If this input section is part of a group of sections sharing one
3582      stub section, then use the id of the first section in the group.
3583      Stub names need to include a section id, as there may well be
3584      more than one stub used to reach say, printf, and we need to
3585      distinguish between them.  */
3586   group = htab->sec_info[input_section->id].u.group;
3587   if (group == NULL)
3588     return NULL;
3589
3590   if (h != NULL && h->u.stub_cache != NULL
3591       && h->u.stub_cache->h == h
3592       && h->u.stub_cache->group == group)
3593     {
3594       stub_entry = h->u.stub_cache;
3595     }
3596   else
3597     {
3598       char *stub_name;
3599
3600       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3601       if (stub_name == NULL)
3602         return NULL;
3603
3604       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3605                                          stub_name, FALSE, FALSE);
3606       if (h != NULL)
3607         h->u.stub_cache = stub_entry;
3608
3609       free (stub_name);
3610     }
3611
3612   return stub_entry;
3613 }
3614
3615 /* Add a new stub entry to the stub hash.  Not all fields of the new
3616    stub entry are initialised.  */
3617
3618 static struct ppc_stub_hash_entry *
3619 ppc_add_stub (const char *stub_name,
3620               asection *section,
3621               struct bfd_link_info *info)
3622 {
3623   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3624   struct map_stub *group;
3625   asection *link_sec;
3626   asection *stub_sec;
3627   struct ppc_stub_hash_entry *stub_entry;
3628
3629   group = htab->sec_info[section->id].u.group;
3630   link_sec = group->link_sec;
3631   stub_sec = group->stub_sec;
3632   if (stub_sec == NULL)
3633     {
3634       size_t namelen;
3635       bfd_size_type len;
3636       char *s_name;
3637
3638       namelen = strlen (link_sec->name);
3639       len = namelen + sizeof (STUB_SUFFIX);
3640       s_name = bfd_alloc (htab->params->stub_bfd, len);
3641       if (s_name == NULL)
3642         return NULL;
3643
3644       memcpy (s_name, link_sec->name, namelen);
3645       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3646       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3647       if (stub_sec == NULL)
3648         return NULL;
3649       group->stub_sec = stub_sec;
3650     }
3651
3652   /* Enter this entry into the linker stub hash table.  */
3653   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3654                                      TRUE, FALSE);
3655   if (stub_entry == NULL)
3656     {
3657       /* xgettext:c-format */
3658       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3659                           section->owner, stub_name);
3660       return NULL;
3661     }
3662
3663   stub_entry->group = group;
3664   stub_entry->stub_offset = 0;
3665   return stub_entry;
3666 }
3667
3668 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3669    not already done.  */
3670
3671 static bfd_boolean
3672 create_got_section (bfd *abfd, struct bfd_link_info *info)
3673 {
3674   asection *got, *relgot;
3675   flagword flags;
3676   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3677
3678   if (!is_ppc64_elf (abfd))
3679     return FALSE;
3680   if (htab == NULL)
3681     return FALSE;
3682
3683   if (!htab->elf.sgot
3684       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3685     return FALSE;
3686
3687   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3688            | SEC_LINKER_CREATED);
3689
3690   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3691   if (!got
3692       || !bfd_set_section_alignment (abfd, got, 3))
3693     return FALSE;
3694
3695   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3696                                                flags | SEC_READONLY);
3697   if (!relgot
3698       || !bfd_set_section_alignment (abfd, relgot, 3))
3699     return FALSE;
3700
3701   ppc64_elf_tdata (abfd)->got = got;
3702   ppc64_elf_tdata (abfd)->relgot = relgot;
3703   return TRUE;
3704 }
3705
3706 /* Follow indirect and warning symbol links.  */
3707
3708 static inline struct bfd_link_hash_entry *
3709 follow_link (struct bfd_link_hash_entry *h)
3710 {
3711   while (h->type == bfd_link_hash_indirect
3712          || h->type == bfd_link_hash_warning)
3713     h = h->u.i.link;
3714   return h;
3715 }
3716
3717 static inline struct elf_link_hash_entry *
3718 elf_follow_link (struct elf_link_hash_entry *h)
3719 {
3720   return (struct elf_link_hash_entry *) follow_link (&h->root);
3721 }
3722
3723 static inline struct ppc_link_hash_entry *
3724 ppc_follow_link (struct ppc_link_hash_entry *h)
3725 {
3726   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3727 }
3728
3729 /* Merge PLT info on FROM with that on TO.  */
3730
3731 static void
3732 move_plt_plist (struct ppc_link_hash_entry *from,
3733                 struct ppc_link_hash_entry *to)
3734 {
3735   if (from->elf.plt.plist != NULL)
3736     {
3737       if (to->elf.plt.plist != NULL)
3738         {
3739           struct plt_entry **entp;
3740           struct plt_entry *ent;
3741
3742           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3743             {
3744               struct plt_entry *dent;
3745
3746               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3747                 if (dent->addend == ent->addend)
3748                   {
3749                     dent->plt.refcount += ent->plt.refcount;
3750                     *entp = ent->next;
3751                     break;
3752                   }
3753               if (dent == NULL)
3754                 entp = &ent->next;
3755             }
3756           *entp = to->elf.plt.plist;
3757         }
3758
3759       to->elf.plt.plist = from->elf.plt.plist;
3760       from->elf.plt.plist = NULL;
3761     }
3762 }
3763
3764 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3765
3766 static void
3767 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3768                                 struct elf_link_hash_entry *dir,
3769                                 struct elf_link_hash_entry *ind)
3770 {
3771   struct ppc_link_hash_entry *edir, *eind;
3772
3773   edir = (struct ppc_link_hash_entry *) dir;
3774   eind = (struct ppc_link_hash_entry *) ind;
3775
3776   edir->is_func |= eind->is_func;
3777   edir->is_func_descriptor |= eind->is_func_descriptor;
3778   edir->tls_mask |= eind->tls_mask;
3779   if (eind->oh != NULL)
3780     edir->oh = ppc_follow_link (eind->oh);
3781
3782   if (edir->elf.versioned != versioned_hidden)
3783     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3784   edir->elf.ref_regular |= eind->elf.ref_regular;
3785   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3786   edir->elf.non_got_ref |= eind->elf.non_got_ref;
3787   edir->elf.needs_plt |= eind->elf.needs_plt;
3788   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3789
3790   /* If we were called to copy over info for a weak sym, don't copy
3791      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
3792      in order to simplify readonly_dynrelocs and save a field in the
3793      symbol hash entry, but that means dyn_relocs can't be used in any
3794      tests about a specific symbol, or affect other symbol flags which
3795      are then tested.  */
3796   if (eind->elf.root.type != bfd_link_hash_indirect)
3797     return;
3798
3799   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3800   if (eind->dyn_relocs != NULL)
3801     {
3802       if (edir->dyn_relocs != NULL)
3803         {
3804           struct elf_dyn_relocs **pp;
3805           struct elf_dyn_relocs *p;
3806
3807           /* Add reloc counts against the indirect sym to the direct sym
3808              list.  Merge any entries against the same section.  */
3809           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3810             {
3811               struct elf_dyn_relocs *q;
3812
3813               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3814                 if (q->sec == p->sec)
3815                   {
3816                     q->pc_count += p->pc_count;
3817                     q->count += p->count;
3818                     *pp = p->next;
3819                     break;
3820                   }
3821               if (q == NULL)
3822                 pp = &p->next;
3823             }
3824           *pp = edir->dyn_relocs;
3825         }
3826
3827       edir->dyn_relocs = eind->dyn_relocs;
3828       eind->dyn_relocs = NULL;
3829     }
3830
3831   /* Copy over got entries that we may have already seen to the
3832      symbol which just became indirect.  */
3833   if (eind->elf.got.glist != NULL)
3834     {
3835       if (edir->elf.got.glist != NULL)
3836         {
3837           struct got_entry **entp;
3838           struct got_entry *ent;
3839
3840           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3841             {
3842               struct got_entry *dent;
3843
3844               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3845                 if (dent->addend == ent->addend
3846                     && dent->owner == ent->owner
3847                     && dent->tls_type == ent->tls_type)
3848                   {
3849                     dent->got.refcount += ent->got.refcount;
3850                     *entp = ent->next;
3851                     break;
3852                   }
3853               if (dent == NULL)
3854                 entp = &ent->next;
3855             }
3856           *entp = edir->elf.got.glist;
3857         }
3858
3859       edir->elf.got.glist = eind->elf.got.glist;
3860       eind->elf.got.glist = NULL;
3861     }
3862
3863   /* And plt entries.  */
3864   move_plt_plist (eind, edir);
3865
3866   if (eind->elf.dynindx != -1)
3867     {
3868       if (edir->elf.dynindx != -1)
3869         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3870                                 edir->elf.dynstr_index);
3871       edir->elf.dynindx = eind->elf.dynindx;
3872       edir->elf.dynstr_index = eind->elf.dynstr_index;
3873       eind->elf.dynindx = -1;
3874       eind->elf.dynstr_index = 0;
3875     }
3876 }
3877
3878 /* Find the function descriptor hash entry from the given function code
3879    hash entry FH.  Link the entries via their OH fields.  */
3880
3881 static struct ppc_link_hash_entry *
3882 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3883 {
3884   struct ppc_link_hash_entry *fdh = fh->oh;
3885
3886   if (fdh == NULL)
3887     {
3888       const char *fd_name = fh->elf.root.root.string + 1;
3889
3890       fdh = (struct ppc_link_hash_entry *)
3891         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3892       if (fdh == NULL)
3893         return fdh;
3894
3895       fdh->is_func_descriptor = 1;
3896       fdh->oh = fh;
3897       fh->is_func = 1;
3898       fh->oh = fdh;
3899     }
3900
3901   fdh = ppc_follow_link (fdh);
3902   fdh->is_func_descriptor = 1;
3903   fdh->oh = fh;
3904   return fdh;
3905 }
3906
3907 /* Make a fake function descriptor sym for the undefined code sym FH.  */
3908
3909 static struct ppc_link_hash_entry *
3910 make_fdh (struct bfd_link_info *info,
3911           struct ppc_link_hash_entry *fh)
3912 {
3913   bfd *abfd = fh->elf.root.u.undef.abfd;
3914   struct bfd_link_hash_entry *bh = NULL;
3915   struct ppc_link_hash_entry *fdh;
3916   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3917                     ? BSF_WEAK
3918                     : BSF_GLOBAL);
3919
3920   if (!_bfd_generic_link_add_one_symbol (info, abfd,
3921                                          fh->elf.root.root.string + 1,
3922                                          flags, bfd_und_section_ptr, 0,
3923                                          NULL, FALSE, FALSE, &bh))
3924     return NULL;
3925
3926   fdh = (struct ppc_link_hash_entry *) bh;
3927   fdh->elf.non_elf = 0;
3928   fdh->fake = 1;
3929   fdh->is_func_descriptor = 1;
3930   fdh->oh = fh;
3931   fh->is_func = 1;
3932   fh->oh = fdh;
3933   return fdh;
3934 }
3935
3936 /* Fix function descriptor symbols defined in .opd sections to be
3937    function type.  */
3938
3939 static bfd_boolean
3940 ppc64_elf_add_symbol_hook (bfd *ibfd,
3941                            struct bfd_link_info *info,
3942                            Elf_Internal_Sym *isym,
3943                            const char **name,
3944                            flagword *flags ATTRIBUTE_UNUSED,
3945                            asection **sec,
3946                            bfd_vma *value)
3947 {
3948   if (*sec != NULL
3949       && strcmp ((*sec)->name, ".opd") == 0)
3950     {
3951       asection *code_sec;
3952
3953       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3954             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3955         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3956
3957       /* If the symbol is a function defined in .opd, and the function
3958          code is in a discarded group, let it appear to be undefined.  */
3959       if (!bfd_link_relocatable (info)
3960           && (*sec)->reloc_count != 0
3961           && opd_entry_value (*sec, *value, &code_sec, NULL,
3962                               FALSE) != (bfd_vma) -1
3963           && discarded_section (code_sec))
3964         {
3965           *sec = bfd_und_section_ptr;
3966           isym->st_shndx = SHN_UNDEF;
3967         }
3968     }
3969   else if (*sec != NULL
3970            && strcmp ((*sec)->name, ".toc") == 0
3971            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3972     {
3973       struct ppc_link_hash_table *htab = ppc_hash_table (info);
3974       if (htab != NULL)
3975         htab->params->object_in_toc = 1;
3976     }
3977
3978   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3979     {
3980       if (abiversion (ibfd) == 0)
3981         set_abiversion (ibfd, 2);
3982       else if (abiversion (ibfd) == 1)
3983         {
3984           _bfd_error_handler (_("symbol '%s' has invalid st_other"
3985                                 " for ABI version 1"), *name);
3986           bfd_set_error (bfd_error_bad_value);
3987           return FALSE;
3988         }
3989     }
3990
3991   return TRUE;
3992 }
3993
3994 /* Merge non-visibility st_other attributes: local entry point.  */
3995
3996 static void
3997 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3998                                   const Elf_Internal_Sym *isym,
3999                                   bfd_boolean definition,
4000                                   bfd_boolean dynamic)
4001 {
4002   if (definition && (!dynamic || !h->def_regular))
4003     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4004                 | ELF_ST_VISIBILITY (h->other));
4005 }
4006
4007 /* Hook called on merging a symbol.  We use this to clear "fake" since
4008    we now have a real symbol.  */
4009
4010 static bfd_boolean
4011 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4012                         const Elf_Internal_Sym *isym,
4013                         asection **psec ATTRIBUTE_UNUSED,
4014                         bfd_boolean newdef ATTRIBUTE_UNUSED,
4015                         bfd_boolean olddef ATTRIBUTE_UNUSED,
4016                         bfd *oldbfd ATTRIBUTE_UNUSED,
4017                         const asection *oldsec ATTRIBUTE_UNUSED)
4018 {
4019   ((struct ppc_link_hash_entry *) h)->fake = 0;
4020   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4021     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4022   return TRUE;
4023 }
4024
4025 /* This function makes an old ABI object reference to ".bar" cause the
4026    inclusion of a new ABI object archive that defines "bar".
4027    NAME is a symbol defined in an archive.  Return a symbol in the hash
4028    table that might be satisfied by the archive symbols.  */
4029
4030 static struct elf_link_hash_entry *
4031 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4032                                  struct bfd_link_info *info,
4033                                  const char *name)
4034 {
4035   struct elf_link_hash_entry *h;
4036   char *dot_name;
4037   size_t len;
4038
4039   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4040   if (h != NULL
4041       /* Don't return this sym if it is a fake function descriptor
4042          created by add_symbol_adjust.  */
4043       && !((struct ppc_link_hash_entry *) h)->fake)
4044     return h;
4045
4046   if (name[0] == '.')
4047     return h;
4048
4049   len = strlen (name);
4050   dot_name = bfd_alloc (abfd, len + 2);
4051   if (dot_name == NULL)
4052     return (struct elf_link_hash_entry *) -1;
4053   dot_name[0] = '.';
4054   memcpy (dot_name + 1, name, len + 1);
4055   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4056   bfd_release (abfd, dot_name);
4057   return h;
4058 }
4059
4060 /* This function satisfies all old ABI object references to ".bar" if a
4061    new ABI object defines "bar".  Well, at least, undefined dot symbols
4062    are made weak.  This stops later archive searches from including an
4063    object if we already have a function descriptor definition.  It also
4064    prevents the linker complaining about undefined symbols.
4065    We also check and correct mismatched symbol visibility here.  The
4066    most restrictive visibility of the function descriptor and the
4067    function entry symbol is used.  */
4068
4069 static bfd_boolean
4070 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4071 {
4072   struct ppc_link_hash_table *htab;
4073   struct ppc_link_hash_entry *fdh;
4074
4075   if (eh->elf.root.type == bfd_link_hash_warning)
4076     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4077
4078   if (eh->elf.root.type == bfd_link_hash_indirect)
4079     return TRUE;
4080
4081   if (eh->elf.root.root.string[0] != '.')
4082     abort ();
4083
4084   htab = ppc_hash_table (info);
4085   if (htab == NULL)
4086     return FALSE;
4087
4088   fdh = lookup_fdh (eh, htab);
4089   if (fdh == NULL
4090       && !bfd_link_relocatable (info)
4091       && (eh->elf.root.type == bfd_link_hash_undefined
4092           || eh->elf.root.type == bfd_link_hash_undefweak)
4093       && eh->elf.ref_regular)
4094     {
4095       /* Make an undefined function descriptor sym, in order to
4096          pull in an --as-needed shared lib.  Archives are handled
4097          elsewhere.  */
4098       fdh = make_fdh (info, eh);
4099       if (fdh == NULL)
4100         return FALSE;
4101     }
4102
4103   if (fdh != NULL)
4104     {
4105       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4106       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4107
4108       /* Make both descriptor and entry symbol have the most
4109          constraining visibility of either symbol.  */
4110       if (entry_vis < descr_vis)
4111         fdh->elf.other += entry_vis - descr_vis;
4112       else if (entry_vis > descr_vis)
4113         eh->elf.other += descr_vis - entry_vis;
4114
4115       /* Propagate reference flags from entry symbol to function
4116          descriptor symbol.  */
4117       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4118       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4119       fdh->elf.ref_regular |= eh->elf.ref_regular;
4120       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4121
4122       if (!fdh->elf.forced_local
4123           && fdh->elf.dynindx == -1
4124           && fdh->elf.versioned != versioned_hidden
4125           && (bfd_link_dll (info)
4126               || fdh->elf.def_dynamic
4127               || fdh->elf.ref_dynamic)
4128           && (eh->elf.ref_regular
4129               || eh->elf.def_regular))
4130         {
4131           if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4132             return FALSE;
4133         }
4134     }
4135
4136   return TRUE;
4137 }
4138
4139 /* Set up opd section info and abiversion for IBFD, and process list
4140    of dot-symbols we made in link_hash_newfunc.  */
4141
4142 static bfd_boolean
4143 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4144 {
4145   struct ppc_link_hash_table *htab;
4146   struct ppc_link_hash_entry **p, *eh;
4147   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4148
4149   if (opd != NULL && opd->size != 0)
4150     {
4151       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4152       ppc64_elf_section_data (opd)->sec_type = sec_opd;
4153
4154       if (abiversion (ibfd) == 0)
4155         set_abiversion (ibfd, 1);
4156       else if (abiversion (ibfd) >= 2)
4157         {
4158           /* xgettext:c-format */
4159           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4160                               ibfd, abiversion (ibfd));
4161           bfd_set_error (bfd_error_bad_value);
4162           return FALSE;
4163         }
4164     }
4165
4166   if (is_ppc64_elf (info->output_bfd))
4167     {
4168       /* For input files without an explicit abiversion in e_flags
4169          we should have flagged any with symbol st_other bits set
4170          as ELFv1 and above flagged those with .opd as ELFv2.
4171          Set the output abiversion if not yet set, and for any input
4172          still ambiguous, take its abiversion from the output.
4173          Differences in ABI are reported later.  */
4174       if (abiversion (info->output_bfd) == 0)
4175         set_abiversion (info->output_bfd, abiversion (ibfd));
4176       else if (abiversion (ibfd) == 0)
4177         set_abiversion (ibfd, abiversion (info->output_bfd));
4178     }
4179
4180   htab = ppc_hash_table (info);
4181   if (htab == NULL)
4182     return TRUE;
4183
4184   if (opd != NULL && opd->size != 0
4185       && (ibfd->flags & DYNAMIC) == 0
4186       && (opd->flags & SEC_RELOC) != 0
4187       && opd->reloc_count != 0
4188       && !bfd_is_abs_section (opd->output_section)
4189       && info->gc_sections)
4190     {
4191       /* Garbage collection needs some extra help with .opd sections.
4192          We don't want to necessarily keep everything referenced by
4193          relocs in .opd, as that would keep all functions.  Instead,
4194          if we reference an .opd symbol (a function descriptor), we
4195          want to keep the function code symbol's section.  This is
4196          easy for global symbols, but for local syms we need to keep
4197          information about the associated function section.  */
4198       bfd_size_type amt;
4199       asection **opd_sym_map;
4200       Elf_Internal_Shdr *symtab_hdr;
4201       Elf_Internal_Rela *relocs, *rel_end, *rel;
4202
4203       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4204       opd_sym_map = bfd_zalloc (ibfd, amt);
4205       if (opd_sym_map == NULL)
4206         return FALSE;
4207       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4208       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4209                                           info->keep_memory);
4210       if (relocs == NULL)
4211         return FALSE;
4212       symtab_hdr = &elf_symtab_hdr (ibfd);
4213       rel_end = relocs + opd->reloc_count - 1;
4214       for (rel = relocs; rel < rel_end; rel++)
4215         {
4216           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4217           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4218
4219           if (r_type == R_PPC64_ADDR64
4220               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4221               && r_symndx < symtab_hdr->sh_info)
4222             {
4223               Elf_Internal_Sym *isym;
4224               asection *s;
4225
4226               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4227               if (isym == NULL)
4228                 {
4229                   if (elf_section_data (opd)->relocs != relocs)
4230                     free (relocs);
4231                   return FALSE;
4232                 }
4233
4234               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4235               if (s != NULL && s != opd)
4236                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4237             }
4238         }
4239       if (elf_section_data (opd)->relocs != relocs)
4240         free (relocs);
4241     }
4242
4243   p = &htab->dot_syms;
4244   while ((eh = *p) != NULL)
4245     {
4246       *p = NULL;
4247       if (&eh->elf == htab->elf.hgot)
4248         ;
4249       else if (htab->elf.hgot == NULL
4250                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4251         htab->elf.hgot = &eh->elf;
4252       else if (abiversion (ibfd) <= 1)
4253         {
4254           htab->need_func_desc_adj = 1;
4255           if (!add_symbol_adjust (eh, info))
4256             return FALSE;
4257         }
4258       p = &eh->u.next_dot_sym;
4259     }
4260   return TRUE;
4261 }
4262
4263 /* Undo hash table changes when an --as-needed input file is determined
4264    not to be needed.  */
4265
4266 static bfd_boolean
4267 ppc64_elf_notice_as_needed (bfd *ibfd,
4268                             struct bfd_link_info *info,
4269                             enum notice_asneeded_action act)
4270 {
4271   if (act == notice_not_needed)
4272     {
4273       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4274
4275       if (htab == NULL)
4276         return FALSE;
4277
4278       htab->dot_syms = NULL;
4279     }
4280   return _bfd_elf_notice_as_needed (ibfd, info, act);
4281 }
4282
4283 /* If --just-symbols against a final linked binary, then assume we need
4284    toc adjusting stubs when calling functions defined there.  */
4285
4286 static void
4287 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4288 {
4289   if ((sec->flags & SEC_CODE) != 0
4290       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4291       && is_ppc64_elf (sec->owner))
4292     {
4293       if (abiversion (sec->owner) >= 2
4294           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4295         sec->has_toc_reloc = 1;
4296     }
4297   _bfd_elf_link_just_syms (sec, info);
4298 }
4299
4300 static struct plt_entry **
4301 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4302                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4303 {
4304   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4305   struct plt_entry **local_plt;
4306   unsigned char *local_got_tls_masks;
4307
4308   if (local_got_ents == NULL)
4309     {
4310       bfd_size_type size = symtab_hdr->sh_info;
4311
4312       size *= (sizeof (*local_got_ents)
4313                + sizeof (*local_plt)
4314                + sizeof (*local_got_tls_masks));
4315       local_got_ents = bfd_zalloc (abfd, size);
4316       if (local_got_ents == NULL)
4317         return NULL;
4318       elf_local_got_ents (abfd) = local_got_ents;
4319     }
4320
4321   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4322     {
4323       struct got_entry *ent;
4324
4325       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4326         if (ent->addend == r_addend
4327             && ent->owner == abfd
4328             && ent->tls_type == tls_type)
4329           break;
4330       if (ent == NULL)
4331         {
4332           bfd_size_type amt = sizeof (*ent);
4333           ent = bfd_alloc (abfd, amt);
4334           if (ent == NULL)
4335             return FALSE;
4336           ent->next = local_got_ents[r_symndx];
4337           ent->addend = r_addend;
4338           ent->owner = abfd;
4339           ent->tls_type = tls_type;
4340           ent->is_indirect = FALSE;
4341           ent->got.refcount = 0;
4342           local_got_ents[r_symndx] = ent;
4343         }
4344       ent->got.refcount += 1;
4345     }
4346
4347   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4348   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4349   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4350
4351   return local_plt + r_symndx;
4352 }
4353
4354 static bfd_boolean
4355 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4356 {
4357   struct plt_entry *ent;
4358
4359   for (ent = *plist; ent != NULL; ent = ent->next)
4360     if (ent->addend == addend)
4361       break;
4362   if (ent == NULL)
4363     {
4364       bfd_size_type amt = sizeof (*ent);
4365       ent = bfd_alloc (abfd, amt);
4366       if (ent == NULL)
4367         return FALSE;
4368       ent->next = *plist;
4369       ent->addend = addend;
4370       ent->plt.refcount = 0;
4371       *plist = ent;
4372     }
4373   ent->plt.refcount += 1;
4374   return TRUE;
4375 }
4376
4377 static bfd_boolean
4378 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4379 {
4380   return (r_type == R_PPC64_REL24
4381           || r_type == R_PPC64_REL24_NOTOC
4382           || r_type == R_PPC64_REL14
4383           || r_type == R_PPC64_REL14_BRTAKEN
4384           || r_type == R_PPC64_REL14_BRNTAKEN
4385           || r_type == R_PPC64_ADDR24
4386           || r_type == R_PPC64_ADDR14
4387           || r_type == R_PPC64_ADDR14_BRTAKEN
4388           || r_type == R_PPC64_ADDR14_BRNTAKEN
4389           || r_type == R_PPC64_PLTCALL
4390           || r_type == R_PPC64_PLTCALL_NOTOC);
4391 }
4392
4393 /* Relocs on inline plt call sequence insns prior to the call.  */
4394
4395 static bfd_boolean
4396 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4397 {
4398   return (r_type == R_PPC64_PLT16_HA
4399           || r_type == R_PPC64_PLT16_HI
4400           || r_type == R_PPC64_PLT16_LO
4401           || r_type == R_PPC64_PLT16_LO_DS
4402           || r_type == R_PPC64_PLT_PCREL34
4403           || r_type == R_PPC64_PLT_PCREL34_NOTOC
4404           || r_type == R_PPC64_PLTSEQ
4405           || r_type == R_PPC64_PLTSEQ_NOTOC);
4406 }
4407
4408 /* Look through the relocs for a section during the first phase, and
4409    calculate needed space in the global offset table, procedure
4410    linkage table, and dynamic reloc sections.  */
4411
4412 static bfd_boolean
4413 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4414                         asection *sec, const Elf_Internal_Rela *relocs)
4415 {
4416   struct ppc_link_hash_table *htab;
4417   Elf_Internal_Shdr *symtab_hdr;
4418   struct elf_link_hash_entry **sym_hashes;
4419   const Elf_Internal_Rela *rel;
4420   const Elf_Internal_Rela *rel_end;
4421   asection *sreloc;
4422   struct elf_link_hash_entry *tga, *dottga;
4423   bfd_boolean is_opd;
4424
4425   if (bfd_link_relocatable (info))
4426     return TRUE;
4427
4428   /* Don't do anything special with non-loaded, non-alloced sections.
4429      In particular, any relocs in such sections should not affect GOT
4430      and PLT reference counting (ie. we don't allow them to create GOT
4431      or PLT entries), there's no possibility or desire to optimize TLS
4432      relocs, and there's not much point in propagating relocs to shared
4433      libs that the dynamic linker won't relocate.  */
4434   if ((sec->flags & SEC_ALLOC) == 0)
4435     return TRUE;
4436
4437   BFD_ASSERT (is_ppc64_elf (abfd));
4438
4439   htab = ppc_hash_table (info);
4440   if (htab == NULL)
4441     return FALSE;
4442
4443   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4444                               FALSE, FALSE, TRUE);
4445   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4446                                  FALSE, FALSE, TRUE);
4447   symtab_hdr = &elf_symtab_hdr (abfd);
4448   sym_hashes = elf_sym_hashes (abfd);
4449   sreloc = NULL;
4450   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4451   rel_end = relocs + sec->reloc_count;
4452   for (rel = relocs; rel < rel_end; rel++)
4453     {
4454       unsigned long r_symndx;
4455       struct elf_link_hash_entry *h;
4456       enum elf_ppc64_reloc_type r_type;
4457       int tls_type;
4458       struct _ppc64_elf_section_data *ppc64_sec;
4459       struct plt_entry **ifunc, **plt_list;
4460       bfd_vma sym_addend;
4461
4462       r_symndx = ELF64_R_SYM (rel->r_info);
4463       if (r_symndx < symtab_hdr->sh_info)
4464         h = NULL;
4465       else
4466         {
4467           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4468           h = elf_follow_link (h);
4469
4470           if (h == htab->elf.hgot)
4471             sec->has_toc_reloc = 1;
4472         }
4473
4474       tls_type = 0;
4475       ifunc = NULL;
4476       r_type = ELF64_R_TYPE (rel->r_info);
4477       switch (r_type)
4478         {
4479         default:
4480           /* Somewhat foolishly, because the ABIs don't specifically
4481              allow it, ppc64 gas and ld support GOT and PLT relocs
4482              with non-zero addends where the addend results in
4483              sym+addend being stored in the GOT or PLT entry.  This
4484              can't be supported for pcrel relocs because the addend is
4485              used to specify the pcrel offset.  */
4486           sym_addend = rel->r_addend;
4487           break;
4488         case R_PPC64_GOT_PCREL34:
4489         case R_PPC64_PLT_PCREL34:
4490         case R_PPC64_PLT_PCREL34_NOTOC:
4491           sym_addend = 0;
4492           break;
4493         }
4494       if (h != NULL)
4495         {
4496           if (h->type == STT_GNU_IFUNC)
4497             {
4498               h->needs_plt = 1;
4499               ifunc = &h->plt.plist;
4500             }
4501         }
4502       else
4503         {
4504           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4505                                                           abfd, r_symndx);
4506           if (isym == NULL)
4507             return FALSE;
4508
4509           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4510             {
4511               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4512                                              sym_addend,
4513                                              NON_GOT | PLT_IFUNC);
4514               if (ifunc == NULL)
4515                 return FALSE;
4516             }
4517         }
4518
4519       switch (r_type)
4520         {
4521         case R_PPC64_TLSGD:
4522         case R_PPC64_TLSLD:
4523           /* These special tls relocs tie a call to __tls_get_addr with
4524              its parameter symbol.  */
4525           if (h != NULL)
4526             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4527           else
4528             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4529                                         sym_addend,
4530                                         NON_GOT | TLS_TLS | TLS_MARK))
4531               return FALSE;
4532           sec->has_tls_reloc = 1;
4533           break;
4534
4535         case R_PPC64_GOT_TLSLD16:
4536         case R_PPC64_GOT_TLSLD16_LO:
4537         case R_PPC64_GOT_TLSLD16_HI:
4538         case R_PPC64_GOT_TLSLD16_HA:
4539           tls_type = TLS_TLS | TLS_LD;
4540           goto dogottls;
4541
4542         case R_PPC64_GOT_TLSGD16:
4543         case R_PPC64_GOT_TLSGD16_LO:
4544         case R_PPC64_GOT_TLSGD16_HI:
4545         case R_PPC64_GOT_TLSGD16_HA:
4546           tls_type = TLS_TLS | TLS_GD;
4547           goto dogottls;
4548
4549         case R_PPC64_GOT_TPREL16_DS:
4550         case R_PPC64_GOT_TPREL16_LO_DS:
4551         case R_PPC64_GOT_TPREL16_HI:
4552         case R_PPC64_GOT_TPREL16_HA:
4553           if (bfd_link_dll (info))
4554             info->flags |= DF_STATIC_TLS;
4555           tls_type = TLS_TLS | TLS_TPREL;
4556           goto dogottls;
4557
4558         case R_PPC64_GOT_DTPREL16_DS:
4559         case R_PPC64_GOT_DTPREL16_LO_DS:
4560         case R_PPC64_GOT_DTPREL16_HI:
4561         case R_PPC64_GOT_DTPREL16_HA:
4562           tls_type = TLS_TLS | TLS_DTPREL;
4563         dogottls:
4564           sec->has_tls_reloc = 1;
4565           goto dogot;
4566
4567         case R_PPC64_GOT16_DS:
4568         case R_PPC64_GOT16_HA:
4569         case R_PPC64_GOT16_LO_DS:
4570           ppc64_elf_tdata (abfd)->has_gotrel = 1;
4571           ppc64_elf_section_data (sec)->has_gotrel = 1;
4572           /* Fall through.  */
4573
4574         case R_PPC64_GOT16:
4575         case R_PPC64_GOT16_HI:
4576         case R_PPC64_GOT16_LO:
4577         case R_PPC64_GOT_PCREL34:
4578         dogot:
4579           /* This symbol requires a global offset table entry.  */
4580           sec->has_toc_reloc = 1;
4581           if (r_type == R_PPC64_GOT_TLSLD16
4582               || r_type == R_PPC64_GOT_TLSGD16
4583               || r_type == R_PPC64_GOT_TPREL16_DS
4584               || r_type == R_PPC64_GOT_DTPREL16_DS
4585               || r_type == R_PPC64_GOT16
4586               || r_type == R_PPC64_GOT16_DS)
4587             {
4588               htab->do_multi_toc = 1;
4589               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4590             }
4591
4592           if (ppc64_elf_tdata (abfd)->got == NULL
4593               && !create_got_section (abfd, info))
4594             return FALSE;
4595
4596           if (h != NULL)
4597             {
4598               struct ppc_link_hash_entry *eh;
4599               struct got_entry *ent;
4600
4601               eh = (struct ppc_link_hash_entry *) h;
4602               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4603                 if (ent->addend == sym_addend
4604                     && ent->owner == abfd
4605                     && ent->tls_type == tls_type)
4606                   break;
4607               if (ent == NULL)
4608                 {
4609                   bfd_size_type amt = sizeof (*ent);
4610                   ent = bfd_alloc (abfd, amt);
4611                   if (ent == NULL)
4612                     return FALSE;
4613                   ent->next = eh->elf.got.glist;
4614                   ent->addend = sym_addend;
4615                   ent->owner = abfd;
4616                   ent->tls_type = tls_type;
4617                   ent->is_indirect = FALSE;
4618                   ent->got.refcount = 0;
4619                   eh->elf.got.glist = ent;
4620                 }
4621               ent->got.refcount += 1;
4622               eh->tls_mask |= tls_type;
4623             }
4624           else
4625             /* This is a global offset table entry for a local symbol.  */
4626             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4627                                         sym_addend, tls_type))
4628               return FALSE;
4629
4630           /* We may also need a plt entry if the symbol turns out to be
4631              an ifunc.  */
4632           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4633             {
4634               if (!update_plt_info (abfd, &h->plt.plist, sym_addend))
4635                 return FALSE;
4636             }
4637           break;
4638
4639         case R_PPC64_PLT16_HA:
4640         case R_PPC64_PLT16_HI:
4641         case R_PPC64_PLT16_LO:
4642         case R_PPC64_PLT16_LO_DS:
4643         case R_PPC64_PLT_PCREL34:
4644         case R_PPC64_PLT_PCREL34_NOTOC:
4645         case R_PPC64_PLT32:
4646         case R_PPC64_PLT64:
4647           /* This symbol requires a procedure linkage table entry.  */
4648           plt_list = ifunc;
4649           if (h != NULL)
4650             {
4651               h->needs_plt = 1;
4652               if (h->root.root.string[0] == '.'
4653                   && h->root.root.string[1] != '\0')
4654                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4655               ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4656               plt_list = &h->plt.plist;
4657             }
4658           if (plt_list == NULL)
4659             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4660                                               sym_addend,
4661                                               NON_GOT | PLT_KEEP);
4662           if (!update_plt_info (abfd, plt_list, sym_addend))
4663             return FALSE;
4664           break;
4665
4666           /* The following relocations don't need to propagate the
4667              relocation if linking a shared object since they are
4668              section relative.  */
4669         case R_PPC64_SECTOFF:
4670         case R_PPC64_SECTOFF_LO:
4671         case R_PPC64_SECTOFF_HI:
4672         case R_PPC64_SECTOFF_HA:
4673         case R_PPC64_SECTOFF_DS:
4674         case R_PPC64_SECTOFF_LO_DS:
4675         case R_PPC64_DTPREL16:
4676         case R_PPC64_DTPREL16_LO:
4677         case R_PPC64_DTPREL16_HI:
4678         case R_PPC64_DTPREL16_HA:
4679         case R_PPC64_DTPREL16_DS:
4680         case R_PPC64_DTPREL16_LO_DS:
4681         case R_PPC64_DTPREL16_HIGH:
4682         case R_PPC64_DTPREL16_HIGHA:
4683         case R_PPC64_DTPREL16_HIGHER:
4684         case R_PPC64_DTPREL16_HIGHERA:
4685         case R_PPC64_DTPREL16_HIGHEST:
4686         case R_PPC64_DTPREL16_HIGHESTA:
4687           break;
4688
4689           /* Nor do these.  */
4690         case R_PPC64_REL16:
4691         case R_PPC64_REL16_LO:
4692         case R_PPC64_REL16_HI:
4693         case R_PPC64_REL16_HA:
4694         case R_PPC64_REL16_HIGH:
4695         case R_PPC64_REL16_HIGHA:
4696         case R_PPC64_REL16_HIGHER:
4697         case R_PPC64_REL16_HIGHERA:
4698         case R_PPC64_REL16_HIGHEST:
4699         case R_PPC64_REL16_HIGHESTA:
4700         case R_PPC64_REL16_HIGHER34:
4701         case R_PPC64_REL16_HIGHERA34:
4702         case R_PPC64_REL16_HIGHEST34:
4703         case R_PPC64_REL16_HIGHESTA34:
4704         case R_PPC64_REL16DX_HA:
4705           break;
4706
4707           /* Not supported as a dynamic relocation.  */
4708         case R_PPC64_ADDR64_LOCAL:
4709           if (bfd_link_pic (info))
4710             {
4711               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4712                 ppc_howto_init ();
4713               /* xgettext:c-format */
4714               info->callbacks->einfo (_("%H: %s reloc unsupported "
4715                                         "in shared libraries and PIEs\n"),
4716                                       abfd, sec, rel->r_offset,
4717                                       ppc64_elf_howto_table[r_type]->name);
4718               bfd_set_error (bfd_error_bad_value);
4719               return FALSE;
4720             }
4721           break;
4722
4723         case R_PPC64_TOC16:
4724         case R_PPC64_TOC16_DS:
4725           htab->do_multi_toc = 1;
4726           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4727           /* Fall through.  */
4728         case R_PPC64_TOC16_LO:
4729         case R_PPC64_TOC16_HI:
4730         case R_PPC64_TOC16_HA:
4731         case R_PPC64_TOC16_LO_DS:
4732           sec->has_toc_reloc = 1;
4733           break;
4734
4735           /* Marker reloc.  */
4736         case R_PPC64_ENTRY:
4737           break;
4738
4739           /* This relocation describes the C++ object vtable hierarchy.
4740              Reconstruct it for later use during GC.  */
4741         case R_PPC64_GNU_VTINHERIT:
4742           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4743             return FALSE;
4744           break;
4745
4746           /* This relocation describes which C++ vtable entries are actually
4747              used.  Record for later use during GC.  */
4748         case R_PPC64_GNU_VTENTRY:
4749           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4750             return FALSE;
4751           break;
4752
4753         case R_PPC64_REL14:
4754         case R_PPC64_REL14_BRTAKEN:
4755         case R_PPC64_REL14_BRNTAKEN:
4756           {
4757             asection *dest = NULL;
4758
4759             /* Heuristic: If jumping outside our section, chances are
4760                we are going to need a stub.  */
4761             if (h != NULL)
4762               {
4763                 /* If the sym is weak it may be overridden later, so
4764                    don't assume we know where a weak sym lives.  */
4765                 if (h->root.type == bfd_link_hash_defined)
4766                   dest = h->root.u.def.section;
4767               }
4768             else
4769               {
4770                 Elf_Internal_Sym *isym;
4771
4772                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4773                                               abfd, r_symndx);
4774                 if (isym == NULL)
4775                   return FALSE;
4776
4777                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4778               }
4779
4780             if (dest != sec)
4781               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4782           }
4783           goto rel24;
4784
4785         case R_PPC64_PLTCALL:
4786         case R_PPC64_PLTCALL_NOTOC:
4787           ppc64_elf_section_data (sec)->has_pltcall = 1;
4788           /* Fall through.  */
4789
4790         case R_PPC64_REL24:
4791         case R_PPC64_REL24_NOTOC:
4792         rel24:
4793           plt_list = ifunc;
4794           if (h != NULL)
4795             {
4796               h->needs_plt = 1;
4797               if (h->root.root.string[0] == '.'
4798                   && h->root.root.string[1] != '\0')
4799                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4800
4801               if (h == tga || h == dottga)
4802                 {
4803                   sec->has_tls_reloc = 1;
4804                   if (rel != relocs
4805                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4806                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4807                     /* We have a new-style __tls_get_addr call with
4808                        a marker reloc.  */
4809                     ;
4810                   else
4811                     /* Mark this section as having an old-style call.  */
4812                     sec->has_tls_get_addr_call = 1;
4813                 }
4814               plt_list = &h->plt.plist;
4815             }
4816
4817           /* We may need a .plt entry if the function this reloc
4818              refers to is in a shared lib.  */
4819           if (plt_list
4820               && !update_plt_info (abfd, plt_list, sym_addend))
4821             return FALSE;
4822           break;
4823
4824         case R_PPC64_ADDR14:
4825         case R_PPC64_ADDR14_BRNTAKEN:
4826         case R_PPC64_ADDR14_BRTAKEN:
4827         case R_PPC64_ADDR24:
4828           goto dodyn;
4829
4830         case R_PPC64_TPREL64:
4831           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4832           if (bfd_link_dll (info))
4833             info->flags |= DF_STATIC_TLS;
4834           goto dotlstoc;
4835
4836         case R_PPC64_DTPMOD64:
4837           if (rel + 1 < rel_end
4838               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4839               && rel[1].r_offset == rel->r_offset + 8)
4840             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4841           else
4842             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4843           goto dotlstoc;
4844
4845         case R_PPC64_DTPREL64:
4846           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4847           if (rel != relocs
4848               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4849               && rel[-1].r_offset == rel->r_offset - 8)
4850             /* This is the second reloc of a dtpmod, dtprel pair.
4851                Don't mark with TLS_DTPREL.  */
4852             goto dodyn;
4853
4854         dotlstoc:
4855           sec->has_tls_reloc = 1;
4856           if (h != NULL)
4857             {
4858               struct ppc_link_hash_entry *eh;
4859               eh = (struct ppc_link_hash_entry *) h;
4860               eh->tls_mask |= tls_type;
4861             }
4862           else
4863             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4864                                         sym_addend, tls_type))
4865               return FALSE;
4866
4867           ppc64_sec = ppc64_elf_section_data (sec);
4868           if (ppc64_sec->sec_type != sec_toc)
4869             {
4870               bfd_size_type amt;
4871
4872               /* One extra to simplify get_tls_mask.  */
4873               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4874               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4875               if (ppc64_sec->u.toc.symndx == NULL)
4876                 return FALSE;
4877               amt = sec->size * sizeof (bfd_vma) / 8;
4878               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4879               if (ppc64_sec->u.toc.add == NULL)
4880                 return FALSE;
4881               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4882               ppc64_sec->sec_type = sec_toc;
4883             }
4884           BFD_ASSERT (rel->r_offset % 8 == 0);
4885           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4886           ppc64_sec->u.toc.add[rel->r_offset / 8] = sym_addend;
4887
4888           /* Mark the second slot of a GD or LD entry.
4889              -1 to indicate GD and -2 to indicate LD.  */
4890           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4891             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4892           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4893             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4894           goto dodyn;
4895
4896         case R_PPC64_TPREL16:
4897         case R_PPC64_TPREL16_LO:
4898         case R_PPC64_TPREL16_HI:
4899         case R_PPC64_TPREL16_HA:
4900         case R_PPC64_TPREL16_DS:
4901         case R_PPC64_TPREL16_LO_DS:
4902         case R_PPC64_TPREL16_HIGH:
4903         case R_PPC64_TPREL16_HIGHA:
4904         case R_PPC64_TPREL16_HIGHER:
4905         case R_PPC64_TPREL16_HIGHERA:
4906         case R_PPC64_TPREL16_HIGHEST:
4907         case R_PPC64_TPREL16_HIGHESTA:
4908           if (bfd_link_dll (info))
4909             info->flags |= DF_STATIC_TLS;
4910           goto dodyn;
4911
4912         case R_PPC64_ADDR64:
4913           if (is_opd
4914               && rel + 1 < rel_end
4915               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4916             {
4917               if (h != NULL)
4918                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4919             }
4920           /* Fall through.  */
4921
4922         case R_PPC64_ADDR16:
4923         case R_PPC64_ADDR16_DS:
4924         case R_PPC64_ADDR16_HA:
4925         case R_PPC64_ADDR16_HI:
4926         case R_PPC64_ADDR16_HIGH:
4927         case R_PPC64_ADDR16_HIGHA:
4928         case R_PPC64_ADDR16_HIGHER:
4929         case R_PPC64_ADDR16_HIGHERA:
4930         case R_PPC64_ADDR16_HIGHEST:
4931         case R_PPC64_ADDR16_HIGHESTA:
4932         case R_PPC64_ADDR16_LO:
4933         case R_PPC64_ADDR16_LO_DS:
4934         case R_PPC64_D34:
4935         case R_PPC64_D34_LO:
4936         case R_PPC64_D34_HI30:
4937         case R_PPC64_D34_HA30:
4938         case R_PPC64_ADDR16_HIGHER34:
4939         case R_PPC64_ADDR16_HIGHERA34:
4940         case R_PPC64_ADDR16_HIGHEST34:
4941         case R_PPC64_ADDR16_HIGHESTA34:
4942         case R_PPC64_D28:
4943           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4944               && rel->r_addend == 0)
4945             {
4946               /* We may need a .plt entry if this reloc refers to a
4947                  function in a shared lib.  */
4948               if (!update_plt_info (abfd, &h->plt.plist, 0))
4949                 return FALSE;
4950               h->pointer_equality_needed = 1;
4951             }
4952           /* Fall through.  */
4953
4954         case R_PPC64_REL30:
4955         case R_PPC64_REL32:
4956         case R_PPC64_REL64:
4957         case R_PPC64_ADDR32:
4958         case R_PPC64_UADDR16:
4959         case R_PPC64_UADDR32:
4960         case R_PPC64_UADDR64:
4961         case R_PPC64_TOC:
4962           if (h != NULL && !bfd_link_pic (info))
4963             /* We may need a copy reloc.  */
4964             h->non_got_ref = 1;
4965
4966           /* Don't propagate .opd relocs.  */
4967           if (NO_OPD_RELOCS && is_opd)
4968             break;
4969
4970           /* If we are creating a shared library, and this is a reloc
4971              against a global symbol, or a non PC relative reloc
4972              against a local symbol, then we need to copy the reloc
4973              into the shared library.  However, if we are linking with
4974              -Bsymbolic, we do not need to copy a reloc against a
4975              global symbol which is defined in an object we are
4976              including in the link (i.e., DEF_REGULAR is set).  At
4977              this point we have not seen all the input files, so it is
4978              possible that DEF_REGULAR is not set now but will be set
4979              later (it is never cleared).  In case of a weak definition,
4980              DEF_REGULAR may be cleared later by a strong definition in
4981              a shared library.  We account for that possibility below by
4982              storing information in the dyn_relocs field of the hash
4983              table entry.  A similar situation occurs when creating
4984              shared libraries and symbol visibility changes render the
4985              symbol local.
4986
4987              If on the other hand, we are creating an executable, we
4988              may need to keep relocations for symbols satisfied by a
4989              dynamic library if we manage to avoid copy relocs for the
4990              symbol.  */
4991         dodyn:
4992           if ((bfd_link_pic (info)
4993                && (must_be_dyn_reloc (info, r_type)
4994                    || (h != NULL
4995                        && (!SYMBOLIC_BIND (info, h)
4996                            || h->root.type == bfd_link_hash_defweak
4997                            || !h->def_regular))))
4998               || (ELIMINATE_COPY_RELOCS
4999                   && !bfd_link_pic (info)
5000                   && h != NULL
5001                   && (h->root.type == bfd_link_hash_defweak
5002                       || !h->def_regular))
5003               || (!bfd_link_pic (info)
5004                   && ifunc != NULL))
5005             {
5006               /* We must copy these reloc types into the output file.
5007                  Create a reloc section in dynobj and make room for
5008                  this reloc.  */
5009               if (sreloc == NULL)
5010                 {
5011                   sreloc = _bfd_elf_make_dynamic_reloc_section
5012                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5013
5014                   if (sreloc == NULL)
5015                     return FALSE;
5016                 }
5017
5018               /* If this is a global symbol, we count the number of
5019                  relocations we need for this symbol.  */
5020               if (h != NULL)
5021                 {
5022                   struct elf_dyn_relocs *p;
5023                   struct elf_dyn_relocs **head;
5024
5025                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5026                   p = *head;
5027                   if (p == NULL || p->sec != sec)
5028                     {
5029                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5030                       if (p == NULL)
5031                         return FALSE;
5032                       p->next = *head;
5033                       *head = p;
5034                       p->sec = sec;
5035                       p->count = 0;
5036                       p->pc_count = 0;
5037                     }
5038                   p->count += 1;
5039                   if (!must_be_dyn_reloc (info, r_type))
5040                     p->pc_count += 1;
5041                 }
5042               else
5043                 {
5044                   /* Track dynamic relocs needed for local syms too.
5045                      We really need local syms available to do this
5046                      easily.  Oh well.  */
5047                   struct ppc_dyn_relocs *p;
5048                   struct ppc_dyn_relocs **head;
5049                   bfd_boolean is_ifunc;
5050                   asection *s;
5051                   void *vpp;
5052                   Elf_Internal_Sym *isym;
5053
5054                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5055                                                 abfd, r_symndx);
5056                   if (isym == NULL)
5057                     return FALSE;
5058
5059                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5060                   if (s == NULL)
5061                     s = sec;
5062
5063                   vpp = &elf_section_data (s)->local_dynrel;
5064                   head = (struct ppc_dyn_relocs **) vpp;
5065                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5066                   p = *head;
5067                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5068                     p = p->next;
5069                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5070                     {
5071                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5072                       if (p == NULL)
5073                         return FALSE;
5074                       p->next = *head;
5075                       *head = p;
5076                       p->sec = sec;
5077                       p->ifunc = is_ifunc;
5078                       p->count = 0;
5079                     }
5080                   p->count += 1;
5081                 }
5082             }
5083           break;
5084
5085         default:
5086           break;
5087         }
5088     }
5089
5090   return TRUE;
5091 }
5092
5093 /* Merge backend specific data from an object file to the output
5094    object file when linking.  */
5095
5096 static bfd_boolean
5097 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5098 {
5099   bfd *obfd = info->output_bfd;
5100   unsigned long iflags, oflags;
5101
5102   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5103     return TRUE;
5104
5105   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5106     return TRUE;
5107
5108   if (!_bfd_generic_verify_endian_match (ibfd, info))
5109     return FALSE;
5110
5111   iflags = elf_elfheader (ibfd)->e_flags;
5112   oflags = elf_elfheader (obfd)->e_flags;
5113
5114   if (iflags & ~EF_PPC64_ABI)
5115     {
5116       _bfd_error_handler
5117         /* xgettext:c-format */
5118         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5119       bfd_set_error (bfd_error_bad_value);
5120       return FALSE;
5121     }
5122   else if (iflags != oflags && iflags != 0)
5123     {
5124       _bfd_error_handler
5125         /* xgettext:c-format */
5126         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5127          ibfd, iflags, oflags);
5128       bfd_set_error (bfd_error_bad_value);
5129       return FALSE;
5130     }
5131
5132   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5133     return FALSE;
5134
5135   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5136   return _bfd_elf_merge_object_attributes (ibfd, info);
5137 }
5138
5139 static bfd_boolean
5140 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5141 {
5142   /* Print normal ELF private data.  */
5143   _bfd_elf_print_private_bfd_data (abfd, ptr);
5144
5145   if (elf_elfheader (abfd)->e_flags != 0)
5146     {
5147       FILE *file = ptr;
5148
5149       fprintf (file, _("private flags = 0x%lx:"),
5150                elf_elfheader (abfd)->e_flags);
5151
5152       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5153         fprintf (file, _(" [abiv%ld]"),
5154                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5155       fputc ('\n', file);
5156     }
5157
5158   return TRUE;
5159 }
5160
5161 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5162    of the code entry point, and its section, which must be in the same
5163    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5164
5165 static bfd_vma
5166 opd_entry_value (asection *opd_sec,
5167                  bfd_vma offset,
5168                  asection **code_sec,
5169                  bfd_vma *code_off,
5170                  bfd_boolean in_code_sec)
5171 {
5172   bfd *opd_bfd = opd_sec->owner;
5173   Elf_Internal_Rela *relocs;
5174   Elf_Internal_Rela *lo, *hi, *look;
5175   bfd_vma val;
5176
5177   /* No relocs implies we are linking a --just-symbols object, or looking
5178      at a final linked executable with addr2line or somesuch.  */
5179   if (opd_sec->reloc_count == 0)
5180     {
5181       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5182
5183       if (contents == NULL)
5184         {
5185           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5186             return (bfd_vma) -1;
5187           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5188         }
5189
5190       /* PR 17512: file: 64b9dfbb.  */
5191       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5192         return (bfd_vma) -1;
5193
5194       val = bfd_get_64 (opd_bfd, contents + offset);
5195       if (code_sec != NULL)
5196         {
5197           asection *sec, *likely = NULL;
5198
5199           if (in_code_sec)
5200             {
5201               sec = *code_sec;
5202               if (sec->vma <= val
5203                   && val < sec->vma + sec->size)
5204                 likely = sec;
5205               else
5206                 val = -1;
5207             }
5208           else
5209             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5210               if (sec->vma <= val
5211                   && (sec->flags & SEC_LOAD) != 0
5212                   && (sec->flags & SEC_ALLOC) != 0)
5213                 likely = sec;
5214           if (likely != NULL)
5215             {
5216               *code_sec = likely;
5217               if (code_off != NULL)
5218                 *code_off = val - likely->vma;
5219             }
5220         }
5221       return val;
5222     }
5223
5224   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5225
5226   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5227   if (relocs == NULL)
5228     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5229   /* PR 17512: file: df8e1fd6.  */
5230   if (relocs == NULL)
5231     return (bfd_vma) -1;
5232
5233   /* Go find the opd reloc at the sym address.  */
5234   lo = relocs;
5235   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5236   val = (bfd_vma) -1;
5237   while (lo < hi)
5238     {
5239       look = lo + (hi - lo) / 2;
5240       if (look->r_offset < offset)
5241         lo = look + 1;
5242       else if (look->r_offset > offset)
5243         hi = look;
5244       else
5245         {
5246           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5247
5248           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5249               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5250             {
5251               unsigned long symndx = ELF64_R_SYM (look->r_info);
5252               asection *sec = NULL;
5253
5254               if (symndx >= symtab_hdr->sh_info
5255                   && elf_sym_hashes (opd_bfd) != NULL)
5256                 {
5257                   struct elf_link_hash_entry **sym_hashes;
5258                   struct elf_link_hash_entry *rh;
5259
5260                   sym_hashes = elf_sym_hashes (opd_bfd);
5261                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5262                   if (rh != NULL)
5263                     {
5264                       rh = elf_follow_link (rh);
5265                       if (rh->root.type != bfd_link_hash_defined
5266                           && rh->root.type != bfd_link_hash_defweak)
5267                         break;
5268                       if (rh->root.u.def.section->owner == opd_bfd)
5269                         {
5270                           val = rh->root.u.def.value;
5271                           sec = rh->root.u.def.section;
5272                         }
5273                     }
5274                 }
5275
5276               if (sec == NULL)
5277                 {
5278                   Elf_Internal_Sym *sym;
5279
5280                   if (symndx < symtab_hdr->sh_info)
5281                     {
5282                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5283                       if (sym == NULL)
5284                         {
5285                           size_t symcnt = symtab_hdr->sh_info;
5286                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5287                                                       symcnt, 0,
5288                                                       NULL, NULL, NULL);
5289                           if (sym == NULL)
5290                             break;
5291                           symtab_hdr->contents = (bfd_byte *) sym;
5292                         }
5293                       sym += symndx;
5294                     }
5295                   else
5296                     {
5297                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5298                                                   1, symndx,
5299                                                   NULL, NULL, NULL);
5300                       if (sym == NULL)
5301                         break;
5302                     }
5303                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5304                   if (sec == NULL)
5305                     break;
5306                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5307                   val = sym->st_value;
5308                 }
5309
5310               val += look->r_addend;
5311               if (code_off != NULL)
5312                 *code_off = val;
5313               if (code_sec != NULL)
5314                 {
5315                   if (in_code_sec && *code_sec != sec)
5316                     return -1;
5317                   else
5318                     *code_sec = sec;
5319                 }
5320               if (sec->output_section != NULL)
5321                 val += sec->output_section->vma + sec->output_offset;
5322             }
5323           break;
5324         }
5325     }
5326
5327   return val;
5328 }
5329
5330 /* If the ELF symbol SYM might be a function in SEC, return the
5331    function size and set *CODE_OFF to the function's entry point,
5332    otherwise return zero.  */
5333
5334 static bfd_size_type
5335 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5336                               bfd_vma *code_off)
5337 {
5338   bfd_size_type size;
5339
5340   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5341                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5342     return 0;
5343
5344   size = 0;
5345   if (!(sym->flags & BSF_SYNTHETIC))
5346     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5347
5348   if (strcmp (sym->section->name, ".opd") == 0)
5349     {
5350       struct _opd_sec_data *opd = get_opd_info (sym->section);
5351       bfd_vma symval = sym->value;
5352
5353       if (opd != NULL
5354           && opd->adjust != NULL
5355           && elf_section_data (sym->section)->relocs != NULL)
5356         {
5357           /* opd_entry_value will use cached relocs that have been
5358              adjusted, but with raw symbols.  That means both local
5359              and global symbols need adjusting.  */
5360           long adjust = opd->adjust[OPD_NDX (symval)];
5361           if (adjust == -1)
5362             return 0;
5363           symval += adjust;
5364         }
5365
5366       if (opd_entry_value (sym->section, symval,
5367                            &sec, code_off, TRUE) == (bfd_vma) -1)
5368         return 0;
5369       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5370          symbol.  This size has nothing to do with the code size of the
5371          function, which is what we're supposed to return, but the
5372          code size isn't available without looking up the dot-sym.
5373          However, doing that would be a waste of time particularly
5374          since elf_find_function will look at the dot-sym anyway.
5375          Now, elf_find_function will keep the largest size of any
5376          function sym found at the code address of interest, so return
5377          1 here to avoid it incorrectly caching a larger function size
5378          for a small function.  This does mean we return the wrong
5379          size for a new-ABI function of size 24, but all that does is
5380          disable caching for such functions.  */
5381       if (size == 24)
5382         size = 1;
5383     }
5384   else
5385     {
5386       if (sym->section != sec)
5387         return 0;
5388       *code_off = sym->value;
5389     }
5390   if (size == 0)
5391     size = 1;
5392   return size;
5393 }
5394
5395 /* Return true if symbol is a strong function defined in an ELFv2
5396    object with st_other localentry bits of zero, ie. its local entry
5397    point coincides with its global entry point.  */
5398
5399 static bfd_boolean
5400 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5401 {
5402   return (h != NULL
5403           && h->type == STT_FUNC
5404           && h->root.type == bfd_link_hash_defined
5405           && (STO_PPC64_LOCAL_MASK & h->other) == 0
5406           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5407           && is_ppc64_elf (h->root.u.def.section->owner)
5408           && abiversion (h->root.u.def.section->owner) >= 2);
5409 }
5410
5411 /* Return true if symbol is defined in a regular object file.  */
5412
5413 static bfd_boolean
5414 is_static_defined (struct elf_link_hash_entry *h)
5415 {
5416   return ((h->root.type == bfd_link_hash_defined
5417            || h->root.type == bfd_link_hash_defweak)
5418           && h->root.u.def.section != NULL
5419           && h->root.u.def.section->output_section != NULL);
5420 }
5421
5422 /* If FDH is a function descriptor symbol, return the associated code
5423    entry symbol if it is defined.  Return NULL otherwise.  */
5424
5425 static struct ppc_link_hash_entry *
5426 defined_code_entry (struct ppc_link_hash_entry *fdh)
5427 {
5428   if (fdh->is_func_descriptor)
5429     {
5430       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5431       if (fh->elf.root.type == bfd_link_hash_defined
5432           || fh->elf.root.type == bfd_link_hash_defweak)
5433         return fh;
5434     }
5435   return NULL;
5436 }
5437
5438 /* If FH is a function code entry symbol, return the associated
5439    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5440
5441 static struct ppc_link_hash_entry *
5442 defined_func_desc (struct ppc_link_hash_entry *fh)
5443 {
5444   if (fh->oh != NULL
5445       && fh->oh->is_func_descriptor)
5446     {
5447       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5448       if (fdh->elf.root.type == bfd_link_hash_defined
5449           || fdh->elf.root.type == bfd_link_hash_defweak)
5450         return fdh;
5451     }
5452   return NULL;
5453 }
5454
5455 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5456
5457 /* Garbage collect sections, after first dealing with dot-symbols.  */
5458
5459 static bfd_boolean
5460 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5461 {
5462   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5463
5464   if (htab != NULL && htab->need_func_desc_adj)
5465     {
5466       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5467       htab->need_func_desc_adj = 0;
5468     }
5469   return bfd_elf_gc_sections (abfd, info);
5470 }
5471
5472 /* Mark all our entry sym sections, both opd and code section.  */
5473
5474 static void
5475 ppc64_elf_gc_keep (struct bfd_link_info *info)
5476 {
5477   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5478   struct bfd_sym_chain *sym;
5479
5480   if (htab == NULL)
5481     return;
5482
5483   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5484     {
5485       struct ppc_link_hash_entry *eh, *fh;
5486       asection *sec;
5487
5488       eh = (struct ppc_link_hash_entry *)
5489         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5490       if (eh == NULL)
5491         continue;
5492       if (eh->elf.root.type != bfd_link_hash_defined
5493           && eh->elf.root.type != bfd_link_hash_defweak)
5494         continue;
5495
5496       fh = defined_code_entry (eh);
5497       if (fh != NULL)
5498         {
5499           sec = fh->elf.root.u.def.section;
5500           sec->flags |= SEC_KEEP;
5501         }
5502       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5503                && opd_entry_value (eh->elf.root.u.def.section,
5504                                    eh->elf.root.u.def.value,
5505                                    &sec, NULL, FALSE) != (bfd_vma) -1)
5506         sec->flags |= SEC_KEEP;
5507
5508       sec = eh->elf.root.u.def.section;
5509       sec->flags |= SEC_KEEP;
5510     }
5511 }
5512
5513 /* Mark sections containing dynamically referenced symbols.  When
5514    building shared libraries, we must assume that any visible symbol is
5515    referenced.  */
5516
5517 static bfd_boolean
5518 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5519 {
5520   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5521   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5522   struct ppc_link_hash_entry *fdh;
5523   struct bfd_elf_dynamic_list *d = info->dynamic_list;
5524
5525   /* Dynamic linking info is on the func descriptor sym.  */
5526   fdh = defined_func_desc (eh);
5527   if (fdh != NULL)
5528     eh = fdh;
5529
5530   if ((eh->elf.root.type == bfd_link_hash_defined
5531        || eh->elf.root.type == bfd_link_hash_defweak)
5532       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5533           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5534               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5535               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5536               && (!bfd_link_executable (info)
5537                   || info->gc_keep_exported
5538                   || info->export_dynamic
5539                   || (eh->elf.dynamic
5540                       && d != NULL
5541                       && (*d->match) (&d->head, NULL,
5542                                       eh->elf.root.root.string)))
5543               && (eh->elf.versioned >= versioned
5544                   || !bfd_hide_sym_by_version (info->version_info,
5545                                                eh->elf.root.root.string)))))
5546     {
5547       asection *code_sec;
5548       struct ppc_link_hash_entry *fh;
5549
5550       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5551
5552       /* Function descriptor syms cause the associated
5553          function code sym section to be marked.  */
5554       fh = defined_code_entry (eh);
5555       if (fh != NULL)
5556         {
5557           code_sec = fh->elf.root.u.def.section;
5558           code_sec->flags |= SEC_KEEP;
5559         }
5560       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5561                && opd_entry_value (eh->elf.root.u.def.section,
5562                                    eh->elf.root.u.def.value,
5563                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
5564         code_sec->flags |= SEC_KEEP;
5565     }
5566
5567   return TRUE;
5568 }
5569
5570 /* Return the section that should be marked against GC for a given
5571    relocation.  */
5572
5573 static asection *
5574 ppc64_elf_gc_mark_hook (asection *sec,
5575                         struct bfd_link_info *info,
5576                         Elf_Internal_Rela *rel,
5577                         struct elf_link_hash_entry *h,
5578                         Elf_Internal_Sym *sym)
5579 {
5580   asection *rsec;
5581
5582   /* Syms return NULL if we're marking .opd, so we avoid marking all
5583      function sections, as all functions are referenced in .opd.  */
5584   rsec = NULL;
5585   if (get_opd_info (sec) != NULL)
5586     return rsec;
5587
5588   if (h != NULL)
5589     {
5590       enum elf_ppc64_reloc_type r_type;
5591       struct ppc_link_hash_entry *eh, *fh, *fdh;
5592
5593       r_type = ELF64_R_TYPE (rel->r_info);
5594       switch (r_type)
5595         {
5596         case R_PPC64_GNU_VTINHERIT:
5597         case R_PPC64_GNU_VTENTRY:
5598           break;
5599
5600         default:
5601           switch (h->root.type)
5602             {
5603             case bfd_link_hash_defined:
5604             case bfd_link_hash_defweak:
5605               eh = (struct ppc_link_hash_entry *) h;
5606               fdh = defined_func_desc (eh);
5607               if (fdh != NULL)
5608                 {
5609                   /* -mcall-aixdesc code references the dot-symbol on
5610                      a call reloc.  Mark the function descriptor too
5611                      against garbage collection.  */
5612                   fdh->elf.mark = 1;
5613                   if (fdh->elf.is_weakalias)
5614                     weakdef (&fdh->elf)->mark = 1;
5615                   eh = fdh;
5616                 }
5617
5618               /* Function descriptor syms cause the associated
5619                  function code sym section to be marked.  */
5620               fh = defined_code_entry (eh);
5621               if (fh != NULL)
5622                 {
5623                   /* They also mark their opd section.  */
5624                   eh->elf.root.u.def.section->gc_mark = 1;
5625
5626                   rsec = fh->elf.root.u.def.section;
5627                 }
5628               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5629                        && opd_entry_value (eh->elf.root.u.def.section,
5630                                            eh->elf.root.u.def.value,
5631                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
5632                 eh->elf.root.u.def.section->gc_mark = 1;
5633               else
5634                 rsec = h->root.u.def.section;
5635               break;
5636
5637             case bfd_link_hash_common:
5638               rsec = h->root.u.c.p->section;
5639               break;
5640
5641             default:
5642               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5643             }
5644         }
5645     }
5646   else
5647     {
5648       struct _opd_sec_data *opd;
5649
5650       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5651       opd = get_opd_info (rsec);
5652       if (opd != NULL && opd->func_sec != NULL)
5653         {
5654           rsec->gc_mark = 1;
5655
5656           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5657         }
5658     }
5659
5660   return rsec;
5661 }
5662
5663 /* The maximum size of .sfpr.  */
5664 #define SFPR_MAX (218*4)
5665
5666 struct sfpr_def_parms
5667 {
5668   const char name[12];
5669   unsigned char lo, hi;
5670   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5671   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5672 };
5673
5674 /* Auto-generate _save*, _rest* functions in .sfpr.
5675    If STUB_SEC is non-null, define alias symbols in STUB_SEC
5676    instead.  */
5677
5678 static bfd_boolean
5679 sfpr_define (struct bfd_link_info *info,
5680              const struct sfpr_def_parms *parm,
5681              asection *stub_sec)
5682 {
5683   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5684   unsigned int i;
5685   size_t len = strlen (parm->name);
5686   bfd_boolean writing = FALSE;
5687   char sym[16];
5688
5689   if (htab == NULL)
5690     return FALSE;
5691
5692   memcpy (sym, parm->name, len);
5693   sym[len + 2] = 0;
5694
5695   for (i = parm->lo; i <= parm->hi; i++)
5696     {
5697       struct ppc_link_hash_entry *h;
5698
5699       sym[len + 0] = i / 10 + '0';
5700       sym[len + 1] = i % 10 + '0';
5701       h = (struct ppc_link_hash_entry *)
5702         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5703       if (stub_sec != NULL)
5704         {
5705           if (h != NULL
5706               && h->elf.root.type == bfd_link_hash_defined
5707               && h->elf.root.u.def.section == htab->sfpr)
5708             {
5709               struct elf_link_hash_entry *s;
5710               char buf[32];
5711               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5712               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5713               if (s == NULL)
5714                 return FALSE;
5715               if (s->root.type == bfd_link_hash_new
5716                   || (s->root.type = bfd_link_hash_defined
5717                       && s->root.u.def.section == stub_sec))
5718                 {
5719                   s->root.type = bfd_link_hash_defined;
5720                   s->root.u.def.section = stub_sec;
5721                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5722                                          + h->elf.root.u.def.value);
5723                   s->ref_regular = 1;
5724                   s->def_regular = 1;
5725                   s->ref_regular_nonweak = 1;
5726                   s->forced_local = 1;
5727                   s->non_elf = 0;
5728                   s->root.linker_def = 1;
5729                 }
5730             }
5731           continue;
5732         }
5733       if (h != NULL)
5734         {
5735           h->save_res = 1;
5736           if (!h->elf.def_regular)
5737             {
5738               h->elf.root.type = bfd_link_hash_defined;
5739               h->elf.root.u.def.section = htab->sfpr;
5740               h->elf.root.u.def.value = htab->sfpr->size;
5741               h->elf.type = STT_FUNC;
5742               h->elf.def_regular = 1;
5743               h->elf.non_elf = 0;
5744               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5745               writing = TRUE;
5746               if (htab->sfpr->contents == NULL)
5747                 {
5748                   htab->sfpr->contents
5749                     = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5750                   if (htab->sfpr->contents == NULL)
5751                     return FALSE;
5752                 }
5753             }
5754         }
5755       if (writing)
5756         {
5757           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5758           if (i != parm->hi)
5759             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5760           else
5761             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5762           htab->sfpr->size = p - htab->sfpr->contents;
5763         }
5764     }
5765
5766   return TRUE;
5767 }
5768
5769 static bfd_byte *
5770 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5771 {
5772   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5773   return p + 4;
5774 }
5775
5776 static bfd_byte *
5777 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5778 {
5779   p = savegpr0 (abfd, p, r);
5780   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5781   p = p + 4;
5782   bfd_put_32 (abfd, BLR, p);
5783   return p + 4;
5784 }
5785
5786 static bfd_byte *
5787 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5788 {
5789   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5790   return p + 4;
5791 }
5792
5793 static bfd_byte *
5794 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5795 {
5796   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5797   p = p + 4;
5798   p = restgpr0 (abfd, p, r);
5799   bfd_put_32 (abfd, MTLR_R0, p);
5800   p = p + 4;
5801   if (r == 29)
5802     {
5803       p = restgpr0 (abfd, p, 30);
5804       p = restgpr0 (abfd, p, 31);
5805     }
5806   bfd_put_32 (abfd, BLR, p);
5807   return p + 4;
5808 }
5809
5810 static bfd_byte *
5811 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5812 {
5813   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5814   return p + 4;
5815 }
5816
5817 static bfd_byte *
5818 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5819 {
5820   p = savegpr1 (abfd, p, r);
5821   bfd_put_32 (abfd, BLR, p);
5822   return p + 4;
5823 }
5824
5825 static bfd_byte *
5826 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5827 {
5828   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5829   return p + 4;
5830 }
5831
5832 static bfd_byte *
5833 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5834 {
5835   p = restgpr1 (abfd, p, r);
5836   bfd_put_32 (abfd, BLR, p);
5837   return p + 4;
5838 }
5839
5840 static bfd_byte *
5841 savefpr (bfd *abfd, bfd_byte *p, int r)
5842 {
5843   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5844   return p + 4;
5845 }
5846
5847 static bfd_byte *
5848 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5849 {
5850   p = savefpr (abfd, p, r);
5851   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5852   p = p + 4;
5853   bfd_put_32 (abfd, BLR, p);
5854   return p + 4;
5855 }
5856
5857 static bfd_byte *
5858 restfpr (bfd *abfd, bfd_byte *p, int r)
5859 {
5860   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5861   return p + 4;
5862 }
5863
5864 static bfd_byte *
5865 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5866 {
5867   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5868   p = p + 4;
5869   p = restfpr (abfd, p, r);
5870   bfd_put_32 (abfd, MTLR_R0, p);
5871   p = p + 4;
5872   if (r == 29)
5873     {
5874       p = restfpr (abfd, p, 30);
5875       p = restfpr (abfd, p, 31);
5876     }
5877   bfd_put_32 (abfd, BLR, p);
5878   return p + 4;
5879 }
5880
5881 static bfd_byte *
5882 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5883 {
5884   p = savefpr (abfd, p, r);
5885   bfd_put_32 (abfd, BLR, p);
5886   return p + 4;
5887 }
5888
5889 static bfd_byte *
5890 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5891 {
5892   p = restfpr (abfd, p, r);
5893   bfd_put_32 (abfd, BLR, p);
5894   return p + 4;
5895 }
5896
5897 static bfd_byte *
5898 savevr (bfd *abfd, bfd_byte *p, int r)
5899 {
5900   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5901   p = p + 4;
5902   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5903   return p + 4;
5904 }
5905
5906 static bfd_byte *
5907 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5908 {
5909   p = savevr (abfd, p, r);
5910   bfd_put_32 (abfd, BLR, p);
5911   return p + 4;
5912 }
5913
5914 static bfd_byte *
5915 restvr (bfd *abfd, bfd_byte *p, int r)
5916 {
5917   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5918   p = p + 4;
5919   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5920   return p + 4;
5921 }
5922
5923 static bfd_byte *
5924 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5925 {
5926   p = restvr (abfd, p, r);
5927   bfd_put_32 (abfd, BLR, p);
5928   return p + 4;
5929 }
5930
5931 /* Called via elf_link_hash_traverse to transfer dynamic linking
5932    information on function code symbol entries to their corresponding
5933    function descriptor symbol entries.  */
5934
5935 static bfd_boolean
5936 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5937 {
5938   struct bfd_link_info *info;
5939   struct ppc_link_hash_table *htab;
5940   struct ppc_link_hash_entry *fh;
5941   struct ppc_link_hash_entry *fdh;
5942   bfd_boolean force_local;
5943
5944   fh = (struct ppc_link_hash_entry *) h;
5945   if (fh->elf.root.type == bfd_link_hash_indirect)
5946     return TRUE;
5947
5948   if (!fh->is_func)
5949     return TRUE;
5950
5951   if (fh->elf.root.root.string[0] != '.'
5952       || fh->elf.root.root.string[1] == '\0')
5953     return TRUE;
5954
5955   info = inf;
5956   htab = ppc_hash_table (info);
5957   if (htab == NULL)
5958     return FALSE;
5959
5960   /* Find the corresponding function descriptor symbol.  */
5961   fdh = lookup_fdh (fh, htab);
5962
5963   /* Resolve undefined references to dot-symbols as the value
5964      in the function descriptor, if we have one in a regular object.
5965      This is to satisfy cases like ".quad .foo".  Calls to functions
5966      in dynamic objects are handled elsewhere.  */
5967   if ((fh->elf.root.type == bfd_link_hash_undefined
5968        || fh->elf.root.type == bfd_link_hash_undefweak)
5969       && (fdh->elf.root.type == bfd_link_hash_defined
5970           || fdh->elf.root.type == bfd_link_hash_defweak)
5971       && get_opd_info (fdh->elf.root.u.def.section) != NULL
5972       && opd_entry_value (fdh->elf.root.u.def.section,
5973                           fdh->elf.root.u.def.value,
5974                           &fh->elf.root.u.def.section,
5975                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5976     {
5977       fh->elf.root.type = fdh->elf.root.type;
5978       fh->elf.forced_local = 1;
5979       fh->elf.def_regular = fdh->elf.def_regular;
5980       fh->elf.def_dynamic = fdh->elf.def_dynamic;
5981     }
5982
5983   if (!fh->elf.dynamic)
5984     {
5985       struct plt_entry *ent;
5986
5987       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5988         if (ent->plt.refcount > 0)
5989           break;
5990       if (ent == NULL)
5991         return TRUE;
5992     }
5993
5994   /* Create a descriptor as undefined if necessary.  */
5995   if (fdh == NULL
5996       && !bfd_link_executable (info)
5997       && (fh->elf.root.type == bfd_link_hash_undefined
5998           || fh->elf.root.type == bfd_link_hash_undefweak))
5999     {
6000       fdh = make_fdh (info, fh);
6001       if (fdh == NULL)
6002         return FALSE;
6003     }
6004
6005   /* We can't support overriding of symbols on a fake descriptor.  */
6006   if (fdh != NULL
6007       && fdh->fake
6008       && (fh->elf.root.type == bfd_link_hash_defined
6009           || fh->elf.root.type == bfd_link_hash_defweak))
6010     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6011
6012   /* Transfer dynamic linking information to the function descriptor.  */
6013   if (fdh != NULL)
6014     {
6015       fdh->elf.ref_regular |= fh->elf.ref_regular;
6016       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6017       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6018       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6019       fdh->elf.dynamic |= fh->elf.dynamic;
6020       fdh->elf.needs_plt |= (fh->elf.needs_plt
6021                              || fh->elf.type == STT_FUNC
6022                              || fh->elf.type == STT_GNU_IFUNC);
6023       move_plt_plist (fh, fdh);
6024
6025       if (!fdh->elf.forced_local
6026           && fh->elf.dynindx != -1)
6027         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6028           return FALSE;
6029     }
6030
6031   /* Now that the info is on the function descriptor, clear the
6032      function code sym info.  Any function code syms for which we
6033      don't have a definition in a regular file, we force local.
6034      This prevents a shared library from exporting syms that have
6035      been imported from another library.  Function code syms that
6036      are really in the library we must leave global to prevent the
6037      linker dragging in a definition from a static library.  */
6038   force_local = (!fh->elf.def_regular
6039                  || fdh == NULL
6040                  || !fdh->elf.def_regular
6041                  || fdh->elf.forced_local);
6042   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6043
6044   return TRUE;
6045 }
6046
6047 static const struct sfpr_def_parms save_res_funcs[] =
6048   {
6049     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6050     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6051     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6052     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6053     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6054     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6055     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6056     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6057     { "._savef", 14, 31, savefpr, savefpr1_tail },
6058     { "._restf", 14, 31, restfpr, restfpr1_tail },
6059     { "_savevr_", 20, 31, savevr, savevr_tail },
6060     { "_restvr_", 20, 31, restvr, restvr_tail }
6061   };
6062
6063 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6064    this hook to a) provide some gcc support functions, and b) transfer
6065    dynamic linking information gathered so far on function code symbol
6066    entries, to their corresponding function descriptor symbol entries.  */
6067
6068 static bfd_boolean
6069 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6070                             struct bfd_link_info *info)
6071 {
6072   struct ppc_link_hash_table *htab;
6073
6074   htab = ppc_hash_table (info);
6075   if (htab == NULL)
6076     return FALSE;
6077
6078   /* Provide any missing _save* and _rest* functions.  */
6079   if (htab->sfpr != NULL)
6080     {
6081       unsigned int i;
6082
6083       htab->sfpr->size = 0;
6084       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6085         if (!sfpr_define (info, &save_res_funcs[i], NULL))
6086           return FALSE;
6087       if (htab->sfpr->size == 0)
6088         htab->sfpr->flags |= SEC_EXCLUDE;
6089     }
6090
6091   if (bfd_link_relocatable (info))
6092     return TRUE;
6093
6094   if (htab->elf.hgot != NULL)
6095     {
6096       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6097       /* Make .TOC. defined so as to prevent it being made dynamic.
6098          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6099       if (!htab->elf.hgot->def_regular
6100           || htab->elf.hgot->root.type != bfd_link_hash_defined)
6101         {
6102           htab->elf.hgot->root.type = bfd_link_hash_defined;
6103           htab->elf.hgot->root.u.def.value = 0;
6104           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6105           htab->elf.hgot->def_regular = 1;
6106           htab->elf.hgot->root.linker_def = 1;
6107         }
6108       htab->elf.hgot->type = STT_OBJECT;
6109       htab->elf.hgot->other
6110         = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6111     }
6112
6113   if (htab->need_func_desc_adj)
6114     {
6115       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6116       htab->need_func_desc_adj = 0;
6117     }
6118
6119   return TRUE;
6120 }
6121
6122 /* Find dynamic relocs for H that apply to read-only sections.  */
6123
6124 static asection *
6125 readonly_dynrelocs (struct elf_link_hash_entry *h)
6126 {
6127   struct ppc_link_hash_entry *eh;
6128   struct elf_dyn_relocs *p;
6129
6130   eh = (struct ppc_link_hash_entry *) h;
6131   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6132     {
6133       asection *s = p->sec->output_section;
6134
6135       if (s != NULL && (s->flags & SEC_READONLY) != 0)
6136         return p->sec;
6137     }
6138   return NULL;
6139 }
6140
6141 /* Return true if we have dynamic relocs against H or any of its weak
6142    aliases, that apply to read-only sections.  Cannot be used after
6143    size_dynamic_sections.  */
6144
6145 static bfd_boolean
6146 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6147 {
6148   struct ppc_link_hash_entry *eh;
6149
6150   eh = (struct ppc_link_hash_entry *) h;
6151   do
6152     {
6153       if (readonly_dynrelocs (&eh->elf))
6154         return TRUE;
6155       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6156     }
6157   while (eh != NULL && &eh->elf != h);
6158
6159   return FALSE;
6160 }
6161
6162 /* Return whether EH has pc-relative dynamic relocs.  */
6163
6164 static bfd_boolean
6165 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6166 {
6167   struct elf_dyn_relocs *p;
6168
6169   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6170     if (p->pc_count != 0)
6171       return TRUE;
6172   return FALSE;
6173 }
6174
6175 /* Return true if a global entry stub will be created for H.  Valid
6176    for ELFv2 before plt entries have been allocated.  */
6177
6178 static bfd_boolean
6179 global_entry_stub (struct elf_link_hash_entry *h)
6180 {
6181   struct plt_entry *pent;
6182
6183   if (!h->pointer_equality_needed
6184       || h->def_regular)
6185     return FALSE;
6186
6187   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6188     if (pent->plt.refcount > 0
6189         && pent->addend == 0)
6190       return TRUE;
6191
6192   return FALSE;
6193 }
6194
6195 /* Adjust a symbol defined by a dynamic object and referenced by a
6196    regular object.  The current definition is in some section of the
6197    dynamic object, but we're not including those sections.  We have to
6198    change the definition to something the rest of the link can
6199    understand.  */
6200
6201 static bfd_boolean
6202 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6203                                  struct elf_link_hash_entry *h)
6204 {
6205   struct ppc_link_hash_table *htab;
6206   asection *s, *srel;
6207
6208   htab = ppc_hash_table (info);
6209   if (htab == NULL)
6210     return FALSE;
6211
6212   /* Deal with function syms.  */
6213   if (h->type == STT_FUNC
6214       || h->type == STT_GNU_IFUNC
6215       || h->needs_plt)
6216     {
6217       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6218                            || SYMBOL_CALLS_LOCAL (info, h)
6219                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6220       /* Discard dyn_relocs when non-pic if we've decided that a
6221          function symbol is local and not an ifunc.  We keep dynamic
6222          relocs for ifuncs when local rather than always emitting a
6223          plt call stub for them and defining the symbol on the call
6224          stub.  We can't do that for ELFv1 anyway (a function symbol
6225          is defined on a descriptor, not code) and it can be faster at
6226          run-time due to not needing to bounce through a stub.  The
6227          dyn_relocs for ifuncs will be applied even in a static
6228          executable.  */
6229       if (!bfd_link_pic (info)
6230           && h->type != STT_GNU_IFUNC
6231           && local)
6232         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6233
6234       /* Clear procedure linkage table information for any symbol that
6235          won't need a .plt entry.  */
6236       struct plt_entry *ent;
6237       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6238         if (ent->plt.refcount > 0)
6239           break;
6240       if (ent == NULL
6241           || (h->type != STT_GNU_IFUNC
6242               && local
6243               && (htab->can_convert_all_inline_plt
6244                   || (((struct ppc_link_hash_entry *) h)->tls_mask
6245                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6246         {
6247           h->plt.plist = NULL;
6248           h->needs_plt = 0;
6249           h->pointer_equality_needed = 0;
6250         }
6251       else if (abiversion (info->output_bfd) >= 2)
6252         {
6253           /* Taking a function's address in a read/write section
6254              doesn't require us to define the function symbol in the
6255              executable on a global entry stub.  A dynamic reloc can
6256              be used instead.  The reason we prefer a few more dynamic
6257              relocs is that calling via a global entry stub costs a
6258              few more instructions, and pointer_equality_needed causes
6259              extra work in ld.so when resolving these symbols.  */
6260           if (global_entry_stub (h))
6261             {
6262               if (!readonly_dynrelocs (h))
6263                 {
6264                   h->pointer_equality_needed = 0;
6265                   /* If we haven't seen a branch reloc and the symbol
6266                      isn't an ifunc then we don't need a plt entry.  */
6267                   if (!h->needs_plt)
6268                     h->plt.plist = NULL;
6269                 }
6270               else if (!bfd_link_pic (info))
6271                 /* We are going to be defining the function symbol on the
6272                    plt stub, so no dyn_relocs needed when non-pic.  */
6273                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6274             }
6275
6276           /* ELFv2 function symbols can't have copy relocs.  */
6277           return TRUE;
6278         }
6279       else if (!h->needs_plt
6280                && !readonly_dynrelocs (h))
6281         {
6282           /* If we haven't seen a branch reloc and the symbol isn't an
6283              ifunc then we don't need a plt entry.  */
6284           h->plt.plist = NULL;
6285           h->pointer_equality_needed = 0;
6286           return TRUE;
6287         }
6288     }
6289   else
6290     h->plt.plist = NULL;
6291
6292   /* If this is a weak symbol, and there is a real definition, the
6293      processor independent code will have arranged for us to see the
6294      real definition first, and we can just use the same value.  */
6295   if (h->is_weakalias)
6296     {
6297       struct elf_link_hash_entry *def = weakdef (h);
6298       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6299       h->root.u.def.section = def->root.u.def.section;
6300       h->root.u.def.value = def->root.u.def.value;
6301       if (def->root.u.def.section == htab->elf.sdynbss
6302           || def->root.u.def.section == htab->elf.sdynrelro)
6303         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6304       return TRUE;
6305     }
6306
6307   /* If we are creating a shared library, we must presume that the
6308      only references to the symbol are via the global offset table.
6309      For such cases we need not do anything here; the relocations will
6310      be handled correctly by relocate_section.  */
6311   if (bfd_link_pic (info))
6312     return TRUE;
6313
6314   /* If there are no references to this symbol that do not use the
6315      GOT, we don't need to generate a copy reloc.  */
6316   if (!h->non_got_ref)
6317     return TRUE;
6318
6319   /* Don't generate a copy reloc for symbols defined in the executable.  */
6320   if (!h->def_dynamic || !h->ref_regular || h->def_regular
6321
6322       /* If -z nocopyreloc was given, don't generate them either.  */
6323       || info->nocopyreloc
6324
6325       /* If we don't find any dynamic relocs in read-only sections, then
6326          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6327       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6328
6329       /* Protected variables do not work with .dynbss.  The copy in
6330          .dynbss won't be used by the shared library with the protected
6331          definition for the variable.  Text relocations are preferable
6332          to an incorrect program.  */
6333       || h->protected_def)
6334     return TRUE;
6335
6336   if (h->plt.plist != NULL)
6337     {
6338       /* We should never get here, but unfortunately there are versions
6339          of gcc out there that improperly (for this ABI) put initialized
6340          function pointers, vtable refs and suchlike in read-only
6341          sections.  Allow them to proceed, but warn that this might
6342          break at runtime.  */
6343       info->callbacks->einfo
6344         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6345            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6346          h->root.root.string);
6347     }
6348
6349   /* This is a reference to a symbol defined by a dynamic object which
6350      is not a function.  */
6351
6352   /* We must allocate the symbol in our .dynbss section, which will
6353      become part of the .bss section of the executable.  There will be
6354      an entry for this symbol in the .dynsym section.  The dynamic
6355      object will contain position independent code, so all references
6356      from the dynamic object to this symbol will go through the global
6357      offset table.  The dynamic linker will use the .dynsym entry to
6358      determine the address it must put in the global offset table, so
6359      both the dynamic object and the regular object will refer to the
6360      same memory location for the variable.  */
6361   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6362     {
6363       s = htab->elf.sdynrelro;
6364       srel = htab->elf.sreldynrelro;
6365     }
6366   else
6367     {
6368       s = htab->elf.sdynbss;
6369       srel = htab->elf.srelbss;
6370     }
6371   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6372     {
6373       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6374          linker to copy the initial value out of the dynamic object
6375          and into the runtime process image.  */
6376       srel->size += sizeof (Elf64_External_Rela);
6377       h->needs_copy = 1;
6378     }
6379
6380   /* We no longer want dyn_relocs.  */
6381   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6382   return _bfd_elf_adjust_dynamic_copy (info, h, s);
6383 }
6384
6385 /* If given a function descriptor symbol, hide both the function code
6386    sym and the descriptor.  */
6387 static void
6388 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6389                        struct elf_link_hash_entry *h,
6390                        bfd_boolean force_local)
6391 {
6392   struct ppc_link_hash_entry *eh;
6393   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6394
6395   if (ppc_hash_table (info) == NULL)
6396     return;
6397
6398   eh = (struct ppc_link_hash_entry *) h;
6399   if (eh->is_func_descriptor)
6400     {
6401       struct ppc_link_hash_entry *fh = eh->oh;
6402
6403       if (fh == NULL)
6404         {
6405           const char *p, *q;
6406           struct elf_link_hash_table *htab = elf_hash_table (info);
6407           char save;
6408
6409           /* We aren't supposed to use alloca in BFD because on
6410              systems which do not have alloca the version in libiberty
6411              calls xmalloc, which might cause the program to crash
6412              when it runs out of memory.  This function doesn't have a
6413              return status, so there's no way to gracefully return an
6414              error.  So cheat.  We know that string[-1] can be safely
6415              accessed;  It's either a string in an ELF string table,
6416              or allocated in an objalloc structure.  */
6417
6418           p = eh->elf.root.root.string - 1;
6419           save = *p;
6420           *(char *) p = '.';
6421           fh = (struct ppc_link_hash_entry *)
6422             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6423           *(char *) p = save;
6424
6425           /* Unfortunately, if it so happens that the string we were
6426              looking for was allocated immediately before this string,
6427              then we overwrote the string terminator.  That's the only
6428              reason the lookup should fail.  */
6429           if (fh == NULL)
6430             {
6431               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6432               while (q >= eh->elf.root.root.string && *q == *p)
6433                 --q, --p;
6434               if (q < eh->elf.root.root.string && *p == '.')
6435                 fh = (struct ppc_link_hash_entry *)
6436                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6437             }
6438           if (fh != NULL)
6439             {
6440               eh->oh = fh;
6441               fh->oh = eh;
6442             }
6443         }
6444       if (fh != NULL)
6445         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6446     }
6447 }
6448
6449 static bfd_boolean
6450 get_sym_h (struct elf_link_hash_entry **hp,
6451            Elf_Internal_Sym **symp,
6452            asection **symsecp,
6453            unsigned char **tls_maskp,
6454            Elf_Internal_Sym **locsymsp,
6455            unsigned long r_symndx,
6456            bfd *ibfd)
6457 {
6458   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6459
6460   if (r_symndx >= symtab_hdr->sh_info)
6461     {
6462       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6463       struct elf_link_hash_entry *h;
6464
6465       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6466       h = elf_follow_link (h);
6467
6468       if (hp != NULL)
6469         *hp = h;
6470
6471       if (symp != NULL)
6472         *symp = NULL;
6473
6474       if (symsecp != NULL)
6475         {
6476           asection *symsec = NULL;
6477           if (h->root.type == bfd_link_hash_defined
6478               || h->root.type == bfd_link_hash_defweak)
6479             symsec = h->root.u.def.section;
6480           *symsecp = symsec;
6481         }
6482
6483       if (tls_maskp != NULL)
6484         {
6485           struct ppc_link_hash_entry *eh;
6486
6487           eh = (struct ppc_link_hash_entry *) h;
6488           *tls_maskp = &eh->tls_mask;
6489         }
6490     }
6491   else
6492     {
6493       Elf_Internal_Sym *sym;
6494       Elf_Internal_Sym *locsyms = *locsymsp;
6495
6496       if (locsyms == NULL)
6497         {
6498           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6499           if (locsyms == NULL)
6500             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6501                                             symtab_hdr->sh_info,
6502                                             0, NULL, NULL, NULL);
6503           if (locsyms == NULL)
6504             return FALSE;
6505           *locsymsp = locsyms;
6506         }
6507       sym = locsyms + r_symndx;
6508
6509       if (hp != NULL)
6510         *hp = NULL;
6511
6512       if (symp != NULL)
6513         *symp = sym;
6514
6515       if (symsecp != NULL)
6516         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6517
6518       if (tls_maskp != NULL)
6519         {
6520           struct got_entry **lgot_ents;
6521           unsigned char *tls_mask;
6522
6523           tls_mask = NULL;
6524           lgot_ents = elf_local_got_ents (ibfd);
6525           if (lgot_ents != NULL)
6526             {
6527               struct plt_entry **local_plt = (struct plt_entry **)
6528                 (lgot_ents + symtab_hdr->sh_info);
6529               unsigned char *lgot_masks = (unsigned char *)
6530                 (local_plt + symtab_hdr->sh_info);
6531               tls_mask = &lgot_masks[r_symndx];
6532             }
6533           *tls_maskp = tls_mask;
6534         }
6535     }
6536   return TRUE;
6537 }
6538
6539 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6540    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6541    type suitable for optimization, and 1 otherwise.  */
6542
6543 static int
6544 get_tls_mask (unsigned char **tls_maskp,
6545               unsigned long *toc_symndx,
6546               bfd_vma *toc_addend,
6547               Elf_Internal_Sym **locsymsp,
6548               const Elf_Internal_Rela *rel,
6549               bfd *ibfd)
6550 {
6551   unsigned long r_symndx;
6552   int next_r;
6553   struct elf_link_hash_entry *h;
6554   Elf_Internal_Sym *sym;
6555   asection *sec;
6556   bfd_vma off;
6557
6558   r_symndx = ELF64_R_SYM (rel->r_info);
6559   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6560     return 0;
6561
6562   if ((*tls_maskp != NULL
6563        && (**tls_maskp & TLS_TLS) != 0
6564        && **tls_maskp != (TLS_TLS | TLS_MARK))
6565       || sec == NULL
6566       || ppc64_elf_section_data (sec) == NULL
6567       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6568     return 1;
6569
6570   /* Look inside a TOC section too.  */
6571   if (h != NULL)
6572     {
6573       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6574       off = h->root.u.def.value;
6575     }
6576   else
6577     off = sym->st_value;
6578   off += rel->r_addend;
6579   BFD_ASSERT (off % 8 == 0);
6580   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6581   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6582   if (toc_symndx != NULL)
6583     *toc_symndx = r_symndx;
6584   if (toc_addend != NULL)
6585     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6586   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6587     return 0;
6588   if ((h == NULL || is_static_defined (h))
6589       && (next_r == -1 || next_r == -2))
6590     return 1 - next_r;
6591   return 1;
6592 }
6593
6594 /* Find (or create) an entry in the tocsave hash table.  */
6595
6596 static struct tocsave_entry *
6597 tocsave_find (struct ppc_link_hash_table *htab,
6598               enum insert_option insert,
6599               Elf_Internal_Sym **local_syms,
6600               const Elf_Internal_Rela *irela,
6601               bfd *ibfd)
6602 {
6603   unsigned long r_indx;
6604   struct elf_link_hash_entry *h;
6605   Elf_Internal_Sym *sym;
6606   struct tocsave_entry ent, *p;
6607   hashval_t hash;
6608   struct tocsave_entry **slot;
6609
6610   r_indx = ELF64_R_SYM (irela->r_info);
6611   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6612     return NULL;
6613   if (ent.sec == NULL || ent.sec->output_section == NULL)
6614     {
6615       _bfd_error_handler
6616         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6617       return NULL;
6618     }
6619
6620   if (h != NULL)
6621     ent.offset = h->root.u.def.value;
6622   else
6623     ent.offset = sym->st_value;
6624   ent.offset += irela->r_addend;
6625
6626   hash = tocsave_htab_hash (&ent);
6627   slot = ((struct tocsave_entry **)
6628           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6629   if (slot == NULL)
6630     return NULL;
6631
6632   if (*slot == NULL)
6633     {
6634       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6635       if (p == NULL)
6636         return NULL;
6637       *p = ent;
6638       *slot = p;
6639     }
6640   return *slot;
6641 }
6642
6643 /* Adjust all global syms defined in opd sections.  In gcc generated
6644    code for the old ABI, these will already have been done.  */
6645
6646 static bfd_boolean
6647 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6648 {
6649   struct ppc_link_hash_entry *eh;
6650   asection *sym_sec;
6651   struct _opd_sec_data *opd;
6652
6653   if (h->root.type == bfd_link_hash_indirect)
6654     return TRUE;
6655
6656   if (h->root.type != bfd_link_hash_defined
6657       && h->root.type != bfd_link_hash_defweak)
6658     return TRUE;
6659
6660   eh = (struct ppc_link_hash_entry *) h;
6661   if (eh->adjust_done)
6662     return TRUE;
6663
6664   sym_sec = eh->elf.root.u.def.section;
6665   opd = get_opd_info (sym_sec);
6666   if (opd != NULL && opd->adjust != NULL)
6667     {
6668       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6669       if (adjust == -1)
6670         {
6671           /* This entry has been deleted.  */
6672           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6673           if (dsec == NULL)
6674             {
6675               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6676                 if (discarded_section (dsec))
6677                   {
6678                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6679                     break;
6680                   }
6681             }
6682           eh->elf.root.u.def.value = 0;
6683           eh->elf.root.u.def.section = dsec;
6684         }
6685       else
6686         eh->elf.root.u.def.value += adjust;
6687       eh->adjust_done = 1;
6688     }
6689   return TRUE;
6690 }
6691
6692 /* Handles decrementing dynamic reloc counts for the reloc specified by
6693    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
6694    have already been determined.  */
6695
6696 static bfd_boolean
6697 dec_dynrel_count (bfd_vma r_info,
6698                   asection *sec,
6699                   struct bfd_link_info *info,
6700                   Elf_Internal_Sym **local_syms,
6701                   struct elf_link_hash_entry *h,
6702                   Elf_Internal_Sym *sym)
6703 {
6704   enum elf_ppc64_reloc_type r_type;
6705   asection *sym_sec = NULL;
6706
6707   /* Can this reloc be dynamic?  This switch, and later tests here
6708      should be kept in sync with the code in check_relocs.  */
6709   r_type = ELF64_R_TYPE (r_info);
6710   switch (r_type)
6711     {
6712     default:
6713       return TRUE;
6714
6715     case R_PPC64_TPREL16:
6716     case R_PPC64_TPREL16_LO:
6717     case R_PPC64_TPREL16_HI:
6718     case R_PPC64_TPREL16_HA:
6719     case R_PPC64_TPREL16_DS:
6720     case R_PPC64_TPREL16_LO_DS:
6721     case R_PPC64_TPREL16_HIGH:
6722     case R_PPC64_TPREL16_HIGHA:
6723     case R_PPC64_TPREL16_HIGHER:
6724     case R_PPC64_TPREL16_HIGHERA:
6725     case R_PPC64_TPREL16_HIGHEST:
6726     case R_PPC64_TPREL16_HIGHESTA:
6727     case R_PPC64_TPREL64:
6728     case R_PPC64_DTPMOD64:
6729     case R_PPC64_DTPREL64:
6730     case R_PPC64_ADDR64:
6731     case R_PPC64_REL30:
6732     case R_PPC64_REL32:
6733     case R_PPC64_REL64:
6734     case R_PPC64_ADDR14:
6735     case R_PPC64_ADDR14_BRNTAKEN:
6736     case R_PPC64_ADDR14_BRTAKEN:
6737     case R_PPC64_ADDR16:
6738     case R_PPC64_ADDR16_DS:
6739     case R_PPC64_ADDR16_HA:
6740     case R_PPC64_ADDR16_HI:
6741     case R_PPC64_ADDR16_HIGH:
6742     case R_PPC64_ADDR16_HIGHA:
6743     case R_PPC64_ADDR16_HIGHER:
6744     case R_PPC64_ADDR16_HIGHERA:
6745     case R_PPC64_ADDR16_HIGHEST:
6746     case R_PPC64_ADDR16_HIGHESTA:
6747     case R_PPC64_ADDR16_LO:
6748     case R_PPC64_ADDR16_LO_DS:
6749     case R_PPC64_ADDR24:
6750     case R_PPC64_ADDR32:
6751     case R_PPC64_UADDR16:
6752     case R_PPC64_UADDR32:
6753     case R_PPC64_UADDR64:
6754     case R_PPC64_TOC:
6755     case R_PPC64_D34:
6756     case R_PPC64_D34_LO:
6757     case R_PPC64_D34_HI30:
6758     case R_PPC64_D34_HA30:
6759     case R_PPC64_ADDR16_HIGHER34:
6760     case R_PPC64_ADDR16_HIGHERA34:
6761     case R_PPC64_ADDR16_HIGHEST34:
6762     case R_PPC64_ADDR16_HIGHESTA34:
6763     case R_PPC64_D28:
6764       break;
6765     }
6766
6767   if (local_syms != NULL)
6768     {
6769       unsigned long r_symndx;
6770       bfd *ibfd = sec->owner;
6771
6772       r_symndx = ELF64_R_SYM (r_info);
6773       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6774         return FALSE;
6775     }
6776
6777   if ((bfd_link_pic (info)
6778        && (must_be_dyn_reloc (info, r_type)
6779            || (h != NULL
6780                && (!SYMBOLIC_BIND (info, h)
6781                    || h->root.type == bfd_link_hash_defweak
6782                    || !h->def_regular))))
6783       || (ELIMINATE_COPY_RELOCS
6784           && !bfd_link_pic (info)
6785           && h != NULL
6786           && (h->root.type == bfd_link_hash_defweak
6787               || !h->def_regular)))
6788     ;
6789   else
6790     return TRUE;
6791
6792   if (h != NULL)
6793     {
6794       struct elf_dyn_relocs *p;
6795       struct elf_dyn_relocs **pp;
6796       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6797
6798       /* elf_gc_sweep may have already removed all dyn relocs associated
6799          with local syms for a given section.  Also, symbol flags are
6800          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
6801          report a dynreloc miscount.  */
6802       if (*pp == NULL && info->gc_sections)
6803         return TRUE;
6804
6805       while ((p = *pp) != NULL)
6806         {
6807           if (p->sec == sec)
6808             {
6809               if (!must_be_dyn_reloc (info, r_type))
6810                 p->pc_count -= 1;
6811               p->count -= 1;
6812               if (p->count == 0)
6813                 *pp = p->next;
6814               return TRUE;
6815             }
6816           pp = &p->next;
6817         }
6818     }
6819   else
6820     {
6821       struct ppc_dyn_relocs *p;
6822       struct ppc_dyn_relocs **pp;
6823       void *vpp;
6824       bfd_boolean is_ifunc;
6825
6826       if (local_syms == NULL)
6827         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6828       if (sym_sec == NULL)
6829         sym_sec = sec;
6830
6831       vpp = &elf_section_data (sym_sec)->local_dynrel;
6832       pp = (struct ppc_dyn_relocs **) vpp;
6833
6834       if (*pp == NULL && info->gc_sections)
6835         return TRUE;
6836
6837       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6838       while ((p = *pp) != NULL)
6839         {
6840           if (p->sec == sec && p->ifunc == is_ifunc)
6841             {
6842               p->count -= 1;
6843               if (p->count == 0)
6844                 *pp = p->next;
6845               return TRUE;
6846             }
6847           pp = &p->next;
6848         }
6849     }
6850
6851   /* xgettext:c-format */
6852   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6853                       sec->owner, sec);
6854   bfd_set_error (bfd_error_bad_value);
6855   return FALSE;
6856 }
6857
6858 /* Remove unused Official Procedure Descriptor entries.  Currently we
6859    only remove those associated with functions in discarded link-once
6860    sections, or weakly defined functions that have been overridden.  It
6861    would be possible to remove many more entries for statically linked
6862    applications.  */
6863
6864 bfd_boolean
6865 ppc64_elf_edit_opd (struct bfd_link_info *info)
6866 {
6867   bfd *ibfd;
6868   bfd_boolean some_edited = FALSE;
6869   asection *need_pad = NULL;
6870   struct ppc_link_hash_table *htab;
6871
6872   htab = ppc_hash_table (info);
6873   if (htab == NULL)
6874     return FALSE;
6875
6876   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6877     {
6878       asection *sec;
6879       Elf_Internal_Rela *relstart, *rel, *relend;
6880       Elf_Internal_Shdr *symtab_hdr;
6881       Elf_Internal_Sym *local_syms;
6882       struct _opd_sec_data *opd;
6883       bfd_boolean need_edit, add_aux_fields, broken;
6884       bfd_size_type cnt_16b = 0;
6885
6886       if (!is_ppc64_elf (ibfd))
6887         continue;
6888
6889       sec = bfd_get_section_by_name (ibfd, ".opd");
6890       if (sec == NULL || sec->size == 0)
6891         continue;
6892
6893       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6894         continue;
6895
6896       if (sec->output_section == bfd_abs_section_ptr)
6897         continue;
6898
6899       /* Look through the section relocs.  */
6900       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6901         continue;
6902
6903       local_syms = NULL;
6904       symtab_hdr = &elf_symtab_hdr (ibfd);
6905
6906       /* Read the relocations.  */
6907       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6908                                             info->keep_memory);
6909       if (relstart == NULL)
6910         return FALSE;
6911
6912       /* First run through the relocs to check they are sane, and to
6913          determine whether we need to edit this opd section.  */
6914       need_edit = FALSE;
6915       broken = FALSE;
6916       need_pad = sec;
6917       relend = relstart + sec->reloc_count;
6918       for (rel = relstart; rel < relend; )
6919         {
6920           enum elf_ppc64_reloc_type r_type;
6921           unsigned long r_symndx;
6922           asection *sym_sec;
6923           struct elf_link_hash_entry *h;
6924           Elf_Internal_Sym *sym;
6925           bfd_vma offset;
6926
6927           /* .opd contains an array of 16 or 24 byte entries.  We're
6928              only interested in the reloc pointing to a function entry
6929              point.  */
6930           offset = rel->r_offset;
6931           if (rel + 1 == relend
6932               || rel[1].r_offset != offset + 8)
6933             {
6934               /* If someone messes with .opd alignment then after a
6935                  "ld -r" we might have padding in the middle of .opd.
6936                  Also, there's nothing to prevent someone putting
6937                  something silly in .opd with the assembler.  No .opd
6938                  optimization for them!  */
6939             broken_opd:
6940               _bfd_error_handler
6941                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6942               broken = TRUE;
6943               break;
6944             }
6945
6946           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6947               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6948             {
6949               _bfd_error_handler
6950                 /* xgettext:c-format */
6951                 (_("%pB: unexpected reloc type %u in .opd section"),
6952                  ibfd, r_type);
6953               broken = TRUE;
6954               break;
6955             }
6956
6957           r_symndx = ELF64_R_SYM (rel->r_info);
6958           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6959                           r_symndx, ibfd))
6960             goto error_ret;
6961
6962           if (sym_sec == NULL || sym_sec->owner == NULL)
6963             {
6964               const char *sym_name;
6965               if (h != NULL)
6966                 sym_name = h->root.root.string;
6967               else
6968                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6969                                              sym_sec);
6970
6971               _bfd_error_handler
6972                 /* xgettext:c-format */
6973                 (_("%pB: undefined sym `%s' in .opd section"),
6974                  ibfd, sym_name);
6975               broken = TRUE;
6976               break;
6977             }
6978
6979           /* opd entries are always for functions defined in the
6980              current input bfd.  If the symbol isn't defined in the
6981              input bfd, then we won't be using the function in this
6982              bfd;  It must be defined in a linkonce section in another
6983              bfd, or is weak.  It's also possible that we are
6984              discarding the function due to a linker script /DISCARD/,
6985              which we test for via the output_section.  */
6986           if (sym_sec->owner != ibfd
6987               || sym_sec->output_section == bfd_abs_section_ptr)
6988             need_edit = TRUE;
6989
6990           rel += 2;
6991           if (rel + 1 == relend
6992               || (rel + 2 < relend
6993                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6994             ++rel;
6995
6996           if (rel == relend)
6997             {
6998               if (sec->size == offset + 24)
6999                 {
7000                   need_pad = NULL;
7001                   break;
7002                 }
7003               if (sec->size == offset + 16)
7004                 {
7005                   cnt_16b++;
7006                   break;
7007                 }
7008               goto broken_opd;
7009             }
7010           else if (rel + 1 < relend
7011                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7012                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7013             {
7014               if (rel[0].r_offset == offset + 16)
7015                 cnt_16b++;
7016               else if (rel[0].r_offset != offset + 24)
7017                 goto broken_opd;
7018             }
7019           else
7020             goto broken_opd;
7021         }
7022
7023       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7024
7025       if (!broken && (need_edit || add_aux_fields))
7026         {
7027           Elf_Internal_Rela *write_rel;
7028           Elf_Internal_Shdr *rel_hdr;
7029           bfd_byte *rptr, *wptr;
7030           bfd_byte *new_contents;
7031           bfd_size_type amt;
7032
7033           new_contents = NULL;
7034           amt = OPD_NDX (sec->size) * sizeof (long);
7035           opd = &ppc64_elf_section_data (sec)->u.opd;
7036           opd->adjust = bfd_zalloc (sec->owner, amt);
7037           if (opd->adjust == NULL)
7038             return FALSE;
7039
7040           /* This seems a waste of time as input .opd sections are all
7041              zeros as generated by gcc, but I suppose there's no reason
7042              this will always be so.  We might start putting something in
7043              the third word of .opd entries.  */
7044           if ((sec->flags & SEC_IN_MEMORY) == 0)
7045             {
7046               bfd_byte *loc;
7047               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7048                 {
7049                   if (loc != NULL)
7050                     free (loc);
7051                 error_ret:
7052                   if (local_syms != NULL
7053                       && symtab_hdr->contents != (unsigned char *) local_syms)
7054                     free (local_syms);
7055                   if (elf_section_data (sec)->relocs != relstart)
7056                     free (relstart);
7057                   return FALSE;
7058                 }
7059               sec->contents = loc;
7060               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7061             }
7062
7063           elf_section_data (sec)->relocs = relstart;
7064
7065           new_contents = sec->contents;
7066           if (add_aux_fields)
7067             {
7068               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7069               if (new_contents == NULL)
7070                 return FALSE;
7071               need_pad = NULL;
7072             }
7073           wptr = new_contents;
7074           rptr = sec->contents;
7075           write_rel = relstart;
7076           for (rel = relstart; rel < relend; )
7077             {
7078               unsigned long r_symndx;
7079               asection *sym_sec;
7080               struct elf_link_hash_entry *h;
7081               struct ppc_link_hash_entry *fdh = NULL;
7082               Elf_Internal_Sym *sym;
7083               long opd_ent_size;
7084               Elf_Internal_Rela *next_rel;
7085               bfd_boolean skip;
7086
7087               r_symndx = ELF64_R_SYM (rel->r_info);
7088               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7089                               r_symndx, ibfd))
7090                 goto error_ret;
7091
7092               next_rel = rel + 2;
7093               if (next_rel + 1 == relend
7094                   || (next_rel + 2 < relend
7095                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7096                 ++next_rel;
7097
7098               /* See if the .opd entry is full 24 byte or
7099                  16 byte (with fd_aux entry overlapped with next
7100                  fd_func).  */
7101               opd_ent_size = 24;
7102               if (next_rel == relend)
7103                 {
7104                   if (sec->size == rel->r_offset + 16)
7105                     opd_ent_size = 16;
7106                 }
7107               else if (next_rel->r_offset == rel->r_offset + 16)
7108                 opd_ent_size = 16;
7109
7110               if (h != NULL
7111                   && h->root.root.string[0] == '.')
7112                 {
7113                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
7114                   if (fdh != NULL)
7115                     {
7116                       fdh = ppc_follow_link (fdh);
7117                       if (fdh->elf.root.type != bfd_link_hash_defined
7118                           && fdh->elf.root.type != bfd_link_hash_defweak)
7119                         fdh = NULL;
7120                     }
7121                 }
7122
7123               skip = (sym_sec->owner != ibfd
7124                       || sym_sec->output_section == bfd_abs_section_ptr);
7125               if (skip)
7126                 {
7127                   if (fdh != NULL && sym_sec->owner == ibfd)
7128                     {
7129                       /* Arrange for the function descriptor sym
7130                          to be dropped.  */
7131                       fdh->elf.root.u.def.value = 0;
7132                       fdh->elf.root.u.def.section = sym_sec;
7133                     }
7134                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7135
7136                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7137                     rel = next_rel;
7138                   else
7139                     while (1)
7140                       {
7141                         if (!dec_dynrel_count (rel->r_info, sec, info,
7142                                                NULL, h, sym))
7143                           goto error_ret;
7144
7145                         if (++rel == next_rel)
7146                           break;
7147
7148                         r_symndx = ELF64_R_SYM (rel->r_info);
7149                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7150                                         r_symndx, ibfd))
7151                           goto error_ret;
7152                       }
7153                 }
7154               else
7155                 {
7156                   /* We'll be keeping this opd entry.  */
7157                   long adjust;
7158
7159                   if (fdh != NULL)
7160                     {
7161                       /* Redefine the function descriptor symbol to
7162                          this location in the opd section.  It is
7163                          necessary to update the value here rather
7164                          than using an array of adjustments as we do
7165                          for local symbols, because various places
7166                          in the generic ELF code use the value
7167                          stored in u.def.value.  */
7168                       fdh->elf.root.u.def.value = wptr - new_contents;
7169                       fdh->adjust_done = 1;
7170                     }
7171
7172                   /* Local syms are a bit tricky.  We could
7173                      tweak them as they can be cached, but
7174                      we'd need to look through the local syms
7175                      for the function descriptor sym which we
7176                      don't have at the moment.  So keep an
7177                      array of adjustments.  */
7178                   adjust = (wptr - new_contents) - (rptr - sec->contents);
7179                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7180
7181                   if (wptr != rptr)
7182                     memcpy (wptr, rptr, opd_ent_size);
7183                   wptr += opd_ent_size;
7184                   if (add_aux_fields && opd_ent_size == 16)
7185                     {
7186                       memset (wptr, '\0', 8);
7187                       wptr += 8;
7188                     }
7189
7190                   /* We need to adjust any reloc offsets to point to the
7191                      new opd entries.  */
7192                   for ( ; rel != next_rel; ++rel)
7193                     {
7194                       rel->r_offset += adjust;
7195                       if (write_rel != rel)
7196                         memcpy (write_rel, rel, sizeof (*rel));
7197                       ++write_rel;
7198                     }
7199                 }
7200
7201               rptr += opd_ent_size;
7202             }
7203
7204           sec->size = wptr - new_contents;
7205           sec->reloc_count = write_rel - relstart;
7206           if (add_aux_fields)
7207             {
7208               free (sec->contents);
7209               sec->contents = new_contents;
7210             }
7211
7212           /* Fudge the header size too, as this is used later in
7213              elf_bfd_final_link if we are emitting relocs.  */
7214           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7215           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7216           some_edited = TRUE;
7217         }
7218       else if (elf_section_data (sec)->relocs != relstart)
7219         free (relstart);
7220
7221       if (local_syms != NULL
7222           && symtab_hdr->contents != (unsigned char *) local_syms)
7223         {
7224           if (!info->keep_memory)
7225             free (local_syms);
7226           else
7227             symtab_hdr->contents = (unsigned char *) local_syms;
7228         }
7229     }
7230
7231   if (some_edited)
7232     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7233
7234   /* If we are doing a final link and the last .opd entry is just 16 byte
7235      long, add a 8 byte padding after it.  */
7236   if (need_pad != NULL && !bfd_link_relocatable (info))
7237     {
7238       bfd_byte *p;
7239
7240       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7241         {
7242           BFD_ASSERT (need_pad->size > 0);
7243
7244           p = bfd_malloc (need_pad->size + 8);
7245           if (p == NULL)
7246             return FALSE;
7247
7248           if (!bfd_get_section_contents (need_pad->owner, need_pad,
7249                                          p, 0, need_pad->size))
7250             return FALSE;
7251
7252           need_pad->contents = p;
7253           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7254         }
7255       else
7256         {
7257           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7258           if (p == NULL)
7259             return FALSE;
7260
7261           need_pad->contents = p;
7262         }
7263
7264       memset (need_pad->contents + need_pad->size, 0, 8);
7265       need_pad->size += 8;
7266     }
7267
7268   return TRUE;
7269 }
7270
7271 /* Analyze inline PLT call relocations to see whether calls to locally
7272    defined functions can be converted to direct calls.  */
7273
7274 bfd_boolean
7275 ppc64_elf_inline_plt (struct bfd_link_info *info)
7276 {
7277   struct ppc_link_hash_table *htab;
7278   bfd *ibfd;
7279   asection *sec;
7280   bfd_vma low_vma, high_vma, limit;
7281
7282   htab = ppc_hash_table (info);
7283   if (htab == NULL)
7284     return FALSE;
7285
7286   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7287      reduced somewhat to cater for possible stubs that might be added
7288      between the call and its destination.  */
7289   if (htab->params->group_size < 0)
7290     {
7291       limit = -htab->params->group_size;
7292       if (limit == 1)
7293         limit = 0x1e00000;
7294     }
7295   else
7296     {
7297       limit = htab->params->group_size;
7298       if (limit == 1)
7299         limit = 0x1c00000;
7300     }
7301
7302   low_vma = -1;
7303   high_vma = 0;
7304   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7305     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7306       {
7307         if (low_vma > sec->vma)
7308           low_vma = sec->vma;
7309         if (high_vma < sec->vma + sec->size)
7310           high_vma = sec->vma + sec->size;
7311       }
7312
7313   /* If a "bl" can reach anywhere in local code sections, then we can
7314      convert all inline PLT sequences to direct calls when the symbol
7315      is local.  */
7316   if (high_vma - low_vma < limit)
7317     {
7318       htab->can_convert_all_inline_plt = 1;
7319       return TRUE;
7320     }
7321
7322   /* Otherwise, go looking through relocs for cases where a direct
7323      call won't reach.  Mark the symbol on any such reloc to disable
7324      the optimization and keep the PLT entry as it seems likely that
7325      this will be better than creating trampolines.  Note that this
7326      will disable the optimization for all inline PLT calls to a
7327      particular symbol, not just those that won't reach.  The
7328      difficulty in doing a more precise optimization is that the
7329      linker needs to make a decision depending on whether a
7330      particular R_PPC64_PLTCALL insn can be turned into a direct
7331      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7332      the sequence, and there is nothing that ties those relocs
7333      together except their symbol.  */
7334
7335   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7336     {
7337       Elf_Internal_Shdr *symtab_hdr;
7338       Elf_Internal_Sym *local_syms;
7339
7340       if (!is_ppc64_elf (ibfd))
7341         continue;
7342
7343       local_syms = NULL;
7344       symtab_hdr = &elf_symtab_hdr (ibfd);
7345
7346       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7347         if (ppc64_elf_section_data (sec)->has_pltcall
7348             && !bfd_is_abs_section (sec->output_section))
7349           {
7350             Elf_Internal_Rela *relstart, *rel, *relend;
7351
7352             /* Read the relocations.  */
7353             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7354                                                   info->keep_memory);
7355             if (relstart == NULL)
7356               return FALSE;
7357
7358             relend = relstart + sec->reloc_count;
7359             for (rel = relstart; rel < relend; )
7360               {
7361                 enum elf_ppc64_reloc_type r_type;
7362                 unsigned long r_symndx;
7363                 asection *sym_sec;
7364                 struct elf_link_hash_entry *h;
7365                 Elf_Internal_Sym *sym;
7366                 unsigned char *tls_maskp;
7367
7368                 r_type = ELF64_R_TYPE (rel->r_info);
7369                 if (r_type != R_PPC64_PLTCALL
7370                     && r_type != R_PPC64_PLTCALL_NOTOC)
7371                   continue;
7372
7373                 r_symndx = ELF64_R_SYM (rel->r_info);
7374                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7375                                 r_symndx, ibfd))
7376                   {
7377                     if (elf_section_data (sec)->relocs != relstart)
7378                       free (relstart);
7379                     if (local_syms != NULL
7380                         && symtab_hdr->contents != (bfd_byte *) local_syms)
7381                       free (local_syms);
7382                     return FALSE;
7383                   }
7384
7385                 if (sym_sec != NULL && sym_sec->output_section != NULL)
7386                   {
7387                     bfd_vma from, to;
7388                     if (h != NULL)
7389                       to = h->root.u.def.value;
7390                     else
7391                       to = sym->st_value;
7392                     to += (rel->r_addend
7393                            + sym_sec->output_offset
7394                            + sym_sec->output_section->vma);
7395                     from = (rel->r_offset
7396                             + sec->output_offset
7397                             + sec->output_section->vma);
7398                     if (to - from + limit < 2 * limit
7399                         && !(r_type == R_PPC64_PLTCALL_NOTOC
7400                              && (((h ? h->other : sym->st_other)
7401                                   & STO_PPC64_LOCAL_MASK)
7402                                  != 1 << STO_PPC64_LOCAL_BIT)))
7403                       *tls_maskp &= ~PLT_KEEP;
7404                   }
7405               }
7406             if (elf_section_data (sec)->relocs != relstart)
7407               free (relstart);
7408           }
7409
7410       if (local_syms != NULL
7411           && symtab_hdr->contents != (unsigned char *) local_syms)
7412         {
7413           if (!info->keep_memory)
7414             free (local_syms);
7415           else
7416             symtab_hdr->contents = (unsigned char *) local_syms;
7417         }
7418     }
7419
7420   return TRUE;
7421 }
7422
7423 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7424
7425 asection *
7426 ppc64_elf_tls_setup (struct bfd_link_info *info)
7427 {
7428   struct ppc_link_hash_table *htab;
7429
7430   htab = ppc_hash_table (info);
7431   if (htab == NULL)
7432     return NULL;
7433
7434   if (abiversion (info->output_bfd) == 1)
7435     htab->opd_abi = 1;
7436
7437   if (htab->params->no_multi_toc)
7438     htab->do_multi_toc = 0;
7439   else if (!htab->do_multi_toc)
7440     htab->params->no_multi_toc = 1;
7441
7442   /* Default to --no-plt-localentry, as this option can cause problems
7443      with symbol interposition.  For example, glibc libpthread.so and
7444      libc.so duplicate many pthread symbols, with a fallback
7445      implementation in libc.so.  In some cases the fallback does more
7446      work than the pthread implementation.  __pthread_condattr_destroy
7447      is one such symbol: the libpthread.so implementation is
7448      localentry:0 while the libc.so implementation is localentry:8.
7449      An app that "cleverly" uses dlopen to only load necessary
7450      libraries at runtime may omit loading libpthread.so when not
7451      running multi-threaded, which then results in the libc.so
7452      fallback symbols being used and ld.so complaining.  Now there
7453      are workarounds in ld (see non_zero_localentry) to detect the
7454      pthread situation, but that may not be the only case where
7455      --plt-localentry can cause trouble.  */
7456   if (htab->params->plt_localentry0 < 0)
7457     htab->params->plt_localentry0 = 0;
7458   if (htab->params->plt_localentry0
7459       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7460                                FALSE, FALSE, FALSE) == NULL)
7461     _bfd_error_handler
7462       (_("warning: --plt-localentry is especially dangerous without "
7463          "ld.so support to detect ABI violations"));
7464
7465   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7466                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7467                                               FALSE, FALSE, TRUE));
7468   /* Move dynamic linking info to the function descriptor sym.  */
7469   if (htab->tls_get_addr != NULL)
7470     func_desc_adjust (&htab->tls_get_addr->elf, info);
7471   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7472                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7473                                                  FALSE, FALSE, TRUE));
7474   if (htab->params->tls_get_addr_opt)
7475     {
7476       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7477
7478       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7479                                   FALSE, FALSE, TRUE);
7480       if (opt != NULL)
7481         func_desc_adjust (opt, info);
7482       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7483                                      FALSE, FALSE, TRUE);
7484       if (opt_fd != NULL
7485           && (opt_fd->root.type == bfd_link_hash_defined
7486               || opt_fd->root.type == bfd_link_hash_defweak))
7487         {
7488           /* If glibc supports an optimized __tls_get_addr call stub,
7489              signalled by the presence of __tls_get_addr_opt, and we'll
7490              be calling __tls_get_addr via a plt call stub, then
7491              make __tls_get_addr point to __tls_get_addr_opt.  */
7492           tga_fd = &htab->tls_get_addr_fd->elf;
7493           if (htab->elf.dynamic_sections_created
7494               && tga_fd != NULL
7495               && (tga_fd->type == STT_FUNC
7496                   || tga_fd->needs_plt)
7497               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7498                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7499             {
7500               struct plt_entry *ent;
7501
7502               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7503                 if (ent->plt.refcount > 0)
7504                   break;
7505               if (ent != NULL)
7506                 {
7507                   tga_fd->root.type = bfd_link_hash_indirect;
7508                   tga_fd->root.u.i.link = &opt_fd->root;
7509                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7510                   opt_fd->mark = 1;
7511                   if (opt_fd->dynindx != -1)
7512                     {
7513                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7514                       opt_fd->dynindx = -1;
7515                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7516                                               opt_fd->dynstr_index);
7517                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7518                         return NULL;
7519                     }
7520                   htab->tls_get_addr_fd
7521                     = (struct ppc_link_hash_entry *) opt_fd;
7522                   tga = &htab->tls_get_addr->elf;
7523                   if (opt != NULL && tga != NULL)
7524                     {
7525                       tga->root.type = bfd_link_hash_indirect;
7526                       tga->root.u.i.link = &opt->root;
7527                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7528                       opt->mark = 1;
7529                       _bfd_elf_link_hash_hide_symbol (info, opt,
7530                                                       tga->forced_local);
7531                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7532                     }
7533                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7534                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7535                   if (htab->tls_get_addr != NULL)
7536                     {
7537                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7538                       htab->tls_get_addr->is_func = 1;
7539                     }
7540                 }
7541             }
7542         }
7543       else if (htab->params->tls_get_addr_opt < 0)
7544         htab->params->tls_get_addr_opt = 0;
7545     }
7546   return _bfd_elf_tls_setup (info->output_bfd, info);
7547 }
7548
7549 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7550    HASH1 or HASH2.  */
7551
7552 static bfd_boolean
7553 branch_reloc_hash_match (const bfd *ibfd,
7554                          const Elf_Internal_Rela *rel,
7555                          const struct ppc_link_hash_entry *hash1,
7556                          const struct ppc_link_hash_entry *hash2)
7557 {
7558   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7559   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7560   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7561
7562   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7563     {
7564       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7565       struct elf_link_hash_entry *h;
7566
7567       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7568       h = elf_follow_link (h);
7569       if (h == &hash1->elf || h == &hash2->elf)
7570         return TRUE;
7571     }
7572   return FALSE;
7573 }
7574
7575 /* Run through all the TLS relocs looking for optimization
7576    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7577    a preliminary section layout so that we know the TLS segment
7578    offsets.  We can't optimize earlier because some optimizations need
7579    to know the tp offset, and we need to optimize before allocating
7580    dynamic relocations.  */
7581
7582 bfd_boolean
7583 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7584 {
7585   bfd *ibfd;
7586   asection *sec;
7587   struct ppc_link_hash_table *htab;
7588   unsigned char *toc_ref;
7589   int pass;
7590
7591   if (!bfd_link_executable (info))
7592     return TRUE;
7593
7594   htab = ppc_hash_table (info);
7595   if (htab == NULL)
7596     return FALSE;
7597
7598   /* Make two passes over the relocs.  On the first pass, mark toc
7599      entries involved with tls relocs, and check that tls relocs
7600      involved in setting up a tls_get_addr call are indeed followed by
7601      such a call.  If they are not, we can't do any tls optimization.
7602      On the second pass twiddle tls_mask flags to notify
7603      relocate_section that optimization can be done, and adjust got
7604      and plt refcounts.  */
7605   toc_ref = NULL;
7606   for (pass = 0; pass < 2; ++pass)
7607     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7608       {
7609         Elf_Internal_Sym *locsyms = NULL;
7610         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7611
7612         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7613           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7614             {
7615               Elf_Internal_Rela *relstart, *rel, *relend;
7616               bfd_boolean found_tls_get_addr_arg = 0;
7617
7618               /* Read the relocations.  */
7619               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7620                                                     info->keep_memory);
7621               if (relstart == NULL)
7622                 {
7623                   free (toc_ref);
7624                   return FALSE;
7625                 }
7626
7627               relend = relstart + sec->reloc_count;
7628               for (rel = relstart; rel < relend; rel++)
7629                 {
7630                   enum elf_ppc64_reloc_type r_type;
7631                   unsigned long r_symndx;
7632                   struct elf_link_hash_entry *h;
7633                   Elf_Internal_Sym *sym;
7634                   asection *sym_sec;
7635                   unsigned char *tls_mask;
7636                   unsigned char tls_set, tls_clear, tls_type = 0;
7637                   bfd_vma value;
7638                   bfd_boolean ok_tprel, is_local;
7639                   long toc_ref_index = 0;
7640                   int expecting_tls_get_addr = 0;
7641                   bfd_boolean ret = FALSE;
7642
7643                   r_symndx = ELF64_R_SYM (rel->r_info);
7644                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7645                                   r_symndx, ibfd))
7646                     {
7647                     err_free_rel:
7648                       if (elf_section_data (sec)->relocs != relstart)
7649                         free (relstart);
7650                       if (toc_ref != NULL)
7651                         free (toc_ref);
7652                       if (locsyms != NULL
7653                           && (elf_symtab_hdr (ibfd).contents
7654                               != (unsigned char *) locsyms))
7655                         free (locsyms);
7656                       return ret;
7657                     }
7658
7659                   if (h != NULL)
7660                     {
7661                       if (h->root.type == bfd_link_hash_defined
7662                           || h->root.type == bfd_link_hash_defweak)
7663                         value = h->root.u.def.value;
7664                       else if (h->root.type == bfd_link_hash_undefweak)
7665                         value = 0;
7666                       else
7667                         {
7668                           found_tls_get_addr_arg = 0;
7669                           continue;
7670                         }
7671                     }
7672                   else
7673                     /* Symbols referenced by TLS relocs must be of type
7674                        STT_TLS.  So no need for .opd local sym adjust.  */
7675                     value = sym->st_value;
7676
7677                   ok_tprel = FALSE;
7678                   is_local = FALSE;
7679                   if (h == NULL
7680                       || !h->def_dynamic)
7681                     {
7682                       is_local = TRUE;
7683                       if (h != NULL
7684                           && h->root.type == bfd_link_hash_undefweak)
7685                         ok_tprel = TRUE;
7686                       else if (sym_sec != NULL
7687                                && sym_sec->output_section != NULL)
7688                         {
7689                           value += sym_sec->output_offset;
7690                           value += sym_sec->output_section->vma;
7691                           value -= htab->elf.tls_sec->vma;
7692                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7693                                       < (bfd_vma) 1 << 32);
7694                         }
7695                     }
7696
7697                   r_type = ELF64_R_TYPE (rel->r_info);
7698                   /* If this section has old-style __tls_get_addr calls
7699                      without marker relocs, then check that each
7700                      __tls_get_addr call reloc is preceded by a reloc
7701                      that conceivably belongs to the __tls_get_addr arg
7702                      setup insn.  If we don't find matching arg setup
7703                      relocs, don't do any tls optimization.  */
7704                   if (pass == 0
7705                       && sec->has_tls_get_addr_call
7706                       && h != NULL
7707                       && (h == &htab->tls_get_addr->elf
7708                           || h == &htab->tls_get_addr_fd->elf)
7709                       && !found_tls_get_addr_arg
7710                       && is_branch_reloc (r_type))
7711                     {
7712                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7713                                                 "TLS optimization disabled\n"),
7714                                               ibfd, sec, rel->r_offset);
7715                       ret = TRUE;
7716                       goto err_free_rel;
7717                     }
7718
7719                   found_tls_get_addr_arg = 0;
7720                   switch (r_type)
7721                     {
7722                     case R_PPC64_GOT_TLSLD16:
7723                     case R_PPC64_GOT_TLSLD16_LO:
7724                       expecting_tls_get_addr = 1;
7725                       found_tls_get_addr_arg = 1;
7726                       /* Fall through.  */
7727
7728                     case R_PPC64_GOT_TLSLD16_HI:
7729                     case R_PPC64_GOT_TLSLD16_HA:
7730                       /* These relocs should never be against a symbol
7731                          defined in a shared lib.  Leave them alone if
7732                          that turns out to be the case.  */
7733                       if (!is_local)
7734                         continue;
7735
7736                       /* LD -> LE */
7737                       tls_set = 0;
7738                       tls_clear = TLS_LD;
7739                       tls_type = TLS_TLS | TLS_LD;
7740                       break;
7741
7742                     case R_PPC64_GOT_TLSGD16:
7743                     case R_PPC64_GOT_TLSGD16_LO:
7744                       expecting_tls_get_addr = 1;
7745                       found_tls_get_addr_arg = 1;
7746                       /* Fall through. */
7747
7748                     case R_PPC64_GOT_TLSGD16_HI:
7749                     case R_PPC64_GOT_TLSGD16_HA:
7750                       if (ok_tprel)
7751                         /* GD -> LE */
7752                         tls_set = 0;
7753                       else
7754                         /* GD -> IE */
7755                         tls_set = TLS_TLS | TLS_TPRELGD;
7756                       tls_clear = TLS_GD;
7757                       tls_type = TLS_TLS | TLS_GD;
7758                       break;
7759
7760                     case R_PPC64_GOT_TPREL16_DS:
7761                     case R_PPC64_GOT_TPREL16_LO_DS:
7762                     case R_PPC64_GOT_TPREL16_HI:
7763                     case R_PPC64_GOT_TPREL16_HA:
7764                       if (ok_tprel)
7765                         {
7766                           /* IE -> LE */
7767                           tls_set = 0;
7768                           tls_clear = TLS_TPREL;
7769                           tls_type = TLS_TLS | TLS_TPREL;
7770                           break;
7771                         }
7772                       continue;
7773
7774                     case R_PPC64_TLSGD:
7775                     case R_PPC64_TLSLD:
7776                       if (rel + 1 < relend
7777                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7778                         {
7779                           if (pass != 0
7780                               && (ELF64_R_TYPE (rel[1].r_info)
7781                                   != R_PPC64_PLTSEQ)
7782                               && (ELF64_R_TYPE (rel[1].r_info)
7783                                   != R_PPC64_PLTSEQ_NOTOC))
7784                             {
7785                               r_symndx = ELF64_R_SYM (rel[1].r_info);
7786                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7787                                               r_symndx, ibfd))
7788                                 goto err_free_rel;
7789                               if (h != NULL)
7790                                 {
7791                                   struct plt_entry *ent = NULL;
7792
7793                                   for (ent = h->plt.plist;
7794                                        ent != NULL;
7795                                        ent = ent->next)
7796                                     if (ent->addend == rel[1].r_addend)
7797                                       break;
7798
7799                                   if (ent != NULL
7800                                       && ent->plt.refcount > 0)
7801                                     ent->plt.refcount -= 1;
7802                                 }
7803                             }
7804                           continue;
7805                         }
7806                       found_tls_get_addr_arg = 1;
7807                       /* Fall through.  */
7808
7809                     case R_PPC64_TLS:
7810                     case R_PPC64_TOC16:
7811                     case R_PPC64_TOC16_LO:
7812                       if (sym_sec == NULL || sym_sec != toc)
7813                         continue;
7814
7815                       /* Mark this toc entry as referenced by a TLS
7816                          code sequence.  We can do that now in the
7817                          case of R_PPC64_TLS, and after checking for
7818                          tls_get_addr for the TOC16 relocs.  */
7819                       if (toc_ref == NULL)
7820                         toc_ref
7821                           = bfd_zmalloc (toc->output_section->rawsize / 8);
7822                       if (toc_ref == NULL)
7823                         goto err_free_rel;
7824
7825                       if (h != NULL)
7826                         value = h->root.u.def.value;
7827                       else
7828                         value = sym->st_value;
7829                       value += rel->r_addend;
7830                       if (value % 8 != 0)
7831                         continue;
7832                       BFD_ASSERT (value < toc->size
7833                                   && toc->output_offset % 8 == 0);
7834                       toc_ref_index = (value + toc->output_offset) / 8;
7835                       if (r_type == R_PPC64_TLS
7836                           || r_type == R_PPC64_TLSGD
7837                           || r_type == R_PPC64_TLSLD)
7838                         {
7839                           toc_ref[toc_ref_index] = 1;
7840                           continue;
7841                         }
7842
7843                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7844                         continue;
7845
7846                       tls_set = 0;
7847                       tls_clear = 0;
7848                       expecting_tls_get_addr = 2;
7849                       break;
7850
7851                     case R_PPC64_TPREL64:
7852                       if (pass == 0
7853                           || sec != toc
7854                           || toc_ref == NULL
7855                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7856                         continue;
7857                       if (ok_tprel)
7858                         {
7859                           /* IE -> LE */
7860                           tls_set = TLS_EXPLICIT;
7861                           tls_clear = TLS_TPREL;
7862                           break;
7863                         }
7864                       continue;
7865
7866                     case R_PPC64_DTPMOD64:
7867                       if (pass == 0
7868                           || sec != toc
7869                           || toc_ref == NULL
7870                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7871                         continue;
7872                       if (rel + 1 < relend
7873                           && (rel[1].r_info
7874                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7875                           && rel[1].r_offset == rel->r_offset + 8)
7876                         {
7877                           if (ok_tprel)
7878                             /* GD -> LE */
7879                             tls_set = TLS_EXPLICIT | TLS_GD;
7880                           else
7881                             /* GD -> IE */
7882                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7883                           tls_clear = TLS_GD;
7884                         }
7885                       else
7886                         {
7887                           if (!is_local)
7888                             continue;
7889
7890                           /* LD -> LE */
7891                           tls_set = TLS_EXPLICIT;
7892                           tls_clear = TLS_LD;
7893                         }
7894                       break;
7895
7896                     default:
7897                       continue;
7898                     }
7899
7900                   if (pass == 0)
7901                     {
7902                       if (!expecting_tls_get_addr
7903                           || !sec->has_tls_get_addr_call)
7904                         continue;
7905
7906                       if (rel + 1 < relend
7907                           && branch_reloc_hash_match (ibfd, rel + 1,
7908                                                       htab->tls_get_addr,
7909                                                       htab->tls_get_addr_fd))
7910                         {
7911                           if (expecting_tls_get_addr == 2)
7912                             {
7913                               /* Check for toc tls entries.  */
7914                               unsigned char *toc_tls;
7915                               int retval;
7916
7917                               retval = get_tls_mask (&toc_tls, NULL, NULL,
7918                                                      &locsyms,
7919                                                      rel, ibfd);
7920                               if (retval == 0)
7921                                 goto err_free_rel;
7922                               if (toc_tls != NULL)
7923                                 {
7924                                   if ((*toc_tls & TLS_TLS) != 0
7925                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7926                                     found_tls_get_addr_arg = 1;
7927                                   if (retval > 1)
7928                                     toc_ref[toc_ref_index] = 1;
7929                                 }
7930                             }
7931                           continue;
7932                         }
7933
7934                       /* Uh oh, we didn't find the expected call.  We
7935                          could just mark this symbol to exclude it
7936                          from tls optimization but it's safer to skip
7937                          the entire optimization.  */
7938                       /* xgettext:c-format */
7939                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7940                                                 "TLS optimization disabled\n"),
7941                                               ibfd, sec, rel->r_offset);
7942                       ret = TRUE;
7943                       goto err_free_rel;
7944                     }
7945
7946                   /* If we don't have old-style __tls_get_addr calls
7947                      without TLSGD/TLSLD marker relocs, and we haven't
7948                      found a new-style __tls_get_addr call with a
7949                      marker for this symbol, then we either have a
7950                      broken object file or an -mlongcall style
7951                      indirect call to __tls_get_addr without a marker.
7952                      Disable optimization in this case.  */
7953                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7954                       && (tls_set & TLS_EXPLICIT) == 0
7955                       && !sec->has_tls_get_addr_call
7956                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
7957                           != (TLS_TLS | TLS_MARK)))
7958                     continue;
7959
7960                   if (expecting_tls_get_addr)
7961                     {
7962                       struct plt_entry *ent = NULL;
7963
7964                       if (htab->tls_get_addr != NULL)
7965                         for (ent = htab->tls_get_addr->elf.plt.plist;
7966                              ent != NULL;
7967                              ent = ent->next)
7968                           if (ent->addend == 0)
7969                             break;
7970
7971                       if (ent == NULL && htab->tls_get_addr_fd != NULL)
7972                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7973                              ent != NULL;
7974                              ent = ent->next)
7975                           if (ent->addend == 0)
7976                             break;
7977
7978                       if (ent != NULL
7979                           && ent->plt.refcount > 0)
7980                         ent->plt.refcount -= 1;
7981                     }
7982
7983                   if (tls_clear == 0)
7984                     continue;
7985
7986                   if ((tls_set & TLS_EXPLICIT) == 0)
7987                     {
7988                       struct got_entry *ent;
7989
7990                       /* Adjust got entry for this reloc.  */
7991                       if (h != NULL)
7992                         ent = h->got.glist;
7993                       else
7994                         ent = elf_local_got_ents (ibfd)[r_symndx];
7995
7996                       for (; ent != NULL; ent = ent->next)
7997                         if (ent->addend == rel->r_addend
7998                             && ent->owner == ibfd
7999                             && ent->tls_type == tls_type)
8000                           break;
8001                       if (ent == NULL)
8002                         abort ();
8003
8004                       if (tls_set == 0)
8005                         {
8006                           /* We managed to get rid of a got entry.  */
8007                           if (ent->got.refcount > 0)
8008                             ent->got.refcount -= 1;
8009                         }
8010                     }
8011                   else
8012                     {
8013                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8014                          we'll lose one or two dyn relocs.  */
8015                       if (!dec_dynrel_count (rel->r_info, sec, info,
8016                                              NULL, h, sym))
8017                         return FALSE;
8018
8019                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8020                         {
8021                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8022                                                  NULL, h, sym))
8023                             return FALSE;
8024                         }
8025                     }
8026
8027                   *tls_mask |= tls_set;
8028                   *tls_mask &= ~tls_clear;
8029                 }
8030
8031               if (elf_section_data (sec)->relocs != relstart)
8032                 free (relstart);
8033             }
8034
8035         if (locsyms != NULL
8036             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8037           {
8038             if (!info->keep_memory)
8039               free (locsyms);
8040             else
8041               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8042           }
8043       }
8044
8045   if (toc_ref != NULL)
8046     free (toc_ref);
8047   htab->do_tls_opt = 1;
8048   return TRUE;
8049 }
8050
8051 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8052    the values of any global symbols in a toc section that has been
8053    edited.  Globals in toc sections should be a rarity, so this function
8054    sets a flag if any are found in toc sections other than the one just
8055    edited, so that further hash table traversals can be avoided.  */
8056
8057 struct adjust_toc_info
8058 {
8059   asection *toc;
8060   unsigned long *skip;
8061   bfd_boolean global_toc_syms;
8062 };
8063
8064 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8065
8066 static bfd_boolean
8067 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8068 {
8069   struct ppc_link_hash_entry *eh;
8070   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8071   unsigned long i;
8072
8073   if (h->root.type != bfd_link_hash_defined
8074       && h->root.type != bfd_link_hash_defweak)
8075     return TRUE;
8076
8077   eh = (struct ppc_link_hash_entry *) h;
8078   if (eh->adjust_done)
8079     return TRUE;
8080
8081   if (eh->elf.root.u.def.section == toc_inf->toc)
8082     {
8083       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8084         i = toc_inf->toc->rawsize >> 3;
8085       else
8086         i = eh->elf.root.u.def.value >> 3;
8087
8088       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8089         {
8090           _bfd_error_handler
8091             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8092           do
8093             ++i;
8094           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8095           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8096         }
8097
8098       eh->elf.root.u.def.value -= toc_inf->skip[i];
8099       eh->adjust_done = 1;
8100     }
8101   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8102     toc_inf->global_toc_syms = TRUE;
8103
8104   return TRUE;
8105 }
8106
8107 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8108    on a _LO variety toc/got reloc.  */
8109
8110 static bfd_boolean
8111 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8112 {
8113   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8114           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8115           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8116           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8117           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8118           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8119           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8120           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8121           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8122           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8123           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8124           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8125           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8126           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8127           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8128           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8129           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8130               /* Exclude lfqu by testing reloc.  If relocs are ever
8131                  defined for the reduced D field in psq_lu then those
8132                  will need testing too.  */
8133               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8134           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8135               && (insn & 1) == 0)
8136           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8137           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8138               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8139               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8140           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8141               && (insn & 1) == 0));
8142 }
8143
8144 /* Examine all relocs referencing .toc sections in order to remove
8145    unused .toc entries.  */
8146
8147 bfd_boolean
8148 ppc64_elf_edit_toc (struct bfd_link_info *info)
8149 {
8150   bfd *ibfd;
8151   struct adjust_toc_info toc_inf;
8152   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8153
8154   htab->do_toc_opt = 1;
8155   toc_inf.global_toc_syms = TRUE;
8156   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8157     {
8158       asection *toc, *sec;
8159       Elf_Internal_Shdr *symtab_hdr;
8160       Elf_Internal_Sym *local_syms;
8161       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8162       unsigned long *skip, *drop;
8163       unsigned char *used;
8164       unsigned char *keep, last, some_unused;
8165
8166       if (!is_ppc64_elf (ibfd))
8167         continue;
8168
8169       toc = bfd_get_section_by_name (ibfd, ".toc");
8170       if (toc == NULL
8171           || toc->size == 0
8172           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8173           || discarded_section (toc))
8174         continue;
8175
8176       toc_relocs = NULL;
8177       local_syms = NULL;
8178       symtab_hdr = &elf_symtab_hdr (ibfd);
8179
8180       /* Look at sections dropped from the final link.  */
8181       skip = NULL;
8182       relstart = NULL;
8183       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8184         {
8185           if (sec->reloc_count == 0
8186               || !discarded_section (sec)
8187               || get_opd_info (sec)
8188               || (sec->flags & SEC_ALLOC) == 0
8189               || (sec->flags & SEC_DEBUGGING) != 0)
8190             continue;
8191
8192           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8193           if (relstart == NULL)
8194             goto error_ret;
8195
8196           /* Run through the relocs to see which toc entries might be
8197              unused.  */
8198           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8199             {
8200               enum elf_ppc64_reloc_type r_type;
8201               unsigned long r_symndx;
8202               asection *sym_sec;
8203               struct elf_link_hash_entry *h;
8204               Elf_Internal_Sym *sym;
8205               bfd_vma val;
8206
8207               r_type = ELF64_R_TYPE (rel->r_info);
8208               switch (r_type)
8209                 {
8210                 default:
8211                   continue;
8212
8213                 case R_PPC64_TOC16:
8214                 case R_PPC64_TOC16_LO:
8215                 case R_PPC64_TOC16_HI:
8216                 case R_PPC64_TOC16_HA:
8217                 case R_PPC64_TOC16_DS:
8218                 case R_PPC64_TOC16_LO_DS:
8219                   break;
8220                 }
8221
8222               r_symndx = ELF64_R_SYM (rel->r_info);
8223               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8224                               r_symndx, ibfd))
8225                 goto error_ret;
8226
8227               if (sym_sec != toc)
8228                 continue;
8229
8230               if (h != NULL)
8231                 val = h->root.u.def.value;
8232               else
8233                 val = sym->st_value;
8234               val += rel->r_addend;
8235
8236               if (val >= toc->size)
8237                 continue;
8238
8239               /* Anything in the toc ought to be aligned to 8 bytes.
8240                  If not, don't mark as unused.  */
8241               if (val & 7)
8242                 continue;
8243
8244               if (skip == NULL)
8245                 {
8246                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8247                   if (skip == NULL)
8248                     goto error_ret;
8249                 }
8250
8251               skip[val >> 3] = ref_from_discarded;
8252             }
8253
8254           if (elf_section_data (sec)->relocs != relstart)
8255             free (relstart);
8256         }
8257
8258       /* For largetoc loads of address constants, we can convert
8259          .  addis rx,2,addr@got@ha
8260          .  ld ry,addr@got@l(rx)
8261          to
8262          .  addis rx,2,addr@toc@ha
8263          .  addi ry,rx,addr@toc@l
8264          when addr is within 2G of the toc pointer.  This then means
8265          that the word storing "addr" in the toc is no longer needed.  */
8266
8267       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8268           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8269           && toc->reloc_count != 0)
8270         {
8271           /* Read toc relocs.  */
8272           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8273                                                   info->keep_memory);
8274           if (toc_relocs == NULL)
8275             goto error_ret;
8276
8277           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8278             {
8279               enum elf_ppc64_reloc_type r_type;
8280               unsigned long r_symndx;
8281               asection *sym_sec;
8282               struct elf_link_hash_entry *h;
8283               Elf_Internal_Sym *sym;
8284               bfd_vma val, addr;
8285
8286               r_type = ELF64_R_TYPE (rel->r_info);
8287               if (r_type != R_PPC64_ADDR64)
8288                 continue;
8289
8290               r_symndx = ELF64_R_SYM (rel->r_info);
8291               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8292                               r_symndx, ibfd))
8293                 goto error_ret;
8294
8295               if (sym_sec == NULL
8296                   || sym_sec->output_section == NULL
8297                   || discarded_section (sym_sec))
8298                 continue;
8299
8300               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8301                 continue;
8302
8303               if (h != NULL)
8304                 {
8305                   if (h->type == STT_GNU_IFUNC)
8306                     continue;
8307                   val = h->root.u.def.value;
8308                 }
8309               else
8310                 {
8311                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8312                     continue;
8313                   val = sym->st_value;
8314                 }
8315               val += rel->r_addend;
8316               val += sym_sec->output_section->vma + sym_sec->output_offset;
8317
8318               /* We don't yet know the exact toc pointer value, but we
8319                  know it will be somewhere in the toc section.  Don't
8320                  optimize if the difference from any possible toc
8321                  pointer is outside [ff..f80008000, 7fff7fff].  */
8322               addr = toc->output_section->vma + TOC_BASE_OFF;
8323               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8324                 continue;
8325
8326               addr = toc->output_section->vma + toc->output_section->rawsize;
8327               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8328                 continue;
8329
8330               if (skip == NULL)
8331                 {
8332                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8333                   if (skip == NULL)
8334                     goto error_ret;
8335                 }
8336
8337               skip[rel->r_offset >> 3]
8338                 |= can_optimize | ((rel - toc_relocs) << 2);
8339             }
8340         }
8341
8342       if (skip == NULL)
8343         continue;
8344
8345       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8346       if (used == NULL)
8347         {
8348         error_ret:
8349           if (local_syms != NULL
8350               && symtab_hdr->contents != (unsigned char *) local_syms)
8351             free (local_syms);
8352           if (sec != NULL
8353               && relstart != NULL
8354               && elf_section_data (sec)->relocs != relstart)
8355             free (relstart);
8356           if (toc_relocs != NULL
8357               && elf_section_data (toc)->relocs != toc_relocs)
8358             free (toc_relocs);
8359           if (skip != NULL)
8360             free (skip);
8361           return FALSE;
8362         }
8363
8364       /* Now check all kept sections that might reference the toc.
8365          Check the toc itself last.  */
8366       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8367                   : ibfd->sections);
8368            sec != NULL;
8369            sec = (sec == toc ? NULL
8370                   : sec->next == NULL ? toc
8371                   : sec->next == toc && toc->next ? toc->next
8372                   : sec->next))
8373         {
8374           int repeat;
8375
8376           if (sec->reloc_count == 0
8377               || discarded_section (sec)
8378               || get_opd_info (sec)
8379               || (sec->flags & SEC_ALLOC) == 0
8380               || (sec->flags & SEC_DEBUGGING) != 0)
8381             continue;
8382
8383           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8384                                                 info->keep_memory);
8385           if (relstart == NULL)
8386             {
8387               free (used);
8388               goto error_ret;
8389             }
8390
8391           /* Mark toc entries referenced as used.  */
8392           do
8393             {
8394               repeat = 0;
8395               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8396                 {
8397                   enum elf_ppc64_reloc_type r_type;
8398                   unsigned long r_symndx;
8399                   asection *sym_sec;
8400                   struct elf_link_hash_entry *h;
8401                   Elf_Internal_Sym *sym;
8402                   bfd_vma val;
8403                   enum {no_check, check_lo, check_ha} insn_check;
8404
8405                   r_type = ELF64_R_TYPE (rel->r_info);
8406                   switch (r_type)
8407                     {
8408                     default:
8409                       insn_check = no_check;
8410                       break;
8411
8412                     case R_PPC64_GOT_TLSLD16_HA:
8413                     case R_PPC64_GOT_TLSGD16_HA:
8414                     case R_PPC64_GOT_TPREL16_HA:
8415                     case R_PPC64_GOT_DTPREL16_HA:
8416                     case R_PPC64_GOT16_HA:
8417                     case R_PPC64_TOC16_HA:
8418                       insn_check = check_ha;
8419                       break;
8420
8421                     case R_PPC64_GOT_TLSLD16_LO:
8422                     case R_PPC64_GOT_TLSGD16_LO:
8423                     case R_PPC64_GOT_TPREL16_LO_DS:
8424                     case R_PPC64_GOT_DTPREL16_LO_DS:
8425                     case R_PPC64_GOT16_LO:
8426                     case R_PPC64_GOT16_LO_DS:
8427                     case R_PPC64_TOC16_LO:
8428                     case R_PPC64_TOC16_LO_DS:
8429                       insn_check = check_lo;
8430                       break;
8431                     }
8432
8433                   if (insn_check != no_check)
8434                     {
8435                       bfd_vma off = rel->r_offset & ~3;
8436                       unsigned char buf[4];
8437                       unsigned int insn;
8438
8439                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8440                         {
8441                           free (used);
8442                           goto error_ret;
8443                         }
8444                       insn = bfd_get_32 (ibfd, buf);
8445                       if (insn_check == check_lo
8446                           ? !ok_lo_toc_insn (insn, r_type)
8447                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8448                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8449                         {
8450                           char str[12];
8451
8452                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8453                           sprintf (str, "%#08x", insn);
8454                           info->callbacks->einfo
8455                             /* xgettext:c-format */
8456                             (_("%H: toc optimization is not supported for"
8457                                " %s instruction\n"),
8458                              ibfd, sec, rel->r_offset & ~3, str);
8459                         }
8460                     }
8461
8462                   switch (r_type)
8463                     {
8464                     case R_PPC64_TOC16:
8465                     case R_PPC64_TOC16_LO:
8466                     case R_PPC64_TOC16_HI:
8467                     case R_PPC64_TOC16_HA:
8468                     case R_PPC64_TOC16_DS:
8469                     case R_PPC64_TOC16_LO_DS:
8470                       /* In case we're taking addresses of toc entries.  */
8471                     case R_PPC64_ADDR64:
8472                       break;
8473
8474                     default:
8475                       continue;
8476                     }
8477
8478                   r_symndx = ELF64_R_SYM (rel->r_info);
8479                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8480                                   r_symndx, ibfd))
8481                     {
8482                       free (used);
8483                       goto error_ret;
8484                     }
8485
8486                   if (sym_sec != toc)
8487                     continue;
8488
8489                   if (h != NULL)
8490                     val = h->root.u.def.value;
8491                   else
8492                     val = sym->st_value;
8493                   val += rel->r_addend;
8494
8495                   if (val >= toc->size)
8496                     continue;
8497
8498                   if ((skip[val >> 3] & can_optimize) != 0)
8499                     {
8500                       bfd_vma off;
8501                       unsigned char opc;
8502
8503                       switch (r_type)
8504                         {
8505                         case R_PPC64_TOC16_HA:
8506                           break;
8507
8508                         case R_PPC64_TOC16_LO_DS:
8509                           off = rel->r_offset;
8510                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8511                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8512                                                          off, 1))
8513                             {
8514                               free (used);
8515                               goto error_ret;
8516                             }
8517                           if ((opc & (0x3f << 2)) == (58u << 2))
8518                             break;
8519                           /* Fall through.  */
8520
8521                         default:
8522                           /* Wrong sort of reloc, or not a ld.  We may
8523                              as well clear ref_from_discarded too.  */
8524                           skip[val >> 3] = 0;
8525                         }
8526                     }
8527
8528                   if (sec != toc)
8529                     used[val >> 3] = 1;
8530                   /* For the toc section, we only mark as used if this
8531                      entry itself isn't unused.  */
8532                   else if ((used[rel->r_offset >> 3]
8533                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8534                            && !used[val >> 3])
8535                     {
8536                       /* Do all the relocs again, to catch reference
8537                          chains.  */
8538                       repeat = 1;
8539                       used[val >> 3] = 1;
8540                     }
8541                 }
8542             }
8543           while (repeat);
8544
8545           if (elf_section_data (sec)->relocs != relstart)
8546             free (relstart);
8547         }
8548
8549       /* Merge the used and skip arrays.  Assume that TOC
8550          doublewords not appearing as either used or unused belong
8551          to an entry more than one doubleword in size.  */
8552       for (drop = skip, keep = used, last = 0, some_unused = 0;
8553            drop < skip + (toc->size + 7) / 8;
8554            ++drop, ++keep)
8555         {
8556           if (*keep)
8557             {
8558               *drop &= ~ref_from_discarded;
8559               if ((*drop & can_optimize) != 0)
8560                 some_unused = 1;
8561               last = 0;
8562             }
8563           else if ((*drop & ref_from_discarded) != 0)
8564             {
8565               some_unused = 1;
8566               last = ref_from_discarded;
8567             }
8568           else
8569             *drop = last;
8570         }
8571
8572       free (used);
8573
8574       if (some_unused)
8575         {
8576           bfd_byte *contents, *src;
8577           unsigned long off;
8578           Elf_Internal_Sym *sym;
8579           bfd_boolean local_toc_syms = FALSE;
8580
8581           /* Shuffle the toc contents, and at the same time convert the
8582              skip array from booleans into offsets.  */
8583           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8584             goto error_ret;
8585
8586           elf_section_data (toc)->this_hdr.contents = contents;
8587
8588           for (src = contents, off = 0, drop = skip;
8589                src < contents + toc->size;
8590                src += 8, ++drop)
8591             {
8592               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8593                 off += 8;
8594               else if (off != 0)
8595                 {
8596                   *drop = off;
8597                   memcpy (src - off, src, 8);
8598                 }
8599             }
8600           *drop = off;
8601           toc->rawsize = toc->size;
8602           toc->size = src - contents - off;
8603
8604           /* Adjust addends for relocs against the toc section sym,
8605              and optimize any accesses we can.  */
8606           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8607             {
8608               if (sec->reloc_count == 0
8609                   || discarded_section (sec))
8610                 continue;
8611
8612               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8613                                                     info->keep_memory);
8614               if (relstart == NULL)
8615                 goto error_ret;
8616
8617               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8618                 {
8619                   enum elf_ppc64_reloc_type r_type;
8620                   unsigned long r_symndx;
8621                   asection *sym_sec;
8622                   struct elf_link_hash_entry *h;
8623                   bfd_vma val;
8624
8625                   r_type = ELF64_R_TYPE (rel->r_info);
8626                   switch (r_type)
8627                     {
8628                     default:
8629                       continue;
8630
8631                     case R_PPC64_TOC16:
8632                     case R_PPC64_TOC16_LO:
8633                     case R_PPC64_TOC16_HI:
8634                     case R_PPC64_TOC16_HA:
8635                     case R_PPC64_TOC16_DS:
8636                     case R_PPC64_TOC16_LO_DS:
8637                     case R_PPC64_ADDR64:
8638                       break;
8639                     }
8640
8641                   r_symndx = ELF64_R_SYM (rel->r_info);
8642                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8643                                   r_symndx, ibfd))
8644                     goto error_ret;
8645
8646                   if (sym_sec != toc)
8647                     continue;
8648
8649                   if (h != NULL)
8650                     val = h->root.u.def.value;
8651                   else
8652                     {
8653                       val = sym->st_value;
8654                       if (val != 0)
8655                         local_toc_syms = TRUE;
8656                     }
8657
8658                   val += rel->r_addend;
8659
8660                   if (val > toc->rawsize)
8661                     val = toc->rawsize;
8662                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8663                     continue;
8664                   else if ((skip[val >> 3] & can_optimize) != 0)
8665                     {
8666                       Elf_Internal_Rela *tocrel
8667                         = toc_relocs + (skip[val >> 3] >> 2);
8668                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8669
8670                       switch (r_type)
8671                         {
8672                         case R_PPC64_TOC16_HA:
8673                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8674                           break;
8675
8676                         case R_PPC64_TOC16_LO_DS:
8677                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8678                           break;
8679
8680                         default:
8681                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8682                             ppc_howto_init ();
8683                           info->callbacks->einfo
8684                             /* xgettext:c-format */
8685                             (_("%H: %s references "
8686                                "optimized away TOC entry\n"),
8687                              ibfd, sec, rel->r_offset,
8688                              ppc64_elf_howto_table[r_type]->name);
8689                           bfd_set_error (bfd_error_bad_value);
8690                           goto error_ret;
8691                         }
8692                       rel->r_addend = tocrel->r_addend;
8693                       elf_section_data (sec)->relocs = relstart;
8694                       continue;
8695                     }
8696
8697                   if (h != NULL || sym->st_value != 0)
8698                     continue;
8699
8700                   rel->r_addend -= skip[val >> 3];
8701                   elf_section_data (sec)->relocs = relstart;
8702                 }
8703
8704               if (elf_section_data (sec)->relocs != relstart)
8705                 free (relstart);
8706             }
8707
8708           /* We shouldn't have local or global symbols defined in the TOC,
8709              but handle them anyway.  */
8710           if (local_syms != NULL)
8711             for (sym = local_syms;
8712                  sym < local_syms + symtab_hdr->sh_info;
8713                  ++sym)
8714               if (sym->st_value != 0
8715                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8716                 {
8717                   unsigned long i;
8718
8719                   if (sym->st_value > toc->rawsize)
8720                     i = toc->rawsize >> 3;
8721                   else
8722                     i = sym->st_value >> 3;
8723
8724                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8725                     {
8726                       if (local_toc_syms)
8727                         _bfd_error_handler
8728                           (_("%s defined on removed toc entry"),
8729                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8730                       do
8731                         ++i;
8732                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8733                       sym->st_value = (bfd_vma) i << 3;
8734                     }
8735
8736                   sym->st_value -= skip[i];
8737                   symtab_hdr->contents = (unsigned char *) local_syms;
8738                 }
8739
8740           /* Adjust any global syms defined in this toc input section.  */
8741           if (toc_inf.global_toc_syms)
8742             {
8743               toc_inf.toc = toc;
8744               toc_inf.skip = skip;
8745               toc_inf.global_toc_syms = FALSE;
8746               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8747                                       &toc_inf);
8748             }
8749
8750           if (toc->reloc_count != 0)
8751             {
8752               Elf_Internal_Shdr *rel_hdr;
8753               Elf_Internal_Rela *wrel;
8754               bfd_size_type sz;
8755
8756               /* Remove unused toc relocs, and adjust those we keep.  */
8757               if (toc_relocs == NULL)
8758                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8759                                                         info->keep_memory);
8760               if (toc_relocs == NULL)
8761                 goto error_ret;
8762
8763               wrel = toc_relocs;
8764               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8765                 if ((skip[rel->r_offset >> 3]
8766                      & (ref_from_discarded | can_optimize)) == 0)
8767                   {
8768                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8769                     wrel->r_info = rel->r_info;
8770                     wrel->r_addend = rel->r_addend;
8771                     ++wrel;
8772                   }
8773                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8774                                             &local_syms, NULL, NULL))
8775                   goto error_ret;
8776
8777               elf_section_data (toc)->relocs = toc_relocs;
8778               toc->reloc_count = wrel - toc_relocs;
8779               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8780               sz = rel_hdr->sh_entsize;
8781               rel_hdr->sh_size = toc->reloc_count * sz;
8782             }
8783         }
8784       else if (toc_relocs != NULL
8785                && elf_section_data (toc)->relocs != toc_relocs)
8786         free (toc_relocs);
8787
8788       if (local_syms != NULL
8789           && symtab_hdr->contents != (unsigned char *) local_syms)
8790         {
8791           if (!info->keep_memory)
8792             free (local_syms);
8793           else
8794             symtab_hdr->contents = (unsigned char *) local_syms;
8795         }
8796       free (skip);
8797     }
8798
8799   /* Look for cases where we can change an indirect GOT access to
8800      a GOT relative access, possibly reducing the number of GOT
8801      entries.  */
8802   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8803     {
8804       asection *sec;
8805       Elf_Internal_Shdr *symtab_hdr;
8806       Elf_Internal_Sym *local_syms;
8807       Elf_Internal_Rela *relstart, *rel;
8808       bfd_vma got;
8809
8810       if (!is_ppc64_elf (ibfd))
8811         continue;
8812
8813       if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8814         continue;
8815
8816       sec = ppc64_elf_tdata (ibfd)->got;
8817       got = sec->output_section->vma + sec->output_offset + 0x8000;
8818
8819       local_syms = NULL;
8820       symtab_hdr = &elf_symtab_hdr (ibfd);
8821
8822       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8823         {
8824           if (sec->reloc_count == 0
8825               || !ppc64_elf_section_data (sec)->has_gotrel
8826               || discarded_section (sec))
8827             continue;
8828
8829           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8830                                                 info->keep_memory);
8831           if (relstart == NULL)
8832             {
8833             got_error_ret:
8834               if (local_syms != NULL
8835                   && symtab_hdr->contents != (unsigned char *) local_syms)
8836                 free (local_syms);
8837               if (sec != NULL
8838                   && relstart != NULL
8839                   && elf_section_data (sec)->relocs != relstart)
8840                 free (relstart);
8841               return FALSE;
8842             }
8843
8844           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8845             {
8846               enum elf_ppc64_reloc_type r_type;
8847               unsigned long r_symndx;
8848               Elf_Internal_Sym *sym;
8849               asection *sym_sec;
8850               struct elf_link_hash_entry *h;
8851               struct got_entry *ent;
8852               bfd_vma val;
8853               unsigned char buf[4];
8854               unsigned int insn;
8855
8856               r_type = ELF64_R_TYPE (rel->r_info);
8857               switch (r_type)
8858                 {
8859                 default:
8860                   continue;
8861
8862                 case R_PPC64_GOT16_DS:
8863                 case R_PPC64_GOT16_HA:
8864                 case R_PPC64_GOT16_LO_DS:
8865                   break;
8866                 }
8867
8868               r_symndx = ELF64_R_SYM (rel->r_info);
8869               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8870                               r_symndx, ibfd))
8871                 goto got_error_ret;
8872
8873               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8874                 continue;
8875
8876               if (h != NULL)
8877                 val = h->root.u.def.value;
8878               else
8879                 val = sym->st_value;
8880               val += rel->r_addend;
8881               val += sym_sec->output_section->vma + sym_sec->output_offset;
8882
8883               switch (r_type)
8884                 {
8885                 default:
8886                   continue;
8887
8888                 case R_PPC64_GOT16_DS:
8889                   if (val - got + 0x8000 >= 0x10000)
8890                     continue;
8891                   if (!bfd_get_section_contents (ibfd, sec, buf,
8892                                                  rel->r_offset & ~3, 4))
8893                     goto got_error_ret;
8894                   insn = bfd_get_32 (ibfd, buf);
8895                   if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8896                     continue;
8897                   break;
8898
8899                 case R_PPC64_GOT16_HA:
8900                   if (val - got + 0x80008000ULL >= 0x100000000ULL)
8901                     continue;
8902
8903                   if (!bfd_get_section_contents (ibfd, sec, buf,
8904                                                  rel->r_offset & ~3, 4))
8905                     goto got_error_ret;
8906                   insn = bfd_get_32 (ibfd, buf);
8907                   if (((insn & ((0x3f << 26) | 0x1f << 16))
8908                        != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8909                     continue;
8910                   break;
8911
8912                 case R_PPC64_GOT16_LO_DS:
8913                   if (val - got + 0x80008000ULL >= 0x100000000ULL)
8914                     continue;
8915                   if (!bfd_get_section_contents (ibfd, sec, buf,
8916                                                  rel->r_offset & ~3, 4))
8917                     goto got_error_ret;
8918                   insn = bfd_get_32 (ibfd, buf);
8919                   if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
8920                     continue;
8921                   break;
8922                 }
8923
8924               if (h != NULL)
8925                 ent = h->got.glist;
8926               else
8927                 {
8928                   struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
8929                   ent = local_got_ents[r_symndx];
8930                 }
8931               for (; ent != NULL; ent = ent->next)
8932                 if (ent->addend == rel->r_addend
8933                     && ent->owner == ibfd
8934                     && ent->tls_type == 0)
8935                   break;
8936               BFD_ASSERT (ent && ent->got.refcount > 0);
8937               ent->got.refcount -= 1;
8938             }
8939
8940           if (elf_section_data (sec)->relocs != relstart)
8941             free (relstart);
8942         }
8943
8944       if (local_syms != NULL
8945           && symtab_hdr->contents != (unsigned char *) local_syms)
8946         {
8947           if (!info->keep_memory)
8948             free (local_syms);
8949           else
8950             symtab_hdr->contents = (unsigned char *) local_syms;
8951         }
8952     }
8953
8954   return TRUE;
8955 }
8956
8957 /* Return true iff input section I references the TOC using
8958    instructions limited to +/-32k offsets.  */
8959
8960 bfd_boolean
8961 ppc64_elf_has_small_toc_reloc (asection *i)
8962 {
8963   return (is_ppc64_elf (i->owner)
8964           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8965 }
8966
8967 /* Allocate space for one GOT entry.  */
8968
8969 static void
8970 allocate_got (struct elf_link_hash_entry *h,
8971               struct bfd_link_info *info,
8972               struct got_entry *gent)
8973 {
8974   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8975   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8976   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8977                  ? 16 : 8);
8978   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8979                   ? 2 : 1) * sizeof (Elf64_External_Rela);
8980   asection *got = ppc64_elf_tdata (gent->owner)->got;
8981
8982   gent->got.offset = got->size;
8983   got->size += entsize;
8984
8985   if (h->type == STT_GNU_IFUNC)
8986     {
8987       htab->elf.irelplt->size += rentsize;
8988       htab->got_reli_size += rentsize;
8989     }
8990   else if (((bfd_link_pic (info)
8991              && !((gent->tls_type & TLS_TPREL) != 0
8992                   && bfd_link_executable (info)
8993                   && SYMBOL_REFERENCES_LOCAL (info, h)))
8994             || (htab->elf.dynamic_sections_created
8995                 && h->dynindx != -1
8996                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8997            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8998     {
8999       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9000       relgot->size += rentsize;
9001     }
9002 }
9003
9004 /* This function merges got entries in the same toc group.  */
9005
9006 static void
9007 merge_got_entries (struct got_entry **pent)
9008 {
9009   struct got_entry *ent, *ent2;
9010
9011   for (ent = *pent; ent != NULL; ent = ent->next)
9012     if (!ent->is_indirect)
9013       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9014         if (!ent2->is_indirect
9015             && ent2->addend == ent->addend
9016             && ent2->tls_type == ent->tls_type
9017             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9018           {
9019             ent2->is_indirect = TRUE;
9020             ent2->got.ent = ent;
9021           }
9022 }
9023
9024 /* If H is undefined, make it dynamic if that makes sense.  */
9025
9026 static bfd_boolean
9027 ensure_undef_dynamic (struct bfd_link_info *info,
9028                       struct elf_link_hash_entry *h)
9029 {
9030   struct elf_link_hash_table *htab = elf_hash_table (info);
9031
9032   if (htab->dynamic_sections_created
9033       && ((info->dynamic_undefined_weak != 0
9034            && h->root.type == bfd_link_hash_undefweak)
9035           || h->root.type == bfd_link_hash_undefined)
9036       && h->dynindx == -1
9037       && !h->forced_local
9038       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9039     return bfd_elf_link_record_dynamic_symbol (info, h);
9040   return TRUE;
9041 }
9042
9043 /* Allocate space in .plt, .got and associated reloc sections for
9044    dynamic relocs.  */
9045
9046 static bfd_boolean
9047 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9048 {
9049   struct bfd_link_info *info;
9050   struct ppc_link_hash_table *htab;
9051   asection *s;
9052   struct ppc_link_hash_entry *eh;
9053   struct got_entry **pgent, *gent;
9054
9055   if (h->root.type == bfd_link_hash_indirect)
9056     return TRUE;
9057
9058   info = (struct bfd_link_info *) inf;
9059   htab = ppc_hash_table (info);
9060   if (htab == NULL)
9061     return FALSE;
9062
9063   eh = (struct ppc_link_hash_entry *) h;
9064   /* Run through the TLS GD got entries first if we're changing them
9065      to TPREL.  */
9066   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9067     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9068       if (gent->got.refcount > 0
9069           && (gent->tls_type & TLS_GD) != 0)
9070         {
9071           /* This was a GD entry that has been converted to TPREL.  If
9072              there happens to be a TPREL entry we can use that one.  */
9073           struct got_entry *ent;
9074           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9075             if (ent->got.refcount > 0
9076                 && (ent->tls_type & TLS_TPREL) != 0
9077                 && ent->addend == gent->addend
9078                 && ent->owner == gent->owner)
9079               {
9080                 gent->got.refcount = 0;
9081                 break;
9082               }
9083
9084           /* If not, then we'll be using our own TPREL entry.  */
9085           if (gent->got.refcount != 0)
9086             gent->tls_type = TLS_TLS | TLS_TPREL;
9087         }
9088
9089   /* Remove any list entry that won't generate a word in the GOT before
9090      we call merge_got_entries.  Otherwise we risk merging to empty
9091      entries.  */
9092   pgent = &h->got.glist;
9093   while ((gent = *pgent) != NULL)
9094     if (gent->got.refcount > 0)
9095       {
9096         if ((gent->tls_type & TLS_LD) != 0
9097             && !h->def_dynamic)
9098           {
9099             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9100             *pgent = gent->next;
9101           }
9102         else
9103           pgent = &gent->next;
9104       }
9105     else
9106       *pgent = gent->next;
9107
9108   if (!htab->do_multi_toc)
9109     merge_got_entries (&h->got.glist);
9110
9111   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9112     if (!gent->is_indirect)
9113       {
9114         /* Make sure this symbol is output as a dynamic symbol.  */
9115         if (!ensure_undef_dynamic (info, h))
9116           return FALSE;
9117
9118         if (!is_ppc64_elf (gent->owner))
9119           abort ();
9120
9121         allocate_got (h, info, gent);
9122       }
9123
9124   /* If no dynamic sections we can't have dynamic relocs, except for
9125      IFUNCs which are handled even in static executables.  */
9126   if (!htab->elf.dynamic_sections_created
9127       && h->type != STT_GNU_IFUNC)
9128     eh->dyn_relocs = NULL;
9129
9130   /* Discard relocs on undefined symbols that must be local.  */
9131   else if (h->root.type == bfd_link_hash_undefined
9132            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9133     eh->dyn_relocs = NULL;
9134
9135   /* Also discard relocs on undefined weak syms with non-default
9136      visibility, or when dynamic_undefined_weak says so.  */
9137   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9138     eh->dyn_relocs = NULL;
9139
9140   if (eh->dyn_relocs != NULL)
9141     {
9142       struct elf_dyn_relocs *p, **pp;
9143
9144       /* In the shared -Bsymbolic case, discard space allocated for
9145          dynamic pc-relative relocs against symbols which turn out to
9146          be defined in regular objects.  For the normal shared case,
9147          discard space for relocs that have become local due to symbol
9148          visibility changes.  */
9149
9150       if (bfd_link_pic (info))
9151         {
9152           /* Relocs that use pc_count are those that appear on a call
9153              insn, or certain REL relocs (see must_be_dyn_reloc) that
9154              can be generated via assembly.  We want calls to
9155              protected symbols to resolve directly to the function
9156              rather than going via the plt.  If people want function
9157              pointer comparisons to work as expected then they should
9158              avoid writing weird assembly.  */
9159           if (SYMBOL_CALLS_LOCAL (info, h))
9160             {
9161               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9162                 {
9163                   p->count -= p->pc_count;
9164                   p->pc_count = 0;
9165                   if (p->count == 0)
9166                     *pp = p->next;
9167                   else
9168                     pp = &p->next;
9169                 }
9170             }
9171
9172           if (eh->dyn_relocs != NULL)
9173             {
9174               /* Make sure this symbol is output as a dynamic symbol.  */
9175               if (!ensure_undef_dynamic (info, h))
9176                 return FALSE;
9177             }
9178         }
9179       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9180         {
9181           /* For the non-pic case, discard space for relocs against
9182              symbols which turn out to need copy relocs or are not
9183              dynamic.  */
9184           if (h->dynamic_adjusted
9185               && !h->def_regular
9186               && !ELF_COMMON_DEF_P (h))
9187             {
9188               /* Make sure this symbol is output as a dynamic symbol.  */
9189               if (!ensure_undef_dynamic (info, h))
9190                 return FALSE;
9191
9192               if (h->dynindx == -1)
9193                 eh->dyn_relocs = NULL;
9194             }
9195           else
9196             eh->dyn_relocs = NULL;
9197         }
9198
9199       /* Finally, allocate space.  */
9200       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9201         {
9202           asection *sreloc = elf_section_data (p->sec)->sreloc;
9203           if (eh->elf.type == STT_GNU_IFUNC)
9204             sreloc = htab->elf.irelplt;
9205           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9206         }
9207     }
9208
9209   /* We might need a PLT entry when the symbol
9210      a) is dynamic, or
9211      b) is an ifunc, or
9212      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9213      d) has plt16 relocs and we are linking statically.  */
9214   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9215       || h->type == STT_GNU_IFUNC
9216       || (h->needs_plt && h->dynamic_adjusted)
9217       || (h->needs_plt
9218           && h->def_regular
9219           && !htab->elf.dynamic_sections_created
9220           && !htab->can_convert_all_inline_plt
9221           && (((struct ppc_link_hash_entry *) h)->tls_mask
9222               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9223     {
9224       struct plt_entry *pent;
9225       bfd_boolean doneone = FALSE;
9226       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9227         if (pent->plt.refcount > 0)
9228           {
9229             if (!htab->elf.dynamic_sections_created
9230                 || h->dynindx == -1)
9231               {
9232                 if (h->type == STT_GNU_IFUNC)
9233                   {
9234                     s = htab->elf.iplt;
9235                     pent->plt.offset = s->size;
9236                     s->size += PLT_ENTRY_SIZE (htab);
9237                     s = htab->elf.irelplt;
9238                   }
9239                 else
9240                   {
9241                     s = htab->pltlocal;
9242                     pent->plt.offset = s->size;
9243                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9244                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9245                   }
9246               }
9247             else
9248               {
9249                 /* If this is the first .plt entry, make room for the special
9250                    first entry.  */
9251                 s = htab->elf.splt;
9252                 if (s->size == 0)
9253                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9254
9255                 pent->plt.offset = s->size;
9256
9257                 /* Make room for this entry.  */
9258                 s->size += PLT_ENTRY_SIZE (htab);
9259
9260                 /* Make room for the .glink code.  */
9261                 s = htab->glink;
9262                 if (s->size == 0)
9263                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9264                 if (htab->opd_abi)
9265                   {
9266                     /* We need bigger stubs past index 32767.  */
9267                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9268                       s->size += 4;
9269                     s->size += 2*4;
9270                   }
9271                 else
9272                   s->size += 4;
9273
9274                 /* We also need to make an entry in the .rela.plt section.  */
9275                 s = htab->elf.srelplt;
9276               }
9277             if (s != NULL)
9278               s->size += sizeof (Elf64_External_Rela);
9279             doneone = TRUE;
9280           }
9281         else
9282           pent->plt.offset = (bfd_vma) -1;
9283       if (!doneone)
9284         {
9285           h->plt.plist = NULL;
9286           h->needs_plt = 0;
9287         }
9288     }
9289   else
9290     {
9291       h->plt.plist = NULL;
9292       h->needs_plt = 0;
9293     }
9294
9295   return TRUE;
9296 }
9297
9298 #define PPC_LO(v) ((v) & 0xffff)
9299 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9300 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9301
9302 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9303    to set up space for global entry stubs.  These are put in glink,
9304    after the branch table.  */
9305
9306 static bfd_boolean
9307 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9308 {
9309   struct bfd_link_info *info;
9310   struct ppc_link_hash_table *htab;
9311   struct plt_entry *pent;
9312   asection *s, *plt;
9313
9314   if (h->root.type == bfd_link_hash_indirect)
9315     return TRUE;
9316
9317   if (!h->pointer_equality_needed)
9318     return TRUE;
9319
9320   if (h->def_regular)
9321     return TRUE;
9322
9323   info = inf;
9324   htab = ppc_hash_table (info);
9325   if (htab == NULL)
9326     return FALSE;
9327
9328   s = htab->global_entry;
9329   plt = htab->elf.splt;
9330   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9331     if (pent->plt.offset != (bfd_vma) -1
9332         && pent->addend == 0)
9333       {
9334         /* For ELFv2, if this symbol is not defined in a regular file
9335            and we are not generating a shared library or pie, then we
9336            need to define the symbol in the executable on a call stub.
9337            This is to avoid text relocations.  */
9338         bfd_vma off, stub_align, stub_off, stub_size;
9339         unsigned int align_power;
9340
9341         stub_size = 16;
9342         stub_off = s->size;
9343         if (htab->params->plt_stub_align >= 0)
9344           align_power = htab->params->plt_stub_align;
9345         else
9346           align_power = -htab->params->plt_stub_align;
9347         /* Setting section alignment is delayed until we know it is
9348            non-empty.  Otherwise the .text output section will be
9349            aligned at least to plt_stub_align even when no global
9350            entry stubs are needed.  */
9351         if (s->alignment_power < align_power)
9352           s->alignment_power = align_power;
9353         stub_align = (bfd_vma) 1 << align_power;
9354         if (htab->params->plt_stub_align >= 0
9355             || ((((stub_off + stub_size - 1) & -stub_align)
9356                  - (stub_off & -stub_align))
9357                 > ((stub_size - 1) & -stub_align)))
9358           stub_off = (stub_off + stub_align - 1) & -stub_align;
9359         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9360         off -= stub_off + s->output_offset + s->output_section->vma;
9361         /* Note that for --plt-stub-align negative we have a possible
9362            dependency between stub offset and size.  Break that
9363            dependency by assuming the max stub size when calculating
9364            the stub offset.  */
9365         if (PPC_HA (off) == 0)
9366           stub_size -= 4;
9367         h->root.type = bfd_link_hash_defined;
9368         h->root.u.def.section = s;
9369         h->root.u.def.value = stub_off;
9370         s->size = stub_off + stub_size;
9371         break;
9372       }
9373   return TRUE;
9374 }
9375
9376 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9377    read-only sections.  */
9378
9379 static bfd_boolean
9380 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9381 {
9382   asection *sec;
9383
9384   if (h->root.type == bfd_link_hash_indirect)
9385     return TRUE;
9386
9387   sec = readonly_dynrelocs (h);
9388   if (sec != NULL)
9389     {
9390       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9391
9392       info->flags |= DF_TEXTREL;
9393       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9394                                 " in read-only section `%pA'\n"),
9395                               sec->owner, h->root.root.string, sec);
9396
9397       /* Not an error, just cut short the traversal.  */
9398       return FALSE;
9399     }
9400   return TRUE;
9401 }
9402
9403 /* Set the sizes of the dynamic sections.  */
9404
9405 static bfd_boolean
9406 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9407                                  struct bfd_link_info *info)
9408 {
9409   struct ppc_link_hash_table *htab;
9410   bfd *dynobj;
9411   asection *s;
9412   bfd_boolean relocs;
9413   bfd *ibfd;
9414   struct got_entry *first_tlsld;
9415
9416   htab = ppc_hash_table (info);
9417   if (htab == NULL)
9418     return FALSE;
9419
9420   dynobj = htab->elf.dynobj;
9421   if (dynobj == NULL)
9422     abort ();
9423
9424   if (htab->elf.dynamic_sections_created)
9425     {
9426       /* Set the contents of the .interp section to the interpreter.  */
9427       if (bfd_link_executable (info) && !info->nointerp)
9428         {
9429           s = bfd_get_linker_section (dynobj, ".interp");
9430           if (s == NULL)
9431             abort ();
9432           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9433           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9434         }
9435     }
9436
9437   /* Set up .got offsets for local syms, and space for local dynamic
9438      relocs.  */
9439   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9440     {
9441       struct got_entry **lgot_ents;
9442       struct got_entry **end_lgot_ents;
9443       struct plt_entry **local_plt;
9444       struct plt_entry **end_local_plt;
9445       unsigned char *lgot_masks;
9446       bfd_size_type locsymcount;
9447       Elf_Internal_Shdr *symtab_hdr;
9448
9449       if (!is_ppc64_elf (ibfd))
9450         continue;
9451
9452       for (s = ibfd->sections; s != NULL; s = s->next)
9453         {
9454           struct ppc_dyn_relocs *p;
9455
9456           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9457             {
9458               if (!bfd_is_abs_section (p->sec)
9459                   && bfd_is_abs_section (p->sec->output_section))
9460                 {
9461                   /* Input section has been discarded, either because
9462                      it is a copy of a linkonce section or due to
9463                      linker script /DISCARD/, so we'll be discarding
9464                      the relocs too.  */
9465                 }
9466               else if (p->count != 0)
9467                 {
9468                   asection *srel = elf_section_data (p->sec)->sreloc;
9469                   if (p->ifunc)
9470                     srel = htab->elf.irelplt;
9471                   srel->size += p->count * sizeof (Elf64_External_Rela);
9472                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9473                     info->flags |= DF_TEXTREL;
9474                 }
9475             }
9476         }
9477
9478       lgot_ents = elf_local_got_ents (ibfd);
9479       if (!lgot_ents)
9480         continue;
9481
9482       symtab_hdr = &elf_symtab_hdr (ibfd);
9483       locsymcount = symtab_hdr->sh_info;
9484       end_lgot_ents = lgot_ents + locsymcount;
9485       local_plt = (struct plt_entry **) end_lgot_ents;
9486       end_local_plt = local_plt + locsymcount;
9487       lgot_masks = (unsigned char *) end_local_plt;
9488       s = ppc64_elf_tdata (ibfd)->got;
9489       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9490         {
9491           struct got_entry **pent, *ent;
9492
9493           pent = lgot_ents;
9494           while ((ent = *pent) != NULL)
9495             if (ent->got.refcount > 0)
9496               {
9497                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9498                   {
9499                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9500                     *pent = ent->next;
9501                   }
9502                 else
9503                   {
9504                     unsigned int ent_size = 8;
9505                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9506
9507                     ent->got.offset = s->size;
9508                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9509                       {
9510                         ent_size *= 2;
9511                         rel_size *= 2;
9512                       }
9513                     s->size += ent_size;
9514                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9515                       {
9516                         htab->elf.irelplt->size += rel_size;
9517                         htab->got_reli_size += rel_size;
9518                       }
9519                     else if (bfd_link_pic (info)
9520                              && !((ent->tls_type & TLS_TPREL) != 0
9521                                   && bfd_link_executable (info)))
9522                       {
9523                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9524                         srel->size += rel_size;
9525                       }
9526                     pent = &ent->next;
9527                   }
9528               }
9529             else
9530               *pent = ent->next;
9531         }
9532
9533       /* Allocate space for plt calls to local syms.  */
9534       lgot_masks = (unsigned char *) end_local_plt;
9535       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9536         {
9537           struct plt_entry *ent;
9538
9539           for (ent = *local_plt; ent != NULL; ent = ent->next)
9540             if (ent->plt.refcount > 0)
9541               {
9542                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9543                   {
9544                     s = htab->elf.iplt;
9545                     ent->plt.offset = s->size;
9546                     s->size += PLT_ENTRY_SIZE (htab);
9547                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9548                   }
9549                 else if (htab->can_convert_all_inline_plt
9550                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9551                   ent->plt.offset = (bfd_vma) -1;
9552                 else
9553                   {
9554                     s = htab->pltlocal;
9555                     ent->plt.offset = s->size;
9556                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9557                     if (bfd_link_pic (info))
9558                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9559                   }
9560               }
9561             else
9562               ent->plt.offset = (bfd_vma) -1;
9563         }
9564     }
9565
9566   /* Allocate global sym .plt and .got entries, and space for global
9567      sym dynamic relocs.  */
9568   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9569
9570   if (!htab->opd_abi && !bfd_link_pic (info))
9571     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9572
9573   first_tlsld = NULL;
9574   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9575     {
9576       struct got_entry *ent;
9577
9578       if (!is_ppc64_elf (ibfd))
9579         continue;
9580
9581       ent = ppc64_tlsld_got (ibfd);
9582       if (ent->got.refcount > 0)
9583         {
9584           if (!htab->do_multi_toc && first_tlsld != NULL)
9585             {
9586               ent->is_indirect = TRUE;
9587               ent->got.ent = first_tlsld;
9588             }
9589           else
9590             {
9591               if (first_tlsld == NULL)
9592                 first_tlsld = ent;
9593               s = ppc64_elf_tdata (ibfd)->got;
9594               ent->got.offset = s->size;
9595               ent->owner = ibfd;
9596               s->size += 16;
9597               if (bfd_link_pic (info))
9598                 {
9599                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9600                   srel->size += sizeof (Elf64_External_Rela);
9601                 }
9602             }
9603         }
9604       else
9605         ent->got.offset = (bfd_vma) -1;
9606     }
9607
9608   /* We now have determined the sizes of the various dynamic sections.
9609      Allocate memory for them.  */
9610   relocs = FALSE;
9611   for (s = dynobj->sections; s != NULL; s = s->next)
9612     {
9613       if ((s->flags & SEC_LINKER_CREATED) == 0)
9614         continue;
9615
9616       if (s == htab->brlt || s == htab->relbrlt)
9617         /* These haven't been allocated yet;  don't strip.  */
9618         continue;
9619       else if (s == htab->elf.sgot
9620                || s == htab->elf.splt
9621                || s == htab->elf.iplt
9622                || s == htab->pltlocal
9623                || s == htab->glink
9624                || s == htab->global_entry
9625                || s == htab->elf.sdynbss
9626                || s == htab->elf.sdynrelro)
9627         {
9628           /* Strip this section if we don't need it; see the
9629              comment below.  */
9630         }
9631       else if (s == htab->glink_eh_frame)
9632         {
9633           if (!bfd_is_abs_section (s->output_section))
9634             /* Not sized yet.  */
9635             continue;
9636         }
9637       else if (CONST_STRNEQ (s->name, ".rela"))
9638         {
9639           if (s->size != 0)
9640             {
9641               if (s != htab->elf.srelplt)
9642                 relocs = TRUE;
9643
9644               /* We use the reloc_count field as a counter if we need
9645                  to copy relocs into the output file.  */
9646               s->reloc_count = 0;
9647             }
9648         }
9649       else
9650         {
9651           /* It's not one of our sections, so don't allocate space.  */
9652           continue;
9653         }
9654
9655       if (s->size == 0)
9656         {
9657           /* If we don't need this section, strip it from the
9658              output file.  This is mostly to handle .rela.bss and
9659              .rela.plt.  We must create both sections in
9660              create_dynamic_sections, because they must be created
9661              before the linker maps input sections to output
9662              sections.  The linker does that before
9663              adjust_dynamic_symbol is called, and it is that
9664              function which decides whether anything needs to go
9665              into these sections.  */
9666           s->flags |= SEC_EXCLUDE;
9667           continue;
9668         }
9669
9670       if (bfd_is_abs_section (s->output_section))
9671         _bfd_error_handler (_("warning: discarding dynamic section %s"),
9672                             s->name);
9673
9674       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9675         continue;
9676
9677       /* Allocate memory for the section contents.  We use bfd_zalloc
9678          here in case unused entries are not reclaimed before the
9679          section's contents are written out.  This should not happen,
9680          but this way if it does we get a R_PPC64_NONE reloc in .rela
9681          sections instead of garbage.
9682          We also rely on the section contents being zero when writing
9683          the GOT and .dynrelro.  */
9684       s->contents = bfd_zalloc (dynobj, s->size);
9685       if (s->contents == NULL)
9686         return FALSE;
9687     }
9688
9689   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9690     {
9691       if (!is_ppc64_elf (ibfd))
9692         continue;
9693
9694       s = ppc64_elf_tdata (ibfd)->got;
9695       if (s != NULL && s != htab->elf.sgot)
9696         {
9697           if (s->size == 0)
9698             s->flags |= SEC_EXCLUDE;
9699           else
9700             {
9701               s->contents = bfd_zalloc (ibfd, s->size);
9702               if (s->contents == NULL)
9703                 return FALSE;
9704             }
9705         }
9706       s = ppc64_elf_tdata (ibfd)->relgot;
9707       if (s != NULL)
9708         {
9709           if (s->size == 0)
9710             s->flags |= SEC_EXCLUDE;
9711           else
9712             {
9713               s->contents = bfd_zalloc (ibfd, s->size);
9714               if (s->contents == NULL)
9715                 return FALSE;
9716               relocs = TRUE;
9717               s->reloc_count = 0;
9718             }
9719         }
9720     }
9721
9722   if (htab->elf.dynamic_sections_created)
9723     {
9724       bfd_boolean tls_opt;
9725
9726       /* Add some entries to the .dynamic section.  We fill in the
9727          values later, in ppc64_elf_finish_dynamic_sections, but we
9728          must add the entries now so that we get the correct size for
9729          the .dynamic section.  The DT_DEBUG entry is filled in by the
9730          dynamic linker and used by the debugger.  */
9731 #define add_dynamic_entry(TAG, VAL) \
9732   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9733
9734       if (bfd_link_executable (info))
9735         {
9736           if (!add_dynamic_entry (DT_DEBUG, 0))
9737             return FALSE;
9738         }
9739
9740       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9741         {
9742           if (!add_dynamic_entry (DT_PLTGOT, 0)
9743               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9744               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9745               || !add_dynamic_entry (DT_JMPREL, 0)
9746               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9747             return FALSE;
9748         }
9749
9750       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9751         {
9752           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9753               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9754             return FALSE;
9755         }
9756
9757       tls_opt = (htab->params->tls_get_addr_opt
9758                  && htab->tls_get_addr_fd != NULL
9759                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9760       if (tls_opt || !htab->opd_abi)
9761         {
9762           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9763             return FALSE;
9764         }
9765
9766       if (relocs)
9767         {
9768           if (!add_dynamic_entry (DT_RELA, 0)
9769               || !add_dynamic_entry (DT_RELASZ, 0)
9770               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9771             return FALSE;
9772
9773           /* If any dynamic relocs apply to a read-only section,
9774              then we need a DT_TEXTREL entry.  */
9775           if ((info->flags & DF_TEXTREL) == 0)
9776             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9777
9778           if ((info->flags & DF_TEXTREL) != 0)
9779             {
9780               if (!add_dynamic_entry (DT_TEXTREL, 0))
9781                 return FALSE;
9782             }
9783         }
9784     }
9785 #undef add_dynamic_entry
9786
9787   return TRUE;
9788 }
9789
9790 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
9791
9792 static bfd_boolean
9793 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9794 {
9795   if (h->plt.plist != NULL
9796       && !h->def_regular
9797       && !h->pointer_equality_needed)
9798     return FALSE;
9799
9800   return _bfd_elf_hash_symbol (h);
9801 }
9802
9803 /* Determine the type of stub needed, if any, for a call.  */
9804
9805 static inline enum ppc_stub_type
9806 ppc_type_of_stub (asection *input_sec,
9807                   const Elf_Internal_Rela *rel,
9808                   struct ppc_link_hash_entry **hash,
9809                   struct plt_entry **plt_ent,
9810                   bfd_vma destination,
9811                   unsigned long local_off)
9812 {
9813   struct ppc_link_hash_entry *h = *hash;
9814   bfd_vma location;
9815   bfd_vma branch_offset;
9816   bfd_vma max_branch_offset;
9817   enum elf_ppc64_reloc_type r_type;
9818
9819   if (h != NULL)
9820     {
9821       struct plt_entry *ent;
9822       struct ppc_link_hash_entry *fdh = h;
9823       if (h->oh != NULL
9824           && h->oh->is_func_descriptor)
9825         {
9826           fdh = ppc_follow_link (h->oh);
9827           *hash = fdh;
9828         }
9829
9830       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9831         if (ent->addend == rel->r_addend
9832             && ent->plt.offset != (bfd_vma) -1)
9833           {
9834             *plt_ent = ent;
9835             return ppc_stub_plt_call;
9836           }
9837
9838       /* Here, we know we don't have a plt entry.  If we don't have a
9839          either a defined function descriptor or a defined entry symbol
9840          in a regular object file, then it is pointless trying to make
9841          any other type of stub.  */
9842       if (!is_static_defined (&fdh->elf)
9843           && !is_static_defined (&h->elf))
9844         return ppc_stub_none;
9845     }
9846   else if (elf_local_got_ents (input_sec->owner) != NULL)
9847     {
9848       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9849       struct plt_entry **local_plt = (struct plt_entry **)
9850         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9851       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9852
9853       if (local_plt[r_symndx] != NULL)
9854         {
9855           struct plt_entry *ent;
9856
9857           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9858             if (ent->addend == rel->r_addend
9859                 && ent->plt.offset != (bfd_vma) -1)
9860               {
9861                 *plt_ent = ent;
9862                 return ppc_stub_plt_call;
9863               }
9864         }
9865     }
9866
9867   /* Determine where the call point is.  */
9868   location = (input_sec->output_offset
9869               + input_sec->output_section->vma
9870               + rel->r_offset);
9871
9872   branch_offset = destination - location;
9873   r_type = ELF64_R_TYPE (rel->r_info);
9874
9875   /* Determine if a long branch stub is needed.  */
9876   max_branch_offset = 1 << 25;
9877   if (r_type == R_PPC64_REL14
9878       || r_type == R_PPC64_REL14_BRTAKEN
9879       || r_type == R_PPC64_REL14_BRNTAKEN)
9880     max_branch_offset = 1 << 15;
9881
9882   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9883     /* We need a stub.  Figure out whether a long_branch or plt_branch
9884        is needed later.  */
9885     return ppc_stub_long_branch;
9886
9887   return ppc_stub_none;
9888 }
9889
9890 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
9891    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9892    .    mflr    %r12
9893    .    bcl     20,31,1f
9894    .1:  mflr    %r11
9895    .    mtlr    %r12
9896    .    lis     %r12,xxx-1b@highest
9897    .    ori     %r12,%r12,xxx-1b@higher
9898    .    sldi    %r12,%r12,32
9899    .    oris    %r12,%r12,xxx-1b@high
9900    .    ori     %r12,%r12,xxx-1b@l
9901    .    add/ldx %r12,%r11,%r12  */
9902
9903 static bfd_byte *
9904 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9905 {
9906   bfd_put_32 (abfd, MFLR_R12, p);
9907   p += 4;
9908   bfd_put_32 (abfd, BCL_20_31, p);
9909   p += 4;
9910   bfd_put_32 (abfd, MFLR_R11, p);
9911   p += 4;
9912   bfd_put_32 (abfd, MTLR_R12, p);
9913   p += 4;
9914   if (off + 0x8000 < 0x10000)
9915     {
9916       if (load)
9917         bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9918       else
9919         bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9920       p += 4;
9921     }
9922   else if (off + 0x80008000ULL < 0x100000000ULL)
9923     {
9924       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9925       p += 4;
9926       if (load)
9927         bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9928       else
9929         bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9930       p += 4;
9931     }
9932   else
9933     {
9934       if (off + 0x800000000000ULL < 0x1000000000000ULL)
9935         {
9936           bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9937           p += 4;
9938         }
9939       else
9940         {
9941           bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9942           p += 4;
9943           if (((off >> 32) & 0xffff) != 0)
9944             {
9945               bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9946               p += 4;
9947             }
9948         }
9949       if (((off >> 32) & 0xffffffffULL) != 0)
9950         {
9951           bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9952           p += 4;
9953         }
9954       if (PPC_HI (off) != 0)
9955         {
9956           bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9957           p += 4;
9958         }
9959       if (PPC_LO (off) != 0)
9960         {
9961           bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9962           p += 4;
9963         }
9964       if (load)
9965         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9966       else
9967         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9968       p += 4;
9969     }
9970   return p;
9971 }
9972
9973 static unsigned int
9974 size_offset (bfd_vma off)
9975 {
9976   unsigned int size;
9977   if (off + 0x8000 < 0x10000)
9978     size = 4;
9979   else if (off + 0x80008000ULL < 0x100000000ULL)
9980     size = 8;
9981   else
9982     {
9983       if (off + 0x800000000000ULL < 0x1000000000000ULL)
9984         size = 4;
9985       else
9986         {
9987           size = 4;
9988           if (((off >> 32) & 0xffff) != 0)
9989             size += 4;
9990         }
9991       if (((off >> 32) & 0xffffffffULL) != 0)
9992         size += 4;
9993       if (PPC_HI (off) != 0)
9994         size += 4;
9995       if (PPC_LO (off) != 0)
9996         size += 4;
9997       size += 4;
9998     }
9999   return size + 16;
10000 }
10001
10002 static unsigned int
10003 num_relocs_for_offset (bfd_vma off)
10004 {
10005   unsigned int num_rel;
10006   if (off + 0x8000 < 0x10000)
10007     num_rel = 1;
10008   else if (off + 0x80008000ULL < 0x100000000ULL)
10009     num_rel = 2;
10010   else
10011     {
10012       num_rel = 1;
10013       if (off + 0x800000000000ULL >= 0x1000000000000ULL
10014           && ((off >> 32) & 0xffff) != 0)
10015         num_rel += 1;
10016       if (PPC_HI (off) != 0)
10017         num_rel += 1;
10018       if (PPC_LO (off) != 0)
10019         num_rel += 1;
10020     }
10021   return num_rel;
10022 }
10023
10024 static Elf_Internal_Rela *
10025 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10026                         bfd_vma roff, bfd_vma targ, bfd_vma off)
10027 {
10028   bfd_vma relative_targ = targ - (roff - 8);
10029   if (bfd_big_endian (info->output_bfd))
10030     roff += 2;
10031   r->r_offset = roff;
10032   r->r_addend = relative_targ + roff;
10033   if (off + 0x8000 < 0x10000)
10034     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10035   else if (off + 0x80008000ULL < 0x100000000ULL)
10036     {
10037       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10038       ++r;
10039       roff += 4;
10040       r->r_offset = roff;
10041       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10042       r->r_addend = relative_targ + roff;
10043     }
10044   else
10045     {
10046       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10047         r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10048       else
10049         {
10050           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10051           if (((off >> 32) & 0xffff) != 0)
10052             {
10053               ++r;
10054               roff += 4;
10055               r->r_offset = roff;
10056               r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10057               r->r_addend = relative_targ + roff;
10058             }
10059         }
10060       if (((off >> 32) & 0xffffffffULL) != 0)
10061         roff += 4;
10062       if (PPC_HI (off) != 0)
10063         {
10064           ++r;
10065           roff += 4;
10066           r->r_offset = roff;
10067           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10068           r->r_addend = relative_targ + roff;
10069         }
10070       if (PPC_LO (off) != 0)
10071         {
10072           ++r;
10073           roff += 4;
10074           r->r_offset = roff;
10075           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10076           r->r_addend = relative_targ + roff;
10077         }
10078     }
10079   return r;
10080 }
10081
10082 /* Emit .eh_frame opcode to advance pc by DELTA.  */
10083
10084 static bfd_byte *
10085 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10086 {
10087   delta /= 4;
10088   if (delta < 64)
10089     *eh++ = DW_CFA_advance_loc + delta;
10090   else if (delta < 256)
10091     {
10092       *eh++ = DW_CFA_advance_loc1;
10093       *eh++ = delta;
10094     }
10095   else if (delta < 65536)
10096     {
10097       *eh++ = DW_CFA_advance_loc2;
10098       bfd_put_16 (abfd, delta, eh);
10099       eh += 2;
10100     }
10101   else
10102     {
10103       *eh++ = DW_CFA_advance_loc4;
10104       bfd_put_32 (abfd, delta, eh);
10105       eh += 4;
10106     }
10107   return eh;
10108 }
10109
10110 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
10111
10112 static unsigned int
10113 eh_advance_size (unsigned int delta)
10114 {
10115   if (delta < 64 * 4)
10116     /* DW_CFA_advance_loc+[1..63].  */
10117     return 1;
10118   if (delta < 256 * 4)
10119     /* DW_CFA_advance_loc1, byte.  */
10120     return 2;
10121   if (delta < 65536 * 4)
10122     /* DW_CFA_advance_loc2, 2 bytes.  */
10123     return 3;
10124   /* DW_CFA_advance_loc4, 4 bytes.  */
10125   return 5;
10126 }
10127
10128 /* With power7 weakly ordered memory model, it is possible for ld.so
10129    to update a plt entry in one thread and have another thread see a
10130    stale zero toc entry.  To avoid this we need some sort of acquire
10131    barrier in the call stub.  One solution is to make the load of the
10132    toc word seem to appear to depend on the load of the function entry
10133    word.  Another solution is to test for r2 being zero, and branch to
10134    the appropriate glink entry if so.
10135
10136    .    fake dep barrier        compare
10137    .    ld 12,xxx(2)            ld 12,xxx(2)
10138    .    mtctr 12                mtctr 12
10139    .    xor 11,12,12            ld 2,xxx+8(2)
10140    .    add 2,2,11              cmpldi 2,0
10141    .    ld 2,xxx+8(2)           bnectr+
10142    .    bctr                    b <glink_entry>
10143
10144    The solution involving the compare turns out to be faster, so
10145    that's what we use unless the branch won't reach.  */
10146
10147 #define ALWAYS_USE_FAKE_DEP 0
10148 #define ALWAYS_EMIT_R2SAVE 0
10149
10150 static inline unsigned int
10151 plt_stub_size (struct ppc_link_hash_table *htab,
10152                struct ppc_stub_hash_entry *stub_entry,
10153                bfd_vma off)
10154 {
10155   unsigned size;
10156
10157   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10158     {
10159       size = 8 + size_offset (off - 8);
10160       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10161         size += 4;
10162       return size;
10163     }
10164
10165   size = 12;
10166   if (ALWAYS_EMIT_R2SAVE
10167       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10168     size += 4;
10169   if (PPC_HA (off) != 0)
10170     size += 4;
10171   if (htab->opd_abi)
10172     {
10173       size += 4;
10174       if (htab->params->plt_static_chain)
10175         size += 4;
10176       if (htab->params->plt_thread_safe
10177           && htab->elf.dynamic_sections_created
10178           && stub_entry->h != NULL
10179           && stub_entry->h->elf.dynindx != -1)
10180         size += 8;
10181       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10182         size += 4;
10183     }
10184   if (stub_entry->h != NULL
10185       && (stub_entry->h == htab->tls_get_addr_fd
10186           || stub_entry->h == htab->tls_get_addr)
10187       && htab->params->tls_get_addr_opt)
10188     {
10189       size += 7 * 4;
10190       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10191         size += 6 * 4;
10192     }
10193   return size;
10194 }
10195
10196 /* Depending on the sign of plt_stub_align:
10197    If positive, return the padding to align to a 2**plt_stub_align
10198    boundary.
10199    If negative, if this stub would cross fewer 2**plt_stub_align
10200    boundaries if we align, then return the padding needed to do so.  */
10201
10202 static inline unsigned int
10203 plt_stub_pad (struct ppc_link_hash_table *htab,
10204               struct ppc_stub_hash_entry *stub_entry,
10205               bfd_vma plt_off)
10206 {
10207   int stub_align;
10208   unsigned stub_size;
10209   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10210
10211   if (htab->params->plt_stub_align >= 0)
10212     {
10213       stub_align = 1 << htab->params->plt_stub_align;
10214       if ((stub_off & (stub_align - 1)) != 0)
10215         return stub_align - (stub_off & (stub_align - 1));
10216       return 0;
10217     }
10218
10219   stub_align = 1 << -htab->params->plt_stub_align;
10220   stub_size = plt_stub_size (htab, stub_entry, plt_off);
10221   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10222       > ((stub_size - 1) & -stub_align))
10223     return stub_align - (stub_off & (stub_align - 1));
10224   return 0;
10225 }
10226
10227 /* Build a .plt call stub.  */
10228
10229 static inline bfd_byte *
10230 build_plt_stub (struct ppc_link_hash_table *htab,
10231                 struct ppc_stub_hash_entry *stub_entry,
10232                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10233 {
10234   bfd *obfd = htab->params->stub_bfd;
10235   bfd_boolean plt_load_toc = htab->opd_abi;
10236   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10237   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10238                                  && htab->elf.dynamic_sections_created
10239                                  && stub_entry->h != NULL
10240                                  && stub_entry->h->elf.dynindx != -1);
10241   bfd_boolean use_fake_dep = plt_thread_safe;
10242   bfd_vma cmp_branch_off = 0;
10243
10244   if (!ALWAYS_USE_FAKE_DEP
10245       && plt_load_toc
10246       && plt_thread_safe
10247       && !((stub_entry->h == htab->tls_get_addr_fd
10248             || stub_entry->h == htab->tls_get_addr)
10249            && htab->params->tls_get_addr_opt))
10250     {
10251       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10252       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10253                           / PLT_ENTRY_SIZE (htab));
10254       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10255       bfd_vma to, from;
10256
10257       if (pltindex > 32768)
10258         glinkoff += (pltindex - 32768) * 4;
10259       to = (glinkoff
10260             + htab->glink->output_offset
10261             + htab->glink->output_section->vma);
10262       from = (p - stub_entry->group->stub_sec->contents
10263               + 4 * (ALWAYS_EMIT_R2SAVE
10264                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10265               + 4 * (PPC_HA (offset) != 0)
10266               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10267                      != PPC_HA (offset))
10268               + 4 * (plt_static_chain != 0)
10269               + 20
10270               + stub_entry->group->stub_sec->output_offset
10271               + stub_entry->group->stub_sec->output_section->vma);
10272       cmp_branch_off = to - from;
10273       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10274     }
10275
10276   if (PPC_HA (offset) != 0)
10277     {
10278       if (r != NULL)
10279         {
10280           if (ALWAYS_EMIT_R2SAVE
10281               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10282             r[0].r_offset += 4;
10283           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10284           r[1].r_offset = r[0].r_offset + 4;
10285           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10286           r[1].r_addend = r[0].r_addend;
10287           if (plt_load_toc)
10288             {
10289               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10290                 {
10291                   r[2].r_offset = r[1].r_offset + 4;
10292                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10293                   r[2].r_addend = r[0].r_addend;
10294                 }
10295               else
10296                 {
10297                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10298                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10299                   r[2].r_addend = r[0].r_addend + 8;
10300                   if (plt_static_chain)
10301                     {
10302                       r[3].r_offset = r[2].r_offset + 4;
10303                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10304                       r[3].r_addend = r[0].r_addend + 16;
10305                     }
10306                 }
10307             }
10308         }
10309       if (ALWAYS_EMIT_R2SAVE
10310           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10311         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10312       if (plt_load_toc)
10313         {
10314           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10315           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10316         }
10317       else
10318         {
10319           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10320           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10321         }
10322       if (plt_load_toc
10323           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10324         {
10325           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10326           offset = 0;
10327         }
10328       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10329       if (plt_load_toc)
10330         {
10331           if (use_fake_dep)
10332             {
10333               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10334               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10335             }
10336           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10337           if (plt_static_chain)
10338             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10339         }
10340     }
10341   else
10342     {
10343       if (r != NULL)
10344         {
10345           if (ALWAYS_EMIT_R2SAVE
10346               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10347             r[0].r_offset += 4;
10348           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10349           if (plt_load_toc)
10350             {
10351               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10352                 {
10353                   r[1].r_offset = r[0].r_offset + 4;
10354                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10355                   r[1].r_addend = r[0].r_addend;
10356                 }
10357               else
10358                 {
10359                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10360                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10361                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10362                   if (plt_static_chain)
10363                     {
10364                       r[2].r_offset = r[1].r_offset + 4;
10365                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10366                       r[2].r_addend = r[0].r_addend + 8;
10367                     }
10368                 }
10369             }
10370         }
10371       if (ALWAYS_EMIT_R2SAVE
10372           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10373         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10374       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10375       if (plt_load_toc
10376           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10377         {
10378           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10379           offset = 0;
10380         }
10381       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10382       if (plt_load_toc)
10383         {
10384           if (use_fake_dep)
10385             {
10386               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10387               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10388             }
10389           if (plt_static_chain)
10390             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10391           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10392         }
10393     }
10394   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10395     {
10396       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10397       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10398       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10399     }
10400   else
10401     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10402   return p;
10403 }
10404
10405 /* Build a special .plt call stub for __tls_get_addr.  */
10406
10407 #define LD_R11_0R3      0xe9630000
10408 #define LD_R12_0R3      0xe9830000
10409 #define MR_R0_R3        0x7c601b78
10410 #define CMPDI_R11_0     0x2c2b0000
10411 #define ADD_R3_R12_R13  0x7c6c6a14
10412 #define BEQLR           0x4d820020
10413 #define MR_R3_R0        0x7c030378
10414 #define STD_R11_0R1     0xf9610000
10415 #define BCTRL           0x4e800421
10416 #define LD_R11_0R1      0xe9610000
10417 #define MTLR_R11        0x7d6803a6
10418
10419 static inline bfd_byte *
10420 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10421                          struct ppc_stub_hash_entry *stub_entry,
10422                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10423 {
10424   bfd *obfd = htab->params->stub_bfd;
10425   bfd_byte *loc = p;
10426
10427   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10428   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10429   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10430   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10431   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10432   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10433   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10434   if (r != NULL)
10435     r[0].r_offset += 7 * 4;
10436   if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10437     return build_plt_stub (htab, stub_entry, p, offset, r);
10438
10439   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10440   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10441
10442   if (r != NULL)
10443     r[0].r_offset += 2 * 4;
10444   p = build_plt_stub (htab, stub_entry, p, offset, r);
10445   bfd_put_32 (obfd, BCTRL, p - 4);
10446
10447   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10448   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10449   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10450   bfd_put_32 (obfd, BLR, p),                    p += 4;
10451
10452   if (htab->glink_eh_frame != NULL
10453       && htab->glink_eh_frame->size != 0)
10454     {
10455       bfd_byte *base, *eh;
10456       unsigned int lr_used, delta;
10457
10458       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10459       eh = base + stub_entry->group->eh_size;
10460       lr_used = stub_entry->stub_offset + (p - 20 - loc);
10461       delta = lr_used - stub_entry->group->lr_restore;
10462       stub_entry->group->lr_restore = lr_used + 16;
10463       eh = eh_advance (htab->elf.dynobj, eh, delta);
10464       *eh++ = DW_CFA_offset_extended_sf;
10465       *eh++ = 65;
10466       *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10467       *eh++ = DW_CFA_advance_loc + 4;
10468       *eh++ = DW_CFA_restore_extended;
10469       *eh++ = 65;
10470       stub_entry->group->eh_size = eh - base;
10471     }
10472   return p;
10473 }
10474
10475 static Elf_Internal_Rela *
10476 get_relocs (asection *sec, int count)
10477 {
10478   Elf_Internal_Rela *relocs;
10479   struct bfd_elf_section_data *elfsec_data;
10480
10481   elfsec_data = elf_section_data (sec);
10482   relocs = elfsec_data->relocs;
10483   if (relocs == NULL)
10484     {
10485       bfd_size_type relsize;
10486       relsize = sec->reloc_count * sizeof (*relocs);
10487       relocs = bfd_alloc (sec->owner, relsize);
10488       if (relocs == NULL)
10489         return NULL;
10490       elfsec_data->relocs = relocs;
10491       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10492                                           sizeof (Elf_Internal_Shdr));
10493       if (elfsec_data->rela.hdr == NULL)
10494         return NULL;
10495       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10496                                         * sizeof (Elf64_External_Rela));
10497       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10498       sec->reloc_count = 0;
10499     }
10500   relocs += sec->reloc_count;
10501   sec->reloc_count += count;
10502   return relocs;
10503 }
10504
10505 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10506    forms, to the equivalent relocs against the global symbol given by
10507    STUB_ENTRY->H.  */
10508
10509 static bfd_boolean
10510 use_global_in_relocs (struct ppc_link_hash_table *htab,
10511                       struct ppc_stub_hash_entry *stub_entry,
10512                       Elf_Internal_Rela *r, unsigned int num_rel)
10513 {
10514   struct elf_link_hash_entry **hashes;
10515   unsigned long symndx;
10516   struct ppc_link_hash_entry *h;
10517   bfd_vma symval;
10518
10519   /* Relocs are always against symbols in their own object file.  Fake
10520      up global sym hashes for the stub bfd (which has no symbols).  */
10521   hashes = elf_sym_hashes (htab->params->stub_bfd);
10522   if (hashes == NULL)
10523     {
10524       bfd_size_type hsize;
10525
10526       /* When called the first time, stub_globals will contain the
10527          total number of symbols seen during stub sizing.  After
10528          allocating, stub_globals is used as an index to fill the
10529          hashes array.  */
10530       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10531       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10532       if (hashes == NULL)
10533         return FALSE;
10534       elf_sym_hashes (htab->params->stub_bfd) = hashes;
10535       htab->stub_globals = 1;
10536     }
10537   symndx = htab->stub_globals++;
10538   h = stub_entry->h;
10539   hashes[symndx] = &h->elf;
10540   if (h->oh != NULL && h->oh->is_func)
10541     h = ppc_follow_link (h->oh);
10542   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10543               || h->elf.root.type == bfd_link_hash_defweak);
10544   symval = (h->elf.root.u.def.value
10545             + h->elf.root.u.def.section->output_offset
10546             + h->elf.root.u.def.section->output_section->vma);
10547   while (num_rel-- != 0)
10548     {
10549       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10550       if (h->elf.root.u.def.section != stub_entry->target_section)
10551         {
10552           /* H is an opd symbol.  The addend must be zero, and the
10553              branch reloc is the only one we can convert.  */
10554           r->r_addend = 0;
10555           break;
10556         }
10557       else
10558         r->r_addend -= symval;
10559       --r;
10560     }
10561   return TRUE;
10562 }
10563
10564 static bfd_vma
10565 get_r2off (struct bfd_link_info *info,
10566            struct ppc_stub_hash_entry *stub_entry)
10567 {
10568   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10569   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10570
10571   if (r2off == 0)
10572     {
10573       /* Support linking -R objects.  Get the toc pointer from the
10574          opd entry.  */
10575       char buf[8];
10576       if (!htab->opd_abi)
10577         return r2off;
10578       asection *opd = stub_entry->h->elf.root.u.def.section;
10579       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10580
10581       if (strcmp (opd->name, ".opd") != 0
10582           || opd->reloc_count != 0)
10583         {
10584           info->callbacks->einfo
10585             (_("%P: cannot find opd entry toc for `%pT'\n"),
10586              stub_entry->h->elf.root.root.string);
10587           bfd_set_error (bfd_error_bad_value);
10588           return (bfd_vma) -1;
10589         }
10590       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10591         return (bfd_vma) -1;
10592       r2off = bfd_get_64 (opd->owner, buf);
10593       r2off -= elf_gp (info->output_bfd);
10594     }
10595   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10596   return r2off;
10597 }
10598
10599 static bfd_boolean
10600 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10601 {
10602   struct ppc_stub_hash_entry *stub_entry;
10603   struct ppc_branch_hash_entry *br_entry;
10604   struct bfd_link_info *info;
10605   struct ppc_link_hash_table *htab;
10606   bfd_byte *loc;
10607   bfd_byte *p, *relp;
10608   bfd_vma targ, off;
10609   Elf_Internal_Rela *r;
10610   asection *plt;
10611   int num_rel;
10612
10613   /* Massage our args to the form they really have.  */
10614   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10615   info = in_arg;
10616
10617   htab = ppc_hash_table (info);
10618   if (htab == NULL)
10619     return FALSE;
10620
10621   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10622   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10623
10624   htab->stub_count[stub_entry->stub_type - 1] += 1;
10625   switch (stub_entry->stub_type)
10626     {
10627     case ppc_stub_long_branch:
10628     case ppc_stub_long_branch_r2off:
10629       /* Branches are relative.  This is where we are going to.  */
10630       targ = (stub_entry->target_value
10631               + stub_entry->target_section->output_offset
10632               + stub_entry->target_section->output_section->vma);
10633       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10634
10635       /* And this is where we are coming from.  */
10636       off = (stub_entry->stub_offset
10637              + stub_entry->group->stub_sec->output_offset
10638              + stub_entry->group->stub_sec->output_section->vma);
10639       off = targ - off;
10640
10641       p = loc;
10642       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10643         {
10644           bfd_vma r2off = get_r2off (info, stub_entry);
10645
10646           if (r2off == (bfd_vma) -1)
10647             {
10648               htab->stub_error = TRUE;
10649               return FALSE;
10650             }
10651           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10652           p += 4;
10653           if (PPC_HA (r2off) != 0)
10654             {
10655               bfd_put_32 (htab->params->stub_bfd,
10656                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10657               p += 4;
10658             }
10659           if (PPC_LO (r2off) != 0)
10660             {
10661               bfd_put_32 (htab->params->stub_bfd,
10662                           ADDI_R2_R2 | PPC_LO (r2off), p);
10663               p += 4;
10664             }
10665           off -= p - loc;
10666         }
10667       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10668       p += 4;
10669
10670       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10671         {
10672           _bfd_error_handler
10673             (_("long branch stub `%s' offset overflow"),
10674              stub_entry->root.string);
10675           htab->stub_error = TRUE;
10676           return FALSE;
10677         }
10678
10679       if (info->emitrelocations)
10680         {
10681           r = get_relocs (stub_entry->group->stub_sec, 1);
10682           if (r == NULL)
10683             return FALSE;
10684           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10685           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10686           r->r_addend = targ;
10687           if (stub_entry->h != NULL
10688               && !use_global_in_relocs (htab, stub_entry, r, 1))
10689             return FALSE;
10690         }
10691       break;
10692
10693     case ppc_stub_plt_branch:
10694     case ppc_stub_plt_branch_r2off:
10695       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10696                                          stub_entry->root.string + 9,
10697                                          FALSE, FALSE);
10698       if (br_entry == NULL)
10699         {
10700           _bfd_error_handler (_("can't find branch stub `%s'"),
10701                               stub_entry->root.string);
10702           htab->stub_error = TRUE;
10703           return FALSE;
10704         }
10705
10706       targ = (stub_entry->target_value
10707               + stub_entry->target_section->output_offset
10708               + stub_entry->target_section->output_section->vma);
10709       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10710         targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10711
10712       bfd_put_64 (htab->brlt->owner, targ,
10713                   htab->brlt->contents + br_entry->offset);
10714
10715       if (br_entry->iter == htab->stub_iteration)
10716         {
10717           br_entry->iter = 0;
10718
10719           if (htab->relbrlt != NULL)
10720             {
10721               /* Create a reloc for the branch lookup table entry.  */
10722               Elf_Internal_Rela rela;
10723               bfd_byte *rl;
10724
10725               rela.r_offset = (br_entry->offset
10726                                + htab->brlt->output_offset
10727                                + htab->brlt->output_section->vma);
10728               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10729               rela.r_addend = targ;
10730
10731               rl = htab->relbrlt->contents;
10732               rl += (htab->relbrlt->reloc_count++
10733                      * sizeof (Elf64_External_Rela));
10734               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10735             }
10736           else if (info->emitrelocations)
10737             {
10738               r = get_relocs (htab->brlt, 1);
10739               if (r == NULL)
10740                 return FALSE;
10741               /* brlt, being SEC_LINKER_CREATED does not go through the
10742                  normal reloc processing.  Symbols and offsets are not
10743                  translated from input file to output file form, so
10744                  set up the offset per the output file.  */
10745               r->r_offset = (br_entry->offset
10746                              + htab->brlt->output_offset
10747                              + htab->brlt->output_section->vma);
10748               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10749               r->r_addend = targ;
10750             }
10751         }
10752
10753       targ = (br_entry->offset
10754               + htab->brlt->output_offset
10755               + htab->brlt->output_section->vma);
10756
10757       off = (elf_gp (info->output_bfd)
10758              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10759       off = targ - off;
10760
10761       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10762         {
10763           info->callbacks->einfo
10764             (_("%P: linkage table error against `%pT'\n"),
10765              stub_entry->root.string);
10766           bfd_set_error (bfd_error_bad_value);
10767           htab->stub_error = TRUE;
10768           return FALSE;
10769         }
10770
10771       if (info->emitrelocations)
10772         {
10773           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10774           if (r == NULL)
10775             return FALSE;
10776           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10777           if (bfd_big_endian (info->output_bfd))
10778             r[0].r_offset += 2;
10779           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10780             r[0].r_offset += 4;
10781           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10782           r[0].r_addend = targ;
10783           if (PPC_HA (off) != 0)
10784             {
10785               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10786               r[1].r_offset = r[0].r_offset + 4;
10787               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10788               r[1].r_addend = r[0].r_addend;
10789             }
10790         }
10791
10792       p = loc;
10793       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10794         {
10795           if (PPC_HA (off) != 0)
10796             {
10797               bfd_put_32 (htab->params->stub_bfd,
10798                           ADDIS_R12_R2 | PPC_HA (off), p);
10799               p += 4;
10800               bfd_put_32 (htab->params->stub_bfd,
10801                           LD_R12_0R12 | PPC_LO (off), p);
10802             }
10803           else
10804             bfd_put_32 (htab->params->stub_bfd,
10805                         LD_R12_0R2 | PPC_LO (off), p);
10806         }
10807       else
10808         {
10809           bfd_vma r2off = get_r2off (info, stub_entry);
10810
10811           if (r2off == (bfd_vma) -1)
10812             {
10813               htab->stub_error = TRUE;
10814               return FALSE;
10815             }
10816
10817           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10818           p += 4;
10819           if (PPC_HA (off) != 0)
10820             {
10821               bfd_put_32 (htab->params->stub_bfd,
10822                           ADDIS_R12_R2 | PPC_HA (off), p);
10823               p += 4;
10824               bfd_put_32 (htab->params->stub_bfd,
10825                           LD_R12_0R12 | PPC_LO (off), p);
10826             }
10827           else
10828             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10829
10830           if (PPC_HA (r2off) != 0)
10831             {
10832               p += 4;
10833               bfd_put_32 (htab->params->stub_bfd,
10834                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10835             }
10836           if (PPC_LO (r2off) != 0)
10837             {
10838               p += 4;
10839               bfd_put_32 (htab->params->stub_bfd,
10840                           ADDI_R2_R2 | PPC_LO (r2off), p);
10841             }
10842         }
10843       p += 4;
10844       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10845       p += 4;
10846       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10847       p += 4;
10848       break;
10849
10850     case ppc_stub_long_branch_notoc:
10851     case ppc_stub_long_branch_both:
10852     case ppc_stub_plt_branch_notoc:
10853     case ppc_stub_plt_branch_both:
10854     case ppc_stub_plt_call_notoc:
10855     case ppc_stub_plt_call_both:
10856       p = loc;
10857       off = (stub_entry->stub_offset
10858              + stub_entry->group->stub_sec->output_offset
10859              + stub_entry->group->stub_sec->output_section->vma);
10860       if (stub_entry->stub_type == ppc_stub_long_branch_both
10861           || stub_entry->stub_type == ppc_stub_plt_branch_both
10862           || stub_entry->stub_type == ppc_stub_plt_call_both)
10863         {
10864           off += 4;
10865           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10866           p += 4;
10867         }
10868       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10869         {
10870           targ = stub_entry->plt_ent->plt.offset & ~1;
10871           if (targ >= (bfd_vma) -2)
10872             abort ();
10873
10874           plt = htab->elf.splt;
10875           if (!htab->elf.dynamic_sections_created
10876               || stub_entry->h == NULL
10877               || stub_entry->h->elf.dynindx == -1)
10878             {
10879               if (stub_entry->symtype == STT_GNU_IFUNC)
10880                 plt = htab->elf.iplt;
10881               else
10882                 plt = htab->pltlocal;
10883             }
10884           targ += plt->output_offset + plt->output_section->vma;
10885         }
10886       else
10887         targ = (stub_entry->target_value
10888                 + stub_entry->target_section->output_offset
10889                 + stub_entry->target_section->output_section->vma);
10890       off = targ - off;
10891
10892       relp = p;
10893       num_rel = 0;
10894       /* The notoc stubs calculate their target (either a PLT entry or
10895          the global entry point of a function) relative to the PC
10896          returned by the "bcl" two instructions past the start of the
10897          sequence emitted by build_offset.  The offset is therefore 8
10898          less than calculated from the start of the sequence.  */
10899       off -= 8;
10900       p = build_offset (htab->params->stub_bfd, p, off,
10901                         stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10902       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
10903         {
10904           bfd_vma from;
10905           num_rel = 1;
10906           from = (stub_entry->stub_offset
10907                   + stub_entry->group->stub_sec->output_offset
10908                   + stub_entry->group->stub_sec->output_section->vma
10909                   + (p - loc));
10910           bfd_put_32 (htab->params->stub_bfd,
10911                       B_DOT | ((targ - from) & 0x3fffffc), p);
10912         }
10913       else
10914         {
10915           bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10916           p += 4;
10917           bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10918         }
10919       p += 4;
10920
10921       if (info->emitrelocations)
10922         {
10923           bfd_vma roff;
10924           num_rel += num_relocs_for_offset (off);
10925           r = get_relocs (stub_entry->group->stub_sec, num_rel);
10926           if (r == NULL)
10927             return FALSE;
10928           roff = relp + 16 - stub_entry->group->stub_sec->contents;
10929           r = emit_relocs_for_offset (info, r, roff, targ, off);
10930           if (stub_entry->stub_type == ppc_stub_long_branch_notoc
10931               || stub_entry->stub_type == ppc_stub_long_branch_both)
10932             {
10933               ++r;
10934               roff = p - 4 - stub_entry->group->stub_sec->contents;
10935               r->r_offset = roff;
10936               r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10937               r->r_addend = targ;
10938               if (stub_entry->h != NULL
10939                   && !use_global_in_relocs (htab, stub_entry, r, num_rel))
10940                 return FALSE;
10941             }
10942         }
10943
10944       if (htab->glink_eh_frame != NULL
10945         && htab->glink_eh_frame->size != 0)
10946         {
10947           bfd_byte *base, *eh;
10948           unsigned int lr_used, delta;
10949
10950           base = (htab->glink_eh_frame->contents
10951                   + stub_entry->group->eh_base + 17);
10952           eh = base + stub_entry->group->eh_size;
10953           lr_used = stub_entry->stub_offset + 8;
10954           if (stub_entry->stub_type == ppc_stub_long_branch_both
10955               || stub_entry->stub_type == ppc_stub_plt_branch_both
10956               || stub_entry->stub_type == ppc_stub_plt_call_both)
10957             lr_used += 4;
10958           delta = lr_used - stub_entry->group->lr_restore;
10959           stub_entry->group->lr_restore = lr_used + 8;
10960           eh = eh_advance (htab->elf.dynobj, eh, delta);
10961           *eh++ = DW_CFA_register;
10962           *eh++ = 65;
10963           *eh++ = 12;
10964           *eh++ = DW_CFA_advance_loc + 2;
10965           *eh++ = DW_CFA_restore_extended;
10966           *eh++ = 65;
10967           stub_entry->group->eh_size = eh - base;
10968         }
10969       break;
10970
10971     case ppc_stub_plt_call:
10972     case ppc_stub_plt_call_r2save:
10973       if (stub_entry->h != NULL
10974           && stub_entry->h->is_func_descriptor
10975           && stub_entry->h->oh != NULL)
10976         {
10977           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10978
10979           /* If the old-ABI "dot-symbol" is undefined make it weak so
10980              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
10981           if (fh->elf.root.type == bfd_link_hash_undefined
10982               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10983                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10984             fh->elf.root.type = bfd_link_hash_undefweak;
10985         }
10986
10987       /* Now build the stub.  */
10988       targ = stub_entry->plt_ent->plt.offset & ~1;
10989       if (targ >= (bfd_vma) -2)
10990         abort ();
10991
10992       plt = htab->elf.splt;
10993       if (!htab->elf.dynamic_sections_created
10994           || stub_entry->h == NULL
10995           || stub_entry->h->elf.dynindx == -1)
10996         {
10997           if (stub_entry->symtype == STT_GNU_IFUNC)
10998             plt = htab->elf.iplt;
10999           else
11000             plt = htab->pltlocal;
11001         }
11002       targ += plt->output_offset + plt->output_section->vma;
11003
11004       off = (elf_gp (info->output_bfd)
11005              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11006       off = targ - off;
11007
11008       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11009         {
11010           info->callbacks->einfo
11011             /* xgettext:c-format */
11012             (_("%P: linkage table error against `%pT'\n"),
11013              stub_entry->h != NULL
11014              ? stub_entry->h->elf.root.root.string
11015              : "<local sym>");
11016           bfd_set_error (bfd_error_bad_value);
11017           htab->stub_error = TRUE;
11018           return FALSE;
11019         }
11020
11021       r = NULL;
11022       if (info->emitrelocations)
11023         {
11024           r = get_relocs (stub_entry->group->stub_sec,
11025                           ((PPC_HA (off) != 0)
11026                            + (htab->opd_abi
11027                               ? 2 + (htab->params->plt_static_chain
11028                                      && PPC_HA (off + 16) == PPC_HA (off))
11029                               : 1)));
11030           if (r == NULL)
11031             return FALSE;
11032           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11033           if (bfd_big_endian (info->output_bfd))
11034             r[0].r_offset += 2;
11035           r[0].r_addend = targ;
11036         }
11037       if (stub_entry->h != NULL
11038           && (stub_entry->h == htab->tls_get_addr_fd
11039               || stub_entry->h == htab->tls_get_addr)
11040           && htab->params->tls_get_addr_opt)
11041         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11042       else
11043         p = build_plt_stub (htab, stub_entry, loc, off, r);
11044       break;
11045
11046     case ppc_stub_save_res:
11047       return TRUE;
11048
11049     default:
11050       BFD_FAIL ();
11051       return FALSE;
11052     }
11053
11054   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11055
11056   if (htab->params->emit_stub_syms)
11057     {
11058       struct elf_link_hash_entry *h;
11059       size_t len1, len2;
11060       char *name;
11061       const char *const stub_str[] = { "long_branch",
11062                                        "long_branch",
11063                                        "long_branch",
11064                                        "long_branch",
11065                                        "plt_branch",
11066                                        "plt_branch",
11067                                        "plt_branch",
11068                                        "plt_branch",
11069                                        "plt_call",
11070                                        "plt_call",
11071                                        "plt_call",
11072                                        "plt_call" };
11073
11074       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11075       len2 = strlen (stub_entry->root.string);
11076       name = bfd_malloc (len1 + len2 + 2);
11077       if (name == NULL)
11078         return FALSE;
11079       memcpy (name, stub_entry->root.string, 9);
11080       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11081       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11082       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11083       if (h == NULL)
11084         return FALSE;
11085       if (h->root.type == bfd_link_hash_new)
11086         {
11087           h->root.type = bfd_link_hash_defined;
11088           h->root.u.def.section = stub_entry->group->stub_sec;
11089           h->root.u.def.value = stub_entry->stub_offset;
11090           h->ref_regular = 1;
11091           h->def_regular = 1;
11092           h->ref_regular_nonweak = 1;
11093           h->forced_local = 1;
11094           h->non_elf = 0;
11095           h->root.linker_def = 1;
11096         }
11097     }
11098
11099   return TRUE;
11100 }
11101
11102 /* As above, but don't actually build the stub.  Just bump offset so
11103    we know stub section sizes, and select plt_branch stubs where
11104    long_branch stubs won't do.  */
11105
11106 static bfd_boolean
11107 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11108 {
11109   struct ppc_stub_hash_entry *stub_entry;
11110   struct bfd_link_info *info;
11111   struct ppc_link_hash_table *htab;
11112   asection *plt;
11113   bfd_vma targ, off, r2off;
11114   unsigned int size, extra, lr_used, delta;
11115
11116   /* Massage our args to the form they really have.  */
11117   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11118   info = in_arg;
11119
11120   htab = ppc_hash_table (info);
11121   if (htab == NULL)
11122     return FALSE;
11123
11124   /* Make a note of the offset within the stubs for this entry.  */
11125   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11126
11127   if (stub_entry->h != NULL
11128       && stub_entry->h->save_res
11129       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11130       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11131     {
11132       /* Don't make stubs to out-of-line register save/restore
11133          functions.  Instead, emit copies of the functions.  */
11134       stub_entry->group->needs_save_res = 1;
11135       stub_entry->stub_type = ppc_stub_save_res;
11136       return TRUE;
11137     }
11138
11139   switch (stub_entry->stub_type)
11140     {
11141     case ppc_stub_plt_branch:
11142     case ppc_stub_plt_branch_r2off:
11143       /* Reset the stub type from the plt branch variant in case we now
11144          can reach with a shorter stub.  */
11145       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11146       /* Fall through.  */
11147     case ppc_stub_long_branch:
11148     case ppc_stub_long_branch_r2off:
11149       targ = (stub_entry->target_value
11150               + stub_entry->target_section->output_offset
11151               + stub_entry->target_section->output_section->vma);
11152       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11153       off = (stub_entry->stub_offset
11154              + stub_entry->group->stub_sec->output_offset
11155              + stub_entry->group->stub_sec->output_section->vma);
11156
11157       size = 4;
11158       r2off = 0;
11159       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11160         {
11161           r2off = get_r2off (info, stub_entry);
11162           if (r2off == (bfd_vma) -1)
11163             {
11164               htab->stub_error = TRUE;
11165               return FALSE;
11166             }
11167           size = 8;
11168           if (PPC_HA (r2off) != 0)
11169             size += 4;
11170           if (PPC_LO (r2off) != 0)
11171             size += 4;
11172           off += size - 4;
11173         }
11174       off = targ - off;
11175
11176       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11177          Do the same for -R objects without function descriptors.  */
11178       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11179            && r2off == 0
11180            && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11181           || off + (1 << 25) >= (bfd_vma) (1 << 26))
11182         {
11183           struct ppc_branch_hash_entry *br_entry;
11184
11185           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11186                                              stub_entry->root.string + 9,
11187                                              TRUE, FALSE);
11188           if (br_entry == NULL)
11189             {
11190               _bfd_error_handler (_("can't build branch stub `%s'"),
11191                                   stub_entry->root.string);
11192               htab->stub_error = TRUE;
11193               return FALSE;
11194             }
11195
11196           if (br_entry->iter != htab->stub_iteration)
11197             {
11198               br_entry->iter = htab->stub_iteration;
11199               br_entry->offset = htab->brlt->size;
11200               htab->brlt->size += 8;
11201
11202               if (htab->relbrlt != NULL)
11203                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11204               else if (info->emitrelocations)
11205                 {
11206                   htab->brlt->reloc_count += 1;
11207                   htab->brlt->flags |= SEC_RELOC;
11208                 }
11209             }
11210
11211           targ = (br_entry->offset
11212                   + htab->brlt->output_offset
11213                   + htab->brlt->output_section->vma);
11214           off = (elf_gp (info->output_bfd)
11215                  + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11216           off = targ - off;
11217
11218           if (info->emitrelocations)
11219             {
11220               stub_entry->group->stub_sec->reloc_count
11221                 += 1 + (PPC_HA (off) != 0);
11222               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11223             }
11224
11225           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11226           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11227             {
11228               size = 12;
11229               if (PPC_HA (off) != 0)
11230                 size = 16;
11231             }
11232           else
11233             {
11234               size = 16;
11235               if (PPC_HA (off) != 0)
11236                 size += 4;
11237
11238               if (PPC_HA (r2off) != 0)
11239                 size += 4;
11240               if (PPC_LO (r2off) != 0)
11241                 size += 4;
11242             }
11243         }
11244       else if (info->emitrelocations)
11245         {
11246           stub_entry->group->stub_sec->reloc_count += 1;
11247           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11248         }
11249       break;
11250
11251     case ppc_stub_plt_branch_notoc:
11252     case ppc_stub_plt_branch_both:
11253       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11254       /* Fall through.  */
11255     case ppc_stub_long_branch_notoc:
11256     case ppc_stub_long_branch_both:
11257       off = (stub_entry->stub_offset
11258              + stub_entry->group->stub_sec->output_offset
11259              + stub_entry->group->stub_sec->output_section->vma);
11260       size = 0;
11261       if (stub_entry->stub_type == ppc_stub_long_branch_both)
11262         size = 4;
11263       off += size;
11264       targ = (stub_entry->target_value
11265               + stub_entry->target_section->output_offset
11266               + stub_entry->target_section->output_section->vma);
11267       off = targ - off;
11268
11269       if (info->emitrelocations)
11270         {
11271           stub_entry->group->stub_sec->reloc_count
11272             += num_relocs_for_offset (off);
11273           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11274         }
11275
11276       extra = size_offset (off - 8);
11277       /* Include branch insn plus those in the offset sequence.  */
11278       size += 4 + extra;
11279       /* The branch insn is at the end, or "extra" bytes along.  So
11280          its offset will be "extra" bytes less that that already
11281          calculated.  */
11282       off -= extra;
11283
11284       /* After the bcl, lr has been modified so we need to emit
11285          .eh_frame info saying the return address is in r12.  */
11286       lr_used = stub_entry->stub_offset + 8;
11287       if (stub_entry->stub_type == ppc_stub_long_branch_both)
11288         lr_used += 4;
11289       /* The eh_frame info will consist of a DW_CFA_advance_loc or
11290          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11291          DW_CFA_restore_extended 65.  */
11292       delta = lr_used - stub_entry->group->lr_restore;
11293       stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11294       stub_entry->group->lr_restore = lr_used + 8;
11295
11296       /* If the branch can't reach, use a plt_branch.  */
11297       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11298         {
11299           stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11300                                     - ppc_stub_long_branch_notoc);
11301           size += 4;
11302         }
11303       else if (info->emitrelocations)
11304         stub_entry->group->stub_sec->reloc_count +=1;
11305       break;
11306
11307     case ppc_stub_plt_call_notoc:
11308     case ppc_stub_plt_call_both:
11309       off = (stub_entry->stub_offset
11310              + stub_entry->group->stub_sec->output_offset
11311              + stub_entry->group->stub_sec->output_section->vma);
11312       if (stub_entry->stub_type == ppc_stub_plt_call_both)
11313         off += 4;
11314       targ = stub_entry->plt_ent->plt.offset & ~1;
11315       if (targ >= (bfd_vma) -2)
11316         abort ();
11317
11318       plt = htab->elf.splt;
11319       if (!htab->elf.dynamic_sections_created
11320           || stub_entry->h == NULL
11321           || stub_entry->h->elf.dynindx == -1)
11322         {
11323           if (stub_entry->symtype == STT_GNU_IFUNC)
11324             plt = htab->elf.iplt;
11325           else
11326             plt = htab->pltlocal;
11327         }
11328       targ += plt->output_offset + plt->output_section->vma;
11329       off = targ - off;
11330
11331       if (htab->params->plt_stub_align != 0)
11332         {
11333           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11334
11335           stub_entry->group->stub_sec->size += pad;
11336           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11337           off -= pad;
11338         }
11339
11340       if (info->emitrelocations)
11341         {
11342           stub_entry->group->stub_sec->reloc_count
11343             += num_relocs_for_offset (off - 8);
11344           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11345         }
11346
11347       size = plt_stub_size (htab, stub_entry, off);
11348
11349       /* After the bcl, lr has been modified so we need to emit
11350          .eh_frame info saying the return address is in r12.  */
11351       lr_used = stub_entry->stub_offset + 8;
11352       if (stub_entry->stub_type == ppc_stub_plt_call_both)
11353         lr_used += 4;
11354       /* The eh_frame info will consist of a DW_CFA_advance_loc or
11355          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11356          DW_CFA_restore_extended 65.  */
11357       delta = lr_used - stub_entry->group->lr_restore;
11358       stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11359       stub_entry->group->lr_restore = lr_used + 8;
11360       break;
11361
11362     case ppc_stub_plt_call:
11363     case ppc_stub_plt_call_r2save:
11364       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11365       if (targ >= (bfd_vma) -2)
11366         abort ();
11367       plt = htab->elf.splt;
11368       if (!htab->elf.dynamic_sections_created
11369           || stub_entry->h == NULL
11370           || stub_entry->h->elf.dynindx == -1)
11371         {
11372           if (stub_entry->symtype == STT_GNU_IFUNC)
11373             plt = htab->elf.iplt;
11374           else
11375             plt = htab->pltlocal;
11376         }
11377       targ += plt->output_offset + plt->output_section->vma;
11378
11379       off = (elf_gp (info->output_bfd)
11380              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11381       off = targ - off;
11382
11383       if (htab->params->plt_stub_align != 0)
11384         {
11385           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11386
11387           stub_entry->group->stub_sec->size += pad;
11388           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11389         }
11390
11391       if (info->emitrelocations)
11392         {
11393           stub_entry->group->stub_sec->reloc_count
11394             += ((PPC_HA (off) != 0)
11395                 + (htab->opd_abi
11396                    ? 2 + (htab->params->plt_static_chain
11397                           && PPC_HA (off + 16) == PPC_HA (off))
11398                    : 1));
11399           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11400         }
11401
11402       size = plt_stub_size (htab, stub_entry, off);
11403
11404       if (stub_entry->h != NULL
11405           && (stub_entry->h == htab->tls_get_addr_fd
11406               || stub_entry->h == htab->tls_get_addr)
11407           && htab->params->tls_get_addr_opt
11408           && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11409         {
11410           /* After the bctrl, lr has been modified so we need to
11411              emit .eh_frame info saying the return address is
11412              on the stack.  In fact we put the EH info specifying
11413              that the return address is on the stack *at* the
11414              call rather than after it, because the EH info for a
11415              call needs to be specified by that point.
11416              See libgcc/unwind-dw2.c execute_cfa_program.  */
11417           lr_used = stub_entry->stub_offset + size - 20;
11418           /* The eh_frame info will consist of a DW_CFA_advance_loc
11419              or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11420              DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
11421           delta = lr_used - stub_entry->group->lr_restore;
11422           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11423           stub_entry->group->lr_restore = size - 4;
11424         }
11425       break;
11426
11427     default:
11428       BFD_FAIL ();
11429       return FALSE;
11430     }
11431
11432   stub_entry->group->stub_sec->size += size;
11433   return TRUE;
11434 }
11435
11436 /* Set up various things so that we can make a list of input sections
11437    for each output section included in the link.  Returns -1 on error,
11438    0 when no stubs will be needed, and 1 on success.  */
11439
11440 int
11441 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11442 {
11443   unsigned int id;
11444   bfd_size_type amt;
11445   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11446
11447   if (htab == NULL)
11448     return -1;
11449
11450   htab->sec_info_arr_size = _bfd_section_id;
11451   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11452   htab->sec_info = bfd_zmalloc (amt);
11453   if (htab->sec_info == NULL)
11454     return -1;
11455
11456   /* Set toc_off for com, und, abs and ind sections.  */
11457   for (id = 0; id < 3; id++)
11458     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11459
11460   return 1;
11461 }
11462
11463 /* Set up for first pass at multitoc partitioning.  */
11464
11465 void
11466 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11467 {
11468   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11469
11470   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11471   htab->toc_bfd = NULL;
11472   htab->toc_first_sec = NULL;
11473 }
11474
11475 /* The linker repeatedly calls this function for each TOC input section
11476    and linker generated GOT section.  Group input bfds such that the toc
11477    within a group is less than 64k in size.  */
11478
11479 bfd_boolean
11480 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11481 {
11482   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11483   bfd_vma addr, off, limit;
11484
11485   if (htab == NULL)
11486     return FALSE;
11487
11488   if (!htab->second_toc_pass)
11489     {
11490       /* Keep track of the first .toc or .got section for this input bfd.  */
11491       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11492
11493       if (new_bfd)
11494         {
11495           htab->toc_bfd = isec->owner;
11496           htab->toc_first_sec = isec;
11497         }
11498
11499       addr = isec->output_offset + isec->output_section->vma;
11500       off = addr - htab->toc_curr;
11501       limit = 0x80008000;
11502       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11503         limit = 0x10000;
11504       if (off + isec->size > limit)
11505         {
11506           addr = (htab->toc_first_sec->output_offset
11507                   + htab->toc_first_sec->output_section->vma);
11508           htab->toc_curr = addr;
11509           htab->toc_curr &= -TOC_BASE_ALIGN;
11510         }
11511
11512       /* toc_curr is the base address of this toc group.  Set elf_gp
11513          for the input section to be the offset relative to the
11514          output toc base plus 0x8000.  Making the input elf_gp an
11515          offset allows us to move the toc as a whole without
11516          recalculating input elf_gp.  */
11517       off = htab->toc_curr - elf_gp (info->output_bfd);
11518       off += TOC_BASE_OFF;
11519
11520       /* Die if someone uses a linker script that doesn't keep input
11521          file .toc and .got together.  */
11522       if (new_bfd
11523           && elf_gp (isec->owner) != 0
11524           && elf_gp (isec->owner) != off)
11525         return FALSE;
11526
11527       elf_gp (isec->owner) = off;
11528       return TRUE;
11529     }
11530
11531   /* During the second pass toc_first_sec points to the start of
11532      a toc group, and toc_curr is used to track the old elf_gp.
11533      We use toc_bfd to ensure we only look at each bfd once.  */
11534   if (htab->toc_bfd == isec->owner)
11535     return TRUE;
11536   htab->toc_bfd = isec->owner;
11537
11538   if (htab->toc_first_sec == NULL
11539       || htab->toc_curr != elf_gp (isec->owner))
11540     {
11541       htab->toc_curr = elf_gp (isec->owner);
11542       htab->toc_first_sec = isec;
11543     }
11544   addr = (htab->toc_first_sec->output_offset
11545           + htab->toc_first_sec->output_section->vma);
11546   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11547   elf_gp (isec->owner) = off;
11548
11549   return TRUE;
11550 }
11551
11552 /* Called via elf_link_hash_traverse to merge GOT entries for global
11553    symbol H.  */
11554
11555 static bfd_boolean
11556 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11557 {
11558   if (h->root.type == bfd_link_hash_indirect)
11559     return TRUE;
11560
11561   merge_got_entries (&h->got.glist);
11562
11563   return TRUE;
11564 }
11565
11566 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11567    symbol H.  */
11568
11569 static bfd_boolean
11570 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11571 {
11572   struct got_entry *gent;
11573
11574   if (h->root.type == bfd_link_hash_indirect)
11575     return TRUE;
11576
11577   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11578     if (!gent->is_indirect)
11579       allocate_got (h, (struct bfd_link_info *) inf, gent);
11580   return TRUE;
11581 }
11582
11583 /* Called on the first multitoc pass after the last call to
11584    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11585    entries.  */
11586
11587 bfd_boolean
11588 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11589 {
11590   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11591   struct bfd *ibfd, *ibfd2;
11592   bfd_boolean done_something;
11593
11594   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11595
11596   if (!htab->do_multi_toc)
11597     return FALSE;
11598
11599   /* Merge global sym got entries within a toc group.  */
11600   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11601
11602   /* And tlsld_got.  */
11603   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11604     {
11605       struct got_entry *ent, *ent2;
11606
11607       if (!is_ppc64_elf (ibfd))
11608         continue;
11609
11610       ent = ppc64_tlsld_got (ibfd);
11611       if (!ent->is_indirect
11612           && ent->got.offset != (bfd_vma) -1)
11613         {
11614           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11615             {
11616               if (!is_ppc64_elf (ibfd2))
11617                 continue;
11618
11619               ent2 = ppc64_tlsld_got (ibfd2);
11620               if (!ent2->is_indirect
11621                   && ent2->got.offset != (bfd_vma) -1
11622                   && elf_gp (ibfd2) == elf_gp (ibfd))
11623                 {
11624                   ent2->is_indirect = TRUE;
11625                   ent2->got.ent = ent;
11626                 }
11627             }
11628         }
11629     }
11630
11631   /* Zap sizes of got sections.  */
11632   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11633   htab->elf.irelplt->size -= htab->got_reli_size;
11634   htab->got_reli_size = 0;
11635
11636   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11637     {
11638       asection *got, *relgot;
11639
11640       if (!is_ppc64_elf (ibfd))
11641         continue;
11642
11643       got = ppc64_elf_tdata (ibfd)->got;
11644       if (got != NULL)
11645         {
11646           got->rawsize = got->size;
11647           got->size = 0;
11648           relgot = ppc64_elf_tdata (ibfd)->relgot;
11649           relgot->rawsize = relgot->size;
11650           relgot->size = 0;
11651         }
11652     }
11653
11654   /* Now reallocate the got, local syms first.  We don't need to
11655      allocate section contents again since we never increase size.  */
11656   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11657     {
11658       struct got_entry **lgot_ents;
11659       struct got_entry **end_lgot_ents;
11660       struct plt_entry **local_plt;
11661       struct plt_entry **end_local_plt;
11662       unsigned char *lgot_masks;
11663       bfd_size_type locsymcount;
11664       Elf_Internal_Shdr *symtab_hdr;
11665       asection *s;
11666
11667       if (!is_ppc64_elf (ibfd))
11668         continue;
11669
11670       lgot_ents = elf_local_got_ents (ibfd);
11671       if (!lgot_ents)
11672         continue;
11673
11674       symtab_hdr = &elf_symtab_hdr (ibfd);
11675       locsymcount = symtab_hdr->sh_info;
11676       end_lgot_ents = lgot_ents + locsymcount;
11677       local_plt = (struct plt_entry **) end_lgot_ents;
11678       end_local_plt = local_plt + locsymcount;
11679       lgot_masks = (unsigned char *) end_local_plt;
11680       s = ppc64_elf_tdata (ibfd)->got;
11681       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11682         {
11683           struct got_entry *ent;
11684
11685           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11686             {
11687               unsigned int ent_size = 8;
11688               unsigned int rel_size = sizeof (Elf64_External_Rela);
11689
11690               ent->got.offset = s->size;
11691               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11692                 {
11693                   ent_size *= 2;
11694                   rel_size *= 2;
11695                 }
11696               s->size += ent_size;
11697               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11698                 {
11699                   htab->elf.irelplt->size += rel_size;
11700                   htab->got_reli_size += rel_size;
11701                 }
11702               else if (bfd_link_pic (info)
11703                        && !((ent->tls_type & TLS_TPREL) != 0
11704                             && bfd_link_executable (info)))
11705                 {
11706                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11707                   srel->size += rel_size;
11708                 }
11709             }
11710         }
11711     }
11712
11713   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11714
11715   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11716     {
11717       struct got_entry *ent;
11718
11719       if (!is_ppc64_elf (ibfd))
11720         continue;
11721
11722       ent = ppc64_tlsld_got (ibfd);
11723       if (!ent->is_indirect
11724           && ent->got.offset != (bfd_vma) -1)
11725         {
11726           asection *s = ppc64_elf_tdata (ibfd)->got;
11727           ent->got.offset = s->size;
11728           s->size += 16;
11729           if (bfd_link_pic (info))
11730             {
11731               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11732               srel->size += sizeof (Elf64_External_Rela);
11733             }
11734         }
11735     }
11736
11737   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11738   if (!done_something)
11739     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11740       {
11741         asection *got;
11742
11743         if (!is_ppc64_elf (ibfd))
11744           continue;
11745
11746         got = ppc64_elf_tdata (ibfd)->got;
11747         if (got != NULL)
11748           {
11749             done_something = got->rawsize != got->size;
11750             if (done_something)
11751               break;
11752           }
11753       }
11754
11755   if (done_something)
11756     (*htab->params->layout_sections_again) ();
11757
11758   /* Set up for second pass over toc sections to recalculate elf_gp
11759      on input sections.  */
11760   htab->toc_bfd = NULL;
11761   htab->toc_first_sec = NULL;
11762   htab->second_toc_pass = TRUE;
11763   return done_something;
11764 }
11765
11766 /* Called after second pass of multitoc partitioning.  */
11767
11768 void
11769 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11770 {
11771   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11772
11773   /* After the second pass, toc_curr tracks the TOC offset used
11774      for code sections below in ppc64_elf_next_input_section.  */
11775   htab->toc_curr = TOC_BASE_OFF;
11776 }
11777
11778 /* No toc references were found in ISEC.  If the code in ISEC makes no
11779    calls, then there's no need to use toc adjusting stubs when branching
11780    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11781    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11782    needed, and 2 if a cyclical call-graph was found but no other reason
11783    for a stub was detected.  If called from the top level, a return of
11784    2 means the same as a return of 0.  */
11785
11786 static int
11787 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11788 {
11789   int ret;
11790
11791   /* Mark this section as checked.  */
11792   isec->call_check_done = 1;
11793
11794   /* We know none of our code bearing sections will need toc stubs.  */
11795   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11796     return 0;
11797
11798   if (isec->size == 0)
11799     return 0;
11800
11801   if (isec->output_section == NULL)
11802     return 0;
11803
11804   ret = 0;
11805   if (isec->reloc_count != 0)
11806     {
11807       Elf_Internal_Rela *relstart, *rel;
11808       Elf_Internal_Sym *local_syms;
11809       struct ppc_link_hash_table *htab;
11810
11811       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11812                                             info->keep_memory);
11813       if (relstart == NULL)
11814         return -1;
11815
11816       /* Look for branches to outside of this section.  */
11817       local_syms = NULL;
11818       htab = ppc_hash_table (info);
11819       if (htab == NULL)
11820         return -1;
11821
11822       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11823         {
11824           enum elf_ppc64_reloc_type r_type;
11825           unsigned long r_symndx;
11826           struct elf_link_hash_entry *h;
11827           struct ppc_link_hash_entry *eh;
11828           Elf_Internal_Sym *sym;
11829           asection *sym_sec;
11830           struct _opd_sec_data *opd;
11831           bfd_vma sym_value;
11832           bfd_vma dest;
11833
11834           r_type = ELF64_R_TYPE (rel->r_info);
11835           if (r_type != R_PPC64_REL24
11836               && r_type != R_PPC64_REL24_NOTOC
11837               && r_type != R_PPC64_REL14
11838               && r_type != R_PPC64_REL14_BRTAKEN
11839               && r_type != R_PPC64_REL14_BRNTAKEN
11840               && r_type != R_PPC64_PLTCALL
11841               && r_type != R_PPC64_PLTCALL_NOTOC)
11842             continue;
11843
11844           r_symndx = ELF64_R_SYM (rel->r_info);
11845           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11846                           isec->owner))
11847             {
11848               ret = -1;
11849               break;
11850             }
11851
11852           /* Calls to dynamic lib functions go through a plt call stub
11853              that uses r2.  */
11854           eh = (struct ppc_link_hash_entry *) h;
11855           if (eh != NULL
11856               && (eh->elf.plt.plist != NULL
11857                   || (eh->oh != NULL
11858                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11859             {
11860               ret = 1;
11861               break;
11862             }
11863
11864           if (sym_sec == NULL)
11865             /* Ignore other undefined symbols.  */
11866             continue;
11867
11868           /* Assume branches to other sections not included in the
11869              link need stubs too, to cover -R and absolute syms.  */
11870           if (sym_sec->output_section == NULL)
11871             {
11872               ret = 1;
11873               break;
11874             }
11875
11876           if (h == NULL)
11877             sym_value = sym->st_value;
11878           else
11879             {
11880               if (h->root.type != bfd_link_hash_defined
11881                   && h->root.type != bfd_link_hash_defweak)
11882                 abort ();
11883               sym_value = h->root.u.def.value;
11884             }
11885           sym_value += rel->r_addend;
11886
11887           /* If this branch reloc uses an opd sym, find the code section.  */
11888           opd = get_opd_info (sym_sec);
11889           if (opd != NULL)
11890             {
11891               if (h == NULL && opd->adjust != NULL)
11892                 {
11893                   long adjust;
11894
11895                   adjust = opd->adjust[OPD_NDX (sym_value)];
11896                   if (adjust == -1)
11897                     /* Assume deleted functions won't ever be called.  */
11898                     continue;
11899                   sym_value += adjust;
11900                 }
11901
11902               dest = opd_entry_value (sym_sec, sym_value,
11903                                       &sym_sec, NULL, FALSE);
11904               if (dest == (bfd_vma) -1)
11905                 continue;
11906             }
11907           else
11908             dest = (sym_value
11909                     + sym_sec->output_offset
11910                     + sym_sec->output_section->vma);
11911
11912           /* Ignore branch to self.  */
11913           if (sym_sec == isec)
11914             continue;
11915
11916           /* If the called function uses the toc, we need a stub.  */
11917           if (sym_sec->has_toc_reloc
11918               || sym_sec->makes_toc_func_call)
11919             {
11920               ret = 1;
11921               break;
11922             }
11923
11924           /* Assume any branch that needs a long branch stub might in fact
11925              need a plt_branch stub.  A plt_branch stub uses r2.  */
11926           else if (dest - (isec->output_offset
11927                            + isec->output_section->vma
11928                            + rel->r_offset) + (1 << 25)
11929                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11930                                                              ? h->other
11931                                                              : sym->st_other))
11932             {
11933               ret = 1;
11934               break;
11935             }
11936
11937           /* If calling back to a section in the process of being
11938              tested, we can't say for sure that no toc adjusting stubs
11939              are needed, so don't return zero.  */
11940           else if (sym_sec->call_check_in_progress)
11941             ret = 2;
11942
11943           /* Branches to another section that itself doesn't have any TOC
11944              references are OK.  Recursively call ourselves to check.  */
11945           else if (!sym_sec->call_check_done)
11946             {
11947               int recur;
11948
11949               /* Mark current section as indeterminate, so that other
11950                  sections that call back to current won't be marked as
11951                  known.  */
11952               isec->call_check_in_progress = 1;
11953               recur = toc_adjusting_stub_needed (info, sym_sec);
11954               isec->call_check_in_progress = 0;
11955
11956               if (recur != 0)
11957                 {
11958                   ret = recur;
11959                   if (recur != 2)
11960                     break;
11961                 }
11962             }
11963         }
11964
11965       if (local_syms != NULL
11966           && (elf_symtab_hdr (isec->owner).contents
11967               != (unsigned char *) local_syms))
11968         free (local_syms);
11969       if (elf_section_data (isec)->relocs != relstart)
11970         free (relstart);
11971     }
11972
11973   if ((ret & 1) == 0
11974       && isec->map_head.s != NULL
11975       && (strcmp (isec->output_section->name, ".init") == 0
11976           || strcmp (isec->output_section->name, ".fini") == 0))
11977     {
11978       if (isec->map_head.s->has_toc_reloc
11979           || isec->map_head.s->makes_toc_func_call)
11980         ret = 1;
11981       else if (!isec->map_head.s->call_check_done)
11982         {
11983           int recur;
11984           isec->call_check_in_progress = 1;
11985           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11986           isec->call_check_in_progress = 0;
11987           if (recur != 0)
11988             ret = recur;
11989         }
11990     }
11991
11992   if (ret == 1)
11993     isec->makes_toc_func_call = 1;
11994
11995   return ret;
11996 }
11997
11998 /* The linker repeatedly calls this function for each input section,
11999    in the order that input sections are linked into output sections.
12000    Build lists of input sections to determine groupings between which
12001    we may insert linker stubs.  */
12002
12003 bfd_boolean
12004 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12005 {
12006   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12007
12008   if (htab == NULL)
12009     return FALSE;
12010
12011   if ((isec->output_section->flags & SEC_CODE) != 0
12012       && isec->output_section->id < htab->sec_info_arr_size)
12013     {
12014       /* This happens to make the list in reverse order,
12015          which is what we want.  */
12016       htab->sec_info[isec->id].u.list
12017         = htab->sec_info[isec->output_section->id].u.list;
12018       htab->sec_info[isec->output_section->id].u.list = isec;
12019     }
12020
12021   if (htab->multi_toc_needed)
12022     {
12023       /* Analyse sections that aren't already flagged as needing a
12024          valid toc pointer.  Exclude .fixup for the linux kernel.
12025          .fixup contains branches, but only back to the function that
12026          hit an exception.  */
12027       if (!(isec->has_toc_reloc
12028             || (isec->flags & SEC_CODE) == 0
12029             || strcmp (isec->name, ".fixup") == 0
12030             || isec->call_check_done))
12031         {
12032           if (toc_adjusting_stub_needed (info, isec) < 0)
12033             return FALSE;
12034         }
12035       /* Make all sections use the TOC assigned for this object file.
12036          This will be wrong for pasted sections;  We fix that in
12037          check_pasted_section().  */
12038       if (elf_gp (isec->owner) != 0)
12039         htab->toc_curr = elf_gp (isec->owner);
12040     }
12041
12042   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12043   return TRUE;
12044 }
12045
12046 /* Check that all .init and .fini sections use the same toc, if they
12047    have toc relocs.  */
12048
12049 static bfd_boolean
12050 check_pasted_section (struct bfd_link_info *info, const char *name)
12051 {
12052   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12053
12054   if (o != NULL)
12055     {
12056       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12057       bfd_vma toc_off = 0;
12058       asection *i;
12059
12060       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12061         if (i->has_toc_reloc)
12062           {
12063             if (toc_off == 0)
12064               toc_off = htab->sec_info[i->id].toc_off;
12065             else if (toc_off != htab->sec_info[i->id].toc_off)
12066               return FALSE;
12067           }
12068
12069       if (toc_off == 0)
12070         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12071           if (i->makes_toc_func_call)
12072             {
12073               toc_off = htab->sec_info[i->id].toc_off;
12074               break;
12075             }
12076
12077       /* Make sure the whole pasted function uses the same toc offset.  */
12078       if (toc_off != 0)
12079         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12080           htab->sec_info[i->id].toc_off = toc_off;
12081     }
12082   return TRUE;
12083 }
12084
12085 bfd_boolean
12086 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12087 {
12088   return (check_pasted_section (info, ".init")
12089           & check_pasted_section (info, ".fini"));
12090 }
12091
12092 /* See whether we can group stub sections together.  Grouping stub
12093    sections may result in fewer stubs.  More importantly, we need to
12094    put all .init* and .fini* stubs at the beginning of the .init or
12095    .fini output sections respectively, because glibc splits the
12096    _init and _fini functions into multiple parts.  Putting a stub in
12097    the middle of a function is not a good idea.  */
12098
12099 static bfd_boolean
12100 group_sections (struct bfd_link_info *info,
12101                 bfd_size_type stub_group_size,
12102                 bfd_boolean stubs_always_before_branch)
12103 {
12104   struct ppc_link_hash_table *htab;
12105   asection *osec;
12106   bfd_boolean suppress_size_errors;
12107
12108   htab = ppc_hash_table (info);
12109   if (htab == NULL)
12110     return FALSE;
12111
12112   suppress_size_errors = FALSE;
12113   if (stub_group_size == 1)
12114     {
12115       /* Default values.  */
12116       if (stubs_always_before_branch)
12117         stub_group_size = 0x1e00000;
12118       else
12119         stub_group_size = 0x1c00000;
12120       suppress_size_errors = TRUE;
12121     }
12122
12123   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12124     {
12125       asection *tail;
12126
12127       if (osec->id >= htab->sec_info_arr_size)
12128         continue;
12129
12130       tail = htab->sec_info[osec->id].u.list;
12131       while (tail != NULL)
12132         {
12133           asection *curr;
12134           asection *prev;
12135           bfd_size_type total;
12136           bfd_boolean big_sec;
12137           bfd_vma curr_toc;
12138           struct map_stub *group;
12139           bfd_size_type group_size;
12140
12141           curr = tail;
12142           total = tail->size;
12143           group_size = (ppc64_elf_section_data (tail) != NULL
12144                         && ppc64_elf_section_data (tail)->has_14bit_branch
12145                         ? stub_group_size >> 10 : stub_group_size);
12146
12147           big_sec = total > group_size;
12148           if (big_sec && !suppress_size_errors)
12149             /* xgettext:c-format */
12150             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12151                                 tail->owner, tail);
12152           curr_toc = htab->sec_info[tail->id].toc_off;
12153
12154           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12155                  && ((total += curr->output_offset - prev->output_offset)
12156                      < (ppc64_elf_section_data (prev) != NULL
12157                         && ppc64_elf_section_data (prev)->has_14bit_branch
12158                         ? (group_size = stub_group_size >> 10) : group_size))
12159                  && htab->sec_info[prev->id].toc_off == curr_toc)
12160             curr = prev;
12161
12162           /* OK, the size from the start of CURR to the end is less
12163              than group_size and thus can be handled by one stub
12164              section.  (or the tail section is itself larger than
12165              group_size, in which case we may be toast.)  We should
12166              really be keeping track of the total size of stubs added
12167              here, as stubs contribute to the final output section
12168              size.  That's a little tricky, and this way will only
12169              break if stubs added make the total size more than 2^25,
12170              ie. for the default stub_group_size, if stubs total more
12171              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12172           group = bfd_alloc (curr->owner, sizeof (*group));
12173           if (group == NULL)
12174             return FALSE;
12175           group->link_sec = curr;
12176           group->stub_sec = NULL;
12177           group->needs_save_res = 0;
12178           group->lr_restore = 0;
12179           group->eh_size = 0;
12180           group->eh_base = 0;
12181           group->next = htab->group;
12182           htab->group = group;
12183           do
12184             {
12185               prev = htab->sec_info[tail->id].u.list;
12186               /* Set up this stub group.  */
12187               htab->sec_info[tail->id].u.group = group;
12188             }
12189           while (tail != curr && (tail = prev) != NULL);
12190
12191           /* But wait, there's more!  Input sections up to group_size
12192              bytes before the stub section can be handled by it too.
12193              Don't do this if we have a really large section after the
12194              stubs, as adding more stubs increases the chance that
12195              branches may not reach into the stub section.  */
12196           if (!stubs_always_before_branch && !big_sec)
12197             {
12198               total = 0;
12199               while (prev != NULL
12200                      && ((total += tail->output_offset - prev->output_offset)
12201                          < (ppc64_elf_section_data (prev) != NULL
12202                             && ppc64_elf_section_data (prev)->has_14bit_branch
12203                             ? (group_size = stub_group_size >> 10)
12204                             : group_size))
12205                      && htab->sec_info[prev->id].toc_off == curr_toc)
12206                 {
12207                   tail = prev;
12208                   prev = htab->sec_info[tail->id].u.list;
12209                   htab->sec_info[tail->id].u.group = group;
12210                 }
12211             }
12212           tail = prev;
12213         }
12214     }
12215   return TRUE;
12216 }
12217
12218 static const unsigned char glink_eh_frame_cie[] =
12219 {
12220   0, 0, 0, 16,                          /* length.  */
12221   0, 0, 0, 0,                           /* id.  */
12222   1,                                    /* CIE version.  */
12223   'z', 'R', 0,                          /* Augmentation string.  */
12224   4,                                    /* Code alignment.  */
12225   0x78,                                 /* Data alignment.  */
12226   65,                                   /* RA reg.  */
12227   1,                                    /* Augmentation size.  */
12228   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12229   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12230 };
12231
12232 /* Stripping output sections is normally done before dynamic section
12233    symbols have been allocated.  This function is called later, and
12234    handles cases like htab->brlt which is mapped to its own output
12235    section.  */
12236
12237 static void
12238 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12239 {
12240   if (isec->size == 0
12241       && isec->output_section->size == 0
12242       && !(isec->output_section->flags & SEC_KEEP)
12243       && !bfd_section_removed_from_list (info->output_bfd,
12244                                          isec->output_section)
12245       && elf_section_data (isec->output_section)->dynindx == 0)
12246     {
12247       isec->output_section->flags |= SEC_EXCLUDE;
12248       bfd_section_list_remove (info->output_bfd, isec->output_section);
12249       info->output_bfd->section_count--;
12250     }
12251 }
12252
12253 /* Determine and set the size of the stub section for a final link.
12254
12255    The basic idea here is to examine all the relocations looking for
12256    PC-relative calls to a target that is unreachable with a "bl"
12257    instruction.  */
12258
12259 bfd_boolean
12260 ppc64_elf_size_stubs (struct bfd_link_info *info)
12261 {
12262   bfd_size_type stub_group_size;
12263   bfd_boolean stubs_always_before_branch;
12264   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12265
12266   if (htab == NULL)
12267     return FALSE;
12268
12269   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12270     htab->params->plt_thread_safe = 1;
12271   if (!htab->opd_abi)
12272     htab->params->plt_thread_safe = 0;
12273   else if (htab->params->plt_thread_safe == -1)
12274     {
12275       static const char *const thread_starter[] =
12276         {
12277           "pthread_create",
12278           /* libstdc++ */
12279           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12280           /* librt */
12281           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12282           "mq_notify", "create_timer",
12283           /* libanl */
12284           "getaddrinfo_a",
12285           /* libgomp */
12286           "GOMP_parallel",
12287           "GOMP_parallel_start",
12288           "GOMP_parallel_loop_static",
12289           "GOMP_parallel_loop_static_start",
12290           "GOMP_parallel_loop_dynamic",
12291           "GOMP_parallel_loop_dynamic_start",
12292           "GOMP_parallel_loop_guided",
12293           "GOMP_parallel_loop_guided_start",
12294           "GOMP_parallel_loop_runtime",
12295           "GOMP_parallel_loop_runtime_start",
12296           "GOMP_parallel_sections",
12297           "GOMP_parallel_sections_start",
12298           /* libgo */
12299           "__go_go",
12300         };
12301       unsigned i;
12302
12303       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12304         {
12305           struct elf_link_hash_entry *h;
12306           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12307                                     FALSE, FALSE, TRUE);
12308           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12309           if (htab->params->plt_thread_safe)
12310             break;
12311         }
12312     }
12313   stubs_always_before_branch = htab->params->group_size < 0;
12314   if (htab->params->group_size < 0)
12315     stub_group_size = -htab->params->group_size;
12316   else
12317     stub_group_size = htab->params->group_size;
12318
12319   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12320     return FALSE;
12321
12322 #define STUB_SHRINK_ITER 20
12323   /* Loop until no stubs added.  After iteration 20 of this loop we may
12324      exit on a stub section shrinking.  This is to break out of a
12325      pathological case where adding stubs on one iteration decreases
12326      section gaps (perhaps due to alignment), which then requires
12327      fewer or smaller stubs on the next iteration.  */
12328
12329   while (1)
12330     {
12331       bfd *input_bfd;
12332       unsigned int bfd_indx;
12333       struct map_stub *group;
12334
12335       htab->stub_iteration += 1;
12336
12337       for (input_bfd = info->input_bfds, bfd_indx = 0;
12338            input_bfd != NULL;
12339            input_bfd = input_bfd->link.next, bfd_indx++)
12340         {
12341           Elf_Internal_Shdr *symtab_hdr;
12342           asection *section;
12343           Elf_Internal_Sym *local_syms = NULL;
12344
12345           if (!is_ppc64_elf (input_bfd))
12346             continue;
12347
12348           /* We'll need the symbol table in a second.  */
12349           symtab_hdr = &elf_symtab_hdr (input_bfd);
12350           if (symtab_hdr->sh_info == 0)
12351             continue;
12352
12353           /* Walk over each section attached to the input bfd.  */
12354           for (section = input_bfd->sections;
12355                section != NULL;
12356                section = section->next)
12357             {
12358               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12359
12360               /* If there aren't any relocs, then there's nothing more
12361                  to do.  */
12362               if ((section->flags & SEC_RELOC) == 0
12363                   || (section->flags & SEC_ALLOC) == 0
12364                   || (section->flags & SEC_LOAD) == 0
12365                   || (section->flags & SEC_CODE) == 0
12366                   || section->reloc_count == 0)
12367                 continue;
12368
12369               /* If this section is a link-once section that will be
12370                  discarded, then don't create any stubs.  */
12371               if (section->output_section == NULL
12372                   || section->output_section->owner != info->output_bfd)
12373                 continue;
12374
12375               /* Get the relocs.  */
12376               internal_relocs
12377                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12378                                              info->keep_memory);
12379               if (internal_relocs == NULL)
12380                 goto error_ret_free_local;
12381
12382               /* Now examine each relocation.  */
12383               irela = internal_relocs;
12384               irelaend = irela + section->reloc_count;
12385               for (; irela < irelaend; irela++)
12386                 {
12387                   enum elf_ppc64_reloc_type r_type;
12388                   unsigned int r_indx;
12389                   enum ppc_stub_type stub_type;
12390                   struct ppc_stub_hash_entry *stub_entry;
12391                   asection *sym_sec, *code_sec;
12392                   bfd_vma sym_value, code_value;
12393                   bfd_vma destination;
12394                   unsigned long local_off;
12395                   bfd_boolean ok_dest;
12396                   struct ppc_link_hash_entry *hash;
12397                   struct ppc_link_hash_entry *fdh;
12398                   struct elf_link_hash_entry *h;
12399                   Elf_Internal_Sym *sym;
12400                   char *stub_name;
12401                   const asection *id_sec;
12402                   struct _opd_sec_data *opd;
12403                   struct plt_entry *plt_ent;
12404
12405                   r_type = ELF64_R_TYPE (irela->r_info);
12406                   r_indx = ELF64_R_SYM (irela->r_info);
12407
12408                   if (r_type >= R_PPC64_max)
12409                     {
12410                       bfd_set_error (bfd_error_bad_value);
12411                       goto error_ret_free_internal;
12412                     }
12413
12414                   /* Only look for stubs on branch instructions.  */
12415                   if (r_type != R_PPC64_REL24
12416                       && r_type != R_PPC64_REL24_NOTOC
12417                       && r_type != R_PPC64_REL14
12418                       && r_type != R_PPC64_REL14_BRTAKEN
12419                       && r_type != R_PPC64_REL14_BRNTAKEN)
12420                     continue;
12421
12422                   /* Now determine the call target, its name, value,
12423                      section.  */
12424                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12425                                   r_indx, input_bfd))
12426                     goto error_ret_free_internal;
12427                   hash = (struct ppc_link_hash_entry *) h;
12428
12429                   ok_dest = FALSE;
12430                   fdh = NULL;
12431                   sym_value = 0;
12432                   if (hash == NULL)
12433                     {
12434                       sym_value = sym->st_value;
12435                       if (sym_sec != NULL
12436                           && sym_sec->output_section != NULL)
12437                         ok_dest = TRUE;
12438                     }
12439                   else if (hash->elf.root.type == bfd_link_hash_defined
12440                            || hash->elf.root.type == bfd_link_hash_defweak)
12441                     {
12442                       sym_value = hash->elf.root.u.def.value;
12443                       if (sym_sec->output_section != NULL)
12444                         ok_dest = TRUE;
12445                     }
12446                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12447                            || hash->elf.root.type == bfd_link_hash_undefined)
12448                     {
12449                       /* Recognise an old ABI func code entry sym, and
12450                          use the func descriptor sym instead if it is
12451                          defined.  */
12452                       if (hash->elf.root.root.string[0] == '.'
12453                           && hash->oh != NULL)
12454                         {
12455                           fdh = ppc_follow_link (hash->oh);
12456                           if (fdh->elf.root.type == bfd_link_hash_defined
12457                               || fdh->elf.root.type == bfd_link_hash_defweak)
12458                             {
12459                               sym_sec = fdh->elf.root.u.def.section;
12460                               sym_value = fdh->elf.root.u.def.value;
12461                               if (sym_sec->output_section != NULL)
12462                                 ok_dest = TRUE;
12463                             }
12464                           else
12465                             fdh = NULL;
12466                         }
12467                     }
12468                   else
12469                     {
12470                       bfd_set_error (bfd_error_bad_value);
12471                       goto error_ret_free_internal;
12472                     }
12473
12474                   destination = 0;
12475                   local_off = 0;
12476                   if (ok_dest)
12477                     {
12478                       sym_value += irela->r_addend;
12479                       destination = (sym_value
12480                                      + sym_sec->output_offset
12481                                      + sym_sec->output_section->vma);
12482                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12483                                                             ? hash->elf.other
12484                                                             : sym->st_other);
12485                     }
12486
12487                   code_sec = sym_sec;
12488                   code_value = sym_value;
12489                   opd = get_opd_info (sym_sec);
12490                   if (opd != NULL)
12491                     {
12492                       bfd_vma dest;
12493
12494                       if (hash == NULL && opd->adjust != NULL)
12495                         {
12496                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12497                           if (adjust == -1)
12498                             continue;
12499                           code_value += adjust;
12500                           sym_value += adjust;
12501                         }
12502                       dest = opd_entry_value (sym_sec, sym_value,
12503                                               &code_sec, &code_value, FALSE);
12504                       if (dest != (bfd_vma) -1)
12505                         {
12506                           destination = dest;
12507                           if (fdh != NULL)
12508                             {
12509                               /* Fixup old ABI sym to point at code
12510                                  entry.  */
12511                               hash->elf.root.type = bfd_link_hash_defweak;
12512                               hash->elf.root.u.def.section = code_sec;
12513                               hash->elf.root.u.def.value = code_value;
12514                             }
12515                         }
12516                     }
12517
12518                   /* Determine what (if any) linker stub is needed.  */
12519                   plt_ent = NULL;
12520                   stub_type = ppc_type_of_stub (section, irela, &hash,
12521                                                 &plt_ent, destination,
12522                                                 local_off);
12523
12524                   if (r_type == R_PPC64_REL24_NOTOC)
12525                     {
12526                       if (stub_type == ppc_stub_plt_call)
12527                         stub_type = ppc_stub_plt_call_notoc;
12528                       else if (stub_type == ppc_stub_long_branch
12529                                || (code_sec != NULL
12530                                    && code_sec->output_section != NULL
12531                                    && (((hash ? hash->elf.other : sym->st_other)
12532                                         & STO_PPC64_LOCAL_MASK)
12533                                        != 1 << STO_PPC64_LOCAL_BIT)))
12534                         stub_type = ppc_stub_long_branch_notoc;
12535                     }
12536                   else if (stub_type != ppc_stub_plt_call)
12537                     {
12538                       /* Check whether we need a TOC adjusting stub.
12539                          Since the linker pastes together pieces from
12540                          different object files when creating the
12541                          _init and _fini functions, it may be that a
12542                          call to what looks like a local sym is in
12543                          fact a call needing a TOC adjustment.  */
12544                       if ((code_sec != NULL
12545                            && code_sec->output_section != NULL
12546                            && (htab->sec_info[code_sec->id].toc_off
12547                                != htab->sec_info[section->id].toc_off)
12548                            && (code_sec->has_toc_reloc
12549                                || code_sec->makes_toc_func_call))
12550                           || (((hash ? hash->elf.other : sym->st_other)
12551                                & STO_PPC64_LOCAL_MASK)
12552                               == 1 << STO_PPC64_LOCAL_BIT))
12553                         stub_type = ppc_stub_long_branch_r2off;
12554                     }
12555
12556                   if (stub_type == ppc_stub_none)
12557                     continue;
12558
12559                   /* __tls_get_addr calls might be eliminated.  */
12560                   if (stub_type != ppc_stub_plt_call
12561                       && stub_type != ppc_stub_plt_call_notoc
12562                       && hash != NULL
12563                       && (hash == htab->tls_get_addr
12564                           || hash == htab->tls_get_addr_fd)
12565                       && section->has_tls_reloc
12566                       && irela != internal_relocs)
12567                     {
12568                       /* Get tls info.  */
12569                       unsigned char *tls_mask;
12570
12571                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12572                                          irela - 1, input_bfd))
12573                         goto error_ret_free_internal;
12574                       if ((*tls_mask & TLS_TLS) != 0)
12575                         continue;
12576                     }
12577
12578                   if (stub_type == ppc_stub_plt_call)
12579                     {
12580                       if (!htab->opd_abi
12581                           && htab->params->plt_localentry0 != 0
12582                           && is_elfv2_localentry0 (&hash->elf))
12583                         htab->has_plt_localentry0 = 1;
12584                       else if (irela + 1 < irelaend
12585                                && irela[1].r_offset == irela->r_offset + 4
12586                                && (ELF64_R_TYPE (irela[1].r_info)
12587                                    == R_PPC64_TOCSAVE))
12588                         {
12589                           if (!tocsave_find (htab, INSERT,
12590                                              &local_syms, irela + 1, input_bfd))
12591                             goto error_ret_free_internal;
12592                         }
12593                       else
12594                         stub_type = ppc_stub_plt_call_r2save;
12595                     }
12596
12597                   /* Support for grouping stub sections.  */
12598                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12599
12600                   /* Get the name of this stub.  */
12601                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12602                   if (!stub_name)
12603                     goto error_ret_free_internal;
12604
12605                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12606                                                      stub_name, FALSE, FALSE);
12607                   if (stub_entry != NULL)
12608                     {
12609                       enum ppc_stub_type old_type;
12610                       /* A stub has already been created, but it may
12611                          not be the required type.  We shouldn't be
12612                          transitioning from plt_call to long_branch
12613                          stubs or vice versa, but we might be
12614                          upgrading from plt_call to plt_call_r2save or
12615                          from long_branch to long_branch_r2off.  */
12616                       free (stub_name);
12617                       old_type = stub_entry->stub_type;
12618                       switch (old_type)
12619                         {
12620                         default:
12621                           abort ();
12622
12623                         case ppc_stub_save_res:
12624                           continue;
12625
12626                         case ppc_stub_plt_call:
12627                         case ppc_stub_plt_call_r2save:
12628                         case ppc_stub_plt_call_notoc:
12629                         case ppc_stub_plt_call_both:
12630                           if (stub_type == ppc_stub_plt_call)
12631                             continue;
12632                           else if (stub_type == ppc_stub_plt_call_r2save)
12633                             {
12634                               if (old_type == ppc_stub_plt_call_notoc)
12635                                 stub_type = ppc_stub_plt_call_both;
12636                             }
12637                           else if (stub_type == ppc_stub_plt_call_notoc)
12638                             {
12639                               if (old_type == ppc_stub_plt_call_r2save)
12640                                 stub_type = ppc_stub_plt_call_both;
12641                             }
12642                           else
12643                             abort ();
12644                           break;
12645
12646                         case ppc_stub_plt_branch:
12647                         case ppc_stub_plt_branch_r2off:
12648                         case ppc_stub_plt_branch_notoc:
12649                         case ppc_stub_plt_branch_both:
12650                           old_type += (ppc_stub_long_branch
12651                                        - ppc_stub_plt_branch);
12652                           /* Fall through.  */
12653                         case ppc_stub_long_branch:
12654                         case ppc_stub_long_branch_r2off:
12655                         case ppc_stub_long_branch_notoc:
12656                         case ppc_stub_long_branch_both:
12657                           if (stub_type == ppc_stub_long_branch)
12658                             continue;
12659                           else if (stub_type == ppc_stub_long_branch_r2off)
12660                             {
12661                               if (old_type == ppc_stub_long_branch_notoc)
12662                                 stub_type = ppc_stub_long_branch_both;
12663                             }
12664                           else if (stub_type == ppc_stub_long_branch_notoc)
12665                             {
12666                               if (old_type == ppc_stub_long_branch_r2off)
12667                                 stub_type = ppc_stub_long_branch_both;
12668                             }
12669                           else
12670                             abort ();
12671                           break;
12672                         }
12673                       if (old_type < stub_type)
12674                         stub_entry->stub_type = stub_type;
12675                       continue;
12676                     }
12677
12678                   stub_entry = ppc_add_stub (stub_name, section, info);
12679                   if (stub_entry == NULL)
12680                     {
12681                       free (stub_name);
12682                     error_ret_free_internal:
12683                       if (elf_section_data (section)->relocs == NULL)
12684                         free (internal_relocs);
12685                     error_ret_free_local:
12686                       if (local_syms != NULL
12687                           && (symtab_hdr->contents
12688                               != (unsigned char *) local_syms))
12689                         free (local_syms);
12690                       return FALSE;
12691                     }
12692
12693                   stub_entry->stub_type = stub_type;
12694                   if (stub_type >= ppc_stub_plt_call
12695                       && stub_type <= ppc_stub_plt_call_both)
12696                     {
12697                       stub_entry->target_value = sym_value;
12698                       stub_entry->target_section = sym_sec;
12699                     }
12700                   else
12701                     {
12702                       stub_entry->target_value = code_value;
12703                       stub_entry->target_section = code_sec;
12704                     }
12705                   stub_entry->h = hash;
12706                   stub_entry->plt_ent = plt_ent;
12707                   stub_entry->symtype
12708                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12709                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12710
12711                   if (hash != NULL
12712                       && (hash->elf.root.type == bfd_link_hash_defined
12713                           || hash->elf.root.type == bfd_link_hash_defweak))
12714                     htab->stub_globals += 1;
12715                 }
12716
12717               /* We're done with the internal relocs, free them.  */
12718               if (elf_section_data (section)->relocs != internal_relocs)
12719                 free (internal_relocs);
12720             }
12721
12722           if (local_syms != NULL
12723               && symtab_hdr->contents != (unsigned char *) local_syms)
12724             {
12725               if (!info->keep_memory)
12726                 free (local_syms);
12727               else
12728                 symtab_hdr->contents = (unsigned char *) local_syms;
12729             }
12730         }
12731
12732       /* We may have added some stubs.  Find out the new size of the
12733          stub sections.  */
12734       for (group = htab->group; group != NULL; group = group->next)
12735         {
12736           group->lr_restore = 0;
12737           group->eh_size = 0;
12738           if (group->stub_sec != NULL)
12739             {
12740               asection *stub_sec = group->stub_sec;
12741
12742               if (htab->stub_iteration <= STUB_SHRINK_ITER
12743                   || stub_sec->rawsize < stub_sec->size)
12744                 /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12745                 stub_sec->rawsize = stub_sec->size;
12746               stub_sec->size = 0;
12747               stub_sec->reloc_count = 0;
12748               stub_sec->flags &= ~SEC_RELOC;
12749             }
12750         }
12751
12752       if (htab->stub_iteration <= STUB_SHRINK_ITER
12753           || htab->brlt->rawsize < htab->brlt->size)
12754         htab->brlt->rawsize = htab->brlt->size;
12755       htab->brlt->size = 0;
12756       htab->brlt->reloc_count = 0;
12757       htab->brlt->flags &= ~SEC_RELOC;
12758       if (htab->relbrlt != NULL)
12759         htab->relbrlt->size = 0;
12760
12761       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12762
12763       for (group = htab->group; group != NULL; group = group->next)
12764         if (group->needs_save_res)
12765           group->stub_sec->size += htab->sfpr->size;
12766
12767       if (info->emitrelocations
12768           && htab->glink != NULL && htab->glink->size != 0)
12769         {
12770           htab->glink->reloc_count = 1;
12771           htab->glink->flags |= SEC_RELOC;
12772         }
12773
12774       if (htab->glink_eh_frame != NULL
12775           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12776           && htab->glink_eh_frame->output_section->size > 8)
12777         {
12778           size_t size = 0, align = 4;
12779
12780           for (group = htab->group; group != NULL; group = group->next)
12781             if (group->eh_size != 0)
12782               size += (group->eh_size + 17 + align - 1) & -align;
12783           if (htab->glink != NULL && htab->glink->size != 0)
12784             size += (24 + align - 1) & -align;
12785           if (size != 0)
12786             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12787           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12788           size = (size + align - 1) & -align;
12789           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12790           htab->glink_eh_frame->size = size;
12791         }
12792
12793       if (htab->params->plt_stub_align != 0)
12794         for (group = htab->group; group != NULL; group = group->next)
12795           if (group->stub_sec != NULL)
12796             {
12797               int align = abs (htab->params->plt_stub_align);
12798               group->stub_sec->size
12799                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12800             }
12801
12802       for (group = htab->group; group != NULL; group = group->next)
12803         if (group->stub_sec != NULL
12804             && group->stub_sec->rawsize != group->stub_sec->size
12805             && (htab->stub_iteration <= STUB_SHRINK_ITER
12806                 || group->stub_sec->rawsize < group->stub_sec->size))
12807           break;
12808
12809       if (group == NULL
12810           && (htab->brlt->rawsize == htab->brlt->size
12811               || (htab->stub_iteration > STUB_SHRINK_ITER
12812                   && htab->brlt->rawsize > htab->brlt->size))
12813           && (htab->glink_eh_frame == NULL
12814               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12815         break;
12816
12817       /* Ask the linker to do its stuff.  */
12818       (*htab->params->layout_sections_again) ();
12819     }
12820
12821   if (htab->glink_eh_frame != NULL
12822       && htab->glink_eh_frame->size != 0)
12823     {
12824       bfd_vma val;
12825       bfd_byte *p, *last_fde;
12826       size_t last_fde_len, size, align, pad;
12827       struct map_stub *group;
12828
12829       /* It is necessary to at least have a rough outline of the
12830          linker generated CIEs and FDEs written before
12831          bfd_elf_discard_info is run, in order for these FDEs to be
12832          indexed in .eh_frame_hdr.  */
12833       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12834       if (p == NULL)
12835         return FALSE;
12836       htab->glink_eh_frame->contents = p;
12837       last_fde = p;
12838       align = 4;
12839
12840       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12841       /* CIE length (rewrite in case little-endian).  */
12842       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12843       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12844       p += last_fde_len + 4;
12845
12846       for (group = htab->group; group != NULL; group = group->next)
12847         if (group->eh_size != 0)
12848           {
12849             group->eh_base = p - htab->glink_eh_frame->contents;
12850             last_fde = p;
12851             last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12852             /* FDE length.  */
12853             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12854             p += 4;
12855             /* CIE pointer.  */
12856             val = p - htab->glink_eh_frame->contents;
12857             bfd_put_32 (htab->elf.dynobj, val, p);
12858             p += 4;
12859             /* Offset to stub section, written later.  */
12860             p += 4;
12861             /* stub section size.  */
12862             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12863             p += 4;
12864             /* Augmentation.  */
12865             p += 1;
12866             /* Make sure we don't have all nops.  This is enough for
12867                elf-eh-frame.c to detect the last non-nop opcode.  */
12868             p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12869             p = last_fde + last_fde_len + 4;
12870           }
12871       if (htab->glink != NULL && htab->glink->size != 0)
12872         {
12873           last_fde = p;
12874           last_fde_len = ((24 + align - 1) & -align) - 4;
12875           /* FDE length.  */
12876           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12877           p += 4;
12878           /* CIE pointer.  */
12879           val = p - htab->glink_eh_frame->contents;
12880           bfd_put_32 (htab->elf.dynobj, val, p);
12881           p += 4;
12882           /* Offset to .glink, written later.  */
12883           p += 4;
12884           /* .glink size.  */
12885           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12886           p += 4;
12887           /* Augmentation.  */
12888           p += 1;
12889
12890           *p++ = DW_CFA_advance_loc + 1;
12891           *p++ = DW_CFA_register;
12892           *p++ = 65;
12893           *p++ = htab->opd_abi ? 12 : 0;
12894           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12895           *p++ = DW_CFA_restore_extended;
12896           *p++ = 65;
12897           p += ((24 + align - 1) & -align) - 24;
12898         }
12899       /* Subsume any padding into the last FDE if user .eh_frame
12900          sections are aligned more than glink_eh_frame.  Otherwise any
12901          zero padding will be seen as a terminator.  */
12902       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12903       size = p - htab->glink_eh_frame->contents;
12904       pad = ((size + align - 1) & -align) - size;
12905       htab->glink_eh_frame->size = size + pad;
12906       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12907     }
12908
12909   maybe_strip_output (info, htab->brlt);
12910   if (htab->glink_eh_frame != NULL)
12911     maybe_strip_output (info, htab->glink_eh_frame);
12912
12913   return TRUE;
12914 }
12915
12916 /* Called after we have determined section placement.  If sections
12917    move, we'll be called again.  Provide a value for TOCstart.  */
12918
12919 bfd_vma
12920 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12921 {
12922   asection *s;
12923   bfd_vma TOCstart, adjust;
12924
12925   if (info != NULL)
12926     {
12927       struct elf_link_hash_entry *h;
12928       struct elf_link_hash_table *htab = elf_hash_table (info);
12929
12930       if (is_elf_hash_table (htab)
12931           && htab->hgot != NULL)
12932         h = htab->hgot;
12933       else
12934         {
12935           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12936           if (is_elf_hash_table (htab))
12937             htab->hgot = h;
12938         }
12939       if (h != NULL
12940           && h->root.type == bfd_link_hash_defined
12941           && !h->root.linker_def
12942           && (!is_elf_hash_table (htab)
12943               || h->def_regular))
12944         {
12945           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12946                       + h->root.u.def.section->output_offset
12947                       + h->root.u.def.section->output_section->vma);
12948           _bfd_set_gp_value (obfd, TOCstart);
12949           return TOCstart;
12950         }
12951     }
12952
12953   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12954      order.  The TOC starts where the first of these sections starts.  */
12955   s = bfd_get_section_by_name (obfd, ".got");
12956   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12957     s = bfd_get_section_by_name (obfd, ".toc");
12958   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12959     s = bfd_get_section_by_name (obfd, ".tocbss");
12960   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12961     s = bfd_get_section_by_name (obfd, ".plt");
12962   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12963     {
12964       /* This may happen for
12965          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12966          .toc directive
12967          o  bad linker script
12968          o --gc-sections and empty TOC sections
12969
12970          FIXME: Warn user?  */
12971
12972       /* Look for a likely section.  We probably won't even be
12973          using TOCstart.  */
12974       for (s = obfd->sections; s != NULL; s = s->next)
12975         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12976                          | SEC_EXCLUDE))
12977             == (SEC_ALLOC | SEC_SMALL_DATA))
12978           break;
12979       if (s == NULL)
12980         for (s = obfd->sections; s != NULL; s = s->next)
12981           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12982               == (SEC_ALLOC | SEC_SMALL_DATA))
12983             break;
12984       if (s == NULL)
12985         for (s = obfd->sections; s != NULL; s = s->next)
12986           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12987               == SEC_ALLOC)
12988             break;
12989       if (s == NULL)
12990         for (s = obfd->sections; s != NULL; s = s->next)
12991           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12992             break;
12993     }
12994
12995   TOCstart = 0;
12996   if (s != NULL)
12997     TOCstart = s->output_section->vma + s->output_offset;
12998
12999   /* Force alignment.  */
13000   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13001   TOCstart -= adjust;
13002   _bfd_set_gp_value (obfd, TOCstart);
13003
13004   if (info != NULL && s != NULL)
13005     {
13006       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13007
13008       if (htab != NULL)
13009         {
13010           if (htab->elf.hgot != NULL)
13011             {
13012               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13013               htab->elf.hgot->root.u.def.section = s;
13014             }
13015         }
13016       else
13017         {
13018           struct bfd_link_hash_entry *bh = NULL;
13019           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13020                                             s, TOC_BASE_OFF - adjust,
13021                                             NULL, FALSE, FALSE, &bh);
13022         }
13023     }
13024   return TOCstart;
13025 }
13026
13027 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13028    write out any global entry stubs, and PLT relocations.  */
13029
13030 static bfd_boolean
13031 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13032 {
13033   struct bfd_link_info *info;
13034   struct ppc_link_hash_table *htab;
13035   struct plt_entry *ent;
13036   asection *s;
13037
13038   if (h->root.type == bfd_link_hash_indirect)
13039     return TRUE;
13040
13041   info = inf;
13042   htab = ppc_hash_table (info);
13043   if (htab == NULL)
13044     return FALSE;
13045
13046   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13047     if (ent->plt.offset != (bfd_vma) -1)
13048       {
13049         /* This symbol has an entry in the procedure linkage
13050            table.  Set it up.  */
13051         Elf_Internal_Rela rela;
13052         asection *plt, *relplt;
13053         bfd_byte *loc;
13054
13055         if (!htab->elf.dynamic_sections_created
13056             || h->dynindx == -1)
13057           {
13058             if (!(h->def_regular
13059                   && (h->root.type == bfd_link_hash_defined
13060                       || h->root.type == bfd_link_hash_defweak)))
13061               continue;
13062             if (h->type == STT_GNU_IFUNC)
13063               {
13064                 plt = htab->elf.iplt;
13065                 relplt = htab->elf.irelplt;
13066                 htab->local_ifunc_resolver = 1;
13067                 if (htab->opd_abi)
13068                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13069                 else
13070                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13071               }
13072             else
13073               {
13074                 plt = htab->pltlocal;
13075                 if (bfd_link_pic (info))
13076                   {
13077                     relplt = htab->relpltlocal;
13078                     if (htab->opd_abi)
13079                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13080                     else
13081                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13082                   }
13083                 else
13084                   relplt = NULL;
13085               }
13086             rela.r_addend = (h->root.u.def.value
13087                              + h->root.u.def.section->output_offset
13088                              + h->root.u.def.section->output_section->vma
13089                              + ent->addend);
13090
13091             if (relplt == NULL)
13092               {
13093                 loc = plt->contents + ent->plt.offset;
13094                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13095                 if (htab->opd_abi)
13096                   {
13097                     bfd_vma toc = elf_gp (info->output_bfd);
13098                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13099                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13100                   }
13101               }
13102             else
13103               {
13104                 rela.r_offset = (plt->output_section->vma
13105                                  + plt->output_offset
13106                                  + ent->plt.offset);
13107                 loc = relplt->contents + (relplt->reloc_count++
13108                                           * sizeof (Elf64_External_Rela));
13109                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13110               }
13111           }
13112         else
13113           {
13114             rela.r_offset = (htab->elf.splt->output_section->vma
13115                              + htab->elf.splt->output_offset
13116                              + ent->plt.offset);
13117             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13118             rela.r_addend = ent->addend;
13119             loc = (htab->elf.srelplt->contents
13120                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13121                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13122             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13123               htab->maybe_local_ifunc_resolver = 1;
13124             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13125           }
13126       }
13127
13128   if (!h->pointer_equality_needed)
13129     return TRUE;
13130
13131   if (h->def_regular)
13132     return TRUE;
13133
13134   s = htab->global_entry;
13135   if (s == NULL || s->size == 0)
13136     return TRUE;
13137
13138   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13139     if (ent->plt.offset != (bfd_vma) -1
13140         && ent->addend == 0)
13141       {
13142         bfd_byte *p;
13143         asection *plt;
13144         bfd_vma off;
13145
13146         p = s->contents + h->root.u.def.value;
13147         plt = htab->elf.splt;
13148         if (!htab->elf.dynamic_sections_created
13149             || h->dynindx == -1)
13150           {
13151             if (h->type == STT_GNU_IFUNC)
13152               plt = htab->elf.iplt;
13153             else
13154               plt = htab->pltlocal;
13155           }
13156         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13157         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13158
13159         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13160           {
13161             info->callbacks->einfo
13162               (_("%P: linkage table error against `%pT'\n"),
13163                h->root.root.string);
13164             bfd_set_error (bfd_error_bad_value);
13165             htab->stub_error = TRUE;
13166           }
13167
13168         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13169         if (htab->params->emit_stub_syms)
13170           {
13171             size_t len = strlen (h->root.root.string);
13172             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13173
13174             if (name == NULL)
13175               return FALSE;
13176
13177             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13178             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13179             if (h == NULL)
13180               return FALSE;
13181             if (h->root.type == bfd_link_hash_new)
13182               {
13183                 h->root.type = bfd_link_hash_defined;
13184                 h->root.u.def.section = s;
13185                 h->root.u.def.value = p - s->contents;
13186                 h->ref_regular = 1;
13187                 h->def_regular = 1;
13188                 h->ref_regular_nonweak = 1;
13189                 h->forced_local = 1;
13190                 h->non_elf = 0;
13191                 h->root.linker_def = 1;
13192               }
13193           }
13194
13195         if (PPC_HA (off) != 0)
13196           {
13197             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13198             p += 4;
13199           }
13200         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13201         p += 4;
13202         bfd_put_32 (s->owner, MTCTR_R12, p);
13203         p += 4;
13204         bfd_put_32 (s->owner, BCTR, p);
13205         break;
13206       }
13207   return TRUE;
13208 }
13209
13210 /* Write PLT relocs for locals.  */
13211
13212 static bfd_boolean
13213 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13214 {
13215   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13216   bfd *ibfd;
13217
13218   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13219     {
13220       struct got_entry **lgot_ents, **end_lgot_ents;
13221       struct plt_entry **local_plt, **lplt, **end_local_plt;
13222       Elf_Internal_Shdr *symtab_hdr;
13223       bfd_size_type locsymcount;
13224       Elf_Internal_Sym *local_syms = NULL;
13225       struct plt_entry *ent;
13226
13227       if (!is_ppc64_elf (ibfd))
13228         continue;
13229
13230       lgot_ents = elf_local_got_ents (ibfd);
13231       if (!lgot_ents)
13232         continue;
13233
13234       symtab_hdr = &elf_symtab_hdr (ibfd);
13235       locsymcount = symtab_hdr->sh_info;
13236       end_lgot_ents = lgot_ents + locsymcount;
13237       local_plt = (struct plt_entry **) end_lgot_ents;
13238       end_local_plt = local_plt + locsymcount;
13239       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13240         for (ent = *lplt; ent != NULL; ent = ent->next)
13241           if (ent->plt.offset != (bfd_vma) -1)
13242             {
13243               Elf_Internal_Sym *sym;
13244               asection *sym_sec;
13245               asection *plt, *relplt;
13246               bfd_byte *loc;
13247               bfd_vma val;
13248
13249               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13250                               lplt - local_plt, ibfd))
13251                 {
13252                   if (local_syms != NULL
13253                       && symtab_hdr->contents != (unsigned char *) local_syms)
13254                     free (local_syms);
13255                   return FALSE;
13256                 }
13257
13258               val = sym->st_value + ent->addend;
13259               if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13260                 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13261               if (sym_sec != NULL && sym_sec->output_section != NULL)
13262                 val += sym_sec->output_offset + sym_sec->output_section->vma;
13263
13264               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13265                 {
13266                   htab->local_ifunc_resolver = 1;
13267                   plt = htab->elf.iplt;
13268                   relplt = htab->elf.irelplt;
13269                 }
13270               else
13271                 {
13272                   plt = htab->pltlocal;
13273                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13274                 }
13275
13276               if (relplt == NULL)
13277                 {
13278                   loc = plt->contents + ent->plt.offset;
13279                   bfd_put_64 (info->output_bfd, val, loc);
13280                   if (htab->opd_abi)
13281                     {
13282                       bfd_vma toc = elf_gp (ibfd);
13283                       bfd_put_64 (info->output_bfd, toc, loc + 8);
13284                     }
13285                 }
13286               else
13287                 {
13288                   Elf_Internal_Rela rela;
13289                   rela.r_offset = (ent->plt.offset
13290                                    + plt->output_offset
13291                                    + plt->output_section->vma);
13292                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13293                     {
13294                       if (htab->opd_abi)
13295                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13296                       else
13297                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13298                     }
13299                   else
13300                     {
13301                       if (htab->opd_abi)
13302                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13303                       else
13304                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13305                     }
13306                   rela.r_addend = val;
13307                   loc = relplt->contents + (relplt->reloc_count++
13308                                             * sizeof (Elf64_External_Rela));
13309                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13310                 }
13311             }
13312
13313       if (local_syms != NULL
13314           && symtab_hdr->contents != (unsigned char *) local_syms)
13315         {
13316           if (!info->keep_memory)
13317             free (local_syms);
13318           else
13319             symtab_hdr->contents = (unsigned char *) local_syms;
13320         }
13321     }
13322   return TRUE;
13323 }
13324
13325 /* Build all the stubs associated with the current output file.
13326    The stubs are kept in a hash table attached to the main linker
13327    hash table.  This function is called via gldelf64ppc_finish.  */
13328
13329 bfd_boolean
13330 ppc64_elf_build_stubs (struct bfd_link_info *info,
13331                        char **stats)
13332 {
13333   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13334   struct map_stub *group;
13335   asection *stub_sec;
13336   bfd_byte *p;
13337   int stub_sec_count = 0;
13338
13339   if (htab == NULL)
13340     return FALSE;
13341
13342   /* Allocate memory to hold the linker stubs.  */
13343   for (group = htab->group; group != NULL; group = group->next)
13344     {
13345       group->eh_size = 0;
13346       group->lr_restore = 0;
13347       if ((stub_sec = group->stub_sec) != NULL
13348           && stub_sec->size != 0)
13349         {
13350           stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13351                                            stub_sec->size);
13352           if (stub_sec->contents == NULL)
13353             return FALSE;
13354           stub_sec->size = 0;
13355         }
13356     }
13357
13358   if (htab->glink != NULL && htab->glink->size != 0)
13359     {
13360       unsigned int indx;
13361       bfd_vma plt0;
13362
13363       /* Build the .glink plt call stub.  */
13364       if (htab->params->emit_stub_syms)
13365         {
13366           struct elf_link_hash_entry *h;
13367           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13368                                     TRUE, FALSE, FALSE);
13369           if (h == NULL)
13370             return FALSE;
13371           if (h->root.type == bfd_link_hash_new)
13372             {
13373               h->root.type = bfd_link_hash_defined;
13374               h->root.u.def.section = htab->glink;
13375               h->root.u.def.value = 8;
13376               h->ref_regular = 1;
13377               h->def_regular = 1;
13378               h->ref_regular_nonweak = 1;
13379               h->forced_local = 1;
13380               h->non_elf = 0;
13381               h->root.linker_def = 1;
13382             }
13383         }
13384       plt0 = (htab->elf.splt->output_section->vma
13385               + htab->elf.splt->output_offset
13386               - 16);
13387       if (info->emitrelocations)
13388         {
13389           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13390           if (r == NULL)
13391             return FALSE;
13392           r->r_offset = (htab->glink->output_offset
13393                          + htab->glink->output_section->vma);
13394           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13395           r->r_addend = plt0;
13396         }
13397       p = htab->glink->contents;
13398       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13399       bfd_put_64 (htab->glink->owner, plt0, p);
13400       p += 8;
13401       if (htab->opd_abi)
13402         {
13403           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13404           p += 4;
13405           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13406           p += 4;
13407           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13408           p += 4;
13409           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13410           p += 4;
13411           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13412           p += 4;
13413           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13414           p += 4;
13415           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13416           p += 4;
13417           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13418           p += 4;
13419           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13420           p += 4;
13421           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13422           p += 4;
13423         }
13424       else
13425         {
13426           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13427           p += 4;
13428           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13429           p += 4;
13430           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13431           p += 4;
13432           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13433           p += 4;
13434           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13435           p += 4;
13436           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13437           p += 4;
13438           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13439           p += 4;
13440           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13441           p += 4;
13442           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13443           p += 4;
13444           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13445           p += 4;
13446           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13447           p += 4;
13448           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13449           p += 4;
13450           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13451           p += 4;
13452         }
13453       bfd_put_32 (htab->glink->owner, BCTR, p);
13454       p += 4;
13455       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13456
13457       /* Build the .glink lazy link call stubs.  */
13458       indx = 0;
13459       while (p < htab->glink->contents + htab->glink->size)
13460         {
13461           if (htab->opd_abi)
13462             {
13463               if (indx < 0x8000)
13464                 {
13465                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13466                   p += 4;
13467                 }
13468               else
13469                 {
13470                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13471                   p += 4;
13472                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13473                               p);
13474                   p += 4;
13475                 }
13476             }
13477           bfd_put_32 (htab->glink->owner,
13478                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13479           indx++;
13480           p += 4;
13481         }
13482     }
13483
13484   /* Build .glink global entry stubs, and PLT relocs for globals.  */
13485   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13486
13487   if (!write_plt_relocs_for_local_syms (info))
13488     return FALSE;
13489
13490   if (htab->brlt != NULL && htab->brlt->size != 0)
13491     {
13492       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13493                                          htab->brlt->size);
13494       if (htab->brlt->contents == NULL)
13495         return FALSE;
13496     }
13497   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13498     {
13499       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13500                                             htab->relbrlt->size);
13501       if (htab->relbrlt->contents == NULL)
13502         return FALSE;
13503     }
13504
13505   /* Build the stubs as directed by the stub hash table.  */
13506   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13507
13508   for (group = htab->group; group != NULL; group = group->next)
13509     if (group->needs_save_res)
13510       group->stub_sec->size += htab->sfpr->size;
13511
13512   if (htab->relbrlt != NULL)
13513     htab->relbrlt->reloc_count = 0;
13514
13515   if (htab->params->plt_stub_align != 0)
13516     for (group = htab->group; group != NULL; group = group->next)
13517       if ((stub_sec = group->stub_sec) != NULL)
13518         {
13519           int align = abs (htab->params->plt_stub_align);
13520           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13521         }
13522
13523   for (group = htab->group; group != NULL; group = group->next)
13524     if (group->needs_save_res)
13525       {
13526         stub_sec = group->stub_sec;
13527         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13528                 htab->sfpr->contents, htab->sfpr->size);
13529         if (htab->params->emit_stub_syms)
13530           {
13531             unsigned int i;
13532
13533             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13534               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13535                 return FALSE;
13536           }
13537       }
13538
13539   if (htab->glink_eh_frame != NULL
13540       && htab->glink_eh_frame->size != 0)
13541     {
13542       bfd_vma val;
13543       size_t align = 4;
13544
13545       p = htab->glink_eh_frame->contents;
13546       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13547
13548       for (group = htab->group; group != NULL; group = group->next)
13549         if (group->eh_size != 0)
13550           {
13551             /* Offset to stub section.  */
13552             val = (group->stub_sec->output_section->vma
13553                    + group->stub_sec->output_offset);
13554             val -= (htab->glink_eh_frame->output_section->vma
13555                     + htab->glink_eh_frame->output_offset
13556                     + (p + 8 - htab->glink_eh_frame->contents));
13557             if (val + 0x80000000 > 0xffffffff)
13558               {
13559                 _bfd_error_handler
13560                   (_("%s offset too large for .eh_frame sdata4 encoding"),
13561                    group->stub_sec->name);
13562                 return FALSE;
13563               }
13564             bfd_put_32 (htab->elf.dynobj, val, p + 8);
13565             p += (group->eh_size + 17 + 3) & -4;
13566           }
13567       if (htab->glink != NULL && htab->glink->size != 0)
13568         {
13569           /* Offset to .glink.  */
13570           val = (htab->glink->output_section->vma
13571                  + htab->glink->output_offset
13572                  + 8);
13573           val -= (htab->glink_eh_frame->output_section->vma
13574                   + htab->glink_eh_frame->output_offset
13575                   + (p + 8 - htab->glink_eh_frame->contents));
13576           if (val + 0x80000000 > 0xffffffff)
13577             {
13578               _bfd_error_handler
13579                 (_("%s offset too large for .eh_frame sdata4 encoding"),
13580                  htab->glink->name);
13581               return FALSE;
13582             }
13583           bfd_put_32 (htab->elf.dynobj, val, p + 8);
13584           p += (24 + align - 1) & -align;
13585         }
13586     }
13587
13588   for (group = htab->group; group != NULL; group = group->next)
13589     if ((stub_sec = group->stub_sec) != NULL)
13590       {
13591         stub_sec_count += 1;
13592         if (stub_sec->rawsize != stub_sec->size
13593             && (htab->stub_iteration <= STUB_SHRINK_ITER
13594                 || stub_sec->rawsize < stub_sec->size))
13595           break;
13596       }
13597
13598   if (group != NULL)
13599     {
13600       htab->stub_error = TRUE;
13601       _bfd_error_handler (_("stubs don't match calculated size"));
13602     }
13603
13604   if (htab->stub_error)
13605     return FALSE;
13606
13607   if (stats != NULL)
13608     {
13609       size_t len;
13610       *stats = bfd_malloc (500);
13611       if (*stats == NULL)
13612         return FALSE;
13613
13614       len = sprintf (*stats,
13615                      ngettext ("linker stubs in %u group\n",
13616                                "linker stubs in %u groups\n",
13617                                stub_sec_count),
13618                      stub_sec_count);
13619       sprintf (*stats + len, _("  branch         %lu\n"
13620                                "  branch toc adj %lu\n"
13621                                "  branch notoc   %lu\n"
13622                                "  branch both    %lu\n"
13623                                "  long branch    %lu\n"
13624                                "  long toc adj   %lu\n"
13625                                "  long notoc     %lu\n"
13626                                "  long both      %lu\n"
13627                                "  plt call       %lu\n"
13628                                "  plt call save  %lu\n"
13629                                "  plt call notoc %lu\n"
13630                                "  plt call both  %lu\n"
13631                                "  global entry   %lu"),
13632                htab->stub_count[ppc_stub_long_branch - 1],
13633                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13634                htab->stub_count[ppc_stub_long_branch_notoc - 1],
13635                htab->stub_count[ppc_stub_long_branch_both - 1],
13636                htab->stub_count[ppc_stub_plt_branch - 1],
13637                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13638                htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13639                htab->stub_count[ppc_stub_plt_branch_both - 1],
13640                htab->stub_count[ppc_stub_plt_call - 1],
13641                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13642                htab->stub_count[ppc_stub_plt_call_notoc - 1],
13643                htab->stub_count[ppc_stub_plt_call_both - 1],
13644                htab->stub_count[ppc_stub_global_entry - 1]);
13645     }
13646   return TRUE;
13647 }
13648
13649 /* What to do when ld finds relocations against symbols defined in
13650    discarded sections.  */
13651
13652 static unsigned int
13653 ppc64_elf_action_discarded (asection *sec)
13654 {
13655   if (strcmp (".opd", sec->name) == 0)
13656     return 0;
13657
13658   if (strcmp (".toc", sec->name) == 0)
13659     return 0;
13660
13661   if (strcmp (".toc1", sec->name) == 0)
13662     return 0;
13663
13664   return _bfd_elf_default_action_discarded (sec);
13665 }
13666
13667 /* The RELOCATE_SECTION function is called by the ELF backend linker
13668    to handle the relocations for a section.
13669
13670    The relocs are always passed as Rela structures; if the section
13671    actually uses Rel structures, the r_addend field will always be
13672    zero.
13673
13674    This function is responsible for adjust the section contents as
13675    necessary, and (if using Rela relocs and generating a
13676    relocatable output file) adjusting the reloc addend as
13677    necessary.
13678
13679    This function does not have to worry about setting the reloc
13680    address or the reloc symbol index.
13681
13682    LOCAL_SYMS is a pointer to the swapped in local symbols.
13683
13684    LOCAL_SECTIONS is an array giving the section in the input file
13685    corresponding to the st_shndx field of each local symbol.
13686
13687    The global hash table entry for the global symbols can be found
13688    via elf_sym_hashes (input_bfd).
13689
13690    When generating relocatable output, this function must handle
13691    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13692    going to be the section symbol corresponding to the output
13693    section, which means that the addend must be adjusted
13694    accordingly.  */
13695
13696 static bfd_boolean
13697 ppc64_elf_relocate_section (bfd *output_bfd,
13698                             struct bfd_link_info *info,
13699                             bfd *input_bfd,
13700                             asection *input_section,
13701                             bfd_byte *contents,
13702                             Elf_Internal_Rela *relocs,
13703                             Elf_Internal_Sym *local_syms,
13704                             asection **local_sections)
13705 {
13706   struct ppc_link_hash_table *htab;
13707   Elf_Internal_Shdr *symtab_hdr;
13708   struct elf_link_hash_entry **sym_hashes;
13709   Elf_Internal_Rela *rel;
13710   Elf_Internal_Rela *wrel;
13711   Elf_Internal_Rela *relend;
13712   Elf_Internal_Rela outrel;
13713   bfd_byte *loc;
13714   struct got_entry **local_got_ents;
13715   bfd_vma TOCstart;
13716   bfd_boolean ret = TRUE;
13717   bfd_boolean is_opd;
13718   /* Assume 'at' branch hints.  */
13719   bfd_boolean is_isa_v2 = TRUE;
13720   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13721
13722   /* Initialize howto table if needed.  */
13723   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13724     ppc_howto_init ();
13725
13726   htab = ppc_hash_table (info);
13727   if (htab == NULL)
13728     return FALSE;
13729
13730   /* Don't relocate stub sections.  */
13731   if (input_section->owner == htab->params->stub_bfd)
13732     return TRUE;
13733
13734   if (!is_ppc64_elf (input_bfd))
13735     {
13736       bfd_set_error (bfd_error_wrong_format);
13737       return FALSE;
13738     }
13739
13740   local_got_ents = elf_local_got_ents (input_bfd);
13741   TOCstart = elf_gp (output_bfd);
13742   symtab_hdr = &elf_symtab_hdr (input_bfd);
13743   sym_hashes = elf_sym_hashes (input_bfd);
13744   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13745
13746   rel = wrel = relocs;
13747   relend = relocs + input_section->reloc_count;
13748   for (; rel < relend; wrel++, rel++)
13749     {
13750       enum elf_ppc64_reloc_type r_type;
13751       bfd_vma addend;
13752       bfd_reloc_status_type r;
13753       Elf_Internal_Sym *sym;
13754       asection *sec;
13755       struct elf_link_hash_entry *h_elf;
13756       struct ppc_link_hash_entry *h;
13757       struct ppc_link_hash_entry *fdh;
13758       const char *sym_name;
13759       unsigned long r_symndx, toc_symndx;
13760       bfd_vma toc_addend;
13761       unsigned char tls_mask, tls_gd, tls_type;
13762       unsigned char sym_type;
13763       bfd_vma relocation;
13764       bfd_boolean unresolved_reloc, save_unresolved_reloc;
13765       bfd_boolean warned;
13766       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13767       unsigned int insn;
13768       unsigned int mask;
13769       struct ppc_stub_hash_entry *stub_entry;
13770       bfd_vma max_br_offset;
13771       bfd_vma from;
13772       Elf_Internal_Rela orig_rel;
13773       reloc_howto_type *howto;
13774       struct reloc_howto_struct alt_howto;
13775
13776     again:
13777       orig_rel = *rel;
13778
13779       r_type = ELF64_R_TYPE (rel->r_info);
13780       r_symndx = ELF64_R_SYM (rel->r_info);
13781
13782       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13783          symbol of the previous ADDR64 reloc.  The symbol gives us the
13784          proper TOC base to use.  */
13785       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13786           && wrel != relocs
13787           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13788           && is_opd)
13789         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13790
13791       sym = NULL;
13792       sec = NULL;
13793       h_elf = NULL;
13794       sym_name = NULL;
13795       unresolved_reloc = FALSE;
13796       warned = FALSE;
13797
13798       if (r_symndx < symtab_hdr->sh_info)
13799         {
13800           /* It's a local symbol.  */
13801           struct _opd_sec_data *opd;
13802
13803           sym = local_syms + r_symndx;
13804           sec = local_sections[r_symndx];
13805           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13806           sym_type = ELF64_ST_TYPE (sym->st_info);
13807           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13808           opd = get_opd_info (sec);
13809           if (opd != NULL && opd->adjust != NULL)
13810             {
13811               long adjust = opd->adjust[OPD_NDX (sym->st_value
13812                                                  + rel->r_addend)];
13813               if (adjust == -1)
13814                 relocation = 0;
13815               else
13816                 {
13817                   /* If this is a relocation against the opd section sym
13818                      and we have edited .opd, adjust the reloc addend so
13819                      that ld -r and ld --emit-relocs output is correct.
13820                      If it is a reloc against some other .opd symbol,
13821                      then the symbol value will be adjusted later.  */
13822                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13823                     rel->r_addend += adjust;
13824                   else
13825                     relocation += adjust;
13826                 }
13827             }
13828         }
13829       else
13830         {
13831           bfd_boolean ignored;
13832
13833           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13834                                    r_symndx, symtab_hdr, sym_hashes,
13835                                    h_elf, sec, relocation,
13836                                    unresolved_reloc, warned, ignored);
13837           sym_name = h_elf->root.root.string;
13838           sym_type = h_elf->type;
13839           if (sec != NULL
13840               && sec->owner == output_bfd
13841               && strcmp (sec->name, ".opd") == 0)
13842             {
13843               /* This is a symbol defined in a linker script.  All
13844                  such are defined in output sections, even those
13845                  defined by simple assignment from a symbol defined in
13846                  an input section.  Transfer the symbol to an
13847                  appropriate input .opd section, so that a branch to
13848                  this symbol will be mapped to the location specified
13849                  by the opd entry.  */
13850               struct bfd_link_order *lo;
13851               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13852                 if (lo->type == bfd_indirect_link_order)
13853                   {
13854                     asection *isec = lo->u.indirect.section;
13855                     if (h_elf->root.u.def.value >= isec->output_offset
13856                         && h_elf->root.u.def.value < (isec->output_offset
13857                                                       + isec->size))
13858                       {
13859                         h_elf->root.u.def.value -= isec->output_offset;
13860                         h_elf->root.u.def.section = isec;
13861                         sec = isec;
13862                         break;
13863                       }
13864                   }
13865             }
13866         }
13867       h = (struct ppc_link_hash_entry *) h_elf;
13868
13869       if (sec != NULL && discarded_section (sec))
13870         {
13871           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13872                                input_bfd, input_section,
13873                                contents, rel->r_offset);
13874           wrel->r_offset = rel->r_offset;
13875           wrel->r_info = 0;
13876           wrel->r_addend = 0;
13877
13878           /* For ld -r, remove relocations in debug sections against
13879              symbols defined in discarded sections.  Not done for
13880              non-debug to preserve relocs in .eh_frame which the
13881              eh_frame editing code expects to be present.  */
13882           if (bfd_link_relocatable (info)
13883               && (input_section->flags & SEC_DEBUGGING))
13884             wrel--;
13885
13886           continue;
13887         }
13888
13889       if (bfd_link_relocatable (info))
13890         goto copy_reloc;
13891
13892       if (h != NULL && &h->elf == htab->elf.hgot)
13893         {
13894           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13895           sec = bfd_abs_section_ptr;
13896           unresolved_reloc = FALSE;
13897         }
13898
13899       /* TLS optimizations.  Replace instruction sequences and relocs
13900          based on information we collected in tls_optimize.  We edit
13901          RELOCS so that --emit-relocs will output something sensible
13902          for the final instruction stream.  */
13903       tls_mask = 0;
13904       tls_gd = 0;
13905       toc_symndx = 0;
13906       if (h != NULL)
13907         tls_mask = h->tls_mask;
13908       else if (local_got_ents != NULL)
13909         {
13910           struct plt_entry **local_plt = (struct plt_entry **)
13911             (local_got_ents + symtab_hdr->sh_info);
13912           unsigned char *lgot_masks = (unsigned char *)
13913             (local_plt + symtab_hdr->sh_info);
13914           tls_mask = lgot_masks[r_symndx];
13915         }
13916       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13917           && (r_type == R_PPC64_TLS
13918               || r_type == R_PPC64_TLSGD
13919               || r_type == R_PPC64_TLSLD))
13920         {
13921           /* Check for toc tls entries.  */
13922           unsigned char *toc_tls;
13923
13924           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13925                              &local_syms, rel, input_bfd))
13926             return FALSE;
13927
13928           if (toc_tls)
13929             tls_mask = *toc_tls;
13930         }
13931
13932       /* Check that tls relocs are used with tls syms, and non-tls
13933          relocs are used with non-tls syms.  */
13934       if (r_symndx != STN_UNDEF
13935           && r_type != R_PPC64_NONE
13936           && (h == NULL
13937               || h->elf.root.type == bfd_link_hash_defined
13938               || h->elf.root.type == bfd_link_hash_defweak)
13939           && (IS_PPC64_TLS_RELOC (r_type)
13940               != (sym_type == STT_TLS
13941                   || (sym_type == STT_SECTION
13942                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13943         {
13944           if ((tls_mask & TLS_TLS) != 0
13945               && (r_type == R_PPC64_TLS
13946                   || r_type == R_PPC64_TLSGD
13947                   || r_type == R_PPC64_TLSLD))
13948             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13949             ;
13950           else
13951             info->callbacks->einfo
13952               (!IS_PPC64_TLS_RELOC (r_type)
13953                /* xgettext:c-format */
13954                ? _("%H: %s used with TLS symbol `%pT'\n")
13955                /* xgettext:c-format */
13956                : _("%H: %s used with non-TLS symbol `%pT'\n"),
13957                input_bfd, input_section, rel->r_offset,
13958                ppc64_elf_howto_table[r_type]->name,
13959                sym_name);
13960         }
13961
13962       /* Ensure reloc mapping code below stays sane.  */
13963       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13964           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13965           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13966           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13967           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13968           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13969           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13970           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13971           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13972           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13973         abort ();
13974
13975       switch (r_type)
13976         {
13977         default:
13978           break;
13979
13980         case R_PPC64_LO_DS_OPT:
13981           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13982           if ((insn & (0x3f << 26)) != 58u << 26)
13983             abort ();
13984           insn += (14u << 26) - (58u << 26);
13985           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13986           r_type = R_PPC64_TOC16_LO;
13987           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13988           break;
13989
13990         case R_PPC64_TOC16:
13991         case R_PPC64_TOC16_LO:
13992         case R_PPC64_TOC16_DS:
13993         case R_PPC64_TOC16_LO_DS:
13994           {
13995             /* Check for toc tls entries.  */
13996             unsigned char *toc_tls;
13997             int retval;
13998
13999             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14000                                    &local_syms, rel, input_bfd);
14001             if (retval == 0)
14002               return FALSE;
14003
14004             if (toc_tls)
14005               {
14006                 tls_mask = *toc_tls;
14007                 if (r_type == R_PPC64_TOC16_DS
14008                     || r_type == R_PPC64_TOC16_LO_DS)
14009                   {
14010                     if ((tls_mask & TLS_TLS) != 0
14011                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14012                       goto toctprel;
14013                   }
14014                 else
14015                   {
14016                     /* If we found a GD reloc pair, then we might be
14017                        doing a GD->IE transition.  */
14018                     if (retval == 2)
14019                       {
14020                         tls_gd = TLS_TPRELGD;
14021                         if ((tls_mask & TLS_TLS) != 0
14022                             && (tls_mask & TLS_GD) == 0)
14023                           goto tls_ldgd_opt;
14024                       }
14025                     else if (retval == 3)
14026                       {
14027                         if ((tls_mask & TLS_TLS) != 0
14028                             && (tls_mask & TLS_LD) == 0)
14029                           goto tls_ldgd_opt;
14030                       }
14031                   }
14032               }
14033           }
14034           break;
14035
14036         case R_PPC64_GOT_TPREL16_HI:
14037         case R_PPC64_GOT_TPREL16_HA:
14038           if ((tls_mask & TLS_TLS) != 0
14039               && (tls_mask & TLS_TPREL) == 0)
14040             {
14041               rel->r_offset -= d_offset;
14042               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14043               r_type = R_PPC64_NONE;
14044               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14045             }
14046           break;
14047
14048         case R_PPC64_GOT_TPREL16_DS:
14049         case R_PPC64_GOT_TPREL16_LO_DS:
14050           if ((tls_mask & TLS_TLS) != 0
14051               && (tls_mask & TLS_TPREL) == 0)
14052             {
14053             toctprel:
14054               insn = bfd_get_32 (input_bfd,
14055                                  contents + rel->r_offset - d_offset);
14056               insn &= 31 << 21;
14057               insn |= 0x3c0d0000;       /* addis 0,13,0 */
14058               bfd_put_32 (input_bfd, insn,
14059                           contents + rel->r_offset - d_offset);
14060               r_type = R_PPC64_TPREL16_HA;
14061               if (toc_symndx != 0)
14062                 {
14063                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14064                   rel->r_addend = toc_addend;
14065                   /* We changed the symbol.  Start over in order to
14066                      get h, sym, sec etc. right.  */
14067                   goto again;
14068                 }
14069               else
14070                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14071             }
14072           break;
14073
14074         case R_PPC64_TLS:
14075           if ((tls_mask & TLS_TLS) != 0
14076               && (tls_mask & TLS_TPREL) == 0)
14077             {
14078               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14079               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14080               if (insn == 0)
14081                 abort ();
14082               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14083               /* Was PPC64_TLS which sits on insn boundary, now
14084                  PPC64_TPREL16_LO which is at low-order half-word.  */
14085               rel->r_offset += d_offset;
14086               r_type = R_PPC64_TPREL16_LO;
14087               if (toc_symndx != 0)
14088                 {
14089                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14090                   rel->r_addend = toc_addend;
14091                   /* We changed the symbol.  Start over in order to
14092                      get h, sym, sec etc. right.  */
14093                   goto again;
14094                 }
14095               else
14096                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14097             }
14098           break;
14099
14100         case R_PPC64_GOT_TLSGD16_HI:
14101         case R_PPC64_GOT_TLSGD16_HA:
14102           tls_gd = TLS_TPRELGD;
14103           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14104             goto tls_gdld_hi;
14105           break;
14106
14107         case R_PPC64_GOT_TLSLD16_HI:
14108         case R_PPC64_GOT_TLSLD16_HA:
14109           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14110             {
14111             tls_gdld_hi:
14112               if ((tls_mask & tls_gd) != 0)
14113                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14114                           + R_PPC64_GOT_TPREL16_DS);
14115               else
14116                 {
14117                   rel->r_offset -= d_offset;
14118                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14119                   r_type = R_PPC64_NONE;
14120                 }
14121               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14122             }
14123           break;
14124
14125         case R_PPC64_GOT_TLSGD16:
14126         case R_PPC64_GOT_TLSGD16_LO:
14127           tls_gd = TLS_TPRELGD;
14128           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14129             goto tls_ldgd_opt;
14130           break;
14131
14132         case R_PPC64_GOT_TLSLD16:
14133         case R_PPC64_GOT_TLSLD16_LO:
14134           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14135             {
14136               unsigned int insn1, insn2;
14137               bfd_vma offset;
14138
14139             tls_ldgd_opt:
14140               offset = (bfd_vma) -1;
14141               /* If not using the newer R_PPC64_TLSGD/LD to mark
14142                  __tls_get_addr calls, we must trust that the call
14143                  stays with its arg setup insns, ie. that the next
14144                  reloc is the __tls_get_addr call associated with
14145                  the current reloc.  Edit both insns.  */
14146               if (input_section->has_tls_get_addr_call
14147                   && rel + 1 < relend
14148                   && branch_reloc_hash_match (input_bfd, rel + 1,
14149                                               htab->tls_get_addr,
14150                                               htab->tls_get_addr_fd))
14151                 offset = rel[1].r_offset;
14152               /* We read the low GOT_TLS (or TOC16) insn because we
14153                  need to keep the destination reg.  It may be
14154                  something other than the usual r3, and moved to r3
14155                  before the call by intervening code.  */
14156               insn1 = bfd_get_32 (input_bfd,
14157                                   contents + rel->r_offset - d_offset);
14158               if ((tls_mask & tls_gd) != 0)
14159                 {
14160                   /* IE */
14161                   insn1 &= (0x1f << 21) | (0x1f << 16);
14162                   insn1 |= 58 << 26;    /* ld */
14163                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14164                   if (offset != (bfd_vma) -1)
14165                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14166                   if ((tls_mask & TLS_EXPLICIT) == 0)
14167                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14168                               + R_PPC64_GOT_TPREL16_DS);
14169                   else
14170                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14171                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14172                 }
14173               else
14174                 {
14175                   /* LE */
14176                   insn1 &= 0x1f << 21;
14177                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
14178                   insn2 = 0x38630000;   /* addi 3,3,0 */
14179                   if (tls_gd == 0)
14180                     {
14181                       /* Was an LD reloc.  */
14182                       if (toc_symndx)
14183                         sec = local_sections[toc_symndx];
14184                       for (r_symndx = 0;
14185                            r_symndx < symtab_hdr->sh_info;
14186                            r_symndx++)
14187                         if (local_sections[r_symndx] == sec)
14188                           break;
14189                       if (r_symndx >= symtab_hdr->sh_info)
14190                         r_symndx = STN_UNDEF;
14191                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14192                       if (r_symndx != STN_UNDEF)
14193                         rel->r_addend -= (local_syms[r_symndx].st_value
14194                                           + sec->output_offset
14195                                           + sec->output_section->vma);
14196                     }
14197                   else if (toc_symndx != 0)
14198                     {
14199                       r_symndx = toc_symndx;
14200                       rel->r_addend = toc_addend;
14201                     }
14202                   r_type = R_PPC64_TPREL16_HA;
14203                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14204                   if (offset != (bfd_vma) -1)
14205                     {
14206                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14207                                                     R_PPC64_TPREL16_LO);
14208                       rel[1].r_offset = offset + d_offset;
14209                       rel[1].r_addend = rel->r_addend;
14210                     }
14211                 }
14212               bfd_put_32 (input_bfd, insn1,
14213                           contents + rel->r_offset - d_offset);
14214               if (offset != (bfd_vma) -1)
14215                 {
14216                   bfd_put_32 (input_bfd, insn2, contents + offset);
14217                   if (offset + 8 <= input_section->size)
14218                     {
14219                       insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14220                       if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14221                         bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14222                     }
14223                 }
14224               if ((tls_mask & tls_gd) == 0
14225                   && (tls_gd == 0 || toc_symndx != 0))
14226                 {
14227                   /* We changed the symbol.  Start over in order
14228                      to get h, sym, sec etc. right.  */
14229                   goto again;
14230                 }
14231             }
14232           break;
14233
14234         case R_PPC64_TLSGD:
14235           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14236               && rel + 1 < relend)
14237             {
14238               unsigned int insn2;
14239               bfd_vma offset = rel->r_offset;
14240               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14241
14242               if (is_plt_seq_reloc (r_type1))
14243                 {
14244                   bfd_put_32 (output_bfd, NOP, contents + offset);
14245                   if (r_type1 == R_PPC64_PLT_PCREL34
14246                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14247                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14248                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14249                   break;
14250                 }
14251
14252               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14253                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14254
14255               if ((tls_mask & TLS_TPRELGD) != 0)
14256                 {
14257                   /* IE */
14258                   r_type = R_PPC64_NONE;
14259                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14260                 }
14261               else
14262                 {
14263                   /* LE */
14264                   if (toc_symndx != 0)
14265                     {
14266                       r_symndx = toc_symndx;
14267                       rel->r_addend = toc_addend;
14268                     }
14269                   r_type = R_PPC64_TPREL16_LO;
14270                   rel->r_offset = offset + d_offset;
14271                   insn2 = 0x38630000;   /* addi 3,3,0 */
14272                 }
14273               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14274               /* Zap the reloc on the _tls_get_addr call too.  */
14275               BFD_ASSERT (offset == rel[1].r_offset);
14276               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14277               bfd_put_32 (input_bfd, insn2, contents + offset);
14278               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14279                 goto again;
14280             }
14281           break;
14282
14283         case R_PPC64_TLSLD:
14284           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14285               && rel + 1 < relend)
14286             {
14287               unsigned int insn2;
14288               bfd_vma offset = rel->r_offset;
14289               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14290
14291               if (is_plt_seq_reloc (r_type1))
14292                 {
14293                   bfd_put_32 (output_bfd, NOP, contents + offset);
14294                   if (r_type1 == R_PPC64_PLT_PCREL34
14295                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14296                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14297                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14298                   break;
14299                 }
14300
14301               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14302                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14303
14304               if (toc_symndx)
14305                 sec = local_sections[toc_symndx];
14306               for (r_symndx = 0;
14307                    r_symndx < symtab_hdr->sh_info;
14308                    r_symndx++)
14309                 if (local_sections[r_symndx] == sec)
14310                   break;
14311               if (r_symndx >= symtab_hdr->sh_info)
14312                 r_symndx = STN_UNDEF;
14313               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14314               if (r_symndx != STN_UNDEF)
14315                 rel->r_addend -= (local_syms[r_symndx].st_value
14316                                   + sec->output_offset
14317                                   + sec->output_section->vma);
14318
14319               r_type = R_PPC64_TPREL16_LO;
14320               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14321               rel->r_offset = offset + d_offset;
14322               /* Zap the reloc on the _tls_get_addr call too.  */
14323               BFD_ASSERT (offset == rel[1].r_offset);
14324               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14325               insn2 = 0x38630000;       /* addi 3,3,0 */
14326               bfd_put_32 (input_bfd, insn2, contents + offset);
14327               goto again;
14328             }
14329           break;
14330
14331         case R_PPC64_DTPMOD64:
14332           if (rel + 1 < relend
14333               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14334               && rel[1].r_offset == rel->r_offset + 8)
14335             {
14336               if ((tls_mask & TLS_GD) == 0)
14337                 {
14338                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14339                   if ((tls_mask & TLS_TPRELGD) != 0)
14340                     r_type = R_PPC64_TPREL64;
14341                   else
14342                     {
14343                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14344                       r_type = R_PPC64_NONE;
14345                     }
14346                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14347                 }
14348             }
14349           else
14350             {
14351               if ((tls_mask & TLS_LD) == 0)
14352                 {
14353                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14354                   r_type = R_PPC64_NONE;
14355                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14356                 }
14357             }
14358           break;
14359
14360         case R_PPC64_TPREL64:
14361           if ((tls_mask & TLS_TPREL) == 0)
14362             {
14363               r_type = R_PPC64_NONE;
14364               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14365             }
14366           break;
14367
14368         case R_PPC64_ENTRY:
14369           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14370           if (!bfd_link_pic (info)
14371               && !info->traditional_format
14372               && relocation + 0x80008000 <= 0xffffffff)
14373             {
14374               unsigned int insn1, insn2;
14375
14376               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14377               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14378               if ((insn1 & ~0xfffc) == LD_R2_0R12
14379                   && insn2 == ADD_R2_R2_R12)
14380                 {
14381                   bfd_put_32 (input_bfd,
14382                               LIS_R2 + PPC_HA (relocation),
14383                               contents + rel->r_offset);
14384                   bfd_put_32 (input_bfd,
14385                               ADDI_R2_R2 + PPC_LO (relocation),
14386                               contents + rel->r_offset + 4);
14387                 }
14388             }
14389           else
14390             {
14391               relocation -= (rel->r_offset
14392                              + input_section->output_offset
14393                              + input_section->output_section->vma);
14394               if (relocation + 0x80008000 <= 0xffffffff)
14395                 {
14396                   unsigned int insn1, insn2;
14397
14398                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14399                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14400                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14401                       && insn2 == ADD_R2_R2_R12)
14402                     {
14403                       bfd_put_32 (input_bfd,
14404                                   ADDIS_R2_R12 + PPC_HA (relocation),
14405                                   contents + rel->r_offset);
14406                       bfd_put_32 (input_bfd,
14407                                   ADDI_R2_R2 + PPC_LO (relocation),
14408                                   contents + rel->r_offset + 4);
14409                     }
14410                 }
14411             }
14412           break;
14413
14414         case R_PPC64_REL16_HA:
14415           /* If we are generating a non-PIC executable, edit
14416              .  0:      addis 2,12,.TOC.-0b@ha
14417              .          addi 2,2,.TOC.-0b@l
14418              used by ELFv2 global entry points to set up r2, to
14419              .          lis 2,.TOC.@ha
14420              .          addi 2,2,.TOC.@l
14421              if .TOC. is in range.  */
14422           if (!bfd_link_pic (info)
14423               && !info->traditional_format
14424               && !htab->opd_abi
14425               && rel->r_addend == d_offset
14426               && h != NULL && &h->elf == htab->elf.hgot
14427               && rel + 1 < relend
14428               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14429               && rel[1].r_offset == rel->r_offset + 4
14430               && rel[1].r_addend == rel->r_addend + 4
14431               && relocation + 0x80008000 <= 0xffffffff)
14432             {
14433               unsigned int insn1, insn2;
14434               bfd_vma offset = rel->r_offset - d_offset;
14435               insn1 = bfd_get_32 (input_bfd, contents + offset);
14436               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14437               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14438                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14439                 {
14440                   r_type = R_PPC64_ADDR16_HA;
14441                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14442                   rel->r_addend -= d_offset;
14443                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14444                   rel[1].r_addend -= d_offset + 4;
14445                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14446                 }
14447             }
14448           break;
14449         }
14450
14451       /* Handle other relocations that tweak non-addend part of insn.  */
14452       insn = 0;
14453       max_br_offset = 1 << 25;
14454       addend = rel->r_addend;
14455       reloc_dest = DEST_NORMAL;
14456       switch (r_type)
14457         {
14458         default:
14459           break;
14460
14461         case R_PPC64_TOCSAVE:
14462           if (relocation + addend == (rel->r_offset
14463                                       + input_section->output_offset
14464                                       + input_section->output_section->vma)
14465               && tocsave_find (htab, NO_INSERT,
14466                                &local_syms, rel, input_bfd))
14467             {
14468               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14469               if (insn == NOP
14470                   || insn == CROR_151515 || insn == CROR_313131)
14471                 bfd_put_32 (input_bfd,
14472                             STD_R2_0R1 + STK_TOC (htab),
14473                             contents + rel->r_offset);
14474             }
14475           break;
14476
14477           /* Branch taken prediction relocations.  */
14478         case R_PPC64_ADDR14_BRTAKEN:
14479         case R_PPC64_REL14_BRTAKEN:
14480           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14481           /* Fall through.  */
14482
14483           /* Branch not taken prediction relocations.  */
14484         case R_PPC64_ADDR14_BRNTAKEN:
14485         case R_PPC64_REL14_BRNTAKEN:
14486           insn |= bfd_get_32 (input_bfd,
14487                               contents + rel->r_offset) & ~(0x01 << 21);
14488           /* Fall through.  */
14489
14490         case R_PPC64_REL14:
14491           max_br_offset = 1 << 15;
14492           /* Fall through.  */
14493
14494         case R_PPC64_REL24:
14495         case R_PPC64_REL24_NOTOC:
14496         case R_PPC64_PLTCALL:
14497         case R_PPC64_PLTCALL_NOTOC:
14498           /* Calls to functions with a different TOC, such as calls to
14499              shared objects, need to alter the TOC pointer.  This is
14500              done using a linkage stub.  A REL24 branching to these
14501              linkage stubs needs to be followed by a nop, as the nop
14502              will be replaced with an instruction to restore the TOC
14503              base pointer.  */
14504           fdh = h;
14505           if (h != NULL
14506               && h->oh != NULL
14507               && h->oh->is_func_descriptor)
14508             fdh = ppc_follow_link (h->oh);
14509           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14510                                            htab);
14511           if ((r_type == R_PPC64_PLTCALL
14512                || r_type == R_PPC64_PLTCALL_NOTOC)
14513               && stub_entry != NULL
14514               && stub_entry->stub_type >= ppc_stub_plt_call
14515               && stub_entry->stub_type <= ppc_stub_plt_call_both)
14516             stub_entry = NULL;
14517
14518           if (stub_entry != NULL
14519               && ((stub_entry->stub_type >= ppc_stub_plt_call
14520                    && stub_entry->stub_type <= ppc_stub_plt_call_both)
14521                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14522                   || stub_entry->stub_type == ppc_stub_plt_branch_both
14523                   || stub_entry->stub_type == ppc_stub_long_branch_r2off
14524                   || stub_entry->stub_type == ppc_stub_long_branch_both))
14525             {
14526               bfd_boolean can_plt_call = FALSE;
14527
14528               if (stub_entry->stub_type == ppc_stub_plt_call
14529                   && !htab->opd_abi
14530                   && htab->params->plt_localentry0 != 0
14531                   && is_elfv2_localentry0 (&h->elf))
14532                 {
14533                   /* The function doesn't use or change r2.  */
14534                   can_plt_call = TRUE;
14535                 }
14536               else if (r_type == R_PPC64_REL24_NOTOC)
14537                 {
14538                   /* NOTOC calls don't need to restore r2.  */
14539                   can_plt_call = TRUE;
14540                 }
14541
14542               /* All of these stubs may modify r2, so there must be a
14543                  branch and link followed by a nop.  The nop is
14544                  replaced by an insn to restore r2.  */
14545               else if (rel->r_offset + 8 <= input_section->size)
14546                 {
14547                   unsigned long br;
14548
14549                   br = bfd_get_32 (input_bfd,
14550                                    contents + rel->r_offset);
14551                   if ((br & 1) != 0)
14552                     {
14553                       unsigned long nop;
14554
14555                       nop = bfd_get_32 (input_bfd,
14556                                         contents + rel->r_offset + 4);
14557                       if (nop == LD_R2_0R1 + STK_TOC (htab))
14558                         can_plt_call = TRUE;
14559                       else if (nop == NOP
14560                                || nop == CROR_151515
14561                                || nop == CROR_313131)
14562                         {
14563                           if (h != NULL
14564                               && (h == htab->tls_get_addr_fd
14565                                   || h == htab->tls_get_addr)
14566                               && htab->params->tls_get_addr_opt)
14567                             {
14568                               /* Special stub used, leave nop alone.  */
14569                             }
14570                           else
14571                             bfd_put_32 (input_bfd,
14572                                         LD_R2_0R1 + STK_TOC (htab),
14573                                         contents + rel->r_offset + 4);
14574                           can_plt_call = TRUE;
14575                         }
14576                     }
14577                 }
14578
14579               if (!can_plt_call && h != NULL)
14580                 {
14581                   const char *name = h->elf.root.root.string;
14582
14583                   if (*name == '.')
14584                     ++name;
14585
14586                   if (strncmp (name, "__libc_start_main", 17) == 0
14587                       && (name[17] == 0 || name[17] == '@'))
14588                     {
14589                       /* Allow crt1 branch to go via a toc adjusting
14590                          stub.  Other calls that never return could do
14591                          the same, if we could detect such.  */
14592                       can_plt_call = TRUE;
14593                     }
14594                 }
14595
14596               if (!can_plt_call)
14597                 {
14598                   /* g++ as of 20130507 emits self-calls without a
14599                      following nop.  This is arguably wrong since we
14600                      have conflicting information.  On the one hand a
14601                      global symbol and on the other a local call
14602                      sequence, but don't error for this special case.
14603                      It isn't possible to cheaply verify we have
14604                      exactly such a call.  Allow all calls to the same
14605                      section.  */
14606                   asection *code_sec = sec;
14607
14608                   if (get_opd_info (sec) != NULL)
14609                     {
14610                       bfd_vma off = (relocation + addend
14611                                      - sec->output_section->vma
14612                                      - sec->output_offset);
14613
14614                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14615                     }
14616                   if (code_sec == input_section)
14617                     can_plt_call = TRUE;
14618                 }
14619
14620               if (!can_plt_call)
14621                 {
14622                   if (stub_entry->stub_type >= ppc_stub_plt_call
14623                       && stub_entry->stub_type <= ppc_stub_plt_call_both)
14624                     info->callbacks->einfo
14625                       /* xgettext:c-format */
14626                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14627                          "(plt call stub)\n"),
14628                        input_bfd, input_section, rel->r_offset, sym_name);
14629                   else
14630                     info->callbacks->einfo
14631                       /* xgettext:c-format */
14632                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14633                          "(toc save/adjust stub)\n"),
14634                        input_bfd, input_section, rel->r_offset, sym_name);
14635
14636                   bfd_set_error (bfd_error_bad_value);
14637                   ret = FALSE;
14638                 }
14639
14640               if (can_plt_call
14641                   && stub_entry->stub_type >= ppc_stub_plt_call
14642                   && stub_entry->stub_type <= ppc_stub_plt_call_both)
14643                 unresolved_reloc = FALSE;
14644             }
14645
14646           if ((stub_entry == NULL
14647                || stub_entry->stub_type == ppc_stub_long_branch
14648                || stub_entry->stub_type == ppc_stub_plt_branch)
14649               && get_opd_info (sec) != NULL)
14650             {
14651               /* The branch destination is the value of the opd entry. */
14652               bfd_vma off = (relocation + addend
14653                              - sec->output_section->vma
14654                              - sec->output_offset);
14655               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14656               if (dest != (bfd_vma) -1)
14657                 {
14658                   relocation = dest;
14659                   addend = 0;
14660                   reloc_dest = DEST_OPD;
14661                 }
14662             }
14663
14664           /* If the branch is out of reach we ought to have a long
14665              branch stub.  */
14666           from = (rel->r_offset
14667                   + input_section->output_offset
14668                   + input_section->output_section->vma);
14669
14670           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14671                                                   ? fdh->elf.other
14672                                                   : sym->st_other);
14673
14674           if (stub_entry != NULL
14675               && (stub_entry->stub_type == ppc_stub_long_branch
14676                   || stub_entry->stub_type == ppc_stub_plt_branch)
14677               && (r_type == R_PPC64_ADDR14_BRTAKEN
14678                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14679                   || (relocation + addend - from + max_br_offset
14680                       < 2 * max_br_offset)))
14681             /* Don't use the stub if this branch is in range.  */
14682             stub_entry = NULL;
14683
14684           if (stub_entry != NULL
14685               && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14686                   || stub_entry->stub_type == ppc_stub_long_branch_both
14687                   || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14688                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14689               && (r_type != R_PPC64_REL24_NOTOC
14690                   || ((fdh ? fdh->elf.other : sym->st_other)
14691                       & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14692               && (relocation + addend - from + max_br_offset
14693                   < 2 * max_br_offset))
14694             stub_entry = NULL;
14695
14696           if (stub_entry != NULL
14697               && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14698                   || stub_entry->stub_type == ppc_stub_long_branch_both
14699                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14700                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14701               && r_type == R_PPC64_REL24_NOTOC
14702               && (relocation + addend - from + max_br_offset
14703                   < 2 * max_br_offset))
14704             stub_entry = NULL;
14705
14706           if (stub_entry != NULL)
14707             {
14708               /* Munge up the value and addend so that we call the stub
14709                  rather than the procedure directly.  */
14710               asection *stub_sec = stub_entry->group->stub_sec;
14711
14712               if (stub_entry->stub_type == ppc_stub_save_res)
14713                 relocation += (stub_sec->output_offset
14714                                + stub_sec->output_section->vma
14715                                + stub_sec->size - htab->sfpr->size
14716                                - htab->sfpr->output_offset
14717                                - htab->sfpr->output_section->vma);
14718               else
14719                 relocation = (stub_entry->stub_offset
14720                               + stub_sec->output_offset
14721                               + stub_sec->output_section->vma);
14722               addend = 0;
14723               reloc_dest = DEST_STUB;
14724
14725               if (((stub_entry->stub_type == ppc_stub_plt_call
14726                     && ALWAYS_EMIT_R2SAVE)
14727                    || stub_entry->stub_type == ppc_stub_plt_call_r2save
14728                    || stub_entry->stub_type == ppc_stub_plt_call_both)
14729                   && !(h != NULL
14730                        && (h == htab->tls_get_addr_fd
14731                            || h == htab->tls_get_addr)
14732                        && htab->params->tls_get_addr_opt)
14733                   && rel + 1 < relend
14734                   && rel[1].r_offset == rel->r_offset + 4
14735                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14736                 relocation += 4;
14737               else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14738                         || stub_entry->stub_type == ppc_stub_plt_branch_both
14739                         || stub_entry->stub_type == ppc_stub_plt_call_both)
14740                        && r_type == R_PPC64_REL24_NOTOC)
14741                 relocation += 4;
14742
14743               if (r_type == R_PPC64_REL24_NOTOC
14744                   && (stub_entry->stub_type == ppc_stub_plt_call_notoc
14745                       || stub_entry->stub_type == ppc_stub_plt_call_both))
14746                 htab->notoc_plt = 1;
14747             }
14748
14749           if (insn != 0)
14750             {
14751               if (is_isa_v2)
14752                 {
14753                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14754                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14755                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14756                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14757                     insn |= 0x02 << 21;
14758                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14759                     insn |= 0x08 << 21;
14760                   else
14761                     break;
14762                 }
14763               else
14764                 {
14765                   /* Invert 'y' bit if not the default.  */
14766                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14767                     insn ^= 0x01 << 21;
14768                 }
14769
14770               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14771             }
14772
14773           /* NOP out calls to undefined weak functions.
14774              We can thus call a weak function without first
14775              checking whether the function is defined.  */
14776           else if (h != NULL
14777                    && h->elf.root.type == bfd_link_hash_undefweak
14778                    && h->elf.dynindx == -1
14779                    && (r_type == R_PPC64_REL24
14780                        || r_type == R_PPC64_REL24_NOTOC)
14781                    && relocation == 0
14782                    && addend == 0)
14783             {
14784               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14785               goto copy_reloc;
14786             }
14787           break;
14788
14789         case R_PPC64_GOT16_DS:
14790           from = TOCstart + htab->sec_info[input_section->id].toc_off;
14791           if (relocation + addend - from + 0x8000 < 0x10000
14792               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14793             {
14794               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14795               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14796                 {
14797                   insn += (14u << 26) - (58u << 26);
14798                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14799                   r_type = R_PPC64_TOC16;
14800                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14801                 }
14802             }
14803           break;
14804
14805         case R_PPC64_GOT16_LO_DS:
14806         case R_PPC64_GOT16_HA:
14807           from = TOCstart + htab->sec_info[input_section->id].toc_off;
14808           if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14809               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14810             {
14811               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14812               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14813                 {
14814                   insn += (14u << 26) - (58u << 26);
14815                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14816                   r_type = R_PPC64_TOC16_LO;
14817                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14818                 }
14819               else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14820                 {
14821                   r_type = R_PPC64_TOC16_HA;
14822                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14823                 }
14824             }
14825           break;
14826         }
14827
14828       /* Set `addend'.  */
14829       tls_type = 0;
14830       save_unresolved_reloc = unresolved_reloc;
14831       switch (r_type)
14832         {
14833         default:
14834           /* xgettext:c-format */
14835           _bfd_error_handler (_("%pB: %s unsupported"),
14836                               input_bfd, ppc64_elf_howto_table[r_type]->name);
14837
14838           bfd_set_error (bfd_error_bad_value);
14839           ret = FALSE;
14840           goto copy_reloc;
14841
14842         case R_PPC64_NONE:
14843         case R_PPC64_TLS:
14844         case R_PPC64_TLSGD:
14845         case R_PPC64_TLSLD:
14846         case R_PPC64_TOCSAVE:
14847         case R_PPC64_GNU_VTINHERIT:
14848         case R_PPC64_GNU_VTENTRY:
14849         case R_PPC64_ENTRY:
14850           goto copy_reloc;
14851
14852           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14853              address in the GOT as relocation value instead of the
14854              symbol's value itself.  Also, create a GOT entry for the
14855              symbol and put the symbol value there.  */
14856         case R_PPC64_GOT_TLSGD16:
14857         case R_PPC64_GOT_TLSGD16_LO:
14858         case R_PPC64_GOT_TLSGD16_HI:
14859         case R_PPC64_GOT_TLSGD16_HA:
14860           tls_type = TLS_TLS | TLS_GD;
14861           goto dogot;
14862
14863         case R_PPC64_GOT_TLSLD16:
14864         case R_PPC64_GOT_TLSLD16_LO:
14865         case R_PPC64_GOT_TLSLD16_HI:
14866         case R_PPC64_GOT_TLSLD16_HA:
14867           tls_type = TLS_TLS | TLS_LD;
14868           goto dogot;
14869
14870         case R_PPC64_GOT_TPREL16_DS:
14871         case R_PPC64_GOT_TPREL16_LO_DS:
14872         case R_PPC64_GOT_TPREL16_HI:
14873         case R_PPC64_GOT_TPREL16_HA:
14874           tls_type = TLS_TLS | TLS_TPREL;
14875           goto dogot;
14876
14877         case R_PPC64_GOT_DTPREL16_DS:
14878         case R_PPC64_GOT_DTPREL16_LO_DS:
14879         case R_PPC64_GOT_DTPREL16_HI:
14880         case R_PPC64_GOT_DTPREL16_HA:
14881           tls_type = TLS_TLS | TLS_DTPREL;
14882           goto dogot;
14883
14884         case R_PPC64_GOT16:
14885         case R_PPC64_GOT16_LO:
14886         case R_PPC64_GOT16_HI:
14887         case R_PPC64_GOT16_HA:
14888         case R_PPC64_GOT16_DS:
14889         case R_PPC64_GOT16_LO_DS:
14890         case R_PPC64_GOT_PCREL34:
14891         dogot:
14892           {
14893             /* Relocation is to the entry for this symbol in the global
14894                offset table.  */
14895             asection *got;
14896             bfd_vma *offp;
14897             bfd_vma off;
14898             unsigned long indx = 0;
14899             struct got_entry *ent;
14900             bfd_vma sym_addend = orig_rel.r_addend;
14901
14902             if (r_type == R_PPC64_GOT_PCREL34)
14903               sym_addend = 0;
14904
14905             if (tls_type == (TLS_TLS | TLS_LD)
14906                 && (h == NULL
14907                     || !h->elf.def_dynamic))
14908               ent = ppc64_tlsld_got (input_bfd);
14909             else
14910               {
14911                 if (h != NULL)
14912                   {
14913                     if (!htab->elf.dynamic_sections_created
14914                         || h->elf.dynindx == -1
14915                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14916                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14917                       /* This is actually a static link, or it is a
14918                          -Bsymbolic link and the symbol is defined
14919                          locally, or the symbol was forced to be local
14920                          because of a version file.  */
14921                       ;
14922                     else
14923                       {
14924                         indx = h->elf.dynindx;
14925                         unresolved_reloc = FALSE;
14926                       }
14927                     ent = h->elf.got.glist;
14928                   }
14929                 else
14930                   {
14931                     if (local_got_ents == NULL)
14932                       abort ();
14933                     ent = local_got_ents[r_symndx];
14934                   }
14935
14936                 for (; ent != NULL; ent = ent->next)
14937                   if (ent->addend == sym_addend
14938                       && ent->owner == input_bfd
14939                       && ent->tls_type == tls_type)
14940                     break;
14941               }
14942
14943             if (ent == NULL)
14944               abort ();
14945             if (ent->is_indirect)
14946               ent = ent->got.ent;
14947             offp = &ent->got.offset;
14948             got = ppc64_elf_tdata (ent->owner)->got;
14949             if (got == NULL)
14950               abort ();
14951
14952             /* The offset must always be a multiple of 8.  We use the
14953                least significant bit to record whether we have already
14954                processed this entry.  */
14955             off = *offp;
14956             if ((off & 1) != 0)
14957               off &= ~1;
14958             else
14959               {
14960                 /* Generate relocs for the dynamic linker, except in
14961                    the case of TLSLD where we'll use one entry per
14962                    module.  */
14963                 asection *relgot;
14964                 bfd_boolean ifunc;
14965
14966                 *offp = off | 1;
14967                 relgot = NULL;
14968                 ifunc = (h != NULL
14969                          ? h->elf.type == STT_GNU_IFUNC
14970                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14971                 if (ifunc)
14972                   {
14973                     relgot = htab->elf.irelplt;
14974                     if (indx == 0)
14975                       htab->local_ifunc_resolver = 1;
14976                     else if (is_static_defined (&h->elf))
14977                       htab->maybe_local_ifunc_resolver = 1;
14978                   }
14979                 else if (indx != 0
14980                          || (bfd_link_pic (info)
14981                              && (h == NULL
14982                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14983                                  || (tls_type == (TLS_TLS | TLS_LD)
14984                                      && !h->elf.def_dynamic))
14985                              && !(tls_type == (TLS_TLS | TLS_TPREL)
14986                                   && bfd_link_executable (info)
14987                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14988                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14989                 if (relgot != NULL)
14990                   {
14991                     outrel.r_offset = (got->output_section->vma
14992                                        + got->output_offset
14993                                        + off);
14994                     outrel.r_addend = sym_addend;
14995                     if (tls_type & (TLS_LD | TLS_GD))
14996                       {
14997                         outrel.r_addend = 0;
14998                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14999                         if (tls_type == (TLS_TLS | TLS_GD))
15000                           {
15001                             loc = relgot->contents;
15002                             loc += (relgot->reloc_count++
15003                                     * sizeof (Elf64_External_Rela));
15004                             bfd_elf64_swap_reloca_out (output_bfd,
15005                                                        &outrel, loc);
15006                             outrel.r_offset += 8;
15007                             outrel.r_addend = sym_addend;
15008                             outrel.r_info
15009                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15010                           }
15011                       }
15012                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
15013                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15014                     else if (tls_type == (TLS_TLS | TLS_TPREL))
15015                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15016                     else if (indx != 0)
15017                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15018                     else
15019                       {
15020                         if (ifunc)
15021                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15022                         else
15023                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15024
15025                         /* Write the .got section contents for the sake
15026                            of prelink.  */
15027                         loc = got->contents + off;
15028                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15029                                     loc);
15030                       }
15031
15032                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15033                       {
15034                         outrel.r_addend += relocation;
15035                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15036                           {
15037                             if (htab->elf.tls_sec == NULL)
15038                               outrel.r_addend = 0;
15039                             else
15040                               outrel.r_addend -= htab->elf.tls_sec->vma;
15041                           }
15042                       }
15043                     loc = relgot->contents;
15044                     loc += (relgot->reloc_count++
15045                             * sizeof (Elf64_External_Rela));
15046                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15047                   }
15048
15049                 /* Init the .got section contents here if we're not
15050                    emitting a reloc.  */
15051                 else
15052                   {
15053                     relocation += sym_addend;
15054                     if (tls_type != 0)
15055                       {
15056                         if (htab->elf.tls_sec == NULL)
15057                           relocation = 0;
15058                         else
15059                           {
15060                             if (tls_type & TLS_LD)
15061                               relocation = 0;
15062                             else
15063                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15064                             if (tls_type & TLS_TPREL)
15065                               relocation += DTP_OFFSET - TP_OFFSET;
15066                           }
15067
15068                         if (tls_type & (TLS_GD | TLS_LD))
15069                           {
15070                             bfd_put_64 (output_bfd, relocation,
15071                                         got->contents + off + 8);
15072                             relocation = 1;
15073                           }
15074                       }
15075                     bfd_put_64 (output_bfd, relocation,
15076                                 got->contents + off);
15077                   }
15078               }
15079
15080             if (off >= (bfd_vma) -2)
15081               abort ();
15082
15083             relocation = got->output_section->vma + got->output_offset + off;
15084             if (r_type != R_PPC64_GOT_PCREL34)
15085               addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15086           }
15087           break;
15088
15089         case R_PPC64_PLT16_HA:
15090         case R_PPC64_PLT16_HI:
15091         case R_PPC64_PLT16_LO:
15092         case R_PPC64_PLT16_LO_DS:
15093         case R_PPC64_PLT_PCREL34:
15094         case R_PPC64_PLT_PCREL34_NOTOC:
15095         case R_PPC64_PLT32:
15096         case R_PPC64_PLT64:
15097         case R_PPC64_PLTSEQ:
15098         case R_PPC64_PLTSEQ_NOTOC:
15099         case R_PPC64_PLTCALL:
15100         case R_PPC64_PLTCALL_NOTOC:
15101           /* Relocation is to the entry for this symbol in the
15102              procedure linkage table.  */
15103           unresolved_reloc = TRUE;
15104           {
15105             struct plt_entry **plt_list = NULL;
15106             if (h != NULL)
15107               plt_list = &h->elf.plt.plist;
15108             else if (local_got_ents != NULL)
15109               {
15110                 struct plt_entry **local_plt = (struct plt_entry **)
15111                   (local_got_ents + symtab_hdr->sh_info);
15112                 plt_list = local_plt + r_symndx;
15113               }
15114             if (plt_list)
15115               {
15116                 struct plt_entry *ent;
15117                 bfd_vma sym_addend = orig_rel.r_addend;
15118
15119                 if (r_type == R_PPC64_PLT_PCREL34
15120                     || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15121                   sym_addend = 0;
15122
15123                 for (ent = *plt_list; ent != NULL; ent = ent->next)
15124                   if (ent->plt.offset != (bfd_vma) -1
15125                       && ent->addend == sym_addend)
15126                     {
15127                       asection *plt;
15128                       bfd_vma got;
15129
15130                       plt = htab->elf.splt;
15131                       if (!htab->elf.dynamic_sections_created
15132                           || h == NULL
15133                           || h->elf.dynindx == -1)
15134                         {
15135                           if (h != NULL
15136                               ? h->elf.type == STT_GNU_IFUNC
15137                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15138                             plt = htab->elf.iplt;
15139                           else
15140                             plt = htab->pltlocal;
15141                         }
15142                       relocation = (plt->output_section->vma
15143                                     + plt->output_offset
15144                                     + ent->plt.offset);
15145                       if (r_type == R_PPC64_PLT16_HA
15146                           || r_type == R_PPC64_PLT16_HI
15147                           || r_type == R_PPC64_PLT16_LO
15148                           || r_type == R_PPC64_PLT16_LO_DS)
15149                         {
15150                           got = (elf_gp (output_bfd)
15151                                  + htab->sec_info[input_section->id].toc_off);
15152                           relocation -= got;
15153                         }
15154                       if (r_type != R_PPC64_PLT_PCREL34
15155                           && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15156                         addend = 0;
15157                       unresolved_reloc = FALSE;
15158                       break;
15159                     }
15160               }
15161           }
15162           break;
15163
15164         case R_PPC64_TOC:
15165           /* Relocation value is TOC base.  */
15166           relocation = TOCstart;
15167           if (r_symndx == STN_UNDEF)
15168             relocation += htab->sec_info[input_section->id].toc_off;
15169           else if (unresolved_reloc)
15170             ;
15171           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15172             relocation += htab->sec_info[sec->id].toc_off;
15173           else
15174             unresolved_reloc = TRUE;
15175           goto dodyn;
15176
15177           /* TOC16 relocs.  We want the offset relative to the TOC base,
15178              which is the address of the start of the TOC plus 0x8000.
15179              The TOC consists of sections .got, .toc, .tocbss, and .plt,
15180              in this order.  */
15181         case R_PPC64_TOC16:
15182         case R_PPC64_TOC16_LO:
15183         case R_PPC64_TOC16_HI:
15184         case R_PPC64_TOC16_DS:
15185         case R_PPC64_TOC16_LO_DS:
15186         case R_PPC64_TOC16_HA:
15187           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15188           break;
15189
15190           /* Relocate against the beginning of the section.  */
15191         case R_PPC64_SECTOFF:
15192         case R_PPC64_SECTOFF_LO:
15193         case R_PPC64_SECTOFF_HI:
15194         case R_PPC64_SECTOFF_DS:
15195         case R_PPC64_SECTOFF_LO_DS:
15196         case R_PPC64_SECTOFF_HA:
15197           if (sec != NULL)
15198             addend -= sec->output_section->vma;
15199           break;
15200
15201         case R_PPC64_REL16:
15202         case R_PPC64_REL16_LO:
15203         case R_PPC64_REL16_HI:
15204         case R_PPC64_REL16_HA:
15205         case R_PPC64_REL16_HIGH:
15206         case R_PPC64_REL16_HIGHA:
15207         case R_PPC64_REL16_HIGHER:
15208         case R_PPC64_REL16_HIGHERA:
15209         case R_PPC64_REL16_HIGHEST:
15210         case R_PPC64_REL16_HIGHESTA:
15211         case R_PPC64_REL16_HIGHER34:
15212         case R_PPC64_REL16_HIGHERA34:
15213         case R_PPC64_REL16_HIGHEST34:
15214         case R_PPC64_REL16_HIGHESTA34:
15215         case R_PPC64_REL16DX_HA:
15216         case R_PPC64_REL14:
15217         case R_PPC64_REL14_BRNTAKEN:
15218         case R_PPC64_REL14_BRTAKEN:
15219         case R_PPC64_REL24:
15220         case R_PPC64_REL24_NOTOC:
15221         case R_PPC64_PCREL34:
15222         case R_PPC64_PCREL28:
15223           break;
15224
15225         case R_PPC64_TPREL16:
15226         case R_PPC64_TPREL16_LO:
15227         case R_PPC64_TPREL16_HI:
15228         case R_PPC64_TPREL16_HA:
15229         case R_PPC64_TPREL16_DS:
15230         case R_PPC64_TPREL16_LO_DS:
15231         case R_PPC64_TPREL16_HIGH:
15232         case R_PPC64_TPREL16_HIGHA:
15233         case R_PPC64_TPREL16_HIGHER:
15234         case R_PPC64_TPREL16_HIGHERA:
15235         case R_PPC64_TPREL16_HIGHEST:
15236         case R_PPC64_TPREL16_HIGHESTA:
15237           if (h != NULL
15238               && h->elf.root.type == bfd_link_hash_undefweak
15239               && h->elf.dynindx == -1)
15240             {
15241               /* Make this relocation against an undefined weak symbol
15242                  resolve to zero.  This is really just a tweak, since
15243                  code using weak externs ought to check that they are
15244                  defined before using them.  */
15245               bfd_byte *p = contents + rel->r_offset - d_offset;
15246
15247               insn = bfd_get_32 (input_bfd, p);
15248               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15249               if (insn != 0)
15250                 bfd_put_32 (input_bfd, insn, p);
15251               break;
15252             }
15253           if (htab->elf.tls_sec != NULL)
15254             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15255           /* The TPREL16 relocs shouldn't really be used in shared
15256              libs or with non-local symbols as that will result in
15257              DT_TEXTREL being set, but support them anyway.  */
15258           goto dodyn;
15259
15260         case R_PPC64_DTPREL16:
15261         case R_PPC64_DTPREL16_LO:
15262         case R_PPC64_DTPREL16_HI:
15263         case R_PPC64_DTPREL16_HA:
15264         case R_PPC64_DTPREL16_DS:
15265         case R_PPC64_DTPREL16_LO_DS:
15266         case R_PPC64_DTPREL16_HIGH:
15267         case R_PPC64_DTPREL16_HIGHA:
15268         case R_PPC64_DTPREL16_HIGHER:
15269         case R_PPC64_DTPREL16_HIGHERA:
15270         case R_PPC64_DTPREL16_HIGHEST:
15271         case R_PPC64_DTPREL16_HIGHESTA:
15272           if (htab->elf.tls_sec != NULL)
15273             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15274           break;
15275
15276         case R_PPC64_ADDR64_LOCAL:
15277           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15278                                               ? h->elf.other
15279                                               : sym->st_other);
15280           break;
15281
15282         case R_PPC64_DTPMOD64:
15283           relocation = 1;
15284           addend = 0;
15285           goto dodyn;
15286
15287         case R_PPC64_TPREL64:
15288           if (htab->elf.tls_sec != NULL)
15289             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15290           goto dodyn;
15291
15292         case R_PPC64_DTPREL64:
15293           if (htab->elf.tls_sec != NULL)
15294             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15295           /* Fall through.  */
15296
15297           /* Relocations that may need to be propagated if this is a
15298              dynamic object.  */
15299         case R_PPC64_REL30:
15300         case R_PPC64_REL32:
15301         case R_PPC64_REL64:
15302         case R_PPC64_ADDR14:
15303         case R_PPC64_ADDR14_BRNTAKEN:
15304         case R_PPC64_ADDR14_BRTAKEN:
15305         case R_PPC64_ADDR16:
15306         case R_PPC64_ADDR16_DS:
15307         case R_PPC64_ADDR16_HA:
15308         case R_PPC64_ADDR16_HI:
15309         case R_PPC64_ADDR16_HIGH:
15310         case R_PPC64_ADDR16_HIGHA:
15311         case R_PPC64_ADDR16_HIGHER:
15312         case R_PPC64_ADDR16_HIGHERA:
15313         case R_PPC64_ADDR16_HIGHEST:
15314         case R_PPC64_ADDR16_HIGHESTA:
15315         case R_PPC64_ADDR16_LO:
15316         case R_PPC64_ADDR16_LO_DS:
15317         case R_PPC64_ADDR16_HIGHER34:
15318         case R_PPC64_ADDR16_HIGHERA34:
15319         case R_PPC64_ADDR16_HIGHEST34:
15320         case R_PPC64_ADDR16_HIGHESTA34:
15321         case R_PPC64_ADDR24:
15322         case R_PPC64_ADDR32:
15323         case R_PPC64_ADDR64:
15324         case R_PPC64_UADDR16:
15325         case R_PPC64_UADDR32:
15326         case R_PPC64_UADDR64:
15327         case R_PPC64_D34:
15328         case R_PPC64_D34_LO:
15329         case R_PPC64_D34_HI30:
15330         case R_PPC64_D34_HA30:
15331         case R_PPC64_D28:
15332         dodyn:
15333           if ((input_section->flags & SEC_ALLOC) == 0)
15334             break;
15335
15336           if (NO_OPD_RELOCS && is_opd)
15337             break;
15338
15339           if (bfd_link_pic (info)
15340               ? ((h == NULL
15341                   || h->dyn_relocs != NULL)
15342                  && ((h != NULL && pc_dynrelocs (h))
15343                      || must_be_dyn_reloc (info, r_type)))
15344               : (h != NULL
15345                  ? h->dyn_relocs != NULL
15346                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15347             {
15348               bfd_boolean skip, relocate;
15349               asection *sreloc;
15350               bfd_vma out_off;
15351               long indx = 0;
15352
15353               /* When generating a dynamic object, these relocations
15354                  are copied into the output file to be resolved at run
15355                  time.  */
15356
15357               skip = FALSE;
15358               relocate = FALSE;
15359
15360               out_off = _bfd_elf_section_offset (output_bfd, info,
15361                                                  input_section, rel->r_offset);
15362               if (out_off == (bfd_vma) -1)
15363                 skip = TRUE;
15364               else if (out_off == (bfd_vma) -2)
15365                 skip = TRUE, relocate = TRUE;
15366               out_off += (input_section->output_section->vma
15367                           + input_section->output_offset);
15368               outrel.r_offset = out_off;
15369               outrel.r_addend = rel->r_addend;
15370
15371               /* Optimize unaligned reloc use.  */
15372               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15373                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15374                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15375               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15376                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15377                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15378               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15379                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15380                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15381
15382               if (skip)
15383                 memset (&outrel, 0, sizeof outrel);
15384               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15385                        && !is_opd
15386                        && r_type != R_PPC64_TOC)
15387                 {
15388                   indx = h->elf.dynindx;
15389                   BFD_ASSERT (indx != -1);
15390                   outrel.r_info = ELF64_R_INFO (indx, r_type);
15391                 }
15392               else
15393                 {
15394                   /* This symbol is local, or marked to become local,
15395                      or this is an opd section reloc which must point
15396                      at a local function.  */
15397                   outrel.r_addend += relocation;
15398                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15399                     {
15400                       if (is_opd && h != NULL)
15401                         {
15402                           /* Lie about opd entries.  This case occurs
15403                              when building shared libraries and we
15404                              reference a function in another shared
15405                              lib.  The same thing happens for a weak
15406                              definition in an application that's
15407                              overridden by a strong definition in a
15408                              shared lib.  (I believe this is a generic
15409                              bug in binutils handling of weak syms.)
15410                              In these cases we won't use the opd
15411                              entry in this lib.  */
15412                           unresolved_reloc = FALSE;
15413                         }
15414                       if (!is_opd
15415                           && r_type == R_PPC64_ADDR64
15416                           && (h != NULL
15417                               ? h->elf.type == STT_GNU_IFUNC
15418                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15419                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15420                       else
15421                         {
15422                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15423
15424                           /* We need to relocate .opd contents for ld.so.
15425                              Prelink also wants simple and consistent rules
15426                              for relocs.  This make all RELATIVE relocs have
15427                              *r_offset equal to r_addend.  */
15428                           relocate = TRUE;
15429                         }
15430                     }
15431                   else
15432                     {
15433                       if (h != NULL
15434                           ? h->elf.type == STT_GNU_IFUNC
15435                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15436                         {
15437                           info->callbacks->einfo
15438                             /* xgettext:c-format */
15439                             (_("%H: %s for indirect "
15440                                "function `%pT' unsupported\n"),
15441                              input_bfd, input_section, rel->r_offset,
15442                              ppc64_elf_howto_table[r_type]->name,
15443                              sym_name);
15444                           ret = FALSE;
15445                         }
15446                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15447                         ;
15448                       else if (sec == NULL || sec->owner == NULL)
15449                         {
15450                           bfd_set_error (bfd_error_bad_value);
15451                           return FALSE;
15452                         }
15453                       else
15454                         {
15455                           asection *osec;
15456
15457                           osec = sec->output_section;
15458                           indx = elf_section_data (osec)->dynindx;
15459
15460                           if (indx == 0)
15461                             {
15462                               if ((osec->flags & SEC_READONLY) == 0
15463                                   && htab->elf.data_index_section != NULL)
15464                                 osec = htab->elf.data_index_section;
15465                               else
15466                                 osec = htab->elf.text_index_section;
15467                               indx = elf_section_data (osec)->dynindx;
15468                             }
15469                           BFD_ASSERT (indx != 0);
15470
15471                           /* We are turning this relocation into one
15472                              against a section symbol, so subtract out
15473                              the output section's address but not the
15474                              offset of the input section in the output
15475                              section.  */
15476                           outrel.r_addend -= osec->vma;
15477                         }
15478
15479                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15480                     }
15481                 }
15482
15483               sreloc = elf_section_data (input_section)->sreloc;
15484               if (h != NULL
15485                   ? h->elf.type == STT_GNU_IFUNC
15486                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15487                 {
15488                   sreloc = htab->elf.irelplt;
15489                   if (indx == 0)
15490                     htab->local_ifunc_resolver = 1;
15491                   else if (is_static_defined (&h->elf))
15492                     htab->maybe_local_ifunc_resolver = 1;
15493                 }
15494               if (sreloc == NULL)
15495                 abort ();
15496
15497               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15498                   >= sreloc->size)
15499                 abort ();
15500               loc = sreloc->contents;
15501               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15502               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15503
15504               /* If this reloc is against an external symbol, it will
15505                  be computed at runtime, so there's no need to do
15506                  anything now.  However, for the sake of prelink ensure
15507                  that the section contents are a known value.  */
15508               if (!relocate)
15509                 {
15510                   unresolved_reloc = FALSE;
15511                   /* The value chosen here is quite arbitrary as ld.so
15512                      ignores section contents except for the special
15513                      case of .opd where the contents might be accessed
15514                      before relocation.  Choose zero, as that won't
15515                      cause reloc overflow.  */
15516                   relocation = 0;
15517                   addend = 0;
15518                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15519                      to improve backward compatibility with older
15520                      versions of ld.  */
15521                   if (r_type == R_PPC64_ADDR64)
15522                     addend = outrel.r_addend;
15523                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15524                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15525                     addend = outrel.r_offset;
15526                 }
15527             }
15528           break;
15529
15530         case R_PPC64_COPY:
15531         case R_PPC64_GLOB_DAT:
15532         case R_PPC64_JMP_SLOT:
15533         case R_PPC64_JMP_IREL:
15534         case R_PPC64_RELATIVE:
15535           /* We shouldn't ever see these dynamic relocs in relocatable
15536              files.  */
15537           /* Fall through.  */
15538
15539         case R_PPC64_PLTGOT16:
15540         case R_PPC64_PLTGOT16_DS:
15541         case R_PPC64_PLTGOT16_HA:
15542         case R_PPC64_PLTGOT16_HI:
15543         case R_PPC64_PLTGOT16_LO:
15544         case R_PPC64_PLTGOT16_LO_DS:
15545         case R_PPC64_PLTREL32:
15546         case R_PPC64_PLTREL64:
15547           /* These ones haven't been implemented yet.  */
15548
15549           info->callbacks->einfo
15550             /* xgettext:c-format */
15551             (_("%P: %pB: %s is not supported for `%pT'\n"),
15552              input_bfd,
15553              ppc64_elf_howto_table[r_type]->name, sym_name);
15554
15555           bfd_set_error (bfd_error_invalid_operation);
15556           ret = FALSE;
15557           goto copy_reloc;
15558         }
15559
15560       /* Multi-instruction sequences that access the TOC can be
15561          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15562          to             nop;           addi rb,r2,x;  */
15563       switch (r_type)
15564         {
15565         default:
15566           break;
15567
15568         case R_PPC64_GOT_TLSLD16_HI:
15569         case R_PPC64_GOT_TLSGD16_HI:
15570         case R_PPC64_GOT_TPREL16_HI:
15571         case R_PPC64_GOT_DTPREL16_HI:
15572         case R_PPC64_GOT16_HI:
15573         case R_PPC64_TOC16_HI:
15574           /* These relocs would only be useful if building up an
15575              offset to later add to r2, perhaps in an indexed
15576              addressing mode instruction.  Don't try to optimize.
15577              Unfortunately, the possibility of someone building up an
15578              offset like this or even with the HA relocs, means that
15579              we need to check the high insn when optimizing the low
15580              insn.  */
15581           break;
15582
15583         case R_PPC64_PLTCALL_NOTOC:
15584           if (!unresolved_reloc)
15585             htab->notoc_plt = 1;
15586           /* Fall through.  */
15587         case R_PPC64_PLTCALL:
15588           if (unresolved_reloc)
15589             {
15590               /* No plt entry.  Make this into a direct call.  */
15591               bfd_byte *p = contents + rel->r_offset;
15592               insn = bfd_get_32 (input_bfd, p);
15593               insn &= 1;
15594               bfd_put_32 (input_bfd, B_DOT | insn, p);
15595               if (r_type == R_PPC64_PLTCALL)
15596                 bfd_put_32 (input_bfd, NOP, p + 4);
15597               unresolved_reloc = save_unresolved_reloc;
15598               r_type = R_PPC64_REL24;
15599             }
15600           break;
15601
15602         case R_PPC64_PLTSEQ_NOTOC:
15603         case R_PPC64_PLTSEQ:
15604           if (unresolved_reloc)
15605             {
15606               unresolved_reloc = FALSE;
15607               goto nop_it;
15608             }
15609           break;
15610
15611         case R_PPC64_PLT_PCREL34_NOTOC:
15612           if (!unresolved_reloc)
15613             htab->notoc_plt = 1;
15614           /* Fall through.  */
15615         case R_PPC64_PLT_PCREL34:
15616           if (unresolved_reloc)
15617             {
15618               bfd_byte *p = contents + rel->r_offset;
15619               bfd_put_32 (input_bfd, PNOP >> 32, p);
15620               bfd_put_32 (input_bfd, PNOP, p + 4);
15621               unresolved_reloc = FALSE;
15622               goto copy_reloc;
15623             }
15624           break;
15625
15626         case R_PPC64_PLT16_HA:
15627           if (unresolved_reloc)
15628             {
15629               unresolved_reloc = FALSE;
15630               goto nop_it;
15631             }
15632           /* Fall through.  */
15633         case R_PPC64_GOT_TLSLD16_HA:
15634         case R_PPC64_GOT_TLSGD16_HA:
15635         case R_PPC64_GOT_TPREL16_HA:
15636         case R_PPC64_GOT_DTPREL16_HA:
15637         case R_PPC64_GOT16_HA:
15638         case R_PPC64_TOC16_HA:
15639           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15640               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15641             {
15642               bfd_byte *p;
15643             nop_it:
15644               p = contents + (rel->r_offset & ~3);
15645               bfd_put_32 (input_bfd, NOP, p);
15646               goto copy_reloc;
15647             }
15648           break;
15649
15650         case R_PPC64_PLT16_LO:
15651         case R_PPC64_PLT16_LO_DS:
15652           if (unresolved_reloc)
15653             {
15654               unresolved_reloc = FALSE;
15655               goto nop_it;
15656             }
15657           /* Fall through.  */
15658         case R_PPC64_GOT_TLSLD16_LO:
15659         case R_PPC64_GOT_TLSGD16_LO:
15660         case R_PPC64_GOT_TPREL16_LO_DS:
15661         case R_PPC64_GOT_DTPREL16_LO_DS:
15662         case R_PPC64_GOT16_LO:
15663         case R_PPC64_GOT16_LO_DS:
15664         case R_PPC64_TOC16_LO:
15665         case R_PPC64_TOC16_LO_DS:
15666           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15667               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15668             {
15669               bfd_byte *p = contents + (rel->r_offset & ~3);
15670               insn = bfd_get_32 (input_bfd, p);
15671               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15672                 {
15673                   /* Transform addic to addi when we change reg.  */
15674                   insn &= ~((0x3f << 26) | (0x1f << 16));
15675                   insn |= (14u << 26) | (2 << 16);
15676                 }
15677               else
15678                 {
15679                   insn &= ~(0x1f << 16);
15680                   insn |= 2 << 16;
15681                 }
15682               bfd_put_32 (input_bfd, insn, p);
15683             }
15684           break;
15685
15686         case R_PPC64_TPREL16_HA:
15687           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15688             {
15689               bfd_byte *p = contents + (rel->r_offset & ~3);
15690               insn = bfd_get_32 (input_bfd, p);
15691               if ((insn & ((0x3f << 26) | 0x1f << 16))
15692                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15693                 /* xgettext:c-format */
15694                 info->callbacks->minfo
15695                   (_("%H: warning: %s unexpected insn %#x.\n"),
15696                    input_bfd, input_section, rel->r_offset,
15697                    ppc64_elf_howto_table[r_type]->name, insn);
15698               else
15699                 {
15700                   bfd_put_32 (input_bfd, NOP, p);
15701                   goto copy_reloc;
15702                 }
15703             }
15704           break;
15705
15706         case R_PPC64_TPREL16_LO:
15707         case R_PPC64_TPREL16_LO_DS:
15708           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15709             {
15710               bfd_byte *p = contents + (rel->r_offset & ~3);
15711               insn = bfd_get_32 (input_bfd, p);
15712               insn &= ~(0x1f << 16);
15713               insn |= 13 << 16;
15714               bfd_put_32 (input_bfd, insn, p);
15715             }
15716           break;
15717         }
15718
15719       /* Do any further special processing.  */
15720       switch (r_type)
15721         {
15722         default:
15723           break;
15724
15725         case R_PPC64_REL16_HA:
15726         case R_PPC64_REL16_HIGHA:
15727         case R_PPC64_REL16_HIGHERA:
15728         case R_PPC64_REL16_HIGHESTA:
15729         case R_PPC64_REL16DX_HA:
15730         case R_PPC64_ADDR16_HA:
15731         case R_PPC64_ADDR16_HIGHA:
15732         case R_PPC64_ADDR16_HIGHERA:
15733         case R_PPC64_ADDR16_HIGHESTA:
15734         case R_PPC64_TOC16_HA:
15735         case R_PPC64_SECTOFF_HA:
15736         case R_PPC64_TPREL16_HA:
15737         case R_PPC64_TPREL16_HIGHA:
15738         case R_PPC64_TPREL16_HIGHERA:
15739         case R_PPC64_TPREL16_HIGHESTA:
15740         case R_PPC64_DTPREL16_HA:
15741         case R_PPC64_DTPREL16_HIGHA:
15742         case R_PPC64_DTPREL16_HIGHERA:
15743         case R_PPC64_DTPREL16_HIGHESTA:
15744           /* It's just possible that this symbol is a weak symbol
15745              that's not actually defined anywhere. In that case,
15746              'sec' would be NULL, and we should leave the symbol
15747              alone (it will be set to zero elsewhere in the link).  */
15748           if (sec == NULL)
15749             break;
15750           /* Fall through.  */
15751
15752         case R_PPC64_GOT16_HA:
15753         case R_PPC64_PLTGOT16_HA:
15754         case R_PPC64_PLT16_HA:
15755         case R_PPC64_GOT_TLSGD16_HA:
15756         case R_PPC64_GOT_TLSLD16_HA:
15757         case R_PPC64_GOT_TPREL16_HA:
15758         case R_PPC64_GOT_DTPREL16_HA:
15759           /* Add 0x10000 if sign bit in 0:15 is set.
15760              Bits 0:15 are not used.  */
15761           addend += 0x8000;
15762           break;
15763
15764         case R_PPC64_D34_HA30:
15765         case R_PPC64_ADDR16_HIGHERA34:
15766         case R_PPC64_ADDR16_HIGHESTA34:
15767         case R_PPC64_REL16_HIGHERA34:
15768         case R_PPC64_REL16_HIGHESTA34:
15769           if (sec != NULL)
15770             addend += 1ULL << 33;
15771           break;
15772
15773         case R_PPC64_ADDR16_DS:
15774         case R_PPC64_ADDR16_LO_DS:
15775         case R_PPC64_GOT16_DS:
15776         case R_PPC64_GOT16_LO_DS:
15777         case R_PPC64_PLT16_LO_DS:
15778         case R_PPC64_SECTOFF_DS:
15779         case R_PPC64_SECTOFF_LO_DS:
15780         case R_PPC64_TOC16_DS:
15781         case R_PPC64_TOC16_LO_DS:
15782         case R_PPC64_PLTGOT16_DS:
15783         case R_PPC64_PLTGOT16_LO_DS:
15784         case R_PPC64_GOT_TPREL16_DS:
15785         case R_PPC64_GOT_TPREL16_LO_DS:
15786         case R_PPC64_GOT_DTPREL16_DS:
15787         case R_PPC64_GOT_DTPREL16_LO_DS:
15788         case R_PPC64_TPREL16_DS:
15789         case R_PPC64_TPREL16_LO_DS:
15790         case R_PPC64_DTPREL16_DS:
15791         case R_PPC64_DTPREL16_LO_DS:
15792           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15793           mask = 3;
15794           /* If this reloc is against an lq, lxv, or stxv insn, then
15795              the value must be a multiple of 16.  This is somewhat of
15796              a hack, but the "correct" way to do this by defining _DQ
15797              forms of all the _DS relocs bloats all reloc switches in
15798              this file.  It doesn't make much sense to use these
15799              relocs in data, so testing the insn should be safe.  */
15800           if ((insn & (0x3f << 26)) == (56u << 26)
15801               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15802             mask = 15;
15803           relocation += addend;
15804           addend = insn & (mask ^ 3);
15805           if ((relocation & mask) != 0)
15806             {
15807               relocation ^= relocation & mask;
15808               info->callbacks->einfo
15809                 /* xgettext:c-format */
15810                 (_("%H: error: %s not a multiple of %u\n"),
15811                  input_bfd, input_section, rel->r_offset,
15812                  ppc64_elf_howto_table[r_type]->name,
15813                  mask + 1);
15814               bfd_set_error (bfd_error_bad_value);
15815               ret = FALSE;
15816               goto copy_reloc;
15817             }
15818           break;
15819         }
15820
15821       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15822          because such sections are not SEC_ALLOC and thus ld.so will
15823          not process them.  */
15824       howto = ppc64_elf_howto_table[(int) r_type];
15825       if (unresolved_reloc
15826           && !((input_section->flags & SEC_DEBUGGING) != 0
15827                && h->elf.def_dynamic)
15828           && _bfd_elf_section_offset (output_bfd, info, input_section,
15829                                       rel->r_offset) != (bfd_vma) -1)
15830         {
15831           info->callbacks->einfo
15832             /* xgettext:c-format */
15833             (_("%H: unresolvable %s against `%pT'\n"),
15834              input_bfd, input_section, rel->r_offset,
15835              howto->name,
15836              h->elf.root.root.string);
15837           ret = FALSE;
15838         }
15839
15840       /* 16-bit fields in insns mostly have signed values, but a
15841          few insns have 16-bit unsigned values.  Really, we should
15842          have different reloc types.  */
15843       if (howto->complain_on_overflow != complain_overflow_dont
15844           && howto->dst_mask == 0xffff
15845           && (input_section->flags & SEC_CODE) != 0)
15846         {
15847           enum complain_overflow complain = complain_overflow_signed;
15848
15849           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15850           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15851             complain = complain_overflow_bitfield;
15852           else if (howto->rightshift == 0
15853                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15854                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15855                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15856                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15857                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15858                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15859             complain = complain_overflow_unsigned;
15860           if (howto->complain_on_overflow != complain)
15861             {
15862               alt_howto = *howto;
15863               alt_howto.complain_on_overflow = complain;
15864               howto = &alt_howto;
15865             }
15866         }
15867
15868       switch (r_type)
15869         {
15870           /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
15871         case R_PPC64_D34:
15872         case R_PPC64_D34_LO:
15873         case R_PPC64_D34_HI30:
15874         case R_PPC64_D34_HA30:
15875         case R_PPC64_PCREL34:
15876         case R_PPC64_GOT_PCREL34:
15877         case R_PPC64_PLT_PCREL34:
15878         case R_PPC64_PLT_PCREL34_NOTOC:
15879         case R_PPC64_D28:
15880         case R_PPC64_PCREL28:
15881           if (rel->r_offset + 8 > input_section->size)
15882             r = bfd_reloc_outofrange;
15883           else
15884             {
15885               uint64_t pinsn;
15886
15887               relocation += addend;
15888               if (howto->pc_relative)
15889                 relocation -= (rel->r_offset
15890                                + input_section->output_offset
15891                                + input_section->output_section->vma);
15892               relocation >>= howto->rightshift;
15893
15894               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15895               pinsn <<= 32;
15896               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15897
15898               pinsn &= ~howto->dst_mask;
15899               pinsn |= (((relocation << 16) | (relocation & 0xffff))
15900                         & howto->dst_mask);
15901               bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
15902               bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
15903               r = bfd_reloc_ok;
15904               if (howto->complain_on_overflow == complain_overflow_signed
15905                   && (relocation + (1ULL << (howto->bitsize - 1))
15906                       >= 1ULL << howto->bitsize))
15907                 r = bfd_reloc_overflow;
15908             }
15909           break;
15910
15911         case R_PPC64_REL16DX_HA:
15912           if (rel->r_offset + 4 > input_section->size)
15913             r = bfd_reloc_outofrange;
15914           else
15915             {
15916               relocation += addend;
15917               relocation -= (rel->r_offset
15918                              + input_section->output_offset
15919                              + input_section->output_section->vma);
15920               relocation = (bfd_signed_vma) relocation >> 16;
15921               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15922               insn &= ~0x1fffc1;
15923               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15924               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15925               r = bfd_reloc_ok;
15926               if (relocation + 0x8000 > 0xffff)
15927                 r = bfd_reloc_overflow;
15928             }
15929           break;
15930
15931         default:
15932           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
15933                                         contents, rel->r_offset,
15934                                         relocation, addend);
15935         }
15936
15937       if (r != bfd_reloc_ok)
15938         {
15939           char *more_info = NULL;
15940           const char *reloc_name = howto->name;
15941
15942           if (reloc_dest != DEST_NORMAL)
15943             {
15944               more_info = bfd_malloc (strlen (reloc_name) + 8);
15945               if (more_info != NULL)
15946                 {
15947                   strcpy (more_info, reloc_name);
15948                   strcat (more_info, (reloc_dest == DEST_OPD
15949                                       ? " (OPD)" : " (stub)"));
15950                   reloc_name = more_info;
15951                 }
15952             }
15953
15954           if (r == bfd_reloc_overflow)
15955             {
15956               /* On code like "if (foo) foo();" don't report overflow
15957                  on a branch to zero when foo is undefined.  */
15958               if (!warned
15959                   && (reloc_dest == DEST_STUB
15960                       || !(h != NULL
15961                            && (h->elf.root.type == bfd_link_hash_undefweak
15962                                || h->elf.root.type == bfd_link_hash_undefined)
15963                            && is_branch_reloc (r_type))))
15964                 info->callbacks->reloc_overflow (info, &h->elf.root,
15965                                                  sym_name, reloc_name,
15966                                                  orig_rel.r_addend,
15967                                                  input_bfd, input_section,
15968                                                  rel->r_offset);
15969             }
15970           else
15971             {
15972               info->callbacks->einfo
15973                 /* xgettext:c-format */
15974                 (_("%H: %s against `%pT': error %d\n"),
15975                  input_bfd, input_section, rel->r_offset,
15976                  reloc_name, sym_name, (int) r);
15977               ret = FALSE;
15978             }
15979           if (more_info != NULL)
15980             free (more_info);
15981         }
15982     copy_reloc:
15983       if (wrel != rel)
15984         *wrel = *rel;
15985     }
15986
15987   if (wrel != rel)
15988     {
15989       Elf_Internal_Shdr *rel_hdr;
15990       size_t deleted = rel - wrel;
15991
15992       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15993       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15994       if (rel_hdr->sh_size == 0)
15995         {
15996           /* It is too late to remove an empty reloc section.  Leave
15997              one NONE reloc.
15998              ??? What is wrong with an empty section???  */
15999           rel_hdr->sh_size = rel_hdr->sh_entsize;
16000           deleted -= 1;
16001         }
16002       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16003       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16004       input_section->reloc_count -= deleted;
16005     }
16006
16007   /* If we're emitting relocations, then shortly after this function
16008      returns, reloc offsets and addends for this section will be
16009      adjusted.  Worse, reloc symbol indices will be for the output
16010      file rather than the input.  Save a copy of the relocs for
16011      opd_entry_value.  */
16012   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16013     {
16014       bfd_size_type amt;
16015       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16016       rel = bfd_alloc (input_bfd, amt);
16017       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16018       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16019       if (rel == NULL)
16020         return FALSE;
16021       memcpy (rel, relocs, amt);
16022     }
16023   return ret;
16024 }
16025
16026 /* Adjust the value of any local symbols in opd sections.  */
16027
16028 static int
16029 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16030                               const char *name ATTRIBUTE_UNUSED,
16031                               Elf_Internal_Sym *elfsym,
16032                               asection *input_sec,
16033                               struct elf_link_hash_entry *h)
16034 {
16035   struct _opd_sec_data *opd;
16036   long adjust;
16037   bfd_vma value;
16038
16039   if (h != NULL)
16040     return 1;
16041
16042   opd = get_opd_info (input_sec);
16043   if (opd == NULL || opd->adjust == NULL)
16044     return 1;
16045
16046   value = elfsym->st_value - input_sec->output_offset;
16047   if (!bfd_link_relocatable (info))
16048     value -= input_sec->output_section->vma;
16049
16050   adjust = opd->adjust[OPD_NDX (value)];
16051   if (adjust == -1)
16052     return 2;
16053
16054   elfsym->st_value += adjust;
16055   return 1;
16056 }
16057
16058 /* Finish up dynamic symbol handling.  We set the contents of various
16059    dynamic sections here.  */
16060
16061 static bfd_boolean
16062 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16063                                  struct bfd_link_info *info,
16064                                  struct elf_link_hash_entry *h,
16065                                  Elf_Internal_Sym *sym)
16066 {
16067   struct ppc_link_hash_table *htab;
16068   struct plt_entry *ent;
16069
16070   htab = ppc_hash_table (info);
16071   if (htab == NULL)
16072     return FALSE;
16073
16074   if (!htab->opd_abi && !h->def_regular)
16075     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16076       if (ent->plt.offset != (bfd_vma) -1)
16077         {
16078           /* Mark the symbol as undefined, rather than as
16079              defined in glink.  Leave the value if there were
16080              any relocations where pointer equality matters
16081              (this is a clue for the dynamic linker, to make
16082              function pointer comparisons work between an
16083              application and shared library), otherwise set it
16084              to zero.  */
16085           sym->st_shndx = SHN_UNDEF;
16086           if (!h->pointer_equality_needed)
16087             sym->st_value = 0;
16088           else if (!h->ref_regular_nonweak)
16089             {
16090               /* This breaks function pointer comparisons, but
16091                  that is better than breaking tests for a NULL
16092                  function pointer.  */
16093               sym->st_value = 0;
16094             }
16095           break;
16096         }
16097
16098   if (h->needs_copy)
16099     {
16100       /* This symbol needs a copy reloc.  Set it up.  */
16101       Elf_Internal_Rela rela;
16102       asection *srel;
16103       bfd_byte *loc;
16104
16105       if (h->dynindx == -1
16106           || (h->root.type != bfd_link_hash_defined
16107               && h->root.type != bfd_link_hash_defweak)
16108           || htab->elf.srelbss == NULL
16109           || htab->elf.sreldynrelro == NULL)
16110         abort ();
16111
16112       rela.r_offset = (h->root.u.def.value
16113                        + h->root.u.def.section->output_section->vma
16114                        + h->root.u.def.section->output_offset);
16115       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16116       rela.r_addend = 0;
16117       if (h->root.u.def.section == htab->elf.sdynrelro)
16118         srel = htab->elf.sreldynrelro;
16119       else
16120         srel = htab->elf.srelbss;
16121       loc = srel->contents;
16122       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16123       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16124     }
16125
16126   return TRUE;
16127 }
16128
16129 /* Used to decide how to sort relocs in an optimal manner for the
16130    dynamic linker, before writing them out.  */
16131
16132 static enum elf_reloc_type_class
16133 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16134                             const asection *rel_sec,
16135                             const Elf_Internal_Rela *rela)
16136 {
16137   enum elf_ppc64_reloc_type r_type;
16138   struct ppc_link_hash_table *htab = ppc_hash_table (info);
16139
16140   if (rel_sec == htab->elf.irelplt)
16141     return reloc_class_ifunc;
16142
16143   r_type = ELF64_R_TYPE (rela->r_info);
16144   switch (r_type)
16145     {
16146     case R_PPC64_RELATIVE:
16147       return reloc_class_relative;
16148     case R_PPC64_JMP_SLOT:
16149       return reloc_class_plt;
16150     case R_PPC64_COPY:
16151       return reloc_class_copy;
16152     default:
16153       return reloc_class_normal;
16154     }
16155 }
16156
16157 /* Finish up the dynamic sections.  */
16158
16159 static bfd_boolean
16160 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16161                                    struct bfd_link_info *info)
16162 {
16163   struct ppc_link_hash_table *htab;
16164   bfd *dynobj;
16165   asection *sdyn;
16166
16167   htab = ppc_hash_table (info);
16168   if (htab == NULL)
16169     return FALSE;
16170
16171   dynobj = htab->elf.dynobj;
16172   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16173
16174   if (htab->elf.dynamic_sections_created)
16175     {
16176       Elf64_External_Dyn *dyncon, *dynconend;
16177
16178       if (sdyn == NULL || htab->elf.sgot == NULL)
16179         abort ();
16180
16181       dyncon = (Elf64_External_Dyn *) sdyn->contents;
16182       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16183       for (; dyncon < dynconend; dyncon++)
16184         {
16185           Elf_Internal_Dyn dyn;
16186           asection *s;
16187
16188           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16189
16190           switch (dyn.d_tag)
16191             {
16192             default:
16193               continue;
16194
16195             case DT_PPC64_GLINK:
16196               s = htab->glink;
16197               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16198               /* We stupidly defined DT_PPC64_GLINK to be the start
16199                  of glink rather than the first entry point, which is
16200                  what ld.so needs, and now have a bigger stub to
16201                  support automatic multiple TOCs.  */
16202               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16203               break;
16204
16205             case DT_PPC64_OPD:
16206               s = bfd_get_section_by_name (output_bfd, ".opd");
16207               if (s == NULL)
16208                 continue;
16209               dyn.d_un.d_ptr = s->vma;
16210               break;
16211
16212             case DT_PPC64_OPT:
16213               if ((htab->do_multi_toc && htab->multi_toc_needed)
16214                   || htab->notoc_plt)
16215                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16216               if (htab->has_plt_localentry0)
16217                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16218               break;
16219
16220             case DT_PPC64_OPDSZ:
16221               s = bfd_get_section_by_name (output_bfd, ".opd");
16222               if (s == NULL)
16223                 continue;
16224               dyn.d_un.d_val = s->size;
16225               break;
16226
16227             case DT_PLTGOT:
16228               s = htab->elf.splt;
16229               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16230               break;
16231
16232             case DT_JMPREL:
16233               s = htab->elf.srelplt;
16234               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16235               break;
16236
16237             case DT_PLTRELSZ:
16238               dyn.d_un.d_val = htab->elf.srelplt->size;
16239               break;
16240
16241             case DT_TEXTREL:
16242               if (htab->local_ifunc_resolver)
16243                 info->callbacks->einfo
16244                   (_("%X%P: text relocations and GNU indirect "
16245                      "functions will result in a segfault at runtime\n"));
16246               else if (htab->maybe_local_ifunc_resolver)
16247                 info->callbacks->einfo
16248                   (_("%P: warning: text relocations and GNU indirect "
16249                      "functions may result in a segfault at runtime\n"));
16250               continue;
16251             }
16252
16253           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16254         }
16255     }
16256
16257   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16258       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16259     {
16260       /* Fill in the first entry in the global offset table.
16261          We use it to hold the link-time TOCbase.  */
16262       bfd_put_64 (output_bfd,
16263                   elf_gp (output_bfd) + TOC_BASE_OFF,
16264                   htab->elf.sgot->contents);
16265
16266       /* Set .got entry size.  */
16267       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16268         = 8;
16269     }
16270
16271   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16272       && htab->elf.splt->output_section != bfd_abs_section_ptr)
16273     {
16274       /* Set .plt entry size.  */
16275       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16276         = PLT_ENTRY_SIZE (htab);
16277     }
16278
16279   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16280      brlt ourselves if emitrelocations.  */
16281   if (htab->brlt != NULL
16282       && htab->brlt->reloc_count != 0
16283       && !_bfd_elf_link_output_relocs (output_bfd,
16284                                        htab->brlt,
16285                                        elf_section_data (htab->brlt)->rela.hdr,
16286                                        elf_section_data (htab->brlt)->relocs,
16287                                        NULL))
16288     return FALSE;
16289
16290   if (htab->glink != NULL
16291       && htab->glink->reloc_count != 0
16292       && !_bfd_elf_link_output_relocs (output_bfd,
16293                                        htab->glink,
16294                                        elf_section_data (htab->glink)->rela.hdr,
16295                                        elf_section_data (htab->glink)->relocs,
16296                                        NULL))
16297     return FALSE;
16298
16299
16300   if (htab->glink_eh_frame != NULL
16301       && htab->glink_eh_frame->size != 0
16302       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16303       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16304                                            htab->glink_eh_frame,
16305                                            htab->glink_eh_frame->contents))
16306     return FALSE;
16307
16308   /* We need to handle writing out multiple GOT sections ourselves,
16309      since we didn't add them to DYNOBJ.  We know dynobj is the first
16310      bfd.  */
16311   while ((dynobj = dynobj->link.next) != NULL)
16312     {
16313       asection *s;
16314
16315       if (!is_ppc64_elf (dynobj))
16316         continue;
16317
16318       s = ppc64_elf_tdata (dynobj)->got;
16319       if (s != NULL
16320           && s->size != 0
16321           && s->output_section != bfd_abs_section_ptr
16322           && !bfd_set_section_contents (output_bfd, s->output_section,
16323                                         s->contents, s->output_offset,
16324                                         s->size))
16325         return FALSE;
16326       s = ppc64_elf_tdata (dynobj)->relgot;
16327       if (s != NULL
16328           && s->size != 0
16329           && s->output_section != bfd_abs_section_ptr
16330           && !bfd_set_section_contents (output_bfd, s->output_section,
16331                                         s->contents, s->output_offset,
16332                                         s->size))
16333         return FALSE;
16334     }
16335
16336   return TRUE;
16337 }
16338
16339 #include "elf64-target.h"
16340
16341 /* FreeBSD support */
16342
16343 #undef  TARGET_LITTLE_SYM
16344 #undef  TARGET_LITTLE_NAME
16345
16346 #undef  TARGET_BIG_SYM
16347 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
16348 #undef  TARGET_BIG_NAME
16349 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16350
16351 #undef  ELF_OSABI
16352 #define ELF_OSABI       ELFOSABI_FREEBSD
16353
16354 #undef  elf64_bed
16355 #define elf64_bed       elf64_powerpc_fbsd_bed
16356
16357 #include "elf64-target.h"