PowerPC GOT_PCREL34 optimisation
[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         case R_PPC64_GOT_PCREL34:
4571           ppc64_elf_tdata (abfd)->has_gotrel = 1;
4572           ppc64_elf_section_data (sec)->has_gotrel = 1;
4573           /* Fall through.  */
4574
4575         case R_PPC64_GOT16:
4576         case R_PPC64_GOT16_HI:
4577         case R_PPC64_GOT16_LO:
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 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8145      pld ra,symbol@got@pcrel
8146      load/store rt,0(ra)
8147    or
8148      paddi ra,symbol@pcrel
8149      load/store rt,0(ra)
8150    may be translated to
8151      pload/pstore rt,symbol@pcrel
8152      nop.
8153    This function returns true if the optimization is possible, placing
8154    the prefix insn in *PINSN1 and a NOP in *PINSN2.
8155
8156    On entry to this function, the linker has already determined that
8157    the pld can be replaced with paddi: *PINSN1 is that paddi insn,
8158    while *PINSN2 is the second instruction.  */
8159
8160 static bfd_boolean
8161 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2)
8162 {
8163   uint32_t insn2 = *pinsn2 >> 32;
8164   uint64_t i1new;
8165
8166   /* Check that regs match.  */
8167   if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
8168     return FALSE;
8169
8170   switch ((insn2 >> 26) & 63)
8171     {
8172     default:
8173       return FALSE;
8174
8175     case 32: /* lwz */
8176     case 34: /* lbz */
8177     case 36: /* stw */
8178     case 38: /* stb */
8179     case 40: /* lhz */
8180     case 42: /* lha */
8181     case 44: /* sth */
8182     case 48: /* lfs */
8183     case 50: /* lfd */
8184     case 52: /* stfs */
8185     case 54: /* stfd */
8186       /* These are the PMLS cases, where we just need to tack a prefix
8187          on the insn.  Check that the D field is zero.  */
8188       if ((insn2 & 0xffff) != 0)
8189         return FALSE;
8190       i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8191                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8192       break;
8193
8194     case 58: /* lwa, ld */
8195       if ((insn2 & 0xfffd) != 0)
8196         return FALSE;
8197       i1new = ((1ULL << 58) | (1ULL << 52)
8198                | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8199                | (insn2 & (31ULL << 21)));
8200       break;
8201
8202     case 57: /* lxsd, lxssp */
8203       if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
8204         return FALSE;
8205       i1new = ((1ULL << 58) | (1ULL << 52)
8206                | ((40ULL | (insn2 & 3)) << 26)
8207                | (insn2 & (31ULL << 21)));
8208       break;
8209
8210     case 61: /* stxsd, stxssp, lxv, stxv  */
8211       if ((insn2 & 3) == 0)
8212         return FALSE;
8213       else if ((insn2 & 3) >= 2)
8214         {
8215           if ((insn2 & 0xfffc) != 0)
8216             return FALSE;
8217           i1new = ((1ULL << 58) | (1ULL << 52)
8218                    | ((44ULL | (insn2 & 3)) << 26)
8219                    | (insn2 & (31ULL << 21)));
8220         }
8221       else
8222         {
8223           if ((insn2 & 0xfff0) != 0)
8224             return FALSE;
8225           i1new = ((1ULL << 58) | (1ULL << 52)
8226                    | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8227                    | (insn2 & (31ULL << 21)));
8228         }
8229       break;
8230
8231     case 56: /* lq */
8232       if ((insn2 & 0xffff) != 0)
8233         return FALSE;
8234       i1new = ((1ULL << 58) | (1ULL << 52)
8235                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8236       break;
8237
8238     case 62: /* std, stq */
8239       if ((insn2 & 0xfffd) != 0)
8240         return FALSE;
8241       i1new = ((1ULL << 58) | (1ULL << 52)
8242                | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8243                | (insn2 & (31ULL << 21)));
8244       break;
8245     }
8246
8247   *pinsn1 = i1new;
8248   *pinsn2 = (uint64_t) NOP << 32;
8249   return TRUE;
8250 }
8251
8252 /* Examine all relocs referencing .toc sections in order to remove
8253    unused .toc entries.  */
8254
8255 bfd_boolean
8256 ppc64_elf_edit_toc (struct bfd_link_info *info)
8257 {
8258   bfd *ibfd;
8259   struct adjust_toc_info toc_inf;
8260   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8261
8262   htab->do_toc_opt = 1;
8263   toc_inf.global_toc_syms = TRUE;
8264   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8265     {
8266       asection *toc, *sec;
8267       Elf_Internal_Shdr *symtab_hdr;
8268       Elf_Internal_Sym *local_syms;
8269       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8270       unsigned long *skip, *drop;
8271       unsigned char *used;
8272       unsigned char *keep, last, some_unused;
8273
8274       if (!is_ppc64_elf (ibfd))
8275         continue;
8276
8277       toc = bfd_get_section_by_name (ibfd, ".toc");
8278       if (toc == NULL
8279           || toc->size == 0
8280           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8281           || discarded_section (toc))
8282         continue;
8283
8284       toc_relocs = NULL;
8285       local_syms = NULL;
8286       symtab_hdr = &elf_symtab_hdr (ibfd);
8287
8288       /* Look at sections dropped from the final link.  */
8289       skip = NULL;
8290       relstart = NULL;
8291       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8292         {
8293           if (sec->reloc_count == 0
8294               || !discarded_section (sec)
8295               || get_opd_info (sec)
8296               || (sec->flags & SEC_ALLOC) == 0
8297               || (sec->flags & SEC_DEBUGGING) != 0)
8298             continue;
8299
8300           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8301           if (relstart == NULL)
8302             goto error_ret;
8303
8304           /* Run through the relocs to see which toc entries might be
8305              unused.  */
8306           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8307             {
8308               enum elf_ppc64_reloc_type r_type;
8309               unsigned long r_symndx;
8310               asection *sym_sec;
8311               struct elf_link_hash_entry *h;
8312               Elf_Internal_Sym *sym;
8313               bfd_vma val;
8314
8315               r_type = ELF64_R_TYPE (rel->r_info);
8316               switch (r_type)
8317                 {
8318                 default:
8319                   continue;
8320
8321                 case R_PPC64_TOC16:
8322                 case R_PPC64_TOC16_LO:
8323                 case R_PPC64_TOC16_HI:
8324                 case R_PPC64_TOC16_HA:
8325                 case R_PPC64_TOC16_DS:
8326                 case R_PPC64_TOC16_LO_DS:
8327                   break;
8328                 }
8329
8330               r_symndx = ELF64_R_SYM (rel->r_info);
8331               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8332                               r_symndx, ibfd))
8333                 goto error_ret;
8334
8335               if (sym_sec != toc)
8336                 continue;
8337
8338               if (h != NULL)
8339                 val = h->root.u.def.value;
8340               else
8341                 val = sym->st_value;
8342               val += rel->r_addend;
8343
8344               if (val >= toc->size)
8345                 continue;
8346
8347               /* Anything in the toc ought to be aligned to 8 bytes.
8348                  If not, don't mark as unused.  */
8349               if (val & 7)
8350                 continue;
8351
8352               if (skip == NULL)
8353                 {
8354                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8355                   if (skip == NULL)
8356                     goto error_ret;
8357                 }
8358
8359               skip[val >> 3] = ref_from_discarded;
8360             }
8361
8362           if (elf_section_data (sec)->relocs != relstart)
8363             free (relstart);
8364         }
8365
8366       /* For largetoc loads of address constants, we can convert
8367          .  addis rx,2,addr@got@ha
8368          .  ld ry,addr@got@l(rx)
8369          to
8370          .  addis rx,2,addr@toc@ha
8371          .  addi ry,rx,addr@toc@l
8372          when addr is within 2G of the toc pointer.  This then means
8373          that the word storing "addr" in the toc is no longer needed.  */
8374
8375       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8376           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8377           && toc->reloc_count != 0)
8378         {
8379           /* Read toc relocs.  */
8380           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8381                                                   info->keep_memory);
8382           if (toc_relocs == NULL)
8383             goto error_ret;
8384
8385           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8386             {
8387               enum elf_ppc64_reloc_type r_type;
8388               unsigned long r_symndx;
8389               asection *sym_sec;
8390               struct elf_link_hash_entry *h;
8391               Elf_Internal_Sym *sym;
8392               bfd_vma val, addr;
8393
8394               r_type = ELF64_R_TYPE (rel->r_info);
8395               if (r_type != R_PPC64_ADDR64)
8396                 continue;
8397
8398               r_symndx = ELF64_R_SYM (rel->r_info);
8399               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8400                               r_symndx, ibfd))
8401                 goto error_ret;
8402
8403               if (sym_sec == NULL
8404                   || sym_sec->output_section == NULL
8405                   || discarded_section (sym_sec))
8406                 continue;
8407
8408               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8409                 continue;
8410
8411               if (h != NULL)
8412                 {
8413                   if (h->type == STT_GNU_IFUNC)
8414                     continue;
8415                   val = h->root.u.def.value;
8416                 }
8417               else
8418                 {
8419                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8420                     continue;
8421                   val = sym->st_value;
8422                 }
8423               val += rel->r_addend;
8424               val += sym_sec->output_section->vma + sym_sec->output_offset;
8425
8426               /* We don't yet know the exact toc pointer value, but we
8427                  know it will be somewhere in the toc section.  Don't
8428                  optimize if the difference from any possible toc
8429                  pointer is outside [ff..f80008000, 7fff7fff].  */
8430               addr = toc->output_section->vma + TOC_BASE_OFF;
8431               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8432                 continue;
8433
8434               addr = toc->output_section->vma + toc->output_section->rawsize;
8435               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8436                 continue;
8437
8438               if (skip == NULL)
8439                 {
8440                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8441                   if (skip == NULL)
8442                     goto error_ret;
8443                 }
8444
8445               skip[rel->r_offset >> 3]
8446                 |= can_optimize | ((rel - toc_relocs) << 2);
8447             }
8448         }
8449
8450       if (skip == NULL)
8451         continue;
8452
8453       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8454       if (used == NULL)
8455         {
8456         error_ret:
8457           if (local_syms != NULL
8458               && symtab_hdr->contents != (unsigned char *) local_syms)
8459             free (local_syms);
8460           if (sec != NULL
8461               && relstart != NULL
8462               && elf_section_data (sec)->relocs != relstart)
8463             free (relstart);
8464           if (toc_relocs != NULL
8465               && elf_section_data (toc)->relocs != toc_relocs)
8466             free (toc_relocs);
8467           if (skip != NULL)
8468             free (skip);
8469           return FALSE;
8470         }
8471
8472       /* Now check all kept sections that might reference the toc.
8473          Check the toc itself last.  */
8474       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8475                   : ibfd->sections);
8476            sec != NULL;
8477            sec = (sec == toc ? NULL
8478                   : sec->next == NULL ? toc
8479                   : sec->next == toc && toc->next ? toc->next
8480                   : sec->next))
8481         {
8482           int repeat;
8483
8484           if (sec->reloc_count == 0
8485               || discarded_section (sec)
8486               || get_opd_info (sec)
8487               || (sec->flags & SEC_ALLOC) == 0
8488               || (sec->flags & SEC_DEBUGGING) != 0)
8489             continue;
8490
8491           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8492                                                 info->keep_memory);
8493           if (relstart == NULL)
8494             {
8495               free (used);
8496               goto error_ret;
8497             }
8498
8499           /* Mark toc entries referenced as used.  */
8500           do
8501             {
8502               repeat = 0;
8503               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8504                 {
8505                   enum elf_ppc64_reloc_type r_type;
8506                   unsigned long r_symndx;
8507                   asection *sym_sec;
8508                   struct elf_link_hash_entry *h;
8509                   Elf_Internal_Sym *sym;
8510                   bfd_vma val;
8511                   enum {no_check, check_lo, check_ha} insn_check;
8512
8513                   r_type = ELF64_R_TYPE (rel->r_info);
8514                   switch (r_type)
8515                     {
8516                     default:
8517                       insn_check = no_check;
8518                       break;
8519
8520                     case R_PPC64_GOT_TLSLD16_HA:
8521                     case R_PPC64_GOT_TLSGD16_HA:
8522                     case R_PPC64_GOT_TPREL16_HA:
8523                     case R_PPC64_GOT_DTPREL16_HA:
8524                     case R_PPC64_GOT16_HA:
8525                     case R_PPC64_TOC16_HA:
8526                       insn_check = check_ha;
8527                       break;
8528
8529                     case R_PPC64_GOT_TLSLD16_LO:
8530                     case R_PPC64_GOT_TLSGD16_LO:
8531                     case R_PPC64_GOT_TPREL16_LO_DS:
8532                     case R_PPC64_GOT_DTPREL16_LO_DS:
8533                     case R_PPC64_GOT16_LO:
8534                     case R_PPC64_GOT16_LO_DS:
8535                     case R_PPC64_TOC16_LO:
8536                     case R_PPC64_TOC16_LO_DS:
8537                       insn_check = check_lo;
8538                       break;
8539                     }
8540
8541                   if (insn_check != no_check)
8542                     {
8543                       bfd_vma off = rel->r_offset & ~3;
8544                       unsigned char buf[4];
8545                       unsigned int insn;
8546
8547                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8548                         {
8549                           free (used);
8550                           goto error_ret;
8551                         }
8552                       insn = bfd_get_32 (ibfd, buf);
8553                       if (insn_check == check_lo
8554                           ? !ok_lo_toc_insn (insn, r_type)
8555                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8556                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8557                         {
8558                           char str[12];
8559
8560                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8561                           sprintf (str, "%#08x", insn);
8562                           info->callbacks->einfo
8563                             /* xgettext:c-format */
8564                             (_("%H: toc optimization is not supported for"
8565                                " %s instruction\n"),
8566                              ibfd, sec, rel->r_offset & ~3, str);
8567                         }
8568                     }
8569
8570                   switch (r_type)
8571                     {
8572                     case R_PPC64_TOC16:
8573                     case R_PPC64_TOC16_LO:
8574                     case R_PPC64_TOC16_HI:
8575                     case R_PPC64_TOC16_HA:
8576                     case R_PPC64_TOC16_DS:
8577                     case R_PPC64_TOC16_LO_DS:
8578                       /* In case we're taking addresses of toc entries.  */
8579                     case R_PPC64_ADDR64:
8580                       break;
8581
8582                     default:
8583                       continue;
8584                     }
8585
8586                   r_symndx = ELF64_R_SYM (rel->r_info);
8587                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8588                                   r_symndx, ibfd))
8589                     {
8590                       free (used);
8591                       goto error_ret;
8592                     }
8593
8594                   if (sym_sec != toc)
8595                     continue;
8596
8597                   if (h != NULL)
8598                     val = h->root.u.def.value;
8599                   else
8600                     val = sym->st_value;
8601                   val += rel->r_addend;
8602
8603                   if (val >= toc->size)
8604                     continue;
8605
8606                   if ((skip[val >> 3] & can_optimize) != 0)
8607                     {
8608                       bfd_vma off;
8609                       unsigned char opc;
8610
8611                       switch (r_type)
8612                         {
8613                         case R_PPC64_TOC16_HA:
8614                           break;
8615
8616                         case R_PPC64_TOC16_LO_DS:
8617                           off = rel->r_offset;
8618                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8619                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8620                                                          off, 1))
8621                             {
8622                               free (used);
8623                               goto error_ret;
8624                             }
8625                           if ((opc & (0x3f << 2)) == (58u << 2))
8626                             break;
8627                           /* Fall through.  */
8628
8629                         default:
8630                           /* Wrong sort of reloc, or not a ld.  We may
8631                              as well clear ref_from_discarded too.  */
8632                           skip[val >> 3] = 0;
8633                         }
8634                     }
8635
8636                   if (sec != toc)
8637                     used[val >> 3] = 1;
8638                   /* For the toc section, we only mark as used if this
8639                      entry itself isn't unused.  */
8640                   else if ((used[rel->r_offset >> 3]
8641                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8642                            && !used[val >> 3])
8643                     {
8644                       /* Do all the relocs again, to catch reference
8645                          chains.  */
8646                       repeat = 1;
8647                       used[val >> 3] = 1;
8648                     }
8649                 }
8650             }
8651           while (repeat);
8652
8653           if (elf_section_data (sec)->relocs != relstart)
8654             free (relstart);
8655         }
8656
8657       /* Merge the used and skip arrays.  Assume that TOC
8658          doublewords not appearing as either used or unused belong
8659          to an entry more than one doubleword in size.  */
8660       for (drop = skip, keep = used, last = 0, some_unused = 0;
8661            drop < skip + (toc->size + 7) / 8;
8662            ++drop, ++keep)
8663         {
8664           if (*keep)
8665             {
8666               *drop &= ~ref_from_discarded;
8667               if ((*drop & can_optimize) != 0)
8668                 some_unused = 1;
8669               last = 0;
8670             }
8671           else if ((*drop & ref_from_discarded) != 0)
8672             {
8673               some_unused = 1;
8674               last = ref_from_discarded;
8675             }
8676           else
8677             *drop = last;
8678         }
8679
8680       free (used);
8681
8682       if (some_unused)
8683         {
8684           bfd_byte *contents, *src;
8685           unsigned long off;
8686           Elf_Internal_Sym *sym;
8687           bfd_boolean local_toc_syms = FALSE;
8688
8689           /* Shuffle the toc contents, and at the same time convert the
8690              skip array from booleans into offsets.  */
8691           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8692             goto error_ret;
8693
8694           elf_section_data (toc)->this_hdr.contents = contents;
8695
8696           for (src = contents, off = 0, drop = skip;
8697                src < contents + toc->size;
8698                src += 8, ++drop)
8699             {
8700               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8701                 off += 8;
8702               else if (off != 0)
8703                 {
8704                   *drop = off;
8705                   memcpy (src - off, src, 8);
8706                 }
8707             }
8708           *drop = off;
8709           toc->rawsize = toc->size;
8710           toc->size = src - contents - off;
8711
8712           /* Adjust addends for relocs against the toc section sym,
8713              and optimize any accesses we can.  */
8714           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8715             {
8716               if (sec->reloc_count == 0
8717                   || discarded_section (sec))
8718                 continue;
8719
8720               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8721                                                     info->keep_memory);
8722               if (relstart == NULL)
8723                 goto error_ret;
8724
8725               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8726                 {
8727                   enum elf_ppc64_reloc_type r_type;
8728                   unsigned long r_symndx;
8729                   asection *sym_sec;
8730                   struct elf_link_hash_entry *h;
8731                   bfd_vma val;
8732
8733                   r_type = ELF64_R_TYPE (rel->r_info);
8734                   switch (r_type)
8735                     {
8736                     default:
8737                       continue;
8738
8739                     case R_PPC64_TOC16:
8740                     case R_PPC64_TOC16_LO:
8741                     case R_PPC64_TOC16_HI:
8742                     case R_PPC64_TOC16_HA:
8743                     case R_PPC64_TOC16_DS:
8744                     case R_PPC64_TOC16_LO_DS:
8745                     case R_PPC64_ADDR64:
8746                       break;
8747                     }
8748
8749                   r_symndx = ELF64_R_SYM (rel->r_info);
8750                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8751                                   r_symndx, ibfd))
8752                     goto error_ret;
8753
8754                   if (sym_sec != toc)
8755                     continue;
8756
8757                   if (h != NULL)
8758                     val = h->root.u.def.value;
8759                   else
8760                     {
8761                       val = sym->st_value;
8762                       if (val != 0)
8763                         local_toc_syms = TRUE;
8764                     }
8765
8766                   val += rel->r_addend;
8767
8768                   if (val > toc->rawsize)
8769                     val = toc->rawsize;
8770                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8771                     continue;
8772                   else if ((skip[val >> 3] & can_optimize) != 0)
8773                     {
8774                       Elf_Internal_Rela *tocrel
8775                         = toc_relocs + (skip[val >> 3] >> 2);
8776                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8777
8778                       switch (r_type)
8779                         {
8780                         case R_PPC64_TOC16_HA:
8781                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8782                           break;
8783
8784                         case R_PPC64_TOC16_LO_DS:
8785                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8786                           break;
8787
8788                         default:
8789                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8790                             ppc_howto_init ();
8791                           info->callbacks->einfo
8792                             /* xgettext:c-format */
8793                             (_("%H: %s references "
8794                                "optimized away TOC entry\n"),
8795                              ibfd, sec, rel->r_offset,
8796                              ppc64_elf_howto_table[r_type]->name);
8797                           bfd_set_error (bfd_error_bad_value);
8798                           goto error_ret;
8799                         }
8800                       rel->r_addend = tocrel->r_addend;
8801                       elf_section_data (sec)->relocs = relstart;
8802                       continue;
8803                     }
8804
8805                   if (h != NULL || sym->st_value != 0)
8806                     continue;
8807
8808                   rel->r_addend -= skip[val >> 3];
8809                   elf_section_data (sec)->relocs = relstart;
8810                 }
8811
8812               if (elf_section_data (sec)->relocs != relstart)
8813                 free (relstart);
8814             }
8815
8816           /* We shouldn't have local or global symbols defined in the TOC,
8817              but handle them anyway.  */
8818           if (local_syms != NULL)
8819             for (sym = local_syms;
8820                  sym < local_syms + symtab_hdr->sh_info;
8821                  ++sym)
8822               if (sym->st_value != 0
8823                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8824                 {
8825                   unsigned long i;
8826
8827                   if (sym->st_value > toc->rawsize)
8828                     i = toc->rawsize >> 3;
8829                   else
8830                     i = sym->st_value >> 3;
8831
8832                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8833                     {
8834                       if (local_toc_syms)
8835                         _bfd_error_handler
8836                           (_("%s defined on removed toc entry"),
8837                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8838                       do
8839                         ++i;
8840                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8841                       sym->st_value = (bfd_vma) i << 3;
8842                     }
8843
8844                   sym->st_value -= skip[i];
8845                   symtab_hdr->contents = (unsigned char *) local_syms;
8846                 }
8847
8848           /* Adjust any global syms defined in this toc input section.  */
8849           if (toc_inf.global_toc_syms)
8850             {
8851               toc_inf.toc = toc;
8852               toc_inf.skip = skip;
8853               toc_inf.global_toc_syms = FALSE;
8854               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8855                                       &toc_inf);
8856             }
8857
8858           if (toc->reloc_count != 0)
8859             {
8860               Elf_Internal_Shdr *rel_hdr;
8861               Elf_Internal_Rela *wrel;
8862               bfd_size_type sz;
8863
8864               /* Remove unused toc relocs, and adjust those we keep.  */
8865               if (toc_relocs == NULL)
8866                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8867                                                         info->keep_memory);
8868               if (toc_relocs == NULL)
8869                 goto error_ret;
8870
8871               wrel = toc_relocs;
8872               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8873                 if ((skip[rel->r_offset >> 3]
8874                      & (ref_from_discarded | can_optimize)) == 0)
8875                   {
8876                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8877                     wrel->r_info = rel->r_info;
8878                     wrel->r_addend = rel->r_addend;
8879                     ++wrel;
8880                   }
8881                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8882                                             &local_syms, NULL, NULL))
8883                   goto error_ret;
8884
8885               elf_section_data (toc)->relocs = toc_relocs;
8886               toc->reloc_count = wrel - toc_relocs;
8887               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8888               sz = rel_hdr->sh_entsize;
8889               rel_hdr->sh_size = toc->reloc_count * sz;
8890             }
8891         }
8892       else if (toc_relocs != NULL
8893                && elf_section_data (toc)->relocs != toc_relocs)
8894         free (toc_relocs);
8895
8896       if (local_syms != NULL
8897           && symtab_hdr->contents != (unsigned char *) local_syms)
8898         {
8899           if (!info->keep_memory)
8900             free (local_syms);
8901           else
8902             symtab_hdr->contents = (unsigned char *) local_syms;
8903         }
8904       free (skip);
8905     }
8906
8907   /* Look for cases where we can change an indirect GOT access to
8908      a GOT relative or PC relative access, possibly reducing the
8909      number of GOT entries.  */
8910   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8911     {
8912       asection *sec;
8913       Elf_Internal_Shdr *symtab_hdr;
8914       Elf_Internal_Sym *local_syms;
8915       Elf_Internal_Rela *relstart, *rel;
8916       bfd_vma got;
8917
8918       if (!is_ppc64_elf (ibfd))
8919         continue;
8920
8921       if (!ppc64_elf_tdata (ibfd)->has_gotrel)
8922         continue;
8923
8924       sec = ppc64_elf_tdata (ibfd)->got;
8925       got = sec->output_section->vma + sec->output_offset + 0x8000;
8926
8927       local_syms = NULL;
8928       symtab_hdr = &elf_symtab_hdr (ibfd);
8929
8930       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8931         {
8932           if (sec->reloc_count == 0
8933               || !ppc64_elf_section_data (sec)->has_gotrel
8934               || discarded_section (sec))
8935             continue;
8936
8937           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8938                                                 info->keep_memory);
8939           if (relstart == NULL)
8940             {
8941             got_error_ret:
8942               if (local_syms != NULL
8943                   && symtab_hdr->contents != (unsigned char *) local_syms)
8944                 free (local_syms);
8945               if (sec != NULL
8946                   && relstart != NULL
8947                   && elf_section_data (sec)->relocs != relstart)
8948                 free (relstart);
8949               return FALSE;
8950             }
8951
8952           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8953             {
8954               enum elf_ppc64_reloc_type r_type;
8955               unsigned long r_symndx;
8956               Elf_Internal_Sym *sym;
8957               asection *sym_sec;
8958               struct elf_link_hash_entry *h;
8959               struct got_entry *ent;
8960               bfd_vma sym_addend, val, pc;
8961               unsigned char buf[8];
8962               unsigned int insn;
8963
8964               r_type = ELF64_R_TYPE (rel->r_info);
8965               switch (r_type)
8966                 {
8967                 default:
8968                   continue;
8969
8970                 case R_PPC64_GOT16_DS:
8971                 case R_PPC64_GOT16_HA:
8972                 case R_PPC64_GOT16_LO_DS:
8973                   sym_addend = rel->r_addend;
8974                   break;
8975
8976                 case R_PPC64_GOT_PCREL34:
8977                   sym_addend = 0;
8978                   break;
8979                 }
8980
8981               r_symndx = ELF64_R_SYM (rel->r_info);
8982               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8983                               r_symndx, ibfd))
8984                 goto got_error_ret;
8985
8986               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8987                 continue;
8988
8989               if (h != NULL)
8990                 val = h->root.u.def.value;
8991               else
8992                 val = sym->st_value;
8993               val += sym_addend;
8994               val += sym_sec->output_section->vma + sym_sec->output_offset;
8995
8996               switch (r_type)
8997                 {
8998                 default:
8999                   continue;
9000
9001                 case R_PPC64_GOT16_DS:
9002                   if (val - got + 0x8000 >= 0x10000)
9003                     continue;
9004                   if (!bfd_get_section_contents (ibfd, sec, buf,
9005                                                  rel->r_offset & ~3, 4))
9006                     goto got_error_ret;
9007                   insn = bfd_get_32 (ibfd, buf);
9008                   if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9009                     continue;
9010                   break;
9011
9012                 case R_PPC64_GOT16_HA:
9013                   if (val - got + 0x80008000ULL >= 0x100000000ULL)
9014                     continue;
9015
9016                   if (!bfd_get_section_contents (ibfd, sec, buf,
9017                                                  rel->r_offset & ~3, 4))
9018                     goto got_error_ret;
9019                   insn = bfd_get_32 (ibfd, buf);
9020                   if (((insn & ((0x3f << 26) | 0x1f << 16))
9021                        != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9022                     continue;
9023                   break;
9024
9025                 case R_PPC64_GOT16_LO_DS:
9026                   if (val - got + 0x80008000ULL >= 0x100000000ULL)
9027                     continue;
9028                   if (!bfd_get_section_contents (ibfd, sec, buf,
9029                                                  rel->r_offset & ~3, 4))
9030                     goto got_error_ret;
9031                   insn = bfd_get_32 (ibfd, buf);
9032                   if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9033                     continue;
9034                   break;
9035
9036                 case R_PPC64_GOT_PCREL34:
9037                   pc = rel->r_offset;
9038                   pc += sec->output_section->vma + sec->output_offset;
9039                   if (val - pc + (1ULL << 33) >= 1ULL << 34)
9040                     continue;
9041                   if (!bfd_get_section_contents (ibfd, sec, buf,
9042                                                  rel->r_offset & ~3, 8))
9043                     goto got_error_ret;
9044                   insn = bfd_get_32 (ibfd, buf);
9045                   if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9046                     continue;
9047                   insn = bfd_get_32 (ibfd, buf + 4);
9048                   if ((insn & (0x3f << 26)) != 57u << 26)
9049                     continue;
9050                   break;
9051                 }
9052
9053               if (h != NULL)
9054                 ent = h->got.glist;
9055               else
9056                 {
9057                   struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9058                   ent = local_got_ents[r_symndx];
9059                 }
9060               for (; ent != NULL; ent = ent->next)
9061                 if (ent->addend == sym_addend
9062                     && ent->owner == ibfd
9063                     && ent->tls_type == 0)
9064                   break;
9065               BFD_ASSERT (ent && ent->got.refcount > 0);
9066               ent->got.refcount -= 1;
9067             }
9068
9069           if (elf_section_data (sec)->relocs != relstart)
9070             free (relstart);
9071         }
9072
9073       if (local_syms != NULL
9074           && symtab_hdr->contents != (unsigned char *) local_syms)
9075         {
9076           if (!info->keep_memory)
9077             free (local_syms);
9078           else
9079             symtab_hdr->contents = (unsigned char *) local_syms;
9080         }
9081     }
9082
9083   return TRUE;
9084 }
9085
9086 /* Return true iff input section I references the TOC using
9087    instructions limited to +/-32k offsets.  */
9088
9089 bfd_boolean
9090 ppc64_elf_has_small_toc_reloc (asection *i)
9091 {
9092   return (is_ppc64_elf (i->owner)
9093           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9094 }
9095
9096 /* Allocate space for one GOT entry.  */
9097
9098 static void
9099 allocate_got (struct elf_link_hash_entry *h,
9100               struct bfd_link_info *info,
9101               struct got_entry *gent)
9102 {
9103   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9104   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9105   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9106                  ? 16 : 8);
9107   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9108                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9109   asection *got = ppc64_elf_tdata (gent->owner)->got;
9110
9111   gent->got.offset = got->size;
9112   got->size += entsize;
9113
9114   if (h->type == STT_GNU_IFUNC)
9115     {
9116       htab->elf.irelplt->size += rentsize;
9117       htab->got_reli_size += rentsize;
9118     }
9119   else if (((bfd_link_pic (info)
9120              && !((gent->tls_type & TLS_TPREL) != 0
9121                   && bfd_link_executable (info)
9122                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9123             || (htab->elf.dynamic_sections_created
9124                 && h->dynindx != -1
9125                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9126            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9127     {
9128       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9129       relgot->size += rentsize;
9130     }
9131 }
9132
9133 /* This function merges got entries in the same toc group.  */
9134
9135 static void
9136 merge_got_entries (struct got_entry **pent)
9137 {
9138   struct got_entry *ent, *ent2;
9139
9140   for (ent = *pent; ent != NULL; ent = ent->next)
9141     if (!ent->is_indirect)
9142       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9143         if (!ent2->is_indirect
9144             && ent2->addend == ent->addend
9145             && ent2->tls_type == ent->tls_type
9146             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9147           {
9148             ent2->is_indirect = TRUE;
9149             ent2->got.ent = ent;
9150           }
9151 }
9152
9153 /* If H is undefined, make it dynamic if that makes sense.  */
9154
9155 static bfd_boolean
9156 ensure_undef_dynamic (struct bfd_link_info *info,
9157                       struct elf_link_hash_entry *h)
9158 {
9159   struct elf_link_hash_table *htab = elf_hash_table (info);
9160
9161   if (htab->dynamic_sections_created
9162       && ((info->dynamic_undefined_weak != 0
9163            && h->root.type == bfd_link_hash_undefweak)
9164           || h->root.type == bfd_link_hash_undefined)
9165       && h->dynindx == -1
9166       && !h->forced_local
9167       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9168     return bfd_elf_link_record_dynamic_symbol (info, h);
9169   return TRUE;
9170 }
9171
9172 /* Allocate space in .plt, .got and associated reloc sections for
9173    dynamic relocs.  */
9174
9175 static bfd_boolean
9176 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9177 {
9178   struct bfd_link_info *info;
9179   struct ppc_link_hash_table *htab;
9180   asection *s;
9181   struct ppc_link_hash_entry *eh;
9182   struct got_entry **pgent, *gent;
9183
9184   if (h->root.type == bfd_link_hash_indirect)
9185     return TRUE;
9186
9187   info = (struct bfd_link_info *) inf;
9188   htab = ppc_hash_table (info);
9189   if (htab == NULL)
9190     return FALSE;
9191
9192   eh = (struct ppc_link_hash_entry *) h;
9193   /* Run through the TLS GD got entries first if we're changing them
9194      to TPREL.  */
9195   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9196     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9197       if (gent->got.refcount > 0
9198           && (gent->tls_type & TLS_GD) != 0)
9199         {
9200           /* This was a GD entry that has been converted to TPREL.  If
9201              there happens to be a TPREL entry we can use that one.  */
9202           struct got_entry *ent;
9203           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9204             if (ent->got.refcount > 0
9205                 && (ent->tls_type & TLS_TPREL) != 0
9206                 && ent->addend == gent->addend
9207                 && ent->owner == gent->owner)
9208               {
9209                 gent->got.refcount = 0;
9210                 break;
9211               }
9212
9213           /* If not, then we'll be using our own TPREL entry.  */
9214           if (gent->got.refcount != 0)
9215             gent->tls_type = TLS_TLS | TLS_TPREL;
9216         }
9217
9218   /* Remove any list entry that won't generate a word in the GOT before
9219      we call merge_got_entries.  Otherwise we risk merging to empty
9220      entries.  */
9221   pgent = &h->got.glist;
9222   while ((gent = *pgent) != NULL)
9223     if (gent->got.refcount > 0)
9224       {
9225         if ((gent->tls_type & TLS_LD) != 0
9226             && !h->def_dynamic)
9227           {
9228             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9229             *pgent = gent->next;
9230           }
9231         else
9232           pgent = &gent->next;
9233       }
9234     else
9235       *pgent = gent->next;
9236
9237   if (!htab->do_multi_toc)
9238     merge_got_entries (&h->got.glist);
9239
9240   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9241     if (!gent->is_indirect)
9242       {
9243         /* Make sure this symbol is output as a dynamic symbol.  */
9244         if (!ensure_undef_dynamic (info, h))
9245           return FALSE;
9246
9247         if (!is_ppc64_elf (gent->owner))
9248           abort ();
9249
9250         allocate_got (h, info, gent);
9251       }
9252
9253   /* If no dynamic sections we can't have dynamic relocs, except for
9254      IFUNCs which are handled even in static executables.  */
9255   if (!htab->elf.dynamic_sections_created
9256       && h->type != STT_GNU_IFUNC)
9257     eh->dyn_relocs = NULL;
9258
9259   /* Discard relocs on undefined symbols that must be local.  */
9260   else if (h->root.type == bfd_link_hash_undefined
9261            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9262     eh->dyn_relocs = NULL;
9263
9264   /* Also discard relocs on undefined weak syms with non-default
9265      visibility, or when dynamic_undefined_weak says so.  */
9266   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9267     eh->dyn_relocs = NULL;
9268
9269   if (eh->dyn_relocs != NULL)
9270     {
9271       struct elf_dyn_relocs *p, **pp;
9272
9273       /* In the shared -Bsymbolic case, discard space allocated for
9274          dynamic pc-relative relocs against symbols which turn out to
9275          be defined in regular objects.  For the normal shared case,
9276          discard space for relocs that have become local due to symbol
9277          visibility changes.  */
9278
9279       if (bfd_link_pic (info))
9280         {
9281           /* Relocs that use pc_count are those that appear on a call
9282              insn, or certain REL relocs (see must_be_dyn_reloc) that
9283              can be generated via assembly.  We want calls to
9284              protected symbols to resolve directly to the function
9285              rather than going via the plt.  If people want function
9286              pointer comparisons to work as expected then they should
9287              avoid writing weird assembly.  */
9288           if (SYMBOL_CALLS_LOCAL (info, h))
9289             {
9290               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9291                 {
9292                   p->count -= p->pc_count;
9293                   p->pc_count = 0;
9294                   if (p->count == 0)
9295                     *pp = p->next;
9296                   else
9297                     pp = &p->next;
9298                 }
9299             }
9300
9301           if (eh->dyn_relocs != NULL)
9302             {
9303               /* Make sure this symbol is output as a dynamic symbol.  */
9304               if (!ensure_undef_dynamic (info, h))
9305                 return FALSE;
9306             }
9307         }
9308       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9309         {
9310           /* For the non-pic case, discard space for relocs against
9311              symbols which turn out to need copy relocs or are not
9312              dynamic.  */
9313           if (h->dynamic_adjusted
9314               && !h->def_regular
9315               && !ELF_COMMON_DEF_P (h))
9316             {
9317               /* Make sure this symbol is output as a dynamic symbol.  */
9318               if (!ensure_undef_dynamic (info, h))
9319                 return FALSE;
9320
9321               if (h->dynindx == -1)
9322                 eh->dyn_relocs = NULL;
9323             }
9324           else
9325             eh->dyn_relocs = NULL;
9326         }
9327
9328       /* Finally, allocate space.  */
9329       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9330         {
9331           asection *sreloc = elf_section_data (p->sec)->sreloc;
9332           if (eh->elf.type == STT_GNU_IFUNC)
9333             sreloc = htab->elf.irelplt;
9334           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9335         }
9336     }
9337
9338   /* We might need a PLT entry when the symbol
9339      a) is dynamic, or
9340      b) is an ifunc, or
9341      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9342      d) has plt16 relocs and we are linking statically.  */
9343   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9344       || h->type == STT_GNU_IFUNC
9345       || (h->needs_plt && h->dynamic_adjusted)
9346       || (h->needs_plt
9347           && h->def_regular
9348           && !htab->elf.dynamic_sections_created
9349           && !htab->can_convert_all_inline_plt
9350           && (((struct ppc_link_hash_entry *) h)->tls_mask
9351               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9352     {
9353       struct plt_entry *pent;
9354       bfd_boolean doneone = FALSE;
9355       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9356         if (pent->plt.refcount > 0)
9357           {
9358             if (!htab->elf.dynamic_sections_created
9359                 || h->dynindx == -1)
9360               {
9361                 if (h->type == STT_GNU_IFUNC)
9362                   {
9363                     s = htab->elf.iplt;
9364                     pent->plt.offset = s->size;
9365                     s->size += PLT_ENTRY_SIZE (htab);
9366                     s = htab->elf.irelplt;
9367                   }
9368                 else
9369                   {
9370                     s = htab->pltlocal;
9371                     pent->plt.offset = s->size;
9372                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9373                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9374                   }
9375               }
9376             else
9377               {
9378                 /* If this is the first .plt entry, make room for the special
9379                    first entry.  */
9380                 s = htab->elf.splt;
9381                 if (s->size == 0)
9382                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9383
9384                 pent->plt.offset = s->size;
9385
9386                 /* Make room for this entry.  */
9387                 s->size += PLT_ENTRY_SIZE (htab);
9388
9389                 /* Make room for the .glink code.  */
9390                 s = htab->glink;
9391                 if (s->size == 0)
9392                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9393                 if (htab->opd_abi)
9394                   {
9395                     /* We need bigger stubs past index 32767.  */
9396                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9397                       s->size += 4;
9398                     s->size += 2*4;
9399                   }
9400                 else
9401                   s->size += 4;
9402
9403                 /* We also need to make an entry in the .rela.plt section.  */
9404                 s = htab->elf.srelplt;
9405               }
9406             if (s != NULL)
9407               s->size += sizeof (Elf64_External_Rela);
9408             doneone = TRUE;
9409           }
9410         else
9411           pent->plt.offset = (bfd_vma) -1;
9412       if (!doneone)
9413         {
9414           h->plt.plist = NULL;
9415           h->needs_plt = 0;
9416         }
9417     }
9418   else
9419     {
9420       h->plt.plist = NULL;
9421       h->needs_plt = 0;
9422     }
9423
9424   return TRUE;
9425 }
9426
9427 #define PPC_LO(v) ((v) & 0xffff)
9428 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9429 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9430
9431 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9432    to set up space for global entry stubs.  These are put in glink,
9433    after the branch table.  */
9434
9435 static bfd_boolean
9436 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9437 {
9438   struct bfd_link_info *info;
9439   struct ppc_link_hash_table *htab;
9440   struct plt_entry *pent;
9441   asection *s, *plt;
9442
9443   if (h->root.type == bfd_link_hash_indirect)
9444     return TRUE;
9445
9446   if (!h->pointer_equality_needed)
9447     return TRUE;
9448
9449   if (h->def_regular)
9450     return TRUE;
9451
9452   info = inf;
9453   htab = ppc_hash_table (info);
9454   if (htab == NULL)
9455     return FALSE;
9456
9457   s = htab->global_entry;
9458   plt = htab->elf.splt;
9459   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9460     if (pent->plt.offset != (bfd_vma) -1
9461         && pent->addend == 0)
9462       {
9463         /* For ELFv2, if this symbol is not defined in a regular file
9464            and we are not generating a shared library or pie, then we
9465            need to define the symbol in the executable on a call stub.
9466            This is to avoid text relocations.  */
9467         bfd_vma off, stub_align, stub_off, stub_size;
9468         unsigned int align_power;
9469
9470         stub_size = 16;
9471         stub_off = s->size;
9472         if (htab->params->plt_stub_align >= 0)
9473           align_power = htab->params->plt_stub_align;
9474         else
9475           align_power = -htab->params->plt_stub_align;
9476         /* Setting section alignment is delayed until we know it is
9477            non-empty.  Otherwise the .text output section will be
9478            aligned at least to plt_stub_align even when no global
9479            entry stubs are needed.  */
9480         if (s->alignment_power < align_power)
9481           s->alignment_power = align_power;
9482         stub_align = (bfd_vma) 1 << align_power;
9483         if (htab->params->plt_stub_align >= 0
9484             || ((((stub_off + stub_size - 1) & -stub_align)
9485                  - (stub_off & -stub_align))
9486                 > ((stub_size - 1) & -stub_align)))
9487           stub_off = (stub_off + stub_align - 1) & -stub_align;
9488         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9489         off -= stub_off + s->output_offset + s->output_section->vma;
9490         /* Note that for --plt-stub-align negative we have a possible
9491            dependency between stub offset and size.  Break that
9492            dependency by assuming the max stub size when calculating
9493            the stub offset.  */
9494         if (PPC_HA (off) == 0)
9495           stub_size -= 4;
9496         h->root.type = bfd_link_hash_defined;
9497         h->root.u.def.section = s;
9498         h->root.u.def.value = stub_off;
9499         s->size = stub_off + stub_size;
9500         break;
9501       }
9502   return TRUE;
9503 }
9504
9505 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9506    read-only sections.  */
9507
9508 static bfd_boolean
9509 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9510 {
9511   asection *sec;
9512
9513   if (h->root.type == bfd_link_hash_indirect)
9514     return TRUE;
9515
9516   sec = readonly_dynrelocs (h);
9517   if (sec != NULL)
9518     {
9519       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9520
9521       info->flags |= DF_TEXTREL;
9522       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9523                                 " in read-only section `%pA'\n"),
9524                               sec->owner, h->root.root.string, sec);
9525
9526       /* Not an error, just cut short the traversal.  */
9527       return FALSE;
9528     }
9529   return TRUE;
9530 }
9531
9532 /* Set the sizes of the dynamic sections.  */
9533
9534 static bfd_boolean
9535 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9536                                  struct bfd_link_info *info)
9537 {
9538   struct ppc_link_hash_table *htab;
9539   bfd *dynobj;
9540   asection *s;
9541   bfd_boolean relocs;
9542   bfd *ibfd;
9543   struct got_entry *first_tlsld;
9544
9545   htab = ppc_hash_table (info);
9546   if (htab == NULL)
9547     return FALSE;
9548
9549   dynobj = htab->elf.dynobj;
9550   if (dynobj == NULL)
9551     abort ();
9552
9553   if (htab->elf.dynamic_sections_created)
9554     {
9555       /* Set the contents of the .interp section to the interpreter.  */
9556       if (bfd_link_executable (info) && !info->nointerp)
9557         {
9558           s = bfd_get_linker_section (dynobj, ".interp");
9559           if (s == NULL)
9560             abort ();
9561           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9562           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9563         }
9564     }
9565
9566   /* Set up .got offsets for local syms, and space for local dynamic
9567      relocs.  */
9568   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9569     {
9570       struct got_entry **lgot_ents;
9571       struct got_entry **end_lgot_ents;
9572       struct plt_entry **local_plt;
9573       struct plt_entry **end_local_plt;
9574       unsigned char *lgot_masks;
9575       bfd_size_type locsymcount;
9576       Elf_Internal_Shdr *symtab_hdr;
9577
9578       if (!is_ppc64_elf (ibfd))
9579         continue;
9580
9581       for (s = ibfd->sections; s != NULL; s = s->next)
9582         {
9583           struct ppc_dyn_relocs *p;
9584
9585           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9586             {
9587               if (!bfd_is_abs_section (p->sec)
9588                   && bfd_is_abs_section (p->sec->output_section))
9589                 {
9590                   /* Input section has been discarded, either because
9591                      it is a copy of a linkonce section or due to
9592                      linker script /DISCARD/, so we'll be discarding
9593                      the relocs too.  */
9594                 }
9595               else if (p->count != 0)
9596                 {
9597                   asection *srel = elf_section_data (p->sec)->sreloc;
9598                   if (p->ifunc)
9599                     srel = htab->elf.irelplt;
9600                   srel->size += p->count * sizeof (Elf64_External_Rela);
9601                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9602                     info->flags |= DF_TEXTREL;
9603                 }
9604             }
9605         }
9606
9607       lgot_ents = elf_local_got_ents (ibfd);
9608       if (!lgot_ents)
9609         continue;
9610
9611       symtab_hdr = &elf_symtab_hdr (ibfd);
9612       locsymcount = symtab_hdr->sh_info;
9613       end_lgot_ents = lgot_ents + locsymcount;
9614       local_plt = (struct plt_entry **) end_lgot_ents;
9615       end_local_plt = local_plt + locsymcount;
9616       lgot_masks = (unsigned char *) end_local_plt;
9617       s = ppc64_elf_tdata (ibfd)->got;
9618       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9619         {
9620           struct got_entry **pent, *ent;
9621
9622           pent = lgot_ents;
9623           while ((ent = *pent) != NULL)
9624             if (ent->got.refcount > 0)
9625               {
9626                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9627                   {
9628                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9629                     *pent = ent->next;
9630                   }
9631                 else
9632                   {
9633                     unsigned int ent_size = 8;
9634                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9635
9636                     ent->got.offset = s->size;
9637                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9638                       {
9639                         ent_size *= 2;
9640                         rel_size *= 2;
9641                       }
9642                     s->size += ent_size;
9643                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9644                       {
9645                         htab->elf.irelplt->size += rel_size;
9646                         htab->got_reli_size += rel_size;
9647                       }
9648                     else if (bfd_link_pic (info)
9649                              && !((ent->tls_type & TLS_TPREL) != 0
9650                                   && bfd_link_executable (info)))
9651                       {
9652                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9653                         srel->size += rel_size;
9654                       }
9655                     pent = &ent->next;
9656                   }
9657               }
9658             else
9659               *pent = ent->next;
9660         }
9661
9662       /* Allocate space for plt calls to local syms.  */
9663       lgot_masks = (unsigned char *) end_local_plt;
9664       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9665         {
9666           struct plt_entry *ent;
9667
9668           for (ent = *local_plt; ent != NULL; ent = ent->next)
9669             if (ent->plt.refcount > 0)
9670               {
9671                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9672                   {
9673                     s = htab->elf.iplt;
9674                     ent->plt.offset = s->size;
9675                     s->size += PLT_ENTRY_SIZE (htab);
9676                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9677                   }
9678                 else if (htab->can_convert_all_inline_plt
9679                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9680                   ent->plt.offset = (bfd_vma) -1;
9681                 else
9682                   {
9683                     s = htab->pltlocal;
9684                     ent->plt.offset = s->size;
9685                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9686                     if (bfd_link_pic (info))
9687                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9688                   }
9689               }
9690             else
9691               ent->plt.offset = (bfd_vma) -1;
9692         }
9693     }
9694
9695   /* Allocate global sym .plt and .got entries, and space for global
9696      sym dynamic relocs.  */
9697   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9698
9699   if (!htab->opd_abi && !bfd_link_pic (info))
9700     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9701
9702   first_tlsld = NULL;
9703   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9704     {
9705       struct got_entry *ent;
9706
9707       if (!is_ppc64_elf (ibfd))
9708         continue;
9709
9710       ent = ppc64_tlsld_got (ibfd);
9711       if (ent->got.refcount > 0)
9712         {
9713           if (!htab->do_multi_toc && first_tlsld != NULL)
9714             {
9715               ent->is_indirect = TRUE;
9716               ent->got.ent = first_tlsld;
9717             }
9718           else
9719             {
9720               if (first_tlsld == NULL)
9721                 first_tlsld = ent;
9722               s = ppc64_elf_tdata (ibfd)->got;
9723               ent->got.offset = s->size;
9724               ent->owner = ibfd;
9725               s->size += 16;
9726               if (bfd_link_pic (info))
9727                 {
9728                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9729                   srel->size += sizeof (Elf64_External_Rela);
9730                 }
9731             }
9732         }
9733       else
9734         ent->got.offset = (bfd_vma) -1;
9735     }
9736
9737   /* We now have determined the sizes of the various dynamic sections.
9738      Allocate memory for them.  */
9739   relocs = FALSE;
9740   for (s = dynobj->sections; s != NULL; s = s->next)
9741     {
9742       if ((s->flags & SEC_LINKER_CREATED) == 0)
9743         continue;
9744
9745       if (s == htab->brlt || s == htab->relbrlt)
9746         /* These haven't been allocated yet;  don't strip.  */
9747         continue;
9748       else if (s == htab->elf.sgot
9749                || s == htab->elf.splt
9750                || s == htab->elf.iplt
9751                || s == htab->pltlocal
9752                || s == htab->glink
9753                || s == htab->global_entry
9754                || s == htab->elf.sdynbss
9755                || s == htab->elf.sdynrelro)
9756         {
9757           /* Strip this section if we don't need it; see the
9758              comment below.  */
9759         }
9760       else if (s == htab->glink_eh_frame)
9761         {
9762           if (!bfd_is_abs_section (s->output_section))
9763             /* Not sized yet.  */
9764             continue;
9765         }
9766       else if (CONST_STRNEQ (s->name, ".rela"))
9767         {
9768           if (s->size != 0)
9769             {
9770               if (s != htab->elf.srelplt)
9771                 relocs = TRUE;
9772
9773               /* We use the reloc_count field as a counter if we need
9774                  to copy relocs into the output file.  */
9775               s->reloc_count = 0;
9776             }
9777         }
9778       else
9779         {
9780           /* It's not one of our sections, so don't allocate space.  */
9781           continue;
9782         }
9783
9784       if (s->size == 0)
9785         {
9786           /* If we don't need this section, strip it from the
9787              output file.  This is mostly to handle .rela.bss and
9788              .rela.plt.  We must create both sections in
9789              create_dynamic_sections, because they must be created
9790              before the linker maps input sections to output
9791              sections.  The linker does that before
9792              adjust_dynamic_symbol is called, and it is that
9793              function which decides whether anything needs to go
9794              into these sections.  */
9795           s->flags |= SEC_EXCLUDE;
9796           continue;
9797         }
9798
9799       if (bfd_is_abs_section (s->output_section))
9800         _bfd_error_handler (_("warning: discarding dynamic section %s"),
9801                             s->name);
9802
9803       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9804         continue;
9805
9806       /* Allocate memory for the section contents.  We use bfd_zalloc
9807          here in case unused entries are not reclaimed before the
9808          section's contents are written out.  This should not happen,
9809          but this way if it does we get a R_PPC64_NONE reloc in .rela
9810          sections instead of garbage.
9811          We also rely on the section contents being zero when writing
9812          the GOT and .dynrelro.  */
9813       s->contents = bfd_zalloc (dynobj, s->size);
9814       if (s->contents == NULL)
9815         return FALSE;
9816     }
9817
9818   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9819     {
9820       if (!is_ppc64_elf (ibfd))
9821         continue;
9822
9823       s = ppc64_elf_tdata (ibfd)->got;
9824       if (s != NULL && s != htab->elf.sgot)
9825         {
9826           if (s->size == 0)
9827             s->flags |= SEC_EXCLUDE;
9828           else
9829             {
9830               s->contents = bfd_zalloc (ibfd, s->size);
9831               if (s->contents == NULL)
9832                 return FALSE;
9833             }
9834         }
9835       s = ppc64_elf_tdata (ibfd)->relgot;
9836       if (s != NULL)
9837         {
9838           if (s->size == 0)
9839             s->flags |= SEC_EXCLUDE;
9840           else
9841             {
9842               s->contents = bfd_zalloc (ibfd, s->size);
9843               if (s->contents == NULL)
9844                 return FALSE;
9845               relocs = TRUE;
9846               s->reloc_count = 0;
9847             }
9848         }
9849     }
9850
9851   if (htab->elf.dynamic_sections_created)
9852     {
9853       bfd_boolean tls_opt;
9854
9855       /* Add some entries to the .dynamic section.  We fill in the
9856          values later, in ppc64_elf_finish_dynamic_sections, but we
9857          must add the entries now so that we get the correct size for
9858          the .dynamic section.  The DT_DEBUG entry is filled in by the
9859          dynamic linker and used by the debugger.  */
9860 #define add_dynamic_entry(TAG, VAL) \
9861   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9862
9863       if (bfd_link_executable (info))
9864         {
9865           if (!add_dynamic_entry (DT_DEBUG, 0))
9866             return FALSE;
9867         }
9868
9869       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9870         {
9871           if (!add_dynamic_entry (DT_PLTGOT, 0)
9872               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9873               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9874               || !add_dynamic_entry (DT_JMPREL, 0)
9875               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9876             return FALSE;
9877         }
9878
9879       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9880         {
9881           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9882               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9883             return FALSE;
9884         }
9885
9886       tls_opt = (htab->params->tls_get_addr_opt
9887                  && htab->tls_get_addr_fd != NULL
9888                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9889       if (tls_opt || !htab->opd_abi)
9890         {
9891           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9892             return FALSE;
9893         }
9894
9895       if (relocs)
9896         {
9897           if (!add_dynamic_entry (DT_RELA, 0)
9898               || !add_dynamic_entry (DT_RELASZ, 0)
9899               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9900             return FALSE;
9901
9902           /* If any dynamic relocs apply to a read-only section,
9903              then we need a DT_TEXTREL entry.  */
9904           if ((info->flags & DF_TEXTREL) == 0)
9905             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9906
9907           if ((info->flags & DF_TEXTREL) != 0)
9908             {
9909               if (!add_dynamic_entry (DT_TEXTREL, 0))
9910                 return FALSE;
9911             }
9912         }
9913     }
9914 #undef add_dynamic_entry
9915
9916   return TRUE;
9917 }
9918
9919 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
9920
9921 static bfd_boolean
9922 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9923 {
9924   if (h->plt.plist != NULL
9925       && !h->def_regular
9926       && !h->pointer_equality_needed)
9927     return FALSE;
9928
9929   return _bfd_elf_hash_symbol (h);
9930 }
9931
9932 /* Determine the type of stub needed, if any, for a call.  */
9933
9934 static inline enum ppc_stub_type
9935 ppc_type_of_stub (asection *input_sec,
9936                   const Elf_Internal_Rela *rel,
9937                   struct ppc_link_hash_entry **hash,
9938                   struct plt_entry **plt_ent,
9939                   bfd_vma destination,
9940                   unsigned long local_off)
9941 {
9942   struct ppc_link_hash_entry *h = *hash;
9943   bfd_vma location;
9944   bfd_vma branch_offset;
9945   bfd_vma max_branch_offset;
9946   enum elf_ppc64_reloc_type r_type;
9947
9948   if (h != NULL)
9949     {
9950       struct plt_entry *ent;
9951       struct ppc_link_hash_entry *fdh = h;
9952       if (h->oh != NULL
9953           && h->oh->is_func_descriptor)
9954         {
9955           fdh = ppc_follow_link (h->oh);
9956           *hash = fdh;
9957         }
9958
9959       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9960         if (ent->addend == rel->r_addend
9961             && ent->plt.offset != (bfd_vma) -1)
9962           {
9963             *plt_ent = ent;
9964             return ppc_stub_plt_call;
9965           }
9966
9967       /* Here, we know we don't have a plt entry.  If we don't have a
9968          either a defined function descriptor or a defined entry symbol
9969          in a regular object file, then it is pointless trying to make
9970          any other type of stub.  */
9971       if (!is_static_defined (&fdh->elf)
9972           && !is_static_defined (&h->elf))
9973         return ppc_stub_none;
9974     }
9975   else if (elf_local_got_ents (input_sec->owner) != NULL)
9976     {
9977       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9978       struct plt_entry **local_plt = (struct plt_entry **)
9979         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9980       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9981
9982       if (local_plt[r_symndx] != NULL)
9983         {
9984           struct plt_entry *ent;
9985
9986           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9987             if (ent->addend == rel->r_addend
9988                 && ent->plt.offset != (bfd_vma) -1)
9989               {
9990                 *plt_ent = ent;
9991                 return ppc_stub_plt_call;
9992               }
9993         }
9994     }
9995
9996   /* Determine where the call point is.  */
9997   location = (input_sec->output_offset
9998               + input_sec->output_section->vma
9999               + rel->r_offset);
10000
10001   branch_offset = destination - location;
10002   r_type = ELF64_R_TYPE (rel->r_info);
10003
10004   /* Determine if a long branch stub is needed.  */
10005   max_branch_offset = 1 << 25;
10006   if (r_type == R_PPC64_REL14
10007       || r_type == R_PPC64_REL14_BRTAKEN
10008       || r_type == R_PPC64_REL14_BRNTAKEN)
10009     max_branch_offset = 1 << 15;
10010
10011   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10012     /* We need a stub.  Figure out whether a long_branch or plt_branch
10013        is needed later.  */
10014     return ppc_stub_long_branch;
10015
10016   return ppc_stub_none;
10017 }
10018
10019 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10020    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10021    .    mflr    %r12
10022    .    bcl     20,31,1f
10023    .1:  mflr    %r11
10024    .    mtlr    %r12
10025    .    lis     %r12,xxx-1b@highest
10026    .    ori     %r12,%r12,xxx-1b@higher
10027    .    sldi    %r12,%r12,32
10028    .    oris    %r12,%r12,xxx-1b@high
10029    .    ori     %r12,%r12,xxx-1b@l
10030    .    add/ldx %r12,%r11,%r12  */
10031
10032 static bfd_byte *
10033 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10034 {
10035   bfd_put_32 (abfd, MFLR_R12, p);
10036   p += 4;
10037   bfd_put_32 (abfd, BCL_20_31, p);
10038   p += 4;
10039   bfd_put_32 (abfd, MFLR_R11, p);
10040   p += 4;
10041   bfd_put_32 (abfd, MTLR_R12, p);
10042   p += 4;
10043   if (off + 0x8000 < 0x10000)
10044     {
10045       if (load)
10046         bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10047       else
10048         bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10049       p += 4;
10050     }
10051   else if (off + 0x80008000ULL < 0x100000000ULL)
10052     {
10053       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10054       p += 4;
10055       if (load)
10056         bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10057       else
10058         bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10059       p += 4;
10060     }
10061   else
10062     {
10063       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10064         {
10065           bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10066           p += 4;
10067         }
10068       else
10069         {
10070           bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10071           p += 4;
10072           if (((off >> 32) & 0xffff) != 0)
10073             {
10074               bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10075               p += 4;
10076             }
10077         }
10078       if (((off >> 32) & 0xffffffffULL) != 0)
10079         {
10080           bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10081           p += 4;
10082         }
10083       if (PPC_HI (off) != 0)
10084         {
10085           bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10086           p += 4;
10087         }
10088       if (PPC_LO (off) != 0)
10089         {
10090           bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10091           p += 4;
10092         }
10093       if (load)
10094         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10095       else
10096         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10097       p += 4;
10098     }
10099   return p;
10100 }
10101
10102 static unsigned int
10103 size_offset (bfd_vma off)
10104 {
10105   unsigned int size;
10106   if (off + 0x8000 < 0x10000)
10107     size = 4;
10108   else if (off + 0x80008000ULL < 0x100000000ULL)
10109     size = 8;
10110   else
10111     {
10112       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10113         size = 4;
10114       else
10115         {
10116           size = 4;
10117           if (((off >> 32) & 0xffff) != 0)
10118             size += 4;
10119         }
10120       if (((off >> 32) & 0xffffffffULL) != 0)
10121         size += 4;
10122       if (PPC_HI (off) != 0)
10123         size += 4;
10124       if (PPC_LO (off) != 0)
10125         size += 4;
10126       size += 4;
10127     }
10128   return size + 16;
10129 }
10130
10131 static unsigned int
10132 num_relocs_for_offset (bfd_vma off)
10133 {
10134   unsigned int num_rel;
10135   if (off + 0x8000 < 0x10000)
10136     num_rel = 1;
10137   else if (off + 0x80008000ULL < 0x100000000ULL)
10138     num_rel = 2;
10139   else
10140     {
10141       num_rel = 1;
10142       if (off + 0x800000000000ULL >= 0x1000000000000ULL
10143           && ((off >> 32) & 0xffff) != 0)
10144         num_rel += 1;
10145       if (PPC_HI (off) != 0)
10146         num_rel += 1;
10147       if (PPC_LO (off) != 0)
10148         num_rel += 1;
10149     }
10150   return num_rel;
10151 }
10152
10153 static Elf_Internal_Rela *
10154 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10155                         bfd_vma roff, bfd_vma targ, bfd_vma off)
10156 {
10157   bfd_vma relative_targ = targ - (roff - 8);
10158   if (bfd_big_endian (info->output_bfd))
10159     roff += 2;
10160   r->r_offset = roff;
10161   r->r_addend = relative_targ + roff;
10162   if (off + 0x8000 < 0x10000)
10163     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10164   else if (off + 0x80008000ULL < 0x100000000ULL)
10165     {
10166       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10167       ++r;
10168       roff += 4;
10169       r->r_offset = roff;
10170       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10171       r->r_addend = relative_targ + roff;
10172     }
10173   else
10174     {
10175       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10176         r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10177       else
10178         {
10179           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10180           if (((off >> 32) & 0xffff) != 0)
10181             {
10182               ++r;
10183               roff += 4;
10184               r->r_offset = roff;
10185               r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10186               r->r_addend = relative_targ + roff;
10187             }
10188         }
10189       if (((off >> 32) & 0xffffffffULL) != 0)
10190         roff += 4;
10191       if (PPC_HI (off) != 0)
10192         {
10193           ++r;
10194           roff += 4;
10195           r->r_offset = roff;
10196           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10197           r->r_addend = relative_targ + roff;
10198         }
10199       if (PPC_LO (off) != 0)
10200         {
10201           ++r;
10202           roff += 4;
10203           r->r_offset = roff;
10204           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10205           r->r_addend = relative_targ + roff;
10206         }
10207     }
10208   return r;
10209 }
10210
10211 /* Emit .eh_frame opcode to advance pc by DELTA.  */
10212
10213 static bfd_byte *
10214 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10215 {
10216   delta /= 4;
10217   if (delta < 64)
10218     *eh++ = DW_CFA_advance_loc + delta;
10219   else if (delta < 256)
10220     {
10221       *eh++ = DW_CFA_advance_loc1;
10222       *eh++ = delta;
10223     }
10224   else if (delta < 65536)
10225     {
10226       *eh++ = DW_CFA_advance_loc2;
10227       bfd_put_16 (abfd, delta, eh);
10228       eh += 2;
10229     }
10230   else
10231     {
10232       *eh++ = DW_CFA_advance_loc4;
10233       bfd_put_32 (abfd, delta, eh);
10234       eh += 4;
10235     }
10236   return eh;
10237 }
10238
10239 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
10240
10241 static unsigned int
10242 eh_advance_size (unsigned int delta)
10243 {
10244   if (delta < 64 * 4)
10245     /* DW_CFA_advance_loc+[1..63].  */
10246     return 1;
10247   if (delta < 256 * 4)
10248     /* DW_CFA_advance_loc1, byte.  */
10249     return 2;
10250   if (delta < 65536 * 4)
10251     /* DW_CFA_advance_loc2, 2 bytes.  */
10252     return 3;
10253   /* DW_CFA_advance_loc4, 4 bytes.  */
10254   return 5;
10255 }
10256
10257 /* With power7 weakly ordered memory model, it is possible for ld.so
10258    to update a plt entry in one thread and have another thread see a
10259    stale zero toc entry.  To avoid this we need some sort of acquire
10260    barrier in the call stub.  One solution is to make the load of the
10261    toc word seem to appear to depend on the load of the function entry
10262    word.  Another solution is to test for r2 being zero, and branch to
10263    the appropriate glink entry if so.
10264
10265    .    fake dep barrier        compare
10266    .    ld 12,xxx(2)            ld 12,xxx(2)
10267    .    mtctr 12                mtctr 12
10268    .    xor 11,12,12            ld 2,xxx+8(2)
10269    .    add 2,2,11              cmpldi 2,0
10270    .    ld 2,xxx+8(2)           bnectr+
10271    .    bctr                    b <glink_entry>
10272
10273    The solution involving the compare turns out to be faster, so
10274    that's what we use unless the branch won't reach.  */
10275
10276 #define ALWAYS_USE_FAKE_DEP 0
10277 #define ALWAYS_EMIT_R2SAVE 0
10278
10279 static inline unsigned int
10280 plt_stub_size (struct ppc_link_hash_table *htab,
10281                struct ppc_stub_hash_entry *stub_entry,
10282                bfd_vma off)
10283 {
10284   unsigned size;
10285
10286   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10287     {
10288       size = 8 + size_offset (off - 8);
10289       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10290         size += 4;
10291       return size;
10292     }
10293
10294   size = 12;
10295   if (ALWAYS_EMIT_R2SAVE
10296       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10297     size += 4;
10298   if (PPC_HA (off) != 0)
10299     size += 4;
10300   if (htab->opd_abi)
10301     {
10302       size += 4;
10303       if (htab->params->plt_static_chain)
10304         size += 4;
10305       if (htab->params->plt_thread_safe
10306           && htab->elf.dynamic_sections_created
10307           && stub_entry->h != NULL
10308           && stub_entry->h->elf.dynindx != -1)
10309         size += 8;
10310       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10311         size += 4;
10312     }
10313   if (stub_entry->h != NULL
10314       && (stub_entry->h == htab->tls_get_addr_fd
10315           || stub_entry->h == htab->tls_get_addr)
10316       && htab->params->tls_get_addr_opt)
10317     {
10318       size += 7 * 4;
10319       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10320         size += 6 * 4;
10321     }
10322   return size;
10323 }
10324
10325 /* Depending on the sign of plt_stub_align:
10326    If positive, return the padding to align to a 2**plt_stub_align
10327    boundary.
10328    If negative, if this stub would cross fewer 2**plt_stub_align
10329    boundaries if we align, then return the padding needed to do so.  */
10330
10331 static inline unsigned int
10332 plt_stub_pad (struct ppc_link_hash_table *htab,
10333               struct ppc_stub_hash_entry *stub_entry,
10334               bfd_vma plt_off)
10335 {
10336   int stub_align;
10337   unsigned stub_size;
10338   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10339
10340   if (htab->params->plt_stub_align >= 0)
10341     {
10342       stub_align = 1 << htab->params->plt_stub_align;
10343       if ((stub_off & (stub_align - 1)) != 0)
10344         return stub_align - (stub_off & (stub_align - 1));
10345       return 0;
10346     }
10347
10348   stub_align = 1 << -htab->params->plt_stub_align;
10349   stub_size = plt_stub_size (htab, stub_entry, plt_off);
10350   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10351       > ((stub_size - 1) & -stub_align))
10352     return stub_align - (stub_off & (stub_align - 1));
10353   return 0;
10354 }
10355
10356 /* Build a .plt call stub.  */
10357
10358 static inline bfd_byte *
10359 build_plt_stub (struct ppc_link_hash_table *htab,
10360                 struct ppc_stub_hash_entry *stub_entry,
10361                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10362 {
10363   bfd *obfd = htab->params->stub_bfd;
10364   bfd_boolean plt_load_toc = htab->opd_abi;
10365   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10366   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10367                                  && htab->elf.dynamic_sections_created
10368                                  && stub_entry->h != NULL
10369                                  && stub_entry->h->elf.dynindx != -1);
10370   bfd_boolean use_fake_dep = plt_thread_safe;
10371   bfd_vma cmp_branch_off = 0;
10372
10373   if (!ALWAYS_USE_FAKE_DEP
10374       && plt_load_toc
10375       && plt_thread_safe
10376       && !((stub_entry->h == htab->tls_get_addr_fd
10377             || stub_entry->h == htab->tls_get_addr)
10378            && htab->params->tls_get_addr_opt))
10379     {
10380       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10381       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10382                           / PLT_ENTRY_SIZE (htab));
10383       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10384       bfd_vma to, from;
10385
10386       if (pltindex > 32768)
10387         glinkoff += (pltindex - 32768) * 4;
10388       to = (glinkoff
10389             + htab->glink->output_offset
10390             + htab->glink->output_section->vma);
10391       from = (p - stub_entry->group->stub_sec->contents
10392               + 4 * (ALWAYS_EMIT_R2SAVE
10393                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10394               + 4 * (PPC_HA (offset) != 0)
10395               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10396                      != PPC_HA (offset))
10397               + 4 * (plt_static_chain != 0)
10398               + 20
10399               + stub_entry->group->stub_sec->output_offset
10400               + stub_entry->group->stub_sec->output_section->vma);
10401       cmp_branch_off = to - from;
10402       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10403     }
10404
10405   if (PPC_HA (offset) != 0)
10406     {
10407       if (r != NULL)
10408         {
10409           if (ALWAYS_EMIT_R2SAVE
10410               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10411             r[0].r_offset += 4;
10412           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10413           r[1].r_offset = r[0].r_offset + 4;
10414           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10415           r[1].r_addend = r[0].r_addend;
10416           if (plt_load_toc)
10417             {
10418               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10419                 {
10420                   r[2].r_offset = r[1].r_offset + 4;
10421                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10422                   r[2].r_addend = r[0].r_addend;
10423                 }
10424               else
10425                 {
10426                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10427                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10428                   r[2].r_addend = r[0].r_addend + 8;
10429                   if (plt_static_chain)
10430                     {
10431                       r[3].r_offset = r[2].r_offset + 4;
10432                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10433                       r[3].r_addend = r[0].r_addend + 16;
10434                     }
10435                 }
10436             }
10437         }
10438       if (ALWAYS_EMIT_R2SAVE
10439           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10440         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10441       if (plt_load_toc)
10442         {
10443           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10444           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10445         }
10446       else
10447         {
10448           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10449           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10450         }
10451       if (plt_load_toc
10452           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10453         {
10454           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10455           offset = 0;
10456         }
10457       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10458       if (plt_load_toc)
10459         {
10460           if (use_fake_dep)
10461             {
10462               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10463               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10464             }
10465           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10466           if (plt_static_chain)
10467             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10468         }
10469     }
10470   else
10471     {
10472       if (r != NULL)
10473         {
10474           if (ALWAYS_EMIT_R2SAVE
10475               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10476             r[0].r_offset += 4;
10477           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10478           if (plt_load_toc)
10479             {
10480               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10481                 {
10482                   r[1].r_offset = r[0].r_offset + 4;
10483                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10484                   r[1].r_addend = r[0].r_addend;
10485                 }
10486               else
10487                 {
10488                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10489                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10490                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10491                   if (plt_static_chain)
10492                     {
10493                       r[2].r_offset = r[1].r_offset + 4;
10494                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10495                       r[2].r_addend = r[0].r_addend + 8;
10496                     }
10497                 }
10498             }
10499         }
10500       if (ALWAYS_EMIT_R2SAVE
10501           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10502         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10503       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10504       if (plt_load_toc
10505           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10506         {
10507           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10508           offset = 0;
10509         }
10510       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10511       if (plt_load_toc)
10512         {
10513           if (use_fake_dep)
10514             {
10515               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10516               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10517             }
10518           if (plt_static_chain)
10519             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10520           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10521         }
10522     }
10523   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10524     {
10525       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10526       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10527       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10528     }
10529   else
10530     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10531   return p;
10532 }
10533
10534 /* Build a special .plt call stub for __tls_get_addr.  */
10535
10536 #define LD_R11_0R3      0xe9630000
10537 #define LD_R12_0R3      0xe9830000
10538 #define MR_R0_R3        0x7c601b78
10539 #define CMPDI_R11_0     0x2c2b0000
10540 #define ADD_R3_R12_R13  0x7c6c6a14
10541 #define BEQLR           0x4d820020
10542 #define MR_R3_R0        0x7c030378
10543 #define STD_R11_0R1     0xf9610000
10544 #define BCTRL           0x4e800421
10545 #define LD_R11_0R1      0xe9610000
10546 #define MTLR_R11        0x7d6803a6
10547
10548 static inline bfd_byte *
10549 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10550                          struct ppc_stub_hash_entry *stub_entry,
10551                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10552 {
10553   bfd *obfd = htab->params->stub_bfd;
10554   bfd_byte *loc = p;
10555
10556   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10557   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10558   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10559   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10560   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10561   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10562   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10563   if (r != NULL)
10564     r[0].r_offset += 7 * 4;
10565   if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10566     return build_plt_stub (htab, stub_entry, p, offset, r);
10567
10568   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10569   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10570
10571   if (r != NULL)
10572     r[0].r_offset += 2 * 4;
10573   p = build_plt_stub (htab, stub_entry, p, offset, r);
10574   bfd_put_32 (obfd, BCTRL, p - 4);
10575
10576   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10577   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10578   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10579   bfd_put_32 (obfd, BLR, p),                    p += 4;
10580
10581   if (htab->glink_eh_frame != NULL
10582       && htab->glink_eh_frame->size != 0)
10583     {
10584       bfd_byte *base, *eh;
10585       unsigned int lr_used, delta;
10586
10587       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10588       eh = base + stub_entry->group->eh_size;
10589       lr_used = stub_entry->stub_offset + (p - 20 - loc);
10590       delta = lr_used - stub_entry->group->lr_restore;
10591       stub_entry->group->lr_restore = lr_used + 16;
10592       eh = eh_advance (htab->elf.dynobj, eh, delta);
10593       *eh++ = DW_CFA_offset_extended_sf;
10594       *eh++ = 65;
10595       *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10596       *eh++ = DW_CFA_advance_loc + 4;
10597       *eh++ = DW_CFA_restore_extended;
10598       *eh++ = 65;
10599       stub_entry->group->eh_size = eh - base;
10600     }
10601   return p;
10602 }
10603
10604 static Elf_Internal_Rela *
10605 get_relocs (asection *sec, int count)
10606 {
10607   Elf_Internal_Rela *relocs;
10608   struct bfd_elf_section_data *elfsec_data;
10609
10610   elfsec_data = elf_section_data (sec);
10611   relocs = elfsec_data->relocs;
10612   if (relocs == NULL)
10613     {
10614       bfd_size_type relsize;
10615       relsize = sec->reloc_count * sizeof (*relocs);
10616       relocs = bfd_alloc (sec->owner, relsize);
10617       if (relocs == NULL)
10618         return NULL;
10619       elfsec_data->relocs = relocs;
10620       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10621                                           sizeof (Elf_Internal_Shdr));
10622       if (elfsec_data->rela.hdr == NULL)
10623         return NULL;
10624       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10625                                         * sizeof (Elf64_External_Rela));
10626       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10627       sec->reloc_count = 0;
10628     }
10629   relocs += sec->reloc_count;
10630   sec->reloc_count += count;
10631   return relocs;
10632 }
10633
10634 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10635    forms, to the equivalent relocs against the global symbol given by
10636    STUB_ENTRY->H.  */
10637
10638 static bfd_boolean
10639 use_global_in_relocs (struct ppc_link_hash_table *htab,
10640                       struct ppc_stub_hash_entry *stub_entry,
10641                       Elf_Internal_Rela *r, unsigned int num_rel)
10642 {
10643   struct elf_link_hash_entry **hashes;
10644   unsigned long symndx;
10645   struct ppc_link_hash_entry *h;
10646   bfd_vma symval;
10647
10648   /* Relocs are always against symbols in their own object file.  Fake
10649      up global sym hashes for the stub bfd (which has no symbols).  */
10650   hashes = elf_sym_hashes (htab->params->stub_bfd);
10651   if (hashes == NULL)
10652     {
10653       bfd_size_type hsize;
10654
10655       /* When called the first time, stub_globals will contain the
10656          total number of symbols seen during stub sizing.  After
10657          allocating, stub_globals is used as an index to fill the
10658          hashes array.  */
10659       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10660       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10661       if (hashes == NULL)
10662         return FALSE;
10663       elf_sym_hashes (htab->params->stub_bfd) = hashes;
10664       htab->stub_globals = 1;
10665     }
10666   symndx = htab->stub_globals++;
10667   h = stub_entry->h;
10668   hashes[symndx] = &h->elf;
10669   if (h->oh != NULL && h->oh->is_func)
10670     h = ppc_follow_link (h->oh);
10671   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10672               || h->elf.root.type == bfd_link_hash_defweak);
10673   symval = (h->elf.root.u.def.value
10674             + h->elf.root.u.def.section->output_offset
10675             + h->elf.root.u.def.section->output_section->vma);
10676   while (num_rel-- != 0)
10677     {
10678       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10679       if (h->elf.root.u.def.section != stub_entry->target_section)
10680         {
10681           /* H is an opd symbol.  The addend must be zero, and the
10682              branch reloc is the only one we can convert.  */
10683           r->r_addend = 0;
10684           break;
10685         }
10686       else
10687         r->r_addend -= symval;
10688       --r;
10689     }
10690   return TRUE;
10691 }
10692
10693 static bfd_vma
10694 get_r2off (struct bfd_link_info *info,
10695            struct ppc_stub_hash_entry *stub_entry)
10696 {
10697   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10698   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10699
10700   if (r2off == 0)
10701     {
10702       /* Support linking -R objects.  Get the toc pointer from the
10703          opd entry.  */
10704       char buf[8];
10705       if (!htab->opd_abi)
10706         return r2off;
10707       asection *opd = stub_entry->h->elf.root.u.def.section;
10708       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10709
10710       if (strcmp (opd->name, ".opd") != 0
10711           || opd->reloc_count != 0)
10712         {
10713           info->callbacks->einfo
10714             (_("%P: cannot find opd entry toc for `%pT'\n"),
10715              stub_entry->h->elf.root.root.string);
10716           bfd_set_error (bfd_error_bad_value);
10717           return (bfd_vma) -1;
10718         }
10719       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10720         return (bfd_vma) -1;
10721       r2off = bfd_get_64 (opd->owner, buf);
10722       r2off -= elf_gp (info->output_bfd);
10723     }
10724   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10725   return r2off;
10726 }
10727
10728 static bfd_boolean
10729 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10730 {
10731   struct ppc_stub_hash_entry *stub_entry;
10732   struct ppc_branch_hash_entry *br_entry;
10733   struct bfd_link_info *info;
10734   struct ppc_link_hash_table *htab;
10735   bfd_byte *loc;
10736   bfd_byte *p, *relp;
10737   bfd_vma targ, off;
10738   Elf_Internal_Rela *r;
10739   asection *plt;
10740   int num_rel;
10741
10742   /* Massage our args to the form they really have.  */
10743   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10744   info = in_arg;
10745
10746   htab = ppc_hash_table (info);
10747   if (htab == NULL)
10748     return FALSE;
10749
10750   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10751   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10752
10753   htab->stub_count[stub_entry->stub_type - 1] += 1;
10754   switch (stub_entry->stub_type)
10755     {
10756     case ppc_stub_long_branch:
10757     case ppc_stub_long_branch_r2off:
10758       /* Branches are relative.  This is where we are going to.  */
10759       targ = (stub_entry->target_value
10760               + stub_entry->target_section->output_offset
10761               + stub_entry->target_section->output_section->vma);
10762       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10763
10764       /* And this is where we are coming from.  */
10765       off = (stub_entry->stub_offset
10766              + stub_entry->group->stub_sec->output_offset
10767              + stub_entry->group->stub_sec->output_section->vma);
10768       off = targ - off;
10769
10770       p = loc;
10771       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10772         {
10773           bfd_vma r2off = get_r2off (info, stub_entry);
10774
10775           if (r2off == (bfd_vma) -1)
10776             {
10777               htab->stub_error = TRUE;
10778               return FALSE;
10779             }
10780           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10781           p += 4;
10782           if (PPC_HA (r2off) != 0)
10783             {
10784               bfd_put_32 (htab->params->stub_bfd,
10785                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10786               p += 4;
10787             }
10788           if (PPC_LO (r2off) != 0)
10789             {
10790               bfd_put_32 (htab->params->stub_bfd,
10791                           ADDI_R2_R2 | PPC_LO (r2off), p);
10792               p += 4;
10793             }
10794           off -= p - loc;
10795         }
10796       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10797       p += 4;
10798
10799       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10800         {
10801           _bfd_error_handler
10802             (_("long branch stub `%s' offset overflow"),
10803              stub_entry->root.string);
10804           htab->stub_error = TRUE;
10805           return FALSE;
10806         }
10807
10808       if (info->emitrelocations)
10809         {
10810           r = get_relocs (stub_entry->group->stub_sec, 1);
10811           if (r == NULL)
10812             return FALSE;
10813           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10814           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10815           r->r_addend = targ;
10816           if (stub_entry->h != NULL
10817               && !use_global_in_relocs (htab, stub_entry, r, 1))
10818             return FALSE;
10819         }
10820       break;
10821
10822     case ppc_stub_plt_branch:
10823     case ppc_stub_plt_branch_r2off:
10824       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10825                                          stub_entry->root.string + 9,
10826                                          FALSE, FALSE);
10827       if (br_entry == NULL)
10828         {
10829           _bfd_error_handler (_("can't find branch stub `%s'"),
10830                               stub_entry->root.string);
10831           htab->stub_error = TRUE;
10832           return FALSE;
10833         }
10834
10835       targ = (stub_entry->target_value
10836               + stub_entry->target_section->output_offset
10837               + stub_entry->target_section->output_section->vma);
10838       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10839         targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10840
10841       bfd_put_64 (htab->brlt->owner, targ,
10842                   htab->brlt->contents + br_entry->offset);
10843
10844       if (br_entry->iter == htab->stub_iteration)
10845         {
10846           br_entry->iter = 0;
10847
10848           if (htab->relbrlt != NULL)
10849             {
10850               /* Create a reloc for the branch lookup table entry.  */
10851               Elf_Internal_Rela rela;
10852               bfd_byte *rl;
10853
10854               rela.r_offset = (br_entry->offset
10855                                + htab->brlt->output_offset
10856                                + htab->brlt->output_section->vma);
10857               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10858               rela.r_addend = targ;
10859
10860               rl = htab->relbrlt->contents;
10861               rl += (htab->relbrlt->reloc_count++
10862                      * sizeof (Elf64_External_Rela));
10863               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10864             }
10865           else if (info->emitrelocations)
10866             {
10867               r = get_relocs (htab->brlt, 1);
10868               if (r == NULL)
10869                 return FALSE;
10870               /* brlt, being SEC_LINKER_CREATED does not go through the
10871                  normal reloc processing.  Symbols and offsets are not
10872                  translated from input file to output file form, so
10873                  set up the offset per the output file.  */
10874               r->r_offset = (br_entry->offset
10875                              + htab->brlt->output_offset
10876                              + htab->brlt->output_section->vma);
10877               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10878               r->r_addend = targ;
10879             }
10880         }
10881
10882       targ = (br_entry->offset
10883               + htab->brlt->output_offset
10884               + htab->brlt->output_section->vma);
10885
10886       off = (elf_gp (info->output_bfd)
10887              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10888       off = targ - off;
10889
10890       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10891         {
10892           info->callbacks->einfo
10893             (_("%P: linkage table error against `%pT'\n"),
10894              stub_entry->root.string);
10895           bfd_set_error (bfd_error_bad_value);
10896           htab->stub_error = TRUE;
10897           return FALSE;
10898         }
10899
10900       if (info->emitrelocations)
10901         {
10902           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10903           if (r == NULL)
10904             return FALSE;
10905           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10906           if (bfd_big_endian (info->output_bfd))
10907             r[0].r_offset += 2;
10908           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10909             r[0].r_offset += 4;
10910           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10911           r[0].r_addend = targ;
10912           if (PPC_HA (off) != 0)
10913             {
10914               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10915               r[1].r_offset = r[0].r_offset + 4;
10916               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10917               r[1].r_addend = r[0].r_addend;
10918             }
10919         }
10920
10921       p = loc;
10922       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10923         {
10924           if (PPC_HA (off) != 0)
10925             {
10926               bfd_put_32 (htab->params->stub_bfd,
10927                           ADDIS_R12_R2 | PPC_HA (off), p);
10928               p += 4;
10929               bfd_put_32 (htab->params->stub_bfd,
10930                           LD_R12_0R12 | PPC_LO (off), p);
10931             }
10932           else
10933             bfd_put_32 (htab->params->stub_bfd,
10934                         LD_R12_0R2 | PPC_LO (off), p);
10935         }
10936       else
10937         {
10938           bfd_vma r2off = get_r2off (info, stub_entry);
10939
10940           if (r2off == (bfd_vma) -1)
10941             {
10942               htab->stub_error = TRUE;
10943               return FALSE;
10944             }
10945
10946           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10947           p += 4;
10948           if (PPC_HA (off) != 0)
10949             {
10950               bfd_put_32 (htab->params->stub_bfd,
10951                           ADDIS_R12_R2 | PPC_HA (off), p);
10952               p += 4;
10953               bfd_put_32 (htab->params->stub_bfd,
10954                           LD_R12_0R12 | PPC_LO (off), p);
10955             }
10956           else
10957             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10958
10959           if (PPC_HA (r2off) != 0)
10960             {
10961               p += 4;
10962               bfd_put_32 (htab->params->stub_bfd,
10963                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10964             }
10965           if (PPC_LO (r2off) != 0)
10966             {
10967               p += 4;
10968               bfd_put_32 (htab->params->stub_bfd,
10969                           ADDI_R2_R2 | PPC_LO (r2off), p);
10970             }
10971         }
10972       p += 4;
10973       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10974       p += 4;
10975       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10976       p += 4;
10977       break;
10978
10979     case ppc_stub_long_branch_notoc:
10980     case ppc_stub_long_branch_both:
10981     case ppc_stub_plt_branch_notoc:
10982     case ppc_stub_plt_branch_both:
10983     case ppc_stub_plt_call_notoc:
10984     case ppc_stub_plt_call_both:
10985       p = loc;
10986       off = (stub_entry->stub_offset
10987              + stub_entry->group->stub_sec->output_offset
10988              + stub_entry->group->stub_sec->output_section->vma);
10989       if (stub_entry->stub_type == ppc_stub_long_branch_both
10990           || stub_entry->stub_type == ppc_stub_plt_branch_both
10991           || stub_entry->stub_type == ppc_stub_plt_call_both)
10992         {
10993           off += 4;
10994           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10995           p += 4;
10996         }
10997       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10998         {
10999           targ = stub_entry->plt_ent->plt.offset & ~1;
11000           if (targ >= (bfd_vma) -2)
11001             abort ();
11002
11003           plt = htab->elf.splt;
11004           if (!htab->elf.dynamic_sections_created
11005               || stub_entry->h == NULL
11006               || stub_entry->h->elf.dynindx == -1)
11007             {
11008               if (stub_entry->symtype == STT_GNU_IFUNC)
11009                 plt = htab->elf.iplt;
11010               else
11011                 plt = htab->pltlocal;
11012             }
11013           targ += plt->output_offset + plt->output_section->vma;
11014         }
11015       else
11016         targ = (stub_entry->target_value
11017                 + stub_entry->target_section->output_offset
11018                 + stub_entry->target_section->output_section->vma);
11019       off = targ - off;
11020
11021       relp = p;
11022       num_rel = 0;
11023       /* The notoc stubs calculate their target (either a PLT entry or
11024          the global entry point of a function) relative to the PC
11025          returned by the "bcl" two instructions past the start of the
11026          sequence emitted by build_offset.  The offset is therefore 8
11027          less than calculated from the start of the sequence.  */
11028       off -= 8;
11029       p = build_offset (htab->params->stub_bfd, p, off,
11030                         stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11031       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11032         {
11033           bfd_vma from;
11034           num_rel = 1;
11035           from = (stub_entry->stub_offset
11036                   + stub_entry->group->stub_sec->output_offset
11037                   + stub_entry->group->stub_sec->output_section->vma
11038                   + (p - loc));
11039           bfd_put_32 (htab->params->stub_bfd,
11040                       B_DOT | ((targ - from) & 0x3fffffc), p);
11041         }
11042       else
11043         {
11044           bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11045           p += 4;
11046           bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11047         }
11048       p += 4;
11049
11050       if (info->emitrelocations)
11051         {
11052           bfd_vma roff;
11053           num_rel += num_relocs_for_offset (off);
11054           r = get_relocs (stub_entry->group->stub_sec, num_rel);
11055           if (r == NULL)
11056             return FALSE;
11057           roff = relp + 16 - stub_entry->group->stub_sec->contents;
11058           r = emit_relocs_for_offset (info, r, roff, targ, off);
11059           if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11060               || stub_entry->stub_type == ppc_stub_long_branch_both)
11061             {
11062               ++r;
11063               roff = p - 4 - stub_entry->group->stub_sec->contents;
11064               r->r_offset = roff;
11065               r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11066               r->r_addend = targ;
11067               if (stub_entry->h != NULL
11068                   && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11069                 return FALSE;
11070             }
11071         }
11072
11073       if (htab->glink_eh_frame != NULL
11074         && htab->glink_eh_frame->size != 0)
11075         {
11076           bfd_byte *base, *eh;
11077           unsigned int lr_used, delta;
11078
11079           base = (htab->glink_eh_frame->contents
11080                   + stub_entry->group->eh_base + 17);
11081           eh = base + stub_entry->group->eh_size;
11082           lr_used = stub_entry->stub_offset + 8;
11083           if (stub_entry->stub_type == ppc_stub_long_branch_both
11084               || stub_entry->stub_type == ppc_stub_plt_branch_both
11085               || stub_entry->stub_type == ppc_stub_plt_call_both)
11086             lr_used += 4;
11087           delta = lr_used - stub_entry->group->lr_restore;
11088           stub_entry->group->lr_restore = lr_used + 8;
11089           eh = eh_advance (htab->elf.dynobj, eh, delta);
11090           *eh++ = DW_CFA_register;
11091           *eh++ = 65;
11092           *eh++ = 12;
11093           *eh++ = DW_CFA_advance_loc + 2;
11094           *eh++ = DW_CFA_restore_extended;
11095           *eh++ = 65;
11096           stub_entry->group->eh_size = eh - base;
11097         }
11098       break;
11099
11100     case ppc_stub_plt_call:
11101     case ppc_stub_plt_call_r2save:
11102       if (stub_entry->h != NULL
11103           && stub_entry->h->is_func_descriptor
11104           && stub_entry->h->oh != NULL)
11105         {
11106           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11107
11108           /* If the old-ABI "dot-symbol" is undefined make it weak so
11109              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11110           if (fh->elf.root.type == bfd_link_hash_undefined
11111               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11112                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11113             fh->elf.root.type = bfd_link_hash_undefweak;
11114         }
11115
11116       /* Now build the stub.  */
11117       targ = stub_entry->plt_ent->plt.offset & ~1;
11118       if (targ >= (bfd_vma) -2)
11119         abort ();
11120
11121       plt = htab->elf.splt;
11122       if (!htab->elf.dynamic_sections_created
11123           || stub_entry->h == NULL
11124           || stub_entry->h->elf.dynindx == -1)
11125         {
11126           if (stub_entry->symtype == STT_GNU_IFUNC)
11127             plt = htab->elf.iplt;
11128           else
11129             plt = htab->pltlocal;
11130         }
11131       targ += plt->output_offset + plt->output_section->vma;
11132
11133       off = (elf_gp (info->output_bfd)
11134              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11135       off = targ - off;
11136
11137       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11138         {
11139           info->callbacks->einfo
11140             /* xgettext:c-format */
11141             (_("%P: linkage table error against `%pT'\n"),
11142              stub_entry->h != NULL
11143              ? stub_entry->h->elf.root.root.string
11144              : "<local sym>");
11145           bfd_set_error (bfd_error_bad_value);
11146           htab->stub_error = TRUE;
11147           return FALSE;
11148         }
11149
11150       r = NULL;
11151       if (info->emitrelocations)
11152         {
11153           r = get_relocs (stub_entry->group->stub_sec,
11154                           ((PPC_HA (off) != 0)
11155                            + (htab->opd_abi
11156                               ? 2 + (htab->params->plt_static_chain
11157                                      && PPC_HA (off + 16) == PPC_HA (off))
11158                               : 1)));
11159           if (r == NULL)
11160             return FALSE;
11161           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11162           if (bfd_big_endian (info->output_bfd))
11163             r[0].r_offset += 2;
11164           r[0].r_addend = targ;
11165         }
11166       if (stub_entry->h != NULL
11167           && (stub_entry->h == htab->tls_get_addr_fd
11168               || stub_entry->h == htab->tls_get_addr)
11169           && htab->params->tls_get_addr_opt)
11170         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11171       else
11172         p = build_plt_stub (htab, stub_entry, loc, off, r);
11173       break;
11174
11175     case ppc_stub_save_res:
11176       return TRUE;
11177
11178     default:
11179       BFD_FAIL ();
11180       return FALSE;
11181     }
11182
11183   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11184
11185   if (htab->params->emit_stub_syms)
11186     {
11187       struct elf_link_hash_entry *h;
11188       size_t len1, len2;
11189       char *name;
11190       const char *const stub_str[] = { "long_branch",
11191                                        "long_branch",
11192                                        "long_branch",
11193                                        "long_branch",
11194                                        "plt_branch",
11195                                        "plt_branch",
11196                                        "plt_branch",
11197                                        "plt_branch",
11198                                        "plt_call",
11199                                        "plt_call",
11200                                        "plt_call",
11201                                        "plt_call" };
11202
11203       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11204       len2 = strlen (stub_entry->root.string);
11205       name = bfd_malloc (len1 + len2 + 2);
11206       if (name == NULL)
11207         return FALSE;
11208       memcpy (name, stub_entry->root.string, 9);
11209       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11210       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11211       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11212       if (h == NULL)
11213         return FALSE;
11214       if (h->root.type == bfd_link_hash_new)
11215         {
11216           h->root.type = bfd_link_hash_defined;
11217           h->root.u.def.section = stub_entry->group->stub_sec;
11218           h->root.u.def.value = stub_entry->stub_offset;
11219           h->ref_regular = 1;
11220           h->def_regular = 1;
11221           h->ref_regular_nonweak = 1;
11222           h->forced_local = 1;
11223           h->non_elf = 0;
11224           h->root.linker_def = 1;
11225         }
11226     }
11227
11228   return TRUE;
11229 }
11230
11231 /* As above, but don't actually build the stub.  Just bump offset so
11232    we know stub section sizes, and select plt_branch stubs where
11233    long_branch stubs won't do.  */
11234
11235 static bfd_boolean
11236 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11237 {
11238   struct ppc_stub_hash_entry *stub_entry;
11239   struct bfd_link_info *info;
11240   struct ppc_link_hash_table *htab;
11241   asection *plt;
11242   bfd_vma targ, off, r2off;
11243   unsigned int size, extra, lr_used, delta;
11244
11245   /* Massage our args to the form they really have.  */
11246   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11247   info = in_arg;
11248
11249   htab = ppc_hash_table (info);
11250   if (htab == NULL)
11251     return FALSE;
11252
11253   /* Make a note of the offset within the stubs for this entry.  */
11254   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11255
11256   if (stub_entry->h != NULL
11257       && stub_entry->h->save_res
11258       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11259       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11260     {
11261       /* Don't make stubs to out-of-line register save/restore
11262          functions.  Instead, emit copies of the functions.  */
11263       stub_entry->group->needs_save_res = 1;
11264       stub_entry->stub_type = ppc_stub_save_res;
11265       return TRUE;
11266     }
11267
11268   switch (stub_entry->stub_type)
11269     {
11270     case ppc_stub_plt_branch:
11271     case ppc_stub_plt_branch_r2off:
11272       /* Reset the stub type from the plt branch variant in case we now
11273          can reach with a shorter stub.  */
11274       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11275       /* Fall through.  */
11276     case ppc_stub_long_branch:
11277     case ppc_stub_long_branch_r2off:
11278       targ = (stub_entry->target_value
11279               + stub_entry->target_section->output_offset
11280               + stub_entry->target_section->output_section->vma);
11281       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11282       off = (stub_entry->stub_offset
11283              + stub_entry->group->stub_sec->output_offset
11284              + stub_entry->group->stub_sec->output_section->vma);
11285
11286       size = 4;
11287       r2off = 0;
11288       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11289         {
11290           r2off = get_r2off (info, stub_entry);
11291           if (r2off == (bfd_vma) -1)
11292             {
11293               htab->stub_error = TRUE;
11294               return FALSE;
11295             }
11296           size = 8;
11297           if (PPC_HA (r2off) != 0)
11298             size += 4;
11299           if (PPC_LO (r2off) != 0)
11300             size += 4;
11301           off += size - 4;
11302         }
11303       off = targ - off;
11304
11305       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11306          Do the same for -R objects without function descriptors.  */
11307       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11308            && r2off == 0
11309            && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11310           || off + (1 << 25) >= (bfd_vma) (1 << 26))
11311         {
11312           struct ppc_branch_hash_entry *br_entry;
11313
11314           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11315                                              stub_entry->root.string + 9,
11316                                              TRUE, FALSE);
11317           if (br_entry == NULL)
11318             {
11319               _bfd_error_handler (_("can't build branch stub `%s'"),
11320                                   stub_entry->root.string);
11321               htab->stub_error = TRUE;
11322               return FALSE;
11323             }
11324
11325           if (br_entry->iter != htab->stub_iteration)
11326             {
11327               br_entry->iter = htab->stub_iteration;
11328               br_entry->offset = htab->brlt->size;
11329               htab->brlt->size += 8;
11330
11331               if (htab->relbrlt != NULL)
11332                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11333               else if (info->emitrelocations)
11334                 {
11335                   htab->brlt->reloc_count += 1;
11336                   htab->brlt->flags |= SEC_RELOC;
11337                 }
11338             }
11339
11340           targ = (br_entry->offset
11341                   + htab->brlt->output_offset
11342                   + htab->brlt->output_section->vma);
11343           off = (elf_gp (info->output_bfd)
11344                  + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11345           off = targ - off;
11346
11347           if (info->emitrelocations)
11348             {
11349               stub_entry->group->stub_sec->reloc_count
11350                 += 1 + (PPC_HA (off) != 0);
11351               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11352             }
11353
11354           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11355           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11356             {
11357               size = 12;
11358               if (PPC_HA (off) != 0)
11359                 size = 16;
11360             }
11361           else
11362             {
11363               size = 16;
11364               if (PPC_HA (off) != 0)
11365                 size += 4;
11366
11367               if (PPC_HA (r2off) != 0)
11368                 size += 4;
11369               if (PPC_LO (r2off) != 0)
11370                 size += 4;
11371             }
11372         }
11373       else if (info->emitrelocations)
11374         {
11375           stub_entry->group->stub_sec->reloc_count += 1;
11376           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11377         }
11378       break;
11379
11380     case ppc_stub_plt_branch_notoc:
11381     case ppc_stub_plt_branch_both:
11382       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11383       /* Fall through.  */
11384     case ppc_stub_long_branch_notoc:
11385     case ppc_stub_long_branch_both:
11386       off = (stub_entry->stub_offset
11387              + stub_entry->group->stub_sec->output_offset
11388              + stub_entry->group->stub_sec->output_section->vma);
11389       size = 0;
11390       if (stub_entry->stub_type == ppc_stub_long_branch_both)
11391         size = 4;
11392       off += size;
11393       targ = (stub_entry->target_value
11394               + stub_entry->target_section->output_offset
11395               + stub_entry->target_section->output_section->vma);
11396       off = targ - off;
11397
11398       if (info->emitrelocations)
11399         {
11400           stub_entry->group->stub_sec->reloc_count
11401             += num_relocs_for_offset (off);
11402           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11403         }
11404
11405       extra = size_offset (off - 8);
11406       /* Include branch insn plus those in the offset sequence.  */
11407       size += 4 + extra;
11408       /* The branch insn is at the end, or "extra" bytes along.  So
11409          its offset will be "extra" bytes less that that already
11410          calculated.  */
11411       off -= extra;
11412
11413       /* After the bcl, lr has been modified so we need to emit
11414          .eh_frame info saying the return address is in r12.  */
11415       lr_used = stub_entry->stub_offset + 8;
11416       if (stub_entry->stub_type == ppc_stub_long_branch_both)
11417         lr_used += 4;
11418       /* The eh_frame info will consist of a DW_CFA_advance_loc or
11419          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11420          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 = lr_used + 8;
11424
11425       /* If the branch can't reach, use a plt_branch.  */
11426       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11427         {
11428           stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11429                                     - ppc_stub_long_branch_notoc);
11430           size += 4;
11431         }
11432       else if (info->emitrelocations)
11433         stub_entry->group->stub_sec->reloc_count +=1;
11434       break;
11435
11436     case ppc_stub_plt_call_notoc:
11437     case ppc_stub_plt_call_both:
11438       off = (stub_entry->stub_offset
11439              + stub_entry->group->stub_sec->output_offset
11440              + stub_entry->group->stub_sec->output_section->vma);
11441       if (stub_entry->stub_type == ppc_stub_plt_call_both)
11442         off += 4;
11443       targ = stub_entry->plt_ent->plt.offset & ~1;
11444       if (targ >= (bfd_vma) -2)
11445         abort ();
11446
11447       plt = htab->elf.splt;
11448       if (!htab->elf.dynamic_sections_created
11449           || stub_entry->h == NULL
11450           || stub_entry->h->elf.dynindx == -1)
11451         {
11452           if (stub_entry->symtype == STT_GNU_IFUNC)
11453             plt = htab->elf.iplt;
11454           else
11455             plt = htab->pltlocal;
11456         }
11457       targ += plt->output_offset + plt->output_section->vma;
11458       off = targ - off;
11459
11460       if (htab->params->plt_stub_align != 0)
11461         {
11462           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11463
11464           stub_entry->group->stub_sec->size += pad;
11465           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11466           off -= pad;
11467         }
11468
11469       if (info->emitrelocations)
11470         {
11471           stub_entry->group->stub_sec->reloc_count
11472             += num_relocs_for_offset (off - 8);
11473           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11474         }
11475
11476       size = plt_stub_size (htab, stub_entry, off);
11477
11478       /* After the bcl, lr has been modified so we need to emit
11479          .eh_frame info saying the return address is in r12.  */
11480       lr_used = stub_entry->stub_offset + 8;
11481       if (stub_entry->stub_type == ppc_stub_plt_call_both)
11482         lr_used += 4;
11483       /* The eh_frame info will consist of a DW_CFA_advance_loc or
11484          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11485          DW_CFA_restore_extended 65.  */
11486       delta = lr_used - stub_entry->group->lr_restore;
11487       stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11488       stub_entry->group->lr_restore = lr_used + 8;
11489       break;
11490
11491     case ppc_stub_plt_call:
11492     case ppc_stub_plt_call_r2save:
11493       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11494       if (targ >= (bfd_vma) -2)
11495         abort ();
11496       plt = htab->elf.splt;
11497       if (!htab->elf.dynamic_sections_created
11498           || stub_entry->h == NULL
11499           || stub_entry->h->elf.dynindx == -1)
11500         {
11501           if (stub_entry->symtype == STT_GNU_IFUNC)
11502             plt = htab->elf.iplt;
11503           else
11504             plt = htab->pltlocal;
11505         }
11506       targ += plt->output_offset + plt->output_section->vma;
11507
11508       off = (elf_gp (info->output_bfd)
11509              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11510       off = targ - off;
11511
11512       if (htab->params->plt_stub_align != 0)
11513         {
11514           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11515
11516           stub_entry->group->stub_sec->size += pad;
11517           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11518         }
11519
11520       if (info->emitrelocations)
11521         {
11522           stub_entry->group->stub_sec->reloc_count
11523             += ((PPC_HA (off) != 0)
11524                 + (htab->opd_abi
11525                    ? 2 + (htab->params->plt_static_chain
11526                           && PPC_HA (off + 16) == PPC_HA (off))
11527                    : 1));
11528           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11529         }
11530
11531       size = plt_stub_size (htab, stub_entry, off);
11532
11533       if (stub_entry->h != NULL
11534           && (stub_entry->h == htab->tls_get_addr_fd
11535               || stub_entry->h == htab->tls_get_addr)
11536           && htab->params->tls_get_addr_opt
11537           && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11538         {
11539           /* After the bctrl, lr has been modified so we need to
11540              emit .eh_frame info saying the return address is
11541              on the stack.  In fact we put the EH info specifying
11542              that the return address is on the stack *at* the
11543              call rather than after it, because the EH info for a
11544              call needs to be specified by that point.
11545              See libgcc/unwind-dw2.c execute_cfa_program.  */
11546           lr_used = stub_entry->stub_offset + size - 20;
11547           /* The eh_frame info will consist of a DW_CFA_advance_loc
11548              or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11549              DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
11550           delta = lr_used - stub_entry->group->lr_restore;
11551           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11552           stub_entry->group->lr_restore = size - 4;
11553         }
11554       break;
11555
11556     default:
11557       BFD_FAIL ();
11558       return FALSE;
11559     }
11560
11561   stub_entry->group->stub_sec->size += size;
11562   return TRUE;
11563 }
11564
11565 /* Set up various things so that we can make a list of input sections
11566    for each output section included in the link.  Returns -1 on error,
11567    0 when no stubs will be needed, and 1 on success.  */
11568
11569 int
11570 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11571 {
11572   unsigned int id;
11573   bfd_size_type amt;
11574   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11575
11576   if (htab == NULL)
11577     return -1;
11578
11579   htab->sec_info_arr_size = _bfd_section_id;
11580   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11581   htab->sec_info = bfd_zmalloc (amt);
11582   if (htab->sec_info == NULL)
11583     return -1;
11584
11585   /* Set toc_off for com, und, abs and ind sections.  */
11586   for (id = 0; id < 3; id++)
11587     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11588
11589   return 1;
11590 }
11591
11592 /* Set up for first pass at multitoc partitioning.  */
11593
11594 void
11595 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11596 {
11597   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11598
11599   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11600   htab->toc_bfd = NULL;
11601   htab->toc_first_sec = NULL;
11602 }
11603
11604 /* The linker repeatedly calls this function for each TOC input section
11605    and linker generated GOT section.  Group input bfds such that the toc
11606    within a group is less than 64k in size.  */
11607
11608 bfd_boolean
11609 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11610 {
11611   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11612   bfd_vma addr, off, limit;
11613
11614   if (htab == NULL)
11615     return FALSE;
11616
11617   if (!htab->second_toc_pass)
11618     {
11619       /* Keep track of the first .toc or .got section for this input bfd.  */
11620       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11621
11622       if (new_bfd)
11623         {
11624           htab->toc_bfd = isec->owner;
11625           htab->toc_first_sec = isec;
11626         }
11627
11628       addr = isec->output_offset + isec->output_section->vma;
11629       off = addr - htab->toc_curr;
11630       limit = 0x80008000;
11631       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11632         limit = 0x10000;
11633       if (off + isec->size > limit)
11634         {
11635           addr = (htab->toc_first_sec->output_offset
11636                   + htab->toc_first_sec->output_section->vma);
11637           htab->toc_curr = addr;
11638           htab->toc_curr &= -TOC_BASE_ALIGN;
11639         }
11640
11641       /* toc_curr is the base address of this toc group.  Set elf_gp
11642          for the input section to be the offset relative to the
11643          output toc base plus 0x8000.  Making the input elf_gp an
11644          offset allows us to move the toc as a whole without
11645          recalculating input elf_gp.  */
11646       off = htab->toc_curr - elf_gp (info->output_bfd);
11647       off += TOC_BASE_OFF;
11648
11649       /* Die if someone uses a linker script that doesn't keep input
11650          file .toc and .got together.  */
11651       if (new_bfd
11652           && elf_gp (isec->owner) != 0
11653           && elf_gp (isec->owner) != off)
11654         return FALSE;
11655
11656       elf_gp (isec->owner) = off;
11657       return TRUE;
11658     }
11659
11660   /* During the second pass toc_first_sec points to the start of
11661      a toc group, and toc_curr is used to track the old elf_gp.
11662      We use toc_bfd to ensure we only look at each bfd once.  */
11663   if (htab->toc_bfd == isec->owner)
11664     return TRUE;
11665   htab->toc_bfd = isec->owner;
11666
11667   if (htab->toc_first_sec == NULL
11668       || htab->toc_curr != elf_gp (isec->owner))
11669     {
11670       htab->toc_curr = elf_gp (isec->owner);
11671       htab->toc_first_sec = isec;
11672     }
11673   addr = (htab->toc_first_sec->output_offset
11674           + htab->toc_first_sec->output_section->vma);
11675   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11676   elf_gp (isec->owner) = off;
11677
11678   return TRUE;
11679 }
11680
11681 /* Called via elf_link_hash_traverse to merge GOT entries for global
11682    symbol H.  */
11683
11684 static bfd_boolean
11685 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11686 {
11687   if (h->root.type == bfd_link_hash_indirect)
11688     return TRUE;
11689
11690   merge_got_entries (&h->got.glist);
11691
11692   return TRUE;
11693 }
11694
11695 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11696    symbol H.  */
11697
11698 static bfd_boolean
11699 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11700 {
11701   struct got_entry *gent;
11702
11703   if (h->root.type == bfd_link_hash_indirect)
11704     return TRUE;
11705
11706   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11707     if (!gent->is_indirect)
11708       allocate_got (h, (struct bfd_link_info *) inf, gent);
11709   return TRUE;
11710 }
11711
11712 /* Called on the first multitoc pass after the last call to
11713    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11714    entries.  */
11715
11716 bfd_boolean
11717 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11718 {
11719   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11720   struct bfd *ibfd, *ibfd2;
11721   bfd_boolean done_something;
11722
11723   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11724
11725   if (!htab->do_multi_toc)
11726     return FALSE;
11727
11728   /* Merge global sym got entries within a toc group.  */
11729   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11730
11731   /* And tlsld_got.  */
11732   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11733     {
11734       struct got_entry *ent, *ent2;
11735
11736       if (!is_ppc64_elf (ibfd))
11737         continue;
11738
11739       ent = ppc64_tlsld_got (ibfd);
11740       if (!ent->is_indirect
11741           && ent->got.offset != (bfd_vma) -1)
11742         {
11743           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11744             {
11745               if (!is_ppc64_elf (ibfd2))
11746                 continue;
11747
11748               ent2 = ppc64_tlsld_got (ibfd2);
11749               if (!ent2->is_indirect
11750                   && ent2->got.offset != (bfd_vma) -1
11751                   && elf_gp (ibfd2) == elf_gp (ibfd))
11752                 {
11753                   ent2->is_indirect = TRUE;
11754                   ent2->got.ent = ent;
11755                 }
11756             }
11757         }
11758     }
11759
11760   /* Zap sizes of got sections.  */
11761   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11762   htab->elf.irelplt->size -= htab->got_reli_size;
11763   htab->got_reli_size = 0;
11764
11765   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11766     {
11767       asection *got, *relgot;
11768
11769       if (!is_ppc64_elf (ibfd))
11770         continue;
11771
11772       got = ppc64_elf_tdata (ibfd)->got;
11773       if (got != NULL)
11774         {
11775           got->rawsize = got->size;
11776           got->size = 0;
11777           relgot = ppc64_elf_tdata (ibfd)->relgot;
11778           relgot->rawsize = relgot->size;
11779           relgot->size = 0;
11780         }
11781     }
11782
11783   /* Now reallocate the got, local syms first.  We don't need to
11784      allocate section contents again since we never increase size.  */
11785   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11786     {
11787       struct got_entry **lgot_ents;
11788       struct got_entry **end_lgot_ents;
11789       struct plt_entry **local_plt;
11790       struct plt_entry **end_local_plt;
11791       unsigned char *lgot_masks;
11792       bfd_size_type locsymcount;
11793       Elf_Internal_Shdr *symtab_hdr;
11794       asection *s;
11795
11796       if (!is_ppc64_elf (ibfd))
11797         continue;
11798
11799       lgot_ents = elf_local_got_ents (ibfd);
11800       if (!lgot_ents)
11801         continue;
11802
11803       symtab_hdr = &elf_symtab_hdr (ibfd);
11804       locsymcount = symtab_hdr->sh_info;
11805       end_lgot_ents = lgot_ents + locsymcount;
11806       local_plt = (struct plt_entry **) end_lgot_ents;
11807       end_local_plt = local_plt + locsymcount;
11808       lgot_masks = (unsigned char *) end_local_plt;
11809       s = ppc64_elf_tdata (ibfd)->got;
11810       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11811         {
11812           struct got_entry *ent;
11813
11814           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11815             {
11816               unsigned int ent_size = 8;
11817               unsigned int rel_size = sizeof (Elf64_External_Rela);
11818
11819               ent->got.offset = s->size;
11820               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11821                 {
11822                   ent_size *= 2;
11823                   rel_size *= 2;
11824                 }
11825               s->size += ent_size;
11826               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11827                 {
11828                   htab->elf.irelplt->size += rel_size;
11829                   htab->got_reli_size += rel_size;
11830                 }
11831               else if (bfd_link_pic (info)
11832                        && !((ent->tls_type & TLS_TPREL) != 0
11833                             && bfd_link_executable (info)))
11834                 {
11835                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11836                   srel->size += rel_size;
11837                 }
11838             }
11839         }
11840     }
11841
11842   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11843
11844   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11845     {
11846       struct got_entry *ent;
11847
11848       if (!is_ppc64_elf (ibfd))
11849         continue;
11850
11851       ent = ppc64_tlsld_got (ibfd);
11852       if (!ent->is_indirect
11853           && ent->got.offset != (bfd_vma) -1)
11854         {
11855           asection *s = ppc64_elf_tdata (ibfd)->got;
11856           ent->got.offset = s->size;
11857           s->size += 16;
11858           if (bfd_link_pic (info))
11859             {
11860               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11861               srel->size += sizeof (Elf64_External_Rela);
11862             }
11863         }
11864     }
11865
11866   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11867   if (!done_something)
11868     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11869       {
11870         asection *got;
11871
11872         if (!is_ppc64_elf (ibfd))
11873           continue;
11874
11875         got = ppc64_elf_tdata (ibfd)->got;
11876         if (got != NULL)
11877           {
11878             done_something = got->rawsize != got->size;
11879             if (done_something)
11880               break;
11881           }
11882       }
11883
11884   if (done_something)
11885     (*htab->params->layout_sections_again) ();
11886
11887   /* Set up for second pass over toc sections to recalculate elf_gp
11888      on input sections.  */
11889   htab->toc_bfd = NULL;
11890   htab->toc_first_sec = NULL;
11891   htab->second_toc_pass = TRUE;
11892   return done_something;
11893 }
11894
11895 /* Called after second pass of multitoc partitioning.  */
11896
11897 void
11898 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11899 {
11900   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11901
11902   /* After the second pass, toc_curr tracks the TOC offset used
11903      for code sections below in ppc64_elf_next_input_section.  */
11904   htab->toc_curr = TOC_BASE_OFF;
11905 }
11906
11907 /* No toc references were found in ISEC.  If the code in ISEC makes no
11908    calls, then there's no need to use toc adjusting stubs when branching
11909    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11910    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11911    needed, and 2 if a cyclical call-graph was found but no other reason
11912    for a stub was detected.  If called from the top level, a return of
11913    2 means the same as a return of 0.  */
11914
11915 static int
11916 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11917 {
11918   int ret;
11919
11920   /* Mark this section as checked.  */
11921   isec->call_check_done = 1;
11922
11923   /* We know none of our code bearing sections will need toc stubs.  */
11924   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11925     return 0;
11926
11927   if (isec->size == 0)
11928     return 0;
11929
11930   if (isec->output_section == NULL)
11931     return 0;
11932
11933   ret = 0;
11934   if (isec->reloc_count != 0)
11935     {
11936       Elf_Internal_Rela *relstart, *rel;
11937       Elf_Internal_Sym *local_syms;
11938       struct ppc_link_hash_table *htab;
11939
11940       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11941                                             info->keep_memory);
11942       if (relstart == NULL)
11943         return -1;
11944
11945       /* Look for branches to outside of this section.  */
11946       local_syms = NULL;
11947       htab = ppc_hash_table (info);
11948       if (htab == NULL)
11949         return -1;
11950
11951       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11952         {
11953           enum elf_ppc64_reloc_type r_type;
11954           unsigned long r_symndx;
11955           struct elf_link_hash_entry *h;
11956           struct ppc_link_hash_entry *eh;
11957           Elf_Internal_Sym *sym;
11958           asection *sym_sec;
11959           struct _opd_sec_data *opd;
11960           bfd_vma sym_value;
11961           bfd_vma dest;
11962
11963           r_type = ELF64_R_TYPE (rel->r_info);
11964           if (r_type != R_PPC64_REL24
11965               && r_type != R_PPC64_REL24_NOTOC
11966               && r_type != R_PPC64_REL14
11967               && r_type != R_PPC64_REL14_BRTAKEN
11968               && r_type != R_PPC64_REL14_BRNTAKEN
11969               && r_type != R_PPC64_PLTCALL
11970               && r_type != R_PPC64_PLTCALL_NOTOC)
11971             continue;
11972
11973           r_symndx = ELF64_R_SYM (rel->r_info);
11974           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11975                           isec->owner))
11976             {
11977               ret = -1;
11978               break;
11979             }
11980
11981           /* Calls to dynamic lib functions go through a plt call stub
11982              that uses r2.  */
11983           eh = (struct ppc_link_hash_entry *) h;
11984           if (eh != NULL
11985               && (eh->elf.plt.plist != NULL
11986                   || (eh->oh != NULL
11987                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11988             {
11989               ret = 1;
11990               break;
11991             }
11992
11993           if (sym_sec == NULL)
11994             /* Ignore other undefined symbols.  */
11995             continue;
11996
11997           /* Assume branches to other sections not included in the
11998              link need stubs too, to cover -R and absolute syms.  */
11999           if (sym_sec->output_section == NULL)
12000             {
12001               ret = 1;
12002               break;
12003             }
12004
12005           if (h == NULL)
12006             sym_value = sym->st_value;
12007           else
12008             {
12009               if (h->root.type != bfd_link_hash_defined
12010                   && h->root.type != bfd_link_hash_defweak)
12011                 abort ();
12012               sym_value = h->root.u.def.value;
12013             }
12014           sym_value += rel->r_addend;
12015
12016           /* If this branch reloc uses an opd sym, find the code section.  */
12017           opd = get_opd_info (sym_sec);
12018           if (opd != NULL)
12019             {
12020               if (h == NULL && opd->adjust != NULL)
12021                 {
12022                   long adjust;
12023
12024                   adjust = opd->adjust[OPD_NDX (sym_value)];
12025                   if (adjust == -1)
12026                     /* Assume deleted functions won't ever be called.  */
12027                     continue;
12028                   sym_value += adjust;
12029                 }
12030
12031               dest = opd_entry_value (sym_sec, sym_value,
12032                                       &sym_sec, NULL, FALSE);
12033               if (dest == (bfd_vma) -1)
12034                 continue;
12035             }
12036           else
12037             dest = (sym_value
12038                     + sym_sec->output_offset
12039                     + sym_sec->output_section->vma);
12040
12041           /* Ignore branch to self.  */
12042           if (sym_sec == isec)
12043             continue;
12044
12045           /* If the called function uses the toc, we need a stub.  */
12046           if (sym_sec->has_toc_reloc
12047               || sym_sec->makes_toc_func_call)
12048             {
12049               ret = 1;
12050               break;
12051             }
12052
12053           /* Assume any branch that needs a long branch stub might in fact
12054              need a plt_branch stub.  A plt_branch stub uses r2.  */
12055           else if (dest - (isec->output_offset
12056                            + isec->output_section->vma
12057                            + rel->r_offset) + (1 << 25)
12058                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12059                                                              ? h->other
12060                                                              : sym->st_other))
12061             {
12062               ret = 1;
12063               break;
12064             }
12065
12066           /* If calling back to a section in the process of being
12067              tested, we can't say for sure that no toc adjusting stubs
12068              are needed, so don't return zero.  */
12069           else if (sym_sec->call_check_in_progress)
12070             ret = 2;
12071
12072           /* Branches to another section that itself doesn't have any TOC
12073              references are OK.  Recursively call ourselves to check.  */
12074           else if (!sym_sec->call_check_done)
12075             {
12076               int recur;
12077
12078               /* Mark current section as indeterminate, so that other
12079                  sections that call back to current won't be marked as
12080                  known.  */
12081               isec->call_check_in_progress = 1;
12082               recur = toc_adjusting_stub_needed (info, sym_sec);
12083               isec->call_check_in_progress = 0;
12084
12085               if (recur != 0)
12086                 {
12087                   ret = recur;
12088                   if (recur != 2)
12089                     break;
12090                 }
12091             }
12092         }
12093
12094       if (local_syms != NULL
12095           && (elf_symtab_hdr (isec->owner).contents
12096               != (unsigned char *) local_syms))
12097         free (local_syms);
12098       if (elf_section_data (isec)->relocs != relstart)
12099         free (relstart);
12100     }
12101
12102   if ((ret & 1) == 0
12103       && isec->map_head.s != NULL
12104       && (strcmp (isec->output_section->name, ".init") == 0
12105           || strcmp (isec->output_section->name, ".fini") == 0))
12106     {
12107       if (isec->map_head.s->has_toc_reloc
12108           || isec->map_head.s->makes_toc_func_call)
12109         ret = 1;
12110       else if (!isec->map_head.s->call_check_done)
12111         {
12112           int recur;
12113           isec->call_check_in_progress = 1;
12114           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12115           isec->call_check_in_progress = 0;
12116           if (recur != 0)
12117             ret = recur;
12118         }
12119     }
12120
12121   if (ret == 1)
12122     isec->makes_toc_func_call = 1;
12123
12124   return ret;
12125 }
12126
12127 /* The linker repeatedly calls this function for each input section,
12128    in the order that input sections are linked into output sections.
12129    Build lists of input sections to determine groupings between which
12130    we may insert linker stubs.  */
12131
12132 bfd_boolean
12133 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12134 {
12135   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12136
12137   if (htab == NULL)
12138     return FALSE;
12139
12140   if ((isec->output_section->flags & SEC_CODE) != 0
12141       && isec->output_section->id < htab->sec_info_arr_size)
12142     {
12143       /* This happens to make the list in reverse order,
12144          which is what we want.  */
12145       htab->sec_info[isec->id].u.list
12146         = htab->sec_info[isec->output_section->id].u.list;
12147       htab->sec_info[isec->output_section->id].u.list = isec;
12148     }
12149
12150   if (htab->multi_toc_needed)
12151     {
12152       /* Analyse sections that aren't already flagged as needing a
12153          valid toc pointer.  Exclude .fixup for the linux kernel.
12154          .fixup contains branches, but only back to the function that
12155          hit an exception.  */
12156       if (!(isec->has_toc_reloc
12157             || (isec->flags & SEC_CODE) == 0
12158             || strcmp (isec->name, ".fixup") == 0
12159             || isec->call_check_done))
12160         {
12161           if (toc_adjusting_stub_needed (info, isec) < 0)
12162             return FALSE;
12163         }
12164       /* Make all sections use the TOC assigned for this object file.
12165          This will be wrong for pasted sections;  We fix that in
12166          check_pasted_section().  */
12167       if (elf_gp (isec->owner) != 0)
12168         htab->toc_curr = elf_gp (isec->owner);
12169     }
12170
12171   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12172   return TRUE;
12173 }
12174
12175 /* Check that all .init and .fini sections use the same toc, if they
12176    have toc relocs.  */
12177
12178 static bfd_boolean
12179 check_pasted_section (struct bfd_link_info *info, const char *name)
12180 {
12181   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12182
12183   if (o != NULL)
12184     {
12185       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12186       bfd_vma toc_off = 0;
12187       asection *i;
12188
12189       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12190         if (i->has_toc_reloc)
12191           {
12192             if (toc_off == 0)
12193               toc_off = htab->sec_info[i->id].toc_off;
12194             else if (toc_off != htab->sec_info[i->id].toc_off)
12195               return FALSE;
12196           }
12197
12198       if (toc_off == 0)
12199         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12200           if (i->makes_toc_func_call)
12201             {
12202               toc_off = htab->sec_info[i->id].toc_off;
12203               break;
12204             }
12205
12206       /* Make sure the whole pasted function uses the same toc offset.  */
12207       if (toc_off != 0)
12208         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12209           htab->sec_info[i->id].toc_off = toc_off;
12210     }
12211   return TRUE;
12212 }
12213
12214 bfd_boolean
12215 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12216 {
12217   return (check_pasted_section (info, ".init")
12218           & check_pasted_section (info, ".fini"));
12219 }
12220
12221 /* See whether we can group stub sections together.  Grouping stub
12222    sections may result in fewer stubs.  More importantly, we need to
12223    put all .init* and .fini* stubs at the beginning of the .init or
12224    .fini output sections respectively, because glibc splits the
12225    _init and _fini functions into multiple parts.  Putting a stub in
12226    the middle of a function is not a good idea.  */
12227
12228 static bfd_boolean
12229 group_sections (struct bfd_link_info *info,
12230                 bfd_size_type stub_group_size,
12231                 bfd_boolean stubs_always_before_branch)
12232 {
12233   struct ppc_link_hash_table *htab;
12234   asection *osec;
12235   bfd_boolean suppress_size_errors;
12236
12237   htab = ppc_hash_table (info);
12238   if (htab == NULL)
12239     return FALSE;
12240
12241   suppress_size_errors = FALSE;
12242   if (stub_group_size == 1)
12243     {
12244       /* Default values.  */
12245       if (stubs_always_before_branch)
12246         stub_group_size = 0x1e00000;
12247       else
12248         stub_group_size = 0x1c00000;
12249       suppress_size_errors = TRUE;
12250     }
12251
12252   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12253     {
12254       asection *tail;
12255
12256       if (osec->id >= htab->sec_info_arr_size)
12257         continue;
12258
12259       tail = htab->sec_info[osec->id].u.list;
12260       while (tail != NULL)
12261         {
12262           asection *curr;
12263           asection *prev;
12264           bfd_size_type total;
12265           bfd_boolean big_sec;
12266           bfd_vma curr_toc;
12267           struct map_stub *group;
12268           bfd_size_type group_size;
12269
12270           curr = tail;
12271           total = tail->size;
12272           group_size = (ppc64_elf_section_data (tail) != NULL
12273                         && ppc64_elf_section_data (tail)->has_14bit_branch
12274                         ? stub_group_size >> 10 : stub_group_size);
12275
12276           big_sec = total > group_size;
12277           if (big_sec && !suppress_size_errors)
12278             /* xgettext:c-format */
12279             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12280                                 tail->owner, tail);
12281           curr_toc = htab->sec_info[tail->id].toc_off;
12282
12283           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12284                  && ((total += curr->output_offset - prev->output_offset)
12285                      < (ppc64_elf_section_data (prev) != NULL
12286                         && ppc64_elf_section_data (prev)->has_14bit_branch
12287                         ? (group_size = stub_group_size >> 10) : group_size))
12288                  && htab->sec_info[prev->id].toc_off == curr_toc)
12289             curr = prev;
12290
12291           /* OK, the size from the start of CURR to the end is less
12292              than group_size and thus can be handled by one stub
12293              section.  (or the tail section is itself larger than
12294              group_size, in which case we may be toast.)  We should
12295              really be keeping track of the total size of stubs added
12296              here, as stubs contribute to the final output section
12297              size.  That's a little tricky, and this way will only
12298              break if stubs added make the total size more than 2^25,
12299              ie. for the default stub_group_size, if stubs total more
12300              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12301           group = bfd_alloc (curr->owner, sizeof (*group));
12302           if (group == NULL)
12303             return FALSE;
12304           group->link_sec = curr;
12305           group->stub_sec = NULL;
12306           group->needs_save_res = 0;
12307           group->lr_restore = 0;
12308           group->eh_size = 0;
12309           group->eh_base = 0;
12310           group->next = htab->group;
12311           htab->group = group;
12312           do
12313             {
12314               prev = htab->sec_info[tail->id].u.list;
12315               /* Set up this stub group.  */
12316               htab->sec_info[tail->id].u.group = group;
12317             }
12318           while (tail != curr && (tail = prev) != NULL);
12319
12320           /* But wait, there's more!  Input sections up to group_size
12321              bytes before the stub section can be handled by it too.
12322              Don't do this if we have a really large section after the
12323              stubs, as adding more stubs increases the chance that
12324              branches may not reach into the stub section.  */
12325           if (!stubs_always_before_branch && !big_sec)
12326             {
12327               total = 0;
12328               while (prev != NULL
12329                      && ((total += tail->output_offset - prev->output_offset)
12330                          < (ppc64_elf_section_data (prev) != NULL
12331                             && ppc64_elf_section_data (prev)->has_14bit_branch
12332                             ? (group_size = stub_group_size >> 10)
12333                             : group_size))
12334                      && htab->sec_info[prev->id].toc_off == curr_toc)
12335                 {
12336                   tail = prev;
12337                   prev = htab->sec_info[tail->id].u.list;
12338                   htab->sec_info[tail->id].u.group = group;
12339                 }
12340             }
12341           tail = prev;
12342         }
12343     }
12344   return TRUE;
12345 }
12346
12347 static const unsigned char glink_eh_frame_cie[] =
12348 {
12349   0, 0, 0, 16,                          /* length.  */
12350   0, 0, 0, 0,                           /* id.  */
12351   1,                                    /* CIE version.  */
12352   'z', 'R', 0,                          /* Augmentation string.  */
12353   4,                                    /* Code alignment.  */
12354   0x78,                                 /* Data alignment.  */
12355   65,                                   /* RA reg.  */
12356   1,                                    /* Augmentation size.  */
12357   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12358   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12359 };
12360
12361 /* Stripping output sections is normally done before dynamic section
12362    symbols have been allocated.  This function is called later, and
12363    handles cases like htab->brlt which is mapped to its own output
12364    section.  */
12365
12366 static void
12367 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12368 {
12369   if (isec->size == 0
12370       && isec->output_section->size == 0
12371       && !(isec->output_section->flags & SEC_KEEP)
12372       && !bfd_section_removed_from_list (info->output_bfd,
12373                                          isec->output_section)
12374       && elf_section_data (isec->output_section)->dynindx == 0)
12375     {
12376       isec->output_section->flags |= SEC_EXCLUDE;
12377       bfd_section_list_remove (info->output_bfd, isec->output_section);
12378       info->output_bfd->section_count--;
12379     }
12380 }
12381
12382 /* Determine and set the size of the stub section for a final link.
12383
12384    The basic idea here is to examine all the relocations looking for
12385    PC-relative calls to a target that is unreachable with a "bl"
12386    instruction.  */
12387
12388 bfd_boolean
12389 ppc64_elf_size_stubs (struct bfd_link_info *info)
12390 {
12391   bfd_size_type stub_group_size;
12392   bfd_boolean stubs_always_before_branch;
12393   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12394
12395   if (htab == NULL)
12396     return FALSE;
12397
12398   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12399     htab->params->plt_thread_safe = 1;
12400   if (!htab->opd_abi)
12401     htab->params->plt_thread_safe = 0;
12402   else if (htab->params->plt_thread_safe == -1)
12403     {
12404       static const char *const thread_starter[] =
12405         {
12406           "pthread_create",
12407           /* libstdc++ */
12408           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12409           /* librt */
12410           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12411           "mq_notify", "create_timer",
12412           /* libanl */
12413           "getaddrinfo_a",
12414           /* libgomp */
12415           "GOMP_parallel",
12416           "GOMP_parallel_start",
12417           "GOMP_parallel_loop_static",
12418           "GOMP_parallel_loop_static_start",
12419           "GOMP_parallel_loop_dynamic",
12420           "GOMP_parallel_loop_dynamic_start",
12421           "GOMP_parallel_loop_guided",
12422           "GOMP_parallel_loop_guided_start",
12423           "GOMP_parallel_loop_runtime",
12424           "GOMP_parallel_loop_runtime_start",
12425           "GOMP_parallel_sections",
12426           "GOMP_parallel_sections_start",
12427           /* libgo */
12428           "__go_go",
12429         };
12430       unsigned i;
12431
12432       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12433         {
12434           struct elf_link_hash_entry *h;
12435           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12436                                     FALSE, FALSE, TRUE);
12437           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12438           if (htab->params->plt_thread_safe)
12439             break;
12440         }
12441     }
12442   stubs_always_before_branch = htab->params->group_size < 0;
12443   if (htab->params->group_size < 0)
12444     stub_group_size = -htab->params->group_size;
12445   else
12446     stub_group_size = htab->params->group_size;
12447
12448   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12449     return FALSE;
12450
12451 #define STUB_SHRINK_ITER 20
12452   /* Loop until no stubs added.  After iteration 20 of this loop we may
12453      exit on a stub section shrinking.  This is to break out of a
12454      pathological case where adding stubs on one iteration decreases
12455      section gaps (perhaps due to alignment), which then requires
12456      fewer or smaller stubs on the next iteration.  */
12457
12458   while (1)
12459     {
12460       bfd *input_bfd;
12461       unsigned int bfd_indx;
12462       struct map_stub *group;
12463
12464       htab->stub_iteration += 1;
12465
12466       for (input_bfd = info->input_bfds, bfd_indx = 0;
12467            input_bfd != NULL;
12468            input_bfd = input_bfd->link.next, bfd_indx++)
12469         {
12470           Elf_Internal_Shdr *symtab_hdr;
12471           asection *section;
12472           Elf_Internal_Sym *local_syms = NULL;
12473
12474           if (!is_ppc64_elf (input_bfd))
12475             continue;
12476
12477           /* We'll need the symbol table in a second.  */
12478           symtab_hdr = &elf_symtab_hdr (input_bfd);
12479           if (symtab_hdr->sh_info == 0)
12480             continue;
12481
12482           /* Walk over each section attached to the input bfd.  */
12483           for (section = input_bfd->sections;
12484                section != NULL;
12485                section = section->next)
12486             {
12487               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12488
12489               /* If there aren't any relocs, then there's nothing more
12490                  to do.  */
12491               if ((section->flags & SEC_RELOC) == 0
12492                   || (section->flags & SEC_ALLOC) == 0
12493                   || (section->flags & SEC_LOAD) == 0
12494                   || (section->flags & SEC_CODE) == 0
12495                   || section->reloc_count == 0)
12496                 continue;
12497
12498               /* If this section is a link-once section that will be
12499                  discarded, then don't create any stubs.  */
12500               if (section->output_section == NULL
12501                   || section->output_section->owner != info->output_bfd)
12502                 continue;
12503
12504               /* Get the relocs.  */
12505               internal_relocs
12506                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12507                                              info->keep_memory);
12508               if (internal_relocs == NULL)
12509                 goto error_ret_free_local;
12510
12511               /* Now examine each relocation.  */
12512               irela = internal_relocs;
12513               irelaend = irela + section->reloc_count;
12514               for (; irela < irelaend; irela++)
12515                 {
12516                   enum elf_ppc64_reloc_type r_type;
12517                   unsigned int r_indx;
12518                   enum ppc_stub_type stub_type;
12519                   struct ppc_stub_hash_entry *stub_entry;
12520                   asection *sym_sec, *code_sec;
12521                   bfd_vma sym_value, code_value;
12522                   bfd_vma destination;
12523                   unsigned long local_off;
12524                   bfd_boolean ok_dest;
12525                   struct ppc_link_hash_entry *hash;
12526                   struct ppc_link_hash_entry *fdh;
12527                   struct elf_link_hash_entry *h;
12528                   Elf_Internal_Sym *sym;
12529                   char *stub_name;
12530                   const asection *id_sec;
12531                   struct _opd_sec_data *opd;
12532                   struct plt_entry *plt_ent;
12533
12534                   r_type = ELF64_R_TYPE (irela->r_info);
12535                   r_indx = ELF64_R_SYM (irela->r_info);
12536
12537                   if (r_type >= R_PPC64_max)
12538                     {
12539                       bfd_set_error (bfd_error_bad_value);
12540                       goto error_ret_free_internal;
12541                     }
12542
12543                   /* Only look for stubs on branch instructions.  */
12544                   if (r_type != R_PPC64_REL24
12545                       && r_type != R_PPC64_REL24_NOTOC
12546                       && r_type != R_PPC64_REL14
12547                       && r_type != R_PPC64_REL14_BRTAKEN
12548                       && r_type != R_PPC64_REL14_BRNTAKEN)
12549                     continue;
12550
12551                   /* Now determine the call target, its name, value,
12552                      section.  */
12553                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12554                                   r_indx, input_bfd))
12555                     goto error_ret_free_internal;
12556                   hash = (struct ppc_link_hash_entry *) h;
12557
12558                   ok_dest = FALSE;
12559                   fdh = NULL;
12560                   sym_value = 0;
12561                   if (hash == NULL)
12562                     {
12563                       sym_value = sym->st_value;
12564                       if (sym_sec != NULL
12565                           && sym_sec->output_section != NULL)
12566                         ok_dest = TRUE;
12567                     }
12568                   else if (hash->elf.root.type == bfd_link_hash_defined
12569                            || hash->elf.root.type == bfd_link_hash_defweak)
12570                     {
12571                       sym_value = hash->elf.root.u.def.value;
12572                       if (sym_sec->output_section != NULL)
12573                         ok_dest = TRUE;
12574                     }
12575                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12576                            || hash->elf.root.type == bfd_link_hash_undefined)
12577                     {
12578                       /* Recognise an old ABI func code entry sym, and
12579                          use the func descriptor sym instead if it is
12580                          defined.  */
12581                       if (hash->elf.root.root.string[0] == '.'
12582                           && hash->oh != NULL)
12583                         {
12584                           fdh = ppc_follow_link (hash->oh);
12585                           if (fdh->elf.root.type == bfd_link_hash_defined
12586                               || fdh->elf.root.type == bfd_link_hash_defweak)
12587                             {
12588                               sym_sec = fdh->elf.root.u.def.section;
12589                               sym_value = fdh->elf.root.u.def.value;
12590                               if (sym_sec->output_section != NULL)
12591                                 ok_dest = TRUE;
12592                             }
12593                           else
12594                             fdh = NULL;
12595                         }
12596                     }
12597                   else
12598                     {
12599                       bfd_set_error (bfd_error_bad_value);
12600                       goto error_ret_free_internal;
12601                     }
12602
12603                   destination = 0;
12604                   local_off = 0;
12605                   if (ok_dest)
12606                     {
12607                       sym_value += irela->r_addend;
12608                       destination = (sym_value
12609                                      + sym_sec->output_offset
12610                                      + sym_sec->output_section->vma);
12611                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12612                                                             ? hash->elf.other
12613                                                             : sym->st_other);
12614                     }
12615
12616                   code_sec = sym_sec;
12617                   code_value = sym_value;
12618                   opd = get_opd_info (sym_sec);
12619                   if (opd != NULL)
12620                     {
12621                       bfd_vma dest;
12622
12623                       if (hash == NULL && opd->adjust != NULL)
12624                         {
12625                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12626                           if (adjust == -1)
12627                             continue;
12628                           code_value += adjust;
12629                           sym_value += adjust;
12630                         }
12631                       dest = opd_entry_value (sym_sec, sym_value,
12632                                               &code_sec, &code_value, FALSE);
12633                       if (dest != (bfd_vma) -1)
12634                         {
12635                           destination = dest;
12636                           if (fdh != NULL)
12637                             {
12638                               /* Fixup old ABI sym to point at code
12639                                  entry.  */
12640                               hash->elf.root.type = bfd_link_hash_defweak;
12641                               hash->elf.root.u.def.section = code_sec;
12642                               hash->elf.root.u.def.value = code_value;
12643                             }
12644                         }
12645                     }
12646
12647                   /* Determine what (if any) linker stub is needed.  */
12648                   plt_ent = NULL;
12649                   stub_type = ppc_type_of_stub (section, irela, &hash,
12650                                                 &plt_ent, destination,
12651                                                 local_off);
12652
12653                   if (r_type == R_PPC64_REL24_NOTOC)
12654                     {
12655                       if (stub_type == ppc_stub_plt_call)
12656                         stub_type = ppc_stub_plt_call_notoc;
12657                       else if (stub_type == ppc_stub_long_branch
12658                                || (code_sec != NULL
12659                                    && code_sec->output_section != NULL
12660                                    && (((hash ? hash->elf.other : sym->st_other)
12661                                         & STO_PPC64_LOCAL_MASK)
12662                                        != 1 << STO_PPC64_LOCAL_BIT)))
12663                         stub_type = ppc_stub_long_branch_notoc;
12664                     }
12665                   else if (stub_type != ppc_stub_plt_call)
12666                     {
12667                       /* Check whether we need a TOC adjusting stub.
12668                          Since the linker pastes together pieces from
12669                          different object files when creating the
12670                          _init and _fini functions, it may be that a
12671                          call to what looks like a local sym is in
12672                          fact a call needing a TOC adjustment.  */
12673                       if ((code_sec != NULL
12674                            && code_sec->output_section != NULL
12675                            && (htab->sec_info[code_sec->id].toc_off
12676                                != htab->sec_info[section->id].toc_off)
12677                            && (code_sec->has_toc_reloc
12678                                || code_sec->makes_toc_func_call))
12679                           || (((hash ? hash->elf.other : sym->st_other)
12680                                & STO_PPC64_LOCAL_MASK)
12681                               == 1 << STO_PPC64_LOCAL_BIT))
12682                         stub_type = ppc_stub_long_branch_r2off;
12683                     }
12684
12685                   if (stub_type == ppc_stub_none)
12686                     continue;
12687
12688                   /* __tls_get_addr calls might be eliminated.  */
12689                   if (stub_type != ppc_stub_plt_call
12690                       && stub_type != ppc_stub_plt_call_notoc
12691                       && hash != NULL
12692                       && (hash == htab->tls_get_addr
12693                           || hash == htab->tls_get_addr_fd)
12694                       && section->has_tls_reloc
12695                       && irela != internal_relocs)
12696                     {
12697                       /* Get tls info.  */
12698                       unsigned char *tls_mask;
12699
12700                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12701                                          irela - 1, input_bfd))
12702                         goto error_ret_free_internal;
12703                       if ((*tls_mask & TLS_TLS) != 0)
12704                         continue;
12705                     }
12706
12707                   if (stub_type == ppc_stub_plt_call)
12708                     {
12709                       if (!htab->opd_abi
12710                           && htab->params->plt_localentry0 != 0
12711                           && is_elfv2_localentry0 (&hash->elf))
12712                         htab->has_plt_localentry0 = 1;
12713                       else if (irela + 1 < irelaend
12714                                && irela[1].r_offset == irela->r_offset + 4
12715                                && (ELF64_R_TYPE (irela[1].r_info)
12716                                    == R_PPC64_TOCSAVE))
12717                         {
12718                           if (!tocsave_find (htab, INSERT,
12719                                              &local_syms, irela + 1, input_bfd))
12720                             goto error_ret_free_internal;
12721                         }
12722                       else
12723                         stub_type = ppc_stub_plt_call_r2save;
12724                     }
12725
12726                   /* Support for grouping stub sections.  */
12727                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12728
12729                   /* Get the name of this stub.  */
12730                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12731                   if (!stub_name)
12732                     goto error_ret_free_internal;
12733
12734                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12735                                                      stub_name, FALSE, FALSE);
12736                   if (stub_entry != NULL)
12737                     {
12738                       enum ppc_stub_type old_type;
12739                       /* A stub has already been created, but it may
12740                          not be the required type.  We shouldn't be
12741                          transitioning from plt_call to long_branch
12742                          stubs or vice versa, but we might be
12743                          upgrading from plt_call to plt_call_r2save or
12744                          from long_branch to long_branch_r2off.  */
12745                       free (stub_name);
12746                       old_type = stub_entry->stub_type;
12747                       switch (old_type)
12748                         {
12749                         default:
12750                           abort ();
12751
12752                         case ppc_stub_save_res:
12753                           continue;
12754
12755                         case ppc_stub_plt_call:
12756                         case ppc_stub_plt_call_r2save:
12757                         case ppc_stub_plt_call_notoc:
12758                         case ppc_stub_plt_call_both:
12759                           if (stub_type == ppc_stub_plt_call)
12760                             continue;
12761                           else if (stub_type == ppc_stub_plt_call_r2save)
12762                             {
12763                               if (old_type == ppc_stub_plt_call_notoc)
12764                                 stub_type = ppc_stub_plt_call_both;
12765                             }
12766                           else if (stub_type == ppc_stub_plt_call_notoc)
12767                             {
12768                               if (old_type == ppc_stub_plt_call_r2save)
12769                                 stub_type = ppc_stub_plt_call_both;
12770                             }
12771                           else
12772                             abort ();
12773                           break;
12774
12775                         case ppc_stub_plt_branch:
12776                         case ppc_stub_plt_branch_r2off:
12777                         case ppc_stub_plt_branch_notoc:
12778                         case ppc_stub_plt_branch_both:
12779                           old_type += (ppc_stub_long_branch
12780                                        - ppc_stub_plt_branch);
12781                           /* Fall through.  */
12782                         case ppc_stub_long_branch:
12783                         case ppc_stub_long_branch_r2off:
12784                         case ppc_stub_long_branch_notoc:
12785                         case ppc_stub_long_branch_both:
12786                           if (stub_type == ppc_stub_long_branch)
12787                             continue;
12788                           else if (stub_type == ppc_stub_long_branch_r2off)
12789                             {
12790                               if (old_type == ppc_stub_long_branch_notoc)
12791                                 stub_type = ppc_stub_long_branch_both;
12792                             }
12793                           else if (stub_type == ppc_stub_long_branch_notoc)
12794                             {
12795                               if (old_type == ppc_stub_long_branch_r2off)
12796                                 stub_type = ppc_stub_long_branch_both;
12797                             }
12798                           else
12799                             abort ();
12800                           break;
12801                         }
12802                       if (old_type < stub_type)
12803                         stub_entry->stub_type = stub_type;
12804                       continue;
12805                     }
12806
12807                   stub_entry = ppc_add_stub (stub_name, section, info);
12808                   if (stub_entry == NULL)
12809                     {
12810                       free (stub_name);
12811                     error_ret_free_internal:
12812                       if (elf_section_data (section)->relocs == NULL)
12813                         free (internal_relocs);
12814                     error_ret_free_local:
12815                       if (local_syms != NULL
12816                           && (symtab_hdr->contents
12817                               != (unsigned char *) local_syms))
12818                         free (local_syms);
12819                       return FALSE;
12820                     }
12821
12822                   stub_entry->stub_type = stub_type;
12823                   if (stub_type >= ppc_stub_plt_call
12824                       && stub_type <= ppc_stub_plt_call_both)
12825                     {
12826                       stub_entry->target_value = sym_value;
12827                       stub_entry->target_section = sym_sec;
12828                     }
12829                   else
12830                     {
12831                       stub_entry->target_value = code_value;
12832                       stub_entry->target_section = code_sec;
12833                     }
12834                   stub_entry->h = hash;
12835                   stub_entry->plt_ent = plt_ent;
12836                   stub_entry->symtype
12837                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12838                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12839
12840                   if (hash != NULL
12841                       && (hash->elf.root.type == bfd_link_hash_defined
12842                           || hash->elf.root.type == bfd_link_hash_defweak))
12843                     htab->stub_globals += 1;
12844                 }
12845
12846               /* We're done with the internal relocs, free them.  */
12847               if (elf_section_data (section)->relocs != internal_relocs)
12848                 free (internal_relocs);
12849             }
12850
12851           if (local_syms != NULL
12852               && symtab_hdr->contents != (unsigned char *) local_syms)
12853             {
12854               if (!info->keep_memory)
12855                 free (local_syms);
12856               else
12857                 symtab_hdr->contents = (unsigned char *) local_syms;
12858             }
12859         }
12860
12861       /* We may have added some stubs.  Find out the new size of the
12862          stub sections.  */
12863       for (group = htab->group; group != NULL; group = group->next)
12864         {
12865           group->lr_restore = 0;
12866           group->eh_size = 0;
12867           if (group->stub_sec != NULL)
12868             {
12869               asection *stub_sec = group->stub_sec;
12870
12871               if (htab->stub_iteration <= STUB_SHRINK_ITER
12872                   || stub_sec->rawsize < stub_sec->size)
12873                 /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12874                 stub_sec->rawsize = stub_sec->size;
12875               stub_sec->size = 0;
12876               stub_sec->reloc_count = 0;
12877               stub_sec->flags &= ~SEC_RELOC;
12878             }
12879         }
12880
12881       if (htab->stub_iteration <= STUB_SHRINK_ITER
12882           || htab->brlt->rawsize < htab->brlt->size)
12883         htab->brlt->rawsize = htab->brlt->size;
12884       htab->brlt->size = 0;
12885       htab->brlt->reloc_count = 0;
12886       htab->brlt->flags &= ~SEC_RELOC;
12887       if (htab->relbrlt != NULL)
12888         htab->relbrlt->size = 0;
12889
12890       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12891
12892       for (group = htab->group; group != NULL; group = group->next)
12893         if (group->needs_save_res)
12894           group->stub_sec->size += htab->sfpr->size;
12895
12896       if (info->emitrelocations
12897           && htab->glink != NULL && htab->glink->size != 0)
12898         {
12899           htab->glink->reloc_count = 1;
12900           htab->glink->flags |= SEC_RELOC;
12901         }
12902
12903       if (htab->glink_eh_frame != NULL
12904           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12905           && htab->glink_eh_frame->output_section->size > 8)
12906         {
12907           size_t size = 0, align = 4;
12908
12909           for (group = htab->group; group != NULL; group = group->next)
12910             if (group->eh_size != 0)
12911               size += (group->eh_size + 17 + align - 1) & -align;
12912           if (htab->glink != NULL && htab->glink->size != 0)
12913             size += (24 + align - 1) & -align;
12914           if (size != 0)
12915             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12916           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12917           size = (size + align - 1) & -align;
12918           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12919           htab->glink_eh_frame->size = size;
12920         }
12921
12922       if (htab->params->plt_stub_align != 0)
12923         for (group = htab->group; group != NULL; group = group->next)
12924           if (group->stub_sec != NULL)
12925             {
12926               int align = abs (htab->params->plt_stub_align);
12927               group->stub_sec->size
12928                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12929             }
12930
12931       for (group = htab->group; group != NULL; group = group->next)
12932         if (group->stub_sec != NULL
12933             && group->stub_sec->rawsize != group->stub_sec->size
12934             && (htab->stub_iteration <= STUB_SHRINK_ITER
12935                 || group->stub_sec->rawsize < group->stub_sec->size))
12936           break;
12937
12938       if (group == NULL
12939           && (htab->brlt->rawsize == htab->brlt->size
12940               || (htab->stub_iteration > STUB_SHRINK_ITER
12941                   && htab->brlt->rawsize > htab->brlt->size))
12942           && (htab->glink_eh_frame == NULL
12943               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12944         break;
12945
12946       /* Ask the linker to do its stuff.  */
12947       (*htab->params->layout_sections_again) ();
12948     }
12949
12950   if (htab->glink_eh_frame != NULL
12951       && htab->glink_eh_frame->size != 0)
12952     {
12953       bfd_vma val;
12954       bfd_byte *p, *last_fde;
12955       size_t last_fde_len, size, align, pad;
12956       struct map_stub *group;
12957
12958       /* It is necessary to at least have a rough outline of the
12959          linker generated CIEs and FDEs written before
12960          bfd_elf_discard_info is run, in order for these FDEs to be
12961          indexed in .eh_frame_hdr.  */
12962       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12963       if (p == NULL)
12964         return FALSE;
12965       htab->glink_eh_frame->contents = p;
12966       last_fde = p;
12967       align = 4;
12968
12969       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12970       /* CIE length (rewrite in case little-endian).  */
12971       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12972       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12973       p += last_fde_len + 4;
12974
12975       for (group = htab->group; group != NULL; group = group->next)
12976         if (group->eh_size != 0)
12977           {
12978             group->eh_base = p - htab->glink_eh_frame->contents;
12979             last_fde = p;
12980             last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12981             /* FDE length.  */
12982             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12983             p += 4;
12984             /* CIE pointer.  */
12985             val = p - htab->glink_eh_frame->contents;
12986             bfd_put_32 (htab->elf.dynobj, val, p);
12987             p += 4;
12988             /* Offset to stub section, written later.  */
12989             p += 4;
12990             /* stub section size.  */
12991             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12992             p += 4;
12993             /* Augmentation.  */
12994             p += 1;
12995             /* Make sure we don't have all nops.  This is enough for
12996                elf-eh-frame.c to detect the last non-nop opcode.  */
12997             p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12998             p = last_fde + last_fde_len + 4;
12999           }
13000       if (htab->glink != NULL && htab->glink->size != 0)
13001         {
13002           last_fde = p;
13003           last_fde_len = ((24 + align - 1) & -align) - 4;
13004           /* FDE length.  */
13005           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13006           p += 4;
13007           /* CIE pointer.  */
13008           val = p - htab->glink_eh_frame->contents;
13009           bfd_put_32 (htab->elf.dynobj, val, p);
13010           p += 4;
13011           /* Offset to .glink, written later.  */
13012           p += 4;
13013           /* .glink size.  */
13014           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13015           p += 4;
13016           /* Augmentation.  */
13017           p += 1;
13018
13019           *p++ = DW_CFA_advance_loc + 1;
13020           *p++ = DW_CFA_register;
13021           *p++ = 65;
13022           *p++ = htab->opd_abi ? 12 : 0;
13023           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13024           *p++ = DW_CFA_restore_extended;
13025           *p++ = 65;
13026           p += ((24 + align - 1) & -align) - 24;
13027         }
13028       /* Subsume any padding into the last FDE if user .eh_frame
13029          sections are aligned more than glink_eh_frame.  Otherwise any
13030          zero padding will be seen as a terminator.  */
13031       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13032       size = p - htab->glink_eh_frame->contents;
13033       pad = ((size + align - 1) & -align) - size;
13034       htab->glink_eh_frame->size = size + pad;
13035       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13036     }
13037
13038   maybe_strip_output (info, htab->brlt);
13039   if (htab->glink_eh_frame != NULL)
13040     maybe_strip_output (info, htab->glink_eh_frame);
13041
13042   return TRUE;
13043 }
13044
13045 /* Called after we have determined section placement.  If sections
13046    move, we'll be called again.  Provide a value for TOCstart.  */
13047
13048 bfd_vma
13049 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13050 {
13051   asection *s;
13052   bfd_vma TOCstart, adjust;
13053
13054   if (info != NULL)
13055     {
13056       struct elf_link_hash_entry *h;
13057       struct elf_link_hash_table *htab = elf_hash_table (info);
13058
13059       if (is_elf_hash_table (htab)
13060           && htab->hgot != NULL)
13061         h = htab->hgot;
13062       else
13063         {
13064           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13065           if (is_elf_hash_table (htab))
13066             htab->hgot = h;
13067         }
13068       if (h != NULL
13069           && h->root.type == bfd_link_hash_defined
13070           && !h->root.linker_def
13071           && (!is_elf_hash_table (htab)
13072               || h->def_regular))
13073         {
13074           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13075                       + h->root.u.def.section->output_offset
13076                       + h->root.u.def.section->output_section->vma);
13077           _bfd_set_gp_value (obfd, TOCstart);
13078           return TOCstart;
13079         }
13080     }
13081
13082   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13083      order.  The TOC starts where the first of these sections starts.  */
13084   s = bfd_get_section_by_name (obfd, ".got");
13085   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13086     s = bfd_get_section_by_name (obfd, ".toc");
13087   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13088     s = bfd_get_section_by_name (obfd, ".tocbss");
13089   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13090     s = bfd_get_section_by_name (obfd, ".plt");
13091   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13092     {
13093       /* This may happen for
13094          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13095          .toc directive
13096          o  bad linker script
13097          o --gc-sections and empty TOC sections
13098
13099          FIXME: Warn user?  */
13100
13101       /* Look for a likely section.  We probably won't even be
13102          using TOCstart.  */
13103       for (s = obfd->sections; s != NULL; s = s->next)
13104         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13105                          | SEC_EXCLUDE))
13106             == (SEC_ALLOC | SEC_SMALL_DATA))
13107           break;
13108       if (s == NULL)
13109         for (s = obfd->sections; s != NULL; s = s->next)
13110           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13111               == (SEC_ALLOC | SEC_SMALL_DATA))
13112             break;
13113       if (s == NULL)
13114         for (s = obfd->sections; s != NULL; s = s->next)
13115           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13116               == SEC_ALLOC)
13117             break;
13118       if (s == NULL)
13119         for (s = obfd->sections; s != NULL; s = s->next)
13120           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13121             break;
13122     }
13123
13124   TOCstart = 0;
13125   if (s != NULL)
13126     TOCstart = s->output_section->vma + s->output_offset;
13127
13128   /* Force alignment.  */
13129   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13130   TOCstart -= adjust;
13131   _bfd_set_gp_value (obfd, TOCstart);
13132
13133   if (info != NULL && s != NULL)
13134     {
13135       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13136
13137       if (htab != NULL)
13138         {
13139           if (htab->elf.hgot != NULL)
13140             {
13141               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13142               htab->elf.hgot->root.u.def.section = s;
13143             }
13144         }
13145       else
13146         {
13147           struct bfd_link_hash_entry *bh = NULL;
13148           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13149                                             s, TOC_BASE_OFF - adjust,
13150                                             NULL, FALSE, FALSE, &bh);
13151         }
13152     }
13153   return TOCstart;
13154 }
13155
13156 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13157    write out any global entry stubs, and PLT relocations.  */
13158
13159 static bfd_boolean
13160 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13161 {
13162   struct bfd_link_info *info;
13163   struct ppc_link_hash_table *htab;
13164   struct plt_entry *ent;
13165   asection *s;
13166
13167   if (h->root.type == bfd_link_hash_indirect)
13168     return TRUE;
13169
13170   info = inf;
13171   htab = ppc_hash_table (info);
13172   if (htab == NULL)
13173     return FALSE;
13174
13175   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13176     if (ent->plt.offset != (bfd_vma) -1)
13177       {
13178         /* This symbol has an entry in the procedure linkage
13179            table.  Set it up.  */
13180         Elf_Internal_Rela rela;
13181         asection *plt, *relplt;
13182         bfd_byte *loc;
13183
13184         if (!htab->elf.dynamic_sections_created
13185             || h->dynindx == -1)
13186           {
13187             if (!(h->def_regular
13188                   && (h->root.type == bfd_link_hash_defined
13189                       || h->root.type == bfd_link_hash_defweak)))
13190               continue;
13191             if (h->type == STT_GNU_IFUNC)
13192               {
13193                 plt = htab->elf.iplt;
13194                 relplt = htab->elf.irelplt;
13195                 htab->local_ifunc_resolver = 1;
13196                 if (htab->opd_abi)
13197                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13198                 else
13199                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13200               }
13201             else
13202               {
13203                 plt = htab->pltlocal;
13204                 if (bfd_link_pic (info))
13205                   {
13206                     relplt = htab->relpltlocal;
13207                     if (htab->opd_abi)
13208                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13209                     else
13210                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13211                   }
13212                 else
13213                   relplt = NULL;
13214               }
13215             rela.r_addend = (h->root.u.def.value
13216                              + h->root.u.def.section->output_offset
13217                              + h->root.u.def.section->output_section->vma
13218                              + ent->addend);
13219
13220             if (relplt == NULL)
13221               {
13222                 loc = plt->contents + ent->plt.offset;
13223                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13224                 if (htab->opd_abi)
13225                   {
13226                     bfd_vma toc = elf_gp (info->output_bfd);
13227                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13228                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13229                   }
13230               }
13231             else
13232               {
13233                 rela.r_offset = (plt->output_section->vma
13234                                  + plt->output_offset
13235                                  + ent->plt.offset);
13236                 loc = relplt->contents + (relplt->reloc_count++
13237                                           * sizeof (Elf64_External_Rela));
13238                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13239               }
13240           }
13241         else
13242           {
13243             rela.r_offset = (htab->elf.splt->output_section->vma
13244                              + htab->elf.splt->output_offset
13245                              + ent->plt.offset);
13246             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13247             rela.r_addend = ent->addend;
13248             loc = (htab->elf.srelplt->contents
13249                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13250                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13251             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13252               htab->maybe_local_ifunc_resolver = 1;
13253             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13254           }
13255       }
13256
13257   if (!h->pointer_equality_needed)
13258     return TRUE;
13259
13260   if (h->def_regular)
13261     return TRUE;
13262
13263   s = htab->global_entry;
13264   if (s == NULL || s->size == 0)
13265     return TRUE;
13266
13267   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13268     if (ent->plt.offset != (bfd_vma) -1
13269         && ent->addend == 0)
13270       {
13271         bfd_byte *p;
13272         asection *plt;
13273         bfd_vma off;
13274
13275         p = s->contents + h->root.u.def.value;
13276         plt = htab->elf.splt;
13277         if (!htab->elf.dynamic_sections_created
13278             || h->dynindx == -1)
13279           {
13280             if (h->type == STT_GNU_IFUNC)
13281               plt = htab->elf.iplt;
13282             else
13283               plt = htab->pltlocal;
13284           }
13285         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13286         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13287
13288         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13289           {
13290             info->callbacks->einfo
13291               (_("%P: linkage table error against `%pT'\n"),
13292                h->root.root.string);
13293             bfd_set_error (bfd_error_bad_value);
13294             htab->stub_error = TRUE;
13295           }
13296
13297         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13298         if (htab->params->emit_stub_syms)
13299           {
13300             size_t len = strlen (h->root.root.string);
13301             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13302
13303             if (name == NULL)
13304               return FALSE;
13305
13306             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13307             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13308             if (h == NULL)
13309               return FALSE;
13310             if (h->root.type == bfd_link_hash_new)
13311               {
13312                 h->root.type = bfd_link_hash_defined;
13313                 h->root.u.def.section = s;
13314                 h->root.u.def.value = p - s->contents;
13315                 h->ref_regular = 1;
13316                 h->def_regular = 1;
13317                 h->ref_regular_nonweak = 1;
13318                 h->forced_local = 1;
13319                 h->non_elf = 0;
13320                 h->root.linker_def = 1;
13321               }
13322           }
13323
13324         if (PPC_HA (off) != 0)
13325           {
13326             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13327             p += 4;
13328           }
13329         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13330         p += 4;
13331         bfd_put_32 (s->owner, MTCTR_R12, p);
13332         p += 4;
13333         bfd_put_32 (s->owner, BCTR, p);
13334         break;
13335       }
13336   return TRUE;
13337 }
13338
13339 /* Write PLT relocs for locals.  */
13340
13341 static bfd_boolean
13342 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13343 {
13344   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13345   bfd *ibfd;
13346
13347   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13348     {
13349       struct got_entry **lgot_ents, **end_lgot_ents;
13350       struct plt_entry **local_plt, **lplt, **end_local_plt;
13351       Elf_Internal_Shdr *symtab_hdr;
13352       bfd_size_type locsymcount;
13353       Elf_Internal_Sym *local_syms = NULL;
13354       struct plt_entry *ent;
13355
13356       if (!is_ppc64_elf (ibfd))
13357         continue;
13358
13359       lgot_ents = elf_local_got_ents (ibfd);
13360       if (!lgot_ents)
13361         continue;
13362
13363       symtab_hdr = &elf_symtab_hdr (ibfd);
13364       locsymcount = symtab_hdr->sh_info;
13365       end_lgot_ents = lgot_ents + locsymcount;
13366       local_plt = (struct plt_entry **) end_lgot_ents;
13367       end_local_plt = local_plt + locsymcount;
13368       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13369         for (ent = *lplt; ent != NULL; ent = ent->next)
13370           if (ent->plt.offset != (bfd_vma) -1)
13371             {
13372               Elf_Internal_Sym *sym;
13373               asection *sym_sec;
13374               asection *plt, *relplt;
13375               bfd_byte *loc;
13376               bfd_vma val;
13377
13378               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13379                               lplt - local_plt, ibfd))
13380                 {
13381                   if (local_syms != NULL
13382                       && symtab_hdr->contents != (unsigned char *) local_syms)
13383                     free (local_syms);
13384                   return FALSE;
13385                 }
13386
13387               val = sym->st_value + ent->addend;
13388               if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13389                 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13390               if (sym_sec != NULL && sym_sec->output_section != NULL)
13391                 val += sym_sec->output_offset + sym_sec->output_section->vma;
13392
13393               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13394                 {
13395                   htab->local_ifunc_resolver = 1;
13396                   plt = htab->elf.iplt;
13397                   relplt = htab->elf.irelplt;
13398                 }
13399               else
13400                 {
13401                   plt = htab->pltlocal;
13402                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13403                 }
13404
13405               if (relplt == NULL)
13406                 {
13407                   loc = plt->contents + ent->plt.offset;
13408                   bfd_put_64 (info->output_bfd, val, loc);
13409                   if (htab->opd_abi)
13410                     {
13411                       bfd_vma toc = elf_gp (ibfd);
13412                       bfd_put_64 (info->output_bfd, toc, loc + 8);
13413                     }
13414                 }
13415               else
13416                 {
13417                   Elf_Internal_Rela rela;
13418                   rela.r_offset = (ent->plt.offset
13419                                    + plt->output_offset
13420                                    + plt->output_section->vma);
13421                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13422                     {
13423                       if (htab->opd_abi)
13424                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13425                       else
13426                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13427                     }
13428                   else
13429                     {
13430                       if (htab->opd_abi)
13431                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13432                       else
13433                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13434                     }
13435                   rela.r_addend = val;
13436                   loc = relplt->contents + (relplt->reloc_count++
13437                                             * sizeof (Elf64_External_Rela));
13438                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13439                 }
13440             }
13441
13442       if (local_syms != NULL
13443           && symtab_hdr->contents != (unsigned char *) local_syms)
13444         {
13445           if (!info->keep_memory)
13446             free (local_syms);
13447           else
13448             symtab_hdr->contents = (unsigned char *) local_syms;
13449         }
13450     }
13451   return TRUE;
13452 }
13453
13454 /* Build all the stubs associated with the current output file.
13455    The stubs are kept in a hash table attached to the main linker
13456    hash table.  This function is called via gldelf64ppc_finish.  */
13457
13458 bfd_boolean
13459 ppc64_elf_build_stubs (struct bfd_link_info *info,
13460                        char **stats)
13461 {
13462   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13463   struct map_stub *group;
13464   asection *stub_sec;
13465   bfd_byte *p;
13466   int stub_sec_count = 0;
13467
13468   if (htab == NULL)
13469     return FALSE;
13470
13471   /* Allocate memory to hold the linker stubs.  */
13472   for (group = htab->group; group != NULL; group = group->next)
13473     {
13474       group->eh_size = 0;
13475       group->lr_restore = 0;
13476       if ((stub_sec = group->stub_sec) != NULL
13477           && stub_sec->size != 0)
13478         {
13479           stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13480                                            stub_sec->size);
13481           if (stub_sec->contents == NULL)
13482             return FALSE;
13483           stub_sec->size = 0;
13484         }
13485     }
13486
13487   if (htab->glink != NULL && htab->glink->size != 0)
13488     {
13489       unsigned int indx;
13490       bfd_vma plt0;
13491
13492       /* Build the .glink plt call stub.  */
13493       if (htab->params->emit_stub_syms)
13494         {
13495           struct elf_link_hash_entry *h;
13496           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13497                                     TRUE, FALSE, FALSE);
13498           if (h == NULL)
13499             return FALSE;
13500           if (h->root.type == bfd_link_hash_new)
13501             {
13502               h->root.type = bfd_link_hash_defined;
13503               h->root.u.def.section = htab->glink;
13504               h->root.u.def.value = 8;
13505               h->ref_regular = 1;
13506               h->def_regular = 1;
13507               h->ref_regular_nonweak = 1;
13508               h->forced_local = 1;
13509               h->non_elf = 0;
13510               h->root.linker_def = 1;
13511             }
13512         }
13513       plt0 = (htab->elf.splt->output_section->vma
13514               + htab->elf.splt->output_offset
13515               - 16);
13516       if (info->emitrelocations)
13517         {
13518           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13519           if (r == NULL)
13520             return FALSE;
13521           r->r_offset = (htab->glink->output_offset
13522                          + htab->glink->output_section->vma);
13523           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13524           r->r_addend = plt0;
13525         }
13526       p = htab->glink->contents;
13527       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13528       bfd_put_64 (htab->glink->owner, plt0, p);
13529       p += 8;
13530       if (htab->opd_abi)
13531         {
13532           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13533           p += 4;
13534           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13535           p += 4;
13536           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13537           p += 4;
13538           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13539           p += 4;
13540           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13541           p += 4;
13542           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13543           p += 4;
13544           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13545           p += 4;
13546           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13547           p += 4;
13548           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13549           p += 4;
13550           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13551           p += 4;
13552         }
13553       else
13554         {
13555           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13556           p += 4;
13557           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13558           p += 4;
13559           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13560           p += 4;
13561           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13562           p += 4;
13563           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13564           p += 4;
13565           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13566           p += 4;
13567           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13568           p += 4;
13569           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13570           p += 4;
13571           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13572           p += 4;
13573           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13574           p += 4;
13575           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13576           p += 4;
13577           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13578           p += 4;
13579           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13580           p += 4;
13581         }
13582       bfd_put_32 (htab->glink->owner, BCTR, p);
13583       p += 4;
13584       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13585
13586       /* Build the .glink lazy link call stubs.  */
13587       indx = 0;
13588       while (p < htab->glink->contents + htab->glink->size)
13589         {
13590           if (htab->opd_abi)
13591             {
13592               if (indx < 0x8000)
13593                 {
13594                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13595                   p += 4;
13596                 }
13597               else
13598                 {
13599                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13600                   p += 4;
13601                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13602                               p);
13603                   p += 4;
13604                 }
13605             }
13606           bfd_put_32 (htab->glink->owner,
13607                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13608           indx++;
13609           p += 4;
13610         }
13611     }
13612
13613   /* Build .glink global entry stubs, and PLT relocs for globals.  */
13614   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13615
13616   if (!write_plt_relocs_for_local_syms (info))
13617     return FALSE;
13618
13619   if (htab->brlt != NULL && htab->brlt->size != 0)
13620     {
13621       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13622                                          htab->brlt->size);
13623       if (htab->brlt->contents == NULL)
13624         return FALSE;
13625     }
13626   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13627     {
13628       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13629                                             htab->relbrlt->size);
13630       if (htab->relbrlt->contents == NULL)
13631         return FALSE;
13632     }
13633
13634   /* Build the stubs as directed by the stub hash table.  */
13635   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13636
13637   for (group = htab->group; group != NULL; group = group->next)
13638     if (group->needs_save_res)
13639       group->stub_sec->size += htab->sfpr->size;
13640
13641   if (htab->relbrlt != NULL)
13642     htab->relbrlt->reloc_count = 0;
13643
13644   if (htab->params->plt_stub_align != 0)
13645     for (group = htab->group; group != NULL; group = group->next)
13646       if ((stub_sec = group->stub_sec) != NULL)
13647         {
13648           int align = abs (htab->params->plt_stub_align);
13649           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13650         }
13651
13652   for (group = htab->group; group != NULL; group = group->next)
13653     if (group->needs_save_res)
13654       {
13655         stub_sec = group->stub_sec;
13656         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13657                 htab->sfpr->contents, htab->sfpr->size);
13658         if (htab->params->emit_stub_syms)
13659           {
13660             unsigned int i;
13661
13662             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13663               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13664                 return FALSE;
13665           }
13666       }
13667
13668   if (htab->glink_eh_frame != NULL
13669       && htab->glink_eh_frame->size != 0)
13670     {
13671       bfd_vma val;
13672       size_t align = 4;
13673
13674       p = htab->glink_eh_frame->contents;
13675       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13676
13677       for (group = htab->group; group != NULL; group = group->next)
13678         if (group->eh_size != 0)
13679           {
13680             /* Offset to stub section.  */
13681             val = (group->stub_sec->output_section->vma
13682                    + group->stub_sec->output_offset);
13683             val -= (htab->glink_eh_frame->output_section->vma
13684                     + htab->glink_eh_frame->output_offset
13685                     + (p + 8 - htab->glink_eh_frame->contents));
13686             if (val + 0x80000000 > 0xffffffff)
13687               {
13688                 _bfd_error_handler
13689                   (_("%s offset too large for .eh_frame sdata4 encoding"),
13690                    group->stub_sec->name);
13691                 return FALSE;
13692               }
13693             bfd_put_32 (htab->elf.dynobj, val, p + 8);
13694             p += (group->eh_size + 17 + 3) & -4;
13695           }
13696       if (htab->glink != NULL && htab->glink->size != 0)
13697         {
13698           /* Offset to .glink.  */
13699           val = (htab->glink->output_section->vma
13700                  + htab->glink->output_offset
13701                  + 8);
13702           val -= (htab->glink_eh_frame->output_section->vma
13703                   + htab->glink_eh_frame->output_offset
13704                   + (p + 8 - htab->glink_eh_frame->contents));
13705           if (val + 0x80000000 > 0xffffffff)
13706             {
13707               _bfd_error_handler
13708                 (_("%s offset too large for .eh_frame sdata4 encoding"),
13709                  htab->glink->name);
13710               return FALSE;
13711             }
13712           bfd_put_32 (htab->elf.dynobj, val, p + 8);
13713           p += (24 + align - 1) & -align;
13714         }
13715     }
13716
13717   for (group = htab->group; group != NULL; group = group->next)
13718     if ((stub_sec = group->stub_sec) != NULL)
13719       {
13720         stub_sec_count += 1;
13721         if (stub_sec->rawsize != stub_sec->size
13722             && (htab->stub_iteration <= STUB_SHRINK_ITER
13723                 || stub_sec->rawsize < stub_sec->size))
13724           break;
13725       }
13726
13727   if (group != NULL)
13728     {
13729       htab->stub_error = TRUE;
13730       _bfd_error_handler (_("stubs don't match calculated size"));
13731     }
13732
13733   if (htab->stub_error)
13734     return FALSE;
13735
13736   if (stats != NULL)
13737     {
13738       size_t len;
13739       *stats = bfd_malloc (500);
13740       if (*stats == NULL)
13741         return FALSE;
13742
13743       len = sprintf (*stats,
13744                      ngettext ("linker stubs in %u group\n",
13745                                "linker stubs in %u groups\n",
13746                                stub_sec_count),
13747                      stub_sec_count);
13748       sprintf (*stats + len, _("  branch         %lu\n"
13749                                "  branch toc adj %lu\n"
13750                                "  branch notoc   %lu\n"
13751                                "  branch both    %lu\n"
13752                                "  long branch    %lu\n"
13753                                "  long toc adj   %lu\n"
13754                                "  long notoc     %lu\n"
13755                                "  long both      %lu\n"
13756                                "  plt call       %lu\n"
13757                                "  plt call save  %lu\n"
13758                                "  plt call notoc %lu\n"
13759                                "  plt call both  %lu\n"
13760                                "  global entry   %lu"),
13761                htab->stub_count[ppc_stub_long_branch - 1],
13762                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13763                htab->stub_count[ppc_stub_long_branch_notoc - 1],
13764                htab->stub_count[ppc_stub_long_branch_both - 1],
13765                htab->stub_count[ppc_stub_plt_branch - 1],
13766                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13767                htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13768                htab->stub_count[ppc_stub_plt_branch_both - 1],
13769                htab->stub_count[ppc_stub_plt_call - 1],
13770                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13771                htab->stub_count[ppc_stub_plt_call_notoc - 1],
13772                htab->stub_count[ppc_stub_plt_call_both - 1],
13773                htab->stub_count[ppc_stub_global_entry - 1]);
13774     }
13775   return TRUE;
13776 }
13777
13778 /* What to do when ld finds relocations against symbols defined in
13779    discarded sections.  */
13780
13781 static unsigned int
13782 ppc64_elf_action_discarded (asection *sec)
13783 {
13784   if (strcmp (".opd", sec->name) == 0)
13785     return 0;
13786
13787   if (strcmp (".toc", sec->name) == 0)
13788     return 0;
13789
13790   if (strcmp (".toc1", sec->name) == 0)
13791     return 0;
13792
13793   return _bfd_elf_default_action_discarded (sec);
13794 }
13795
13796 /* The RELOCATE_SECTION function is called by the ELF backend linker
13797    to handle the relocations for a section.
13798
13799    The relocs are always passed as Rela structures; if the section
13800    actually uses Rel structures, the r_addend field will always be
13801    zero.
13802
13803    This function is responsible for adjust the section contents as
13804    necessary, and (if using Rela relocs and generating a
13805    relocatable output file) adjusting the reloc addend as
13806    necessary.
13807
13808    This function does not have to worry about setting the reloc
13809    address or the reloc symbol index.
13810
13811    LOCAL_SYMS is a pointer to the swapped in local symbols.
13812
13813    LOCAL_SECTIONS is an array giving the section in the input file
13814    corresponding to the st_shndx field of each local symbol.
13815
13816    The global hash table entry for the global symbols can be found
13817    via elf_sym_hashes (input_bfd).
13818
13819    When generating relocatable output, this function must handle
13820    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13821    going to be the section symbol corresponding to the output
13822    section, which means that the addend must be adjusted
13823    accordingly.  */
13824
13825 static bfd_boolean
13826 ppc64_elf_relocate_section (bfd *output_bfd,
13827                             struct bfd_link_info *info,
13828                             bfd *input_bfd,
13829                             asection *input_section,
13830                             bfd_byte *contents,
13831                             Elf_Internal_Rela *relocs,
13832                             Elf_Internal_Sym *local_syms,
13833                             asection **local_sections)
13834 {
13835   struct ppc_link_hash_table *htab;
13836   Elf_Internal_Shdr *symtab_hdr;
13837   struct elf_link_hash_entry **sym_hashes;
13838   Elf_Internal_Rela *rel;
13839   Elf_Internal_Rela *wrel;
13840   Elf_Internal_Rela *relend;
13841   Elf_Internal_Rela outrel;
13842   bfd_byte *loc;
13843   struct got_entry **local_got_ents;
13844   bfd_vma TOCstart;
13845   bfd_boolean ret = TRUE;
13846   bfd_boolean is_opd;
13847   /* Assume 'at' branch hints.  */
13848   bfd_boolean is_isa_v2 = TRUE;
13849   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13850
13851   /* Initialize howto table if needed.  */
13852   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13853     ppc_howto_init ();
13854
13855   htab = ppc_hash_table (info);
13856   if (htab == NULL)
13857     return FALSE;
13858
13859   /* Don't relocate stub sections.  */
13860   if (input_section->owner == htab->params->stub_bfd)
13861     return TRUE;
13862
13863   if (!is_ppc64_elf (input_bfd))
13864     {
13865       bfd_set_error (bfd_error_wrong_format);
13866       return FALSE;
13867     }
13868
13869   local_got_ents = elf_local_got_ents (input_bfd);
13870   TOCstart = elf_gp (output_bfd);
13871   symtab_hdr = &elf_symtab_hdr (input_bfd);
13872   sym_hashes = elf_sym_hashes (input_bfd);
13873   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13874
13875   rel = wrel = relocs;
13876   relend = relocs + input_section->reloc_count;
13877   for (; rel < relend; wrel++, rel++)
13878     {
13879       enum elf_ppc64_reloc_type r_type;
13880       bfd_vma addend;
13881       bfd_reloc_status_type r;
13882       Elf_Internal_Sym *sym;
13883       asection *sec;
13884       struct elf_link_hash_entry *h_elf;
13885       struct ppc_link_hash_entry *h;
13886       struct ppc_link_hash_entry *fdh;
13887       const char *sym_name;
13888       unsigned long r_symndx, toc_symndx;
13889       bfd_vma toc_addend;
13890       unsigned char tls_mask, tls_gd, tls_type;
13891       unsigned char sym_type;
13892       bfd_vma relocation;
13893       bfd_boolean unresolved_reloc, save_unresolved_reloc;
13894       bfd_boolean warned;
13895       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13896       unsigned int insn;
13897       unsigned int mask;
13898       struct ppc_stub_hash_entry *stub_entry;
13899       bfd_vma max_br_offset;
13900       bfd_vma from;
13901       Elf_Internal_Rela orig_rel;
13902       reloc_howto_type *howto;
13903       struct reloc_howto_struct alt_howto;
13904       uint64_t pinsn;
13905       bfd_vma offset;
13906
13907     again:
13908       orig_rel = *rel;
13909
13910       r_type = ELF64_R_TYPE (rel->r_info);
13911       r_symndx = ELF64_R_SYM (rel->r_info);
13912
13913       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13914          symbol of the previous ADDR64 reloc.  The symbol gives us the
13915          proper TOC base to use.  */
13916       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13917           && wrel != relocs
13918           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13919           && is_opd)
13920         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13921
13922       sym = NULL;
13923       sec = NULL;
13924       h_elf = NULL;
13925       sym_name = NULL;
13926       unresolved_reloc = FALSE;
13927       warned = FALSE;
13928
13929       if (r_symndx < symtab_hdr->sh_info)
13930         {
13931           /* It's a local symbol.  */
13932           struct _opd_sec_data *opd;
13933
13934           sym = local_syms + r_symndx;
13935           sec = local_sections[r_symndx];
13936           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13937           sym_type = ELF64_ST_TYPE (sym->st_info);
13938           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13939           opd = get_opd_info (sec);
13940           if (opd != NULL && opd->adjust != NULL)
13941             {
13942               long adjust = opd->adjust[OPD_NDX (sym->st_value
13943                                                  + rel->r_addend)];
13944               if (adjust == -1)
13945                 relocation = 0;
13946               else
13947                 {
13948                   /* If this is a relocation against the opd section sym
13949                      and we have edited .opd, adjust the reloc addend so
13950                      that ld -r and ld --emit-relocs output is correct.
13951                      If it is a reloc against some other .opd symbol,
13952                      then the symbol value will be adjusted later.  */
13953                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13954                     rel->r_addend += adjust;
13955                   else
13956                     relocation += adjust;
13957                 }
13958             }
13959         }
13960       else
13961         {
13962           bfd_boolean ignored;
13963
13964           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13965                                    r_symndx, symtab_hdr, sym_hashes,
13966                                    h_elf, sec, relocation,
13967                                    unresolved_reloc, warned, ignored);
13968           sym_name = h_elf->root.root.string;
13969           sym_type = h_elf->type;
13970           if (sec != NULL
13971               && sec->owner == output_bfd
13972               && strcmp (sec->name, ".opd") == 0)
13973             {
13974               /* This is a symbol defined in a linker script.  All
13975                  such are defined in output sections, even those
13976                  defined by simple assignment from a symbol defined in
13977                  an input section.  Transfer the symbol to an
13978                  appropriate input .opd section, so that a branch to
13979                  this symbol will be mapped to the location specified
13980                  by the opd entry.  */
13981               struct bfd_link_order *lo;
13982               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13983                 if (lo->type == bfd_indirect_link_order)
13984                   {
13985                     asection *isec = lo->u.indirect.section;
13986                     if (h_elf->root.u.def.value >= isec->output_offset
13987                         && h_elf->root.u.def.value < (isec->output_offset
13988                                                       + isec->size))
13989                       {
13990                         h_elf->root.u.def.value -= isec->output_offset;
13991                         h_elf->root.u.def.section = isec;
13992                         sec = isec;
13993                         break;
13994                       }
13995                   }
13996             }
13997         }
13998       h = (struct ppc_link_hash_entry *) h_elf;
13999
14000       if (sec != NULL && discarded_section (sec))
14001         {
14002           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14003                                input_bfd, input_section,
14004                                contents, rel->r_offset);
14005           wrel->r_offset = rel->r_offset;
14006           wrel->r_info = 0;
14007           wrel->r_addend = 0;
14008
14009           /* For ld -r, remove relocations in debug sections against
14010              symbols defined in discarded sections.  Not done for
14011              non-debug to preserve relocs in .eh_frame which the
14012              eh_frame editing code expects to be present.  */
14013           if (bfd_link_relocatable (info)
14014               && (input_section->flags & SEC_DEBUGGING))
14015             wrel--;
14016
14017           continue;
14018         }
14019
14020       if (bfd_link_relocatable (info))
14021         goto copy_reloc;
14022
14023       if (h != NULL && &h->elf == htab->elf.hgot)
14024         {
14025           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14026           sec = bfd_abs_section_ptr;
14027           unresolved_reloc = FALSE;
14028         }
14029
14030       /* TLS optimizations.  Replace instruction sequences and relocs
14031          based on information we collected in tls_optimize.  We edit
14032          RELOCS so that --emit-relocs will output something sensible
14033          for the final instruction stream.  */
14034       tls_mask = 0;
14035       tls_gd = 0;
14036       toc_symndx = 0;
14037       if (h != NULL)
14038         tls_mask = h->tls_mask;
14039       else if (local_got_ents != NULL)
14040         {
14041           struct plt_entry **local_plt = (struct plt_entry **)
14042             (local_got_ents + symtab_hdr->sh_info);
14043           unsigned char *lgot_masks = (unsigned char *)
14044             (local_plt + symtab_hdr->sh_info);
14045           tls_mask = lgot_masks[r_symndx];
14046         }
14047       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14048           && (r_type == R_PPC64_TLS
14049               || r_type == R_PPC64_TLSGD
14050               || r_type == R_PPC64_TLSLD))
14051         {
14052           /* Check for toc tls entries.  */
14053           unsigned char *toc_tls;
14054
14055           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14056                              &local_syms, rel, input_bfd))
14057             return FALSE;
14058
14059           if (toc_tls)
14060             tls_mask = *toc_tls;
14061         }
14062
14063       /* Check that tls relocs are used with tls syms, and non-tls
14064          relocs are used with non-tls syms.  */
14065       if (r_symndx != STN_UNDEF
14066           && r_type != R_PPC64_NONE
14067           && (h == NULL
14068               || h->elf.root.type == bfd_link_hash_defined
14069               || h->elf.root.type == bfd_link_hash_defweak)
14070           && (IS_PPC64_TLS_RELOC (r_type)
14071               != (sym_type == STT_TLS
14072                   || (sym_type == STT_SECTION
14073                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14074         {
14075           if ((tls_mask & TLS_TLS) != 0
14076               && (r_type == R_PPC64_TLS
14077                   || r_type == R_PPC64_TLSGD
14078                   || r_type == R_PPC64_TLSLD))
14079             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
14080             ;
14081           else
14082             info->callbacks->einfo
14083               (!IS_PPC64_TLS_RELOC (r_type)
14084                /* xgettext:c-format */
14085                ? _("%H: %s used with TLS symbol `%pT'\n")
14086                /* xgettext:c-format */
14087                : _("%H: %s used with non-TLS symbol `%pT'\n"),
14088                input_bfd, input_section, rel->r_offset,
14089                ppc64_elf_howto_table[r_type]->name,
14090                sym_name);
14091         }
14092
14093       /* Ensure reloc mapping code below stays sane.  */
14094       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14095           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14096           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
14097           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14098           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14099           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14100           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
14101           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14102           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14103           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14104         abort ();
14105
14106       switch (r_type)
14107         {
14108         default:
14109           break;
14110
14111         case R_PPC64_LO_DS_OPT:
14112           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14113           if ((insn & (0x3f << 26)) != 58u << 26)
14114             abort ();
14115           insn += (14u << 26) - (58u << 26);
14116           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14117           r_type = R_PPC64_TOC16_LO;
14118           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14119           break;
14120
14121         case R_PPC64_TOC16:
14122         case R_PPC64_TOC16_LO:
14123         case R_PPC64_TOC16_DS:
14124         case R_PPC64_TOC16_LO_DS:
14125           {
14126             /* Check for toc tls entries.  */
14127             unsigned char *toc_tls;
14128             int retval;
14129
14130             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14131                                    &local_syms, rel, input_bfd);
14132             if (retval == 0)
14133               return FALSE;
14134
14135             if (toc_tls)
14136               {
14137                 tls_mask = *toc_tls;
14138                 if (r_type == R_PPC64_TOC16_DS
14139                     || r_type == R_PPC64_TOC16_LO_DS)
14140                   {
14141                     if ((tls_mask & TLS_TLS) != 0
14142                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14143                       goto toctprel;
14144                   }
14145                 else
14146                   {
14147                     /* If we found a GD reloc pair, then we might be
14148                        doing a GD->IE transition.  */
14149                     if (retval == 2)
14150                       {
14151                         tls_gd = TLS_TPRELGD;
14152                         if ((tls_mask & TLS_TLS) != 0
14153                             && (tls_mask & TLS_GD) == 0)
14154                           goto tls_ldgd_opt;
14155                       }
14156                     else if (retval == 3)
14157                       {
14158                         if ((tls_mask & TLS_TLS) != 0
14159                             && (tls_mask & TLS_LD) == 0)
14160                           goto tls_ldgd_opt;
14161                       }
14162                   }
14163               }
14164           }
14165           break;
14166
14167         case R_PPC64_GOT_TPREL16_HI:
14168         case R_PPC64_GOT_TPREL16_HA:
14169           if ((tls_mask & TLS_TLS) != 0
14170               && (tls_mask & TLS_TPREL) == 0)
14171             {
14172               rel->r_offset -= d_offset;
14173               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14174               r_type = R_PPC64_NONE;
14175               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14176             }
14177           break;
14178
14179         case R_PPC64_GOT_TPREL16_DS:
14180         case R_PPC64_GOT_TPREL16_LO_DS:
14181           if ((tls_mask & TLS_TLS) != 0
14182               && (tls_mask & TLS_TPREL) == 0)
14183             {
14184             toctprel:
14185               insn = bfd_get_32 (input_bfd,
14186                                  contents + rel->r_offset - d_offset);
14187               insn &= 31 << 21;
14188               insn |= 0x3c0d0000;       /* addis 0,13,0 */
14189               bfd_put_32 (input_bfd, insn,
14190                           contents + rel->r_offset - d_offset);
14191               r_type = R_PPC64_TPREL16_HA;
14192               if (toc_symndx != 0)
14193                 {
14194                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14195                   rel->r_addend = toc_addend;
14196                   /* We changed the symbol.  Start over in order to
14197                      get h, sym, sec etc. right.  */
14198                   goto again;
14199                 }
14200               else
14201                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14202             }
14203           break;
14204
14205         case R_PPC64_TLS:
14206           if ((tls_mask & TLS_TLS) != 0
14207               && (tls_mask & TLS_TPREL) == 0)
14208             {
14209               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14210               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14211               if (insn == 0)
14212                 abort ();
14213               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14214               /* Was PPC64_TLS which sits on insn boundary, now
14215                  PPC64_TPREL16_LO which is at low-order half-word.  */
14216               rel->r_offset += d_offset;
14217               r_type = R_PPC64_TPREL16_LO;
14218               if (toc_symndx != 0)
14219                 {
14220                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14221                   rel->r_addend = toc_addend;
14222                   /* We changed the symbol.  Start over in order to
14223                      get h, sym, sec etc. right.  */
14224                   goto again;
14225                 }
14226               else
14227                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14228             }
14229           break;
14230
14231         case R_PPC64_GOT_TLSGD16_HI:
14232         case R_PPC64_GOT_TLSGD16_HA:
14233           tls_gd = TLS_TPRELGD;
14234           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14235             goto tls_gdld_hi;
14236           break;
14237
14238         case R_PPC64_GOT_TLSLD16_HI:
14239         case R_PPC64_GOT_TLSLD16_HA:
14240           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14241             {
14242             tls_gdld_hi:
14243               if ((tls_mask & tls_gd) != 0)
14244                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14245                           + R_PPC64_GOT_TPREL16_DS);
14246               else
14247                 {
14248                   rel->r_offset -= d_offset;
14249                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14250                   r_type = R_PPC64_NONE;
14251                 }
14252               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14253             }
14254           break;
14255
14256         case R_PPC64_GOT_TLSGD16:
14257         case R_PPC64_GOT_TLSGD16_LO:
14258           tls_gd = TLS_TPRELGD;
14259           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14260             goto tls_ldgd_opt;
14261           break;
14262
14263         case R_PPC64_GOT_TLSLD16:
14264         case R_PPC64_GOT_TLSLD16_LO:
14265           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14266             {
14267               unsigned int insn1, insn2;
14268
14269             tls_ldgd_opt:
14270               offset = (bfd_vma) -1;
14271               /* If not using the newer R_PPC64_TLSGD/LD to mark
14272                  __tls_get_addr calls, we must trust that the call
14273                  stays with its arg setup insns, ie. that the next
14274                  reloc is the __tls_get_addr call associated with
14275                  the current reloc.  Edit both insns.  */
14276               if (input_section->has_tls_get_addr_call
14277                   && rel + 1 < relend
14278                   && branch_reloc_hash_match (input_bfd, rel + 1,
14279                                               htab->tls_get_addr,
14280                                               htab->tls_get_addr_fd))
14281                 offset = rel[1].r_offset;
14282               /* We read the low GOT_TLS (or TOC16) insn because we
14283                  need to keep the destination reg.  It may be
14284                  something other than the usual r3, and moved to r3
14285                  before the call by intervening code.  */
14286               insn1 = bfd_get_32 (input_bfd,
14287                                   contents + rel->r_offset - d_offset);
14288               if ((tls_mask & tls_gd) != 0)
14289                 {
14290                   /* IE */
14291                   insn1 &= (0x1f << 21) | (0x1f << 16);
14292                   insn1 |= 58 << 26;    /* ld */
14293                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14294                   if (offset != (bfd_vma) -1)
14295                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14296                   if ((tls_mask & TLS_EXPLICIT) == 0)
14297                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14298                               + R_PPC64_GOT_TPREL16_DS);
14299                   else
14300                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14301                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14302                 }
14303               else
14304                 {
14305                   /* LE */
14306                   insn1 &= 0x1f << 21;
14307                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
14308                   insn2 = 0x38630000;   /* addi 3,3,0 */
14309                   if (tls_gd == 0)
14310                     {
14311                       /* Was an LD reloc.  */
14312                       if (toc_symndx)
14313                         sec = local_sections[toc_symndx];
14314                       for (r_symndx = 0;
14315                            r_symndx < symtab_hdr->sh_info;
14316                            r_symndx++)
14317                         if (local_sections[r_symndx] == sec)
14318                           break;
14319                       if (r_symndx >= symtab_hdr->sh_info)
14320                         r_symndx = STN_UNDEF;
14321                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14322                       if (r_symndx != STN_UNDEF)
14323                         rel->r_addend -= (local_syms[r_symndx].st_value
14324                                           + sec->output_offset
14325                                           + sec->output_section->vma);
14326                     }
14327                   else if (toc_symndx != 0)
14328                     {
14329                       r_symndx = toc_symndx;
14330                       rel->r_addend = toc_addend;
14331                     }
14332                   r_type = R_PPC64_TPREL16_HA;
14333                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14334                   if (offset != (bfd_vma) -1)
14335                     {
14336                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14337                                                     R_PPC64_TPREL16_LO);
14338                       rel[1].r_offset = offset + d_offset;
14339                       rel[1].r_addend = rel->r_addend;
14340                     }
14341                 }
14342               bfd_put_32 (input_bfd, insn1,
14343                           contents + rel->r_offset - d_offset);
14344               if (offset != (bfd_vma) -1)
14345                 {
14346                   bfd_put_32 (input_bfd, insn2, contents + offset);
14347                   if (offset + 8 <= input_section->size)
14348                     {
14349                       insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14350                       if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14351                         bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14352                     }
14353                 }
14354               if ((tls_mask & tls_gd) == 0
14355                   && (tls_gd == 0 || toc_symndx != 0))
14356                 {
14357                   /* We changed the symbol.  Start over in order
14358                      to get h, sym, sec etc. right.  */
14359                   goto again;
14360                 }
14361             }
14362           break;
14363
14364         case R_PPC64_TLSGD:
14365           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14366               && rel + 1 < relend)
14367             {
14368               unsigned int insn2;
14369               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14370
14371               offset = rel->r_offset;
14372               if (is_plt_seq_reloc (r_type1))
14373                 {
14374                   bfd_put_32 (output_bfd, NOP, contents + offset);
14375                   if (r_type1 == R_PPC64_PLT_PCREL34
14376                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14377                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14378                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14379                   break;
14380                 }
14381
14382               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14383                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14384
14385               if ((tls_mask & TLS_TPRELGD) != 0)
14386                 {
14387                   /* IE */
14388                   r_type = R_PPC64_NONE;
14389                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14390                 }
14391               else
14392                 {
14393                   /* LE */
14394                   if (toc_symndx != 0)
14395                     {
14396                       r_symndx = toc_symndx;
14397                       rel->r_addend = toc_addend;
14398                     }
14399                   r_type = R_PPC64_TPREL16_LO;
14400                   rel->r_offset = offset + d_offset;
14401                   insn2 = 0x38630000;   /* addi 3,3,0 */
14402                 }
14403               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14404               /* Zap the reloc on the _tls_get_addr call too.  */
14405               BFD_ASSERT (offset == rel[1].r_offset);
14406               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14407               bfd_put_32 (input_bfd, insn2, contents + offset);
14408               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14409                 goto again;
14410             }
14411           break;
14412
14413         case R_PPC64_TLSLD:
14414           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14415               && rel + 1 < relend)
14416             {
14417               unsigned int insn2;
14418               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14419
14420               offset = rel->r_offset;
14421               if (is_plt_seq_reloc (r_type1))
14422                 {
14423                   bfd_put_32 (output_bfd, NOP, contents + offset);
14424                   if (r_type1 == R_PPC64_PLT_PCREL34
14425                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14426                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14427                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14428                   break;
14429                 }
14430
14431               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14432                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14433
14434               if (toc_symndx)
14435                 sec = local_sections[toc_symndx];
14436               for (r_symndx = 0;
14437                    r_symndx < symtab_hdr->sh_info;
14438                    r_symndx++)
14439                 if (local_sections[r_symndx] == sec)
14440                   break;
14441               if (r_symndx >= symtab_hdr->sh_info)
14442                 r_symndx = STN_UNDEF;
14443               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14444               if (r_symndx != STN_UNDEF)
14445                 rel->r_addend -= (local_syms[r_symndx].st_value
14446                                   + sec->output_offset
14447                                   + sec->output_section->vma);
14448
14449               r_type = R_PPC64_TPREL16_LO;
14450               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14451               rel->r_offset = offset + d_offset;
14452               /* Zap the reloc on the _tls_get_addr call too.  */
14453               BFD_ASSERT (offset == rel[1].r_offset);
14454               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14455               insn2 = 0x38630000;       /* addi 3,3,0 */
14456               bfd_put_32 (input_bfd, insn2, contents + offset);
14457               goto again;
14458             }
14459           break;
14460
14461         case R_PPC64_DTPMOD64:
14462           if (rel + 1 < relend
14463               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14464               && rel[1].r_offset == rel->r_offset + 8)
14465             {
14466               if ((tls_mask & TLS_GD) == 0)
14467                 {
14468                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14469                   if ((tls_mask & TLS_TPRELGD) != 0)
14470                     r_type = R_PPC64_TPREL64;
14471                   else
14472                     {
14473                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14474                       r_type = R_PPC64_NONE;
14475                     }
14476                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14477                 }
14478             }
14479           else
14480             {
14481               if ((tls_mask & TLS_LD) == 0)
14482                 {
14483                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14484                   r_type = R_PPC64_NONE;
14485                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14486                 }
14487             }
14488           break;
14489
14490         case R_PPC64_TPREL64:
14491           if ((tls_mask & TLS_TPREL) == 0)
14492             {
14493               r_type = R_PPC64_NONE;
14494               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14495             }
14496           break;
14497
14498         case R_PPC64_ENTRY:
14499           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14500           if (!bfd_link_pic (info)
14501               && !info->traditional_format
14502               && relocation + 0x80008000 <= 0xffffffff)
14503             {
14504               unsigned int insn1, insn2;
14505
14506               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14507               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14508               if ((insn1 & ~0xfffc) == LD_R2_0R12
14509                   && insn2 == ADD_R2_R2_R12)
14510                 {
14511                   bfd_put_32 (input_bfd,
14512                               LIS_R2 + PPC_HA (relocation),
14513                               contents + rel->r_offset);
14514                   bfd_put_32 (input_bfd,
14515                               ADDI_R2_R2 + PPC_LO (relocation),
14516                               contents + rel->r_offset + 4);
14517                 }
14518             }
14519           else
14520             {
14521               relocation -= (rel->r_offset
14522                              + input_section->output_offset
14523                              + input_section->output_section->vma);
14524               if (relocation + 0x80008000 <= 0xffffffff)
14525                 {
14526                   unsigned int insn1, insn2;
14527
14528                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14529                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14530                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14531                       && insn2 == ADD_R2_R2_R12)
14532                     {
14533                       bfd_put_32 (input_bfd,
14534                                   ADDIS_R2_R12 + PPC_HA (relocation),
14535                                   contents + rel->r_offset);
14536                       bfd_put_32 (input_bfd,
14537                                   ADDI_R2_R2 + PPC_LO (relocation),
14538                                   contents + rel->r_offset + 4);
14539                     }
14540                 }
14541             }
14542           break;
14543
14544         case R_PPC64_REL16_HA:
14545           /* If we are generating a non-PIC executable, edit
14546              .  0:      addis 2,12,.TOC.-0b@ha
14547              .          addi 2,2,.TOC.-0b@l
14548              used by ELFv2 global entry points to set up r2, to
14549              .          lis 2,.TOC.@ha
14550              .          addi 2,2,.TOC.@l
14551              if .TOC. is in range.  */
14552           if (!bfd_link_pic (info)
14553               && !info->traditional_format
14554               && !htab->opd_abi
14555               && rel->r_addend == d_offset
14556               && h != NULL && &h->elf == htab->elf.hgot
14557               && rel + 1 < relend
14558               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14559               && rel[1].r_offset == rel->r_offset + 4
14560               && rel[1].r_addend == rel->r_addend + 4
14561               && relocation + 0x80008000 <= 0xffffffff)
14562             {
14563               unsigned int insn1, insn2;
14564               offset = rel->r_offset - d_offset;
14565               insn1 = bfd_get_32 (input_bfd, contents + offset);
14566               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14567               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14568                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14569                 {
14570                   r_type = R_PPC64_ADDR16_HA;
14571                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14572                   rel->r_addend -= d_offset;
14573                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14574                   rel[1].r_addend -= d_offset + 4;
14575                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14576                 }
14577             }
14578           break;
14579         }
14580
14581       /* Handle other relocations that tweak non-addend part of insn.  */
14582       insn = 0;
14583       max_br_offset = 1 << 25;
14584       addend = rel->r_addend;
14585       reloc_dest = DEST_NORMAL;
14586       switch (r_type)
14587         {
14588         default:
14589           break;
14590
14591         case R_PPC64_TOCSAVE:
14592           if (relocation + addend == (rel->r_offset
14593                                       + input_section->output_offset
14594                                       + input_section->output_section->vma)
14595               && tocsave_find (htab, NO_INSERT,
14596                                &local_syms, rel, input_bfd))
14597             {
14598               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14599               if (insn == NOP
14600                   || insn == CROR_151515 || insn == CROR_313131)
14601                 bfd_put_32 (input_bfd,
14602                             STD_R2_0R1 + STK_TOC (htab),
14603                             contents + rel->r_offset);
14604             }
14605           break;
14606
14607           /* Branch taken prediction relocations.  */
14608         case R_PPC64_ADDR14_BRTAKEN:
14609         case R_PPC64_REL14_BRTAKEN:
14610           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14611           /* Fall through.  */
14612
14613           /* Branch not taken prediction relocations.  */
14614         case R_PPC64_ADDR14_BRNTAKEN:
14615         case R_PPC64_REL14_BRNTAKEN:
14616           insn |= bfd_get_32 (input_bfd,
14617                               contents + rel->r_offset) & ~(0x01 << 21);
14618           /* Fall through.  */
14619
14620         case R_PPC64_REL14:
14621           max_br_offset = 1 << 15;
14622           /* Fall through.  */
14623
14624         case R_PPC64_REL24:
14625         case R_PPC64_REL24_NOTOC:
14626         case R_PPC64_PLTCALL:
14627         case R_PPC64_PLTCALL_NOTOC:
14628           /* Calls to functions with a different TOC, such as calls to
14629              shared objects, need to alter the TOC pointer.  This is
14630              done using a linkage stub.  A REL24 branching to these
14631              linkage stubs needs to be followed by a nop, as the nop
14632              will be replaced with an instruction to restore the TOC
14633              base pointer.  */
14634           fdh = h;
14635           if (h != NULL
14636               && h->oh != NULL
14637               && h->oh->is_func_descriptor)
14638             fdh = ppc_follow_link (h->oh);
14639           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14640                                            htab);
14641           if ((r_type == R_PPC64_PLTCALL
14642                || r_type == R_PPC64_PLTCALL_NOTOC)
14643               && stub_entry != NULL
14644               && stub_entry->stub_type >= ppc_stub_plt_call
14645               && stub_entry->stub_type <= ppc_stub_plt_call_both)
14646             stub_entry = NULL;
14647
14648           if (stub_entry != NULL
14649               && ((stub_entry->stub_type >= ppc_stub_plt_call
14650                    && stub_entry->stub_type <= ppc_stub_plt_call_both)
14651                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14652                   || stub_entry->stub_type == ppc_stub_plt_branch_both
14653                   || stub_entry->stub_type == ppc_stub_long_branch_r2off
14654                   || stub_entry->stub_type == ppc_stub_long_branch_both))
14655             {
14656               bfd_boolean can_plt_call = FALSE;
14657
14658               if (stub_entry->stub_type == ppc_stub_plt_call
14659                   && !htab->opd_abi
14660                   && htab->params->plt_localentry0 != 0
14661                   && is_elfv2_localentry0 (&h->elf))
14662                 {
14663                   /* The function doesn't use or change r2.  */
14664                   can_plt_call = TRUE;
14665                 }
14666               else if (r_type == R_PPC64_REL24_NOTOC)
14667                 {
14668                   /* NOTOC calls don't need to restore r2.  */
14669                   can_plt_call = TRUE;
14670                 }
14671
14672               /* All of these stubs may modify r2, so there must be a
14673                  branch and link followed by a nop.  The nop is
14674                  replaced by an insn to restore r2.  */
14675               else if (rel->r_offset + 8 <= input_section->size)
14676                 {
14677                   unsigned long br;
14678
14679                   br = bfd_get_32 (input_bfd,
14680                                    contents + rel->r_offset);
14681                   if ((br & 1) != 0)
14682                     {
14683                       unsigned long nop;
14684
14685                       nop = bfd_get_32 (input_bfd,
14686                                         contents + rel->r_offset + 4);
14687                       if (nop == LD_R2_0R1 + STK_TOC (htab))
14688                         can_plt_call = TRUE;
14689                       else if (nop == NOP
14690                                || nop == CROR_151515
14691                                || nop == CROR_313131)
14692                         {
14693                           if (h != NULL
14694                               && (h == htab->tls_get_addr_fd
14695                                   || h == htab->tls_get_addr)
14696                               && htab->params->tls_get_addr_opt)
14697                             {
14698                               /* Special stub used, leave nop alone.  */
14699                             }
14700                           else
14701                             bfd_put_32 (input_bfd,
14702                                         LD_R2_0R1 + STK_TOC (htab),
14703                                         contents + rel->r_offset + 4);
14704                           can_plt_call = TRUE;
14705                         }
14706                     }
14707                 }
14708
14709               if (!can_plt_call && h != NULL)
14710                 {
14711                   const char *name = h->elf.root.root.string;
14712
14713                   if (*name == '.')
14714                     ++name;
14715
14716                   if (strncmp (name, "__libc_start_main", 17) == 0
14717                       && (name[17] == 0 || name[17] == '@'))
14718                     {
14719                       /* Allow crt1 branch to go via a toc adjusting
14720                          stub.  Other calls that never return could do
14721                          the same, if we could detect such.  */
14722                       can_plt_call = TRUE;
14723                     }
14724                 }
14725
14726               if (!can_plt_call)
14727                 {
14728                   /* g++ as of 20130507 emits self-calls without a
14729                      following nop.  This is arguably wrong since we
14730                      have conflicting information.  On the one hand a
14731                      global symbol and on the other a local call
14732                      sequence, but don't error for this special case.
14733                      It isn't possible to cheaply verify we have
14734                      exactly such a call.  Allow all calls to the same
14735                      section.  */
14736                   asection *code_sec = sec;
14737
14738                   if (get_opd_info (sec) != NULL)
14739                     {
14740                       bfd_vma off = (relocation + addend
14741                                      - sec->output_section->vma
14742                                      - sec->output_offset);
14743
14744                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14745                     }
14746                   if (code_sec == input_section)
14747                     can_plt_call = TRUE;
14748                 }
14749
14750               if (!can_plt_call)
14751                 {
14752                   if (stub_entry->stub_type >= ppc_stub_plt_call
14753                       && stub_entry->stub_type <= ppc_stub_plt_call_both)
14754                     info->callbacks->einfo
14755                       /* xgettext:c-format */
14756                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14757                          "(plt call stub)\n"),
14758                        input_bfd, input_section, rel->r_offset, sym_name);
14759                   else
14760                     info->callbacks->einfo
14761                       /* xgettext:c-format */
14762                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14763                          "(toc save/adjust stub)\n"),
14764                        input_bfd, input_section, rel->r_offset, sym_name);
14765
14766                   bfd_set_error (bfd_error_bad_value);
14767                   ret = FALSE;
14768                 }
14769
14770               if (can_plt_call
14771                   && stub_entry->stub_type >= ppc_stub_plt_call
14772                   && stub_entry->stub_type <= ppc_stub_plt_call_both)
14773                 unresolved_reloc = FALSE;
14774             }
14775
14776           if ((stub_entry == NULL
14777                || stub_entry->stub_type == ppc_stub_long_branch
14778                || stub_entry->stub_type == ppc_stub_plt_branch)
14779               && get_opd_info (sec) != NULL)
14780             {
14781               /* The branch destination is the value of the opd entry. */
14782               bfd_vma off = (relocation + addend
14783                              - sec->output_section->vma
14784                              - sec->output_offset);
14785               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14786               if (dest != (bfd_vma) -1)
14787                 {
14788                   relocation = dest;
14789                   addend = 0;
14790                   reloc_dest = DEST_OPD;
14791                 }
14792             }
14793
14794           /* If the branch is out of reach we ought to have a long
14795              branch stub.  */
14796           from = (rel->r_offset
14797                   + input_section->output_offset
14798                   + input_section->output_section->vma);
14799
14800           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14801                                                   ? fdh->elf.other
14802                                                   : sym->st_other);
14803
14804           if (stub_entry != NULL
14805               && (stub_entry->stub_type == ppc_stub_long_branch
14806                   || stub_entry->stub_type == ppc_stub_plt_branch)
14807               && (r_type == R_PPC64_ADDR14_BRTAKEN
14808                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14809                   || (relocation + addend - from + max_br_offset
14810                       < 2 * max_br_offset)))
14811             /* Don't use the stub if this branch is in range.  */
14812             stub_entry = NULL;
14813
14814           if (stub_entry != NULL
14815               && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14816                   || stub_entry->stub_type == ppc_stub_long_branch_both
14817                   || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14818                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14819               && (r_type != R_PPC64_REL24_NOTOC
14820                   || ((fdh ? fdh->elf.other : sym->st_other)
14821                       & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14822               && (relocation + addend - from + max_br_offset
14823                   < 2 * max_br_offset))
14824             stub_entry = NULL;
14825
14826           if (stub_entry != NULL
14827               && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14828                   || stub_entry->stub_type == ppc_stub_long_branch_both
14829                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14830                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14831               && r_type == R_PPC64_REL24_NOTOC
14832               && (relocation + addend - from + max_br_offset
14833                   < 2 * max_br_offset))
14834             stub_entry = NULL;
14835
14836           if (stub_entry != NULL)
14837             {
14838               /* Munge up the value and addend so that we call the stub
14839                  rather than the procedure directly.  */
14840               asection *stub_sec = stub_entry->group->stub_sec;
14841
14842               if (stub_entry->stub_type == ppc_stub_save_res)
14843                 relocation += (stub_sec->output_offset
14844                                + stub_sec->output_section->vma
14845                                + stub_sec->size - htab->sfpr->size
14846                                - htab->sfpr->output_offset
14847                                - htab->sfpr->output_section->vma);
14848               else
14849                 relocation = (stub_entry->stub_offset
14850                               + stub_sec->output_offset
14851                               + stub_sec->output_section->vma);
14852               addend = 0;
14853               reloc_dest = DEST_STUB;
14854
14855               if (((stub_entry->stub_type == ppc_stub_plt_call
14856                     && ALWAYS_EMIT_R2SAVE)
14857                    || stub_entry->stub_type == ppc_stub_plt_call_r2save
14858                    || stub_entry->stub_type == ppc_stub_plt_call_both)
14859                   && !(h != NULL
14860                        && (h == htab->tls_get_addr_fd
14861                            || h == htab->tls_get_addr)
14862                        && htab->params->tls_get_addr_opt)
14863                   && rel + 1 < relend
14864                   && rel[1].r_offset == rel->r_offset + 4
14865                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14866                 relocation += 4;
14867               else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14868                         || stub_entry->stub_type == ppc_stub_plt_branch_both
14869                         || stub_entry->stub_type == ppc_stub_plt_call_both)
14870                        && r_type == R_PPC64_REL24_NOTOC)
14871                 relocation += 4;
14872
14873               if (r_type == R_PPC64_REL24_NOTOC
14874                   && (stub_entry->stub_type == ppc_stub_plt_call_notoc
14875                       || stub_entry->stub_type == ppc_stub_plt_call_both))
14876                 htab->notoc_plt = 1;
14877             }
14878
14879           if (insn != 0)
14880             {
14881               if (is_isa_v2)
14882                 {
14883                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14884                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14885                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14886                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14887                     insn |= 0x02 << 21;
14888                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14889                     insn |= 0x08 << 21;
14890                   else
14891                     break;
14892                 }
14893               else
14894                 {
14895                   /* Invert 'y' bit if not the default.  */
14896                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14897                     insn ^= 0x01 << 21;
14898                 }
14899
14900               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14901             }
14902
14903           /* NOP out calls to undefined weak functions.
14904              We can thus call a weak function without first
14905              checking whether the function is defined.  */
14906           else if (h != NULL
14907                    && h->elf.root.type == bfd_link_hash_undefweak
14908                    && h->elf.dynindx == -1
14909                    && (r_type == R_PPC64_REL24
14910                        || r_type == R_PPC64_REL24_NOTOC)
14911                    && relocation == 0
14912                    && addend == 0)
14913             {
14914               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14915               goto copy_reloc;
14916             }
14917           break;
14918
14919         case R_PPC64_GOT16_DS:
14920           from = TOCstart + htab->sec_info[input_section->id].toc_off;
14921           if (relocation + addend - from + 0x8000 < 0x10000
14922               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14923             {
14924               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14925               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14926                 {
14927                   insn += (14u << 26) - (58u << 26);
14928                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14929                   r_type = R_PPC64_TOC16;
14930                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14931                 }
14932             }
14933           break;
14934
14935         case R_PPC64_GOT16_LO_DS:
14936         case R_PPC64_GOT16_HA:
14937           from = TOCstart + htab->sec_info[input_section->id].toc_off;
14938           if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
14939               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14940             {
14941               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14942               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
14943                 {
14944                   insn += (14u << 26) - (58u << 26);
14945                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
14946                   r_type = R_PPC64_TOC16_LO;
14947                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14948                 }
14949               else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
14950                 {
14951                   r_type = R_PPC64_TOC16_HA;
14952                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14953                 }
14954             }
14955           break;
14956
14957         case R_PPC64_GOT_PCREL34:
14958           from = (rel->r_offset
14959                   + input_section->output_section->vma
14960                   + input_section->output_offset);
14961           if (relocation - from + (1ULL << 33) < 1ULL << 34
14962               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14963             {
14964               offset = rel->r_offset;
14965               pinsn = bfd_get_32 (input_bfd, contents + offset);
14966               pinsn <<= 32;
14967               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14968               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14969                    == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
14970                 {
14971                   /* Replace with paddi.  */
14972                   pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
14973                   r_type = R_PPC64_PCREL34;
14974                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14975                   bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
14976                   bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
14977                   goto pcrelopt;
14978                 }
14979             }
14980           break;
14981
14982         case R_PPC64_PCREL34:
14983           if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
14984             {
14985               offset = rel->r_offset;
14986               pinsn = bfd_get_32 (input_bfd, contents + offset);
14987               pinsn <<= 32;
14988               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
14989               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
14990                    == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
14991                        | (14ULL << 26) /* paddi */))
14992                 {
14993                 pcrelopt:
14994                   if (rel + 1 < relend
14995                       && rel[1].r_offset == offset
14996                       && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
14997                     {
14998                       bfd_vma off2 = rel[1].r_addend;
14999                       if (off2 == 0)
15000                         /* zero means next insn.  */
15001                         off2 = 8;
15002                       off2 += offset;
15003                       if (off2 + 4 <= input_section->size)
15004                         {
15005                           uint64_t pinsn2;
15006                           pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15007                           pinsn2 <<= 32;
15008                           if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15009                             break;
15010                           if (xlate_pcrel_opt (&pinsn, &pinsn2))
15011                             {
15012                               bfd_put_32 (input_bfd, pinsn >> 32,
15013                                           contents + offset);
15014                               bfd_put_32 (input_bfd, pinsn,
15015                                           contents + offset + 4);
15016                               bfd_put_32 (input_bfd, pinsn2 >> 32,
15017                                           contents + off2);
15018                             }
15019                         }
15020                     }
15021                 }
15022             }
15023           break;
15024         }
15025
15026       /* Set `addend'.  */
15027       tls_type = 0;
15028       save_unresolved_reloc = unresolved_reloc;
15029       switch (r_type)
15030         {
15031         default:
15032           /* xgettext:c-format */
15033           _bfd_error_handler (_("%pB: %s unsupported"),
15034                               input_bfd, ppc64_elf_howto_table[r_type]->name);
15035
15036           bfd_set_error (bfd_error_bad_value);
15037           ret = FALSE;
15038           goto copy_reloc;
15039
15040         case R_PPC64_NONE:
15041         case R_PPC64_TLS:
15042         case R_PPC64_TLSGD:
15043         case R_PPC64_TLSLD:
15044         case R_PPC64_TOCSAVE:
15045         case R_PPC64_GNU_VTINHERIT:
15046         case R_PPC64_GNU_VTENTRY:
15047         case R_PPC64_ENTRY:
15048         case R_PPC64_PCREL_OPT:
15049           goto copy_reloc;
15050
15051           /* GOT16 relocations.  Like an ADDR16 using the symbol's
15052              address in the GOT as relocation value instead of the
15053              symbol's value itself.  Also, create a GOT entry for the
15054              symbol and put the symbol value there.  */
15055         case R_PPC64_GOT_TLSGD16:
15056         case R_PPC64_GOT_TLSGD16_LO:
15057         case R_PPC64_GOT_TLSGD16_HI:
15058         case R_PPC64_GOT_TLSGD16_HA:
15059           tls_type = TLS_TLS | TLS_GD;
15060           goto dogot;
15061
15062         case R_PPC64_GOT_TLSLD16:
15063         case R_PPC64_GOT_TLSLD16_LO:
15064         case R_PPC64_GOT_TLSLD16_HI:
15065         case R_PPC64_GOT_TLSLD16_HA:
15066           tls_type = TLS_TLS | TLS_LD;
15067           goto dogot;
15068
15069         case R_PPC64_GOT_TPREL16_DS:
15070         case R_PPC64_GOT_TPREL16_LO_DS:
15071         case R_PPC64_GOT_TPREL16_HI:
15072         case R_PPC64_GOT_TPREL16_HA:
15073           tls_type = TLS_TLS | TLS_TPREL;
15074           goto dogot;
15075
15076         case R_PPC64_GOT_DTPREL16_DS:
15077         case R_PPC64_GOT_DTPREL16_LO_DS:
15078         case R_PPC64_GOT_DTPREL16_HI:
15079         case R_PPC64_GOT_DTPREL16_HA:
15080           tls_type = TLS_TLS | TLS_DTPREL;
15081           goto dogot;
15082
15083         case R_PPC64_GOT16:
15084         case R_PPC64_GOT16_LO:
15085         case R_PPC64_GOT16_HI:
15086         case R_PPC64_GOT16_HA:
15087         case R_PPC64_GOT16_DS:
15088         case R_PPC64_GOT16_LO_DS:
15089         case R_PPC64_GOT_PCREL34:
15090         dogot:
15091           {
15092             /* Relocation is to the entry for this symbol in the global
15093                offset table.  */
15094             asection *got;
15095             bfd_vma *offp;
15096             bfd_vma off;
15097             unsigned long indx = 0;
15098             struct got_entry *ent;
15099             bfd_vma sym_addend = orig_rel.r_addend;
15100
15101             if (r_type == R_PPC64_GOT_PCREL34)
15102               sym_addend = 0;
15103
15104             if (tls_type == (TLS_TLS | TLS_LD)
15105                 && (h == NULL
15106                     || !h->elf.def_dynamic))
15107               ent = ppc64_tlsld_got (input_bfd);
15108             else
15109               {
15110                 if (h != NULL)
15111                   {
15112                     if (!htab->elf.dynamic_sections_created
15113                         || h->elf.dynindx == -1
15114                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15115                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15116                       /* This is actually a static link, or it is a
15117                          -Bsymbolic link and the symbol is defined
15118                          locally, or the symbol was forced to be local
15119                          because of a version file.  */
15120                       ;
15121                     else
15122                       {
15123                         indx = h->elf.dynindx;
15124                         unresolved_reloc = FALSE;
15125                       }
15126                     ent = h->elf.got.glist;
15127                   }
15128                 else
15129                   {
15130                     if (local_got_ents == NULL)
15131                       abort ();
15132                     ent = local_got_ents[r_symndx];
15133                   }
15134
15135                 for (; ent != NULL; ent = ent->next)
15136                   if (ent->addend == sym_addend
15137                       && ent->owner == input_bfd
15138                       && ent->tls_type == tls_type)
15139                     break;
15140               }
15141
15142             if (ent == NULL)
15143               abort ();
15144             if (ent->is_indirect)
15145               ent = ent->got.ent;
15146             offp = &ent->got.offset;
15147             got = ppc64_elf_tdata (ent->owner)->got;
15148             if (got == NULL)
15149               abort ();
15150
15151             /* The offset must always be a multiple of 8.  We use the
15152                least significant bit to record whether we have already
15153                processed this entry.  */
15154             off = *offp;
15155             if ((off & 1) != 0)
15156               off &= ~1;
15157             else
15158               {
15159                 /* Generate relocs for the dynamic linker, except in
15160                    the case of TLSLD where we'll use one entry per
15161                    module.  */
15162                 asection *relgot;
15163                 bfd_boolean ifunc;
15164
15165                 *offp = off | 1;
15166                 relgot = NULL;
15167                 ifunc = (h != NULL
15168                          ? h->elf.type == STT_GNU_IFUNC
15169                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15170                 if (ifunc)
15171                   {
15172                     relgot = htab->elf.irelplt;
15173                     if (indx == 0)
15174                       htab->local_ifunc_resolver = 1;
15175                     else if (is_static_defined (&h->elf))
15176                       htab->maybe_local_ifunc_resolver = 1;
15177                   }
15178                 else if (indx != 0
15179                          || (bfd_link_pic (info)
15180                              && (h == NULL
15181                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15182                                  || (tls_type == (TLS_TLS | TLS_LD)
15183                                      && !h->elf.def_dynamic))
15184                              && !(tls_type == (TLS_TLS | TLS_TPREL)
15185                                   && bfd_link_executable (info)
15186                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15187                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
15188                 if (relgot != NULL)
15189                   {
15190                     outrel.r_offset = (got->output_section->vma
15191                                        + got->output_offset
15192                                        + off);
15193                     outrel.r_addend = sym_addend;
15194                     if (tls_type & (TLS_LD | TLS_GD))
15195                       {
15196                         outrel.r_addend = 0;
15197                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15198                         if (tls_type == (TLS_TLS | TLS_GD))
15199                           {
15200                             loc = relgot->contents;
15201                             loc += (relgot->reloc_count++
15202                                     * sizeof (Elf64_External_Rela));
15203                             bfd_elf64_swap_reloca_out (output_bfd,
15204                                                        &outrel, loc);
15205                             outrel.r_offset += 8;
15206                             outrel.r_addend = sym_addend;
15207                             outrel.r_info
15208                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15209                           }
15210                       }
15211                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
15212                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15213                     else if (tls_type == (TLS_TLS | TLS_TPREL))
15214                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15215                     else if (indx != 0)
15216                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15217                     else
15218                       {
15219                         if (ifunc)
15220                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15221                         else
15222                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15223
15224                         /* Write the .got section contents for the sake
15225                            of prelink.  */
15226                         loc = got->contents + off;
15227                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15228                                     loc);
15229                       }
15230
15231                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15232                       {
15233                         outrel.r_addend += relocation;
15234                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15235                           {
15236                             if (htab->elf.tls_sec == NULL)
15237                               outrel.r_addend = 0;
15238                             else
15239                               outrel.r_addend -= htab->elf.tls_sec->vma;
15240                           }
15241                       }
15242                     loc = relgot->contents;
15243                     loc += (relgot->reloc_count++
15244                             * sizeof (Elf64_External_Rela));
15245                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15246                   }
15247
15248                 /* Init the .got section contents here if we're not
15249                    emitting a reloc.  */
15250                 else
15251                   {
15252                     relocation += sym_addend;
15253                     if (tls_type != 0)
15254                       {
15255                         if (htab->elf.tls_sec == NULL)
15256                           relocation = 0;
15257                         else
15258                           {
15259                             if (tls_type & TLS_LD)
15260                               relocation = 0;
15261                             else
15262                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15263                             if (tls_type & TLS_TPREL)
15264                               relocation += DTP_OFFSET - TP_OFFSET;
15265                           }
15266
15267                         if (tls_type & (TLS_GD | TLS_LD))
15268                           {
15269                             bfd_put_64 (output_bfd, relocation,
15270                                         got->contents + off + 8);
15271                             relocation = 1;
15272                           }
15273                       }
15274                     bfd_put_64 (output_bfd, relocation,
15275                                 got->contents + off);
15276                   }
15277               }
15278
15279             if (off >= (bfd_vma) -2)
15280               abort ();
15281
15282             relocation = got->output_section->vma + got->output_offset + off;
15283             if (r_type != R_PPC64_GOT_PCREL34)
15284               addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15285           }
15286           break;
15287
15288         case R_PPC64_PLT16_HA:
15289         case R_PPC64_PLT16_HI:
15290         case R_PPC64_PLT16_LO:
15291         case R_PPC64_PLT16_LO_DS:
15292         case R_PPC64_PLT_PCREL34:
15293         case R_PPC64_PLT_PCREL34_NOTOC:
15294         case R_PPC64_PLT32:
15295         case R_PPC64_PLT64:
15296         case R_PPC64_PLTSEQ:
15297         case R_PPC64_PLTSEQ_NOTOC:
15298         case R_PPC64_PLTCALL:
15299         case R_PPC64_PLTCALL_NOTOC:
15300           /* Relocation is to the entry for this symbol in the
15301              procedure linkage table.  */
15302           unresolved_reloc = TRUE;
15303           {
15304             struct plt_entry **plt_list = NULL;
15305             if (h != NULL)
15306               plt_list = &h->elf.plt.plist;
15307             else if (local_got_ents != NULL)
15308               {
15309                 struct plt_entry **local_plt = (struct plt_entry **)
15310                   (local_got_ents + symtab_hdr->sh_info);
15311                 plt_list = local_plt + r_symndx;
15312               }
15313             if (plt_list)
15314               {
15315                 struct plt_entry *ent;
15316                 bfd_vma sym_addend = orig_rel.r_addend;
15317
15318                 if (r_type == R_PPC64_PLT_PCREL34
15319                     || r_type == R_PPC64_PLT_PCREL34_NOTOC)
15320                   sym_addend = 0;
15321
15322                 for (ent = *plt_list; ent != NULL; ent = ent->next)
15323                   if (ent->plt.offset != (bfd_vma) -1
15324                       && ent->addend == sym_addend)
15325                     {
15326                       asection *plt;
15327                       bfd_vma got;
15328
15329                       plt = htab->elf.splt;
15330                       if (!htab->elf.dynamic_sections_created
15331                           || h == NULL
15332                           || h->elf.dynindx == -1)
15333                         {
15334                           if (h != NULL
15335                               ? h->elf.type == STT_GNU_IFUNC
15336                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15337                             plt = htab->elf.iplt;
15338                           else
15339                             plt = htab->pltlocal;
15340                         }
15341                       relocation = (plt->output_section->vma
15342                                     + plt->output_offset
15343                                     + ent->plt.offset);
15344                       if (r_type == R_PPC64_PLT16_HA
15345                           || r_type == R_PPC64_PLT16_HI
15346                           || r_type == R_PPC64_PLT16_LO
15347                           || r_type == R_PPC64_PLT16_LO_DS)
15348                         {
15349                           got = (elf_gp (output_bfd)
15350                                  + htab->sec_info[input_section->id].toc_off);
15351                           relocation -= got;
15352                         }
15353                       if (r_type != R_PPC64_PLT_PCREL34
15354                           && r_type != R_PPC64_PLT_PCREL34_NOTOC)
15355                         addend = 0;
15356                       unresolved_reloc = FALSE;
15357                       break;
15358                     }
15359               }
15360           }
15361           break;
15362
15363         case R_PPC64_TOC:
15364           /* Relocation value is TOC base.  */
15365           relocation = TOCstart;
15366           if (r_symndx == STN_UNDEF)
15367             relocation += htab->sec_info[input_section->id].toc_off;
15368           else if (unresolved_reloc)
15369             ;
15370           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15371             relocation += htab->sec_info[sec->id].toc_off;
15372           else
15373             unresolved_reloc = TRUE;
15374           goto dodyn;
15375
15376           /* TOC16 relocs.  We want the offset relative to the TOC base,
15377              which is the address of the start of the TOC plus 0x8000.
15378              The TOC consists of sections .got, .toc, .tocbss, and .plt,
15379              in this order.  */
15380         case R_PPC64_TOC16:
15381         case R_PPC64_TOC16_LO:
15382         case R_PPC64_TOC16_HI:
15383         case R_PPC64_TOC16_DS:
15384         case R_PPC64_TOC16_LO_DS:
15385         case R_PPC64_TOC16_HA:
15386           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15387           break;
15388
15389           /* Relocate against the beginning of the section.  */
15390         case R_PPC64_SECTOFF:
15391         case R_PPC64_SECTOFF_LO:
15392         case R_PPC64_SECTOFF_HI:
15393         case R_PPC64_SECTOFF_DS:
15394         case R_PPC64_SECTOFF_LO_DS:
15395         case R_PPC64_SECTOFF_HA:
15396           if (sec != NULL)
15397             addend -= sec->output_section->vma;
15398           break;
15399
15400         case R_PPC64_REL16:
15401         case R_PPC64_REL16_LO:
15402         case R_PPC64_REL16_HI:
15403         case R_PPC64_REL16_HA:
15404         case R_PPC64_REL16_HIGH:
15405         case R_PPC64_REL16_HIGHA:
15406         case R_PPC64_REL16_HIGHER:
15407         case R_PPC64_REL16_HIGHERA:
15408         case R_PPC64_REL16_HIGHEST:
15409         case R_PPC64_REL16_HIGHESTA:
15410         case R_PPC64_REL16_HIGHER34:
15411         case R_PPC64_REL16_HIGHERA34:
15412         case R_PPC64_REL16_HIGHEST34:
15413         case R_PPC64_REL16_HIGHESTA34:
15414         case R_PPC64_REL16DX_HA:
15415         case R_PPC64_REL14:
15416         case R_PPC64_REL14_BRNTAKEN:
15417         case R_PPC64_REL14_BRTAKEN:
15418         case R_PPC64_REL24:
15419         case R_PPC64_REL24_NOTOC:
15420         case R_PPC64_PCREL34:
15421         case R_PPC64_PCREL28:
15422           break;
15423
15424         case R_PPC64_TPREL16:
15425         case R_PPC64_TPREL16_LO:
15426         case R_PPC64_TPREL16_HI:
15427         case R_PPC64_TPREL16_HA:
15428         case R_PPC64_TPREL16_DS:
15429         case R_PPC64_TPREL16_LO_DS:
15430         case R_PPC64_TPREL16_HIGH:
15431         case R_PPC64_TPREL16_HIGHA:
15432         case R_PPC64_TPREL16_HIGHER:
15433         case R_PPC64_TPREL16_HIGHERA:
15434         case R_PPC64_TPREL16_HIGHEST:
15435         case R_PPC64_TPREL16_HIGHESTA:
15436           if (h != NULL
15437               && h->elf.root.type == bfd_link_hash_undefweak
15438               && h->elf.dynindx == -1)
15439             {
15440               /* Make this relocation against an undefined weak symbol
15441                  resolve to zero.  This is really just a tweak, since
15442                  code using weak externs ought to check that they are
15443                  defined before using them.  */
15444               bfd_byte *p = contents + rel->r_offset - d_offset;
15445
15446               insn = bfd_get_32 (input_bfd, p);
15447               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15448               if (insn != 0)
15449                 bfd_put_32 (input_bfd, insn, p);
15450               break;
15451             }
15452           if (htab->elf.tls_sec != NULL)
15453             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15454           /* The TPREL16 relocs shouldn't really be used in shared
15455              libs or with non-local symbols as that will result in
15456              DT_TEXTREL being set, but support them anyway.  */
15457           goto dodyn;
15458
15459         case R_PPC64_DTPREL16:
15460         case R_PPC64_DTPREL16_LO:
15461         case R_PPC64_DTPREL16_HI:
15462         case R_PPC64_DTPREL16_HA:
15463         case R_PPC64_DTPREL16_DS:
15464         case R_PPC64_DTPREL16_LO_DS:
15465         case R_PPC64_DTPREL16_HIGH:
15466         case R_PPC64_DTPREL16_HIGHA:
15467         case R_PPC64_DTPREL16_HIGHER:
15468         case R_PPC64_DTPREL16_HIGHERA:
15469         case R_PPC64_DTPREL16_HIGHEST:
15470         case R_PPC64_DTPREL16_HIGHESTA:
15471           if (htab->elf.tls_sec != NULL)
15472             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15473           break;
15474
15475         case R_PPC64_ADDR64_LOCAL:
15476           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15477                                               ? h->elf.other
15478                                               : sym->st_other);
15479           break;
15480
15481         case R_PPC64_DTPMOD64:
15482           relocation = 1;
15483           addend = 0;
15484           goto dodyn;
15485
15486         case R_PPC64_TPREL64:
15487           if (htab->elf.tls_sec != NULL)
15488             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15489           goto dodyn;
15490
15491         case R_PPC64_DTPREL64:
15492           if (htab->elf.tls_sec != NULL)
15493             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15494           /* Fall through.  */
15495
15496           /* Relocations that may need to be propagated if this is a
15497              dynamic object.  */
15498         case R_PPC64_REL30:
15499         case R_PPC64_REL32:
15500         case R_PPC64_REL64:
15501         case R_PPC64_ADDR14:
15502         case R_PPC64_ADDR14_BRNTAKEN:
15503         case R_PPC64_ADDR14_BRTAKEN:
15504         case R_PPC64_ADDR16:
15505         case R_PPC64_ADDR16_DS:
15506         case R_PPC64_ADDR16_HA:
15507         case R_PPC64_ADDR16_HI:
15508         case R_PPC64_ADDR16_HIGH:
15509         case R_PPC64_ADDR16_HIGHA:
15510         case R_PPC64_ADDR16_HIGHER:
15511         case R_PPC64_ADDR16_HIGHERA:
15512         case R_PPC64_ADDR16_HIGHEST:
15513         case R_PPC64_ADDR16_HIGHESTA:
15514         case R_PPC64_ADDR16_LO:
15515         case R_PPC64_ADDR16_LO_DS:
15516         case R_PPC64_ADDR16_HIGHER34:
15517         case R_PPC64_ADDR16_HIGHERA34:
15518         case R_PPC64_ADDR16_HIGHEST34:
15519         case R_PPC64_ADDR16_HIGHESTA34:
15520         case R_PPC64_ADDR24:
15521         case R_PPC64_ADDR32:
15522         case R_PPC64_ADDR64:
15523         case R_PPC64_UADDR16:
15524         case R_PPC64_UADDR32:
15525         case R_PPC64_UADDR64:
15526         case R_PPC64_D34:
15527         case R_PPC64_D34_LO:
15528         case R_PPC64_D34_HI30:
15529         case R_PPC64_D34_HA30:
15530         case R_PPC64_D28:
15531         dodyn:
15532           if ((input_section->flags & SEC_ALLOC) == 0)
15533             break;
15534
15535           if (NO_OPD_RELOCS && is_opd)
15536             break;
15537
15538           if (bfd_link_pic (info)
15539               ? ((h == NULL
15540                   || h->dyn_relocs != NULL)
15541                  && ((h != NULL && pc_dynrelocs (h))
15542                      || must_be_dyn_reloc (info, r_type)))
15543               : (h != NULL
15544                  ? h->dyn_relocs != NULL
15545                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15546             {
15547               bfd_boolean skip, relocate;
15548               asection *sreloc;
15549               bfd_vma out_off;
15550               long indx = 0;
15551
15552               /* When generating a dynamic object, these relocations
15553                  are copied into the output file to be resolved at run
15554                  time.  */
15555
15556               skip = FALSE;
15557               relocate = FALSE;
15558
15559               out_off = _bfd_elf_section_offset (output_bfd, info,
15560                                                  input_section, rel->r_offset);
15561               if (out_off == (bfd_vma) -1)
15562                 skip = TRUE;
15563               else if (out_off == (bfd_vma) -2)
15564                 skip = TRUE, relocate = TRUE;
15565               out_off += (input_section->output_section->vma
15566                           + input_section->output_offset);
15567               outrel.r_offset = out_off;
15568               outrel.r_addend = rel->r_addend;
15569
15570               /* Optimize unaligned reloc use.  */
15571               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15572                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15573                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15574               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15575                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15576                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15577               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15578                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15579                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15580
15581               if (skip)
15582                 memset (&outrel, 0, sizeof outrel);
15583               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15584                        && !is_opd
15585                        && r_type != R_PPC64_TOC)
15586                 {
15587                   indx = h->elf.dynindx;
15588                   BFD_ASSERT (indx != -1);
15589                   outrel.r_info = ELF64_R_INFO (indx, r_type);
15590                 }
15591               else
15592                 {
15593                   /* This symbol is local, or marked to become local,
15594                      or this is an opd section reloc which must point
15595                      at a local function.  */
15596                   outrel.r_addend += relocation;
15597                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15598                     {
15599                       if (is_opd && h != NULL)
15600                         {
15601                           /* Lie about opd entries.  This case occurs
15602                              when building shared libraries and we
15603                              reference a function in another shared
15604                              lib.  The same thing happens for a weak
15605                              definition in an application that's
15606                              overridden by a strong definition in a
15607                              shared lib.  (I believe this is a generic
15608                              bug in binutils handling of weak syms.)
15609                              In these cases we won't use the opd
15610                              entry in this lib.  */
15611                           unresolved_reloc = FALSE;
15612                         }
15613                       if (!is_opd
15614                           && r_type == R_PPC64_ADDR64
15615                           && (h != NULL
15616                               ? h->elf.type == STT_GNU_IFUNC
15617                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15618                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15619                       else
15620                         {
15621                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15622
15623                           /* We need to relocate .opd contents for ld.so.
15624                              Prelink also wants simple and consistent rules
15625                              for relocs.  This make all RELATIVE relocs have
15626                              *r_offset equal to r_addend.  */
15627                           relocate = TRUE;
15628                         }
15629                     }
15630                   else
15631                     {
15632                       if (h != NULL
15633                           ? h->elf.type == STT_GNU_IFUNC
15634                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15635                         {
15636                           info->callbacks->einfo
15637                             /* xgettext:c-format */
15638                             (_("%H: %s for indirect "
15639                                "function `%pT' unsupported\n"),
15640                              input_bfd, input_section, rel->r_offset,
15641                              ppc64_elf_howto_table[r_type]->name,
15642                              sym_name);
15643                           ret = FALSE;
15644                         }
15645                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15646                         ;
15647                       else if (sec == NULL || sec->owner == NULL)
15648                         {
15649                           bfd_set_error (bfd_error_bad_value);
15650                           return FALSE;
15651                         }
15652                       else
15653                         {
15654                           asection *osec;
15655
15656                           osec = sec->output_section;
15657                           indx = elf_section_data (osec)->dynindx;
15658
15659                           if (indx == 0)
15660                             {
15661                               if ((osec->flags & SEC_READONLY) == 0
15662                                   && htab->elf.data_index_section != NULL)
15663                                 osec = htab->elf.data_index_section;
15664                               else
15665                                 osec = htab->elf.text_index_section;
15666                               indx = elf_section_data (osec)->dynindx;
15667                             }
15668                           BFD_ASSERT (indx != 0);
15669
15670                           /* We are turning this relocation into one
15671                              against a section symbol, so subtract out
15672                              the output section's address but not the
15673                              offset of the input section in the output
15674                              section.  */
15675                           outrel.r_addend -= osec->vma;
15676                         }
15677
15678                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15679                     }
15680                 }
15681
15682               sreloc = elf_section_data (input_section)->sreloc;
15683               if (h != NULL
15684                   ? h->elf.type == STT_GNU_IFUNC
15685                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15686                 {
15687                   sreloc = htab->elf.irelplt;
15688                   if (indx == 0)
15689                     htab->local_ifunc_resolver = 1;
15690                   else if (is_static_defined (&h->elf))
15691                     htab->maybe_local_ifunc_resolver = 1;
15692                 }
15693               if (sreloc == NULL)
15694                 abort ();
15695
15696               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15697                   >= sreloc->size)
15698                 abort ();
15699               loc = sreloc->contents;
15700               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15701               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15702
15703               /* If this reloc is against an external symbol, it will
15704                  be computed at runtime, so there's no need to do
15705                  anything now.  However, for the sake of prelink ensure
15706                  that the section contents are a known value.  */
15707               if (!relocate)
15708                 {
15709                   unresolved_reloc = FALSE;
15710                   /* The value chosen here is quite arbitrary as ld.so
15711                      ignores section contents except for the special
15712                      case of .opd where the contents might be accessed
15713                      before relocation.  Choose zero, as that won't
15714                      cause reloc overflow.  */
15715                   relocation = 0;
15716                   addend = 0;
15717                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15718                      to improve backward compatibility with older
15719                      versions of ld.  */
15720                   if (r_type == R_PPC64_ADDR64)
15721                     addend = outrel.r_addend;
15722                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15723                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15724                     addend = outrel.r_offset;
15725                 }
15726             }
15727           break;
15728
15729         case R_PPC64_COPY:
15730         case R_PPC64_GLOB_DAT:
15731         case R_PPC64_JMP_SLOT:
15732         case R_PPC64_JMP_IREL:
15733         case R_PPC64_RELATIVE:
15734           /* We shouldn't ever see these dynamic relocs in relocatable
15735              files.  */
15736           /* Fall through.  */
15737
15738         case R_PPC64_PLTGOT16:
15739         case R_PPC64_PLTGOT16_DS:
15740         case R_PPC64_PLTGOT16_HA:
15741         case R_PPC64_PLTGOT16_HI:
15742         case R_PPC64_PLTGOT16_LO:
15743         case R_PPC64_PLTGOT16_LO_DS:
15744         case R_PPC64_PLTREL32:
15745         case R_PPC64_PLTREL64:
15746           /* These ones haven't been implemented yet.  */
15747
15748           info->callbacks->einfo
15749             /* xgettext:c-format */
15750             (_("%P: %pB: %s is not supported for `%pT'\n"),
15751              input_bfd,
15752              ppc64_elf_howto_table[r_type]->name, sym_name);
15753
15754           bfd_set_error (bfd_error_invalid_operation);
15755           ret = FALSE;
15756           goto copy_reloc;
15757         }
15758
15759       /* Multi-instruction sequences that access the TOC can be
15760          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15761          to             nop;           addi rb,r2,x;  */
15762       switch (r_type)
15763         {
15764         default:
15765           break;
15766
15767         case R_PPC64_GOT_TLSLD16_HI:
15768         case R_PPC64_GOT_TLSGD16_HI:
15769         case R_PPC64_GOT_TPREL16_HI:
15770         case R_PPC64_GOT_DTPREL16_HI:
15771         case R_PPC64_GOT16_HI:
15772         case R_PPC64_TOC16_HI:
15773           /* These relocs would only be useful if building up an
15774              offset to later add to r2, perhaps in an indexed
15775              addressing mode instruction.  Don't try to optimize.
15776              Unfortunately, the possibility of someone building up an
15777              offset like this or even with the HA relocs, means that
15778              we need to check the high insn when optimizing the low
15779              insn.  */
15780           break;
15781
15782         case R_PPC64_PLTCALL_NOTOC:
15783           if (!unresolved_reloc)
15784             htab->notoc_plt = 1;
15785           /* Fall through.  */
15786         case R_PPC64_PLTCALL:
15787           if (unresolved_reloc)
15788             {
15789               /* No plt entry.  Make this into a direct call.  */
15790               bfd_byte *p = contents + rel->r_offset;
15791               insn = bfd_get_32 (input_bfd, p);
15792               insn &= 1;
15793               bfd_put_32 (input_bfd, B_DOT | insn, p);
15794               if (r_type == R_PPC64_PLTCALL)
15795                 bfd_put_32 (input_bfd, NOP, p + 4);
15796               unresolved_reloc = save_unresolved_reloc;
15797               r_type = R_PPC64_REL24;
15798             }
15799           break;
15800
15801         case R_PPC64_PLTSEQ_NOTOC:
15802         case R_PPC64_PLTSEQ:
15803           if (unresolved_reloc)
15804             {
15805               unresolved_reloc = FALSE;
15806               goto nop_it;
15807             }
15808           break;
15809
15810         case R_PPC64_PLT_PCREL34_NOTOC:
15811           if (!unresolved_reloc)
15812             htab->notoc_plt = 1;
15813           /* Fall through.  */
15814         case R_PPC64_PLT_PCREL34:
15815           if (unresolved_reloc)
15816             {
15817               bfd_byte *p = contents + rel->r_offset;
15818               bfd_put_32 (input_bfd, PNOP >> 32, p);
15819               bfd_put_32 (input_bfd, PNOP, p + 4);
15820               unresolved_reloc = FALSE;
15821               goto copy_reloc;
15822             }
15823           break;
15824
15825         case R_PPC64_PLT16_HA:
15826           if (unresolved_reloc)
15827             {
15828               unresolved_reloc = FALSE;
15829               goto nop_it;
15830             }
15831           /* Fall through.  */
15832         case R_PPC64_GOT_TLSLD16_HA:
15833         case R_PPC64_GOT_TLSGD16_HA:
15834         case R_PPC64_GOT_TPREL16_HA:
15835         case R_PPC64_GOT_DTPREL16_HA:
15836         case R_PPC64_GOT16_HA:
15837         case R_PPC64_TOC16_HA:
15838           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15839               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15840             {
15841               bfd_byte *p;
15842             nop_it:
15843               p = contents + (rel->r_offset & ~3);
15844               bfd_put_32 (input_bfd, NOP, p);
15845               goto copy_reloc;
15846             }
15847           break;
15848
15849         case R_PPC64_PLT16_LO:
15850         case R_PPC64_PLT16_LO_DS:
15851           if (unresolved_reloc)
15852             {
15853               unresolved_reloc = FALSE;
15854               goto nop_it;
15855             }
15856           /* Fall through.  */
15857         case R_PPC64_GOT_TLSLD16_LO:
15858         case R_PPC64_GOT_TLSGD16_LO:
15859         case R_PPC64_GOT_TPREL16_LO_DS:
15860         case R_PPC64_GOT_DTPREL16_LO_DS:
15861         case R_PPC64_GOT16_LO:
15862         case R_PPC64_GOT16_LO_DS:
15863         case R_PPC64_TOC16_LO:
15864         case R_PPC64_TOC16_LO_DS:
15865           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15866               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15867             {
15868               bfd_byte *p = contents + (rel->r_offset & ~3);
15869               insn = bfd_get_32 (input_bfd, p);
15870               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15871                 {
15872                   /* Transform addic to addi when we change reg.  */
15873                   insn &= ~((0x3f << 26) | (0x1f << 16));
15874                   insn |= (14u << 26) | (2 << 16);
15875                 }
15876               else
15877                 {
15878                   insn &= ~(0x1f << 16);
15879                   insn |= 2 << 16;
15880                 }
15881               bfd_put_32 (input_bfd, insn, p);
15882             }
15883           break;
15884
15885         case R_PPC64_TPREL16_HA:
15886           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15887             {
15888               bfd_byte *p = contents + (rel->r_offset & ~3);
15889               insn = bfd_get_32 (input_bfd, p);
15890               if ((insn & ((0x3f << 26) | 0x1f << 16))
15891                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15892                 /* xgettext:c-format */
15893                 info->callbacks->minfo
15894                   (_("%H: warning: %s unexpected insn %#x.\n"),
15895                    input_bfd, input_section, rel->r_offset,
15896                    ppc64_elf_howto_table[r_type]->name, insn);
15897               else
15898                 {
15899                   bfd_put_32 (input_bfd, NOP, p);
15900                   goto copy_reloc;
15901                 }
15902             }
15903           break;
15904
15905         case R_PPC64_TPREL16_LO:
15906         case R_PPC64_TPREL16_LO_DS:
15907           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15908             {
15909               bfd_byte *p = contents + (rel->r_offset & ~3);
15910               insn = bfd_get_32 (input_bfd, p);
15911               insn &= ~(0x1f << 16);
15912               insn |= 13 << 16;
15913               bfd_put_32 (input_bfd, insn, p);
15914             }
15915           break;
15916         }
15917
15918       /* Do any further special processing.  */
15919       switch (r_type)
15920         {
15921         default:
15922           break;
15923
15924         case R_PPC64_REL16_HA:
15925         case R_PPC64_REL16_HIGHA:
15926         case R_PPC64_REL16_HIGHERA:
15927         case R_PPC64_REL16_HIGHESTA:
15928         case R_PPC64_REL16DX_HA:
15929         case R_PPC64_ADDR16_HA:
15930         case R_PPC64_ADDR16_HIGHA:
15931         case R_PPC64_ADDR16_HIGHERA:
15932         case R_PPC64_ADDR16_HIGHESTA:
15933         case R_PPC64_TOC16_HA:
15934         case R_PPC64_SECTOFF_HA:
15935         case R_PPC64_TPREL16_HA:
15936         case R_PPC64_TPREL16_HIGHA:
15937         case R_PPC64_TPREL16_HIGHERA:
15938         case R_PPC64_TPREL16_HIGHESTA:
15939         case R_PPC64_DTPREL16_HA:
15940         case R_PPC64_DTPREL16_HIGHA:
15941         case R_PPC64_DTPREL16_HIGHERA:
15942         case R_PPC64_DTPREL16_HIGHESTA:
15943           /* It's just possible that this symbol is a weak symbol
15944              that's not actually defined anywhere. In that case,
15945              'sec' would be NULL, and we should leave the symbol
15946              alone (it will be set to zero elsewhere in the link).  */
15947           if (sec == NULL)
15948             break;
15949           /* Fall through.  */
15950
15951         case R_PPC64_GOT16_HA:
15952         case R_PPC64_PLTGOT16_HA:
15953         case R_PPC64_PLT16_HA:
15954         case R_PPC64_GOT_TLSGD16_HA:
15955         case R_PPC64_GOT_TLSLD16_HA:
15956         case R_PPC64_GOT_TPREL16_HA:
15957         case R_PPC64_GOT_DTPREL16_HA:
15958           /* Add 0x10000 if sign bit in 0:15 is set.
15959              Bits 0:15 are not used.  */
15960           addend += 0x8000;
15961           break;
15962
15963         case R_PPC64_D34_HA30:
15964         case R_PPC64_ADDR16_HIGHERA34:
15965         case R_PPC64_ADDR16_HIGHESTA34:
15966         case R_PPC64_REL16_HIGHERA34:
15967         case R_PPC64_REL16_HIGHESTA34:
15968           if (sec != NULL)
15969             addend += 1ULL << 33;
15970           break;
15971
15972         case R_PPC64_ADDR16_DS:
15973         case R_PPC64_ADDR16_LO_DS:
15974         case R_PPC64_GOT16_DS:
15975         case R_PPC64_GOT16_LO_DS:
15976         case R_PPC64_PLT16_LO_DS:
15977         case R_PPC64_SECTOFF_DS:
15978         case R_PPC64_SECTOFF_LO_DS:
15979         case R_PPC64_TOC16_DS:
15980         case R_PPC64_TOC16_LO_DS:
15981         case R_PPC64_PLTGOT16_DS:
15982         case R_PPC64_PLTGOT16_LO_DS:
15983         case R_PPC64_GOT_TPREL16_DS:
15984         case R_PPC64_GOT_TPREL16_LO_DS:
15985         case R_PPC64_GOT_DTPREL16_DS:
15986         case R_PPC64_GOT_DTPREL16_LO_DS:
15987         case R_PPC64_TPREL16_DS:
15988         case R_PPC64_TPREL16_LO_DS:
15989         case R_PPC64_DTPREL16_DS:
15990         case R_PPC64_DTPREL16_LO_DS:
15991           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15992           mask = 3;
15993           /* If this reloc is against an lq, lxv, or stxv insn, then
15994              the value must be a multiple of 16.  This is somewhat of
15995              a hack, but the "correct" way to do this by defining _DQ
15996              forms of all the _DS relocs bloats all reloc switches in
15997              this file.  It doesn't make much sense to use these
15998              relocs in data, so testing the insn should be safe.  */
15999           if ((insn & (0x3f << 26)) == (56u << 26)
16000               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16001             mask = 15;
16002           relocation += addend;
16003           addend = insn & (mask ^ 3);
16004           if ((relocation & mask) != 0)
16005             {
16006               relocation ^= relocation & mask;
16007               info->callbacks->einfo
16008                 /* xgettext:c-format */
16009                 (_("%H: error: %s not a multiple of %u\n"),
16010                  input_bfd, input_section, rel->r_offset,
16011                  ppc64_elf_howto_table[r_type]->name,
16012                  mask + 1);
16013               bfd_set_error (bfd_error_bad_value);
16014               ret = FALSE;
16015               goto copy_reloc;
16016             }
16017           break;
16018         }
16019
16020       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16021          because such sections are not SEC_ALLOC and thus ld.so will
16022          not process them.  */
16023       howto = ppc64_elf_howto_table[(int) r_type];
16024       if (unresolved_reloc
16025           && !((input_section->flags & SEC_DEBUGGING) != 0
16026                && h->elf.def_dynamic)
16027           && _bfd_elf_section_offset (output_bfd, info, input_section,
16028                                       rel->r_offset) != (bfd_vma) -1)
16029         {
16030           info->callbacks->einfo
16031             /* xgettext:c-format */
16032             (_("%H: unresolvable %s against `%pT'\n"),
16033              input_bfd, input_section, rel->r_offset,
16034              howto->name,
16035              h->elf.root.root.string);
16036           ret = FALSE;
16037         }
16038
16039       /* 16-bit fields in insns mostly have signed values, but a
16040          few insns have 16-bit unsigned values.  Really, we should
16041          have different reloc types.  */
16042       if (howto->complain_on_overflow != complain_overflow_dont
16043           && howto->dst_mask == 0xffff
16044           && (input_section->flags & SEC_CODE) != 0)
16045         {
16046           enum complain_overflow complain = complain_overflow_signed;
16047
16048           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16049           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16050             complain = complain_overflow_bitfield;
16051           else if (howto->rightshift == 0
16052                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16053                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16054                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16055                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16056                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16057                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16058             complain = complain_overflow_unsigned;
16059           if (howto->complain_on_overflow != complain)
16060             {
16061               alt_howto = *howto;
16062               alt_howto.complain_on_overflow = complain;
16063               howto = &alt_howto;
16064             }
16065         }
16066
16067       switch (r_type)
16068         {
16069           /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
16070         case R_PPC64_D34:
16071         case R_PPC64_D34_LO:
16072         case R_PPC64_D34_HI30:
16073         case R_PPC64_D34_HA30:
16074         case R_PPC64_PCREL34:
16075         case R_PPC64_GOT_PCREL34:
16076         case R_PPC64_PLT_PCREL34:
16077         case R_PPC64_PLT_PCREL34_NOTOC:
16078         case R_PPC64_D28:
16079         case R_PPC64_PCREL28:
16080           if (rel->r_offset + 8 > input_section->size)
16081             r = bfd_reloc_outofrange;
16082           else
16083             {
16084               relocation += addend;
16085               if (howto->pc_relative)
16086                 relocation -= (rel->r_offset
16087                                + input_section->output_offset
16088                                + input_section->output_section->vma);
16089               relocation >>= howto->rightshift;
16090
16091               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16092               pinsn <<= 32;
16093               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16094
16095               pinsn &= ~howto->dst_mask;
16096               pinsn |= (((relocation << 16) | (relocation & 0xffff))
16097                         & howto->dst_mask);
16098               bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16099               bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16100               r = bfd_reloc_ok;
16101               if (howto->complain_on_overflow == complain_overflow_signed
16102                   && (relocation + (1ULL << (howto->bitsize - 1))
16103                       >= 1ULL << howto->bitsize))
16104                 r = bfd_reloc_overflow;
16105             }
16106           break;
16107
16108         case R_PPC64_REL16DX_HA:
16109           if (rel->r_offset + 4 > input_section->size)
16110             r = bfd_reloc_outofrange;
16111           else
16112             {
16113               relocation += addend;
16114               relocation -= (rel->r_offset
16115                              + input_section->output_offset
16116                              + input_section->output_section->vma);
16117               relocation = (bfd_signed_vma) relocation >> 16;
16118               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16119               insn &= ~0x1fffc1;
16120               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16121               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16122               r = bfd_reloc_ok;
16123               if (relocation + 0x8000 > 0xffff)
16124                 r = bfd_reloc_overflow;
16125             }
16126           break;
16127
16128         default:
16129           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16130                                         contents, rel->r_offset,
16131                                         relocation, addend);
16132         }
16133
16134       if (r != bfd_reloc_ok)
16135         {
16136           char *more_info = NULL;
16137           const char *reloc_name = howto->name;
16138
16139           if (reloc_dest != DEST_NORMAL)
16140             {
16141               more_info = bfd_malloc (strlen (reloc_name) + 8);
16142               if (more_info != NULL)
16143                 {
16144                   strcpy (more_info, reloc_name);
16145                   strcat (more_info, (reloc_dest == DEST_OPD
16146                                       ? " (OPD)" : " (stub)"));
16147                   reloc_name = more_info;
16148                 }
16149             }
16150
16151           if (r == bfd_reloc_overflow)
16152             {
16153               /* On code like "if (foo) foo();" don't report overflow
16154                  on a branch to zero when foo is undefined.  */
16155               if (!warned
16156                   && (reloc_dest == DEST_STUB
16157                       || !(h != NULL
16158                            && (h->elf.root.type == bfd_link_hash_undefweak
16159                                || h->elf.root.type == bfd_link_hash_undefined)
16160                            && is_branch_reloc (r_type))))
16161                 info->callbacks->reloc_overflow (info, &h->elf.root,
16162                                                  sym_name, reloc_name,
16163                                                  orig_rel.r_addend,
16164                                                  input_bfd, input_section,
16165                                                  rel->r_offset);
16166             }
16167           else
16168             {
16169               info->callbacks->einfo
16170                 /* xgettext:c-format */
16171                 (_("%H: %s against `%pT': error %d\n"),
16172                  input_bfd, input_section, rel->r_offset,
16173                  reloc_name, sym_name, (int) r);
16174               ret = FALSE;
16175             }
16176           if (more_info != NULL)
16177             free (more_info);
16178         }
16179     copy_reloc:
16180       if (wrel != rel)
16181         *wrel = *rel;
16182     }
16183
16184   if (wrel != rel)
16185     {
16186       Elf_Internal_Shdr *rel_hdr;
16187       size_t deleted = rel - wrel;
16188
16189       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16190       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16191       if (rel_hdr->sh_size == 0)
16192         {
16193           /* It is too late to remove an empty reloc section.  Leave
16194              one NONE reloc.
16195              ??? What is wrong with an empty section???  */
16196           rel_hdr->sh_size = rel_hdr->sh_entsize;
16197           deleted -= 1;
16198         }
16199       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16200       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16201       input_section->reloc_count -= deleted;
16202     }
16203
16204   /* If we're emitting relocations, then shortly after this function
16205      returns, reloc offsets and addends for this section will be
16206      adjusted.  Worse, reloc symbol indices will be for the output
16207      file rather than the input.  Save a copy of the relocs for
16208      opd_entry_value.  */
16209   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16210     {
16211       bfd_size_type amt;
16212       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16213       rel = bfd_alloc (input_bfd, amt);
16214       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16215       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16216       if (rel == NULL)
16217         return FALSE;
16218       memcpy (rel, relocs, amt);
16219     }
16220   return ret;
16221 }
16222
16223 /* Adjust the value of any local symbols in opd sections.  */
16224
16225 static int
16226 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16227                               const char *name ATTRIBUTE_UNUSED,
16228                               Elf_Internal_Sym *elfsym,
16229                               asection *input_sec,
16230                               struct elf_link_hash_entry *h)
16231 {
16232   struct _opd_sec_data *opd;
16233   long adjust;
16234   bfd_vma value;
16235
16236   if (h != NULL)
16237     return 1;
16238
16239   opd = get_opd_info (input_sec);
16240   if (opd == NULL || opd->adjust == NULL)
16241     return 1;
16242
16243   value = elfsym->st_value - input_sec->output_offset;
16244   if (!bfd_link_relocatable (info))
16245     value -= input_sec->output_section->vma;
16246
16247   adjust = opd->adjust[OPD_NDX (value)];
16248   if (adjust == -1)
16249     return 2;
16250
16251   elfsym->st_value += adjust;
16252   return 1;
16253 }
16254
16255 /* Finish up dynamic symbol handling.  We set the contents of various
16256    dynamic sections here.  */
16257
16258 static bfd_boolean
16259 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16260                                  struct bfd_link_info *info,
16261                                  struct elf_link_hash_entry *h,
16262                                  Elf_Internal_Sym *sym)
16263 {
16264   struct ppc_link_hash_table *htab;
16265   struct plt_entry *ent;
16266
16267   htab = ppc_hash_table (info);
16268   if (htab == NULL)
16269     return FALSE;
16270
16271   if (!htab->opd_abi && !h->def_regular)
16272     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16273       if (ent->plt.offset != (bfd_vma) -1)
16274         {
16275           /* Mark the symbol as undefined, rather than as
16276              defined in glink.  Leave the value if there were
16277              any relocations where pointer equality matters
16278              (this is a clue for the dynamic linker, to make
16279              function pointer comparisons work between an
16280              application and shared library), otherwise set it
16281              to zero.  */
16282           sym->st_shndx = SHN_UNDEF;
16283           if (!h->pointer_equality_needed)
16284             sym->st_value = 0;
16285           else if (!h->ref_regular_nonweak)
16286             {
16287               /* This breaks function pointer comparisons, but
16288                  that is better than breaking tests for a NULL
16289                  function pointer.  */
16290               sym->st_value = 0;
16291             }
16292           break;
16293         }
16294
16295   if (h->needs_copy)
16296     {
16297       /* This symbol needs a copy reloc.  Set it up.  */
16298       Elf_Internal_Rela rela;
16299       asection *srel;
16300       bfd_byte *loc;
16301
16302       if (h->dynindx == -1
16303           || (h->root.type != bfd_link_hash_defined
16304               && h->root.type != bfd_link_hash_defweak)
16305           || htab->elf.srelbss == NULL
16306           || htab->elf.sreldynrelro == NULL)
16307         abort ();
16308
16309       rela.r_offset = (h->root.u.def.value
16310                        + h->root.u.def.section->output_section->vma
16311                        + h->root.u.def.section->output_offset);
16312       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16313       rela.r_addend = 0;
16314       if (h->root.u.def.section == htab->elf.sdynrelro)
16315         srel = htab->elf.sreldynrelro;
16316       else
16317         srel = htab->elf.srelbss;
16318       loc = srel->contents;
16319       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16320       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16321     }
16322
16323   return TRUE;
16324 }
16325
16326 /* Used to decide how to sort relocs in an optimal manner for the
16327    dynamic linker, before writing them out.  */
16328
16329 static enum elf_reloc_type_class
16330 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16331                             const asection *rel_sec,
16332                             const Elf_Internal_Rela *rela)
16333 {
16334   enum elf_ppc64_reloc_type r_type;
16335   struct ppc_link_hash_table *htab = ppc_hash_table (info);
16336
16337   if (rel_sec == htab->elf.irelplt)
16338     return reloc_class_ifunc;
16339
16340   r_type = ELF64_R_TYPE (rela->r_info);
16341   switch (r_type)
16342     {
16343     case R_PPC64_RELATIVE:
16344       return reloc_class_relative;
16345     case R_PPC64_JMP_SLOT:
16346       return reloc_class_plt;
16347     case R_PPC64_COPY:
16348       return reloc_class_copy;
16349     default:
16350       return reloc_class_normal;
16351     }
16352 }
16353
16354 /* Finish up the dynamic sections.  */
16355
16356 static bfd_boolean
16357 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16358                                    struct bfd_link_info *info)
16359 {
16360   struct ppc_link_hash_table *htab;
16361   bfd *dynobj;
16362   asection *sdyn;
16363
16364   htab = ppc_hash_table (info);
16365   if (htab == NULL)
16366     return FALSE;
16367
16368   dynobj = htab->elf.dynobj;
16369   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16370
16371   if (htab->elf.dynamic_sections_created)
16372     {
16373       Elf64_External_Dyn *dyncon, *dynconend;
16374
16375       if (sdyn == NULL || htab->elf.sgot == NULL)
16376         abort ();
16377
16378       dyncon = (Elf64_External_Dyn *) sdyn->contents;
16379       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16380       for (; dyncon < dynconend; dyncon++)
16381         {
16382           Elf_Internal_Dyn dyn;
16383           asection *s;
16384
16385           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16386
16387           switch (dyn.d_tag)
16388             {
16389             default:
16390               continue;
16391
16392             case DT_PPC64_GLINK:
16393               s = htab->glink;
16394               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16395               /* We stupidly defined DT_PPC64_GLINK to be the start
16396                  of glink rather than the first entry point, which is
16397                  what ld.so needs, and now have a bigger stub to
16398                  support automatic multiple TOCs.  */
16399               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16400               break;
16401
16402             case DT_PPC64_OPD:
16403               s = bfd_get_section_by_name (output_bfd, ".opd");
16404               if (s == NULL)
16405                 continue;
16406               dyn.d_un.d_ptr = s->vma;
16407               break;
16408
16409             case DT_PPC64_OPT:
16410               if ((htab->do_multi_toc && htab->multi_toc_needed)
16411                   || htab->notoc_plt)
16412                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16413               if (htab->has_plt_localentry0)
16414                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16415               break;
16416
16417             case DT_PPC64_OPDSZ:
16418               s = bfd_get_section_by_name (output_bfd, ".opd");
16419               if (s == NULL)
16420                 continue;
16421               dyn.d_un.d_val = s->size;
16422               break;
16423
16424             case DT_PLTGOT:
16425               s = htab->elf.splt;
16426               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16427               break;
16428
16429             case DT_JMPREL:
16430               s = htab->elf.srelplt;
16431               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16432               break;
16433
16434             case DT_PLTRELSZ:
16435               dyn.d_un.d_val = htab->elf.srelplt->size;
16436               break;
16437
16438             case DT_TEXTREL:
16439               if (htab->local_ifunc_resolver)
16440                 info->callbacks->einfo
16441                   (_("%X%P: text relocations and GNU indirect "
16442                      "functions will result in a segfault at runtime\n"));
16443               else if (htab->maybe_local_ifunc_resolver)
16444                 info->callbacks->einfo
16445                   (_("%P: warning: text relocations and GNU indirect "
16446                      "functions may result in a segfault at runtime\n"));
16447               continue;
16448             }
16449
16450           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16451         }
16452     }
16453
16454   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16455       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16456     {
16457       /* Fill in the first entry in the global offset table.
16458          We use it to hold the link-time TOCbase.  */
16459       bfd_put_64 (output_bfd,
16460                   elf_gp (output_bfd) + TOC_BASE_OFF,
16461                   htab->elf.sgot->contents);
16462
16463       /* Set .got entry size.  */
16464       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16465         = 8;
16466     }
16467
16468   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16469       && htab->elf.splt->output_section != bfd_abs_section_ptr)
16470     {
16471       /* Set .plt entry size.  */
16472       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16473         = PLT_ENTRY_SIZE (htab);
16474     }
16475
16476   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16477      brlt ourselves if emitrelocations.  */
16478   if (htab->brlt != NULL
16479       && htab->brlt->reloc_count != 0
16480       && !_bfd_elf_link_output_relocs (output_bfd,
16481                                        htab->brlt,
16482                                        elf_section_data (htab->brlt)->rela.hdr,
16483                                        elf_section_data (htab->brlt)->relocs,
16484                                        NULL))
16485     return FALSE;
16486
16487   if (htab->glink != NULL
16488       && htab->glink->reloc_count != 0
16489       && !_bfd_elf_link_output_relocs (output_bfd,
16490                                        htab->glink,
16491                                        elf_section_data (htab->glink)->rela.hdr,
16492                                        elf_section_data (htab->glink)->relocs,
16493                                        NULL))
16494     return FALSE;
16495
16496
16497   if (htab->glink_eh_frame != NULL
16498       && htab->glink_eh_frame->size != 0
16499       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16500       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16501                                            htab->glink_eh_frame,
16502                                            htab->glink_eh_frame->contents))
16503     return FALSE;
16504
16505   /* We need to handle writing out multiple GOT sections ourselves,
16506      since we didn't add them to DYNOBJ.  We know dynobj is the first
16507      bfd.  */
16508   while ((dynobj = dynobj->link.next) != NULL)
16509     {
16510       asection *s;
16511
16512       if (!is_ppc64_elf (dynobj))
16513         continue;
16514
16515       s = ppc64_elf_tdata (dynobj)->got;
16516       if (s != NULL
16517           && s->size != 0
16518           && s->output_section != bfd_abs_section_ptr
16519           && !bfd_set_section_contents (output_bfd, s->output_section,
16520                                         s->contents, s->output_offset,
16521                                         s->size))
16522         return FALSE;
16523       s = ppc64_elf_tdata (dynobj)->relgot;
16524       if (s != NULL
16525           && s->size != 0
16526           && s->output_section != bfd_abs_section_ptr
16527           && !bfd_set_section_contents (output_bfd, s->output_section,
16528                                         s->contents, s->output_offset,
16529                                         s->size))
16530         return FALSE;
16531     }
16532
16533   return TRUE;
16534 }
16535
16536 #include "elf64-target.h"
16537
16538 /* FreeBSD support */
16539
16540 #undef  TARGET_LITTLE_SYM
16541 #undef  TARGET_LITTLE_NAME
16542
16543 #undef  TARGET_BIG_SYM
16544 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
16545 #undef  TARGET_BIG_NAME
16546 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16547
16548 #undef  ELF_OSABI
16549 #define ELF_OSABI       ELFOSABI_FREEBSD
16550
16551 #undef  elf64_bed
16552 #define elf64_bed       elf64_powerpc_fbsd_bed
16553
16554 #include "elf64-target.h"