[PR ld/25062] arm: sign extend the addend of R_ARM_TLS_GOTDESC
[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 LI_R11_0        0x39600000      /* li    %r11,0         */
191 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
192 #define LIS_R11         0x3d600000      /* lis %r11,xxx@ha        */
193 #define LIS_R12         0x3d800000      /* lis %r12,xxx@ha        */
194 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
195 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
196 #define ADDIS_R12_R11   0x3d8b0000      /* addis %r12,%r11,xxx@ha */
197 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
198 #define ORIS_R12_R12_0  0x658c0000      /* oris  %r12,%r12,xxx@hi */
199 #define ORI_R11_R11_0   0x616b0000      /* ori   %r11,%r11,xxx@l  */
200 #define ORI_R12_R12_0   0x618c0000      /* ori   %r12,%r12,xxx@l  */
201 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
202 #define SLDI_R11_R11_34 0x796b1746      /* sldi  %r11,%r11,34     */
203 #define SLDI_R12_R12_32 0x799c07c6      /* sldi  %r12,%r12,32     */
204 #define LDX_R12_R11_R12 0x7d8b602a      /* ldx   %r12,%r11,%r12   */
205 #define ADD_R12_R11_R12 0x7d8b6214      /* add   %r12,%r11,%r12   */
206 #define PADDI_R12_PC    0x0610000039800000ULL
207 #define PLD_R12_PC      0x04100000e5800000ULL
208 #define PNOP            0x0700000000000000ULL
209
210 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
211 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
212   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
213                                         /* 0:                           */
214                                         /*  .quad plt0-1f               */
215                                         /* __glink:                     */
216 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
217 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
218                                         /* 1:                           */
219 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
220                                         /*  ld %2,(0b-1b)(%11)          */
221 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
222 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
223                                         /*  ld %12,0(%11)               */
224                                         /*  ld %2,8(%11)                */
225                                         /*  mtctr %12                   */
226                                         /*  ld %11,16(%11)              */
227                                         /*  bctr                        */
228 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
229 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
230 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
231 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
232 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
233
234 /* Pad with this.  */
235 #define NOP             0x60000000
236
237 /* Some other nops.  */
238 #define CROR_151515     0x4def7b82
239 #define CROR_313131     0x4ffffb82
240
241 /* .glink entries for the first 32k functions are two instructions.  */
242 #define LI_R0_0         0x38000000      /* li    %r0,0          */
243 #define B_DOT           0x48000000      /* b     .              */
244
245 /* After that, we need two instructions to load the index, followed by
246    a branch.  */
247 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
248 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
249
250 /* Instructions used by the save and restore reg functions.  */
251 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
252 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
253 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
254 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
255 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
256 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
257 #define LI_R12_0        0x39800000      /* li    %r12,0         */
258 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
259 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
260 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
261 #define BLR             0x4e800020      /* blr                  */
262
263 /* Since .opd is an array of descriptors and each entry will end up
264    with identical R_PPC64_RELATIVE relocs, there is really no need to
265    propagate .opd relocs;  The dynamic linker should be taught to
266    relocate .opd without reloc entries.  */
267 #ifndef NO_OPD_RELOCS
268 #define NO_OPD_RELOCS 0
269 #endif
270
271 #ifndef ARRAY_SIZE
272 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
273 #endif
274
275 static inline int
276 abiversion (bfd *abfd)
277 {
278   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
279 }
280
281 static inline void
282 set_abiversion (bfd *abfd, int ver)
283 {
284   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
285   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
286 }
287 \f
288 /* Relocation HOWTO's.  */
289 /* Like other ELF RELA targets that don't apply multiple
290    field-altering relocations to the same localation, src_mask is
291    always zero and pcrel_offset is the same as pc_relative.
292    PowerPC can always use a zero bitpos, even when the field is not at
293    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
294    and bitpos=2 which matches the ABI description, or as we do here,
295    rightshift=0, bitsize=26 and bitpos=0.  */
296 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
297             complain, special_func)                             \
298   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,       \
299          complain_overflow_ ## complain, special_func,          \
300          #type, FALSE, 0, mask, pc_relative)
301
302 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
303
304 static reloc_howto_type ppc64_elf_howto_raw[] =
305 {
306   /* This reloc does nothing.  */
307   HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
308        bfd_elf_generic_reloc),
309
310   /* A standard 32 bit relocation.  */
311   HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
312        bfd_elf_generic_reloc),
313
314   /* An absolute 26 bit branch; the lower two bits must be zero.
315      FIXME: we don't check that, we just clear them.  */
316   HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
317        bfd_elf_generic_reloc),
318
319   /* A standard 16 bit relocation.  */
320   HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
321        bfd_elf_generic_reloc),
322
323   /* A 16 bit relocation without overflow.  */
324   HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
325        bfd_elf_generic_reloc),
326
327   /* Bits 16-31 of an address.  */
328   HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
329        bfd_elf_generic_reloc),
330
331   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
332      bits, treated as a signed number, is negative.  */
333   HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
334        ppc64_elf_ha_reloc),
335
336   /* An absolute 16 bit branch; the lower two bits must be zero.
337      FIXME: we don't check that, we just clear them.  */
338   HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
339        ppc64_elf_branch_reloc),
340
341   /* An absolute 16 bit branch, for which bit 10 should be set to
342      indicate that the branch is expected to be taken.  The lower two
343      bits must be zero.  */
344   HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
345        ppc64_elf_brtaken_reloc),
346
347   /* An absolute 16 bit branch, for which bit 10 should be set to
348      indicate that the branch is not expected to be taken.  The lower
349      two bits must be zero.  */
350   HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
351        ppc64_elf_brtaken_reloc),
352
353   /* A relative 26 bit branch; the lower two bits must be zero.  */
354   HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
355        ppc64_elf_branch_reloc),
356
357   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
358   HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
359        ppc64_elf_branch_reloc),
360
361   /* A relative 16 bit branch; the lower two bits must be zero.  */
362   HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
363        ppc64_elf_branch_reloc),
364
365   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
366      the branch is expected to be taken.  The lower two bits must be
367      zero.  */
368   HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
369        ppc64_elf_brtaken_reloc),
370
371   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
372      the branch is not expected to be taken.  The lower two bits must
373      be zero.  */
374   HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
375        ppc64_elf_brtaken_reloc),
376
377   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
378      symbol.  */
379   HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
380        ppc64_elf_unhandled_reloc),
381
382   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
383      the symbol.  */
384   HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
385        ppc64_elf_unhandled_reloc),
386
387   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388      the symbol.  */
389   HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
390        ppc64_elf_unhandled_reloc),
391
392   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
393      the symbol.  */
394   HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
395        ppc64_elf_unhandled_reloc),
396
397   /* This is used only by the dynamic linker.  The symbol should exist
398      both in the object being run and in some shared library.  The
399      dynamic linker copies the data addressed by the symbol from the
400      shared library into the object, because the object being
401      run has to have the data at some particular address.  */
402   HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
403        ppc64_elf_unhandled_reloc),
404
405   /* Like R_PPC64_ADDR64, but used when setting global offset table
406      entries.  */
407   HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
408        ppc64_elf_unhandled_reloc),
409
410   /* Created by the link editor.  Marks a procedure linkage table
411      entry for a symbol.  */
412   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
413        ppc64_elf_unhandled_reloc),
414
415   /* Used only by the dynamic linker.  When the object is run, this
416      doubleword64 is set to the load address of the object, plus the
417      addend.  */
418   HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419        bfd_elf_generic_reloc),
420
421   /* Like R_PPC64_ADDR32, but may be unaligned.  */
422   HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
423        bfd_elf_generic_reloc),
424
425   /* Like R_PPC64_ADDR16, but may be unaligned.  */
426   HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
427        bfd_elf_generic_reloc),
428
429   /* 32-bit PC relative.  */
430   HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431        bfd_elf_generic_reloc),
432
433   /* 32-bit relocation to the symbol's procedure linkage table.  */
434   HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
435        ppc64_elf_unhandled_reloc),
436
437   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
438      FIXME: R_PPC64_PLTREL32 not supported.  */
439   HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
440        ppc64_elf_unhandled_reloc),
441
442   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
443      the symbol.  */
444   HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
445        ppc64_elf_unhandled_reloc),
446
447   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
448      the symbol.  */
449   HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
450        ppc64_elf_unhandled_reloc),
451
452   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
453      the symbol.  */
454   HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
455        ppc64_elf_unhandled_reloc),
456
457   /* 16-bit section relative relocation.  */
458   HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
459        ppc64_elf_sectoff_reloc),
460
461   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
462   HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
463        ppc64_elf_sectoff_reloc),
464
465   /* 16-bit upper half section relative relocation.  */
466   HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
467        ppc64_elf_sectoff_reloc),
468
469   /* 16-bit upper half adjusted section relative relocation.  */
470   HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
471        ppc64_elf_sectoff_ha_reloc),
472
473   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
474   HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
475        bfd_elf_generic_reloc),
476
477   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
478
479   /* A standard 64-bit relocation.  */
480   HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
481        bfd_elf_generic_reloc),
482
483   /* The bits 32-47 of an address.  */
484   HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
485        bfd_elf_generic_reloc),
486
487   /* The bits 32-47 of an address, plus 1 if the contents of the low
488      16 bits, treated as a signed number, is negative.  */
489   HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
490        ppc64_elf_ha_reloc),
491
492   /* The bits 48-63 of an address.  */
493   HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
494        bfd_elf_generic_reloc),
495
496   /* The bits 48-63 of an address, plus 1 if the contents of the low
497      16 bits, treated as a signed number, is negative.  */
498   HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
499        ppc64_elf_ha_reloc),
500
501   /* Like ADDR64, but may be unaligned.  */
502   HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
503        bfd_elf_generic_reloc),
504
505   /* 64-bit relative relocation.  */
506   HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
507        bfd_elf_generic_reloc),
508
509   /* 64-bit relocation to the symbol's procedure linkage table.  */
510   HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
511        ppc64_elf_unhandled_reloc),
512
513   /* 64-bit PC relative relocation to the symbol's procedure linkage
514      table.  */
515   /* FIXME: R_PPC64_PLTREL64 not supported.  */
516   HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
517        ppc64_elf_unhandled_reloc),
518
519   /* 16 bit TOC-relative relocation.  */
520   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
521   HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
522        ppc64_elf_toc_reloc),
523
524   /* 16 bit TOC-relative relocation without overflow.  */
525   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
526   HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
527        ppc64_elf_toc_reloc),
528
529   /* 16 bit TOC-relative relocation, high 16 bits.  */
530   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
531   HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
532        ppc64_elf_toc_reloc),
533
534   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
535      contents of the low 16 bits, treated as a signed number, is
536      negative.  */
537   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
538   HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
539        ppc64_elf_toc_ha_reloc),
540
541   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
542   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
543   HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
544        ppc64_elf_toc64_reloc),
545
546   /* Like R_PPC64_GOT16, but also informs the link editor that the
547      value to relocate may (!) refer to a PLT entry which the link
548      editor (a) may replace with the symbol value.  If the link editor
549      is unable to fully resolve the symbol, it may (b) create a PLT
550      entry and store the address to the new PLT entry in the GOT.
551      This permits lazy resolution of function symbols at run time.
552      The link editor may also skip all of this and just (c) emit a
553      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
554   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
555     HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
556           ppc64_elf_unhandled_reloc),
557
558   /* Like R_PPC64_PLTGOT16, but without overflow.  */
559   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
560   HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
561        ppc64_elf_unhandled_reloc),
562
563   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
564   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
565   HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
566        ppc64_elf_unhandled_reloc),
567
568   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
569      1 if the contents of the low 16 bits, treated as a signed number,
570      is negative.  */
571   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
572   HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
573        ppc64_elf_unhandled_reloc),
574
575   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
576   HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
577        bfd_elf_generic_reloc),
578
579   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
580   HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
581        bfd_elf_generic_reloc),
582
583   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
584   HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
585        ppc64_elf_unhandled_reloc),
586
587   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
588   HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
589        ppc64_elf_unhandled_reloc),
590
591   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
592   HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593        ppc64_elf_unhandled_reloc),
594
595   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
596   HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
597        ppc64_elf_sectoff_reloc),
598
599   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
600   HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
601        ppc64_elf_sectoff_reloc),
602
603   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
604   HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605        ppc64_elf_toc_reloc),
606
607   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
608   HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
609        ppc64_elf_toc_reloc),
610
611   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
612   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
613   HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
614        ppc64_elf_unhandled_reloc),
615
616   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
617   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
618   HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
619        ppc64_elf_unhandled_reloc),
620
621   /* Marker relocs for TLS.  */
622   HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
623        bfd_elf_generic_reloc),
624
625   HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
626        bfd_elf_generic_reloc),
627
628   HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
629        bfd_elf_generic_reloc),
630
631   /* Marker reloc for optimizing r2 save in prologue rather than on
632      each plt call stub.  */
633   HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
634        bfd_elf_generic_reloc),
635
636   /* Marker relocs on inline plt call instructions.  */
637   HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
638        bfd_elf_generic_reloc),
639
640   HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
641        bfd_elf_generic_reloc),
642
643   /* Computes the load module index of the load module that contains the
644      definition of its TLS sym.  */
645   HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
646        ppc64_elf_unhandled_reloc),
647
648   /* Computes a dtv-relative displacement, the difference between the value
649      of sym+add and the base address of the thread-local storage block that
650      contains the definition of sym, minus 0x8000.  */
651   HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
652        ppc64_elf_unhandled_reloc),
653
654   /* A 16 bit dtprel reloc.  */
655   HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
656        ppc64_elf_unhandled_reloc),
657
658   /* Like DTPREL16, but no overflow.  */
659   HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
660        ppc64_elf_unhandled_reloc),
661
662   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
663   HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
664        ppc64_elf_unhandled_reloc),
665
666   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
667   HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
668        ppc64_elf_unhandled_reloc),
669
670   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
671   HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
672        ppc64_elf_unhandled_reloc),
673
674   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
675   HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
676        ppc64_elf_unhandled_reloc),
677
678   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
679   HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
680        ppc64_elf_unhandled_reloc),
681
682   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
683   HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
684        ppc64_elf_unhandled_reloc),
685
686   /* Like DTPREL16, but for insns with a DS field.  */
687   HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
688        ppc64_elf_unhandled_reloc),
689
690   /* Like DTPREL16_DS, but no overflow.  */
691   HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
692        ppc64_elf_unhandled_reloc),
693
694   /* Computes a tp-relative displacement, the difference between the value of
695      sym+add and the value of the thread pointer (r13).  */
696   HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
697        ppc64_elf_unhandled_reloc),
698
699   /* A 16 bit tprel reloc.  */
700   HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
701        ppc64_elf_unhandled_reloc),
702
703   /* Like TPREL16, but no overflow.  */
704   HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
705        ppc64_elf_unhandled_reloc),
706
707   /* Like TPREL16_LO, but next higher group of 16 bits.  */
708   HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
709        ppc64_elf_unhandled_reloc),
710
711   /* Like TPREL16_HI, but adjust for low 16 bits.  */
712   HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
713        ppc64_elf_unhandled_reloc),
714
715   /* Like TPREL16_HI, but next higher group of 16 bits.  */
716   HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
717        ppc64_elf_unhandled_reloc),
718
719   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
720   HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
721        ppc64_elf_unhandled_reloc),
722
723   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
724   HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
725        ppc64_elf_unhandled_reloc),
726
727   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
728   HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
729        ppc64_elf_unhandled_reloc),
730
731   /* Like TPREL16, but for insns with a DS field.  */
732   HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
733        ppc64_elf_unhandled_reloc),
734
735   /* Like TPREL16_DS, but no overflow.  */
736   HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
737        ppc64_elf_unhandled_reloc),
738
739   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
740      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
741      to the first entry relative to the TOC base (r2).  */
742   HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
743        ppc64_elf_unhandled_reloc),
744
745   /* Like GOT_TLSGD16, but no overflow.  */
746   HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
747        ppc64_elf_unhandled_reloc),
748
749   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
750   HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
751        ppc64_elf_unhandled_reloc),
752
753   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
754   HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
755        ppc64_elf_unhandled_reloc),
756
757   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
758      with values (sym+add)@dtpmod and zero, and computes the offset to the
759      first entry relative to the TOC base (r2).  */
760   HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
761        ppc64_elf_unhandled_reloc),
762
763   /* Like GOT_TLSLD16, but no overflow.  */
764   HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
765        ppc64_elf_unhandled_reloc),
766
767   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
768   HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
769        ppc64_elf_unhandled_reloc),
770
771   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
772   HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
773        ppc64_elf_unhandled_reloc),
774
775   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
776      the offset to the entry relative to the TOC base (r2).  */
777   HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
778        ppc64_elf_unhandled_reloc),
779
780   /* Like GOT_DTPREL16_DS, but no overflow.  */
781   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
782        ppc64_elf_unhandled_reloc),
783
784   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
785   HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
786        ppc64_elf_unhandled_reloc),
787
788   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
789   HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
790        ppc64_elf_unhandled_reloc),
791
792   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
793      offset to the entry relative to the TOC base (r2).  */
794   HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
795        ppc64_elf_unhandled_reloc),
796
797   /* Like GOT_TPREL16_DS, but no overflow.  */
798   HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
799        ppc64_elf_unhandled_reloc),
800
801   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
802   HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
803        ppc64_elf_unhandled_reloc),
804
805   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
806   HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
807        ppc64_elf_unhandled_reloc),
808
809   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
810        ppc64_elf_unhandled_reloc),
811
812   HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
813        bfd_elf_generic_reloc),
814
815   /* A 16 bit relative relocation.  */
816   HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
817        bfd_elf_generic_reloc),
818
819   /* A 16 bit relative relocation without overflow.  */
820   HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
821        bfd_elf_generic_reloc),
822
823   /* The high order 16 bits of a relative address.  */
824   HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
825        bfd_elf_generic_reloc),
826
827   /* The high order 16 bits of a relative address, plus 1 if the contents of
828      the low 16 bits, treated as a signed number, is negative.  */
829   HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
830        ppc64_elf_ha_reloc),
831
832   HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
833        bfd_elf_generic_reloc),
834
835   HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
836        ppc64_elf_ha_reloc),
837
838   HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
839        bfd_elf_generic_reloc),
840
841   HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
842        ppc64_elf_ha_reloc),
843
844   HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
845        bfd_elf_generic_reloc),
846
847   HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
848        ppc64_elf_ha_reloc),
849
850   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
851   HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
852        ppc64_elf_ha_reloc),
853
854   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
855   HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
856        ppc64_elf_ha_reloc),
857
858   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
859   HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
860        bfd_elf_generic_reloc),
861
862   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
863   HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
864        ppc64_elf_ha_reloc),
865
866   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
867   HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
868        ppc64_elf_unhandled_reloc),
869
870   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
871   HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
872        ppc64_elf_unhandled_reloc),
873
874   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
875   HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
876        ppc64_elf_unhandled_reloc),
877
878   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
879   HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
880        ppc64_elf_unhandled_reloc),
881
882   /* Marker reloc on ELFv2 large-model function entry.  */
883   HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
884        bfd_elf_generic_reloc),
885
886   /* Like ADDR64, but use local entry point of function.  */
887   HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
888        bfd_elf_generic_reloc),
889
890   HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
891        bfd_elf_generic_reloc),
892
893   HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
894        bfd_elf_generic_reloc),
895
896   HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
897        bfd_elf_generic_reloc),
898
899   HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
900        ppc64_elf_prefix_reloc),
901
902   HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
903        ppc64_elf_prefix_reloc),
904
905   HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
906        ppc64_elf_prefix_reloc),
907
908   HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
909        ppc64_elf_prefix_reloc),
910
911   HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
912        ppc64_elf_prefix_reloc),
913
914   HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
915        ppc64_elf_unhandled_reloc),
916
917   HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
918        ppc64_elf_unhandled_reloc),
919
920   HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
921        ppc64_elf_unhandled_reloc),
922
923   HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
924        ppc64_elf_unhandled_reloc),
925
926   HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
927        ppc64_elf_unhandled_reloc),
928
929   HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
930        ppc64_elf_unhandled_reloc),
931
932   HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
933        ppc64_elf_unhandled_reloc),
934
935   HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
936        ppc64_elf_unhandled_reloc),
937
938   HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
939        ppc64_elf_unhandled_reloc),
940
941   HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
942        bfd_elf_generic_reloc),
943
944   HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
945        ppc64_elf_ha_reloc),
946
947   HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
948        bfd_elf_generic_reloc),
949
950   HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
951        ppc64_elf_ha_reloc),
952
953   HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
954        bfd_elf_generic_reloc),
955
956   HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
957        ppc64_elf_ha_reloc),
958
959   HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
960        bfd_elf_generic_reloc),
961
962   HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
963        ppc64_elf_ha_reloc),
964
965   HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
966        ppc64_elf_prefix_reloc),
967
968   HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
969        ppc64_elf_prefix_reloc),
970
971   /* GNU extension to record C++ vtable hierarchy.  */
972   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
973        NULL),
974
975   /* GNU extension to record C++ vtable member usage.  */
976   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
977        NULL),
978 };
979
980 \f
981 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
982    be done.  */
983
984 static void
985 ppc_howto_init (void)
986 {
987   unsigned int i, type;
988
989   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
990     {
991       type = ppc64_elf_howto_raw[i].type;
992       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
993       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
994     }
995 }
996
997 static reloc_howto_type *
998 ppc64_elf_reloc_type_lookup (bfd *abfd,
999                              bfd_reloc_code_real_type code)
1000 {
1001   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1002
1003   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1004     /* Initialize howto table if needed.  */
1005     ppc_howto_init ();
1006
1007   switch (code)
1008     {
1009     default:
1010       /* xgettext:c-format */
1011       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1012                           (int) code);
1013       bfd_set_error (bfd_error_bad_value);
1014       return NULL;
1015
1016     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
1017       break;
1018     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
1019       break;
1020     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
1021       break;
1022     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
1023       break;
1024     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1025       break;
1026     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1027       break;
1028     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
1029       break;
1030     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1031       break;
1032     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
1033       break;
1034     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1035       break;
1036     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1037       break;
1038     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1039       break;
1040     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1041       break;
1042     case BFD_RELOC_PPC64_REL24_NOTOC:           r = R_PPC64_REL24_NOTOC;
1043       break;
1044     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1045       break;
1046     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1047       break;
1048     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1049       break;
1050     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1051       break;
1052     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1053       break;
1054     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1055       break;
1056     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1057       break;
1058     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1059       break;
1060     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1061       break;
1062     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1063       break;
1064     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1065       break;
1066     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1067       break;
1068     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1069       break;
1070     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1071       break;
1072     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1073       break;
1074     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1075       break;
1076     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1077       break;
1078     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1079       break;
1080     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1081       break;
1082     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1083       break;
1084     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1085       break;
1086     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1087       break;
1088     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1089       break;
1090     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1091       break;
1092     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1093       break;
1094     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1095       break;
1096     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1097       break;
1098     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1099       break;
1100     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1101       break;
1102     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1103       break;
1104     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1105       break;
1106     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1107       break;
1108     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1109       break;
1110     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1111       break;
1112     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1113       break;
1114     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1115       break;
1116     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1117       break;
1118     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1119       break;
1120     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1121       break;
1122     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1123       break;
1124     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1125       break;
1126     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1127       break;
1128     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1129       break;
1130     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
1131       break;
1132     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
1133       break;
1134     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
1135       break;
1136     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
1137       break;
1138     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
1139       break;
1140     case BFD_RELOC_PPC64_TLS_PCREL:
1141     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
1142       break;
1143     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
1144       break;
1145     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
1146       break;
1147     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
1148       break;
1149     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
1150       break;
1151     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
1152       break;
1153     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
1154       break;
1155     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
1156       break;
1157     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
1158       break;
1159     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
1160       break;
1161     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
1162       break;
1163     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
1164       break;
1165     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
1166       break;
1167     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
1168       break;
1169     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
1170       break;
1171     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
1172       break;
1173     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
1174       break;
1175     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
1176       break;
1177     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
1178       break;
1179     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
1180       break;
1181     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
1182       break;
1183     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
1184       break;
1185     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
1186       break;
1187     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
1188       break;
1189     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
1190       break;
1191     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
1192       break;
1193     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
1194       break;
1195     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
1196       break;
1197     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
1198       break;
1199     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
1200       break;
1201     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
1202       break;
1203     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
1204       break;
1205     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
1206       break;
1207     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
1208       break;
1209     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
1210       break;
1211     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
1212       break;
1213     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
1214       break;
1215     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
1216       break;
1217     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
1218       break;
1219     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
1220       break;
1221     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
1222       break;
1223     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
1224       break;
1225     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
1226       break;
1227     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
1228       break;
1229     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
1230       break;
1231     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
1232       break;
1233     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
1234       break;
1235     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
1236       break;
1237     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
1238       break;
1239     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
1240       break;
1241     case BFD_RELOC_PPC64_REL16_HIGH:            r = R_PPC64_REL16_HIGH;
1242       break;
1243     case BFD_RELOC_PPC64_REL16_HIGHA:           r = R_PPC64_REL16_HIGHA;
1244       break;
1245     case BFD_RELOC_PPC64_REL16_HIGHER:          r = R_PPC64_REL16_HIGHER;
1246       break;
1247     case BFD_RELOC_PPC64_REL16_HIGHERA:         r = R_PPC64_REL16_HIGHERA;
1248       break;
1249     case BFD_RELOC_PPC64_REL16_HIGHEST:         r = R_PPC64_REL16_HIGHEST;
1250       break;
1251     case BFD_RELOC_PPC64_REL16_HIGHESTA:        r = R_PPC64_REL16_HIGHESTA;
1252       break;
1253     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
1254       break;
1255     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
1256       break;
1257     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
1258       break;
1259     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
1260       break;
1261     case BFD_RELOC_PPC64_D34:                   r = R_PPC64_D34;
1262       break;
1263     case BFD_RELOC_PPC64_D34_LO:                r = R_PPC64_D34_LO;
1264       break;
1265     case BFD_RELOC_PPC64_D34_HI30:              r = R_PPC64_D34_HI30;
1266       break;
1267     case BFD_RELOC_PPC64_D34_HA30:              r = R_PPC64_D34_HA30;
1268       break;
1269     case BFD_RELOC_PPC64_PCREL34:               r = R_PPC64_PCREL34;
1270       break;
1271     case BFD_RELOC_PPC64_GOT_PCREL34:           r = R_PPC64_GOT_PCREL34;
1272       break;
1273     case BFD_RELOC_PPC64_PLT_PCREL34:           r = R_PPC64_PLT_PCREL34;
1274       break;
1275     case BFD_RELOC_PPC64_TPREL34:               r = R_PPC64_TPREL34;
1276       break;
1277     case BFD_RELOC_PPC64_DTPREL34:              r = R_PPC64_DTPREL34;
1278       break;
1279     case BFD_RELOC_PPC64_GOT_TLSGD34:           r = R_PPC64_GOT_TLSGD34;
1280       break;
1281     case BFD_RELOC_PPC64_GOT_TLSLD34:           r = R_PPC64_GOT_TLSLD34;
1282       break;
1283     case BFD_RELOC_PPC64_GOT_TPREL34:           r = R_PPC64_GOT_TPREL34;
1284       break;
1285     case BFD_RELOC_PPC64_GOT_DTPREL34:          r = R_PPC64_GOT_DTPREL34;
1286       break;
1287     case BFD_RELOC_PPC64_ADDR16_HIGHER34:       r = R_PPC64_ADDR16_HIGHER34;
1288       break;
1289     case BFD_RELOC_PPC64_ADDR16_HIGHERA34:      r = R_PPC64_ADDR16_HIGHERA34;
1290       break;
1291     case BFD_RELOC_PPC64_ADDR16_HIGHEST34:      r = R_PPC64_ADDR16_HIGHEST34;
1292       break;
1293     case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:     r = R_PPC64_ADDR16_HIGHESTA34;
1294       break;
1295     case BFD_RELOC_PPC64_REL16_HIGHER34:        r = R_PPC64_REL16_HIGHER34;
1296       break;
1297     case BFD_RELOC_PPC64_REL16_HIGHERA34:       r = R_PPC64_REL16_HIGHERA34;
1298       break;
1299     case BFD_RELOC_PPC64_REL16_HIGHEST34:       r = R_PPC64_REL16_HIGHEST34;
1300       break;
1301     case BFD_RELOC_PPC64_REL16_HIGHESTA34:      r = R_PPC64_REL16_HIGHESTA34;
1302       break;
1303     case BFD_RELOC_PPC64_D28:                   r = R_PPC64_D28;
1304       break;
1305     case BFD_RELOC_PPC64_PCREL28:               r = R_PPC64_PCREL28;
1306       break;
1307     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
1308       break;
1309     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
1310       break;
1311     }
1312
1313   return ppc64_elf_howto_table[r];
1314 };
1315
1316 static reloc_howto_type *
1317 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1318                              const char *r_name)
1319 {
1320   unsigned int i;
1321
1322   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1323     if (ppc64_elf_howto_raw[i].name != NULL
1324         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1325       return &ppc64_elf_howto_raw[i];
1326
1327   return NULL;
1328 }
1329
1330 /* Set the howto pointer for a PowerPC ELF reloc.  */
1331
1332 static bfd_boolean
1333 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1334                          Elf_Internal_Rela *dst)
1335 {
1336   unsigned int type;
1337
1338   /* Initialize howto table if needed.  */
1339   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1340     ppc_howto_init ();
1341
1342   type = ELF64_R_TYPE (dst->r_info);
1343   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1344     {
1345       /* xgettext:c-format */
1346       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1347                           abfd, type);
1348       bfd_set_error (bfd_error_bad_value);
1349       return FALSE;
1350     }
1351   cache_ptr->howto = ppc64_elf_howto_table[type];
1352   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1353     {
1354       /* xgettext:c-format */
1355       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1356                           abfd, type);
1357       bfd_set_error (bfd_error_bad_value);
1358       return FALSE;
1359     }
1360
1361   return TRUE;
1362 }
1363
1364 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1365
1366 static bfd_reloc_status_type
1367 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1368                     void *data, asection *input_section,
1369                     bfd *output_bfd, char **error_message)
1370 {
1371   enum elf_ppc64_reloc_type r_type;
1372   long insn;
1373   bfd_size_type octets;
1374   bfd_vma value;
1375
1376   /* If this is a relocatable link (output_bfd test tells us), just
1377      call the generic function.  Any adjustment will be done at final
1378      link time.  */
1379   if (output_bfd != NULL)
1380     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1381                                   input_section, output_bfd, error_message);
1382
1383   /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1384      We won't actually be using the low bits, so trashing them
1385      doesn't matter.  */
1386   r_type = reloc_entry->howto->type;
1387   if (r_type == R_PPC64_ADDR16_HIGHERA34
1388       || r_type == R_PPC64_ADDR16_HIGHESTA34
1389       || r_type == R_PPC64_REL16_HIGHERA34
1390       || r_type == R_PPC64_REL16_HIGHESTA34)
1391     reloc_entry->addend += 1ULL << 33;
1392   else
1393     reloc_entry->addend += 1U << 15;
1394   if (r_type != R_PPC64_REL16DX_HA)
1395     return bfd_reloc_continue;
1396
1397   value = 0;
1398   if (!bfd_is_com_section (symbol->section))
1399     value = symbol->value;
1400   value += (reloc_entry->addend
1401             + symbol->section->output_offset
1402             + symbol->section->output_section->vma);
1403   value -= (reloc_entry->address
1404             + input_section->output_offset
1405             + input_section->output_section->vma);
1406   value = (bfd_signed_vma) value >> 16;
1407
1408   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1409   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1410   insn &= ~0x1fffc1;
1411   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1412   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1413   if (value + 0x8000 > 0xffff)
1414     return bfd_reloc_overflow;
1415   return bfd_reloc_ok;
1416 }
1417
1418 static bfd_reloc_status_type
1419 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1420                         void *data, asection *input_section,
1421                         bfd *output_bfd, char **error_message)
1422 {
1423   if (output_bfd != NULL)
1424     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1425                                   input_section, output_bfd, error_message);
1426
1427   if (strcmp (symbol->section->name, ".opd") == 0
1428       && (symbol->section->owner->flags & DYNAMIC) == 0)
1429     {
1430       bfd_vma dest = opd_entry_value (symbol->section,
1431                                       symbol->value + reloc_entry->addend,
1432                                       NULL, NULL, FALSE);
1433       if (dest != (bfd_vma) -1)
1434         reloc_entry->addend = dest - (symbol->value
1435                                       + symbol->section->output_section->vma
1436                                       + symbol->section->output_offset);
1437     }
1438   else
1439     {
1440       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1441
1442       if (symbol->section->owner != abfd
1443           && symbol->section->owner != NULL
1444           && abiversion (symbol->section->owner) >= 2)
1445         {
1446           unsigned int i;
1447
1448           for (i = 0; i < symbol->section->owner->symcount; ++i)
1449             {
1450               asymbol *symdef = symbol->section->owner->outsymbols[i];
1451
1452               if (strcmp (symdef->name, symbol->name) == 0)
1453                 {
1454                   elfsym = (elf_symbol_type *) symdef;
1455                   break;
1456                 }
1457             }
1458         }
1459       reloc_entry->addend
1460         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1461     }
1462   return bfd_reloc_continue;
1463 }
1464
1465 static bfd_reloc_status_type
1466 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467                          void *data, asection *input_section,
1468                          bfd *output_bfd, char **error_message)
1469 {
1470   long insn;
1471   enum elf_ppc64_reloc_type r_type;
1472   bfd_size_type octets;
1473   /* Assume 'at' branch hints.  */
1474   bfd_boolean is_isa_v2 = TRUE;
1475
1476   /* If this is a relocatable link (output_bfd test tells us), just
1477      call the generic function.  Any adjustment will be done at final
1478      link time.  */
1479   if (output_bfd != NULL)
1480     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1481                                   input_section, output_bfd, error_message);
1482
1483   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1484   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1485   insn &= ~(0x01 << 21);
1486   r_type = reloc_entry->howto->type;
1487   if (r_type == R_PPC64_ADDR14_BRTAKEN
1488       || r_type == R_PPC64_REL14_BRTAKEN)
1489     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1490
1491   if (is_isa_v2)
1492     {
1493       /* Set 'a' bit.  This is 0b00010 in BO field for branch
1494          on CR(BI) insns (BO == 001at or 011at), and 0b01000
1495          for branch on CTR insns (BO == 1a00t or 1a01t).  */
1496       if ((insn & (0x14 << 21)) == (0x04 << 21))
1497         insn |= 0x02 << 21;
1498       else if ((insn & (0x14 << 21)) == (0x10 << 21))
1499         insn |= 0x08 << 21;
1500       else
1501         goto out;
1502     }
1503   else
1504     {
1505       bfd_vma target = 0;
1506       bfd_vma from;
1507
1508       if (!bfd_is_com_section (symbol->section))
1509         target = symbol->value;
1510       target += symbol->section->output_section->vma;
1511       target += symbol->section->output_offset;
1512       target += reloc_entry->addend;
1513
1514       from = (reloc_entry->address
1515               + input_section->output_offset
1516               + input_section->output_section->vma);
1517
1518       /* Invert 'y' bit if not the default.  */
1519       if ((bfd_signed_vma) (target - from) < 0)
1520         insn ^= 0x01 << 21;
1521     }
1522   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1523  out:
1524   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1525                                  input_section, output_bfd, error_message);
1526 }
1527
1528 static bfd_reloc_status_type
1529 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1530                          void *data, asection *input_section,
1531                          bfd *output_bfd, char **error_message)
1532 {
1533   /* If this is a relocatable link (output_bfd test tells us), just
1534      call the generic function.  Any adjustment will be done at final
1535      link time.  */
1536   if (output_bfd != NULL)
1537     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1538                                   input_section, output_bfd, error_message);
1539
1540   /* Subtract the symbol section base address.  */
1541   reloc_entry->addend -= symbol->section->output_section->vma;
1542   return bfd_reloc_continue;
1543 }
1544
1545 static bfd_reloc_status_type
1546 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1547                             void *data, asection *input_section,
1548                             bfd *output_bfd, char **error_message)
1549 {
1550   /* If this is a relocatable link (output_bfd test tells us), just
1551      call the generic function.  Any adjustment will be done at final
1552      link time.  */
1553   if (output_bfd != NULL)
1554     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1555                                   input_section, output_bfd, error_message);
1556
1557   /* Subtract the symbol section base address.  */
1558   reloc_entry->addend -= symbol->section->output_section->vma;
1559
1560   /* Adjust the addend for sign extension of the low 16 bits.  */
1561   reloc_entry->addend += 0x8000;
1562   return bfd_reloc_continue;
1563 }
1564
1565 static bfd_reloc_status_type
1566 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567                      void *data, asection *input_section,
1568                      bfd *output_bfd, char **error_message)
1569 {
1570   bfd_vma TOCstart;
1571
1572   /* If this is a relocatable link (output_bfd test tells us), just
1573      call the generic function.  Any adjustment will be done at final
1574      link time.  */
1575   if (output_bfd != NULL)
1576     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1577                                   input_section, output_bfd, error_message);
1578
1579   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1580   if (TOCstart == 0)
1581     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1582
1583   /* Subtract the TOC base address.  */
1584   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1585   return bfd_reloc_continue;
1586 }
1587
1588 static bfd_reloc_status_type
1589 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590                         void *data, asection *input_section,
1591                         bfd *output_bfd, char **error_message)
1592 {
1593   bfd_vma TOCstart;
1594
1595   /* If this is a relocatable link (output_bfd test tells us), just
1596      call the generic function.  Any adjustment will be done at final
1597      link time.  */
1598   if (output_bfd != NULL)
1599     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1600                                   input_section, output_bfd, error_message);
1601
1602   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1603   if (TOCstart == 0)
1604     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1605
1606   /* Subtract the TOC base address.  */
1607   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1608
1609   /* Adjust the addend for sign extension of the low 16 bits.  */
1610   reloc_entry->addend += 0x8000;
1611   return bfd_reloc_continue;
1612 }
1613
1614 static bfd_reloc_status_type
1615 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1616                        void *data, asection *input_section,
1617                        bfd *output_bfd, char **error_message)
1618 {
1619   bfd_vma TOCstart;
1620   bfd_size_type octets;
1621
1622   /* If this is a relocatable link (output_bfd test tells us), just
1623      call the generic function.  Any adjustment will be done at final
1624      link time.  */
1625   if (output_bfd != NULL)
1626     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627                                   input_section, output_bfd, error_message);
1628
1629   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630   if (TOCstart == 0)
1631     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1632
1633   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1634   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1635   return bfd_reloc_ok;
1636 }
1637
1638 static bfd_reloc_status_type
1639 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640                         void *data, asection *input_section,
1641                         bfd *output_bfd, char **error_message)
1642 {
1643   uint64_t insn;
1644   bfd_vma targ;
1645
1646   if (output_bfd != NULL)
1647     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1648                                   input_section, output_bfd, error_message);
1649
1650   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1651   insn <<= 32;
1652   insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1653
1654   targ = (symbol->section->output_section->vma
1655           + symbol->section->output_offset
1656           + reloc_entry->addend);
1657   if (!bfd_is_com_section (symbol->section))
1658     targ += symbol->value;
1659   if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1660     targ += 1ULL << 33;
1661   if (reloc_entry->howto->pc_relative)
1662     {
1663       bfd_vma from = (reloc_entry->address
1664                       + input_section->output_offset
1665                       + input_section->output_section->vma);
1666       targ -=from;
1667     }
1668   targ >>= reloc_entry->howto->rightshift;
1669   insn &= ~reloc_entry->howto->dst_mask;
1670   insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1671   bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1672   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1673   if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1674       && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1675           >= 1ULL << reloc_entry->howto->bitsize))
1676     return bfd_reloc_overflow;
1677   return bfd_reloc_ok;
1678 }
1679
1680 static bfd_reloc_status_type
1681 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1682                            void *data, asection *input_section,
1683                            bfd *output_bfd, char **error_message)
1684 {
1685   /* If this is a relocatable link (output_bfd test tells us), just
1686      call the generic function.  Any adjustment will be done at final
1687      link time.  */
1688   if (output_bfd != NULL)
1689     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1690                                   input_section, output_bfd, error_message);
1691
1692   if (error_message != NULL)
1693     {
1694       static char buf[60];
1695       sprintf (buf, "generic linker can't handle %s",
1696                reloc_entry->howto->name);
1697       *error_message = buf;
1698     }
1699   return bfd_reloc_dangerous;
1700 }
1701
1702 /* Track GOT entries needed for a given symbol.  We might need more
1703    than one got entry per symbol.  */
1704 struct got_entry
1705 {
1706   struct got_entry *next;
1707
1708   /* The symbol addend that we'll be placing in the GOT.  */
1709   bfd_vma addend;
1710
1711   /* Unlike other ELF targets, we use separate GOT entries for the same
1712      symbol referenced from different input files.  This is to support
1713      automatic multiple TOC/GOT sections, where the TOC base can vary
1714      from one input file to another.  After partitioning into TOC groups
1715      we merge entries within the group.
1716
1717      Point to the BFD owning this GOT entry.  */
1718   bfd *owner;
1719
1720   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1721      TLS_TPREL or TLS_DTPREL for tls entries.  */
1722   unsigned char tls_type;
1723
1724   /* Non-zero if got.ent points to real entry.  */
1725   unsigned char is_indirect;
1726
1727   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1728   union
1729   {
1730     bfd_signed_vma refcount;
1731     bfd_vma offset;
1732     struct got_entry *ent;
1733   } got;
1734 };
1735
1736 /* The same for PLT.  */
1737 struct plt_entry
1738 {
1739   struct plt_entry *next;
1740
1741   bfd_vma addend;
1742
1743   union
1744   {
1745     bfd_signed_vma refcount;
1746     bfd_vma offset;
1747   } plt;
1748 };
1749
1750 struct ppc64_elf_obj_tdata
1751 {
1752   struct elf_obj_tdata elf;
1753
1754   /* Shortcuts to dynamic linker sections.  */
1755   asection *got;
1756   asection *relgot;
1757
1758   /* Used during garbage collection.  We attach global symbols defined
1759      on removed .opd entries to this section so that the sym is removed.  */
1760   asection *deleted_section;
1761
1762   /* TLS local dynamic got entry handling.  Support for multiple GOT
1763      sections means we potentially need one of these for each input bfd.  */
1764   struct got_entry tlsld_got;
1765
1766   union
1767   {
1768     /* A copy of relocs before they are modified for --emit-relocs.  */
1769     Elf_Internal_Rela *relocs;
1770
1771     /* Section contents.  */
1772     bfd_byte *contents;
1773   } opd;
1774
1775   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1776      the reloc to be in the range -32768 to 32767.  */
1777   unsigned int has_small_toc_reloc : 1;
1778
1779   /* Set if toc/got ha relocs detected not using r2, or lo reloc
1780      instruction not one we handle.  */
1781   unsigned int unexpected_toc_insn : 1;
1782
1783   /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1784      this file.  */
1785   unsigned int has_optrel : 1;
1786 };
1787
1788 #define ppc64_elf_tdata(bfd) \
1789   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1790
1791 #define ppc64_tlsld_got(bfd) \
1792   (&ppc64_elf_tdata (bfd)->tlsld_got)
1793
1794 #define is_ppc64_elf(bfd) \
1795   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1796    && elf_object_id (bfd) == PPC64_ELF_DATA)
1797
1798 /* Override the generic function because we store some extras.  */
1799
1800 static bfd_boolean
1801 ppc64_elf_mkobject (bfd *abfd)
1802 {
1803   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1804                                   PPC64_ELF_DATA);
1805 }
1806
1807 /* Fix bad default arch selected for a 64 bit input bfd when the
1808    default is 32 bit.  Also select arch based on apuinfo.  */
1809
1810 static bfd_boolean
1811 ppc64_elf_object_p (bfd *abfd)
1812 {
1813   if (!abfd->arch_info->the_default)
1814     return TRUE;
1815
1816   if (abfd->arch_info->bits_per_word == 32)
1817     {
1818       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1819
1820       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1821         {
1822           /* Relies on arch after 32 bit default being 64 bit default.  */
1823           abfd->arch_info = abfd->arch_info->next;
1824           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1825         }
1826     }
1827   return _bfd_elf_ppc_set_arch (abfd);
1828 }
1829
1830 /* Support for core dump NOTE sections.  */
1831
1832 static bfd_boolean
1833 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1834 {
1835   size_t offset, size;
1836
1837   if (note->descsz != 504)
1838     return FALSE;
1839
1840   /* pr_cursig */
1841   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1842
1843   /* pr_pid */
1844   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1845
1846   /* pr_reg */
1847   offset = 112;
1848   size = 384;
1849
1850   /* Make a ".reg/999" section.  */
1851   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1852                                           size, note->descpos + offset);
1853 }
1854
1855 static bfd_boolean
1856 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1857 {
1858   if (note->descsz != 136)
1859     return FALSE;
1860
1861   elf_tdata (abfd)->core->pid
1862     = bfd_get_32 (abfd, note->descdata + 24);
1863   elf_tdata (abfd)->core->program
1864     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1865   elf_tdata (abfd)->core->command
1866     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1867
1868   return TRUE;
1869 }
1870
1871 static char *
1872 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1873                            ...)
1874 {
1875   switch (note_type)
1876     {
1877     default:
1878       return NULL;
1879
1880     case NT_PRPSINFO:
1881       {
1882         char data[136] ATTRIBUTE_NONSTRING;
1883         va_list ap;
1884
1885         va_start (ap, note_type);
1886         memset (data, 0, sizeof (data));
1887         strncpy (data + 40, va_arg (ap, const char *), 16);
1888 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1889         DIAGNOSTIC_PUSH;
1890         /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1891            -Wstringop-truncation:
1892            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1893          */
1894         DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1895 #endif
1896         strncpy (data + 56, va_arg (ap, const char *), 80);
1897 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1898         DIAGNOSTIC_POP;
1899 #endif
1900         va_end (ap);
1901         return elfcore_write_note (abfd, buf, bufsiz,
1902                                    "CORE", note_type, data, sizeof (data));
1903       }
1904
1905     case NT_PRSTATUS:
1906       {
1907         char data[504];
1908         va_list ap;
1909         long pid;
1910         int cursig;
1911         const void *greg;
1912
1913         va_start (ap, note_type);
1914         memset (data, 0, 112);
1915         pid = va_arg (ap, long);
1916         bfd_put_32 (abfd, pid, data + 32);
1917         cursig = va_arg (ap, int);
1918         bfd_put_16 (abfd, cursig, data + 12);
1919         greg = va_arg (ap, const void *);
1920         memcpy (data + 112, greg, 384);
1921         memset (data + 496, 0, 8);
1922         va_end (ap);
1923         return elfcore_write_note (abfd, buf, bufsiz,
1924                                    "CORE", note_type, data, sizeof (data));
1925       }
1926     }
1927 }
1928
1929 /* Add extra PPC sections.  */
1930
1931 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1932 {
1933   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1934   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1935   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1936   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1937   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1938   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1939   { NULL,                     0,  0, 0,            0 }
1940 };
1941
1942 enum _ppc64_sec_type {
1943   sec_normal = 0,
1944   sec_opd = 1,
1945   sec_toc = 2
1946 };
1947
1948 struct _ppc64_elf_section_data
1949 {
1950   struct bfd_elf_section_data elf;
1951
1952   union
1953   {
1954     /* An array with one entry for each opd function descriptor,
1955        and some spares since opd entries may be either 16 or 24 bytes.  */
1956 #define OPD_NDX(OFF) ((OFF) >> 4)
1957     struct _opd_sec_data
1958     {
1959       /* Points to the function code section for local opd entries.  */
1960       asection **func_sec;
1961
1962       /* After editing .opd, adjust references to opd local syms.  */
1963       long *adjust;
1964     } opd;
1965
1966     /* An array for toc sections, indexed by offset/8.  */
1967     struct _toc_sec_data
1968     {
1969       /* Specifies the relocation symbol index used at a given toc offset.  */
1970       unsigned *symndx;
1971
1972       /* And the relocation addend.  */
1973       bfd_vma *add;
1974     } toc;
1975   } u;
1976
1977   enum _ppc64_sec_type sec_type:2;
1978
1979   /* Flag set when small branches are detected.  Used to
1980      select suitable defaults for the stub group size.  */
1981   unsigned int has_14bit_branch:1;
1982
1983   /* Flag set when PLTCALL relocs are detected.  */
1984   unsigned int has_pltcall:1;
1985
1986   /* Flag set when section has PLT/GOT/TOC relocations that can be
1987      optimised.  */
1988   unsigned int has_optrel:1;
1989 };
1990
1991 #define ppc64_elf_section_data(sec) \
1992   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1993
1994 static bfd_boolean
1995 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1996 {
1997   if (!sec->used_by_bfd)
1998     {
1999       struct _ppc64_elf_section_data *sdata;
2000       bfd_size_type amt = sizeof (*sdata);
2001
2002       sdata = bfd_zalloc (abfd, amt);
2003       if (sdata == NULL)
2004         return FALSE;
2005       sec->used_by_bfd = sdata;
2006     }
2007
2008   return _bfd_elf_new_section_hook (abfd, sec);
2009 }
2010
2011 static struct _opd_sec_data *
2012 get_opd_info (asection * sec)
2013 {
2014   if (sec != NULL
2015       && ppc64_elf_section_data (sec) != NULL
2016       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2017     return &ppc64_elf_section_data (sec)->u.opd;
2018   return NULL;
2019 }
2020 \f
2021 /* Parameters for the qsort hook.  */
2022 static bfd_boolean synthetic_relocatable;
2023 static asection *synthetic_opd;
2024
2025 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2026
2027 static int
2028 compare_symbols (const void *ap, const void *bp)
2029 {
2030   const asymbol *a = *(const asymbol **) ap;
2031   const asymbol *b = *(const asymbol **) bp;
2032
2033   /* Section symbols first.  */
2034   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2035     return -1;
2036   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2037     return 1;
2038
2039   /* then .opd symbols.  */
2040   if (synthetic_opd != NULL)
2041     {
2042       if (strcmp (a->section->name, ".opd") == 0
2043           && strcmp (b->section->name, ".opd") != 0)
2044         return -1;
2045       if (strcmp (a->section->name, ".opd") != 0
2046           && strcmp (b->section->name, ".opd") == 0)
2047         return 1;
2048     }
2049
2050   /* then other code symbols.  */
2051   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2052        == (SEC_CODE | SEC_ALLOC))
2053       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2054           != (SEC_CODE | SEC_ALLOC)))
2055     return -1;
2056
2057   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2058        != (SEC_CODE | SEC_ALLOC))
2059       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2060           == (SEC_CODE | SEC_ALLOC)))
2061     return 1;
2062
2063   if (synthetic_relocatable)
2064     {
2065       if (a->section->id < b->section->id)
2066         return -1;
2067
2068       if (a->section->id > b->section->id)
2069         return 1;
2070     }
2071
2072   if (a->value + a->section->vma < b->value + b->section->vma)
2073     return -1;
2074
2075   if (a->value + a->section->vma > b->value + b->section->vma)
2076     return 1;
2077
2078   /* For syms with the same value, prefer strong dynamic global function
2079      syms over other syms.  */
2080   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2081     return -1;
2082
2083   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2084     return 1;
2085
2086   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2087     return -1;
2088
2089   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2090     return 1;
2091
2092   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2093     return -1;
2094
2095   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2096     return 1;
2097
2098   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2099     return -1;
2100
2101   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2102     return 1;
2103
2104   return a > b;
2105 }
2106
2107 /* Search SYMS for a symbol of the given VALUE.  */
2108
2109 static asymbol *
2110 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
2111 {
2112   long mid;
2113
2114   if (id == (unsigned) -1)
2115     {
2116       while (lo < hi)
2117         {
2118           mid = (lo + hi) >> 1;
2119           if (syms[mid]->value + syms[mid]->section->vma < value)
2120             lo = mid + 1;
2121           else if (syms[mid]->value + syms[mid]->section->vma > value)
2122             hi = mid;
2123           else
2124             return syms[mid];
2125         }
2126     }
2127   else
2128     {
2129       while (lo < hi)
2130         {
2131           mid = (lo + hi) >> 1;
2132           if (syms[mid]->section->id < id)
2133             lo = mid + 1;
2134           else if (syms[mid]->section->id > id)
2135             hi = mid;
2136           else if (syms[mid]->value < value)
2137             lo = mid + 1;
2138           else if (syms[mid]->value > value)
2139             hi = mid;
2140           else
2141             return syms[mid];
2142         }
2143     }
2144   return NULL;
2145 }
2146
2147 static bfd_boolean
2148 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2149 {
2150   bfd_vma vma = *(bfd_vma *) ptr;
2151   return ((section->flags & SEC_ALLOC) != 0
2152           && section->vma <= vma
2153           && vma < section->vma + section->size);
2154 }
2155
2156 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2157    entry syms.  Also generate @plt symbols for the glink branch table.
2158    Returns count of synthetic symbols in RET or -1 on error.  */
2159
2160 static long
2161 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2162                                 long static_count, asymbol **static_syms,
2163                                 long dyn_count, asymbol **dyn_syms,
2164                                 asymbol **ret)
2165 {
2166   asymbol *s;
2167   size_t i, j, count;
2168   char *names;
2169   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2170   asection *opd = NULL;
2171   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2172   asymbol **syms;
2173   int abi = abiversion (abfd);
2174
2175   *ret = NULL;
2176
2177   if (abi < 2)
2178     {
2179       opd = bfd_get_section_by_name (abfd, ".opd");
2180       if (opd == NULL && abi == 1)
2181         return 0;
2182     }
2183
2184   syms = NULL;
2185   codesecsym = 0;
2186   codesecsymend = 0;
2187   secsymend = 0;
2188   opdsymend = 0;
2189   symcount = 0;
2190   if (opd != NULL)
2191     {
2192       symcount = static_count;
2193       if (!relocatable)
2194         symcount += dyn_count;
2195       if (symcount == 0)
2196         return 0;
2197
2198       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2199       if (syms == NULL)
2200         return -1;
2201
2202       if (!relocatable && static_count != 0 && dyn_count != 0)
2203         {
2204           /* Use both symbol tables.  */
2205           memcpy (syms, static_syms, static_count * sizeof (*syms));
2206           memcpy (syms + static_count, dyn_syms,
2207                   (dyn_count + 1) * sizeof (*syms));
2208         }
2209       else if (!relocatable && static_count == 0)
2210         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2211       else
2212         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2213
2214       /* Trim uninteresting symbols.  Interesting symbols are section,
2215          function, and notype symbols.  */
2216       for (i = 0, j = 0; i < symcount; ++i)
2217         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2218                                | BSF_RELC | BSF_SRELC)) == 0)
2219           syms[j++] = syms[i];
2220       symcount = j;
2221
2222       synthetic_relocatable = relocatable;
2223       synthetic_opd = opd;
2224       qsort (syms, symcount, sizeof (*syms), compare_symbols);
2225
2226       if (!relocatable && symcount > 1)
2227         {
2228           /* Trim duplicate syms, since we may have merged the normal
2229              and dynamic symbols.  Actually, we only care about syms
2230              that have different values, so trim any with the same
2231              value.  Don't consider ifunc and ifunc resolver symbols
2232              duplicates however, because GDB wants to know whether a
2233              text symbol is an ifunc resolver.  */
2234           for (i = 1, j = 1; i < symcount; ++i)
2235             {
2236               const asymbol *s0 = syms[i - 1];
2237               const asymbol *s1 = syms[i];
2238
2239               if ((s0->value + s0->section->vma
2240                    != s1->value + s1->section->vma)
2241                   || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2242                       != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2243                 syms[j++] = syms[i];
2244             }
2245           symcount = j;
2246         }
2247
2248       i = 0;
2249       /* Note that here and in compare_symbols we can't compare opd and
2250          sym->section directly.  With separate debug info files, the
2251          symbols will be extracted from the debug file while abfd passed
2252          to this function is the real binary.  */
2253       if (strcmp (syms[i]->section->name, ".opd") == 0)
2254         ++i;
2255       codesecsym = i;
2256
2257       for (; i < symcount; ++i)
2258         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2259                                          | SEC_THREAD_LOCAL))
2260              != (SEC_CODE | SEC_ALLOC))
2261             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2262           break;
2263       codesecsymend = i;
2264
2265       for (; i < symcount; ++i)
2266         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2267           break;
2268       secsymend = i;
2269
2270       for (; i < symcount; ++i)
2271         if (strcmp (syms[i]->section->name, ".opd") != 0)
2272           break;
2273       opdsymend = i;
2274
2275       for (; i < symcount; ++i)
2276         if (((syms[i]->section->flags
2277               & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2278             != (SEC_CODE | SEC_ALLOC))
2279           break;
2280       symcount = i;
2281     }
2282   count = 0;
2283
2284   if (relocatable)
2285     {
2286       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2287       arelent *r;
2288       size_t size;
2289       size_t relcount;
2290
2291       if (opdsymend == secsymend)
2292         goto done;
2293
2294       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2295       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2296       if (relcount == 0)
2297         goto done;
2298
2299       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2300         {
2301           count = -1;
2302           goto done;
2303         }
2304
2305       size = 0;
2306       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2307         {
2308           asymbol *sym;
2309
2310           while (r < opd->relocation + relcount
2311                  && r->address < syms[i]->value + opd->vma)
2312             ++r;
2313
2314           if (r == opd->relocation + relcount)
2315             break;
2316
2317           if (r->address != syms[i]->value + opd->vma)
2318             continue;
2319
2320           if (r->howto->type != R_PPC64_ADDR64)
2321             continue;
2322
2323           sym = *r->sym_ptr_ptr;
2324           if (!sym_exists_at (syms, opdsymend, symcount,
2325                               sym->section->id, sym->value + r->addend))
2326             {
2327               ++count;
2328               size += sizeof (asymbol);
2329               size += strlen (syms[i]->name) + 2;
2330             }
2331         }
2332
2333       if (size == 0)
2334         goto done;
2335       s = *ret = bfd_malloc (size);
2336       if (s == NULL)
2337         {
2338           count = -1;
2339           goto done;
2340         }
2341
2342       names = (char *) (s + count);
2343
2344       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2345         {
2346           asymbol *sym;
2347
2348           while (r < opd->relocation + relcount
2349                  && r->address < syms[i]->value + opd->vma)
2350             ++r;
2351
2352           if (r == opd->relocation + relcount)
2353             break;
2354
2355           if (r->address != syms[i]->value + opd->vma)
2356             continue;
2357
2358           if (r->howto->type != R_PPC64_ADDR64)
2359             continue;
2360
2361           sym = *r->sym_ptr_ptr;
2362           if (!sym_exists_at (syms, opdsymend, symcount,
2363                               sym->section->id, sym->value + r->addend))
2364             {
2365               size_t len;
2366
2367               *s = *syms[i];
2368               s->flags |= BSF_SYNTHETIC;
2369               s->section = sym->section;
2370               s->value = sym->value + r->addend;
2371               s->name = names;
2372               *names++ = '.';
2373               len = strlen (syms[i]->name);
2374               memcpy (names, syms[i]->name, len + 1);
2375               names += len + 1;
2376               /* Have udata.p point back to the original symbol this
2377                  synthetic symbol was derived from.  */
2378               s->udata.p = syms[i];
2379               s++;
2380             }
2381         }
2382     }
2383   else
2384     {
2385       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2386       bfd_byte *contents = NULL;
2387       size_t size;
2388       size_t plt_count = 0;
2389       bfd_vma glink_vma = 0, resolv_vma = 0;
2390       asection *dynamic, *glink = NULL, *relplt = NULL;
2391       arelent *p;
2392
2393       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2394         {
2395         free_contents_and_exit_err:
2396           count = -1;
2397         free_contents_and_exit:
2398           if (contents)
2399             free (contents);
2400           goto done;
2401         }
2402
2403       size = 0;
2404       for (i = secsymend; i < opdsymend; ++i)
2405         {
2406           bfd_vma ent;
2407
2408           /* Ignore bogus symbols.  */
2409           if (syms[i]->value > opd->size - 8)
2410             continue;
2411
2412           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2413           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2414             {
2415               ++count;
2416               size += sizeof (asymbol);
2417               size += strlen (syms[i]->name) + 2;
2418             }
2419         }
2420
2421       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2422       if (dyn_count != 0
2423           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2424         {
2425           bfd_byte *dynbuf, *extdyn, *extdynend;
2426           size_t extdynsize;
2427           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2428
2429           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2430             goto free_contents_and_exit_err;
2431
2432           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2433           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2434
2435           extdyn = dynbuf;
2436           extdynend = extdyn + dynamic->size;
2437           for (; extdyn < extdynend; extdyn += extdynsize)
2438             {
2439               Elf_Internal_Dyn dyn;
2440               (*swap_dyn_in) (abfd, extdyn, &dyn);
2441
2442               if (dyn.d_tag == DT_NULL)
2443                 break;
2444
2445               if (dyn.d_tag == DT_PPC64_GLINK)
2446                 {
2447                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2448                      See comment in ppc64_elf_finish_dynamic_sections. */
2449                   glink_vma = dyn.d_un.d_val + 8 * 4;
2450                   /* The .glink section usually does not survive the final
2451                      link; search for the section (usually .text) where the
2452                      glink stubs now reside.  */
2453                   glink = bfd_sections_find_if (abfd, section_covers_vma,
2454                                                 &glink_vma);
2455                   break;
2456                 }
2457             }
2458
2459           free (dynbuf);
2460         }
2461
2462       if (glink != NULL)
2463         {
2464           /* Determine __glink trampoline by reading the relative branch
2465              from the first glink stub.  */
2466           bfd_byte buf[4];
2467           unsigned int off = 0;
2468
2469           while (bfd_get_section_contents (abfd, glink, buf,
2470                                            glink_vma + off - glink->vma, 4))
2471             {
2472               unsigned int insn = bfd_get_32 (abfd, buf);
2473               insn ^= B_DOT;
2474               if ((insn & ~0x3fffffc) == 0)
2475                 {
2476                   resolv_vma
2477                     = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2478                   break;
2479                 }
2480               off += 4;
2481               if (off > 4)
2482                 break;
2483             }
2484
2485           if (resolv_vma)
2486             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2487
2488           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2489           if (relplt != NULL)
2490             {
2491               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2492               if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2493                 goto free_contents_and_exit_err;
2494
2495               plt_count = relplt->size / sizeof (Elf64_External_Rela);
2496               size += plt_count * sizeof (asymbol);
2497
2498               p = relplt->relocation;
2499               for (i = 0; i < plt_count; i++, p++)
2500                 {
2501                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2502                   if (p->addend != 0)
2503                     size += sizeof ("+0x") - 1 + 16;
2504                 }
2505             }
2506         }
2507
2508       if (size == 0)
2509         goto free_contents_and_exit;
2510       s = *ret = bfd_malloc (size);
2511       if (s == NULL)
2512         goto free_contents_and_exit_err;
2513
2514       names = (char *) (s + count + plt_count + (resolv_vma != 0));
2515
2516       for (i = secsymend; i < opdsymend; ++i)
2517         {
2518           bfd_vma ent;
2519
2520           if (syms[i]->value > opd->size - 8)
2521             continue;
2522
2523           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2524           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2525             {
2526               size_t lo, hi;
2527               size_t len;
2528               asection *sec = abfd->sections;
2529
2530               *s = *syms[i];
2531               lo = codesecsym;
2532               hi = codesecsymend;
2533               while (lo < hi)
2534                 {
2535                   size_t mid = (lo + hi) >> 1;
2536                   if (syms[mid]->section->vma < ent)
2537                     lo = mid + 1;
2538                   else if (syms[mid]->section->vma > ent)
2539                     hi = mid;
2540                   else
2541                     {
2542                       sec = syms[mid]->section;
2543                       break;
2544                     }
2545                 }
2546
2547               if (lo >= hi && lo > codesecsym)
2548                 sec = syms[lo - 1]->section;
2549
2550               for (; sec != NULL; sec = sec->next)
2551                 {
2552                   if (sec->vma > ent)
2553                     break;
2554                   /* SEC_LOAD may not be set if SEC is from a separate debug
2555                      info file.  */
2556                   if ((sec->flags & SEC_ALLOC) == 0)
2557                     break;
2558                   if ((sec->flags & SEC_CODE) != 0)
2559                     s->section = sec;
2560                 }
2561               s->flags |= BSF_SYNTHETIC;
2562               s->value = ent - s->section->vma;
2563               s->name = names;
2564               *names++ = '.';
2565               len = strlen (syms[i]->name);
2566               memcpy (names, syms[i]->name, len + 1);
2567               names += len + 1;
2568               /* Have udata.p point back to the original symbol this
2569                  synthetic symbol was derived from.  */
2570               s->udata.p = syms[i];
2571               s++;
2572             }
2573         }
2574       free (contents);
2575
2576       if (glink != NULL && relplt != NULL)
2577         {
2578           if (resolv_vma)
2579             {
2580               /* Add a symbol for the main glink trampoline.  */
2581               memset (s, 0, sizeof *s);
2582               s->the_bfd = abfd;
2583               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2584               s->section = glink;
2585               s->value = resolv_vma - glink->vma;
2586               s->name = names;
2587               memcpy (names, "__glink_PLTresolve",
2588                       sizeof ("__glink_PLTresolve"));
2589               names += sizeof ("__glink_PLTresolve");
2590               s++;
2591               count++;
2592             }
2593
2594           /* FIXME: It would be very much nicer to put sym@plt on the
2595              stub rather than on the glink branch table entry.  The
2596              objdump disassembler would then use a sensible symbol
2597              name on plt calls.  The difficulty in doing so is
2598              a) finding the stubs, and,
2599              b) matching stubs against plt entries, and,
2600              c) there can be multiple stubs for a given plt entry.
2601
2602              Solving (a) could be done by code scanning, but older
2603              ppc64 binaries used different stubs to current code.
2604              (b) is the tricky one since you need to known the toc
2605              pointer for at least one function that uses a pic stub to
2606              be able to calculate the plt address referenced.
2607              (c) means gdb would need to set multiple breakpoints (or
2608              find the glink branch itself) when setting breakpoints
2609              for pending shared library loads.  */
2610           p = relplt->relocation;
2611           for (i = 0; i < plt_count; i++, p++)
2612             {
2613               size_t len;
2614
2615               *s = **p->sym_ptr_ptr;
2616               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2617                  we are defining a symbol, ensure one of them is set.  */
2618               if ((s->flags & BSF_LOCAL) == 0)
2619                 s->flags |= BSF_GLOBAL;
2620               s->flags |= BSF_SYNTHETIC;
2621               s->section = glink;
2622               s->value = glink_vma - glink->vma;
2623               s->name = names;
2624               s->udata.p = NULL;
2625               len = strlen ((*p->sym_ptr_ptr)->name);
2626               memcpy (names, (*p->sym_ptr_ptr)->name, len);
2627               names += len;
2628               if (p->addend != 0)
2629                 {
2630                   memcpy (names, "+0x", sizeof ("+0x") - 1);
2631                   names += sizeof ("+0x") - 1;
2632                   bfd_sprintf_vma (abfd, names, p->addend);
2633                   names += strlen (names);
2634                 }
2635               memcpy (names, "@plt", sizeof ("@plt"));
2636               names += sizeof ("@plt");
2637               s++;
2638               if (abi < 2)
2639                 {
2640                   glink_vma += 8;
2641                   if (i >= 0x8000)
2642                     glink_vma += 4;
2643                 }
2644               else
2645                 glink_vma += 4;
2646             }
2647           count += plt_count;
2648         }
2649     }
2650
2651  done:
2652   free (syms);
2653   return count;
2654 }
2655 \f
2656 /* The following functions are specific to the ELF linker, while
2657    functions above are used generally.  Those named ppc64_elf_* are
2658    called by the main ELF linker code.  They appear in this file more
2659    or less in the order in which they are called.  eg.
2660    ppc64_elf_check_relocs is called early in the link process,
2661    ppc64_elf_finish_dynamic_sections is one of the last functions
2662    called.
2663
2664    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2665    functions have both a function code symbol and a function descriptor
2666    symbol.  A call to foo in a relocatable object file looks like:
2667
2668    .            .text
2669    .    x:
2670    .            bl      .foo
2671    .            nop
2672
2673    The function definition in another object file might be:
2674
2675    .            .section .opd
2676    .    foo:    .quad   .foo
2677    .            .quad   .TOC.@tocbase
2678    .            .quad   0
2679    .
2680    .            .text
2681    .    .foo:   blr
2682
2683    When the linker resolves the call during a static link, the branch
2684    unsurprisingly just goes to .foo and the .opd information is unused.
2685    If the function definition is in a shared library, things are a little
2686    different:  The call goes via a plt call stub, the opd information gets
2687    copied to the plt, and the linker patches the nop.
2688
2689    .    x:
2690    .            bl      .foo_stub
2691    .            ld      2,40(1)
2692    .
2693    .
2694    .    .foo_stub:
2695    .            std     2,40(1)                 # in practice, the call stub
2696    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
2697    .            addi    11,11,Lfoo@toc@l        # this is the general idea
2698    .            ld      12,0(11)
2699    .            ld      2,8(11)
2700    .            mtctr   12
2701    .            ld      11,16(11)
2702    .            bctr
2703    .
2704    .            .section .plt
2705    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2706
2707    The "reloc ()" notation is supposed to indicate that the linker emits
2708    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2709    copying.
2710
2711    What are the difficulties here?  Well, firstly, the relocations
2712    examined by the linker in check_relocs are against the function code
2713    sym .foo, while the dynamic relocation in the plt is emitted against
2714    the function descriptor symbol, foo.  Somewhere along the line, we need
2715    to carefully copy dynamic link information from one symbol to the other.
2716    Secondly, the generic part of the elf linker will make .foo a dynamic
2717    symbol as is normal for most other backends.  We need foo dynamic
2718    instead, at least for an application final link.  However, when
2719    creating a shared library containing foo, we need to have both symbols
2720    dynamic so that references to .foo are satisfied during the early
2721    stages of linking.  Otherwise the linker might decide to pull in a
2722    definition from some other object, eg. a static library.
2723
2724    Update: As of August 2004, we support a new convention.  Function
2725    calls may use the function descriptor symbol, ie. "bl foo".  This
2726    behaves exactly as "bl .foo".  */
2727
2728 /* Of those relocs that might be copied as dynamic relocs, this
2729    function selects those that must be copied when linking a shared
2730    library or PIE, even when the symbol is local.  */
2731
2732 static int
2733 must_be_dyn_reloc (struct bfd_link_info *info,
2734                    enum elf_ppc64_reloc_type r_type)
2735 {
2736   switch (r_type)
2737     {
2738     default:
2739       /* Only relative relocs can be resolved when the object load
2740          address isn't fixed.  DTPREL64 is excluded because the
2741          dynamic linker needs to differentiate global dynamic from
2742          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2743       return 1;
2744
2745     case R_PPC64_REL32:
2746     case R_PPC64_REL64:
2747     case R_PPC64_REL30:
2748       return 0;
2749
2750     case R_PPC64_TPREL16:
2751     case R_PPC64_TPREL16_LO:
2752     case R_PPC64_TPREL16_HI:
2753     case R_PPC64_TPREL16_HA:
2754     case R_PPC64_TPREL16_DS:
2755     case R_PPC64_TPREL16_LO_DS:
2756     case R_PPC64_TPREL16_HIGH:
2757     case R_PPC64_TPREL16_HIGHA:
2758     case R_PPC64_TPREL16_HIGHER:
2759     case R_PPC64_TPREL16_HIGHERA:
2760     case R_PPC64_TPREL16_HIGHEST:
2761     case R_PPC64_TPREL16_HIGHESTA:
2762     case R_PPC64_TPREL64:
2763     case R_PPC64_TPREL34:
2764       /* These relocations are relative but in a shared library the
2765          linker doesn't know the thread pointer base.  */
2766       return bfd_link_dll (info);
2767     }
2768 }
2769
2770 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2771    copying dynamic variables from a shared lib into an app's dynbss
2772    section, and instead use a dynamic relocation to point into the
2773    shared lib.  With code that gcc generates, it's vital that this be
2774    enabled;  In the PowerPC64 ABI, the address of a function is actually
2775    the address of a function descriptor, which resides in the .opd
2776    section.  gcc uses the descriptor directly rather than going via the
2777    GOT as some other ABI's do, which means that initialized function
2778    pointers must reference the descriptor.  Thus, a function pointer
2779    initialized to the address of a function in a shared library will
2780    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
2781    redefines the function descriptor symbol to point to the copy.  This
2782    presents a problem as a plt entry for that function is also
2783    initialized from the function descriptor symbol and the copy reloc
2784    may not be initialized first.  */
2785 #define ELIMINATE_COPY_RELOCS 1
2786
2787 /* Section name for stubs is the associated section name plus this
2788    string.  */
2789 #define STUB_SUFFIX ".stub"
2790
2791 /* Linker stubs.
2792    ppc_stub_long_branch:
2793    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2794    destination, but a 24 bit branch in a stub section will reach.
2795    .    b       dest
2796
2797    ppc_stub_plt_branch:
2798    Similar to the above, but a 24 bit branch in the stub section won't
2799    reach its destination.
2800    .    addis   %r11,%r2,xxx@toc@ha
2801    .    ld      %r12,xxx@toc@l(%r11)
2802    .    mtctr   %r12
2803    .    bctr
2804
2805    ppc_stub_plt_call:
2806    Used to call a function in a shared library.  If it so happens that
2807    the plt entry referenced crosses a 64k boundary, then an extra
2808    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2809    ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2810    .    addis   %r11,%r2,xxx@toc@ha
2811    .    ld      %r12,xxx+0@toc@l(%r11)
2812    .    mtctr   %r12
2813    .    ld      %r2,xxx+8@toc@l(%r11)
2814    .    ld      %r11,xxx+16@toc@l(%r11)
2815    .    bctr
2816
2817    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2818    code to adjust the value and save r2 to support multiple toc sections.
2819    A ppc_stub_long_branch with an r2 offset looks like:
2820    .    std     %r2,40(%r1)
2821    .    addis   %r2,%r2,off@ha
2822    .    addi    %r2,%r2,off@l
2823    .    b       dest
2824
2825    A ppc_stub_plt_branch with an r2 offset looks like:
2826    .    std     %r2,40(%r1)
2827    .    addis   %r11,%r2,xxx@toc@ha
2828    .    ld      %r12,xxx@toc@l(%r11)
2829    .    addis   %r2,%r2,off@ha
2830    .    addi    %r2,%r2,off@l
2831    .    mtctr   %r12
2832    .    bctr
2833
2834    All of the above stubs are shown as their ELFv1 variants.  ELFv2
2835    variants exist too, simpler for plt calls since a new toc pointer
2836    and static chain are not loaded by the stub.  In addition, ELFv2
2837    has some more complex stubs to handle calls marked with NOTOC
2838    relocs from functions where r2 is not a valid toc pointer.  These
2839    come in two flavours, the ones shown below, and _both variants that
2840    start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2841    one call is from a function where r2 is used as the toc pointer but
2842    needs a toc adjusting stub for small-model multi-toc, and another
2843    call is from a function where r2 is not valid.
2844    ppc_stub_long_branch_notoc:
2845    .    mflr    %r12
2846    .    bcl     20,31,1f
2847    .  1:
2848    .    mflr    %r11
2849    .    mtlr    %r12
2850    .    addis   %r12,%r11,dest-1b@ha
2851    .    addi    %r12,%r12,dest-1b@l
2852    .    b       dest
2853
2854    ppc_stub_plt_branch_notoc:
2855    .    mflr    %r12
2856    .    bcl     20,31,1f
2857    .  1:
2858    .    mflr    %r11
2859    .    mtlr    %r12
2860    .    lis     %r12,xxx-1b@highest
2861    .    ori     %r12,%r12,xxx-1b@higher
2862    .    sldi    %r12,%r12,32
2863    .    oris    %r12,%r12,xxx-1b@high
2864    .    ori     %r12,%r12,xxx-1b@l
2865    .    add     %r12,%r11,%r12
2866    .    mtctr   %r12
2867    .    bctr
2868
2869    ppc_stub_plt_call_notoc:
2870    .    mflr    %r12
2871    .    bcl     20,31,1f
2872    .  1:
2873    .    mflr    %r11
2874    .    mtlr    %r12
2875    .    lis     %r12,xxx-1b@highest
2876    .    ori     %r12,%r12,xxx-1b@higher
2877    .    sldi    %r12,%r12,32
2878    .    oris    %r12,%r12,xxx-1b@high
2879    .    ori     %r12,%r12,xxx-1b@l
2880    .    ldx     %r12,%r11,%r12
2881    .    mtctr   %r12
2882    .    bctr
2883
2884    There are also ELFv1 powerxx variants of these stubs.
2885    ppc_stub_long_branch_notoc:
2886    .    pla     %r12,dest@pcrel
2887    .    b       dest
2888    ppc_stub_plt_branch_notoc:
2889    .    lis     %r11,(dest-1f)@highesta34
2890    .    ori     %r11,%r11,(dest-1f)@highera34
2891    .    sldi    %r11,%r11,34
2892    . 1: pla     %r12,dest@pcrel
2893    .    add     %r12,%r11,%r12
2894    .    mtctr   %r12
2895    .    bctr
2896    ppc_stub_plt_call_notoc:
2897    .    lis     %r11,(xxx-1f)@highesta34
2898    .    ori     %r11,%r11,(xxx-1f)@highera34
2899    .    sldi    %r11,%r11,34
2900    . 1: pla     %r12,xxx@pcrel
2901    .    ldx     %r12,%r11,%r12
2902    .    mtctr   %r12
2903    .    bctr
2904
2905    In cases where the high instructions would add zero, they are
2906    omitted and following instructions modified in some cases.
2907    For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2908    to
2909    .    pld     %r12,xxx@pcrel
2910    .    mtctr   %r12
2911    .    bctr
2912
2913    For a given stub group (a set of sections all using the same toc
2914    pointer value) there will be just one stub type used for any
2915    particular function symbol.  For example, if printf is called from
2916    code with the tocsave optimization (ie. r2 saved in function
2917    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2918    and from other code without the tocsave optimization requiring a
2919    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2920    type will be created.  Calls with the tocsave optimization will
2921    enter this stub after the instruction saving r2.  A similar
2922    situation exists when calls are marked with R_PPC64_REL24_NOTOC
2923    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2924    to call an external function like printf.  If other calls to printf
2925    require a ppc_stub_plt_call linkage stub then a single
2926    ppc_stub_plt_call_notoc linkage stub will be used for both types of
2927    call.  If other calls to printf require a ppc_stub_plt_call_r2save
2928    linkage stub then a single ppc_stub_plt_call_both linkage stub will
2929    be created and calls not requiring r2 to be saved will enter the
2930    stub after the r2 save instruction.  There is an analogous
2931    hierarchy of long branch and plt branch stubs for local call
2932    linkage.  */
2933
2934 enum ppc_stub_type
2935 {
2936   ppc_stub_none,
2937   ppc_stub_long_branch,
2938   ppc_stub_long_branch_r2off,
2939   ppc_stub_long_branch_notoc,
2940   ppc_stub_long_branch_both, /* r2off and notoc variants both needed.  */
2941   ppc_stub_plt_branch,
2942   ppc_stub_plt_branch_r2off,
2943   ppc_stub_plt_branch_notoc,
2944   ppc_stub_plt_branch_both,
2945   ppc_stub_plt_call,
2946   ppc_stub_plt_call_r2save,
2947   ppc_stub_plt_call_notoc,
2948   ppc_stub_plt_call_both,
2949   ppc_stub_global_entry,
2950   ppc_stub_save_res
2951 };
2952
2953 /* Information on stub grouping.  */
2954 struct map_stub
2955 {
2956   /* The stub section.  */
2957   asection *stub_sec;
2958   /* This is the section to which stubs in the group will be attached.  */
2959   asection *link_sec;
2960   /* Next group.  */
2961   struct map_stub *next;
2962   /* Whether to emit a copy of register save/restore functions in this
2963      group.  */
2964   int needs_save_res;
2965   /* Current offset within stubs after the insn restoring lr in a
2966      _notoc or _both stub using bcl for pc-relative addressing, or
2967      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
2968   unsigned int lr_restore;
2969   /* Accumulated size of EH info emitted to describe return address
2970      if stubs modify lr.  Does not include 17 byte FDE header.  */
2971   unsigned int eh_size;
2972   /* Offset in glink_eh_frame to the start of EH info for this group.  */
2973   unsigned int eh_base;
2974 };
2975
2976 struct ppc_stub_hash_entry
2977 {
2978   /* Base hash table entry structure.  */
2979   struct bfd_hash_entry root;
2980
2981   enum ppc_stub_type stub_type;
2982
2983   /* Group information.  */
2984   struct map_stub *group;
2985
2986   /* Offset within stub_sec of the beginning of this stub.  */
2987   bfd_vma stub_offset;
2988
2989   /* Given the symbol's value and its section we can determine its final
2990      value when building the stubs (so the stub knows where to jump.  */
2991   bfd_vma target_value;
2992   asection *target_section;
2993
2994   /* The symbol table entry, if any, that this was derived from.  */
2995   struct ppc_link_hash_entry *h;
2996   struct plt_entry *plt_ent;
2997
2998   /* Symbol type.  */
2999   unsigned char symtype;
3000
3001   /* Symbol st_other.  */
3002   unsigned char other;
3003 };
3004
3005 struct ppc_branch_hash_entry
3006 {
3007   /* Base hash table entry structure.  */
3008   struct bfd_hash_entry root;
3009
3010   /* Offset within branch lookup table.  */
3011   unsigned int offset;
3012
3013   /* Generation marker.  */
3014   unsigned int iter;
3015 };
3016
3017 /* Used to track dynamic relocations for local symbols.  */
3018 struct ppc_dyn_relocs
3019 {
3020   struct ppc_dyn_relocs *next;
3021
3022   /* The input section of the reloc.  */
3023   asection *sec;
3024
3025   /* Total number of relocs copied for the input section.  */
3026   unsigned int count : 31;
3027
3028   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3029   unsigned int ifunc : 1;
3030 };
3031
3032 struct ppc_link_hash_entry
3033 {
3034   struct elf_link_hash_entry elf;
3035
3036   union
3037   {
3038     /* A pointer to the most recently used stub hash entry against this
3039        symbol.  */
3040     struct ppc_stub_hash_entry *stub_cache;
3041
3042     /* A pointer to the next symbol starting with a '.'  */
3043     struct ppc_link_hash_entry *next_dot_sym;
3044   } u;
3045
3046   /* Track dynamic relocs copied for this symbol.  */
3047   struct elf_dyn_relocs *dyn_relocs;
3048
3049   /* Link between function code and descriptor symbols.  */
3050   struct ppc_link_hash_entry *oh;
3051
3052   /* Flag function code and descriptor symbols.  */
3053   unsigned int is_func:1;
3054   unsigned int is_func_descriptor:1;
3055   unsigned int fake:1;
3056
3057   /* Whether global opd/toc sym has been adjusted or not.
3058      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3059      should be set for all globals defined in any opd/toc section.  */
3060   unsigned int adjust_done:1;
3061
3062   /* Set if this is an out-of-line register save/restore function,
3063      with non-standard calling convention.  */
3064   unsigned int save_res:1;
3065
3066   /* Set if a duplicate symbol with non-zero localentry is detected,
3067      even when the duplicate symbol does not provide a definition.  */
3068   unsigned int non_zero_localentry:1;
3069
3070   /* Contexts in which symbol is used in the GOT (or TOC).
3071      Bits are or'd into the mask as the corresponding relocs are
3072      encountered during check_relocs, with TLS_TLS being set when any
3073      of the other TLS bits are set.  tls_optimize clears bits when
3074      optimizing to indicate the corresponding GOT entry type is not
3075      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
3076      set TLS_GDIE when a GD reloc turns into an IE one.
3077      These flags are also kept for local symbols.  */
3078 #define TLS_TLS          1      /* Any TLS reloc.  */
3079 #define TLS_GD           2      /* GD reloc. */
3080 #define TLS_LD           4      /* LD reloc. */
3081 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
3082 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
3083 #define TLS_MARK        32      /* __tls_get_addr call marked. */
3084 #define TLS_GDIE        64      /* GOT TPREL reloc resulting from GD->IE. */
3085 #define TLS_EXPLICIT   256      /* TOC section TLS reloc, not stored. */
3086   unsigned char tls_mask;
3087
3088   /* The above field is also used to mark function symbols.  In which
3089      case TLS_TLS will be 0.  */
3090 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
3091 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
3092 #define NON_GOT        256      /* local symbol plt, not stored.  */
3093 };
3094
3095 /* ppc64 ELF linker hash table.  */
3096
3097 struct ppc_link_hash_table
3098 {
3099   struct elf_link_hash_table elf;
3100
3101   /* The stub hash table.  */
3102   struct bfd_hash_table stub_hash_table;
3103
3104   /* Another hash table for plt_branch stubs.  */
3105   struct bfd_hash_table branch_hash_table;
3106
3107   /* Hash table for function prologue tocsave.  */
3108   htab_t tocsave_htab;
3109
3110   /* Various options and other info passed from the linker.  */
3111   struct ppc64_elf_params *params;
3112
3113   /* The size of sec_info below.  */
3114   unsigned int sec_info_arr_size;
3115
3116   /* Per-section array of extra section info.  Done this way rather
3117      than as part of ppc64_elf_section_data so we have the info for
3118      non-ppc64 sections.  */
3119   struct
3120   {
3121     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3122     bfd_vma toc_off;
3123
3124     union
3125     {
3126       /* The section group that this section belongs to.  */
3127       struct map_stub *group;
3128       /* A temp section list pointer.  */
3129       asection *list;
3130     } u;
3131   } *sec_info;
3132
3133   /* Linked list of groups.  */
3134   struct map_stub *group;
3135
3136   /* Temp used when calculating TOC pointers.  */
3137   bfd_vma toc_curr;
3138   bfd *toc_bfd;
3139   asection *toc_first_sec;
3140
3141   /* Used when adding symbols.  */
3142   struct ppc_link_hash_entry *dot_syms;
3143
3144   /* Shortcuts to get to dynamic linker sections.  */
3145   asection *glink;
3146   asection *global_entry;
3147   asection *sfpr;
3148   asection *pltlocal;
3149   asection *relpltlocal;
3150   asection *brlt;
3151   asection *relbrlt;
3152   asection *glink_eh_frame;
3153
3154   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3155   struct ppc_link_hash_entry *tls_get_addr;
3156   struct ppc_link_hash_entry *tls_get_addr_fd;
3157
3158   /* The size of reliplt used by got entry relocs.  */
3159   bfd_size_type got_reli_size;
3160
3161   /* Statistics.  */
3162   unsigned long stub_count[ppc_stub_global_entry];
3163
3164   /* Number of stubs against global syms.  */
3165   unsigned long stub_globals;
3166
3167   /* Set if we're linking code with function descriptors.  */
3168   unsigned int opd_abi:1;
3169
3170   /* Support for multiple toc sections.  */
3171   unsigned int do_multi_toc:1;
3172   unsigned int multi_toc_needed:1;
3173   unsigned int second_toc_pass:1;
3174   unsigned int do_toc_opt:1;
3175
3176   /* Set if tls optimization is enabled.  */
3177   unsigned int do_tls_opt:1;
3178
3179   /* Set if inline plt calls should be converted to direct calls.  */
3180   unsigned int can_convert_all_inline_plt:1;
3181
3182   /* Set on error.  */
3183   unsigned int stub_error:1;
3184
3185   /* Whether func_desc_adjust needs to be run over symbols.  */
3186   unsigned int need_func_desc_adj:1;
3187
3188   /* Whether there exist local gnu indirect function resolvers,
3189      referenced by dynamic relocations.  */
3190   unsigned int local_ifunc_resolver:1;
3191   unsigned int maybe_local_ifunc_resolver:1;
3192
3193   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
3194   unsigned int has_plt_localentry0:1;
3195
3196   /* Whether calls are made via the PLT from NOTOC functions.  */
3197   unsigned int notoc_plt:1;
3198
3199   /* Whether to use powerxx instructions in linkage stubs.  */
3200   unsigned int powerxx_stubs:1;
3201
3202   /* Incremented every time we size stubs.  */
3203   unsigned int stub_iteration;
3204
3205   /* Small local sym cache.  */
3206   struct sym_cache sym_cache;
3207 };
3208
3209 /* Rename some of the generic section flags to better document how they
3210    are used here.  */
3211
3212 /* Nonzero if this section has TLS related relocations.  */
3213 #define has_tls_reloc sec_flg0
3214
3215 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3216    relocations.  */
3217 #define nomark_tls_get_addr sec_flg1
3218
3219 /* Nonzero if this section has any toc or got relocs.  */
3220 #define has_toc_reloc sec_flg2
3221
3222 /* Nonzero if this section has a call to another section that uses
3223    the toc or got.  */
3224 #define makes_toc_func_call sec_flg3
3225
3226 /* Recursion protection when determining above flag.  */
3227 #define call_check_in_progress sec_flg4
3228 #define call_check_done sec_flg5
3229
3230 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3231
3232 #define ppc_hash_table(p) \
3233   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3234   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3235
3236 #define ppc_stub_hash_lookup(table, string, create, copy) \
3237   ((struct ppc_stub_hash_entry *) \
3238    bfd_hash_lookup ((table), (string), (create), (copy)))
3239
3240 #define ppc_branch_hash_lookup(table, string, create, copy) \
3241   ((struct ppc_branch_hash_entry *) \
3242    bfd_hash_lookup ((table), (string), (create), (copy)))
3243
3244 /* Create an entry in the stub hash table.  */
3245
3246 static struct bfd_hash_entry *
3247 stub_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_stub_hash_entry));
3256       if (entry == NULL)
3257         return entry;
3258     }
3259
3260   /* Call the allocation method of the superclass.  */
3261   entry = bfd_hash_newfunc (entry, table, string);
3262   if (entry != NULL)
3263     {
3264       struct ppc_stub_hash_entry *eh;
3265
3266       /* Initialize the local fields.  */
3267       eh = (struct ppc_stub_hash_entry *) entry;
3268       eh->stub_type = ppc_stub_none;
3269       eh->group = NULL;
3270       eh->stub_offset = 0;
3271       eh->target_value = 0;
3272       eh->target_section = NULL;
3273       eh->h = NULL;
3274       eh->plt_ent = NULL;
3275       eh->other = 0;
3276     }
3277
3278   return entry;
3279 }
3280
3281 /* Create an entry in the branch hash table.  */
3282
3283 static struct bfd_hash_entry *
3284 branch_hash_newfunc (struct bfd_hash_entry *entry,
3285                      struct bfd_hash_table *table,
3286                      const char *string)
3287 {
3288   /* Allocate the structure if it has not already been allocated by a
3289      subclass.  */
3290   if (entry == NULL)
3291     {
3292       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3293       if (entry == NULL)
3294         return entry;
3295     }
3296
3297   /* Call the allocation method of the superclass.  */
3298   entry = bfd_hash_newfunc (entry, table, string);
3299   if (entry != NULL)
3300     {
3301       struct ppc_branch_hash_entry *eh;
3302
3303       /* Initialize the local fields.  */
3304       eh = (struct ppc_branch_hash_entry *) entry;
3305       eh->offset = 0;
3306       eh->iter = 0;
3307     }
3308
3309   return entry;
3310 }
3311
3312 /* Create an entry in a ppc64 ELF linker hash table.  */
3313
3314 static struct bfd_hash_entry *
3315 link_hash_newfunc (struct bfd_hash_entry *entry,
3316                    struct bfd_hash_table *table,
3317                    const char *string)
3318 {
3319   /* Allocate the structure if it has not already been allocated by a
3320      subclass.  */
3321   if (entry == NULL)
3322     {
3323       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3324       if (entry == NULL)
3325         return entry;
3326     }
3327
3328   /* Call the allocation method of the superclass.  */
3329   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3330   if (entry != NULL)
3331     {
3332       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3333
3334       memset (&eh->u.stub_cache, 0,
3335               (sizeof (struct ppc_link_hash_entry)
3336                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3337
3338       /* When making function calls, old ABI code references function entry
3339          points (dot symbols), while new ABI code references the function
3340          descriptor symbol.  We need to make any combination of reference and
3341          definition work together, without breaking archive linking.
3342
3343          For a defined function "foo" and an undefined call to "bar":
3344          An old object defines "foo" and ".foo", references ".bar" (possibly
3345          "bar" too).
3346          A new object defines "foo" and references "bar".
3347
3348          A new object thus has no problem with its undefined symbols being
3349          satisfied by definitions in an old object.  On the other hand, the
3350          old object won't have ".bar" satisfied by a new object.
3351
3352          Keep a list of newly added dot-symbols.  */
3353
3354       if (string[0] == '.')
3355         {
3356           struct ppc_link_hash_table *htab;
3357
3358           htab = (struct ppc_link_hash_table *) table;
3359           eh->u.next_dot_sym = htab->dot_syms;
3360           htab->dot_syms = eh;
3361         }
3362     }
3363
3364   return entry;
3365 }
3366
3367 struct tocsave_entry
3368 {
3369   asection *sec;
3370   bfd_vma offset;
3371 };
3372
3373 static hashval_t
3374 tocsave_htab_hash (const void *p)
3375 {
3376   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3377   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3378 }
3379
3380 static int
3381 tocsave_htab_eq (const void *p1, const void *p2)
3382 {
3383   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3384   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3385   return e1->sec == e2->sec && e1->offset == e2->offset;
3386 }
3387
3388 /* Destroy a ppc64 ELF linker hash table.  */
3389
3390 static void
3391 ppc64_elf_link_hash_table_free (bfd *obfd)
3392 {
3393   struct ppc_link_hash_table *htab;
3394
3395   htab = (struct ppc_link_hash_table *) obfd->link.hash;
3396   if (htab->tocsave_htab)
3397     htab_delete (htab->tocsave_htab);
3398   bfd_hash_table_free (&htab->branch_hash_table);
3399   bfd_hash_table_free (&htab->stub_hash_table);
3400   _bfd_elf_link_hash_table_free (obfd);
3401 }
3402
3403 /* Create a ppc64 ELF linker hash table.  */
3404
3405 static struct bfd_link_hash_table *
3406 ppc64_elf_link_hash_table_create (bfd *abfd)
3407 {
3408   struct ppc_link_hash_table *htab;
3409   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3410
3411   htab = bfd_zmalloc (amt);
3412   if (htab == NULL)
3413     return NULL;
3414
3415   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3416                                       sizeof (struct ppc_link_hash_entry),
3417                                       PPC64_ELF_DATA))
3418     {
3419       free (htab);
3420       return NULL;
3421     }
3422
3423   /* Init the stub hash table too.  */
3424   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3425                             sizeof (struct ppc_stub_hash_entry)))
3426     {
3427       _bfd_elf_link_hash_table_free (abfd);
3428       return NULL;
3429     }
3430
3431   /* And the branch hash table.  */
3432   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3433                             sizeof (struct ppc_branch_hash_entry)))
3434     {
3435       bfd_hash_table_free (&htab->stub_hash_table);
3436       _bfd_elf_link_hash_table_free (abfd);
3437       return NULL;
3438     }
3439
3440   htab->tocsave_htab = htab_try_create (1024,
3441                                         tocsave_htab_hash,
3442                                         tocsave_htab_eq,
3443                                         NULL);
3444   if (htab->tocsave_htab == NULL)
3445     {
3446       ppc64_elf_link_hash_table_free (abfd);
3447       return NULL;
3448     }
3449   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3450
3451   /* Initializing two fields of the union is just cosmetic.  We really
3452      only care about glist, but when compiled on a 32-bit host the
3453      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3454      debugger inspection of these fields look nicer.  */
3455   htab->elf.init_got_refcount.refcount = 0;
3456   htab->elf.init_got_refcount.glist = NULL;
3457   htab->elf.init_plt_refcount.refcount = 0;
3458   htab->elf.init_plt_refcount.glist = NULL;
3459   htab->elf.init_got_offset.offset = 0;
3460   htab->elf.init_got_offset.glist = NULL;
3461   htab->elf.init_plt_offset.offset = 0;
3462   htab->elf.init_plt_offset.glist = NULL;
3463
3464   return &htab->elf.root;
3465 }
3466
3467 /* Create sections for linker generated code.  */
3468
3469 static bfd_boolean
3470 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3471 {
3472   struct ppc_link_hash_table *htab;
3473   flagword flags;
3474
3475   htab = ppc_hash_table (info);
3476
3477   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3478            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3479   if (htab->params->save_restore_funcs)
3480     {
3481       /* Create .sfpr for code to save and restore fp regs.  */
3482       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3483                                                        flags);
3484       if (htab->sfpr == NULL
3485           || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3486         return FALSE;
3487     }
3488
3489   if (bfd_link_relocatable (info))
3490     return TRUE;
3491
3492   /* Create .glink for lazy dynamic linking support.  */
3493   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3494                                                     flags);
3495   if (htab->glink == NULL
3496       || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3497     return FALSE;
3498
3499   /* The part of .glink used by global entry stubs, separate so that
3500      it can be aligned appropriately without affecting htab->glink.  */
3501   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3502                                                            flags);
3503   if (htab->global_entry == NULL
3504       || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3505     return FALSE;
3506
3507   if (!info->no_ld_generated_unwind_info)
3508     {
3509       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3510                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3511       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3512                                                                  ".eh_frame",
3513                                                                  flags);
3514       if (htab->glink_eh_frame == NULL
3515           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3516         return FALSE;
3517     }
3518
3519   flags = SEC_ALLOC | SEC_LINKER_CREATED;
3520   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3521   if (htab->elf.iplt == NULL
3522       || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3523     return FALSE;
3524
3525   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3526            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3527   htab->elf.irelplt
3528     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3529   if (htab->elf.irelplt == NULL
3530       || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3531     return FALSE;
3532
3533   /* Create branch lookup table for plt_branch stubs.  */
3534   flags = (SEC_ALLOC | SEC_LOAD
3535            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3536   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3537                                                    flags);
3538   if (htab->brlt == NULL
3539       || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3540     return FALSE;
3541
3542   /* Local plt entries, put in .branch_lt but a separate section for
3543      convenience.  */
3544   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3545                                                        flags);
3546   if (htab->pltlocal == NULL
3547       || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3548     return FALSE;
3549
3550   if (!bfd_link_pic (info))
3551     return TRUE;
3552
3553   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3554            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3555   htab->relbrlt
3556     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3557   if (htab->relbrlt == NULL
3558       || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3559     return FALSE;
3560
3561   htab->relpltlocal
3562     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3563   if (htab->relpltlocal == NULL
3564       || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3565     return FALSE;
3566
3567   return TRUE;
3568 }
3569
3570 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3571
3572 bfd_boolean
3573 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3574                          struct ppc64_elf_params *params)
3575 {
3576   struct ppc_link_hash_table *htab;
3577
3578   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3579
3580 /* Always hook our dynamic sections into the first bfd, which is the
3581    linker created stub bfd.  This ensures that the GOT header is at
3582    the start of the output TOC section.  */
3583   htab = ppc_hash_table (info);
3584   htab->elf.dynobj = params->stub_bfd;
3585   htab->params = params;
3586
3587   return create_linkage_sections (htab->elf.dynobj, info);
3588 }
3589
3590 /* Build a name for an entry in the stub hash table.  */
3591
3592 static char *
3593 ppc_stub_name (const asection *input_section,
3594                const asection *sym_sec,
3595                const struct ppc_link_hash_entry *h,
3596                const Elf_Internal_Rela *rel)
3597 {
3598   char *stub_name;
3599   ssize_t len;
3600
3601   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3602      offsets from a sym as a branch target?  In fact, we could
3603      probably assume the addend is always zero.  */
3604   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3605
3606   if (h)
3607     {
3608       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3609       stub_name = bfd_malloc (len);
3610       if (stub_name == NULL)
3611         return stub_name;
3612
3613       len = sprintf (stub_name, "%08x.%s+%x",
3614                      input_section->id & 0xffffffff,
3615                      h->elf.root.root.string,
3616                      (int) rel->r_addend & 0xffffffff);
3617     }
3618   else
3619     {
3620       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3621       stub_name = bfd_malloc (len);
3622       if (stub_name == NULL)
3623         return stub_name;
3624
3625       len = sprintf (stub_name, "%08x.%x:%x+%x",
3626                      input_section->id & 0xffffffff,
3627                      sym_sec->id & 0xffffffff,
3628                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3629                      (int) rel->r_addend & 0xffffffff);
3630     }
3631   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3632     stub_name[len - 2] = 0;
3633   return stub_name;
3634 }
3635
3636 /* Look up an entry in the stub hash.  Stub entries are cached because
3637    creating the stub name takes a bit of time.  */
3638
3639 static struct ppc_stub_hash_entry *
3640 ppc_get_stub_entry (const asection *input_section,
3641                     const asection *sym_sec,
3642                     struct ppc_link_hash_entry *h,
3643                     const Elf_Internal_Rela *rel,
3644                     struct ppc_link_hash_table *htab)
3645 {
3646   struct ppc_stub_hash_entry *stub_entry;
3647   struct map_stub *group;
3648
3649   /* If this input section is part of a group of sections sharing one
3650      stub section, then use the id of the first section in the group.
3651      Stub names need to include a section id, as there may well be
3652      more than one stub used to reach say, printf, and we need to
3653      distinguish between them.  */
3654   group = htab->sec_info[input_section->id].u.group;
3655   if (group == NULL)
3656     return NULL;
3657
3658   if (h != NULL && h->u.stub_cache != NULL
3659       && h->u.stub_cache->h == h
3660       && h->u.stub_cache->group == group)
3661     {
3662       stub_entry = h->u.stub_cache;
3663     }
3664   else
3665     {
3666       char *stub_name;
3667
3668       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3669       if (stub_name == NULL)
3670         return NULL;
3671
3672       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3673                                          stub_name, FALSE, FALSE);
3674       if (h != NULL)
3675         h->u.stub_cache = stub_entry;
3676
3677       free (stub_name);
3678     }
3679
3680   return stub_entry;
3681 }
3682
3683 /* Add a new stub entry to the stub hash.  Not all fields of the new
3684    stub entry are initialised.  */
3685
3686 static struct ppc_stub_hash_entry *
3687 ppc_add_stub (const char *stub_name,
3688               asection *section,
3689               struct bfd_link_info *info)
3690 {
3691   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3692   struct map_stub *group;
3693   asection *link_sec;
3694   asection *stub_sec;
3695   struct ppc_stub_hash_entry *stub_entry;
3696
3697   group = htab->sec_info[section->id].u.group;
3698   link_sec = group->link_sec;
3699   stub_sec = group->stub_sec;
3700   if (stub_sec == NULL)
3701     {
3702       size_t namelen;
3703       bfd_size_type len;
3704       char *s_name;
3705
3706       namelen = strlen (link_sec->name);
3707       len = namelen + sizeof (STUB_SUFFIX);
3708       s_name = bfd_alloc (htab->params->stub_bfd, len);
3709       if (s_name == NULL)
3710         return NULL;
3711
3712       memcpy (s_name, link_sec->name, namelen);
3713       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3714       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3715       if (stub_sec == NULL)
3716         return NULL;
3717       group->stub_sec = stub_sec;
3718     }
3719
3720   /* Enter this entry into the linker stub hash table.  */
3721   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3722                                      TRUE, FALSE);
3723   if (stub_entry == NULL)
3724     {
3725       /* xgettext:c-format */
3726       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3727                           section->owner, stub_name);
3728       return NULL;
3729     }
3730
3731   stub_entry->group = group;
3732   stub_entry->stub_offset = 0;
3733   return stub_entry;
3734 }
3735
3736 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3737    not already done.  */
3738
3739 static bfd_boolean
3740 create_got_section (bfd *abfd, struct bfd_link_info *info)
3741 {
3742   asection *got, *relgot;
3743   flagword flags;
3744   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3745
3746   if (!is_ppc64_elf (abfd))
3747     return FALSE;
3748   if (htab == NULL)
3749     return FALSE;
3750
3751   if (!htab->elf.sgot
3752       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3753     return FALSE;
3754
3755   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3756            | SEC_LINKER_CREATED);
3757
3758   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3759   if (!got
3760       || !bfd_set_section_alignment (abfd, got, 3))
3761     return FALSE;
3762
3763   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3764                                                flags | SEC_READONLY);
3765   if (!relgot
3766       || !bfd_set_section_alignment (abfd, relgot, 3))
3767     return FALSE;
3768
3769   ppc64_elf_tdata (abfd)->got = got;
3770   ppc64_elf_tdata (abfd)->relgot = relgot;
3771   return TRUE;
3772 }
3773
3774 /* Follow indirect and warning symbol links.  */
3775
3776 static inline struct bfd_link_hash_entry *
3777 follow_link (struct bfd_link_hash_entry *h)
3778 {
3779   while (h->type == bfd_link_hash_indirect
3780          || h->type == bfd_link_hash_warning)
3781     h = h->u.i.link;
3782   return h;
3783 }
3784
3785 static inline struct elf_link_hash_entry *
3786 elf_follow_link (struct elf_link_hash_entry *h)
3787 {
3788   return (struct elf_link_hash_entry *) follow_link (&h->root);
3789 }
3790
3791 static inline struct ppc_link_hash_entry *
3792 ppc_follow_link (struct ppc_link_hash_entry *h)
3793 {
3794   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3795 }
3796
3797 /* Merge PLT info on FROM with that on TO.  */
3798
3799 static void
3800 move_plt_plist (struct ppc_link_hash_entry *from,
3801                 struct ppc_link_hash_entry *to)
3802 {
3803   if (from->elf.plt.plist != NULL)
3804     {
3805       if (to->elf.plt.plist != NULL)
3806         {
3807           struct plt_entry **entp;
3808           struct plt_entry *ent;
3809
3810           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3811             {
3812               struct plt_entry *dent;
3813
3814               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3815                 if (dent->addend == ent->addend)
3816                   {
3817                     dent->plt.refcount += ent->plt.refcount;
3818                     *entp = ent->next;
3819                     break;
3820                   }
3821               if (dent == NULL)
3822                 entp = &ent->next;
3823             }
3824           *entp = to->elf.plt.plist;
3825         }
3826
3827       to->elf.plt.plist = from->elf.plt.plist;
3828       from->elf.plt.plist = NULL;
3829     }
3830 }
3831
3832 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3833
3834 static void
3835 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3836                                 struct elf_link_hash_entry *dir,
3837                                 struct elf_link_hash_entry *ind)
3838 {
3839   struct ppc_link_hash_entry *edir, *eind;
3840
3841   edir = (struct ppc_link_hash_entry *) dir;
3842   eind = (struct ppc_link_hash_entry *) ind;
3843
3844   edir->is_func |= eind->is_func;
3845   edir->is_func_descriptor |= eind->is_func_descriptor;
3846   edir->tls_mask |= eind->tls_mask;
3847   if (eind->oh != NULL)
3848     edir->oh = ppc_follow_link (eind->oh);
3849
3850   if (edir->elf.versioned != versioned_hidden)
3851     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3852   edir->elf.ref_regular |= eind->elf.ref_regular;
3853   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3854   edir->elf.non_got_ref |= eind->elf.non_got_ref;
3855   edir->elf.needs_plt |= eind->elf.needs_plt;
3856   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3857
3858   /* If we were called to copy over info for a weak sym, don't copy
3859      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
3860      in order to simplify readonly_dynrelocs and save a field in the
3861      symbol hash entry, but that means dyn_relocs can't be used in any
3862      tests about a specific symbol, or affect other symbol flags which
3863      are then tested.  */
3864   if (eind->elf.root.type != bfd_link_hash_indirect)
3865     return;
3866
3867   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3868   if (eind->dyn_relocs != NULL)
3869     {
3870       if (edir->dyn_relocs != NULL)
3871         {
3872           struct elf_dyn_relocs **pp;
3873           struct elf_dyn_relocs *p;
3874
3875           /* Add reloc counts against the indirect sym to the direct sym
3876              list.  Merge any entries against the same section.  */
3877           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3878             {
3879               struct elf_dyn_relocs *q;
3880
3881               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3882                 if (q->sec == p->sec)
3883                   {
3884                     q->pc_count += p->pc_count;
3885                     q->count += p->count;
3886                     *pp = p->next;
3887                     break;
3888                   }
3889               if (q == NULL)
3890                 pp = &p->next;
3891             }
3892           *pp = edir->dyn_relocs;
3893         }
3894
3895       edir->dyn_relocs = eind->dyn_relocs;
3896       eind->dyn_relocs = NULL;
3897     }
3898
3899   /* Copy over got entries that we may have already seen to the
3900      symbol which just became indirect.  */
3901   if (eind->elf.got.glist != NULL)
3902     {
3903       if (edir->elf.got.glist != NULL)
3904         {
3905           struct got_entry **entp;
3906           struct got_entry *ent;
3907
3908           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3909             {
3910               struct got_entry *dent;
3911
3912               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3913                 if (dent->addend == ent->addend
3914                     && dent->owner == ent->owner
3915                     && dent->tls_type == ent->tls_type)
3916                   {
3917                     dent->got.refcount += ent->got.refcount;
3918                     *entp = ent->next;
3919                     break;
3920                   }
3921               if (dent == NULL)
3922                 entp = &ent->next;
3923             }
3924           *entp = edir->elf.got.glist;
3925         }
3926
3927       edir->elf.got.glist = eind->elf.got.glist;
3928       eind->elf.got.glist = NULL;
3929     }
3930
3931   /* And plt entries.  */
3932   move_plt_plist (eind, edir);
3933
3934   if (eind->elf.dynindx != -1)
3935     {
3936       if (edir->elf.dynindx != -1)
3937         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3938                                 edir->elf.dynstr_index);
3939       edir->elf.dynindx = eind->elf.dynindx;
3940       edir->elf.dynstr_index = eind->elf.dynstr_index;
3941       eind->elf.dynindx = -1;
3942       eind->elf.dynstr_index = 0;
3943     }
3944 }
3945
3946 /* Find the function descriptor hash entry from the given function code
3947    hash entry FH.  Link the entries via their OH fields.  */
3948
3949 static struct ppc_link_hash_entry *
3950 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3951 {
3952   struct ppc_link_hash_entry *fdh = fh->oh;
3953
3954   if (fdh == NULL)
3955     {
3956       const char *fd_name = fh->elf.root.root.string + 1;
3957
3958       fdh = (struct ppc_link_hash_entry *)
3959         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3960       if (fdh == NULL)
3961         return fdh;
3962
3963       fdh->is_func_descriptor = 1;
3964       fdh->oh = fh;
3965       fh->is_func = 1;
3966       fh->oh = fdh;
3967     }
3968
3969   fdh = ppc_follow_link (fdh);
3970   fdh->is_func_descriptor = 1;
3971   fdh->oh = fh;
3972   return fdh;
3973 }
3974
3975 /* Make a fake function descriptor sym for the undefined code sym FH.  */
3976
3977 static struct ppc_link_hash_entry *
3978 make_fdh (struct bfd_link_info *info,
3979           struct ppc_link_hash_entry *fh)
3980 {
3981   bfd *abfd = fh->elf.root.u.undef.abfd;
3982   struct bfd_link_hash_entry *bh = NULL;
3983   struct ppc_link_hash_entry *fdh;
3984   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3985                     ? BSF_WEAK
3986                     : BSF_GLOBAL);
3987
3988   if (!_bfd_generic_link_add_one_symbol (info, abfd,
3989                                          fh->elf.root.root.string + 1,
3990                                          flags, bfd_und_section_ptr, 0,
3991                                          NULL, FALSE, FALSE, &bh))
3992     return NULL;
3993
3994   fdh = (struct ppc_link_hash_entry *) bh;
3995   fdh->elf.non_elf = 0;
3996   fdh->fake = 1;
3997   fdh->is_func_descriptor = 1;
3998   fdh->oh = fh;
3999   fh->is_func = 1;
4000   fh->oh = fdh;
4001   return fdh;
4002 }
4003
4004 /* Fix function descriptor symbols defined in .opd sections to be
4005    function type.  */
4006
4007 static bfd_boolean
4008 ppc64_elf_add_symbol_hook (bfd *ibfd,
4009                            struct bfd_link_info *info,
4010                            Elf_Internal_Sym *isym,
4011                            const char **name,
4012                            flagword *flags ATTRIBUTE_UNUSED,
4013                            asection **sec,
4014                            bfd_vma *value)
4015 {
4016   if (*sec != NULL
4017       && strcmp ((*sec)->name, ".opd") == 0)
4018     {
4019       asection *code_sec;
4020
4021       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4022             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4023         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4024
4025       /* If the symbol is a function defined in .opd, and the function
4026          code is in a discarded group, let it appear to be undefined.  */
4027       if (!bfd_link_relocatable (info)
4028           && (*sec)->reloc_count != 0
4029           && opd_entry_value (*sec, *value, &code_sec, NULL,
4030                               FALSE) != (bfd_vma) -1
4031           && discarded_section (code_sec))
4032         {
4033           *sec = bfd_und_section_ptr;
4034           isym->st_shndx = SHN_UNDEF;
4035         }
4036     }
4037   else if (*sec != NULL
4038            && strcmp ((*sec)->name, ".toc") == 0
4039            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4040     {
4041       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4042       if (htab != NULL)
4043         htab->params->object_in_toc = 1;
4044     }
4045
4046   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4047     {
4048       if (abiversion (ibfd) == 0)
4049         set_abiversion (ibfd, 2);
4050       else if (abiversion (ibfd) == 1)
4051         {
4052           _bfd_error_handler (_("symbol '%s' has invalid st_other"
4053                                 " for ABI version 1"), *name);
4054           bfd_set_error (bfd_error_bad_value);
4055           return FALSE;
4056         }
4057     }
4058
4059   return TRUE;
4060 }
4061
4062 /* Merge non-visibility st_other attributes: local entry point.  */
4063
4064 static void
4065 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4066                                   const Elf_Internal_Sym *isym,
4067                                   bfd_boolean definition,
4068                                   bfd_boolean dynamic)
4069 {
4070   if (definition && (!dynamic || !h->def_regular))
4071     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4072                 | ELF_ST_VISIBILITY (h->other));
4073 }
4074
4075 /* Hook called on merging a symbol.  We use this to clear "fake" since
4076    we now have a real symbol.  */
4077
4078 static bfd_boolean
4079 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4080                         const Elf_Internal_Sym *isym,
4081                         asection **psec ATTRIBUTE_UNUSED,
4082                         bfd_boolean newdef ATTRIBUTE_UNUSED,
4083                         bfd_boolean olddef ATTRIBUTE_UNUSED,
4084                         bfd *oldbfd ATTRIBUTE_UNUSED,
4085                         const asection *oldsec ATTRIBUTE_UNUSED)
4086 {
4087   ((struct ppc_link_hash_entry *) h)->fake = 0;
4088   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4089     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
4090   return TRUE;
4091 }
4092
4093 /* This function makes an old ABI object reference to ".bar" cause the
4094    inclusion of a new ABI object archive that defines "bar".
4095    NAME is a symbol defined in an archive.  Return a symbol in the hash
4096    table that might be satisfied by the archive symbols.  */
4097
4098 static struct elf_link_hash_entry *
4099 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4100                                  struct bfd_link_info *info,
4101                                  const char *name)
4102 {
4103   struct elf_link_hash_entry *h;
4104   char *dot_name;
4105   size_t len;
4106
4107   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4108   if (h != NULL
4109       /* Don't return this sym if it is a fake function descriptor
4110          created by add_symbol_adjust.  */
4111       && !((struct ppc_link_hash_entry *) h)->fake)
4112     return h;
4113
4114   if (name[0] == '.')
4115     return h;
4116
4117   len = strlen (name);
4118   dot_name = bfd_alloc (abfd, len + 2);
4119   if (dot_name == NULL)
4120     return (struct elf_link_hash_entry *) -1;
4121   dot_name[0] = '.';
4122   memcpy (dot_name + 1, name, len + 1);
4123   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4124   bfd_release (abfd, dot_name);
4125   return h;
4126 }
4127
4128 /* This function satisfies all old ABI object references to ".bar" if a
4129    new ABI object defines "bar".  Well, at least, undefined dot symbols
4130    are made weak.  This stops later archive searches from including an
4131    object if we already have a function descriptor definition.  It also
4132    prevents the linker complaining about undefined symbols.
4133    We also check and correct mismatched symbol visibility here.  The
4134    most restrictive visibility of the function descriptor and the
4135    function entry symbol is used.  */
4136
4137 static bfd_boolean
4138 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4139 {
4140   struct ppc_link_hash_table *htab;
4141   struct ppc_link_hash_entry *fdh;
4142
4143   if (eh->elf.root.type == bfd_link_hash_warning)
4144     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4145
4146   if (eh->elf.root.type == bfd_link_hash_indirect)
4147     return TRUE;
4148
4149   if (eh->elf.root.root.string[0] != '.')
4150     abort ();
4151
4152   htab = ppc_hash_table (info);
4153   if (htab == NULL)
4154     return FALSE;
4155
4156   fdh = lookup_fdh (eh, htab);
4157   if (fdh == NULL
4158       && !bfd_link_relocatable (info)
4159       && (eh->elf.root.type == bfd_link_hash_undefined
4160           || eh->elf.root.type == bfd_link_hash_undefweak)
4161       && eh->elf.ref_regular)
4162     {
4163       /* Make an undefined function descriptor sym, in order to
4164          pull in an --as-needed shared lib.  Archives are handled
4165          elsewhere.  */
4166       fdh = make_fdh (info, eh);
4167       if (fdh == NULL)
4168         return FALSE;
4169     }
4170
4171   if (fdh != NULL)
4172     {
4173       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4174       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4175
4176       /* Make both descriptor and entry symbol have the most
4177          constraining visibility of either symbol.  */
4178       if (entry_vis < descr_vis)
4179         fdh->elf.other += entry_vis - descr_vis;
4180       else if (entry_vis > descr_vis)
4181         eh->elf.other += descr_vis - entry_vis;
4182
4183       /* Propagate reference flags from entry symbol to function
4184          descriptor symbol.  */
4185       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4186       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4187       fdh->elf.ref_regular |= eh->elf.ref_regular;
4188       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4189
4190       if (!fdh->elf.forced_local
4191           && fdh->elf.dynindx == -1
4192           && fdh->elf.versioned != versioned_hidden
4193           && (bfd_link_dll (info)
4194               || fdh->elf.def_dynamic
4195               || fdh->elf.ref_dynamic)
4196           && (eh->elf.ref_regular
4197               || eh->elf.def_regular))
4198         {
4199           if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4200             return FALSE;
4201         }
4202     }
4203
4204   return TRUE;
4205 }
4206
4207 /* Set up opd section info and abiversion for IBFD, and process list
4208    of dot-symbols we made in link_hash_newfunc.  */
4209
4210 static bfd_boolean
4211 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4212 {
4213   struct ppc_link_hash_table *htab;
4214   struct ppc_link_hash_entry **p, *eh;
4215   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4216
4217   if (opd != NULL && opd->size != 0)
4218     {
4219       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4220       ppc64_elf_section_data (opd)->sec_type = sec_opd;
4221
4222       if (abiversion (ibfd) == 0)
4223         set_abiversion (ibfd, 1);
4224       else if (abiversion (ibfd) >= 2)
4225         {
4226           /* xgettext:c-format */
4227           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4228                               ibfd, abiversion (ibfd));
4229           bfd_set_error (bfd_error_bad_value);
4230           return FALSE;
4231         }
4232     }
4233
4234   if (is_ppc64_elf (info->output_bfd))
4235     {
4236       /* For input files without an explicit abiversion in e_flags
4237          we should have flagged any with symbol st_other bits set
4238          as ELFv1 and above flagged those with .opd as ELFv2.
4239          Set the output abiversion if not yet set, and for any input
4240          still ambiguous, take its abiversion from the output.
4241          Differences in ABI are reported later.  */
4242       if (abiversion (info->output_bfd) == 0)
4243         set_abiversion (info->output_bfd, abiversion (ibfd));
4244       else if (abiversion (ibfd) == 0)
4245         set_abiversion (ibfd, abiversion (info->output_bfd));
4246     }
4247
4248   htab = ppc_hash_table (info);
4249   if (htab == NULL)
4250     return TRUE;
4251
4252   if (opd != NULL && opd->size != 0
4253       && (ibfd->flags & DYNAMIC) == 0
4254       && (opd->flags & SEC_RELOC) != 0
4255       && opd->reloc_count != 0
4256       && !bfd_is_abs_section (opd->output_section)
4257       && info->gc_sections)
4258     {
4259       /* Garbage collection needs some extra help with .opd sections.
4260          We don't want to necessarily keep everything referenced by
4261          relocs in .opd, as that would keep all functions.  Instead,
4262          if we reference an .opd symbol (a function descriptor), we
4263          want to keep the function code symbol's section.  This is
4264          easy for global symbols, but for local syms we need to keep
4265          information about the associated function section.  */
4266       bfd_size_type amt;
4267       asection **opd_sym_map;
4268       Elf_Internal_Shdr *symtab_hdr;
4269       Elf_Internal_Rela *relocs, *rel_end, *rel;
4270
4271       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4272       opd_sym_map = bfd_zalloc (ibfd, amt);
4273       if (opd_sym_map == NULL)
4274         return FALSE;
4275       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4276       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4277                                           info->keep_memory);
4278       if (relocs == NULL)
4279         return FALSE;
4280       symtab_hdr = &elf_symtab_hdr (ibfd);
4281       rel_end = relocs + opd->reloc_count - 1;
4282       for (rel = relocs; rel < rel_end; rel++)
4283         {
4284           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4285           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4286
4287           if (r_type == R_PPC64_ADDR64
4288               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4289               && r_symndx < symtab_hdr->sh_info)
4290             {
4291               Elf_Internal_Sym *isym;
4292               asection *s;
4293
4294               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4295               if (isym == NULL)
4296                 {
4297                   if (elf_section_data (opd)->relocs != relocs)
4298                     free (relocs);
4299                   return FALSE;
4300                 }
4301
4302               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4303               if (s != NULL && s != opd)
4304                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4305             }
4306         }
4307       if (elf_section_data (opd)->relocs != relocs)
4308         free (relocs);
4309     }
4310
4311   p = &htab->dot_syms;
4312   while ((eh = *p) != NULL)
4313     {
4314       *p = NULL;
4315       if (&eh->elf == htab->elf.hgot)
4316         ;
4317       else if (htab->elf.hgot == NULL
4318                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4319         htab->elf.hgot = &eh->elf;
4320       else if (abiversion (ibfd) <= 1)
4321         {
4322           htab->need_func_desc_adj = 1;
4323           if (!add_symbol_adjust (eh, info))
4324             return FALSE;
4325         }
4326       p = &eh->u.next_dot_sym;
4327     }
4328   return TRUE;
4329 }
4330
4331 /* Undo hash table changes when an --as-needed input file is determined
4332    not to be needed.  */
4333
4334 static bfd_boolean
4335 ppc64_elf_notice_as_needed (bfd *ibfd,
4336                             struct bfd_link_info *info,
4337                             enum notice_asneeded_action act)
4338 {
4339   if (act == notice_not_needed)
4340     {
4341       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4342
4343       if (htab == NULL)
4344         return FALSE;
4345
4346       htab->dot_syms = NULL;
4347     }
4348   return _bfd_elf_notice_as_needed (ibfd, info, act);
4349 }
4350
4351 /* If --just-symbols against a final linked binary, then assume we need
4352    toc adjusting stubs when calling functions defined there.  */
4353
4354 static void
4355 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4356 {
4357   if ((sec->flags & SEC_CODE) != 0
4358       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4359       && is_ppc64_elf (sec->owner))
4360     {
4361       if (abiversion (sec->owner) >= 2
4362           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4363         sec->has_toc_reloc = 1;
4364     }
4365   _bfd_elf_link_just_syms (sec, info);
4366 }
4367
4368 static struct plt_entry **
4369 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4370                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4371 {
4372   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4373   struct plt_entry **local_plt;
4374   unsigned char *local_got_tls_masks;
4375
4376   if (local_got_ents == NULL)
4377     {
4378       bfd_size_type size = symtab_hdr->sh_info;
4379
4380       size *= (sizeof (*local_got_ents)
4381                + sizeof (*local_plt)
4382                + sizeof (*local_got_tls_masks));
4383       local_got_ents = bfd_zalloc (abfd, size);
4384       if (local_got_ents == NULL)
4385         return NULL;
4386       elf_local_got_ents (abfd) = local_got_ents;
4387     }
4388
4389   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4390     {
4391       struct got_entry *ent;
4392
4393       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4394         if (ent->addend == r_addend
4395             && ent->owner == abfd
4396             && ent->tls_type == tls_type)
4397           break;
4398       if (ent == NULL)
4399         {
4400           bfd_size_type amt = sizeof (*ent);
4401           ent = bfd_alloc (abfd, amt);
4402           if (ent == NULL)
4403             return FALSE;
4404           ent->next = local_got_ents[r_symndx];
4405           ent->addend = r_addend;
4406           ent->owner = abfd;
4407           ent->tls_type = tls_type;
4408           ent->is_indirect = FALSE;
4409           ent->got.refcount = 0;
4410           local_got_ents[r_symndx] = ent;
4411         }
4412       ent->got.refcount += 1;
4413     }
4414
4415   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4416   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4417   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4418
4419   return local_plt + r_symndx;
4420 }
4421
4422 static bfd_boolean
4423 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4424 {
4425   struct plt_entry *ent;
4426
4427   for (ent = *plist; ent != NULL; ent = ent->next)
4428     if (ent->addend == addend)
4429       break;
4430   if (ent == NULL)
4431     {
4432       bfd_size_type amt = sizeof (*ent);
4433       ent = bfd_alloc (abfd, amt);
4434       if (ent == NULL)
4435         return FALSE;
4436       ent->next = *plist;
4437       ent->addend = addend;
4438       ent->plt.refcount = 0;
4439       *plist = ent;
4440     }
4441   ent->plt.refcount += 1;
4442   return TRUE;
4443 }
4444
4445 static bfd_boolean
4446 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4447 {
4448   return (r_type == R_PPC64_REL24
4449           || r_type == R_PPC64_REL24_NOTOC
4450           || r_type == R_PPC64_REL14
4451           || r_type == R_PPC64_REL14_BRTAKEN
4452           || r_type == R_PPC64_REL14_BRNTAKEN
4453           || r_type == R_PPC64_ADDR24
4454           || r_type == R_PPC64_ADDR14
4455           || r_type == R_PPC64_ADDR14_BRTAKEN
4456           || r_type == R_PPC64_ADDR14_BRNTAKEN
4457           || r_type == R_PPC64_PLTCALL
4458           || r_type == R_PPC64_PLTCALL_NOTOC);
4459 }
4460
4461 /* Relocs on inline plt call sequence insns prior to the call.  */
4462
4463 static bfd_boolean
4464 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4465 {
4466   return (r_type == R_PPC64_PLT16_HA
4467           || r_type == R_PPC64_PLT16_HI
4468           || r_type == R_PPC64_PLT16_LO
4469           || r_type == R_PPC64_PLT16_LO_DS
4470           || r_type == R_PPC64_PLT_PCREL34
4471           || r_type == R_PPC64_PLT_PCREL34_NOTOC
4472           || r_type == R_PPC64_PLTSEQ
4473           || r_type == R_PPC64_PLTSEQ_NOTOC);
4474 }
4475
4476 /* Look through the relocs for a section during the first phase, and
4477    calculate needed space in the global offset table, procedure
4478    linkage table, and dynamic reloc sections.  */
4479
4480 static bfd_boolean
4481 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4482                         asection *sec, const Elf_Internal_Rela *relocs)
4483 {
4484   struct ppc_link_hash_table *htab;
4485   Elf_Internal_Shdr *symtab_hdr;
4486   struct elf_link_hash_entry **sym_hashes;
4487   const Elf_Internal_Rela *rel;
4488   const Elf_Internal_Rela *rel_end;
4489   asection *sreloc;
4490   struct elf_link_hash_entry *tga, *dottga;
4491   bfd_boolean is_opd;
4492
4493   if (bfd_link_relocatable (info))
4494     return TRUE;
4495
4496   /* Don't do anything special with non-loaded, non-alloced sections.
4497      In particular, any relocs in such sections should not affect GOT
4498      and PLT reference counting (ie. we don't allow them to create GOT
4499      or PLT entries), there's no possibility or desire to optimize TLS
4500      relocs, and there's not much point in propagating relocs to shared
4501      libs that the dynamic linker won't relocate.  */
4502   if ((sec->flags & SEC_ALLOC) == 0)
4503     return TRUE;
4504
4505   BFD_ASSERT (is_ppc64_elf (abfd));
4506
4507   htab = ppc_hash_table (info);
4508   if (htab == NULL)
4509     return FALSE;
4510
4511   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4512                               FALSE, FALSE, TRUE);
4513   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4514                                  FALSE, FALSE, TRUE);
4515   symtab_hdr = &elf_symtab_hdr (abfd);
4516   sym_hashes = elf_sym_hashes (abfd);
4517   sreloc = NULL;
4518   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4519   rel_end = relocs + sec->reloc_count;
4520   for (rel = relocs; rel < rel_end; rel++)
4521     {
4522       unsigned long r_symndx;
4523       struct elf_link_hash_entry *h;
4524       enum elf_ppc64_reloc_type r_type;
4525       int tls_type;
4526       struct _ppc64_elf_section_data *ppc64_sec;
4527       struct plt_entry **ifunc, **plt_list;
4528
4529       r_symndx = ELF64_R_SYM (rel->r_info);
4530       if (r_symndx < symtab_hdr->sh_info)
4531         h = NULL;
4532       else
4533         {
4534           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4535           h = elf_follow_link (h);
4536
4537           if (h == htab->elf.hgot)
4538             sec->has_toc_reloc = 1;
4539         }
4540
4541       r_type = ELF64_R_TYPE (rel->r_info);
4542       switch (r_type)
4543         {
4544         case R_PPC64_D34:
4545         case R_PPC64_D34_LO:
4546         case R_PPC64_D34_HI30:
4547         case R_PPC64_D34_HA30:
4548         case R_PPC64_D28:
4549         case R_PPC64_TPREL34:
4550         case R_PPC64_DTPREL34:
4551         case R_PPC64_PCREL34:
4552         case R_PPC64_GOT_PCREL34:
4553         case R_PPC64_GOT_TLSGD34:
4554         case R_PPC64_GOT_TLSLD34:
4555         case R_PPC64_GOT_TPREL34:
4556         case R_PPC64_GOT_DTPREL34:
4557         case R_PPC64_PLT_PCREL34:
4558         case R_PPC64_PLT_PCREL34_NOTOC:
4559         case R_PPC64_PCREL28:
4560           htab->powerxx_stubs = 1;
4561           break;
4562         default:
4563           break;
4564         }
4565
4566       switch (r_type)
4567         {
4568         case R_PPC64_PLT16_HA:
4569         case R_PPC64_GOT_TLSLD16_HA:
4570         case R_PPC64_GOT_TLSGD16_HA:
4571         case R_PPC64_GOT_TPREL16_HA:
4572         case R_PPC64_GOT_DTPREL16_HA:
4573         case R_PPC64_GOT16_HA:
4574         case R_PPC64_TOC16_HA:
4575         case R_PPC64_PLT16_LO:
4576         case R_PPC64_PLT16_LO_DS:
4577         case R_PPC64_GOT_TLSLD16_LO:
4578         case R_PPC64_GOT_TLSGD16_LO:
4579         case R_PPC64_GOT_TPREL16_LO_DS:
4580         case R_PPC64_GOT_DTPREL16_LO_DS:
4581         case R_PPC64_GOT16_LO:
4582         case R_PPC64_GOT16_LO_DS:
4583         case R_PPC64_TOC16_LO:
4584         case R_PPC64_TOC16_LO_DS:
4585         case R_PPC64_GOT_PCREL34:
4586           ppc64_elf_tdata (abfd)->has_optrel = 1;
4587           ppc64_elf_section_data (sec)->has_optrel = 1;
4588           break;
4589         default:
4590           break;
4591         }
4592
4593       ifunc = NULL;
4594       if (h != NULL)
4595         {
4596           if (h->type == STT_GNU_IFUNC)
4597             {
4598               h->needs_plt = 1;
4599               ifunc = &h->plt.plist;
4600             }
4601         }
4602       else
4603         {
4604           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4605                                                           abfd, r_symndx);
4606           if (isym == NULL)
4607             return FALSE;
4608
4609           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4610             {
4611               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4612                                              rel->r_addend,
4613                                              NON_GOT | PLT_IFUNC);
4614               if (ifunc == NULL)
4615                 return FALSE;
4616             }
4617         }
4618
4619       tls_type = 0;
4620       switch (r_type)
4621         {
4622         case R_PPC64_TLSGD:
4623         case R_PPC64_TLSLD:
4624           /* These special tls relocs tie a call to __tls_get_addr with
4625              its parameter symbol.  */
4626           if (h != NULL)
4627             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4628           else
4629             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4630                                         rel->r_addend,
4631                                         NON_GOT | TLS_TLS | TLS_MARK))
4632               return FALSE;
4633           sec->has_tls_reloc = 1;
4634           break;
4635
4636         case R_PPC64_GOT_TLSLD16:
4637         case R_PPC64_GOT_TLSLD16_LO:
4638         case R_PPC64_GOT_TLSLD16_HI:
4639         case R_PPC64_GOT_TLSLD16_HA:
4640         case R_PPC64_GOT_TLSLD34:
4641           tls_type = TLS_TLS | TLS_LD;
4642           goto dogottls;
4643
4644         case R_PPC64_GOT_TLSGD16:
4645         case R_PPC64_GOT_TLSGD16_LO:
4646         case R_PPC64_GOT_TLSGD16_HI:
4647         case R_PPC64_GOT_TLSGD16_HA:
4648         case R_PPC64_GOT_TLSGD34:
4649           tls_type = TLS_TLS | TLS_GD;
4650           goto dogottls;
4651
4652         case R_PPC64_GOT_TPREL16_DS:
4653         case R_PPC64_GOT_TPREL16_LO_DS:
4654         case R_PPC64_GOT_TPREL16_HI:
4655         case R_PPC64_GOT_TPREL16_HA:
4656         case R_PPC64_GOT_TPREL34:
4657           if (bfd_link_dll (info))
4658             info->flags |= DF_STATIC_TLS;
4659           tls_type = TLS_TLS | TLS_TPREL;
4660           goto dogottls;
4661
4662         case R_PPC64_GOT_DTPREL16_DS:
4663         case R_PPC64_GOT_DTPREL16_LO_DS:
4664         case R_PPC64_GOT_DTPREL16_HI:
4665         case R_PPC64_GOT_DTPREL16_HA:
4666         case R_PPC64_GOT_DTPREL34:
4667           tls_type = TLS_TLS | TLS_DTPREL;
4668         dogottls:
4669           sec->has_tls_reloc = 1;
4670           goto dogot;
4671
4672         case R_PPC64_GOT16:
4673         case R_PPC64_GOT16_LO:
4674         case R_PPC64_GOT16_HI:
4675         case R_PPC64_GOT16_HA:
4676         case R_PPC64_GOT16_DS:
4677         case R_PPC64_GOT16_LO_DS:
4678         case R_PPC64_GOT_PCREL34:
4679         dogot:
4680           /* This symbol requires a global offset table entry.  */
4681           sec->has_toc_reloc = 1;
4682           if (r_type == R_PPC64_GOT_TLSLD16
4683               || r_type == R_PPC64_GOT_TLSGD16
4684               || r_type == R_PPC64_GOT_TPREL16_DS
4685               || r_type == R_PPC64_GOT_DTPREL16_DS
4686               || r_type == R_PPC64_GOT16
4687               || r_type == R_PPC64_GOT16_DS)
4688             {
4689               htab->do_multi_toc = 1;
4690               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4691             }
4692
4693           if (ppc64_elf_tdata (abfd)->got == NULL
4694               && !create_got_section (abfd, info))
4695             return FALSE;
4696
4697           if (h != NULL)
4698             {
4699               struct ppc_link_hash_entry *eh;
4700               struct got_entry *ent;
4701
4702               eh = (struct ppc_link_hash_entry *) h;
4703               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4704                 if (ent->addend == rel->r_addend
4705                     && ent->owner == abfd
4706                     && ent->tls_type == tls_type)
4707                   break;
4708               if (ent == NULL)
4709                 {
4710                   bfd_size_type amt = sizeof (*ent);
4711                   ent = bfd_alloc (abfd, amt);
4712                   if (ent == NULL)
4713                     return FALSE;
4714                   ent->next = eh->elf.got.glist;
4715                   ent->addend = rel->r_addend;
4716                   ent->owner = abfd;
4717                   ent->tls_type = tls_type;
4718                   ent->is_indirect = FALSE;
4719                   ent->got.refcount = 0;
4720                   eh->elf.got.glist = ent;
4721                 }
4722               ent->got.refcount += 1;
4723               eh->tls_mask |= tls_type;
4724             }
4725           else
4726             /* This is a global offset table entry for a local symbol.  */
4727             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4728                                         rel->r_addend, tls_type))
4729               return FALSE;
4730
4731           /* We may also need a plt entry if the symbol turns out to be
4732              an ifunc.  */
4733           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4734             {
4735               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4736                 return FALSE;
4737             }
4738           break;
4739
4740         case R_PPC64_PLT16_HA:
4741         case R_PPC64_PLT16_HI:
4742         case R_PPC64_PLT16_LO:
4743         case R_PPC64_PLT16_LO_DS:
4744         case R_PPC64_PLT_PCREL34:
4745         case R_PPC64_PLT_PCREL34_NOTOC:
4746         case R_PPC64_PLT32:
4747         case R_PPC64_PLT64:
4748           /* This symbol requires a procedure linkage table entry.  */
4749           plt_list = ifunc;
4750           if (h != NULL)
4751             {
4752               h->needs_plt = 1;
4753               if (h->root.root.string[0] == '.'
4754                   && h->root.root.string[1] != '\0')
4755                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4756               ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4757               plt_list = &h->plt.plist;
4758             }
4759           if (plt_list == NULL)
4760             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4761                                               rel->r_addend,
4762                                               NON_GOT | PLT_KEEP);
4763           if (!update_plt_info (abfd, plt_list, rel->r_addend))
4764             return FALSE;
4765           break;
4766
4767           /* The following relocations don't need to propagate the
4768              relocation if linking a shared object since they are
4769              section relative.  */
4770         case R_PPC64_SECTOFF:
4771         case R_PPC64_SECTOFF_LO:
4772         case R_PPC64_SECTOFF_HI:
4773         case R_PPC64_SECTOFF_HA:
4774         case R_PPC64_SECTOFF_DS:
4775         case R_PPC64_SECTOFF_LO_DS:
4776         case R_PPC64_DTPREL16:
4777         case R_PPC64_DTPREL16_LO:
4778         case R_PPC64_DTPREL16_HI:
4779         case R_PPC64_DTPREL16_HA:
4780         case R_PPC64_DTPREL16_DS:
4781         case R_PPC64_DTPREL16_LO_DS:
4782         case R_PPC64_DTPREL16_HIGH:
4783         case R_PPC64_DTPREL16_HIGHA:
4784         case R_PPC64_DTPREL16_HIGHER:
4785         case R_PPC64_DTPREL16_HIGHERA:
4786         case R_PPC64_DTPREL16_HIGHEST:
4787         case R_PPC64_DTPREL16_HIGHESTA:
4788           break;
4789
4790           /* Nor do these.  */
4791         case R_PPC64_REL16:
4792         case R_PPC64_REL16_LO:
4793         case R_PPC64_REL16_HI:
4794         case R_PPC64_REL16_HA:
4795         case R_PPC64_REL16_HIGH:
4796         case R_PPC64_REL16_HIGHA:
4797         case R_PPC64_REL16_HIGHER:
4798         case R_PPC64_REL16_HIGHERA:
4799         case R_PPC64_REL16_HIGHEST:
4800         case R_PPC64_REL16_HIGHESTA:
4801         case R_PPC64_REL16_HIGHER34:
4802         case R_PPC64_REL16_HIGHERA34:
4803         case R_PPC64_REL16_HIGHEST34:
4804         case R_PPC64_REL16_HIGHESTA34:
4805         case R_PPC64_REL16DX_HA:
4806           break;
4807
4808           /* Not supported as a dynamic relocation.  */
4809         case R_PPC64_ADDR64_LOCAL:
4810           if (bfd_link_pic (info))
4811             {
4812               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4813                 ppc_howto_init ();
4814               /* xgettext:c-format */
4815               info->callbacks->einfo (_("%H: %s reloc unsupported "
4816                                         "in shared libraries and PIEs\n"),
4817                                       abfd, sec, rel->r_offset,
4818                                       ppc64_elf_howto_table[r_type]->name);
4819               bfd_set_error (bfd_error_bad_value);
4820               return FALSE;
4821             }
4822           break;
4823
4824         case R_PPC64_TOC16:
4825         case R_PPC64_TOC16_DS:
4826           htab->do_multi_toc = 1;
4827           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4828           /* Fall through.  */
4829         case R_PPC64_TOC16_LO:
4830         case R_PPC64_TOC16_HI:
4831         case R_PPC64_TOC16_HA:
4832         case R_PPC64_TOC16_LO_DS:
4833           sec->has_toc_reloc = 1;
4834           break;
4835
4836           /* Marker reloc.  */
4837         case R_PPC64_ENTRY:
4838           break;
4839
4840           /* This relocation describes the C++ object vtable hierarchy.
4841              Reconstruct it for later use during GC.  */
4842         case R_PPC64_GNU_VTINHERIT:
4843           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4844             return FALSE;
4845           break;
4846
4847           /* This relocation describes which C++ vtable entries are actually
4848              used.  Record for later use during GC.  */
4849         case R_PPC64_GNU_VTENTRY:
4850           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4851             return FALSE;
4852           break;
4853
4854         case R_PPC64_REL14:
4855         case R_PPC64_REL14_BRTAKEN:
4856         case R_PPC64_REL14_BRNTAKEN:
4857           {
4858             asection *dest = NULL;
4859
4860             /* Heuristic: If jumping outside our section, chances are
4861                we are going to need a stub.  */
4862             if (h != NULL)
4863               {
4864                 /* If the sym is weak it may be overridden later, so
4865                    don't assume we know where a weak sym lives.  */
4866                 if (h->root.type == bfd_link_hash_defined)
4867                   dest = h->root.u.def.section;
4868               }
4869             else
4870               {
4871                 Elf_Internal_Sym *isym;
4872
4873                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4874                                               abfd, r_symndx);
4875                 if (isym == NULL)
4876                   return FALSE;
4877
4878                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4879               }
4880
4881             if (dest != sec)
4882               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4883           }
4884           goto rel24;
4885
4886         case R_PPC64_PLTCALL:
4887         case R_PPC64_PLTCALL_NOTOC:
4888           ppc64_elf_section_data (sec)->has_pltcall = 1;
4889           /* Fall through.  */
4890
4891         case R_PPC64_REL24:
4892         case R_PPC64_REL24_NOTOC:
4893         rel24:
4894           plt_list = ifunc;
4895           if (h != NULL)
4896             {
4897               h->needs_plt = 1;
4898               if (h->root.root.string[0] == '.'
4899                   && h->root.root.string[1] != '\0')
4900                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4901
4902               if (h == tga || h == dottga)
4903                 {
4904                   sec->has_tls_reloc = 1;
4905                   if (rel != relocs
4906                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4907                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4908                     /* We have a new-style __tls_get_addr call with
4909                        a marker reloc.  */
4910                     ;
4911                   else
4912                     /* Mark this section as having an old-style call.  */
4913                     sec->nomark_tls_get_addr = 1;
4914                 }
4915               plt_list = &h->plt.plist;
4916             }
4917
4918           /* We may need a .plt entry if the function this reloc
4919              refers to is in a shared lib.  */
4920           if (plt_list
4921               && !update_plt_info (abfd, plt_list, rel->r_addend))
4922             return FALSE;
4923           break;
4924
4925         case R_PPC64_ADDR14:
4926         case R_PPC64_ADDR14_BRNTAKEN:
4927         case R_PPC64_ADDR14_BRTAKEN:
4928         case R_PPC64_ADDR24:
4929           goto dodyn;
4930
4931         case R_PPC64_TPREL64:
4932           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4933           if (bfd_link_dll (info))
4934             info->flags |= DF_STATIC_TLS;
4935           goto dotlstoc;
4936
4937         case R_PPC64_DTPMOD64:
4938           if (rel + 1 < rel_end
4939               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4940               && rel[1].r_offset == rel->r_offset + 8)
4941             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4942           else
4943             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4944           goto dotlstoc;
4945
4946         case R_PPC64_DTPREL64:
4947           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4948           if (rel != relocs
4949               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4950               && rel[-1].r_offset == rel->r_offset - 8)
4951             /* This is the second reloc of a dtpmod, dtprel pair.
4952                Don't mark with TLS_DTPREL.  */
4953             goto dodyn;
4954
4955         dotlstoc:
4956           sec->has_tls_reloc = 1;
4957           if (h != NULL)
4958             {
4959               struct ppc_link_hash_entry *eh;
4960               eh = (struct ppc_link_hash_entry *) h;
4961               eh->tls_mask |= tls_type & 0xff;
4962             }
4963           else
4964             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4965                                         rel->r_addend, tls_type))
4966               return FALSE;
4967
4968           ppc64_sec = ppc64_elf_section_data (sec);
4969           if (ppc64_sec->sec_type != sec_toc)
4970             {
4971               bfd_size_type amt;
4972
4973               /* One extra to simplify get_tls_mask.  */
4974               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4975               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4976               if (ppc64_sec->u.toc.symndx == NULL)
4977                 return FALSE;
4978               amt = sec->size * sizeof (bfd_vma) / 8;
4979               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4980               if (ppc64_sec->u.toc.add == NULL)
4981                 return FALSE;
4982               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4983               ppc64_sec->sec_type = sec_toc;
4984             }
4985           BFD_ASSERT (rel->r_offset % 8 == 0);
4986           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4987           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4988
4989           /* Mark the second slot of a GD or LD entry.
4990              -1 to indicate GD and -2 to indicate LD.  */
4991           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4992             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4993           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4994             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4995           goto dodyn;
4996
4997         case R_PPC64_TPREL16:
4998         case R_PPC64_TPREL16_LO:
4999         case R_PPC64_TPREL16_HI:
5000         case R_PPC64_TPREL16_HA:
5001         case R_PPC64_TPREL16_DS:
5002         case R_PPC64_TPREL16_LO_DS:
5003         case R_PPC64_TPREL16_HIGH:
5004         case R_PPC64_TPREL16_HIGHA:
5005         case R_PPC64_TPREL16_HIGHER:
5006         case R_PPC64_TPREL16_HIGHERA:
5007         case R_PPC64_TPREL16_HIGHEST:
5008         case R_PPC64_TPREL16_HIGHESTA:
5009         case R_PPC64_TPREL34:
5010           if (bfd_link_dll (info))
5011             info->flags |= DF_STATIC_TLS;
5012           goto dodyn;
5013
5014         case R_PPC64_ADDR64:
5015           if (is_opd
5016               && rel + 1 < rel_end
5017               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5018             {
5019               if (h != NULL)
5020                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5021             }
5022           /* Fall through.  */
5023
5024         case R_PPC64_ADDR16:
5025         case R_PPC64_ADDR16_DS:
5026         case R_PPC64_ADDR16_HA:
5027         case R_PPC64_ADDR16_HI:
5028         case R_PPC64_ADDR16_HIGH:
5029         case R_PPC64_ADDR16_HIGHA:
5030         case R_PPC64_ADDR16_HIGHER:
5031         case R_PPC64_ADDR16_HIGHERA:
5032         case R_PPC64_ADDR16_HIGHEST:
5033         case R_PPC64_ADDR16_HIGHESTA:
5034         case R_PPC64_ADDR16_LO:
5035         case R_PPC64_ADDR16_LO_DS:
5036         case R_PPC64_D34:
5037         case R_PPC64_D34_LO:
5038         case R_PPC64_D34_HI30:
5039         case R_PPC64_D34_HA30:
5040         case R_PPC64_ADDR16_HIGHER34:
5041         case R_PPC64_ADDR16_HIGHERA34:
5042         case R_PPC64_ADDR16_HIGHEST34:
5043         case R_PPC64_ADDR16_HIGHESTA34:
5044         case R_PPC64_D28:
5045           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5046               && rel->r_addend == 0)
5047             {
5048               /* We may need a .plt entry if this reloc refers to a
5049                  function in a shared lib.  */
5050               if (!update_plt_info (abfd, &h->plt.plist, 0))
5051                 return FALSE;
5052               h->pointer_equality_needed = 1;
5053             }
5054           /* Fall through.  */
5055
5056         case R_PPC64_REL30:
5057         case R_PPC64_REL32:
5058         case R_PPC64_REL64:
5059         case R_PPC64_ADDR32:
5060         case R_PPC64_UADDR16:
5061         case R_PPC64_UADDR32:
5062         case R_PPC64_UADDR64:
5063         case R_PPC64_TOC:
5064           if (h != NULL && !bfd_link_pic (info))
5065             /* We may need a copy reloc.  */
5066             h->non_got_ref = 1;
5067
5068           /* Don't propagate .opd relocs.  */
5069           if (NO_OPD_RELOCS && is_opd)
5070             break;
5071
5072           /* If we are creating a shared library, and this is a reloc
5073              against a global symbol, or a non PC relative reloc
5074              against a local symbol, then we need to copy the reloc
5075              into the shared library.  However, if we are linking with
5076              -Bsymbolic, we do not need to copy a reloc against a
5077              global symbol which is defined in an object we are
5078              including in the link (i.e., DEF_REGULAR is set).  At
5079              this point we have not seen all the input files, so it is
5080              possible that DEF_REGULAR is not set now but will be set
5081              later (it is never cleared).  In case of a weak definition,
5082              DEF_REGULAR may be cleared later by a strong definition in
5083              a shared library.  We account for that possibility below by
5084              storing information in the dyn_relocs field of the hash
5085              table entry.  A similar situation occurs when creating
5086              shared libraries and symbol visibility changes render the
5087              symbol local.
5088
5089              If on the other hand, we are creating an executable, we
5090              may need to keep relocations for symbols satisfied by a
5091              dynamic library if we manage to avoid copy relocs for the
5092              symbol.  */
5093         dodyn:
5094           if ((bfd_link_pic (info)
5095                && (must_be_dyn_reloc (info, r_type)
5096                    || (h != NULL
5097                        && (!SYMBOLIC_BIND (info, h)
5098                            || h->root.type == bfd_link_hash_defweak
5099                            || !h->def_regular))))
5100               || (ELIMINATE_COPY_RELOCS
5101                   && !bfd_link_pic (info)
5102                   && h != NULL
5103                   && (h->root.type == bfd_link_hash_defweak
5104                       || !h->def_regular))
5105               || (!bfd_link_pic (info)
5106                   && ifunc != NULL))
5107             {
5108               /* We must copy these reloc types into the output file.
5109                  Create a reloc section in dynobj and make room for
5110                  this reloc.  */
5111               if (sreloc == NULL)
5112                 {
5113                   sreloc = _bfd_elf_make_dynamic_reloc_section
5114                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5115
5116                   if (sreloc == NULL)
5117                     return FALSE;
5118                 }
5119
5120               /* If this is a global symbol, we count the number of
5121                  relocations we need for this symbol.  */
5122               if (h != NULL)
5123                 {
5124                   struct elf_dyn_relocs *p;
5125                   struct elf_dyn_relocs **head;
5126
5127                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5128                   p = *head;
5129                   if (p == NULL || p->sec != sec)
5130                     {
5131                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5132                       if (p == NULL)
5133                         return FALSE;
5134                       p->next = *head;
5135                       *head = p;
5136                       p->sec = sec;
5137                       p->count = 0;
5138                       p->pc_count = 0;
5139                     }
5140                   p->count += 1;
5141                   if (!must_be_dyn_reloc (info, r_type))
5142                     p->pc_count += 1;
5143                 }
5144               else
5145                 {
5146                   /* Track dynamic relocs needed for local syms too.
5147                      We really need local syms available to do this
5148                      easily.  Oh well.  */
5149                   struct ppc_dyn_relocs *p;
5150                   struct ppc_dyn_relocs **head;
5151                   bfd_boolean is_ifunc;
5152                   asection *s;
5153                   void *vpp;
5154                   Elf_Internal_Sym *isym;
5155
5156                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5157                                                 abfd, r_symndx);
5158                   if (isym == NULL)
5159                     return FALSE;
5160
5161                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5162                   if (s == NULL)
5163                     s = sec;
5164
5165                   vpp = &elf_section_data (s)->local_dynrel;
5166                   head = (struct ppc_dyn_relocs **) vpp;
5167                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5168                   p = *head;
5169                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5170                     p = p->next;
5171                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5172                     {
5173                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5174                       if (p == NULL)
5175                         return FALSE;
5176                       p->next = *head;
5177                       *head = p;
5178                       p->sec = sec;
5179                       p->ifunc = is_ifunc;
5180                       p->count = 0;
5181                     }
5182                   p->count += 1;
5183                 }
5184             }
5185           break;
5186
5187         default:
5188           break;
5189         }
5190     }
5191
5192   return TRUE;
5193 }
5194
5195 /* Merge backend specific data from an object file to the output
5196    object file when linking.  */
5197
5198 static bfd_boolean
5199 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5200 {
5201   bfd *obfd = info->output_bfd;
5202   unsigned long iflags, oflags;
5203
5204   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5205     return TRUE;
5206
5207   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5208     return TRUE;
5209
5210   if (!_bfd_generic_verify_endian_match (ibfd, info))
5211     return FALSE;
5212
5213   iflags = elf_elfheader (ibfd)->e_flags;
5214   oflags = elf_elfheader (obfd)->e_flags;
5215
5216   if (iflags & ~EF_PPC64_ABI)
5217     {
5218       _bfd_error_handler
5219         /* xgettext:c-format */
5220         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5221       bfd_set_error (bfd_error_bad_value);
5222       return FALSE;
5223     }
5224   else if (iflags != oflags && iflags != 0)
5225     {
5226       _bfd_error_handler
5227         /* xgettext:c-format */
5228         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5229          ibfd, iflags, oflags);
5230       bfd_set_error (bfd_error_bad_value);
5231       return FALSE;
5232     }
5233
5234   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5235     return FALSE;
5236
5237   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5238   return _bfd_elf_merge_object_attributes (ibfd, info);
5239 }
5240
5241 static bfd_boolean
5242 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5243 {
5244   /* Print normal ELF private data.  */
5245   _bfd_elf_print_private_bfd_data (abfd, ptr);
5246
5247   if (elf_elfheader (abfd)->e_flags != 0)
5248     {
5249       FILE *file = ptr;
5250
5251       fprintf (file, _("private flags = 0x%lx:"),
5252                elf_elfheader (abfd)->e_flags);
5253
5254       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5255         fprintf (file, _(" [abiv%ld]"),
5256                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5257       fputc ('\n', file);
5258     }
5259
5260   return TRUE;
5261 }
5262
5263 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5264    of the code entry point, and its section, which must be in the same
5265    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5266
5267 static bfd_vma
5268 opd_entry_value (asection *opd_sec,
5269                  bfd_vma offset,
5270                  asection **code_sec,
5271                  bfd_vma *code_off,
5272                  bfd_boolean in_code_sec)
5273 {
5274   bfd *opd_bfd = opd_sec->owner;
5275   Elf_Internal_Rela *relocs;
5276   Elf_Internal_Rela *lo, *hi, *look;
5277   bfd_vma val;
5278
5279   /* No relocs implies we are linking a --just-symbols object, or looking
5280      at a final linked executable with addr2line or somesuch.  */
5281   if (opd_sec->reloc_count == 0)
5282     {
5283       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5284
5285       if (contents == NULL)
5286         {
5287           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5288             return (bfd_vma) -1;
5289           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5290         }
5291
5292       /* PR 17512: file: 64b9dfbb.  */
5293       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5294         return (bfd_vma) -1;
5295
5296       val = bfd_get_64 (opd_bfd, contents + offset);
5297       if (code_sec != NULL)
5298         {
5299           asection *sec, *likely = NULL;
5300
5301           if (in_code_sec)
5302             {
5303               sec = *code_sec;
5304               if (sec->vma <= val
5305                   && val < sec->vma + sec->size)
5306                 likely = sec;
5307               else
5308                 val = -1;
5309             }
5310           else
5311             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5312               if (sec->vma <= val
5313                   && (sec->flags & SEC_LOAD) != 0
5314                   && (sec->flags & SEC_ALLOC) != 0)
5315                 likely = sec;
5316           if (likely != NULL)
5317             {
5318               *code_sec = likely;
5319               if (code_off != NULL)
5320                 *code_off = val - likely->vma;
5321             }
5322         }
5323       return val;
5324     }
5325
5326   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5327
5328   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5329   if (relocs == NULL)
5330     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5331   /* PR 17512: file: df8e1fd6.  */
5332   if (relocs == NULL)
5333     return (bfd_vma) -1;
5334
5335   /* Go find the opd reloc at the sym address.  */
5336   lo = relocs;
5337   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5338   val = (bfd_vma) -1;
5339   while (lo < hi)
5340     {
5341       look = lo + (hi - lo) / 2;
5342       if (look->r_offset < offset)
5343         lo = look + 1;
5344       else if (look->r_offset > offset)
5345         hi = look;
5346       else
5347         {
5348           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5349
5350           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5351               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5352             {
5353               unsigned long symndx = ELF64_R_SYM (look->r_info);
5354               asection *sec = NULL;
5355
5356               if (symndx >= symtab_hdr->sh_info
5357                   && elf_sym_hashes (opd_bfd) != NULL)
5358                 {
5359                   struct elf_link_hash_entry **sym_hashes;
5360                   struct elf_link_hash_entry *rh;
5361
5362                   sym_hashes = elf_sym_hashes (opd_bfd);
5363                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5364                   if (rh != NULL)
5365                     {
5366                       rh = elf_follow_link (rh);
5367                       if (rh->root.type != bfd_link_hash_defined
5368                           && rh->root.type != bfd_link_hash_defweak)
5369                         break;
5370                       if (rh->root.u.def.section->owner == opd_bfd)
5371                         {
5372                           val = rh->root.u.def.value;
5373                           sec = rh->root.u.def.section;
5374                         }
5375                     }
5376                 }
5377
5378               if (sec == NULL)
5379                 {
5380                   Elf_Internal_Sym *sym;
5381
5382                   if (symndx < symtab_hdr->sh_info)
5383                     {
5384                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5385                       if (sym == NULL)
5386                         {
5387                           size_t symcnt = symtab_hdr->sh_info;
5388                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5389                                                       symcnt, 0,
5390                                                       NULL, NULL, NULL);
5391                           if (sym == NULL)
5392                             break;
5393                           symtab_hdr->contents = (bfd_byte *) sym;
5394                         }
5395                       sym += symndx;
5396                     }
5397                   else
5398                     {
5399                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5400                                                   1, symndx,
5401                                                   NULL, NULL, NULL);
5402                       if (sym == NULL)
5403                         break;
5404                     }
5405                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5406                   if (sec == NULL)
5407                     break;
5408                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5409                   val = sym->st_value;
5410                 }
5411
5412               val += look->r_addend;
5413               if (code_off != NULL)
5414                 *code_off = val;
5415               if (code_sec != NULL)
5416                 {
5417                   if (in_code_sec && *code_sec != sec)
5418                     return -1;
5419                   else
5420                     *code_sec = sec;
5421                 }
5422               if (sec->output_section != NULL)
5423                 val += sec->output_section->vma + sec->output_offset;
5424             }
5425           break;
5426         }
5427     }
5428
5429   return val;
5430 }
5431
5432 /* If the ELF symbol SYM might be a function in SEC, return the
5433    function size and set *CODE_OFF to the function's entry point,
5434    otherwise return zero.  */
5435
5436 static bfd_size_type
5437 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5438                               bfd_vma *code_off)
5439 {
5440   bfd_size_type size;
5441
5442   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5443                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5444     return 0;
5445
5446   size = 0;
5447   if (!(sym->flags & BSF_SYNTHETIC))
5448     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5449
5450   if (strcmp (sym->section->name, ".opd") == 0)
5451     {
5452       struct _opd_sec_data *opd = get_opd_info (sym->section);
5453       bfd_vma symval = sym->value;
5454
5455       if (opd != NULL
5456           && opd->adjust != NULL
5457           && elf_section_data (sym->section)->relocs != NULL)
5458         {
5459           /* opd_entry_value will use cached relocs that have been
5460              adjusted, but with raw symbols.  That means both local
5461              and global symbols need adjusting.  */
5462           long adjust = opd->adjust[OPD_NDX (symval)];
5463           if (adjust == -1)
5464             return 0;
5465           symval += adjust;
5466         }
5467
5468       if (opd_entry_value (sym->section, symval,
5469                            &sec, code_off, TRUE) == (bfd_vma) -1)
5470         return 0;
5471       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5472          symbol.  This size has nothing to do with the code size of the
5473          function, which is what we're supposed to return, but the
5474          code size isn't available without looking up the dot-sym.
5475          However, doing that would be a waste of time particularly
5476          since elf_find_function will look at the dot-sym anyway.
5477          Now, elf_find_function will keep the largest size of any
5478          function sym found at the code address of interest, so return
5479          1 here to avoid it incorrectly caching a larger function size
5480          for a small function.  This does mean we return the wrong
5481          size for a new-ABI function of size 24, but all that does is
5482          disable caching for such functions.  */
5483       if (size == 24)
5484         size = 1;
5485     }
5486   else
5487     {
5488       if (sym->section != sec)
5489         return 0;
5490       *code_off = sym->value;
5491     }
5492   if (size == 0)
5493     size = 1;
5494   return size;
5495 }
5496
5497 /* Return true if symbol is a strong function defined in an ELFv2
5498    object with st_other localentry bits of zero, ie. its local entry
5499    point coincides with its global entry point.  */
5500
5501 static bfd_boolean
5502 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5503 {
5504   return (h != NULL
5505           && h->type == STT_FUNC
5506           && h->root.type == bfd_link_hash_defined
5507           && (STO_PPC64_LOCAL_MASK & h->other) == 0
5508           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5509           && is_ppc64_elf (h->root.u.def.section->owner)
5510           && abiversion (h->root.u.def.section->owner) >= 2);
5511 }
5512
5513 /* Return true if symbol is defined in a regular object file.  */
5514
5515 static bfd_boolean
5516 is_static_defined (struct elf_link_hash_entry *h)
5517 {
5518   return ((h->root.type == bfd_link_hash_defined
5519            || h->root.type == bfd_link_hash_defweak)
5520           && h->root.u.def.section != NULL
5521           && h->root.u.def.section->output_section != NULL);
5522 }
5523
5524 /* If FDH is a function descriptor symbol, return the associated code
5525    entry symbol if it is defined.  Return NULL otherwise.  */
5526
5527 static struct ppc_link_hash_entry *
5528 defined_code_entry (struct ppc_link_hash_entry *fdh)
5529 {
5530   if (fdh->is_func_descriptor)
5531     {
5532       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5533       if (fh->elf.root.type == bfd_link_hash_defined
5534           || fh->elf.root.type == bfd_link_hash_defweak)
5535         return fh;
5536     }
5537   return NULL;
5538 }
5539
5540 /* If FH is a function code entry symbol, return the associated
5541    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5542
5543 static struct ppc_link_hash_entry *
5544 defined_func_desc (struct ppc_link_hash_entry *fh)
5545 {
5546   if (fh->oh != NULL
5547       && fh->oh->is_func_descriptor)
5548     {
5549       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5550       if (fdh->elf.root.type == bfd_link_hash_defined
5551           || fdh->elf.root.type == bfd_link_hash_defweak)
5552         return fdh;
5553     }
5554   return NULL;
5555 }
5556
5557 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5558
5559 /* Garbage collect sections, after first dealing with dot-symbols.  */
5560
5561 static bfd_boolean
5562 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5563 {
5564   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5565
5566   if (htab != NULL && htab->need_func_desc_adj)
5567     {
5568       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5569       htab->need_func_desc_adj = 0;
5570     }
5571   return bfd_elf_gc_sections (abfd, info);
5572 }
5573
5574 /* Mark all our entry sym sections, both opd and code section.  */
5575
5576 static void
5577 ppc64_elf_gc_keep (struct bfd_link_info *info)
5578 {
5579   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5580   struct bfd_sym_chain *sym;
5581
5582   if (htab == NULL)
5583     return;
5584
5585   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5586     {
5587       struct ppc_link_hash_entry *eh, *fh;
5588       asection *sec;
5589
5590       eh = (struct ppc_link_hash_entry *)
5591         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5592       if (eh == NULL)
5593         continue;
5594       if (eh->elf.root.type != bfd_link_hash_defined
5595           && eh->elf.root.type != bfd_link_hash_defweak)
5596         continue;
5597
5598       fh = defined_code_entry (eh);
5599       if (fh != NULL)
5600         {
5601           sec = fh->elf.root.u.def.section;
5602           sec->flags |= SEC_KEEP;
5603         }
5604       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5605                && opd_entry_value (eh->elf.root.u.def.section,
5606                                    eh->elf.root.u.def.value,
5607                                    &sec, NULL, FALSE) != (bfd_vma) -1)
5608         sec->flags |= SEC_KEEP;
5609
5610       sec = eh->elf.root.u.def.section;
5611       sec->flags |= SEC_KEEP;
5612     }
5613 }
5614
5615 /* Mark sections containing dynamically referenced symbols.  When
5616    building shared libraries, we must assume that any visible symbol is
5617    referenced.  */
5618
5619 static bfd_boolean
5620 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5621 {
5622   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5623   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5624   struct ppc_link_hash_entry *fdh;
5625   struct bfd_elf_dynamic_list *d = info->dynamic_list;
5626
5627   /* Dynamic linking info is on the func descriptor sym.  */
5628   fdh = defined_func_desc (eh);
5629   if (fdh != NULL)
5630     eh = fdh;
5631
5632   if ((eh->elf.root.type == bfd_link_hash_defined
5633        || eh->elf.root.type == bfd_link_hash_defweak)
5634       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5635           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5636               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5637               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5638               && (!bfd_link_executable (info)
5639                   || info->gc_keep_exported
5640                   || info->export_dynamic
5641                   || (eh->elf.dynamic
5642                       && d != NULL
5643                       && (*d->match) (&d->head, NULL,
5644                                       eh->elf.root.root.string)))
5645               && (eh->elf.versioned >= versioned
5646                   || !bfd_hide_sym_by_version (info->version_info,
5647                                                eh->elf.root.root.string)))))
5648     {
5649       asection *code_sec;
5650       struct ppc_link_hash_entry *fh;
5651
5652       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5653
5654       /* Function descriptor syms cause the associated
5655          function code sym section to be marked.  */
5656       fh = defined_code_entry (eh);
5657       if (fh != NULL)
5658         {
5659           code_sec = fh->elf.root.u.def.section;
5660           code_sec->flags |= SEC_KEEP;
5661         }
5662       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5663                && opd_entry_value (eh->elf.root.u.def.section,
5664                                    eh->elf.root.u.def.value,
5665                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
5666         code_sec->flags |= SEC_KEEP;
5667     }
5668
5669   return TRUE;
5670 }
5671
5672 /* Return the section that should be marked against GC for a given
5673    relocation.  */
5674
5675 static asection *
5676 ppc64_elf_gc_mark_hook (asection *sec,
5677                         struct bfd_link_info *info,
5678                         Elf_Internal_Rela *rel,
5679                         struct elf_link_hash_entry *h,
5680                         Elf_Internal_Sym *sym)
5681 {
5682   asection *rsec;
5683
5684   /* Syms return NULL if we're marking .opd, so we avoid marking all
5685      function sections, as all functions are referenced in .opd.  */
5686   rsec = NULL;
5687   if (get_opd_info (sec) != NULL)
5688     return rsec;
5689
5690   if (h != NULL)
5691     {
5692       enum elf_ppc64_reloc_type r_type;
5693       struct ppc_link_hash_entry *eh, *fh, *fdh;
5694
5695       r_type = ELF64_R_TYPE (rel->r_info);
5696       switch (r_type)
5697         {
5698         case R_PPC64_GNU_VTINHERIT:
5699         case R_PPC64_GNU_VTENTRY:
5700           break;
5701
5702         default:
5703           switch (h->root.type)
5704             {
5705             case bfd_link_hash_defined:
5706             case bfd_link_hash_defweak:
5707               eh = (struct ppc_link_hash_entry *) h;
5708               fdh = defined_func_desc (eh);
5709               if (fdh != NULL)
5710                 {
5711                   /* -mcall-aixdesc code references the dot-symbol on
5712                      a call reloc.  Mark the function descriptor too
5713                      against garbage collection.  */
5714                   fdh->elf.mark = 1;
5715                   if (fdh->elf.is_weakalias)
5716                     weakdef (&fdh->elf)->mark = 1;
5717                   eh = fdh;
5718                 }
5719
5720               /* Function descriptor syms cause the associated
5721                  function code sym section to be marked.  */
5722               fh = defined_code_entry (eh);
5723               if (fh != NULL)
5724                 {
5725                   /* They also mark their opd section.  */
5726                   eh->elf.root.u.def.section->gc_mark = 1;
5727
5728                   rsec = fh->elf.root.u.def.section;
5729                 }
5730               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5731                        && opd_entry_value (eh->elf.root.u.def.section,
5732                                            eh->elf.root.u.def.value,
5733                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
5734                 eh->elf.root.u.def.section->gc_mark = 1;
5735               else
5736                 rsec = h->root.u.def.section;
5737               break;
5738
5739             case bfd_link_hash_common:
5740               rsec = h->root.u.c.p->section;
5741               break;
5742
5743             default:
5744               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5745             }
5746         }
5747     }
5748   else
5749     {
5750       struct _opd_sec_data *opd;
5751
5752       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5753       opd = get_opd_info (rsec);
5754       if (opd != NULL && opd->func_sec != NULL)
5755         {
5756           rsec->gc_mark = 1;
5757
5758           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5759         }
5760     }
5761
5762   return rsec;
5763 }
5764
5765 /* The maximum size of .sfpr.  */
5766 #define SFPR_MAX (218*4)
5767
5768 struct sfpr_def_parms
5769 {
5770   const char name[12];
5771   unsigned char lo, hi;
5772   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5773   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5774 };
5775
5776 /* Auto-generate _save*, _rest* functions in .sfpr.
5777    If STUB_SEC is non-null, define alias symbols in STUB_SEC
5778    instead.  */
5779
5780 static bfd_boolean
5781 sfpr_define (struct bfd_link_info *info,
5782              const struct sfpr_def_parms *parm,
5783              asection *stub_sec)
5784 {
5785   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5786   unsigned int i;
5787   size_t len = strlen (parm->name);
5788   bfd_boolean writing = FALSE;
5789   char sym[16];
5790
5791   if (htab == NULL)
5792     return FALSE;
5793
5794   memcpy (sym, parm->name, len);
5795   sym[len + 2] = 0;
5796
5797   for (i = parm->lo; i <= parm->hi; i++)
5798     {
5799       struct ppc_link_hash_entry *h;
5800
5801       sym[len + 0] = i / 10 + '0';
5802       sym[len + 1] = i % 10 + '0';
5803       h = (struct ppc_link_hash_entry *)
5804         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5805       if (stub_sec != NULL)
5806         {
5807           if (h != NULL
5808               && h->elf.root.type == bfd_link_hash_defined
5809               && h->elf.root.u.def.section == htab->sfpr)
5810             {
5811               struct elf_link_hash_entry *s;
5812               char buf[32];
5813               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5814               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5815               if (s == NULL)
5816                 return FALSE;
5817               if (s->root.type == bfd_link_hash_new
5818                   || (s->root.type = bfd_link_hash_defined
5819                       && s->root.u.def.section == stub_sec))
5820                 {
5821                   s->root.type = bfd_link_hash_defined;
5822                   s->root.u.def.section = stub_sec;
5823                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5824                                          + h->elf.root.u.def.value);
5825                   s->ref_regular = 1;
5826                   s->def_regular = 1;
5827                   s->ref_regular_nonweak = 1;
5828                   s->forced_local = 1;
5829                   s->non_elf = 0;
5830                   s->root.linker_def = 1;
5831                 }
5832             }
5833           continue;
5834         }
5835       if (h != NULL)
5836         {
5837           h->save_res = 1;
5838           if (!h->elf.def_regular)
5839             {
5840               h->elf.root.type = bfd_link_hash_defined;
5841               h->elf.root.u.def.section = htab->sfpr;
5842               h->elf.root.u.def.value = htab->sfpr->size;
5843               h->elf.type = STT_FUNC;
5844               h->elf.def_regular = 1;
5845               h->elf.non_elf = 0;
5846               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5847               writing = TRUE;
5848               if (htab->sfpr->contents == NULL)
5849                 {
5850                   htab->sfpr->contents
5851                     = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5852                   if (htab->sfpr->contents == NULL)
5853                     return FALSE;
5854                 }
5855             }
5856         }
5857       if (writing)
5858         {
5859           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5860           if (i != parm->hi)
5861             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5862           else
5863             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5864           htab->sfpr->size = p - htab->sfpr->contents;
5865         }
5866     }
5867
5868   return TRUE;
5869 }
5870
5871 static bfd_byte *
5872 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5873 {
5874   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5875   return p + 4;
5876 }
5877
5878 static bfd_byte *
5879 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5880 {
5881   p = savegpr0 (abfd, p, r);
5882   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5883   p = p + 4;
5884   bfd_put_32 (abfd, BLR, p);
5885   return p + 4;
5886 }
5887
5888 static bfd_byte *
5889 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5890 {
5891   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5892   return p + 4;
5893 }
5894
5895 static bfd_byte *
5896 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5897 {
5898   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5899   p = p + 4;
5900   p = restgpr0 (abfd, p, r);
5901   bfd_put_32 (abfd, MTLR_R0, p);
5902   p = p + 4;
5903   if (r == 29)
5904     {
5905       p = restgpr0 (abfd, p, 30);
5906       p = restgpr0 (abfd, p, 31);
5907     }
5908   bfd_put_32 (abfd, BLR, p);
5909   return p + 4;
5910 }
5911
5912 static bfd_byte *
5913 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5914 {
5915   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5916   return p + 4;
5917 }
5918
5919 static bfd_byte *
5920 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5921 {
5922   p = savegpr1 (abfd, p, r);
5923   bfd_put_32 (abfd, BLR, p);
5924   return p + 4;
5925 }
5926
5927 static bfd_byte *
5928 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5929 {
5930   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5931   return p + 4;
5932 }
5933
5934 static bfd_byte *
5935 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5936 {
5937   p = restgpr1 (abfd, p, r);
5938   bfd_put_32 (abfd, BLR, p);
5939   return p + 4;
5940 }
5941
5942 static bfd_byte *
5943 savefpr (bfd *abfd, bfd_byte *p, int r)
5944 {
5945   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5946   return p + 4;
5947 }
5948
5949 static bfd_byte *
5950 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5951 {
5952   p = savefpr (abfd, p, r);
5953   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5954   p = p + 4;
5955   bfd_put_32 (abfd, BLR, p);
5956   return p + 4;
5957 }
5958
5959 static bfd_byte *
5960 restfpr (bfd *abfd, bfd_byte *p, int r)
5961 {
5962   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5963   return p + 4;
5964 }
5965
5966 static bfd_byte *
5967 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5968 {
5969   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5970   p = p + 4;
5971   p = restfpr (abfd, p, r);
5972   bfd_put_32 (abfd, MTLR_R0, p);
5973   p = p + 4;
5974   if (r == 29)
5975     {
5976       p = restfpr (abfd, p, 30);
5977       p = restfpr (abfd, p, 31);
5978     }
5979   bfd_put_32 (abfd, BLR, p);
5980   return p + 4;
5981 }
5982
5983 static bfd_byte *
5984 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5985 {
5986   p = savefpr (abfd, p, r);
5987   bfd_put_32 (abfd, BLR, p);
5988   return p + 4;
5989 }
5990
5991 static bfd_byte *
5992 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5993 {
5994   p = restfpr (abfd, p, r);
5995   bfd_put_32 (abfd, BLR, p);
5996   return p + 4;
5997 }
5998
5999 static bfd_byte *
6000 savevr (bfd *abfd, bfd_byte *p, int r)
6001 {
6002   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6003   p = p + 4;
6004   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6005   return p + 4;
6006 }
6007
6008 static bfd_byte *
6009 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6010 {
6011   p = savevr (abfd, p, r);
6012   bfd_put_32 (abfd, BLR, p);
6013   return p + 4;
6014 }
6015
6016 static bfd_byte *
6017 restvr (bfd *abfd, bfd_byte *p, int r)
6018 {
6019   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6020   p = p + 4;
6021   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6022   return p + 4;
6023 }
6024
6025 static bfd_byte *
6026 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6027 {
6028   p = restvr (abfd, p, r);
6029   bfd_put_32 (abfd, BLR, p);
6030   return p + 4;
6031 }
6032
6033 /* Called via elf_link_hash_traverse to transfer dynamic linking
6034    information on function code symbol entries to their corresponding
6035    function descriptor symbol entries.  */
6036
6037 static bfd_boolean
6038 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6039 {
6040   struct bfd_link_info *info;
6041   struct ppc_link_hash_table *htab;
6042   struct ppc_link_hash_entry *fh;
6043   struct ppc_link_hash_entry *fdh;
6044   bfd_boolean force_local;
6045
6046   fh = (struct ppc_link_hash_entry *) h;
6047   if (fh->elf.root.type == bfd_link_hash_indirect)
6048     return TRUE;
6049
6050   if (!fh->is_func)
6051     return TRUE;
6052
6053   if (fh->elf.root.root.string[0] != '.'
6054       || fh->elf.root.root.string[1] == '\0')
6055     return TRUE;
6056
6057   info = inf;
6058   htab = ppc_hash_table (info);
6059   if (htab == NULL)
6060     return FALSE;
6061
6062   /* Find the corresponding function descriptor symbol.  */
6063   fdh = lookup_fdh (fh, htab);
6064
6065   /* Resolve undefined references to dot-symbols as the value
6066      in the function descriptor, if we have one in a regular object.
6067      This is to satisfy cases like ".quad .foo".  Calls to functions
6068      in dynamic objects are handled elsewhere.  */
6069   if ((fh->elf.root.type == bfd_link_hash_undefined
6070        || fh->elf.root.type == bfd_link_hash_undefweak)
6071       && (fdh->elf.root.type == bfd_link_hash_defined
6072           || fdh->elf.root.type == bfd_link_hash_defweak)
6073       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6074       && opd_entry_value (fdh->elf.root.u.def.section,
6075                           fdh->elf.root.u.def.value,
6076                           &fh->elf.root.u.def.section,
6077                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6078     {
6079       fh->elf.root.type = fdh->elf.root.type;
6080       fh->elf.forced_local = 1;
6081       fh->elf.def_regular = fdh->elf.def_regular;
6082       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6083     }
6084
6085   if (!fh->elf.dynamic)
6086     {
6087       struct plt_entry *ent;
6088
6089       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6090         if (ent->plt.refcount > 0)
6091           break;
6092       if (ent == NULL)
6093         return TRUE;
6094     }
6095
6096   /* Create a descriptor as undefined if necessary.  */
6097   if (fdh == NULL
6098       && !bfd_link_executable (info)
6099       && (fh->elf.root.type == bfd_link_hash_undefined
6100           || fh->elf.root.type == bfd_link_hash_undefweak))
6101     {
6102       fdh = make_fdh (info, fh);
6103       if (fdh == NULL)
6104         return FALSE;
6105     }
6106
6107   /* We can't support overriding of symbols on a fake descriptor.  */
6108   if (fdh != NULL
6109       && fdh->fake
6110       && (fh->elf.root.type == bfd_link_hash_defined
6111           || fh->elf.root.type == bfd_link_hash_defweak))
6112     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6113
6114   /* Transfer dynamic linking information to the function descriptor.  */
6115   if (fdh != NULL)
6116     {
6117       fdh->elf.ref_regular |= fh->elf.ref_regular;
6118       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6119       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6120       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6121       fdh->elf.dynamic |= fh->elf.dynamic;
6122       fdh->elf.needs_plt |= (fh->elf.needs_plt
6123                              || fh->elf.type == STT_FUNC
6124                              || fh->elf.type == STT_GNU_IFUNC);
6125       move_plt_plist (fh, fdh);
6126
6127       if (!fdh->elf.forced_local
6128           && fh->elf.dynindx != -1)
6129         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6130           return FALSE;
6131     }
6132
6133   /* Now that the info is on the function descriptor, clear the
6134      function code sym info.  Any function code syms for which we
6135      don't have a definition in a regular file, we force local.
6136      This prevents a shared library from exporting syms that have
6137      been imported from another library.  Function code syms that
6138      are really in the library we must leave global to prevent the
6139      linker dragging in a definition from a static library.  */
6140   force_local = (!fh->elf.def_regular
6141                  || fdh == NULL
6142                  || !fdh->elf.def_regular
6143                  || fdh->elf.forced_local);
6144   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6145
6146   return TRUE;
6147 }
6148
6149 static const struct sfpr_def_parms save_res_funcs[] =
6150   {
6151     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6152     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6153     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6154     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6155     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6156     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6157     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6158     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6159     { "._savef", 14, 31, savefpr, savefpr1_tail },
6160     { "._restf", 14, 31, restfpr, restfpr1_tail },
6161     { "_savevr_", 20, 31, savevr, savevr_tail },
6162     { "_restvr_", 20, 31, restvr, restvr_tail }
6163   };
6164
6165 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6166    this hook to a) provide some gcc support functions, and b) transfer
6167    dynamic linking information gathered so far on function code symbol
6168    entries, to their corresponding function descriptor symbol entries.  */
6169
6170 static bfd_boolean
6171 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6172                             struct bfd_link_info *info)
6173 {
6174   struct ppc_link_hash_table *htab;
6175
6176   htab = ppc_hash_table (info);
6177   if (htab == NULL)
6178     return FALSE;
6179
6180   /* Provide any missing _save* and _rest* functions.  */
6181   if (htab->sfpr != NULL)
6182     {
6183       unsigned int i;
6184
6185       htab->sfpr->size = 0;
6186       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6187         if (!sfpr_define (info, &save_res_funcs[i], NULL))
6188           return FALSE;
6189       if (htab->sfpr->size == 0)
6190         htab->sfpr->flags |= SEC_EXCLUDE;
6191     }
6192
6193   if (bfd_link_relocatable (info))
6194     return TRUE;
6195
6196   if (htab->elf.hgot != NULL)
6197     {
6198       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6199       /* Make .TOC. defined so as to prevent it being made dynamic.
6200          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6201       if (!htab->elf.hgot->def_regular
6202           || htab->elf.hgot->root.type != bfd_link_hash_defined)
6203         {
6204           htab->elf.hgot->root.type = bfd_link_hash_defined;
6205           htab->elf.hgot->root.u.def.value = 0;
6206           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6207           htab->elf.hgot->def_regular = 1;
6208           htab->elf.hgot->root.linker_def = 1;
6209         }
6210       htab->elf.hgot->type = STT_OBJECT;
6211       htab->elf.hgot->other
6212         = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6213     }
6214
6215   if (htab->need_func_desc_adj)
6216     {
6217       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6218       htab->need_func_desc_adj = 0;
6219     }
6220
6221   return TRUE;
6222 }
6223
6224 /* Find dynamic relocs for H that apply to read-only sections.  */
6225
6226 static asection *
6227 readonly_dynrelocs (struct elf_link_hash_entry *h)
6228 {
6229   struct ppc_link_hash_entry *eh;
6230   struct elf_dyn_relocs *p;
6231
6232   eh = (struct ppc_link_hash_entry *) h;
6233   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6234     {
6235       asection *s = p->sec->output_section;
6236
6237       if (s != NULL && (s->flags & SEC_READONLY) != 0)
6238         return p->sec;
6239     }
6240   return NULL;
6241 }
6242
6243 /* Return true if we have dynamic relocs against H or any of its weak
6244    aliases, that apply to read-only sections.  Cannot be used after
6245    size_dynamic_sections.  */
6246
6247 static bfd_boolean
6248 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6249 {
6250   struct ppc_link_hash_entry *eh;
6251
6252   eh = (struct ppc_link_hash_entry *) h;
6253   do
6254     {
6255       if (readonly_dynrelocs (&eh->elf))
6256         return TRUE;
6257       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
6258     }
6259   while (eh != NULL && &eh->elf != h);
6260
6261   return FALSE;
6262 }
6263
6264 /* Return whether EH has pc-relative dynamic relocs.  */
6265
6266 static bfd_boolean
6267 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6268 {
6269   struct elf_dyn_relocs *p;
6270
6271   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6272     if (p->pc_count != 0)
6273       return TRUE;
6274   return FALSE;
6275 }
6276
6277 /* Return true if a global entry stub will be created for H.  Valid
6278    for ELFv2 before plt entries have been allocated.  */
6279
6280 static bfd_boolean
6281 global_entry_stub (struct elf_link_hash_entry *h)
6282 {
6283   struct plt_entry *pent;
6284
6285   if (!h->pointer_equality_needed
6286       || h->def_regular)
6287     return FALSE;
6288
6289   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6290     if (pent->plt.refcount > 0
6291         && pent->addend == 0)
6292       return TRUE;
6293
6294   return FALSE;
6295 }
6296
6297 /* Adjust a symbol defined by a dynamic object and referenced by a
6298    regular object.  The current definition is in some section of the
6299    dynamic object, but we're not including those sections.  We have to
6300    change the definition to something the rest of the link can
6301    understand.  */
6302
6303 static bfd_boolean
6304 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6305                                  struct elf_link_hash_entry *h)
6306 {
6307   struct ppc_link_hash_table *htab;
6308   asection *s, *srel;
6309
6310   htab = ppc_hash_table (info);
6311   if (htab == NULL)
6312     return FALSE;
6313
6314   /* Deal with function syms.  */
6315   if (h->type == STT_FUNC
6316       || h->type == STT_GNU_IFUNC
6317       || h->needs_plt)
6318     {
6319       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
6320                            || SYMBOL_CALLS_LOCAL (info, h)
6321                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6322       /* Discard dyn_relocs when non-pic if we've decided that a
6323          function symbol is local and not an ifunc.  We keep dynamic
6324          relocs for ifuncs when local rather than always emitting a
6325          plt call stub for them and defining the symbol on the call
6326          stub.  We can't do that for ELFv1 anyway (a function symbol
6327          is defined on a descriptor, not code) and it can be faster at
6328          run-time due to not needing to bounce through a stub.  The
6329          dyn_relocs for ifuncs will be applied even in a static
6330          executable.  */
6331       if (!bfd_link_pic (info)
6332           && h->type != STT_GNU_IFUNC
6333           && local)
6334         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6335
6336       /* Clear procedure linkage table information for any symbol that
6337          won't need a .plt entry.  */
6338       struct plt_entry *ent;
6339       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6340         if (ent->plt.refcount > 0)
6341           break;
6342       if (ent == NULL
6343           || (h->type != STT_GNU_IFUNC
6344               && local
6345               && (htab->can_convert_all_inline_plt
6346                   || (((struct ppc_link_hash_entry *) h)->tls_mask
6347                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6348         {
6349           h->plt.plist = NULL;
6350           h->needs_plt = 0;
6351           h->pointer_equality_needed = 0;
6352         }
6353       else if (abiversion (info->output_bfd) >= 2)
6354         {
6355           /* Taking a function's address in a read/write section
6356              doesn't require us to define the function symbol in the
6357              executable on a global entry stub.  A dynamic reloc can
6358              be used instead.  The reason we prefer a few more dynamic
6359              relocs is that calling via a global entry stub costs a
6360              few more instructions, and pointer_equality_needed causes
6361              extra work in ld.so when resolving these symbols.  */
6362           if (global_entry_stub (h))
6363             {
6364               if (!readonly_dynrelocs (h))
6365                 {
6366                   h->pointer_equality_needed = 0;
6367                   /* If we haven't seen a branch reloc and the symbol
6368                      isn't an ifunc then we don't need a plt entry.  */
6369                   if (!h->needs_plt)
6370                     h->plt.plist = NULL;
6371                 }
6372               else if (!bfd_link_pic (info))
6373                 /* We are going to be defining the function symbol on the
6374                    plt stub, so no dyn_relocs needed when non-pic.  */
6375                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6376             }
6377
6378           /* ELFv2 function symbols can't have copy relocs.  */
6379           return TRUE;
6380         }
6381       else if (!h->needs_plt
6382                && !readonly_dynrelocs (h))
6383         {
6384           /* If we haven't seen a branch reloc and the symbol isn't an
6385              ifunc then we don't need a plt entry.  */
6386           h->plt.plist = NULL;
6387           h->pointer_equality_needed = 0;
6388           return TRUE;
6389         }
6390     }
6391   else
6392     h->plt.plist = NULL;
6393
6394   /* If this is a weak symbol, and there is a real definition, the
6395      processor independent code will have arranged for us to see the
6396      real definition first, and we can just use the same value.  */
6397   if (h->is_weakalias)
6398     {
6399       struct elf_link_hash_entry *def = weakdef (h);
6400       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6401       h->root.u.def.section = def->root.u.def.section;
6402       h->root.u.def.value = def->root.u.def.value;
6403       if (def->root.u.def.section == htab->elf.sdynbss
6404           || def->root.u.def.section == htab->elf.sdynrelro)
6405         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6406       return TRUE;
6407     }
6408
6409   /* If we are creating a shared library, we must presume that the
6410      only references to the symbol are via the global offset table.
6411      For such cases we need not do anything here; the relocations will
6412      be handled correctly by relocate_section.  */
6413   if (bfd_link_pic (info))
6414     return TRUE;
6415
6416   /* If there are no references to this symbol that do not use the
6417      GOT, we don't need to generate a copy reloc.  */
6418   if (!h->non_got_ref)
6419     return TRUE;
6420
6421   /* Don't generate a copy reloc for symbols defined in the executable.  */
6422   if (!h->def_dynamic || !h->ref_regular || h->def_regular
6423
6424       /* If -z nocopyreloc was given, don't generate them either.  */
6425       || info->nocopyreloc
6426
6427       /* If we don't find any dynamic relocs in read-only sections, then
6428          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6429       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6430
6431       /* Protected variables do not work with .dynbss.  The copy in
6432          .dynbss won't be used by the shared library with the protected
6433          definition for the variable.  Text relocations are preferable
6434          to an incorrect program.  */
6435       || h->protected_def)
6436     return TRUE;
6437
6438   if (h->plt.plist != NULL)
6439     {
6440       /* We should never get here, but unfortunately there are versions
6441          of gcc out there that improperly (for this ABI) put initialized
6442          function pointers, vtable refs and suchlike in read-only
6443          sections.  Allow them to proceed, but warn that this might
6444          break at runtime.  */
6445       info->callbacks->einfo
6446         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6447            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6448          h->root.root.string);
6449     }
6450
6451   /* This is a reference to a symbol defined by a dynamic object which
6452      is not a function.  */
6453
6454   /* We must allocate the symbol in our .dynbss section, which will
6455      become part of the .bss section of the executable.  There will be
6456      an entry for this symbol in the .dynsym section.  The dynamic
6457      object will contain position independent code, so all references
6458      from the dynamic object to this symbol will go through the global
6459      offset table.  The dynamic linker will use the .dynsym entry to
6460      determine the address it must put in the global offset table, so
6461      both the dynamic object and the regular object will refer to the
6462      same memory location for the variable.  */
6463   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6464     {
6465       s = htab->elf.sdynrelro;
6466       srel = htab->elf.sreldynrelro;
6467     }
6468   else
6469     {
6470       s = htab->elf.sdynbss;
6471       srel = htab->elf.srelbss;
6472     }
6473   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6474     {
6475       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6476          linker to copy the initial value out of the dynamic object
6477          and into the runtime process image.  */
6478       srel->size += sizeof (Elf64_External_Rela);
6479       h->needs_copy = 1;
6480     }
6481
6482   /* We no longer want dyn_relocs.  */
6483   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6484   return _bfd_elf_adjust_dynamic_copy (info, h, s);
6485 }
6486
6487 /* If given a function descriptor symbol, hide both the function code
6488    sym and the descriptor.  */
6489 static void
6490 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6491                        struct elf_link_hash_entry *h,
6492                        bfd_boolean force_local)
6493 {
6494   struct ppc_link_hash_entry *eh;
6495   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6496
6497   if (ppc_hash_table (info) == NULL)
6498     return;
6499
6500   eh = (struct ppc_link_hash_entry *) h;
6501   if (eh->is_func_descriptor)
6502     {
6503       struct ppc_link_hash_entry *fh = eh->oh;
6504
6505       if (fh == NULL)
6506         {
6507           const char *p, *q;
6508           struct elf_link_hash_table *htab = elf_hash_table (info);
6509           char save;
6510
6511           /* We aren't supposed to use alloca in BFD because on
6512              systems which do not have alloca the version in libiberty
6513              calls xmalloc, which might cause the program to crash
6514              when it runs out of memory.  This function doesn't have a
6515              return status, so there's no way to gracefully return an
6516              error.  So cheat.  We know that string[-1] can be safely
6517              accessed;  It's either a string in an ELF string table,
6518              or allocated in an objalloc structure.  */
6519
6520           p = eh->elf.root.root.string - 1;
6521           save = *p;
6522           *(char *) p = '.';
6523           fh = (struct ppc_link_hash_entry *)
6524             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6525           *(char *) p = save;
6526
6527           /* Unfortunately, if it so happens that the string we were
6528              looking for was allocated immediately before this string,
6529              then we overwrote the string terminator.  That's the only
6530              reason the lookup should fail.  */
6531           if (fh == NULL)
6532             {
6533               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6534               while (q >= eh->elf.root.root.string && *q == *p)
6535                 --q, --p;
6536               if (q < eh->elf.root.root.string && *p == '.')
6537                 fh = (struct ppc_link_hash_entry *)
6538                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6539             }
6540           if (fh != NULL)
6541             {
6542               eh->oh = fh;
6543               fh->oh = eh;
6544             }
6545         }
6546       if (fh != NULL)
6547         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6548     }
6549 }
6550
6551 static bfd_boolean
6552 get_sym_h (struct elf_link_hash_entry **hp,
6553            Elf_Internal_Sym **symp,
6554            asection **symsecp,
6555            unsigned char **tls_maskp,
6556            Elf_Internal_Sym **locsymsp,
6557            unsigned long r_symndx,
6558            bfd *ibfd)
6559 {
6560   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6561
6562   if (r_symndx >= symtab_hdr->sh_info)
6563     {
6564       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6565       struct elf_link_hash_entry *h;
6566
6567       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6568       h = elf_follow_link (h);
6569
6570       if (hp != NULL)
6571         *hp = h;
6572
6573       if (symp != NULL)
6574         *symp = NULL;
6575
6576       if (symsecp != NULL)
6577         {
6578           asection *symsec = NULL;
6579           if (h->root.type == bfd_link_hash_defined
6580               || h->root.type == bfd_link_hash_defweak)
6581             symsec = h->root.u.def.section;
6582           *symsecp = symsec;
6583         }
6584
6585       if (tls_maskp != NULL)
6586         {
6587           struct ppc_link_hash_entry *eh;
6588
6589           eh = (struct ppc_link_hash_entry *) h;
6590           *tls_maskp = &eh->tls_mask;
6591         }
6592     }
6593   else
6594     {
6595       Elf_Internal_Sym *sym;
6596       Elf_Internal_Sym *locsyms = *locsymsp;
6597
6598       if (locsyms == NULL)
6599         {
6600           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6601           if (locsyms == NULL)
6602             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6603                                             symtab_hdr->sh_info,
6604                                             0, NULL, NULL, NULL);
6605           if (locsyms == NULL)
6606             return FALSE;
6607           *locsymsp = locsyms;
6608         }
6609       sym = locsyms + r_symndx;
6610
6611       if (hp != NULL)
6612         *hp = NULL;
6613
6614       if (symp != NULL)
6615         *symp = sym;
6616
6617       if (symsecp != NULL)
6618         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6619
6620       if (tls_maskp != NULL)
6621         {
6622           struct got_entry **lgot_ents;
6623           unsigned char *tls_mask;
6624
6625           tls_mask = NULL;
6626           lgot_ents = elf_local_got_ents (ibfd);
6627           if (lgot_ents != NULL)
6628             {
6629               struct plt_entry **local_plt = (struct plt_entry **)
6630                 (lgot_ents + symtab_hdr->sh_info);
6631               unsigned char *lgot_masks = (unsigned char *)
6632                 (local_plt + symtab_hdr->sh_info);
6633               tls_mask = &lgot_masks[r_symndx];
6634             }
6635           *tls_maskp = tls_mask;
6636         }
6637     }
6638   return TRUE;
6639 }
6640
6641 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6642    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6643    type suitable for optimization, and 1 otherwise.  */
6644
6645 static int
6646 get_tls_mask (unsigned char **tls_maskp,
6647               unsigned long *toc_symndx,
6648               bfd_vma *toc_addend,
6649               Elf_Internal_Sym **locsymsp,
6650               const Elf_Internal_Rela *rel,
6651               bfd *ibfd)
6652 {
6653   unsigned long r_symndx;
6654   int next_r;
6655   struct elf_link_hash_entry *h;
6656   Elf_Internal_Sym *sym;
6657   asection *sec;
6658   bfd_vma off;
6659
6660   r_symndx = ELF64_R_SYM (rel->r_info);
6661   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6662     return 0;
6663
6664   if ((*tls_maskp != NULL
6665        && (**tls_maskp & TLS_TLS) != 0
6666        && **tls_maskp != (TLS_TLS | TLS_MARK))
6667       || sec == NULL
6668       || ppc64_elf_section_data (sec) == NULL
6669       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6670     return 1;
6671
6672   /* Look inside a TOC section too.  */
6673   if (h != NULL)
6674     {
6675       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6676       off = h->root.u.def.value;
6677     }
6678   else
6679     off = sym->st_value;
6680   off += rel->r_addend;
6681   BFD_ASSERT (off % 8 == 0);
6682   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6683   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6684   if (toc_symndx != NULL)
6685     *toc_symndx = r_symndx;
6686   if (toc_addend != NULL)
6687     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6688   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6689     return 0;
6690   if ((h == NULL || is_static_defined (h))
6691       && (next_r == -1 || next_r == -2))
6692     return 1 - next_r;
6693   return 1;
6694 }
6695
6696 /* Find (or create) an entry in the tocsave hash table.  */
6697
6698 static struct tocsave_entry *
6699 tocsave_find (struct ppc_link_hash_table *htab,
6700               enum insert_option insert,
6701               Elf_Internal_Sym **local_syms,
6702               const Elf_Internal_Rela *irela,
6703               bfd *ibfd)
6704 {
6705   unsigned long r_indx;
6706   struct elf_link_hash_entry *h;
6707   Elf_Internal_Sym *sym;
6708   struct tocsave_entry ent, *p;
6709   hashval_t hash;
6710   struct tocsave_entry **slot;
6711
6712   r_indx = ELF64_R_SYM (irela->r_info);
6713   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6714     return NULL;
6715   if (ent.sec == NULL || ent.sec->output_section == NULL)
6716     {
6717       _bfd_error_handler
6718         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6719       return NULL;
6720     }
6721
6722   if (h != NULL)
6723     ent.offset = h->root.u.def.value;
6724   else
6725     ent.offset = sym->st_value;
6726   ent.offset += irela->r_addend;
6727
6728   hash = tocsave_htab_hash (&ent);
6729   slot = ((struct tocsave_entry **)
6730           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6731   if (slot == NULL)
6732     return NULL;
6733
6734   if (*slot == NULL)
6735     {
6736       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6737       if (p == NULL)
6738         return NULL;
6739       *p = ent;
6740       *slot = p;
6741     }
6742   return *slot;
6743 }
6744
6745 /* Adjust all global syms defined in opd sections.  In gcc generated
6746    code for the old ABI, these will already have been done.  */
6747
6748 static bfd_boolean
6749 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6750 {
6751   struct ppc_link_hash_entry *eh;
6752   asection *sym_sec;
6753   struct _opd_sec_data *opd;
6754
6755   if (h->root.type == bfd_link_hash_indirect)
6756     return TRUE;
6757
6758   if (h->root.type != bfd_link_hash_defined
6759       && h->root.type != bfd_link_hash_defweak)
6760     return TRUE;
6761
6762   eh = (struct ppc_link_hash_entry *) h;
6763   if (eh->adjust_done)
6764     return TRUE;
6765
6766   sym_sec = eh->elf.root.u.def.section;
6767   opd = get_opd_info (sym_sec);
6768   if (opd != NULL && opd->adjust != NULL)
6769     {
6770       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6771       if (adjust == -1)
6772         {
6773           /* This entry has been deleted.  */
6774           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6775           if (dsec == NULL)
6776             {
6777               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6778                 if (discarded_section (dsec))
6779                   {
6780                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6781                     break;
6782                   }
6783             }
6784           eh->elf.root.u.def.value = 0;
6785           eh->elf.root.u.def.section = dsec;
6786         }
6787       else
6788         eh->elf.root.u.def.value += adjust;
6789       eh->adjust_done = 1;
6790     }
6791   return TRUE;
6792 }
6793
6794 /* Handles decrementing dynamic reloc counts for the reloc specified by
6795    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
6796    have already been determined.  */
6797
6798 static bfd_boolean
6799 dec_dynrel_count (bfd_vma r_info,
6800                   asection *sec,
6801                   struct bfd_link_info *info,
6802                   Elf_Internal_Sym **local_syms,
6803                   struct elf_link_hash_entry *h,
6804                   Elf_Internal_Sym *sym)
6805 {
6806   enum elf_ppc64_reloc_type r_type;
6807   asection *sym_sec = NULL;
6808
6809   /* Can this reloc be dynamic?  This switch, and later tests here
6810      should be kept in sync with the code in check_relocs.  */
6811   r_type = ELF64_R_TYPE (r_info);
6812   switch (r_type)
6813     {
6814     default:
6815       return TRUE;
6816
6817     case R_PPC64_TPREL16:
6818     case R_PPC64_TPREL16_LO:
6819     case R_PPC64_TPREL16_HI:
6820     case R_PPC64_TPREL16_HA:
6821     case R_PPC64_TPREL16_DS:
6822     case R_PPC64_TPREL16_LO_DS:
6823     case R_PPC64_TPREL16_HIGH:
6824     case R_PPC64_TPREL16_HIGHA:
6825     case R_PPC64_TPREL16_HIGHER:
6826     case R_PPC64_TPREL16_HIGHERA:
6827     case R_PPC64_TPREL16_HIGHEST:
6828     case R_PPC64_TPREL16_HIGHESTA:
6829     case R_PPC64_TPREL64:
6830     case R_PPC64_TPREL34:
6831     case R_PPC64_DTPMOD64:
6832     case R_PPC64_DTPREL64:
6833     case R_PPC64_ADDR64:
6834     case R_PPC64_REL30:
6835     case R_PPC64_REL32:
6836     case R_PPC64_REL64:
6837     case R_PPC64_ADDR14:
6838     case R_PPC64_ADDR14_BRNTAKEN:
6839     case R_PPC64_ADDR14_BRTAKEN:
6840     case R_PPC64_ADDR16:
6841     case R_PPC64_ADDR16_DS:
6842     case R_PPC64_ADDR16_HA:
6843     case R_PPC64_ADDR16_HI:
6844     case R_PPC64_ADDR16_HIGH:
6845     case R_PPC64_ADDR16_HIGHA:
6846     case R_PPC64_ADDR16_HIGHER:
6847     case R_PPC64_ADDR16_HIGHERA:
6848     case R_PPC64_ADDR16_HIGHEST:
6849     case R_PPC64_ADDR16_HIGHESTA:
6850     case R_PPC64_ADDR16_LO:
6851     case R_PPC64_ADDR16_LO_DS:
6852     case R_PPC64_ADDR24:
6853     case R_PPC64_ADDR32:
6854     case R_PPC64_UADDR16:
6855     case R_PPC64_UADDR32:
6856     case R_PPC64_UADDR64:
6857     case R_PPC64_TOC:
6858     case R_PPC64_D34:
6859     case R_PPC64_D34_LO:
6860     case R_PPC64_D34_HI30:
6861     case R_PPC64_D34_HA30:
6862     case R_PPC64_ADDR16_HIGHER34:
6863     case R_PPC64_ADDR16_HIGHERA34:
6864     case R_PPC64_ADDR16_HIGHEST34:
6865     case R_PPC64_ADDR16_HIGHESTA34:
6866     case R_PPC64_D28:
6867       break;
6868     }
6869
6870   if (local_syms != NULL)
6871     {
6872       unsigned long r_symndx;
6873       bfd *ibfd = sec->owner;
6874
6875       r_symndx = ELF64_R_SYM (r_info);
6876       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6877         return FALSE;
6878     }
6879
6880   if ((bfd_link_pic (info)
6881        && (must_be_dyn_reloc (info, r_type)
6882            || (h != NULL
6883                && (!SYMBOLIC_BIND (info, h)
6884                    || h->root.type == bfd_link_hash_defweak
6885                    || !h->def_regular))))
6886       || (ELIMINATE_COPY_RELOCS
6887           && !bfd_link_pic (info)
6888           && h != NULL
6889           && (h->root.type == bfd_link_hash_defweak
6890               || !h->def_regular)))
6891     ;
6892   else
6893     return TRUE;
6894
6895   if (h != NULL)
6896     {
6897       struct elf_dyn_relocs *p;
6898       struct elf_dyn_relocs **pp;
6899       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6900
6901       /* elf_gc_sweep may have already removed all dyn relocs associated
6902          with local syms for a given section.  Also, symbol flags are
6903          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
6904          report a dynreloc miscount.  */
6905       if (*pp == NULL && info->gc_sections)
6906         return TRUE;
6907
6908       while ((p = *pp) != NULL)
6909         {
6910           if (p->sec == sec)
6911             {
6912               if (!must_be_dyn_reloc (info, r_type))
6913                 p->pc_count -= 1;
6914               p->count -= 1;
6915               if (p->count == 0)
6916                 *pp = p->next;
6917               return TRUE;
6918             }
6919           pp = &p->next;
6920         }
6921     }
6922   else
6923     {
6924       struct ppc_dyn_relocs *p;
6925       struct ppc_dyn_relocs **pp;
6926       void *vpp;
6927       bfd_boolean is_ifunc;
6928
6929       if (local_syms == NULL)
6930         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6931       if (sym_sec == NULL)
6932         sym_sec = sec;
6933
6934       vpp = &elf_section_data (sym_sec)->local_dynrel;
6935       pp = (struct ppc_dyn_relocs **) vpp;
6936
6937       if (*pp == NULL && info->gc_sections)
6938         return TRUE;
6939
6940       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6941       while ((p = *pp) != NULL)
6942         {
6943           if (p->sec == sec && p->ifunc == is_ifunc)
6944             {
6945               p->count -= 1;
6946               if (p->count == 0)
6947                 *pp = p->next;
6948               return TRUE;
6949             }
6950           pp = &p->next;
6951         }
6952     }
6953
6954   /* xgettext:c-format */
6955   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6956                       sec->owner, sec);
6957   bfd_set_error (bfd_error_bad_value);
6958   return FALSE;
6959 }
6960
6961 /* Remove unused Official Procedure Descriptor entries.  Currently we
6962    only remove those associated with functions in discarded link-once
6963    sections, or weakly defined functions that have been overridden.  It
6964    would be possible to remove many more entries for statically linked
6965    applications.  */
6966
6967 bfd_boolean
6968 ppc64_elf_edit_opd (struct bfd_link_info *info)
6969 {
6970   bfd *ibfd;
6971   bfd_boolean some_edited = FALSE;
6972   asection *need_pad = NULL;
6973   struct ppc_link_hash_table *htab;
6974
6975   htab = ppc_hash_table (info);
6976   if (htab == NULL)
6977     return FALSE;
6978
6979   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6980     {
6981       asection *sec;
6982       Elf_Internal_Rela *relstart, *rel, *relend;
6983       Elf_Internal_Shdr *symtab_hdr;
6984       Elf_Internal_Sym *local_syms;
6985       struct _opd_sec_data *opd;
6986       bfd_boolean need_edit, add_aux_fields, broken;
6987       bfd_size_type cnt_16b = 0;
6988
6989       if (!is_ppc64_elf (ibfd))
6990         continue;
6991
6992       sec = bfd_get_section_by_name (ibfd, ".opd");
6993       if (sec == NULL || sec->size == 0)
6994         continue;
6995
6996       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6997         continue;
6998
6999       if (sec->output_section == bfd_abs_section_ptr)
7000         continue;
7001
7002       /* Look through the section relocs.  */
7003       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7004         continue;
7005
7006       local_syms = NULL;
7007       symtab_hdr = &elf_symtab_hdr (ibfd);
7008
7009       /* Read the relocations.  */
7010       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7011                                             info->keep_memory);
7012       if (relstart == NULL)
7013         return FALSE;
7014
7015       /* First run through the relocs to check they are sane, and to
7016          determine whether we need to edit this opd section.  */
7017       need_edit = FALSE;
7018       broken = FALSE;
7019       need_pad = sec;
7020       relend = relstart + sec->reloc_count;
7021       for (rel = relstart; rel < relend; )
7022         {
7023           enum elf_ppc64_reloc_type r_type;
7024           unsigned long r_symndx;
7025           asection *sym_sec;
7026           struct elf_link_hash_entry *h;
7027           Elf_Internal_Sym *sym;
7028           bfd_vma offset;
7029
7030           /* .opd contains an array of 16 or 24 byte entries.  We're
7031              only interested in the reloc pointing to a function entry
7032              point.  */
7033           offset = rel->r_offset;
7034           if (rel + 1 == relend
7035               || rel[1].r_offset != offset + 8)
7036             {
7037               /* If someone messes with .opd alignment then after a
7038                  "ld -r" we might have padding in the middle of .opd.
7039                  Also, there's nothing to prevent someone putting
7040                  something silly in .opd with the assembler.  No .opd
7041                  optimization for them!  */
7042             broken_opd:
7043               _bfd_error_handler
7044                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7045               broken = TRUE;
7046               break;
7047             }
7048
7049           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7050               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7051             {
7052               _bfd_error_handler
7053                 /* xgettext:c-format */
7054                 (_("%pB: unexpected reloc type %u in .opd section"),
7055                  ibfd, r_type);
7056               broken = TRUE;
7057               break;
7058             }
7059
7060           r_symndx = ELF64_R_SYM (rel->r_info);
7061           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7062                           r_symndx, ibfd))
7063             goto error_ret;
7064
7065           if (sym_sec == NULL || sym_sec->owner == NULL)
7066             {
7067               const char *sym_name;
7068               if (h != NULL)
7069                 sym_name = h->root.root.string;
7070               else
7071                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7072                                              sym_sec);
7073
7074               _bfd_error_handler
7075                 /* xgettext:c-format */
7076                 (_("%pB: undefined sym `%s' in .opd section"),
7077                  ibfd, sym_name);
7078               broken = TRUE;
7079               break;
7080             }
7081
7082           /* opd entries are always for functions defined in the
7083              current input bfd.  If the symbol isn't defined in the
7084              input bfd, then we won't be using the function in this
7085              bfd;  It must be defined in a linkonce section in another
7086              bfd, or is weak.  It's also possible that we are
7087              discarding the function due to a linker script /DISCARD/,
7088              which we test for via the output_section.  */
7089           if (sym_sec->owner != ibfd
7090               || sym_sec->output_section == bfd_abs_section_ptr)
7091             need_edit = TRUE;
7092
7093           rel += 2;
7094           if (rel + 1 == relend
7095               || (rel + 2 < relend
7096                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7097             ++rel;
7098
7099           if (rel == relend)
7100             {
7101               if (sec->size == offset + 24)
7102                 {
7103                   need_pad = NULL;
7104                   break;
7105                 }
7106               if (sec->size == offset + 16)
7107                 {
7108                   cnt_16b++;
7109                   break;
7110                 }
7111               goto broken_opd;
7112             }
7113           else if (rel + 1 < relend
7114                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7115                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7116             {
7117               if (rel[0].r_offset == offset + 16)
7118                 cnt_16b++;
7119               else if (rel[0].r_offset != offset + 24)
7120                 goto broken_opd;
7121             }
7122           else
7123             goto broken_opd;
7124         }
7125
7126       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7127
7128       if (!broken && (need_edit || add_aux_fields))
7129         {
7130           Elf_Internal_Rela *write_rel;
7131           Elf_Internal_Shdr *rel_hdr;
7132           bfd_byte *rptr, *wptr;
7133           bfd_byte *new_contents;
7134           bfd_size_type amt;
7135
7136           new_contents = NULL;
7137           amt = OPD_NDX (sec->size) * sizeof (long);
7138           opd = &ppc64_elf_section_data (sec)->u.opd;
7139           opd->adjust = bfd_zalloc (sec->owner, amt);
7140           if (opd->adjust == NULL)
7141             return FALSE;
7142
7143           /* This seems a waste of time as input .opd sections are all
7144              zeros as generated by gcc, but I suppose there's no reason
7145              this will always be so.  We might start putting something in
7146              the third word of .opd entries.  */
7147           if ((sec->flags & SEC_IN_MEMORY) == 0)
7148             {
7149               bfd_byte *loc;
7150               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7151                 {
7152                   if (loc != NULL)
7153                     free (loc);
7154                 error_ret:
7155                   if (local_syms != NULL
7156                       && symtab_hdr->contents != (unsigned char *) local_syms)
7157                     free (local_syms);
7158                   if (elf_section_data (sec)->relocs != relstart)
7159                     free (relstart);
7160                   return FALSE;
7161                 }
7162               sec->contents = loc;
7163               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7164             }
7165
7166           elf_section_data (sec)->relocs = relstart;
7167
7168           new_contents = sec->contents;
7169           if (add_aux_fields)
7170             {
7171               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7172               if (new_contents == NULL)
7173                 return FALSE;
7174               need_pad = NULL;
7175             }
7176           wptr = new_contents;
7177           rptr = sec->contents;
7178           write_rel = relstart;
7179           for (rel = relstart; rel < relend; )
7180             {
7181               unsigned long r_symndx;
7182               asection *sym_sec;
7183               struct elf_link_hash_entry *h;
7184               struct ppc_link_hash_entry *fdh = NULL;
7185               Elf_Internal_Sym *sym;
7186               long opd_ent_size;
7187               Elf_Internal_Rela *next_rel;
7188               bfd_boolean skip;
7189
7190               r_symndx = ELF64_R_SYM (rel->r_info);
7191               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7192                               r_symndx, ibfd))
7193                 goto error_ret;
7194
7195               next_rel = rel + 2;
7196               if (next_rel + 1 == relend
7197                   || (next_rel + 2 < relend
7198                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7199                 ++next_rel;
7200
7201               /* See if the .opd entry is full 24 byte or
7202                  16 byte (with fd_aux entry overlapped with next
7203                  fd_func).  */
7204               opd_ent_size = 24;
7205               if (next_rel == relend)
7206                 {
7207                   if (sec->size == rel->r_offset + 16)
7208                     opd_ent_size = 16;
7209                 }
7210               else if (next_rel->r_offset == rel->r_offset + 16)
7211                 opd_ent_size = 16;
7212
7213               if (h != NULL
7214                   && h->root.root.string[0] == '.')
7215                 {
7216                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
7217                   if (fdh != NULL)
7218                     {
7219                       fdh = ppc_follow_link (fdh);
7220                       if (fdh->elf.root.type != bfd_link_hash_defined
7221                           && fdh->elf.root.type != bfd_link_hash_defweak)
7222                         fdh = NULL;
7223                     }
7224                 }
7225
7226               skip = (sym_sec->owner != ibfd
7227                       || sym_sec->output_section == bfd_abs_section_ptr);
7228               if (skip)
7229                 {
7230                   if (fdh != NULL && sym_sec->owner == ibfd)
7231                     {
7232                       /* Arrange for the function descriptor sym
7233                          to be dropped.  */
7234                       fdh->elf.root.u.def.value = 0;
7235                       fdh->elf.root.u.def.section = sym_sec;
7236                     }
7237                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7238
7239                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7240                     rel = next_rel;
7241                   else
7242                     while (1)
7243                       {
7244                         if (!dec_dynrel_count (rel->r_info, sec, info,
7245                                                NULL, h, sym))
7246                           goto error_ret;
7247
7248                         if (++rel == next_rel)
7249                           break;
7250
7251                         r_symndx = ELF64_R_SYM (rel->r_info);
7252                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7253                                         r_symndx, ibfd))
7254                           goto error_ret;
7255                       }
7256                 }
7257               else
7258                 {
7259                   /* We'll be keeping this opd entry.  */
7260                   long adjust;
7261
7262                   if (fdh != NULL)
7263                     {
7264                       /* Redefine the function descriptor symbol to
7265                          this location in the opd section.  It is
7266                          necessary to update the value here rather
7267                          than using an array of adjustments as we do
7268                          for local symbols, because various places
7269                          in the generic ELF code use the value
7270                          stored in u.def.value.  */
7271                       fdh->elf.root.u.def.value = wptr - new_contents;
7272                       fdh->adjust_done = 1;
7273                     }
7274
7275                   /* Local syms are a bit tricky.  We could
7276                      tweak them as they can be cached, but
7277                      we'd need to look through the local syms
7278                      for the function descriptor sym which we
7279                      don't have at the moment.  So keep an
7280                      array of adjustments.  */
7281                   adjust = (wptr - new_contents) - (rptr - sec->contents);
7282                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7283
7284                   if (wptr != rptr)
7285                     memcpy (wptr, rptr, opd_ent_size);
7286                   wptr += opd_ent_size;
7287                   if (add_aux_fields && opd_ent_size == 16)
7288                     {
7289                       memset (wptr, '\0', 8);
7290                       wptr += 8;
7291                     }
7292
7293                   /* We need to adjust any reloc offsets to point to the
7294                      new opd entries.  */
7295                   for ( ; rel != next_rel; ++rel)
7296                     {
7297                       rel->r_offset += adjust;
7298                       if (write_rel != rel)
7299                         memcpy (write_rel, rel, sizeof (*rel));
7300                       ++write_rel;
7301                     }
7302                 }
7303
7304               rptr += opd_ent_size;
7305             }
7306
7307           sec->size = wptr - new_contents;
7308           sec->reloc_count = write_rel - relstart;
7309           if (add_aux_fields)
7310             {
7311               free (sec->contents);
7312               sec->contents = new_contents;
7313             }
7314
7315           /* Fudge the header size too, as this is used later in
7316              elf_bfd_final_link if we are emitting relocs.  */
7317           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7318           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7319           some_edited = TRUE;
7320         }
7321       else if (elf_section_data (sec)->relocs != relstart)
7322         free (relstart);
7323
7324       if (local_syms != NULL
7325           && symtab_hdr->contents != (unsigned char *) local_syms)
7326         {
7327           if (!info->keep_memory)
7328             free (local_syms);
7329           else
7330             symtab_hdr->contents = (unsigned char *) local_syms;
7331         }
7332     }
7333
7334   if (some_edited)
7335     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7336
7337   /* If we are doing a final link and the last .opd entry is just 16 byte
7338      long, add a 8 byte padding after it.  */
7339   if (need_pad != NULL && !bfd_link_relocatable (info))
7340     {
7341       bfd_byte *p;
7342
7343       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7344         {
7345           BFD_ASSERT (need_pad->size > 0);
7346
7347           p = bfd_malloc (need_pad->size + 8);
7348           if (p == NULL)
7349             return FALSE;
7350
7351           if (!bfd_get_section_contents (need_pad->owner, need_pad,
7352                                          p, 0, need_pad->size))
7353             return FALSE;
7354
7355           need_pad->contents = p;
7356           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7357         }
7358       else
7359         {
7360           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7361           if (p == NULL)
7362             return FALSE;
7363
7364           need_pad->contents = p;
7365         }
7366
7367       memset (need_pad->contents + need_pad->size, 0, 8);
7368       need_pad->size += 8;
7369     }
7370
7371   return TRUE;
7372 }
7373
7374 /* Analyze inline PLT call relocations to see whether calls to locally
7375    defined functions can be converted to direct calls.  */
7376
7377 bfd_boolean
7378 ppc64_elf_inline_plt (struct bfd_link_info *info)
7379 {
7380   struct ppc_link_hash_table *htab;
7381   bfd *ibfd;
7382   asection *sec;
7383   bfd_vma low_vma, high_vma, limit;
7384
7385   htab = ppc_hash_table (info);
7386   if (htab == NULL)
7387     return FALSE;
7388
7389   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7390      reduced somewhat to cater for possible stubs that might be added
7391      between the call and its destination.  */
7392   if (htab->params->group_size < 0)
7393     {
7394       limit = -htab->params->group_size;
7395       if (limit == 1)
7396         limit = 0x1e00000;
7397     }
7398   else
7399     {
7400       limit = htab->params->group_size;
7401       if (limit == 1)
7402         limit = 0x1c00000;
7403     }
7404
7405   low_vma = -1;
7406   high_vma = 0;
7407   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7408     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7409       {
7410         if (low_vma > sec->vma)
7411           low_vma = sec->vma;
7412         if (high_vma < sec->vma + sec->size)
7413           high_vma = sec->vma + sec->size;
7414       }
7415
7416   /* If a "bl" can reach anywhere in local code sections, then we can
7417      convert all inline PLT sequences to direct calls when the symbol
7418      is local.  */
7419   if (high_vma - low_vma < limit)
7420     {
7421       htab->can_convert_all_inline_plt = 1;
7422       return TRUE;
7423     }
7424
7425   /* Otherwise, go looking through relocs for cases where a direct
7426      call won't reach.  Mark the symbol on any such reloc to disable
7427      the optimization and keep the PLT entry as it seems likely that
7428      this will be better than creating trampolines.  Note that this
7429      will disable the optimization for all inline PLT calls to a
7430      particular symbol, not just those that won't reach.  The
7431      difficulty in doing a more precise optimization is that the
7432      linker needs to make a decision depending on whether a
7433      particular R_PPC64_PLTCALL insn can be turned into a direct
7434      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7435      the sequence, and there is nothing that ties those relocs
7436      together except their symbol.  */
7437
7438   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7439     {
7440       Elf_Internal_Shdr *symtab_hdr;
7441       Elf_Internal_Sym *local_syms;
7442
7443       if (!is_ppc64_elf (ibfd))
7444         continue;
7445
7446       local_syms = NULL;
7447       symtab_hdr = &elf_symtab_hdr (ibfd);
7448
7449       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7450         if (ppc64_elf_section_data (sec)->has_pltcall
7451             && !bfd_is_abs_section (sec->output_section))
7452           {
7453             Elf_Internal_Rela *relstart, *rel, *relend;
7454
7455             /* Read the relocations.  */
7456             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7457                                                   info->keep_memory);
7458             if (relstart == NULL)
7459               return FALSE;
7460
7461             relend = relstart + sec->reloc_count;
7462             for (rel = relstart; rel < relend; )
7463               {
7464                 enum elf_ppc64_reloc_type r_type;
7465                 unsigned long r_symndx;
7466                 asection *sym_sec;
7467                 struct elf_link_hash_entry *h;
7468                 Elf_Internal_Sym *sym;
7469                 unsigned char *tls_maskp;
7470
7471                 r_type = ELF64_R_TYPE (rel->r_info);
7472                 if (r_type != R_PPC64_PLTCALL
7473                     && r_type != R_PPC64_PLTCALL_NOTOC)
7474                   continue;
7475
7476                 r_symndx = ELF64_R_SYM (rel->r_info);
7477                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7478                                 r_symndx, ibfd))
7479                   {
7480                     if (elf_section_data (sec)->relocs != relstart)
7481                       free (relstart);
7482                     if (local_syms != NULL
7483                         && symtab_hdr->contents != (bfd_byte *) local_syms)
7484                       free (local_syms);
7485                     return FALSE;
7486                   }
7487
7488                 if (sym_sec != NULL && sym_sec->output_section != NULL)
7489                   {
7490                     bfd_vma from, to;
7491                     if (h != NULL)
7492                       to = h->root.u.def.value;
7493                     else
7494                       to = sym->st_value;
7495                     to += (rel->r_addend
7496                            + sym_sec->output_offset
7497                            + sym_sec->output_section->vma);
7498                     from = (rel->r_offset
7499                             + sec->output_offset
7500                             + sec->output_section->vma);
7501                     if (to - from + limit < 2 * limit
7502                         && !(r_type == R_PPC64_PLTCALL_NOTOC
7503                              && (((h ? h->other : sym->st_other)
7504                                   & STO_PPC64_LOCAL_MASK)
7505                                  > 1 << STO_PPC64_LOCAL_BIT)))
7506                       *tls_maskp &= ~PLT_KEEP;
7507                   }
7508               }
7509             if (elf_section_data (sec)->relocs != relstart)
7510               free (relstart);
7511           }
7512
7513       if (local_syms != NULL
7514           && symtab_hdr->contents != (unsigned char *) local_syms)
7515         {
7516           if (!info->keep_memory)
7517             free (local_syms);
7518           else
7519             symtab_hdr->contents = (unsigned char *) local_syms;
7520         }
7521     }
7522
7523   return TRUE;
7524 }
7525
7526 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7527
7528 asection *
7529 ppc64_elf_tls_setup (struct bfd_link_info *info)
7530 {
7531   struct ppc_link_hash_table *htab;
7532
7533   htab = ppc_hash_table (info);
7534   if (htab == NULL)
7535     return NULL;
7536
7537   if (abiversion (info->output_bfd) == 1)
7538     htab->opd_abi = 1;
7539
7540   if (htab->params->no_multi_toc)
7541     htab->do_multi_toc = 0;
7542   else if (!htab->do_multi_toc)
7543     htab->params->no_multi_toc = 1;
7544
7545   /* Default to --no-plt-localentry, as this option can cause problems
7546      with symbol interposition.  For example, glibc libpthread.so and
7547      libc.so duplicate many pthread symbols, with a fallback
7548      implementation in libc.so.  In some cases the fallback does more
7549      work than the pthread implementation.  __pthread_condattr_destroy
7550      is one such symbol: the libpthread.so implementation is
7551      localentry:0 while the libc.so implementation is localentry:8.
7552      An app that "cleverly" uses dlopen to only load necessary
7553      libraries at runtime may omit loading libpthread.so when not
7554      running multi-threaded, which then results in the libc.so
7555      fallback symbols being used and ld.so complaining.  Now there
7556      are workarounds in ld (see non_zero_localentry) to detect the
7557      pthread situation, but that may not be the only case where
7558      --plt-localentry can cause trouble.  */
7559   if (htab->params->plt_localentry0 < 0)
7560     htab->params->plt_localentry0 = 0;
7561   if (htab->params->plt_localentry0
7562       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7563                                FALSE, FALSE, FALSE) == NULL)
7564     _bfd_error_handler
7565       (_("warning: --plt-localentry is especially dangerous without "
7566          "ld.so support to detect ABI violations"));
7567
7568   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7569                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7570                                               FALSE, FALSE, TRUE));
7571   /* Move dynamic linking info to the function descriptor sym.  */
7572   if (htab->tls_get_addr != NULL)
7573     func_desc_adjust (&htab->tls_get_addr->elf, info);
7574   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7575                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7576                                                  FALSE, FALSE, TRUE));
7577   if (htab->params->tls_get_addr_opt)
7578     {
7579       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7580
7581       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7582                                   FALSE, FALSE, TRUE);
7583       if (opt != NULL)
7584         func_desc_adjust (opt, info);
7585       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7586                                      FALSE, FALSE, TRUE);
7587       if (opt_fd != NULL
7588           && (opt_fd->root.type == bfd_link_hash_defined
7589               || opt_fd->root.type == bfd_link_hash_defweak))
7590         {
7591           /* If glibc supports an optimized __tls_get_addr call stub,
7592              signalled by the presence of __tls_get_addr_opt, and we'll
7593              be calling __tls_get_addr via a plt call stub, then
7594              make __tls_get_addr point to __tls_get_addr_opt.  */
7595           tga_fd = &htab->tls_get_addr_fd->elf;
7596           if (htab->elf.dynamic_sections_created
7597               && tga_fd != NULL
7598               && (tga_fd->type == STT_FUNC
7599                   || tga_fd->needs_plt)
7600               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7601                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7602             {
7603               struct plt_entry *ent;
7604
7605               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7606                 if (ent->plt.refcount > 0)
7607                   break;
7608               if (ent != NULL)
7609                 {
7610                   tga_fd->root.type = bfd_link_hash_indirect;
7611                   tga_fd->root.u.i.link = &opt_fd->root;
7612                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7613                   opt_fd->mark = 1;
7614                   if (opt_fd->dynindx != -1)
7615                     {
7616                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7617                       opt_fd->dynindx = -1;
7618                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7619                                               opt_fd->dynstr_index);
7620                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7621                         return NULL;
7622                     }
7623                   htab->tls_get_addr_fd
7624                     = (struct ppc_link_hash_entry *) opt_fd;
7625                   tga = &htab->tls_get_addr->elf;
7626                   if (opt != NULL && tga != NULL)
7627                     {
7628                       tga->root.type = bfd_link_hash_indirect;
7629                       tga->root.u.i.link = &opt->root;
7630                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7631                       opt->mark = 1;
7632                       _bfd_elf_link_hash_hide_symbol (info, opt,
7633                                                       tga->forced_local);
7634                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7635                     }
7636                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7637                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7638                   if (htab->tls_get_addr != NULL)
7639                     {
7640                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7641                       htab->tls_get_addr->is_func = 1;
7642                     }
7643                 }
7644             }
7645         }
7646       else if (htab->params->tls_get_addr_opt < 0)
7647         htab->params->tls_get_addr_opt = 0;
7648     }
7649   return _bfd_elf_tls_setup (info->output_bfd, info);
7650 }
7651
7652 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7653    HASH1 or HASH2.  */
7654
7655 static bfd_boolean
7656 branch_reloc_hash_match (const bfd *ibfd,
7657                          const Elf_Internal_Rela *rel,
7658                          const struct ppc_link_hash_entry *hash1,
7659                          const struct ppc_link_hash_entry *hash2)
7660 {
7661   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7662   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7663   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7664
7665   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7666     {
7667       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7668       struct elf_link_hash_entry *h;
7669
7670       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7671       h = elf_follow_link (h);
7672       if (h == &hash1->elf || h == &hash2->elf)
7673         return TRUE;
7674     }
7675   return FALSE;
7676 }
7677
7678 /* Run through all the TLS relocs looking for optimization
7679    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7680    a preliminary section layout so that we know the TLS segment
7681    offsets.  We can't optimize earlier because some optimizations need
7682    to know the tp offset, and we need to optimize before allocating
7683    dynamic relocations.  */
7684
7685 bfd_boolean
7686 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7687 {
7688   bfd *ibfd;
7689   asection *sec;
7690   struct ppc_link_hash_table *htab;
7691   unsigned char *toc_ref;
7692   int pass;
7693
7694   if (!bfd_link_executable (info))
7695     return TRUE;
7696
7697   htab = ppc_hash_table (info);
7698   if (htab == NULL)
7699     return FALSE;
7700
7701   /* Make two passes over the relocs.  On the first pass, mark toc
7702      entries involved with tls relocs, and check that tls relocs
7703      involved in setting up a tls_get_addr call are indeed followed by
7704      such a call.  If they are not, we can't do any tls optimization.
7705      On the second pass twiddle tls_mask flags to notify
7706      relocate_section that optimization can be done, and adjust got
7707      and plt refcounts.  */
7708   toc_ref = NULL;
7709   for (pass = 0; pass < 2; ++pass)
7710     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7711       {
7712         Elf_Internal_Sym *locsyms = NULL;
7713         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7714
7715         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7716           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7717             {
7718               Elf_Internal_Rela *relstart, *rel, *relend;
7719               bfd_boolean found_tls_get_addr_arg = 0;
7720
7721               /* Read the relocations.  */
7722               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7723                                                     info->keep_memory);
7724               if (relstart == NULL)
7725                 {
7726                   free (toc_ref);
7727                   return FALSE;
7728                 }
7729
7730               relend = relstart + sec->reloc_count;
7731               for (rel = relstart; rel < relend; rel++)
7732                 {
7733                   enum elf_ppc64_reloc_type r_type;
7734                   unsigned long r_symndx;
7735                   struct elf_link_hash_entry *h;
7736                   Elf_Internal_Sym *sym;
7737                   asection *sym_sec;
7738                   unsigned char *tls_mask;
7739                   unsigned int tls_set, tls_clear, tls_type = 0;
7740                   bfd_vma value;
7741                   bfd_boolean ok_tprel, is_local;
7742                   long toc_ref_index = 0;
7743                   int expecting_tls_get_addr = 0;
7744                   bfd_boolean ret = FALSE;
7745
7746                   r_symndx = ELF64_R_SYM (rel->r_info);
7747                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7748                                   r_symndx, ibfd))
7749                     {
7750                     err_free_rel:
7751                       if (elf_section_data (sec)->relocs != relstart)
7752                         free (relstart);
7753                       if (toc_ref != NULL)
7754                         free (toc_ref);
7755                       if (locsyms != NULL
7756                           && (elf_symtab_hdr (ibfd).contents
7757                               != (unsigned char *) locsyms))
7758                         free (locsyms);
7759                       return ret;
7760                     }
7761
7762                   if (h != NULL)
7763                     {
7764                       if (h->root.type == bfd_link_hash_defined
7765                           || h->root.type == bfd_link_hash_defweak)
7766                         value = h->root.u.def.value;
7767                       else if (h->root.type == bfd_link_hash_undefweak)
7768                         value = 0;
7769                       else
7770                         {
7771                           found_tls_get_addr_arg = 0;
7772                           continue;
7773                         }
7774                     }
7775                   else
7776                     /* Symbols referenced by TLS relocs must be of type
7777                        STT_TLS.  So no need for .opd local sym adjust.  */
7778                     value = sym->st_value;
7779
7780                   ok_tprel = FALSE;
7781                   is_local = SYMBOL_REFERENCES_LOCAL (info, h);
7782                   if (is_local)
7783                     {
7784                       if (h != NULL
7785                           && h->root.type == bfd_link_hash_undefweak)
7786                         ok_tprel = TRUE;
7787                       else if (sym_sec != NULL
7788                                && sym_sec->output_section != NULL)
7789                         {
7790                           value += sym_sec->output_offset;
7791                           value += sym_sec->output_section->vma;
7792                           value -= htab->elf.tls_sec->vma + TP_OFFSET;
7793                           /* Note that even though the prefix insns
7794                              allow a 1<<33 offset we use the same test
7795                              as for addis;addi.  There may be a mix of
7796                              pcrel and non-pcrel code and the decision
7797                              to optimise is per symbol, not per TLS
7798                              sequence.  */
7799                           ok_tprel = value + 0x80008000ULL < 1ULL << 32;
7800                         }
7801                     }
7802
7803                   r_type = ELF64_R_TYPE (rel->r_info);
7804                   /* If this section has old-style __tls_get_addr calls
7805                      without marker relocs, then check that each
7806                      __tls_get_addr call reloc is preceded by a reloc
7807                      that conceivably belongs to the __tls_get_addr arg
7808                      setup insn.  If we don't find matching arg setup
7809                      relocs, don't do any tls optimization.  */
7810                   if (pass == 0
7811                       && sec->nomark_tls_get_addr
7812                       && h != NULL
7813                       && (h == &htab->tls_get_addr->elf
7814                           || h == &htab->tls_get_addr_fd->elf)
7815                       && !found_tls_get_addr_arg
7816                       && is_branch_reloc (r_type))
7817                     {
7818                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7819                                                 "TLS optimization disabled\n"),
7820                                               ibfd, sec, rel->r_offset);
7821                       ret = TRUE;
7822                       goto err_free_rel;
7823                     }
7824
7825                   found_tls_get_addr_arg = 0;
7826                   switch (r_type)
7827                     {
7828                     case R_PPC64_GOT_TLSLD16:
7829                     case R_PPC64_GOT_TLSLD16_LO:
7830                     case R_PPC64_GOT_TLSLD34:
7831                       expecting_tls_get_addr = 1;
7832                       found_tls_get_addr_arg = 1;
7833                       /* Fall through.  */
7834
7835                     case R_PPC64_GOT_TLSLD16_HI:
7836                     case R_PPC64_GOT_TLSLD16_HA:
7837                       /* These relocs should never be against a symbol
7838                          defined in a shared lib.  Leave them alone if
7839                          that turns out to be the case.  */
7840                       if (!is_local)
7841                         continue;
7842
7843                       /* LD -> LE */
7844                       tls_set = 0;
7845                       tls_clear = TLS_LD;
7846                       tls_type = TLS_TLS | TLS_LD;
7847                       break;
7848
7849                     case R_PPC64_GOT_TLSGD16:
7850                     case R_PPC64_GOT_TLSGD16_LO:
7851                     case R_PPC64_GOT_TLSGD34:
7852                       expecting_tls_get_addr = 1;
7853                       found_tls_get_addr_arg = 1;
7854                       /* Fall through. */
7855
7856                     case R_PPC64_GOT_TLSGD16_HI:
7857                     case R_PPC64_GOT_TLSGD16_HA:
7858                       if (ok_tprel)
7859                         /* GD -> LE */
7860                         tls_set = 0;
7861                       else
7862                         /* GD -> IE */
7863                         tls_set = TLS_TLS | TLS_GDIE;
7864                       tls_clear = TLS_GD;
7865                       tls_type = TLS_TLS | TLS_GD;
7866                       break;
7867
7868                     case R_PPC64_GOT_TPREL34:
7869                     case R_PPC64_GOT_TPREL16_DS:
7870                     case R_PPC64_GOT_TPREL16_LO_DS:
7871                     case R_PPC64_GOT_TPREL16_HI:
7872                     case R_PPC64_GOT_TPREL16_HA:
7873                       if (ok_tprel)
7874                         {
7875                           /* IE -> LE */
7876                           tls_set = 0;
7877                           tls_clear = TLS_TPREL;
7878                           tls_type = TLS_TLS | TLS_TPREL;
7879                           break;
7880                         }
7881                       continue;
7882
7883                     case R_PPC64_TLSLD:
7884                       if (!is_local)
7885                         continue;
7886                       /* Fall through.  */
7887                     case R_PPC64_TLSGD:
7888                       if (rel + 1 < relend
7889                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7890                         {
7891                           if (pass != 0
7892                               && (ELF64_R_TYPE (rel[1].r_info)
7893                                   != R_PPC64_PLTSEQ)
7894                               && (ELF64_R_TYPE (rel[1].r_info)
7895                                   != R_PPC64_PLTSEQ_NOTOC))
7896                             {
7897                               r_symndx = ELF64_R_SYM (rel[1].r_info);
7898                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7899                                               r_symndx, ibfd))
7900                                 goto err_free_rel;
7901                               if (h != NULL)
7902                                 {
7903                                   struct plt_entry *ent = NULL;
7904
7905                                   for (ent = h->plt.plist;
7906                                        ent != NULL;
7907                                        ent = ent->next)
7908                                     if (ent->addend == rel[1].r_addend)
7909                                       break;
7910
7911                                   if (ent != NULL
7912                                       && ent->plt.refcount > 0)
7913                                     ent->plt.refcount -= 1;
7914                                 }
7915                             }
7916                           continue;
7917                         }
7918                       found_tls_get_addr_arg = 1;
7919                       /* Fall through.  */
7920
7921                     case R_PPC64_TLS:
7922                     case R_PPC64_TOC16:
7923                     case R_PPC64_TOC16_LO:
7924                       if (sym_sec == NULL || sym_sec != toc)
7925                         continue;
7926
7927                       /* Mark this toc entry as referenced by a TLS
7928                          code sequence.  We can do that now in the
7929                          case of R_PPC64_TLS, and after checking for
7930                          tls_get_addr for the TOC16 relocs.  */
7931                       if (toc_ref == NULL)
7932                         toc_ref
7933                           = bfd_zmalloc (toc->output_section->rawsize / 8);
7934                       if (toc_ref == NULL)
7935                         goto err_free_rel;
7936
7937                       if (h != NULL)
7938                         value = h->root.u.def.value;
7939                       else
7940                         value = sym->st_value;
7941                       value += rel->r_addend;
7942                       if (value % 8 != 0)
7943                         continue;
7944                       BFD_ASSERT (value < toc->size
7945                                   && toc->output_offset % 8 == 0);
7946                       toc_ref_index = (value + toc->output_offset) / 8;
7947                       if (r_type == R_PPC64_TLS
7948                           || r_type == R_PPC64_TLSGD
7949                           || r_type == R_PPC64_TLSLD)
7950                         {
7951                           toc_ref[toc_ref_index] = 1;
7952                           continue;
7953                         }
7954
7955                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7956                         continue;
7957
7958                       tls_set = 0;
7959                       tls_clear = 0;
7960                       expecting_tls_get_addr = 2;
7961                       break;
7962
7963                     case R_PPC64_TPREL64:
7964                       if (pass == 0
7965                           || sec != toc
7966                           || toc_ref == NULL
7967                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7968                         continue;
7969                       if (ok_tprel)
7970                         {
7971                           /* IE -> LE */
7972                           tls_set = TLS_EXPLICIT;
7973                           tls_clear = TLS_TPREL;
7974                           break;
7975                         }
7976                       continue;
7977
7978                     case R_PPC64_DTPMOD64:
7979                       if (pass == 0
7980                           || sec != toc
7981                           || toc_ref == NULL
7982                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7983                         continue;
7984                       if (rel + 1 < relend
7985                           && (rel[1].r_info
7986                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7987                           && rel[1].r_offset == rel->r_offset + 8)
7988                         {
7989                           if (ok_tprel)
7990                             /* GD -> LE */
7991                             tls_set = TLS_EXPLICIT | TLS_GD;
7992                           else
7993                             /* GD -> IE */
7994                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
7995                           tls_clear = TLS_GD;
7996                         }
7997                       else
7998                         {
7999                           if (!is_local)
8000                             continue;
8001
8002                           /* LD -> LE */
8003                           tls_set = TLS_EXPLICIT;
8004                           tls_clear = TLS_LD;
8005                         }
8006                       break;
8007
8008                     default:
8009                       continue;
8010                     }
8011
8012                   if (pass == 0)
8013                     {
8014                       if (!expecting_tls_get_addr
8015                           || !sec->nomark_tls_get_addr)
8016                         continue;
8017
8018                       if (rel + 1 < relend
8019                           && branch_reloc_hash_match (ibfd, rel + 1,
8020                                                       htab->tls_get_addr,
8021                                                       htab->tls_get_addr_fd))
8022                         {
8023                           if (expecting_tls_get_addr == 2)
8024                             {
8025                               /* Check for toc tls entries.  */
8026                               unsigned char *toc_tls;
8027                               int retval;
8028
8029                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8030                                                      &locsyms,
8031                                                      rel, ibfd);
8032                               if (retval == 0)
8033                                 goto err_free_rel;
8034                               if (toc_tls != NULL)
8035                                 {
8036                                   if ((*toc_tls & TLS_TLS) != 0
8037                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8038                                     found_tls_get_addr_arg = 1;
8039                                   if (retval > 1)
8040                                     toc_ref[toc_ref_index] = 1;
8041                                 }
8042                             }
8043                           continue;
8044                         }
8045
8046                       /* Uh oh, we didn't find the expected call.  We
8047                          could just mark this symbol to exclude it
8048                          from tls optimization but it's safer to skip
8049                          the entire optimization.  */
8050                       /* xgettext:c-format */
8051                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8052                                                 "TLS optimization disabled\n"),
8053                                               ibfd, sec, rel->r_offset);
8054                       ret = TRUE;
8055                       goto err_free_rel;
8056                     }
8057
8058                   /* If we don't have old-style __tls_get_addr calls
8059                      without TLSGD/TLSLD marker relocs, and we haven't
8060                      found a new-style __tls_get_addr call with a
8061                      marker for this symbol, then we either have a
8062                      broken object file or an -mlongcall style
8063                      indirect call to __tls_get_addr without a marker.
8064                      Disable optimization in this case.  */
8065                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8066                       && (tls_set & TLS_EXPLICIT) == 0
8067                       && !sec->nomark_tls_get_addr
8068                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
8069                           != (TLS_TLS | TLS_MARK)))
8070                     continue;
8071
8072                   if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8073                     {
8074                       struct plt_entry *ent = NULL;
8075
8076                       if (htab->tls_get_addr != NULL)
8077                         for (ent = htab->tls_get_addr->elf.plt.plist;
8078                              ent != NULL;
8079                              ent = ent->next)
8080                           if (ent->addend == 0)
8081                             break;
8082
8083                       if (ent == NULL && htab->tls_get_addr_fd != NULL)
8084                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8085                              ent != NULL;
8086                              ent = ent->next)
8087                           if (ent->addend == 0)
8088                             break;
8089
8090                       if (ent != NULL
8091                           && ent->plt.refcount > 0)
8092                         ent->plt.refcount -= 1;
8093                     }
8094
8095                   if (tls_clear == 0)
8096                     continue;
8097
8098                   if ((tls_set & TLS_EXPLICIT) == 0)
8099                     {
8100                       struct got_entry *ent;
8101
8102                       /* Adjust got entry for this reloc.  */
8103                       if (h != NULL)
8104                         ent = h->got.glist;
8105                       else
8106                         ent = elf_local_got_ents (ibfd)[r_symndx];
8107
8108                       for (; ent != NULL; ent = ent->next)
8109                         if (ent->addend == rel->r_addend
8110                             && ent->owner == ibfd
8111                             && ent->tls_type == tls_type)
8112                           break;
8113                       if (ent == NULL)
8114                         abort ();
8115
8116                       if (tls_set == 0)
8117                         {
8118                           /* We managed to get rid of a got entry.  */
8119                           if (ent->got.refcount > 0)
8120                             ent->got.refcount -= 1;
8121                         }
8122                     }
8123                   else
8124                     {
8125                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8126                          we'll lose one or two dyn relocs.  */
8127                       if (!dec_dynrel_count (rel->r_info, sec, info,
8128                                              NULL, h, sym))
8129                         return FALSE;
8130
8131                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8132                         {
8133                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8134                                                  NULL, h, sym))
8135                             return FALSE;
8136                         }
8137                     }
8138
8139                   *tls_mask |= tls_set & 0xff;
8140                   *tls_mask &= ~tls_clear;
8141                 }
8142
8143               if (elf_section_data (sec)->relocs != relstart)
8144                 free (relstart);
8145             }
8146
8147         if (locsyms != NULL
8148             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8149           {
8150             if (!info->keep_memory)
8151               free (locsyms);
8152             else
8153               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8154           }
8155       }
8156
8157   if (toc_ref != NULL)
8158     free (toc_ref);
8159   htab->do_tls_opt = 1;
8160   return TRUE;
8161 }
8162
8163 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8164    the values of any global symbols in a toc section that has been
8165    edited.  Globals in toc sections should be a rarity, so this function
8166    sets a flag if any are found in toc sections other than the one just
8167    edited, so that further hash table traversals can be avoided.  */
8168
8169 struct adjust_toc_info
8170 {
8171   asection *toc;
8172   unsigned long *skip;
8173   bfd_boolean global_toc_syms;
8174 };
8175
8176 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8177
8178 static bfd_boolean
8179 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8180 {
8181   struct ppc_link_hash_entry *eh;
8182   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8183   unsigned long i;
8184
8185   if (h->root.type != bfd_link_hash_defined
8186       && h->root.type != bfd_link_hash_defweak)
8187     return TRUE;
8188
8189   eh = (struct ppc_link_hash_entry *) h;
8190   if (eh->adjust_done)
8191     return TRUE;
8192
8193   if (eh->elf.root.u.def.section == toc_inf->toc)
8194     {
8195       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8196         i = toc_inf->toc->rawsize >> 3;
8197       else
8198         i = eh->elf.root.u.def.value >> 3;
8199
8200       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8201         {
8202           _bfd_error_handler
8203             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8204           do
8205             ++i;
8206           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8207           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8208         }
8209
8210       eh->elf.root.u.def.value -= toc_inf->skip[i];
8211       eh->adjust_done = 1;
8212     }
8213   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8214     toc_inf->global_toc_syms = TRUE;
8215
8216   return TRUE;
8217 }
8218
8219 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8220    on a _LO variety toc/got reloc.  */
8221
8222 static bfd_boolean
8223 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8224 {
8225   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8226           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8227           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8228           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8229           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8230           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8231           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8232           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8233           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8234           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8235           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8236           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8237           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8238           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8239           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8240           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8241           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8242               /* Exclude lfqu by testing reloc.  If relocs are ever
8243                  defined for the reduced D field in psq_lu then those
8244                  will need testing too.  */
8245               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8246           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8247               && (insn & 1) == 0)
8248           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8249           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8250               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8251               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8252           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8253               && (insn & 1) == 0));
8254 }
8255
8256 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8257      pld ra,symbol@got@pcrel
8258      load/store rt,off(ra)
8259    or
8260      pla ra,symbol@pcrel
8261      load/store rt,off(ra)
8262    may be translated to
8263      pload/pstore rt,symbol+off@pcrel
8264      nop.
8265    This function returns true if the optimization is possible, placing
8266    the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8267
8268    On entry to this function, the linker has already determined that
8269    the pld can be replaced with pla: *PINSN1 is that pla insn,
8270    while *PINSN2 is the second instruction.  */
8271
8272 static bfd_boolean
8273 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8274 {
8275   uint64_t insn1 = *pinsn1;
8276   uint64_t insn2 = *pinsn2;
8277   bfd_signed_vma off;
8278
8279   if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8280     {
8281       /* Check that regs match.  */
8282       if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8283         return FALSE;
8284
8285       /* P8LS or PMLS form, non-pcrel.  */
8286       if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8287         return FALSE;
8288
8289       *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8290       *pinsn2 = PNOP;
8291       off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8292       *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8293       return TRUE;
8294     }
8295
8296   insn2 >>= 32;
8297
8298   /* Check that regs match.  */
8299   if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8300     return FALSE;
8301
8302   switch ((insn2 >> 26) & 63)
8303     {
8304     default:
8305       return FALSE;
8306
8307     case 32: /* lwz */
8308     case 34: /* lbz */
8309     case 36: /* stw */
8310     case 38: /* stb */
8311     case 40: /* lhz */
8312     case 42: /* lha */
8313     case 44: /* sth */
8314     case 48: /* lfs */
8315     case 50: /* lfd */
8316     case 52: /* stfs */
8317     case 54: /* stfd */
8318       /* These are the PMLS cases, where we just need to tack a prefix
8319          on the insn.  */
8320       insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8321                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8322       off = insn2 & 0xffff;
8323       break;
8324
8325     case 58: /* lwa, ld */
8326       if ((insn2 & 1) != 0)
8327         return FALSE;
8328       insn1 = ((1ULL << 58) | (1ULL << 52)
8329                | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8330                | (insn2 & (31ULL << 21)));
8331       off = insn2 & 0xfffc;
8332       break;
8333
8334     case 57: /* lxsd, lxssp */
8335       if ((insn2 & 3) < 2)
8336         return FALSE;
8337       insn1 = ((1ULL << 58) | (1ULL << 52)
8338                | ((40ULL | (insn2 & 3)) << 26)
8339                | (insn2 & (31ULL << 21)));
8340       off = insn2 & 0xfffc;
8341       break;
8342
8343     case 61: /* stxsd, stxssp, lxv, stxv  */
8344       if ((insn2 & 3) == 0)
8345         return FALSE;
8346       else if ((insn2 & 3) >= 2)
8347         {
8348           insn1 = ((1ULL << 58) | (1ULL << 52)
8349                    | ((44ULL | (insn2 & 3)) << 26)
8350                    | (insn2 & (31ULL << 21)));
8351           off = insn2 & 0xfffc;
8352         }
8353       else
8354         {
8355           insn1 = ((1ULL << 58) | (1ULL << 52)
8356                    | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8357                    | (insn2 & (31ULL << 21)));
8358           off = insn2 & 0xfff0;
8359         }
8360       break;
8361
8362     case 56: /* lq */
8363       insn1 = ((1ULL << 58) | (1ULL << 52)
8364                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8365       off = insn2 & 0xffff;
8366       break;
8367
8368     case 62: /* std, stq */
8369       if ((insn2 & 1) != 0)
8370         return FALSE;
8371       insn1 = ((1ULL << 58) | (1ULL << 52)
8372                | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8373                | (insn2 & (31ULL << 21)));
8374       off = insn2 & 0xfffc;
8375       break;
8376     }
8377
8378   *pinsn1 = insn1;
8379   *pinsn2 = (uint64_t) NOP << 32;
8380   *poff = (off ^ 0x8000) - 0x8000;
8381   return TRUE;
8382 }
8383
8384 /* Examine all relocs referencing .toc sections in order to remove
8385    unused .toc entries.  */
8386
8387 bfd_boolean
8388 ppc64_elf_edit_toc (struct bfd_link_info *info)
8389 {
8390   bfd *ibfd;
8391   struct adjust_toc_info toc_inf;
8392   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8393
8394   htab->do_toc_opt = 1;
8395   toc_inf.global_toc_syms = TRUE;
8396   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8397     {
8398       asection *toc, *sec;
8399       Elf_Internal_Shdr *symtab_hdr;
8400       Elf_Internal_Sym *local_syms;
8401       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8402       unsigned long *skip, *drop;
8403       unsigned char *used;
8404       unsigned char *keep, last, some_unused;
8405
8406       if (!is_ppc64_elf (ibfd))
8407         continue;
8408
8409       toc = bfd_get_section_by_name (ibfd, ".toc");
8410       if (toc == NULL
8411           || toc->size == 0
8412           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8413           || discarded_section (toc))
8414         continue;
8415
8416       toc_relocs = NULL;
8417       local_syms = NULL;
8418       symtab_hdr = &elf_symtab_hdr (ibfd);
8419
8420       /* Look at sections dropped from the final link.  */
8421       skip = NULL;
8422       relstart = NULL;
8423       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8424         {
8425           if (sec->reloc_count == 0
8426               || !discarded_section (sec)
8427               || get_opd_info (sec)
8428               || (sec->flags & SEC_ALLOC) == 0
8429               || (sec->flags & SEC_DEBUGGING) != 0)
8430             continue;
8431
8432           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8433           if (relstart == NULL)
8434             goto error_ret;
8435
8436           /* Run through the relocs to see which toc entries might be
8437              unused.  */
8438           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8439             {
8440               enum elf_ppc64_reloc_type r_type;
8441               unsigned long r_symndx;
8442               asection *sym_sec;
8443               struct elf_link_hash_entry *h;
8444               Elf_Internal_Sym *sym;
8445               bfd_vma val;
8446
8447               r_type = ELF64_R_TYPE (rel->r_info);
8448               switch (r_type)
8449                 {
8450                 default:
8451                   continue;
8452
8453                 case R_PPC64_TOC16:
8454                 case R_PPC64_TOC16_LO:
8455                 case R_PPC64_TOC16_HI:
8456                 case R_PPC64_TOC16_HA:
8457                 case R_PPC64_TOC16_DS:
8458                 case R_PPC64_TOC16_LO_DS:
8459                   break;
8460                 }
8461
8462               r_symndx = ELF64_R_SYM (rel->r_info);
8463               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8464                               r_symndx, ibfd))
8465                 goto error_ret;
8466
8467               if (sym_sec != toc)
8468                 continue;
8469
8470               if (h != NULL)
8471                 val = h->root.u.def.value;
8472               else
8473                 val = sym->st_value;
8474               val += rel->r_addend;
8475
8476               if (val >= toc->size)
8477                 continue;
8478
8479               /* Anything in the toc ought to be aligned to 8 bytes.
8480                  If not, don't mark as unused.  */
8481               if (val & 7)
8482                 continue;
8483
8484               if (skip == NULL)
8485                 {
8486                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8487                   if (skip == NULL)
8488                     goto error_ret;
8489                 }
8490
8491               skip[val >> 3] = ref_from_discarded;
8492             }
8493
8494           if (elf_section_data (sec)->relocs != relstart)
8495             free (relstart);
8496         }
8497
8498       /* For largetoc loads of address constants, we can convert
8499          .  addis rx,2,addr@got@ha
8500          .  ld ry,addr@got@l(rx)
8501          to
8502          .  addis rx,2,addr@toc@ha
8503          .  addi ry,rx,addr@toc@l
8504          when addr is within 2G of the toc pointer.  This then means
8505          that the word storing "addr" in the toc is no longer needed.  */
8506
8507       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8508           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8509           && toc->reloc_count != 0)
8510         {
8511           /* Read toc relocs.  */
8512           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8513                                                   info->keep_memory);
8514           if (toc_relocs == NULL)
8515             goto error_ret;
8516
8517           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8518             {
8519               enum elf_ppc64_reloc_type r_type;
8520               unsigned long r_symndx;
8521               asection *sym_sec;
8522               struct elf_link_hash_entry *h;
8523               Elf_Internal_Sym *sym;
8524               bfd_vma val, addr;
8525
8526               r_type = ELF64_R_TYPE (rel->r_info);
8527               if (r_type != R_PPC64_ADDR64)
8528                 continue;
8529
8530               r_symndx = ELF64_R_SYM (rel->r_info);
8531               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8532                               r_symndx, ibfd))
8533                 goto error_ret;
8534
8535               if (sym_sec == NULL
8536                   || sym_sec->output_section == NULL
8537                   || discarded_section (sym_sec))
8538                 continue;
8539
8540               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8541                 continue;
8542
8543               if (h != NULL)
8544                 {
8545                   if (h->type == STT_GNU_IFUNC)
8546                     continue;
8547                   val = h->root.u.def.value;
8548                 }
8549               else
8550                 {
8551                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8552                     continue;
8553                   val = sym->st_value;
8554                 }
8555               val += rel->r_addend;
8556               val += sym_sec->output_section->vma + sym_sec->output_offset;
8557
8558               /* We don't yet know the exact toc pointer value, but we
8559                  know it will be somewhere in the toc section.  Don't
8560                  optimize if the difference from any possible toc
8561                  pointer is outside [ff..f80008000, 7fff7fff].  */
8562               addr = toc->output_section->vma + TOC_BASE_OFF;
8563               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8564                 continue;
8565
8566               addr = toc->output_section->vma + toc->output_section->rawsize;
8567               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8568                 continue;
8569
8570               if (skip == NULL)
8571                 {
8572                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8573                   if (skip == NULL)
8574                     goto error_ret;
8575                 }
8576
8577               skip[rel->r_offset >> 3]
8578                 |= can_optimize | ((rel - toc_relocs) << 2);
8579             }
8580         }
8581
8582       if (skip == NULL)
8583         continue;
8584
8585       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8586       if (used == NULL)
8587         {
8588         error_ret:
8589           if (local_syms != NULL
8590               && symtab_hdr->contents != (unsigned char *) local_syms)
8591             free (local_syms);
8592           if (sec != NULL
8593               && relstart != NULL
8594               && elf_section_data (sec)->relocs != relstart)
8595             free (relstart);
8596           if (toc_relocs != NULL
8597               && elf_section_data (toc)->relocs != toc_relocs)
8598             free (toc_relocs);
8599           if (skip != NULL)
8600             free (skip);
8601           return FALSE;
8602         }
8603
8604       /* Now check all kept sections that might reference the toc.
8605          Check the toc itself last.  */
8606       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8607                   : ibfd->sections);
8608            sec != NULL;
8609            sec = (sec == toc ? NULL
8610                   : sec->next == NULL ? toc
8611                   : sec->next == toc && toc->next ? toc->next
8612                   : sec->next))
8613         {
8614           int repeat;
8615
8616           if (sec->reloc_count == 0
8617               || discarded_section (sec)
8618               || get_opd_info (sec)
8619               || (sec->flags & SEC_ALLOC) == 0
8620               || (sec->flags & SEC_DEBUGGING) != 0)
8621             continue;
8622
8623           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8624                                                 info->keep_memory);
8625           if (relstart == NULL)
8626             {
8627               free (used);
8628               goto error_ret;
8629             }
8630
8631           /* Mark toc entries referenced as used.  */
8632           do
8633             {
8634               repeat = 0;
8635               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8636                 {
8637                   enum elf_ppc64_reloc_type r_type;
8638                   unsigned long r_symndx;
8639                   asection *sym_sec;
8640                   struct elf_link_hash_entry *h;
8641                   Elf_Internal_Sym *sym;
8642                   bfd_vma val;
8643
8644                   r_type = ELF64_R_TYPE (rel->r_info);
8645                   switch (r_type)
8646                     {
8647                     case R_PPC64_TOC16:
8648                     case R_PPC64_TOC16_LO:
8649                     case R_PPC64_TOC16_HI:
8650                     case R_PPC64_TOC16_HA:
8651                     case R_PPC64_TOC16_DS:
8652                     case R_PPC64_TOC16_LO_DS:
8653                       /* In case we're taking addresses of toc entries.  */
8654                     case R_PPC64_ADDR64:
8655                       break;
8656
8657                     default:
8658                       continue;
8659                     }
8660
8661                   r_symndx = ELF64_R_SYM (rel->r_info);
8662                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8663                                   r_symndx, ibfd))
8664                     {
8665                       free (used);
8666                       goto error_ret;
8667                     }
8668
8669                   if (sym_sec != toc)
8670                     continue;
8671
8672                   if (h != NULL)
8673                     val = h->root.u.def.value;
8674                   else
8675                     val = sym->st_value;
8676                   val += rel->r_addend;
8677
8678                   if (val >= toc->size)
8679                     continue;
8680
8681                   if ((skip[val >> 3] & can_optimize) != 0)
8682                     {
8683                       bfd_vma off;
8684                       unsigned char opc;
8685
8686                       switch (r_type)
8687                         {
8688                         case R_PPC64_TOC16_HA:
8689                           break;
8690
8691                         case R_PPC64_TOC16_LO_DS:
8692                           off = rel->r_offset;
8693                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8694                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8695                                                          off, 1))
8696                             {
8697                               free (used);
8698                               goto error_ret;
8699                             }
8700                           if ((opc & (0x3f << 2)) == (58u << 2))
8701                             break;
8702                           /* Fall through.  */
8703
8704                         default:
8705                           /* Wrong sort of reloc, or not a ld.  We may
8706                              as well clear ref_from_discarded too.  */
8707                           skip[val >> 3] = 0;
8708                         }
8709                     }
8710
8711                   if (sec != toc)
8712                     used[val >> 3] = 1;
8713                   /* For the toc section, we only mark as used if this
8714                      entry itself isn't unused.  */
8715                   else if ((used[rel->r_offset >> 3]
8716                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8717                            && !used[val >> 3])
8718                     {
8719                       /* Do all the relocs again, to catch reference
8720                          chains.  */
8721                       repeat = 1;
8722                       used[val >> 3] = 1;
8723                     }
8724                 }
8725             }
8726           while (repeat);
8727
8728           if (elf_section_data (sec)->relocs != relstart)
8729             free (relstart);
8730         }
8731
8732       /* Merge the used and skip arrays.  Assume that TOC
8733          doublewords not appearing as either used or unused belong
8734          to an entry more than one doubleword in size.  */
8735       for (drop = skip, keep = used, last = 0, some_unused = 0;
8736            drop < skip + (toc->size + 7) / 8;
8737            ++drop, ++keep)
8738         {
8739           if (*keep)
8740             {
8741               *drop &= ~ref_from_discarded;
8742               if ((*drop & can_optimize) != 0)
8743                 some_unused = 1;
8744               last = 0;
8745             }
8746           else if ((*drop & ref_from_discarded) != 0)
8747             {
8748               some_unused = 1;
8749               last = ref_from_discarded;
8750             }
8751           else
8752             *drop = last;
8753         }
8754
8755       free (used);
8756
8757       if (some_unused)
8758         {
8759           bfd_byte *contents, *src;
8760           unsigned long off;
8761           Elf_Internal_Sym *sym;
8762           bfd_boolean local_toc_syms = FALSE;
8763
8764           /* Shuffle the toc contents, and at the same time convert the
8765              skip array from booleans into offsets.  */
8766           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8767             goto error_ret;
8768
8769           elf_section_data (toc)->this_hdr.contents = contents;
8770
8771           for (src = contents, off = 0, drop = skip;
8772                src < contents + toc->size;
8773                src += 8, ++drop)
8774             {
8775               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8776                 off += 8;
8777               else if (off != 0)
8778                 {
8779                   *drop = off;
8780                   memcpy (src - off, src, 8);
8781                 }
8782             }
8783           *drop = off;
8784           toc->rawsize = toc->size;
8785           toc->size = src - contents - off;
8786
8787           /* Adjust addends for relocs against the toc section sym,
8788              and optimize any accesses we can.  */
8789           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8790             {
8791               if (sec->reloc_count == 0
8792                   || discarded_section (sec))
8793                 continue;
8794
8795               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8796                                                     info->keep_memory);
8797               if (relstart == NULL)
8798                 goto error_ret;
8799
8800               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8801                 {
8802                   enum elf_ppc64_reloc_type r_type;
8803                   unsigned long r_symndx;
8804                   asection *sym_sec;
8805                   struct elf_link_hash_entry *h;
8806                   bfd_vma val;
8807
8808                   r_type = ELF64_R_TYPE (rel->r_info);
8809                   switch (r_type)
8810                     {
8811                     default:
8812                       continue;
8813
8814                     case R_PPC64_TOC16:
8815                     case R_PPC64_TOC16_LO:
8816                     case R_PPC64_TOC16_HI:
8817                     case R_PPC64_TOC16_HA:
8818                     case R_PPC64_TOC16_DS:
8819                     case R_PPC64_TOC16_LO_DS:
8820                     case R_PPC64_ADDR64:
8821                       break;
8822                     }
8823
8824                   r_symndx = ELF64_R_SYM (rel->r_info);
8825                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8826                                   r_symndx, ibfd))
8827                     goto error_ret;
8828
8829                   if (sym_sec != toc)
8830                     continue;
8831
8832                   if (h != NULL)
8833                     val = h->root.u.def.value;
8834                   else
8835                     {
8836                       val = sym->st_value;
8837                       if (val != 0)
8838                         local_toc_syms = TRUE;
8839                     }
8840
8841                   val += rel->r_addend;
8842
8843                   if (val > toc->rawsize)
8844                     val = toc->rawsize;
8845                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8846                     continue;
8847                   else if ((skip[val >> 3] & can_optimize) != 0)
8848                     {
8849                       Elf_Internal_Rela *tocrel
8850                         = toc_relocs + (skip[val >> 3] >> 2);
8851                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8852
8853                       switch (r_type)
8854                         {
8855                         case R_PPC64_TOC16_HA:
8856                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8857                           break;
8858
8859                         case R_PPC64_TOC16_LO_DS:
8860                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8861                           break;
8862
8863                         default:
8864                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8865                             ppc_howto_init ();
8866                           info->callbacks->einfo
8867                             /* xgettext:c-format */
8868                             (_("%H: %s references "
8869                                "optimized away TOC entry\n"),
8870                              ibfd, sec, rel->r_offset,
8871                              ppc64_elf_howto_table[r_type]->name);
8872                           bfd_set_error (bfd_error_bad_value);
8873                           goto error_ret;
8874                         }
8875                       rel->r_addend = tocrel->r_addend;
8876                       elf_section_data (sec)->relocs = relstart;
8877                       continue;
8878                     }
8879
8880                   if (h != NULL || sym->st_value != 0)
8881                     continue;
8882
8883                   rel->r_addend -= skip[val >> 3];
8884                   elf_section_data (sec)->relocs = relstart;
8885                 }
8886
8887               if (elf_section_data (sec)->relocs != relstart)
8888                 free (relstart);
8889             }
8890
8891           /* We shouldn't have local or global symbols defined in the TOC,
8892              but handle them anyway.  */
8893           if (local_syms != NULL)
8894             for (sym = local_syms;
8895                  sym < local_syms + symtab_hdr->sh_info;
8896                  ++sym)
8897               if (sym->st_value != 0
8898                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8899                 {
8900                   unsigned long i;
8901
8902                   if (sym->st_value > toc->rawsize)
8903                     i = toc->rawsize >> 3;
8904                   else
8905                     i = sym->st_value >> 3;
8906
8907                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8908                     {
8909                       if (local_toc_syms)
8910                         _bfd_error_handler
8911                           (_("%s defined on removed toc entry"),
8912                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8913                       do
8914                         ++i;
8915                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8916                       sym->st_value = (bfd_vma) i << 3;
8917                     }
8918
8919                   sym->st_value -= skip[i];
8920                   symtab_hdr->contents = (unsigned char *) local_syms;
8921                 }
8922
8923           /* Adjust any global syms defined in this toc input section.  */
8924           if (toc_inf.global_toc_syms)
8925             {
8926               toc_inf.toc = toc;
8927               toc_inf.skip = skip;
8928               toc_inf.global_toc_syms = FALSE;
8929               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8930                                       &toc_inf);
8931             }
8932
8933           if (toc->reloc_count != 0)
8934             {
8935               Elf_Internal_Shdr *rel_hdr;
8936               Elf_Internal_Rela *wrel;
8937               bfd_size_type sz;
8938
8939               /* Remove unused toc relocs, and adjust those we keep.  */
8940               if (toc_relocs == NULL)
8941                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8942                                                         info->keep_memory);
8943               if (toc_relocs == NULL)
8944                 goto error_ret;
8945
8946               wrel = toc_relocs;
8947               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8948                 if ((skip[rel->r_offset >> 3]
8949                      & (ref_from_discarded | can_optimize)) == 0)
8950                   {
8951                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8952                     wrel->r_info = rel->r_info;
8953                     wrel->r_addend = rel->r_addend;
8954                     ++wrel;
8955                   }
8956                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8957                                             &local_syms, NULL, NULL))
8958                   goto error_ret;
8959
8960               elf_section_data (toc)->relocs = toc_relocs;
8961               toc->reloc_count = wrel - toc_relocs;
8962               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8963               sz = rel_hdr->sh_entsize;
8964               rel_hdr->sh_size = toc->reloc_count * sz;
8965             }
8966         }
8967       else if (toc_relocs != NULL
8968                && elf_section_data (toc)->relocs != toc_relocs)
8969         free (toc_relocs);
8970
8971       if (local_syms != NULL
8972           && symtab_hdr->contents != (unsigned char *) local_syms)
8973         {
8974           if (!info->keep_memory)
8975             free (local_syms);
8976           else
8977             symtab_hdr->contents = (unsigned char *) local_syms;
8978         }
8979       free (skip);
8980     }
8981
8982   /* Look for cases where we can change an indirect GOT access to
8983      a GOT relative or PC relative access, possibly reducing the
8984      number of GOT entries.  */
8985   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8986     {
8987       asection *sec;
8988       Elf_Internal_Shdr *symtab_hdr;
8989       Elf_Internal_Sym *local_syms;
8990       Elf_Internal_Rela *relstart, *rel;
8991       bfd_vma got;
8992
8993       if (!is_ppc64_elf (ibfd))
8994         continue;
8995
8996       if (!ppc64_elf_tdata (ibfd)->has_optrel)
8997         continue;
8998
8999       sec = ppc64_elf_tdata (ibfd)->got;
9000       got = 0;
9001       if (sec != NULL)
9002         got = sec->output_section->vma + sec->output_offset + 0x8000;
9003
9004       local_syms = NULL;
9005       symtab_hdr = &elf_symtab_hdr (ibfd);
9006
9007       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9008         {
9009           if (sec->reloc_count == 0
9010               || !ppc64_elf_section_data (sec)->has_optrel
9011               || discarded_section (sec))
9012             continue;
9013
9014           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9015                                                 info->keep_memory);
9016           if (relstart == NULL)
9017             {
9018             got_error_ret:
9019               if (local_syms != NULL
9020                   && symtab_hdr->contents != (unsigned char *) local_syms)
9021                 free (local_syms);
9022               if (sec != NULL
9023                   && relstart != NULL
9024                   && elf_section_data (sec)->relocs != relstart)
9025                 free (relstart);
9026               return FALSE;
9027             }
9028
9029           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9030             {
9031               enum elf_ppc64_reloc_type r_type;
9032               unsigned long r_symndx;
9033               Elf_Internal_Sym *sym;
9034               asection *sym_sec;
9035               struct elf_link_hash_entry *h;
9036               struct got_entry *ent;
9037               bfd_vma val, pc;
9038               unsigned char buf[8];
9039               unsigned int insn;
9040               enum {no_check, check_lo, check_ha} insn_check;
9041
9042               r_type = ELF64_R_TYPE (rel->r_info);
9043               switch (r_type)
9044                 {
9045                 default:
9046                   insn_check = no_check;
9047                   break;
9048
9049                 case R_PPC64_PLT16_HA:
9050                 case R_PPC64_GOT_TLSLD16_HA:
9051                 case R_PPC64_GOT_TLSGD16_HA:
9052                 case R_PPC64_GOT_TPREL16_HA:
9053                 case R_PPC64_GOT_DTPREL16_HA:
9054                 case R_PPC64_GOT16_HA:
9055                 case R_PPC64_TOC16_HA:
9056                   insn_check = check_ha;
9057                   break;
9058
9059                 case R_PPC64_PLT16_LO:
9060                 case R_PPC64_PLT16_LO_DS:
9061                 case R_PPC64_GOT_TLSLD16_LO:
9062                 case R_PPC64_GOT_TLSGD16_LO:
9063                 case R_PPC64_GOT_TPREL16_LO_DS:
9064                 case R_PPC64_GOT_DTPREL16_LO_DS:
9065                 case R_PPC64_GOT16_LO:
9066                 case R_PPC64_GOT16_LO_DS:
9067                 case R_PPC64_TOC16_LO:
9068                 case R_PPC64_TOC16_LO_DS:
9069                   insn_check = check_lo;
9070                   break;
9071                 }
9072
9073               if (insn_check != no_check)
9074                 {
9075                   bfd_vma off = rel->r_offset & ~3;
9076
9077                   if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9078                     goto got_error_ret;
9079
9080                   insn = bfd_get_32 (ibfd, buf);
9081                   if (insn_check == check_lo
9082                       ? !ok_lo_toc_insn (insn, r_type)
9083                       : ((insn & ((0x3f << 26) | 0x1f << 16))
9084                          != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9085                     {
9086                       char str[12];
9087
9088                       ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9089                       sprintf (str, "%#08x", insn);
9090                       info->callbacks->einfo
9091                         /* xgettext:c-format */
9092                         (_("%H: got/toc optimization is not supported for"
9093                            " %s instruction\n"),
9094                          ibfd, sec, rel->r_offset & ~3, str);
9095                       continue;
9096                     }
9097                 }
9098
9099               switch (r_type)
9100                 {
9101                 /* Note that we don't delete GOT entries for
9102                    R_PPC64_GOT16_DS since we'd need a lot more
9103                    analysis.  For starters, the preliminary layout is
9104                    before the GOT, PLT, dynamic sections and stubs are
9105                    laid out.  Then we'd need to allow for changes in
9106                    distance between sections caused by alignment.  */
9107                 default:
9108                   continue;
9109
9110                 case R_PPC64_GOT16_HA:
9111                 case R_PPC64_GOT16_LO_DS:
9112                 case R_PPC64_GOT_PCREL34:
9113                   break;
9114                 }
9115
9116               r_symndx = ELF64_R_SYM (rel->r_info);
9117               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9118                               r_symndx, ibfd))
9119                 goto got_error_ret;
9120
9121               if (sym_sec == NULL
9122                   || sym_sec->output_section == NULL
9123                   || discarded_section (sym_sec))
9124                 continue;
9125
9126               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9127                 continue;
9128
9129               if (h != NULL)
9130                 val = h->root.u.def.value;
9131               else
9132                 val = sym->st_value;
9133               val += rel->r_addend;
9134               val += sym_sec->output_section->vma + sym_sec->output_offset;
9135
9136 /* Fudge factor to allow for the fact that the preliminary layout
9137    isn't exact.  Reduce limits by this factor.  */
9138 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9139
9140               switch (r_type)
9141                 {
9142                 default:
9143                   continue;
9144
9145                 case R_PPC64_GOT16_HA:
9146                   if (val - got + LIMIT_ADJUST (0x80008000ULL)
9147                       >= LIMIT_ADJUST (0x100000000ULL))
9148                     continue;
9149
9150                   if (!bfd_get_section_contents (ibfd, sec, buf,
9151                                                  rel->r_offset & ~3, 4))
9152                     goto got_error_ret;
9153                   insn = bfd_get_32 (ibfd, buf);
9154                   if (((insn & ((0x3f << 26) | 0x1f << 16))
9155                        != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9156                     continue;
9157                   break;
9158
9159                 case R_PPC64_GOT16_LO_DS:
9160                   if (val - got + LIMIT_ADJUST (0x80008000ULL)
9161                       >= LIMIT_ADJUST (0x100000000ULL))
9162                     continue;
9163                   if (!bfd_get_section_contents (ibfd, sec, buf,
9164                                                  rel->r_offset & ~3, 4))
9165                     goto got_error_ret;
9166                   insn = bfd_get_32 (ibfd, buf);
9167                   if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
9168                     continue;
9169                   break;
9170
9171                 case R_PPC64_GOT_PCREL34:
9172                   pc = rel->r_offset;
9173                   pc += sec->output_section->vma + sec->output_offset;
9174                   if (val - pc + LIMIT_ADJUST (1ULL << 33)
9175                       >= LIMIT_ADJUST (1ULL << 34))
9176                     continue;
9177                   if (!bfd_get_section_contents (ibfd, sec, buf,
9178                                                  rel->r_offset & ~3, 8))
9179                     goto got_error_ret;
9180                   insn = bfd_get_32 (ibfd, buf);
9181                   if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9182                     continue;
9183                   insn = bfd_get_32 (ibfd, buf + 4);
9184                   if ((insn & (0x3f << 26)) != 57u << 26)
9185                     continue;
9186                   break;
9187                 }
9188 #undef LIMIT_ADJUST
9189
9190               if (h != NULL)
9191                 ent = h->got.glist;
9192               else
9193                 {
9194                   struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9195                   ent = local_got_ents[r_symndx];
9196                 }
9197               for (; ent != NULL; ent = ent->next)
9198                 if (ent->addend == rel->r_addend
9199                     && ent->owner == ibfd
9200                     && ent->tls_type == 0)
9201                   break;
9202               BFD_ASSERT (ent && ent->got.refcount > 0);
9203               ent->got.refcount -= 1;
9204             }
9205
9206           if (elf_section_data (sec)->relocs != relstart)
9207             free (relstart);
9208         }
9209
9210       if (local_syms != NULL
9211           && symtab_hdr->contents != (unsigned char *) local_syms)
9212         {
9213           if (!info->keep_memory)
9214             free (local_syms);
9215           else
9216             symtab_hdr->contents = (unsigned char *) local_syms;
9217         }
9218     }
9219
9220   return TRUE;
9221 }
9222
9223 /* Return true iff input section I references the TOC using
9224    instructions limited to +/-32k offsets.  */
9225
9226 bfd_boolean
9227 ppc64_elf_has_small_toc_reloc (asection *i)
9228 {
9229   return (is_ppc64_elf (i->owner)
9230           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9231 }
9232
9233 /* Allocate space for one GOT entry.  */
9234
9235 static void
9236 allocate_got (struct elf_link_hash_entry *h,
9237               struct bfd_link_info *info,
9238               struct got_entry *gent)
9239 {
9240   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9241   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9242   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9243                  ? 16 : 8);
9244   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9245                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9246   asection *got = ppc64_elf_tdata (gent->owner)->got;
9247
9248   gent->got.offset = got->size;
9249   got->size += entsize;
9250
9251   if (h->type == STT_GNU_IFUNC)
9252     {
9253       htab->elf.irelplt->size += rentsize;
9254       htab->got_reli_size += rentsize;
9255     }
9256   else if (((bfd_link_pic (info)
9257              && !(gent->tls_type != 0
9258                   && bfd_link_executable (info)
9259                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9260             || (htab->elf.dynamic_sections_created
9261                 && h->dynindx != -1
9262                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9263            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9264     {
9265       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9266       relgot->size += rentsize;
9267     }
9268 }
9269
9270 /* This function merges got entries in the same toc group.  */
9271
9272 static void
9273 merge_got_entries (struct got_entry **pent)
9274 {
9275   struct got_entry *ent, *ent2;
9276
9277   for (ent = *pent; ent != NULL; ent = ent->next)
9278     if (!ent->is_indirect)
9279       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9280         if (!ent2->is_indirect
9281             && ent2->addend == ent->addend
9282             && ent2->tls_type == ent->tls_type
9283             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9284           {
9285             ent2->is_indirect = TRUE;
9286             ent2->got.ent = ent;
9287           }
9288 }
9289
9290 /* If H is undefined, make it dynamic if that makes sense.  */
9291
9292 static bfd_boolean
9293 ensure_undef_dynamic (struct bfd_link_info *info,
9294                       struct elf_link_hash_entry *h)
9295 {
9296   struct elf_link_hash_table *htab = elf_hash_table (info);
9297
9298   if (htab->dynamic_sections_created
9299       && ((info->dynamic_undefined_weak != 0
9300            && h->root.type == bfd_link_hash_undefweak)
9301           || h->root.type == bfd_link_hash_undefined)
9302       && h->dynindx == -1
9303       && !h->forced_local
9304       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9305     return bfd_elf_link_record_dynamic_symbol (info, h);
9306   return TRUE;
9307 }
9308
9309 /* Allocate space in .plt, .got and associated reloc sections for
9310    dynamic relocs.  */
9311
9312 static bfd_boolean
9313 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9314 {
9315   struct bfd_link_info *info;
9316   struct ppc_link_hash_table *htab;
9317   asection *s;
9318   struct ppc_link_hash_entry *eh;
9319   struct got_entry **pgent, *gent;
9320
9321   if (h->root.type == bfd_link_hash_indirect)
9322     return TRUE;
9323
9324   info = (struct bfd_link_info *) inf;
9325   htab = ppc_hash_table (info);
9326   if (htab == NULL)
9327     return FALSE;
9328
9329   eh = (struct ppc_link_hash_entry *) h;
9330   /* Run through the TLS GD got entries first if we're changing them
9331      to TPREL.  */
9332   if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9333     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9334       if (gent->got.refcount > 0
9335           && (gent->tls_type & TLS_GD) != 0)
9336         {
9337           /* This was a GD entry that has been converted to TPREL.  If
9338              there happens to be a TPREL entry we can use that one.  */
9339           struct got_entry *ent;
9340           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9341             if (ent->got.refcount > 0
9342                 && (ent->tls_type & TLS_TPREL) != 0
9343                 && ent->addend == gent->addend
9344                 && ent->owner == gent->owner)
9345               {
9346                 gent->got.refcount = 0;
9347                 break;
9348               }
9349
9350           /* If not, then we'll be using our own TPREL entry.  */
9351           if (gent->got.refcount != 0)
9352             gent->tls_type = TLS_TLS | TLS_TPREL;
9353         }
9354
9355   /* Remove any list entry that won't generate a word in the GOT before
9356      we call merge_got_entries.  Otherwise we risk merging to empty
9357      entries.  */
9358   pgent = &h->got.glist;
9359   while ((gent = *pgent) != NULL)
9360     if (gent->got.refcount > 0)
9361       {
9362         if ((gent->tls_type & TLS_LD) != 0
9363             && SYMBOL_REFERENCES_LOCAL (info, h))
9364           {
9365             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9366             *pgent = gent->next;
9367           }
9368         else
9369           pgent = &gent->next;
9370       }
9371     else
9372       *pgent = gent->next;
9373
9374   if (!htab->do_multi_toc)
9375     merge_got_entries (&h->got.glist);
9376
9377   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9378     if (!gent->is_indirect)
9379       {
9380         /* Make sure this symbol is output as a dynamic symbol.  */
9381         if (!ensure_undef_dynamic (info, h))
9382           return FALSE;
9383
9384         if (!is_ppc64_elf (gent->owner))
9385           abort ();
9386
9387         allocate_got (h, info, gent);
9388       }
9389
9390   /* If no dynamic sections we can't have dynamic relocs, except for
9391      IFUNCs which are handled even in static executables.  */
9392   if (!htab->elf.dynamic_sections_created
9393       && h->type != STT_GNU_IFUNC)
9394     eh->dyn_relocs = NULL;
9395
9396   /* Discard relocs on undefined symbols that must be local.  */
9397   else if (h->root.type == bfd_link_hash_undefined
9398            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9399     eh->dyn_relocs = NULL;
9400
9401   /* Also discard relocs on undefined weak syms with non-default
9402      visibility, or when dynamic_undefined_weak says so.  */
9403   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9404     eh->dyn_relocs = NULL;
9405
9406   if (eh->dyn_relocs != NULL)
9407     {
9408       struct elf_dyn_relocs *p, **pp;
9409
9410       /* In the shared -Bsymbolic case, discard space allocated for
9411          dynamic pc-relative relocs against symbols which turn out to
9412          be defined in regular objects.  For the normal shared case,
9413          discard space for relocs that have become local due to symbol
9414          visibility changes.  */
9415
9416       if (bfd_link_pic (info))
9417         {
9418           /* Relocs that use pc_count are those that appear on a call
9419              insn, or certain REL relocs (see must_be_dyn_reloc) that
9420              can be generated via assembly.  We want calls to
9421              protected symbols to resolve directly to the function
9422              rather than going via the plt.  If people want function
9423              pointer comparisons to work as expected then they should
9424              avoid writing weird assembly.  */
9425           if (SYMBOL_CALLS_LOCAL (info, h))
9426             {
9427               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9428                 {
9429                   p->count -= p->pc_count;
9430                   p->pc_count = 0;
9431                   if (p->count == 0)
9432                     *pp = p->next;
9433                   else
9434                     pp = &p->next;
9435                 }
9436             }
9437
9438           if (eh->dyn_relocs != NULL)
9439             {
9440               /* Make sure this symbol is output as a dynamic symbol.  */
9441               if (!ensure_undef_dynamic (info, h))
9442                 return FALSE;
9443             }
9444         }
9445       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9446         {
9447           /* For the non-pic case, discard space for relocs against
9448              symbols which turn out to need copy relocs or are not
9449              dynamic.  */
9450           if (h->dynamic_adjusted
9451               && !h->def_regular
9452               && !ELF_COMMON_DEF_P (h))
9453             {
9454               /* Make sure this symbol is output as a dynamic symbol.  */
9455               if (!ensure_undef_dynamic (info, h))
9456                 return FALSE;
9457
9458               if (h->dynindx == -1)
9459                 eh->dyn_relocs = NULL;
9460             }
9461           else
9462             eh->dyn_relocs = NULL;
9463         }
9464
9465       /* Finally, allocate space.  */
9466       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9467         {
9468           asection *sreloc = elf_section_data (p->sec)->sreloc;
9469           if (eh->elf.type == STT_GNU_IFUNC)
9470             sreloc = htab->elf.irelplt;
9471           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9472         }
9473     }
9474
9475   /* We might need a PLT entry when the symbol
9476      a) is dynamic, or
9477      b) is an ifunc, or
9478      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9479      d) has plt16 relocs and we are linking statically.  */
9480   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9481       || h->type == STT_GNU_IFUNC
9482       || (h->needs_plt && h->dynamic_adjusted)
9483       || (h->needs_plt
9484           && h->def_regular
9485           && !htab->elf.dynamic_sections_created
9486           && !htab->can_convert_all_inline_plt
9487           && (((struct ppc_link_hash_entry *) h)->tls_mask
9488               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9489     {
9490       struct plt_entry *pent;
9491       bfd_boolean doneone = FALSE;
9492       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9493         if (pent->plt.refcount > 0)
9494           {
9495             if (!htab->elf.dynamic_sections_created
9496                 || h->dynindx == -1)
9497               {
9498                 if (h->type == STT_GNU_IFUNC)
9499                   {
9500                     s = htab->elf.iplt;
9501                     pent->plt.offset = s->size;
9502                     s->size += PLT_ENTRY_SIZE (htab);
9503                     s = htab->elf.irelplt;
9504                   }
9505                 else
9506                   {
9507                     s = htab->pltlocal;
9508                     pent->plt.offset = s->size;
9509                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9510                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9511                   }
9512               }
9513             else
9514               {
9515                 /* If this is the first .plt entry, make room for the special
9516                    first entry.  */
9517                 s = htab->elf.splt;
9518                 if (s->size == 0)
9519                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9520
9521                 pent->plt.offset = s->size;
9522
9523                 /* Make room for this entry.  */
9524                 s->size += PLT_ENTRY_SIZE (htab);
9525
9526                 /* Make room for the .glink code.  */
9527                 s = htab->glink;
9528                 if (s->size == 0)
9529                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9530                 if (htab->opd_abi)
9531                   {
9532                     /* We need bigger stubs past index 32767.  */
9533                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9534                       s->size += 4;
9535                     s->size += 2*4;
9536                   }
9537                 else
9538                   s->size += 4;
9539
9540                 /* We also need to make an entry in the .rela.plt section.  */
9541                 s = htab->elf.srelplt;
9542               }
9543             if (s != NULL)
9544               s->size += sizeof (Elf64_External_Rela);
9545             doneone = TRUE;
9546           }
9547         else
9548           pent->plt.offset = (bfd_vma) -1;
9549       if (!doneone)
9550         {
9551           h->plt.plist = NULL;
9552           h->needs_plt = 0;
9553         }
9554     }
9555   else
9556     {
9557       h->plt.plist = NULL;
9558       h->needs_plt = 0;
9559     }
9560
9561   return TRUE;
9562 }
9563
9564 #define PPC_LO(v) ((v) & 0xffff)
9565 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9566 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9567 #define D34(v) \
9568   ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9569 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9570
9571 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9572    to set up space for global entry stubs.  These are put in glink,
9573    after the branch table.  */
9574
9575 static bfd_boolean
9576 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9577 {
9578   struct bfd_link_info *info;
9579   struct ppc_link_hash_table *htab;
9580   struct plt_entry *pent;
9581   asection *s, *plt;
9582
9583   if (h->root.type == bfd_link_hash_indirect)
9584     return TRUE;
9585
9586   if (!h->pointer_equality_needed)
9587     return TRUE;
9588
9589   if (h->def_regular)
9590     return TRUE;
9591
9592   info = inf;
9593   htab = ppc_hash_table (info);
9594   if (htab == NULL)
9595     return FALSE;
9596
9597   s = htab->global_entry;
9598   plt = htab->elf.splt;
9599   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9600     if (pent->plt.offset != (bfd_vma) -1
9601         && pent->addend == 0)
9602       {
9603         /* For ELFv2, if this symbol is not defined in a regular file
9604            and we are not generating a shared library or pie, then we
9605            need to define the symbol in the executable on a call stub.
9606            This is to avoid text relocations.  */
9607         bfd_vma off, stub_align, stub_off, stub_size;
9608         unsigned int align_power;
9609
9610         stub_size = 16;
9611         stub_off = s->size;
9612         if (htab->params->plt_stub_align >= 0)
9613           align_power = htab->params->plt_stub_align;
9614         else
9615           align_power = -htab->params->plt_stub_align;
9616         /* Setting section alignment is delayed until we know it is
9617            non-empty.  Otherwise the .text output section will be
9618            aligned at least to plt_stub_align even when no global
9619            entry stubs are needed.  */
9620         if (s->alignment_power < align_power)
9621           s->alignment_power = align_power;
9622         stub_align = (bfd_vma) 1 << align_power;
9623         if (htab->params->plt_stub_align >= 0
9624             || ((((stub_off + stub_size - 1) & -stub_align)
9625                  - (stub_off & -stub_align))
9626                 > ((stub_size - 1) & -stub_align)))
9627           stub_off = (stub_off + stub_align - 1) & -stub_align;
9628         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9629         off -= stub_off + s->output_offset + s->output_section->vma;
9630         /* Note that for --plt-stub-align negative we have a possible
9631            dependency between stub offset and size.  Break that
9632            dependency by assuming the max stub size when calculating
9633            the stub offset.  */
9634         if (PPC_HA (off) == 0)
9635           stub_size -= 4;
9636         h->root.type = bfd_link_hash_defined;
9637         h->root.u.def.section = s;
9638         h->root.u.def.value = stub_off;
9639         s->size = stub_off + stub_size;
9640         break;
9641       }
9642   return TRUE;
9643 }
9644
9645 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9646    read-only sections.  */
9647
9648 static bfd_boolean
9649 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9650 {
9651   asection *sec;
9652
9653   if (h->root.type == bfd_link_hash_indirect)
9654     return TRUE;
9655
9656   sec = readonly_dynrelocs (h);
9657   if (sec != NULL)
9658     {
9659       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9660
9661       info->flags |= DF_TEXTREL;
9662       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9663                                 " in read-only section `%pA'\n"),
9664                               sec->owner, h->root.root.string, sec);
9665
9666       /* Not an error, just cut short the traversal.  */
9667       return FALSE;
9668     }
9669   return TRUE;
9670 }
9671
9672 /* Set the sizes of the dynamic sections.  */
9673
9674 static bfd_boolean
9675 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9676                                  struct bfd_link_info *info)
9677 {
9678   struct ppc_link_hash_table *htab;
9679   bfd *dynobj;
9680   asection *s;
9681   bfd_boolean relocs;
9682   bfd *ibfd;
9683   struct got_entry *first_tlsld;
9684
9685   htab = ppc_hash_table (info);
9686   if (htab == NULL)
9687     return FALSE;
9688
9689   dynobj = htab->elf.dynobj;
9690   if (dynobj == NULL)
9691     abort ();
9692
9693   if (htab->elf.dynamic_sections_created)
9694     {
9695       /* Set the contents of the .interp section to the interpreter.  */
9696       if (bfd_link_executable (info) && !info->nointerp)
9697         {
9698           s = bfd_get_linker_section (dynobj, ".interp");
9699           if (s == NULL)
9700             abort ();
9701           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9702           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9703         }
9704     }
9705
9706   /* Set up .got offsets for local syms, and space for local dynamic
9707      relocs.  */
9708   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9709     {
9710       struct got_entry **lgot_ents;
9711       struct got_entry **end_lgot_ents;
9712       struct plt_entry **local_plt;
9713       struct plt_entry **end_local_plt;
9714       unsigned char *lgot_masks;
9715       bfd_size_type locsymcount;
9716       Elf_Internal_Shdr *symtab_hdr;
9717
9718       if (!is_ppc64_elf (ibfd))
9719         continue;
9720
9721       for (s = ibfd->sections; s != NULL; s = s->next)
9722         {
9723           struct ppc_dyn_relocs *p;
9724
9725           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9726             {
9727               if (!bfd_is_abs_section (p->sec)
9728                   && bfd_is_abs_section (p->sec->output_section))
9729                 {
9730                   /* Input section has been discarded, either because
9731                      it is a copy of a linkonce section or due to
9732                      linker script /DISCARD/, so we'll be discarding
9733                      the relocs too.  */
9734                 }
9735               else if (p->count != 0)
9736                 {
9737                   asection *srel = elf_section_data (p->sec)->sreloc;
9738                   if (p->ifunc)
9739                     srel = htab->elf.irelplt;
9740                   srel->size += p->count * sizeof (Elf64_External_Rela);
9741                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9742                     info->flags |= DF_TEXTREL;
9743                 }
9744             }
9745         }
9746
9747       lgot_ents = elf_local_got_ents (ibfd);
9748       if (!lgot_ents)
9749         continue;
9750
9751       symtab_hdr = &elf_symtab_hdr (ibfd);
9752       locsymcount = symtab_hdr->sh_info;
9753       end_lgot_ents = lgot_ents + locsymcount;
9754       local_plt = (struct plt_entry **) end_lgot_ents;
9755       end_local_plt = local_plt + locsymcount;
9756       lgot_masks = (unsigned char *) end_local_plt;
9757       s = ppc64_elf_tdata (ibfd)->got;
9758       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9759         {
9760           struct got_entry **pent, *ent;
9761
9762           pent = lgot_ents;
9763           while ((ent = *pent) != NULL)
9764             if (ent->got.refcount > 0)
9765               {
9766                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9767                   {
9768                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9769                     *pent = ent->next;
9770                   }
9771                 else
9772                   {
9773                     unsigned int ent_size = 8;
9774                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9775
9776                     ent->got.offset = s->size;
9777                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9778                       {
9779                         ent_size *= 2;
9780                         rel_size *= 2;
9781                       }
9782                     s->size += ent_size;
9783                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9784                       {
9785                         htab->elf.irelplt->size += rel_size;
9786                         htab->got_reli_size += rel_size;
9787                       }
9788                     else if (bfd_link_pic (info)
9789                              && !(ent->tls_type != 0
9790                                   && bfd_link_executable (info)))
9791                       {
9792                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9793                         srel->size += rel_size;
9794                       }
9795                     pent = &ent->next;
9796                   }
9797               }
9798             else
9799               *pent = ent->next;
9800         }
9801
9802       /* Allocate space for plt calls to local syms.  */
9803       lgot_masks = (unsigned char *) end_local_plt;
9804       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9805         {
9806           struct plt_entry *ent;
9807
9808           for (ent = *local_plt; ent != NULL; ent = ent->next)
9809             if (ent->plt.refcount > 0)
9810               {
9811                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9812                   {
9813                     s = htab->elf.iplt;
9814                     ent->plt.offset = s->size;
9815                     s->size += PLT_ENTRY_SIZE (htab);
9816                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9817                   }
9818                 else if (htab->can_convert_all_inline_plt
9819                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9820                   ent->plt.offset = (bfd_vma) -1;
9821                 else
9822                   {
9823                     s = htab->pltlocal;
9824                     ent->plt.offset = s->size;
9825                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9826                     if (bfd_link_pic (info))
9827                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9828                   }
9829               }
9830             else
9831               ent->plt.offset = (bfd_vma) -1;
9832         }
9833     }
9834
9835   /* Allocate global sym .plt and .got entries, and space for global
9836      sym dynamic relocs.  */
9837   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9838
9839   if (!htab->opd_abi && !bfd_link_pic (info))
9840     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9841
9842   first_tlsld = NULL;
9843   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9844     {
9845       struct got_entry *ent;
9846
9847       if (!is_ppc64_elf (ibfd))
9848         continue;
9849
9850       ent = ppc64_tlsld_got (ibfd);
9851       if (ent->got.refcount > 0)
9852         {
9853           if (!htab->do_multi_toc && first_tlsld != NULL)
9854             {
9855               ent->is_indirect = TRUE;
9856               ent->got.ent = first_tlsld;
9857             }
9858           else
9859             {
9860               if (first_tlsld == NULL)
9861                 first_tlsld = ent;
9862               s = ppc64_elf_tdata (ibfd)->got;
9863               ent->got.offset = s->size;
9864               ent->owner = ibfd;
9865               s->size += 16;
9866               if (bfd_link_dll (info))
9867                 {
9868                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9869                   srel->size += sizeof (Elf64_External_Rela);
9870                 }
9871             }
9872         }
9873       else
9874         ent->got.offset = (bfd_vma) -1;
9875     }
9876
9877   /* We now have determined the sizes of the various dynamic sections.
9878      Allocate memory for them.  */
9879   relocs = FALSE;
9880   for (s = dynobj->sections; s != NULL; s = s->next)
9881     {
9882       if ((s->flags & SEC_LINKER_CREATED) == 0)
9883         continue;
9884
9885       if (s == htab->brlt || s == htab->relbrlt)
9886         /* These haven't been allocated yet;  don't strip.  */
9887         continue;
9888       else if (s == htab->elf.sgot
9889                || s == htab->elf.splt
9890                || s == htab->elf.iplt
9891                || s == htab->pltlocal
9892                || s == htab->glink
9893                || s == htab->global_entry
9894                || s == htab->elf.sdynbss
9895                || s == htab->elf.sdynrelro)
9896         {
9897           /* Strip this section if we don't need it; see the
9898              comment below.  */
9899         }
9900       else if (s == htab->glink_eh_frame)
9901         {
9902           if (!bfd_is_abs_section (s->output_section))
9903             /* Not sized yet.  */
9904             continue;
9905         }
9906       else if (CONST_STRNEQ (s->name, ".rela"))
9907         {
9908           if (s->size != 0)
9909             {
9910               if (s != htab->elf.srelplt)
9911                 relocs = TRUE;
9912
9913               /* We use the reloc_count field as a counter if we need
9914                  to copy relocs into the output file.  */
9915               s->reloc_count = 0;
9916             }
9917         }
9918       else
9919         {
9920           /* It's not one of our sections, so don't allocate space.  */
9921           continue;
9922         }
9923
9924       if (s->size == 0)
9925         {
9926           /* If we don't need this section, strip it from the
9927              output file.  This is mostly to handle .rela.bss and
9928              .rela.plt.  We must create both sections in
9929              create_dynamic_sections, because they must be created
9930              before the linker maps input sections to output
9931              sections.  The linker does that before
9932              adjust_dynamic_symbol is called, and it is that
9933              function which decides whether anything needs to go
9934              into these sections.  */
9935           s->flags |= SEC_EXCLUDE;
9936           continue;
9937         }
9938
9939       if (bfd_is_abs_section (s->output_section))
9940         _bfd_error_handler (_("warning: discarding dynamic section %s"),
9941                             s->name);
9942
9943       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9944         continue;
9945
9946       /* Allocate memory for the section contents.  We use bfd_zalloc
9947          here in case unused entries are not reclaimed before the
9948          section's contents are written out.  This should not happen,
9949          but this way if it does we get a R_PPC64_NONE reloc in .rela
9950          sections instead of garbage.
9951          We also rely on the section contents being zero when writing
9952          the GOT and .dynrelro.  */
9953       s->contents = bfd_zalloc (dynobj, s->size);
9954       if (s->contents == NULL)
9955         return FALSE;
9956     }
9957
9958   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9959     {
9960       if (!is_ppc64_elf (ibfd))
9961         continue;
9962
9963       s = ppc64_elf_tdata (ibfd)->got;
9964       if (s != NULL && s != htab->elf.sgot)
9965         {
9966           if (s->size == 0)
9967             s->flags |= SEC_EXCLUDE;
9968           else
9969             {
9970               s->contents = bfd_zalloc (ibfd, s->size);
9971               if (s->contents == NULL)
9972                 return FALSE;
9973             }
9974         }
9975       s = ppc64_elf_tdata (ibfd)->relgot;
9976       if (s != NULL)
9977         {
9978           if (s->size == 0)
9979             s->flags |= SEC_EXCLUDE;
9980           else
9981             {
9982               s->contents = bfd_zalloc (ibfd, s->size);
9983               if (s->contents == NULL)
9984                 return FALSE;
9985               relocs = TRUE;
9986               s->reloc_count = 0;
9987             }
9988         }
9989     }
9990
9991   if (htab->elf.dynamic_sections_created)
9992     {
9993       bfd_boolean tls_opt;
9994
9995       /* Add some entries to the .dynamic section.  We fill in the
9996          values later, in ppc64_elf_finish_dynamic_sections, but we
9997          must add the entries now so that we get the correct size for
9998          the .dynamic section.  The DT_DEBUG entry is filled in by the
9999          dynamic linker and used by the debugger.  */
10000 #define add_dynamic_entry(TAG, VAL) \
10001   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10002
10003       if (bfd_link_executable (info))
10004         {
10005           if (!add_dynamic_entry (DT_DEBUG, 0))
10006             return FALSE;
10007         }
10008
10009       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10010         {
10011           if (!add_dynamic_entry (DT_PLTGOT, 0)
10012               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10013               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10014               || !add_dynamic_entry (DT_JMPREL, 0)
10015               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10016             return FALSE;
10017         }
10018
10019       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10020         {
10021           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10022               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10023             return FALSE;
10024         }
10025
10026       tls_opt = (htab->params->tls_get_addr_opt
10027                  && htab->tls_get_addr_fd != NULL
10028                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10029       if (tls_opt || !htab->opd_abi)
10030         {
10031           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10032             return FALSE;
10033         }
10034
10035       if (relocs)
10036         {
10037           if (!add_dynamic_entry (DT_RELA, 0)
10038               || !add_dynamic_entry (DT_RELASZ, 0)
10039               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10040             return FALSE;
10041
10042           /* If any dynamic relocs apply to a read-only section,
10043              then we need a DT_TEXTREL entry.  */
10044           if ((info->flags & DF_TEXTREL) == 0)
10045             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10046
10047           if ((info->flags & DF_TEXTREL) != 0)
10048             {
10049               if (!add_dynamic_entry (DT_TEXTREL, 0))
10050                 return FALSE;
10051             }
10052         }
10053     }
10054 #undef add_dynamic_entry
10055
10056   return TRUE;
10057 }
10058
10059 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10060
10061 static bfd_boolean
10062 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10063 {
10064   if (h->plt.plist != NULL
10065       && !h->def_regular
10066       && !h->pointer_equality_needed)
10067     return FALSE;
10068
10069   return _bfd_elf_hash_symbol (h);
10070 }
10071
10072 /* Determine the type of stub needed, if any, for a call.  */
10073
10074 static inline enum ppc_stub_type
10075 ppc_type_of_stub (asection *input_sec,
10076                   const Elf_Internal_Rela *rel,
10077                   struct ppc_link_hash_entry **hash,
10078                   struct plt_entry **plt_ent,
10079                   bfd_vma destination,
10080                   unsigned long local_off)
10081 {
10082   struct ppc_link_hash_entry *h = *hash;
10083   bfd_vma location;
10084   bfd_vma branch_offset;
10085   bfd_vma max_branch_offset;
10086   enum elf_ppc64_reloc_type r_type;
10087
10088   if (h != NULL)
10089     {
10090       struct plt_entry *ent;
10091       struct ppc_link_hash_entry *fdh = h;
10092       if (h->oh != NULL
10093           && h->oh->is_func_descriptor)
10094         {
10095           fdh = ppc_follow_link (h->oh);
10096           *hash = fdh;
10097         }
10098
10099       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10100         if (ent->addend == rel->r_addend
10101             && ent->plt.offset != (bfd_vma) -1)
10102           {
10103             *plt_ent = ent;
10104             return ppc_stub_plt_call;
10105           }
10106
10107       /* Here, we know we don't have a plt entry.  If we don't have a
10108          either a defined function descriptor or a defined entry symbol
10109          in a regular object file, then it is pointless trying to make
10110          any other type of stub.  */
10111       if (!is_static_defined (&fdh->elf)
10112           && !is_static_defined (&h->elf))
10113         return ppc_stub_none;
10114     }
10115   else if (elf_local_got_ents (input_sec->owner) != NULL)
10116     {
10117       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10118       struct plt_entry **local_plt = (struct plt_entry **)
10119         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10120       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10121
10122       if (local_plt[r_symndx] != NULL)
10123         {
10124           struct plt_entry *ent;
10125
10126           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10127             if (ent->addend == rel->r_addend
10128                 && ent->plt.offset != (bfd_vma) -1)
10129               {
10130                 *plt_ent = ent;
10131                 return ppc_stub_plt_call;
10132               }
10133         }
10134     }
10135
10136   /* Determine where the call point is.  */
10137   location = (input_sec->output_offset
10138               + input_sec->output_section->vma
10139               + rel->r_offset);
10140
10141   branch_offset = destination - location;
10142   r_type = ELF64_R_TYPE (rel->r_info);
10143
10144   /* Determine if a long branch stub is needed.  */
10145   max_branch_offset = 1 << 25;
10146   if (r_type == R_PPC64_REL14
10147       || r_type == R_PPC64_REL14_BRTAKEN
10148       || r_type == R_PPC64_REL14_BRNTAKEN)
10149     max_branch_offset = 1 << 15;
10150
10151   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10152     /* We need a stub.  Figure out whether a long_branch or plt_branch
10153        is needed later.  */
10154     return ppc_stub_long_branch;
10155
10156   return ppc_stub_none;
10157 }
10158
10159 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10160    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10161    .    mflr    %r12
10162    .    bcl     20,31,1f
10163    .1:  mflr    %r11
10164    .    mtlr    %r12
10165    .    lis     %r12,xxx-1b@highest
10166    .    ori     %r12,%r12,xxx-1b@higher
10167    .    sldi    %r12,%r12,32
10168    .    oris    %r12,%r12,xxx-1b@high
10169    .    ori     %r12,%r12,xxx-1b@l
10170    .    add/ldx %r12,%r11,%r12  */
10171
10172 static bfd_byte *
10173 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10174 {
10175   bfd_put_32 (abfd, MFLR_R12, p);
10176   p += 4;
10177   bfd_put_32 (abfd, BCL_20_31, p);
10178   p += 4;
10179   bfd_put_32 (abfd, MFLR_R11, p);
10180   p += 4;
10181   bfd_put_32 (abfd, MTLR_R12, p);
10182   p += 4;
10183   if (off + 0x8000 < 0x10000)
10184     {
10185       if (load)
10186         bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10187       else
10188         bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10189       p += 4;
10190     }
10191   else if (off + 0x80008000ULL < 0x100000000ULL)
10192     {
10193       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10194       p += 4;
10195       if (load)
10196         bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10197       else
10198         bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10199       p += 4;
10200     }
10201   else
10202     {
10203       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10204         {
10205           bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10206           p += 4;
10207         }
10208       else
10209         {
10210           bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10211           p += 4;
10212           if (((off >> 32) & 0xffff) != 0)
10213             {
10214               bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10215               p += 4;
10216             }
10217         }
10218       if (((off >> 32) & 0xffffffffULL) != 0)
10219         {
10220           bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10221           p += 4;
10222         }
10223       if (PPC_HI (off) != 0)
10224         {
10225           bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10226           p += 4;
10227         }
10228       if (PPC_LO (off) != 0)
10229         {
10230           bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10231           p += 4;
10232         }
10233       if (load)
10234         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10235       else
10236         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10237       p += 4;
10238     }
10239   return p;
10240 }
10241
10242 static unsigned int
10243 size_offset (bfd_vma off)
10244 {
10245   unsigned int size;
10246   if (off + 0x8000 < 0x10000)
10247     size = 4;
10248   else if (off + 0x80008000ULL < 0x100000000ULL)
10249     size = 8;
10250   else
10251     {
10252       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10253         size = 4;
10254       else
10255         {
10256           size = 4;
10257           if (((off >> 32) & 0xffff) != 0)
10258             size += 4;
10259         }
10260       if (((off >> 32) & 0xffffffffULL) != 0)
10261         size += 4;
10262       if (PPC_HI (off) != 0)
10263         size += 4;
10264       if (PPC_LO (off) != 0)
10265         size += 4;
10266       size += 4;
10267     }
10268   return size + 16;
10269 }
10270
10271 static unsigned int
10272 num_relocs_for_offset (bfd_vma off)
10273 {
10274   unsigned int num_rel;
10275   if (off + 0x8000 < 0x10000)
10276     num_rel = 1;
10277   else if (off + 0x80008000ULL < 0x100000000ULL)
10278     num_rel = 2;
10279   else
10280     {
10281       num_rel = 1;
10282       if (off + 0x800000000000ULL >= 0x1000000000000ULL
10283           && ((off >> 32) & 0xffff) != 0)
10284         num_rel += 1;
10285       if (PPC_HI (off) != 0)
10286         num_rel += 1;
10287       if (PPC_LO (off) != 0)
10288         num_rel += 1;
10289     }
10290   return num_rel;
10291 }
10292
10293 static Elf_Internal_Rela *
10294 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10295                         bfd_vma roff, bfd_vma targ, bfd_vma off)
10296 {
10297   bfd_vma relative_targ = targ - (roff - 8);
10298   if (bfd_big_endian (info->output_bfd))
10299     roff += 2;
10300   r->r_offset = roff;
10301   r->r_addend = relative_targ + roff;
10302   if (off + 0x8000 < 0x10000)
10303     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10304   else if (off + 0x80008000ULL < 0x100000000ULL)
10305     {
10306       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10307       ++r;
10308       roff += 4;
10309       r->r_offset = roff;
10310       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10311       r->r_addend = relative_targ + roff;
10312     }
10313   else
10314     {
10315       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10316         r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10317       else
10318         {
10319           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10320           if (((off >> 32) & 0xffff) != 0)
10321             {
10322               ++r;
10323               roff += 4;
10324               r->r_offset = roff;
10325               r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10326               r->r_addend = relative_targ + roff;
10327             }
10328         }
10329       if (((off >> 32) & 0xffffffffULL) != 0)
10330         roff += 4;
10331       if (PPC_HI (off) != 0)
10332         {
10333           ++r;
10334           roff += 4;
10335           r->r_offset = roff;
10336           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10337           r->r_addend = relative_targ + roff;
10338         }
10339       if (PPC_LO (off) != 0)
10340         {
10341           ++r;
10342           roff += 4;
10343           r->r_offset = roff;
10344           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10345           r->r_addend = relative_targ + roff;
10346         }
10347     }
10348   return r;
10349 }
10350
10351 static bfd_byte *
10352 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10353                       bfd_boolean load)
10354 {
10355   uint64_t insn;
10356   if (off - odd + (1ULL << 33) < 1ULL << 34)
10357     {
10358       off -= odd;
10359       if (odd)
10360         {
10361           bfd_put_32 (abfd, NOP, p);
10362           p += 4;
10363         }
10364       if (load)
10365         insn = PLD_R12_PC;
10366       else
10367         insn = PADDI_R12_PC;
10368       insn |= D34 (off);
10369       bfd_put_32 (abfd, insn >> 32, p);
10370       p += 4;
10371       bfd_put_32 (abfd, insn, p);
10372     }
10373   /* The minimum value for paddi is -0x200000000.  The minimum value
10374      for li is -0x8000, which when shifted by 34 and added gives a
10375      minimum value of -0x2000200000000.  The maximum value is
10376      0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
10377   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10378     {
10379       off -= 8 - odd;
10380       bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10381       p += 4;
10382       if (!odd)
10383         {
10384           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10385           p += 4;
10386         }
10387       insn = PADDI_R12_PC | D34 (off);
10388       bfd_put_32 (abfd, insn >> 32, p);
10389       p += 4;
10390       bfd_put_32 (abfd, insn, p);
10391       p += 4;
10392       if (odd)
10393         {
10394           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10395           p += 4;
10396         }
10397       if (load)
10398         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10399       else
10400         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10401     }
10402   else
10403     {
10404       off -= odd + 8;
10405       bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10406       p += 4;
10407       bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10408       p += 4;
10409       if (odd)
10410         {
10411           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10412           p += 4;
10413         }
10414       insn = PADDI_R12_PC | D34 (off);
10415       bfd_put_32 (abfd, insn >> 32, p);
10416       p += 4;
10417       bfd_put_32 (abfd, insn, p);
10418       p += 4;
10419       if (!odd)
10420         {
10421           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10422           p += 4;
10423         }
10424       if (load)
10425         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10426       else
10427         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10428     }
10429   p += 4;
10430   return p;
10431 }
10432
10433 static unsigned int
10434 size_powerxx_offset (bfd_vma off, int odd)
10435 {
10436   if (off - odd + (1ULL << 33) < 1ULL << 34)
10437     return odd + 8;
10438   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10439     return 20;
10440   else
10441     return 24;
10442 }
10443
10444 static unsigned int
10445 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10446 {
10447   if (off - odd + (1ULL << 33) < 1ULL << 34)
10448     return 1;
10449   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10450     return 2;
10451   else
10452     return 3;
10453 }
10454
10455 static Elf_Internal_Rela *
10456 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10457                                 Elf_Internal_Rela *r, bfd_vma roff,
10458                                 bfd_vma targ, bfd_vma off, int odd)
10459 {
10460   if (off - odd + (1ULL << 33) < 1ULL << 34)
10461     roff += odd;
10462   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10463     {
10464       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10465       r->r_offset = roff + d_offset;
10466       r->r_addend = targ + 8 - odd - d_offset;
10467       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10468       ++r;
10469       roff += 8 - odd;
10470     }
10471   else
10472     {
10473       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10474       r->r_offset = roff + d_offset;
10475       r->r_addend = targ + 8 + odd - d_offset;
10476       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10477       ++r;
10478       roff += 4;
10479       r->r_offset = roff + d_offset;
10480       r->r_addend = targ + 4 + odd - d_offset;
10481       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10482       ++r;
10483       roff += 4 + odd;
10484     }
10485   r->r_offset = roff;
10486   r->r_addend = targ;
10487   r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10488   return r;
10489 }
10490
10491 /* Emit .eh_frame opcode to advance pc by DELTA.  */
10492
10493 static bfd_byte *
10494 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10495 {
10496   delta /= 4;
10497   if (delta < 64)
10498     *eh++ = DW_CFA_advance_loc + delta;
10499   else if (delta < 256)
10500     {
10501       *eh++ = DW_CFA_advance_loc1;
10502       *eh++ = delta;
10503     }
10504   else if (delta < 65536)
10505     {
10506       *eh++ = DW_CFA_advance_loc2;
10507       bfd_put_16 (abfd, delta, eh);
10508       eh += 2;
10509     }
10510   else
10511     {
10512       *eh++ = DW_CFA_advance_loc4;
10513       bfd_put_32 (abfd, delta, eh);
10514       eh += 4;
10515     }
10516   return eh;
10517 }
10518
10519 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
10520
10521 static unsigned int
10522 eh_advance_size (unsigned int delta)
10523 {
10524   if (delta < 64 * 4)
10525     /* DW_CFA_advance_loc+[1..63].  */
10526     return 1;
10527   if (delta < 256 * 4)
10528     /* DW_CFA_advance_loc1, byte.  */
10529     return 2;
10530   if (delta < 65536 * 4)
10531     /* DW_CFA_advance_loc2, 2 bytes.  */
10532     return 3;
10533   /* DW_CFA_advance_loc4, 4 bytes.  */
10534   return 5;
10535 }
10536
10537 /* With power7 weakly ordered memory model, it is possible for ld.so
10538    to update a plt entry in one thread and have another thread see a
10539    stale zero toc entry.  To avoid this we need some sort of acquire
10540    barrier in the call stub.  One solution is to make the load of the
10541    toc word seem to appear to depend on the load of the function entry
10542    word.  Another solution is to test for r2 being zero, and branch to
10543    the appropriate glink entry if so.
10544
10545    .    fake dep barrier        compare
10546    .    ld 12,xxx(2)            ld 12,xxx(2)
10547    .    mtctr 12                mtctr 12
10548    .    xor 11,12,12            ld 2,xxx+8(2)
10549    .    add 2,2,11              cmpldi 2,0
10550    .    ld 2,xxx+8(2)           bnectr+
10551    .    bctr                    b <glink_entry>
10552
10553    The solution involving the compare turns out to be faster, so
10554    that's what we use unless the branch won't reach.  */
10555
10556 #define ALWAYS_USE_FAKE_DEP 0
10557 #define ALWAYS_EMIT_R2SAVE 0
10558
10559 static inline unsigned int
10560 plt_stub_size (struct ppc_link_hash_table *htab,
10561                struct ppc_stub_hash_entry *stub_entry,
10562                bfd_vma off)
10563 {
10564   unsigned size;
10565
10566   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10567     {
10568       if (htab->powerxx_stubs)
10569         {
10570           bfd_vma start = (stub_entry->stub_offset
10571                            + stub_entry->group->stub_sec->output_offset
10572                            + stub_entry->group->stub_sec->output_section->vma);
10573           if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10574             start += 4;
10575           size = 8 + size_powerxx_offset (off, start & 4);
10576         }
10577       else
10578         size = 8 + size_offset (off - 8);
10579       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10580         size += 4;
10581       return size;
10582     }
10583
10584   size = 12;
10585   if (ALWAYS_EMIT_R2SAVE
10586       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10587     size += 4;
10588   if (PPC_HA (off) != 0)
10589     size += 4;
10590   if (htab->opd_abi)
10591     {
10592       size += 4;
10593       if (htab->params->plt_static_chain)
10594         size += 4;
10595       if (htab->params->plt_thread_safe
10596           && htab->elf.dynamic_sections_created
10597           && stub_entry->h != NULL
10598           && stub_entry->h->elf.dynindx != -1)
10599         size += 8;
10600       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10601         size += 4;
10602     }
10603   if (stub_entry->h != NULL
10604       && (stub_entry->h == htab->tls_get_addr_fd
10605           || stub_entry->h == htab->tls_get_addr)
10606       && htab->params->tls_get_addr_opt)
10607     {
10608       size += 7 * 4;
10609       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10610         size += 6 * 4;
10611     }
10612   return size;
10613 }
10614
10615 /* Depending on the sign of plt_stub_align:
10616    If positive, return the padding to align to a 2**plt_stub_align
10617    boundary.
10618    If negative, if this stub would cross fewer 2**plt_stub_align
10619    boundaries if we align, then return the padding needed to do so.  */
10620
10621 static inline unsigned int
10622 plt_stub_pad (struct ppc_link_hash_table *htab,
10623               struct ppc_stub_hash_entry *stub_entry,
10624               bfd_vma plt_off)
10625 {
10626   int stub_align;
10627   unsigned stub_size;
10628   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10629
10630   if (htab->params->plt_stub_align >= 0)
10631     {
10632       stub_align = 1 << htab->params->plt_stub_align;
10633       if ((stub_off & (stub_align - 1)) != 0)
10634         return stub_align - (stub_off & (stub_align - 1));
10635       return 0;
10636     }
10637
10638   stub_align = 1 << -htab->params->plt_stub_align;
10639   stub_size = plt_stub_size (htab, stub_entry, plt_off);
10640   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10641       > ((stub_size - 1) & -stub_align))
10642     return stub_align - (stub_off & (stub_align - 1));
10643   return 0;
10644 }
10645
10646 /* Build a .plt call stub.  */
10647
10648 static inline bfd_byte *
10649 build_plt_stub (struct ppc_link_hash_table *htab,
10650                 struct ppc_stub_hash_entry *stub_entry,
10651                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10652 {
10653   bfd *obfd = htab->params->stub_bfd;
10654   bfd_boolean plt_load_toc = htab->opd_abi;
10655   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10656   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10657                                  && htab->elf.dynamic_sections_created
10658                                  && stub_entry->h != NULL
10659                                  && stub_entry->h->elf.dynindx != -1);
10660   bfd_boolean use_fake_dep = plt_thread_safe;
10661   bfd_vma cmp_branch_off = 0;
10662
10663   if (!ALWAYS_USE_FAKE_DEP
10664       && plt_load_toc
10665       && plt_thread_safe
10666       && !((stub_entry->h == htab->tls_get_addr_fd
10667             || stub_entry->h == htab->tls_get_addr)
10668            && htab->params->tls_get_addr_opt))
10669     {
10670       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10671       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10672                           / PLT_ENTRY_SIZE (htab));
10673       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10674       bfd_vma to, from;
10675
10676       if (pltindex > 32768)
10677         glinkoff += (pltindex - 32768) * 4;
10678       to = (glinkoff
10679             + htab->glink->output_offset
10680             + htab->glink->output_section->vma);
10681       from = (p - stub_entry->group->stub_sec->contents
10682               + 4 * (ALWAYS_EMIT_R2SAVE
10683                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10684               + 4 * (PPC_HA (offset) != 0)
10685               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10686                      != PPC_HA (offset))
10687               + 4 * (plt_static_chain != 0)
10688               + 20
10689               + stub_entry->group->stub_sec->output_offset
10690               + stub_entry->group->stub_sec->output_section->vma);
10691       cmp_branch_off = to - from;
10692       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10693     }
10694
10695   if (PPC_HA (offset) != 0)
10696     {
10697       if (r != NULL)
10698         {
10699           if (ALWAYS_EMIT_R2SAVE
10700               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10701             r[0].r_offset += 4;
10702           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10703           r[1].r_offset = r[0].r_offset + 4;
10704           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10705           r[1].r_addend = r[0].r_addend;
10706           if (plt_load_toc)
10707             {
10708               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10709                 {
10710                   r[2].r_offset = r[1].r_offset + 4;
10711                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10712                   r[2].r_addend = r[0].r_addend;
10713                 }
10714               else
10715                 {
10716                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10717                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10718                   r[2].r_addend = r[0].r_addend + 8;
10719                   if (plt_static_chain)
10720                     {
10721                       r[3].r_offset = r[2].r_offset + 4;
10722                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10723                       r[3].r_addend = r[0].r_addend + 16;
10724                     }
10725                 }
10726             }
10727         }
10728       if (ALWAYS_EMIT_R2SAVE
10729           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10730         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10731       if (plt_load_toc)
10732         {
10733           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10734           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10735         }
10736       else
10737         {
10738           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10739           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10740         }
10741       if (plt_load_toc
10742           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10743         {
10744           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10745           offset = 0;
10746         }
10747       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10748       if (plt_load_toc)
10749         {
10750           if (use_fake_dep)
10751             {
10752               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10753               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10754             }
10755           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10756           if (plt_static_chain)
10757             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10758         }
10759     }
10760   else
10761     {
10762       if (r != NULL)
10763         {
10764           if (ALWAYS_EMIT_R2SAVE
10765               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10766             r[0].r_offset += 4;
10767           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10768           if (plt_load_toc)
10769             {
10770               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10771                 {
10772                   r[1].r_offset = r[0].r_offset + 4;
10773                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10774                   r[1].r_addend = r[0].r_addend;
10775                 }
10776               else
10777                 {
10778                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10779                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10780                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10781                   if (plt_static_chain)
10782                     {
10783                       r[2].r_offset = r[1].r_offset + 4;
10784                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10785                       r[2].r_addend = r[0].r_addend + 8;
10786                     }
10787                 }
10788             }
10789         }
10790       if (ALWAYS_EMIT_R2SAVE
10791           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10792         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10793       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10794       if (plt_load_toc
10795           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10796         {
10797           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10798           offset = 0;
10799         }
10800       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10801       if (plt_load_toc)
10802         {
10803           if (use_fake_dep)
10804             {
10805               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10806               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10807             }
10808           if (plt_static_chain)
10809             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10810           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10811         }
10812     }
10813   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10814     {
10815       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10816       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10817       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10818     }
10819   else
10820     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10821   return p;
10822 }
10823
10824 /* Build a special .plt call stub for __tls_get_addr.  */
10825
10826 #define LD_R11_0R3      0xe9630000
10827 #define LD_R12_0R3      0xe9830000
10828 #define MR_R0_R3        0x7c601b78
10829 #define CMPDI_R11_0     0x2c2b0000
10830 #define ADD_R3_R12_R13  0x7c6c6a14
10831 #define BEQLR           0x4d820020
10832 #define MR_R3_R0        0x7c030378
10833 #define STD_R11_0R1     0xf9610000
10834 #define BCTRL           0x4e800421
10835 #define LD_R11_0R1      0xe9610000
10836 #define MTLR_R11        0x7d6803a6
10837
10838 static inline bfd_byte *
10839 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10840                          struct ppc_stub_hash_entry *stub_entry,
10841                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10842 {
10843   bfd *obfd = htab->params->stub_bfd;
10844   bfd_byte *loc = p;
10845
10846   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10847   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10848   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10849   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10850   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10851   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10852   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10853   if (r != NULL)
10854     r[0].r_offset += 7 * 4;
10855   if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
10856     return build_plt_stub (htab, stub_entry, p, offset, r);
10857
10858   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10859   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10860
10861   if (r != NULL)
10862     r[0].r_offset += 2 * 4;
10863   p = build_plt_stub (htab, stub_entry, p, offset, r);
10864   bfd_put_32 (obfd, BCTRL, p - 4);
10865
10866   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10867   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10868   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10869   bfd_put_32 (obfd, BLR, p),                    p += 4;
10870
10871   if (htab->glink_eh_frame != NULL
10872       && htab->glink_eh_frame->size != 0)
10873     {
10874       bfd_byte *base, *eh;
10875       unsigned int lr_used, delta;
10876
10877       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
10878       eh = base + stub_entry->group->eh_size;
10879       lr_used = stub_entry->stub_offset + (p - 20 - loc);
10880       delta = lr_used - stub_entry->group->lr_restore;
10881       stub_entry->group->lr_restore = lr_used + 16;
10882       eh = eh_advance (htab->elf.dynobj, eh, delta);
10883       *eh++ = DW_CFA_offset_extended_sf;
10884       *eh++ = 65;
10885       *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
10886       *eh++ = DW_CFA_advance_loc + 4;
10887       *eh++ = DW_CFA_restore_extended;
10888       *eh++ = 65;
10889       stub_entry->group->eh_size = eh - base;
10890     }
10891   return p;
10892 }
10893
10894 static Elf_Internal_Rela *
10895 get_relocs (asection *sec, int count)
10896 {
10897   Elf_Internal_Rela *relocs;
10898   struct bfd_elf_section_data *elfsec_data;
10899
10900   elfsec_data = elf_section_data (sec);
10901   relocs = elfsec_data->relocs;
10902   if (relocs == NULL)
10903     {
10904       bfd_size_type relsize;
10905       relsize = sec->reloc_count * sizeof (*relocs);
10906       relocs = bfd_alloc (sec->owner, relsize);
10907       if (relocs == NULL)
10908         return NULL;
10909       elfsec_data->relocs = relocs;
10910       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10911                                           sizeof (Elf_Internal_Shdr));
10912       if (elfsec_data->rela.hdr == NULL)
10913         return NULL;
10914       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10915                                         * sizeof (Elf64_External_Rela));
10916       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10917       sec->reloc_count = 0;
10918     }
10919   relocs += sec->reloc_count;
10920   sec->reloc_count += count;
10921   return relocs;
10922 }
10923
10924 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
10925    forms, to the equivalent relocs against the global symbol given by
10926    STUB_ENTRY->H.  */
10927
10928 static bfd_boolean
10929 use_global_in_relocs (struct ppc_link_hash_table *htab,
10930                       struct ppc_stub_hash_entry *stub_entry,
10931                       Elf_Internal_Rela *r, unsigned int num_rel)
10932 {
10933   struct elf_link_hash_entry **hashes;
10934   unsigned long symndx;
10935   struct ppc_link_hash_entry *h;
10936   bfd_vma symval;
10937
10938   /* Relocs are always against symbols in their own object file.  Fake
10939      up global sym hashes for the stub bfd (which has no symbols).  */
10940   hashes = elf_sym_hashes (htab->params->stub_bfd);
10941   if (hashes == NULL)
10942     {
10943       bfd_size_type hsize;
10944
10945       /* When called the first time, stub_globals will contain the
10946          total number of symbols seen during stub sizing.  After
10947          allocating, stub_globals is used as an index to fill the
10948          hashes array.  */
10949       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10950       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10951       if (hashes == NULL)
10952         return FALSE;
10953       elf_sym_hashes (htab->params->stub_bfd) = hashes;
10954       htab->stub_globals = 1;
10955     }
10956   symndx = htab->stub_globals++;
10957   h = stub_entry->h;
10958   hashes[symndx] = &h->elf;
10959   if (h->oh != NULL && h->oh->is_func)
10960     h = ppc_follow_link (h->oh);
10961   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
10962               || h->elf.root.type == bfd_link_hash_defweak);
10963   symval = (h->elf.root.u.def.value
10964             + h->elf.root.u.def.section->output_offset
10965             + h->elf.root.u.def.section->output_section->vma);
10966   while (num_rel-- != 0)
10967     {
10968       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
10969       if (h->elf.root.u.def.section != stub_entry->target_section)
10970         {
10971           /* H is an opd symbol.  The addend must be zero, and the
10972              branch reloc is the only one we can convert.  */
10973           r->r_addend = 0;
10974           break;
10975         }
10976       else
10977         r->r_addend -= symval;
10978       --r;
10979     }
10980   return TRUE;
10981 }
10982
10983 static bfd_vma
10984 get_r2off (struct bfd_link_info *info,
10985            struct ppc_stub_hash_entry *stub_entry)
10986 {
10987   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10988   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10989
10990   if (r2off == 0)
10991     {
10992       /* Support linking -R objects.  Get the toc pointer from the
10993          opd entry.  */
10994       char buf[8];
10995       if (!htab->opd_abi)
10996         return r2off;
10997       asection *opd = stub_entry->h->elf.root.u.def.section;
10998       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10999
11000       if (strcmp (opd->name, ".opd") != 0
11001           || opd->reloc_count != 0)
11002         {
11003           info->callbacks->einfo
11004             (_("%P: cannot find opd entry toc for `%pT'\n"),
11005              stub_entry->h->elf.root.root.string);
11006           bfd_set_error (bfd_error_bad_value);
11007           return (bfd_vma) -1;
11008         }
11009       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11010         return (bfd_vma) -1;
11011       r2off = bfd_get_64 (opd->owner, buf);
11012       r2off -= elf_gp (info->output_bfd);
11013     }
11014   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11015   return r2off;
11016 }
11017
11018 static bfd_boolean
11019 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11020 {
11021   struct ppc_stub_hash_entry *stub_entry;
11022   struct ppc_branch_hash_entry *br_entry;
11023   struct bfd_link_info *info;
11024   struct ppc_link_hash_table *htab;
11025   bfd_byte *loc;
11026   bfd_byte *p, *relp;
11027   bfd_vma targ, off;
11028   Elf_Internal_Rela *r;
11029   asection *plt;
11030   int num_rel;
11031   int odd;
11032
11033   /* Massage our args to the form they really have.  */
11034   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11035   info = in_arg;
11036
11037   htab = ppc_hash_table (info);
11038   if (htab == NULL)
11039     return FALSE;
11040
11041   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11042   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11043
11044   htab->stub_count[stub_entry->stub_type - 1] += 1;
11045   switch (stub_entry->stub_type)
11046     {
11047     case ppc_stub_long_branch:
11048     case ppc_stub_long_branch_r2off:
11049       /* Branches are relative.  This is where we are going to.  */
11050       targ = (stub_entry->target_value
11051               + stub_entry->target_section->output_offset
11052               + stub_entry->target_section->output_section->vma);
11053       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11054
11055       /* And this is where we are coming from.  */
11056       off = (stub_entry->stub_offset
11057              + stub_entry->group->stub_sec->output_offset
11058              + stub_entry->group->stub_sec->output_section->vma);
11059       off = targ - off;
11060
11061       p = loc;
11062       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11063         {
11064           bfd_vma r2off = get_r2off (info, stub_entry);
11065
11066           if (r2off == (bfd_vma) -1)
11067             {
11068               htab->stub_error = TRUE;
11069               return FALSE;
11070             }
11071           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11072           p += 4;
11073           if (PPC_HA (r2off) != 0)
11074             {
11075               bfd_put_32 (htab->params->stub_bfd,
11076                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11077               p += 4;
11078             }
11079           if (PPC_LO (r2off) != 0)
11080             {
11081               bfd_put_32 (htab->params->stub_bfd,
11082                           ADDI_R2_R2 | PPC_LO (r2off), p);
11083               p += 4;
11084             }
11085           off -= p - loc;
11086         }
11087       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11088       p += 4;
11089
11090       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11091         {
11092           _bfd_error_handler
11093             (_("long branch stub `%s' offset overflow"),
11094              stub_entry->root.string);
11095           htab->stub_error = TRUE;
11096           return FALSE;
11097         }
11098
11099       if (info->emitrelocations)
11100         {
11101           r = get_relocs (stub_entry->group->stub_sec, 1);
11102           if (r == NULL)
11103             return FALSE;
11104           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11105           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11106           r->r_addend = targ;
11107           if (stub_entry->h != NULL
11108               && !use_global_in_relocs (htab, stub_entry, r, 1))
11109             return FALSE;
11110         }
11111       break;
11112
11113     case ppc_stub_plt_branch:
11114     case ppc_stub_plt_branch_r2off:
11115       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11116                                          stub_entry->root.string + 9,
11117                                          FALSE, FALSE);
11118       if (br_entry == NULL)
11119         {
11120           _bfd_error_handler (_("can't find branch stub `%s'"),
11121                               stub_entry->root.string);
11122           htab->stub_error = TRUE;
11123           return FALSE;
11124         }
11125
11126       targ = (stub_entry->target_value
11127               + stub_entry->target_section->output_offset
11128               + stub_entry->target_section->output_section->vma);
11129       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11130         targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11131
11132       bfd_put_64 (htab->brlt->owner, targ,
11133                   htab->brlt->contents + br_entry->offset);
11134
11135       if (br_entry->iter == htab->stub_iteration)
11136         {
11137           br_entry->iter = 0;
11138
11139           if (htab->relbrlt != NULL)
11140             {
11141               /* Create a reloc for the branch lookup table entry.  */
11142               Elf_Internal_Rela rela;
11143               bfd_byte *rl;
11144
11145               rela.r_offset = (br_entry->offset
11146                                + htab->brlt->output_offset
11147                                + htab->brlt->output_section->vma);
11148               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11149               rela.r_addend = targ;
11150
11151               rl = htab->relbrlt->contents;
11152               rl += (htab->relbrlt->reloc_count++
11153                      * sizeof (Elf64_External_Rela));
11154               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11155             }
11156           else if (info->emitrelocations)
11157             {
11158               r = get_relocs (htab->brlt, 1);
11159               if (r == NULL)
11160                 return FALSE;
11161               /* brlt, being SEC_LINKER_CREATED does not go through the
11162                  normal reloc processing.  Symbols and offsets are not
11163                  translated from input file to output file form, so
11164                  set up the offset per the output file.  */
11165               r->r_offset = (br_entry->offset
11166                              + htab->brlt->output_offset
11167                              + htab->brlt->output_section->vma);
11168               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11169               r->r_addend = targ;
11170             }
11171         }
11172
11173       targ = (br_entry->offset
11174               + htab->brlt->output_offset
11175               + htab->brlt->output_section->vma);
11176
11177       off = (elf_gp (info->output_bfd)
11178              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11179       off = targ - off;
11180
11181       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11182         {
11183           info->callbacks->einfo
11184             (_("%P: linkage table error against `%pT'\n"),
11185              stub_entry->root.string);
11186           bfd_set_error (bfd_error_bad_value);
11187           htab->stub_error = TRUE;
11188           return FALSE;
11189         }
11190
11191       if (info->emitrelocations)
11192         {
11193           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11194           if (r == NULL)
11195             return FALSE;
11196           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11197           if (bfd_big_endian (info->output_bfd))
11198             r[0].r_offset += 2;
11199           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11200             r[0].r_offset += 4;
11201           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11202           r[0].r_addend = targ;
11203           if (PPC_HA (off) != 0)
11204             {
11205               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11206               r[1].r_offset = r[0].r_offset + 4;
11207               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11208               r[1].r_addend = r[0].r_addend;
11209             }
11210         }
11211
11212       p = loc;
11213       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11214         {
11215           if (PPC_HA (off) != 0)
11216             {
11217               bfd_put_32 (htab->params->stub_bfd,
11218                           ADDIS_R12_R2 | PPC_HA (off), p);
11219               p += 4;
11220               bfd_put_32 (htab->params->stub_bfd,
11221                           LD_R12_0R12 | PPC_LO (off), p);
11222             }
11223           else
11224             bfd_put_32 (htab->params->stub_bfd,
11225                         LD_R12_0R2 | PPC_LO (off), p);
11226         }
11227       else
11228         {
11229           bfd_vma r2off = get_r2off (info, stub_entry);
11230
11231           if (r2off == (bfd_vma) -1)
11232             {
11233               htab->stub_error = TRUE;
11234               return FALSE;
11235             }
11236
11237           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11238           p += 4;
11239           if (PPC_HA (off) != 0)
11240             {
11241               bfd_put_32 (htab->params->stub_bfd,
11242                           ADDIS_R12_R2 | PPC_HA (off), p);
11243               p += 4;
11244               bfd_put_32 (htab->params->stub_bfd,
11245                           LD_R12_0R12 | PPC_LO (off), p);
11246             }
11247           else
11248             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11249
11250           if (PPC_HA (r2off) != 0)
11251             {
11252               p += 4;
11253               bfd_put_32 (htab->params->stub_bfd,
11254                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11255             }
11256           if (PPC_LO (r2off) != 0)
11257             {
11258               p += 4;
11259               bfd_put_32 (htab->params->stub_bfd,
11260                           ADDI_R2_R2 | PPC_LO (r2off), p);
11261             }
11262         }
11263       p += 4;
11264       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11265       p += 4;
11266       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11267       p += 4;
11268       break;
11269
11270     case ppc_stub_long_branch_notoc:
11271     case ppc_stub_long_branch_both:
11272     case ppc_stub_plt_branch_notoc:
11273     case ppc_stub_plt_branch_both:
11274     case ppc_stub_plt_call_notoc:
11275     case ppc_stub_plt_call_both:
11276       p = loc;
11277       off = (stub_entry->stub_offset
11278              + stub_entry->group->stub_sec->output_offset
11279              + stub_entry->group->stub_sec->output_section->vma);
11280       if (stub_entry->stub_type == ppc_stub_long_branch_both
11281           || stub_entry->stub_type == ppc_stub_plt_branch_both
11282           || stub_entry->stub_type == ppc_stub_plt_call_both)
11283         {
11284           off += 4;
11285           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11286           p += 4;
11287         }
11288       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11289         {
11290           targ = stub_entry->plt_ent->plt.offset & ~1;
11291           if (targ >= (bfd_vma) -2)
11292             abort ();
11293
11294           plt = htab->elf.splt;
11295           if (!htab->elf.dynamic_sections_created
11296               || stub_entry->h == NULL
11297               || stub_entry->h->elf.dynindx == -1)
11298             {
11299               if (stub_entry->symtype == STT_GNU_IFUNC)
11300                 plt = htab->elf.iplt;
11301               else
11302                 plt = htab->pltlocal;
11303             }
11304           targ += plt->output_offset + plt->output_section->vma;
11305         }
11306       else
11307         targ = (stub_entry->target_value
11308                 + stub_entry->target_section->output_offset
11309                 + stub_entry->target_section->output_section->vma);
11310       odd = off & 4;
11311       off = targ - off;
11312
11313       relp = p;
11314       num_rel = 0;
11315       if (htab->powerxx_stubs)
11316         {
11317           bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11318           p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11319         }
11320       else
11321         {
11322           /* The notoc stubs calculate their target (either a PLT entry or
11323              the global entry point of a function) relative to the PC
11324              returned by the "bcl" two instructions past the start of the
11325              sequence emitted by build_offset.  The offset is therefore 8
11326              less than calculated from the start of the sequence.  */
11327           off -= 8;
11328           p = build_offset (htab->params->stub_bfd, p, off,
11329                             stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11330         }
11331
11332       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11333         {
11334           bfd_vma from;
11335           num_rel = 1;
11336           from = (stub_entry->stub_offset
11337                   + stub_entry->group->stub_sec->output_offset
11338                   + stub_entry->group->stub_sec->output_section->vma
11339                   + (p - loc));
11340           bfd_put_32 (htab->params->stub_bfd,
11341                       B_DOT | ((targ - from) & 0x3fffffc), p);
11342         }
11343       else
11344         {
11345           bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11346           p += 4;
11347           bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11348         }
11349       p += 4;
11350
11351       if (info->emitrelocations)
11352         {
11353           bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11354           if (htab->powerxx_stubs)
11355             num_rel += num_relocs_for_powerxx_offset (off, odd);
11356           else
11357             {
11358               num_rel += num_relocs_for_offset (off);
11359               roff += 16;
11360             }
11361           r = get_relocs (stub_entry->group->stub_sec, num_rel);
11362           if (r == NULL)
11363             return FALSE;
11364           if (htab->powerxx_stubs)
11365             r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11366           else
11367             r = emit_relocs_for_offset (info, r, roff, targ, off);
11368           if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11369               || stub_entry->stub_type == ppc_stub_long_branch_both)
11370             {
11371               ++r;
11372               roff = p - 4 - stub_entry->group->stub_sec->contents;
11373               r->r_offset = roff;
11374               r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11375               r->r_addend = targ;
11376               if (stub_entry->h != NULL
11377                   && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11378                 return FALSE;
11379             }
11380         }
11381
11382       if (!htab->powerxx_stubs
11383           && htab->glink_eh_frame != NULL
11384           && htab->glink_eh_frame->size != 0)
11385         {
11386           bfd_byte *base, *eh;
11387           unsigned int lr_used, delta;
11388
11389           base = (htab->glink_eh_frame->contents
11390                   + stub_entry->group->eh_base + 17);
11391           eh = base + stub_entry->group->eh_size;
11392           lr_used = stub_entry->stub_offset + 8;
11393           if (stub_entry->stub_type == ppc_stub_long_branch_both
11394               || stub_entry->stub_type == ppc_stub_plt_branch_both
11395               || stub_entry->stub_type == ppc_stub_plt_call_both)
11396             lr_used += 4;
11397           delta = lr_used - stub_entry->group->lr_restore;
11398           stub_entry->group->lr_restore = lr_used + 8;
11399           eh = eh_advance (htab->elf.dynobj, eh, delta);
11400           *eh++ = DW_CFA_register;
11401           *eh++ = 65;
11402           *eh++ = 12;
11403           *eh++ = DW_CFA_advance_loc + 2;
11404           *eh++ = DW_CFA_restore_extended;
11405           *eh++ = 65;
11406           stub_entry->group->eh_size = eh - base;
11407         }
11408       break;
11409
11410     case ppc_stub_plt_call:
11411     case ppc_stub_plt_call_r2save:
11412       if (stub_entry->h != NULL
11413           && stub_entry->h->is_func_descriptor
11414           && stub_entry->h->oh != NULL)
11415         {
11416           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11417
11418           /* If the old-ABI "dot-symbol" is undefined make it weak so
11419              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11420           if (fh->elf.root.type == bfd_link_hash_undefined
11421               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11422                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11423             fh->elf.root.type = bfd_link_hash_undefweak;
11424         }
11425
11426       /* Now build the stub.  */
11427       targ = stub_entry->plt_ent->plt.offset & ~1;
11428       if (targ >= (bfd_vma) -2)
11429         abort ();
11430
11431       plt = htab->elf.splt;
11432       if (!htab->elf.dynamic_sections_created
11433           || stub_entry->h == NULL
11434           || stub_entry->h->elf.dynindx == -1)
11435         {
11436           if (stub_entry->symtype == STT_GNU_IFUNC)
11437             plt = htab->elf.iplt;
11438           else
11439             plt = htab->pltlocal;
11440         }
11441       targ += plt->output_offset + plt->output_section->vma;
11442
11443       off = (elf_gp (info->output_bfd)
11444              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11445       off = targ - off;
11446
11447       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11448         {
11449           info->callbacks->einfo
11450             /* xgettext:c-format */
11451             (_("%P: linkage table error against `%pT'\n"),
11452              stub_entry->h != NULL
11453              ? stub_entry->h->elf.root.root.string
11454              : "<local sym>");
11455           bfd_set_error (bfd_error_bad_value);
11456           htab->stub_error = TRUE;
11457           return FALSE;
11458         }
11459
11460       r = NULL;
11461       if (info->emitrelocations)
11462         {
11463           r = get_relocs (stub_entry->group->stub_sec,
11464                           ((PPC_HA (off) != 0)
11465                            + (htab->opd_abi
11466                               ? 2 + (htab->params->plt_static_chain
11467                                      && PPC_HA (off + 16) == PPC_HA (off))
11468                               : 1)));
11469           if (r == NULL)
11470             return FALSE;
11471           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11472           if (bfd_big_endian (info->output_bfd))
11473             r[0].r_offset += 2;
11474           r[0].r_addend = targ;
11475         }
11476       if (stub_entry->h != NULL
11477           && (stub_entry->h == htab->tls_get_addr_fd
11478               || stub_entry->h == htab->tls_get_addr)
11479           && htab->params->tls_get_addr_opt)
11480         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11481       else
11482         p = build_plt_stub (htab, stub_entry, loc, off, r);
11483       break;
11484
11485     case ppc_stub_save_res:
11486       return TRUE;
11487
11488     default:
11489       BFD_FAIL ();
11490       return FALSE;
11491     }
11492
11493   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11494
11495   if (htab->params->emit_stub_syms)
11496     {
11497       struct elf_link_hash_entry *h;
11498       size_t len1, len2;
11499       char *name;
11500       const char *const stub_str[] = { "long_branch",
11501                                        "long_branch",
11502                                        "long_branch",
11503                                        "long_branch",
11504                                        "plt_branch",
11505                                        "plt_branch",
11506                                        "plt_branch",
11507                                        "plt_branch",
11508                                        "plt_call",
11509                                        "plt_call",
11510                                        "plt_call",
11511                                        "plt_call" };
11512
11513       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11514       len2 = strlen (stub_entry->root.string);
11515       name = bfd_malloc (len1 + len2 + 2);
11516       if (name == NULL)
11517         return FALSE;
11518       memcpy (name, stub_entry->root.string, 9);
11519       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11520       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11521       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11522       if (h == NULL)
11523         return FALSE;
11524       if (h->root.type == bfd_link_hash_new)
11525         {
11526           h->root.type = bfd_link_hash_defined;
11527           h->root.u.def.section = stub_entry->group->stub_sec;
11528           h->root.u.def.value = stub_entry->stub_offset;
11529           h->ref_regular = 1;
11530           h->def_regular = 1;
11531           h->ref_regular_nonweak = 1;
11532           h->forced_local = 1;
11533           h->non_elf = 0;
11534           h->root.linker_def = 1;
11535         }
11536     }
11537
11538   return TRUE;
11539 }
11540
11541 /* As above, but don't actually build the stub.  Just bump offset so
11542    we know stub section sizes, and select plt_branch stubs where
11543    long_branch stubs won't do.  */
11544
11545 static bfd_boolean
11546 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11547 {
11548   struct ppc_stub_hash_entry *stub_entry;
11549   struct bfd_link_info *info;
11550   struct ppc_link_hash_table *htab;
11551   asection *plt;
11552   bfd_vma targ, off, r2off;
11553   unsigned int size, extra, lr_used, delta, odd;
11554
11555   /* Massage our args to the form they really have.  */
11556   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11557   info = in_arg;
11558
11559   htab = ppc_hash_table (info);
11560   if (htab == NULL)
11561     return FALSE;
11562
11563   /* Make a note of the offset within the stubs for this entry.  */
11564   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11565
11566   if (stub_entry->h != NULL
11567       && stub_entry->h->save_res
11568       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11569       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11570     {
11571       /* Don't make stubs to out-of-line register save/restore
11572          functions.  Instead, emit copies of the functions.  */
11573       stub_entry->group->needs_save_res = 1;
11574       stub_entry->stub_type = ppc_stub_save_res;
11575       return TRUE;
11576     }
11577
11578   switch (stub_entry->stub_type)
11579     {
11580     case ppc_stub_plt_branch:
11581     case ppc_stub_plt_branch_r2off:
11582       /* Reset the stub type from the plt branch variant in case we now
11583          can reach with a shorter stub.  */
11584       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11585       /* Fall through.  */
11586     case ppc_stub_long_branch:
11587     case ppc_stub_long_branch_r2off:
11588       targ = (stub_entry->target_value
11589               + stub_entry->target_section->output_offset
11590               + stub_entry->target_section->output_section->vma);
11591       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11592       off = (stub_entry->stub_offset
11593              + stub_entry->group->stub_sec->output_offset
11594              + stub_entry->group->stub_sec->output_section->vma);
11595
11596       size = 4;
11597       r2off = 0;
11598       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11599         {
11600           r2off = get_r2off (info, stub_entry);
11601           if (r2off == (bfd_vma) -1)
11602             {
11603               htab->stub_error = TRUE;
11604               return FALSE;
11605             }
11606           size = 8;
11607           if (PPC_HA (r2off) != 0)
11608             size += 4;
11609           if (PPC_LO (r2off) != 0)
11610             size += 4;
11611           off += size - 4;
11612         }
11613       off = targ - off;
11614
11615       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11616          Do the same for -R objects without function descriptors.  */
11617       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11618            && r2off == 0
11619            && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11620           || off + (1 << 25) >= (bfd_vma) (1 << 26))
11621         {
11622           struct ppc_branch_hash_entry *br_entry;
11623
11624           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11625                                              stub_entry->root.string + 9,
11626                                              TRUE, FALSE);
11627           if (br_entry == NULL)
11628             {
11629               _bfd_error_handler (_("can't build branch stub `%s'"),
11630                                   stub_entry->root.string);
11631               htab->stub_error = TRUE;
11632               return FALSE;
11633             }
11634
11635           if (br_entry->iter != htab->stub_iteration)
11636             {
11637               br_entry->iter = htab->stub_iteration;
11638               br_entry->offset = htab->brlt->size;
11639               htab->brlt->size += 8;
11640
11641               if (htab->relbrlt != NULL)
11642                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11643               else if (info->emitrelocations)
11644                 {
11645                   htab->brlt->reloc_count += 1;
11646                   htab->brlt->flags |= SEC_RELOC;
11647                 }
11648             }
11649
11650           targ = (br_entry->offset
11651                   + htab->brlt->output_offset
11652                   + htab->brlt->output_section->vma);
11653           off = (elf_gp (info->output_bfd)
11654                  + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11655           off = targ - off;
11656
11657           if (info->emitrelocations)
11658             {
11659               stub_entry->group->stub_sec->reloc_count
11660                 += 1 + (PPC_HA (off) != 0);
11661               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11662             }
11663
11664           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11665           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11666             {
11667               size = 12;
11668               if (PPC_HA (off) != 0)
11669                 size = 16;
11670             }
11671           else
11672             {
11673               size = 16;
11674               if (PPC_HA (off) != 0)
11675                 size += 4;
11676
11677               if (PPC_HA (r2off) != 0)
11678                 size += 4;
11679               if (PPC_LO (r2off) != 0)
11680                 size += 4;
11681             }
11682         }
11683       else if (info->emitrelocations)
11684         {
11685           stub_entry->group->stub_sec->reloc_count += 1;
11686           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11687         }
11688       break;
11689
11690     case ppc_stub_plt_branch_notoc:
11691     case ppc_stub_plt_branch_both:
11692       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11693       /* Fall through.  */
11694     case ppc_stub_long_branch_notoc:
11695     case ppc_stub_long_branch_both:
11696       off = (stub_entry->stub_offset
11697              + stub_entry->group->stub_sec->output_offset
11698              + stub_entry->group->stub_sec->output_section->vma);
11699       size = 0;
11700       if (stub_entry->stub_type == ppc_stub_long_branch_both)
11701         size = 4;
11702       off += size;
11703       targ = (stub_entry->target_value
11704               + stub_entry->target_section->output_offset
11705               + stub_entry->target_section->output_section->vma);
11706       odd = off & 4;
11707       off = targ - off;
11708
11709       if (info->emitrelocations)
11710         {
11711           unsigned int num_rel;
11712           if (htab->powerxx_stubs)
11713             num_rel = num_relocs_for_powerxx_offset (off, odd);
11714           else
11715             num_rel = num_relocs_for_offset (off - 8);
11716           stub_entry->group->stub_sec->reloc_count += num_rel;
11717           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11718         }
11719
11720       if (htab->powerxx_stubs)
11721         extra = size_powerxx_offset (off, odd);
11722       else
11723         extra = size_offset (off - 8);
11724       /* Include branch insn plus those in the offset sequence.  */
11725       size += 4 + extra;
11726       /* The branch insn is at the end, or "extra" bytes along.  So
11727          its offset will be "extra" bytes less that that already
11728          calculated.  */
11729       off -= extra;
11730
11731       if (!htab->powerxx_stubs)
11732         {
11733           /* After the bcl, lr has been modified so we need to emit
11734              .eh_frame info saying the return address is in r12.  */
11735           lr_used = stub_entry->stub_offset + 8;
11736           if (stub_entry->stub_type == ppc_stub_long_branch_both)
11737             lr_used += 4;
11738           /* The eh_frame info will consist of a DW_CFA_advance_loc or
11739              variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11740              DW_CFA_restore_extended 65.  */
11741           delta = lr_used - stub_entry->group->lr_restore;
11742           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11743           stub_entry->group->lr_restore = lr_used + 8;
11744         }
11745
11746       /* If the branch can't reach, use a plt_branch.  */
11747       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11748         {
11749           stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11750                                     - ppc_stub_long_branch_notoc);
11751           size += 4;
11752         }
11753       else if (info->emitrelocations)
11754         stub_entry->group->stub_sec->reloc_count +=1;
11755       break;
11756
11757     case ppc_stub_plt_call_notoc:
11758     case ppc_stub_plt_call_both:
11759       off = (stub_entry->stub_offset
11760              + stub_entry->group->stub_sec->output_offset
11761              + stub_entry->group->stub_sec->output_section->vma);
11762       if (stub_entry->stub_type == ppc_stub_plt_call_both)
11763         off += 4;
11764       targ = stub_entry->plt_ent->plt.offset & ~1;
11765       if (targ >= (bfd_vma) -2)
11766         abort ();
11767
11768       plt = htab->elf.splt;
11769       if (!htab->elf.dynamic_sections_created
11770           || stub_entry->h == NULL
11771           || stub_entry->h->elf.dynindx == -1)
11772         {
11773           if (stub_entry->symtype == STT_GNU_IFUNC)
11774             plt = htab->elf.iplt;
11775           else
11776             plt = htab->pltlocal;
11777         }
11778       targ += plt->output_offset + plt->output_section->vma;
11779       odd = off & 4;
11780       off = targ - off;
11781
11782       if (htab->params->plt_stub_align != 0)
11783         {
11784           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11785
11786           stub_entry->group->stub_sec->size += pad;
11787           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11788           off -= pad;
11789         }
11790
11791       if (info->emitrelocations)
11792         {
11793           unsigned int num_rel;
11794           if (htab->powerxx_stubs)
11795             num_rel = num_relocs_for_powerxx_offset (off, odd);
11796           else
11797             num_rel = num_relocs_for_offset (off - 8);
11798           stub_entry->group->stub_sec->reloc_count += num_rel;
11799           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11800         }
11801
11802       size = plt_stub_size (htab, stub_entry, off);
11803
11804       if (!htab->powerxx_stubs)
11805         {
11806           /* After the bcl, lr has been modified so we need to emit
11807              .eh_frame info saying the return address is in r12.  */
11808           lr_used = stub_entry->stub_offset + 8;
11809           if (stub_entry->stub_type == ppc_stub_plt_call_both)
11810             lr_used += 4;
11811           /* The eh_frame info will consist of a DW_CFA_advance_loc or
11812              variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
11813              DW_CFA_restore_extended 65.  */
11814           delta = lr_used - stub_entry->group->lr_restore;
11815           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11816           stub_entry->group->lr_restore = lr_used + 8;
11817         }
11818       break;
11819
11820     case ppc_stub_plt_call:
11821     case ppc_stub_plt_call_r2save:
11822       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11823       if (targ >= (bfd_vma) -2)
11824         abort ();
11825       plt = htab->elf.splt;
11826       if (!htab->elf.dynamic_sections_created
11827           || stub_entry->h == NULL
11828           || stub_entry->h->elf.dynindx == -1)
11829         {
11830           if (stub_entry->symtype == STT_GNU_IFUNC)
11831             plt = htab->elf.iplt;
11832           else
11833             plt = htab->pltlocal;
11834         }
11835       targ += plt->output_offset + plt->output_section->vma;
11836
11837       off = (elf_gp (info->output_bfd)
11838              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11839       off = targ - off;
11840
11841       if (htab->params->plt_stub_align != 0)
11842         {
11843           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11844
11845           stub_entry->group->stub_sec->size += pad;
11846           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11847         }
11848
11849       if (info->emitrelocations)
11850         {
11851           stub_entry->group->stub_sec->reloc_count
11852             += ((PPC_HA (off) != 0)
11853                 + (htab->opd_abi
11854                    ? 2 + (htab->params->plt_static_chain
11855                           && PPC_HA (off + 16) == PPC_HA (off))
11856                    : 1));
11857           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11858         }
11859
11860       size = plt_stub_size (htab, stub_entry, off);
11861
11862       if (stub_entry->h != NULL
11863           && (stub_entry->h == htab->tls_get_addr_fd
11864               || stub_entry->h == htab->tls_get_addr)
11865           && htab->params->tls_get_addr_opt
11866           && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11867         {
11868           /* After the bctrl, lr has been modified so we need to
11869              emit .eh_frame info saying the return address is
11870              on the stack.  In fact we put the EH info specifying
11871              that the return address is on the stack *at* the
11872              call rather than after it, because the EH info for a
11873              call needs to be specified by that point.
11874              See libgcc/unwind-dw2.c execute_cfa_program.  */
11875           lr_used = stub_entry->stub_offset + size - 20;
11876           /* The eh_frame info will consist of a DW_CFA_advance_loc
11877              or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
11878              DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
11879           delta = lr_used - stub_entry->group->lr_restore;
11880           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
11881           stub_entry->group->lr_restore = size - 4;
11882         }
11883       break;
11884
11885     default:
11886       BFD_FAIL ();
11887       return FALSE;
11888     }
11889
11890   stub_entry->group->stub_sec->size += size;
11891   return TRUE;
11892 }
11893
11894 /* Set up various things so that we can make a list of input sections
11895    for each output section included in the link.  Returns -1 on error,
11896    0 when no stubs will be needed, and 1 on success.  */
11897
11898 int
11899 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11900 {
11901   unsigned int id;
11902   bfd_size_type amt;
11903   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11904
11905   if (htab == NULL)
11906     return -1;
11907
11908   htab->sec_info_arr_size = _bfd_section_id;
11909   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11910   htab->sec_info = bfd_zmalloc (amt);
11911   if (htab->sec_info == NULL)
11912     return -1;
11913
11914   /* Set toc_off for com, und, abs and ind sections.  */
11915   for (id = 0; id < 3; id++)
11916     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11917
11918   return 1;
11919 }
11920
11921 /* Set up for first pass at multitoc partitioning.  */
11922
11923 void
11924 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11925 {
11926   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11927
11928   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11929   htab->toc_bfd = NULL;
11930   htab->toc_first_sec = NULL;
11931 }
11932
11933 /* The linker repeatedly calls this function for each TOC input section
11934    and linker generated GOT section.  Group input bfds such that the toc
11935    within a group is less than 64k in size.  */
11936
11937 bfd_boolean
11938 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11939 {
11940   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11941   bfd_vma addr, off, limit;
11942
11943   if (htab == NULL)
11944     return FALSE;
11945
11946   if (!htab->second_toc_pass)
11947     {
11948       /* Keep track of the first .toc or .got section for this input bfd.  */
11949       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11950
11951       if (new_bfd)
11952         {
11953           htab->toc_bfd = isec->owner;
11954           htab->toc_first_sec = isec;
11955         }
11956
11957       addr = isec->output_offset + isec->output_section->vma;
11958       off = addr - htab->toc_curr;
11959       limit = 0x80008000;
11960       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11961         limit = 0x10000;
11962       if (off + isec->size > limit)
11963         {
11964           addr = (htab->toc_first_sec->output_offset
11965                   + htab->toc_first_sec->output_section->vma);
11966           htab->toc_curr = addr;
11967           htab->toc_curr &= -TOC_BASE_ALIGN;
11968         }
11969
11970       /* toc_curr is the base address of this toc group.  Set elf_gp
11971          for the input section to be the offset relative to the
11972          output toc base plus 0x8000.  Making the input elf_gp an
11973          offset allows us to move the toc as a whole without
11974          recalculating input elf_gp.  */
11975       off = htab->toc_curr - elf_gp (info->output_bfd);
11976       off += TOC_BASE_OFF;
11977
11978       /* Die if someone uses a linker script that doesn't keep input
11979          file .toc and .got together.  */
11980       if (new_bfd
11981           && elf_gp (isec->owner) != 0
11982           && elf_gp (isec->owner) != off)
11983         return FALSE;
11984
11985       elf_gp (isec->owner) = off;
11986       return TRUE;
11987     }
11988
11989   /* During the second pass toc_first_sec points to the start of
11990      a toc group, and toc_curr is used to track the old elf_gp.
11991      We use toc_bfd to ensure we only look at each bfd once.  */
11992   if (htab->toc_bfd == isec->owner)
11993     return TRUE;
11994   htab->toc_bfd = isec->owner;
11995
11996   if (htab->toc_first_sec == NULL
11997       || htab->toc_curr != elf_gp (isec->owner))
11998     {
11999       htab->toc_curr = elf_gp (isec->owner);
12000       htab->toc_first_sec = isec;
12001     }
12002   addr = (htab->toc_first_sec->output_offset
12003           + htab->toc_first_sec->output_section->vma);
12004   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12005   elf_gp (isec->owner) = off;
12006
12007   return TRUE;
12008 }
12009
12010 /* Called via elf_link_hash_traverse to merge GOT entries for global
12011    symbol H.  */
12012
12013 static bfd_boolean
12014 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12015 {
12016   if (h->root.type == bfd_link_hash_indirect)
12017     return TRUE;
12018
12019   merge_got_entries (&h->got.glist);
12020
12021   return TRUE;
12022 }
12023
12024 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12025    symbol H.  */
12026
12027 static bfd_boolean
12028 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12029 {
12030   struct got_entry *gent;
12031
12032   if (h->root.type == bfd_link_hash_indirect)
12033     return TRUE;
12034
12035   for (gent = h->got.glist; gent != NULL; gent = gent->next)
12036     if (!gent->is_indirect)
12037       allocate_got (h, (struct bfd_link_info *) inf, gent);
12038   return TRUE;
12039 }
12040
12041 /* Called on the first multitoc pass after the last call to
12042    ppc64_elf_next_toc_section.  This function removes duplicate GOT
12043    entries.  */
12044
12045 bfd_boolean
12046 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12047 {
12048   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12049   struct bfd *ibfd, *ibfd2;
12050   bfd_boolean done_something;
12051
12052   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12053
12054   if (!htab->do_multi_toc)
12055     return FALSE;
12056
12057   /* Merge global sym got entries within a toc group.  */
12058   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12059
12060   /* And tlsld_got.  */
12061   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12062     {
12063       struct got_entry *ent, *ent2;
12064
12065       if (!is_ppc64_elf (ibfd))
12066         continue;
12067
12068       ent = ppc64_tlsld_got (ibfd);
12069       if (!ent->is_indirect
12070           && ent->got.offset != (bfd_vma) -1)
12071         {
12072           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12073             {
12074               if (!is_ppc64_elf (ibfd2))
12075                 continue;
12076
12077               ent2 = ppc64_tlsld_got (ibfd2);
12078               if (!ent2->is_indirect
12079                   && ent2->got.offset != (bfd_vma) -1
12080                   && elf_gp (ibfd2) == elf_gp (ibfd))
12081                 {
12082                   ent2->is_indirect = TRUE;
12083                   ent2->got.ent = ent;
12084                 }
12085             }
12086         }
12087     }
12088
12089   /* Zap sizes of got sections.  */
12090   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12091   htab->elf.irelplt->size -= htab->got_reli_size;
12092   htab->got_reli_size = 0;
12093
12094   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12095     {
12096       asection *got, *relgot;
12097
12098       if (!is_ppc64_elf (ibfd))
12099         continue;
12100
12101       got = ppc64_elf_tdata (ibfd)->got;
12102       if (got != NULL)
12103         {
12104           got->rawsize = got->size;
12105           got->size = 0;
12106           relgot = ppc64_elf_tdata (ibfd)->relgot;
12107           relgot->rawsize = relgot->size;
12108           relgot->size = 0;
12109         }
12110     }
12111
12112   /* Now reallocate the got, local syms first.  We don't need to
12113      allocate section contents again since we never increase size.  */
12114   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12115     {
12116       struct got_entry **lgot_ents;
12117       struct got_entry **end_lgot_ents;
12118       struct plt_entry **local_plt;
12119       struct plt_entry **end_local_plt;
12120       unsigned char *lgot_masks;
12121       bfd_size_type locsymcount;
12122       Elf_Internal_Shdr *symtab_hdr;
12123       asection *s;
12124
12125       if (!is_ppc64_elf (ibfd))
12126         continue;
12127
12128       lgot_ents = elf_local_got_ents (ibfd);
12129       if (!lgot_ents)
12130         continue;
12131
12132       symtab_hdr = &elf_symtab_hdr (ibfd);
12133       locsymcount = symtab_hdr->sh_info;
12134       end_lgot_ents = lgot_ents + locsymcount;
12135       local_plt = (struct plt_entry **) end_lgot_ents;
12136       end_local_plt = local_plt + locsymcount;
12137       lgot_masks = (unsigned char *) end_local_plt;
12138       s = ppc64_elf_tdata (ibfd)->got;
12139       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12140         {
12141           struct got_entry *ent;
12142
12143           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12144             {
12145               unsigned int ent_size = 8;
12146               unsigned int rel_size = sizeof (Elf64_External_Rela);
12147
12148               ent->got.offset = s->size;
12149               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12150                 {
12151                   ent_size *= 2;
12152                   rel_size *= 2;
12153                 }
12154               s->size += ent_size;
12155               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12156                 {
12157                   htab->elf.irelplt->size += rel_size;
12158                   htab->got_reli_size += rel_size;
12159                 }
12160               else if (bfd_link_pic (info)
12161                        && !(ent->tls_type != 0
12162                             && bfd_link_executable (info)))
12163                 {
12164                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12165                   srel->size += rel_size;
12166                 }
12167             }
12168         }
12169     }
12170
12171   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12172
12173   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12174     {
12175       struct got_entry *ent;
12176
12177       if (!is_ppc64_elf (ibfd))
12178         continue;
12179
12180       ent = ppc64_tlsld_got (ibfd);
12181       if (!ent->is_indirect
12182           && ent->got.offset != (bfd_vma) -1)
12183         {
12184           asection *s = ppc64_elf_tdata (ibfd)->got;
12185           ent->got.offset = s->size;
12186           s->size += 16;
12187           if (bfd_link_dll (info))
12188             {
12189               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12190               srel->size += sizeof (Elf64_External_Rela);
12191             }
12192         }
12193     }
12194
12195   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12196   if (!done_something)
12197     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12198       {
12199         asection *got;
12200
12201         if (!is_ppc64_elf (ibfd))
12202           continue;
12203
12204         got = ppc64_elf_tdata (ibfd)->got;
12205         if (got != NULL)
12206           {
12207             done_something = got->rawsize != got->size;
12208             if (done_something)
12209               break;
12210           }
12211       }
12212
12213   if (done_something)
12214     (*htab->params->layout_sections_again) ();
12215
12216   /* Set up for second pass over toc sections to recalculate elf_gp
12217      on input sections.  */
12218   htab->toc_bfd = NULL;
12219   htab->toc_first_sec = NULL;
12220   htab->second_toc_pass = TRUE;
12221   return done_something;
12222 }
12223
12224 /* Called after second pass of multitoc partitioning.  */
12225
12226 void
12227 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12228 {
12229   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12230
12231   /* After the second pass, toc_curr tracks the TOC offset used
12232      for code sections below in ppc64_elf_next_input_section.  */
12233   htab->toc_curr = TOC_BASE_OFF;
12234 }
12235
12236 /* No toc references were found in ISEC.  If the code in ISEC makes no
12237    calls, then there's no need to use toc adjusting stubs when branching
12238    into ISEC.  Actually, indirect calls from ISEC are OK as they will
12239    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
12240    needed, and 2 if a cyclical call-graph was found but no other reason
12241    for a stub was detected.  If called from the top level, a return of
12242    2 means the same as a return of 0.  */
12243
12244 static int
12245 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12246 {
12247   int ret;
12248
12249   /* Mark this section as checked.  */
12250   isec->call_check_done = 1;
12251
12252   /* We know none of our code bearing sections will need toc stubs.  */
12253   if ((isec->flags & SEC_LINKER_CREATED) != 0)
12254     return 0;
12255
12256   if (isec->size == 0)
12257     return 0;
12258
12259   if (isec->output_section == NULL)
12260     return 0;
12261
12262   ret = 0;
12263   if (isec->reloc_count != 0)
12264     {
12265       Elf_Internal_Rela *relstart, *rel;
12266       Elf_Internal_Sym *local_syms;
12267       struct ppc_link_hash_table *htab;
12268
12269       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12270                                             info->keep_memory);
12271       if (relstart == NULL)
12272         return -1;
12273
12274       /* Look for branches to outside of this section.  */
12275       local_syms = NULL;
12276       htab = ppc_hash_table (info);
12277       if (htab == NULL)
12278         return -1;
12279
12280       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12281         {
12282           enum elf_ppc64_reloc_type r_type;
12283           unsigned long r_symndx;
12284           struct elf_link_hash_entry *h;
12285           struct ppc_link_hash_entry *eh;
12286           Elf_Internal_Sym *sym;
12287           asection *sym_sec;
12288           struct _opd_sec_data *opd;
12289           bfd_vma sym_value;
12290           bfd_vma dest;
12291
12292           r_type = ELF64_R_TYPE (rel->r_info);
12293           if (r_type != R_PPC64_REL24
12294               && r_type != R_PPC64_REL24_NOTOC
12295               && r_type != R_PPC64_REL14
12296               && r_type != R_PPC64_REL14_BRTAKEN
12297               && r_type != R_PPC64_REL14_BRNTAKEN
12298               && r_type != R_PPC64_PLTCALL
12299               && r_type != R_PPC64_PLTCALL_NOTOC)
12300             continue;
12301
12302           r_symndx = ELF64_R_SYM (rel->r_info);
12303           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12304                           isec->owner))
12305             {
12306               ret = -1;
12307               break;
12308             }
12309
12310           /* Calls to dynamic lib functions go through a plt call stub
12311              that uses r2.  */
12312           eh = (struct ppc_link_hash_entry *) h;
12313           if (eh != NULL
12314               && (eh->elf.plt.plist != NULL
12315                   || (eh->oh != NULL
12316                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12317             {
12318               ret = 1;
12319               break;
12320             }
12321
12322           if (sym_sec == NULL)
12323             /* Ignore other undefined symbols.  */
12324             continue;
12325
12326           /* Assume branches to other sections not included in the
12327              link need stubs too, to cover -R and absolute syms.  */
12328           if (sym_sec->output_section == NULL)
12329             {
12330               ret = 1;
12331               break;
12332             }
12333
12334           if (h == NULL)
12335             sym_value = sym->st_value;
12336           else
12337             {
12338               if (h->root.type != bfd_link_hash_defined
12339                   && h->root.type != bfd_link_hash_defweak)
12340                 abort ();
12341               sym_value = h->root.u.def.value;
12342             }
12343           sym_value += rel->r_addend;
12344
12345           /* If this branch reloc uses an opd sym, find the code section.  */
12346           opd = get_opd_info (sym_sec);
12347           if (opd != NULL)
12348             {
12349               if (h == NULL && opd->adjust != NULL)
12350                 {
12351                   long adjust;
12352
12353                   adjust = opd->adjust[OPD_NDX (sym_value)];
12354                   if (adjust == -1)
12355                     /* Assume deleted functions won't ever be called.  */
12356                     continue;
12357                   sym_value += adjust;
12358                 }
12359
12360               dest = opd_entry_value (sym_sec, sym_value,
12361                                       &sym_sec, NULL, FALSE);
12362               if (dest == (bfd_vma) -1)
12363                 continue;
12364             }
12365           else
12366             dest = (sym_value
12367                     + sym_sec->output_offset
12368                     + sym_sec->output_section->vma);
12369
12370           /* Ignore branch to self.  */
12371           if (sym_sec == isec)
12372             continue;
12373
12374           /* If the called function uses the toc, we need a stub.  */
12375           if (sym_sec->has_toc_reloc
12376               || sym_sec->makes_toc_func_call)
12377             {
12378               ret = 1;
12379               break;
12380             }
12381
12382           /* Assume any branch that needs a long branch stub might in fact
12383              need a plt_branch stub.  A plt_branch stub uses r2.  */
12384           else if (dest - (isec->output_offset
12385                            + isec->output_section->vma
12386                            + rel->r_offset) + (1 << 25)
12387                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12388                                                              ? h->other
12389                                                              : sym->st_other))
12390             {
12391               ret = 1;
12392               break;
12393             }
12394
12395           /* If calling back to a section in the process of being
12396              tested, we can't say for sure that no toc adjusting stubs
12397              are needed, so don't return zero.  */
12398           else if (sym_sec->call_check_in_progress)
12399             ret = 2;
12400
12401           /* Branches to another section that itself doesn't have any TOC
12402              references are OK.  Recursively call ourselves to check.  */
12403           else if (!sym_sec->call_check_done)
12404             {
12405               int recur;
12406
12407               /* Mark current section as indeterminate, so that other
12408                  sections that call back to current won't be marked as
12409                  known.  */
12410               isec->call_check_in_progress = 1;
12411               recur = toc_adjusting_stub_needed (info, sym_sec);
12412               isec->call_check_in_progress = 0;
12413
12414               if (recur != 0)
12415                 {
12416                   ret = recur;
12417                   if (recur != 2)
12418                     break;
12419                 }
12420             }
12421         }
12422
12423       if (local_syms != NULL
12424           && (elf_symtab_hdr (isec->owner).contents
12425               != (unsigned char *) local_syms))
12426         free (local_syms);
12427       if (elf_section_data (isec)->relocs != relstart)
12428         free (relstart);
12429     }
12430
12431   if ((ret & 1) == 0
12432       && isec->map_head.s != NULL
12433       && (strcmp (isec->output_section->name, ".init") == 0
12434           || strcmp (isec->output_section->name, ".fini") == 0))
12435     {
12436       if (isec->map_head.s->has_toc_reloc
12437           || isec->map_head.s->makes_toc_func_call)
12438         ret = 1;
12439       else if (!isec->map_head.s->call_check_done)
12440         {
12441           int recur;
12442           isec->call_check_in_progress = 1;
12443           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12444           isec->call_check_in_progress = 0;
12445           if (recur != 0)
12446             ret = recur;
12447         }
12448     }
12449
12450   if (ret == 1)
12451     isec->makes_toc_func_call = 1;
12452
12453   return ret;
12454 }
12455
12456 /* The linker repeatedly calls this function for each input section,
12457    in the order that input sections are linked into output sections.
12458    Build lists of input sections to determine groupings between which
12459    we may insert linker stubs.  */
12460
12461 bfd_boolean
12462 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12463 {
12464   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12465
12466   if (htab == NULL)
12467     return FALSE;
12468
12469   if ((isec->output_section->flags & SEC_CODE) != 0
12470       && isec->output_section->id < htab->sec_info_arr_size)
12471     {
12472       /* This happens to make the list in reverse order,
12473          which is what we want.  */
12474       htab->sec_info[isec->id].u.list
12475         = htab->sec_info[isec->output_section->id].u.list;
12476       htab->sec_info[isec->output_section->id].u.list = isec;
12477     }
12478
12479   if (htab->multi_toc_needed)
12480     {
12481       /* Analyse sections that aren't already flagged as needing a
12482          valid toc pointer.  Exclude .fixup for the linux kernel.
12483          .fixup contains branches, but only back to the function that
12484          hit an exception.  */
12485       if (!(isec->has_toc_reloc
12486             || (isec->flags & SEC_CODE) == 0
12487             || strcmp (isec->name, ".fixup") == 0
12488             || isec->call_check_done))
12489         {
12490           if (toc_adjusting_stub_needed (info, isec) < 0)
12491             return FALSE;
12492         }
12493       /* Make all sections use the TOC assigned for this object file.
12494          This will be wrong for pasted sections;  We fix that in
12495          check_pasted_section().  */
12496       if (elf_gp (isec->owner) != 0)
12497         htab->toc_curr = elf_gp (isec->owner);
12498     }
12499
12500   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12501   return TRUE;
12502 }
12503
12504 /* Check that all .init and .fini sections use the same toc, if they
12505    have toc relocs.  */
12506
12507 static bfd_boolean
12508 check_pasted_section (struct bfd_link_info *info, const char *name)
12509 {
12510   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12511
12512   if (o != NULL)
12513     {
12514       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12515       bfd_vma toc_off = 0;
12516       asection *i;
12517
12518       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12519         if (i->has_toc_reloc)
12520           {
12521             if (toc_off == 0)
12522               toc_off = htab->sec_info[i->id].toc_off;
12523             else if (toc_off != htab->sec_info[i->id].toc_off)
12524               return FALSE;
12525           }
12526
12527       if (toc_off == 0)
12528         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12529           if (i->makes_toc_func_call)
12530             {
12531               toc_off = htab->sec_info[i->id].toc_off;
12532               break;
12533             }
12534
12535       /* Make sure the whole pasted function uses the same toc offset.  */
12536       if (toc_off != 0)
12537         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12538           htab->sec_info[i->id].toc_off = toc_off;
12539     }
12540   return TRUE;
12541 }
12542
12543 bfd_boolean
12544 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12545 {
12546   return (check_pasted_section (info, ".init")
12547           & check_pasted_section (info, ".fini"));
12548 }
12549
12550 /* See whether we can group stub sections together.  Grouping stub
12551    sections may result in fewer stubs.  More importantly, we need to
12552    put all .init* and .fini* stubs at the beginning of the .init or
12553    .fini output sections respectively, because glibc splits the
12554    _init and _fini functions into multiple parts.  Putting a stub in
12555    the middle of a function is not a good idea.  */
12556
12557 static bfd_boolean
12558 group_sections (struct bfd_link_info *info,
12559                 bfd_size_type stub_group_size,
12560                 bfd_boolean stubs_always_before_branch)
12561 {
12562   struct ppc_link_hash_table *htab;
12563   asection *osec;
12564   bfd_boolean suppress_size_errors;
12565
12566   htab = ppc_hash_table (info);
12567   if (htab == NULL)
12568     return FALSE;
12569
12570   suppress_size_errors = FALSE;
12571   if (stub_group_size == 1)
12572     {
12573       /* Default values.  */
12574       if (stubs_always_before_branch)
12575         stub_group_size = 0x1e00000;
12576       else
12577         stub_group_size = 0x1c00000;
12578       suppress_size_errors = TRUE;
12579     }
12580
12581   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12582     {
12583       asection *tail;
12584
12585       if (osec->id >= htab->sec_info_arr_size)
12586         continue;
12587
12588       tail = htab->sec_info[osec->id].u.list;
12589       while (tail != NULL)
12590         {
12591           asection *curr;
12592           asection *prev;
12593           bfd_size_type total;
12594           bfd_boolean big_sec;
12595           bfd_vma curr_toc;
12596           struct map_stub *group;
12597           bfd_size_type group_size;
12598
12599           curr = tail;
12600           total = tail->size;
12601           group_size = (ppc64_elf_section_data (tail) != NULL
12602                         && ppc64_elf_section_data (tail)->has_14bit_branch
12603                         ? stub_group_size >> 10 : stub_group_size);
12604
12605           big_sec = total > group_size;
12606           if (big_sec && !suppress_size_errors)
12607             /* xgettext:c-format */
12608             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12609                                 tail->owner, tail);
12610           curr_toc = htab->sec_info[tail->id].toc_off;
12611
12612           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12613                  && ((total += curr->output_offset - prev->output_offset)
12614                      < (ppc64_elf_section_data (prev) != NULL
12615                         && ppc64_elf_section_data (prev)->has_14bit_branch
12616                         ? (group_size = stub_group_size >> 10) : group_size))
12617                  && htab->sec_info[prev->id].toc_off == curr_toc)
12618             curr = prev;
12619
12620           /* OK, the size from the start of CURR to the end is less
12621              than group_size and thus can be handled by one stub
12622              section.  (or the tail section is itself larger than
12623              group_size, in which case we may be toast.)  We should
12624              really be keeping track of the total size of stubs added
12625              here, as stubs contribute to the final output section
12626              size.  That's a little tricky, and this way will only
12627              break if stubs added make the total size more than 2^25,
12628              ie. for the default stub_group_size, if stubs total more
12629              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12630           group = bfd_alloc (curr->owner, sizeof (*group));
12631           if (group == NULL)
12632             return FALSE;
12633           group->link_sec = curr;
12634           group->stub_sec = NULL;
12635           group->needs_save_res = 0;
12636           group->lr_restore = 0;
12637           group->eh_size = 0;
12638           group->eh_base = 0;
12639           group->next = htab->group;
12640           htab->group = group;
12641           do
12642             {
12643               prev = htab->sec_info[tail->id].u.list;
12644               /* Set up this stub group.  */
12645               htab->sec_info[tail->id].u.group = group;
12646             }
12647           while (tail != curr && (tail = prev) != NULL);
12648
12649           /* But wait, there's more!  Input sections up to group_size
12650              bytes before the stub section can be handled by it too.
12651              Don't do this if we have a really large section after the
12652              stubs, as adding more stubs increases the chance that
12653              branches may not reach into the stub section.  */
12654           if (!stubs_always_before_branch && !big_sec)
12655             {
12656               total = 0;
12657               while (prev != NULL
12658                      && ((total += tail->output_offset - prev->output_offset)
12659                          < (ppc64_elf_section_data (prev) != NULL
12660                             && ppc64_elf_section_data (prev)->has_14bit_branch
12661                             ? (group_size = stub_group_size >> 10)
12662                             : group_size))
12663                      && htab->sec_info[prev->id].toc_off == curr_toc)
12664                 {
12665                   tail = prev;
12666                   prev = htab->sec_info[tail->id].u.list;
12667                   htab->sec_info[tail->id].u.group = group;
12668                 }
12669             }
12670           tail = prev;
12671         }
12672     }
12673   return TRUE;
12674 }
12675
12676 static const unsigned char glink_eh_frame_cie[] =
12677 {
12678   0, 0, 0, 16,                          /* length.  */
12679   0, 0, 0, 0,                           /* id.  */
12680   1,                                    /* CIE version.  */
12681   'z', 'R', 0,                          /* Augmentation string.  */
12682   4,                                    /* Code alignment.  */
12683   0x78,                                 /* Data alignment.  */
12684   65,                                   /* RA reg.  */
12685   1,                                    /* Augmentation size.  */
12686   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12687   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12688 };
12689
12690 /* Stripping output sections is normally done before dynamic section
12691    symbols have been allocated.  This function is called later, and
12692    handles cases like htab->brlt which is mapped to its own output
12693    section.  */
12694
12695 static void
12696 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12697 {
12698   if (isec->size == 0
12699       && isec->output_section->size == 0
12700       && !(isec->output_section->flags & SEC_KEEP)
12701       && !bfd_section_removed_from_list (info->output_bfd,
12702                                          isec->output_section)
12703       && elf_section_data (isec->output_section)->dynindx == 0)
12704     {
12705       isec->output_section->flags |= SEC_EXCLUDE;
12706       bfd_section_list_remove (info->output_bfd, isec->output_section);
12707       info->output_bfd->section_count--;
12708     }
12709 }
12710
12711 /* Determine and set the size of the stub section for a final link.
12712
12713    The basic idea here is to examine all the relocations looking for
12714    PC-relative calls to a target that is unreachable with a "bl"
12715    instruction.  */
12716
12717 bfd_boolean
12718 ppc64_elf_size_stubs (struct bfd_link_info *info)
12719 {
12720   bfd_size_type stub_group_size;
12721   bfd_boolean stubs_always_before_branch;
12722   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12723
12724   if (htab == NULL)
12725     return FALSE;
12726
12727   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12728     htab->params->plt_thread_safe = 1;
12729   if (!htab->opd_abi)
12730     htab->params->plt_thread_safe = 0;
12731   else if (htab->params->plt_thread_safe == -1)
12732     {
12733       static const char *const thread_starter[] =
12734         {
12735           "pthread_create",
12736           /* libstdc++ */
12737           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12738           /* librt */
12739           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12740           "mq_notify", "create_timer",
12741           /* libanl */
12742           "getaddrinfo_a",
12743           /* libgomp */
12744           "GOMP_parallel",
12745           "GOMP_parallel_start",
12746           "GOMP_parallel_loop_static",
12747           "GOMP_parallel_loop_static_start",
12748           "GOMP_parallel_loop_dynamic",
12749           "GOMP_parallel_loop_dynamic_start",
12750           "GOMP_parallel_loop_guided",
12751           "GOMP_parallel_loop_guided_start",
12752           "GOMP_parallel_loop_runtime",
12753           "GOMP_parallel_loop_runtime_start",
12754           "GOMP_parallel_sections",
12755           "GOMP_parallel_sections_start",
12756           /* libgo */
12757           "__go_go",
12758         };
12759       unsigned i;
12760
12761       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12762         {
12763           struct elf_link_hash_entry *h;
12764           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12765                                     FALSE, FALSE, TRUE);
12766           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12767           if (htab->params->plt_thread_safe)
12768             break;
12769         }
12770     }
12771   stubs_always_before_branch = htab->params->group_size < 0;
12772   if (htab->params->group_size < 0)
12773     stub_group_size = -htab->params->group_size;
12774   else
12775     stub_group_size = htab->params->group_size;
12776
12777   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12778     return FALSE;
12779
12780 #define STUB_SHRINK_ITER 20
12781   /* Loop until no stubs added.  After iteration 20 of this loop we may
12782      exit on a stub section shrinking.  This is to break out of a
12783      pathological case where adding stubs on one iteration decreases
12784      section gaps (perhaps due to alignment), which then requires
12785      fewer or smaller stubs on the next iteration.  */
12786
12787   while (1)
12788     {
12789       bfd *input_bfd;
12790       unsigned int bfd_indx;
12791       struct map_stub *group;
12792
12793       htab->stub_iteration += 1;
12794
12795       for (input_bfd = info->input_bfds, bfd_indx = 0;
12796            input_bfd != NULL;
12797            input_bfd = input_bfd->link.next, bfd_indx++)
12798         {
12799           Elf_Internal_Shdr *symtab_hdr;
12800           asection *section;
12801           Elf_Internal_Sym *local_syms = NULL;
12802
12803           if (!is_ppc64_elf (input_bfd))
12804             continue;
12805
12806           /* We'll need the symbol table in a second.  */
12807           symtab_hdr = &elf_symtab_hdr (input_bfd);
12808           if (symtab_hdr->sh_info == 0)
12809             continue;
12810
12811           /* Walk over each section attached to the input bfd.  */
12812           for (section = input_bfd->sections;
12813                section != NULL;
12814                section = section->next)
12815             {
12816               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12817
12818               /* If there aren't any relocs, then there's nothing more
12819                  to do.  */
12820               if ((section->flags & SEC_RELOC) == 0
12821                   || (section->flags & SEC_ALLOC) == 0
12822                   || (section->flags & SEC_LOAD) == 0
12823                   || (section->flags & SEC_CODE) == 0
12824                   || section->reloc_count == 0)
12825                 continue;
12826
12827               /* If this section is a link-once section that will be
12828                  discarded, then don't create any stubs.  */
12829               if (section->output_section == NULL
12830                   || section->output_section->owner != info->output_bfd)
12831                 continue;
12832
12833               /* Get the relocs.  */
12834               internal_relocs
12835                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12836                                              info->keep_memory);
12837               if (internal_relocs == NULL)
12838                 goto error_ret_free_local;
12839
12840               /* Now examine each relocation.  */
12841               irela = internal_relocs;
12842               irelaend = irela + section->reloc_count;
12843               for (; irela < irelaend; irela++)
12844                 {
12845                   enum elf_ppc64_reloc_type r_type;
12846                   unsigned int r_indx;
12847                   enum ppc_stub_type stub_type;
12848                   struct ppc_stub_hash_entry *stub_entry;
12849                   asection *sym_sec, *code_sec;
12850                   bfd_vma sym_value, code_value;
12851                   bfd_vma destination;
12852                   unsigned long local_off;
12853                   bfd_boolean ok_dest;
12854                   struct ppc_link_hash_entry *hash;
12855                   struct ppc_link_hash_entry *fdh;
12856                   struct elf_link_hash_entry *h;
12857                   Elf_Internal_Sym *sym;
12858                   char *stub_name;
12859                   const asection *id_sec;
12860                   struct _opd_sec_data *opd;
12861                   struct plt_entry *plt_ent;
12862
12863                   r_type = ELF64_R_TYPE (irela->r_info);
12864                   r_indx = ELF64_R_SYM (irela->r_info);
12865
12866                   if (r_type >= R_PPC64_max)
12867                     {
12868                       bfd_set_error (bfd_error_bad_value);
12869                       goto error_ret_free_internal;
12870                     }
12871
12872                   /* Only look for stubs on branch instructions.  */
12873                   if (r_type != R_PPC64_REL24
12874                       && r_type != R_PPC64_REL24_NOTOC
12875                       && r_type != R_PPC64_REL14
12876                       && r_type != R_PPC64_REL14_BRTAKEN
12877                       && r_type != R_PPC64_REL14_BRNTAKEN)
12878                     continue;
12879
12880                   /* Now determine the call target, its name, value,
12881                      section.  */
12882                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12883                                   r_indx, input_bfd))
12884                     goto error_ret_free_internal;
12885                   hash = (struct ppc_link_hash_entry *) h;
12886
12887                   ok_dest = FALSE;
12888                   fdh = NULL;
12889                   sym_value = 0;
12890                   if (hash == NULL)
12891                     {
12892                       sym_value = sym->st_value;
12893                       if (sym_sec != NULL
12894                           && sym_sec->output_section != NULL)
12895                         ok_dest = TRUE;
12896                     }
12897                   else if (hash->elf.root.type == bfd_link_hash_defined
12898                            || hash->elf.root.type == bfd_link_hash_defweak)
12899                     {
12900                       sym_value = hash->elf.root.u.def.value;
12901                       if (sym_sec->output_section != NULL)
12902                         ok_dest = TRUE;
12903                     }
12904                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12905                            || hash->elf.root.type == bfd_link_hash_undefined)
12906                     {
12907                       /* Recognise an old ABI func code entry sym, and
12908                          use the func descriptor sym instead if it is
12909                          defined.  */
12910                       if (hash->elf.root.root.string[0] == '.'
12911                           && hash->oh != NULL)
12912                         {
12913                           fdh = ppc_follow_link (hash->oh);
12914                           if (fdh->elf.root.type == bfd_link_hash_defined
12915                               || fdh->elf.root.type == bfd_link_hash_defweak)
12916                             {
12917                               sym_sec = fdh->elf.root.u.def.section;
12918                               sym_value = fdh->elf.root.u.def.value;
12919                               if (sym_sec->output_section != NULL)
12920                                 ok_dest = TRUE;
12921                             }
12922                           else
12923                             fdh = NULL;
12924                         }
12925                     }
12926                   else
12927                     {
12928                       bfd_set_error (bfd_error_bad_value);
12929                       goto error_ret_free_internal;
12930                     }
12931
12932                   destination = 0;
12933                   local_off = 0;
12934                   if (ok_dest)
12935                     {
12936                       sym_value += irela->r_addend;
12937                       destination = (sym_value
12938                                      + sym_sec->output_offset
12939                                      + sym_sec->output_section->vma);
12940                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12941                                                             ? hash->elf.other
12942                                                             : sym->st_other);
12943                     }
12944
12945                   code_sec = sym_sec;
12946                   code_value = sym_value;
12947                   opd = get_opd_info (sym_sec);
12948                   if (opd != NULL)
12949                     {
12950                       bfd_vma dest;
12951
12952                       if (hash == NULL && opd->adjust != NULL)
12953                         {
12954                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12955                           if (adjust == -1)
12956                             continue;
12957                           code_value += adjust;
12958                           sym_value += adjust;
12959                         }
12960                       dest = opd_entry_value (sym_sec, sym_value,
12961                                               &code_sec, &code_value, FALSE);
12962                       if (dest != (bfd_vma) -1)
12963                         {
12964                           destination = dest;
12965                           if (fdh != NULL)
12966                             {
12967                               /* Fixup old ABI sym to point at code
12968                                  entry.  */
12969                               hash->elf.root.type = bfd_link_hash_defweak;
12970                               hash->elf.root.u.def.section = code_sec;
12971                               hash->elf.root.u.def.value = code_value;
12972                             }
12973                         }
12974                     }
12975
12976                   /* Determine what (if any) linker stub is needed.  */
12977                   plt_ent = NULL;
12978                   stub_type = ppc_type_of_stub (section, irela, &hash,
12979                                                 &plt_ent, destination,
12980                                                 local_off);
12981
12982                   if (r_type == R_PPC64_REL24_NOTOC)
12983                     {
12984                       if (stub_type == ppc_stub_plt_call)
12985                         stub_type = ppc_stub_plt_call_notoc;
12986                       else if (stub_type == ppc_stub_long_branch
12987                                || (code_sec != NULL
12988                                    && code_sec->output_section != NULL
12989                                    && (((hash ? hash->elf.other : sym->st_other)
12990                                         & STO_PPC64_LOCAL_MASK)
12991                                        > 1 << STO_PPC64_LOCAL_BIT)))
12992                         stub_type = ppc_stub_long_branch_notoc;
12993                     }
12994                   else if (stub_type != ppc_stub_plt_call)
12995                     {
12996                       /* Check whether we need a TOC adjusting stub.
12997                          Since the linker pastes together pieces from
12998                          different object files when creating the
12999                          _init and _fini functions, it may be that a
13000                          call to what looks like a local sym is in
13001                          fact a call needing a TOC adjustment.  */
13002                       if ((code_sec != NULL
13003                            && code_sec->output_section != NULL
13004                            && (htab->sec_info[code_sec->id].toc_off
13005                                != htab->sec_info[section->id].toc_off)
13006                            && (code_sec->has_toc_reloc
13007                                || code_sec->makes_toc_func_call))
13008                           || (((hash ? hash->elf.other : sym->st_other)
13009                                & STO_PPC64_LOCAL_MASK)
13010                               == 1 << STO_PPC64_LOCAL_BIT))
13011                         stub_type = ppc_stub_long_branch_r2off;
13012                     }
13013
13014                   if (stub_type == ppc_stub_none)
13015                     continue;
13016
13017                   /* __tls_get_addr calls might be eliminated.  */
13018                   if (stub_type != ppc_stub_plt_call
13019                       && stub_type != ppc_stub_plt_call_notoc
13020                       && hash != NULL
13021                       && (hash == htab->tls_get_addr
13022                           || hash == htab->tls_get_addr_fd)
13023                       && section->has_tls_reloc
13024                       && irela != internal_relocs)
13025                     {
13026                       /* Get tls info.  */
13027                       unsigned char *tls_mask;
13028
13029                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13030                                          irela - 1, input_bfd))
13031                         goto error_ret_free_internal;
13032                       if ((*tls_mask & TLS_TLS) != 0)
13033                         continue;
13034                     }
13035
13036                   if (stub_type == ppc_stub_plt_call)
13037                     {
13038                       if (!htab->opd_abi
13039                           && htab->params->plt_localentry0 != 0
13040                           && is_elfv2_localentry0 (&hash->elf))
13041                         htab->has_plt_localentry0 = 1;
13042                       else if (irela + 1 < irelaend
13043                                && irela[1].r_offset == irela->r_offset + 4
13044                                && (ELF64_R_TYPE (irela[1].r_info)
13045                                    == R_PPC64_TOCSAVE))
13046                         {
13047                           if (!tocsave_find (htab, INSERT,
13048                                              &local_syms, irela + 1, input_bfd))
13049                             goto error_ret_free_internal;
13050                         }
13051                       else
13052                         stub_type = ppc_stub_plt_call_r2save;
13053                     }
13054
13055                   /* Support for grouping stub sections.  */
13056                   id_sec = htab->sec_info[section->id].u.group->link_sec;
13057
13058                   /* Get the name of this stub.  */
13059                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13060                   if (!stub_name)
13061                     goto error_ret_free_internal;
13062
13063                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13064                                                      stub_name, FALSE, FALSE);
13065                   if (stub_entry != NULL)
13066                     {
13067                       enum ppc_stub_type old_type;
13068                       /* A stub has already been created, but it may
13069                          not be the required type.  We shouldn't be
13070                          transitioning from plt_call to long_branch
13071                          stubs or vice versa, but we might be
13072                          upgrading from plt_call to plt_call_r2save or
13073                          from long_branch to long_branch_r2off.  */
13074                       free (stub_name);
13075                       old_type = stub_entry->stub_type;
13076                       switch (old_type)
13077                         {
13078                         default:
13079                           abort ();
13080
13081                         case ppc_stub_save_res:
13082                           continue;
13083
13084                         case ppc_stub_plt_call:
13085                         case ppc_stub_plt_call_r2save:
13086                         case ppc_stub_plt_call_notoc:
13087                         case ppc_stub_plt_call_both:
13088                           if (stub_type == ppc_stub_plt_call)
13089                             continue;
13090                           else if (stub_type == ppc_stub_plt_call_r2save)
13091                             {
13092                               if (old_type == ppc_stub_plt_call_notoc)
13093                                 stub_type = ppc_stub_plt_call_both;
13094                             }
13095                           else if (stub_type == ppc_stub_plt_call_notoc)
13096                             {
13097                               if (old_type == ppc_stub_plt_call_r2save)
13098                                 stub_type = ppc_stub_plt_call_both;
13099                             }
13100                           else
13101                             abort ();
13102                           break;
13103
13104                         case ppc_stub_plt_branch:
13105                         case ppc_stub_plt_branch_r2off:
13106                         case ppc_stub_plt_branch_notoc:
13107                         case ppc_stub_plt_branch_both:
13108                           old_type += (ppc_stub_long_branch
13109                                        - ppc_stub_plt_branch);
13110                           /* Fall through.  */
13111                         case ppc_stub_long_branch:
13112                         case ppc_stub_long_branch_r2off:
13113                         case ppc_stub_long_branch_notoc:
13114                         case ppc_stub_long_branch_both:
13115                           if (stub_type == ppc_stub_long_branch)
13116                             continue;
13117                           else if (stub_type == ppc_stub_long_branch_r2off)
13118                             {
13119                               if (old_type == ppc_stub_long_branch_notoc)
13120                                 stub_type = ppc_stub_long_branch_both;
13121                             }
13122                           else if (stub_type == ppc_stub_long_branch_notoc)
13123                             {
13124                               if (old_type == ppc_stub_long_branch_r2off)
13125                                 stub_type = ppc_stub_long_branch_both;
13126                             }
13127                           else
13128                             abort ();
13129                           break;
13130                         }
13131                       if (old_type < stub_type)
13132                         stub_entry->stub_type = stub_type;
13133                       continue;
13134                     }
13135
13136                   stub_entry = ppc_add_stub (stub_name, section, info);
13137                   if (stub_entry == NULL)
13138                     {
13139                       free (stub_name);
13140                     error_ret_free_internal:
13141                       if (elf_section_data (section)->relocs == NULL)
13142                         free (internal_relocs);
13143                     error_ret_free_local:
13144                       if (local_syms != NULL
13145                           && (symtab_hdr->contents
13146                               != (unsigned char *) local_syms))
13147                         free (local_syms);
13148                       return FALSE;
13149                     }
13150
13151                   stub_entry->stub_type = stub_type;
13152                   if (stub_type >= ppc_stub_plt_call
13153                       && stub_type <= ppc_stub_plt_call_both)
13154                     {
13155                       stub_entry->target_value = sym_value;
13156                       stub_entry->target_section = sym_sec;
13157                     }
13158                   else
13159                     {
13160                       stub_entry->target_value = code_value;
13161                       stub_entry->target_section = code_sec;
13162                     }
13163                   stub_entry->h = hash;
13164                   stub_entry->plt_ent = plt_ent;
13165                   stub_entry->symtype
13166                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13167                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
13168
13169                   if (hash != NULL
13170                       && (hash->elf.root.type == bfd_link_hash_defined
13171                           || hash->elf.root.type == bfd_link_hash_defweak))
13172                     htab->stub_globals += 1;
13173                 }
13174
13175               /* We're done with the internal relocs, free them.  */
13176               if (elf_section_data (section)->relocs != internal_relocs)
13177                 free (internal_relocs);
13178             }
13179
13180           if (local_syms != NULL
13181               && symtab_hdr->contents != (unsigned char *) local_syms)
13182             {
13183               if (!info->keep_memory)
13184                 free (local_syms);
13185               else
13186                 symtab_hdr->contents = (unsigned char *) local_syms;
13187             }
13188         }
13189
13190       /* We may have added some stubs.  Find out the new size of the
13191          stub sections.  */
13192       for (group = htab->group; group != NULL; group = group->next)
13193         {
13194           group->lr_restore = 0;
13195           group->eh_size = 0;
13196           if (group->stub_sec != NULL)
13197             {
13198               asection *stub_sec = group->stub_sec;
13199
13200               if (htab->stub_iteration <= STUB_SHRINK_ITER
13201                   || stub_sec->rawsize < stub_sec->size)
13202                 /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
13203                 stub_sec->rawsize = stub_sec->size;
13204               stub_sec->size = 0;
13205               stub_sec->reloc_count = 0;
13206               stub_sec->flags &= ~SEC_RELOC;
13207             }
13208         }
13209
13210       if (htab->stub_iteration <= STUB_SHRINK_ITER
13211           || htab->brlt->rawsize < htab->brlt->size)
13212         htab->brlt->rawsize = htab->brlt->size;
13213       htab->brlt->size = 0;
13214       htab->brlt->reloc_count = 0;
13215       htab->brlt->flags &= ~SEC_RELOC;
13216       if (htab->relbrlt != NULL)
13217         htab->relbrlt->size = 0;
13218
13219       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13220
13221       for (group = htab->group; group != NULL; group = group->next)
13222         if (group->needs_save_res)
13223           group->stub_sec->size += htab->sfpr->size;
13224
13225       if (info->emitrelocations
13226           && htab->glink != NULL && htab->glink->size != 0)
13227         {
13228           htab->glink->reloc_count = 1;
13229           htab->glink->flags |= SEC_RELOC;
13230         }
13231
13232       if (htab->glink_eh_frame != NULL
13233           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13234           && htab->glink_eh_frame->output_section->size > 8)
13235         {
13236           size_t size = 0, align = 4;
13237
13238           for (group = htab->group; group != NULL; group = group->next)
13239             if (group->eh_size != 0)
13240               size += (group->eh_size + 17 + align - 1) & -align;
13241           if (htab->glink != NULL && htab->glink->size != 0)
13242             size += (24 + align - 1) & -align;
13243           if (size != 0)
13244             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13245           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13246           size = (size + align - 1) & -align;
13247           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13248           htab->glink_eh_frame->size = size;
13249         }
13250
13251       if (htab->params->plt_stub_align != 0)
13252         for (group = htab->group; group != NULL; group = group->next)
13253           if (group->stub_sec != NULL)
13254             {
13255               int align = abs (htab->params->plt_stub_align);
13256               group->stub_sec->size
13257                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13258             }
13259
13260       for (group = htab->group; group != NULL; group = group->next)
13261         if (group->stub_sec != NULL
13262             && group->stub_sec->rawsize != group->stub_sec->size
13263             && (htab->stub_iteration <= STUB_SHRINK_ITER
13264                 || group->stub_sec->rawsize < group->stub_sec->size))
13265           break;
13266
13267       if (group == NULL
13268           && (htab->brlt->rawsize == htab->brlt->size
13269               || (htab->stub_iteration > STUB_SHRINK_ITER
13270                   && htab->brlt->rawsize > htab->brlt->size))
13271           && (htab->glink_eh_frame == NULL
13272               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13273         break;
13274
13275       /* Ask the linker to do its stuff.  */
13276       (*htab->params->layout_sections_again) ();
13277     }
13278
13279   if (htab->glink_eh_frame != NULL
13280       && htab->glink_eh_frame->size != 0)
13281     {
13282       bfd_vma val;
13283       bfd_byte *p, *last_fde;
13284       size_t last_fde_len, size, align, pad;
13285       struct map_stub *group;
13286
13287       /* It is necessary to at least have a rough outline of the
13288          linker generated CIEs and FDEs written before
13289          bfd_elf_discard_info is run, in order for these FDEs to be
13290          indexed in .eh_frame_hdr.  */
13291       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13292       if (p == NULL)
13293         return FALSE;
13294       htab->glink_eh_frame->contents = p;
13295       last_fde = p;
13296       align = 4;
13297
13298       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13299       /* CIE length (rewrite in case little-endian).  */
13300       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13301       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13302       p += last_fde_len + 4;
13303
13304       for (group = htab->group; group != NULL; group = group->next)
13305         if (group->eh_size != 0)
13306           {
13307             group->eh_base = p - htab->glink_eh_frame->contents;
13308             last_fde = p;
13309             last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13310             /* FDE length.  */
13311             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13312             p += 4;
13313             /* CIE pointer.  */
13314             val = p - htab->glink_eh_frame->contents;
13315             bfd_put_32 (htab->elf.dynobj, val, p);
13316             p += 4;
13317             /* Offset to stub section, written later.  */
13318             p += 4;
13319             /* stub section size.  */
13320             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13321             p += 4;
13322             /* Augmentation.  */
13323             p += 1;
13324             /* Make sure we don't have all nops.  This is enough for
13325                elf-eh-frame.c to detect the last non-nop opcode.  */
13326             p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13327             p = last_fde + last_fde_len + 4;
13328           }
13329       if (htab->glink != NULL && htab->glink->size != 0)
13330         {
13331           last_fde = p;
13332           last_fde_len = ((24 + align - 1) & -align) - 4;
13333           /* FDE length.  */
13334           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13335           p += 4;
13336           /* CIE pointer.  */
13337           val = p - htab->glink_eh_frame->contents;
13338           bfd_put_32 (htab->elf.dynobj, val, p);
13339           p += 4;
13340           /* Offset to .glink, written later.  */
13341           p += 4;
13342           /* .glink size.  */
13343           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13344           p += 4;
13345           /* Augmentation.  */
13346           p += 1;
13347
13348           *p++ = DW_CFA_advance_loc + 1;
13349           *p++ = DW_CFA_register;
13350           *p++ = 65;
13351           *p++ = htab->opd_abi ? 12 : 0;
13352           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13353           *p++ = DW_CFA_restore_extended;
13354           *p++ = 65;
13355           p += ((24 + align - 1) & -align) - 24;
13356         }
13357       /* Subsume any padding into the last FDE if user .eh_frame
13358          sections are aligned more than glink_eh_frame.  Otherwise any
13359          zero padding will be seen as a terminator.  */
13360       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13361       size = p - htab->glink_eh_frame->contents;
13362       pad = ((size + align - 1) & -align) - size;
13363       htab->glink_eh_frame->size = size + pad;
13364       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13365     }
13366
13367   maybe_strip_output (info, htab->brlt);
13368   if (htab->glink_eh_frame != NULL)
13369     maybe_strip_output (info, htab->glink_eh_frame);
13370
13371   return TRUE;
13372 }
13373
13374 /* Called after we have determined section placement.  If sections
13375    move, we'll be called again.  Provide a value for TOCstart.  */
13376
13377 bfd_vma
13378 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13379 {
13380   asection *s;
13381   bfd_vma TOCstart, adjust;
13382
13383   if (info != NULL)
13384     {
13385       struct elf_link_hash_entry *h;
13386       struct elf_link_hash_table *htab = elf_hash_table (info);
13387
13388       if (is_elf_hash_table (htab)
13389           && htab->hgot != NULL)
13390         h = htab->hgot;
13391       else
13392         {
13393           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13394           if (is_elf_hash_table (htab))
13395             htab->hgot = h;
13396         }
13397       if (h != NULL
13398           && h->root.type == bfd_link_hash_defined
13399           && !h->root.linker_def
13400           && (!is_elf_hash_table (htab)
13401               || h->def_regular))
13402         {
13403           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13404                       + h->root.u.def.section->output_offset
13405                       + h->root.u.def.section->output_section->vma);
13406           _bfd_set_gp_value (obfd, TOCstart);
13407           return TOCstart;
13408         }
13409     }
13410
13411   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13412      order.  The TOC starts where the first of these sections starts.  */
13413   s = bfd_get_section_by_name (obfd, ".got");
13414   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13415     s = bfd_get_section_by_name (obfd, ".toc");
13416   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13417     s = bfd_get_section_by_name (obfd, ".tocbss");
13418   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13419     s = bfd_get_section_by_name (obfd, ".plt");
13420   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13421     {
13422       /* This may happen for
13423          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13424          .toc directive
13425          o  bad linker script
13426          o --gc-sections and empty TOC sections
13427
13428          FIXME: Warn user?  */
13429
13430       /* Look for a likely section.  We probably won't even be
13431          using TOCstart.  */
13432       for (s = obfd->sections; s != NULL; s = s->next)
13433         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13434                          | SEC_EXCLUDE))
13435             == (SEC_ALLOC | SEC_SMALL_DATA))
13436           break;
13437       if (s == NULL)
13438         for (s = obfd->sections; s != NULL; s = s->next)
13439           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13440               == (SEC_ALLOC | SEC_SMALL_DATA))
13441             break;
13442       if (s == NULL)
13443         for (s = obfd->sections; s != NULL; s = s->next)
13444           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13445               == SEC_ALLOC)
13446             break;
13447       if (s == NULL)
13448         for (s = obfd->sections; s != NULL; s = s->next)
13449           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13450             break;
13451     }
13452
13453   TOCstart = 0;
13454   if (s != NULL)
13455     TOCstart = s->output_section->vma + s->output_offset;
13456
13457   /* Force alignment.  */
13458   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13459   TOCstart -= adjust;
13460   _bfd_set_gp_value (obfd, TOCstart);
13461
13462   if (info != NULL && s != NULL)
13463     {
13464       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13465
13466       if (htab != NULL)
13467         {
13468           if (htab->elf.hgot != NULL)
13469             {
13470               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13471               htab->elf.hgot->root.u.def.section = s;
13472             }
13473         }
13474       else
13475         {
13476           struct bfd_link_hash_entry *bh = NULL;
13477           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13478                                             s, TOC_BASE_OFF - adjust,
13479                                             NULL, FALSE, FALSE, &bh);
13480         }
13481     }
13482   return TOCstart;
13483 }
13484
13485 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13486    write out any global entry stubs, and PLT relocations.  */
13487
13488 static bfd_boolean
13489 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13490 {
13491   struct bfd_link_info *info;
13492   struct ppc_link_hash_table *htab;
13493   struct plt_entry *ent;
13494   asection *s;
13495
13496   if (h->root.type == bfd_link_hash_indirect)
13497     return TRUE;
13498
13499   info = inf;
13500   htab = ppc_hash_table (info);
13501   if (htab == NULL)
13502     return FALSE;
13503
13504   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13505     if (ent->plt.offset != (bfd_vma) -1)
13506       {
13507         /* This symbol has an entry in the procedure linkage
13508            table.  Set it up.  */
13509         Elf_Internal_Rela rela;
13510         asection *plt, *relplt;
13511         bfd_byte *loc;
13512
13513         if (!htab->elf.dynamic_sections_created
13514             || h->dynindx == -1)
13515           {
13516             if (!(h->def_regular
13517                   && (h->root.type == bfd_link_hash_defined
13518                       || h->root.type == bfd_link_hash_defweak)))
13519               continue;
13520             if (h->type == STT_GNU_IFUNC)
13521               {
13522                 plt = htab->elf.iplt;
13523                 relplt = htab->elf.irelplt;
13524                 htab->local_ifunc_resolver = 1;
13525                 if (htab->opd_abi)
13526                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13527                 else
13528                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13529               }
13530             else
13531               {
13532                 plt = htab->pltlocal;
13533                 if (bfd_link_pic (info))
13534                   {
13535                     relplt = htab->relpltlocal;
13536                     if (htab->opd_abi)
13537                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13538                     else
13539                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13540                   }
13541                 else
13542                   relplt = NULL;
13543               }
13544             rela.r_addend = (h->root.u.def.value
13545                              + h->root.u.def.section->output_offset
13546                              + h->root.u.def.section->output_section->vma
13547                              + ent->addend);
13548
13549             if (relplt == NULL)
13550               {
13551                 loc = plt->contents + ent->plt.offset;
13552                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13553                 if (htab->opd_abi)
13554                   {
13555                     bfd_vma toc = elf_gp (info->output_bfd);
13556                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13557                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13558                   }
13559               }
13560             else
13561               {
13562                 rela.r_offset = (plt->output_section->vma
13563                                  + plt->output_offset
13564                                  + ent->plt.offset);
13565                 loc = relplt->contents + (relplt->reloc_count++
13566                                           * sizeof (Elf64_External_Rela));
13567                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13568               }
13569           }
13570         else
13571           {
13572             rela.r_offset = (htab->elf.splt->output_section->vma
13573                              + htab->elf.splt->output_offset
13574                              + ent->plt.offset);
13575             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13576             rela.r_addend = ent->addend;
13577             loc = (htab->elf.srelplt->contents
13578                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13579                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13580             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13581               htab->maybe_local_ifunc_resolver = 1;
13582             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13583           }
13584       }
13585
13586   if (!h->pointer_equality_needed)
13587     return TRUE;
13588
13589   if (h->def_regular)
13590     return TRUE;
13591
13592   s = htab->global_entry;
13593   if (s == NULL || s->size == 0)
13594     return TRUE;
13595
13596   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13597     if (ent->plt.offset != (bfd_vma) -1
13598         && ent->addend == 0)
13599       {
13600         bfd_byte *p;
13601         asection *plt;
13602         bfd_vma off;
13603
13604         p = s->contents + h->root.u.def.value;
13605         plt = htab->elf.splt;
13606         if (!htab->elf.dynamic_sections_created
13607             || h->dynindx == -1)
13608           {
13609             if (h->type == STT_GNU_IFUNC)
13610               plt = htab->elf.iplt;
13611             else
13612               plt = htab->pltlocal;
13613           }
13614         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13615         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13616
13617         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13618           {
13619             info->callbacks->einfo
13620               (_("%P: linkage table error against `%pT'\n"),
13621                h->root.root.string);
13622             bfd_set_error (bfd_error_bad_value);
13623             htab->stub_error = TRUE;
13624           }
13625
13626         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13627         if (htab->params->emit_stub_syms)
13628           {
13629             size_t len = strlen (h->root.root.string);
13630             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13631
13632             if (name == NULL)
13633               return FALSE;
13634
13635             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13636             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13637             if (h == NULL)
13638               return FALSE;
13639             if (h->root.type == bfd_link_hash_new)
13640               {
13641                 h->root.type = bfd_link_hash_defined;
13642                 h->root.u.def.section = s;
13643                 h->root.u.def.value = p - s->contents;
13644                 h->ref_regular = 1;
13645                 h->def_regular = 1;
13646                 h->ref_regular_nonweak = 1;
13647                 h->forced_local = 1;
13648                 h->non_elf = 0;
13649                 h->root.linker_def = 1;
13650               }
13651           }
13652
13653         if (PPC_HA (off) != 0)
13654           {
13655             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13656             p += 4;
13657           }
13658         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13659         p += 4;
13660         bfd_put_32 (s->owner, MTCTR_R12, p);
13661         p += 4;
13662         bfd_put_32 (s->owner, BCTR, p);
13663         break;
13664       }
13665   return TRUE;
13666 }
13667
13668 /* Write PLT relocs for locals.  */
13669
13670 static bfd_boolean
13671 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13672 {
13673   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13674   bfd *ibfd;
13675
13676   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13677     {
13678       struct got_entry **lgot_ents, **end_lgot_ents;
13679       struct plt_entry **local_plt, **lplt, **end_local_plt;
13680       Elf_Internal_Shdr *symtab_hdr;
13681       bfd_size_type locsymcount;
13682       Elf_Internal_Sym *local_syms = NULL;
13683       struct plt_entry *ent;
13684
13685       if (!is_ppc64_elf (ibfd))
13686         continue;
13687
13688       lgot_ents = elf_local_got_ents (ibfd);
13689       if (!lgot_ents)
13690         continue;
13691
13692       symtab_hdr = &elf_symtab_hdr (ibfd);
13693       locsymcount = symtab_hdr->sh_info;
13694       end_lgot_ents = lgot_ents + locsymcount;
13695       local_plt = (struct plt_entry **) end_lgot_ents;
13696       end_local_plt = local_plt + locsymcount;
13697       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13698         for (ent = *lplt; ent != NULL; ent = ent->next)
13699           if (ent->plt.offset != (bfd_vma) -1)
13700             {
13701               Elf_Internal_Sym *sym;
13702               asection *sym_sec;
13703               asection *plt, *relplt;
13704               bfd_byte *loc;
13705               bfd_vma val;
13706
13707               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13708                               lplt - local_plt, ibfd))
13709                 {
13710                   if (local_syms != NULL
13711                       && symtab_hdr->contents != (unsigned char *) local_syms)
13712                     free (local_syms);
13713                   return FALSE;
13714                 }
13715
13716               val = sym->st_value + ent->addend;
13717               if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13718                 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13719               if (sym_sec != NULL && sym_sec->output_section != NULL)
13720                 val += sym_sec->output_offset + sym_sec->output_section->vma;
13721
13722               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13723                 {
13724                   htab->local_ifunc_resolver = 1;
13725                   plt = htab->elf.iplt;
13726                   relplt = htab->elf.irelplt;
13727                 }
13728               else
13729                 {
13730                   plt = htab->pltlocal;
13731                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13732                 }
13733
13734               if (relplt == NULL)
13735                 {
13736                   loc = plt->contents + ent->plt.offset;
13737                   bfd_put_64 (info->output_bfd, val, loc);
13738                   if (htab->opd_abi)
13739                     {
13740                       bfd_vma toc = elf_gp (ibfd);
13741                       bfd_put_64 (info->output_bfd, toc, loc + 8);
13742                     }
13743                 }
13744               else
13745                 {
13746                   Elf_Internal_Rela rela;
13747                   rela.r_offset = (ent->plt.offset
13748                                    + plt->output_offset
13749                                    + plt->output_section->vma);
13750                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13751                     {
13752                       if (htab->opd_abi)
13753                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13754                       else
13755                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13756                     }
13757                   else
13758                     {
13759                       if (htab->opd_abi)
13760                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13761                       else
13762                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13763                     }
13764                   rela.r_addend = val;
13765                   loc = relplt->contents + (relplt->reloc_count++
13766                                             * sizeof (Elf64_External_Rela));
13767                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13768                 }
13769             }
13770
13771       if (local_syms != NULL
13772           && symtab_hdr->contents != (unsigned char *) local_syms)
13773         {
13774           if (!info->keep_memory)
13775             free (local_syms);
13776           else
13777             symtab_hdr->contents = (unsigned char *) local_syms;
13778         }
13779     }
13780   return TRUE;
13781 }
13782
13783 /* Build all the stubs associated with the current output file.
13784    The stubs are kept in a hash table attached to the main linker
13785    hash table.  This function is called via gldelf64ppc_finish.  */
13786
13787 bfd_boolean
13788 ppc64_elf_build_stubs (struct bfd_link_info *info,
13789                        char **stats)
13790 {
13791   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13792   struct map_stub *group;
13793   asection *stub_sec;
13794   bfd_byte *p;
13795   int stub_sec_count = 0;
13796
13797   if (htab == NULL)
13798     return FALSE;
13799
13800   /* Allocate memory to hold the linker stubs.  */
13801   for (group = htab->group; group != NULL; group = group->next)
13802     {
13803       group->eh_size = 0;
13804       group->lr_restore = 0;
13805       if ((stub_sec = group->stub_sec) != NULL
13806           && stub_sec->size != 0)
13807         {
13808           stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
13809                                            stub_sec->size);
13810           if (stub_sec->contents == NULL)
13811             return FALSE;
13812           stub_sec->size = 0;
13813         }
13814     }
13815
13816   if (htab->glink != NULL && htab->glink->size != 0)
13817     {
13818       unsigned int indx;
13819       bfd_vma plt0;
13820
13821       /* Build the .glink plt call stub.  */
13822       if (htab->params->emit_stub_syms)
13823         {
13824           struct elf_link_hash_entry *h;
13825           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13826                                     TRUE, FALSE, FALSE);
13827           if (h == NULL)
13828             return FALSE;
13829           if (h->root.type == bfd_link_hash_new)
13830             {
13831               h->root.type = bfd_link_hash_defined;
13832               h->root.u.def.section = htab->glink;
13833               h->root.u.def.value = 8;
13834               h->ref_regular = 1;
13835               h->def_regular = 1;
13836               h->ref_regular_nonweak = 1;
13837               h->forced_local = 1;
13838               h->non_elf = 0;
13839               h->root.linker_def = 1;
13840             }
13841         }
13842       plt0 = (htab->elf.splt->output_section->vma
13843               + htab->elf.splt->output_offset
13844               - 16);
13845       if (info->emitrelocations)
13846         {
13847           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13848           if (r == NULL)
13849             return FALSE;
13850           r->r_offset = (htab->glink->output_offset
13851                          + htab->glink->output_section->vma);
13852           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13853           r->r_addend = plt0;
13854         }
13855       p = htab->glink->contents;
13856       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13857       bfd_put_64 (htab->glink->owner, plt0, p);
13858       p += 8;
13859       if (htab->opd_abi)
13860         {
13861           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13862           p += 4;
13863           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13864           p += 4;
13865           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13866           p += 4;
13867           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13868           p += 4;
13869           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13870           p += 4;
13871           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13872           p += 4;
13873           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13874           p += 4;
13875           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13876           p += 4;
13877           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13878           p += 4;
13879           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13880           p += 4;
13881         }
13882       else
13883         {
13884           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13885           p += 4;
13886           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13887           p += 4;
13888           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13889           p += 4;
13890           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13891           p += 4;
13892           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13893           p += 4;
13894           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13895           p += 4;
13896           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13897           p += 4;
13898           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13899           p += 4;
13900           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13901           p += 4;
13902           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13903           p += 4;
13904           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13905           p += 4;
13906           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13907           p += 4;
13908           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13909           p += 4;
13910         }
13911       bfd_put_32 (htab->glink->owner, BCTR, p);
13912       p += 4;
13913       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13914
13915       /* Build the .glink lazy link call stubs.  */
13916       indx = 0;
13917       while (p < htab->glink->contents + htab->glink->size)
13918         {
13919           if (htab->opd_abi)
13920             {
13921               if (indx < 0x8000)
13922                 {
13923                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13924                   p += 4;
13925                 }
13926               else
13927                 {
13928                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13929                   p += 4;
13930                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13931                               p);
13932                   p += 4;
13933                 }
13934             }
13935           bfd_put_32 (htab->glink->owner,
13936                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13937           indx++;
13938           p += 4;
13939         }
13940     }
13941
13942   /* Build .glink global entry stubs, and PLT relocs for globals.  */
13943   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13944
13945   if (!write_plt_relocs_for_local_syms (info))
13946     return FALSE;
13947
13948   if (htab->brlt != NULL && htab->brlt->size != 0)
13949     {
13950       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13951                                          htab->brlt->size);
13952       if (htab->brlt->contents == NULL)
13953         return FALSE;
13954     }
13955   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13956     {
13957       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13958                                             htab->relbrlt->size);
13959       if (htab->relbrlt->contents == NULL)
13960         return FALSE;
13961     }
13962
13963   /* Build the stubs as directed by the stub hash table.  */
13964   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13965
13966   for (group = htab->group; group != NULL; group = group->next)
13967     if (group->needs_save_res)
13968       group->stub_sec->size += htab->sfpr->size;
13969
13970   if (htab->relbrlt != NULL)
13971     htab->relbrlt->reloc_count = 0;
13972
13973   if (htab->params->plt_stub_align != 0)
13974     for (group = htab->group; group != NULL; group = group->next)
13975       if ((stub_sec = group->stub_sec) != NULL)
13976         {
13977           int align = abs (htab->params->plt_stub_align);
13978           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13979         }
13980
13981   for (group = htab->group; group != NULL; group = group->next)
13982     if (group->needs_save_res)
13983       {
13984         stub_sec = group->stub_sec;
13985         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13986                 htab->sfpr->contents, htab->sfpr->size);
13987         if (htab->params->emit_stub_syms)
13988           {
13989             unsigned int i;
13990
13991             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13992               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13993                 return FALSE;
13994           }
13995       }
13996
13997   if (htab->glink_eh_frame != NULL
13998       && htab->glink_eh_frame->size != 0)
13999     {
14000       bfd_vma val;
14001       size_t align = 4;
14002
14003       p = htab->glink_eh_frame->contents;
14004       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14005
14006       for (group = htab->group; group != NULL; group = group->next)
14007         if (group->eh_size != 0)
14008           {
14009             /* Offset to stub section.  */
14010             val = (group->stub_sec->output_section->vma
14011                    + group->stub_sec->output_offset);
14012             val -= (htab->glink_eh_frame->output_section->vma
14013                     + htab->glink_eh_frame->output_offset
14014                     + (p + 8 - htab->glink_eh_frame->contents));
14015             if (val + 0x80000000 > 0xffffffff)
14016               {
14017                 _bfd_error_handler
14018                   (_("%s offset too large for .eh_frame sdata4 encoding"),
14019                    group->stub_sec->name);
14020                 return FALSE;
14021               }
14022             bfd_put_32 (htab->elf.dynobj, val, p + 8);
14023             p += (group->eh_size + 17 + 3) & -4;
14024           }
14025       if (htab->glink != NULL && htab->glink->size != 0)
14026         {
14027           /* Offset to .glink.  */
14028           val = (htab->glink->output_section->vma
14029                  + htab->glink->output_offset
14030                  + 8);
14031           val -= (htab->glink_eh_frame->output_section->vma
14032                   + htab->glink_eh_frame->output_offset
14033                   + (p + 8 - htab->glink_eh_frame->contents));
14034           if (val + 0x80000000 > 0xffffffff)
14035             {
14036               _bfd_error_handler
14037                 (_("%s offset too large for .eh_frame sdata4 encoding"),
14038                  htab->glink->name);
14039               return FALSE;
14040             }
14041           bfd_put_32 (htab->elf.dynobj, val, p + 8);
14042           p += (24 + align - 1) & -align;
14043         }
14044     }
14045
14046   for (group = htab->group; group != NULL; group = group->next)
14047     if ((stub_sec = group->stub_sec) != NULL)
14048       {
14049         stub_sec_count += 1;
14050         if (stub_sec->rawsize != stub_sec->size
14051             && (htab->stub_iteration <= STUB_SHRINK_ITER
14052                 || stub_sec->rawsize < stub_sec->size))
14053           break;
14054       }
14055
14056   if (group != NULL)
14057     {
14058       htab->stub_error = TRUE;
14059       _bfd_error_handler (_("stubs don't match calculated size"));
14060     }
14061
14062   if (htab->stub_error)
14063     return FALSE;
14064
14065   if (stats != NULL)
14066     {
14067       size_t len;
14068       *stats = bfd_malloc (500);
14069       if (*stats == NULL)
14070         return FALSE;
14071
14072       len = sprintf (*stats,
14073                      ngettext ("linker stubs in %u group\n",
14074                                "linker stubs in %u groups\n",
14075                                stub_sec_count),
14076                      stub_sec_count);
14077       sprintf (*stats + len, _("  branch         %lu\n"
14078                                "  branch toc adj %lu\n"
14079                                "  branch notoc   %lu\n"
14080                                "  branch both    %lu\n"
14081                                "  long branch    %lu\n"
14082                                "  long toc adj   %lu\n"
14083                                "  long notoc     %lu\n"
14084                                "  long both      %lu\n"
14085                                "  plt call       %lu\n"
14086                                "  plt call save  %lu\n"
14087                                "  plt call notoc %lu\n"
14088                                "  plt call both  %lu\n"
14089                                "  global entry   %lu"),
14090                htab->stub_count[ppc_stub_long_branch - 1],
14091                htab->stub_count[ppc_stub_long_branch_r2off - 1],
14092                htab->stub_count[ppc_stub_long_branch_notoc - 1],
14093                htab->stub_count[ppc_stub_long_branch_both - 1],
14094                htab->stub_count[ppc_stub_plt_branch - 1],
14095                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14096                htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14097                htab->stub_count[ppc_stub_plt_branch_both - 1],
14098                htab->stub_count[ppc_stub_plt_call - 1],
14099                htab->stub_count[ppc_stub_plt_call_r2save - 1],
14100                htab->stub_count[ppc_stub_plt_call_notoc - 1],
14101                htab->stub_count[ppc_stub_plt_call_both - 1],
14102                htab->stub_count[ppc_stub_global_entry - 1]);
14103     }
14104   return TRUE;
14105 }
14106
14107 /* What to do when ld finds relocations against symbols defined in
14108    discarded sections.  */
14109
14110 static unsigned int
14111 ppc64_elf_action_discarded (asection *sec)
14112 {
14113   if (strcmp (".opd", sec->name) == 0)
14114     return 0;
14115
14116   if (strcmp (".toc", sec->name) == 0)
14117     return 0;
14118
14119   if (strcmp (".toc1", sec->name) == 0)
14120     return 0;
14121
14122   return _bfd_elf_default_action_discarded (sec);
14123 }
14124
14125 /* The RELOCATE_SECTION function is called by the ELF backend linker
14126    to handle the relocations for a section.
14127
14128    The relocs are always passed as Rela structures; if the section
14129    actually uses Rel structures, the r_addend field will always be
14130    zero.
14131
14132    This function is responsible for adjust the section contents as
14133    necessary, and (if using Rela relocs and generating a
14134    relocatable output file) adjusting the reloc addend as
14135    necessary.
14136
14137    This function does not have to worry about setting the reloc
14138    address or the reloc symbol index.
14139
14140    LOCAL_SYMS is a pointer to the swapped in local symbols.
14141
14142    LOCAL_SECTIONS is an array giving the section in the input file
14143    corresponding to the st_shndx field of each local symbol.
14144
14145    The global hash table entry for the global symbols can be found
14146    via elf_sym_hashes (input_bfd).
14147
14148    When generating relocatable output, this function must handle
14149    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
14150    going to be the section symbol corresponding to the output
14151    section, which means that the addend must be adjusted
14152    accordingly.  */
14153
14154 static bfd_boolean
14155 ppc64_elf_relocate_section (bfd *output_bfd,
14156                             struct bfd_link_info *info,
14157                             bfd *input_bfd,
14158                             asection *input_section,
14159                             bfd_byte *contents,
14160                             Elf_Internal_Rela *relocs,
14161                             Elf_Internal_Sym *local_syms,
14162                             asection **local_sections)
14163 {
14164   struct ppc_link_hash_table *htab;
14165   Elf_Internal_Shdr *symtab_hdr;
14166   struct elf_link_hash_entry **sym_hashes;
14167   Elf_Internal_Rela *rel;
14168   Elf_Internal_Rela *wrel;
14169   Elf_Internal_Rela *relend;
14170   Elf_Internal_Rela outrel;
14171   bfd_byte *loc;
14172   struct got_entry **local_got_ents;
14173   bfd_vma TOCstart;
14174   bfd_boolean ret = TRUE;
14175   bfd_boolean is_opd;
14176   /* Assume 'at' branch hints.  */
14177   bfd_boolean is_isa_v2 = TRUE;
14178   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14179
14180   /* Initialize howto table if needed.  */
14181   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14182     ppc_howto_init ();
14183
14184   htab = ppc_hash_table (info);
14185   if (htab == NULL)
14186     return FALSE;
14187
14188   /* Don't relocate stub sections.  */
14189   if (input_section->owner == htab->params->stub_bfd)
14190     return TRUE;
14191
14192   if (!is_ppc64_elf (input_bfd))
14193     {
14194       bfd_set_error (bfd_error_wrong_format);
14195       return FALSE;
14196     }
14197
14198   local_got_ents = elf_local_got_ents (input_bfd);
14199   TOCstart = elf_gp (output_bfd);
14200   symtab_hdr = &elf_symtab_hdr (input_bfd);
14201   sym_hashes = elf_sym_hashes (input_bfd);
14202   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14203
14204   rel = wrel = relocs;
14205   relend = relocs + input_section->reloc_count;
14206   for (; rel < relend; wrel++, rel++)
14207     {
14208       enum elf_ppc64_reloc_type r_type;
14209       bfd_vma addend;
14210       bfd_reloc_status_type r;
14211       Elf_Internal_Sym *sym;
14212       asection *sec;
14213       struct elf_link_hash_entry *h_elf;
14214       struct ppc_link_hash_entry *h;
14215       struct ppc_link_hash_entry *fdh;
14216       const char *sym_name;
14217       unsigned long r_symndx, toc_symndx;
14218       bfd_vma toc_addend;
14219       unsigned char tls_mask, tls_gd, tls_type;
14220       unsigned char sym_type;
14221       bfd_vma relocation;
14222       bfd_boolean unresolved_reloc, save_unresolved_reloc;
14223       bfd_boolean warned;
14224       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14225       unsigned int insn;
14226       unsigned int mask;
14227       struct ppc_stub_hash_entry *stub_entry;
14228       bfd_vma max_br_offset;
14229       bfd_vma from;
14230       Elf_Internal_Rela orig_rel;
14231       reloc_howto_type *howto;
14232       struct reloc_howto_struct alt_howto;
14233       uint64_t pinsn;
14234       bfd_vma offset;
14235
14236     again:
14237       orig_rel = *rel;
14238
14239       r_type = ELF64_R_TYPE (rel->r_info);
14240       r_symndx = ELF64_R_SYM (rel->r_info);
14241
14242       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14243          symbol of the previous ADDR64 reloc.  The symbol gives us the
14244          proper TOC base to use.  */
14245       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14246           && wrel != relocs
14247           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14248           && is_opd)
14249         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14250
14251       sym = NULL;
14252       sec = NULL;
14253       h_elf = NULL;
14254       sym_name = NULL;
14255       unresolved_reloc = FALSE;
14256       warned = FALSE;
14257
14258       if (r_symndx < symtab_hdr->sh_info)
14259         {
14260           /* It's a local symbol.  */
14261           struct _opd_sec_data *opd;
14262
14263           sym = local_syms + r_symndx;
14264           sec = local_sections[r_symndx];
14265           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14266           sym_type = ELF64_ST_TYPE (sym->st_info);
14267           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14268           opd = get_opd_info (sec);
14269           if (opd != NULL && opd->adjust != NULL)
14270             {
14271               long adjust = opd->adjust[OPD_NDX (sym->st_value
14272                                                  + rel->r_addend)];
14273               if (adjust == -1)
14274                 relocation = 0;
14275               else
14276                 {
14277                   /* If this is a relocation against the opd section sym
14278                      and we have edited .opd, adjust the reloc addend so
14279                      that ld -r and ld --emit-relocs output is correct.
14280                      If it is a reloc against some other .opd symbol,
14281                      then the symbol value will be adjusted later.  */
14282                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14283                     rel->r_addend += adjust;
14284                   else
14285                     relocation += adjust;
14286                 }
14287             }
14288         }
14289       else
14290         {
14291           bfd_boolean ignored;
14292
14293           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14294                                    r_symndx, symtab_hdr, sym_hashes,
14295                                    h_elf, sec, relocation,
14296                                    unresolved_reloc, warned, ignored);
14297           sym_name = h_elf->root.root.string;
14298           sym_type = h_elf->type;
14299           if (sec != NULL
14300               && sec->owner == output_bfd
14301               && strcmp (sec->name, ".opd") == 0)
14302             {
14303               /* This is a symbol defined in a linker script.  All
14304                  such are defined in output sections, even those
14305                  defined by simple assignment from a symbol defined in
14306                  an input section.  Transfer the symbol to an
14307                  appropriate input .opd section, so that a branch to
14308                  this symbol will be mapped to the location specified
14309                  by the opd entry.  */
14310               struct bfd_link_order *lo;
14311               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14312                 if (lo->type == bfd_indirect_link_order)
14313                   {
14314                     asection *isec = lo->u.indirect.section;
14315                     if (h_elf->root.u.def.value >= isec->output_offset
14316                         && h_elf->root.u.def.value < (isec->output_offset
14317                                                       + isec->size))
14318                       {
14319                         h_elf->root.u.def.value -= isec->output_offset;
14320                         h_elf->root.u.def.section = isec;
14321                         sec = isec;
14322                         break;
14323                       }
14324                   }
14325             }
14326         }
14327       h = (struct ppc_link_hash_entry *) h_elf;
14328
14329       if (sec != NULL && discarded_section (sec))
14330         {
14331           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14332                                input_bfd, input_section,
14333                                contents, rel->r_offset);
14334           wrel->r_offset = rel->r_offset;
14335           wrel->r_info = 0;
14336           wrel->r_addend = 0;
14337
14338           /* For ld -r, remove relocations in debug sections against
14339              symbols defined in discarded sections.  Not done for
14340              non-debug to preserve relocs in .eh_frame which the
14341              eh_frame editing code expects to be present.  */
14342           if (bfd_link_relocatable (info)
14343               && (input_section->flags & SEC_DEBUGGING))
14344             wrel--;
14345
14346           continue;
14347         }
14348
14349       if (bfd_link_relocatable (info))
14350         goto copy_reloc;
14351
14352       if (h != NULL && &h->elf == htab->elf.hgot)
14353         {
14354           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14355           sec = bfd_abs_section_ptr;
14356           unresolved_reloc = FALSE;
14357         }
14358
14359       /* TLS optimizations.  Replace instruction sequences and relocs
14360          based on information we collected in tls_optimize.  We edit
14361          RELOCS so that --emit-relocs will output something sensible
14362          for the final instruction stream.  */
14363       tls_mask = 0;
14364       tls_gd = 0;
14365       toc_symndx = 0;
14366       if (h != NULL)
14367         tls_mask = h->tls_mask;
14368       else if (local_got_ents != NULL)
14369         {
14370           struct plt_entry **local_plt = (struct plt_entry **)
14371             (local_got_ents + symtab_hdr->sh_info);
14372           unsigned char *lgot_masks = (unsigned char *)
14373             (local_plt + symtab_hdr->sh_info);
14374           tls_mask = lgot_masks[r_symndx];
14375         }
14376       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14377           && (r_type == R_PPC64_TLS
14378               || r_type == R_PPC64_TLSGD
14379               || r_type == R_PPC64_TLSLD))
14380         {
14381           /* Check for toc tls entries.  */
14382           unsigned char *toc_tls;
14383
14384           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14385                              &local_syms, rel, input_bfd))
14386             return FALSE;
14387
14388           if (toc_tls)
14389             tls_mask = *toc_tls;
14390         }
14391
14392       /* Check that tls relocs are used with tls syms, and non-tls
14393          relocs are used with non-tls syms.  */
14394       if (r_symndx != STN_UNDEF
14395           && r_type != R_PPC64_NONE
14396           && (h == NULL
14397               || h->elf.root.type == bfd_link_hash_defined
14398               || h->elf.root.type == bfd_link_hash_defweak)
14399           && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
14400         {
14401           if ((tls_mask & TLS_TLS) != 0
14402               && (r_type == R_PPC64_TLS
14403                   || r_type == R_PPC64_TLSGD
14404                   || r_type == R_PPC64_TLSLD))
14405             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
14406             ;
14407           else
14408             info->callbacks->einfo
14409               (!IS_PPC64_TLS_RELOC (r_type)
14410                /* xgettext:c-format */
14411                ? _("%H: %s used with TLS symbol `%pT'\n")
14412                /* xgettext:c-format */
14413                : _("%H: %s used with non-TLS symbol `%pT'\n"),
14414                input_bfd, input_section, rel->r_offset,
14415                ppc64_elf_howto_table[r_type]->name,
14416                sym_name);
14417         }
14418
14419       /* Ensure reloc mapping code below stays sane.  */
14420       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14421           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14422           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
14423           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14424           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14425           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14426           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
14427           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14428           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14429           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14430         abort ();
14431
14432       switch (r_type)
14433         {
14434         default:
14435           break;
14436
14437         case R_PPC64_LO_DS_OPT:
14438           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14439           if ((insn & (0x3f << 26)) != 58u << 26)
14440             abort ();
14441           insn += (14u << 26) - (58u << 26);
14442           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14443           r_type = R_PPC64_TOC16_LO;
14444           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14445           break;
14446
14447         case R_PPC64_TOC16:
14448         case R_PPC64_TOC16_LO:
14449         case R_PPC64_TOC16_DS:
14450         case R_PPC64_TOC16_LO_DS:
14451           {
14452             /* Check for toc tls entries.  */
14453             unsigned char *toc_tls;
14454             int retval;
14455
14456             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14457                                    &local_syms, rel, input_bfd);
14458             if (retval == 0)
14459               return FALSE;
14460
14461             if (toc_tls)
14462               {
14463                 tls_mask = *toc_tls;
14464                 if (r_type == R_PPC64_TOC16_DS
14465                     || r_type == R_PPC64_TOC16_LO_DS)
14466                   {
14467                     if ((tls_mask & TLS_TLS) != 0
14468                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14469                       goto toctprel;
14470                   }
14471                 else
14472                   {
14473                     /* If we found a GD reloc pair, then we might be
14474                        doing a GD->IE transition.  */
14475                     if (retval == 2)
14476                       {
14477                         tls_gd = TLS_GDIE;
14478                         if ((tls_mask & TLS_TLS) != 0
14479                             && (tls_mask & TLS_GD) == 0)
14480                           goto tls_ldgd_opt;
14481                       }
14482                     else if (retval == 3)
14483                       {
14484                         if ((tls_mask & TLS_TLS) != 0
14485                             && (tls_mask & TLS_LD) == 0)
14486                           goto tls_ldgd_opt;
14487                       }
14488                   }
14489               }
14490           }
14491           break;
14492
14493         case R_PPC64_GOT_TPREL16_HI:
14494         case R_PPC64_GOT_TPREL16_HA:
14495           if ((tls_mask & TLS_TLS) != 0
14496               && (tls_mask & TLS_TPREL) == 0)
14497             {
14498               rel->r_offset -= d_offset;
14499               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14500               r_type = R_PPC64_NONE;
14501               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14502             }
14503           break;
14504
14505         case R_PPC64_GOT_TPREL16_DS:
14506         case R_PPC64_GOT_TPREL16_LO_DS:
14507           if ((tls_mask & TLS_TLS) != 0
14508               && (tls_mask & TLS_TPREL) == 0)
14509             {
14510             toctprel:
14511               insn = bfd_get_32 (input_bfd,
14512                                  contents + rel->r_offset - d_offset);
14513               insn &= 31 << 21;
14514               insn |= 0x3c0d0000;       /* addis 0,13,0 */
14515               bfd_put_32 (input_bfd, insn,
14516                           contents + rel->r_offset - d_offset);
14517               r_type = R_PPC64_TPREL16_HA;
14518               if (toc_symndx != 0)
14519                 {
14520                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14521                   rel->r_addend = toc_addend;
14522                   /* We changed the symbol.  Start over in order to
14523                      get h, sym, sec etc. right.  */
14524                   goto again;
14525                 }
14526               else
14527                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14528             }
14529           break;
14530
14531         case R_PPC64_GOT_TPREL34:
14532           if ((tls_mask & TLS_TLS) != 0
14533               && (tls_mask & TLS_TPREL) == 0)
14534             {
14535               /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
14536               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14537               pinsn <<= 32;
14538               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14539               pinsn += ((2ULL << 56) + (-1ULL << 52)
14540                         + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
14541               bfd_put_32 (input_bfd, pinsn >> 32,
14542                           contents + rel->r_offset);
14543               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14544                           contents + rel->r_offset + 4);
14545               r_type = R_PPC64_TPREL34;
14546               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14547             }
14548           break;
14549
14550         case R_PPC64_TLS:
14551           if ((tls_mask & TLS_TLS) != 0
14552               && (tls_mask & TLS_TPREL) == 0)
14553             {
14554               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14555               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14556               if (insn == 0)
14557                 break;
14558               if ((rel->r_offset & 3) == 0)
14559                 {
14560                   bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14561                   /* Was PPC64_TLS which sits on insn boundary, now
14562                      PPC64_TPREL16_LO which is at low-order half-word.  */
14563                   rel->r_offset += d_offset;
14564                   r_type = R_PPC64_TPREL16_LO;
14565                   if (toc_symndx != 0)
14566                     {
14567                       rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14568                       rel->r_addend = toc_addend;
14569                       /* We changed the symbol.  Start over in order to
14570                          get h, sym, sec etc. right.  */
14571                       goto again;
14572                     }
14573                   else
14574                     rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14575                 }
14576               else if ((rel->r_offset & 3) == 1)
14577                 {
14578                   /* For pcrel IE to LE we already have the full
14579                      offset and thus don't need an addi here.  A nop
14580                      or mr will do.  */
14581                   if ((insn & (0x3f << 26)) == 14 << 26)
14582                     {
14583                       /* Extract regs from addi rt,ra,si.  */
14584                       unsigned int rt = (insn >> 21) & 0x1f;
14585                       unsigned int ra = (insn >> 16) & 0x1f;
14586                       if (rt == ra)
14587                         insn = NOP;
14588                       else
14589                         {
14590                           /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
14591                           insn = (rt << 16) | (ra << 21) | (ra << 11);
14592                           insn |= (31u << 26) | (444u << 1);
14593                         }
14594                     }
14595                   bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
14596                 }
14597             }
14598           break;
14599
14600         case R_PPC64_GOT_TLSGD16_HI:
14601         case R_PPC64_GOT_TLSGD16_HA:
14602           tls_gd = TLS_GDIE;
14603           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14604             goto tls_gdld_hi;
14605           break;
14606
14607         case R_PPC64_GOT_TLSLD16_HI:
14608         case R_PPC64_GOT_TLSLD16_HA:
14609           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14610             {
14611             tls_gdld_hi:
14612               if ((tls_mask & tls_gd) != 0)
14613                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14614                           + R_PPC64_GOT_TPREL16_DS);
14615               else
14616                 {
14617                   rel->r_offset -= d_offset;
14618                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14619                   r_type = R_PPC64_NONE;
14620                 }
14621               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14622             }
14623           break;
14624
14625         case R_PPC64_GOT_TLSGD16:
14626         case R_PPC64_GOT_TLSGD16_LO:
14627           tls_gd = TLS_GDIE;
14628           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14629             goto tls_ldgd_opt;
14630           break;
14631
14632         case R_PPC64_GOT_TLSLD16:
14633         case R_PPC64_GOT_TLSLD16_LO:
14634           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14635             {
14636               unsigned int insn1, insn2;
14637
14638             tls_ldgd_opt:
14639               offset = (bfd_vma) -1;
14640               /* If not using the newer R_PPC64_TLSGD/LD to mark
14641                  __tls_get_addr calls, we must trust that the call
14642                  stays with its arg setup insns, ie. that the next
14643                  reloc is the __tls_get_addr call associated with
14644                  the current reloc.  Edit both insns.  */
14645               if (input_section->nomark_tls_get_addr
14646                   && rel + 1 < relend
14647                   && branch_reloc_hash_match (input_bfd, rel + 1,
14648                                               htab->tls_get_addr,
14649                                               htab->tls_get_addr_fd))
14650                 offset = rel[1].r_offset;
14651               /* We read the low GOT_TLS (or TOC16) insn because we
14652                  need to keep the destination reg.  It may be
14653                  something other than the usual r3, and moved to r3
14654                  before the call by intervening code.  */
14655               insn1 = bfd_get_32 (input_bfd,
14656                                   contents + rel->r_offset - d_offset);
14657               if ((tls_mask & tls_gd) != 0)
14658                 {
14659                   /* IE */
14660                   insn1 &= (0x1f << 21) | (0x1f << 16);
14661                   insn1 |= 58 << 26;    /* ld */
14662                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14663                   if (offset != (bfd_vma) -1)
14664                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14665                   if (r_type == R_PPC64_TOC16
14666                       || r_type == R_PPC64_TOC16_LO)
14667                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14668                   else
14669                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
14670                               + R_PPC64_GOT_TPREL16_DS);
14671                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14672                 }
14673               else
14674                 {
14675                   /* LE */
14676                   insn1 &= 0x1f << 21;
14677                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
14678                   insn2 = 0x38630000;   /* addi 3,3,0 */
14679                   if (tls_gd == 0)
14680                     {
14681                       /* Was an LD reloc.  */
14682                       r_symndx = STN_UNDEF;
14683                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14684                     }
14685                   else if (toc_symndx != 0)
14686                     {
14687                       r_symndx = toc_symndx;
14688                       rel->r_addend = toc_addend;
14689                     }
14690                   r_type = R_PPC64_TPREL16_HA;
14691                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14692                   if (offset != (bfd_vma) -1)
14693                     {
14694                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14695                                                     R_PPC64_TPREL16_LO);
14696                       rel[1].r_offset = offset + d_offset;
14697                       rel[1].r_addend = rel->r_addend;
14698                     }
14699                 }
14700               bfd_put_32 (input_bfd, insn1,
14701                           contents + rel->r_offset - d_offset);
14702               if (offset != (bfd_vma) -1)
14703                 {
14704                   bfd_put_32 (input_bfd, insn2, contents + offset);
14705                   if (offset + 8 <= input_section->size)
14706                     {
14707                       insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14708                       if (insn2 == LD_R2_0R1 + STK_TOC (htab))
14709                         bfd_put_32 (input_bfd, NOP, contents + offset + 4);
14710                     }
14711                 }
14712               if ((tls_mask & tls_gd) == 0
14713                   && (tls_gd == 0 || toc_symndx != 0))
14714                 {
14715                   /* We changed the symbol.  Start over in order
14716                      to get h, sym, sec etc. right.  */
14717                   goto again;
14718                 }
14719             }
14720           break;
14721
14722         case R_PPC64_GOT_TLSGD34:
14723           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14724             {
14725               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14726               pinsn <<= 32;
14727               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14728               if ((tls_mask & TLS_GDIE) != 0)
14729                 {
14730                   /* IE, pla -> pld  */
14731                   pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
14732                   r_type = R_PPC64_GOT_TPREL34;
14733                 }
14734               else
14735                 {
14736                   /* LE, pla pcrel -> paddi r13  */
14737                   pinsn += (-1ULL << 52) + (13ULL << 16);
14738                   r_type = R_PPC64_TPREL34;
14739                 }
14740               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14741               bfd_put_32 (input_bfd, pinsn >> 32,
14742                           contents + rel->r_offset);
14743               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14744                           contents + rel->r_offset + 4);
14745             }
14746           break;
14747
14748         case R_PPC64_GOT_TLSLD34:
14749           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14750             {
14751               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14752               pinsn <<= 32;
14753               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14754               pinsn += (-1ULL << 52) + (13ULL << 16);
14755               bfd_put_32 (input_bfd, pinsn >> 32,
14756                           contents + rel->r_offset);
14757               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
14758                           contents + rel->r_offset + 4);
14759               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14760               r_symndx = STN_UNDEF;
14761               r_type = R_PPC64_TPREL34;
14762               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14763               goto again;
14764             }
14765           break;
14766
14767         case R_PPC64_TLSGD:
14768           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14769               && rel + 1 < relend)
14770             {
14771               unsigned int insn2;
14772               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14773
14774               offset = rel->r_offset;
14775               if (is_plt_seq_reloc (r_type1))
14776                 {
14777                   bfd_put_32 (output_bfd, NOP, contents + offset);
14778                   if (r_type1 == R_PPC64_PLT_PCREL34
14779                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14780                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14781                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14782                   break;
14783                 }
14784
14785               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14786                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14787
14788               if ((tls_mask & TLS_GDIE) != 0)
14789                 {
14790                   /* IE */
14791                   r_type = R_PPC64_NONE;
14792                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14793                 }
14794               else
14795                 {
14796                   /* LE */
14797                   if (toc_symndx != 0)
14798                     {
14799                       r_symndx = toc_symndx;
14800                       rel->r_addend = toc_addend;
14801                     }
14802                   if (r_type1 == R_PPC64_REL24_NOTOC
14803                       || r_type1 == R_PPC64_PLTCALL_NOTOC)
14804                     {
14805                       r_type = R_PPC64_NONE;
14806                       insn2 = NOP;
14807                     }
14808                   else
14809                     {
14810                       rel->r_offset = offset + d_offset;
14811                       r_type = R_PPC64_TPREL16_LO;
14812                       insn2 = 0x38630000;       /* addi 3,3,0 */
14813                     }
14814                 }
14815               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14816               /* Zap the reloc on the _tls_get_addr call too.  */
14817               BFD_ASSERT (offset == rel[1].r_offset);
14818               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14819               bfd_put_32 (input_bfd, insn2, contents + offset);
14820               if ((tls_mask & TLS_GDIE) == 0
14821                   && toc_symndx != 0
14822                   && r_type != R_PPC64_NONE)
14823                 goto again;
14824             }
14825           break;
14826
14827         case R_PPC64_TLSLD:
14828           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14829               && rel + 1 < relend)
14830             {
14831               unsigned int insn2;
14832               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
14833
14834               offset = rel->r_offset;
14835               if (is_plt_seq_reloc (r_type1))
14836                 {
14837                   bfd_put_32 (output_bfd, NOP, contents + offset);
14838                   if (r_type1 == R_PPC64_PLT_PCREL34
14839                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
14840                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14841                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14842                   break;
14843                 }
14844
14845               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14846                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14847
14848               if (r_type1 == R_PPC64_REL24_NOTOC
14849                   || r_type1 == R_PPC64_PLTCALL_NOTOC)
14850                 {
14851                   r_type = R_PPC64_NONE;
14852                   insn2 = NOP;
14853                 }
14854               else
14855                 {
14856                   rel->r_offset = offset + d_offset;
14857                   r_symndx = STN_UNDEF;
14858                   r_type = R_PPC64_TPREL16_LO;
14859                   rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14860                   insn2 = 0x38630000;   /* addi 3,3,0 */
14861                 }
14862               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14863               /* Zap the reloc on the _tls_get_addr call too.  */
14864               BFD_ASSERT (offset == rel[1].r_offset);
14865               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14866               bfd_put_32 (input_bfd, insn2, contents + offset);
14867               if (r_type != R_PPC64_NONE)
14868                 goto again;
14869             }
14870           break;
14871
14872         case R_PPC64_DTPMOD64:
14873           if (rel + 1 < relend
14874               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14875               && rel[1].r_offset == rel->r_offset + 8)
14876             {
14877               if ((tls_mask & TLS_GD) == 0)
14878                 {
14879                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14880                   if ((tls_mask & TLS_GDIE) != 0)
14881                     r_type = R_PPC64_TPREL64;
14882                   else
14883                     {
14884                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14885                       r_type = R_PPC64_NONE;
14886                     }
14887                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14888                 }
14889             }
14890           else
14891             {
14892               if ((tls_mask & TLS_LD) == 0)
14893                 {
14894                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14895                   r_type = R_PPC64_NONE;
14896                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14897                 }
14898             }
14899           break;
14900
14901         case R_PPC64_TPREL64:
14902           if ((tls_mask & TLS_TPREL) == 0)
14903             {
14904               r_type = R_PPC64_NONE;
14905               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14906             }
14907           break;
14908
14909         case R_PPC64_ENTRY:
14910           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14911           if (!bfd_link_pic (info)
14912               && !info->traditional_format
14913               && relocation + 0x80008000 <= 0xffffffff)
14914             {
14915               unsigned int insn1, insn2;
14916
14917               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14918               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14919               if ((insn1 & ~0xfffc) == LD_R2_0R12
14920                   && insn2 == ADD_R2_R2_R12)
14921                 {
14922                   bfd_put_32 (input_bfd,
14923                               LIS_R2 + PPC_HA (relocation),
14924                               contents + rel->r_offset);
14925                   bfd_put_32 (input_bfd,
14926                               ADDI_R2_R2 + PPC_LO (relocation),
14927                               contents + rel->r_offset + 4);
14928                 }
14929             }
14930           else
14931             {
14932               relocation -= (rel->r_offset
14933                              + input_section->output_offset
14934                              + input_section->output_section->vma);
14935               if (relocation + 0x80008000 <= 0xffffffff)
14936                 {
14937                   unsigned int insn1, insn2;
14938
14939                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14940                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14941                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14942                       && insn2 == ADD_R2_R2_R12)
14943                     {
14944                       bfd_put_32 (input_bfd,
14945                                   ADDIS_R2_R12 + PPC_HA (relocation),
14946                                   contents + rel->r_offset);
14947                       bfd_put_32 (input_bfd,
14948                                   ADDI_R2_R2 + PPC_LO (relocation),
14949                                   contents + rel->r_offset + 4);
14950                     }
14951                 }
14952             }
14953           break;
14954
14955         case R_PPC64_REL16_HA:
14956           /* If we are generating a non-PIC executable, edit
14957              .  0:      addis 2,12,.TOC.-0b@ha
14958              .          addi 2,2,.TOC.-0b@l
14959              used by ELFv2 global entry points to set up r2, to
14960              .          lis 2,.TOC.@ha
14961              .          addi 2,2,.TOC.@l
14962              if .TOC. is in range.  */
14963           if (!bfd_link_pic (info)
14964               && !info->traditional_format
14965               && !htab->opd_abi
14966               && rel->r_addend == d_offset
14967               && h != NULL && &h->elf == htab->elf.hgot
14968               && rel + 1 < relend
14969               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14970               && rel[1].r_offset == rel->r_offset + 4
14971               && rel[1].r_addend == rel->r_addend + 4
14972               && relocation + 0x80008000 <= 0xffffffff)
14973             {
14974               unsigned int insn1, insn2;
14975               offset = rel->r_offset - d_offset;
14976               insn1 = bfd_get_32 (input_bfd, contents + offset);
14977               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14978               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14979                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14980                 {
14981                   r_type = R_PPC64_ADDR16_HA;
14982                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14983                   rel->r_addend -= d_offset;
14984                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14985                   rel[1].r_addend -= d_offset + 4;
14986                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14987                 }
14988             }
14989           break;
14990         }
14991
14992       /* Handle other relocations that tweak non-addend part of insn.  */
14993       insn = 0;
14994       max_br_offset = 1 << 25;
14995       addend = rel->r_addend;
14996       reloc_dest = DEST_NORMAL;
14997       switch (r_type)
14998         {
14999         default:
15000           break;
15001
15002         case R_PPC64_TOCSAVE:
15003           if (relocation + addend == (rel->r_offset
15004                                       + input_section->output_offset
15005                                       + input_section->output_section->vma)
15006               && tocsave_find (htab, NO_INSERT,
15007                                &local_syms, rel, input_bfd))
15008             {
15009               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15010               if (insn == NOP
15011                   || insn == CROR_151515 || insn == CROR_313131)
15012                 bfd_put_32 (input_bfd,
15013                             STD_R2_0R1 + STK_TOC (htab),
15014                             contents + rel->r_offset);
15015             }
15016           break;
15017
15018           /* Branch taken prediction relocations.  */
15019         case R_PPC64_ADDR14_BRTAKEN:
15020         case R_PPC64_REL14_BRTAKEN:
15021           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
15022           /* Fall through.  */
15023
15024           /* Branch not taken prediction relocations.  */
15025         case R_PPC64_ADDR14_BRNTAKEN:
15026         case R_PPC64_REL14_BRNTAKEN:
15027           insn |= bfd_get_32 (input_bfd,
15028                               contents + rel->r_offset) & ~(0x01 << 21);
15029           /* Fall through.  */
15030
15031         case R_PPC64_REL14:
15032           max_br_offset = 1 << 15;
15033           /* Fall through.  */
15034
15035         case R_PPC64_REL24:
15036         case R_PPC64_REL24_NOTOC:
15037         case R_PPC64_PLTCALL:
15038         case R_PPC64_PLTCALL_NOTOC:
15039           /* Calls to functions with a different TOC, such as calls to
15040              shared objects, need to alter the TOC pointer.  This is
15041              done using a linkage stub.  A REL24 branching to these
15042              linkage stubs needs to be followed by a nop, as the nop
15043              will be replaced with an instruction to restore the TOC
15044              base pointer.  */
15045           fdh = h;
15046           if (h != NULL
15047               && h->oh != NULL
15048               && h->oh->is_func_descriptor)
15049             fdh = ppc_follow_link (h->oh);
15050           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15051                                            htab);
15052           if ((r_type == R_PPC64_PLTCALL
15053                || r_type == R_PPC64_PLTCALL_NOTOC)
15054               && stub_entry != NULL
15055               && stub_entry->stub_type >= ppc_stub_plt_call
15056               && stub_entry->stub_type <= ppc_stub_plt_call_both)
15057             stub_entry = NULL;
15058
15059           if (stub_entry != NULL
15060               && ((stub_entry->stub_type >= ppc_stub_plt_call
15061                    && stub_entry->stub_type <= ppc_stub_plt_call_both)
15062                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15063                   || stub_entry->stub_type == ppc_stub_plt_branch_both
15064                   || stub_entry->stub_type == ppc_stub_long_branch_r2off
15065                   || stub_entry->stub_type == ppc_stub_long_branch_both))
15066             {
15067               bfd_boolean can_plt_call = FALSE;
15068
15069               if (stub_entry->stub_type == ppc_stub_plt_call
15070                   && !htab->opd_abi
15071                   && htab->params->plt_localentry0 != 0
15072                   && is_elfv2_localentry0 (&h->elf))
15073                 {
15074                   /* The function doesn't use or change r2.  */
15075                   can_plt_call = TRUE;
15076                 }
15077               else if (r_type == R_PPC64_REL24_NOTOC)
15078                 {
15079                   /* NOTOC calls don't need to restore r2.  */
15080                   can_plt_call = TRUE;
15081                 }
15082
15083               /* All of these stubs may modify r2, so there must be a
15084                  branch and link followed by a nop.  The nop is
15085                  replaced by an insn to restore r2.  */
15086               else if (rel->r_offset + 8 <= input_section->size)
15087                 {
15088                   unsigned long br;
15089
15090                   br = bfd_get_32 (input_bfd,
15091                                    contents + rel->r_offset);
15092                   if ((br & 1) != 0)
15093                     {
15094                       unsigned long nop;
15095
15096                       nop = bfd_get_32 (input_bfd,
15097                                         contents + rel->r_offset + 4);
15098                       if (nop == LD_R2_0R1 + STK_TOC (htab))
15099                         can_plt_call = TRUE;
15100                       else if (nop == NOP
15101                                || nop == CROR_151515
15102                                || nop == CROR_313131)
15103                         {
15104                           if (h != NULL
15105                               && (h == htab->tls_get_addr_fd
15106                                   || h == htab->tls_get_addr)
15107                               && htab->params->tls_get_addr_opt)
15108                             {
15109                               /* Special stub used, leave nop alone.  */
15110                             }
15111                           else
15112                             bfd_put_32 (input_bfd,
15113                                         LD_R2_0R1 + STK_TOC (htab),
15114                                         contents + rel->r_offset + 4);
15115                           can_plt_call = TRUE;
15116                         }
15117                     }
15118                 }
15119
15120               if (!can_plt_call && h != NULL)
15121                 {
15122                   const char *name = h->elf.root.root.string;
15123
15124                   if (*name == '.')
15125                     ++name;
15126
15127                   if (strncmp (name, "__libc_start_main", 17) == 0
15128                       && (name[17] == 0 || name[17] == '@'))
15129                     {
15130                       /* Allow crt1 branch to go via a toc adjusting
15131                          stub.  Other calls that never return could do
15132                          the same, if we could detect such.  */
15133                       can_plt_call = TRUE;
15134                     }
15135                 }
15136
15137               if (!can_plt_call)
15138                 {
15139                   /* g++ as of 20130507 emits self-calls without a
15140                      following nop.  This is arguably wrong since we
15141                      have conflicting information.  On the one hand a
15142                      global symbol and on the other a local call
15143                      sequence, but don't error for this special case.
15144                      It isn't possible to cheaply verify we have
15145                      exactly such a call.  Allow all calls to the same
15146                      section.  */
15147                   asection *code_sec = sec;
15148
15149                   if (get_opd_info (sec) != NULL)
15150                     {
15151                       bfd_vma off = (relocation + addend
15152                                      - sec->output_section->vma
15153                                      - sec->output_offset);
15154
15155                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15156                     }
15157                   if (code_sec == input_section)
15158                     can_plt_call = TRUE;
15159                 }
15160
15161               if (!can_plt_call)
15162                 {
15163                   if (stub_entry->stub_type >= ppc_stub_plt_call
15164                       && stub_entry->stub_type <= ppc_stub_plt_call_both)
15165                     info->callbacks->einfo
15166                       /* xgettext:c-format */
15167                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
15168                          "(plt call stub)\n"),
15169                        input_bfd, input_section, rel->r_offset, sym_name);
15170                   else
15171                     info->callbacks->einfo
15172                       /* xgettext:c-format */
15173                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
15174                          "(toc save/adjust stub)\n"),
15175                        input_bfd, input_section, rel->r_offset, sym_name);
15176
15177                   bfd_set_error (bfd_error_bad_value);
15178                   ret = FALSE;
15179                 }
15180
15181               if (can_plt_call
15182                   && stub_entry->stub_type >= ppc_stub_plt_call
15183                   && stub_entry->stub_type <= ppc_stub_plt_call_both)
15184                 unresolved_reloc = FALSE;
15185             }
15186
15187           if ((stub_entry == NULL
15188                || stub_entry->stub_type == ppc_stub_long_branch
15189                || stub_entry->stub_type == ppc_stub_plt_branch)
15190               && get_opd_info (sec) != NULL)
15191             {
15192               /* The branch destination is the value of the opd entry. */
15193               bfd_vma off = (relocation + addend
15194                              - sec->output_section->vma
15195                              - sec->output_offset);
15196               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15197               if (dest != (bfd_vma) -1)
15198                 {
15199                   relocation = dest;
15200                   addend = 0;
15201                   reloc_dest = DEST_OPD;
15202                 }
15203             }
15204
15205           /* If the branch is out of reach we ought to have a long
15206              branch stub.  */
15207           from = (rel->r_offset
15208                   + input_section->output_offset
15209                   + input_section->output_section->vma);
15210
15211           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15212                                                   ? fdh->elf.other
15213                                                   : sym->st_other);
15214
15215           if (stub_entry != NULL
15216               && (stub_entry->stub_type == ppc_stub_long_branch
15217                   || stub_entry->stub_type == ppc_stub_plt_branch)
15218               && (r_type == R_PPC64_ADDR14_BRTAKEN
15219                   || r_type == R_PPC64_ADDR14_BRNTAKEN
15220                   || (relocation + addend - from + max_br_offset
15221                       < 2 * max_br_offset)))
15222             /* Don't use the stub if this branch is in range.  */
15223             stub_entry = NULL;
15224
15225           if (stub_entry != NULL
15226               && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15227                   || stub_entry->stub_type == ppc_stub_long_branch_both
15228                   || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15229                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
15230               && (r_type != R_PPC64_REL24_NOTOC
15231                   || ((fdh ? fdh->elf.other : sym->st_other)
15232                       & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15233               && (relocation + addend - from + max_br_offset
15234                   < 2 * max_br_offset))
15235             stub_entry = NULL;
15236
15237           if (stub_entry != NULL
15238               && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15239                   || stub_entry->stub_type == ppc_stub_long_branch_both
15240                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15241                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
15242               && r_type == R_PPC64_REL24_NOTOC
15243               && (relocation + addend - from + max_br_offset
15244                   < 2 * max_br_offset))
15245             stub_entry = NULL;
15246
15247           if (stub_entry != NULL)
15248             {
15249               /* Munge up the value and addend so that we call the stub
15250                  rather than the procedure directly.  */
15251               asection *stub_sec = stub_entry->group->stub_sec;
15252
15253               if (stub_entry->stub_type == ppc_stub_save_res)
15254                 relocation += (stub_sec->output_offset
15255                                + stub_sec->output_section->vma
15256                                + stub_sec->size - htab->sfpr->size
15257                                - htab->sfpr->output_offset
15258                                - htab->sfpr->output_section->vma);
15259               else
15260                 relocation = (stub_entry->stub_offset
15261                               + stub_sec->output_offset
15262                               + stub_sec->output_section->vma);
15263               addend = 0;
15264               reloc_dest = DEST_STUB;
15265
15266               if (((stub_entry->stub_type == ppc_stub_plt_call
15267                     && ALWAYS_EMIT_R2SAVE)
15268                    || stub_entry->stub_type == ppc_stub_plt_call_r2save
15269                    || stub_entry->stub_type == ppc_stub_plt_call_both)
15270                   && !(h != NULL
15271                        && (h == htab->tls_get_addr_fd
15272                            || h == htab->tls_get_addr)
15273                        && htab->params->tls_get_addr_opt)
15274                   && rel + 1 < relend
15275                   && rel[1].r_offset == rel->r_offset + 4
15276                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15277                 relocation += 4;
15278               else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15279                         || stub_entry->stub_type == ppc_stub_plt_branch_both
15280                         || stub_entry->stub_type == ppc_stub_plt_call_both)
15281                        && r_type == R_PPC64_REL24_NOTOC)
15282                 relocation += 4;
15283
15284               if (r_type == R_PPC64_REL24_NOTOC
15285                   && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15286                       || stub_entry->stub_type == ppc_stub_plt_call_both))
15287                 htab->notoc_plt = 1;
15288             }
15289
15290           if (insn != 0)
15291             {
15292               if (is_isa_v2)
15293                 {
15294                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
15295                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
15296                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
15297                   if ((insn & (0x14 << 21)) == (0x04 << 21))
15298                     insn |= 0x02 << 21;
15299                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
15300                     insn |= 0x08 << 21;
15301                   else
15302                     break;
15303                 }
15304               else
15305                 {
15306                   /* Invert 'y' bit if not the default.  */
15307                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
15308                     insn ^= 0x01 << 21;
15309                 }
15310
15311               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15312             }
15313
15314           /* NOP out calls to undefined weak functions.
15315              We can thus call a weak function without first
15316              checking whether the function is defined.  */
15317           else if (h != NULL
15318                    && h->elf.root.type == bfd_link_hash_undefweak
15319                    && h->elf.dynindx == -1
15320                    && (r_type == R_PPC64_REL24
15321                        || r_type == R_PPC64_REL24_NOTOC)
15322                    && relocation == 0
15323                    && addend == 0)
15324             {
15325               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15326               goto copy_reloc;
15327             }
15328           break;
15329
15330         case R_PPC64_GOT16_DS:
15331           from = TOCstart + htab->sec_info[input_section->id].toc_off;
15332           if (relocation + addend - from + 0x8000 < 0x10000
15333               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15334             {
15335               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15336               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15337                 {
15338                   insn += (14u << 26) - (58u << 26);
15339                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15340                   r_type = R_PPC64_TOC16;
15341                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15342                 }
15343             }
15344           break;
15345
15346         case R_PPC64_GOT16_LO_DS:
15347         case R_PPC64_GOT16_HA:
15348           from = TOCstart + htab->sec_info[input_section->id].toc_off;
15349           if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15350               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15351             {
15352               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15353               if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
15354                 {
15355                   insn += (14u << 26) - (58u << 26);
15356                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15357                   r_type = R_PPC64_TOC16_LO;
15358                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15359                 }
15360               else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
15361                 {
15362                   r_type = R_PPC64_TOC16_HA;
15363                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15364                 }
15365             }
15366           break;
15367
15368         case R_PPC64_GOT_PCREL34:
15369           from = (rel->r_offset
15370                   + input_section->output_section->vma
15371                   + input_section->output_offset);
15372           if (relocation - from + (1ULL << 33) < 1ULL << 34
15373               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15374             {
15375               offset = rel->r_offset;
15376               pinsn = bfd_get_32 (input_bfd, contents + offset);
15377               pinsn <<= 32;
15378               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15379               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15380                    == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15381                 {
15382                   /* Replace with paddi.  */
15383                   pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15384                   r_type = R_PPC64_PCREL34;
15385                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15386                   bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15387                   bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15388                   goto pcrelopt;
15389                 }
15390             }
15391           break;
15392
15393         case R_PPC64_PCREL34:
15394           if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15395             {
15396               offset = rel->r_offset;
15397               pinsn = bfd_get_32 (input_bfd, contents + offset);
15398               pinsn <<= 32;
15399               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15400               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15401                    == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15402                        | (14ULL << 26) /* paddi */))
15403                 {
15404                 pcrelopt:
15405                   if (rel + 1 < relend
15406                       && rel[1].r_offset == offset
15407                       && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15408                     {
15409                       bfd_vma off2 = rel[1].r_addend;
15410                       if (off2 == 0)
15411                         /* zero means next insn.  */
15412                         off2 = 8;
15413                       off2 += offset;
15414                       if (off2 + 4 <= input_section->size)
15415                         {
15416                           uint64_t pinsn2;
15417                           bfd_signed_vma addend_off;
15418                           pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15419                           pinsn2 <<= 32;
15420                           if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15421                             {
15422                               if (off2 + 8 > input_section->size)
15423                                 break;
15424                               pinsn2 |= bfd_get_32 (input_bfd,
15425                                                     contents + off2 + 4);
15426                             }
15427                           if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
15428                             {
15429                               addend += addend_off;
15430                               rel->r_addend = addend;
15431                               bfd_put_32 (input_bfd, pinsn >> 32,
15432                                           contents + offset);
15433                               bfd_put_32 (input_bfd, pinsn,
15434                                           contents + offset + 4);
15435                               bfd_put_32 (input_bfd, pinsn2 >> 32,
15436                                           contents + off2);
15437                               if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15438                                 bfd_put_32 (input_bfd, pinsn2,
15439                                             contents + off2 + 4);
15440                             }
15441                         }
15442                     }
15443                 }
15444             }
15445           break;
15446         }
15447
15448       tls_type = 0;
15449       save_unresolved_reloc = unresolved_reloc;
15450       switch (r_type)
15451         {
15452         default:
15453           /* xgettext:c-format */
15454           _bfd_error_handler (_("%pB: %s unsupported"),
15455                               input_bfd, ppc64_elf_howto_table[r_type]->name);
15456
15457           bfd_set_error (bfd_error_bad_value);
15458           ret = FALSE;
15459           goto copy_reloc;
15460
15461         case R_PPC64_NONE:
15462         case R_PPC64_TLS:
15463         case R_PPC64_TLSGD:
15464         case R_PPC64_TLSLD:
15465         case R_PPC64_TOCSAVE:
15466         case R_PPC64_GNU_VTINHERIT:
15467         case R_PPC64_GNU_VTENTRY:
15468         case R_PPC64_ENTRY:
15469         case R_PPC64_PCREL_OPT:
15470           goto copy_reloc;
15471
15472           /* GOT16 relocations.  Like an ADDR16 using the symbol's
15473              address in the GOT as relocation value instead of the
15474              symbol's value itself.  Also, create a GOT entry for the
15475              symbol and put the symbol value there.  */
15476         case R_PPC64_GOT_TLSGD16:
15477         case R_PPC64_GOT_TLSGD16_LO:
15478         case R_PPC64_GOT_TLSGD16_HI:
15479         case R_PPC64_GOT_TLSGD16_HA:
15480         case R_PPC64_GOT_TLSGD34:
15481           tls_type = TLS_TLS | TLS_GD;
15482           goto dogot;
15483
15484         case R_PPC64_GOT_TLSLD16:
15485         case R_PPC64_GOT_TLSLD16_LO:
15486         case R_PPC64_GOT_TLSLD16_HI:
15487         case R_PPC64_GOT_TLSLD16_HA:
15488         case R_PPC64_GOT_TLSLD34:
15489           tls_type = TLS_TLS | TLS_LD;
15490           goto dogot;
15491
15492         case R_PPC64_GOT_TPREL16_DS:
15493         case R_PPC64_GOT_TPREL16_LO_DS:
15494         case R_PPC64_GOT_TPREL16_HI:
15495         case R_PPC64_GOT_TPREL16_HA:
15496         case R_PPC64_GOT_TPREL34:
15497           tls_type = TLS_TLS | TLS_TPREL;
15498           goto dogot;
15499
15500         case R_PPC64_GOT_DTPREL16_DS:
15501         case R_PPC64_GOT_DTPREL16_LO_DS:
15502         case R_PPC64_GOT_DTPREL16_HI:
15503         case R_PPC64_GOT_DTPREL16_HA:
15504         case R_PPC64_GOT_DTPREL34:
15505           tls_type = TLS_TLS | TLS_DTPREL;
15506           goto dogot;
15507
15508         case R_PPC64_GOT16:
15509         case R_PPC64_GOT16_LO:
15510         case R_PPC64_GOT16_HI:
15511         case R_PPC64_GOT16_HA:
15512         case R_PPC64_GOT16_DS:
15513         case R_PPC64_GOT16_LO_DS:
15514         case R_PPC64_GOT_PCREL34:
15515         dogot:
15516           {
15517             /* Relocation is to the entry for this symbol in the global
15518                offset table.  */
15519             asection *got;
15520             bfd_vma *offp;
15521             bfd_vma off;
15522             unsigned long indx = 0;
15523             struct got_entry *ent;
15524
15525             if (tls_type == (TLS_TLS | TLS_LD)
15526                 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15527               ent = ppc64_tlsld_got (input_bfd);
15528             else
15529               {
15530                 if (h != NULL)
15531                   {
15532                     if (!htab->elf.dynamic_sections_created
15533                         || h->elf.dynindx == -1
15534                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15535                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15536                       /* This is actually a static link, or it is a
15537                          -Bsymbolic link and the symbol is defined
15538                          locally, or the symbol was forced to be local
15539                          because of a version file.  */
15540                       ;
15541                     else
15542                       {
15543                         indx = h->elf.dynindx;
15544                         unresolved_reloc = FALSE;
15545                       }
15546                     ent = h->elf.got.glist;
15547                   }
15548                 else
15549                   {
15550                     if (local_got_ents == NULL)
15551                       abort ();
15552                     ent = local_got_ents[r_symndx];
15553                   }
15554
15555                 for (; ent != NULL; ent = ent->next)
15556                   if (ent->addend == orig_rel.r_addend
15557                       && ent->owner == input_bfd
15558                       && ent->tls_type == tls_type)
15559                     break;
15560               }
15561
15562             if (ent == NULL)
15563               abort ();
15564             if (ent->is_indirect)
15565               ent = ent->got.ent;
15566             offp = &ent->got.offset;
15567             got = ppc64_elf_tdata (ent->owner)->got;
15568             if (got == NULL)
15569               abort ();
15570
15571             /* The offset must always be a multiple of 8.  We use the
15572                least significant bit to record whether we have already
15573                processed this entry.  */
15574             off = *offp;
15575             if ((off & 1) != 0)
15576               off &= ~1;
15577             else
15578               {
15579                 /* Generate relocs for the dynamic linker, except in
15580                    the case of TLSLD where we'll use one entry per
15581                    module.  */
15582                 asection *relgot;
15583                 bfd_boolean ifunc;
15584
15585                 *offp = off | 1;
15586                 relgot = NULL;
15587                 ifunc = (h != NULL
15588                          ? h->elf.type == STT_GNU_IFUNC
15589                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15590                 if (ifunc)
15591                   {
15592                     relgot = htab->elf.irelplt;
15593                     if (indx == 0)
15594                       htab->local_ifunc_resolver = 1;
15595                     else if (is_static_defined (&h->elf))
15596                       htab->maybe_local_ifunc_resolver = 1;
15597                   }
15598                 else if (indx != 0
15599                          || (bfd_link_pic (info)
15600                              && (h == NULL
15601                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15602                              && !(tls_type != 0
15603                                   && bfd_link_executable (info)
15604                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15605                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
15606                 if (relgot != NULL)
15607                   {
15608                     outrel.r_offset = (got->output_section->vma
15609                                        + got->output_offset
15610                                        + off);
15611                     outrel.r_addend = orig_rel.r_addend;
15612                     if (tls_type & (TLS_LD | TLS_GD))
15613                       {
15614                         outrel.r_addend = 0;
15615                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15616                         if (tls_type == (TLS_TLS | TLS_GD))
15617                           {
15618                             loc = relgot->contents;
15619                             loc += (relgot->reloc_count++
15620                                     * sizeof (Elf64_External_Rela));
15621                             bfd_elf64_swap_reloca_out (output_bfd,
15622                                                        &outrel, loc);
15623                             outrel.r_offset += 8;
15624                             outrel.r_addend = orig_rel.r_addend;
15625                             outrel.r_info
15626                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15627                           }
15628                       }
15629                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
15630                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15631                     else if (tls_type == (TLS_TLS | TLS_TPREL))
15632                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15633                     else if (indx != 0)
15634                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15635                     else
15636                       {
15637                         if (ifunc)
15638                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15639                         else
15640                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15641
15642                         /* Write the .got section contents for the sake
15643                            of prelink.  */
15644                         loc = got->contents + off;
15645                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15646                                     loc);
15647                       }
15648
15649                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15650                       {
15651                         outrel.r_addend += relocation;
15652                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15653                           {
15654                             if (htab->elf.tls_sec == NULL)
15655                               outrel.r_addend = 0;
15656                             else
15657                               outrel.r_addend -= htab->elf.tls_sec->vma;
15658                           }
15659                       }
15660                     loc = relgot->contents;
15661                     loc += (relgot->reloc_count++
15662                             * sizeof (Elf64_External_Rela));
15663                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15664                   }
15665
15666                 /* Init the .got section contents here if we're not
15667                    emitting a reloc.  */
15668                 else
15669                   {
15670                     relocation += orig_rel.r_addend;
15671                     if (tls_type != 0)
15672                       {
15673                         if (htab->elf.tls_sec == NULL)
15674                           relocation = 0;
15675                         else
15676                           {
15677                             if (tls_type & TLS_LD)
15678                               relocation = 0;
15679                             else
15680                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15681                             if (tls_type & TLS_TPREL)
15682                               relocation += DTP_OFFSET - TP_OFFSET;
15683                           }
15684
15685                         if (tls_type & (TLS_GD | TLS_LD))
15686                           {
15687                             bfd_put_64 (output_bfd, relocation,
15688                                         got->contents + off + 8);
15689                             relocation = 1;
15690                           }
15691                       }
15692                     bfd_put_64 (output_bfd, relocation,
15693                                 got->contents + off);
15694                   }
15695               }
15696
15697             if (off >= (bfd_vma) -2)
15698               abort ();
15699
15700             relocation = got->output_section->vma + got->output_offset + off;
15701             addend = 0;
15702             if (!(r_type == R_PPC64_GOT_PCREL34
15703                   || r_type == R_PPC64_GOT_TLSGD34
15704                   || r_type == R_PPC64_GOT_TLSLD34
15705                   || r_type == R_PPC64_GOT_TPREL34
15706                   || r_type == R_PPC64_GOT_DTPREL34))
15707               addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15708           }
15709           break;
15710
15711         case R_PPC64_PLT16_HA:
15712         case R_PPC64_PLT16_HI:
15713         case R_PPC64_PLT16_LO:
15714         case R_PPC64_PLT16_LO_DS:
15715         case R_PPC64_PLT_PCREL34:
15716         case R_PPC64_PLT_PCREL34_NOTOC:
15717         case R_PPC64_PLT32:
15718         case R_PPC64_PLT64:
15719         case R_PPC64_PLTSEQ:
15720         case R_PPC64_PLTSEQ_NOTOC:
15721         case R_PPC64_PLTCALL:
15722         case R_PPC64_PLTCALL_NOTOC:
15723           /* Relocation is to the entry for this symbol in the
15724              procedure linkage table.  */
15725           unresolved_reloc = TRUE;
15726           {
15727             struct plt_entry **plt_list = NULL;
15728             if (h != NULL)
15729               plt_list = &h->elf.plt.plist;
15730             else if (local_got_ents != NULL)
15731               {
15732                 struct plt_entry **local_plt = (struct plt_entry **)
15733                   (local_got_ents + symtab_hdr->sh_info);
15734                 plt_list = local_plt + r_symndx;
15735               }
15736             if (plt_list)
15737               {
15738                 struct plt_entry *ent;
15739
15740                 for (ent = *plt_list; ent != NULL; ent = ent->next)
15741                   if (ent->plt.offset != (bfd_vma) -1
15742                       && ent->addend == orig_rel.r_addend)
15743                     {
15744                       asection *plt;
15745                       bfd_vma got;
15746
15747                       plt = htab->elf.splt;
15748                       if (!htab->elf.dynamic_sections_created
15749                           || h == NULL
15750                           || h->elf.dynindx == -1)
15751                         {
15752                           if (h != NULL
15753                               ? h->elf.type == STT_GNU_IFUNC
15754                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15755                             plt = htab->elf.iplt;
15756                           else
15757                             plt = htab->pltlocal;
15758                         }
15759                       relocation = (plt->output_section->vma
15760                                     + plt->output_offset
15761                                     + ent->plt.offset);
15762                       if (r_type == R_PPC64_PLT16_HA
15763                           || r_type == R_PPC64_PLT16_HI
15764                           || r_type == R_PPC64_PLT16_LO
15765                           || r_type == R_PPC64_PLT16_LO_DS)
15766                         {
15767                           got = (elf_gp (output_bfd)
15768                                  + htab->sec_info[input_section->id].toc_off);
15769                           relocation -= got;
15770                         }
15771                       addend = 0;
15772                       unresolved_reloc = FALSE;
15773                       break;
15774                     }
15775               }
15776           }
15777           break;
15778
15779         case R_PPC64_TOC:
15780           /* Relocation value is TOC base.  */
15781           relocation = TOCstart;
15782           if (r_symndx == STN_UNDEF)
15783             relocation += htab->sec_info[input_section->id].toc_off;
15784           else if (unresolved_reloc)
15785             ;
15786           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15787             relocation += htab->sec_info[sec->id].toc_off;
15788           else
15789             unresolved_reloc = TRUE;
15790           goto dodyn;
15791
15792           /* TOC16 relocs.  We want the offset relative to the TOC base,
15793              which is the address of the start of the TOC plus 0x8000.
15794              The TOC consists of sections .got, .toc, .tocbss, and .plt,
15795              in this order.  */
15796         case R_PPC64_TOC16:
15797         case R_PPC64_TOC16_LO:
15798         case R_PPC64_TOC16_HI:
15799         case R_PPC64_TOC16_DS:
15800         case R_PPC64_TOC16_LO_DS:
15801         case R_PPC64_TOC16_HA:
15802           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15803           break;
15804
15805           /* Relocate against the beginning of the section.  */
15806         case R_PPC64_SECTOFF:
15807         case R_PPC64_SECTOFF_LO:
15808         case R_PPC64_SECTOFF_HI:
15809         case R_PPC64_SECTOFF_DS:
15810         case R_PPC64_SECTOFF_LO_DS:
15811         case R_PPC64_SECTOFF_HA:
15812           if (sec != NULL)
15813             addend -= sec->output_section->vma;
15814           break;
15815
15816         case R_PPC64_REL16:
15817         case R_PPC64_REL16_LO:
15818         case R_PPC64_REL16_HI:
15819         case R_PPC64_REL16_HA:
15820         case R_PPC64_REL16_HIGH:
15821         case R_PPC64_REL16_HIGHA:
15822         case R_PPC64_REL16_HIGHER:
15823         case R_PPC64_REL16_HIGHERA:
15824         case R_PPC64_REL16_HIGHEST:
15825         case R_PPC64_REL16_HIGHESTA:
15826         case R_PPC64_REL16_HIGHER34:
15827         case R_PPC64_REL16_HIGHERA34:
15828         case R_PPC64_REL16_HIGHEST34:
15829         case R_PPC64_REL16_HIGHESTA34:
15830         case R_PPC64_REL16DX_HA:
15831         case R_PPC64_REL14:
15832         case R_PPC64_REL14_BRNTAKEN:
15833         case R_PPC64_REL14_BRTAKEN:
15834         case R_PPC64_REL24:
15835         case R_PPC64_REL24_NOTOC:
15836         case R_PPC64_PCREL34:
15837         case R_PPC64_PCREL28:
15838           break;
15839
15840         case R_PPC64_TPREL16:
15841         case R_PPC64_TPREL16_LO:
15842         case R_PPC64_TPREL16_HI:
15843         case R_PPC64_TPREL16_HA:
15844         case R_PPC64_TPREL16_DS:
15845         case R_PPC64_TPREL16_LO_DS:
15846         case R_PPC64_TPREL16_HIGH:
15847         case R_PPC64_TPREL16_HIGHA:
15848         case R_PPC64_TPREL16_HIGHER:
15849         case R_PPC64_TPREL16_HIGHERA:
15850         case R_PPC64_TPREL16_HIGHEST:
15851         case R_PPC64_TPREL16_HIGHESTA:
15852         case R_PPC64_TPREL34:
15853           if (h != NULL
15854               && h->elf.root.type == bfd_link_hash_undefweak
15855               && h->elf.dynindx == -1)
15856             {
15857               /* Make this relocation against an undefined weak symbol
15858                  resolve to zero.  This is really just a tweak, since
15859                  code using weak externs ought to check that they are
15860                  defined before using them.  */
15861               bfd_byte *p = contents + rel->r_offset - d_offset;
15862
15863               insn = bfd_get_32 (input_bfd, p);
15864               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15865               if (insn != 0)
15866                 bfd_put_32 (input_bfd, insn, p);
15867               break;
15868             }
15869           if (htab->elf.tls_sec != NULL)
15870             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15871           /* The TPREL16 relocs shouldn't really be used in shared
15872              libs or with non-local symbols as that will result in
15873              DT_TEXTREL being set, but support them anyway.  */
15874           goto dodyn;
15875
15876         case R_PPC64_DTPREL16:
15877         case R_PPC64_DTPREL16_LO:
15878         case R_PPC64_DTPREL16_HI:
15879         case R_PPC64_DTPREL16_HA:
15880         case R_PPC64_DTPREL16_DS:
15881         case R_PPC64_DTPREL16_LO_DS:
15882         case R_PPC64_DTPREL16_HIGH:
15883         case R_PPC64_DTPREL16_HIGHA:
15884         case R_PPC64_DTPREL16_HIGHER:
15885         case R_PPC64_DTPREL16_HIGHERA:
15886         case R_PPC64_DTPREL16_HIGHEST:
15887         case R_PPC64_DTPREL16_HIGHESTA:
15888         case R_PPC64_DTPREL34:
15889           if (htab->elf.tls_sec != NULL)
15890             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15891           break;
15892
15893         case R_PPC64_ADDR64_LOCAL:
15894           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15895                                               ? h->elf.other
15896                                               : sym->st_other);
15897           break;
15898
15899         case R_PPC64_DTPMOD64:
15900           relocation = 1;
15901           addend = 0;
15902           goto dodyn;
15903
15904         case R_PPC64_TPREL64:
15905           if (htab->elf.tls_sec != NULL)
15906             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15907           goto dodyn;
15908
15909         case R_PPC64_DTPREL64:
15910           if (htab->elf.tls_sec != NULL)
15911             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15912           /* Fall through.  */
15913
15914           /* Relocations that may need to be propagated if this is a
15915              dynamic object.  */
15916         case R_PPC64_REL30:
15917         case R_PPC64_REL32:
15918         case R_PPC64_REL64:
15919         case R_PPC64_ADDR14:
15920         case R_PPC64_ADDR14_BRNTAKEN:
15921         case R_PPC64_ADDR14_BRTAKEN:
15922         case R_PPC64_ADDR16:
15923         case R_PPC64_ADDR16_DS:
15924         case R_PPC64_ADDR16_HA:
15925         case R_PPC64_ADDR16_HI:
15926         case R_PPC64_ADDR16_HIGH:
15927         case R_PPC64_ADDR16_HIGHA:
15928         case R_PPC64_ADDR16_HIGHER:
15929         case R_PPC64_ADDR16_HIGHERA:
15930         case R_PPC64_ADDR16_HIGHEST:
15931         case R_PPC64_ADDR16_HIGHESTA:
15932         case R_PPC64_ADDR16_LO:
15933         case R_PPC64_ADDR16_LO_DS:
15934         case R_PPC64_ADDR16_HIGHER34:
15935         case R_PPC64_ADDR16_HIGHERA34:
15936         case R_PPC64_ADDR16_HIGHEST34:
15937         case R_PPC64_ADDR16_HIGHESTA34:
15938         case R_PPC64_ADDR24:
15939         case R_PPC64_ADDR32:
15940         case R_PPC64_ADDR64:
15941         case R_PPC64_UADDR16:
15942         case R_PPC64_UADDR32:
15943         case R_PPC64_UADDR64:
15944         case R_PPC64_D34:
15945         case R_PPC64_D34_LO:
15946         case R_PPC64_D34_HI30:
15947         case R_PPC64_D34_HA30:
15948         case R_PPC64_D28:
15949         dodyn:
15950           if ((input_section->flags & SEC_ALLOC) == 0)
15951             break;
15952
15953           if (NO_OPD_RELOCS && is_opd)
15954             break;
15955
15956           if (bfd_link_pic (info)
15957               ? ((h == NULL
15958                   || h->dyn_relocs != NULL)
15959                  && ((h != NULL && pc_dynrelocs (h))
15960                      || must_be_dyn_reloc (info, r_type)))
15961               : (h != NULL
15962                  ? h->dyn_relocs != NULL
15963                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15964             {
15965               bfd_boolean skip, relocate;
15966               asection *sreloc;
15967               bfd_vma out_off;
15968               long indx = 0;
15969
15970               /* When generating a dynamic object, these relocations
15971                  are copied into the output file to be resolved at run
15972                  time.  */
15973
15974               skip = FALSE;
15975               relocate = FALSE;
15976
15977               out_off = _bfd_elf_section_offset (output_bfd, info,
15978                                                  input_section, rel->r_offset);
15979               if (out_off == (bfd_vma) -1)
15980                 skip = TRUE;
15981               else if (out_off == (bfd_vma) -2)
15982                 skip = TRUE, relocate = TRUE;
15983               out_off += (input_section->output_section->vma
15984                           + input_section->output_offset);
15985               outrel.r_offset = out_off;
15986               outrel.r_addend = rel->r_addend;
15987
15988               /* Optimize unaligned reloc use.  */
15989               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15990                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15991                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15992               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15993                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15994                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15995               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15996                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15997                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15998
15999               if (skip)
16000                 memset (&outrel, 0, sizeof outrel);
16001               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16002                        && !is_opd
16003                        && r_type != R_PPC64_TOC)
16004                 {
16005                   indx = h->elf.dynindx;
16006                   BFD_ASSERT (indx != -1);
16007                   outrel.r_info = ELF64_R_INFO (indx, r_type);
16008                 }
16009               else
16010                 {
16011                   /* This symbol is local, or marked to become local,
16012                      or this is an opd section reloc which must point
16013                      at a local function.  */
16014                   outrel.r_addend += relocation;
16015                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16016                     {
16017                       if (is_opd && h != NULL)
16018                         {
16019                           /* Lie about opd entries.  This case occurs
16020                              when building shared libraries and we
16021                              reference a function in another shared
16022                              lib.  The same thing happens for a weak
16023                              definition in an application that's
16024                              overridden by a strong definition in a
16025                              shared lib.  (I believe this is a generic
16026                              bug in binutils handling of weak syms.)
16027                              In these cases we won't use the opd
16028                              entry in this lib.  */
16029                           unresolved_reloc = FALSE;
16030                         }
16031                       if (!is_opd
16032                           && r_type == R_PPC64_ADDR64
16033                           && (h != NULL
16034                               ? h->elf.type == STT_GNU_IFUNC
16035                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16036                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16037                       else
16038                         {
16039                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16040
16041                           /* We need to relocate .opd contents for ld.so.
16042                              Prelink also wants simple and consistent rules
16043                              for relocs.  This make all RELATIVE relocs have
16044                              *r_offset equal to r_addend.  */
16045                           relocate = TRUE;
16046                         }
16047                     }
16048                   else
16049                     {
16050                       if (h != NULL
16051                           ? h->elf.type == STT_GNU_IFUNC
16052                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16053                         {
16054                           info->callbacks->einfo
16055                             /* xgettext:c-format */
16056                             (_("%H: %s for indirect "
16057                                "function `%pT' unsupported\n"),
16058                              input_bfd, input_section, rel->r_offset,
16059                              ppc64_elf_howto_table[r_type]->name,
16060                              sym_name);
16061                           ret = FALSE;
16062                         }
16063                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16064                         ;
16065                       else if (sec == NULL || sec->owner == NULL)
16066                         {
16067                           bfd_set_error (bfd_error_bad_value);
16068                           return FALSE;
16069                         }
16070                       else
16071                         {
16072                           asection *osec = sec->output_section;
16073
16074                           if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16075                             {
16076                               /* TLS symbol values are relative to the
16077                                  TLS segment.  Dynamic relocations for
16078                                  local TLS symbols therefore can't be
16079                                  reduced to a relocation against their
16080                                  section symbol because it holds the
16081                                  address of the section, not a value
16082                                  relative to the TLS segment.  We could
16083                                  change the .tdata dynamic section symbol
16084                                  to be zero value but STN_UNDEF works
16085                                  and is used elsewhere, eg. for TPREL64
16086                                  GOT relocs against local TLS symbols.  */
16087                               osec = htab->elf.tls_sec;
16088                               indx = 0;
16089                             }
16090                           else
16091                             {
16092                               indx = elf_section_data (osec)->dynindx;
16093                               if (indx == 0)
16094                                 {
16095                                   if ((osec->flags & SEC_READONLY) == 0
16096                                       && htab->elf.data_index_section != NULL)
16097                                     osec = htab->elf.data_index_section;
16098                                   else
16099                                     osec = htab->elf.text_index_section;
16100                                   indx = elf_section_data (osec)->dynindx;
16101                                 }
16102                               BFD_ASSERT (indx != 0);
16103                             }
16104
16105                           /* We are turning this relocation into one
16106                              against a section symbol, so subtract out
16107                              the output section's address but not the
16108                              offset of the input section in the output
16109                              section.  */
16110                           outrel.r_addend -= osec->vma;
16111                         }
16112
16113                       outrel.r_info = ELF64_R_INFO (indx, r_type);
16114                     }
16115                 }
16116
16117               sreloc = elf_section_data (input_section)->sreloc;
16118               if (h != NULL
16119                   ? h->elf.type == STT_GNU_IFUNC
16120                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16121                 {
16122                   sreloc = htab->elf.irelplt;
16123                   if (indx == 0)
16124                     htab->local_ifunc_resolver = 1;
16125                   else if (is_static_defined (&h->elf))
16126                     htab->maybe_local_ifunc_resolver = 1;
16127                 }
16128               if (sreloc == NULL)
16129                 abort ();
16130
16131               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16132                   >= sreloc->size)
16133                 abort ();
16134               loc = sreloc->contents;
16135               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16136               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16137
16138               /* If this reloc is against an external symbol, it will
16139                  be computed at runtime, so there's no need to do
16140                  anything now.  However, for the sake of prelink ensure
16141                  that the section contents are a known value.  */
16142               if (!relocate)
16143                 {
16144                   unresolved_reloc = FALSE;
16145                   /* The value chosen here is quite arbitrary as ld.so
16146                      ignores section contents except for the special
16147                      case of .opd where the contents might be accessed
16148                      before relocation.  Choose zero, as that won't
16149                      cause reloc overflow.  */
16150                   relocation = 0;
16151                   addend = 0;
16152                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16153                      to improve backward compatibility with older
16154                      versions of ld.  */
16155                   if (r_type == R_PPC64_ADDR64)
16156                     addend = outrel.r_addend;
16157                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
16158                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
16159                     addend = outrel.r_offset;
16160                 }
16161             }
16162           break;
16163
16164         case R_PPC64_COPY:
16165         case R_PPC64_GLOB_DAT:
16166         case R_PPC64_JMP_SLOT:
16167         case R_PPC64_JMP_IREL:
16168         case R_PPC64_RELATIVE:
16169           /* We shouldn't ever see these dynamic relocs in relocatable
16170              files.  */
16171           /* Fall through.  */
16172
16173         case R_PPC64_PLTGOT16:
16174         case R_PPC64_PLTGOT16_DS:
16175         case R_PPC64_PLTGOT16_HA:
16176         case R_PPC64_PLTGOT16_HI:
16177         case R_PPC64_PLTGOT16_LO:
16178         case R_PPC64_PLTGOT16_LO_DS:
16179         case R_PPC64_PLTREL32:
16180         case R_PPC64_PLTREL64:
16181           /* These ones haven't been implemented yet.  */
16182
16183           info->callbacks->einfo
16184             /* xgettext:c-format */
16185             (_("%P: %pB: %s is not supported for `%pT'\n"),
16186              input_bfd,
16187              ppc64_elf_howto_table[r_type]->name, sym_name);
16188
16189           bfd_set_error (bfd_error_invalid_operation);
16190           ret = FALSE;
16191           goto copy_reloc;
16192         }
16193
16194       /* Multi-instruction sequences that access the TOC can be
16195          optimized, eg. addis ra,r2,0; addi rb,ra,x;
16196          to             nop;           addi rb,r2,x;  */
16197       switch (r_type)
16198         {
16199         default:
16200           break;
16201
16202         case R_PPC64_GOT_TLSLD16_HI:
16203         case R_PPC64_GOT_TLSGD16_HI:
16204         case R_PPC64_GOT_TPREL16_HI:
16205         case R_PPC64_GOT_DTPREL16_HI:
16206         case R_PPC64_GOT16_HI:
16207         case R_PPC64_TOC16_HI:
16208           /* These relocs would only be useful if building up an
16209              offset to later add to r2, perhaps in an indexed
16210              addressing mode instruction.  Don't try to optimize.
16211              Unfortunately, the possibility of someone building up an
16212              offset like this or even with the HA relocs, means that
16213              we need to check the high insn when optimizing the low
16214              insn.  */
16215           break;
16216
16217         case R_PPC64_PLTCALL_NOTOC:
16218           if (!unresolved_reloc)
16219             htab->notoc_plt = 1;
16220           /* Fall through.  */
16221         case R_PPC64_PLTCALL:
16222           if (unresolved_reloc)
16223             {
16224               /* No plt entry.  Make this into a direct call.  */
16225               bfd_byte *p = contents + rel->r_offset;
16226               insn = bfd_get_32 (input_bfd, p);
16227               insn &= 1;
16228               bfd_put_32 (input_bfd, B_DOT | insn, p);
16229               if (r_type == R_PPC64_PLTCALL)
16230                 bfd_put_32 (input_bfd, NOP, p + 4);
16231               unresolved_reloc = save_unresolved_reloc;
16232               r_type = R_PPC64_REL24;
16233             }
16234           break;
16235
16236         case R_PPC64_PLTSEQ_NOTOC:
16237         case R_PPC64_PLTSEQ:
16238           if (unresolved_reloc)
16239             {
16240               unresolved_reloc = FALSE;
16241               goto nop_it;
16242             }
16243           break;
16244
16245         case R_PPC64_PLT_PCREL34_NOTOC:
16246           if (!unresolved_reloc)
16247             htab->notoc_plt = 1;
16248           /* Fall through.  */
16249         case R_PPC64_PLT_PCREL34:
16250           if (unresolved_reloc)
16251             {
16252               bfd_byte *p = contents + rel->r_offset;
16253               bfd_put_32 (input_bfd, PNOP >> 32, p);
16254               bfd_put_32 (input_bfd, PNOP, p + 4);
16255               unresolved_reloc = FALSE;
16256               goto copy_reloc;
16257             }
16258           break;
16259
16260         case R_PPC64_PLT16_HA:
16261           if (unresolved_reloc)
16262             {
16263               unresolved_reloc = FALSE;
16264               goto nop_it;
16265             }
16266           /* Fall through.  */
16267         case R_PPC64_GOT_TLSLD16_HA:
16268         case R_PPC64_GOT_TLSGD16_HA:
16269         case R_PPC64_GOT_TPREL16_HA:
16270         case R_PPC64_GOT_DTPREL16_HA:
16271         case R_PPC64_GOT16_HA:
16272         case R_PPC64_TOC16_HA:
16273           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16274               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16275             {
16276               bfd_byte *p;
16277             nop_it:
16278               p = contents + (rel->r_offset & ~3);
16279               bfd_put_32 (input_bfd, NOP, p);
16280               goto copy_reloc;
16281             }
16282           break;
16283
16284         case R_PPC64_PLT16_LO:
16285         case R_PPC64_PLT16_LO_DS:
16286           if (unresolved_reloc)
16287             {
16288               unresolved_reloc = FALSE;
16289               goto nop_it;
16290             }
16291           /* Fall through.  */
16292         case R_PPC64_GOT_TLSLD16_LO:
16293         case R_PPC64_GOT_TLSGD16_LO:
16294         case R_PPC64_GOT_TPREL16_LO_DS:
16295         case R_PPC64_GOT_DTPREL16_LO_DS:
16296         case R_PPC64_GOT16_LO:
16297         case R_PPC64_GOT16_LO_DS:
16298         case R_PPC64_TOC16_LO:
16299         case R_PPC64_TOC16_LO_DS:
16300           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16301               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16302             {
16303               bfd_byte *p = contents + (rel->r_offset & ~3);
16304               insn = bfd_get_32 (input_bfd, p);
16305               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16306                 {
16307                   /* Transform addic to addi when we change reg.  */
16308                   insn &= ~((0x3f << 26) | (0x1f << 16));
16309                   insn |= (14u << 26) | (2 << 16);
16310                 }
16311               else
16312                 {
16313                   insn &= ~(0x1f << 16);
16314                   insn |= 2 << 16;
16315                 }
16316               bfd_put_32 (input_bfd, insn, p);
16317             }
16318           break;
16319
16320         case R_PPC64_TPREL16_HA:
16321           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16322             {
16323               bfd_byte *p = contents + (rel->r_offset & ~3);
16324               insn = bfd_get_32 (input_bfd, p);
16325               if ((insn & ((0x3f << 26) | 0x1f << 16))
16326                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16327                 /* xgettext:c-format */
16328                 info->callbacks->minfo
16329                   (_("%H: warning: %s unexpected insn %#x.\n"),
16330                    input_bfd, input_section, rel->r_offset,
16331                    ppc64_elf_howto_table[r_type]->name, insn);
16332               else
16333                 {
16334                   bfd_put_32 (input_bfd, NOP, p);
16335                   goto copy_reloc;
16336                 }
16337             }
16338           break;
16339
16340         case R_PPC64_TPREL16_LO:
16341         case R_PPC64_TPREL16_LO_DS:
16342           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16343             {
16344               bfd_byte *p = contents + (rel->r_offset & ~3);
16345               insn = bfd_get_32 (input_bfd, p);
16346               insn &= ~(0x1f << 16);
16347               insn |= 13 << 16;
16348               bfd_put_32 (input_bfd, insn, p);
16349             }
16350           break;
16351         }
16352
16353       /* Do any further special processing.  */
16354       switch (r_type)
16355         {
16356         default:
16357           break;
16358
16359         case R_PPC64_REL16_HA:
16360         case R_PPC64_REL16_HIGHA:
16361         case R_PPC64_REL16_HIGHERA:
16362         case R_PPC64_REL16_HIGHESTA:
16363         case R_PPC64_REL16DX_HA:
16364         case R_PPC64_ADDR16_HA:
16365         case R_PPC64_ADDR16_HIGHA:
16366         case R_PPC64_ADDR16_HIGHERA:
16367         case R_PPC64_ADDR16_HIGHESTA:
16368         case R_PPC64_TOC16_HA:
16369         case R_PPC64_SECTOFF_HA:
16370         case R_PPC64_TPREL16_HA:
16371         case R_PPC64_TPREL16_HIGHA:
16372         case R_PPC64_TPREL16_HIGHERA:
16373         case R_PPC64_TPREL16_HIGHESTA:
16374         case R_PPC64_DTPREL16_HA:
16375         case R_PPC64_DTPREL16_HIGHA:
16376         case R_PPC64_DTPREL16_HIGHERA:
16377         case R_PPC64_DTPREL16_HIGHESTA:
16378           /* It's just possible that this symbol is a weak symbol
16379              that's not actually defined anywhere. In that case,
16380              'sec' would be NULL, and we should leave the symbol
16381              alone (it will be set to zero elsewhere in the link).  */
16382           if (sec == NULL)
16383             break;
16384           /* Fall through.  */
16385
16386         case R_PPC64_GOT16_HA:
16387         case R_PPC64_PLTGOT16_HA:
16388         case R_PPC64_PLT16_HA:
16389         case R_PPC64_GOT_TLSGD16_HA:
16390         case R_PPC64_GOT_TLSLD16_HA:
16391         case R_PPC64_GOT_TPREL16_HA:
16392         case R_PPC64_GOT_DTPREL16_HA:
16393           /* Add 0x10000 if sign bit in 0:15 is set.
16394              Bits 0:15 are not used.  */
16395           addend += 0x8000;
16396           break;
16397
16398         case R_PPC64_D34_HA30:
16399         case R_PPC64_ADDR16_HIGHERA34:
16400         case R_PPC64_ADDR16_HIGHESTA34:
16401         case R_PPC64_REL16_HIGHERA34:
16402         case R_PPC64_REL16_HIGHESTA34:
16403           if (sec != NULL)
16404             addend += 1ULL << 33;
16405           break;
16406
16407         case R_PPC64_ADDR16_DS:
16408         case R_PPC64_ADDR16_LO_DS:
16409         case R_PPC64_GOT16_DS:
16410         case R_PPC64_GOT16_LO_DS:
16411         case R_PPC64_PLT16_LO_DS:
16412         case R_PPC64_SECTOFF_DS:
16413         case R_PPC64_SECTOFF_LO_DS:
16414         case R_PPC64_TOC16_DS:
16415         case R_PPC64_TOC16_LO_DS:
16416         case R_PPC64_PLTGOT16_DS:
16417         case R_PPC64_PLTGOT16_LO_DS:
16418         case R_PPC64_GOT_TPREL16_DS:
16419         case R_PPC64_GOT_TPREL16_LO_DS:
16420         case R_PPC64_GOT_DTPREL16_DS:
16421         case R_PPC64_GOT_DTPREL16_LO_DS:
16422         case R_PPC64_TPREL16_DS:
16423         case R_PPC64_TPREL16_LO_DS:
16424         case R_PPC64_DTPREL16_DS:
16425         case R_PPC64_DTPREL16_LO_DS:
16426           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16427           mask = 3;
16428           /* If this reloc is against an lq, lxv, or stxv insn, then
16429              the value must be a multiple of 16.  This is somewhat of
16430              a hack, but the "correct" way to do this by defining _DQ
16431              forms of all the _DS relocs bloats all reloc switches in
16432              this file.  It doesn't make much sense to use these
16433              relocs in data, so testing the insn should be safe.  */
16434           if ((insn & (0x3f << 26)) == (56u << 26)
16435               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16436             mask = 15;
16437           relocation += addend;
16438           addend = insn & (mask ^ 3);
16439           if ((relocation & mask) != 0)
16440             {
16441               relocation ^= relocation & mask;
16442               info->callbacks->einfo
16443                 /* xgettext:c-format */
16444                 (_("%H: error: %s not a multiple of %u\n"),
16445                  input_bfd, input_section, rel->r_offset,
16446                  ppc64_elf_howto_table[r_type]->name,
16447                  mask + 1);
16448               bfd_set_error (bfd_error_bad_value);
16449               ret = FALSE;
16450               goto copy_reloc;
16451             }
16452           break;
16453         }
16454
16455       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16456          because such sections are not SEC_ALLOC and thus ld.so will
16457          not process them.  */
16458       howto = ppc64_elf_howto_table[(int) r_type];
16459       if (unresolved_reloc
16460           && !((input_section->flags & SEC_DEBUGGING) != 0
16461                && h->elf.def_dynamic)
16462           && _bfd_elf_section_offset (output_bfd, info, input_section,
16463                                       rel->r_offset) != (bfd_vma) -1)
16464         {
16465           info->callbacks->einfo
16466             /* xgettext:c-format */
16467             (_("%H: unresolvable %s against `%pT'\n"),
16468              input_bfd, input_section, rel->r_offset,
16469              howto->name,
16470              h->elf.root.root.string);
16471           ret = FALSE;
16472         }
16473
16474       /* 16-bit fields in insns mostly have signed values, but a
16475          few insns have 16-bit unsigned values.  Really, we should
16476          have different reloc types.  */
16477       if (howto->complain_on_overflow != complain_overflow_dont
16478           && howto->dst_mask == 0xffff
16479           && (input_section->flags & SEC_CODE) != 0)
16480         {
16481           enum complain_overflow complain = complain_overflow_signed;
16482
16483           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16484           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16485             complain = complain_overflow_bitfield;
16486           else if (howto->rightshift == 0
16487                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16488                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16489                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16490                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16491                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16492                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16493             complain = complain_overflow_unsigned;
16494           if (howto->complain_on_overflow != complain)
16495             {
16496               alt_howto = *howto;
16497               alt_howto.complain_on_overflow = complain;
16498               howto = &alt_howto;
16499             }
16500         }
16501
16502       switch (r_type)
16503         {
16504           /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
16505         case R_PPC64_D34:
16506         case R_PPC64_D34_LO:
16507         case R_PPC64_D34_HI30:
16508         case R_PPC64_D34_HA30:
16509         case R_PPC64_PCREL34:
16510         case R_PPC64_GOT_PCREL34:
16511         case R_PPC64_TPREL34:
16512         case R_PPC64_DTPREL34:
16513         case R_PPC64_GOT_TLSGD34:
16514         case R_PPC64_GOT_TLSLD34:
16515         case R_PPC64_GOT_TPREL34:
16516         case R_PPC64_GOT_DTPREL34:
16517         case R_PPC64_PLT_PCREL34:
16518         case R_PPC64_PLT_PCREL34_NOTOC:
16519         case R_PPC64_D28:
16520         case R_PPC64_PCREL28:
16521           if (rel->r_offset + 8 > input_section->size)
16522             r = bfd_reloc_outofrange;
16523           else
16524             {
16525               relocation += addend;
16526               if (howto->pc_relative)
16527                 relocation -= (rel->r_offset
16528                                + input_section->output_offset
16529                                + input_section->output_section->vma);
16530               relocation >>= howto->rightshift;
16531
16532               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16533               pinsn <<= 32;
16534               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16535
16536               pinsn &= ~howto->dst_mask;
16537               pinsn |= (((relocation << 16) | (relocation & 0xffff))
16538                         & howto->dst_mask);
16539               bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
16540               bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
16541               r = bfd_reloc_ok;
16542               if (howto->complain_on_overflow == complain_overflow_signed
16543                   && (relocation + (1ULL << (howto->bitsize - 1))
16544                       >= 1ULL << howto->bitsize))
16545                 r = bfd_reloc_overflow;
16546             }
16547           break;
16548
16549         case R_PPC64_REL16DX_HA:
16550           if (rel->r_offset + 4 > input_section->size)
16551             r = bfd_reloc_outofrange;
16552           else
16553             {
16554               relocation += addend;
16555               relocation -= (rel->r_offset
16556                              + input_section->output_offset
16557                              + input_section->output_section->vma);
16558               relocation = (bfd_signed_vma) relocation >> 16;
16559               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16560               insn &= ~0x1fffc1;
16561               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16562               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16563               r = bfd_reloc_ok;
16564               if (relocation + 0x8000 > 0xffff)
16565                 r = bfd_reloc_overflow;
16566             }
16567           break;
16568
16569         default:
16570           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
16571                                         contents, rel->r_offset,
16572                                         relocation, addend);
16573         }
16574
16575       if (r != bfd_reloc_ok)
16576         {
16577           char *more_info = NULL;
16578           const char *reloc_name = howto->name;
16579
16580           if (reloc_dest != DEST_NORMAL)
16581             {
16582               more_info = bfd_malloc (strlen (reloc_name) + 8);
16583               if (more_info != NULL)
16584                 {
16585                   strcpy (more_info, reloc_name);
16586                   strcat (more_info, (reloc_dest == DEST_OPD
16587                                       ? " (OPD)" : " (stub)"));
16588                   reloc_name = more_info;
16589                 }
16590             }
16591
16592           if (r == bfd_reloc_overflow)
16593             {
16594               /* On code like "if (foo) foo();" don't report overflow
16595                  on a branch to zero when foo is undefined.  */
16596               if (!warned
16597                   && (reloc_dest == DEST_STUB
16598                       || !(h != NULL
16599                            && (h->elf.root.type == bfd_link_hash_undefweak
16600                                || h->elf.root.type == bfd_link_hash_undefined)
16601                            && is_branch_reloc (r_type))))
16602                 info->callbacks->reloc_overflow (info, &h->elf.root,
16603                                                  sym_name, reloc_name,
16604                                                  orig_rel.r_addend,
16605                                                  input_bfd, input_section,
16606                                                  rel->r_offset);
16607             }
16608           else
16609             {
16610               info->callbacks->einfo
16611                 /* xgettext:c-format */
16612                 (_("%H: %s against `%pT': error %d\n"),
16613                  input_bfd, input_section, rel->r_offset,
16614                  reloc_name, sym_name, (int) r);
16615               ret = FALSE;
16616             }
16617           if (more_info != NULL)
16618             free (more_info);
16619         }
16620     copy_reloc:
16621       if (wrel != rel)
16622         *wrel = *rel;
16623     }
16624
16625   if (wrel != rel)
16626     {
16627       Elf_Internal_Shdr *rel_hdr;
16628       size_t deleted = rel - wrel;
16629
16630       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16631       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16632       if (rel_hdr->sh_size == 0)
16633         {
16634           /* It is too late to remove an empty reloc section.  Leave
16635              one NONE reloc.
16636              ??? What is wrong with an empty section???  */
16637           rel_hdr->sh_size = rel_hdr->sh_entsize;
16638           deleted -= 1;
16639         }
16640       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16641       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16642       input_section->reloc_count -= deleted;
16643     }
16644
16645   /* If we're emitting relocations, then shortly after this function
16646      returns, reloc offsets and addends for this section will be
16647      adjusted.  Worse, reloc symbol indices will be for the output
16648      file rather than the input.  Save a copy of the relocs for
16649      opd_entry_value.  */
16650   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16651     {
16652       bfd_size_type amt;
16653       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16654       rel = bfd_alloc (input_bfd, amt);
16655       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16656       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16657       if (rel == NULL)
16658         return FALSE;
16659       memcpy (rel, relocs, amt);
16660     }
16661   return ret;
16662 }
16663
16664 /* Adjust the value of any local symbols in opd sections.  */
16665
16666 static int
16667 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16668                               const char *name ATTRIBUTE_UNUSED,
16669                               Elf_Internal_Sym *elfsym,
16670                               asection *input_sec,
16671                               struct elf_link_hash_entry *h)
16672 {
16673   struct _opd_sec_data *opd;
16674   long adjust;
16675   bfd_vma value;
16676
16677   if (h != NULL)
16678     return 1;
16679
16680   opd = get_opd_info (input_sec);
16681   if (opd == NULL || opd->adjust == NULL)
16682     return 1;
16683
16684   value = elfsym->st_value - input_sec->output_offset;
16685   if (!bfd_link_relocatable (info))
16686     value -= input_sec->output_section->vma;
16687
16688   adjust = opd->adjust[OPD_NDX (value)];
16689   if (adjust == -1)
16690     return 2;
16691
16692   elfsym->st_value += adjust;
16693   return 1;
16694 }
16695
16696 /* Finish up dynamic symbol handling.  We set the contents of various
16697    dynamic sections here.  */
16698
16699 static bfd_boolean
16700 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16701                                  struct bfd_link_info *info,
16702                                  struct elf_link_hash_entry *h,
16703                                  Elf_Internal_Sym *sym)
16704 {
16705   struct ppc_link_hash_table *htab;
16706   struct plt_entry *ent;
16707
16708   htab = ppc_hash_table (info);
16709   if (htab == NULL)
16710     return FALSE;
16711
16712   if (!htab->opd_abi && !h->def_regular)
16713     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16714       if (ent->plt.offset != (bfd_vma) -1)
16715         {
16716           /* Mark the symbol as undefined, rather than as
16717              defined in glink.  Leave the value if there were
16718              any relocations where pointer equality matters
16719              (this is a clue for the dynamic linker, to make
16720              function pointer comparisons work between an
16721              application and shared library), otherwise set it
16722              to zero.  */
16723           sym->st_shndx = SHN_UNDEF;
16724           if (!h->pointer_equality_needed)
16725             sym->st_value = 0;
16726           else if (!h->ref_regular_nonweak)
16727             {
16728               /* This breaks function pointer comparisons, but
16729                  that is better than breaking tests for a NULL
16730                  function pointer.  */
16731               sym->st_value = 0;
16732             }
16733           break;
16734         }
16735
16736   if (h->needs_copy)
16737     {
16738       /* This symbol needs a copy reloc.  Set it up.  */
16739       Elf_Internal_Rela rela;
16740       asection *srel;
16741       bfd_byte *loc;
16742
16743       if (h->dynindx == -1
16744           || (h->root.type != bfd_link_hash_defined
16745               && h->root.type != bfd_link_hash_defweak)
16746           || htab->elf.srelbss == NULL
16747           || htab->elf.sreldynrelro == NULL)
16748         abort ();
16749
16750       rela.r_offset = (h->root.u.def.value
16751                        + h->root.u.def.section->output_section->vma
16752                        + h->root.u.def.section->output_offset);
16753       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16754       rela.r_addend = 0;
16755       if (h->root.u.def.section == htab->elf.sdynrelro)
16756         srel = htab->elf.sreldynrelro;
16757       else
16758         srel = htab->elf.srelbss;
16759       loc = srel->contents;
16760       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16761       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16762     }
16763
16764   return TRUE;
16765 }
16766
16767 /* Used to decide how to sort relocs in an optimal manner for the
16768    dynamic linker, before writing them out.  */
16769
16770 static enum elf_reloc_type_class
16771 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16772                             const asection *rel_sec,
16773                             const Elf_Internal_Rela *rela)
16774 {
16775   enum elf_ppc64_reloc_type r_type;
16776   struct ppc_link_hash_table *htab = ppc_hash_table (info);
16777
16778   if (rel_sec == htab->elf.irelplt)
16779     return reloc_class_ifunc;
16780
16781   r_type = ELF64_R_TYPE (rela->r_info);
16782   switch (r_type)
16783     {
16784     case R_PPC64_RELATIVE:
16785       return reloc_class_relative;
16786     case R_PPC64_JMP_SLOT:
16787       return reloc_class_plt;
16788     case R_PPC64_COPY:
16789       return reloc_class_copy;
16790     default:
16791       return reloc_class_normal;
16792     }
16793 }
16794
16795 /* Finish up the dynamic sections.  */
16796
16797 static bfd_boolean
16798 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16799                                    struct bfd_link_info *info)
16800 {
16801   struct ppc_link_hash_table *htab;
16802   bfd *dynobj;
16803   asection *sdyn;
16804
16805   htab = ppc_hash_table (info);
16806   if (htab == NULL)
16807     return FALSE;
16808
16809   dynobj = htab->elf.dynobj;
16810   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16811
16812   if (htab->elf.dynamic_sections_created)
16813     {
16814       Elf64_External_Dyn *dyncon, *dynconend;
16815
16816       if (sdyn == NULL || htab->elf.sgot == NULL)
16817         abort ();
16818
16819       dyncon = (Elf64_External_Dyn *) sdyn->contents;
16820       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16821       for (; dyncon < dynconend; dyncon++)
16822         {
16823           Elf_Internal_Dyn dyn;
16824           asection *s;
16825
16826           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16827
16828           switch (dyn.d_tag)
16829             {
16830             default:
16831               continue;
16832
16833             case DT_PPC64_GLINK:
16834               s = htab->glink;
16835               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16836               /* We stupidly defined DT_PPC64_GLINK to be the start
16837                  of glink rather than the first entry point, which is
16838                  what ld.so needs, and now have a bigger stub to
16839                  support automatic multiple TOCs.  */
16840               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16841               break;
16842
16843             case DT_PPC64_OPD:
16844               s = bfd_get_section_by_name (output_bfd, ".opd");
16845               if (s == NULL)
16846                 continue;
16847               dyn.d_un.d_ptr = s->vma;
16848               break;
16849
16850             case DT_PPC64_OPT:
16851               if ((htab->do_multi_toc && htab->multi_toc_needed)
16852                   || htab->notoc_plt)
16853                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16854               if (htab->has_plt_localentry0)
16855                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16856               break;
16857
16858             case DT_PPC64_OPDSZ:
16859               s = bfd_get_section_by_name (output_bfd, ".opd");
16860               if (s == NULL)
16861                 continue;
16862               dyn.d_un.d_val = s->size;
16863               break;
16864
16865             case DT_PLTGOT:
16866               s = htab->elf.splt;
16867               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16868               break;
16869
16870             case DT_JMPREL:
16871               s = htab->elf.srelplt;
16872               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16873               break;
16874
16875             case DT_PLTRELSZ:
16876               dyn.d_un.d_val = htab->elf.srelplt->size;
16877               break;
16878
16879             case DT_TEXTREL:
16880               if (htab->local_ifunc_resolver)
16881                 info->callbacks->einfo
16882                   (_("%X%P: text relocations and GNU indirect "
16883                      "functions will result in a segfault at runtime\n"));
16884               else if (htab->maybe_local_ifunc_resolver)
16885                 info->callbacks->einfo
16886                   (_("%P: warning: text relocations and GNU indirect "
16887                      "functions may result in a segfault at runtime\n"));
16888               continue;
16889             }
16890
16891           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16892         }
16893     }
16894
16895   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16896       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16897     {
16898       /* Fill in the first entry in the global offset table.
16899          We use it to hold the link-time TOCbase.  */
16900       bfd_put_64 (output_bfd,
16901                   elf_gp (output_bfd) + TOC_BASE_OFF,
16902                   htab->elf.sgot->contents);
16903
16904       /* Set .got entry size.  */
16905       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16906         = 8;
16907     }
16908
16909   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16910       && htab->elf.splt->output_section != bfd_abs_section_ptr)
16911     {
16912       /* Set .plt entry size.  */
16913       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16914         = PLT_ENTRY_SIZE (htab);
16915     }
16916
16917   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16918      brlt ourselves if emitrelocations.  */
16919   if (htab->brlt != NULL
16920       && htab->brlt->reloc_count != 0
16921       && !_bfd_elf_link_output_relocs (output_bfd,
16922                                        htab->brlt,
16923                                        elf_section_data (htab->brlt)->rela.hdr,
16924                                        elf_section_data (htab->brlt)->relocs,
16925                                        NULL))
16926     return FALSE;
16927
16928   if (htab->glink != NULL
16929       && htab->glink->reloc_count != 0
16930       && !_bfd_elf_link_output_relocs (output_bfd,
16931                                        htab->glink,
16932                                        elf_section_data (htab->glink)->rela.hdr,
16933                                        elf_section_data (htab->glink)->relocs,
16934                                        NULL))
16935     return FALSE;
16936
16937
16938   if (htab->glink_eh_frame != NULL
16939       && htab->glink_eh_frame->size != 0
16940       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16941       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16942                                            htab->glink_eh_frame,
16943                                            htab->glink_eh_frame->contents))
16944     return FALSE;
16945
16946   /* We need to handle writing out multiple GOT sections ourselves,
16947      since we didn't add them to DYNOBJ.  We know dynobj is the first
16948      bfd.  */
16949   while ((dynobj = dynobj->link.next) != NULL)
16950     {
16951       asection *s;
16952
16953       if (!is_ppc64_elf (dynobj))
16954         continue;
16955
16956       s = ppc64_elf_tdata (dynobj)->got;
16957       if (s != NULL
16958           && s->size != 0
16959           && s->output_section != bfd_abs_section_ptr
16960           && !bfd_set_section_contents (output_bfd, s->output_section,
16961                                         s->contents, s->output_offset,
16962                                         s->size))
16963         return FALSE;
16964       s = ppc64_elf_tdata (dynobj)->relgot;
16965       if (s != NULL
16966           && s->size != 0
16967           && s->output_section != bfd_abs_section_ptr
16968           && !bfd_set_section_contents (output_bfd, s->output_section,
16969                                         s->contents, s->output_offset,
16970                                         s->size))
16971         return FALSE;
16972     }
16973
16974   return TRUE;
16975 }
16976
16977 #include "elf64-target.h"
16978
16979 /* FreeBSD support */
16980
16981 #undef  TARGET_LITTLE_SYM
16982 #undef  TARGET_LITTLE_NAME
16983
16984 #undef  TARGET_BIG_SYM
16985 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
16986 #undef  TARGET_BIG_NAME
16987 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16988
16989 #undef  ELF_OSABI
16990 #define ELF_OSABI       ELFOSABI_FREEBSD
16991
16992 #undef  elf64_bed
16993 #define elf64_bed       elf64_powerpc_fbsd_bed
16994
16995 #include "elf64-target.h"