]> Git Repo - binutils.git/blob - bfd/elf64-ppc.c
PowerPC64 GOT reloc optimisation
[binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2020 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <[email protected]>,
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 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
39 #define OCTETS_PER_BYTE(ABFD, SEC) 1
40
41 static bfd_reloc_status_type ppc64_elf_ha_reloc
42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_branch_reloc
44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_reloc
52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_toc64_reloc
56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_reloc_status_type ppc64_elf_prefix_reloc
58   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
59 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
60   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
61 static bfd_vma opd_entry_value
62   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
63
64 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
65 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
66 #define TARGET_BIG_SYM          powerpc_elf64_vec
67 #define TARGET_BIG_NAME         "elf64-powerpc"
68 #define ELF_ARCH                bfd_arch_powerpc
69 #define ELF_TARGET_ID           PPC64_ELF_DATA
70 #define ELF_MACHINE_CODE        EM_PPC64
71 #define ELF_MAXPAGESIZE         0x10000
72 #define ELF_COMMONPAGESIZE      0x1000
73 #define ELF_RELROPAGESIZE       ELF_MAXPAGESIZE
74 #define elf_info_to_howto       ppc64_elf_info_to_howto
75
76 #define elf_backend_want_got_sym 0
77 #define elf_backend_want_plt_sym 0
78 #define elf_backend_plt_alignment 3
79 #define elf_backend_plt_not_loaded 1
80 #define elf_backend_got_header_size 8
81 #define elf_backend_want_dynrelro 1
82 #define elf_backend_can_gc_sections 1
83 #define elf_backend_can_refcount 1
84 #define elf_backend_rela_normal 1
85 #define elf_backend_dtrel_excludes_plt 1
86 #define elf_backend_default_execstack 0
87
88 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
89 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
90 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
91 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
92 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
93 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
94 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
95 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
96 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
97 #define bfd_elf64_bfd_gc_sections             ppc64_elf_gc_sections
98
99 #define elf_backend_object_p                  ppc64_elf_object_p
100 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
101 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
102 #define elf_backend_write_core_note           ppc64_elf_write_core_note
103 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
104 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
105 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
106 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
107 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
108 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
109 #define elf_backend_check_relocs              ppc64_elf_check_relocs
110 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
111 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
112 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
113 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
114 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
115 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
116 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
117 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
118 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
119 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
120 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
121 #define elf_backend_action_discarded          ppc64_elf_action_discarded
122 #define elf_backend_relocate_section          ppc64_elf_relocate_section
123 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
124 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
125 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
126 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
127 #define elf_backend_special_sections          ppc64_elf_special_sections
128 #define elf_backend_section_flags             ppc64_elf_section_flags
129 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
130 #define elf_backend_merge_symbol              ppc64_elf_merge_symbol
131 #define elf_backend_get_reloc_section         bfd_get_section_by_name
132
133 /* The name of the dynamic interpreter.  This is put in the .interp
134    section.  */
135 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
136
137 /* The size in bytes of an entry in the procedure linkage table.  */
138 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
139 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
140
141 /* The initial size of the plt reserved for the dynamic linker.  */
142 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
143
144 /* Offsets to some stack save slots.  */
145 #define STK_LR 16
146 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
147 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
148    CR save slot.  Used only by optimised __tls_get_addr call stub,
149    relying on __tls_get_addr_opt not saving CR..  */
150 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
151
152 /* TOC base pointers offset from start of TOC.  */
153 #define TOC_BASE_OFF    0x8000
154 /* TOC base alignment.  */
155 #define TOC_BASE_ALIGN  256
156
157 /* Offset of tp and dtp pointers from start of TLS block.  */
158 #define TP_OFFSET       0x7000
159 #define DTP_OFFSET      0x8000
160
161 /* .plt call stub instructions.  The normal stub is like this, but
162    sometimes the .plt entry crosses a 64k boundary and we need to
163    insert an addi to adjust r11.  */
164 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
165 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
166 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
167 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
168 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
169 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
170 #define BCTR            0x4e800420      /* bctr                      */
171
172 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
173 #define ADDI_R12_R11    0x398b0000      /* addi %r12,%r11,off@l  */
174 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
175 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
176 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
177
178 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
179 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
180 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
181 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
182 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
183 #define BNECTR          0x4ca20420      /* bnectr+               */
184 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
185
186 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
187 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
188 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
189
190 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
191 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
192 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
193
194 #define LI_R11_0        0x39600000      /* li    %r11,0         */
195 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
196 #define LIS_R11         0x3d600000      /* lis %r11,xxx@ha        */
197 #define LIS_R12         0x3d800000      /* lis %r12,xxx@ha        */
198 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
199 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
200 #define ADDIS_R12_R11   0x3d8b0000      /* addis %r12,%r11,xxx@ha */
201 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
202 #define ORIS_R12_R12_0  0x658c0000      /* oris  %r12,%r12,xxx@hi */
203 #define ORI_R11_R11_0   0x616b0000      /* ori   %r11,%r11,xxx@l  */
204 #define ORI_R12_R12_0   0x618c0000      /* ori   %r12,%r12,xxx@l  */
205 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
206 #define SLDI_R11_R11_34 0x796b1746      /* sldi  %r11,%r11,34     */
207 #define SLDI_R12_R12_32 0x799c07c6      /* sldi  %r12,%r12,32     */
208 #define LDX_R12_R11_R12 0x7d8b602a      /* ldx   %r12,%r11,%r12   */
209 #define ADD_R12_R11_R12 0x7d8b6214      /* add   %r12,%r11,%r12   */
210 #define PADDI_R12_PC    0x0610000039800000ULL
211 #define PLD_R12_PC      0x04100000e5800000ULL
212 #define PNOP            0x0700000000000000ULL
213
214 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
215 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
216   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
217                                         /* 0:                           */
218                                         /*  .quad plt0-1f               */
219                                         /* __glink:                     */
220 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
221 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
222                                         /* 1:                           */
223 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
224                                         /*  ld %2,(0b-1b)(%11)          */
225 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
226 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
227                                         /*  ld %12,0(%11)               */
228                                         /*  ld %2,8(%11)                */
229                                         /*  mtctr %12                   */
230                                         /*  ld %11,16(%11)              */
231                                         /*  bctr                        */
232 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
233 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
234 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
235 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
236 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
237
238 /* Pad with this.  */
239 #define NOP             0x60000000
240
241 /* Some other nops.  */
242 #define CROR_151515     0x4def7b82
243 #define CROR_313131     0x4ffffb82
244
245 /* .glink entries for the first 32k functions are two instructions.  */
246 #define LI_R0_0         0x38000000      /* li    %r0,0          */
247 #define B_DOT           0x48000000      /* b     .              */
248
249 /* After that, we need two instructions to load the index, followed by
250    a branch.  */
251 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
252 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
253
254 /* Instructions used by the save and restore reg functions.  */
255 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
256 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
257 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
258 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
259 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
260 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
261 #define LI_R12_0        0x39800000      /* li    %r12,0         */
262 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
263 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
264 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
265 #define BLR             0x4e800020      /* blr                  */
266
267 /* Since .opd is an array of descriptors and each entry will end up
268    with identical R_PPC64_RELATIVE relocs, there is really no need to
269    propagate .opd relocs;  The dynamic linker should be taught to
270    relocate .opd without reloc entries.  */
271 #ifndef NO_OPD_RELOCS
272 #define NO_OPD_RELOCS 0
273 #endif
274
275 #ifndef ARRAY_SIZE
276 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
277 #endif
278
279 static inline int
280 abiversion (bfd *abfd)
281 {
282   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
283 }
284
285 static inline void
286 set_abiversion (bfd *abfd, int ver)
287 {
288   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
289   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
290 }
291 \f
292 /* Relocation HOWTO's.  */
293 /* Like other ELF RELA targets that don't apply multiple
294    field-altering relocations to the same localation, src_mask is
295    always zero and pcrel_offset is the same as pc_relative.
296    PowerPC can always use a zero bitpos, even when the field is not at
297    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
298    and bitpos=2 which matches the ABI description, or as we do here,
299    rightshift=0, bitsize=26 and bitpos=0.  */
300 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
301             complain, special_func)                             \
302   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,       \
303          complain_overflow_ ## complain, special_func,          \
304          #type, FALSE, 0, mask, pc_relative)
305
306 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
307
308 static reloc_howto_type ppc64_elf_howto_raw[] =
309 {
310   /* This reloc does nothing.  */
311   HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
312        bfd_elf_generic_reloc),
313
314   /* A standard 32 bit relocation.  */
315   HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
316        bfd_elf_generic_reloc),
317
318   /* An absolute 26 bit branch; the lower two bits must be zero.
319      FIXME: we don't check that, we just clear them.  */
320   HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
321        bfd_elf_generic_reloc),
322
323   /* A standard 16 bit relocation.  */
324   HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
325        bfd_elf_generic_reloc),
326
327   /* A 16 bit relocation without overflow.  */
328   HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
329        bfd_elf_generic_reloc),
330
331   /* Bits 16-31 of an address.  */
332   HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
333        bfd_elf_generic_reloc),
334
335   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
336      bits, treated as a signed number, is negative.  */
337   HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
338        ppc64_elf_ha_reloc),
339
340   /* An absolute 16 bit branch; the lower two bits must be zero.
341      FIXME: we don't check that, we just clear them.  */
342   HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
343        ppc64_elf_branch_reloc),
344
345   /* An absolute 16 bit branch, for which bit 10 should be set to
346      indicate that the branch is expected to be taken.  The lower two
347      bits must be zero.  */
348   HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
349        ppc64_elf_brtaken_reloc),
350
351   /* An absolute 16 bit branch, for which bit 10 should be set to
352      indicate that the branch is not expected to be taken.  The lower
353      two bits must be zero.  */
354   HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
355        ppc64_elf_brtaken_reloc),
356
357   /* A relative 26 bit branch; the lower two bits must be zero.  */
358   HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
359        ppc64_elf_branch_reloc),
360
361   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
362   HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
363        ppc64_elf_branch_reloc),
364
365   /* A relative 16 bit branch; the lower two bits must be zero.  */
366   HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
367        ppc64_elf_branch_reloc),
368
369   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
370      the branch is expected to be taken.  The lower two bits must be
371      zero.  */
372   HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
373        ppc64_elf_brtaken_reloc),
374
375   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
376      the branch is not expected to be taken.  The lower two bits must
377      be zero.  */
378   HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
379        ppc64_elf_brtaken_reloc),
380
381   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
382      symbol.  */
383   HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
384        ppc64_elf_unhandled_reloc),
385
386   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
387      the symbol.  */
388   HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
389        ppc64_elf_unhandled_reloc),
390
391   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
392      the symbol.  */
393   HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
394        ppc64_elf_unhandled_reloc),
395
396   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
397      the symbol.  */
398   HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
399        ppc64_elf_unhandled_reloc),
400
401   /* This is used only by the dynamic linker.  The symbol should exist
402      both in the object being run and in some shared library.  The
403      dynamic linker copies the data addressed by the symbol from the
404      shared library into the object, because the object being
405      run has to have the data at some particular address.  */
406   HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
407        ppc64_elf_unhandled_reloc),
408
409   /* Like R_PPC64_ADDR64, but used when setting global offset table
410      entries.  */
411   HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
412        ppc64_elf_unhandled_reloc),
413
414   /* Created by the link editor.  Marks a procedure linkage table
415      entry for a symbol.  */
416   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
417        ppc64_elf_unhandled_reloc),
418
419   /* Used only by the dynamic linker.  When the object is run, this
420      doubleword64 is set to the load address of the object, plus the
421      addend.  */
422   HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
423        bfd_elf_generic_reloc),
424
425   /* Like R_PPC64_ADDR32, but may be unaligned.  */
426   HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
427        bfd_elf_generic_reloc),
428
429   /* Like R_PPC64_ADDR16, but may be unaligned.  */
430   HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
431        bfd_elf_generic_reloc),
432
433   /* 32-bit PC relative.  */
434   HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
435        bfd_elf_generic_reloc),
436
437   /* 32-bit relocation to the symbol's procedure linkage table.  */
438   HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
439        ppc64_elf_unhandled_reloc),
440
441   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
442      FIXME: R_PPC64_PLTREL32 not supported.  */
443   HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
444        ppc64_elf_unhandled_reloc),
445
446   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
447      the symbol.  */
448   HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
449        ppc64_elf_unhandled_reloc),
450
451   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
452      the symbol.  */
453   HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
454        ppc64_elf_unhandled_reloc),
455
456   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
457      the symbol.  */
458   HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
459        ppc64_elf_unhandled_reloc),
460
461   /* 16-bit section relative relocation.  */
462   HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
463        ppc64_elf_sectoff_reloc),
464
465   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
466   HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
467        ppc64_elf_sectoff_reloc),
468
469   /* 16-bit upper half section relative relocation.  */
470   HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
471        ppc64_elf_sectoff_reloc),
472
473   /* 16-bit upper half adjusted section relative relocation.  */
474   HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
475        ppc64_elf_sectoff_ha_reloc),
476
477   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
478   HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
479        bfd_elf_generic_reloc),
480
481   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
482
483   /* A standard 64-bit relocation.  */
484   HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
485        bfd_elf_generic_reloc),
486
487   /* The bits 32-47 of an address.  */
488   HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
489        bfd_elf_generic_reloc),
490
491   /* The bits 32-47 of an address, plus 1 if the contents of the low
492      16 bits, treated as a signed number, is negative.  */
493   HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
494        ppc64_elf_ha_reloc),
495
496   /* The bits 48-63 of an address.  */
497   HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
498        bfd_elf_generic_reloc),
499
500   /* The bits 48-63 of an address, plus 1 if the contents of the low
501      16 bits, treated as a signed number, is negative.  */
502   HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
503        ppc64_elf_ha_reloc),
504
505   /* Like ADDR64, but may be unaligned.  */
506   HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
507        bfd_elf_generic_reloc),
508
509   /* 64-bit relative relocation.  */
510   HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
511        bfd_elf_generic_reloc),
512
513   /* 64-bit relocation to the symbol's procedure linkage table.  */
514   HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
515        ppc64_elf_unhandled_reloc),
516
517   /* 64-bit PC relative relocation to the symbol's procedure linkage
518      table.  */
519   /* FIXME: R_PPC64_PLTREL64 not supported.  */
520   HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
521        ppc64_elf_unhandled_reloc),
522
523   /* 16 bit TOC-relative relocation.  */
524   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
525   HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
526        ppc64_elf_toc_reloc),
527
528   /* 16 bit TOC-relative relocation without overflow.  */
529   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
530   HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
531        ppc64_elf_toc_reloc),
532
533   /* 16 bit TOC-relative relocation, high 16 bits.  */
534   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
535   HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
536        ppc64_elf_toc_reloc),
537
538   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
539      contents of the low 16 bits, treated as a signed number, is
540      negative.  */
541   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
542   HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
543        ppc64_elf_toc_ha_reloc),
544
545   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
546   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
547   HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
548        ppc64_elf_toc64_reloc),
549
550   /* Like R_PPC64_GOT16, but also informs the link editor that the
551      value to relocate may (!) refer to a PLT entry which the link
552      editor (a) may replace with the symbol value.  If the link editor
553      is unable to fully resolve the symbol, it may (b) create a PLT
554      entry and store the address to the new PLT entry in the GOT.
555      This permits lazy resolution of function symbols at run time.
556      The link editor may also skip all of this and just (c) emit a
557      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
558   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
559     HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
560           ppc64_elf_unhandled_reloc),
561
562   /* Like R_PPC64_PLTGOT16, but without overflow.  */
563   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
564   HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
565        ppc64_elf_unhandled_reloc),
566
567   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
568   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
569   HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
570        ppc64_elf_unhandled_reloc),
571
572   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
573      1 if the contents of the low 16 bits, treated as a signed number,
574      is negative.  */
575   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
576   HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
577        ppc64_elf_unhandled_reloc),
578
579   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
580   HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
581        bfd_elf_generic_reloc),
582
583   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
584   HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
585        bfd_elf_generic_reloc),
586
587   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
588   HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
589        ppc64_elf_unhandled_reloc),
590
591   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
592   HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
593        ppc64_elf_unhandled_reloc),
594
595   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
596   HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
597        ppc64_elf_unhandled_reloc),
598
599   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
600   HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
601        ppc64_elf_sectoff_reloc),
602
603   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
604   HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
605        ppc64_elf_sectoff_reloc),
606
607   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
608   HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
609        ppc64_elf_toc_reloc),
610
611   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
612   HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
613        ppc64_elf_toc_reloc),
614
615   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
616   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
617   HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
618        ppc64_elf_unhandled_reloc),
619
620   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
621   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
622   HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
623        ppc64_elf_unhandled_reloc),
624
625   /* Marker relocs for TLS.  */
626   HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
627        bfd_elf_generic_reloc),
628
629   HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
630        bfd_elf_generic_reloc),
631
632   HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
633        bfd_elf_generic_reloc),
634
635   /* Marker reloc for optimizing r2 save in prologue rather than on
636      each plt call stub.  */
637   HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
638        bfd_elf_generic_reloc),
639
640   /* Marker relocs on inline plt call instructions.  */
641   HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
642        bfd_elf_generic_reloc),
643
644   HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
645        bfd_elf_generic_reloc),
646
647   /* Computes the load module index of the load module that contains the
648      definition of its TLS sym.  */
649   HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
650        ppc64_elf_unhandled_reloc),
651
652   /* Computes a dtv-relative displacement, the difference between the value
653      of sym+add and the base address of the thread-local storage block that
654      contains the definition of sym, minus 0x8000.  */
655   HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
656        ppc64_elf_unhandled_reloc),
657
658   /* A 16 bit dtprel reloc.  */
659   HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
660        ppc64_elf_unhandled_reloc),
661
662   /* Like DTPREL16, but no overflow.  */
663   HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
664        ppc64_elf_unhandled_reloc),
665
666   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
667   HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
668        ppc64_elf_unhandled_reloc),
669
670   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
671   HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
672        ppc64_elf_unhandled_reloc),
673
674   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
675   HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
676        ppc64_elf_unhandled_reloc),
677
678   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
679   HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
680        ppc64_elf_unhandled_reloc),
681
682   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
683   HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
684        ppc64_elf_unhandled_reloc),
685
686   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
687   HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
688        ppc64_elf_unhandled_reloc),
689
690   /* Like DTPREL16, but for insns with a DS field.  */
691   HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
692        ppc64_elf_unhandled_reloc),
693
694   /* Like DTPREL16_DS, but no overflow.  */
695   HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
696        ppc64_elf_unhandled_reloc),
697
698   /* Computes a tp-relative displacement, the difference between the value of
699      sym+add and the value of the thread pointer (r13).  */
700   HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
701        ppc64_elf_unhandled_reloc),
702
703   /* A 16 bit tprel reloc.  */
704   HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
705        ppc64_elf_unhandled_reloc),
706
707   /* Like TPREL16, but no overflow.  */
708   HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
709        ppc64_elf_unhandled_reloc),
710
711   /* Like TPREL16_LO, but next higher group of 16 bits.  */
712   HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
713        ppc64_elf_unhandled_reloc),
714
715   /* Like TPREL16_HI, but adjust for low 16 bits.  */
716   HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
717        ppc64_elf_unhandled_reloc),
718
719   /* Like TPREL16_HI, but next higher group of 16 bits.  */
720   HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
721        ppc64_elf_unhandled_reloc),
722
723   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
724   HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
725        ppc64_elf_unhandled_reloc),
726
727   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
728   HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
729        ppc64_elf_unhandled_reloc),
730
731   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
732   HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
733        ppc64_elf_unhandled_reloc),
734
735   /* Like TPREL16, but for insns with a DS field.  */
736   HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
737        ppc64_elf_unhandled_reloc),
738
739   /* Like TPREL16_DS, but no overflow.  */
740   HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
741        ppc64_elf_unhandled_reloc),
742
743   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
744      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
745      to the first entry relative to the TOC base (r2).  */
746   HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
747        ppc64_elf_unhandled_reloc),
748
749   /* Like GOT_TLSGD16, but no overflow.  */
750   HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
751        ppc64_elf_unhandled_reloc),
752
753   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
754   HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
755        ppc64_elf_unhandled_reloc),
756
757   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
758   HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
759        ppc64_elf_unhandled_reloc),
760
761   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
762      with values (sym+add)@dtpmod and zero, and computes the offset to the
763      first entry relative to the TOC base (r2).  */
764   HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
765        ppc64_elf_unhandled_reloc),
766
767   /* Like GOT_TLSLD16, but no overflow.  */
768   HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
769        ppc64_elf_unhandled_reloc),
770
771   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
772   HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
773        ppc64_elf_unhandled_reloc),
774
775   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
776   HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
777        ppc64_elf_unhandled_reloc),
778
779   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
780      the offset to the entry relative to the TOC base (r2).  */
781   HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
782        ppc64_elf_unhandled_reloc),
783
784   /* Like GOT_DTPREL16_DS, but no overflow.  */
785   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
786        ppc64_elf_unhandled_reloc),
787
788   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
789   HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
790        ppc64_elf_unhandled_reloc),
791
792   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
793   HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
794        ppc64_elf_unhandled_reloc),
795
796   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
797      offset to the entry relative to the TOC base (r2).  */
798   HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
799        ppc64_elf_unhandled_reloc),
800
801   /* Like GOT_TPREL16_DS, but no overflow.  */
802   HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
803        ppc64_elf_unhandled_reloc),
804
805   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
806   HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
807        ppc64_elf_unhandled_reloc),
808
809   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
810   HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
811        ppc64_elf_unhandled_reloc),
812
813   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
814        ppc64_elf_unhandled_reloc),
815
816   HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
817        bfd_elf_generic_reloc),
818
819   /* A 16 bit relative relocation.  */
820   HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
821        bfd_elf_generic_reloc),
822
823   /* A 16 bit relative relocation without overflow.  */
824   HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
825        bfd_elf_generic_reloc),
826
827   /* The high order 16 bits of a relative address.  */
828   HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
829        bfd_elf_generic_reloc),
830
831   /* The high order 16 bits of a relative address, plus 1 if the contents of
832      the low 16 bits, treated as a signed number, is negative.  */
833   HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
834        ppc64_elf_ha_reloc),
835
836   HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
837        bfd_elf_generic_reloc),
838
839   HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
840        ppc64_elf_ha_reloc),
841
842   HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
843        bfd_elf_generic_reloc),
844
845   HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
846        ppc64_elf_ha_reloc),
847
848   HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
849        bfd_elf_generic_reloc),
850
851   HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
852        ppc64_elf_ha_reloc),
853
854   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
855   HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
856        ppc64_elf_ha_reloc),
857
858   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
859   HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
860        ppc64_elf_ha_reloc),
861
862   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
863   HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
864        bfd_elf_generic_reloc),
865
866   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
867   HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
868        ppc64_elf_ha_reloc),
869
870   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
871   HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
872        ppc64_elf_unhandled_reloc),
873
874   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
875   HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
876        ppc64_elf_unhandled_reloc),
877
878   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
879   HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
880        ppc64_elf_unhandled_reloc),
881
882   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
883   HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
884        ppc64_elf_unhandled_reloc),
885
886   /* Marker reloc on ELFv2 large-model function entry.  */
887   HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
888        bfd_elf_generic_reloc),
889
890   /* Like ADDR64, but use local entry point of function.  */
891   HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
892        bfd_elf_generic_reloc),
893
894   HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
895        bfd_elf_generic_reloc),
896
897   HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
898        bfd_elf_generic_reloc),
899
900   HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
901        bfd_elf_generic_reloc),
902
903   HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
904        ppc64_elf_prefix_reloc),
905
906   HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
907        ppc64_elf_prefix_reloc),
908
909   HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
910        ppc64_elf_prefix_reloc),
911
912   HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
913        ppc64_elf_prefix_reloc),
914
915   HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
916        ppc64_elf_prefix_reloc),
917
918   HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
919        ppc64_elf_unhandled_reloc),
920
921   HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
922        ppc64_elf_unhandled_reloc),
923
924   HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
925        ppc64_elf_unhandled_reloc),
926
927   HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
928        ppc64_elf_unhandled_reloc),
929
930   HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
931        ppc64_elf_unhandled_reloc),
932
933   HOW (R_PPC64_GOT_TLSGD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
934        ppc64_elf_unhandled_reloc),
935
936   HOW (R_PPC64_GOT_TLSLD34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
937        ppc64_elf_unhandled_reloc),
938
939   HOW (R_PPC64_GOT_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
940        ppc64_elf_unhandled_reloc),
941
942   HOW (R_PPC64_GOT_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
943        ppc64_elf_unhandled_reloc),
944
945   HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
946        bfd_elf_generic_reloc),
947
948   HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
949        ppc64_elf_ha_reloc),
950
951   HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
952        bfd_elf_generic_reloc),
953
954   HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
955        ppc64_elf_ha_reloc),
956
957   HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
958        bfd_elf_generic_reloc),
959
960   HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
961        ppc64_elf_ha_reloc),
962
963   HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
964        bfd_elf_generic_reloc),
965
966   HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
967        ppc64_elf_ha_reloc),
968
969   HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
970        ppc64_elf_prefix_reloc),
971
972   HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
973        ppc64_elf_prefix_reloc),
974
975   /* GNU extension to record C++ vtable hierarchy.  */
976   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
977        NULL),
978
979   /* GNU extension to record C++ vtable member usage.  */
980   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
981        NULL),
982 };
983
984 \f
985 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
986    be done.  */
987
988 static void
989 ppc_howto_init (void)
990 {
991   unsigned int i, type;
992
993   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
994     {
995       type = ppc64_elf_howto_raw[i].type;
996       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
997       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
998     }
999 }
1000
1001 static reloc_howto_type *
1002 ppc64_elf_reloc_type_lookup (bfd *abfd,
1003                              bfd_reloc_code_real_type code)
1004 {
1005   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1006
1007   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1008     /* Initialize howto table if needed.  */
1009     ppc_howto_init ();
1010
1011   switch (code)
1012     {
1013     default:
1014       /* xgettext:c-format */
1015       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1016                           (int) code);
1017       bfd_set_error (bfd_error_bad_value);
1018       return NULL;
1019
1020     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
1021       break;
1022     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
1023       break;
1024     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
1025       break;
1026     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
1027       break;
1028     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
1029       break;
1030     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
1031       break;
1032     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
1033       break;
1034     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
1035       break;
1036     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
1037       break;
1038     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
1039       break;
1040     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
1041       break;
1042     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
1043       break;
1044     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
1045       break;
1046     case BFD_RELOC_PPC64_REL24_NOTOC:           r = R_PPC64_REL24_NOTOC;
1047       break;
1048     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
1049       break;
1050     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
1051       break;
1052     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
1053       break;
1054     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
1055       break;
1056     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
1057       break;
1058     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
1059       break;
1060     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
1061       break;
1062     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
1063       break;
1064     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
1065       break;
1066     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
1067       break;
1068     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
1069       break;
1070     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
1071       break;
1072     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
1073       break;
1074     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
1075       break;
1076     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
1077       break;
1078     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
1079       break;
1080     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
1081       break;
1082     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
1083       break;
1084     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
1085       break;
1086     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
1087       break;
1088     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
1089       break;
1090     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
1091       break;
1092     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
1093       break;
1094     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
1095       break;
1096     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
1097       break;
1098     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
1099       break;
1100     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
1101       break;
1102     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
1103       break;
1104     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
1105       break;
1106     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
1107       break;
1108     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
1109       break;
1110     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
1111       break;
1112     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1113       break;
1114     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1115       break;
1116     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1117       break;
1118     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1119       break;
1120     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1121       break;
1122     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1123       break;
1124     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1125       break;
1126     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1127       break;
1128     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1129       break;
1130     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1131       break;
1132     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1133       break;
1134     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
1135       break;
1136     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
1137       break;
1138     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
1139       break;
1140     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
1141       break;
1142     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
1143       break;
1144     case BFD_RELOC_PPC64_TLS_PCREL:
1145     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
1146       break;
1147     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
1148       break;
1149     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
1150       break;
1151     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
1152       break;
1153     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
1154       break;
1155     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
1156       break;
1157     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
1158       break;
1159     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
1160       break;
1161     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
1162       break;
1163     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
1164       break;
1165     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
1166       break;
1167     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
1168       break;
1169     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
1170       break;
1171     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
1172       break;
1173     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
1174       break;
1175     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
1176       break;
1177     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
1178       break;
1179     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
1180       break;
1181     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
1182       break;
1183     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
1184       break;
1185     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
1186       break;
1187     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
1188       break;
1189     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
1190       break;
1191     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
1192       break;
1193     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
1194       break;
1195     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
1196       break;
1197     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
1198       break;
1199     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
1200       break;
1201     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
1202       break;
1203     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
1204       break;
1205     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
1206       break;
1207     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
1208       break;
1209     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
1210       break;
1211     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
1212       break;
1213     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
1214       break;
1215     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
1216       break;
1217     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
1218       break;
1219     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
1220       break;
1221     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
1222       break;
1223     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
1224       break;
1225     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
1226       break;
1227     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
1228       break;
1229     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
1230       break;
1231     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
1232       break;
1233     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
1234       break;
1235     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
1236       break;
1237     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
1238       break;
1239     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
1240       break;
1241     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
1242       break;
1243     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
1244       break;
1245     case BFD_RELOC_PPC64_REL16_HIGH:            r = R_PPC64_REL16_HIGH;
1246       break;
1247     case BFD_RELOC_PPC64_REL16_HIGHA:           r = R_PPC64_REL16_HIGHA;
1248       break;
1249     case BFD_RELOC_PPC64_REL16_HIGHER:          r = R_PPC64_REL16_HIGHER;
1250       break;
1251     case BFD_RELOC_PPC64_REL16_HIGHERA:         r = R_PPC64_REL16_HIGHERA;
1252       break;
1253     case BFD_RELOC_PPC64_REL16_HIGHEST:         r = R_PPC64_REL16_HIGHEST;
1254       break;
1255     case BFD_RELOC_PPC64_REL16_HIGHESTA:        r = R_PPC64_REL16_HIGHESTA;
1256       break;
1257     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
1258       break;
1259     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
1260       break;
1261     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
1262       break;
1263     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
1264       break;
1265     case BFD_RELOC_PPC64_D34:                   r = R_PPC64_D34;
1266       break;
1267     case BFD_RELOC_PPC64_D34_LO:                r = R_PPC64_D34_LO;
1268       break;
1269     case BFD_RELOC_PPC64_D34_HI30:              r = R_PPC64_D34_HI30;
1270       break;
1271     case BFD_RELOC_PPC64_D34_HA30:              r = R_PPC64_D34_HA30;
1272       break;
1273     case BFD_RELOC_PPC64_PCREL34:               r = R_PPC64_PCREL34;
1274       break;
1275     case BFD_RELOC_PPC64_GOT_PCREL34:           r = R_PPC64_GOT_PCREL34;
1276       break;
1277     case BFD_RELOC_PPC64_PLT_PCREL34:           r = R_PPC64_PLT_PCREL34;
1278       break;
1279     case BFD_RELOC_PPC64_TPREL34:               r = R_PPC64_TPREL34;
1280       break;
1281     case BFD_RELOC_PPC64_DTPREL34:              r = R_PPC64_DTPREL34;
1282       break;
1283     case BFD_RELOC_PPC64_GOT_TLSGD34:           r = R_PPC64_GOT_TLSGD34;
1284       break;
1285     case BFD_RELOC_PPC64_GOT_TLSLD34:           r = R_PPC64_GOT_TLSLD34;
1286       break;
1287     case BFD_RELOC_PPC64_GOT_TPREL34:           r = R_PPC64_GOT_TPREL34;
1288       break;
1289     case BFD_RELOC_PPC64_GOT_DTPREL34:          r = R_PPC64_GOT_DTPREL34;
1290       break;
1291     case BFD_RELOC_PPC64_ADDR16_HIGHER34:       r = R_PPC64_ADDR16_HIGHER34;
1292       break;
1293     case BFD_RELOC_PPC64_ADDR16_HIGHERA34:      r = R_PPC64_ADDR16_HIGHERA34;
1294       break;
1295     case BFD_RELOC_PPC64_ADDR16_HIGHEST34:      r = R_PPC64_ADDR16_HIGHEST34;
1296       break;
1297     case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:     r = R_PPC64_ADDR16_HIGHESTA34;
1298       break;
1299     case BFD_RELOC_PPC64_REL16_HIGHER34:        r = R_PPC64_REL16_HIGHER34;
1300       break;
1301     case BFD_RELOC_PPC64_REL16_HIGHERA34:       r = R_PPC64_REL16_HIGHERA34;
1302       break;
1303     case BFD_RELOC_PPC64_REL16_HIGHEST34:       r = R_PPC64_REL16_HIGHEST34;
1304       break;
1305     case BFD_RELOC_PPC64_REL16_HIGHESTA34:      r = R_PPC64_REL16_HIGHESTA34;
1306       break;
1307     case BFD_RELOC_PPC64_D28:                   r = R_PPC64_D28;
1308       break;
1309     case BFD_RELOC_PPC64_PCREL28:               r = R_PPC64_PCREL28;
1310       break;
1311     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
1312       break;
1313     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
1314       break;
1315     }
1316
1317   return ppc64_elf_howto_table[r];
1318 };
1319
1320 static reloc_howto_type *
1321 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1322                              const char *r_name)
1323 {
1324   unsigned int i;
1325
1326   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1327     if (ppc64_elf_howto_raw[i].name != NULL
1328         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1329       return &ppc64_elf_howto_raw[i];
1330
1331   return NULL;
1332 }
1333
1334 /* Set the howto pointer for a PowerPC ELF reloc.  */
1335
1336 static bfd_boolean
1337 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1338                          Elf_Internal_Rela *dst)
1339 {
1340   unsigned int type;
1341
1342   /* Initialize howto table if needed.  */
1343   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1344     ppc_howto_init ();
1345
1346   type = ELF64_R_TYPE (dst->r_info);
1347   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1348     {
1349       /* xgettext:c-format */
1350       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1351                           abfd, type);
1352       bfd_set_error (bfd_error_bad_value);
1353       return FALSE;
1354     }
1355   cache_ptr->howto = ppc64_elf_howto_table[type];
1356   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1357     {
1358       /* xgettext:c-format */
1359       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1360                           abfd, type);
1361       bfd_set_error (bfd_error_bad_value);
1362       return FALSE;
1363     }
1364
1365   return TRUE;
1366 }
1367
1368 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1369
1370 static bfd_reloc_status_type
1371 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1372                     void *data, asection *input_section,
1373                     bfd *output_bfd, char **error_message)
1374 {
1375   enum elf_ppc64_reloc_type r_type;
1376   long insn;
1377   bfd_size_type octets;
1378   bfd_vma value;
1379
1380   /* If this is a relocatable link (output_bfd test tells us), just
1381      call the generic function.  Any adjustment will be done at final
1382      link time.  */
1383   if (output_bfd != NULL)
1384     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1385                                   input_section, output_bfd, error_message);
1386
1387   /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1388      We won't actually be using the low bits, so trashing them
1389      doesn't matter.  */
1390   r_type = reloc_entry->howto->type;
1391   if (r_type == R_PPC64_ADDR16_HIGHERA34
1392       || r_type == R_PPC64_ADDR16_HIGHESTA34
1393       || r_type == R_PPC64_REL16_HIGHERA34
1394       || r_type == R_PPC64_REL16_HIGHESTA34)
1395     reloc_entry->addend += 1ULL << 33;
1396   else
1397     reloc_entry->addend += 1U << 15;
1398   if (r_type != R_PPC64_REL16DX_HA)
1399     return bfd_reloc_continue;
1400
1401   value = 0;
1402   if (!bfd_is_com_section (symbol->section))
1403     value = symbol->value;
1404   value += (reloc_entry->addend
1405             + symbol->section->output_offset
1406             + symbol->section->output_section->vma);
1407   value -= (reloc_entry->address
1408             + input_section->output_offset
1409             + input_section->output_section->vma);
1410   value = (bfd_signed_vma) value >> 16;
1411
1412   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1413   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1414   insn &= ~0x1fffc1;
1415   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1416   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1417   if (value + 0x8000 > 0xffff)
1418     return bfd_reloc_overflow;
1419   return bfd_reloc_ok;
1420 }
1421
1422 static bfd_reloc_status_type
1423 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1424                         void *data, asection *input_section,
1425                         bfd *output_bfd, char **error_message)
1426 {
1427   if (output_bfd != NULL)
1428     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1429                                   input_section, output_bfd, error_message);
1430
1431   if (strcmp (symbol->section->name, ".opd") == 0
1432       && (symbol->section->owner->flags & DYNAMIC) == 0)
1433     {
1434       bfd_vma dest = opd_entry_value (symbol->section,
1435                                       symbol->value + reloc_entry->addend,
1436                                       NULL, NULL, FALSE);
1437       if (dest != (bfd_vma) -1)
1438         reloc_entry->addend = dest - (symbol->value
1439                                       + symbol->section->output_section->vma
1440                                       + symbol->section->output_offset);
1441     }
1442   else
1443     {
1444       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1445
1446       if (symbol->section->owner != abfd
1447           && symbol->section->owner != NULL
1448           && abiversion (symbol->section->owner) >= 2)
1449         {
1450           unsigned int i;
1451
1452           for (i = 0; i < symbol->section->owner->symcount; ++i)
1453             {
1454               asymbol *symdef = symbol->section->owner->outsymbols[i];
1455
1456               if (strcmp (symdef->name, symbol->name) == 0)
1457                 {
1458                   elfsym = (elf_symbol_type *) symdef;
1459                   break;
1460                 }
1461             }
1462         }
1463       reloc_entry->addend
1464         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1465     }
1466   return bfd_reloc_continue;
1467 }
1468
1469 static bfd_reloc_status_type
1470 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1471                          void *data, asection *input_section,
1472                          bfd *output_bfd, char **error_message)
1473 {
1474   long insn;
1475   enum elf_ppc64_reloc_type r_type;
1476   bfd_size_type octets;
1477   /* Assume 'at' branch hints.  */
1478   bfd_boolean is_isa_v2 = TRUE;
1479
1480   /* If this is a relocatable link (output_bfd test tells us), just
1481      call the generic function.  Any adjustment will be done at final
1482      link time.  */
1483   if (output_bfd != NULL)
1484     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1485                                   input_section, output_bfd, error_message);
1486
1487   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1488   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1489   insn &= ~(0x01 << 21);
1490   r_type = reloc_entry->howto->type;
1491   if (r_type == R_PPC64_ADDR14_BRTAKEN
1492       || r_type == R_PPC64_REL14_BRTAKEN)
1493     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1494
1495   if (is_isa_v2)
1496     {
1497       /* Set 'a' bit.  This is 0b00010 in BO field for branch
1498          on CR(BI) insns (BO == 001at or 011at), and 0b01000
1499          for branch on CTR insns (BO == 1a00t or 1a01t).  */
1500       if ((insn & (0x14 << 21)) == (0x04 << 21))
1501         insn |= 0x02 << 21;
1502       else if ((insn & (0x14 << 21)) == (0x10 << 21))
1503         insn |= 0x08 << 21;
1504       else
1505         goto out;
1506     }
1507   else
1508     {
1509       bfd_vma target = 0;
1510       bfd_vma from;
1511
1512       if (!bfd_is_com_section (symbol->section))
1513         target = symbol->value;
1514       target += symbol->section->output_section->vma;
1515       target += symbol->section->output_offset;
1516       target += reloc_entry->addend;
1517
1518       from = (reloc_entry->address
1519               + input_section->output_offset
1520               + input_section->output_section->vma);
1521
1522       /* Invert 'y' bit if not the default.  */
1523       if ((bfd_signed_vma) (target - from) < 0)
1524         insn ^= 0x01 << 21;
1525     }
1526   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1527  out:
1528   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1529                                  input_section, output_bfd, error_message);
1530 }
1531
1532 static bfd_reloc_status_type
1533 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1534                          void *data, asection *input_section,
1535                          bfd *output_bfd, char **error_message)
1536 {
1537   /* If this is a relocatable link (output_bfd test tells us), just
1538      call the generic function.  Any adjustment will be done at final
1539      link time.  */
1540   if (output_bfd != NULL)
1541     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1542                                   input_section, output_bfd, error_message);
1543
1544   /* Subtract the symbol section base address.  */
1545   reloc_entry->addend -= symbol->section->output_section->vma;
1546   return bfd_reloc_continue;
1547 }
1548
1549 static bfd_reloc_status_type
1550 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1551                             void *data, asection *input_section,
1552                             bfd *output_bfd, char **error_message)
1553 {
1554   /* If this is a relocatable link (output_bfd test tells us), just
1555      call the generic function.  Any adjustment will be done at final
1556      link time.  */
1557   if (output_bfd != NULL)
1558     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1559                                   input_section, output_bfd, error_message);
1560
1561   /* Subtract the symbol section base address.  */
1562   reloc_entry->addend -= symbol->section->output_section->vma;
1563
1564   /* Adjust the addend for sign extension of the low 16 bits.  */
1565   reloc_entry->addend += 0x8000;
1566   return bfd_reloc_continue;
1567 }
1568
1569 static bfd_reloc_status_type
1570 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1571                      void *data, asection *input_section,
1572                      bfd *output_bfd, char **error_message)
1573 {
1574   bfd_vma TOCstart;
1575
1576   /* If this is a relocatable link (output_bfd test tells us), just
1577      call the generic function.  Any adjustment will be done at final
1578      link time.  */
1579   if (output_bfd != NULL)
1580     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1581                                   input_section, output_bfd, error_message);
1582
1583   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1584   if (TOCstart == 0)
1585     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1586
1587   /* Subtract the TOC base address.  */
1588   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1589   return bfd_reloc_continue;
1590 }
1591
1592 static bfd_reloc_status_type
1593 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1594                         void *data, asection *input_section,
1595                         bfd *output_bfd, char **error_message)
1596 {
1597   bfd_vma TOCstart;
1598
1599   /* If this is a relocatable link (output_bfd test tells us), just
1600      call the generic function.  Any adjustment will be done at final
1601      link time.  */
1602   if (output_bfd != NULL)
1603     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1604                                   input_section, output_bfd, error_message);
1605
1606   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1607   if (TOCstart == 0)
1608     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1609
1610   /* Subtract the TOC base address.  */
1611   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1612
1613   /* Adjust the addend for sign extension of the low 16 bits.  */
1614   reloc_entry->addend += 0x8000;
1615   return bfd_reloc_continue;
1616 }
1617
1618 static bfd_reloc_status_type
1619 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1620                        void *data, asection *input_section,
1621                        bfd *output_bfd, char **error_message)
1622 {
1623   bfd_vma TOCstart;
1624   bfd_size_type octets;
1625
1626   /* If this is a relocatable link (output_bfd test tells us), just
1627      call the generic function.  Any adjustment will be done at final
1628      link time.  */
1629   if (output_bfd != NULL)
1630     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1631                                   input_section, output_bfd, error_message);
1632
1633   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1634   if (TOCstart == 0)
1635     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1636
1637   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1638   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1639   return bfd_reloc_ok;
1640 }
1641
1642 static bfd_reloc_status_type
1643 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1644                         void *data, asection *input_section,
1645                         bfd *output_bfd, char **error_message)
1646 {
1647   uint64_t insn;
1648   bfd_vma targ;
1649
1650   if (output_bfd != NULL)
1651     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1652                                   input_section, output_bfd, error_message);
1653
1654   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1655   insn <<= 32;
1656   insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1657
1658   targ = (symbol->section->output_section->vma
1659           + symbol->section->output_offset
1660           + reloc_entry->addend);
1661   if (!bfd_is_com_section (symbol->section))
1662     targ += symbol->value;
1663   if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1664     targ += 1ULL << 33;
1665   if (reloc_entry->howto->pc_relative)
1666     {
1667       bfd_vma from = (reloc_entry->address
1668                       + input_section->output_offset
1669                       + input_section->output_section->vma);
1670       targ -=from;
1671     }
1672   targ >>= reloc_entry->howto->rightshift;
1673   insn &= ~reloc_entry->howto->dst_mask;
1674   insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1675   bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1676   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1677   if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1678       && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1679           >= 1ULL << reloc_entry->howto->bitsize))
1680     return bfd_reloc_overflow;
1681   return bfd_reloc_ok;
1682 }
1683
1684 static bfd_reloc_status_type
1685 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686                            void *data, asection *input_section,
1687                            bfd *output_bfd, char **error_message)
1688 {
1689   /* If this is a relocatable link (output_bfd test tells us), just
1690      call the generic function.  Any adjustment will be done at final
1691      link time.  */
1692   if (output_bfd != NULL)
1693     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1694                                   input_section, output_bfd, error_message);
1695
1696   if (error_message != NULL)
1697     {
1698       static char buf[60];
1699       sprintf (buf, "generic linker can't handle %s",
1700                reloc_entry->howto->name);
1701       *error_message = buf;
1702     }
1703   return bfd_reloc_dangerous;
1704 }
1705
1706 /* Track GOT entries needed for a given symbol.  We might need more
1707    than one got entry per symbol.  */
1708 struct got_entry
1709 {
1710   struct got_entry *next;
1711
1712   /* The symbol addend that we'll be placing in the GOT.  */
1713   bfd_vma addend;
1714
1715   /* Unlike other ELF targets, we use separate GOT entries for the same
1716      symbol referenced from different input files.  This is to support
1717      automatic multiple TOC/GOT sections, where the TOC base can vary
1718      from one input file to another.  After partitioning into TOC groups
1719      we merge entries within the group.
1720
1721      Point to the BFD owning this GOT entry.  */
1722   bfd *owner;
1723
1724   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1725      TLS_TPREL or TLS_DTPREL for tls entries.  */
1726   unsigned char tls_type;
1727
1728   /* Non-zero if got.ent points to real entry.  */
1729   unsigned char is_indirect;
1730
1731   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1732   union
1733   {
1734     bfd_signed_vma refcount;
1735     bfd_vma offset;
1736     struct got_entry *ent;
1737   } got;
1738 };
1739
1740 /* The same for PLT.  */
1741 struct plt_entry
1742 {
1743   struct plt_entry *next;
1744
1745   bfd_vma addend;
1746
1747   union
1748   {
1749     bfd_signed_vma refcount;
1750     bfd_vma offset;
1751   } plt;
1752 };
1753
1754 struct ppc64_elf_obj_tdata
1755 {
1756   struct elf_obj_tdata elf;
1757
1758   /* Shortcuts to dynamic linker sections.  */
1759   asection *got;
1760   asection *relgot;
1761
1762   /* Used during garbage collection.  We attach global symbols defined
1763      on removed .opd entries to this section so that the sym is removed.  */
1764   asection *deleted_section;
1765
1766   /* TLS local dynamic got entry handling.  Support for multiple GOT
1767      sections means we potentially need one of these for each input bfd.  */
1768   struct got_entry tlsld_got;
1769
1770   union
1771   {
1772     /* A copy of relocs before they are modified for --emit-relocs.  */
1773     Elf_Internal_Rela *relocs;
1774
1775     /* Section contents.  */
1776     bfd_byte *contents;
1777   } opd;
1778
1779   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1780      the reloc to be in the range -32768 to 32767.  */
1781   unsigned int has_small_toc_reloc : 1;
1782
1783   /* Set if toc/got ha relocs detected not using r2, or lo reloc
1784      instruction not one we handle.  */
1785   unsigned int unexpected_toc_insn : 1;
1786
1787   /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1788      this file.  */
1789   unsigned int has_optrel : 1;
1790 };
1791
1792 #define ppc64_elf_tdata(bfd) \
1793   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1794
1795 #define ppc64_tlsld_got(bfd) \
1796   (&ppc64_elf_tdata (bfd)->tlsld_got)
1797
1798 #define is_ppc64_elf(bfd) \
1799   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1800    && elf_object_id (bfd) == PPC64_ELF_DATA)
1801
1802 /* Override the generic function because we store some extras.  */
1803
1804 static bfd_boolean
1805 ppc64_elf_mkobject (bfd *abfd)
1806 {
1807   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1808                                   PPC64_ELF_DATA);
1809 }
1810
1811 /* Fix bad default arch selected for a 64 bit input bfd when the
1812    default is 32 bit.  Also select arch based on apuinfo.  */
1813
1814 static bfd_boolean
1815 ppc64_elf_object_p (bfd *abfd)
1816 {
1817   if (!abfd->arch_info->the_default)
1818     return TRUE;
1819
1820   if (abfd->arch_info->bits_per_word == 32)
1821     {
1822       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1823
1824       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1825         {
1826           /* Relies on arch after 32 bit default being 64 bit default.  */
1827           abfd->arch_info = abfd->arch_info->next;
1828           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1829         }
1830     }
1831   return _bfd_elf_ppc_set_arch (abfd);
1832 }
1833
1834 /* Support for core dump NOTE sections.  */
1835
1836 static bfd_boolean
1837 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1838 {
1839   size_t offset, size;
1840
1841   if (note->descsz != 504)
1842     return FALSE;
1843
1844   /* pr_cursig */
1845   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1846
1847   /* pr_pid */
1848   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1849
1850   /* pr_reg */
1851   offset = 112;
1852   size = 384;
1853
1854   /* Make a ".reg/999" section.  */
1855   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1856                                           size, note->descpos + offset);
1857 }
1858
1859 static bfd_boolean
1860 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1861 {
1862   if (note->descsz != 136)
1863     return FALSE;
1864
1865   elf_tdata (abfd)->core->pid
1866     = bfd_get_32 (abfd, note->descdata + 24);
1867   elf_tdata (abfd)->core->program
1868     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1869   elf_tdata (abfd)->core->command
1870     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1871
1872   return TRUE;
1873 }
1874
1875 static char *
1876 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1877                            ...)
1878 {
1879   switch (note_type)
1880     {
1881     default:
1882       return NULL;
1883
1884     case NT_PRPSINFO:
1885       {
1886         char data[136] ATTRIBUTE_NONSTRING;
1887         va_list ap;
1888
1889         va_start (ap, note_type);
1890         memset (data, 0, sizeof (data));
1891         strncpy (data + 40, va_arg (ap, const char *), 16);
1892 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1893         DIAGNOSTIC_PUSH;
1894         /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1895            -Wstringop-truncation:
1896            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1897          */
1898         DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1899 #endif
1900         strncpy (data + 56, va_arg (ap, const char *), 80);
1901 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1902         DIAGNOSTIC_POP;
1903 #endif
1904         va_end (ap);
1905         return elfcore_write_note (abfd, buf, bufsiz,
1906                                    "CORE", note_type, data, sizeof (data));
1907       }
1908
1909     case NT_PRSTATUS:
1910       {
1911         char data[504];
1912         va_list ap;
1913         long pid;
1914         int cursig;
1915         const void *greg;
1916
1917         va_start (ap, note_type);
1918         memset (data, 0, 112);
1919         pid = va_arg (ap, long);
1920         bfd_put_32 (abfd, pid, data + 32);
1921         cursig = va_arg (ap, int);
1922         bfd_put_16 (abfd, cursig, data + 12);
1923         greg = va_arg (ap, const void *);
1924         memcpy (data + 112, greg, 384);
1925         memset (data + 496, 0, 8);
1926         va_end (ap);
1927         return elfcore_write_note (abfd, buf, bufsiz,
1928                                    "CORE", note_type, data, sizeof (data));
1929       }
1930     }
1931 }
1932
1933 /* Add extra PPC sections.  */
1934
1935 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1936 {
1937   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1938   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1939   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1940   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1941   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1942   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1943   { NULL,                     0,  0, 0,            0 }
1944 };
1945
1946 enum _ppc64_sec_type {
1947   sec_normal = 0,
1948   sec_opd = 1,
1949   sec_toc = 2
1950 };
1951
1952 struct _ppc64_elf_section_data
1953 {
1954   struct bfd_elf_section_data elf;
1955
1956   union
1957   {
1958     /* An array with one entry for each opd function descriptor,
1959        and some spares since opd entries may be either 16 or 24 bytes.  */
1960 #define OPD_NDX(OFF) ((OFF) >> 4)
1961     struct _opd_sec_data
1962     {
1963       /* Points to the function code section for local opd entries.  */
1964       asection **func_sec;
1965
1966       /* After editing .opd, adjust references to opd local syms.  */
1967       long *adjust;
1968     } opd;
1969
1970     /* An array for toc sections, indexed by offset/8.  */
1971     struct _toc_sec_data
1972     {
1973       /* Specifies the relocation symbol index used at a given toc offset.  */
1974       unsigned *symndx;
1975
1976       /* And the relocation addend.  */
1977       bfd_vma *add;
1978     } toc;
1979   } u;
1980
1981   enum _ppc64_sec_type sec_type:2;
1982
1983   /* Flag set when small branches are detected.  Used to
1984      select suitable defaults for the stub group size.  */
1985   unsigned int has_14bit_branch:1;
1986
1987   /* Flag set when PLTCALL relocs are detected.  */
1988   unsigned int has_pltcall:1;
1989
1990   /* Flag set when section has PLT/GOT/TOC relocations that can be
1991      optimised.  */
1992   unsigned int has_optrel:1;
1993 };
1994
1995 #define ppc64_elf_section_data(sec) \
1996   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1997
1998 static bfd_boolean
1999 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2000 {
2001   if (!sec->used_by_bfd)
2002     {
2003       struct _ppc64_elf_section_data *sdata;
2004       size_t amt = sizeof (*sdata);
2005
2006       sdata = bfd_zalloc (abfd, amt);
2007       if (sdata == NULL)
2008         return FALSE;
2009       sec->used_by_bfd = sdata;
2010     }
2011
2012   return _bfd_elf_new_section_hook (abfd, sec);
2013 }
2014
2015 static bfd_boolean
2016 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2017 {
2018   const char *name = hdr->bfd_section->name;
2019
2020   if (strncmp (name, ".sbss", 5) == 0
2021       || strncmp (name, ".sdata", 6) == 0)
2022     hdr->bfd_section->flags |= SEC_SMALL_DATA;
2023
2024   return TRUE;
2025 }
2026
2027 static struct _opd_sec_data *
2028 get_opd_info (asection * sec)
2029 {
2030   if (sec != NULL
2031       && ppc64_elf_section_data (sec) != NULL
2032       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2033     return &ppc64_elf_section_data (sec)->u.opd;
2034   return NULL;
2035 }
2036 \f
2037 /* Parameters for the qsort hook.  */
2038 static bfd_boolean synthetic_relocatable;
2039 static asection *synthetic_opd;
2040
2041 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
2042
2043 static int
2044 compare_symbols (const void *ap, const void *bp)
2045 {
2046   const asymbol *a = *(const asymbol **) ap;
2047   const asymbol *b = *(const asymbol **) bp;
2048
2049   /* Section symbols first.  */
2050   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2051     return -1;
2052   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2053     return 1;
2054
2055   /* then .opd symbols.  */
2056   if (synthetic_opd != NULL)
2057     {
2058       if (strcmp (a->section->name, ".opd") == 0
2059           && strcmp (b->section->name, ".opd") != 0)
2060         return -1;
2061       if (strcmp (a->section->name, ".opd") != 0
2062           && strcmp (b->section->name, ".opd") == 0)
2063         return 1;
2064     }
2065
2066   /* then other code symbols.  */
2067   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2068        == (SEC_CODE | SEC_ALLOC))
2069       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2070           != (SEC_CODE | SEC_ALLOC)))
2071     return -1;
2072
2073   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2074        != (SEC_CODE | SEC_ALLOC))
2075       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2076           == (SEC_CODE | SEC_ALLOC)))
2077     return 1;
2078
2079   if (synthetic_relocatable)
2080     {
2081       if (a->section->id < b->section->id)
2082         return -1;
2083
2084       if (a->section->id > b->section->id)
2085         return 1;
2086     }
2087
2088   if (a->value + a->section->vma < b->value + b->section->vma)
2089     return -1;
2090
2091   if (a->value + a->section->vma > b->value + b->section->vma)
2092     return 1;
2093
2094   /* For syms with the same value, prefer strong dynamic global function
2095      syms over other syms.  */
2096   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2097     return -1;
2098
2099   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2100     return 1;
2101
2102   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2103     return -1;
2104
2105   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2106     return 1;
2107
2108   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2109     return -1;
2110
2111   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2112     return 1;
2113
2114   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2115     return -1;
2116
2117   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2118     return 1;
2119
2120   /* Finally, sort on where the symbol is in memory.  The symbols will
2121      be in at most two malloc'd blocks, one for static syms, one for
2122      dynamic syms, and we distinguish the two blocks above by testing
2123      BSF_DYNAMIC.  Since we are sorting the symbol pointers which were
2124      originally in the same order as the symbols (and we're not
2125      sorting the symbols themselves), this ensures a stable sort.  */
2126   if (a < b)
2127     return -1;
2128   if (a > b)
2129     return 1;
2130   return 0;
2131 }
2132
2133 /* Search SYMS for a symbol of the given VALUE.  */
2134
2135 static asymbol *
2136 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2137                bfd_vma value)
2138 {
2139   size_t mid;
2140
2141   if (id == (unsigned) -1)
2142     {
2143       while (lo < hi)
2144         {
2145           mid = (lo + hi) >> 1;
2146           if (syms[mid]->value + syms[mid]->section->vma < value)
2147             lo = mid + 1;
2148           else if (syms[mid]->value + syms[mid]->section->vma > value)
2149             hi = mid;
2150           else
2151             return syms[mid];
2152         }
2153     }
2154   else
2155     {
2156       while (lo < hi)
2157         {
2158           mid = (lo + hi) >> 1;
2159           if (syms[mid]->section->id < id)
2160             lo = mid + 1;
2161           else if (syms[mid]->section->id > id)
2162             hi = mid;
2163           else if (syms[mid]->value < value)
2164             lo = mid + 1;
2165           else if (syms[mid]->value > value)
2166             hi = mid;
2167           else
2168             return syms[mid];
2169         }
2170     }
2171   return NULL;
2172 }
2173
2174 static bfd_boolean
2175 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2176 {
2177   bfd_vma vma = *(bfd_vma *) ptr;
2178   return ((section->flags & SEC_ALLOC) != 0
2179           && section->vma <= vma
2180           && vma < section->vma + section->size);
2181 }
2182
2183 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2184    entry syms.  Also generate @plt symbols for the glink branch table.
2185    Returns count of synthetic symbols in RET or -1 on error.  */
2186
2187 static long
2188 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2189                                 long static_count, asymbol **static_syms,
2190                                 long dyn_count, asymbol **dyn_syms,
2191                                 asymbol **ret)
2192 {
2193   asymbol *s;
2194   size_t i, j, count;
2195   char *names;
2196   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2197   asection *opd = NULL;
2198   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2199   asymbol **syms;
2200   int abi = abiversion (abfd);
2201
2202   *ret = NULL;
2203
2204   if (abi < 2)
2205     {
2206       opd = bfd_get_section_by_name (abfd, ".opd");
2207       if (opd == NULL && abi == 1)
2208         return 0;
2209     }
2210
2211   syms = NULL;
2212   codesecsym = 0;
2213   codesecsymend = 0;
2214   secsymend = 0;
2215   opdsymend = 0;
2216   symcount = 0;
2217   if (opd != NULL)
2218     {
2219       symcount = static_count;
2220       if (!relocatable)
2221         symcount += dyn_count;
2222       if (symcount == 0)
2223         return 0;
2224
2225       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2226       if (syms == NULL)
2227         return -1;
2228
2229       if (!relocatable && static_count != 0 && dyn_count != 0)
2230         {
2231           /* Use both symbol tables.  */
2232           memcpy (syms, static_syms, static_count * sizeof (*syms));
2233           memcpy (syms + static_count, dyn_syms,
2234                   (dyn_count + 1) * sizeof (*syms));
2235         }
2236       else if (!relocatable && static_count == 0)
2237         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2238       else
2239         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2240
2241       /* Trim uninteresting symbols.  Interesting symbols are section,
2242          function, and notype symbols.  */
2243       for (i = 0, j = 0; i < symcount; ++i)
2244         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2245                                | BSF_RELC | BSF_SRELC)) == 0)
2246           syms[j++] = syms[i];
2247       symcount = j;
2248
2249       synthetic_relocatable = relocatable;
2250       synthetic_opd = opd;
2251       qsort (syms, symcount, sizeof (*syms), compare_symbols);
2252
2253       if (!relocatable && symcount > 1)
2254         {
2255           /* Trim duplicate syms, since we may have merged the normal
2256              and dynamic symbols.  Actually, we only care about syms
2257              that have different values, so trim any with the same
2258              value.  Don't consider ifunc and ifunc resolver symbols
2259              duplicates however, because GDB wants to know whether a
2260              text symbol is an ifunc resolver.  */
2261           for (i = 1, j = 1; i < symcount; ++i)
2262             {
2263               const asymbol *s0 = syms[i - 1];
2264               const asymbol *s1 = syms[i];
2265
2266               if ((s0->value + s0->section->vma
2267                    != s1->value + s1->section->vma)
2268                   || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2269                       != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2270                 syms[j++] = syms[i];
2271             }
2272           symcount = j;
2273         }
2274
2275       i = 0;
2276       /* Note that here and in compare_symbols we can't compare opd and
2277          sym->section directly.  With separate debug info files, the
2278          symbols will be extracted from the debug file while abfd passed
2279          to this function is the real binary.  */
2280       if (strcmp (syms[i]->section->name, ".opd") == 0)
2281         ++i;
2282       codesecsym = i;
2283
2284       for (; i < symcount; ++i)
2285         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2286                                          | SEC_THREAD_LOCAL))
2287              != (SEC_CODE | SEC_ALLOC))
2288             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2289           break;
2290       codesecsymend = i;
2291
2292       for (; i < symcount; ++i)
2293         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2294           break;
2295       secsymend = i;
2296
2297       for (; i < symcount; ++i)
2298         if (strcmp (syms[i]->section->name, ".opd") != 0)
2299           break;
2300       opdsymend = i;
2301
2302       for (; i < symcount; ++i)
2303         if (((syms[i]->section->flags
2304               & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2305             != (SEC_CODE | SEC_ALLOC))
2306           break;
2307       symcount = i;
2308     }
2309   count = 0;
2310
2311   if (relocatable)
2312     {
2313       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2314       arelent *r;
2315       size_t size;
2316       size_t relcount;
2317
2318       if (opdsymend == secsymend)
2319         goto done;
2320
2321       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2322       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2323       if (relcount == 0)
2324         goto done;
2325
2326       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2327         {
2328           count = -1;
2329           goto done;
2330         }
2331
2332       size = 0;
2333       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2334         {
2335           asymbol *sym;
2336
2337           while (r < opd->relocation + relcount
2338                  && r->address < syms[i]->value + opd->vma)
2339             ++r;
2340
2341           if (r == opd->relocation + relcount)
2342             break;
2343
2344           if (r->address != syms[i]->value + opd->vma)
2345             continue;
2346
2347           if (r->howto->type != R_PPC64_ADDR64)
2348             continue;
2349
2350           sym = *r->sym_ptr_ptr;
2351           if (!sym_exists_at (syms, opdsymend, symcount,
2352                               sym->section->id, sym->value + r->addend))
2353             {
2354               ++count;
2355               size += sizeof (asymbol);
2356               size += strlen (syms[i]->name) + 2;
2357             }
2358         }
2359
2360       if (size == 0)
2361         goto done;
2362       s = *ret = bfd_malloc (size);
2363       if (s == NULL)
2364         {
2365           count = -1;
2366           goto done;
2367         }
2368
2369       names = (char *) (s + count);
2370
2371       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2372         {
2373           asymbol *sym;
2374
2375           while (r < opd->relocation + relcount
2376                  && r->address < syms[i]->value + opd->vma)
2377             ++r;
2378
2379           if (r == opd->relocation + relcount)
2380             break;
2381
2382           if (r->address != syms[i]->value + opd->vma)
2383             continue;
2384
2385           if (r->howto->type != R_PPC64_ADDR64)
2386             continue;
2387
2388           sym = *r->sym_ptr_ptr;
2389           if (!sym_exists_at (syms, opdsymend, symcount,
2390                               sym->section->id, sym->value + r->addend))
2391             {
2392               size_t len;
2393
2394               *s = *syms[i];
2395               s->flags |= BSF_SYNTHETIC;
2396               s->section = sym->section;
2397               s->value = sym->value + r->addend;
2398               s->name = names;
2399               *names++ = '.';
2400               len = strlen (syms[i]->name);
2401               memcpy (names, syms[i]->name, len + 1);
2402               names += len + 1;
2403               /* Have udata.p point back to the original symbol this
2404                  synthetic symbol was derived from.  */
2405               s->udata.p = syms[i];
2406               s++;
2407             }
2408         }
2409     }
2410   else
2411     {
2412       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2413       bfd_byte *contents = NULL;
2414       size_t size;
2415       size_t plt_count = 0;
2416       bfd_vma glink_vma = 0, resolv_vma = 0;
2417       asection *dynamic, *glink = NULL, *relplt = NULL;
2418       arelent *p;
2419
2420       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2421         {
2422         free_contents_and_exit_err:
2423           count = -1;
2424         free_contents_and_exit:
2425           if (contents)
2426             free (contents);
2427           goto done;
2428         }
2429
2430       size = 0;
2431       for (i = secsymend; i < opdsymend; ++i)
2432         {
2433           bfd_vma ent;
2434
2435           /* Ignore bogus symbols.  */
2436           if (syms[i]->value > opd->size - 8)
2437             continue;
2438
2439           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2440           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2441             {
2442               ++count;
2443               size += sizeof (asymbol);
2444               size += strlen (syms[i]->name) + 2;
2445             }
2446         }
2447
2448       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2449       if (dyn_count != 0
2450           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2451         {
2452           bfd_byte *dynbuf, *extdyn, *extdynend;
2453           size_t extdynsize;
2454           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2455
2456           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2457             goto free_contents_and_exit_err;
2458
2459           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2460           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2461
2462           extdyn = dynbuf;
2463           extdynend = extdyn + dynamic->size;
2464           for (; extdyn < extdynend; extdyn += extdynsize)
2465             {
2466               Elf_Internal_Dyn dyn;
2467               (*swap_dyn_in) (abfd, extdyn, &dyn);
2468
2469               if (dyn.d_tag == DT_NULL)
2470                 break;
2471
2472               if (dyn.d_tag == DT_PPC64_GLINK)
2473                 {
2474                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2475                      See comment in ppc64_elf_finish_dynamic_sections. */
2476                   glink_vma = dyn.d_un.d_val + 8 * 4;
2477                   /* The .glink section usually does not survive the final
2478                      link; search for the section (usually .text) where the
2479                      glink stubs now reside.  */
2480                   glink = bfd_sections_find_if (abfd, section_covers_vma,
2481                                                 &glink_vma);
2482                   break;
2483                 }
2484             }
2485
2486           free (dynbuf);
2487         }
2488
2489       if (glink != NULL)
2490         {
2491           /* Determine __glink trampoline by reading the relative branch
2492              from the first glink stub.  */
2493           bfd_byte buf[4];
2494           unsigned int off = 0;
2495
2496           while (bfd_get_section_contents (abfd, glink, buf,
2497                                            glink_vma + off - glink->vma, 4))
2498             {
2499               unsigned int insn = bfd_get_32 (abfd, buf);
2500               insn ^= B_DOT;
2501               if ((insn & ~0x3fffffc) == 0)
2502                 {
2503                   resolv_vma
2504                     = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2505                   break;
2506                 }
2507               off += 4;
2508               if (off > 4)
2509                 break;
2510             }
2511
2512           if (resolv_vma)
2513             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2514
2515           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2516           if (relplt != NULL)
2517             {
2518               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2519               if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2520                 goto free_contents_and_exit_err;
2521
2522               plt_count = relplt->size / sizeof (Elf64_External_Rela);
2523               size += plt_count * sizeof (asymbol);
2524
2525               p = relplt->relocation;
2526               for (i = 0; i < plt_count; i++, p++)
2527                 {
2528                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2529                   if (p->addend != 0)
2530                     size += sizeof ("+0x") - 1 + 16;
2531                 }
2532             }
2533         }
2534
2535       if (size == 0)
2536         goto free_contents_and_exit;
2537       s = *ret = bfd_malloc (size);
2538       if (s == NULL)
2539         goto free_contents_and_exit_err;
2540
2541       names = (char *) (s + count + plt_count + (resolv_vma != 0));
2542
2543       for (i = secsymend; i < opdsymend; ++i)
2544         {
2545           bfd_vma ent;
2546
2547           if (syms[i]->value > opd->size - 8)
2548             continue;
2549
2550           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2551           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2552             {
2553               size_t lo, hi;
2554               size_t len;
2555               asection *sec = abfd->sections;
2556
2557               *s = *syms[i];
2558               lo = codesecsym;
2559               hi = codesecsymend;
2560               while (lo < hi)
2561                 {
2562                   size_t mid = (lo + hi) >> 1;
2563                   if (syms[mid]->section->vma < ent)
2564                     lo = mid + 1;
2565                   else if (syms[mid]->section->vma > ent)
2566                     hi = mid;
2567                   else
2568                     {
2569                       sec = syms[mid]->section;
2570                       break;
2571                     }
2572                 }
2573
2574               if (lo >= hi && lo > codesecsym)
2575                 sec = syms[lo - 1]->section;
2576
2577               for (; sec != NULL; sec = sec->next)
2578                 {
2579                   if (sec->vma > ent)
2580                     break;
2581                   /* SEC_LOAD may not be set if SEC is from a separate debug
2582                      info file.  */
2583                   if ((sec->flags & SEC_ALLOC) == 0)
2584                     break;
2585                   if ((sec->flags & SEC_CODE) != 0)
2586                     s->section = sec;
2587                 }
2588               s->flags |= BSF_SYNTHETIC;
2589               s->value = ent - s->section->vma;
2590               s->name = names;
2591               *names++ = '.';
2592               len = strlen (syms[i]->name);
2593               memcpy (names, syms[i]->name, len + 1);
2594               names += len + 1;
2595               /* Have udata.p point back to the original symbol this
2596                  synthetic symbol was derived from.  */
2597               s->udata.p = syms[i];
2598               s++;
2599             }
2600         }
2601       free (contents);
2602
2603       if (glink != NULL && relplt != NULL)
2604         {
2605           if (resolv_vma)
2606             {
2607               /* Add a symbol for the main glink trampoline.  */
2608               memset (s, 0, sizeof *s);
2609               s->the_bfd = abfd;
2610               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2611               s->section = glink;
2612               s->value = resolv_vma - glink->vma;
2613               s->name = names;
2614               memcpy (names, "__glink_PLTresolve",
2615                       sizeof ("__glink_PLTresolve"));
2616               names += sizeof ("__glink_PLTresolve");
2617               s++;
2618               count++;
2619             }
2620
2621           /* FIXME: It would be very much nicer to put sym@plt on the
2622              stub rather than on the glink branch table entry.  The
2623              objdump disassembler would then use a sensible symbol
2624              name on plt calls.  The difficulty in doing so is
2625              a) finding the stubs, and,
2626              b) matching stubs against plt entries, and,
2627              c) there can be multiple stubs for a given plt entry.
2628
2629              Solving (a) could be done by code scanning, but older
2630              ppc64 binaries used different stubs to current code.
2631              (b) is the tricky one since you need to known the toc
2632              pointer for at least one function that uses a pic stub to
2633              be able to calculate the plt address referenced.
2634              (c) means gdb would need to set multiple breakpoints (or
2635              find the glink branch itself) when setting breakpoints
2636              for pending shared library loads.  */
2637           p = relplt->relocation;
2638           for (i = 0; i < plt_count; i++, p++)
2639             {
2640               size_t len;
2641
2642               *s = **p->sym_ptr_ptr;
2643               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2644                  we are defining a symbol, ensure one of them is set.  */
2645               if ((s->flags & BSF_LOCAL) == 0)
2646                 s->flags |= BSF_GLOBAL;
2647               s->flags |= BSF_SYNTHETIC;
2648               s->section = glink;
2649               s->value = glink_vma - glink->vma;
2650               s->name = names;
2651               s->udata.p = NULL;
2652               len = strlen ((*p->sym_ptr_ptr)->name);
2653               memcpy (names, (*p->sym_ptr_ptr)->name, len);
2654               names += len;
2655               if (p->addend != 0)
2656                 {
2657                   memcpy (names, "+0x", sizeof ("+0x") - 1);
2658                   names += sizeof ("+0x") - 1;
2659                   bfd_sprintf_vma (abfd, names, p->addend);
2660                   names += strlen (names);
2661                 }
2662               memcpy (names, "@plt", sizeof ("@plt"));
2663               names += sizeof ("@plt");
2664               s++;
2665               if (abi < 2)
2666                 {
2667                   glink_vma += 8;
2668                   if (i >= 0x8000)
2669                     glink_vma += 4;
2670                 }
2671               else
2672                 glink_vma += 4;
2673             }
2674           count += plt_count;
2675         }
2676     }
2677
2678  done:
2679   free (syms);
2680   return count;
2681 }
2682 \f
2683 /* The following functions are specific to the ELF linker, while
2684    functions above are used generally.  Those named ppc64_elf_* are
2685    called by the main ELF linker code.  They appear in this file more
2686    or less in the order in which they are called.  eg.
2687    ppc64_elf_check_relocs is called early in the link process,
2688    ppc64_elf_finish_dynamic_sections is one of the last functions
2689    called.
2690
2691    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2692    functions have both a function code symbol and a function descriptor
2693    symbol.  A call to foo in a relocatable object file looks like:
2694
2695    .            .text
2696    .    x:
2697    .            bl      .foo
2698    .            nop
2699
2700    The function definition in another object file might be:
2701
2702    .            .section .opd
2703    .    foo:    .quad   .foo
2704    .            .quad   .TOC.@tocbase
2705    .            .quad   0
2706    .
2707    .            .text
2708    .    .foo:   blr
2709
2710    When the linker resolves the call during a static link, the branch
2711    unsurprisingly just goes to .foo and the .opd information is unused.
2712    If the function definition is in a shared library, things are a little
2713    different:  The call goes via a plt call stub, the opd information gets
2714    copied to the plt, and the linker patches the nop.
2715
2716    .    x:
2717    .            bl      .foo_stub
2718    .            ld      2,40(1)
2719    .
2720    .
2721    .    .foo_stub:
2722    .            std     2,40(1)                 # in practice, the call stub
2723    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
2724    .            addi    11,11,Lfoo@toc@l        # this is the general idea
2725    .            ld      12,0(11)
2726    .            ld      2,8(11)
2727    .            mtctr   12
2728    .            ld      11,16(11)
2729    .            bctr
2730    .
2731    .            .section .plt
2732    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2733
2734    The "reloc ()" notation is supposed to indicate that the linker emits
2735    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2736    copying.
2737
2738    What are the difficulties here?  Well, firstly, the relocations
2739    examined by the linker in check_relocs are against the function code
2740    sym .foo, while the dynamic relocation in the plt is emitted against
2741    the function descriptor symbol, foo.  Somewhere along the line, we need
2742    to carefully copy dynamic link information from one symbol to the other.
2743    Secondly, the generic part of the elf linker will make .foo a dynamic
2744    symbol as is normal for most other backends.  We need foo dynamic
2745    instead, at least for an application final link.  However, when
2746    creating a shared library containing foo, we need to have both symbols
2747    dynamic so that references to .foo are satisfied during the early
2748    stages of linking.  Otherwise the linker might decide to pull in a
2749    definition from some other object, eg. a static library.
2750
2751    Update: As of August 2004, we support a new convention.  Function
2752    calls may use the function descriptor symbol, ie. "bl foo".  This
2753    behaves exactly as "bl .foo".  */
2754
2755 /* Of those relocs that might be copied as dynamic relocs, this
2756    function selects those that must be copied when linking a shared
2757    library or PIE, even when the symbol is local.  */
2758
2759 static int
2760 must_be_dyn_reloc (struct bfd_link_info *info,
2761                    enum elf_ppc64_reloc_type r_type)
2762 {
2763   switch (r_type)
2764     {
2765     default:
2766       /* Only relative relocs can be resolved when the object load
2767          address isn't fixed.  DTPREL64 is excluded because the
2768          dynamic linker needs to differentiate global dynamic from
2769          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2770       return 1;
2771
2772     case R_PPC64_REL32:
2773     case R_PPC64_REL64:
2774     case R_PPC64_REL30:
2775     case R_PPC64_TOC16:
2776     case R_PPC64_TOC16_DS:
2777     case R_PPC64_TOC16_LO:
2778     case R_PPC64_TOC16_HI:
2779     case R_PPC64_TOC16_HA:
2780     case R_PPC64_TOC16_LO_DS:
2781       return 0;
2782
2783     case R_PPC64_TPREL16:
2784     case R_PPC64_TPREL16_LO:
2785     case R_PPC64_TPREL16_HI:
2786     case R_PPC64_TPREL16_HA:
2787     case R_PPC64_TPREL16_DS:
2788     case R_PPC64_TPREL16_LO_DS:
2789     case R_PPC64_TPREL16_HIGH:
2790     case R_PPC64_TPREL16_HIGHA:
2791     case R_PPC64_TPREL16_HIGHER:
2792     case R_PPC64_TPREL16_HIGHERA:
2793     case R_PPC64_TPREL16_HIGHEST:
2794     case R_PPC64_TPREL16_HIGHESTA:
2795     case R_PPC64_TPREL64:
2796     case R_PPC64_TPREL34:
2797       /* These relocations are relative but in a shared library the
2798          linker doesn't know the thread pointer base.  */
2799       return bfd_link_dll (info);
2800     }
2801 }
2802
2803 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2804    copying dynamic variables from a shared lib into an app's .dynbss
2805    section, and instead use a dynamic relocation to point into the
2806    shared lib.  With code that gcc generates it is vital that this be
2807    enabled;  In the PowerPC64 ELFv1 ABI the address of a function is
2808    actually the address of a function descriptor which resides in the
2809    .opd section.  gcc uses the descriptor directly rather than going
2810    via the GOT as some other ABIs do, which means that initialized
2811    function pointers reference the descriptor.  Thus, a function
2812    pointer initialized to the address of a function in a shared
2813    library will either require a .dynbss copy and a copy reloc, or a
2814    dynamic reloc.  Using a .dynbss copy redefines the function
2815    descriptor symbol to point to the copy.  This presents a problem as
2816    a PLT entry for that function is also initialized from the function
2817    descriptor symbol and the copy may not be initialized first.  */
2818 #define ELIMINATE_COPY_RELOCS 1
2819
2820 /* Section name for stubs is the associated section name plus this
2821    string.  */
2822 #define STUB_SUFFIX ".stub"
2823
2824 /* Linker stubs.
2825    ppc_stub_long_branch:
2826    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2827    destination, but a 24 bit branch in a stub section will reach.
2828    .    b       dest
2829
2830    ppc_stub_plt_branch:
2831    Similar to the above, but a 24 bit branch in the stub section won't
2832    reach its destination.
2833    .    addis   %r12,%r2,xxx@toc@ha
2834    .    ld      %r12,xxx@toc@l(%r12)
2835    .    mtctr   %r12
2836    .    bctr
2837
2838    ppc_stub_plt_call:
2839    Used to call a function in a shared library.  If it so happens that
2840    the plt entry referenced crosses a 64k boundary, then an extra
2841    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2842    ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2843    .    addis   %r11,%r2,xxx@toc@ha
2844    .    ld      %r12,xxx+0@toc@l(%r11)
2845    .    mtctr   %r12
2846    .    ld      %r2,xxx+8@toc@l(%r11)
2847    .    ld      %r11,xxx+16@toc@l(%r11)
2848    .    bctr
2849
2850    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2851    code to adjust the value and save r2 to support multiple toc sections.
2852    A ppc_stub_long_branch with an r2 offset looks like:
2853    .    std     %r2,40(%r1)
2854    .    addis   %r2,%r2,off@ha
2855    .    addi    %r2,%r2,off@l
2856    .    b       dest
2857
2858    A ppc_stub_plt_branch with an r2 offset looks like:
2859    .    std     %r2,40(%r1)
2860    .    addis   %r12,%r2,xxx@toc@ha
2861    .    ld      %r12,xxx@toc@l(%r12)
2862    .    addis   %r2,%r2,off@ha
2863    .    addi    %r2,%r2,off@l
2864    .    mtctr   %r12
2865    .    bctr
2866
2867    All of the above stubs are shown as their ELFv1 variants.  ELFv2
2868    variants exist too, simpler for plt calls since a new toc pointer
2869    and static chain are not loaded by the stub.  In addition, ELFv2
2870    has some more complex stubs to handle calls marked with NOTOC
2871    relocs from functions where r2 is not a valid toc pointer.  These
2872    come in two flavours, the ones shown below, and _both variants that
2873    start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2874    one call is from a function where r2 is used as the toc pointer but
2875    needs a toc adjusting stub for small-model multi-toc, and another
2876    call is from a function where r2 is not valid.
2877    ppc_stub_long_branch_notoc:
2878    .    mflr    %r12
2879    .    bcl     20,31,1f
2880    .  1:
2881    .    mflr    %r11
2882    .    mtlr    %r12
2883    .    addis   %r12,%r11,dest-1b@ha
2884    .    addi    %r12,%r12,dest-1b@l
2885    .    b       dest
2886
2887    ppc_stub_plt_branch_notoc:
2888    .    mflr    %r12
2889    .    bcl     20,31,1f
2890    .  1:
2891    .    mflr    %r11
2892    .    mtlr    %r12
2893    .    lis     %r12,xxx-1b@highest
2894    .    ori     %r12,%r12,xxx-1b@higher
2895    .    sldi    %r12,%r12,32
2896    .    oris    %r12,%r12,xxx-1b@high
2897    .    ori     %r12,%r12,xxx-1b@l
2898    .    add     %r12,%r11,%r12
2899    .    mtctr   %r12
2900    .    bctr
2901
2902    ppc_stub_plt_call_notoc:
2903    .    mflr    %r12
2904    .    bcl     20,31,1f
2905    .  1:
2906    .    mflr    %r11
2907    .    mtlr    %r12
2908    .    lis     %r12,xxx-1b@highest
2909    .    ori     %r12,%r12,xxx-1b@higher
2910    .    sldi    %r12,%r12,32
2911    .    oris    %r12,%r12,xxx-1b@high
2912    .    ori     %r12,%r12,xxx-1b@l
2913    .    ldx     %r12,%r11,%r12
2914    .    mtctr   %r12
2915    .    bctr
2916
2917    There are also ELFv1 powerxx variants of these stubs.
2918    ppc_stub_long_branch_notoc:
2919    .    pla     %r12,dest@pcrel
2920    .    b       dest
2921    ppc_stub_plt_branch_notoc:
2922    .    lis     %r11,(dest-1f)@highesta34
2923    .    ori     %r11,%r11,(dest-1f)@highera34
2924    .    sldi    %r11,%r11,34
2925    . 1: pla     %r12,dest@pcrel
2926    .    add     %r12,%r11,%r12
2927    .    mtctr   %r12
2928    .    bctr
2929    ppc_stub_plt_call_notoc:
2930    .    lis     %r11,(xxx-1f)@highesta34
2931    .    ori     %r11,%r11,(xxx-1f)@highera34
2932    .    sldi    %r11,%r11,34
2933    . 1: pla     %r12,xxx@pcrel
2934    .    ldx     %r12,%r11,%r12
2935    .    mtctr   %r12
2936    .    bctr
2937
2938    In cases where the high instructions would add zero, they are
2939    omitted and following instructions modified in some cases.
2940    For example, a powerxx ppc_stub_plt_call_notoc might simplify down
2941    to
2942    .    pld     %r12,xxx@pcrel
2943    .    mtctr   %r12
2944    .    bctr
2945
2946    For a given stub group (a set of sections all using the same toc
2947    pointer value) there will be just one stub type used for any
2948    particular function symbol.  For example, if printf is called from
2949    code with the tocsave optimization (ie. r2 saved in function
2950    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2951    and from other code without the tocsave optimization requiring a
2952    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2953    type will be created.  Calls with the tocsave optimization will
2954    enter this stub after the instruction saving r2.  A similar
2955    situation exists when calls are marked with R_PPC64_REL24_NOTOC
2956    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2957    to call an external function like printf.  If other calls to printf
2958    require a ppc_stub_plt_call linkage stub then a single
2959    ppc_stub_plt_call_notoc linkage stub will be used for both types of
2960    call.  If other calls to printf require a ppc_stub_plt_call_r2save
2961    linkage stub then a single ppc_stub_plt_call_both linkage stub will
2962    be created and calls not requiring r2 to be saved will enter the
2963    stub after the r2 save instruction.  There is an analogous
2964    hierarchy of long branch and plt branch stubs for local call
2965    linkage.  */
2966
2967 enum ppc_stub_type
2968 {
2969   ppc_stub_none,
2970   ppc_stub_long_branch,
2971   ppc_stub_long_branch_r2off,
2972   ppc_stub_long_branch_notoc,
2973   ppc_stub_long_branch_both, /* r2off and notoc variants both needed.  */
2974   ppc_stub_plt_branch,
2975   ppc_stub_plt_branch_r2off,
2976   ppc_stub_plt_branch_notoc,
2977   ppc_stub_plt_branch_both,
2978   ppc_stub_plt_call,
2979   ppc_stub_plt_call_r2save,
2980   ppc_stub_plt_call_notoc,
2981   ppc_stub_plt_call_both,
2982   ppc_stub_global_entry,
2983   ppc_stub_save_res
2984 };
2985
2986 /* Information on stub grouping.  */
2987 struct map_stub
2988 {
2989   /* The stub section.  */
2990   asection *stub_sec;
2991   /* This is the section to which stubs in the group will be attached.  */
2992   asection *link_sec;
2993   /* Next group.  */
2994   struct map_stub *next;
2995   /* Whether to emit a copy of register save/restore functions in this
2996      group.  */
2997   int needs_save_res;
2998   /* Current offset within stubs after the insn restoring lr in a
2999      _notoc or _both stub using bcl for pc-relative addressing, or
3000      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
3001   unsigned int lr_restore;
3002   /* Accumulated size of EH info emitted to describe return address
3003      if stubs modify lr.  Does not include 17 byte FDE header.  */
3004   unsigned int eh_size;
3005   /* Offset in glink_eh_frame to the start of EH info for this group.  */
3006   unsigned int eh_base;
3007 };
3008
3009 struct ppc_stub_hash_entry
3010 {
3011   /* Base hash table entry structure.  */
3012   struct bfd_hash_entry root;
3013
3014   enum ppc_stub_type stub_type;
3015
3016   /* Group information.  */
3017   struct map_stub *group;
3018
3019   /* Offset within stub_sec of the beginning of this stub.  */
3020   bfd_vma stub_offset;
3021
3022   /* Given the symbol's value and its section we can determine its final
3023      value when building the stubs (so the stub knows where to jump.  */
3024   bfd_vma target_value;
3025   asection *target_section;
3026
3027   /* The symbol table entry, if any, that this was derived from.  */
3028   struct ppc_link_hash_entry *h;
3029   struct plt_entry *plt_ent;
3030
3031   /* Symbol type.  */
3032   unsigned char symtype;
3033
3034   /* Symbol st_other.  */
3035   unsigned char other;
3036 };
3037
3038 struct ppc_branch_hash_entry
3039 {
3040   /* Base hash table entry structure.  */
3041   struct bfd_hash_entry root;
3042
3043   /* Offset within branch lookup table.  */
3044   unsigned int offset;
3045
3046   /* Generation marker.  */
3047   unsigned int iter;
3048 };
3049
3050 /* Used to track dynamic relocations for local symbols.  */
3051 struct ppc_dyn_relocs
3052 {
3053   struct ppc_dyn_relocs *next;
3054
3055   /* The input section of the reloc.  */
3056   asection *sec;
3057
3058   /* Total number of relocs copied for the input section.  */
3059   unsigned int count : 31;
3060
3061   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3062   unsigned int ifunc : 1;
3063 };
3064
3065 struct ppc_link_hash_entry
3066 {
3067   struct elf_link_hash_entry elf;
3068
3069   union
3070   {
3071     /* A pointer to the most recently used stub hash entry against this
3072        symbol.  */
3073     struct ppc_stub_hash_entry *stub_cache;
3074
3075     /* A pointer to the next symbol starting with a '.'  */
3076     struct ppc_link_hash_entry *next_dot_sym;
3077   } u;
3078
3079   /* Track dynamic relocs copied for this symbol.  */
3080   struct elf_dyn_relocs *dyn_relocs;
3081
3082   /* Link between function code and descriptor symbols.  */
3083   struct ppc_link_hash_entry *oh;
3084
3085   /* Flag function code and descriptor symbols.  */
3086   unsigned int is_func:1;
3087   unsigned int is_func_descriptor:1;
3088   unsigned int fake:1;
3089
3090   /* Whether global opd/toc sym has been adjusted or not.
3091      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3092      should be set for all globals defined in any opd/toc section.  */
3093   unsigned int adjust_done:1;
3094
3095   /* Set if this is an out-of-line register save/restore function,
3096      with non-standard calling convention.  */
3097   unsigned int save_res:1;
3098
3099   /* Set if a duplicate symbol with non-zero localentry is detected,
3100      even when the duplicate symbol does not provide a definition.  */
3101   unsigned int non_zero_localentry:1;
3102
3103   /* Contexts in which symbol is used in the GOT (or TOC).
3104      Bits are or'd into the mask as the corresponding relocs are
3105      encountered during check_relocs, with TLS_TLS being set when any
3106      of the other TLS bits are set.  tls_optimize clears bits when
3107      optimizing to indicate the corresponding GOT entry type is not
3108      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
3109      set TLS_GDIE when a GD reloc turns into an IE one.
3110      These flags are also kept for local symbols.  */
3111 #define TLS_TLS          1      /* Any TLS reloc.  */
3112 #define TLS_GD           2      /* GD reloc. */
3113 #define TLS_LD           4      /* LD reloc. */
3114 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
3115 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
3116 #define TLS_MARK        32      /* __tls_get_addr call marked. */
3117 #define TLS_GDIE        64      /* GOT TPREL reloc resulting from GD->IE. */
3118 #define TLS_EXPLICIT   256      /* TOC section TLS reloc, not stored. */
3119   unsigned char tls_mask;
3120
3121   /* The above field is also used to mark function symbols.  In which
3122      case TLS_TLS will be 0.  */
3123 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
3124 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
3125 #define NON_GOT        256      /* local symbol plt, not stored.  */
3126 };
3127
3128 static inline struct ppc_link_hash_entry *
3129 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3130 {
3131   return (struct ppc_link_hash_entry *) ent;
3132 }
3133
3134 /* ppc64 ELF linker hash table.  */
3135
3136 struct ppc_link_hash_table
3137 {
3138   struct elf_link_hash_table elf;
3139
3140   /* The stub hash table.  */
3141   struct bfd_hash_table stub_hash_table;
3142
3143   /* Another hash table for plt_branch stubs.  */
3144   struct bfd_hash_table branch_hash_table;
3145
3146   /* Hash table for function prologue tocsave.  */
3147   htab_t tocsave_htab;
3148
3149   /* Various options and other info passed from the linker.  */
3150   struct ppc64_elf_params *params;
3151
3152   /* The size of sec_info below.  */
3153   unsigned int sec_info_arr_size;
3154
3155   /* Per-section array of extra section info.  Done this way rather
3156      than as part of ppc64_elf_section_data so we have the info for
3157      non-ppc64 sections.  */
3158   struct
3159   {
3160     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
3161     bfd_vma toc_off;
3162
3163     union
3164     {
3165       /* The section group that this section belongs to.  */
3166       struct map_stub *group;
3167       /* A temp section list pointer.  */
3168       asection *list;
3169     } u;
3170   } *sec_info;
3171
3172   /* Linked list of groups.  */
3173   struct map_stub *group;
3174
3175   /* Temp used when calculating TOC pointers.  */
3176   bfd_vma toc_curr;
3177   bfd *toc_bfd;
3178   asection *toc_first_sec;
3179
3180   /* Used when adding symbols.  */
3181   struct ppc_link_hash_entry *dot_syms;
3182
3183   /* Shortcuts to get to dynamic linker sections.  */
3184   asection *glink;
3185   asection *global_entry;
3186   asection *sfpr;
3187   asection *pltlocal;
3188   asection *relpltlocal;
3189   asection *brlt;
3190   asection *relbrlt;
3191   asection *glink_eh_frame;
3192
3193   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
3194   struct ppc_link_hash_entry *tls_get_addr;
3195   struct ppc_link_hash_entry *tls_get_addr_fd;
3196   struct ppc_link_hash_entry *tga_desc;
3197   struct ppc_link_hash_entry *tga_desc_fd;
3198   struct map_stub *tga_group;
3199
3200   /* The size of reliplt used by got entry relocs.  */
3201   bfd_size_type got_reli_size;
3202
3203   /* Statistics.  */
3204   unsigned long stub_count[ppc_stub_global_entry];
3205
3206   /* Number of stubs against global syms.  */
3207   unsigned long stub_globals;
3208
3209   /* Set if we're linking code with function descriptors.  */
3210   unsigned int opd_abi:1;
3211
3212   /* Support for multiple toc sections.  */
3213   unsigned int do_multi_toc:1;
3214   unsigned int multi_toc_needed:1;
3215   unsigned int second_toc_pass:1;
3216   unsigned int do_toc_opt:1;
3217
3218   /* Set if tls optimization is enabled.  */
3219   unsigned int do_tls_opt:1;
3220
3221   /* Set if inline plt calls should be converted to direct calls.  */
3222   unsigned int can_convert_all_inline_plt:1;
3223
3224   /* Set on error.  */
3225   unsigned int stub_error:1;
3226
3227   /* Whether func_desc_adjust needs to be run over symbols.  */
3228   unsigned int need_func_desc_adj:1;
3229
3230   /* Whether there exist local gnu indirect function resolvers,
3231      referenced by dynamic relocations.  */
3232   unsigned int local_ifunc_resolver:1;
3233   unsigned int maybe_local_ifunc_resolver:1;
3234
3235   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
3236   unsigned int has_plt_localentry0:1;
3237
3238   /* Whether calls are made via the PLT from NOTOC functions.  */
3239   unsigned int notoc_plt:1;
3240
3241   /* Whether to use powerxx instructions in linkage stubs.  */
3242   unsigned int powerxx_stubs:1;
3243
3244   /* Incremented every time we size stubs.  */
3245   unsigned int stub_iteration;
3246
3247   /* Small local sym cache.  */
3248   struct sym_cache sym_cache;
3249 };
3250
3251 /* Rename some of the generic section flags to better document how they
3252    are used here.  */
3253
3254 /* Nonzero if this section has TLS related relocations.  */
3255 #define has_tls_reloc sec_flg0
3256
3257 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3258    relocations.  */
3259 #define nomark_tls_get_addr sec_flg1
3260
3261 /* Nonzero if this section has any toc or got relocs.  */
3262 #define has_toc_reloc sec_flg2
3263
3264 /* Nonzero if this section has a call to another section that uses
3265    the toc or got.  */
3266 #define makes_toc_func_call sec_flg3
3267
3268 /* Recursion protection when determining above flag.  */
3269 #define call_check_in_progress sec_flg4
3270 #define call_check_done sec_flg5
3271
3272 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
3273
3274 #define ppc_hash_table(p) \
3275   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3276   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3277
3278 #define ppc_stub_hash_lookup(table, string, create, copy) \
3279   ((struct ppc_stub_hash_entry *) \
3280    bfd_hash_lookup ((table), (string), (create), (copy)))
3281
3282 #define ppc_branch_hash_lookup(table, string, create, copy) \
3283   ((struct ppc_branch_hash_entry *) \
3284    bfd_hash_lookup ((table), (string), (create), (copy)))
3285
3286 /* Create an entry in the stub hash table.  */
3287
3288 static struct bfd_hash_entry *
3289 stub_hash_newfunc (struct bfd_hash_entry *entry,
3290                    struct bfd_hash_table *table,
3291                    const char *string)
3292 {
3293   /* Allocate the structure if it has not already been allocated by a
3294      subclass.  */
3295   if (entry == NULL)
3296     {
3297       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3298       if (entry == NULL)
3299         return entry;
3300     }
3301
3302   /* Call the allocation method of the superclass.  */
3303   entry = bfd_hash_newfunc (entry, table, string);
3304   if (entry != NULL)
3305     {
3306       struct ppc_stub_hash_entry *eh;
3307
3308       /* Initialize the local fields.  */
3309       eh = (struct ppc_stub_hash_entry *) entry;
3310       eh->stub_type = ppc_stub_none;
3311       eh->group = NULL;
3312       eh->stub_offset = 0;
3313       eh->target_value = 0;
3314       eh->target_section = NULL;
3315       eh->h = NULL;
3316       eh->plt_ent = NULL;
3317       eh->other = 0;
3318     }
3319
3320   return entry;
3321 }
3322
3323 /* Create an entry in the branch hash table.  */
3324
3325 static struct bfd_hash_entry *
3326 branch_hash_newfunc (struct bfd_hash_entry *entry,
3327                      struct bfd_hash_table *table,
3328                      const char *string)
3329 {
3330   /* Allocate the structure if it has not already been allocated by a
3331      subclass.  */
3332   if (entry == NULL)
3333     {
3334       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3335       if (entry == NULL)
3336         return entry;
3337     }
3338
3339   /* Call the allocation method of the superclass.  */
3340   entry = bfd_hash_newfunc (entry, table, string);
3341   if (entry != NULL)
3342     {
3343       struct ppc_branch_hash_entry *eh;
3344
3345       /* Initialize the local fields.  */
3346       eh = (struct ppc_branch_hash_entry *) entry;
3347       eh->offset = 0;
3348       eh->iter = 0;
3349     }
3350
3351   return entry;
3352 }
3353
3354 /* Create an entry in a ppc64 ELF linker hash table.  */
3355
3356 static struct bfd_hash_entry *
3357 link_hash_newfunc (struct bfd_hash_entry *entry,
3358                    struct bfd_hash_table *table,
3359                    const char *string)
3360 {
3361   /* Allocate the structure if it has not already been allocated by a
3362      subclass.  */
3363   if (entry == NULL)
3364     {
3365       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3366       if (entry == NULL)
3367         return entry;
3368     }
3369
3370   /* Call the allocation method of the superclass.  */
3371   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3372   if (entry != NULL)
3373     {
3374       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3375
3376       memset (&eh->u.stub_cache, 0,
3377               (sizeof (struct ppc_link_hash_entry)
3378                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3379
3380       /* When making function calls, old ABI code references function entry
3381          points (dot symbols), while new ABI code references the function
3382          descriptor symbol.  We need to make any combination of reference and
3383          definition work together, without breaking archive linking.
3384
3385          For a defined function "foo" and an undefined call to "bar":
3386          An old object defines "foo" and ".foo", references ".bar" (possibly
3387          "bar" too).
3388          A new object defines "foo" and references "bar".
3389
3390          A new object thus has no problem with its undefined symbols being
3391          satisfied by definitions in an old object.  On the other hand, the
3392          old object won't have ".bar" satisfied by a new object.
3393
3394          Keep a list of newly added dot-symbols.  */
3395
3396       if (string[0] == '.')
3397         {
3398           struct ppc_link_hash_table *htab;
3399
3400           htab = (struct ppc_link_hash_table *) table;
3401           eh->u.next_dot_sym = htab->dot_syms;
3402           htab->dot_syms = eh;
3403         }
3404     }
3405
3406   return entry;
3407 }
3408
3409 struct tocsave_entry
3410 {
3411   asection *sec;
3412   bfd_vma offset;
3413 };
3414
3415 static hashval_t
3416 tocsave_htab_hash (const void *p)
3417 {
3418   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3419   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3420 }
3421
3422 static int
3423 tocsave_htab_eq (const void *p1, const void *p2)
3424 {
3425   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3426   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3427   return e1->sec == e2->sec && e1->offset == e2->offset;
3428 }
3429
3430 /* Destroy a ppc64 ELF linker hash table.  */
3431
3432 static void
3433 ppc64_elf_link_hash_table_free (bfd *obfd)
3434 {
3435   struct ppc_link_hash_table *htab;
3436
3437   htab = (struct ppc_link_hash_table *) obfd->link.hash;
3438   if (htab->tocsave_htab)
3439     htab_delete (htab->tocsave_htab);
3440   bfd_hash_table_free (&htab->branch_hash_table);
3441   bfd_hash_table_free (&htab->stub_hash_table);
3442   _bfd_elf_link_hash_table_free (obfd);
3443 }
3444
3445 /* Create a ppc64 ELF linker hash table.  */
3446
3447 static struct bfd_link_hash_table *
3448 ppc64_elf_link_hash_table_create (bfd *abfd)
3449 {
3450   struct ppc_link_hash_table *htab;
3451   size_t amt = sizeof (struct ppc_link_hash_table);
3452
3453   htab = bfd_zmalloc (amt);
3454   if (htab == NULL)
3455     return NULL;
3456
3457   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3458                                       sizeof (struct ppc_link_hash_entry),
3459                                       PPC64_ELF_DATA))
3460     {
3461       free (htab);
3462       return NULL;
3463     }
3464
3465   /* Init the stub hash table too.  */
3466   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3467                             sizeof (struct ppc_stub_hash_entry)))
3468     {
3469       _bfd_elf_link_hash_table_free (abfd);
3470       return NULL;
3471     }
3472
3473   /* And the branch hash table.  */
3474   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3475                             sizeof (struct ppc_branch_hash_entry)))
3476     {
3477       bfd_hash_table_free (&htab->stub_hash_table);
3478       _bfd_elf_link_hash_table_free (abfd);
3479       return NULL;
3480     }
3481
3482   htab->tocsave_htab = htab_try_create (1024,
3483                                         tocsave_htab_hash,
3484                                         tocsave_htab_eq,
3485                                         NULL);
3486   if (htab->tocsave_htab == NULL)
3487     {
3488       ppc64_elf_link_hash_table_free (abfd);
3489       return NULL;
3490     }
3491   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3492
3493   /* Initializing two fields of the union is just cosmetic.  We really
3494      only care about glist, but when compiled on a 32-bit host the
3495      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3496      debugger inspection of these fields look nicer.  */
3497   htab->elf.init_got_refcount.refcount = 0;
3498   htab->elf.init_got_refcount.glist = NULL;
3499   htab->elf.init_plt_refcount.refcount = 0;
3500   htab->elf.init_plt_refcount.glist = NULL;
3501   htab->elf.init_got_offset.offset = 0;
3502   htab->elf.init_got_offset.glist = NULL;
3503   htab->elf.init_plt_offset.offset = 0;
3504   htab->elf.init_plt_offset.glist = NULL;
3505
3506   return &htab->elf.root;
3507 }
3508
3509 /* Create sections for linker generated code.  */
3510
3511 static bfd_boolean
3512 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3513 {
3514   struct ppc_link_hash_table *htab;
3515   flagword flags;
3516
3517   htab = ppc_hash_table (info);
3518
3519   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3520            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3521   if (htab->params->save_restore_funcs)
3522     {
3523       /* Create .sfpr for code to save and restore fp regs.  */
3524       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3525                                                        flags);
3526       if (htab->sfpr == NULL
3527           || !bfd_set_section_alignment (htab->sfpr, 2))
3528         return FALSE;
3529     }
3530
3531   if (bfd_link_relocatable (info))
3532     return TRUE;
3533
3534   /* Create .glink for lazy dynamic linking support.  */
3535   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3536                                                     flags);
3537   if (htab->glink == NULL
3538       || !bfd_set_section_alignment (htab->glink, 3))
3539     return FALSE;
3540
3541   /* The part of .glink used by global entry stubs, separate so that
3542      it can be aligned appropriately without affecting htab->glink.  */
3543   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3544                                                            flags);
3545   if (htab->global_entry == NULL
3546       || !bfd_set_section_alignment (htab->global_entry, 2))
3547     return FALSE;
3548
3549   if (!info->no_ld_generated_unwind_info)
3550     {
3551       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3552                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3553       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3554                                                                  ".eh_frame",
3555                                                                  flags);
3556       if (htab->glink_eh_frame == NULL
3557           || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3558         return FALSE;
3559     }
3560
3561   flags = SEC_ALLOC | SEC_LINKER_CREATED;
3562   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3563   if (htab->elf.iplt == NULL
3564       || !bfd_set_section_alignment (htab->elf.iplt, 3))
3565     return FALSE;
3566
3567   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3568            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3569   htab->elf.irelplt
3570     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3571   if (htab->elf.irelplt == NULL
3572       || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3573     return FALSE;
3574
3575   /* Create branch lookup table for plt_branch stubs.  */
3576   flags = (SEC_ALLOC | SEC_LOAD
3577            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3578   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3579                                                    flags);
3580   if (htab->brlt == NULL
3581       || !bfd_set_section_alignment (htab->brlt, 3))
3582     return FALSE;
3583
3584   /* Local plt entries, put in .branch_lt but a separate section for
3585      convenience.  */
3586   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3587                                                        flags);
3588   if (htab->pltlocal == NULL
3589       || !bfd_set_section_alignment (htab->pltlocal, 3))
3590     return FALSE;
3591
3592   if (!bfd_link_pic (info))
3593     return TRUE;
3594
3595   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3596            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3597   htab->relbrlt
3598     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3599   if (htab->relbrlt == NULL
3600       || !bfd_set_section_alignment (htab->relbrlt, 3))
3601     return FALSE;
3602
3603   htab->relpltlocal
3604     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3605   if (htab->relpltlocal == NULL
3606       || !bfd_set_section_alignment (htab->relpltlocal, 3))
3607     return FALSE;
3608
3609   return TRUE;
3610 }
3611
3612 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3613
3614 bfd_boolean
3615 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3616                          struct ppc64_elf_params *params)
3617 {
3618   struct ppc_link_hash_table *htab;
3619
3620   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3621
3622 /* Always hook our dynamic sections into the first bfd, which is the
3623    linker created stub bfd.  This ensures that the GOT header is at
3624    the start of the output TOC section.  */
3625   htab = ppc_hash_table (info);
3626   htab->elf.dynobj = params->stub_bfd;
3627   htab->params = params;
3628
3629   return create_linkage_sections (htab->elf.dynobj, info);
3630 }
3631
3632 /* Build a name for an entry in the stub hash table.  */
3633
3634 static char *
3635 ppc_stub_name (const asection *input_section,
3636                const asection *sym_sec,
3637                const struct ppc_link_hash_entry *h,
3638                const Elf_Internal_Rela *rel)
3639 {
3640   char *stub_name;
3641   ssize_t len;
3642
3643   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3644      offsets from a sym as a branch target?  In fact, we could
3645      probably assume the addend is always zero.  */
3646   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3647
3648   if (h)
3649     {
3650       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3651       stub_name = bfd_malloc (len);
3652       if (stub_name == NULL)
3653         return stub_name;
3654
3655       len = sprintf (stub_name, "%08x.%s+%x",
3656                      input_section->id & 0xffffffff,
3657                      h->elf.root.root.string,
3658                      (int) rel->r_addend & 0xffffffff);
3659     }
3660   else
3661     {
3662       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3663       stub_name = bfd_malloc (len);
3664       if (stub_name == NULL)
3665         return stub_name;
3666
3667       len = sprintf (stub_name, "%08x.%x:%x+%x",
3668                      input_section->id & 0xffffffff,
3669                      sym_sec->id & 0xffffffff,
3670                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3671                      (int) rel->r_addend & 0xffffffff);
3672     }
3673   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3674     stub_name[len - 2] = 0;
3675   return stub_name;
3676 }
3677
3678 /* Look up an entry in the stub hash.  Stub entries are cached because
3679    creating the stub name takes a bit of time.  */
3680
3681 static struct ppc_stub_hash_entry *
3682 ppc_get_stub_entry (const asection *input_section,
3683                     const asection *sym_sec,
3684                     struct ppc_link_hash_entry *h,
3685                     const Elf_Internal_Rela *rel,
3686                     struct ppc_link_hash_table *htab)
3687 {
3688   struct ppc_stub_hash_entry *stub_entry;
3689   struct map_stub *group;
3690
3691   /* If this input section is part of a group of sections sharing one
3692      stub section, then use the id of the first section in the group.
3693      Stub names need to include a section id, as there may well be
3694      more than one stub used to reach say, printf, and we need to
3695      distinguish between them.  */
3696   group = htab->sec_info[input_section->id].u.group;
3697   if (group == NULL)
3698     return NULL;
3699
3700   if (h != NULL && h->u.stub_cache != NULL
3701       && h->u.stub_cache->h == h
3702       && h->u.stub_cache->group == group)
3703     {
3704       stub_entry = h->u.stub_cache;
3705     }
3706   else
3707     {
3708       char *stub_name;
3709
3710       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3711       if (stub_name == NULL)
3712         return NULL;
3713
3714       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3715                                          stub_name, FALSE, FALSE);
3716       if (h != NULL)
3717         h->u.stub_cache = stub_entry;
3718
3719       free (stub_name);
3720     }
3721
3722   return stub_entry;
3723 }
3724
3725 /* Add a new stub entry to the stub hash.  Not all fields of the new
3726    stub entry are initialised.  */
3727
3728 static struct ppc_stub_hash_entry *
3729 ppc_add_stub (const char *stub_name,
3730               asection *section,
3731               struct bfd_link_info *info)
3732 {
3733   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3734   struct map_stub *group;
3735   asection *link_sec;
3736   asection *stub_sec;
3737   struct ppc_stub_hash_entry *stub_entry;
3738
3739   group = htab->sec_info[section->id].u.group;
3740   link_sec = group->link_sec;
3741   stub_sec = group->stub_sec;
3742   if (stub_sec == NULL)
3743     {
3744       size_t namelen;
3745       bfd_size_type len;
3746       char *s_name;
3747
3748       namelen = strlen (link_sec->name);
3749       len = namelen + sizeof (STUB_SUFFIX);
3750       s_name = bfd_alloc (htab->params->stub_bfd, len);
3751       if (s_name == NULL)
3752         return NULL;
3753
3754       memcpy (s_name, link_sec->name, namelen);
3755       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3756       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3757       if (stub_sec == NULL)
3758         return NULL;
3759       group->stub_sec = stub_sec;
3760     }
3761
3762   /* Enter this entry into the linker stub hash table.  */
3763   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3764                                      TRUE, FALSE);
3765   if (stub_entry == NULL)
3766     {
3767       /* xgettext:c-format */
3768       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3769                           section->owner, stub_name);
3770       return NULL;
3771     }
3772
3773   stub_entry->group = group;
3774   stub_entry->stub_offset = 0;
3775   return stub_entry;
3776 }
3777
3778 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3779    not already done.  */
3780
3781 static bfd_boolean
3782 create_got_section (bfd *abfd, struct bfd_link_info *info)
3783 {
3784   asection *got, *relgot;
3785   flagword flags;
3786   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3787
3788   if (!is_ppc64_elf (abfd))
3789     return FALSE;
3790   if (htab == NULL)
3791     return FALSE;
3792
3793   if (!htab->elf.sgot
3794       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3795     return FALSE;
3796
3797   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3798            | SEC_LINKER_CREATED);
3799
3800   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3801   if (!got
3802       || !bfd_set_section_alignment (got, 3))
3803     return FALSE;
3804
3805   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3806                                                flags | SEC_READONLY);
3807   if (!relgot
3808       || !bfd_set_section_alignment (relgot, 3))
3809     return FALSE;
3810
3811   ppc64_elf_tdata (abfd)->got = got;
3812   ppc64_elf_tdata (abfd)->relgot = relgot;
3813   return TRUE;
3814 }
3815
3816 /* Follow indirect and warning symbol links.  */
3817
3818 static inline struct bfd_link_hash_entry *
3819 follow_link (struct bfd_link_hash_entry *h)
3820 {
3821   while (h->type == bfd_link_hash_indirect
3822          || h->type == bfd_link_hash_warning)
3823     h = h->u.i.link;
3824   return h;
3825 }
3826
3827 static inline struct elf_link_hash_entry *
3828 elf_follow_link (struct elf_link_hash_entry *h)
3829 {
3830   return (struct elf_link_hash_entry *) follow_link (&h->root);
3831 }
3832
3833 static inline struct ppc_link_hash_entry *
3834 ppc_follow_link (struct ppc_link_hash_entry *h)
3835 {
3836   return ppc_elf_hash_entry (elf_follow_link (&h->elf));
3837 }
3838
3839 /* Merge PLT info on FROM with that on TO.  */
3840
3841 static void
3842 move_plt_plist (struct ppc_link_hash_entry *from,
3843                 struct ppc_link_hash_entry *to)
3844 {
3845   if (from->elf.plt.plist != NULL)
3846     {
3847       if (to->elf.plt.plist != NULL)
3848         {
3849           struct plt_entry **entp;
3850           struct plt_entry *ent;
3851
3852           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3853             {
3854               struct plt_entry *dent;
3855
3856               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3857                 if (dent->addend == ent->addend)
3858                   {
3859                     dent->plt.refcount += ent->plt.refcount;
3860                     *entp = ent->next;
3861                     break;
3862                   }
3863               if (dent == NULL)
3864                 entp = &ent->next;
3865             }
3866           *entp = to->elf.plt.plist;
3867         }
3868
3869       to->elf.plt.plist = from->elf.plt.plist;
3870       from->elf.plt.plist = NULL;
3871     }
3872 }
3873
3874 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3875
3876 static void
3877 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3878                                 struct elf_link_hash_entry *dir,
3879                                 struct elf_link_hash_entry *ind)
3880 {
3881   struct ppc_link_hash_entry *edir, *eind;
3882
3883   edir = ppc_elf_hash_entry (dir);
3884   eind = ppc_elf_hash_entry (ind);
3885
3886   edir->is_func |= eind->is_func;
3887   edir->is_func_descriptor |= eind->is_func_descriptor;
3888   edir->tls_mask |= eind->tls_mask;
3889   if (eind->oh != NULL)
3890     edir->oh = ppc_follow_link (eind->oh);
3891
3892   if (edir->elf.versioned != versioned_hidden)
3893     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3894   edir->elf.ref_regular |= eind->elf.ref_regular;
3895   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3896   edir->elf.non_got_ref |= eind->elf.non_got_ref;
3897   edir->elf.needs_plt |= eind->elf.needs_plt;
3898   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3899
3900   /* If we were called to copy over info for a weak sym, don't copy
3901      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
3902      in order to simplify readonly_dynrelocs and save a field in the
3903      symbol hash entry, but that means dyn_relocs can't be used in any
3904      tests about a specific symbol, or affect other symbol flags which
3905      are then tested.  */
3906   if (eind->elf.root.type != bfd_link_hash_indirect)
3907     return;
3908
3909   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3910   if (eind->dyn_relocs != NULL)
3911     {
3912       if (edir->dyn_relocs != NULL)
3913         {
3914           struct elf_dyn_relocs **pp;
3915           struct elf_dyn_relocs *p;
3916
3917           /* Add reloc counts against the indirect sym to the direct sym
3918              list.  Merge any entries against the same section.  */
3919           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3920             {
3921               struct elf_dyn_relocs *q;
3922
3923               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3924                 if (q->sec == p->sec)
3925                   {
3926                     q->pc_count += p->pc_count;
3927                     q->count += p->count;
3928                     *pp = p->next;
3929                     break;
3930                   }
3931               if (q == NULL)
3932                 pp = &p->next;
3933             }
3934           *pp = edir->dyn_relocs;
3935         }
3936
3937       edir->dyn_relocs = eind->dyn_relocs;
3938       eind->dyn_relocs = NULL;
3939     }
3940
3941   /* Copy over got entries that we may have already seen to the
3942      symbol which just became indirect.  */
3943   if (eind->elf.got.glist != NULL)
3944     {
3945       if (edir->elf.got.glist != NULL)
3946         {
3947           struct got_entry **entp;
3948           struct got_entry *ent;
3949
3950           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3951             {
3952               struct got_entry *dent;
3953
3954               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3955                 if (dent->addend == ent->addend
3956                     && dent->owner == ent->owner
3957                     && dent->tls_type == ent->tls_type)
3958                   {
3959                     dent->got.refcount += ent->got.refcount;
3960                     *entp = ent->next;
3961                     break;
3962                   }
3963               if (dent == NULL)
3964                 entp = &ent->next;
3965             }
3966           *entp = edir->elf.got.glist;
3967         }
3968
3969       edir->elf.got.glist = eind->elf.got.glist;
3970       eind->elf.got.glist = NULL;
3971     }
3972
3973   /* And plt entries.  */
3974   move_plt_plist (eind, edir);
3975
3976   if (eind->elf.dynindx != -1)
3977     {
3978       if (edir->elf.dynindx != -1)
3979         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3980                                 edir->elf.dynstr_index);
3981       edir->elf.dynindx = eind->elf.dynindx;
3982       edir->elf.dynstr_index = eind->elf.dynstr_index;
3983       eind->elf.dynindx = -1;
3984       eind->elf.dynstr_index = 0;
3985     }
3986 }
3987
3988 /* Find the function descriptor hash entry from the given function code
3989    hash entry FH.  Link the entries via their OH fields.  */
3990
3991 static struct ppc_link_hash_entry *
3992 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3993 {
3994   struct ppc_link_hash_entry *fdh = fh->oh;
3995
3996   if (fdh == NULL)
3997     {
3998       const char *fd_name = fh->elf.root.root.string + 1;
3999
4000       fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4001                                                       FALSE, FALSE, FALSE));
4002       if (fdh == NULL)
4003         return fdh;
4004
4005       fdh->is_func_descriptor = 1;
4006       fdh->oh = fh;
4007       fh->is_func = 1;
4008       fh->oh = fdh;
4009     }
4010
4011   fdh = ppc_follow_link (fdh);
4012   fdh->is_func_descriptor = 1;
4013   fdh->oh = fh;
4014   return fdh;
4015 }
4016
4017 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4018
4019 static struct ppc_link_hash_entry *
4020 make_fdh (struct bfd_link_info *info,
4021           struct ppc_link_hash_entry *fh)
4022 {
4023   bfd *abfd = fh->elf.root.u.undef.abfd;
4024   struct bfd_link_hash_entry *bh = NULL;
4025   struct ppc_link_hash_entry *fdh;
4026   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4027                     ? BSF_WEAK
4028                     : BSF_GLOBAL);
4029
4030   if (!_bfd_generic_link_add_one_symbol (info, abfd,
4031                                          fh->elf.root.root.string + 1,
4032                                          flags, bfd_und_section_ptr, 0,
4033                                          NULL, FALSE, FALSE, &bh))
4034     return NULL;
4035
4036   fdh = (struct ppc_link_hash_entry *) bh;
4037   fdh->elf.non_elf = 0;
4038   fdh->fake = 1;
4039   fdh->is_func_descriptor = 1;
4040   fdh->oh = fh;
4041   fh->is_func = 1;
4042   fh->oh = fdh;
4043   return fdh;
4044 }
4045
4046 /* Fix function descriptor symbols defined in .opd sections to be
4047    function type.  */
4048
4049 static bfd_boolean
4050 ppc64_elf_add_symbol_hook (bfd *ibfd,
4051                            struct bfd_link_info *info,
4052                            Elf_Internal_Sym *isym,
4053                            const char **name,
4054                            flagword *flags ATTRIBUTE_UNUSED,
4055                            asection **sec,
4056                            bfd_vma *value)
4057 {
4058   if (*sec != NULL
4059       && strcmp ((*sec)->name, ".opd") == 0)
4060     {
4061       asection *code_sec;
4062
4063       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4064             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4065         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4066
4067       /* If the symbol is a function defined in .opd, and the function
4068          code is in a discarded group, let it appear to be undefined.  */
4069       if (!bfd_link_relocatable (info)
4070           && (*sec)->reloc_count != 0
4071           && opd_entry_value (*sec, *value, &code_sec, NULL,
4072                               FALSE) != (bfd_vma) -1
4073           && discarded_section (code_sec))
4074         {
4075           *sec = bfd_und_section_ptr;
4076           isym->st_shndx = SHN_UNDEF;
4077         }
4078     }
4079   else if (*sec != NULL
4080            && strcmp ((*sec)->name, ".toc") == 0
4081            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4082     {
4083       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4084       if (htab != NULL)
4085         htab->params->object_in_toc = 1;
4086     }
4087
4088   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4089     {
4090       if (abiversion (ibfd) == 0)
4091         set_abiversion (ibfd, 2);
4092       else if (abiversion (ibfd) == 1)
4093         {
4094           _bfd_error_handler (_("symbol '%s' has invalid st_other"
4095                                 " for ABI version 1"), *name);
4096           bfd_set_error (bfd_error_bad_value);
4097           return FALSE;
4098         }
4099     }
4100
4101   return TRUE;
4102 }
4103
4104 /* Merge non-visibility st_other attributes: local entry point.  */
4105
4106 static void
4107 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4108                                   const Elf_Internal_Sym *isym,
4109                                   bfd_boolean definition,
4110                                   bfd_boolean dynamic)
4111 {
4112   if (definition && (!dynamic || !h->def_regular))
4113     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4114                 | ELF_ST_VISIBILITY (h->other));
4115 }
4116
4117 /* Hook called on merging a symbol.  We use this to clear "fake" since
4118    we now have a real symbol.  */
4119
4120 static bfd_boolean
4121 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4122                         const Elf_Internal_Sym *isym,
4123                         asection **psec ATTRIBUTE_UNUSED,
4124                         bfd_boolean newdef ATTRIBUTE_UNUSED,
4125                         bfd_boolean olddef ATTRIBUTE_UNUSED,
4126                         bfd *oldbfd ATTRIBUTE_UNUSED,
4127                         const asection *oldsec ATTRIBUTE_UNUSED)
4128 {
4129   ppc_elf_hash_entry (h)->fake = 0;
4130   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4131     ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4132   return TRUE;
4133 }
4134
4135 /* This function makes an old ABI object reference to ".bar" cause the
4136    inclusion of a new ABI object archive that defines "bar".
4137    NAME is a symbol defined in an archive.  Return a symbol in the hash
4138    table that might be satisfied by the archive symbols.  */
4139
4140 static struct elf_link_hash_entry *
4141 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4142                                  struct bfd_link_info *info,
4143                                  const char *name)
4144 {
4145   struct elf_link_hash_entry *h;
4146   char *dot_name;
4147   size_t len;
4148
4149   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4150   if (h != NULL
4151       /* Don't return this sym if it is a fake function descriptor
4152          created by add_symbol_adjust.  */
4153       && !ppc_elf_hash_entry (h)->fake)
4154     return h;
4155
4156   if (name[0] == '.')
4157     return h;
4158
4159   len = strlen (name);
4160   dot_name = bfd_alloc (abfd, len + 2);
4161   if (dot_name == NULL)
4162     return (struct elf_link_hash_entry *) -1;
4163   dot_name[0] = '.';
4164   memcpy (dot_name + 1, name, len + 1);
4165   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4166   bfd_release (abfd, dot_name);
4167   if (h != NULL)
4168     return h;
4169
4170   if (strcmp (name, "__tls_get_addr_opt") == 0)
4171     h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4172   return h;
4173 }
4174
4175 /* This function satisfies all old ABI object references to ".bar" if a
4176    new ABI object defines "bar".  Well, at least, undefined dot symbols
4177    are made weak.  This stops later archive searches from including an
4178    object if we already have a function descriptor definition.  It also
4179    prevents the linker complaining about undefined symbols.
4180    We also check and correct mismatched symbol visibility here.  The
4181    most restrictive visibility of the function descriptor and the
4182    function entry symbol is used.  */
4183
4184 static bfd_boolean
4185 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4186 {
4187   struct ppc_link_hash_table *htab;
4188   struct ppc_link_hash_entry *fdh;
4189
4190   if (eh->elf.root.type == bfd_link_hash_warning)
4191     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4192
4193   if (eh->elf.root.type == bfd_link_hash_indirect)
4194     return TRUE;
4195
4196   if (eh->elf.root.root.string[0] != '.')
4197     abort ();
4198
4199   htab = ppc_hash_table (info);
4200   if (htab == NULL)
4201     return FALSE;
4202
4203   fdh = lookup_fdh (eh, htab);
4204   if (fdh == NULL
4205       && !bfd_link_relocatable (info)
4206       && (eh->elf.root.type == bfd_link_hash_undefined
4207           || eh->elf.root.type == bfd_link_hash_undefweak)
4208       && eh->elf.ref_regular)
4209     {
4210       /* Make an undefined function descriptor sym, in order to
4211          pull in an --as-needed shared lib.  Archives are handled
4212          elsewhere.  */
4213       fdh = make_fdh (info, eh);
4214       if (fdh == NULL)
4215         return FALSE;
4216     }
4217
4218   if (fdh != NULL)
4219     {
4220       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4221       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4222
4223       /* Make both descriptor and entry symbol have the most
4224          constraining visibility of either symbol.  */
4225       if (entry_vis < descr_vis)
4226         fdh->elf.other += entry_vis - descr_vis;
4227       else if (entry_vis > descr_vis)
4228         eh->elf.other += descr_vis - entry_vis;
4229
4230       /* Propagate reference flags from entry symbol to function
4231          descriptor symbol.  */
4232       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4233       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4234       fdh->elf.ref_regular |= eh->elf.ref_regular;
4235       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4236
4237       if (!fdh->elf.forced_local
4238           && fdh->elf.dynindx == -1
4239           && fdh->elf.versioned != versioned_hidden
4240           && (bfd_link_dll (info)
4241               || fdh->elf.def_dynamic
4242               || fdh->elf.ref_dynamic)
4243           && (eh->elf.ref_regular
4244               || eh->elf.def_regular))
4245         {
4246           if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4247             return FALSE;
4248         }
4249     }
4250
4251   return TRUE;
4252 }
4253
4254 /* Set up opd section info and abiversion for IBFD, and process list
4255    of dot-symbols we made in link_hash_newfunc.  */
4256
4257 static bfd_boolean
4258 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4259 {
4260   struct ppc_link_hash_table *htab;
4261   struct ppc_link_hash_entry **p, *eh;
4262   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4263
4264   if (opd != NULL && opd->size != 0)
4265     {
4266       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4267       ppc64_elf_section_data (opd)->sec_type = sec_opd;
4268
4269       if (abiversion (ibfd) == 0)
4270         set_abiversion (ibfd, 1);
4271       else if (abiversion (ibfd) >= 2)
4272         {
4273           /* xgettext:c-format */
4274           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4275                               ibfd, abiversion (ibfd));
4276           bfd_set_error (bfd_error_bad_value);
4277           return FALSE;
4278         }
4279     }
4280
4281   if (is_ppc64_elf (info->output_bfd))
4282     {
4283       /* For input files without an explicit abiversion in e_flags
4284          we should have flagged any with symbol st_other bits set
4285          as ELFv1 and above flagged those with .opd as ELFv2.
4286          Set the output abiversion if not yet set, and for any input
4287          still ambiguous, take its abiversion from the output.
4288          Differences in ABI are reported later.  */
4289       if (abiversion (info->output_bfd) == 0)
4290         set_abiversion (info->output_bfd, abiversion (ibfd));
4291       else if (abiversion (ibfd) == 0)
4292         set_abiversion (ibfd, abiversion (info->output_bfd));
4293     }
4294
4295   htab = ppc_hash_table (info);
4296   if (htab == NULL)
4297     return TRUE;
4298
4299   if (opd != NULL && opd->size != 0
4300       && (ibfd->flags & DYNAMIC) == 0
4301       && (opd->flags & SEC_RELOC) != 0
4302       && opd->reloc_count != 0
4303       && !bfd_is_abs_section (opd->output_section)
4304       && info->gc_sections)
4305     {
4306       /* Garbage collection needs some extra help with .opd sections.
4307          We don't want to necessarily keep everything referenced by
4308          relocs in .opd, as that would keep all functions.  Instead,
4309          if we reference an .opd symbol (a function descriptor), we
4310          want to keep the function code symbol's section.  This is
4311          easy for global symbols, but for local syms we need to keep
4312          information about the associated function section.  */
4313       bfd_size_type amt;
4314       asection **opd_sym_map;
4315       Elf_Internal_Shdr *symtab_hdr;
4316       Elf_Internal_Rela *relocs, *rel_end, *rel;
4317
4318       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4319       opd_sym_map = bfd_zalloc (ibfd, amt);
4320       if (opd_sym_map == NULL)
4321         return FALSE;
4322       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4323       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4324                                           info->keep_memory);
4325       if (relocs == NULL)
4326         return FALSE;
4327       symtab_hdr = &elf_symtab_hdr (ibfd);
4328       rel_end = relocs + opd->reloc_count - 1;
4329       for (rel = relocs; rel < rel_end; rel++)
4330         {
4331           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4332           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4333
4334           if (r_type == R_PPC64_ADDR64
4335               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4336               && r_symndx < symtab_hdr->sh_info)
4337             {
4338               Elf_Internal_Sym *isym;
4339               asection *s;
4340
4341               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4342               if (isym == NULL)
4343                 {
4344                   if (elf_section_data (opd)->relocs != relocs)
4345                     free (relocs);
4346                   return FALSE;
4347                 }
4348
4349               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4350               if (s != NULL && s != opd)
4351                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4352             }
4353         }
4354       if (elf_section_data (opd)->relocs != relocs)
4355         free (relocs);
4356     }
4357
4358   p = &htab->dot_syms;
4359   while ((eh = *p) != NULL)
4360     {
4361       *p = NULL;
4362       if (&eh->elf == htab->elf.hgot)
4363         ;
4364       else if (htab->elf.hgot == NULL
4365                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4366         htab->elf.hgot = &eh->elf;
4367       else if (abiversion (ibfd) <= 1)
4368         {
4369           htab->need_func_desc_adj = 1;
4370           if (!add_symbol_adjust (eh, info))
4371             return FALSE;
4372         }
4373       p = &eh->u.next_dot_sym;
4374     }
4375   return TRUE;
4376 }
4377
4378 /* Undo hash table changes when an --as-needed input file is determined
4379    not to be needed.  */
4380
4381 static bfd_boolean
4382 ppc64_elf_notice_as_needed (bfd *ibfd,
4383                             struct bfd_link_info *info,
4384                             enum notice_asneeded_action act)
4385 {
4386   if (act == notice_not_needed)
4387     {
4388       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4389
4390       if (htab == NULL)
4391         return FALSE;
4392
4393       htab->dot_syms = NULL;
4394     }
4395   return _bfd_elf_notice_as_needed (ibfd, info, act);
4396 }
4397
4398 /* If --just-symbols against a final linked binary, then assume we need
4399    toc adjusting stubs when calling functions defined there.  */
4400
4401 static void
4402 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4403 {
4404   if ((sec->flags & SEC_CODE) != 0
4405       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4406       && is_ppc64_elf (sec->owner))
4407     {
4408       if (abiversion (sec->owner) >= 2
4409           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4410         sec->has_toc_reloc = 1;
4411     }
4412   _bfd_elf_link_just_syms (sec, info);
4413 }
4414
4415 static struct plt_entry **
4416 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4417                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4418 {
4419   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4420   struct plt_entry **local_plt;
4421   unsigned char *local_got_tls_masks;
4422
4423   if (local_got_ents == NULL)
4424     {
4425       bfd_size_type size = symtab_hdr->sh_info;
4426
4427       size *= (sizeof (*local_got_ents)
4428                + sizeof (*local_plt)
4429                + sizeof (*local_got_tls_masks));
4430       local_got_ents = bfd_zalloc (abfd, size);
4431       if (local_got_ents == NULL)
4432         return NULL;
4433       elf_local_got_ents (abfd) = local_got_ents;
4434     }
4435
4436   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4437     {
4438       struct got_entry *ent;
4439
4440       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4441         if (ent->addend == r_addend
4442             && ent->owner == abfd
4443             && ent->tls_type == tls_type)
4444           break;
4445       if (ent == NULL)
4446         {
4447           size_t amt = sizeof (*ent);
4448           ent = bfd_alloc (abfd, amt);
4449           if (ent == NULL)
4450             return FALSE;
4451           ent->next = local_got_ents[r_symndx];
4452           ent->addend = r_addend;
4453           ent->owner = abfd;
4454           ent->tls_type = tls_type;
4455           ent->is_indirect = FALSE;
4456           ent->got.refcount = 0;
4457           local_got_ents[r_symndx] = ent;
4458         }
4459       ent->got.refcount += 1;
4460     }
4461
4462   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4463   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4464   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4465
4466   return local_plt + r_symndx;
4467 }
4468
4469 static bfd_boolean
4470 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4471 {
4472   struct plt_entry *ent;
4473
4474   for (ent = *plist; ent != NULL; ent = ent->next)
4475     if (ent->addend == addend)
4476       break;
4477   if (ent == NULL)
4478     {
4479       size_t amt = sizeof (*ent);
4480       ent = bfd_alloc (abfd, amt);
4481       if (ent == NULL)
4482         return FALSE;
4483       ent->next = *plist;
4484       ent->addend = addend;
4485       ent->plt.refcount = 0;
4486       *plist = ent;
4487     }
4488   ent->plt.refcount += 1;
4489   return TRUE;
4490 }
4491
4492 static bfd_boolean
4493 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4494 {
4495   return (r_type == R_PPC64_REL24
4496           || r_type == R_PPC64_REL24_NOTOC
4497           || r_type == R_PPC64_REL14
4498           || r_type == R_PPC64_REL14_BRTAKEN
4499           || r_type == R_PPC64_REL14_BRNTAKEN
4500           || r_type == R_PPC64_ADDR24
4501           || r_type == R_PPC64_ADDR14
4502           || r_type == R_PPC64_ADDR14_BRTAKEN
4503           || r_type == R_PPC64_ADDR14_BRNTAKEN
4504           || r_type == R_PPC64_PLTCALL
4505           || r_type == R_PPC64_PLTCALL_NOTOC);
4506 }
4507
4508 /* Relocs on inline plt call sequence insns prior to the call.  */
4509
4510 static bfd_boolean
4511 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4512 {
4513   return (r_type == R_PPC64_PLT16_HA
4514           || r_type == R_PPC64_PLT16_HI
4515           || r_type == R_PPC64_PLT16_LO
4516           || r_type == R_PPC64_PLT16_LO_DS
4517           || r_type == R_PPC64_PLT_PCREL34
4518           || r_type == R_PPC64_PLT_PCREL34_NOTOC
4519           || r_type == R_PPC64_PLTSEQ
4520           || r_type == R_PPC64_PLTSEQ_NOTOC);
4521 }
4522
4523 /* Look through the relocs for a section during the first phase, and
4524    calculate needed space in the global offset table, procedure
4525    linkage table, and dynamic reloc sections.  */
4526
4527 static bfd_boolean
4528 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4529                         asection *sec, const Elf_Internal_Rela *relocs)
4530 {
4531   struct ppc_link_hash_table *htab;
4532   Elf_Internal_Shdr *symtab_hdr;
4533   struct elf_link_hash_entry **sym_hashes;
4534   const Elf_Internal_Rela *rel;
4535   const Elf_Internal_Rela *rel_end;
4536   asection *sreloc;
4537   struct elf_link_hash_entry *tga, *dottga;
4538   bfd_boolean is_opd;
4539
4540   if (bfd_link_relocatable (info))
4541     return TRUE;
4542
4543   /* Don't do anything special with non-loaded, non-alloced sections.
4544      In particular, any relocs in such sections should not affect GOT
4545      and PLT reference counting (ie. we don't allow them to create GOT
4546      or PLT entries), there's no possibility or desire to optimize TLS
4547      relocs, and there's not much point in propagating relocs to shared
4548      libs that the dynamic linker won't relocate.  */
4549   if ((sec->flags & SEC_ALLOC) == 0)
4550     return TRUE;
4551
4552   BFD_ASSERT (is_ppc64_elf (abfd));
4553
4554   htab = ppc_hash_table (info);
4555   if (htab == NULL)
4556     return FALSE;
4557
4558   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4559                               FALSE, FALSE, TRUE);
4560   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4561                                  FALSE, FALSE, TRUE);
4562   symtab_hdr = &elf_symtab_hdr (abfd);
4563   sym_hashes = elf_sym_hashes (abfd);
4564   sreloc = NULL;
4565   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4566   rel_end = relocs + sec->reloc_count;
4567   for (rel = relocs; rel < rel_end; rel++)
4568     {
4569       unsigned long r_symndx;
4570       struct elf_link_hash_entry *h;
4571       enum elf_ppc64_reloc_type r_type;
4572       int tls_type;
4573       struct _ppc64_elf_section_data *ppc64_sec;
4574       struct plt_entry **ifunc, **plt_list;
4575
4576       r_symndx = ELF64_R_SYM (rel->r_info);
4577       if (r_symndx < symtab_hdr->sh_info)
4578         h = NULL;
4579       else
4580         {
4581           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4582           h = elf_follow_link (h);
4583
4584           if (h == htab->elf.hgot)
4585             sec->has_toc_reloc = 1;
4586         }
4587
4588       r_type = ELF64_R_TYPE (rel->r_info);
4589       switch (r_type)
4590         {
4591         case R_PPC64_D34:
4592         case R_PPC64_D34_LO:
4593         case R_PPC64_D34_HI30:
4594         case R_PPC64_D34_HA30:
4595         case R_PPC64_D28:
4596         case R_PPC64_TPREL34:
4597         case R_PPC64_DTPREL34:
4598         case R_PPC64_PCREL34:
4599         case R_PPC64_GOT_PCREL34:
4600         case R_PPC64_GOT_TLSGD34:
4601         case R_PPC64_GOT_TLSLD34:
4602         case R_PPC64_GOT_TPREL34:
4603         case R_PPC64_GOT_DTPREL34:
4604         case R_PPC64_PLT_PCREL34:
4605         case R_PPC64_PLT_PCREL34_NOTOC:
4606         case R_PPC64_PCREL28:
4607           htab->powerxx_stubs = 1;
4608           break;
4609         default:
4610           break;
4611         }
4612
4613       switch (r_type)
4614         {
4615         case R_PPC64_PLT16_HA:
4616         case R_PPC64_GOT_TLSLD16_HA:
4617         case R_PPC64_GOT_TLSGD16_HA:
4618         case R_PPC64_GOT_TPREL16_HA:
4619         case R_PPC64_GOT_DTPREL16_HA:
4620         case R_PPC64_GOT16_HA:
4621         case R_PPC64_TOC16_HA:
4622         case R_PPC64_PLT16_LO:
4623         case R_PPC64_PLT16_LO_DS:
4624         case R_PPC64_GOT_TLSLD16_LO:
4625         case R_PPC64_GOT_TLSGD16_LO:
4626         case R_PPC64_GOT_TPREL16_LO_DS:
4627         case R_PPC64_GOT_DTPREL16_LO_DS:
4628         case R_PPC64_GOT16_LO:
4629         case R_PPC64_GOT16_LO_DS:
4630         case R_PPC64_TOC16_LO:
4631         case R_PPC64_TOC16_LO_DS:
4632         case R_PPC64_GOT_PCREL34:
4633           ppc64_elf_tdata (abfd)->has_optrel = 1;
4634           ppc64_elf_section_data (sec)->has_optrel = 1;
4635           break;
4636         default:
4637           break;
4638         }
4639
4640       ifunc = NULL;
4641       if (h != NULL)
4642         {
4643           if (h->type == STT_GNU_IFUNC)
4644             {
4645               h->needs_plt = 1;
4646               ifunc = &h->plt.plist;
4647             }
4648         }
4649       else
4650         {
4651           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4652                                                           abfd, r_symndx);
4653           if (isym == NULL)
4654             return FALSE;
4655
4656           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4657             {
4658               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4659                                              rel->r_addend,
4660                                              NON_GOT | PLT_IFUNC);
4661               if (ifunc == NULL)
4662                 return FALSE;
4663             }
4664         }
4665
4666       tls_type = 0;
4667       switch (r_type)
4668         {
4669         case R_PPC64_TLSGD:
4670         case R_PPC64_TLSLD:
4671           /* These special tls relocs tie a call to __tls_get_addr with
4672              its parameter symbol.  */
4673           if (h != NULL)
4674             ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4675           else
4676             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4677                                         rel->r_addend,
4678                                         NON_GOT | TLS_TLS | TLS_MARK))
4679               return FALSE;
4680           sec->has_tls_reloc = 1;
4681           break;
4682
4683         case R_PPC64_GOT_TLSLD16:
4684         case R_PPC64_GOT_TLSLD16_LO:
4685         case R_PPC64_GOT_TLSLD16_HI:
4686         case R_PPC64_GOT_TLSLD16_HA:
4687         case R_PPC64_GOT_TLSLD34:
4688           tls_type = TLS_TLS | TLS_LD;
4689           goto dogottls;
4690
4691         case R_PPC64_GOT_TLSGD16:
4692         case R_PPC64_GOT_TLSGD16_LO:
4693         case R_PPC64_GOT_TLSGD16_HI:
4694         case R_PPC64_GOT_TLSGD16_HA:
4695         case R_PPC64_GOT_TLSGD34:
4696           tls_type = TLS_TLS | TLS_GD;
4697           goto dogottls;
4698
4699         case R_PPC64_GOT_TPREL16_DS:
4700         case R_PPC64_GOT_TPREL16_LO_DS:
4701         case R_PPC64_GOT_TPREL16_HI:
4702         case R_PPC64_GOT_TPREL16_HA:
4703         case R_PPC64_GOT_TPREL34:
4704           if (bfd_link_dll (info))
4705             info->flags |= DF_STATIC_TLS;
4706           tls_type = TLS_TLS | TLS_TPREL;
4707           goto dogottls;
4708
4709         case R_PPC64_GOT_DTPREL16_DS:
4710         case R_PPC64_GOT_DTPREL16_LO_DS:
4711         case R_PPC64_GOT_DTPREL16_HI:
4712         case R_PPC64_GOT_DTPREL16_HA:
4713         case R_PPC64_GOT_DTPREL34:
4714           tls_type = TLS_TLS | TLS_DTPREL;
4715         dogottls:
4716           sec->has_tls_reloc = 1;
4717           goto dogot;
4718
4719         case R_PPC64_GOT16:
4720         case R_PPC64_GOT16_LO:
4721         case R_PPC64_GOT16_HI:
4722         case R_PPC64_GOT16_HA:
4723         case R_PPC64_GOT16_DS:
4724         case R_PPC64_GOT16_LO_DS:
4725         case R_PPC64_GOT_PCREL34:
4726         dogot:
4727           /* This symbol requires a global offset table entry.  */
4728           sec->has_toc_reloc = 1;
4729           if (r_type == R_PPC64_GOT_TLSLD16
4730               || r_type == R_PPC64_GOT_TLSGD16
4731               || r_type == R_PPC64_GOT_TPREL16_DS
4732               || r_type == R_PPC64_GOT_DTPREL16_DS
4733               || r_type == R_PPC64_GOT16
4734               || r_type == R_PPC64_GOT16_DS)
4735             {
4736               htab->do_multi_toc = 1;
4737               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4738             }
4739
4740           if (ppc64_elf_tdata (abfd)->got == NULL
4741               && !create_got_section (abfd, info))
4742             return FALSE;
4743
4744           if (h != NULL)
4745             {
4746               struct ppc_link_hash_entry *eh;
4747               struct got_entry *ent;
4748
4749               eh = ppc_elf_hash_entry (h);
4750               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4751                 if (ent->addend == rel->r_addend
4752                     && ent->owner == abfd
4753                     && ent->tls_type == tls_type)
4754                   break;
4755               if (ent == NULL)
4756                 {
4757                   size_t amt = sizeof (*ent);
4758                   ent = bfd_alloc (abfd, amt);
4759                   if (ent == NULL)
4760                     return FALSE;
4761                   ent->next = eh->elf.got.glist;
4762                   ent->addend = rel->r_addend;
4763                   ent->owner = abfd;
4764                   ent->tls_type = tls_type;
4765                   ent->is_indirect = FALSE;
4766                   ent->got.refcount = 0;
4767                   eh->elf.got.glist = ent;
4768                 }
4769               ent->got.refcount += 1;
4770               eh->tls_mask |= tls_type;
4771             }
4772           else
4773             /* This is a global offset table entry for a local symbol.  */
4774             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4775                                         rel->r_addend, tls_type))
4776               return FALSE;
4777
4778           /* We may also need a plt entry if the symbol turns out to be
4779              an ifunc.  */
4780           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4781             {
4782               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4783                 return FALSE;
4784             }
4785           break;
4786
4787         case R_PPC64_PLT16_HA:
4788         case R_PPC64_PLT16_HI:
4789         case R_PPC64_PLT16_LO:
4790         case R_PPC64_PLT16_LO_DS:
4791         case R_PPC64_PLT_PCREL34:
4792         case R_PPC64_PLT_PCREL34_NOTOC:
4793         case R_PPC64_PLT32:
4794         case R_PPC64_PLT64:
4795           /* This symbol requires a procedure linkage table entry.  */
4796           plt_list = ifunc;
4797           if (h != NULL)
4798             {
4799               h->needs_plt = 1;
4800               if (h->root.root.string[0] == '.'
4801                   && h->root.root.string[1] != '\0')
4802                 ppc_elf_hash_entry (h)->is_func = 1;
4803               ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
4804               plt_list = &h->plt.plist;
4805             }
4806           if (plt_list == NULL)
4807             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4808                                               rel->r_addend,
4809                                               NON_GOT | PLT_KEEP);
4810           if (!update_plt_info (abfd, plt_list, rel->r_addend))
4811             return FALSE;
4812           break;
4813
4814           /* The following relocations don't need to propagate the
4815              relocation if linking a shared object since they are
4816              section relative.  */
4817         case R_PPC64_SECTOFF:
4818         case R_PPC64_SECTOFF_LO:
4819         case R_PPC64_SECTOFF_HI:
4820         case R_PPC64_SECTOFF_HA:
4821         case R_PPC64_SECTOFF_DS:
4822         case R_PPC64_SECTOFF_LO_DS:
4823         case R_PPC64_DTPREL16:
4824         case R_PPC64_DTPREL16_LO:
4825         case R_PPC64_DTPREL16_HI:
4826         case R_PPC64_DTPREL16_HA:
4827         case R_PPC64_DTPREL16_DS:
4828         case R_PPC64_DTPREL16_LO_DS:
4829         case R_PPC64_DTPREL16_HIGH:
4830         case R_PPC64_DTPREL16_HIGHA:
4831         case R_PPC64_DTPREL16_HIGHER:
4832         case R_PPC64_DTPREL16_HIGHERA:
4833         case R_PPC64_DTPREL16_HIGHEST:
4834         case R_PPC64_DTPREL16_HIGHESTA:
4835           break;
4836
4837           /* Nor do these.  */
4838         case R_PPC64_REL16:
4839         case R_PPC64_REL16_LO:
4840         case R_PPC64_REL16_HI:
4841         case R_PPC64_REL16_HA:
4842         case R_PPC64_REL16_HIGH:
4843         case R_PPC64_REL16_HIGHA:
4844         case R_PPC64_REL16_HIGHER:
4845         case R_PPC64_REL16_HIGHERA:
4846         case R_PPC64_REL16_HIGHEST:
4847         case R_PPC64_REL16_HIGHESTA:
4848         case R_PPC64_REL16_HIGHER34:
4849         case R_PPC64_REL16_HIGHERA34:
4850         case R_PPC64_REL16_HIGHEST34:
4851         case R_PPC64_REL16_HIGHESTA34:
4852         case R_PPC64_REL16DX_HA:
4853           break;
4854
4855           /* Not supported as a dynamic relocation.  */
4856         case R_PPC64_ADDR64_LOCAL:
4857           if (bfd_link_pic (info))
4858             {
4859               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4860                 ppc_howto_init ();
4861               /* xgettext:c-format */
4862               info->callbacks->einfo (_("%H: %s reloc unsupported "
4863                                         "in shared libraries and PIEs\n"),
4864                                       abfd, sec, rel->r_offset,
4865                                       ppc64_elf_howto_table[r_type]->name);
4866               bfd_set_error (bfd_error_bad_value);
4867               return FALSE;
4868             }
4869           break;
4870
4871         case R_PPC64_TOC16:
4872         case R_PPC64_TOC16_DS:
4873           htab->do_multi_toc = 1;
4874           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4875           /* Fall through.  */
4876         case R_PPC64_TOC16_LO:
4877         case R_PPC64_TOC16_HI:
4878         case R_PPC64_TOC16_HA:
4879         case R_PPC64_TOC16_LO_DS:
4880           sec->has_toc_reloc = 1;
4881           if (h != NULL && bfd_link_executable (info))
4882             {
4883               /* We may need a copy reloc.  */
4884               h->non_got_ref = 1;
4885               /* Strongly prefer a copy reloc over a dynamic reloc.
4886                  glibc ld.so as of 2019-08 will error out if one of
4887                  these relocations is emitted.  */
4888               h->needs_copy = 1;
4889               goto dodyn;
4890             }
4891           break;
4892
4893           /* Marker reloc.  */
4894         case R_PPC64_ENTRY:
4895           break;
4896
4897           /* This relocation describes the C++ object vtable hierarchy.
4898              Reconstruct it for later use during GC.  */
4899         case R_PPC64_GNU_VTINHERIT:
4900           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4901             return FALSE;
4902           break;
4903
4904           /* This relocation describes which C++ vtable entries are actually
4905              used.  Record for later use during GC.  */
4906         case R_PPC64_GNU_VTENTRY:
4907           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4908             return FALSE;
4909           break;
4910
4911         case R_PPC64_REL14:
4912         case R_PPC64_REL14_BRTAKEN:
4913         case R_PPC64_REL14_BRNTAKEN:
4914           {
4915             asection *dest = NULL;
4916
4917             /* Heuristic: If jumping outside our section, chances are
4918                we are going to need a stub.  */
4919             if (h != NULL)
4920               {
4921                 /* If the sym is weak it may be overridden later, so
4922                    don't assume we know where a weak sym lives.  */
4923                 if (h->root.type == bfd_link_hash_defined)
4924                   dest = h->root.u.def.section;
4925               }
4926             else
4927               {
4928                 Elf_Internal_Sym *isym;
4929
4930                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4931                                               abfd, r_symndx);
4932                 if (isym == NULL)
4933                   return FALSE;
4934
4935                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4936               }
4937
4938             if (dest != sec)
4939               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4940           }
4941           goto rel24;
4942
4943         case R_PPC64_PLTCALL:
4944         case R_PPC64_PLTCALL_NOTOC:
4945           ppc64_elf_section_data (sec)->has_pltcall = 1;
4946           /* Fall through.  */
4947
4948         case R_PPC64_REL24:
4949         case R_PPC64_REL24_NOTOC:
4950         rel24:
4951           plt_list = ifunc;
4952           if (h != NULL)
4953             {
4954               h->needs_plt = 1;
4955               if (h->root.root.string[0] == '.'
4956                   && h->root.root.string[1] != '\0')
4957                 ppc_elf_hash_entry (h)->is_func = 1;
4958
4959               if (h == tga || h == dottga)
4960                 {
4961                   sec->has_tls_reloc = 1;
4962                   if (rel != relocs
4963                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4964                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4965                     /* We have a new-style __tls_get_addr call with
4966                        a marker reloc.  */
4967                     ;
4968                   else
4969                     /* Mark this section as having an old-style call.  */
4970                     sec->nomark_tls_get_addr = 1;
4971                 }
4972               plt_list = &h->plt.plist;
4973             }
4974
4975           /* We may need a .plt entry if the function this reloc
4976              refers to is in a shared lib.  */
4977           if (plt_list
4978               && !update_plt_info (abfd, plt_list, rel->r_addend))
4979             return FALSE;
4980           break;
4981
4982         case R_PPC64_ADDR14:
4983         case R_PPC64_ADDR14_BRNTAKEN:
4984         case R_PPC64_ADDR14_BRTAKEN:
4985         case R_PPC64_ADDR24:
4986           goto dodyn;
4987
4988         case R_PPC64_TPREL64:
4989           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4990           if (bfd_link_dll (info))
4991             info->flags |= DF_STATIC_TLS;
4992           goto dotlstoc;
4993
4994         case R_PPC64_DTPMOD64:
4995           if (rel + 1 < rel_end
4996               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4997               && rel[1].r_offset == rel->r_offset + 8)
4998             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4999           else
5000             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5001           goto dotlstoc;
5002
5003         case R_PPC64_DTPREL64:
5004           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5005           if (rel != relocs
5006               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5007               && rel[-1].r_offset == rel->r_offset - 8)
5008             /* This is the second reloc of a dtpmod, dtprel pair.
5009                Don't mark with TLS_DTPREL.  */
5010             goto dodyn;
5011
5012         dotlstoc:
5013           sec->has_tls_reloc = 1;
5014           if (h != NULL)
5015             ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5016           else
5017             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5018                                         rel->r_addend, tls_type))
5019               return FALSE;
5020
5021           ppc64_sec = ppc64_elf_section_data (sec);
5022           if (ppc64_sec->sec_type != sec_toc)
5023             {
5024               bfd_size_type amt;
5025
5026               /* One extra to simplify get_tls_mask.  */
5027               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5028               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5029               if (ppc64_sec->u.toc.symndx == NULL)
5030                 return FALSE;
5031               amt = sec->size * sizeof (bfd_vma) / 8;
5032               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5033               if (ppc64_sec->u.toc.add == NULL)
5034                 return FALSE;
5035               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5036               ppc64_sec->sec_type = sec_toc;
5037             }
5038           BFD_ASSERT (rel->r_offset % 8 == 0);
5039           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5040           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5041
5042           /* Mark the second slot of a GD or LD entry.
5043              -1 to indicate GD and -2 to indicate LD.  */
5044           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5045             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5046           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5047             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5048           goto dodyn;
5049
5050         case R_PPC64_TPREL16:
5051         case R_PPC64_TPREL16_LO:
5052         case R_PPC64_TPREL16_HI:
5053         case R_PPC64_TPREL16_HA:
5054         case R_PPC64_TPREL16_DS:
5055         case R_PPC64_TPREL16_LO_DS:
5056         case R_PPC64_TPREL16_HIGH:
5057         case R_PPC64_TPREL16_HIGHA:
5058         case R_PPC64_TPREL16_HIGHER:
5059         case R_PPC64_TPREL16_HIGHERA:
5060         case R_PPC64_TPREL16_HIGHEST:
5061         case R_PPC64_TPREL16_HIGHESTA:
5062         case R_PPC64_TPREL34:
5063           if (bfd_link_dll (info))
5064             info->flags |= DF_STATIC_TLS;
5065           goto dodyn;
5066
5067         case R_PPC64_ADDR64:
5068           if (is_opd
5069               && rel + 1 < rel_end
5070               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5071             {
5072               if (h != NULL)
5073                 ppc_elf_hash_entry (h)->is_func = 1;
5074             }
5075           /* Fall through.  */
5076
5077         case R_PPC64_ADDR16:
5078         case R_PPC64_ADDR16_DS:
5079         case R_PPC64_ADDR16_HA:
5080         case R_PPC64_ADDR16_HI:
5081         case R_PPC64_ADDR16_HIGH:
5082         case R_PPC64_ADDR16_HIGHA:
5083         case R_PPC64_ADDR16_HIGHER:
5084         case R_PPC64_ADDR16_HIGHERA:
5085         case R_PPC64_ADDR16_HIGHEST:
5086         case R_PPC64_ADDR16_HIGHESTA:
5087         case R_PPC64_ADDR16_LO:
5088         case R_PPC64_ADDR16_LO_DS:
5089         case R_PPC64_D34:
5090         case R_PPC64_D34_LO:
5091         case R_PPC64_D34_HI30:
5092         case R_PPC64_D34_HA30:
5093         case R_PPC64_ADDR16_HIGHER34:
5094         case R_PPC64_ADDR16_HIGHERA34:
5095         case R_PPC64_ADDR16_HIGHEST34:
5096         case R_PPC64_ADDR16_HIGHESTA34:
5097         case R_PPC64_D28:
5098           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5099               && rel->r_addend == 0)
5100             {
5101               /* We may need a .plt entry if this reloc refers to a
5102                  function in a shared lib.  */
5103               if (!update_plt_info (abfd, &h->plt.plist, 0))
5104                 return FALSE;
5105               h->pointer_equality_needed = 1;
5106             }
5107           /* Fall through.  */
5108
5109         case R_PPC64_REL30:
5110         case R_PPC64_REL32:
5111         case R_PPC64_REL64:
5112         case R_PPC64_ADDR32:
5113         case R_PPC64_UADDR16:
5114         case R_PPC64_UADDR32:
5115         case R_PPC64_UADDR64:
5116         case R_PPC64_TOC:
5117           if (h != NULL && bfd_link_executable (info))
5118             /* We may need a copy reloc.  */
5119             h->non_got_ref = 1;
5120
5121           /* Don't propagate .opd relocs.  */
5122           if (NO_OPD_RELOCS && is_opd)
5123             break;
5124
5125           /* If we are creating a shared library, and this is a reloc
5126              against a global symbol, or a non PC relative reloc
5127              against a local symbol, then we need to copy the reloc
5128              into the shared library.  However, if we are linking with
5129              -Bsymbolic, we do not need to copy a reloc against a
5130              global symbol which is defined in an object we are
5131              including in the link (i.e., DEF_REGULAR is set).  At
5132              this point we have not seen all the input files, so it is
5133              possible that DEF_REGULAR is not set now but will be set
5134              later (it is never cleared).  In case of a weak definition,
5135              DEF_REGULAR may be cleared later by a strong definition in
5136              a shared library.  We account for that possibility below by
5137              storing information in the dyn_relocs field of the hash
5138              table entry.  A similar situation occurs when creating
5139              shared libraries and symbol visibility changes render the
5140              symbol local.
5141
5142              If on the other hand, we are creating an executable, we
5143              may need to keep relocations for symbols satisfied by a
5144              dynamic library if we manage to avoid copy relocs for the
5145              symbol.  */
5146         dodyn:
5147           if ((h != NULL
5148                && (h->root.type == bfd_link_hash_defweak
5149                    || !h->def_regular))
5150               || (h != NULL
5151                   && !bfd_link_executable (info)
5152                   && !SYMBOLIC_BIND (info, h))
5153               || (bfd_link_pic (info)
5154                   && must_be_dyn_reloc (info, r_type))
5155               || (!bfd_link_pic (info)
5156                   && ifunc != NULL))
5157             {
5158               /* We must copy these reloc types into the output file.
5159                  Create a reloc section in dynobj and make room for
5160                  this reloc.  */
5161               if (sreloc == NULL)
5162                 {
5163                   sreloc = _bfd_elf_make_dynamic_reloc_section
5164                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5165
5166                   if (sreloc == NULL)
5167                     return FALSE;
5168                 }
5169
5170               /* If this is a global symbol, we count the number of
5171                  relocations we need for this symbol.  */
5172               if (h != NULL)
5173                 {
5174                   struct elf_dyn_relocs *p;
5175                   struct elf_dyn_relocs **head;
5176
5177                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
5178                   p = *head;
5179                   if (p == NULL || p->sec != sec)
5180                     {
5181                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5182                       if (p == NULL)
5183                         return FALSE;
5184                       p->next = *head;
5185                       *head = p;
5186                       p->sec = sec;
5187                       p->count = 0;
5188                       p->pc_count = 0;
5189                     }
5190                   p->count += 1;
5191                   if (!must_be_dyn_reloc (info, r_type))
5192                     p->pc_count += 1;
5193                 }
5194               else
5195                 {
5196                   /* Track dynamic relocs needed for local syms too.
5197                      We really need local syms available to do this
5198                      easily.  Oh well.  */
5199                   struct ppc_dyn_relocs *p;
5200                   struct ppc_dyn_relocs **head;
5201                   bfd_boolean is_ifunc;
5202                   asection *s;
5203                   void *vpp;
5204                   Elf_Internal_Sym *isym;
5205
5206                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5207                                                 abfd, r_symndx);
5208                   if (isym == NULL)
5209                     return FALSE;
5210
5211                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5212                   if (s == NULL)
5213                     s = sec;
5214
5215                   vpp = &elf_section_data (s)->local_dynrel;
5216                   head = (struct ppc_dyn_relocs **) vpp;
5217                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5218                   p = *head;
5219                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5220                     p = p->next;
5221                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5222                     {
5223                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5224                       if (p == NULL)
5225                         return FALSE;
5226                       p->next = *head;
5227                       *head = p;
5228                       p->sec = sec;
5229                       p->ifunc = is_ifunc;
5230                       p->count = 0;
5231                     }
5232                   p->count += 1;
5233                 }
5234             }
5235           break;
5236
5237         default:
5238           break;
5239         }
5240     }
5241
5242   return TRUE;
5243 }
5244
5245 /* Merge backend specific data from an object file to the output
5246    object file when linking.  */
5247
5248 static bfd_boolean
5249 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5250 {
5251   bfd *obfd = info->output_bfd;
5252   unsigned long iflags, oflags;
5253
5254   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5255     return TRUE;
5256
5257   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5258     return TRUE;
5259
5260   if (!_bfd_generic_verify_endian_match (ibfd, info))
5261     return FALSE;
5262
5263   iflags = elf_elfheader (ibfd)->e_flags;
5264   oflags = elf_elfheader (obfd)->e_flags;
5265
5266   if (iflags & ~EF_PPC64_ABI)
5267     {
5268       _bfd_error_handler
5269         /* xgettext:c-format */
5270         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5271       bfd_set_error (bfd_error_bad_value);
5272       return FALSE;
5273     }
5274   else if (iflags != oflags && iflags != 0)
5275     {
5276       _bfd_error_handler
5277         /* xgettext:c-format */
5278         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5279          ibfd, iflags, oflags);
5280       bfd_set_error (bfd_error_bad_value);
5281       return FALSE;
5282     }
5283
5284   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5285     return FALSE;
5286
5287   /* Merge Tag_compatibility attributes and any common GNU ones.  */
5288   return _bfd_elf_merge_object_attributes (ibfd, info);
5289 }
5290
5291 static bfd_boolean
5292 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5293 {
5294   /* Print normal ELF private data.  */
5295   _bfd_elf_print_private_bfd_data (abfd, ptr);
5296
5297   if (elf_elfheader (abfd)->e_flags != 0)
5298     {
5299       FILE *file = ptr;
5300
5301       fprintf (file, _("private flags = 0x%lx:"),
5302                elf_elfheader (abfd)->e_flags);
5303
5304       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5305         fprintf (file, _(" [abiv%ld]"),
5306                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5307       fputc ('\n', file);
5308     }
5309
5310   return TRUE;
5311 }
5312
5313 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
5314    of the code entry point, and its section, which must be in the same
5315    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
5316
5317 static bfd_vma
5318 opd_entry_value (asection *opd_sec,
5319                  bfd_vma offset,
5320                  asection **code_sec,
5321                  bfd_vma *code_off,
5322                  bfd_boolean in_code_sec)
5323 {
5324   bfd *opd_bfd = opd_sec->owner;
5325   Elf_Internal_Rela *relocs;
5326   Elf_Internal_Rela *lo, *hi, *look;
5327   bfd_vma val;
5328
5329   /* No relocs implies we are linking a --just-symbols object, or looking
5330      at a final linked executable with addr2line or somesuch.  */
5331   if (opd_sec->reloc_count == 0)
5332     {
5333       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5334
5335       if (contents == NULL)
5336         {
5337           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5338             return (bfd_vma) -1;
5339           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5340         }
5341
5342       /* PR 17512: file: 64b9dfbb.  */
5343       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5344         return (bfd_vma) -1;
5345
5346       val = bfd_get_64 (opd_bfd, contents + offset);
5347       if (code_sec != NULL)
5348         {
5349           asection *sec, *likely = NULL;
5350
5351           if (in_code_sec)
5352             {
5353               sec = *code_sec;
5354               if (sec->vma <= val
5355                   && val < sec->vma + sec->size)
5356                 likely = sec;
5357               else
5358                 val = -1;
5359             }
5360           else
5361             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5362               if (sec->vma <= val
5363                   && (sec->flags & SEC_LOAD) != 0
5364                   && (sec->flags & SEC_ALLOC) != 0)
5365                 likely = sec;
5366           if (likely != NULL)
5367             {
5368               *code_sec = likely;
5369               if (code_off != NULL)
5370                 *code_off = val - likely->vma;
5371             }
5372         }
5373       return val;
5374     }
5375
5376   BFD_ASSERT (is_ppc64_elf (opd_bfd));
5377
5378   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5379   if (relocs == NULL)
5380     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5381   /* PR 17512: file: df8e1fd6.  */
5382   if (relocs == NULL)
5383     return (bfd_vma) -1;
5384
5385   /* Go find the opd reloc at the sym address.  */
5386   lo = relocs;
5387   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5388   val = (bfd_vma) -1;
5389   while (lo < hi)
5390     {
5391       look = lo + (hi - lo) / 2;
5392       if (look->r_offset < offset)
5393         lo = look + 1;
5394       else if (look->r_offset > offset)
5395         hi = look;
5396       else
5397         {
5398           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5399
5400           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5401               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5402             {
5403               unsigned long symndx = ELF64_R_SYM (look->r_info);
5404               asection *sec = NULL;
5405
5406               if (symndx >= symtab_hdr->sh_info
5407                   && elf_sym_hashes (opd_bfd) != NULL)
5408                 {
5409                   struct elf_link_hash_entry **sym_hashes;
5410                   struct elf_link_hash_entry *rh;
5411
5412                   sym_hashes = elf_sym_hashes (opd_bfd);
5413                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5414                   if (rh != NULL)
5415                     {
5416                       rh = elf_follow_link (rh);
5417                       if (rh->root.type != bfd_link_hash_defined
5418                           && rh->root.type != bfd_link_hash_defweak)
5419                         break;
5420                       if (rh->root.u.def.section->owner == opd_bfd)
5421                         {
5422                           val = rh->root.u.def.value;
5423                           sec = rh->root.u.def.section;
5424                         }
5425                     }
5426                 }
5427
5428               if (sec == NULL)
5429                 {
5430                   Elf_Internal_Sym *sym;
5431
5432                   if (symndx < symtab_hdr->sh_info)
5433                     {
5434                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5435                       if (sym == NULL)
5436                         {
5437                           size_t symcnt = symtab_hdr->sh_info;
5438                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5439                                                       symcnt, 0,
5440                                                       NULL, NULL, NULL);
5441                           if (sym == NULL)
5442                             break;
5443                           symtab_hdr->contents = (bfd_byte *) sym;
5444                         }
5445                       sym += symndx;
5446                     }
5447                   else
5448                     {
5449                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5450                                                   1, symndx,
5451                                                   NULL, NULL, NULL);
5452                       if (sym == NULL)
5453                         break;
5454                     }
5455                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5456                   if (sec == NULL)
5457                     break;
5458                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5459                   val = sym->st_value;
5460                 }
5461
5462               val += look->r_addend;
5463               if (code_off != NULL)
5464                 *code_off = val;
5465               if (code_sec != NULL)
5466                 {
5467                   if (in_code_sec && *code_sec != sec)
5468                     return -1;
5469                   else
5470                     *code_sec = sec;
5471                 }
5472               if (sec->output_section != NULL)
5473                 val += sec->output_section->vma + sec->output_offset;
5474             }
5475           break;
5476         }
5477     }
5478
5479   return val;
5480 }
5481
5482 /* If the ELF symbol SYM might be a function in SEC, return the
5483    function size and set *CODE_OFF to the function's entry point,
5484    otherwise return zero.  */
5485
5486 static bfd_size_type
5487 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5488                               bfd_vma *code_off)
5489 {
5490   bfd_size_type size;
5491
5492   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5493                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5494     return 0;
5495
5496   size = 0;
5497   if (!(sym->flags & BSF_SYNTHETIC))
5498     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5499
5500   if (strcmp (sym->section->name, ".opd") == 0)
5501     {
5502       struct _opd_sec_data *opd = get_opd_info (sym->section);
5503       bfd_vma symval = sym->value;
5504
5505       if (opd != NULL
5506           && opd->adjust != NULL
5507           && elf_section_data (sym->section)->relocs != NULL)
5508         {
5509           /* opd_entry_value will use cached relocs that have been
5510              adjusted, but with raw symbols.  That means both local
5511              and global symbols need adjusting.  */
5512           long adjust = opd->adjust[OPD_NDX (symval)];
5513           if (adjust == -1)
5514             return 0;
5515           symval += adjust;
5516         }
5517
5518       if (opd_entry_value (sym->section, symval,
5519                            &sec, code_off, TRUE) == (bfd_vma) -1)
5520         return 0;
5521       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5522          symbol.  This size has nothing to do with the code size of the
5523          function, which is what we're supposed to return, but the
5524          code size isn't available without looking up the dot-sym.
5525          However, doing that would be a waste of time particularly
5526          since elf_find_function will look at the dot-sym anyway.
5527          Now, elf_find_function will keep the largest size of any
5528          function sym found at the code address of interest, so return
5529          1 here to avoid it incorrectly caching a larger function size
5530          for a small function.  This does mean we return the wrong
5531          size for a new-ABI function of size 24, but all that does is
5532          disable caching for such functions.  */
5533       if (size == 24)
5534         size = 1;
5535     }
5536   else
5537     {
5538       if (sym->section != sec)
5539         return 0;
5540       *code_off = sym->value;
5541     }
5542   if (size == 0)
5543     size = 1;
5544   return size;
5545 }
5546
5547 /* Return true if symbol is a strong function defined in an ELFv2
5548    object with st_other localentry bits of zero, ie. its local entry
5549    point coincides with its global entry point.  */
5550
5551 static bfd_boolean
5552 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5553 {
5554   return (h != NULL
5555           && h->type == STT_FUNC
5556           && h->root.type == bfd_link_hash_defined
5557           && (STO_PPC64_LOCAL_MASK & h->other) == 0
5558           && !ppc_elf_hash_entry (h)->non_zero_localentry
5559           && is_ppc64_elf (h->root.u.def.section->owner)
5560           && abiversion (h->root.u.def.section->owner) >= 2);
5561 }
5562
5563 /* Return true if symbol is defined in a regular object file.  */
5564
5565 static bfd_boolean
5566 is_static_defined (struct elf_link_hash_entry *h)
5567 {
5568   return ((h->root.type == bfd_link_hash_defined
5569            || h->root.type == bfd_link_hash_defweak)
5570           && h->root.u.def.section != NULL
5571           && h->root.u.def.section->output_section != NULL);
5572 }
5573
5574 /* If FDH is a function descriptor symbol, return the associated code
5575    entry symbol if it is defined.  Return NULL otherwise.  */
5576
5577 static struct ppc_link_hash_entry *
5578 defined_code_entry (struct ppc_link_hash_entry *fdh)
5579 {
5580   if (fdh->is_func_descriptor)
5581     {
5582       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5583       if (fh->elf.root.type == bfd_link_hash_defined
5584           || fh->elf.root.type == bfd_link_hash_defweak)
5585         return fh;
5586     }
5587   return NULL;
5588 }
5589
5590 /* If FH is a function code entry symbol, return the associated
5591    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5592
5593 static struct ppc_link_hash_entry *
5594 defined_func_desc (struct ppc_link_hash_entry *fh)
5595 {
5596   if (fh->oh != NULL
5597       && fh->oh->is_func_descriptor)
5598     {
5599       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5600       if (fdh->elf.root.type == bfd_link_hash_defined
5601           || fdh->elf.root.type == bfd_link_hash_defweak)
5602         return fdh;
5603     }
5604   return NULL;
5605 }
5606
5607 /* Given H is a symbol that satisfies is_static_defined, return the
5608    value in the output file.  */
5609
5610 static bfd_vma
5611 defined_sym_val (struct elf_link_hash_entry *h)
5612 {
5613   return (h->root.u.def.section->output_section->vma
5614           + h->root.u.def.section->output_offset
5615           + h->root.u.def.value);
5616 }
5617
5618 /* Return true if H matches __tls_get_addr or one of its variants.  */
5619
5620 static bfd_boolean
5621 is_tls_get_addr (struct elf_link_hash_entry *h,
5622                  struct ppc_link_hash_table *htab)
5623 {
5624   return (h == &htab->tls_get_addr_fd->elf || h == &htab->tga_desc_fd->elf
5625           || h == &htab->tls_get_addr->elf || h == &htab->tga_desc->elf);
5626 }
5627
5628 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5629
5630 /* Garbage collect sections, after first dealing with dot-symbols.  */
5631
5632 static bfd_boolean
5633 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5634 {
5635   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5636
5637   if (htab != NULL && htab->need_func_desc_adj)
5638     {
5639       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5640       htab->need_func_desc_adj = 0;
5641     }
5642   return bfd_elf_gc_sections (abfd, info);
5643 }
5644
5645 /* Mark all our entry sym sections, both opd and code section.  */
5646
5647 static void
5648 ppc64_elf_gc_keep (struct bfd_link_info *info)
5649 {
5650   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5651   struct bfd_sym_chain *sym;
5652
5653   if (htab == NULL)
5654     return;
5655
5656   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5657     {
5658       struct ppc_link_hash_entry *eh, *fh;
5659       asection *sec;
5660
5661       eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5662                                                      FALSE, FALSE, TRUE));
5663       if (eh == NULL)
5664         continue;
5665       if (eh->elf.root.type != bfd_link_hash_defined
5666           && eh->elf.root.type != bfd_link_hash_defweak)
5667         continue;
5668
5669       fh = defined_code_entry (eh);
5670       if (fh != NULL)
5671         {
5672           sec = fh->elf.root.u.def.section;
5673           sec->flags |= SEC_KEEP;
5674         }
5675       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5676                && opd_entry_value (eh->elf.root.u.def.section,
5677                                    eh->elf.root.u.def.value,
5678                                    &sec, NULL, FALSE) != (bfd_vma) -1)
5679         sec->flags |= SEC_KEEP;
5680
5681       sec = eh->elf.root.u.def.section;
5682       sec->flags |= SEC_KEEP;
5683     }
5684 }
5685
5686 /* Mark sections containing dynamically referenced symbols.  When
5687    building shared libraries, we must assume that any visible symbol is
5688    referenced.  */
5689
5690 static bfd_boolean
5691 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5692 {
5693   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5694   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5695   struct ppc_link_hash_entry *fdh;
5696   struct bfd_elf_dynamic_list *d = info->dynamic_list;
5697
5698   /* Dynamic linking info is on the func descriptor sym.  */
5699   fdh = defined_func_desc (eh);
5700   if (fdh != NULL)
5701     eh = fdh;
5702
5703   if ((eh->elf.root.type == bfd_link_hash_defined
5704        || eh->elf.root.type == bfd_link_hash_defweak)
5705       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5706           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5707               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5708               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5709               && (!bfd_link_executable (info)
5710                   || info->gc_keep_exported
5711                   || info->export_dynamic
5712                   || (eh->elf.dynamic
5713                       && d != NULL
5714                       && (*d->match) (&d->head, NULL,
5715                                       eh->elf.root.root.string)))
5716               && (eh->elf.versioned >= versioned
5717                   || !bfd_hide_sym_by_version (info->version_info,
5718                                                eh->elf.root.root.string)))))
5719     {
5720       asection *code_sec;
5721       struct ppc_link_hash_entry *fh;
5722
5723       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5724
5725       /* Function descriptor syms cause the associated
5726          function code sym section to be marked.  */
5727       fh = defined_code_entry (eh);
5728       if (fh != NULL)
5729         {
5730           code_sec = fh->elf.root.u.def.section;
5731           code_sec->flags |= SEC_KEEP;
5732         }
5733       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5734                && opd_entry_value (eh->elf.root.u.def.section,
5735                                    eh->elf.root.u.def.value,
5736                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
5737         code_sec->flags |= SEC_KEEP;
5738     }
5739
5740   return TRUE;
5741 }
5742
5743 /* Return the section that should be marked against GC for a given
5744    relocation.  */
5745
5746 static asection *
5747 ppc64_elf_gc_mark_hook (asection *sec,
5748                         struct bfd_link_info *info,
5749                         Elf_Internal_Rela *rel,
5750                         struct elf_link_hash_entry *h,
5751                         Elf_Internal_Sym *sym)
5752 {
5753   asection *rsec;
5754
5755   /* Syms return NULL if we're marking .opd, so we avoid marking all
5756      function sections, as all functions are referenced in .opd.  */
5757   rsec = NULL;
5758   if (get_opd_info (sec) != NULL)
5759     return rsec;
5760
5761   if (h != NULL)
5762     {
5763       enum elf_ppc64_reloc_type r_type;
5764       struct ppc_link_hash_entry *eh, *fh, *fdh;
5765
5766       r_type = ELF64_R_TYPE (rel->r_info);
5767       switch (r_type)
5768         {
5769         case R_PPC64_GNU_VTINHERIT:
5770         case R_PPC64_GNU_VTENTRY:
5771           break;
5772
5773         default:
5774           switch (h->root.type)
5775             {
5776             case bfd_link_hash_defined:
5777             case bfd_link_hash_defweak:
5778               eh = ppc_elf_hash_entry (h);
5779               fdh = defined_func_desc (eh);
5780               if (fdh != NULL)
5781                 {
5782                   /* -mcall-aixdesc code references the dot-symbol on
5783                      a call reloc.  Mark the function descriptor too
5784                      against garbage collection.  */
5785                   fdh->elf.mark = 1;
5786                   if (fdh->elf.is_weakalias)
5787                     weakdef (&fdh->elf)->mark = 1;
5788                   eh = fdh;
5789                 }
5790
5791               /* Function descriptor syms cause the associated
5792                  function code sym section to be marked.  */
5793               fh = defined_code_entry (eh);
5794               if (fh != NULL)
5795                 {
5796                   /* They also mark their opd section.  */
5797                   eh->elf.root.u.def.section->gc_mark = 1;
5798
5799                   rsec = fh->elf.root.u.def.section;
5800                 }
5801               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5802                        && opd_entry_value (eh->elf.root.u.def.section,
5803                                            eh->elf.root.u.def.value,
5804                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
5805                 eh->elf.root.u.def.section->gc_mark = 1;
5806               else
5807                 rsec = h->root.u.def.section;
5808               break;
5809
5810             case bfd_link_hash_common:
5811               rsec = h->root.u.c.p->section;
5812               break;
5813
5814             default:
5815               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5816             }
5817         }
5818     }
5819   else
5820     {
5821       struct _opd_sec_data *opd;
5822
5823       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5824       opd = get_opd_info (rsec);
5825       if (opd != NULL && opd->func_sec != NULL)
5826         {
5827           rsec->gc_mark = 1;
5828
5829           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5830         }
5831     }
5832
5833   return rsec;
5834 }
5835
5836 /* The maximum size of .sfpr.  */
5837 #define SFPR_MAX (218*4)
5838
5839 struct sfpr_def_parms
5840 {
5841   const char name[12];
5842   unsigned char lo, hi;
5843   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5844   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5845 };
5846
5847 /* Auto-generate _save*, _rest* functions in .sfpr.
5848    If STUB_SEC is non-null, define alias symbols in STUB_SEC
5849    instead.  */
5850
5851 static bfd_boolean
5852 sfpr_define (struct bfd_link_info *info,
5853              const struct sfpr_def_parms *parm,
5854              asection *stub_sec)
5855 {
5856   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5857   unsigned int i;
5858   size_t len = strlen (parm->name);
5859   bfd_boolean writing = FALSE;
5860   char sym[16];
5861
5862   if (htab == NULL)
5863     return FALSE;
5864
5865   memcpy (sym, parm->name, len);
5866   sym[len + 2] = 0;
5867
5868   for (i = parm->lo; i <= parm->hi; i++)
5869     {
5870       struct ppc_link_hash_entry *h;
5871
5872       sym[len + 0] = i / 10 + '0';
5873       sym[len + 1] = i % 10 + '0';
5874       h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5875                                                     writing, TRUE, TRUE));
5876       if (stub_sec != NULL)
5877         {
5878           if (h != NULL
5879               && h->elf.root.type == bfd_link_hash_defined
5880               && h->elf.root.u.def.section == htab->sfpr)
5881             {
5882               struct elf_link_hash_entry *s;
5883               char buf[32];
5884               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5885               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5886               if (s == NULL)
5887                 return FALSE;
5888               if (s->root.type == bfd_link_hash_new)
5889                 {
5890                   s->root.type = bfd_link_hash_defined;
5891                   s->root.u.def.section = stub_sec;
5892                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5893                                          + h->elf.root.u.def.value);
5894                   s->ref_regular = 1;
5895                   s->def_regular = 1;
5896                   s->ref_regular_nonweak = 1;
5897                   s->forced_local = 1;
5898                   s->non_elf = 0;
5899                   s->root.linker_def = 1;
5900                 }
5901             }
5902           continue;
5903         }
5904       if (h != NULL)
5905         {
5906           h->save_res = 1;
5907           if (!h->elf.def_regular)
5908             {
5909               h->elf.root.type = bfd_link_hash_defined;
5910               h->elf.root.u.def.section = htab->sfpr;
5911               h->elf.root.u.def.value = htab->sfpr->size;
5912               h->elf.type = STT_FUNC;
5913               h->elf.def_regular = 1;
5914               h->elf.non_elf = 0;
5915               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5916               writing = TRUE;
5917               if (htab->sfpr->contents == NULL)
5918                 {
5919                   htab->sfpr->contents
5920                     = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5921                   if (htab->sfpr->contents == NULL)
5922                     return FALSE;
5923                 }
5924             }
5925         }
5926       if (writing)
5927         {
5928           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5929           if (i != parm->hi)
5930             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5931           else
5932             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5933           htab->sfpr->size = p - htab->sfpr->contents;
5934         }
5935     }
5936
5937   return TRUE;
5938 }
5939
5940 static bfd_byte *
5941 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5942 {
5943   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5944   return p + 4;
5945 }
5946
5947 static bfd_byte *
5948 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5949 {
5950   p = savegpr0 (abfd, p, r);
5951   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5952   p = p + 4;
5953   bfd_put_32 (abfd, BLR, p);
5954   return p + 4;
5955 }
5956
5957 static bfd_byte *
5958 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5959 {
5960   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5961   return p + 4;
5962 }
5963
5964 static bfd_byte *
5965 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5966 {
5967   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5968   p = p + 4;
5969   p = restgpr0 (abfd, p, r);
5970   bfd_put_32 (abfd, MTLR_R0, p);
5971   p = p + 4;
5972   if (r == 29)
5973     {
5974       p = restgpr0 (abfd, p, 30);
5975       p = restgpr0 (abfd, p, 31);
5976     }
5977   bfd_put_32 (abfd, BLR, p);
5978   return p + 4;
5979 }
5980
5981 static bfd_byte *
5982 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5983 {
5984   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5985   return p + 4;
5986 }
5987
5988 static bfd_byte *
5989 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5990 {
5991   p = savegpr1 (abfd, p, r);
5992   bfd_put_32 (abfd, BLR, p);
5993   return p + 4;
5994 }
5995
5996 static bfd_byte *
5997 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5998 {
5999   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6000   return p + 4;
6001 }
6002
6003 static bfd_byte *
6004 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6005 {
6006   p = restgpr1 (abfd, p, r);
6007   bfd_put_32 (abfd, BLR, p);
6008   return p + 4;
6009 }
6010
6011 static bfd_byte *
6012 savefpr (bfd *abfd, bfd_byte *p, int r)
6013 {
6014   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6015   return p + 4;
6016 }
6017
6018 static bfd_byte *
6019 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6020 {
6021   p = savefpr (abfd, p, r);
6022   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6023   p = p + 4;
6024   bfd_put_32 (abfd, BLR, p);
6025   return p + 4;
6026 }
6027
6028 static bfd_byte *
6029 restfpr (bfd *abfd, bfd_byte *p, int r)
6030 {
6031   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6032   return p + 4;
6033 }
6034
6035 static bfd_byte *
6036 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6037 {
6038   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6039   p = p + 4;
6040   p = restfpr (abfd, p, r);
6041   bfd_put_32 (abfd, MTLR_R0, p);
6042   p = p + 4;
6043   if (r == 29)
6044     {
6045       p = restfpr (abfd, p, 30);
6046       p = restfpr (abfd, p, 31);
6047     }
6048   bfd_put_32 (abfd, BLR, p);
6049   return p + 4;
6050 }
6051
6052 static bfd_byte *
6053 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6054 {
6055   p = savefpr (abfd, p, r);
6056   bfd_put_32 (abfd, BLR, p);
6057   return p + 4;
6058 }
6059
6060 static bfd_byte *
6061 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6062 {
6063   p = restfpr (abfd, p, r);
6064   bfd_put_32 (abfd, BLR, p);
6065   return p + 4;
6066 }
6067
6068 static bfd_byte *
6069 savevr (bfd *abfd, bfd_byte *p, int r)
6070 {
6071   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6072   p = p + 4;
6073   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6074   return p + 4;
6075 }
6076
6077 static bfd_byte *
6078 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6079 {
6080   p = savevr (abfd, p, r);
6081   bfd_put_32 (abfd, BLR, p);
6082   return p + 4;
6083 }
6084
6085 static bfd_byte *
6086 restvr (bfd *abfd, bfd_byte *p, int r)
6087 {
6088   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6089   p = p + 4;
6090   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6091   return p + 4;
6092 }
6093
6094 static bfd_byte *
6095 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6096 {
6097   p = restvr (abfd, p, r);
6098   bfd_put_32 (abfd, BLR, p);
6099   return p + 4;
6100 }
6101
6102 #define STDU_R1_0R1     0xf8210001
6103 #define ADDI_R1_R1      0x38210000
6104
6105 /* Emit prologue of wrapper preserving regs around a call to
6106    __tls_get_addr_opt.  */
6107
6108 static bfd_byte *
6109 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6110 {
6111   unsigned int i;
6112
6113   bfd_put_32 (obfd, MFLR_R0, p);
6114   p += 4;
6115   bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6116   p += 4;
6117
6118   if (htab->opd_abi)
6119     {
6120       for (i = 4; i < 12; i++)
6121         {
6122           bfd_put_32 (obfd,
6123                       STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6124           p += 4;
6125         }
6126       bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6127       p += 4;
6128     }
6129   else
6130     {
6131       for (i = 4; i < 12; i++)
6132         {
6133           bfd_put_32 (obfd,
6134                       STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6135           p += 4;
6136         }
6137       bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6138       p += 4;
6139     }
6140   return p;
6141 }
6142
6143 /* Emit epilogue of wrapper preserving regs around a call to
6144    __tls_get_addr_opt.  */
6145
6146 static bfd_byte *
6147 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6148 {
6149   unsigned int i;
6150
6151   if (htab->opd_abi)
6152     {
6153       for (i = 4; i < 12; i++)
6154         {
6155           bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6156           p += 4;
6157         }
6158       bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6159       p += 4;
6160     }
6161   else
6162     {
6163       for (i = 4; i < 12; i++)
6164         {
6165           bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6166           p += 4;
6167         }
6168       bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6169       p += 4;
6170     }
6171   bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6172   p += 4;
6173   bfd_put_32 (obfd, MTLR_R0, p);
6174   p += 4;
6175   bfd_put_32 (obfd, BLR, p);
6176   p += 4;
6177   return p;
6178 }
6179
6180 /* Called via elf_link_hash_traverse to transfer dynamic linking
6181    information on function code symbol entries to their corresponding
6182    function descriptor symbol entries.  */
6183
6184 static bfd_boolean
6185 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6186 {
6187   struct bfd_link_info *info;
6188   struct ppc_link_hash_table *htab;
6189   struct ppc_link_hash_entry *fh;
6190   struct ppc_link_hash_entry *fdh;
6191   bfd_boolean force_local;
6192
6193   fh = ppc_elf_hash_entry (h);
6194   if (fh->elf.root.type == bfd_link_hash_indirect)
6195     return TRUE;
6196
6197   if (!fh->is_func)
6198     return TRUE;
6199
6200   if (fh->elf.root.root.string[0] != '.'
6201       || fh->elf.root.root.string[1] == '\0')
6202     return TRUE;
6203
6204   info = inf;
6205   htab = ppc_hash_table (info);
6206   if (htab == NULL)
6207     return FALSE;
6208
6209   /* Find the corresponding function descriptor symbol.  */
6210   fdh = lookup_fdh (fh, htab);
6211
6212   /* Resolve undefined references to dot-symbols as the value
6213      in the function descriptor, if we have one in a regular object.
6214      This is to satisfy cases like ".quad .foo".  Calls to functions
6215      in dynamic objects are handled elsewhere.  */
6216   if ((fh->elf.root.type == bfd_link_hash_undefined
6217        || fh->elf.root.type == bfd_link_hash_undefweak)
6218       && (fdh->elf.root.type == bfd_link_hash_defined
6219           || fdh->elf.root.type == bfd_link_hash_defweak)
6220       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6221       && opd_entry_value (fdh->elf.root.u.def.section,
6222                           fdh->elf.root.u.def.value,
6223                           &fh->elf.root.u.def.section,
6224                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6225     {
6226       fh->elf.root.type = fdh->elf.root.type;
6227       fh->elf.forced_local = 1;
6228       fh->elf.def_regular = fdh->elf.def_regular;
6229       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6230     }
6231
6232   if (!fh->elf.dynamic)
6233     {
6234       struct plt_entry *ent;
6235
6236       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6237         if (ent->plt.refcount > 0)
6238           break;
6239       if (ent == NULL)
6240         return TRUE;
6241     }
6242
6243   /* Create a descriptor as undefined if necessary.  */
6244   if (fdh == NULL
6245       && !bfd_link_executable (info)
6246       && (fh->elf.root.type == bfd_link_hash_undefined
6247           || fh->elf.root.type == bfd_link_hash_undefweak))
6248     {
6249       fdh = make_fdh (info, fh);
6250       if (fdh == NULL)
6251         return FALSE;
6252     }
6253
6254   /* We can't support overriding of symbols on a fake descriptor.  */
6255   if (fdh != NULL
6256       && fdh->fake
6257       && (fh->elf.root.type == bfd_link_hash_defined
6258           || fh->elf.root.type == bfd_link_hash_defweak))
6259     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6260
6261   /* Transfer dynamic linking information to the function descriptor.  */
6262   if (fdh != NULL)
6263     {
6264       fdh->elf.ref_regular |= fh->elf.ref_regular;
6265       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6266       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6267       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6268       fdh->elf.dynamic |= fh->elf.dynamic;
6269       fdh->elf.needs_plt |= (fh->elf.needs_plt
6270                              || fh->elf.type == STT_FUNC
6271                              || fh->elf.type == STT_GNU_IFUNC);
6272       move_plt_plist (fh, fdh);
6273
6274       if (!fdh->elf.forced_local
6275           && fh->elf.dynindx != -1)
6276         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6277           return FALSE;
6278     }
6279
6280   /* Now that the info is on the function descriptor, clear the
6281      function code sym info.  Any function code syms for which we
6282      don't have a definition in a regular file, we force local.
6283      This prevents a shared library from exporting syms that have
6284      been imported from another library.  Function code syms that
6285      are really in the library we must leave global to prevent the
6286      linker dragging in a definition from a static library.  */
6287   force_local = (!fh->elf.def_regular
6288                  || fdh == NULL
6289                  || !fdh->elf.def_regular
6290                  || fdh->elf.forced_local);
6291   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6292
6293   return TRUE;
6294 }
6295
6296 static const struct sfpr_def_parms save_res_funcs[] =
6297   {
6298     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6299     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6300     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6301     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6302     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6303     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6304     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6305     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6306     { "._savef", 14, 31, savefpr, savefpr1_tail },
6307     { "._restf", 14, 31, restfpr, restfpr1_tail },
6308     { "_savevr_", 20, 31, savevr, savevr_tail },
6309     { "_restvr_", 20, 31, restvr, restvr_tail }
6310   };
6311
6312 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6313    this hook to a) provide some gcc support functions, and b) transfer
6314    dynamic linking information gathered so far on function code symbol
6315    entries, to their corresponding function descriptor symbol entries.  */
6316
6317 static bfd_boolean
6318 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6319                             struct bfd_link_info *info)
6320 {
6321   struct ppc_link_hash_table *htab;
6322
6323   htab = ppc_hash_table (info);
6324   if (htab == NULL)
6325     return FALSE;
6326
6327   /* Provide any missing _save* and _rest* functions.  */
6328   if (htab->sfpr != NULL)
6329     {
6330       unsigned int i;
6331
6332       htab->sfpr->size = 0;
6333       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6334         if (!sfpr_define (info, &save_res_funcs[i], NULL))
6335           return FALSE;
6336       if (htab->sfpr->size == 0)
6337         htab->sfpr->flags |= SEC_EXCLUDE;
6338     }
6339
6340   if (bfd_link_relocatable (info))
6341     return TRUE;
6342
6343   if (htab->elf.hgot != NULL)
6344     {
6345       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6346       /* Make .TOC. defined so as to prevent it being made dynamic.
6347          The wrong value here is fixed later in ppc64_elf_set_toc.  */
6348       if (!htab->elf.hgot->def_regular
6349           || htab->elf.hgot->root.type != bfd_link_hash_defined)
6350         {
6351           htab->elf.hgot->root.type = bfd_link_hash_defined;
6352           htab->elf.hgot->root.u.def.value = 0;
6353           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6354           htab->elf.hgot->def_regular = 1;
6355           htab->elf.hgot->root.linker_def = 1;
6356         }
6357       htab->elf.hgot->type = STT_OBJECT;
6358       htab->elf.hgot->other
6359         = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6360     }
6361
6362   if (htab->need_func_desc_adj)
6363     {
6364       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6365       htab->need_func_desc_adj = 0;
6366     }
6367
6368   return TRUE;
6369 }
6370
6371 /* Find dynamic relocs for H that apply to read-only sections.  */
6372
6373 static asection *
6374 readonly_dynrelocs (struct elf_link_hash_entry *h)
6375 {
6376   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6377   struct elf_dyn_relocs *p;
6378
6379   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6380     {
6381       asection *s = p->sec->output_section;
6382
6383       if (s != NULL && (s->flags & SEC_READONLY) != 0)
6384         return p->sec;
6385     }
6386   return NULL;
6387 }
6388
6389 /* Return true if we have dynamic relocs against H or any of its weak
6390    aliases, that apply to read-only sections.  Cannot be used after
6391    size_dynamic_sections.  */
6392
6393 static bfd_boolean
6394 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6395 {
6396   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6397   do
6398     {
6399       if (readonly_dynrelocs (&eh->elf))
6400         return TRUE;
6401       eh = ppc_elf_hash_entry (eh->elf.u.alias);
6402     }
6403   while (eh != NULL && &eh->elf != h);
6404
6405   return FALSE;
6406 }
6407
6408 /* Return whether EH has pc-relative dynamic relocs.  */
6409
6410 static bfd_boolean
6411 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6412 {
6413   struct elf_dyn_relocs *p;
6414
6415   for (p = eh->dyn_relocs; p != NULL; p = p->next)
6416     if (p->pc_count != 0)
6417       return TRUE;
6418   return FALSE;
6419 }
6420
6421 /* Return true if a global entry stub will be created for H.  Valid
6422    for ELFv2 before plt entries have been allocated.  */
6423
6424 static bfd_boolean
6425 global_entry_stub (struct elf_link_hash_entry *h)
6426 {
6427   struct plt_entry *pent;
6428
6429   if (!h->pointer_equality_needed
6430       || h->def_regular)
6431     return FALSE;
6432
6433   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6434     if (pent->plt.refcount > 0
6435         && pent->addend == 0)
6436       return TRUE;
6437
6438   return FALSE;
6439 }
6440
6441 /* Adjust a symbol defined by a dynamic object and referenced by a
6442    regular object.  The current definition is in some section of the
6443    dynamic object, but we're not including those sections.  We have to
6444    change the definition to something the rest of the link can
6445    understand.  */
6446
6447 static bfd_boolean
6448 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6449                                  struct elf_link_hash_entry *h)
6450 {
6451   struct ppc_link_hash_table *htab;
6452   asection *s, *srel;
6453
6454   htab = ppc_hash_table (info);
6455   if (htab == NULL)
6456     return FALSE;
6457
6458   /* Deal with function syms.  */
6459   if (h->type == STT_FUNC
6460       || h->type == STT_GNU_IFUNC
6461       || h->needs_plt)
6462     {
6463       bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
6464                            || SYMBOL_CALLS_LOCAL (info, h)
6465                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6466       /* Discard dyn_relocs when non-pic if we've decided that a
6467          function symbol is local and not an ifunc.  We keep dynamic
6468          relocs for ifuncs when local rather than always emitting a
6469          plt call stub for them and defining the symbol on the call
6470          stub.  We can't do that for ELFv1 anyway (a function symbol
6471          is defined on a descriptor, not code) and it can be faster at
6472          run-time due to not needing to bounce through a stub.  The
6473          dyn_relocs for ifuncs will be applied even in a static
6474          executable.  */
6475       if (!bfd_link_pic (info)
6476           && h->type != STT_GNU_IFUNC
6477           && local)
6478         ppc_elf_hash_entry (h)->dyn_relocs = NULL;
6479
6480       /* Clear procedure linkage table information for any symbol that
6481          won't need a .plt entry.  */
6482       struct plt_entry *ent;
6483       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6484         if (ent->plt.refcount > 0)
6485           break;
6486       if (ent == NULL
6487           || (h->type != STT_GNU_IFUNC
6488               && local
6489               && (htab->can_convert_all_inline_plt
6490                   || (ppc_elf_hash_entry (h)->tls_mask
6491                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6492         {
6493           h->plt.plist = NULL;
6494           h->needs_plt = 0;
6495           h->pointer_equality_needed = 0;
6496         }
6497       else if (abiversion (info->output_bfd) >= 2)
6498         {
6499           /* Taking a function's address in a read/write section
6500              doesn't require us to define the function symbol in the
6501              executable on a global entry stub.  A dynamic reloc can
6502              be used instead.  The reason we prefer a few more dynamic
6503              relocs is that calling via a global entry stub costs a
6504              few more instructions, and pointer_equality_needed causes
6505              extra work in ld.so when resolving these symbols.  */
6506           if (global_entry_stub (h))
6507             {
6508               if (!readonly_dynrelocs (h))
6509                 {
6510                   h->pointer_equality_needed = 0;
6511                   /* If we haven't seen a branch reloc and the symbol
6512                      isn't an ifunc then we don't need a plt entry.  */
6513                   if (!h->needs_plt)
6514                     h->plt.plist = NULL;
6515                 }
6516               else if (!bfd_link_pic (info))
6517                 /* We are going to be defining the function symbol on the
6518                    plt stub, so no dyn_relocs needed when non-pic.  */
6519                 ppc_elf_hash_entry (h)->dyn_relocs = NULL;
6520             }
6521
6522           /* ELFv2 function symbols can't have copy relocs.  */
6523           return TRUE;
6524         }
6525       else if (!h->needs_plt
6526                && !readonly_dynrelocs (h))
6527         {
6528           /* If we haven't seen a branch reloc and the symbol isn't an
6529              ifunc then we don't need a plt entry.  */
6530           h->plt.plist = NULL;
6531           h->pointer_equality_needed = 0;
6532           return TRUE;
6533         }
6534     }
6535   else
6536     h->plt.plist = NULL;
6537
6538   /* If this is a weak symbol, and there is a real definition, the
6539      processor independent code will have arranged for us to see the
6540      real definition first, and we can just use the same value.  */
6541   if (h->is_weakalias)
6542     {
6543       struct elf_link_hash_entry *def = weakdef (h);
6544       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6545       h->root.u.def.section = def->root.u.def.section;
6546       h->root.u.def.value = def->root.u.def.value;
6547       if (def->root.u.def.section == htab->elf.sdynbss
6548           || def->root.u.def.section == htab->elf.sdynrelro)
6549         ppc_elf_hash_entry (h)->dyn_relocs = NULL;
6550       return TRUE;
6551     }
6552
6553   /* If we are creating a shared library, we must presume that the
6554      only references to the symbol are via the global offset table.
6555      For such cases we need not do anything here; the relocations will
6556      be handled correctly by relocate_section.  */
6557   if (!bfd_link_executable (info))
6558     return TRUE;
6559
6560   /* If there are no references to this symbol that do not use the
6561      GOT, we don't need to generate a copy reloc.  */
6562   if (!h->non_got_ref)
6563     return TRUE;
6564
6565   /* Don't generate a copy reloc for symbols defined in the executable.  */
6566   if (!h->def_dynamic || !h->ref_regular || h->def_regular
6567
6568       /* If -z nocopyreloc was given, don't generate them either.  */
6569       || info->nocopyreloc
6570
6571       /* If we don't find any dynamic relocs in read-only sections, then
6572          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6573       || (ELIMINATE_COPY_RELOCS
6574           && !h->needs_copy
6575           && !alias_readonly_dynrelocs (h))
6576
6577       /* Protected variables do not work with .dynbss.  The copy in
6578          .dynbss won't be used by the shared library with the protected
6579          definition for the variable.  Text relocations are preferable
6580          to an incorrect program.  */
6581       || h->protected_def)
6582     return TRUE;
6583
6584   if (h->type == STT_FUNC
6585       || h->type == STT_GNU_IFUNC)
6586     {
6587       /* .dynbss copies of function symbols only work if we have
6588          ELFv1 dot-symbols.  ELFv1 compilers since 2004 default to not
6589          use dot-symbols and set the function symbol size to the text
6590          size of the function rather than the size of the descriptor.
6591          That's wrong for copying a descriptor.  */
6592       if (ppc_elf_hash_entry (h)->oh == NULL
6593           || !(h->size == 24 || h->size == 16))
6594         return TRUE;
6595
6596       /* We should never get here, but unfortunately there are old
6597          versions of gcc (circa gcc-3.2) that improperly for the
6598          ELFv1 ABI put initialized function pointers, vtable refs and
6599          suchlike in read-only sections.  Allow them to proceed, but
6600          warn that this might break at runtime.  */
6601       info->callbacks->einfo
6602         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6603            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6604          h->root.root.string);
6605     }
6606
6607   /* This is a reference to a symbol defined by a dynamic object which
6608      is not a function.  */
6609
6610   /* We must allocate the symbol in our .dynbss section, which will
6611      become part of the .bss section of the executable.  There will be
6612      an entry for this symbol in the .dynsym section.  The dynamic
6613      object will contain position independent code, so all references
6614      from the dynamic object to this symbol will go through the global
6615      offset table.  The dynamic linker will use the .dynsym entry to
6616      determine the address it must put in the global offset table, so
6617      both the dynamic object and the regular object will refer to the
6618      same memory location for the variable.  */
6619   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6620     {
6621       s = htab->elf.sdynrelro;
6622       srel = htab->elf.sreldynrelro;
6623     }
6624   else
6625     {
6626       s = htab->elf.sdynbss;
6627       srel = htab->elf.srelbss;
6628     }
6629   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6630     {
6631       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6632          linker to copy the initial value out of the dynamic object
6633          and into the runtime process image.  */
6634       srel->size += sizeof (Elf64_External_Rela);
6635       h->needs_copy = 1;
6636     }
6637
6638   /* We no longer want dyn_relocs.  */
6639   ppc_elf_hash_entry (h)->dyn_relocs = NULL;
6640   return _bfd_elf_adjust_dynamic_copy (info, h, s);
6641 }
6642
6643 /* If given a function descriptor symbol, hide both the function code
6644    sym and the descriptor.  */
6645 static void
6646 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6647                        struct elf_link_hash_entry *h,
6648                        bfd_boolean force_local)
6649 {
6650   struct ppc_link_hash_entry *eh;
6651   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6652
6653   if (ppc_hash_table (info) == NULL)
6654     return;
6655
6656   eh = ppc_elf_hash_entry (h);
6657   if (eh->is_func_descriptor)
6658     {
6659       struct ppc_link_hash_entry *fh = eh->oh;
6660
6661       if (fh == NULL)
6662         {
6663           const char *p, *q;
6664           struct elf_link_hash_table *htab = elf_hash_table (info);
6665           char save;
6666
6667           /* We aren't supposed to use alloca in BFD because on
6668              systems which do not have alloca the version in libiberty
6669              calls xmalloc, which might cause the program to crash
6670              when it runs out of memory.  This function doesn't have a
6671              return status, so there's no way to gracefully return an
6672              error.  So cheat.  We know that string[-1] can be safely
6673              accessed;  It's either a string in an ELF string table,
6674              or allocated in an objalloc structure.  */
6675
6676           p = eh->elf.root.root.string - 1;
6677           save = *p;
6678           *(char *) p = '.';
6679           fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6680                                                          FALSE, FALSE));
6681           *(char *) p = save;
6682
6683           /* Unfortunately, if it so happens that the string we were
6684              looking for was allocated immediately before this string,
6685              then we overwrote the string terminator.  That's the only
6686              reason the lookup should fail.  */
6687           if (fh == NULL)
6688             {
6689               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6690               while (q >= eh->elf.root.root.string && *q == *p)
6691                 --q, --p;
6692               if (q < eh->elf.root.root.string && *p == '.')
6693                 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6694                                                                FALSE, FALSE));
6695             }
6696           if (fh != NULL)
6697             {
6698               eh->oh = fh;
6699               fh->oh = eh;
6700             }
6701         }
6702       if (fh != NULL)
6703         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6704     }
6705 }
6706
6707 static bfd_boolean
6708 get_sym_h (struct elf_link_hash_entry **hp,
6709            Elf_Internal_Sym **symp,
6710            asection **symsecp,
6711            unsigned char **tls_maskp,
6712            Elf_Internal_Sym **locsymsp,
6713            unsigned long r_symndx,
6714            bfd *ibfd)
6715 {
6716   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6717
6718   if (r_symndx >= symtab_hdr->sh_info)
6719     {
6720       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6721       struct elf_link_hash_entry *h;
6722
6723       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6724       h = elf_follow_link (h);
6725
6726       if (hp != NULL)
6727         *hp = h;
6728
6729       if (symp != NULL)
6730         *symp = NULL;
6731
6732       if (symsecp != NULL)
6733         {
6734           asection *symsec = NULL;
6735           if (h->root.type == bfd_link_hash_defined
6736               || h->root.type == bfd_link_hash_defweak)
6737             symsec = h->root.u.def.section;
6738           *symsecp = symsec;
6739         }
6740
6741       if (tls_maskp != NULL)
6742         *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6743     }
6744   else
6745     {
6746       Elf_Internal_Sym *sym;
6747       Elf_Internal_Sym *locsyms = *locsymsp;
6748
6749       if (locsyms == NULL)
6750         {
6751           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6752           if (locsyms == NULL)
6753             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6754                                             symtab_hdr->sh_info,
6755                                             0, NULL, NULL, NULL);
6756           if (locsyms == NULL)
6757             return FALSE;
6758           *locsymsp = locsyms;
6759         }
6760       sym = locsyms + r_symndx;
6761
6762       if (hp != NULL)
6763         *hp = NULL;
6764
6765       if (symp != NULL)
6766         *symp = sym;
6767
6768       if (symsecp != NULL)
6769         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6770
6771       if (tls_maskp != NULL)
6772         {
6773           struct got_entry **lgot_ents;
6774           unsigned char *tls_mask;
6775
6776           tls_mask = NULL;
6777           lgot_ents = elf_local_got_ents (ibfd);
6778           if (lgot_ents != NULL)
6779             {
6780               struct plt_entry **local_plt = (struct plt_entry **)
6781                 (lgot_ents + symtab_hdr->sh_info);
6782               unsigned char *lgot_masks = (unsigned char *)
6783                 (local_plt + symtab_hdr->sh_info);
6784               tls_mask = &lgot_masks[r_symndx];
6785             }
6786           *tls_maskp = tls_mask;
6787         }
6788     }
6789   return TRUE;
6790 }
6791
6792 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6793    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6794    type suitable for optimization, and 1 otherwise.  */
6795
6796 static int
6797 get_tls_mask (unsigned char **tls_maskp,
6798               unsigned long *toc_symndx,
6799               bfd_vma *toc_addend,
6800               Elf_Internal_Sym **locsymsp,
6801               const Elf_Internal_Rela *rel,
6802               bfd *ibfd)
6803 {
6804   unsigned long r_symndx;
6805   int next_r;
6806   struct elf_link_hash_entry *h;
6807   Elf_Internal_Sym *sym;
6808   asection *sec;
6809   bfd_vma off;
6810
6811   r_symndx = ELF64_R_SYM (rel->r_info);
6812   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6813     return 0;
6814
6815   if ((*tls_maskp != NULL
6816        && (**tls_maskp & TLS_TLS) != 0
6817        && **tls_maskp != (TLS_TLS | TLS_MARK))
6818       || sec == NULL
6819       || ppc64_elf_section_data (sec) == NULL
6820       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6821     return 1;
6822
6823   /* Look inside a TOC section too.  */
6824   if (h != NULL)
6825     {
6826       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6827       off = h->root.u.def.value;
6828     }
6829   else
6830     off = sym->st_value;
6831   off += rel->r_addend;
6832   BFD_ASSERT (off % 8 == 0);
6833   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6834   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6835   if (toc_symndx != NULL)
6836     *toc_symndx = r_symndx;
6837   if (toc_addend != NULL)
6838     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6839   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6840     return 0;
6841   if ((h == NULL || is_static_defined (h))
6842       && (next_r == -1 || next_r == -2))
6843     return 1 - next_r;
6844   return 1;
6845 }
6846
6847 /* Find (or create) an entry in the tocsave hash table.  */
6848
6849 static struct tocsave_entry *
6850 tocsave_find (struct ppc_link_hash_table *htab,
6851               enum insert_option insert,
6852               Elf_Internal_Sym **local_syms,
6853               const Elf_Internal_Rela *irela,
6854               bfd *ibfd)
6855 {
6856   unsigned long r_indx;
6857   struct elf_link_hash_entry *h;
6858   Elf_Internal_Sym *sym;
6859   struct tocsave_entry ent, *p;
6860   hashval_t hash;
6861   struct tocsave_entry **slot;
6862
6863   r_indx = ELF64_R_SYM (irela->r_info);
6864   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6865     return NULL;
6866   if (ent.sec == NULL || ent.sec->output_section == NULL)
6867     {
6868       _bfd_error_handler
6869         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6870       return NULL;
6871     }
6872
6873   if (h != NULL)
6874     ent.offset = h->root.u.def.value;
6875   else
6876     ent.offset = sym->st_value;
6877   ent.offset += irela->r_addend;
6878
6879   hash = tocsave_htab_hash (&ent);
6880   slot = ((struct tocsave_entry **)
6881           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6882   if (slot == NULL)
6883     return NULL;
6884
6885   if (*slot == NULL)
6886     {
6887       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6888       if (p == NULL)
6889         return NULL;
6890       *p = ent;
6891       *slot = p;
6892     }
6893   return *slot;
6894 }
6895
6896 /* Adjust all global syms defined in opd sections.  In gcc generated
6897    code for the old ABI, these will already have been done.  */
6898
6899 static bfd_boolean
6900 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6901 {
6902   struct ppc_link_hash_entry *eh;
6903   asection *sym_sec;
6904   struct _opd_sec_data *opd;
6905
6906   if (h->root.type == bfd_link_hash_indirect)
6907     return TRUE;
6908
6909   if (h->root.type != bfd_link_hash_defined
6910       && h->root.type != bfd_link_hash_defweak)
6911     return TRUE;
6912
6913   eh = ppc_elf_hash_entry (h);
6914   if (eh->adjust_done)
6915     return TRUE;
6916
6917   sym_sec = eh->elf.root.u.def.section;
6918   opd = get_opd_info (sym_sec);
6919   if (opd != NULL && opd->adjust != NULL)
6920     {
6921       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6922       if (adjust == -1)
6923         {
6924           /* This entry has been deleted.  */
6925           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6926           if (dsec == NULL)
6927             {
6928               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6929                 if (discarded_section (dsec))
6930                   {
6931                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6932                     break;
6933                   }
6934             }
6935           eh->elf.root.u.def.value = 0;
6936           eh->elf.root.u.def.section = dsec;
6937         }
6938       else
6939         eh->elf.root.u.def.value += adjust;
6940       eh->adjust_done = 1;
6941     }
6942   return TRUE;
6943 }
6944
6945 /* Handles decrementing dynamic reloc counts for the reloc specified by
6946    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
6947    have already been determined.  */
6948
6949 static bfd_boolean
6950 dec_dynrel_count (bfd_vma r_info,
6951                   asection *sec,
6952                   struct bfd_link_info *info,
6953                   Elf_Internal_Sym **local_syms,
6954                   struct elf_link_hash_entry *h,
6955                   Elf_Internal_Sym *sym)
6956 {
6957   enum elf_ppc64_reloc_type r_type;
6958   asection *sym_sec = NULL;
6959
6960   /* Can this reloc be dynamic?  This switch, and later tests here
6961      should be kept in sync with the code in check_relocs.  */
6962   r_type = ELF64_R_TYPE (r_info);
6963   switch (r_type)
6964     {
6965     default:
6966       return TRUE;
6967
6968     case R_PPC64_TOC16:
6969     case R_PPC64_TOC16_DS:
6970     case R_PPC64_TOC16_LO:
6971     case R_PPC64_TOC16_HI:
6972     case R_PPC64_TOC16_HA:
6973     case R_PPC64_TOC16_LO_DS:
6974       if (h == NULL)
6975         return TRUE;
6976       break;
6977
6978     case R_PPC64_TPREL16:
6979     case R_PPC64_TPREL16_LO:
6980     case R_PPC64_TPREL16_HI:
6981     case R_PPC64_TPREL16_HA:
6982     case R_PPC64_TPREL16_DS:
6983     case R_PPC64_TPREL16_LO_DS:
6984     case R_PPC64_TPREL16_HIGH:
6985     case R_PPC64_TPREL16_HIGHA:
6986     case R_PPC64_TPREL16_HIGHER:
6987     case R_PPC64_TPREL16_HIGHERA:
6988     case R_PPC64_TPREL16_HIGHEST:
6989     case R_PPC64_TPREL16_HIGHESTA:
6990     case R_PPC64_TPREL64:
6991     case R_PPC64_TPREL34:
6992     case R_PPC64_DTPMOD64:
6993     case R_PPC64_DTPREL64:
6994     case R_PPC64_ADDR64:
6995     case R_PPC64_REL30:
6996     case R_PPC64_REL32:
6997     case R_PPC64_REL64:
6998     case R_PPC64_ADDR14:
6999     case R_PPC64_ADDR14_BRNTAKEN:
7000     case R_PPC64_ADDR14_BRTAKEN:
7001     case R_PPC64_ADDR16:
7002     case R_PPC64_ADDR16_DS:
7003     case R_PPC64_ADDR16_HA:
7004     case R_PPC64_ADDR16_HI:
7005     case R_PPC64_ADDR16_HIGH:
7006     case R_PPC64_ADDR16_HIGHA:
7007     case R_PPC64_ADDR16_HIGHER:
7008     case R_PPC64_ADDR16_HIGHERA:
7009     case R_PPC64_ADDR16_HIGHEST:
7010     case R_PPC64_ADDR16_HIGHESTA:
7011     case R_PPC64_ADDR16_LO:
7012     case R_PPC64_ADDR16_LO_DS:
7013     case R_PPC64_ADDR24:
7014     case R_PPC64_ADDR32:
7015     case R_PPC64_UADDR16:
7016     case R_PPC64_UADDR32:
7017     case R_PPC64_UADDR64:
7018     case R_PPC64_TOC:
7019     case R_PPC64_D34:
7020     case R_PPC64_D34_LO:
7021     case R_PPC64_D34_HI30:
7022     case R_PPC64_D34_HA30:
7023     case R_PPC64_ADDR16_HIGHER34:
7024     case R_PPC64_ADDR16_HIGHERA34:
7025     case R_PPC64_ADDR16_HIGHEST34:
7026     case R_PPC64_ADDR16_HIGHESTA34:
7027     case R_PPC64_D28:
7028       break;
7029     }
7030
7031   if (local_syms != NULL)
7032     {
7033       unsigned long r_symndx;
7034       bfd *ibfd = sec->owner;
7035
7036       r_symndx = ELF64_R_SYM (r_info);
7037       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7038         return FALSE;
7039     }
7040
7041   if ((h != NULL
7042        && (h->root.type == bfd_link_hash_defweak
7043            || !h->def_regular))
7044       || (h != NULL
7045           && !bfd_link_executable (info)
7046           && !SYMBOLIC_BIND (info, h))
7047       || (bfd_link_pic (info)
7048           && must_be_dyn_reloc (info, r_type))
7049       || (!bfd_link_pic (info)
7050           && (h != NULL
7051               ? h->type == STT_GNU_IFUNC
7052               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7053     ;
7054   else
7055     return TRUE;
7056
7057   if (h != NULL)
7058     {
7059       struct elf_dyn_relocs *p;
7060       struct elf_dyn_relocs **pp;
7061       pp = &ppc_elf_hash_entry (h)->dyn_relocs;
7062
7063       /* elf_gc_sweep may have already removed all dyn relocs associated
7064          with local syms for a given section.  Also, symbol flags are
7065          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7066          report a dynreloc miscount.  */
7067       if (*pp == NULL && info->gc_sections)
7068         return TRUE;
7069
7070       while ((p = *pp) != NULL)
7071         {
7072           if (p->sec == sec)
7073             {
7074               if (!must_be_dyn_reloc (info, r_type))
7075                 p->pc_count -= 1;
7076               p->count -= 1;
7077               if (p->count == 0)
7078                 *pp = p->next;
7079               return TRUE;
7080             }
7081           pp = &p->next;
7082         }
7083     }
7084   else
7085     {
7086       struct ppc_dyn_relocs *p;
7087       struct ppc_dyn_relocs **pp;
7088       void *vpp;
7089       bfd_boolean is_ifunc;
7090
7091       if (local_syms == NULL)
7092         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7093       if (sym_sec == NULL)
7094         sym_sec = sec;
7095
7096       vpp = &elf_section_data (sym_sec)->local_dynrel;
7097       pp = (struct ppc_dyn_relocs **) vpp;
7098
7099       if (*pp == NULL && info->gc_sections)
7100         return TRUE;
7101
7102       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7103       while ((p = *pp) != NULL)
7104         {
7105           if (p->sec == sec && p->ifunc == is_ifunc)
7106             {
7107               p->count -= 1;
7108               if (p->count == 0)
7109                 *pp = p->next;
7110               return TRUE;
7111             }
7112           pp = &p->next;
7113         }
7114     }
7115
7116   /* xgettext:c-format */
7117   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7118                       sec->owner, sec);
7119   bfd_set_error (bfd_error_bad_value);
7120   return FALSE;
7121 }
7122
7123 /* Remove unused Official Procedure Descriptor entries.  Currently we
7124    only remove those associated with functions in discarded link-once
7125    sections, or weakly defined functions that have been overridden.  It
7126    would be possible to remove many more entries for statically linked
7127    applications.  */
7128
7129 bfd_boolean
7130 ppc64_elf_edit_opd (struct bfd_link_info *info)
7131 {
7132   bfd *ibfd;
7133   bfd_boolean some_edited = FALSE;
7134   asection *need_pad = NULL;
7135   struct ppc_link_hash_table *htab;
7136
7137   htab = ppc_hash_table (info);
7138   if (htab == NULL)
7139     return FALSE;
7140
7141   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7142     {
7143       asection *sec;
7144       Elf_Internal_Rela *relstart, *rel, *relend;
7145       Elf_Internal_Shdr *symtab_hdr;
7146       Elf_Internal_Sym *local_syms;
7147       struct _opd_sec_data *opd;
7148       bfd_boolean need_edit, add_aux_fields, broken;
7149       bfd_size_type cnt_16b = 0;
7150
7151       if (!is_ppc64_elf (ibfd))
7152         continue;
7153
7154       sec = bfd_get_section_by_name (ibfd, ".opd");
7155       if (sec == NULL || sec->size == 0)
7156         continue;
7157
7158       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7159         continue;
7160
7161       if (sec->output_section == bfd_abs_section_ptr)
7162         continue;
7163
7164       /* Look through the section relocs.  */
7165       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7166         continue;
7167
7168       local_syms = NULL;
7169       symtab_hdr = &elf_symtab_hdr (ibfd);
7170
7171       /* Read the relocations.  */
7172       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7173                                             info->keep_memory);
7174       if (relstart == NULL)
7175         return FALSE;
7176
7177       /* First run through the relocs to check they are sane, and to
7178          determine whether we need to edit this opd section.  */
7179       need_edit = FALSE;
7180       broken = FALSE;
7181       need_pad = sec;
7182       relend = relstart + sec->reloc_count;
7183       for (rel = relstart; rel < relend; )
7184         {
7185           enum elf_ppc64_reloc_type r_type;
7186           unsigned long r_symndx;
7187           asection *sym_sec;
7188           struct elf_link_hash_entry *h;
7189           Elf_Internal_Sym *sym;
7190           bfd_vma offset;
7191
7192           /* .opd contains an array of 16 or 24 byte entries.  We're
7193              only interested in the reloc pointing to a function entry
7194              point.  */
7195           offset = rel->r_offset;
7196           if (rel + 1 == relend
7197               || rel[1].r_offset != offset + 8)
7198             {
7199               /* If someone messes with .opd alignment then after a
7200                  "ld -r" we might have padding in the middle of .opd.
7201                  Also, there's nothing to prevent someone putting
7202                  something silly in .opd with the assembler.  No .opd
7203                  optimization for them!  */
7204             broken_opd:
7205               _bfd_error_handler
7206                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7207               broken = TRUE;
7208               break;
7209             }
7210
7211           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7212               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7213             {
7214               _bfd_error_handler
7215                 /* xgettext:c-format */
7216                 (_("%pB: unexpected reloc type %u in .opd section"),
7217                  ibfd, r_type);
7218               broken = TRUE;
7219               break;
7220             }
7221
7222           r_symndx = ELF64_R_SYM (rel->r_info);
7223           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7224                           r_symndx, ibfd))
7225             goto error_ret;
7226
7227           if (sym_sec == NULL || sym_sec->owner == NULL)
7228             {
7229               const char *sym_name;
7230               if (h != NULL)
7231                 sym_name = h->root.root.string;
7232               else
7233                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7234                                              sym_sec);
7235
7236               _bfd_error_handler
7237                 /* xgettext:c-format */
7238                 (_("%pB: undefined sym `%s' in .opd section"),
7239                  ibfd, sym_name);
7240               broken = TRUE;
7241               break;
7242             }
7243
7244           /* opd entries are always for functions defined in the
7245              current input bfd.  If the symbol isn't defined in the
7246              input bfd, then we won't be using the function in this
7247              bfd;  It must be defined in a linkonce section in another
7248              bfd, or is weak.  It's also possible that we are
7249              discarding the function due to a linker script /DISCARD/,
7250              which we test for via the output_section.  */
7251           if (sym_sec->owner != ibfd
7252               || sym_sec->output_section == bfd_abs_section_ptr)
7253             need_edit = TRUE;
7254
7255           rel += 2;
7256           if (rel + 1 == relend
7257               || (rel + 2 < relend
7258                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7259             ++rel;
7260
7261           if (rel == relend)
7262             {
7263               if (sec->size == offset + 24)
7264                 {
7265                   need_pad = NULL;
7266                   break;
7267                 }
7268               if (sec->size == offset + 16)
7269                 {
7270                   cnt_16b++;
7271                   break;
7272                 }
7273               goto broken_opd;
7274             }
7275           else if (rel + 1 < relend
7276                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7277                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7278             {
7279               if (rel[0].r_offset == offset + 16)
7280                 cnt_16b++;
7281               else if (rel[0].r_offset != offset + 24)
7282                 goto broken_opd;
7283             }
7284           else
7285             goto broken_opd;
7286         }
7287
7288       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7289
7290       if (!broken && (need_edit || add_aux_fields))
7291         {
7292           Elf_Internal_Rela *write_rel;
7293           Elf_Internal_Shdr *rel_hdr;
7294           bfd_byte *rptr, *wptr;
7295           bfd_byte *new_contents;
7296           bfd_size_type amt;
7297
7298           new_contents = NULL;
7299           amt = OPD_NDX (sec->size) * sizeof (long);
7300           opd = &ppc64_elf_section_data (sec)->u.opd;
7301           opd->adjust = bfd_zalloc (sec->owner, amt);
7302           if (opd->adjust == NULL)
7303             return FALSE;
7304
7305           /* This seems a waste of time as input .opd sections are all
7306              zeros as generated by gcc, but I suppose there's no reason
7307              this will always be so.  We might start putting something in
7308              the third word of .opd entries.  */
7309           if ((sec->flags & SEC_IN_MEMORY) == 0)
7310             {
7311               bfd_byte *loc;
7312               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7313                 {
7314                   if (loc != NULL)
7315                     free (loc);
7316                 error_ret:
7317                   if (local_syms != NULL
7318                       && symtab_hdr->contents != (unsigned char *) local_syms)
7319                     free (local_syms);
7320                   if (elf_section_data (sec)->relocs != relstart)
7321                     free (relstart);
7322                   return FALSE;
7323                 }
7324               sec->contents = loc;
7325               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7326             }
7327
7328           elf_section_data (sec)->relocs = relstart;
7329
7330           new_contents = sec->contents;
7331           if (add_aux_fields)
7332             {
7333               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7334               if (new_contents == NULL)
7335                 return FALSE;
7336               need_pad = NULL;
7337             }
7338           wptr = new_contents;
7339           rptr = sec->contents;
7340           write_rel = relstart;
7341           for (rel = relstart; rel < relend; )
7342             {
7343               unsigned long r_symndx;
7344               asection *sym_sec;
7345               struct elf_link_hash_entry *h;
7346               struct ppc_link_hash_entry *fdh = NULL;
7347               Elf_Internal_Sym *sym;
7348               long opd_ent_size;
7349               Elf_Internal_Rela *next_rel;
7350               bfd_boolean skip;
7351
7352               r_symndx = ELF64_R_SYM (rel->r_info);
7353               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7354                               r_symndx, ibfd))
7355                 goto error_ret;
7356
7357               next_rel = rel + 2;
7358               if (next_rel + 1 == relend
7359                   || (next_rel + 2 < relend
7360                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7361                 ++next_rel;
7362
7363               /* See if the .opd entry is full 24 byte or
7364                  16 byte (with fd_aux entry overlapped with next
7365                  fd_func).  */
7366               opd_ent_size = 24;
7367               if (next_rel == relend)
7368                 {
7369                   if (sec->size == rel->r_offset + 16)
7370                     opd_ent_size = 16;
7371                 }
7372               else if (next_rel->r_offset == rel->r_offset + 16)
7373                 opd_ent_size = 16;
7374
7375               if (h != NULL
7376                   && h->root.root.string[0] == '.')
7377                 {
7378                   fdh = ppc_elf_hash_entry (h)->oh;
7379                   if (fdh != NULL)
7380                     {
7381                       fdh = ppc_follow_link (fdh);
7382                       if (fdh->elf.root.type != bfd_link_hash_defined
7383                           && fdh->elf.root.type != bfd_link_hash_defweak)
7384                         fdh = NULL;
7385                     }
7386                 }
7387
7388               skip = (sym_sec->owner != ibfd
7389                       || sym_sec->output_section == bfd_abs_section_ptr);
7390               if (skip)
7391                 {
7392                   if (fdh != NULL && sym_sec->owner == ibfd)
7393                     {
7394                       /* Arrange for the function descriptor sym
7395                          to be dropped.  */
7396                       fdh->elf.root.u.def.value = 0;
7397                       fdh->elf.root.u.def.section = sym_sec;
7398                     }
7399                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7400
7401                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7402                     rel = next_rel;
7403                   else
7404                     while (1)
7405                       {
7406                         if (!dec_dynrel_count (rel->r_info, sec, info,
7407                                                NULL, h, sym))
7408                           goto error_ret;
7409
7410                         if (++rel == next_rel)
7411                           break;
7412
7413                         r_symndx = ELF64_R_SYM (rel->r_info);
7414                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7415                                         r_symndx, ibfd))
7416                           goto error_ret;
7417                       }
7418                 }
7419               else
7420                 {
7421                   /* We'll be keeping this opd entry.  */
7422                   long adjust;
7423
7424                   if (fdh != NULL)
7425                     {
7426                       /* Redefine the function descriptor symbol to
7427                          this location in the opd section.  It is
7428                          necessary to update the value here rather
7429                          than using an array of adjustments as we do
7430                          for local symbols, because various places
7431                          in the generic ELF code use the value
7432                          stored in u.def.value.  */
7433                       fdh->elf.root.u.def.value = wptr - new_contents;
7434                       fdh->adjust_done = 1;
7435                     }
7436
7437                   /* Local syms are a bit tricky.  We could
7438                      tweak them as they can be cached, but
7439                      we'd need to look through the local syms
7440                      for the function descriptor sym which we
7441                      don't have at the moment.  So keep an
7442                      array of adjustments.  */
7443                   adjust = (wptr - new_contents) - (rptr - sec->contents);
7444                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7445
7446                   if (wptr != rptr)
7447                     memcpy (wptr, rptr, opd_ent_size);
7448                   wptr += opd_ent_size;
7449                   if (add_aux_fields && opd_ent_size == 16)
7450                     {
7451                       memset (wptr, '\0', 8);
7452                       wptr += 8;
7453                     }
7454
7455                   /* We need to adjust any reloc offsets to point to the
7456                      new opd entries.  */
7457                   for ( ; rel != next_rel; ++rel)
7458                     {
7459                       rel->r_offset += adjust;
7460                       if (write_rel != rel)
7461                         memcpy (write_rel, rel, sizeof (*rel));
7462                       ++write_rel;
7463                     }
7464                 }
7465
7466               rptr += opd_ent_size;
7467             }
7468
7469           sec->size = wptr - new_contents;
7470           sec->reloc_count = write_rel - relstart;
7471           if (add_aux_fields)
7472             {
7473               free (sec->contents);
7474               sec->contents = new_contents;
7475             }
7476
7477           /* Fudge the header size too, as this is used later in
7478              elf_bfd_final_link if we are emitting relocs.  */
7479           rel_hdr = _bfd_elf_single_rel_hdr (sec);
7480           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7481           some_edited = TRUE;
7482         }
7483       else if (elf_section_data (sec)->relocs != relstart)
7484         free (relstart);
7485
7486       if (local_syms != NULL
7487           && symtab_hdr->contents != (unsigned char *) local_syms)
7488         {
7489           if (!info->keep_memory)
7490             free (local_syms);
7491           else
7492             symtab_hdr->contents = (unsigned char *) local_syms;
7493         }
7494     }
7495
7496   if (some_edited)
7497     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7498
7499   /* If we are doing a final link and the last .opd entry is just 16 byte
7500      long, add a 8 byte padding after it.  */
7501   if (need_pad != NULL && !bfd_link_relocatable (info))
7502     {
7503       bfd_byte *p;
7504
7505       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7506         {
7507           BFD_ASSERT (need_pad->size > 0);
7508
7509           p = bfd_malloc (need_pad->size + 8);
7510           if (p == NULL)
7511             return FALSE;
7512
7513           if (!bfd_get_section_contents (need_pad->owner, need_pad,
7514                                          p, 0, need_pad->size))
7515             return FALSE;
7516
7517           need_pad->contents = p;
7518           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7519         }
7520       else
7521         {
7522           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7523           if (p == NULL)
7524             return FALSE;
7525
7526           need_pad->contents = p;
7527         }
7528
7529       memset (need_pad->contents + need_pad->size, 0, 8);
7530       need_pad->size += 8;
7531     }
7532
7533   return TRUE;
7534 }
7535
7536 /* Analyze inline PLT call relocations to see whether calls to locally
7537    defined functions can be converted to direct calls.  */
7538
7539 bfd_boolean
7540 ppc64_elf_inline_plt (struct bfd_link_info *info)
7541 {
7542   struct ppc_link_hash_table *htab;
7543   bfd *ibfd;
7544   asection *sec;
7545   bfd_vma low_vma, high_vma, limit;
7546
7547   htab = ppc_hash_table (info);
7548   if (htab == NULL)
7549     return FALSE;
7550
7551   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7552      reduced somewhat to cater for possible stubs that might be added
7553      between the call and its destination.  */
7554   if (htab->params->group_size < 0)
7555     {
7556       limit = -htab->params->group_size;
7557       if (limit == 1)
7558         limit = 0x1e00000;
7559     }
7560   else
7561     {
7562       limit = htab->params->group_size;
7563       if (limit == 1)
7564         limit = 0x1c00000;
7565     }
7566
7567   low_vma = -1;
7568   high_vma = 0;
7569   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7570     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7571       {
7572         if (low_vma > sec->vma)
7573           low_vma = sec->vma;
7574         if (high_vma < sec->vma + sec->size)
7575           high_vma = sec->vma + sec->size;
7576       }
7577
7578   /* If a "bl" can reach anywhere in local code sections, then we can
7579      convert all inline PLT sequences to direct calls when the symbol
7580      is local.  */
7581   if (high_vma - low_vma < limit)
7582     {
7583       htab->can_convert_all_inline_plt = 1;
7584       return TRUE;
7585     }
7586
7587   /* Otherwise, go looking through relocs for cases where a direct
7588      call won't reach.  Mark the symbol on any such reloc to disable
7589      the optimization and keep the PLT entry as it seems likely that
7590      this will be better than creating trampolines.  Note that this
7591      will disable the optimization for all inline PLT calls to a
7592      particular symbol, not just those that won't reach.  The
7593      difficulty in doing a more precise optimization is that the
7594      linker needs to make a decision depending on whether a
7595      particular R_PPC64_PLTCALL insn can be turned into a direct
7596      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7597      the sequence, and there is nothing that ties those relocs
7598      together except their symbol.  */
7599
7600   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7601     {
7602       Elf_Internal_Shdr *symtab_hdr;
7603       Elf_Internal_Sym *local_syms;
7604
7605       if (!is_ppc64_elf (ibfd))
7606         continue;
7607
7608       local_syms = NULL;
7609       symtab_hdr = &elf_symtab_hdr (ibfd);
7610
7611       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7612         if (ppc64_elf_section_data (sec)->has_pltcall
7613             && !bfd_is_abs_section (sec->output_section))
7614           {
7615             Elf_Internal_Rela *relstart, *rel, *relend;
7616
7617             /* Read the relocations.  */
7618             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7619                                                   info->keep_memory);
7620             if (relstart == NULL)
7621               return FALSE;
7622
7623             relend = relstart + sec->reloc_count;
7624             for (rel = relstart; rel < relend; rel++)
7625               {
7626                 enum elf_ppc64_reloc_type r_type;
7627                 unsigned long r_symndx;
7628                 asection *sym_sec;
7629                 struct elf_link_hash_entry *h;
7630                 Elf_Internal_Sym *sym;
7631                 unsigned char *tls_maskp;
7632
7633                 r_type = ELF64_R_TYPE (rel->r_info);
7634                 if (r_type != R_PPC64_PLTCALL
7635                     && r_type != R_PPC64_PLTCALL_NOTOC)
7636                   continue;
7637
7638                 r_symndx = ELF64_R_SYM (rel->r_info);
7639                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7640                                 r_symndx, ibfd))
7641                   {
7642                     if (elf_section_data (sec)->relocs != relstart)
7643                       free (relstart);
7644                     if (local_syms != NULL
7645                         && symtab_hdr->contents != (bfd_byte *) local_syms)
7646                       free (local_syms);
7647                     return FALSE;
7648                   }
7649
7650                 if (sym_sec != NULL && sym_sec->output_section != NULL)
7651                   {
7652                     bfd_vma from, to;
7653                     if (h != NULL)
7654                       to = h->root.u.def.value;
7655                     else
7656                       to = sym->st_value;
7657                     to += (rel->r_addend
7658                            + sym_sec->output_offset
7659                            + sym_sec->output_section->vma);
7660                     from = (rel->r_offset
7661                             + sec->output_offset
7662                             + sec->output_section->vma);
7663                     if (to - from + limit < 2 * limit
7664                         && !(r_type == R_PPC64_PLTCALL_NOTOC
7665                              && (((h ? h->other : sym->st_other)
7666                                   & STO_PPC64_LOCAL_MASK)
7667                                  > 1 << STO_PPC64_LOCAL_BIT)))
7668                       *tls_maskp &= ~PLT_KEEP;
7669                   }
7670               }
7671             if (elf_section_data (sec)->relocs != relstart)
7672               free (relstart);
7673           }
7674
7675       if (local_syms != NULL
7676           && symtab_hdr->contents != (unsigned char *) local_syms)
7677         {
7678           if (!info->keep_memory)
7679             free (local_syms);
7680           else
7681             symtab_hdr->contents = (unsigned char *) local_syms;
7682         }
7683     }
7684
7685   return TRUE;
7686 }
7687
7688 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7689
7690 asection *
7691 ppc64_elf_tls_setup (struct bfd_link_info *info)
7692 {
7693   struct ppc_link_hash_table *htab;
7694   struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7695
7696   htab = ppc_hash_table (info);
7697   if (htab == NULL)
7698     return NULL;
7699
7700   if (abiversion (info->output_bfd) == 1)
7701     htab->opd_abi = 1;
7702
7703   if (htab->params->no_multi_toc)
7704     htab->do_multi_toc = 0;
7705   else if (!htab->do_multi_toc)
7706     htab->params->no_multi_toc = 1;
7707
7708   /* Default to --no-plt-localentry, as this option can cause problems
7709      with symbol interposition.  For example, glibc libpthread.so and
7710      libc.so duplicate many pthread symbols, with a fallback
7711      implementation in libc.so.  In some cases the fallback does more
7712      work than the pthread implementation.  __pthread_condattr_destroy
7713      is one such symbol: the libpthread.so implementation is
7714      localentry:0 while the libc.so implementation is localentry:8.
7715      An app that "cleverly" uses dlopen to only load necessary
7716      libraries at runtime may omit loading libpthread.so when not
7717      running multi-threaded, which then results in the libc.so
7718      fallback symbols being used and ld.so complaining.  Now there
7719      are workarounds in ld (see non_zero_localentry) to detect the
7720      pthread situation, but that may not be the only case where
7721      --plt-localentry can cause trouble.  */
7722   if (htab->params->plt_localentry0 < 0)
7723     htab->params->plt_localentry0 = 0;
7724   if (htab->params->plt_localentry0
7725       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7726                                FALSE, FALSE, FALSE) == NULL)
7727     _bfd_error_handler
7728       (_("warning: --plt-localentry is especially dangerous without "
7729          "ld.so support to detect ABI violations"));
7730
7731   tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7732                               FALSE, FALSE, TRUE);
7733   htab->tls_get_addr = ppc_elf_hash_entry (tga);
7734
7735   /* Move dynamic linking info to the function descriptor sym.  */
7736   if (tga != NULL)
7737     func_desc_adjust (tga, info);
7738   tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7739                                  FALSE, FALSE, TRUE);
7740   htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7741
7742   desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7743                                FALSE, FALSE, TRUE);
7744   htab->tga_desc = ppc_elf_hash_entry (desc);
7745   if (desc != NULL)
7746     func_desc_adjust (desc, info);
7747   desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7748                                   FALSE, FALSE, TRUE);
7749   htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7750
7751   if (htab->params->tls_get_addr_opt)
7752     {
7753       struct elf_link_hash_entry *opt, *opt_fd;
7754
7755       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7756                                   FALSE, FALSE, TRUE);
7757       if (opt != NULL)
7758         func_desc_adjust (opt, info);
7759       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7760                                      FALSE, FALSE, TRUE);
7761       if (opt_fd != NULL
7762           && (opt_fd->root.type == bfd_link_hash_defined
7763               || opt_fd->root.type == bfd_link_hash_defweak))
7764         {
7765           /* If glibc supports an optimized __tls_get_addr call stub,
7766              signalled by the presence of __tls_get_addr_opt, and we'll
7767              be calling __tls_get_addr via a plt call stub, then
7768              make __tls_get_addr point to __tls_get_addr_opt.  */
7769           if (!(htab->elf.dynamic_sections_created
7770                 && tga_fd != NULL
7771                 && (tga_fd->type == STT_FUNC
7772                     || tga_fd->needs_plt)
7773                 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7774                      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7775             tga_fd = NULL;
7776           if (!(htab->elf.dynamic_sections_created
7777                 && desc_fd != NULL
7778                 && (desc_fd->type == STT_FUNC
7779                     || desc_fd->needs_plt)
7780                 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7781                      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7782             desc_fd = NULL;
7783
7784           if (tga_fd != NULL || desc_fd != NULL)
7785             {
7786               struct plt_entry *ent = NULL;
7787
7788               if (tga_fd != NULL)
7789                 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7790                   if (ent->plt.refcount > 0)
7791                     break;
7792               if (ent == NULL && desc_fd != NULL)
7793                 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7794                   if (ent->plt.refcount > 0)
7795                     break;
7796               if (ent != NULL)
7797                 {
7798                   if (tga_fd != NULL)
7799                     {
7800                       tga_fd->root.type = bfd_link_hash_indirect;
7801                       tga_fd->root.u.i.link = &opt_fd->root;
7802                       tga_fd->root.u.i.warning = NULL;
7803                       ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7804                     }
7805                   if (desc_fd != NULL)
7806                     {
7807                       desc_fd->root.type = bfd_link_hash_indirect;
7808                       desc_fd->root.u.i.link = &opt_fd->root;
7809                       desc_fd->root.u.i.warning = NULL;
7810                       ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7811                     }
7812                   opt_fd->mark = 1;
7813                   if (opt_fd->dynindx != -1)
7814                     {
7815                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7816                       opt_fd->dynindx = -1;
7817                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7818                                               opt_fd->dynstr_index);
7819                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7820                         return NULL;
7821                     }
7822                   if (tga_fd != NULL)
7823                     {
7824                       htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
7825                       tga = &htab->tls_get_addr->elf;
7826                       if (opt != NULL && tga != NULL)
7827                         {
7828                           tga->root.type = bfd_link_hash_indirect;
7829                           tga->root.u.i.link = &opt->root;
7830                           tga->root.u.i.warning = NULL;
7831                           ppc64_elf_copy_indirect_symbol (info, opt, tga);
7832                           opt->mark = 1;
7833                           _bfd_elf_link_hash_hide_symbol (info, opt,
7834                                                           tga->forced_local);
7835                           htab->tls_get_addr = ppc_elf_hash_entry (opt);
7836                         }
7837                       htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7838                       htab->tls_get_addr_fd->is_func_descriptor = 1;
7839                       if (htab->tls_get_addr != NULL)
7840                         {
7841                           htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7842                           htab->tls_get_addr->is_func = 1;
7843                         }
7844                     }
7845                   if (desc_fd != NULL)
7846                     {
7847                       htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7848                       if (opt != NULL && desc != NULL)
7849                         {
7850                           desc->root.type = bfd_link_hash_indirect;
7851                           desc->root.u.i.link = &opt->root;
7852                           desc->root.u.i.warning = NULL;
7853                           ppc64_elf_copy_indirect_symbol (info, opt, desc);
7854                           opt->mark = 1;
7855                           _bfd_elf_link_hash_hide_symbol (info, opt,
7856                                                           desc->forced_local);
7857                           htab->tga_desc = ppc_elf_hash_entry (opt);
7858                         }
7859                       htab->tga_desc_fd->oh = htab->tga_desc;
7860                       htab->tga_desc_fd->is_func_descriptor = 1;
7861                       if (htab->tga_desc != NULL)
7862                         {
7863                           htab->tga_desc->oh = htab->tga_desc_fd;
7864                           htab->tga_desc->is_func = 1;
7865                         }
7866                     }
7867                 }
7868             }
7869         }
7870       else if (htab->params->tls_get_addr_opt < 0)
7871         htab->params->tls_get_addr_opt = 0;
7872     }
7873
7874   if (htab->tga_desc_fd != NULL
7875       && htab->params->tls_get_addr_opt
7876       && htab->params->no_tls_get_addr_regsave == -1)
7877     htab->params->no_tls_get_addr_regsave = 0;
7878
7879   return _bfd_elf_tls_setup (info->output_bfd, info);
7880 }
7881
7882 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7883    any of HASH1, HASH2, HASH3, or HASH4.  */
7884
7885 static bfd_boolean
7886 branch_reloc_hash_match (const bfd *ibfd,
7887                          const Elf_Internal_Rela *rel,
7888                          const struct ppc_link_hash_entry *hash1,
7889                          const struct ppc_link_hash_entry *hash2,
7890                          const struct ppc_link_hash_entry *hash3,
7891                          const struct ppc_link_hash_entry *hash4)
7892 {
7893   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7894   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7895   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7896
7897   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7898     {
7899       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7900       struct elf_link_hash_entry *h;
7901
7902       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7903       h = elf_follow_link (h);
7904       if (h == &hash1->elf || h == &hash2->elf
7905           || h == &hash3->elf || h == &hash4->elf)
7906         return TRUE;
7907     }
7908   return FALSE;
7909 }
7910
7911 /* Run through all the TLS relocs looking for optimization
7912    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7913    a preliminary section layout so that we know the TLS segment
7914    offsets.  We can't optimize earlier because some optimizations need
7915    to know the tp offset, and we need to optimize before allocating
7916    dynamic relocations.  */
7917
7918 bfd_boolean
7919 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7920 {
7921   bfd *ibfd;
7922   asection *sec;
7923   struct ppc_link_hash_table *htab;
7924   unsigned char *toc_ref;
7925   int pass;
7926
7927   if (!bfd_link_executable (info))
7928     return TRUE;
7929
7930   htab = ppc_hash_table (info);
7931   if (htab == NULL)
7932     return FALSE;
7933
7934   /* Make two passes over the relocs.  On the first pass, mark toc
7935      entries involved with tls relocs, and check that tls relocs
7936      involved in setting up a tls_get_addr call are indeed followed by
7937      such a call.  If they are not, we can't do any tls optimization.
7938      On the second pass twiddle tls_mask flags to notify
7939      relocate_section that optimization can be done, and adjust got
7940      and plt refcounts.  */
7941   toc_ref = NULL;
7942   for (pass = 0; pass < 2; ++pass)
7943     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7944       {
7945         Elf_Internal_Sym *locsyms = NULL;
7946         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7947
7948         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7949           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7950             {
7951               Elf_Internal_Rela *relstart, *rel, *relend;
7952               bfd_boolean found_tls_get_addr_arg = 0;
7953
7954               /* Read the relocations.  */
7955               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7956                                                     info->keep_memory);
7957               if (relstart == NULL)
7958                 {
7959                   free (toc_ref);
7960                   return FALSE;
7961                 }
7962
7963               relend = relstart + sec->reloc_count;
7964               for (rel = relstart; rel < relend; rel++)
7965                 {
7966                   enum elf_ppc64_reloc_type r_type;
7967                   unsigned long r_symndx;
7968                   struct elf_link_hash_entry *h;
7969                   Elf_Internal_Sym *sym;
7970                   asection *sym_sec;
7971                   unsigned char *tls_mask;
7972                   unsigned int tls_set, tls_clear, tls_type = 0;
7973                   bfd_vma value;
7974                   bfd_boolean ok_tprel, is_local;
7975                   long toc_ref_index = 0;
7976                   int expecting_tls_get_addr = 0;
7977                   bfd_boolean ret = FALSE;
7978
7979                   r_symndx = ELF64_R_SYM (rel->r_info);
7980                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7981                                   r_symndx, ibfd))
7982                     {
7983                     err_free_rel:
7984                       if (elf_section_data (sec)->relocs != relstart)
7985                         free (relstart);
7986                       if (toc_ref != NULL)
7987                         free (toc_ref);
7988                       if (locsyms != NULL
7989                           && (elf_symtab_hdr (ibfd).contents
7990                               != (unsigned char *) locsyms))
7991                         free (locsyms);
7992                       return ret;
7993                     }
7994
7995                   if (h != NULL)
7996                     {
7997                       if (h->root.type == bfd_link_hash_defined
7998                           || h->root.type == bfd_link_hash_defweak)
7999                         value = h->root.u.def.value;
8000                       else if (h->root.type == bfd_link_hash_undefweak)
8001                         value = 0;
8002                       else
8003                         {
8004                           found_tls_get_addr_arg = 0;
8005                           continue;
8006                         }
8007                     }
8008                   else
8009                     /* Symbols referenced by TLS relocs must be of type
8010                        STT_TLS.  So no need for .opd local sym adjust.  */
8011                     value = sym->st_value;
8012
8013                   ok_tprel = FALSE;
8014                   is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8015                   if (is_local)
8016                     {
8017                       if (h != NULL
8018                           && h->root.type == bfd_link_hash_undefweak)
8019                         ok_tprel = TRUE;
8020                       else if (sym_sec != NULL
8021                                && sym_sec->output_section != NULL)
8022                         {
8023                           value += sym_sec->output_offset;
8024                           value += sym_sec->output_section->vma;
8025                           value -= htab->elf.tls_sec->vma + TP_OFFSET;
8026                           /* Note that even though the prefix insns
8027                              allow a 1<<33 offset we use the same test
8028                              as for addis;addi.  There may be a mix of
8029                              pcrel and non-pcrel code and the decision
8030                              to optimise is per symbol, not per TLS
8031                              sequence.  */
8032                           ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8033                         }
8034                     }
8035
8036                   r_type = ELF64_R_TYPE (rel->r_info);
8037                   /* If this section has old-style __tls_get_addr calls
8038                      without marker relocs, then check that each
8039                      __tls_get_addr call reloc is preceded by a reloc
8040                      that conceivably belongs to the __tls_get_addr arg
8041                      setup insn.  If we don't find matching arg setup
8042                      relocs, don't do any tls optimization.  */
8043                   if (pass == 0
8044                       && sec->nomark_tls_get_addr
8045                       && h != NULL
8046                       && is_tls_get_addr (h, htab)
8047                       && !found_tls_get_addr_arg
8048                       && is_branch_reloc (r_type))
8049                     {
8050                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8051                                                 "TLS optimization disabled\n"),
8052                                               ibfd, sec, rel->r_offset);
8053                       ret = TRUE;
8054                       goto err_free_rel;
8055                     }
8056
8057                   found_tls_get_addr_arg = 0;
8058                   switch (r_type)
8059                     {
8060                     case R_PPC64_GOT_TLSLD16:
8061                     case R_PPC64_GOT_TLSLD16_LO:
8062                     case R_PPC64_GOT_TLSLD34:
8063                       expecting_tls_get_addr = 1;
8064                       found_tls_get_addr_arg = 1;
8065                       /* Fall through.  */
8066
8067                     case R_PPC64_GOT_TLSLD16_HI:
8068                     case R_PPC64_GOT_TLSLD16_HA:
8069                       /* These relocs should never be against a symbol
8070                          defined in a shared lib.  Leave them alone if
8071                          that turns out to be the case.  */
8072                       if (!is_local)
8073                         continue;
8074
8075                       /* LD -> LE */
8076                       tls_set = 0;
8077                       tls_clear = TLS_LD;
8078                       tls_type = TLS_TLS | TLS_LD;
8079                       break;
8080
8081                     case R_PPC64_GOT_TLSGD16:
8082                     case R_PPC64_GOT_TLSGD16_LO:
8083                     case R_PPC64_GOT_TLSGD34:
8084                       expecting_tls_get_addr = 1;
8085                       found_tls_get_addr_arg = 1;
8086                       /* Fall through. */
8087
8088                     case R_PPC64_GOT_TLSGD16_HI:
8089                     case R_PPC64_GOT_TLSGD16_HA:
8090                       if (ok_tprel)
8091                         /* GD -> LE */
8092                         tls_set = 0;
8093                       else
8094                         /* GD -> IE */
8095                         tls_set = TLS_TLS | TLS_GDIE;
8096                       tls_clear = TLS_GD;
8097                       tls_type = TLS_TLS | TLS_GD;
8098                       break;
8099
8100                     case R_PPC64_GOT_TPREL34:
8101                     case R_PPC64_GOT_TPREL16_DS:
8102                     case R_PPC64_GOT_TPREL16_LO_DS:
8103                     case R_PPC64_GOT_TPREL16_HI:
8104                     case R_PPC64_GOT_TPREL16_HA:
8105                       if (ok_tprel)
8106                         {
8107                           /* IE -> LE */
8108                           tls_set = 0;
8109                           tls_clear = TLS_TPREL;
8110                           tls_type = TLS_TLS | TLS_TPREL;
8111                           break;
8112                         }
8113                       continue;
8114
8115                     case R_PPC64_TLSLD:
8116                       if (!is_local)
8117                         continue;
8118                       /* Fall through.  */
8119                     case R_PPC64_TLSGD:
8120                       if (rel + 1 < relend
8121                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8122                         {
8123                           if (pass != 0
8124                               && (ELF64_R_TYPE (rel[1].r_info)
8125                                   != R_PPC64_PLTSEQ)
8126                               && (ELF64_R_TYPE (rel[1].r_info)
8127                                   != R_PPC64_PLTSEQ_NOTOC))
8128                             {
8129                               r_symndx = ELF64_R_SYM (rel[1].r_info);
8130                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8131                                               r_symndx, ibfd))
8132                                 goto err_free_rel;
8133                               if (h != NULL)
8134                                 {
8135                                   struct plt_entry *ent = NULL;
8136
8137                                   for (ent = h->plt.plist;
8138                                        ent != NULL;
8139                                        ent = ent->next)
8140                                     if (ent->addend == rel[1].r_addend)
8141                                       break;
8142
8143                                   if (ent != NULL
8144                                       && ent->plt.refcount > 0)
8145                                     ent->plt.refcount -= 1;
8146                                 }
8147                             }
8148                           continue;
8149                         }
8150                       found_tls_get_addr_arg = 1;
8151                       /* Fall through.  */
8152
8153                     case R_PPC64_TLS:
8154                     case R_PPC64_TOC16:
8155                     case R_PPC64_TOC16_LO:
8156                       if (sym_sec == NULL || sym_sec != toc)
8157                         continue;
8158
8159                       /* Mark this toc entry as referenced by a TLS
8160                          code sequence.  We can do that now in the
8161                          case of R_PPC64_TLS, and after checking for
8162                          tls_get_addr for the TOC16 relocs.  */
8163                       if (toc_ref == NULL)
8164                         toc_ref
8165                           = bfd_zmalloc (toc->output_section->rawsize / 8);
8166                       if (toc_ref == NULL)
8167                         goto err_free_rel;
8168
8169                       if (h != NULL)
8170                         value = h->root.u.def.value;
8171                       else
8172                         value = sym->st_value;
8173                       value += rel->r_addend;
8174                       if (value % 8 != 0)
8175                         continue;
8176                       BFD_ASSERT (value < toc->size
8177                                   && toc->output_offset % 8 == 0);
8178                       toc_ref_index = (value + toc->output_offset) / 8;
8179                       if (r_type == R_PPC64_TLS
8180                           || r_type == R_PPC64_TLSGD
8181                           || r_type == R_PPC64_TLSLD)
8182                         {
8183                           toc_ref[toc_ref_index] = 1;
8184                           continue;
8185                         }
8186
8187                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8188                         continue;
8189
8190                       tls_set = 0;
8191                       tls_clear = 0;
8192                       expecting_tls_get_addr = 2;
8193                       break;
8194
8195                     case R_PPC64_TPREL64:
8196                       if (pass == 0
8197                           || sec != toc
8198                           || toc_ref == NULL
8199                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8200                         continue;
8201                       if (ok_tprel)
8202                         {
8203                           /* IE -> LE */
8204                           tls_set = TLS_EXPLICIT;
8205                           tls_clear = TLS_TPREL;
8206                           break;
8207                         }
8208                       continue;
8209
8210                     case R_PPC64_DTPMOD64:
8211                       if (pass == 0
8212                           || sec != toc
8213                           || toc_ref == NULL
8214                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8215                         continue;
8216                       if (rel + 1 < relend
8217                           && (rel[1].r_info
8218                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8219                           && rel[1].r_offset == rel->r_offset + 8)
8220                         {
8221                           if (ok_tprel)
8222                             /* GD -> LE */
8223                             tls_set = TLS_EXPLICIT | TLS_GD;
8224                           else
8225                             /* GD -> IE */
8226                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8227                           tls_clear = TLS_GD;
8228                         }
8229                       else
8230                         {
8231                           if (!is_local)
8232                             continue;
8233
8234                           /* LD -> LE */
8235                           tls_set = TLS_EXPLICIT;
8236                           tls_clear = TLS_LD;
8237                         }
8238                       break;
8239
8240                     default:
8241                       continue;
8242                     }
8243
8244                   if (pass == 0)
8245                     {
8246                       if (!expecting_tls_get_addr
8247                           || !sec->nomark_tls_get_addr)
8248                         continue;
8249
8250                       if (rel + 1 < relend
8251                           && branch_reloc_hash_match (ibfd, rel + 1,
8252                                                       htab->tls_get_addr_fd,
8253                                                       htab->tga_desc_fd,
8254                                                       htab->tls_get_addr,
8255                                                       htab->tga_desc))
8256                         {
8257                           if (expecting_tls_get_addr == 2)
8258                             {
8259                               /* Check for toc tls entries.  */
8260                               unsigned char *toc_tls;
8261                               int retval;
8262
8263                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8264                                                      &locsyms,
8265                                                      rel, ibfd);
8266                               if (retval == 0)
8267                                 goto err_free_rel;
8268                               if (toc_tls != NULL)
8269                                 {
8270                                   if ((*toc_tls & TLS_TLS) != 0
8271                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8272                                     found_tls_get_addr_arg = 1;
8273                                   if (retval > 1)
8274                                     toc_ref[toc_ref_index] = 1;
8275                                 }
8276                             }
8277                           continue;
8278                         }
8279
8280                       /* Uh oh, we didn't find the expected call.  We
8281                          could just mark this symbol to exclude it
8282                          from tls optimization but it's safer to skip
8283                          the entire optimization.  */
8284                       /* xgettext:c-format */
8285                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8286                                                 "TLS optimization disabled\n"),
8287                                               ibfd, sec, rel->r_offset);
8288                       ret = TRUE;
8289                       goto err_free_rel;
8290                     }
8291
8292                   /* If we don't have old-style __tls_get_addr calls
8293                      without TLSGD/TLSLD marker relocs, and we haven't
8294                      found a new-style __tls_get_addr call with a
8295                      marker for this symbol, then we either have a
8296                      broken object file or an -mlongcall style
8297                      indirect call to __tls_get_addr without a marker.
8298                      Disable optimization in this case.  */
8299                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8300                       && (tls_set & TLS_EXPLICIT) == 0
8301                       && !sec->nomark_tls_get_addr
8302                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
8303                           != (TLS_TLS | TLS_MARK)))
8304                     continue;
8305
8306                   if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8307                     {
8308                       struct plt_entry *ent = NULL;
8309
8310                       if (htab->tls_get_addr_fd != NULL)
8311                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8312                              ent != NULL;
8313                              ent = ent->next)
8314                           if (ent->addend == 0)
8315                             break;
8316
8317                       if (ent == NULL && htab->tga_desc_fd != NULL)
8318                         for (ent = htab->tga_desc_fd->elf.plt.plist;
8319                              ent != NULL;
8320                              ent = ent->next)
8321                           if (ent->addend == 0)
8322                             break;
8323
8324                       if (ent == NULL && htab->tls_get_addr != NULL)
8325                         for (ent = htab->tls_get_addr->elf.plt.plist;
8326                              ent != NULL;
8327                              ent = ent->next)
8328                           if (ent->addend == 0)
8329                             break;
8330
8331                       if (ent == NULL && htab->tga_desc != NULL)
8332                         for (ent = htab->tga_desc->elf.plt.plist;
8333                              ent != NULL;
8334                              ent = ent->next)
8335                           if (ent->addend == 0)
8336                             break;
8337
8338                       if (ent != NULL
8339                           && ent->plt.refcount > 0)
8340                         ent->plt.refcount -= 1;
8341                     }
8342
8343                   if (tls_clear == 0)
8344                     continue;
8345
8346                   if ((tls_set & TLS_EXPLICIT) == 0)
8347                     {
8348                       struct got_entry *ent;
8349
8350                       /* Adjust got entry for this reloc.  */
8351                       if (h != NULL)
8352                         ent = h->got.glist;
8353                       else
8354                         ent = elf_local_got_ents (ibfd)[r_symndx];
8355
8356                       for (; ent != NULL; ent = ent->next)
8357                         if (ent->addend == rel->r_addend
8358                             && ent->owner == ibfd
8359                             && ent->tls_type == tls_type)
8360                           break;
8361                       if (ent == NULL)
8362                         abort ();
8363
8364                       if (tls_set == 0)
8365                         {
8366                           /* We managed to get rid of a got entry.  */
8367                           if (ent->got.refcount > 0)
8368                             ent->got.refcount -= 1;
8369                         }
8370                     }
8371                   else
8372                     {
8373                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8374                          we'll lose one or two dyn relocs.  */
8375                       if (!dec_dynrel_count (rel->r_info, sec, info,
8376                                              NULL, h, sym))
8377                         return FALSE;
8378
8379                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8380                         {
8381                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8382                                                  NULL, h, sym))
8383                             return FALSE;
8384                         }
8385                     }
8386
8387                   *tls_mask |= tls_set & 0xff;
8388                   *tls_mask &= ~tls_clear;
8389                 }
8390
8391               if (elf_section_data (sec)->relocs != relstart)
8392                 free (relstart);
8393             }
8394
8395         if (locsyms != NULL
8396             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8397           {
8398             if (!info->keep_memory)
8399               free (locsyms);
8400             else
8401               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8402           }
8403       }
8404
8405   if (toc_ref != NULL)
8406     free (toc_ref);
8407   htab->do_tls_opt = 1;
8408   return TRUE;
8409 }
8410
8411 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8412    the values of any global symbols in a toc section that has been
8413    edited.  Globals in toc sections should be a rarity, so this function
8414    sets a flag if any are found in toc sections other than the one just
8415    edited, so that further hash table traversals can be avoided.  */
8416
8417 struct adjust_toc_info
8418 {
8419   asection *toc;
8420   unsigned long *skip;
8421   bfd_boolean global_toc_syms;
8422 };
8423
8424 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8425
8426 static bfd_boolean
8427 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8428 {
8429   struct ppc_link_hash_entry *eh;
8430   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8431   unsigned long i;
8432
8433   if (h->root.type != bfd_link_hash_defined
8434       && h->root.type != bfd_link_hash_defweak)
8435     return TRUE;
8436
8437   eh = ppc_elf_hash_entry (h);
8438   if (eh->adjust_done)
8439     return TRUE;
8440
8441   if (eh->elf.root.u.def.section == toc_inf->toc)
8442     {
8443       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8444         i = toc_inf->toc->rawsize >> 3;
8445       else
8446         i = eh->elf.root.u.def.value >> 3;
8447
8448       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8449         {
8450           _bfd_error_handler
8451             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8452           do
8453             ++i;
8454           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8455           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8456         }
8457
8458       eh->elf.root.u.def.value -= toc_inf->skip[i];
8459       eh->adjust_done = 1;
8460     }
8461   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8462     toc_inf->global_toc_syms = TRUE;
8463
8464   return TRUE;
8465 }
8466
8467 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8468    on a _LO variety toc/got reloc.  */
8469
8470 static bfd_boolean
8471 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8472 {
8473   return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8474           || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8475           || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8476           || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8477           || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8478           || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8479           || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8480           || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8481           || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8482           || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8483           || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8484           || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8485           || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8486           || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8487           || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8488           || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8489           || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8490               /* Exclude lfqu by testing reloc.  If relocs are ever
8491                  defined for the reduced D field in psq_lu then those
8492                  will need testing too.  */
8493               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8494           || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8495               && (insn & 1) == 0)
8496           || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8497           || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8498               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8499               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8500           || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8501               && (insn & 1) == 0));
8502 }
8503
8504 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8505      pld ra,symbol@got@pcrel
8506      load/store rt,off(ra)
8507    or
8508      pla ra,symbol@pcrel
8509      load/store rt,off(ra)
8510    may be translated to
8511      pload/pstore rt,symbol+off@pcrel
8512      nop.
8513    This function returns true if the optimization is possible, placing
8514    the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8515
8516    On entry to this function, the linker has already determined that
8517    the pld can be replaced with pla: *PINSN1 is that pla insn,
8518    while *PINSN2 is the second instruction.  */
8519
8520 static bfd_boolean
8521 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8522 {
8523   uint64_t insn1 = *pinsn1;
8524   uint64_t insn2 = *pinsn2;
8525   bfd_signed_vma off;
8526
8527   if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8528     {
8529       /* Check that regs match.  */
8530       if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8531         return FALSE;
8532
8533       /* P8LS or PMLS form, non-pcrel.  */
8534       if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8535         return FALSE;
8536
8537       *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8538       *pinsn2 = PNOP;
8539       off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8540       *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8541       return TRUE;
8542     }
8543
8544   insn2 >>= 32;
8545
8546   /* Check that regs match.  */
8547   if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8548     return FALSE;
8549
8550   switch ((insn2 >> 26) & 63)
8551     {
8552     default:
8553       return FALSE;
8554
8555     case 32: /* lwz */
8556     case 34: /* lbz */
8557     case 36: /* stw */
8558     case 38: /* stb */
8559     case 40: /* lhz */
8560     case 42: /* lha */
8561     case 44: /* sth */
8562     case 48: /* lfs */
8563     case 50: /* lfd */
8564     case 52: /* stfs */
8565     case 54: /* stfd */
8566       /* These are the PMLS cases, where we just need to tack a prefix
8567          on the insn.  */
8568       insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8569                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8570       off = insn2 & 0xffff;
8571       break;
8572
8573     case 58: /* lwa, ld */
8574       if ((insn2 & 1) != 0)
8575         return FALSE;
8576       insn1 = ((1ULL << 58) | (1ULL << 52)
8577                | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8578                | (insn2 & (31ULL << 21)));
8579       off = insn2 & 0xfffc;
8580       break;
8581
8582     case 57: /* lxsd, lxssp */
8583       if ((insn2 & 3) < 2)
8584         return FALSE;
8585       insn1 = ((1ULL << 58) | (1ULL << 52)
8586                | ((40ULL | (insn2 & 3)) << 26)
8587                | (insn2 & (31ULL << 21)));
8588       off = insn2 & 0xfffc;
8589       break;
8590
8591     case 61: /* stxsd, stxssp, lxv, stxv  */
8592       if ((insn2 & 3) == 0)
8593         return FALSE;
8594       else if ((insn2 & 3) >= 2)
8595         {
8596           insn1 = ((1ULL << 58) | (1ULL << 52)
8597                    | ((44ULL | (insn2 & 3)) << 26)
8598                    | (insn2 & (31ULL << 21)));
8599           off = insn2 & 0xfffc;
8600         }
8601       else
8602         {
8603           insn1 = ((1ULL << 58) | (1ULL << 52)
8604                    | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8605                    | (insn2 & (31ULL << 21)));
8606           off = insn2 & 0xfff0;
8607         }
8608       break;
8609
8610     case 56: /* lq */
8611       insn1 = ((1ULL << 58) | (1ULL << 52)
8612                | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8613       off = insn2 & 0xffff;
8614       break;
8615
8616     case 62: /* std, stq */
8617       if ((insn2 & 1) != 0)
8618         return FALSE;
8619       insn1 = ((1ULL << 58) | (1ULL << 52)
8620                | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8621                | (insn2 & (31ULL << 21)));
8622       off = insn2 & 0xfffc;
8623       break;
8624     }
8625
8626   *pinsn1 = insn1;
8627   *pinsn2 = (uint64_t) NOP << 32;
8628   *poff = (off ^ 0x8000) - 0x8000;
8629   return TRUE;
8630 }
8631
8632 /* Examine all relocs referencing .toc sections in order to remove
8633    unused .toc entries.  */
8634
8635 bfd_boolean
8636 ppc64_elf_edit_toc (struct bfd_link_info *info)
8637 {
8638   bfd *ibfd;
8639   struct adjust_toc_info toc_inf;
8640   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8641
8642   htab->do_toc_opt = 1;
8643   toc_inf.global_toc_syms = TRUE;
8644   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8645     {
8646       asection *toc, *sec;
8647       Elf_Internal_Shdr *symtab_hdr;
8648       Elf_Internal_Sym *local_syms;
8649       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8650       unsigned long *skip, *drop;
8651       unsigned char *used;
8652       unsigned char *keep, last, some_unused;
8653
8654       if (!is_ppc64_elf (ibfd))
8655         continue;
8656
8657       toc = bfd_get_section_by_name (ibfd, ".toc");
8658       if (toc == NULL
8659           || toc->size == 0
8660           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8661           || discarded_section (toc))
8662         continue;
8663
8664       toc_relocs = NULL;
8665       local_syms = NULL;
8666       symtab_hdr = &elf_symtab_hdr (ibfd);
8667
8668       /* Look at sections dropped from the final link.  */
8669       skip = NULL;
8670       relstart = NULL;
8671       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8672         {
8673           if (sec->reloc_count == 0
8674               || !discarded_section (sec)
8675               || get_opd_info (sec)
8676               || (sec->flags & SEC_ALLOC) == 0
8677               || (sec->flags & SEC_DEBUGGING) != 0)
8678             continue;
8679
8680           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8681           if (relstart == NULL)
8682             goto error_ret;
8683
8684           /* Run through the relocs to see which toc entries might be
8685              unused.  */
8686           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8687             {
8688               enum elf_ppc64_reloc_type r_type;
8689               unsigned long r_symndx;
8690               asection *sym_sec;
8691               struct elf_link_hash_entry *h;
8692               Elf_Internal_Sym *sym;
8693               bfd_vma val;
8694
8695               r_type = ELF64_R_TYPE (rel->r_info);
8696               switch (r_type)
8697                 {
8698                 default:
8699                   continue;
8700
8701                 case R_PPC64_TOC16:
8702                 case R_PPC64_TOC16_LO:
8703                 case R_PPC64_TOC16_HI:
8704                 case R_PPC64_TOC16_HA:
8705                 case R_PPC64_TOC16_DS:
8706                 case R_PPC64_TOC16_LO_DS:
8707                   break;
8708                 }
8709
8710               r_symndx = ELF64_R_SYM (rel->r_info);
8711               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8712                               r_symndx, ibfd))
8713                 goto error_ret;
8714
8715               if (sym_sec != toc)
8716                 continue;
8717
8718               if (h != NULL)
8719                 val = h->root.u.def.value;
8720               else
8721                 val = sym->st_value;
8722               val += rel->r_addend;
8723
8724               if (val >= toc->size)
8725                 continue;
8726
8727               /* Anything in the toc ought to be aligned to 8 bytes.
8728                  If not, don't mark as unused.  */
8729               if (val & 7)
8730                 continue;
8731
8732               if (skip == NULL)
8733                 {
8734                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8735                   if (skip == NULL)
8736                     goto error_ret;
8737                 }
8738
8739               skip[val >> 3] = ref_from_discarded;
8740             }
8741
8742           if (elf_section_data (sec)->relocs != relstart)
8743             free (relstart);
8744         }
8745
8746       /* For largetoc loads of address constants, we can convert
8747          .  addis rx,2,addr@got@ha
8748          .  ld ry,addr@got@l(rx)
8749          to
8750          .  addis rx,2,addr@toc@ha
8751          .  addi ry,rx,addr@toc@l
8752          when addr is within 2G of the toc pointer.  This then means
8753          that the word storing "addr" in the toc is no longer needed.  */
8754
8755       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8756           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8757           && toc->reloc_count != 0)
8758         {
8759           /* Read toc relocs.  */
8760           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8761                                                   info->keep_memory);
8762           if (toc_relocs == NULL)
8763             goto error_ret;
8764
8765           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8766             {
8767               enum elf_ppc64_reloc_type r_type;
8768               unsigned long r_symndx;
8769               asection *sym_sec;
8770               struct elf_link_hash_entry *h;
8771               Elf_Internal_Sym *sym;
8772               bfd_vma val, addr;
8773
8774               r_type = ELF64_R_TYPE (rel->r_info);
8775               if (r_type != R_PPC64_ADDR64)
8776                 continue;
8777
8778               r_symndx = ELF64_R_SYM (rel->r_info);
8779               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8780                               r_symndx, ibfd))
8781                 goto error_ret;
8782
8783               if (sym_sec == NULL
8784                   || sym_sec->output_section == NULL
8785                   || discarded_section (sym_sec))
8786                 continue;
8787
8788               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8789                 continue;
8790
8791               if (h != NULL)
8792                 {
8793                   if (h->type == STT_GNU_IFUNC)
8794                     continue;
8795                   val = h->root.u.def.value;
8796                 }
8797               else
8798                 {
8799                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8800                     continue;
8801                   val = sym->st_value;
8802                 }
8803               val += rel->r_addend;
8804               val += sym_sec->output_section->vma + sym_sec->output_offset;
8805
8806               /* We don't yet know the exact toc pointer value, but we
8807                  know it will be somewhere in the toc section.  Don't
8808                  optimize if the difference from any possible toc
8809                  pointer is outside [ff..f80008000, 7fff7fff].  */
8810               addr = toc->output_section->vma + TOC_BASE_OFF;
8811               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8812                 continue;
8813
8814               addr = toc->output_section->vma + toc->output_section->rawsize;
8815               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8816                 continue;
8817
8818               if (skip == NULL)
8819                 {
8820                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8821                   if (skip == NULL)
8822                     goto error_ret;
8823                 }
8824
8825               skip[rel->r_offset >> 3]
8826                 |= can_optimize | ((rel - toc_relocs) << 2);
8827             }
8828         }
8829
8830       if (skip == NULL)
8831         continue;
8832
8833       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8834       if (used == NULL)
8835         {
8836         error_ret:
8837           if (local_syms != NULL
8838               && symtab_hdr->contents != (unsigned char *) local_syms)
8839             free (local_syms);
8840           if (sec != NULL
8841               && relstart != NULL
8842               && elf_section_data (sec)->relocs != relstart)
8843             free (relstart);
8844           if (toc_relocs != NULL
8845               && elf_section_data (toc)->relocs != toc_relocs)
8846             free (toc_relocs);
8847           if (skip != NULL)
8848             free (skip);
8849           return FALSE;
8850         }
8851
8852       /* Now check all kept sections that might reference the toc.
8853          Check the toc itself last.  */
8854       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8855                   : ibfd->sections);
8856            sec != NULL;
8857            sec = (sec == toc ? NULL
8858                   : sec->next == NULL ? toc
8859                   : sec->next == toc && toc->next ? toc->next
8860                   : sec->next))
8861         {
8862           int repeat;
8863
8864           if (sec->reloc_count == 0
8865               || discarded_section (sec)
8866               || get_opd_info (sec)
8867               || (sec->flags & SEC_ALLOC) == 0
8868               || (sec->flags & SEC_DEBUGGING) != 0)
8869             continue;
8870
8871           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8872                                                 info->keep_memory);
8873           if (relstart == NULL)
8874             {
8875               free (used);
8876               goto error_ret;
8877             }
8878
8879           /* Mark toc entries referenced as used.  */
8880           do
8881             {
8882               repeat = 0;
8883               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8884                 {
8885                   enum elf_ppc64_reloc_type r_type;
8886                   unsigned long r_symndx;
8887                   asection *sym_sec;
8888                   struct elf_link_hash_entry *h;
8889                   Elf_Internal_Sym *sym;
8890                   bfd_vma val;
8891
8892                   r_type = ELF64_R_TYPE (rel->r_info);
8893                   switch (r_type)
8894                     {
8895                     case R_PPC64_TOC16:
8896                     case R_PPC64_TOC16_LO:
8897                     case R_PPC64_TOC16_HI:
8898                     case R_PPC64_TOC16_HA:
8899                     case R_PPC64_TOC16_DS:
8900                     case R_PPC64_TOC16_LO_DS:
8901                       /* In case we're taking addresses of toc entries.  */
8902                     case R_PPC64_ADDR64:
8903                       break;
8904
8905                     default:
8906                       continue;
8907                     }
8908
8909                   r_symndx = ELF64_R_SYM (rel->r_info);
8910                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8911                                   r_symndx, ibfd))
8912                     {
8913                       free (used);
8914                       goto error_ret;
8915                     }
8916
8917                   if (sym_sec != toc)
8918                     continue;
8919
8920                   if (h != NULL)
8921                     val = h->root.u.def.value;
8922                   else
8923                     val = sym->st_value;
8924                   val += rel->r_addend;
8925
8926                   if (val >= toc->size)
8927                     continue;
8928
8929                   if ((skip[val >> 3] & can_optimize) != 0)
8930                     {
8931                       bfd_vma off;
8932                       unsigned char opc;
8933
8934                       switch (r_type)
8935                         {
8936                         case R_PPC64_TOC16_HA:
8937                           break;
8938
8939                         case R_PPC64_TOC16_LO_DS:
8940                           off = rel->r_offset;
8941                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8942                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8943                                                          off, 1))
8944                             {
8945                               free (used);
8946                               goto error_ret;
8947                             }
8948                           if ((opc & (0x3f << 2)) == (58u << 2))
8949                             break;
8950                           /* Fall through.  */
8951
8952                         default:
8953                           /* Wrong sort of reloc, or not a ld.  We may
8954                              as well clear ref_from_discarded too.  */
8955                           skip[val >> 3] = 0;
8956                         }
8957                     }
8958
8959                   if (sec != toc)
8960                     used[val >> 3] = 1;
8961                   /* For the toc section, we only mark as used if this
8962                      entry itself isn't unused.  */
8963                   else if ((used[rel->r_offset >> 3]
8964                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8965                            && !used[val >> 3])
8966                     {
8967                       /* Do all the relocs again, to catch reference
8968                          chains.  */
8969                       repeat = 1;
8970                       used[val >> 3] = 1;
8971                     }
8972                 }
8973             }
8974           while (repeat);
8975
8976           if (elf_section_data (sec)->relocs != relstart)
8977             free (relstart);
8978         }
8979
8980       /* Merge the used and skip arrays.  Assume that TOC
8981          doublewords not appearing as either used or unused belong
8982          to an entry more than one doubleword in size.  */
8983       for (drop = skip, keep = used, last = 0, some_unused = 0;
8984            drop < skip + (toc->size + 7) / 8;
8985            ++drop, ++keep)
8986         {
8987           if (*keep)
8988             {
8989               *drop &= ~ref_from_discarded;
8990               if ((*drop & can_optimize) != 0)
8991                 some_unused = 1;
8992               last = 0;
8993             }
8994           else if ((*drop & ref_from_discarded) != 0)
8995             {
8996               some_unused = 1;
8997               last = ref_from_discarded;
8998             }
8999           else
9000             *drop = last;
9001         }
9002
9003       free (used);
9004
9005       if (some_unused)
9006         {
9007           bfd_byte *contents, *src;
9008           unsigned long off;
9009           Elf_Internal_Sym *sym;
9010           bfd_boolean local_toc_syms = FALSE;
9011
9012           /* Shuffle the toc contents, and at the same time convert the
9013              skip array from booleans into offsets.  */
9014           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9015             goto error_ret;
9016
9017           elf_section_data (toc)->this_hdr.contents = contents;
9018
9019           for (src = contents, off = 0, drop = skip;
9020                src < contents + toc->size;
9021                src += 8, ++drop)
9022             {
9023               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9024                 off += 8;
9025               else if (off != 0)
9026                 {
9027                   *drop = off;
9028                   memcpy (src - off, src, 8);
9029                 }
9030             }
9031           *drop = off;
9032           toc->rawsize = toc->size;
9033           toc->size = src - contents - off;
9034
9035           /* Adjust addends for relocs against the toc section sym,
9036              and optimize any accesses we can.  */
9037           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9038             {
9039               if (sec->reloc_count == 0
9040                   || discarded_section (sec))
9041                 continue;
9042
9043               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9044                                                     info->keep_memory);
9045               if (relstart == NULL)
9046                 goto error_ret;
9047
9048               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9049                 {
9050                   enum elf_ppc64_reloc_type r_type;
9051                   unsigned long r_symndx;
9052                   asection *sym_sec;
9053                   struct elf_link_hash_entry *h;
9054                   bfd_vma val;
9055
9056                   r_type = ELF64_R_TYPE (rel->r_info);
9057                   switch (r_type)
9058                     {
9059                     default:
9060                       continue;
9061
9062                     case R_PPC64_TOC16:
9063                     case R_PPC64_TOC16_LO:
9064                     case R_PPC64_TOC16_HI:
9065                     case R_PPC64_TOC16_HA:
9066                     case R_PPC64_TOC16_DS:
9067                     case R_PPC64_TOC16_LO_DS:
9068                     case R_PPC64_ADDR64:
9069                       break;
9070                     }
9071
9072                   r_symndx = ELF64_R_SYM (rel->r_info);
9073                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9074                                   r_symndx, ibfd))
9075                     goto error_ret;
9076
9077                   if (sym_sec != toc)
9078                     continue;
9079
9080                   if (h != NULL)
9081                     val = h->root.u.def.value;
9082                   else
9083                     {
9084                       val = sym->st_value;
9085                       if (val != 0)
9086                         local_toc_syms = TRUE;
9087                     }
9088
9089                   val += rel->r_addend;
9090
9091                   if (val > toc->rawsize)
9092                     val = toc->rawsize;
9093                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9094                     continue;
9095                   else if ((skip[val >> 3] & can_optimize) != 0)
9096                     {
9097                       Elf_Internal_Rela *tocrel
9098                         = toc_relocs + (skip[val >> 3] >> 2);
9099                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9100
9101                       switch (r_type)
9102                         {
9103                         case R_PPC64_TOC16_HA:
9104                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9105                           break;
9106
9107                         case R_PPC64_TOC16_LO_DS:
9108                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9109                           break;
9110
9111                         default:
9112                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9113                             ppc_howto_init ();
9114                           info->callbacks->einfo
9115                             /* xgettext:c-format */
9116                             (_("%H: %s references "
9117                                "optimized away TOC entry\n"),
9118                              ibfd, sec, rel->r_offset,
9119                              ppc64_elf_howto_table[r_type]->name);
9120                           bfd_set_error (bfd_error_bad_value);
9121                           goto error_ret;
9122                         }
9123                       rel->r_addend = tocrel->r_addend;
9124                       elf_section_data (sec)->relocs = relstart;
9125                       continue;
9126                     }
9127
9128                   if (h != NULL || sym->st_value != 0)
9129                     continue;
9130
9131                   rel->r_addend -= skip[val >> 3];
9132                   elf_section_data (sec)->relocs = relstart;
9133                 }
9134
9135               if (elf_section_data (sec)->relocs != relstart)
9136                 free (relstart);
9137             }
9138
9139           /* We shouldn't have local or global symbols defined in the TOC,
9140              but handle them anyway.  */
9141           if (local_syms != NULL)
9142             for (sym = local_syms;
9143                  sym < local_syms + symtab_hdr->sh_info;
9144                  ++sym)
9145               if (sym->st_value != 0
9146                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9147                 {
9148                   unsigned long i;
9149
9150                   if (sym->st_value > toc->rawsize)
9151                     i = toc->rawsize >> 3;
9152                   else
9153                     i = sym->st_value >> 3;
9154
9155                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9156                     {
9157                       if (local_toc_syms)
9158                         _bfd_error_handler
9159                           (_("%s defined on removed toc entry"),
9160                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9161                       do
9162                         ++i;
9163                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9164                       sym->st_value = (bfd_vma) i << 3;
9165                     }
9166
9167                   sym->st_value -= skip[i];
9168                   symtab_hdr->contents = (unsigned char *) local_syms;
9169                 }
9170
9171           /* Adjust any global syms defined in this toc input section.  */
9172           if (toc_inf.global_toc_syms)
9173             {
9174               toc_inf.toc = toc;
9175               toc_inf.skip = skip;
9176               toc_inf.global_toc_syms = FALSE;
9177               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9178                                       &toc_inf);
9179             }
9180
9181           if (toc->reloc_count != 0)
9182             {
9183               Elf_Internal_Shdr *rel_hdr;
9184               Elf_Internal_Rela *wrel;
9185               bfd_size_type sz;
9186
9187               /* Remove unused toc relocs, and adjust those we keep.  */
9188               if (toc_relocs == NULL)
9189                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9190                                                         info->keep_memory);
9191               if (toc_relocs == NULL)
9192                 goto error_ret;
9193
9194               wrel = toc_relocs;
9195               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9196                 if ((skip[rel->r_offset >> 3]
9197                      & (ref_from_discarded | can_optimize)) == 0)
9198                   {
9199                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9200                     wrel->r_info = rel->r_info;
9201                     wrel->r_addend = rel->r_addend;
9202                     ++wrel;
9203                   }
9204                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9205                                             &local_syms, NULL, NULL))
9206                   goto error_ret;
9207
9208               elf_section_data (toc)->relocs = toc_relocs;
9209               toc->reloc_count = wrel - toc_relocs;
9210               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9211               sz = rel_hdr->sh_entsize;
9212               rel_hdr->sh_size = toc->reloc_count * sz;
9213             }
9214         }
9215       else if (toc_relocs != NULL
9216                && elf_section_data (toc)->relocs != toc_relocs)
9217         free (toc_relocs);
9218
9219       if (local_syms != NULL
9220           && symtab_hdr->contents != (unsigned char *) local_syms)
9221         {
9222           if (!info->keep_memory)
9223             free (local_syms);
9224           else
9225             symtab_hdr->contents = (unsigned char *) local_syms;
9226         }
9227       free (skip);
9228     }
9229
9230   /* Look for cases where we can change an indirect GOT access to
9231      a GOT relative or PC relative access, possibly reducing the
9232      number of GOT entries.  */
9233   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9234     {
9235       asection *sec;
9236       Elf_Internal_Shdr *symtab_hdr;
9237       Elf_Internal_Sym *local_syms;
9238       Elf_Internal_Rela *relstart, *rel;
9239       bfd_vma got;
9240
9241       if (!is_ppc64_elf (ibfd))
9242         continue;
9243
9244       if (!ppc64_elf_tdata (ibfd)->has_optrel)
9245         continue;
9246
9247       sec = ppc64_elf_tdata (ibfd)->got;
9248       got = 0;
9249       if (sec != NULL)
9250         got = sec->output_section->vma + sec->output_offset + 0x8000;
9251
9252       local_syms = NULL;
9253       symtab_hdr = &elf_symtab_hdr (ibfd);
9254
9255       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9256         {
9257           if (sec->reloc_count == 0
9258               || !ppc64_elf_section_data (sec)->has_optrel
9259               || discarded_section (sec))
9260             continue;
9261
9262           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9263                                                 info->keep_memory);
9264           if (relstart == NULL)
9265             {
9266             got_error_ret:
9267               if (local_syms != NULL
9268                   && symtab_hdr->contents != (unsigned char *) local_syms)
9269                 free (local_syms);
9270               if (sec != NULL
9271                   && relstart != NULL
9272                   && elf_section_data (sec)->relocs != relstart)
9273                 free (relstart);
9274               return FALSE;
9275             }
9276
9277           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9278             {
9279               enum elf_ppc64_reloc_type r_type;
9280               unsigned long r_symndx;
9281               Elf_Internal_Sym *sym;
9282               asection *sym_sec;
9283               struct elf_link_hash_entry *h;
9284               struct got_entry *ent;
9285               bfd_vma val, pc;
9286               unsigned char buf[8];
9287               unsigned int insn;
9288               enum {no_check, check_lo, check_ha} insn_check;
9289
9290               r_type = ELF64_R_TYPE (rel->r_info);
9291               switch (r_type)
9292                 {
9293                 default:
9294                   insn_check = no_check;
9295                   break;
9296
9297                 case R_PPC64_PLT16_HA:
9298                 case R_PPC64_GOT_TLSLD16_HA:
9299                 case R_PPC64_GOT_TLSGD16_HA:
9300                 case R_PPC64_GOT_TPREL16_HA:
9301                 case R_PPC64_GOT_DTPREL16_HA:
9302                 case R_PPC64_GOT16_HA:
9303                 case R_PPC64_TOC16_HA:
9304                   insn_check = check_ha;
9305                   break;
9306
9307                 case R_PPC64_PLT16_LO:
9308                 case R_PPC64_PLT16_LO_DS:
9309                 case R_PPC64_GOT_TLSLD16_LO:
9310                 case R_PPC64_GOT_TLSGD16_LO:
9311                 case R_PPC64_GOT_TPREL16_LO_DS:
9312                 case R_PPC64_GOT_DTPREL16_LO_DS:
9313                 case R_PPC64_GOT16_LO:
9314                 case R_PPC64_GOT16_LO_DS:
9315                 case R_PPC64_TOC16_LO:
9316                 case R_PPC64_TOC16_LO_DS:
9317                   insn_check = check_lo;
9318                   break;
9319                 }
9320
9321               if (insn_check != no_check)
9322                 {
9323                   bfd_vma off = rel->r_offset & ~3;
9324
9325                   if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9326                     goto got_error_ret;
9327
9328                   insn = bfd_get_32 (ibfd, buf);
9329                   if (insn_check == check_lo
9330                       ? !ok_lo_toc_insn (insn, r_type)
9331                       : ((insn & ((0x3fu << 26) | 0x1f << 16))
9332                          != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9333                     {
9334                       char str[12];
9335
9336                       ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9337                       sprintf (str, "%#08x", insn);
9338                       info->callbacks->einfo
9339                         /* xgettext:c-format */
9340                         (_("%H: got/toc optimization is not supported for"
9341                            " %s instruction\n"),
9342                          ibfd, sec, rel->r_offset & ~3, str);
9343                       continue;
9344                     }
9345                 }
9346
9347               switch (r_type)
9348                 {
9349                 /* Note that we don't delete GOT entries for
9350                    R_PPC64_GOT16_DS since we'd need a lot more
9351                    analysis.  For starters, the preliminary layout is
9352                    before the GOT, PLT, dynamic sections and stubs are
9353                    laid out.  Then we'd need to allow for changes in
9354                    distance between sections caused by alignment.  */
9355                 default:
9356                   continue;
9357
9358                 case R_PPC64_GOT16_HA:
9359                 case R_PPC64_GOT16_LO_DS:
9360                 case R_PPC64_GOT_PCREL34:
9361                   break;
9362                 }
9363
9364               r_symndx = ELF64_R_SYM (rel->r_info);
9365               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9366                               r_symndx, ibfd))
9367                 goto got_error_ret;
9368
9369               if (sym_sec == NULL
9370                   || sym_sec->output_section == NULL
9371                   || discarded_section (sym_sec))
9372                 continue;
9373
9374               if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9375                 continue;
9376
9377               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9378                 continue;
9379
9380               if (h != NULL)
9381                 val = h->root.u.def.value;
9382               else
9383                 val = sym->st_value;
9384               val += rel->r_addend;
9385               val += sym_sec->output_section->vma + sym_sec->output_offset;
9386
9387 /* Fudge factor to allow for the fact that the preliminary layout
9388    isn't exact.  Reduce limits by this factor.  */
9389 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9390
9391               switch (r_type)
9392                 {
9393                 default:
9394                   continue;
9395
9396                 case R_PPC64_GOT16_HA:
9397                   if (val - got + LIMIT_ADJUST (0x80008000ULL)
9398                       >= LIMIT_ADJUST (0x100000000ULL))
9399                     continue;
9400
9401                   if (!bfd_get_section_contents (ibfd, sec, buf,
9402                                                  rel->r_offset & ~3, 4))
9403                     goto got_error_ret;
9404                   insn = bfd_get_32 (ibfd, buf);
9405                   if (((insn & ((0x3fu << 26) | 0x1f << 16))
9406                        != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9407                     continue;
9408                   break;
9409
9410                 case R_PPC64_GOT16_LO_DS:
9411                   if (val - got + LIMIT_ADJUST (0x80008000ULL)
9412                       >= LIMIT_ADJUST (0x100000000ULL))
9413                     continue;
9414                   if (!bfd_get_section_contents (ibfd, sec, buf,
9415                                                  rel->r_offset & ~3, 4))
9416                     goto got_error_ret;
9417                   insn = bfd_get_32 (ibfd, buf);
9418                   if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9419                     continue;
9420                   break;
9421
9422                 case R_PPC64_GOT_PCREL34:
9423                   pc = rel->r_offset;
9424                   pc += sec->output_section->vma + sec->output_offset;
9425                   if (val - pc + LIMIT_ADJUST (1ULL << 33)
9426                       >= LIMIT_ADJUST (1ULL << 34))
9427                     continue;
9428                   if (!bfd_get_section_contents (ibfd, sec, buf,
9429                                                  rel->r_offset & ~3, 8))
9430                     goto got_error_ret;
9431                   insn = bfd_get_32 (ibfd, buf);
9432                   if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9433                     continue;
9434                   insn = bfd_get_32 (ibfd, buf + 4);
9435                   if ((insn & (0x3fu << 26)) != 57u << 26)
9436                     continue;
9437                   break;
9438                 }
9439 #undef LIMIT_ADJUST
9440
9441               if (h != NULL)
9442                 ent = h->got.glist;
9443               else
9444                 {
9445                   struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9446                   ent = local_got_ents[r_symndx];
9447                 }
9448               for (; ent != NULL; ent = ent->next)
9449                 if (ent->addend == rel->r_addend
9450                     && ent->owner == ibfd
9451                     && ent->tls_type == 0)
9452                   break;
9453               BFD_ASSERT (ent && ent->got.refcount > 0);
9454               ent->got.refcount -= 1;
9455             }
9456
9457           if (elf_section_data (sec)->relocs != relstart)
9458             free (relstart);
9459         }
9460
9461       if (local_syms != NULL
9462           && symtab_hdr->contents != (unsigned char *) local_syms)
9463         {
9464           if (!info->keep_memory)
9465             free (local_syms);
9466           else
9467             symtab_hdr->contents = (unsigned char *) local_syms;
9468         }
9469     }
9470
9471   return TRUE;
9472 }
9473
9474 /* Return true iff input section I references the TOC using
9475    instructions limited to +/-32k offsets.  */
9476
9477 bfd_boolean
9478 ppc64_elf_has_small_toc_reloc (asection *i)
9479 {
9480   return (is_ppc64_elf (i->owner)
9481           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9482 }
9483
9484 /* Allocate space for one GOT entry.  */
9485
9486 static void
9487 allocate_got (struct elf_link_hash_entry *h,
9488               struct bfd_link_info *info,
9489               struct got_entry *gent)
9490 {
9491   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9492   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9493   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9494                  ? 16 : 8);
9495   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9496                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9497   asection *got = ppc64_elf_tdata (gent->owner)->got;
9498
9499   gent->got.offset = got->size;
9500   got->size += entsize;
9501
9502   if (h->type == STT_GNU_IFUNC)
9503     {
9504       htab->elf.irelplt->size += rentsize;
9505       htab->got_reli_size += rentsize;
9506     }
9507   else if (((bfd_link_pic (info)
9508              && !(gent->tls_type != 0
9509                   && bfd_link_executable (info)
9510                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9511             || (htab->elf.dynamic_sections_created
9512                 && h->dynindx != -1
9513                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9514            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9515     {
9516       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9517       relgot->size += rentsize;
9518     }
9519 }
9520
9521 /* This function merges got entries in the same toc group.  */
9522
9523 static void
9524 merge_got_entries (struct got_entry **pent)
9525 {
9526   struct got_entry *ent, *ent2;
9527
9528   for (ent = *pent; ent != NULL; ent = ent->next)
9529     if (!ent->is_indirect)
9530       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9531         if (!ent2->is_indirect
9532             && ent2->addend == ent->addend
9533             && ent2->tls_type == ent->tls_type
9534             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9535           {
9536             ent2->is_indirect = TRUE;
9537             ent2->got.ent = ent;
9538           }
9539 }
9540
9541 /* If H is undefined, make it dynamic if that makes sense.  */
9542
9543 static bfd_boolean
9544 ensure_undef_dynamic (struct bfd_link_info *info,
9545                       struct elf_link_hash_entry *h)
9546 {
9547   struct elf_link_hash_table *htab = elf_hash_table (info);
9548
9549   if (htab->dynamic_sections_created
9550       && ((info->dynamic_undefined_weak != 0
9551            && h->root.type == bfd_link_hash_undefweak)
9552           || h->root.type == bfd_link_hash_undefined)
9553       && h->dynindx == -1
9554       && !h->forced_local
9555       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9556     return bfd_elf_link_record_dynamic_symbol (info, h);
9557   return TRUE;
9558 }
9559
9560 /* Allocate space in .plt, .got and associated reloc sections for
9561    dynamic relocs.  */
9562
9563 static bfd_boolean
9564 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9565 {
9566   struct bfd_link_info *info;
9567   struct ppc_link_hash_table *htab;
9568   asection *s;
9569   struct ppc_link_hash_entry *eh;
9570   struct got_entry **pgent, *gent;
9571
9572   if (h->root.type == bfd_link_hash_indirect)
9573     return TRUE;
9574
9575   info = (struct bfd_link_info *) inf;
9576   htab = ppc_hash_table (info);
9577   if (htab == NULL)
9578     return FALSE;
9579
9580   eh = ppc_elf_hash_entry (h);
9581   /* Run through the TLS GD got entries first if we're changing them
9582      to TPREL.  */
9583   if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9584     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9585       if (gent->got.refcount > 0
9586           && (gent->tls_type & TLS_GD) != 0)
9587         {
9588           /* This was a GD entry that has been converted to TPREL.  If
9589              there happens to be a TPREL entry we can use that one.  */
9590           struct got_entry *ent;
9591           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9592             if (ent->got.refcount > 0
9593                 && (ent->tls_type & TLS_TPREL) != 0
9594                 && ent->addend == gent->addend
9595                 && ent->owner == gent->owner)
9596               {
9597                 gent->got.refcount = 0;
9598                 break;
9599               }
9600
9601           /* If not, then we'll be using our own TPREL entry.  */
9602           if (gent->got.refcount != 0)
9603             gent->tls_type = TLS_TLS | TLS_TPREL;
9604         }
9605
9606   /* Remove any list entry that won't generate a word in the GOT before
9607      we call merge_got_entries.  Otherwise we risk merging to empty
9608      entries.  */
9609   pgent = &h->got.glist;
9610   while ((gent = *pgent) != NULL)
9611     if (gent->got.refcount > 0)
9612       {
9613         if ((gent->tls_type & TLS_LD) != 0
9614             && SYMBOL_REFERENCES_LOCAL (info, h))
9615           {
9616             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9617             *pgent = gent->next;
9618           }
9619         else
9620           pgent = &gent->next;
9621       }
9622     else
9623       *pgent = gent->next;
9624
9625   if (!htab->do_multi_toc)
9626     merge_got_entries (&h->got.glist);
9627
9628   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9629     if (!gent->is_indirect)
9630       {
9631         /* Ensure we catch all the cases where this symbol should
9632            be made dynamic.  */
9633         if (!ensure_undef_dynamic (info, h))
9634           return FALSE;
9635
9636         if (!is_ppc64_elf (gent->owner))
9637           abort ();
9638
9639         allocate_got (h, info, gent);
9640       }
9641
9642   /* If no dynamic sections we can't have dynamic relocs, except for
9643      IFUNCs which are handled even in static executables.  */
9644   if (!htab->elf.dynamic_sections_created
9645       && h->type != STT_GNU_IFUNC)
9646     eh->dyn_relocs = NULL;
9647
9648   /* Discard relocs on undefined symbols that must be local.  */
9649   else if (h->root.type == bfd_link_hash_undefined
9650            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9651     eh->dyn_relocs = NULL;
9652
9653   /* Also discard relocs on undefined weak syms with non-default
9654      visibility, or when dynamic_undefined_weak says so.  */
9655   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9656     eh->dyn_relocs = NULL;
9657
9658   if (eh->dyn_relocs != NULL)
9659     {
9660       struct elf_dyn_relocs *p, **pp;
9661
9662       /* In the shared -Bsymbolic case, discard space allocated for
9663          dynamic pc-relative relocs against symbols which turn out to
9664          be defined in regular objects.  For the normal shared case,
9665          discard space for relocs that have become local due to symbol
9666          visibility changes.  */
9667       if (bfd_link_pic (info))
9668         {
9669           /* Relocs that use pc_count are those that appear on a call
9670              insn, or certain REL relocs (see must_be_dyn_reloc) that
9671              can be generated via assembly.  We want calls to
9672              protected symbols to resolve directly to the function
9673              rather than going via the plt.  If people want function
9674              pointer comparisons to work as expected then they should
9675              avoid writing weird assembly.  */
9676           if (SYMBOL_CALLS_LOCAL (info, h))
9677             {
9678               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9679                 {
9680                   p->count -= p->pc_count;
9681                   p->pc_count = 0;
9682                   if (p->count == 0)
9683                     *pp = p->next;
9684                   else
9685                     pp = &p->next;
9686                 }
9687             }
9688
9689           if (eh->dyn_relocs != NULL)
9690             {
9691               /* Ensure we catch all the cases where this symbol
9692                  should be made dynamic.  */
9693               if (!ensure_undef_dynamic (info, h))
9694                 return FALSE;
9695             }
9696         }
9697
9698       /* For a fixed position executable, discard space for
9699          relocs against symbols which are not dynamic.  */
9700       else if (h->type != STT_GNU_IFUNC)
9701         {
9702           if (h->dynamic_adjusted
9703               && !h->def_regular
9704               && !ELF_COMMON_DEF_P (h))
9705             {
9706               /* Ensure we catch all the cases where this symbol
9707                  should be made dynamic.  */
9708               if (!ensure_undef_dynamic (info, h))
9709                 return FALSE;
9710
9711               /* But if that didn't work out, discard dynamic relocs.  */
9712               if (h->dynindx == -1)
9713                 eh->dyn_relocs = NULL;
9714             }
9715           else
9716             eh->dyn_relocs = NULL;
9717         }
9718
9719       /* Finally, allocate space.  */
9720       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9721         {
9722           asection *sreloc = elf_section_data (p->sec)->sreloc;
9723           if (eh->elf.type == STT_GNU_IFUNC)
9724             sreloc = htab->elf.irelplt;
9725           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9726         }
9727     }
9728
9729   /* We might need a PLT entry when the symbol
9730      a) is dynamic, or
9731      b) is an ifunc, or
9732      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9733      d) has plt16 relocs and we are linking statically.  */
9734   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9735       || h->type == STT_GNU_IFUNC
9736       || (h->needs_plt && h->dynamic_adjusted)
9737       || (h->needs_plt
9738           && h->def_regular
9739           && !htab->elf.dynamic_sections_created
9740           && !htab->can_convert_all_inline_plt
9741           && (ppc_elf_hash_entry (h)->tls_mask
9742               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9743     {
9744       struct plt_entry *pent;
9745       bfd_boolean doneone = FALSE;
9746       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9747         if (pent->plt.refcount > 0)
9748           {
9749             if (!htab->elf.dynamic_sections_created
9750                 || h->dynindx == -1)
9751               {
9752                 if (h->type == STT_GNU_IFUNC)
9753                   {
9754                     s = htab->elf.iplt;
9755                     pent->plt.offset = s->size;
9756                     s->size += PLT_ENTRY_SIZE (htab);
9757                     s = htab->elf.irelplt;
9758                   }
9759                 else
9760                   {
9761                     s = htab->pltlocal;
9762                     pent->plt.offset = s->size;
9763                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9764                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9765                   }
9766               }
9767             else
9768               {
9769                 /* If this is the first .plt entry, make room for the special
9770                    first entry.  */
9771                 s = htab->elf.splt;
9772                 if (s->size == 0)
9773                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9774
9775                 pent->plt.offset = s->size;
9776
9777                 /* Make room for this entry.  */
9778                 s->size += PLT_ENTRY_SIZE (htab);
9779
9780                 /* Make room for the .glink code.  */
9781                 s = htab->glink;
9782                 if (s->size == 0)
9783                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9784                 if (htab->opd_abi)
9785                   {
9786                     /* We need bigger stubs past index 32767.  */
9787                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9788                       s->size += 4;
9789                     s->size += 2*4;
9790                   }
9791                 else
9792                   s->size += 4;
9793
9794                 /* We also need to make an entry in the .rela.plt section.  */
9795                 s = htab->elf.srelplt;
9796               }
9797             if (s != NULL)
9798               s->size += sizeof (Elf64_External_Rela);
9799             doneone = TRUE;
9800           }
9801         else
9802           pent->plt.offset = (bfd_vma) -1;
9803       if (!doneone)
9804         {
9805           h->plt.plist = NULL;
9806           h->needs_plt = 0;
9807         }
9808     }
9809   else
9810     {
9811       h->plt.plist = NULL;
9812       h->needs_plt = 0;
9813     }
9814
9815   return TRUE;
9816 }
9817
9818 #define PPC_LO(v) ((v) & 0xffff)
9819 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9820 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9821 #define D34(v) \
9822   ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9823 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9824
9825 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9826    to set up space for global entry stubs.  These are put in glink,
9827    after the branch table.  */
9828
9829 static bfd_boolean
9830 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9831 {
9832   struct bfd_link_info *info;
9833   struct ppc_link_hash_table *htab;
9834   struct plt_entry *pent;
9835   asection *s, *plt;
9836
9837   if (h->root.type == bfd_link_hash_indirect)
9838     return TRUE;
9839
9840   if (!h->pointer_equality_needed)
9841     return TRUE;
9842
9843   if (h->def_regular)
9844     return TRUE;
9845
9846   info = inf;
9847   htab = ppc_hash_table (info);
9848   if (htab == NULL)
9849     return FALSE;
9850
9851   s = htab->global_entry;
9852   plt = htab->elf.splt;
9853   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9854     if (pent->plt.offset != (bfd_vma) -1
9855         && pent->addend == 0)
9856       {
9857         /* For ELFv2, if this symbol is not defined in a regular file
9858            and we are not generating a shared library or pie, then we
9859            need to define the symbol in the executable on a call stub.
9860            This is to avoid text relocations.  */
9861         bfd_vma off, stub_align, stub_off, stub_size;
9862         unsigned int align_power;
9863
9864         stub_size = 16;
9865         stub_off = s->size;
9866         if (htab->params->plt_stub_align >= 0)
9867           align_power = htab->params->plt_stub_align;
9868         else
9869           align_power = -htab->params->plt_stub_align;
9870         /* Setting section alignment is delayed until we know it is
9871            non-empty.  Otherwise the .text output section will be
9872            aligned at least to plt_stub_align even when no global
9873            entry stubs are needed.  */
9874         if (s->alignment_power < align_power)
9875           s->alignment_power = align_power;
9876         stub_align = (bfd_vma) 1 << align_power;
9877         if (htab->params->plt_stub_align >= 0
9878             || ((((stub_off + stub_size - 1) & -stub_align)
9879                  - (stub_off & -stub_align))
9880                 > ((stub_size - 1) & -stub_align)))
9881           stub_off = (stub_off + stub_align - 1) & -stub_align;
9882         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9883         off -= stub_off + s->output_offset + s->output_section->vma;
9884         /* Note that for --plt-stub-align negative we have a possible
9885            dependency between stub offset and size.  Break that
9886            dependency by assuming the max stub size when calculating
9887            the stub offset.  */
9888         if (PPC_HA (off) == 0)
9889           stub_size -= 4;
9890         h->root.type = bfd_link_hash_defined;
9891         h->root.u.def.section = s;
9892         h->root.u.def.value = stub_off;
9893         s->size = stub_off + stub_size;
9894         break;
9895       }
9896   return TRUE;
9897 }
9898
9899 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9900    read-only sections.  */
9901
9902 static bfd_boolean
9903 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9904 {
9905   asection *sec;
9906
9907   if (h->root.type == bfd_link_hash_indirect)
9908     return TRUE;
9909
9910   sec = readonly_dynrelocs (h);
9911   if (sec != NULL)
9912     {
9913       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9914
9915       info->flags |= DF_TEXTREL;
9916       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
9917                                 " in read-only section `%pA'\n"),
9918                               sec->owner, h->root.root.string, sec);
9919
9920       /* Not an error, just cut short the traversal.  */
9921       return FALSE;
9922     }
9923   return TRUE;
9924 }
9925
9926 /* Set the sizes of the dynamic sections.  */
9927
9928 static bfd_boolean
9929 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9930                                  struct bfd_link_info *info)
9931 {
9932   struct ppc_link_hash_table *htab;
9933   bfd *dynobj;
9934   asection *s;
9935   bfd_boolean relocs;
9936   bfd *ibfd;
9937   struct got_entry *first_tlsld;
9938
9939   htab = ppc_hash_table (info);
9940   if (htab == NULL)
9941     return FALSE;
9942
9943   dynobj = htab->elf.dynobj;
9944   if (dynobj == NULL)
9945     abort ();
9946
9947   if (htab->elf.dynamic_sections_created)
9948     {
9949       /* Set the contents of the .interp section to the interpreter.  */
9950       if (bfd_link_executable (info) && !info->nointerp)
9951         {
9952           s = bfd_get_linker_section (dynobj, ".interp");
9953           if (s == NULL)
9954             abort ();
9955           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9956           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9957         }
9958     }
9959
9960   /* Set up .got offsets for local syms, and space for local dynamic
9961      relocs.  */
9962   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9963     {
9964       struct got_entry **lgot_ents;
9965       struct got_entry **end_lgot_ents;
9966       struct plt_entry **local_plt;
9967       struct plt_entry **end_local_plt;
9968       unsigned char *lgot_masks;
9969       bfd_size_type locsymcount;
9970       Elf_Internal_Shdr *symtab_hdr;
9971
9972       if (!is_ppc64_elf (ibfd))
9973         continue;
9974
9975       for (s = ibfd->sections; s != NULL; s = s->next)
9976         {
9977           struct ppc_dyn_relocs *p;
9978
9979           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9980             {
9981               if (!bfd_is_abs_section (p->sec)
9982                   && bfd_is_abs_section (p->sec->output_section))
9983                 {
9984                   /* Input section has been discarded, either because
9985                      it is a copy of a linkonce section or due to
9986                      linker script /DISCARD/, so we'll be discarding
9987                      the relocs too.  */
9988                 }
9989               else if (p->count != 0)
9990                 {
9991                   asection *srel = elf_section_data (p->sec)->sreloc;
9992                   if (p->ifunc)
9993                     srel = htab->elf.irelplt;
9994                   srel->size += p->count * sizeof (Elf64_External_Rela);
9995                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9996                     info->flags |= DF_TEXTREL;
9997                 }
9998             }
9999         }
10000
10001       lgot_ents = elf_local_got_ents (ibfd);
10002       if (!lgot_ents)
10003         continue;
10004
10005       symtab_hdr = &elf_symtab_hdr (ibfd);
10006       locsymcount = symtab_hdr->sh_info;
10007       end_lgot_ents = lgot_ents + locsymcount;
10008       local_plt = (struct plt_entry **) end_lgot_ents;
10009       end_local_plt = local_plt + locsymcount;
10010       lgot_masks = (unsigned char *) end_local_plt;
10011       s = ppc64_elf_tdata (ibfd)->got;
10012       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10013         {
10014           struct got_entry **pent, *ent;
10015
10016           pent = lgot_ents;
10017           while ((ent = *pent) != NULL)
10018             if (ent->got.refcount > 0)
10019               {
10020                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10021                   {
10022                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10023                     *pent = ent->next;
10024                   }
10025                 else
10026                   {
10027                     unsigned int ent_size = 8;
10028                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10029
10030                     ent->got.offset = s->size;
10031                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10032                       {
10033                         ent_size *= 2;
10034                         rel_size *= 2;
10035                       }
10036                     s->size += ent_size;
10037                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10038                       {
10039                         htab->elf.irelplt->size += rel_size;
10040                         htab->got_reli_size += rel_size;
10041                       }
10042                     else if (bfd_link_pic (info)
10043                              && !(ent->tls_type != 0
10044                                   && bfd_link_executable (info)))
10045                       {
10046                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10047                         srel->size += rel_size;
10048                       }
10049                     pent = &ent->next;
10050                   }
10051               }
10052             else
10053               *pent = ent->next;
10054         }
10055
10056       /* Allocate space for plt calls to local syms.  */
10057       lgot_masks = (unsigned char *) end_local_plt;
10058       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10059         {
10060           struct plt_entry *ent;
10061
10062           for (ent = *local_plt; ent != NULL; ent = ent->next)
10063             if (ent->plt.refcount > 0)
10064               {
10065                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10066                   {
10067                     s = htab->elf.iplt;
10068                     ent->plt.offset = s->size;
10069                     s->size += PLT_ENTRY_SIZE (htab);
10070                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10071                   }
10072                 else if (htab->can_convert_all_inline_plt
10073                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10074                   ent->plt.offset = (bfd_vma) -1;
10075                 else
10076                   {
10077                     s = htab->pltlocal;
10078                     ent->plt.offset = s->size;
10079                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10080                     if (bfd_link_pic (info))
10081                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10082                   }
10083               }
10084             else
10085               ent->plt.offset = (bfd_vma) -1;
10086         }
10087     }
10088
10089   /* Allocate global sym .plt and .got entries, and space for global
10090      sym dynamic relocs.  */
10091   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10092
10093   if (!htab->opd_abi && !bfd_link_pic (info))
10094     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10095
10096   first_tlsld = NULL;
10097   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10098     {
10099       struct got_entry *ent;
10100
10101       if (!is_ppc64_elf (ibfd))
10102         continue;
10103
10104       ent = ppc64_tlsld_got (ibfd);
10105       if (ent->got.refcount > 0)
10106         {
10107           if (!htab->do_multi_toc && first_tlsld != NULL)
10108             {
10109               ent->is_indirect = TRUE;
10110               ent->got.ent = first_tlsld;
10111             }
10112           else
10113             {
10114               if (first_tlsld == NULL)
10115                 first_tlsld = ent;
10116               s = ppc64_elf_tdata (ibfd)->got;
10117               ent->got.offset = s->size;
10118               ent->owner = ibfd;
10119               s->size += 16;
10120               if (bfd_link_dll (info))
10121                 {
10122                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10123                   srel->size += sizeof (Elf64_External_Rela);
10124                 }
10125             }
10126         }
10127       else
10128         ent->got.offset = (bfd_vma) -1;
10129     }
10130
10131   /* We now have determined the sizes of the various dynamic sections.
10132      Allocate memory for them.  */
10133   relocs = FALSE;
10134   for (s = dynobj->sections; s != NULL; s = s->next)
10135     {
10136       if ((s->flags & SEC_LINKER_CREATED) == 0)
10137         continue;
10138
10139       if (s == htab->brlt || s == htab->relbrlt)
10140         /* These haven't been allocated yet;  don't strip.  */
10141         continue;
10142       else if (s == htab->elf.sgot
10143                || s == htab->elf.splt
10144                || s == htab->elf.iplt
10145                || s == htab->pltlocal
10146                || s == htab->glink
10147                || s == htab->global_entry
10148                || s == htab->elf.sdynbss
10149                || s == htab->elf.sdynrelro)
10150         {
10151           /* Strip this section if we don't need it; see the
10152              comment below.  */
10153         }
10154       else if (s == htab->glink_eh_frame)
10155         {
10156           if (!bfd_is_abs_section (s->output_section))
10157             /* Not sized yet.  */
10158             continue;
10159         }
10160       else if (CONST_STRNEQ (s->name, ".rela"))
10161         {
10162           if (s->size != 0)
10163             {
10164               if (s != htab->elf.srelplt)
10165                 relocs = TRUE;
10166
10167               /* We use the reloc_count field as a counter if we need
10168                  to copy relocs into the output file.  */
10169               s->reloc_count = 0;
10170             }
10171         }
10172       else
10173         {
10174           /* It's not one of our sections, so don't allocate space.  */
10175           continue;
10176         }
10177
10178       if (s->size == 0)
10179         {
10180           /* If we don't need this section, strip it from the
10181              output file.  This is mostly to handle .rela.bss and
10182              .rela.plt.  We must create both sections in
10183              create_dynamic_sections, because they must be created
10184              before the linker maps input sections to output
10185              sections.  The linker does that before
10186              adjust_dynamic_symbol is called, and it is that
10187              function which decides whether anything needs to go
10188              into these sections.  */
10189           s->flags |= SEC_EXCLUDE;
10190           continue;
10191         }
10192
10193       if (bfd_is_abs_section (s->output_section))
10194         _bfd_error_handler (_("warning: discarding dynamic section %s"),
10195                             s->name);
10196
10197       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10198         continue;
10199
10200       /* Allocate memory for the section contents.  We use bfd_zalloc
10201          here in case unused entries are not reclaimed before the
10202          section's contents are written out.  This should not happen,
10203          but this way if it does we get a R_PPC64_NONE reloc in .rela
10204          sections instead of garbage.
10205          We also rely on the section contents being zero when writing
10206          the GOT and .dynrelro.  */
10207       s->contents = bfd_zalloc (dynobj, s->size);
10208       if (s->contents == NULL)
10209         return FALSE;
10210     }
10211
10212   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10213     {
10214       if (!is_ppc64_elf (ibfd))
10215         continue;
10216
10217       s = ppc64_elf_tdata (ibfd)->got;
10218       if (s != NULL && s != htab->elf.sgot)
10219         {
10220           if (s->size == 0)
10221             s->flags |= SEC_EXCLUDE;
10222           else
10223             {
10224               s->contents = bfd_zalloc (ibfd, s->size);
10225               if (s->contents == NULL)
10226                 return FALSE;
10227             }
10228         }
10229       s = ppc64_elf_tdata (ibfd)->relgot;
10230       if (s != NULL)
10231         {
10232           if (s->size == 0)
10233             s->flags |= SEC_EXCLUDE;
10234           else
10235             {
10236               s->contents = bfd_zalloc (ibfd, s->size);
10237               if (s->contents == NULL)
10238                 return FALSE;
10239               relocs = TRUE;
10240               s->reloc_count = 0;
10241             }
10242         }
10243     }
10244
10245   if (htab->elf.dynamic_sections_created)
10246     {
10247       bfd_boolean tls_opt;
10248
10249       /* Add some entries to the .dynamic section.  We fill in the
10250          values later, in ppc64_elf_finish_dynamic_sections, but we
10251          must add the entries now so that we get the correct size for
10252          the .dynamic section.  The DT_DEBUG entry is filled in by the
10253          dynamic linker and used by the debugger.  */
10254 #define add_dynamic_entry(TAG, VAL) \
10255   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10256
10257       if (bfd_link_executable (info))
10258         {
10259           if (!add_dynamic_entry (DT_DEBUG, 0))
10260             return FALSE;
10261         }
10262
10263       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10264         {
10265           if (!add_dynamic_entry (DT_PLTGOT, 0)
10266               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10267               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10268               || !add_dynamic_entry (DT_JMPREL, 0)
10269               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10270             return FALSE;
10271         }
10272
10273       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10274         {
10275           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10276               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10277             return FALSE;
10278         }
10279
10280       tls_opt = (htab->params->tls_get_addr_opt
10281                  && ((htab->tls_get_addr_fd != NULL
10282                       && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10283                      || (htab->tga_desc_fd != NULL
10284                          && htab->tga_desc_fd->elf.plt.plist != NULL)));
10285       if (tls_opt || !htab->opd_abi)
10286         {
10287           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10288             return FALSE;
10289         }
10290
10291       if (relocs)
10292         {
10293           if (!add_dynamic_entry (DT_RELA, 0)
10294               || !add_dynamic_entry (DT_RELASZ, 0)
10295               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10296             return FALSE;
10297
10298           /* If any dynamic relocs apply to a read-only section,
10299              then we need a DT_TEXTREL entry.  */
10300           if ((info->flags & DF_TEXTREL) == 0)
10301             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10302
10303           if ((info->flags & DF_TEXTREL) != 0)
10304             {
10305               if (!add_dynamic_entry (DT_TEXTREL, 0))
10306                 return FALSE;
10307             }
10308         }
10309     }
10310 #undef add_dynamic_entry
10311
10312   return TRUE;
10313 }
10314
10315 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10316
10317 static bfd_boolean
10318 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10319 {
10320   if (h->plt.plist != NULL
10321       && !h->def_regular
10322       && !h->pointer_equality_needed)
10323     return FALSE;
10324
10325   return _bfd_elf_hash_symbol (h);
10326 }
10327
10328 /* Determine the type of stub needed, if any, for a call.  */
10329
10330 static inline enum ppc_stub_type
10331 ppc_type_of_stub (asection *input_sec,
10332                   const Elf_Internal_Rela *rel,
10333                   struct ppc_link_hash_entry **hash,
10334                   struct plt_entry **plt_ent,
10335                   bfd_vma destination,
10336                   unsigned long local_off)
10337 {
10338   struct ppc_link_hash_entry *h = *hash;
10339   bfd_vma location;
10340   bfd_vma branch_offset;
10341   bfd_vma max_branch_offset;
10342   enum elf_ppc64_reloc_type r_type;
10343
10344   if (h != NULL)
10345     {
10346       struct plt_entry *ent;
10347       struct ppc_link_hash_entry *fdh = h;
10348       if (h->oh != NULL
10349           && h->oh->is_func_descriptor)
10350         {
10351           fdh = ppc_follow_link (h->oh);
10352           *hash = fdh;
10353         }
10354
10355       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10356         if (ent->addend == rel->r_addend
10357             && ent->plt.offset != (bfd_vma) -1)
10358           {
10359             *plt_ent = ent;
10360             return ppc_stub_plt_call;
10361           }
10362
10363       /* Here, we know we don't have a plt entry.  If we don't have a
10364          either a defined function descriptor or a defined entry symbol
10365          in a regular object file, then it is pointless trying to make
10366          any other type of stub.  */
10367       if (!is_static_defined (&fdh->elf)
10368           && !is_static_defined (&h->elf))
10369         return ppc_stub_none;
10370     }
10371   else if (elf_local_got_ents (input_sec->owner) != NULL)
10372     {
10373       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10374       struct plt_entry **local_plt = (struct plt_entry **)
10375         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10376       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10377
10378       if (local_plt[r_symndx] != NULL)
10379         {
10380           struct plt_entry *ent;
10381
10382           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10383             if (ent->addend == rel->r_addend
10384                 && ent->plt.offset != (bfd_vma) -1)
10385               {
10386                 *plt_ent = ent;
10387                 return ppc_stub_plt_call;
10388               }
10389         }
10390     }
10391
10392   /* Determine where the call point is.  */
10393   location = (input_sec->output_offset
10394               + input_sec->output_section->vma
10395               + rel->r_offset);
10396
10397   branch_offset = destination - location;
10398   r_type = ELF64_R_TYPE (rel->r_info);
10399
10400   /* Determine if a long branch stub is needed.  */
10401   max_branch_offset = 1 << 25;
10402   if (r_type == R_PPC64_REL14
10403       || r_type == R_PPC64_REL14_BRTAKEN
10404       || r_type == R_PPC64_REL14_BRNTAKEN)
10405     max_branch_offset = 1 << 15;
10406
10407   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10408     /* We need a stub.  Figure out whether a long_branch or plt_branch
10409        is needed later.  */
10410     return ppc_stub_long_branch;
10411
10412   return ppc_stub_none;
10413 }
10414
10415 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10416    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10417    .    mflr    %r12
10418    .    bcl     20,31,1f
10419    .1:  mflr    %r11
10420    .    mtlr    %r12
10421    .    lis     %r12,xxx-1b@highest
10422    .    ori     %r12,%r12,xxx-1b@higher
10423    .    sldi    %r12,%r12,32
10424    .    oris    %r12,%r12,xxx-1b@high
10425    .    ori     %r12,%r12,xxx-1b@l
10426    .    add/ldx %r12,%r11,%r12  */
10427
10428 static bfd_byte *
10429 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10430 {
10431   bfd_put_32 (abfd, MFLR_R12, p);
10432   p += 4;
10433   bfd_put_32 (abfd, BCL_20_31, p);
10434   p += 4;
10435   bfd_put_32 (abfd, MFLR_R11, p);
10436   p += 4;
10437   bfd_put_32 (abfd, MTLR_R12, p);
10438   p += 4;
10439   if (off + 0x8000 < 0x10000)
10440     {
10441       if (load)
10442         bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10443       else
10444         bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10445       p += 4;
10446     }
10447   else if (off + 0x80008000ULL < 0x100000000ULL)
10448     {
10449       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10450       p += 4;
10451       if (load)
10452         bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10453       else
10454         bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10455       p += 4;
10456     }
10457   else
10458     {
10459       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10460         {
10461           bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10462           p += 4;
10463         }
10464       else
10465         {
10466           bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10467           p += 4;
10468           if (((off >> 32) & 0xffff) != 0)
10469             {
10470               bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10471               p += 4;
10472             }
10473         }
10474       if (((off >> 32) & 0xffffffffULL) != 0)
10475         {
10476           bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10477           p += 4;
10478         }
10479       if (PPC_HI (off) != 0)
10480         {
10481           bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10482           p += 4;
10483         }
10484       if (PPC_LO (off) != 0)
10485         {
10486           bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10487           p += 4;
10488         }
10489       if (load)
10490         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10491       else
10492         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10493       p += 4;
10494     }
10495   return p;
10496 }
10497
10498 static unsigned int
10499 size_offset (bfd_vma off)
10500 {
10501   unsigned int size;
10502   if (off + 0x8000 < 0x10000)
10503     size = 4;
10504   else if (off + 0x80008000ULL < 0x100000000ULL)
10505     size = 8;
10506   else
10507     {
10508       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10509         size = 4;
10510       else
10511         {
10512           size = 4;
10513           if (((off >> 32) & 0xffff) != 0)
10514             size += 4;
10515         }
10516       if (((off >> 32) & 0xffffffffULL) != 0)
10517         size += 4;
10518       if (PPC_HI (off) != 0)
10519         size += 4;
10520       if (PPC_LO (off) != 0)
10521         size += 4;
10522       size += 4;
10523     }
10524   return size + 16;
10525 }
10526
10527 static unsigned int
10528 num_relocs_for_offset (bfd_vma off)
10529 {
10530   unsigned int num_rel;
10531   if (off + 0x8000 < 0x10000)
10532     num_rel = 1;
10533   else if (off + 0x80008000ULL < 0x100000000ULL)
10534     num_rel = 2;
10535   else
10536     {
10537       num_rel = 1;
10538       if (off + 0x800000000000ULL >= 0x1000000000000ULL
10539           && ((off >> 32) & 0xffff) != 0)
10540         num_rel += 1;
10541       if (PPC_HI (off) != 0)
10542         num_rel += 1;
10543       if (PPC_LO (off) != 0)
10544         num_rel += 1;
10545     }
10546   return num_rel;
10547 }
10548
10549 static Elf_Internal_Rela *
10550 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10551                         bfd_vma roff, bfd_vma targ, bfd_vma off)
10552 {
10553   bfd_vma relative_targ = targ - (roff - 8);
10554   if (bfd_big_endian (info->output_bfd))
10555     roff += 2;
10556   r->r_offset = roff;
10557   r->r_addend = relative_targ + roff;
10558   if (off + 0x8000 < 0x10000)
10559     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10560   else if (off + 0x80008000ULL < 0x100000000ULL)
10561     {
10562       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10563       ++r;
10564       roff += 4;
10565       r->r_offset = roff;
10566       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10567       r->r_addend = relative_targ + roff;
10568     }
10569   else
10570     {
10571       if (off + 0x800000000000ULL < 0x1000000000000ULL)
10572         r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10573       else
10574         {
10575           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10576           if (((off >> 32) & 0xffff) != 0)
10577             {
10578               ++r;
10579               roff += 4;
10580               r->r_offset = roff;
10581               r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10582               r->r_addend = relative_targ + roff;
10583             }
10584         }
10585       if (((off >> 32) & 0xffffffffULL) != 0)
10586         roff += 4;
10587       if (PPC_HI (off) != 0)
10588         {
10589           ++r;
10590           roff += 4;
10591           r->r_offset = roff;
10592           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10593           r->r_addend = relative_targ + roff;
10594         }
10595       if (PPC_LO (off) != 0)
10596         {
10597           ++r;
10598           roff += 4;
10599           r->r_offset = roff;
10600           r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10601           r->r_addend = relative_targ + roff;
10602         }
10603     }
10604   return r;
10605 }
10606
10607 static bfd_byte *
10608 build_powerxx_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10609                       bfd_boolean load)
10610 {
10611   uint64_t insn;
10612   if (off - odd + (1ULL << 33) < 1ULL << 34)
10613     {
10614       off -= odd;
10615       if (odd)
10616         {
10617           bfd_put_32 (abfd, NOP, p);
10618           p += 4;
10619         }
10620       if (load)
10621         insn = PLD_R12_PC;
10622       else
10623         insn = PADDI_R12_PC;
10624       insn |= D34 (off);
10625       bfd_put_32 (abfd, insn >> 32, p);
10626       p += 4;
10627       bfd_put_32 (abfd, insn, p);
10628     }
10629   /* The minimum value for paddi is -0x200000000.  The minimum value
10630      for li is -0x8000, which when shifted by 34 and added gives a
10631      minimum value of -0x2000200000000.  The maximum value is
10632      0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
10633   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10634     {
10635       off -= 8 - odd;
10636       bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10637       p += 4;
10638       if (!odd)
10639         {
10640           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10641           p += 4;
10642         }
10643       insn = PADDI_R12_PC | D34 (off);
10644       bfd_put_32 (abfd, insn >> 32, p);
10645       p += 4;
10646       bfd_put_32 (abfd, insn, p);
10647       p += 4;
10648       if (odd)
10649         {
10650           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10651           p += 4;
10652         }
10653       if (load)
10654         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10655       else
10656         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10657     }
10658   else
10659     {
10660       off -= odd + 8;
10661       bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10662       p += 4;
10663       bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10664       p += 4;
10665       if (odd)
10666         {
10667           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10668           p += 4;
10669         }
10670       insn = PADDI_R12_PC | D34 (off);
10671       bfd_put_32 (abfd, insn >> 32, p);
10672       p += 4;
10673       bfd_put_32 (abfd, insn, p);
10674       p += 4;
10675       if (!odd)
10676         {
10677           bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10678           p += 4;
10679         }
10680       if (load)
10681         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10682       else
10683         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10684     }
10685   p += 4;
10686   return p;
10687 }
10688
10689 static unsigned int
10690 size_powerxx_offset (bfd_vma off, int odd)
10691 {
10692   if (off - odd + (1ULL << 33) < 1ULL << 34)
10693     return odd + 8;
10694   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10695     return 20;
10696   else
10697     return 24;
10698 }
10699
10700 static unsigned int
10701 num_relocs_for_powerxx_offset (bfd_vma off, int odd)
10702 {
10703   if (off - odd + (1ULL << 33) < 1ULL << 34)
10704     return 1;
10705   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10706     return 2;
10707   else
10708     return 3;
10709 }
10710
10711 static Elf_Internal_Rela *
10712 emit_relocs_for_powerxx_offset (struct bfd_link_info *info,
10713                                 Elf_Internal_Rela *r, bfd_vma roff,
10714                                 bfd_vma targ, bfd_vma off, int odd)
10715 {
10716   if (off - odd + (1ULL << 33) < 1ULL << 34)
10717     roff += odd;
10718   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10719     {
10720       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10721       r->r_offset = roff + d_offset;
10722       r->r_addend = targ + 8 - odd - d_offset;
10723       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10724       ++r;
10725       roff += 8 - odd;
10726     }
10727   else
10728     {
10729       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10730       r->r_offset = roff + d_offset;
10731       r->r_addend = targ + 8 + odd - d_offset;
10732       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10733       ++r;
10734       roff += 4;
10735       r->r_offset = roff + d_offset;
10736       r->r_addend = targ + 4 + odd - d_offset;
10737       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10738       ++r;
10739       roff += 4 + odd;
10740     }
10741   r->r_offset = roff;
10742   r->r_addend = targ;
10743   r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10744   return r;
10745 }
10746
10747 /* Emit .eh_frame opcode to advance pc by DELTA.  */
10748
10749 static bfd_byte *
10750 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10751 {
10752   delta /= 4;
10753   if (delta < 64)
10754     *eh++ = DW_CFA_advance_loc + delta;
10755   else if (delta < 256)
10756     {
10757       *eh++ = DW_CFA_advance_loc1;
10758       *eh++ = delta;
10759     }
10760   else if (delta < 65536)
10761     {
10762       *eh++ = DW_CFA_advance_loc2;
10763       bfd_put_16 (abfd, delta, eh);
10764       eh += 2;
10765     }
10766   else
10767     {
10768       *eh++ = DW_CFA_advance_loc4;
10769       bfd_put_32 (abfd, delta, eh);
10770       eh += 4;
10771     }
10772   return eh;
10773 }
10774
10775 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
10776
10777 static unsigned int
10778 eh_advance_size (unsigned int delta)
10779 {
10780   if (delta < 64 * 4)
10781     /* DW_CFA_advance_loc+[1..63].  */
10782     return 1;
10783   if (delta < 256 * 4)
10784     /* DW_CFA_advance_loc1, byte.  */
10785     return 2;
10786   if (delta < 65536 * 4)
10787     /* DW_CFA_advance_loc2, 2 bytes.  */
10788     return 3;
10789   /* DW_CFA_advance_loc4, 4 bytes.  */
10790   return 5;
10791 }
10792
10793 /* With power7 weakly ordered memory model, it is possible for ld.so
10794    to update a plt entry in one thread and have another thread see a
10795    stale zero toc entry.  To avoid this we need some sort of acquire
10796    barrier in the call stub.  One solution is to make the load of the
10797    toc word seem to appear to depend on the load of the function entry
10798    word.  Another solution is to test for r2 being zero, and branch to
10799    the appropriate glink entry if so.
10800
10801    .    fake dep barrier        compare
10802    .    ld 12,xxx(2)            ld 12,xxx(2)
10803    .    mtctr 12                mtctr 12
10804    .    xor 11,12,12            ld 2,xxx+8(2)
10805    .    add 2,2,11              cmpldi 2,0
10806    .    ld 2,xxx+8(2)           bnectr+
10807    .    bctr                    b <glink_entry>
10808
10809    The solution involving the compare turns out to be faster, so
10810    that's what we use unless the branch won't reach.  */
10811
10812 #define ALWAYS_USE_FAKE_DEP 0
10813 #define ALWAYS_EMIT_R2SAVE 0
10814
10815 static inline unsigned int
10816 plt_stub_size (struct ppc_link_hash_table *htab,
10817                struct ppc_stub_hash_entry *stub_entry,
10818                bfd_vma off)
10819 {
10820   unsigned size;
10821
10822   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10823     {
10824       if (htab->powerxx_stubs)
10825         {
10826           bfd_vma start = (stub_entry->stub_offset
10827                            + stub_entry->group->stub_sec->output_offset
10828                            + stub_entry->group->stub_sec->output_section->vma);
10829           if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10830             start += 4;
10831           size = 8 + size_powerxx_offset (off, start & 4);
10832         }
10833       else
10834         size = 8 + size_offset (off - 8);
10835       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10836         size += 4;
10837       return size;
10838     }
10839
10840   size = 12;
10841   if (ALWAYS_EMIT_R2SAVE
10842       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10843     size += 4;
10844   if (PPC_HA (off) != 0)
10845     size += 4;
10846   if (htab->opd_abi)
10847     {
10848       size += 4;
10849       if (htab->params->plt_static_chain)
10850         size += 4;
10851       if (htab->params->plt_thread_safe
10852           && htab->elf.dynamic_sections_created
10853           && stub_entry->h != NULL
10854           && stub_entry->h->elf.dynindx != -1)
10855         size += 8;
10856       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10857         size += 4;
10858     }
10859   if (stub_entry->h != NULL
10860       && is_tls_get_addr (&stub_entry->h->elf, htab)
10861       && htab->params->tls_get_addr_opt)
10862     {
10863       if (htab->params->no_tls_get_addr_regsave)
10864         {
10865           size += 7 * 4;
10866           if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10867             size += 6 * 4;
10868         }
10869       else
10870         {
10871           size += 30 * 4;
10872           if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
10873             size += 4;
10874         }
10875     }
10876   return size;
10877 }
10878
10879 /* Depending on the sign of plt_stub_align:
10880    If positive, return the padding to align to a 2**plt_stub_align
10881    boundary.
10882    If negative, if this stub would cross fewer 2**plt_stub_align
10883    boundaries if we align, then return the padding needed to do so.  */
10884
10885 static inline unsigned int
10886 plt_stub_pad (struct ppc_link_hash_table *htab,
10887               struct ppc_stub_hash_entry *stub_entry,
10888               bfd_vma plt_off)
10889 {
10890   int stub_align;
10891   unsigned stub_size;
10892   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10893
10894   if (htab->params->plt_stub_align >= 0)
10895     {
10896       stub_align = 1 << htab->params->plt_stub_align;
10897       if ((stub_off & (stub_align - 1)) != 0)
10898         return stub_align - (stub_off & (stub_align - 1));
10899       return 0;
10900     }
10901
10902   stub_align = 1 << -htab->params->plt_stub_align;
10903   stub_size = plt_stub_size (htab, stub_entry, plt_off);
10904   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10905       > ((stub_size - 1) & -stub_align))
10906     return stub_align - (stub_off & (stub_align - 1));
10907   return 0;
10908 }
10909
10910 /* Build a .plt call stub.  */
10911
10912 static inline bfd_byte *
10913 build_plt_stub (struct ppc_link_hash_table *htab,
10914                 struct ppc_stub_hash_entry *stub_entry,
10915                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10916 {
10917   bfd *obfd = htab->params->stub_bfd;
10918   bfd_boolean plt_load_toc = htab->opd_abi;
10919   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10920   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10921                                  && htab->elf.dynamic_sections_created
10922                                  && stub_entry->h != NULL
10923                                  && stub_entry->h->elf.dynindx != -1);
10924   bfd_boolean use_fake_dep = plt_thread_safe;
10925   bfd_vma cmp_branch_off = 0;
10926
10927   if (!ALWAYS_USE_FAKE_DEP
10928       && plt_load_toc
10929       && plt_thread_safe
10930       && !(is_tls_get_addr (&stub_entry->h->elf, htab)
10931            && htab->params->tls_get_addr_opt))
10932     {
10933       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10934       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10935                           / PLT_ENTRY_SIZE (htab));
10936       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10937       bfd_vma to, from;
10938
10939       if (pltindex > 32768)
10940         glinkoff += (pltindex - 32768) * 4;
10941       to = (glinkoff
10942             + htab->glink->output_offset
10943             + htab->glink->output_section->vma);
10944       from = (p - stub_entry->group->stub_sec->contents
10945               + 4 * (ALWAYS_EMIT_R2SAVE
10946                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10947               + 4 * (PPC_HA (offset) != 0)
10948               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10949                      != PPC_HA (offset))
10950               + 4 * (plt_static_chain != 0)
10951               + 20
10952               + stub_entry->group->stub_sec->output_offset
10953               + stub_entry->group->stub_sec->output_section->vma);
10954       cmp_branch_off = to - from;
10955       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10956     }
10957
10958   if (PPC_HA (offset) != 0)
10959     {
10960       if (r != NULL)
10961         {
10962           if (ALWAYS_EMIT_R2SAVE
10963               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10964             r[0].r_offset += 4;
10965           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10966           r[1].r_offset = r[0].r_offset + 4;
10967           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10968           r[1].r_addend = r[0].r_addend;
10969           if (plt_load_toc)
10970             {
10971               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10972                 {
10973                   r[2].r_offset = r[1].r_offset + 4;
10974                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10975                   r[2].r_addend = r[0].r_addend;
10976                 }
10977               else
10978                 {
10979                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10980                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10981                   r[2].r_addend = r[0].r_addend + 8;
10982                   if (plt_static_chain)
10983                     {
10984                       r[3].r_offset = r[2].r_offset + 4;
10985                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10986                       r[3].r_addend = r[0].r_addend + 16;
10987                     }
10988                 }
10989             }
10990         }
10991       if (ALWAYS_EMIT_R2SAVE
10992           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10993         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10994       if (plt_load_toc)
10995         {
10996           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10997           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10998         }
10999       else
11000         {
11001           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11002           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
11003         }
11004       if (plt_load_toc
11005           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11006         {
11007           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11008           offset = 0;
11009         }
11010       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
11011       if (plt_load_toc)
11012         {
11013           if (use_fake_dep)
11014             {
11015               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
11016               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
11017             }
11018           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11019           if (plt_static_chain)
11020             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11021         }
11022     }
11023   else
11024     {
11025       if (r != NULL)
11026         {
11027           if (ALWAYS_EMIT_R2SAVE
11028               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11029             r[0].r_offset += 4;
11030           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11031           if (plt_load_toc)
11032             {
11033               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11034                 {
11035                   r[1].r_offset = r[0].r_offset + 4;
11036                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11037                   r[1].r_addend = r[0].r_addend;
11038                 }
11039               else
11040                 {
11041                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11042                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11043                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11044                   if (plt_static_chain)
11045                     {
11046                       r[2].r_offset = r[1].r_offset + 4;
11047                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11048                       r[2].r_addend = r[0].r_addend + 8;
11049                     }
11050                 }
11051             }
11052         }
11053       if (ALWAYS_EMIT_R2SAVE
11054           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11055         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
11056       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
11057       if (plt_load_toc
11058           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11059         {
11060           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
11061           offset = 0;
11062         }
11063       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
11064       if (plt_load_toc)
11065         {
11066           if (use_fake_dep)
11067             {
11068               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
11069               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
11070             }
11071           if (plt_static_chain)
11072             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11073           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11074         }
11075     }
11076   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11077     {
11078       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
11079       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
11080       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11081     }
11082   else
11083     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
11084   return p;
11085 }
11086
11087 /* Build a special .plt call stub for __tls_get_addr.  */
11088
11089 #define LD_R0_0R3       0xe8030000
11090 #define LD_R12_0R3      0xe9830000
11091 #define MR_R0_R3        0x7c601b78
11092 #define CMPDI_R0_0      0x2c200000
11093 #define ADD_R3_R12_R13  0x7c6c6a14
11094 #define BEQLR           0x4d820020
11095 #define MR_R3_R0        0x7c030378
11096 #define BCTRL           0x4e800421
11097
11098 static inline bfd_byte *
11099 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11100                          struct ppc_stub_hash_entry *stub_entry,
11101                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11102 {
11103   bfd *obfd = htab->params->stub_bfd;
11104   bfd_byte *loc = p;
11105   unsigned int i;
11106
11107   bfd_put_32 (obfd, LD_R0_0R3 + 0, p),          p += 4;
11108   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
11109   bfd_put_32 (obfd, CMPDI_R0_0, p),             p += 4;
11110   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
11111   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
11112   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
11113   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
11114   if (htab->params->no_tls_get_addr_regsave)
11115     {
11116       if (r != NULL)
11117         r[0].r_offset += 7 * 4;
11118       if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
11119         return build_plt_stub (htab, stub_entry, p, offset, r);
11120
11121       bfd_put_32 (obfd, MFLR_R0, p);
11122       p += 4;
11123       bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11124       p += 4;
11125
11126       if (r != NULL)
11127         r[0].r_offset += 2 * 4;
11128       p = build_plt_stub (htab, stub_entry, p, offset, r);
11129       bfd_put_32 (obfd, BCTRL, p - 4);
11130
11131       bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11132       p += 4;
11133       bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11134       p += 4;
11135       bfd_put_32 (obfd, MTLR_R0, p);
11136       p += 4;
11137       bfd_put_32 (obfd, BLR, p);
11138       p += 4;
11139     }
11140   else
11141     {
11142       p = tls_get_addr_prologue (obfd, p, htab);
11143
11144       if (r != NULL)
11145         r[0].r_offset += 18 * 4;
11146
11147       p = build_plt_stub (htab, stub_entry, p, offset, r);
11148       bfd_put_32 (obfd, BCTRL, p - 4);
11149
11150       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
11151         {
11152           bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11153           p += 4;
11154         }
11155
11156       p = tls_get_addr_epilogue (obfd, p, htab);
11157     }
11158
11159   if (htab->glink_eh_frame != NULL
11160       && htab->glink_eh_frame->size != 0)
11161     {
11162       bfd_byte *base, *eh;
11163
11164       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11165       eh = base + stub_entry->group->eh_size;
11166       if (htab->params->no_tls_get_addr_regsave)
11167         {
11168           unsigned int lr_used, delta;
11169           lr_used = stub_entry->stub_offset + (p - 20 - loc);
11170           delta = lr_used - stub_entry->group->lr_restore;
11171           stub_entry->group->lr_restore = lr_used + 16;
11172           eh = eh_advance (htab->elf.dynobj, eh, delta);
11173           *eh++ = DW_CFA_offset_extended_sf;
11174           *eh++ = 65;
11175           *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11176           *eh++ = DW_CFA_advance_loc + 4;
11177         }
11178       else
11179         {
11180           unsigned int cfa_updt, delta;
11181           /* After the bctrl, lr has been modified so we need to emit
11182              .eh_frame info saying the return address is on the stack.  In
11183              fact we must put the EH info at or before the call rather
11184              than after it, because the EH info for a call needs to be
11185              specified by that point.
11186              See libgcc/unwind-dw2.c execute_cfa_program.
11187              Any stack pointer update must be described immediately after
11188              the instruction making the change, and since the stdu occurs
11189              after saving regs we put all the reg saves and the cfa
11190              change there.  */
11191           cfa_updt = stub_entry->stub_offset + 18 * 4;
11192           delta = cfa_updt - stub_entry->group->lr_restore;
11193           stub_entry->group->lr_restore
11194             = stub_entry->stub_offset + (p - loc) - 4;
11195           eh = eh_advance (htab->elf.dynobj, eh, delta);
11196           *eh++ = DW_CFA_def_cfa_offset;
11197           if (htab->opd_abi)
11198             {
11199               *eh++ = 128;
11200               *eh++ = 1;
11201             }
11202           else
11203             *eh++ = 96;
11204           *eh++ = DW_CFA_offset_extended_sf;
11205           *eh++ = 65;
11206           *eh++ = (-16 / 8) & 0x7f;
11207           for (i = 4; i < 12; i++)
11208             {
11209               *eh++ = DW_CFA_offset + i;
11210               *eh++ = (htab->opd_abi ? 13 : 12) - i;
11211             }
11212           *eh++ = (DW_CFA_advance_loc
11213                    + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11214           *eh++ = DW_CFA_def_cfa_offset;
11215           *eh++ = 0;
11216           for (i = 4; i < 12; i++)
11217             *eh++ = DW_CFA_restore + i;
11218           *eh++ = DW_CFA_advance_loc + 2;
11219         }
11220       *eh++ = DW_CFA_restore_extended;
11221       *eh++ = 65;
11222       stub_entry->group->eh_size = eh - base;
11223     }
11224   return p;
11225 }
11226
11227 static Elf_Internal_Rela *
11228 get_relocs (asection *sec, int count)
11229 {
11230   Elf_Internal_Rela *relocs;
11231   struct bfd_elf_section_data *elfsec_data;
11232
11233   elfsec_data = elf_section_data (sec);
11234   relocs = elfsec_data->relocs;
11235   if (relocs == NULL)
11236     {
11237       bfd_size_type relsize;
11238       relsize = sec->reloc_count * sizeof (*relocs);
11239       relocs = bfd_alloc (sec->owner, relsize);
11240       if (relocs == NULL)
11241         return NULL;
11242       elfsec_data->relocs = relocs;
11243       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11244                                           sizeof (Elf_Internal_Shdr));
11245       if (elfsec_data->rela.hdr == NULL)
11246         return NULL;
11247       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11248                                         * sizeof (Elf64_External_Rela));
11249       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11250       sec->reloc_count = 0;
11251     }
11252   relocs += sec->reloc_count;
11253   sec->reloc_count += count;
11254   return relocs;
11255 }
11256
11257 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11258    forms, to the equivalent relocs against the global symbol given by
11259    STUB_ENTRY->H.  */
11260
11261 static bfd_boolean
11262 use_global_in_relocs (struct ppc_link_hash_table *htab,
11263                       struct ppc_stub_hash_entry *stub_entry,
11264                       Elf_Internal_Rela *r, unsigned int num_rel)
11265 {
11266   struct elf_link_hash_entry **hashes;
11267   unsigned long symndx;
11268   struct ppc_link_hash_entry *h;
11269   bfd_vma symval;
11270
11271   /* Relocs are always against symbols in their own object file.  Fake
11272      up global sym hashes for the stub bfd (which has no symbols).  */
11273   hashes = elf_sym_hashes (htab->params->stub_bfd);
11274   if (hashes == NULL)
11275     {
11276       bfd_size_type hsize;
11277
11278       /* When called the first time, stub_globals will contain the
11279          total number of symbols seen during stub sizing.  After
11280          allocating, stub_globals is used as an index to fill the
11281          hashes array.  */
11282       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11283       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11284       if (hashes == NULL)
11285         return FALSE;
11286       elf_sym_hashes (htab->params->stub_bfd) = hashes;
11287       htab->stub_globals = 1;
11288     }
11289   symndx = htab->stub_globals++;
11290   h = stub_entry->h;
11291   hashes[symndx] = &h->elf;
11292   if (h->oh != NULL && h->oh->is_func)
11293     h = ppc_follow_link (h->oh);
11294   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11295               || h->elf.root.type == bfd_link_hash_defweak);
11296   symval = defined_sym_val (&h->elf);
11297   while (num_rel-- != 0)
11298     {
11299       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11300       if (h->elf.root.u.def.section != stub_entry->target_section)
11301         {
11302           /* H is an opd symbol.  The addend must be zero, and the
11303              branch reloc is the only one we can convert.  */
11304           r->r_addend = 0;
11305           break;
11306         }
11307       else
11308         r->r_addend -= symval;
11309       --r;
11310     }
11311   return TRUE;
11312 }
11313
11314 static bfd_vma
11315 get_r2off (struct bfd_link_info *info,
11316            struct ppc_stub_hash_entry *stub_entry)
11317 {
11318   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11319   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11320
11321   if (r2off == 0)
11322     {
11323       /* Support linking -R objects.  Get the toc pointer from the
11324          opd entry.  */
11325       char buf[8];
11326       if (!htab->opd_abi)
11327         return r2off;
11328       asection *opd = stub_entry->h->elf.root.u.def.section;
11329       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11330
11331       if (strcmp (opd->name, ".opd") != 0
11332           || opd->reloc_count != 0)
11333         {
11334           info->callbacks->einfo
11335             (_("%P: cannot find opd entry toc for `%pT'\n"),
11336              stub_entry->h->elf.root.root.string);
11337           bfd_set_error (bfd_error_bad_value);
11338           return (bfd_vma) -1;
11339         }
11340       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11341         return (bfd_vma) -1;
11342       r2off = bfd_get_64 (opd->owner, buf);
11343       r2off -= elf_gp (info->output_bfd);
11344     }
11345   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11346   return r2off;
11347 }
11348
11349 static bfd_boolean
11350 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11351 {
11352   struct ppc_stub_hash_entry *stub_entry;
11353   struct ppc_branch_hash_entry *br_entry;
11354   struct bfd_link_info *info;
11355   struct ppc_link_hash_table *htab;
11356   bfd_byte *loc;
11357   bfd_byte *p, *relp;
11358   bfd_vma targ, off;
11359   Elf_Internal_Rela *r;
11360   asection *plt;
11361   int num_rel;
11362   int odd;
11363
11364   /* Massage our args to the form they really have.  */
11365   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11366   info = in_arg;
11367
11368   /* Fail if the target section could not be assigned to an output
11369      section.  The user should fix his linker script.  */
11370   if (stub_entry->target_section != NULL
11371       && stub_entry->target_section->output_section == NULL
11372       && info->non_contiguous_regions)
11373     info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11374                               "Retry without --enable-non-contiguous-regions.\n"),
11375                             stub_entry->target_section);
11376
11377   /* Same for the group.  */
11378   if (stub_entry->group->stub_sec != NULL
11379       && stub_entry->group->stub_sec->output_section == NULL
11380       && info->non_contiguous_regions)
11381     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11382                               "output section. Retry without "
11383                               "--enable-non-contiguous-regions.\n"),
11384                             stub_entry->group->stub_sec,
11385                             stub_entry->target_section);
11386
11387   htab = ppc_hash_table (info);
11388   if (htab == NULL)
11389     return FALSE;
11390
11391   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11392   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11393
11394   htab->stub_count[stub_entry->stub_type - 1] += 1;
11395   switch (stub_entry->stub_type)
11396     {
11397     case ppc_stub_long_branch:
11398     case ppc_stub_long_branch_r2off:
11399       /* Branches are relative.  This is where we are going to.  */
11400       targ = (stub_entry->target_value
11401               + stub_entry->target_section->output_offset
11402               + stub_entry->target_section->output_section->vma);
11403       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11404
11405       /* And this is where we are coming from.  */
11406       off = (stub_entry->stub_offset
11407              + stub_entry->group->stub_sec->output_offset
11408              + stub_entry->group->stub_sec->output_section->vma);
11409       off = targ - off;
11410
11411       p = loc;
11412       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11413         {
11414           bfd_vma r2off = get_r2off (info, stub_entry);
11415
11416           if (r2off == (bfd_vma) -1)
11417             {
11418               htab->stub_error = TRUE;
11419               return FALSE;
11420             }
11421           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11422           p += 4;
11423           if (PPC_HA (r2off) != 0)
11424             {
11425               bfd_put_32 (htab->params->stub_bfd,
11426                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11427               p += 4;
11428             }
11429           if (PPC_LO (r2off) != 0)
11430             {
11431               bfd_put_32 (htab->params->stub_bfd,
11432                           ADDI_R2_R2 | PPC_LO (r2off), p);
11433               p += 4;
11434             }
11435           off -= p - loc;
11436         }
11437       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11438       p += 4;
11439
11440       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11441         {
11442           _bfd_error_handler
11443             (_("long branch stub `%s' offset overflow"),
11444              stub_entry->root.string);
11445           htab->stub_error = TRUE;
11446           return FALSE;
11447         }
11448
11449       if (info->emitrelocations)
11450         {
11451           r = get_relocs (stub_entry->group->stub_sec, 1);
11452           if (r == NULL)
11453             return FALSE;
11454           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11455           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11456           r->r_addend = targ;
11457           if (stub_entry->h != NULL
11458               && !use_global_in_relocs (htab, stub_entry, r, 1))
11459             return FALSE;
11460         }
11461       break;
11462
11463     case ppc_stub_plt_branch:
11464     case ppc_stub_plt_branch_r2off:
11465       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11466                                          stub_entry->root.string + 9,
11467                                          FALSE, FALSE);
11468       if (br_entry == NULL)
11469         {
11470           _bfd_error_handler (_("can't find branch stub `%s'"),
11471                               stub_entry->root.string);
11472           htab->stub_error = TRUE;
11473           return FALSE;
11474         }
11475
11476       targ = (stub_entry->target_value
11477               + stub_entry->target_section->output_offset
11478               + stub_entry->target_section->output_section->vma);
11479       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11480         targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11481
11482       bfd_put_64 (htab->brlt->owner, targ,
11483                   htab->brlt->contents + br_entry->offset);
11484
11485       if (br_entry->iter == htab->stub_iteration)
11486         {
11487           br_entry->iter = 0;
11488
11489           if (htab->relbrlt != NULL)
11490             {
11491               /* Create a reloc for the branch lookup table entry.  */
11492               Elf_Internal_Rela rela;
11493               bfd_byte *rl;
11494
11495               rela.r_offset = (br_entry->offset
11496                                + htab->brlt->output_offset
11497                                + htab->brlt->output_section->vma);
11498               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11499               rela.r_addend = targ;
11500
11501               rl = htab->relbrlt->contents;
11502               rl += (htab->relbrlt->reloc_count++
11503                      * sizeof (Elf64_External_Rela));
11504               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11505             }
11506           else if (info->emitrelocations)
11507             {
11508               r = get_relocs (htab->brlt, 1);
11509               if (r == NULL)
11510                 return FALSE;
11511               /* brlt, being SEC_LINKER_CREATED does not go through the
11512                  normal reloc processing.  Symbols and offsets are not
11513                  translated from input file to output file form, so
11514                  set up the offset per the output file.  */
11515               r->r_offset = (br_entry->offset
11516                              + htab->brlt->output_offset
11517                              + htab->brlt->output_section->vma);
11518               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11519               r->r_addend = targ;
11520             }
11521         }
11522
11523       targ = (br_entry->offset
11524               + htab->brlt->output_offset
11525               + htab->brlt->output_section->vma);
11526
11527       off = (elf_gp (info->output_bfd)
11528              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11529       off = targ - off;
11530
11531       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11532         {
11533           info->callbacks->einfo
11534             (_("%P: linkage table error against `%pT'\n"),
11535              stub_entry->root.string);
11536           bfd_set_error (bfd_error_bad_value);
11537           htab->stub_error = TRUE;
11538           return FALSE;
11539         }
11540
11541       if (info->emitrelocations)
11542         {
11543           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11544           if (r == NULL)
11545             return FALSE;
11546           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11547           if (bfd_big_endian (info->output_bfd))
11548             r[0].r_offset += 2;
11549           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11550             r[0].r_offset += 4;
11551           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11552           r[0].r_addend = targ;
11553           if (PPC_HA (off) != 0)
11554             {
11555               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11556               r[1].r_offset = r[0].r_offset + 4;
11557               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11558               r[1].r_addend = r[0].r_addend;
11559             }
11560         }
11561
11562       p = loc;
11563       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11564         {
11565           if (PPC_HA (off) != 0)
11566             {
11567               bfd_put_32 (htab->params->stub_bfd,
11568                           ADDIS_R12_R2 | PPC_HA (off), p);
11569               p += 4;
11570               bfd_put_32 (htab->params->stub_bfd,
11571                           LD_R12_0R12 | PPC_LO (off), p);
11572             }
11573           else
11574             bfd_put_32 (htab->params->stub_bfd,
11575                         LD_R12_0R2 | PPC_LO (off), p);
11576         }
11577       else
11578         {
11579           bfd_vma r2off = get_r2off (info, stub_entry);
11580
11581           if (r2off == (bfd_vma) -1)
11582             {
11583               htab->stub_error = TRUE;
11584               return FALSE;
11585             }
11586
11587           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11588           p += 4;
11589           if (PPC_HA (off) != 0)
11590             {
11591               bfd_put_32 (htab->params->stub_bfd,
11592                           ADDIS_R12_R2 | PPC_HA (off), p);
11593               p += 4;
11594               bfd_put_32 (htab->params->stub_bfd,
11595                           LD_R12_0R12 | PPC_LO (off), p);
11596             }
11597           else
11598             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11599
11600           if (PPC_HA (r2off) != 0)
11601             {
11602               p += 4;
11603               bfd_put_32 (htab->params->stub_bfd,
11604                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11605             }
11606           if (PPC_LO (r2off) != 0)
11607             {
11608               p += 4;
11609               bfd_put_32 (htab->params->stub_bfd,
11610                           ADDI_R2_R2 | PPC_LO (r2off), p);
11611             }
11612         }
11613       p += 4;
11614       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11615       p += 4;
11616       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11617       p += 4;
11618       break;
11619
11620     case ppc_stub_long_branch_notoc:
11621     case ppc_stub_long_branch_both:
11622     case ppc_stub_plt_branch_notoc:
11623     case ppc_stub_plt_branch_both:
11624     case ppc_stub_plt_call_notoc:
11625     case ppc_stub_plt_call_both:
11626       p = loc;
11627       off = (stub_entry->stub_offset
11628              + stub_entry->group->stub_sec->output_offset
11629              + stub_entry->group->stub_sec->output_section->vma);
11630       if (stub_entry->stub_type == ppc_stub_long_branch_both
11631           || stub_entry->stub_type == ppc_stub_plt_branch_both
11632           || stub_entry->stub_type == ppc_stub_plt_call_both)
11633         {
11634           off += 4;
11635           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11636           p += 4;
11637         }
11638       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11639         {
11640           targ = stub_entry->plt_ent->plt.offset & ~1;
11641           if (targ >= (bfd_vma) -2)
11642             abort ();
11643
11644           plt = htab->elf.splt;
11645           if (!htab->elf.dynamic_sections_created
11646               || stub_entry->h == NULL
11647               || stub_entry->h->elf.dynindx == -1)
11648             {
11649               if (stub_entry->symtype == STT_GNU_IFUNC)
11650                 plt = htab->elf.iplt;
11651               else
11652                 plt = htab->pltlocal;
11653             }
11654           targ += plt->output_offset + plt->output_section->vma;
11655         }
11656       else
11657         targ = (stub_entry->target_value
11658                 + stub_entry->target_section->output_offset
11659                 + stub_entry->target_section->output_section->vma);
11660       odd = off & 4;
11661       off = targ - off;
11662
11663       relp = p;
11664       num_rel = 0;
11665       if (htab->powerxx_stubs)
11666         {
11667           bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11668           p = build_powerxx_offset (htab->params->stub_bfd, p, off, odd, load);
11669         }
11670       else
11671         {
11672           /* The notoc stubs calculate their target (either a PLT entry or
11673              the global entry point of a function) relative to the PC
11674              returned by the "bcl" two instructions past the start of the
11675              sequence emitted by build_offset.  The offset is therefore 8
11676              less than calculated from the start of the sequence.  */
11677           off -= 8;
11678           p = build_offset (htab->params->stub_bfd, p, off,
11679                             stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11680         }
11681
11682       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11683         {
11684           bfd_vma from;
11685           num_rel = 1;
11686           from = (stub_entry->stub_offset
11687                   + stub_entry->group->stub_sec->output_offset
11688                   + stub_entry->group->stub_sec->output_section->vma
11689                   + (p - loc));
11690           bfd_put_32 (htab->params->stub_bfd,
11691                       B_DOT | ((targ - from) & 0x3fffffc), p);
11692         }
11693       else
11694         {
11695           bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11696           p += 4;
11697           bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11698         }
11699       p += 4;
11700
11701       if (info->emitrelocations)
11702         {
11703           bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11704           if (htab->powerxx_stubs)
11705             num_rel += num_relocs_for_powerxx_offset (off, odd);
11706           else
11707             {
11708               num_rel += num_relocs_for_offset (off);
11709               roff += 16;
11710             }
11711           r = get_relocs (stub_entry->group->stub_sec, num_rel);
11712           if (r == NULL)
11713             return FALSE;
11714           if (htab->powerxx_stubs)
11715             r = emit_relocs_for_powerxx_offset (info, r, roff, targ, off, odd);
11716           else
11717             r = emit_relocs_for_offset (info, r, roff, targ, off);
11718           if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11719               || stub_entry->stub_type == ppc_stub_long_branch_both)
11720             {
11721               ++r;
11722               roff = p - 4 - stub_entry->group->stub_sec->contents;
11723               r->r_offset = roff;
11724               r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11725               r->r_addend = targ;
11726               if (stub_entry->h != NULL
11727                   && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11728                 return FALSE;
11729             }
11730         }
11731
11732       if (!htab->powerxx_stubs
11733           && htab->glink_eh_frame != NULL
11734           && htab->glink_eh_frame->size != 0)
11735         {
11736           bfd_byte *base, *eh;
11737           unsigned int lr_used, delta;
11738
11739           base = (htab->glink_eh_frame->contents
11740                   + stub_entry->group->eh_base + 17);
11741           eh = base + stub_entry->group->eh_size;
11742           lr_used = stub_entry->stub_offset + 8;
11743           if (stub_entry->stub_type == ppc_stub_long_branch_both
11744               || stub_entry->stub_type == ppc_stub_plt_branch_both
11745               || stub_entry->stub_type == ppc_stub_plt_call_both)
11746             lr_used += 4;
11747           delta = lr_used - stub_entry->group->lr_restore;
11748           stub_entry->group->lr_restore = lr_used + 8;
11749           eh = eh_advance (htab->elf.dynobj, eh, delta);
11750           *eh++ = DW_CFA_register;
11751           *eh++ = 65;
11752           *eh++ = 12;
11753           *eh++ = DW_CFA_advance_loc + 2;
11754           *eh++ = DW_CFA_restore_extended;
11755           *eh++ = 65;
11756           stub_entry->group->eh_size = eh - base;
11757         }
11758       break;
11759
11760     case ppc_stub_plt_call:
11761     case ppc_stub_plt_call_r2save:
11762       if (stub_entry->h != NULL
11763           && stub_entry->h->is_func_descriptor
11764           && stub_entry->h->oh != NULL)
11765         {
11766           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11767
11768           /* If the old-ABI "dot-symbol" is undefined make it weak so
11769              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11770           if (fh->elf.root.type == bfd_link_hash_undefined
11771               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11772                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11773             fh->elf.root.type = bfd_link_hash_undefweak;
11774         }
11775
11776       /* Now build the stub.  */
11777       targ = stub_entry->plt_ent->plt.offset & ~1;
11778       if (targ >= (bfd_vma) -2)
11779         abort ();
11780
11781       plt = htab->elf.splt;
11782       if (!htab->elf.dynamic_sections_created
11783           || stub_entry->h == NULL
11784           || stub_entry->h->elf.dynindx == -1)
11785         {
11786           if (stub_entry->symtype == STT_GNU_IFUNC)
11787             plt = htab->elf.iplt;
11788           else
11789             plt = htab->pltlocal;
11790         }
11791       targ += plt->output_offset + plt->output_section->vma;
11792
11793       off = (elf_gp (info->output_bfd)
11794              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11795       off = targ - off;
11796
11797       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11798         {
11799           info->callbacks->einfo
11800             /* xgettext:c-format */
11801             (_("%P: linkage table error against `%pT'\n"),
11802              stub_entry->h != NULL
11803              ? stub_entry->h->elf.root.root.string
11804              : "<local sym>");
11805           bfd_set_error (bfd_error_bad_value);
11806           htab->stub_error = TRUE;
11807           return FALSE;
11808         }
11809
11810       r = NULL;
11811       if (info->emitrelocations)
11812         {
11813           r = get_relocs (stub_entry->group->stub_sec,
11814                           ((PPC_HA (off) != 0)
11815                            + (htab->opd_abi
11816                               ? 2 + (htab->params->plt_static_chain
11817                                      && PPC_HA (off + 16) == PPC_HA (off))
11818                               : 1)));
11819           if (r == NULL)
11820             return FALSE;
11821           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11822           if (bfd_big_endian (info->output_bfd))
11823             r[0].r_offset += 2;
11824           r[0].r_addend = targ;
11825         }
11826       if (stub_entry->h != NULL
11827           && is_tls_get_addr (&stub_entry->h->elf, htab)
11828           && htab->params->tls_get_addr_opt)
11829         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11830       else
11831         p = build_plt_stub (htab, stub_entry, loc, off, r);
11832       break;
11833
11834     case ppc_stub_save_res:
11835       return TRUE;
11836
11837     default:
11838       BFD_FAIL ();
11839       return FALSE;
11840     }
11841
11842   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11843
11844   if (htab->params->emit_stub_syms)
11845     {
11846       struct elf_link_hash_entry *h;
11847       size_t len1, len2;
11848       char *name;
11849       const char *const stub_str[] = { "long_branch",
11850                                        "long_branch",
11851                                        "long_branch",
11852                                        "long_branch",
11853                                        "plt_branch",
11854                                        "plt_branch",
11855                                        "plt_branch",
11856                                        "plt_branch",
11857                                        "plt_call",
11858                                        "plt_call",
11859                                        "plt_call",
11860                                        "plt_call" };
11861
11862       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11863       len2 = strlen (stub_entry->root.string);
11864       name = bfd_malloc (len1 + len2 + 2);
11865       if (name == NULL)
11866         return FALSE;
11867       memcpy (name, stub_entry->root.string, 9);
11868       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11869       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11870       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11871       if (h == NULL)
11872         return FALSE;
11873       if (h->root.type == bfd_link_hash_new)
11874         {
11875           h->root.type = bfd_link_hash_defined;
11876           h->root.u.def.section = stub_entry->group->stub_sec;
11877           h->root.u.def.value = stub_entry->stub_offset;
11878           h->ref_regular = 1;
11879           h->def_regular = 1;
11880           h->ref_regular_nonweak = 1;
11881           h->forced_local = 1;
11882           h->non_elf = 0;
11883           h->root.linker_def = 1;
11884         }
11885     }
11886
11887   return TRUE;
11888 }
11889
11890 /* As above, but don't actually build the stub.  Just bump offset so
11891    we know stub section sizes, and select plt_branch stubs where
11892    long_branch stubs won't do.  */
11893
11894 static bfd_boolean
11895 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11896 {
11897   struct ppc_stub_hash_entry *stub_entry;
11898   struct bfd_link_info *info;
11899   struct ppc_link_hash_table *htab;
11900   asection *plt;
11901   bfd_vma targ, off, r2off;
11902   unsigned int size, extra, lr_used, delta, odd;
11903
11904   /* Massage our args to the form they really have.  */
11905   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11906   info = in_arg;
11907
11908   htab = ppc_hash_table (info);
11909   if (htab == NULL)
11910     return FALSE;
11911
11912   /* Fail if the target section could not be assigned to an output
11913      section.  The user should fix his linker script.  */
11914   if (stub_entry->target_section != NULL
11915       && stub_entry->target_section->output_section == NULL
11916       && info->non_contiguous_regions)
11917     info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
11918                               "Retry without --enable-non-contiguous-regions.\n"),
11919                             stub_entry->target_section);
11920
11921   /* Same for the group.  */
11922   if (stub_entry->group->stub_sec != NULL
11923       && stub_entry->group->stub_sec->output_section == NULL
11924       && info->non_contiguous_regions)
11925     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11926                               "output section. Retry without "
11927                               "--enable-non-contiguous-regions.\n"),
11928                             stub_entry->group->stub_sec,
11929                             stub_entry->target_section);
11930
11931   /* Make a note of the offset within the stubs for this entry.  */
11932   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11933
11934   if (stub_entry->h != NULL
11935       && stub_entry->h->save_res
11936       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11937       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11938     {
11939       /* Don't make stubs to out-of-line register save/restore
11940          functions.  Instead, emit copies of the functions.  */
11941       stub_entry->group->needs_save_res = 1;
11942       stub_entry->stub_type = ppc_stub_save_res;
11943       return TRUE;
11944     }
11945
11946   switch (stub_entry->stub_type)
11947     {
11948     case ppc_stub_plt_branch:
11949     case ppc_stub_plt_branch_r2off:
11950       /* Reset the stub type from the plt branch variant in case we now
11951          can reach with a shorter stub.  */
11952       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11953       /* Fall through.  */
11954     case ppc_stub_long_branch:
11955     case ppc_stub_long_branch_r2off:
11956       targ = (stub_entry->target_value
11957               + stub_entry->target_section->output_offset
11958               + stub_entry->target_section->output_section->vma);
11959       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11960       off = (stub_entry->stub_offset
11961              + stub_entry->group->stub_sec->output_offset
11962              + stub_entry->group->stub_sec->output_section->vma);
11963
11964       size = 4;
11965       r2off = 0;
11966       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11967         {
11968           r2off = get_r2off (info, stub_entry);
11969           if (r2off == (bfd_vma) -1)
11970             {
11971               htab->stub_error = TRUE;
11972               return FALSE;
11973             }
11974           size = 8;
11975           if (PPC_HA (r2off) != 0)
11976             size += 4;
11977           if (PPC_LO (r2off) != 0)
11978             size += 4;
11979           off += size - 4;
11980         }
11981       off = targ - off;
11982
11983       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11984          Do the same for -R objects without function descriptors.  */
11985       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
11986            && r2off == 0
11987            && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
11988           || off + (1 << 25) >= (bfd_vma) (1 << 26))
11989         {
11990           struct ppc_branch_hash_entry *br_entry;
11991
11992           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11993                                              stub_entry->root.string + 9,
11994                                              TRUE, FALSE);
11995           if (br_entry == NULL)
11996             {
11997               _bfd_error_handler (_("can't build branch stub `%s'"),
11998                                   stub_entry->root.string);
11999               htab->stub_error = TRUE;
12000               return FALSE;
12001             }
12002
12003           if (br_entry->iter != htab->stub_iteration)
12004             {
12005               br_entry->iter = htab->stub_iteration;
12006               br_entry->offset = htab->brlt->size;
12007               htab->brlt->size += 8;
12008
12009               if (htab->relbrlt != NULL)
12010                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12011               else if (info->emitrelocations)
12012                 {
12013                   htab->brlt->reloc_count += 1;
12014                   htab->brlt->flags |= SEC_RELOC;
12015                 }
12016             }
12017
12018           targ = (br_entry->offset
12019                   + htab->brlt->output_offset
12020                   + htab->brlt->output_section->vma);
12021           off = (elf_gp (info->output_bfd)
12022                  + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12023           off = targ - off;
12024
12025           if (info->emitrelocations)
12026             {
12027               stub_entry->group->stub_sec->reloc_count
12028                 += 1 + (PPC_HA (off) != 0);
12029               stub_entry->group->stub_sec->flags |= SEC_RELOC;
12030             }
12031
12032           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12033           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12034             {
12035               size = 12;
12036               if (PPC_HA (off) != 0)
12037                 size = 16;
12038             }
12039           else
12040             {
12041               size = 16;
12042               if (PPC_HA (off) != 0)
12043                 size += 4;
12044
12045               if (PPC_HA (r2off) != 0)
12046                 size += 4;
12047               if (PPC_LO (r2off) != 0)
12048                 size += 4;
12049             }
12050         }
12051       else if (info->emitrelocations)
12052         {
12053           stub_entry->group->stub_sec->reloc_count += 1;
12054           stub_entry->group->stub_sec->flags |= SEC_RELOC;
12055         }
12056       break;
12057
12058     case ppc_stub_plt_branch_notoc:
12059     case ppc_stub_plt_branch_both:
12060       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12061       /* Fall through.  */
12062     case ppc_stub_long_branch_notoc:
12063     case ppc_stub_long_branch_both:
12064       off = (stub_entry->stub_offset
12065              + stub_entry->group->stub_sec->output_offset
12066              + stub_entry->group->stub_sec->output_section->vma);
12067       size = 0;
12068       if (stub_entry->stub_type == ppc_stub_long_branch_both)
12069         size = 4;
12070       off += size;
12071       targ = (stub_entry->target_value
12072               + stub_entry->target_section->output_offset
12073               + stub_entry->target_section->output_section->vma);
12074       odd = off & 4;
12075       off = targ - off;
12076
12077       if (info->emitrelocations)
12078         {
12079           unsigned int num_rel;
12080           if (htab->powerxx_stubs)
12081             num_rel = num_relocs_for_powerxx_offset (off, odd);
12082           else
12083             num_rel = num_relocs_for_offset (off - 8);
12084           stub_entry->group->stub_sec->reloc_count += num_rel;
12085           stub_entry->group->stub_sec->flags |= SEC_RELOC;
12086         }
12087
12088       if (htab->powerxx_stubs)
12089         extra = size_powerxx_offset (off, odd);
12090       else
12091         extra = size_offset (off - 8);
12092       /* Include branch insn plus those in the offset sequence.  */
12093       size += 4 + extra;
12094       /* The branch insn is at the end, or "extra" bytes along.  So
12095          its offset will be "extra" bytes less that that already
12096          calculated.  */
12097       off -= extra;
12098
12099       if (!htab->powerxx_stubs)
12100         {
12101           /* After the bcl, lr has been modified so we need to emit
12102              .eh_frame info saying the return address is in r12.  */
12103           lr_used = stub_entry->stub_offset + 8;
12104           if (stub_entry->stub_type == ppc_stub_long_branch_both)
12105             lr_used += 4;
12106           /* The eh_frame info will consist of a DW_CFA_advance_loc or
12107              variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12108              DW_CFA_restore_extended 65.  */
12109           delta = lr_used - stub_entry->group->lr_restore;
12110           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12111           stub_entry->group->lr_restore = lr_used + 8;
12112         }
12113
12114       /* If the branch can't reach, use a plt_branch.  */
12115       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12116         {
12117           stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12118                                     - ppc_stub_long_branch_notoc);
12119           size += 4;
12120         }
12121       else if (info->emitrelocations)
12122         stub_entry->group->stub_sec->reloc_count +=1;
12123       break;
12124
12125     case ppc_stub_plt_call_notoc:
12126     case ppc_stub_plt_call_both:
12127       off = (stub_entry->stub_offset
12128              + stub_entry->group->stub_sec->output_offset
12129              + stub_entry->group->stub_sec->output_section->vma);
12130       if (stub_entry->stub_type == ppc_stub_plt_call_both)
12131         off += 4;
12132       targ = stub_entry->plt_ent->plt.offset & ~1;
12133       if (targ >= (bfd_vma) -2)
12134         abort ();
12135
12136       plt = htab->elf.splt;
12137       if (!htab->elf.dynamic_sections_created
12138           || stub_entry->h == NULL
12139           || stub_entry->h->elf.dynindx == -1)
12140         {
12141           if (stub_entry->symtype == STT_GNU_IFUNC)
12142             plt = htab->elf.iplt;
12143           else
12144             plt = htab->pltlocal;
12145         }
12146       targ += plt->output_offset + plt->output_section->vma;
12147       odd = off & 4;
12148       off = targ - off;
12149
12150       if (htab->params->plt_stub_align != 0)
12151         {
12152           unsigned pad = plt_stub_pad (htab, stub_entry, off);
12153
12154           stub_entry->group->stub_sec->size += pad;
12155           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12156           off -= pad;
12157         }
12158
12159       if (info->emitrelocations)
12160         {
12161           unsigned int num_rel;
12162           if (htab->powerxx_stubs)
12163             num_rel = num_relocs_for_powerxx_offset (off, odd);
12164           else
12165             num_rel = num_relocs_for_offset (off - 8);
12166           stub_entry->group->stub_sec->reloc_count += num_rel;
12167           stub_entry->group->stub_sec->flags |= SEC_RELOC;
12168         }
12169
12170       size = plt_stub_size (htab, stub_entry, off);
12171
12172       if (!htab->powerxx_stubs)
12173         {
12174           /* After the bcl, lr has been modified so we need to emit
12175              .eh_frame info saying the return address is in r12.  */
12176           lr_used = stub_entry->stub_offset + 8;
12177           if (stub_entry->stub_type == ppc_stub_plt_call_both)
12178             lr_used += 4;
12179           /* The eh_frame info will consist of a DW_CFA_advance_loc or
12180              variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12181              DW_CFA_restore_extended 65.  */
12182           delta = lr_used - stub_entry->group->lr_restore;
12183           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12184           stub_entry->group->lr_restore = lr_used + 8;
12185         }
12186       break;
12187
12188     case ppc_stub_plt_call:
12189     case ppc_stub_plt_call_r2save:
12190       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12191       if (targ >= (bfd_vma) -2)
12192         abort ();
12193       plt = htab->elf.splt;
12194       if (!htab->elf.dynamic_sections_created
12195           || stub_entry->h == NULL
12196           || stub_entry->h->elf.dynindx == -1)
12197         {
12198           if (stub_entry->symtype == STT_GNU_IFUNC)
12199             plt = htab->elf.iplt;
12200           else
12201             plt = htab->pltlocal;
12202         }
12203       targ += plt->output_offset + plt->output_section->vma;
12204
12205       off = (elf_gp (info->output_bfd)
12206              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12207       off = targ - off;
12208
12209       if (htab->params->plt_stub_align != 0)
12210         {
12211           unsigned pad = plt_stub_pad (htab, stub_entry, off);
12212
12213           stub_entry->group->stub_sec->size += pad;
12214           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12215         }
12216
12217       if (info->emitrelocations)
12218         {
12219           stub_entry->group->stub_sec->reloc_count
12220             += ((PPC_HA (off) != 0)
12221                 + (htab->opd_abi
12222                    ? 2 + (htab->params->plt_static_chain
12223                           && PPC_HA (off + 16) == PPC_HA (off))
12224                    : 1));
12225           stub_entry->group->stub_sec->flags |= SEC_RELOC;
12226         }
12227
12228       size = plt_stub_size (htab, stub_entry, off);
12229
12230       if (stub_entry->h != NULL
12231           && is_tls_get_addr (&stub_entry->h->elf, htab)
12232           && htab->params->tls_get_addr_opt
12233           && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12234         {
12235           if (htab->params->no_tls_get_addr_regsave)
12236             {
12237               lr_used = stub_entry->stub_offset + size - 20;
12238               /* The eh_frame info will consist of a DW_CFA_advance_loc
12239                  or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12240                  DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
12241               delta = lr_used - stub_entry->group->lr_restore;
12242               stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12243             }
12244           else
12245             {
12246               /* Adjustments to r1 need to be described.  */
12247               unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12248               delta = cfa_updt - stub_entry->group->lr_restore;
12249               stub_entry->group->eh_size += eh_advance_size (delta);
12250               stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12251             }
12252           stub_entry->group->lr_restore = size - 4;
12253         }
12254       break;
12255
12256     default:
12257       BFD_FAIL ();
12258       return FALSE;
12259     }
12260
12261   stub_entry->group->stub_sec->size += size;
12262   return TRUE;
12263 }
12264
12265 /* Set up various things so that we can make a list of input sections
12266    for each output section included in the link.  Returns -1 on error,
12267    0 when no stubs will be needed, and 1 on success.  */
12268
12269 int
12270 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12271 {
12272   unsigned int id;
12273   size_t amt;
12274   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12275
12276   if (htab == NULL)
12277     return -1;
12278
12279   htab->sec_info_arr_size = _bfd_section_id;
12280   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12281   htab->sec_info = bfd_zmalloc (amt);
12282   if (htab->sec_info == NULL)
12283     return -1;
12284
12285   /* Set toc_off for com, und, abs and ind sections.  */
12286   for (id = 0; id < 3; id++)
12287     htab->sec_info[id].toc_off = TOC_BASE_OFF;
12288
12289   return 1;
12290 }
12291
12292 /* Set up for first pass at multitoc partitioning.  */
12293
12294 void
12295 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12296 {
12297   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12298
12299   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12300   htab->toc_bfd = NULL;
12301   htab->toc_first_sec = NULL;
12302 }
12303
12304 /* The linker repeatedly calls this function for each TOC input section
12305    and linker generated GOT section.  Group input bfds such that the toc
12306    within a group is less than 64k in size.  */
12307
12308 bfd_boolean
12309 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12310 {
12311   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12312   bfd_vma addr, off, limit;
12313
12314   if (htab == NULL)
12315     return FALSE;
12316
12317   if (!htab->second_toc_pass)
12318     {
12319       /* Keep track of the first .toc or .got section for this input bfd.  */
12320       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12321
12322       if (new_bfd)
12323         {
12324           htab->toc_bfd = isec->owner;
12325           htab->toc_first_sec = isec;
12326         }
12327
12328       addr = isec->output_offset + isec->output_section->vma;
12329       off = addr - htab->toc_curr;
12330       limit = 0x80008000;
12331       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12332         limit = 0x10000;
12333       if (off + isec->size > limit)
12334         {
12335           addr = (htab->toc_first_sec->output_offset
12336                   + htab->toc_first_sec->output_section->vma);
12337           htab->toc_curr = addr;
12338           htab->toc_curr &= -TOC_BASE_ALIGN;
12339         }
12340
12341       /* toc_curr is the base address of this toc group.  Set elf_gp
12342          for the input section to be the offset relative to the
12343          output toc base plus 0x8000.  Making the input elf_gp an
12344          offset allows us to move the toc as a whole without
12345          recalculating input elf_gp.  */
12346       off = htab->toc_curr - elf_gp (info->output_bfd);
12347       off += TOC_BASE_OFF;
12348
12349       /* Die if someone uses a linker script that doesn't keep input
12350          file .toc and .got together.  */
12351       if (new_bfd
12352           && elf_gp (isec->owner) != 0
12353           && elf_gp (isec->owner) != off)
12354         return FALSE;
12355
12356       elf_gp (isec->owner) = off;
12357       return TRUE;
12358     }
12359
12360   /* During the second pass toc_first_sec points to the start of
12361      a toc group, and toc_curr is used to track the old elf_gp.
12362      We use toc_bfd to ensure we only look at each bfd once.  */
12363   if (htab->toc_bfd == isec->owner)
12364     return TRUE;
12365   htab->toc_bfd = isec->owner;
12366
12367   if (htab->toc_first_sec == NULL
12368       || htab->toc_curr != elf_gp (isec->owner))
12369     {
12370       htab->toc_curr = elf_gp (isec->owner);
12371       htab->toc_first_sec = isec;
12372     }
12373   addr = (htab->toc_first_sec->output_offset
12374           + htab->toc_first_sec->output_section->vma);
12375   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12376   elf_gp (isec->owner) = off;
12377
12378   return TRUE;
12379 }
12380
12381 /* Called via elf_link_hash_traverse to merge GOT entries for global
12382    symbol H.  */
12383
12384 static bfd_boolean
12385 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12386 {
12387   if (h->root.type == bfd_link_hash_indirect)
12388     return TRUE;
12389
12390   merge_got_entries (&h->got.glist);
12391
12392   return TRUE;
12393 }
12394
12395 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12396    symbol H.  */
12397
12398 static bfd_boolean
12399 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12400 {
12401   struct got_entry *gent;
12402
12403   if (h->root.type == bfd_link_hash_indirect)
12404     return TRUE;
12405
12406   for (gent = h->got.glist; gent != NULL; gent = gent->next)
12407     if (!gent->is_indirect)
12408       allocate_got (h, (struct bfd_link_info *) inf, gent);
12409   return TRUE;
12410 }
12411
12412 /* Called on the first multitoc pass after the last call to
12413    ppc64_elf_next_toc_section.  This function removes duplicate GOT
12414    entries.  */
12415
12416 bfd_boolean
12417 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12418 {
12419   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12420   struct bfd *ibfd, *ibfd2;
12421   bfd_boolean done_something;
12422
12423   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12424
12425   if (!htab->do_multi_toc)
12426     return FALSE;
12427
12428   /* Merge global sym got entries within a toc group.  */
12429   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12430
12431   /* And tlsld_got.  */
12432   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12433     {
12434       struct got_entry *ent, *ent2;
12435
12436       if (!is_ppc64_elf (ibfd))
12437         continue;
12438
12439       ent = ppc64_tlsld_got (ibfd);
12440       if (!ent->is_indirect
12441           && ent->got.offset != (bfd_vma) -1)
12442         {
12443           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12444             {
12445               if (!is_ppc64_elf (ibfd2))
12446                 continue;
12447
12448               ent2 = ppc64_tlsld_got (ibfd2);
12449               if (!ent2->is_indirect
12450                   && ent2->got.offset != (bfd_vma) -1
12451                   && elf_gp (ibfd2) == elf_gp (ibfd))
12452                 {
12453                   ent2->is_indirect = TRUE;
12454                   ent2->got.ent = ent;
12455                 }
12456             }
12457         }
12458     }
12459
12460   /* Zap sizes of got sections.  */
12461   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12462   htab->elf.irelplt->size -= htab->got_reli_size;
12463   htab->got_reli_size = 0;
12464
12465   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12466     {
12467       asection *got, *relgot;
12468
12469       if (!is_ppc64_elf (ibfd))
12470         continue;
12471
12472       got = ppc64_elf_tdata (ibfd)->got;
12473       if (got != NULL)
12474         {
12475           got->rawsize = got->size;
12476           got->size = 0;
12477           relgot = ppc64_elf_tdata (ibfd)->relgot;
12478           relgot->rawsize = relgot->size;
12479           relgot->size = 0;
12480         }
12481     }
12482
12483   /* Now reallocate the got, local syms first.  We don't need to
12484      allocate section contents again since we never increase size.  */
12485   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12486     {
12487       struct got_entry **lgot_ents;
12488       struct got_entry **end_lgot_ents;
12489       struct plt_entry **local_plt;
12490       struct plt_entry **end_local_plt;
12491       unsigned char *lgot_masks;
12492       bfd_size_type locsymcount;
12493       Elf_Internal_Shdr *symtab_hdr;
12494       asection *s;
12495
12496       if (!is_ppc64_elf (ibfd))
12497         continue;
12498
12499       lgot_ents = elf_local_got_ents (ibfd);
12500       if (!lgot_ents)
12501         continue;
12502
12503       symtab_hdr = &elf_symtab_hdr (ibfd);
12504       locsymcount = symtab_hdr->sh_info;
12505       end_lgot_ents = lgot_ents + locsymcount;
12506       local_plt = (struct plt_entry **) end_lgot_ents;
12507       end_local_plt = local_plt + locsymcount;
12508       lgot_masks = (unsigned char *) end_local_plt;
12509       s = ppc64_elf_tdata (ibfd)->got;
12510       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12511         {
12512           struct got_entry *ent;
12513
12514           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12515             {
12516               unsigned int ent_size = 8;
12517               unsigned int rel_size = sizeof (Elf64_External_Rela);
12518
12519               ent->got.offset = s->size;
12520               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12521                 {
12522                   ent_size *= 2;
12523                   rel_size *= 2;
12524                 }
12525               s->size += ent_size;
12526               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12527                 {
12528                   htab->elf.irelplt->size += rel_size;
12529                   htab->got_reli_size += rel_size;
12530                 }
12531               else if (bfd_link_pic (info)
12532                        && !(ent->tls_type != 0
12533                             && bfd_link_executable (info)))
12534                 {
12535                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12536                   srel->size += rel_size;
12537                 }
12538             }
12539         }
12540     }
12541
12542   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12543
12544   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12545     {
12546       struct got_entry *ent;
12547
12548       if (!is_ppc64_elf (ibfd))
12549         continue;
12550
12551       ent = ppc64_tlsld_got (ibfd);
12552       if (!ent->is_indirect
12553           && ent->got.offset != (bfd_vma) -1)
12554         {
12555           asection *s = ppc64_elf_tdata (ibfd)->got;
12556           ent->got.offset = s->size;
12557           s->size += 16;
12558           if (bfd_link_dll (info))
12559             {
12560               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12561               srel->size += sizeof (Elf64_External_Rela);
12562             }
12563         }
12564     }
12565
12566   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12567   if (!done_something)
12568     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12569       {
12570         asection *got;
12571
12572         if (!is_ppc64_elf (ibfd))
12573           continue;
12574
12575         got = ppc64_elf_tdata (ibfd)->got;
12576         if (got != NULL)
12577           {
12578             done_something = got->rawsize != got->size;
12579             if (done_something)
12580               break;
12581           }
12582       }
12583
12584   if (done_something)
12585     (*htab->params->layout_sections_again) ();
12586
12587   /* Set up for second pass over toc sections to recalculate elf_gp
12588      on input sections.  */
12589   htab->toc_bfd = NULL;
12590   htab->toc_first_sec = NULL;
12591   htab->second_toc_pass = TRUE;
12592   return done_something;
12593 }
12594
12595 /* Called after second pass of multitoc partitioning.  */
12596
12597 void
12598 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12599 {
12600   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12601
12602   /* After the second pass, toc_curr tracks the TOC offset used
12603      for code sections below in ppc64_elf_next_input_section.  */
12604   htab->toc_curr = TOC_BASE_OFF;
12605 }
12606
12607 /* No toc references were found in ISEC.  If the code in ISEC makes no
12608    calls, then there's no need to use toc adjusting stubs when branching
12609    into ISEC.  Actually, indirect calls from ISEC are OK as they will
12610    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
12611    needed, and 2 if a cyclical call-graph was found but no other reason
12612    for a stub was detected.  If called from the top level, a return of
12613    2 means the same as a return of 0.  */
12614
12615 static int
12616 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12617 {
12618   int ret;
12619
12620   /* Mark this section as checked.  */
12621   isec->call_check_done = 1;
12622
12623   /* We know none of our code bearing sections will need toc stubs.  */
12624   if ((isec->flags & SEC_LINKER_CREATED) != 0)
12625     return 0;
12626
12627   if (isec->size == 0)
12628     return 0;
12629
12630   if (isec->output_section == NULL)
12631     return 0;
12632
12633   ret = 0;
12634   if (isec->reloc_count != 0)
12635     {
12636       Elf_Internal_Rela *relstart, *rel;
12637       Elf_Internal_Sym *local_syms;
12638       struct ppc_link_hash_table *htab;
12639
12640       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12641                                             info->keep_memory);
12642       if (relstart == NULL)
12643         return -1;
12644
12645       /* Look for branches to outside of this section.  */
12646       local_syms = NULL;
12647       htab = ppc_hash_table (info);
12648       if (htab == NULL)
12649         return -1;
12650
12651       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12652         {
12653           enum elf_ppc64_reloc_type r_type;
12654           unsigned long r_symndx;
12655           struct elf_link_hash_entry *h;
12656           struct ppc_link_hash_entry *eh;
12657           Elf_Internal_Sym *sym;
12658           asection *sym_sec;
12659           struct _opd_sec_data *opd;
12660           bfd_vma sym_value;
12661           bfd_vma dest;
12662
12663           r_type = ELF64_R_TYPE (rel->r_info);
12664           if (r_type != R_PPC64_REL24
12665               && r_type != R_PPC64_REL24_NOTOC
12666               && r_type != R_PPC64_REL14
12667               && r_type != R_PPC64_REL14_BRTAKEN
12668               && r_type != R_PPC64_REL14_BRNTAKEN
12669               && r_type != R_PPC64_PLTCALL
12670               && r_type != R_PPC64_PLTCALL_NOTOC)
12671             continue;
12672
12673           r_symndx = ELF64_R_SYM (rel->r_info);
12674           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12675                           isec->owner))
12676             {
12677               ret = -1;
12678               break;
12679             }
12680
12681           /* Calls to dynamic lib functions go through a plt call stub
12682              that uses r2.  */
12683           eh = ppc_elf_hash_entry (h);
12684           if (eh != NULL
12685               && (eh->elf.plt.plist != NULL
12686                   || (eh->oh != NULL
12687                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12688             {
12689               ret = 1;
12690               break;
12691             }
12692
12693           if (sym_sec == NULL)
12694             /* Ignore other undefined symbols.  */
12695             continue;
12696
12697           /* Assume branches to other sections not included in the
12698              link need stubs too, to cover -R and absolute syms.  */
12699           if (sym_sec->output_section == NULL)
12700             {
12701               ret = 1;
12702               break;
12703             }
12704
12705           if (h == NULL)
12706             sym_value = sym->st_value;
12707           else
12708             {
12709               if (h->root.type != bfd_link_hash_defined
12710                   && h->root.type != bfd_link_hash_defweak)
12711                 abort ();
12712               sym_value = h->root.u.def.value;
12713             }
12714           sym_value += rel->r_addend;
12715
12716           /* If this branch reloc uses an opd sym, find the code section.  */
12717           opd = get_opd_info (sym_sec);
12718           if (opd != NULL)
12719             {
12720               if (h == NULL && opd->adjust != NULL)
12721                 {
12722                   long adjust;
12723
12724                   adjust = opd->adjust[OPD_NDX (sym_value)];
12725                   if (adjust == -1)
12726                     /* Assume deleted functions won't ever be called.  */
12727                     continue;
12728                   sym_value += adjust;
12729                 }
12730
12731               dest = opd_entry_value (sym_sec, sym_value,
12732                                       &sym_sec, NULL, FALSE);
12733               if (dest == (bfd_vma) -1)
12734                 continue;
12735             }
12736           else
12737             dest = (sym_value
12738                     + sym_sec->output_offset
12739                     + sym_sec->output_section->vma);
12740
12741           /* Ignore branch to self.  */
12742           if (sym_sec == isec)
12743             continue;
12744
12745           /* If the called function uses the toc, we need a stub.  */
12746           if (sym_sec->has_toc_reloc
12747               || sym_sec->makes_toc_func_call)
12748             {
12749               ret = 1;
12750               break;
12751             }
12752
12753           /* Assume any branch that needs a long branch stub might in fact
12754              need a plt_branch stub.  A plt_branch stub uses r2.  */
12755           else if (dest - (isec->output_offset
12756                            + isec->output_section->vma
12757                            + rel->r_offset) + (1 << 25)
12758                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12759                                                              ? h->other
12760                                                              : sym->st_other))
12761             {
12762               ret = 1;
12763               break;
12764             }
12765
12766           /* If calling back to a section in the process of being
12767              tested, we can't say for sure that no toc adjusting stubs
12768              are needed, so don't return zero.  */
12769           else if (sym_sec->call_check_in_progress)
12770             ret = 2;
12771
12772           /* Branches to another section that itself doesn't have any TOC
12773              references are OK.  Recursively call ourselves to check.  */
12774           else if (!sym_sec->call_check_done)
12775             {
12776               int recur;
12777
12778               /* Mark current section as indeterminate, so that other
12779                  sections that call back to current won't be marked as
12780                  known.  */
12781               isec->call_check_in_progress = 1;
12782               recur = toc_adjusting_stub_needed (info, sym_sec);
12783               isec->call_check_in_progress = 0;
12784
12785               if (recur != 0)
12786                 {
12787                   ret = recur;
12788                   if (recur != 2)
12789                     break;
12790                 }
12791             }
12792         }
12793
12794       if (local_syms != NULL
12795           && (elf_symtab_hdr (isec->owner).contents
12796               != (unsigned char *) local_syms))
12797         free (local_syms);
12798       if (elf_section_data (isec)->relocs != relstart)
12799         free (relstart);
12800     }
12801
12802   if ((ret & 1) == 0
12803       && isec->map_head.s != NULL
12804       && (strcmp (isec->output_section->name, ".init") == 0
12805           || strcmp (isec->output_section->name, ".fini") == 0))
12806     {
12807       if (isec->map_head.s->has_toc_reloc
12808           || isec->map_head.s->makes_toc_func_call)
12809         ret = 1;
12810       else if (!isec->map_head.s->call_check_done)
12811         {
12812           int recur;
12813           isec->call_check_in_progress = 1;
12814           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12815           isec->call_check_in_progress = 0;
12816           if (recur != 0)
12817             ret = recur;
12818         }
12819     }
12820
12821   if (ret == 1)
12822     isec->makes_toc_func_call = 1;
12823
12824   return ret;
12825 }
12826
12827 /* The linker repeatedly calls this function for each input section,
12828    in the order that input sections are linked into output sections.
12829    Build lists of input sections to determine groupings between which
12830    we may insert linker stubs.  */
12831
12832 bfd_boolean
12833 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12834 {
12835   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12836
12837   if (htab == NULL)
12838     return FALSE;
12839
12840   if ((isec->output_section->flags & SEC_CODE) != 0
12841       && isec->output_section->id < htab->sec_info_arr_size)
12842     {
12843       /* This happens to make the list in reverse order,
12844          which is what we want.  */
12845       htab->sec_info[isec->id].u.list
12846         = htab->sec_info[isec->output_section->id].u.list;
12847       htab->sec_info[isec->output_section->id].u.list = isec;
12848     }
12849
12850   if (htab->multi_toc_needed)
12851     {
12852       /* Analyse sections that aren't already flagged as needing a
12853          valid toc pointer.  Exclude .fixup for the linux kernel.
12854          .fixup contains branches, but only back to the function that
12855          hit an exception.  */
12856       if (!(isec->has_toc_reloc
12857             || (isec->flags & SEC_CODE) == 0
12858             || strcmp (isec->name, ".fixup") == 0
12859             || isec->call_check_done))
12860         {
12861           if (toc_adjusting_stub_needed (info, isec) < 0)
12862             return FALSE;
12863         }
12864       /* Make all sections use the TOC assigned for this object file.
12865          This will be wrong for pasted sections;  We fix that in
12866          check_pasted_section().  */
12867       if (elf_gp (isec->owner) != 0)
12868         htab->toc_curr = elf_gp (isec->owner);
12869     }
12870
12871   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12872   return TRUE;
12873 }
12874
12875 /* Check that all .init and .fini sections use the same toc, if they
12876    have toc relocs.  */
12877
12878 static bfd_boolean
12879 check_pasted_section (struct bfd_link_info *info, const char *name)
12880 {
12881   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12882
12883   if (o != NULL)
12884     {
12885       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12886       bfd_vma toc_off = 0;
12887       asection *i;
12888
12889       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12890         if (i->has_toc_reloc)
12891           {
12892             if (toc_off == 0)
12893               toc_off = htab->sec_info[i->id].toc_off;
12894             else if (toc_off != htab->sec_info[i->id].toc_off)
12895               return FALSE;
12896           }
12897
12898       if (toc_off == 0)
12899         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12900           if (i->makes_toc_func_call)
12901             {
12902               toc_off = htab->sec_info[i->id].toc_off;
12903               break;
12904             }
12905
12906       /* Make sure the whole pasted function uses the same toc offset.  */
12907       if (toc_off != 0)
12908         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12909           htab->sec_info[i->id].toc_off = toc_off;
12910     }
12911   return TRUE;
12912 }
12913
12914 bfd_boolean
12915 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12916 {
12917   return (check_pasted_section (info, ".init")
12918           & check_pasted_section (info, ".fini"));
12919 }
12920
12921 /* See whether we can group stub sections together.  Grouping stub
12922    sections may result in fewer stubs.  More importantly, we need to
12923    put all .init* and .fini* stubs at the beginning of the .init or
12924    .fini output sections respectively, because glibc splits the
12925    _init and _fini functions into multiple parts.  Putting a stub in
12926    the middle of a function is not a good idea.  */
12927
12928 static bfd_boolean
12929 group_sections (struct bfd_link_info *info,
12930                 bfd_size_type stub_group_size,
12931                 bfd_boolean stubs_always_before_branch)
12932 {
12933   struct ppc_link_hash_table *htab;
12934   asection *osec;
12935   bfd_boolean suppress_size_errors;
12936
12937   htab = ppc_hash_table (info);
12938   if (htab == NULL)
12939     return FALSE;
12940
12941   suppress_size_errors = FALSE;
12942   if (stub_group_size == 1)
12943     {
12944       /* Default values.  */
12945       if (stubs_always_before_branch)
12946         stub_group_size = 0x1e00000;
12947       else
12948         stub_group_size = 0x1c00000;
12949       suppress_size_errors = TRUE;
12950     }
12951
12952   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12953     {
12954       asection *tail;
12955
12956       if (osec->id >= htab->sec_info_arr_size)
12957         continue;
12958
12959       tail = htab->sec_info[osec->id].u.list;
12960       while (tail != NULL)
12961         {
12962           asection *curr;
12963           asection *prev;
12964           bfd_size_type total;
12965           bfd_boolean big_sec;
12966           bfd_vma curr_toc;
12967           struct map_stub *group;
12968           bfd_size_type group_size;
12969
12970           curr = tail;
12971           total = tail->size;
12972           group_size = (ppc64_elf_section_data (tail) != NULL
12973                         && ppc64_elf_section_data (tail)->has_14bit_branch
12974                         ? stub_group_size >> 10 : stub_group_size);
12975
12976           big_sec = total > group_size;
12977           if (big_sec && !suppress_size_errors)
12978             /* xgettext:c-format */
12979             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12980                                 tail->owner, tail);
12981           curr_toc = htab->sec_info[tail->id].toc_off;
12982
12983           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12984                  && ((total += curr->output_offset - prev->output_offset)
12985                      < (ppc64_elf_section_data (prev) != NULL
12986                         && ppc64_elf_section_data (prev)->has_14bit_branch
12987                         ? (group_size = stub_group_size >> 10) : group_size))
12988                  && htab->sec_info[prev->id].toc_off == curr_toc)
12989             curr = prev;
12990
12991           /* OK, the size from the start of CURR to the end is less
12992              than group_size and thus can be handled by one stub
12993              section.  (or the tail section is itself larger than
12994              group_size, in which case we may be toast.)  We should
12995              really be keeping track of the total size of stubs added
12996              here, as stubs contribute to the final output section
12997              size.  That's a little tricky, and this way will only
12998              break if stubs added make the total size more than 2^25,
12999              ie. for the default stub_group_size, if stubs total more
13000              than 2097152 bytes, or nearly 75000 plt call stubs.  */
13001           group = bfd_alloc (curr->owner, sizeof (*group));
13002           if (group == NULL)
13003             return FALSE;
13004           group->link_sec = curr;
13005           group->stub_sec = NULL;
13006           group->needs_save_res = 0;
13007           group->lr_restore = 0;
13008           group->eh_size = 0;
13009           group->eh_base = 0;
13010           group->next = htab->group;
13011           htab->group = group;
13012           do
13013             {
13014               prev = htab->sec_info[tail->id].u.list;
13015               /* Set up this stub group.  */
13016               htab->sec_info[tail->id].u.group = group;
13017             }
13018           while (tail != curr && (tail = prev) != NULL);
13019
13020           /* But wait, there's more!  Input sections up to group_size
13021              bytes before the stub section can be handled by it too.
13022              Don't do this if we have a really large section after the
13023              stubs, as adding more stubs increases the chance that
13024              branches may not reach into the stub section.  */
13025           if (!stubs_always_before_branch && !big_sec)
13026             {
13027               total = 0;
13028               while (prev != NULL
13029                      && ((total += tail->output_offset - prev->output_offset)
13030                          < (ppc64_elf_section_data (prev) != NULL
13031                             && ppc64_elf_section_data (prev)->has_14bit_branch
13032                             ? (group_size = stub_group_size >> 10)
13033                             : group_size))
13034                      && htab->sec_info[prev->id].toc_off == curr_toc)
13035                 {
13036                   tail = prev;
13037                   prev = htab->sec_info[tail->id].u.list;
13038                   htab->sec_info[tail->id].u.group = group;
13039                 }
13040             }
13041           tail = prev;
13042         }
13043     }
13044   return TRUE;
13045 }
13046
13047 static const unsigned char glink_eh_frame_cie[] =
13048 {
13049   0, 0, 0, 16,                          /* length.  */
13050   0, 0, 0, 0,                           /* id.  */
13051   1,                                    /* CIE version.  */
13052   'z', 'R', 0,                          /* Augmentation string.  */
13053   4,                                    /* Code alignment.  */
13054   0x78,                                 /* Data alignment.  */
13055   65,                                   /* RA reg.  */
13056   1,                                    /* Augmentation size.  */
13057   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
13058   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
13059 };
13060
13061 /* Stripping output sections is normally done before dynamic section
13062    symbols have been allocated.  This function is called later, and
13063    handles cases like htab->brlt which is mapped to its own output
13064    section.  */
13065
13066 static void
13067 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13068 {
13069   if (isec->size == 0
13070       && isec->output_section->size == 0
13071       && !(isec->output_section->flags & SEC_KEEP)
13072       && !bfd_section_removed_from_list (info->output_bfd,
13073                                          isec->output_section)
13074       && elf_section_data (isec->output_section)->dynindx == 0)
13075     {
13076       isec->output_section->flags |= SEC_EXCLUDE;
13077       bfd_section_list_remove (info->output_bfd, isec->output_section);
13078       info->output_bfd->section_count--;
13079     }
13080 }
13081
13082 /* Determine and set the size of the stub section for a final link.
13083
13084    The basic idea here is to examine all the relocations looking for
13085    PC-relative calls to a target that is unreachable with a "bl"
13086    instruction.  */
13087
13088 bfd_boolean
13089 ppc64_elf_size_stubs (struct bfd_link_info *info)
13090 {
13091   bfd_size_type stub_group_size;
13092   bfd_boolean stubs_always_before_branch;
13093   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13094
13095   if (htab == NULL)
13096     return FALSE;
13097
13098   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13099     htab->params->plt_thread_safe = 1;
13100   if (!htab->opd_abi)
13101     htab->params->plt_thread_safe = 0;
13102   else if (htab->params->plt_thread_safe == -1)
13103     {
13104       static const char *const thread_starter[] =
13105         {
13106           "pthread_create",
13107           /* libstdc++ */
13108           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13109           /* librt */
13110           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13111           "mq_notify", "create_timer",
13112           /* libanl */
13113           "getaddrinfo_a",
13114           /* libgomp */
13115           "GOMP_parallel",
13116           "GOMP_parallel_start",
13117           "GOMP_parallel_loop_static",
13118           "GOMP_parallel_loop_static_start",
13119           "GOMP_parallel_loop_dynamic",
13120           "GOMP_parallel_loop_dynamic_start",
13121           "GOMP_parallel_loop_guided",
13122           "GOMP_parallel_loop_guided_start",
13123           "GOMP_parallel_loop_runtime",
13124           "GOMP_parallel_loop_runtime_start",
13125           "GOMP_parallel_sections",
13126           "GOMP_parallel_sections_start",
13127           /* libgo */
13128           "__go_go",
13129         };
13130       unsigned i;
13131
13132       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13133         {
13134           struct elf_link_hash_entry *h;
13135           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13136                                     FALSE, FALSE, TRUE);
13137           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13138           if (htab->params->plt_thread_safe)
13139             break;
13140         }
13141     }
13142   stubs_always_before_branch = htab->params->group_size < 0;
13143   if (htab->params->group_size < 0)
13144     stub_group_size = -htab->params->group_size;
13145   else
13146     stub_group_size = htab->params->group_size;
13147
13148   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13149     return FALSE;
13150
13151   htab->tga_group = NULL;
13152   if (!htab->params->no_tls_get_addr_regsave
13153       && htab->tga_desc_fd != NULL
13154       && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13155           || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13156       && htab->tls_get_addr_fd != NULL
13157       && is_static_defined (&htab->tls_get_addr_fd->elf))
13158     {
13159       asection *sym_sec, *code_sec, *stub_sec;
13160       bfd_vma sym_value;
13161       struct _opd_sec_data *opd;
13162
13163       sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13164       sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13165       code_sec = sym_sec;
13166       opd = get_opd_info (sym_sec);
13167       if (opd != NULL)
13168         opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
13169       htab->tga_group = htab->sec_info[code_sec->id].u.group;
13170       stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13171                                                     htab->tga_group->link_sec);
13172       if (stub_sec == NULL)
13173         return FALSE;
13174       htab->tga_group->stub_sec = stub_sec;
13175
13176       htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13177       htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13178       htab->tga_desc_fd->elf.root.u.def.value = 0;
13179       htab->tga_desc_fd->elf.type = STT_FUNC;
13180       htab->tga_desc_fd->elf.def_regular = 1;
13181       htab->tga_desc_fd->elf.non_elf = 0;
13182       _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
13183     }
13184
13185 #define STUB_SHRINK_ITER 20
13186   /* Loop until no stubs added.  After iteration 20 of this loop we may
13187      exit on a stub section shrinking.  This is to break out of a
13188      pathological case where adding stubs on one iteration decreases
13189      section gaps (perhaps due to alignment), which then requires
13190      fewer or smaller stubs on the next iteration.  */
13191
13192   while (1)
13193     {
13194       bfd *input_bfd;
13195       unsigned int bfd_indx;
13196       struct map_stub *group;
13197
13198       htab->stub_iteration += 1;
13199
13200       for (input_bfd = info->input_bfds, bfd_indx = 0;
13201            input_bfd != NULL;
13202            input_bfd = input_bfd->link.next, bfd_indx++)
13203         {
13204           Elf_Internal_Shdr *symtab_hdr;
13205           asection *section;
13206           Elf_Internal_Sym *local_syms = NULL;
13207
13208           if (!is_ppc64_elf (input_bfd))
13209             continue;
13210
13211           /* We'll need the symbol table in a second.  */
13212           symtab_hdr = &elf_symtab_hdr (input_bfd);
13213           if (symtab_hdr->sh_info == 0)
13214             continue;
13215
13216           /* Walk over each section attached to the input bfd.  */
13217           for (section = input_bfd->sections;
13218                section != NULL;
13219                section = section->next)
13220             {
13221               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13222
13223               /* If there aren't any relocs, then there's nothing more
13224                  to do.  */
13225               if ((section->flags & SEC_RELOC) == 0
13226                   || (section->flags & SEC_ALLOC) == 0
13227                   || (section->flags & SEC_LOAD) == 0
13228                   || (section->flags & SEC_CODE) == 0
13229                   || section->reloc_count == 0)
13230                 continue;
13231
13232               /* If this section is a link-once section that will be
13233                  discarded, then don't create any stubs.  */
13234               if (section->output_section == NULL
13235                   || section->output_section->owner != info->output_bfd)
13236                 continue;
13237
13238               /* Get the relocs.  */
13239               internal_relocs
13240                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13241                                              info->keep_memory);
13242               if (internal_relocs == NULL)
13243                 goto error_ret_free_local;
13244
13245               /* Now examine each relocation.  */
13246               irela = internal_relocs;
13247               irelaend = irela + section->reloc_count;
13248               for (; irela < irelaend; irela++)
13249                 {
13250                   enum elf_ppc64_reloc_type r_type;
13251                   unsigned int r_indx;
13252                   enum ppc_stub_type stub_type;
13253                   struct ppc_stub_hash_entry *stub_entry;
13254                   asection *sym_sec, *code_sec;
13255                   bfd_vma sym_value, code_value;
13256                   bfd_vma destination;
13257                   unsigned long local_off;
13258                   bfd_boolean ok_dest;
13259                   struct ppc_link_hash_entry *hash;
13260                   struct ppc_link_hash_entry *fdh;
13261                   struct elf_link_hash_entry *h;
13262                   Elf_Internal_Sym *sym;
13263                   char *stub_name;
13264                   const asection *id_sec;
13265                   struct _opd_sec_data *opd;
13266                   struct plt_entry *plt_ent;
13267
13268                   r_type = ELF64_R_TYPE (irela->r_info);
13269                   r_indx = ELF64_R_SYM (irela->r_info);
13270
13271                   if (r_type >= R_PPC64_max)
13272                     {
13273                       bfd_set_error (bfd_error_bad_value);
13274                       goto error_ret_free_internal;
13275                     }
13276
13277                   /* Only look for stubs on branch instructions.  */
13278                   if (r_type != R_PPC64_REL24
13279                       && r_type != R_PPC64_REL24_NOTOC
13280                       && r_type != R_PPC64_REL14
13281                       && r_type != R_PPC64_REL14_BRTAKEN
13282                       && r_type != R_PPC64_REL14_BRNTAKEN)
13283                     continue;
13284
13285                   /* Now determine the call target, its name, value,
13286                      section.  */
13287                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13288                                   r_indx, input_bfd))
13289                     goto error_ret_free_internal;
13290                   hash = ppc_elf_hash_entry (h);
13291
13292                   ok_dest = FALSE;
13293                   fdh = NULL;
13294                   sym_value = 0;
13295                   if (hash == NULL)
13296                     {
13297                       sym_value = sym->st_value;
13298                       if (sym_sec != NULL
13299                           && sym_sec->output_section != NULL)
13300                         ok_dest = TRUE;
13301                     }
13302                   else if (hash->elf.root.type == bfd_link_hash_defined
13303                            || hash->elf.root.type == bfd_link_hash_defweak)
13304                     {
13305                       sym_value = hash->elf.root.u.def.value;
13306                       if (sym_sec->output_section != NULL)
13307                         ok_dest = TRUE;
13308                     }
13309                   else if (hash->elf.root.type == bfd_link_hash_undefweak
13310                            || hash->elf.root.type == bfd_link_hash_undefined)
13311                     {
13312                       /* Recognise an old ABI func code entry sym, and
13313                          use the func descriptor sym instead if it is
13314                          defined.  */
13315                       if (hash->elf.root.root.string[0] == '.'
13316                           && hash->oh != NULL)
13317                         {
13318                           fdh = ppc_follow_link (hash->oh);
13319                           if (fdh->elf.root.type == bfd_link_hash_defined
13320                               || fdh->elf.root.type == bfd_link_hash_defweak)
13321                             {
13322                               sym_sec = fdh->elf.root.u.def.section;
13323                               sym_value = fdh->elf.root.u.def.value;
13324                               if (sym_sec->output_section != NULL)
13325                                 ok_dest = TRUE;
13326                             }
13327                           else
13328                             fdh = NULL;
13329                         }
13330                     }
13331                   else
13332                     {
13333                       bfd_set_error (bfd_error_bad_value);
13334                       goto error_ret_free_internal;
13335                     }
13336
13337                   destination = 0;
13338                   local_off = 0;
13339                   if (ok_dest)
13340                     {
13341                       sym_value += irela->r_addend;
13342                       destination = (sym_value
13343                                      + sym_sec->output_offset
13344                                      + sym_sec->output_section->vma);
13345                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13346                                                             ? hash->elf.other
13347                                                             : sym->st_other);
13348                     }
13349
13350                   code_sec = sym_sec;
13351                   code_value = sym_value;
13352                   opd = get_opd_info (sym_sec);
13353                   if (opd != NULL)
13354                     {
13355                       bfd_vma dest;
13356
13357                       if (hash == NULL && opd->adjust != NULL)
13358                         {
13359                           long adjust = opd->adjust[OPD_NDX (sym_value)];
13360                           if (adjust == -1)
13361                             continue;
13362                           code_value += adjust;
13363                           sym_value += adjust;
13364                         }
13365                       dest = opd_entry_value (sym_sec, sym_value,
13366                                               &code_sec, &code_value, FALSE);
13367                       if (dest != (bfd_vma) -1)
13368                         {
13369                           destination = dest;
13370                           if (fdh != NULL)
13371                             {
13372                               /* Fixup old ABI sym to point at code
13373                                  entry.  */
13374                               hash->elf.root.type = bfd_link_hash_defweak;
13375                               hash->elf.root.u.def.section = code_sec;
13376                               hash->elf.root.u.def.value = code_value;
13377                             }
13378                         }
13379                     }
13380
13381                   /* Determine what (if any) linker stub is needed.  */
13382                   plt_ent = NULL;
13383                   stub_type = ppc_type_of_stub (section, irela, &hash,
13384                                                 &plt_ent, destination,
13385                                                 local_off);
13386
13387                   if (r_type == R_PPC64_REL24_NOTOC)
13388                     {
13389                       if (stub_type == ppc_stub_plt_call)
13390                         stub_type = ppc_stub_plt_call_notoc;
13391                       else if (stub_type == ppc_stub_long_branch
13392                                || (code_sec != NULL
13393                                    && code_sec->output_section != NULL
13394                                    && (((hash ? hash->elf.other : sym->st_other)
13395                                         & STO_PPC64_LOCAL_MASK)
13396                                        > 1 << STO_PPC64_LOCAL_BIT)))
13397                         stub_type = ppc_stub_long_branch_notoc;
13398                     }
13399                   else if (stub_type != ppc_stub_plt_call)
13400                     {
13401                       /* Check whether we need a TOC adjusting stub.
13402                          Since the linker pastes together pieces from
13403                          different object files when creating the
13404                          _init and _fini functions, it may be that a
13405                          call to what looks like a local sym is in
13406                          fact a call needing a TOC adjustment.  */
13407                       if ((code_sec != NULL
13408                            && code_sec->output_section != NULL
13409                            && (htab->sec_info[code_sec->id].toc_off
13410                                != htab->sec_info[section->id].toc_off)
13411                            && (code_sec->has_toc_reloc
13412                                || code_sec->makes_toc_func_call))
13413                           || (((hash ? hash->elf.other : sym->st_other)
13414                                & STO_PPC64_LOCAL_MASK)
13415                               == 1 << STO_PPC64_LOCAL_BIT))
13416                         stub_type = ppc_stub_long_branch_r2off;
13417                     }
13418
13419                   if (stub_type == ppc_stub_none)
13420                     continue;
13421
13422                   /* __tls_get_addr calls might be eliminated.  */
13423                   if (stub_type != ppc_stub_plt_call
13424                       && stub_type != ppc_stub_plt_call_notoc
13425                       && hash != NULL
13426                       && is_tls_get_addr (&hash->elf, htab)
13427                       && section->has_tls_reloc
13428                       && irela != internal_relocs)
13429                     {
13430                       /* Get tls info.  */
13431                       unsigned char *tls_mask;
13432
13433                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13434                                          irela - 1, input_bfd))
13435                         goto error_ret_free_internal;
13436                       if ((*tls_mask & TLS_TLS) != 0
13437                           && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13438                         continue;
13439                     }
13440
13441                   if (stub_type == ppc_stub_plt_call)
13442                     {
13443                       if (!htab->opd_abi
13444                           && htab->params->plt_localentry0 != 0
13445                           && is_elfv2_localentry0 (&hash->elf))
13446                         htab->has_plt_localentry0 = 1;
13447                       else if (irela + 1 < irelaend
13448                                && irela[1].r_offset == irela->r_offset + 4
13449                                && (ELF64_R_TYPE (irela[1].r_info)
13450                                    == R_PPC64_TOCSAVE))
13451                         {
13452                           if (!tocsave_find (htab, INSERT,
13453                                              &local_syms, irela + 1, input_bfd))
13454                             goto error_ret_free_internal;
13455                         }
13456                       else
13457                         stub_type = ppc_stub_plt_call_r2save;
13458                     }
13459
13460                   /* Support for grouping stub sections.  */
13461                   id_sec = htab->sec_info[section->id].u.group->link_sec;
13462
13463                   /* Get the name of this stub.  */
13464                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13465                   if (!stub_name)
13466                     goto error_ret_free_internal;
13467
13468                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13469                                                      stub_name, FALSE, FALSE);
13470                   if (stub_entry != NULL)
13471                     {
13472                       enum ppc_stub_type old_type;
13473                       /* A stub has already been created, but it may
13474                          not be the required type.  We shouldn't be
13475                          transitioning from plt_call to long_branch
13476                          stubs or vice versa, but we might be
13477                          upgrading from plt_call to plt_call_r2save or
13478                          from long_branch to long_branch_r2off.  */
13479                       free (stub_name);
13480                       old_type = stub_entry->stub_type;
13481                       switch (old_type)
13482                         {
13483                         default:
13484                           abort ();
13485
13486                         case ppc_stub_save_res:
13487                           continue;
13488
13489                         case ppc_stub_plt_call:
13490                         case ppc_stub_plt_call_r2save:
13491                         case ppc_stub_plt_call_notoc:
13492                         case ppc_stub_plt_call_both:
13493                           if (stub_type == ppc_stub_plt_call)
13494                             continue;
13495                           else if (stub_type == ppc_stub_plt_call_r2save)
13496                             {
13497                               if (old_type == ppc_stub_plt_call_notoc)
13498                                 stub_type = ppc_stub_plt_call_both;
13499                             }
13500                           else if (stub_type == ppc_stub_plt_call_notoc)
13501                             {
13502                               if (old_type == ppc_stub_plt_call_r2save)
13503                                 stub_type = ppc_stub_plt_call_both;
13504                             }
13505                           else
13506                             abort ();
13507                           break;
13508
13509                         case ppc_stub_plt_branch:
13510                         case ppc_stub_plt_branch_r2off:
13511                         case ppc_stub_plt_branch_notoc:
13512                         case ppc_stub_plt_branch_both:
13513                           old_type += (ppc_stub_long_branch
13514                                        - ppc_stub_plt_branch);
13515                           /* Fall through.  */
13516                         case ppc_stub_long_branch:
13517                         case ppc_stub_long_branch_r2off:
13518                         case ppc_stub_long_branch_notoc:
13519                         case ppc_stub_long_branch_both:
13520                           if (stub_type == ppc_stub_long_branch)
13521                             continue;
13522                           else if (stub_type == ppc_stub_long_branch_r2off)
13523                             {
13524                               if (old_type == ppc_stub_long_branch_notoc)
13525                                 stub_type = ppc_stub_long_branch_both;
13526                             }
13527                           else if (stub_type == ppc_stub_long_branch_notoc)
13528                             {
13529                               if (old_type == ppc_stub_long_branch_r2off)
13530                                 stub_type = ppc_stub_long_branch_both;
13531                             }
13532                           else
13533                             abort ();
13534                           break;
13535                         }
13536                       if (old_type < stub_type)
13537                         stub_entry->stub_type = stub_type;
13538                       continue;
13539                     }
13540
13541                   stub_entry = ppc_add_stub (stub_name, section, info);
13542                   if (stub_entry == NULL)
13543                     {
13544                       free (stub_name);
13545                     error_ret_free_internal:
13546                       if (elf_section_data (section)->relocs == NULL)
13547                         free (internal_relocs);
13548                     error_ret_free_local:
13549                       if (local_syms != NULL
13550                           && (symtab_hdr->contents
13551                               != (unsigned char *) local_syms))
13552                         free (local_syms);
13553                       return FALSE;
13554                     }
13555
13556                   stub_entry->stub_type = stub_type;
13557                   if (stub_type >= ppc_stub_plt_call
13558                       && stub_type <= ppc_stub_plt_call_both)
13559                     {
13560                       stub_entry->target_value = sym_value;
13561                       stub_entry->target_section = sym_sec;
13562                     }
13563                   else
13564                     {
13565                       stub_entry->target_value = code_value;
13566                       stub_entry->target_section = code_sec;
13567                     }
13568                   stub_entry->h = hash;
13569                   stub_entry->plt_ent = plt_ent;
13570                   stub_entry->symtype
13571                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13572                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
13573
13574                   if (hash != NULL
13575                       && (hash->elf.root.type == bfd_link_hash_defined
13576                           || hash->elf.root.type == bfd_link_hash_defweak))
13577                     htab->stub_globals += 1;
13578                 }
13579
13580               /* We're done with the internal relocs, free them.  */
13581               if (elf_section_data (section)->relocs != internal_relocs)
13582                 free (internal_relocs);
13583             }
13584
13585           if (local_syms != NULL
13586               && symtab_hdr->contents != (unsigned char *) local_syms)
13587             {
13588               if (!info->keep_memory)
13589                 free (local_syms);
13590               else
13591                 symtab_hdr->contents = (unsigned char *) local_syms;
13592             }
13593         }
13594
13595       /* We may have added some stubs.  Find out the new size of the
13596          stub sections.  */
13597       for (group = htab->group; group != NULL; group = group->next)
13598         {
13599           group->lr_restore = 0;
13600           group->eh_size = 0;
13601           if (group->stub_sec != NULL)
13602             {
13603               asection *stub_sec = group->stub_sec;
13604
13605               if (htab->stub_iteration <= STUB_SHRINK_ITER
13606                   || stub_sec->rawsize < stub_sec->size)
13607                 /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
13608                 stub_sec->rawsize = stub_sec->size;
13609               stub_sec->size = 0;
13610               stub_sec->reloc_count = 0;
13611               stub_sec->flags &= ~SEC_RELOC;
13612             }
13613         }
13614       if (htab->tga_group != NULL)
13615         {
13616           /* See emit_tga_desc and emit_tga_desc_eh_frame.  */
13617           htab->tga_group->eh_size
13618             = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13619           htab->tga_group->lr_restore = 23 * 4;
13620           htab->tga_group->stub_sec->size = 24 * 4;
13621         }
13622
13623       if (htab->stub_iteration <= STUB_SHRINK_ITER
13624           || htab->brlt->rawsize < htab->brlt->size)
13625         htab->brlt->rawsize = htab->brlt->size;
13626       htab->brlt->size = 0;
13627       htab->brlt->reloc_count = 0;
13628       htab->brlt->flags &= ~SEC_RELOC;
13629       if (htab->relbrlt != NULL)
13630         htab->relbrlt->size = 0;
13631
13632       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13633
13634       for (group = htab->group; group != NULL; group = group->next)
13635         if (group->needs_save_res)
13636           group->stub_sec->size += htab->sfpr->size;
13637
13638       if (info->emitrelocations
13639           && htab->glink != NULL && htab->glink->size != 0)
13640         {
13641           htab->glink->reloc_count = 1;
13642           htab->glink->flags |= SEC_RELOC;
13643         }
13644
13645       if (htab->glink_eh_frame != NULL
13646           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13647           && htab->glink_eh_frame->output_section->size > 8)
13648         {
13649           size_t size = 0, align = 4;
13650
13651           for (group = htab->group; group != NULL; group = group->next)
13652             if (group->eh_size != 0)
13653               size += (group->eh_size + 17 + align - 1) & -align;
13654           if (htab->glink != NULL && htab->glink->size != 0)
13655             size += (24 + align - 1) & -align;
13656           if (size != 0)
13657             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13658           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13659           size = (size + align - 1) & -align;
13660           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13661           htab->glink_eh_frame->size = size;
13662         }
13663
13664       if (htab->params->plt_stub_align != 0)
13665         for (group = htab->group; group != NULL; group = group->next)
13666           if (group->stub_sec != NULL)
13667             {
13668               int align = abs (htab->params->plt_stub_align);
13669               group->stub_sec->size
13670                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13671             }
13672
13673       for (group = htab->group; group != NULL; group = group->next)
13674         if (group->stub_sec != NULL
13675             && group->stub_sec->rawsize != group->stub_sec->size
13676             && (htab->stub_iteration <= STUB_SHRINK_ITER
13677                 || group->stub_sec->rawsize < group->stub_sec->size))
13678           break;
13679
13680       if (group == NULL
13681           && (htab->brlt->rawsize == htab->brlt->size
13682               || (htab->stub_iteration > STUB_SHRINK_ITER
13683                   && htab->brlt->rawsize > htab->brlt->size))
13684           && (htab->glink_eh_frame == NULL
13685               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13686           && (htab->tga_group == NULL
13687               || htab->stub_iteration > 1))
13688         break;
13689
13690       /* Ask the linker to do its stuff.  */
13691       (*htab->params->layout_sections_again) ();
13692     }
13693
13694   if (htab->glink_eh_frame != NULL
13695       && htab->glink_eh_frame->size != 0)
13696     {
13697       bfd_vma val;
13698       bfd_byte *p, *last_fde;
13699       size_t last_fde_len, size, align, pad;
13700       struct map_stub *group;
13701
13702       /* It is necessary to at least have a rough outline of the
13703          linker generated CIEs and FDEs written before
13704          bfd_elf_discard_info is run, in order for these FDEs to be
13705          indexed in .eh_frame_hdr.  */
13706       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13707       if (p == NULL)
13708         return FALSE;
13709       htab->glink_eh_frame->contents = p;
13710       last_fde = p;
13711       align = 4;
13712
13713       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13714       /* CIE length (rewrite in case little-endian).  */
13715       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13716       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13717       p += last_fde_len + 4;
13718
13719       for (group = htab->group; group != NULL; group = group->next)
13720         if (group->eh_size != 0)
13721           {
13722             group->eh_base = p - htab->glink_eh_frame->contents;
13723             last_fde = p;
13724             last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13725             /* FDE length.  */
13726             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13727             p += 4;
13728             /* CIE pointer.  */
13729             val = p - htab->glink_eh_frame->contents;
13730             bfd_put_32 (htab->elf.dynobj, val, p);
13731             p += 4;
13732             /* Offset to stub section, written later.  */
13733             p += 4;
13734             /* stub section size.  */
13735             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13736             p += 4;
13737             /* Augmentation.  */
13738             p += 1;
13739             /* Make sure we don't have all nops.  This is enough for
13740                elf-eh-frame.c to detect the last non-nop opcode.  */
13741             p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13742             p = last_fde + last_fde_len + 4;
13743           }
13744       if (htab->glink != NULL && htab->glink->size != 0)
13745         {
13746           last_fde = p;
13747           last_fde_len = ((24 + align - 1) & -align) - 4;
13748           /* FDE length.  */
13749           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13750           p += 4;
13751           /* CIE pointer.  */
13752           val = p - htab->glink_eh_frame->contents;
13753           bfd_put_32 (htab->elf.dynobj, val, p);
13754           p += 4;
13755           /* Offset to .glink, written later.  */
13756           p += 4;
13757           /* .glink size.  */
13758           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13759           p += 4;
13760           /* Augmentation.  */
13761           p += 1;
13762
13763           *p++ = DW_CFA_advance_loc + 1;
13764           *p++ = DW_CFA_register;
13765           *p++ = 65;
13766           *p++ = htab->opd_abi ? 12 : 0;
13767           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13768           *p++ = DW_CFA_restore_extended;
13769           *p++ = 65;
13770           p += ((24 + align - 1) & -align) - 24;
13771         }
13772       /* Subsume any padding into the last FDE if user .eh_frame
13773          sections are aligned more than glink_eh_frame.  Otherwise any
13774          zero padding will be seen as a terminator.  */
13775       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13776       size = p - htab->glink_eh_frame->contents;
13777       pad = ((size + align - 1) & -align) - size;
13778       htab->glink_eh_frame->size = size + pad;
13779       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13780     }
13781
13782   maybe_strip_output (info, htab->brlt);
13783   if (htab->glink_eh_frame != NULL)
13784     maybe_strip_output (info, htab->glink_eh_frame);
13785
13786   return TRUE;
13787 }
13788
13789 /* Called after we have determined section placement.  If sections
13790    move, we'll be called again.  Provide a value for TOCstart.  */
13791
13792 bfd_vma
13793 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13794 {
13795   asection *s;
13796   bfd_vma TOCstart, adjust;
13797
13798   if (info != NULL)
13799     {
13800       struct elf_link_hash_entry *h;
13801       struct elf_link_hash_table *htab = elf_hash_table (info);
13802
13803       if (is_elf_hash_table (htab)
13804           && htab->hgot != NULL)
13805         h = htab->hgot;
13806       else
13807         {
13808           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13809           if (is_elf_hash_table (htab))
13810             htab->hgot = h;
13811         }
13812       if (h != NULL
13813           && h->root.type == bfd_link_hash_defined
13814           && !h->root.linker_def
13815           && (!is_elf_hash_table (htab)
13816               || h->def_regular))
13817         {
13818           TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
13819           _bfd_set_gp_value (obfd, TOCstart);
13820           return TOCstart;
13821         }
13822     }
13823
13824   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13825      order.  The TOC starts where the first of these sections starts.  */
13826   s = bfd_get_section_by_name (obfd, ".got");
13827   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13828     s = bfd_get_section_by_name (obfd, ".toc");
13829   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13830     s = bfd_get_section_by_name (obfd, ".tocbss");
13831   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13832     s = bfd_get_section_by_name (obfd, ".plt");
13833   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13834     {
13835       /* This may happen for
13836          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13837          .toc directive
13838          o  bad linker script
13839          o --gc-sections and empty TOC sections
13840
13841          FIXME: Warn user?  */
13842
13843       /* Look for a likely section.  We probably won't even be
13844          using TOCstart.  */
13845       for (s = obfd->sections; s != NULL; s = s->next)
13846         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13847                          | SEC_EXCLUDE))
13848             == (SEC_ALLOC | SEC_SMALL_DATA))
13849           break;
13850       if (s == NULL)
13851         for (s = obfd->sections; s != NULL; s = s->next)
13852           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13853               == (SEC_ALLOC | SEC_SMALL_DATA))
13854             break;
13855       if (s == NULL)
13856         for (s = obfd->sections; s != NULL; s = s->next)
13857           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13858               == SEC_ALLOC)
13859             break;
13860       if (s == NULL)
13861         for (s = obfd->sections; s != NULL; s = s->next)
13862           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13863             break;
13864     }
13865
13866   TOCstart = 0;
13867   if (s != NULL)
13868     TOCstart = s->output_section->vma + s->output_offset;
13869
13870   /* Force alignment.  */
13871   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13872   TOCstart -= adjust;
13873   _bfd_set_gp_value (obfd, TOCstart);
13874
13875   if (info != NULL && s != NULL)
13876     {
13877       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13878
13879       if (htab != NULL)
13880         {
13881           if (htab->elf.hgot != NULL)
13882             {
13883               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13884               htab->elf.hgot->root.u.def.section = s;
13885             }
13886         }
13887       else
13888         {
13889           struct bfd_link_hash_entry *bh = NULL;
13890           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13891                                             s, TOC_BASE_OFF - adjust,
13892                                             NULL, FALSE, FALSE, &bh);
13893         }
13894     }
13895   return TOCstart;
13896 }
13897
13898 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13899    write out any global entry stubs, and PLT relocations.  */
13900
13901 static bfd_boolean
13902 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13903 {
13904   struct bfd_link_info *info;
13905   struct ppc_link_hash_table *htab;
13906   struct plt_entry *ent;
13907   asection *s;
13908
13909   if (h->root.type == bfd_link_hash_indirect)
13910     return TRUE;
13911
13912   info = inf;
13913   htab = ppc_hash_table (info);
13914   if (htab == NULL)
13915     return FALSE;
13916
13917   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13918     if (ent->plt.offset != (bfd_vma) -1)
13919       {
13920         /* This symbol has an entry in the procedure linkage
13921            table.  Set it up.  */
13922         Elf_Internal_Rela rela;
13923         asection *plt, *relplt;
13924         bfd_byte *loc;
13925
13926         if (!htab->elf.dynamic_sections_created
13927             || h->dynindx == -1)
13928           {
13929             if (!(h->def_regular
13930                   && (h->root.type == bfd_link_hash_defined
13931                       || h->root.type == bfd_link_hash_defweak)))
13932               continue;
13933             if (h->type == STT_GNU_IFUNC)
13934               {
13935                 plt = htab->elf.iplt;
13936                 relplt = htab->elf.irelplt;
13937                 htab->local_ifunc_resolver = 1;
13938                 if (htab->opd_abi)
13939                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13940                 else
13941                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13942               }
13943             else
13944               {
13945                 plt = htab->pltlocal;
13946                 if (bfd_link_pic (info))
13947                   {
13948                     relplt = htab->relpltlocal;
13949                     if (htab->opd_abi)
13950                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13951                     else
13952                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13953                   }
13954                 else
13955                   relplt = NULL;
13956               }
13957             rela.r_addend = defined_sym_val (h) + ent->addend;
13958
13959             if (relplt == NULL)
13960               {
13961                 loc = plt->contents + ent->plt.offset;
13962                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13963                 if (htab->opd_abi)
13964                   {
13965                     bfd_vma toc = elf_gp (info->output_bfd);
13966                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13967                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13968                   }
13969               }
13970             else
13971               {
13972                 rela.r_offset = (plt->output_section->vma
13973                                  + plt->output_offset
13974                                  + ent->plt.offset);
13975                 loc = relplt->contents + (relplt->reloc_count++
13976                                           * sizeof (Elf64_External_Rela));
13977                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13978               }
13979           }
13980         else
13981           {
13982             rela.r_offset = (htab->elf.splt->output_section->vma
13983                              + htab->elf.splt->output_offset
13984                              + ent->plt.offset);
13985             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13986             rela.r_addend = ent->addend;
13987             loc = (htab->elf.srelplt->contents
13988                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13989                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13990             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13991               htab->maybe_local_ifunc_resolver = 1;
13992             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13993           }
13994       }
13995
13996   if (!h->pointer_equality_needed)
13997     return TRUE;
13998
13999   if (h->def_regular)
14000     return TRUE;
14001
14002   s = htab->global_entry;
14003   if (s == NULL || s->size == 0)
14004     return TRUE;
14005
14006   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14007     if (ent->plt.offset != (bfd_vma) -1
14008         && ent->addend == 0)
14009       {
14010         bfd_byte *p;
14011         asection *plt;
14012         bfd_vma off;
14013
14014         p = s->contents + h->root.u.def.value;
14015         plt = htab->elf.splt;
14016         if (!htab->elf.dynamic_sections_created
14017             || h->dynindx == -1)
14018           {
14019             if (h->type == STT_GNU_IFUNC)
14020               plt = htab->elf.iplt;
14021             else
14022               plt = htab->pltlocal;
14023           }
14024         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14025         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14026
14027         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14028           {
14029             info->callbacks->einfo
14030               (_("%P: linkage table error against `%pT'\n"),
14031                h->root.root.string);
14032             bfd_set_error (bfd_error_bad_value);
14033             htab->stub_error = TRUE;
14034           }
14035
14036         htab->stub_count[ppc_stub_global_entry - 1] += 1;
14037         if (htab->params->emit_stub_syms)
14038           {
14039             size_t len = strlen (h->root.root.string);
14040             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14041
14042             if (name == NULL)
14043               return FALSE;
14044
14045             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14046             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
14047             if (h == NULL)
14048               return FALSE;
14049             if (h->root.type == bfd_link_hash_new)
14050               {
14051                 h->root.type = bfd_link_hash_defined;
14052                 h->root.u.def.section = s;
14053                 h->root.u.def.value = p - s->contents;
14054                 h->ref_regular = 1;
14055                 h->def_regular = 1;
14056                 h->ref_regular_nonweak = 1;
14057                 h->forced_local = 1;
14058                 h->non_elf = 0;
14059                 h->root.linker_def = 1;
14060               }
14061           }
14062
14063         if (PPC_HA (off) != 0)
14064           {
14065             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14066             p += 4;
14067           }
14068         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14069         p += 4;
14070         bfd_put_32 (s->owner, MTCTR_R12, p);
14071         p += 4;
14072         bfd_put_32 (s->owner, BCTR, p);
14073         break;
14074       }
14075   return TRUE;
14076 }
14077
14078 /* Write PLT relocs for locals.  */
14079
14080 static bfd_boolean
14081 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14082 {
14083   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14084   bfd *ibfd;
14085
14086   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14087     {
14088       struct got_entry **lgot_ents, **end_lgot_ents;
14089       struct plt_entry **local_plt, **lplt, **end_local_plt;
14090       Elf_Internal_Shdr *symtab_hdr;
14091       bfd_size_type locsymcount;
14092       Elf_Internal_Sym *local_syms = NULL;
14093       struct plt_entry *ent;
14094
14095       if (!is_ppc64_elf (ibfd))
14096         continue;
14097
14098       lgot_ents = elf_local_got_ents (ibfd);
14099       if (!lgot_ents)
14100         continue;
14101
14102       symtab_hdr = &elf_symtab_hdr (ibfd);
14103       locsymcount = symtab_hdr->sh_info;
14104       end_lgot_ents = lgot_ents + locsymcount;
14105       local_plt = (struct plt_entry **) end_lgot_ents;
14106       end_local_plt = local_plt + locsymcount;
14107       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14108         for (ent = *lplt; ent != NULL; ent = ent->next)
14109           if (ent->plt.offset != (bfd_vma) -1)
14110             {
14111               Elf_Internal_Sym *sym;
14112               asection *sym_sec;
14113               asection *plt, *relplt;
14114               bfd_byte *loc;
14115               bfd_vma val;
14116
14117               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14118                               lplt - local_plt, ibfd))
14119                 {
14120                   if (local_syms != NULL
14121                       && symtab_hdr->contents != (unsigned char *) local_syms)
14122                     free (local_syms);
14123                   return FALSE;
14124                 }
14125
14126               val = sym->st_value + ent->addend;
14127               if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
14128                 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
14129               if (sym_sec != NULL && sym_sec->output_section != NULL)
14130                 val += sym_sec->output_offset + sym_sec->output_section->vma;
14131
14132               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14133                 {
14134                   htab->local_ifunc_resolver = 1;
14135                   plt = htab->elf.iplt;
14136                   relplt = htab->elf.irelplt;
14137                 }
14138               else
14139                 {
14140                   plt = htab->pltlocal;
14141                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14142                 }
14143
14144               if (relplt == NULL)
14145                 {
14146                   loc = plt->contents + ent->plt.offset;
14147                   bfd_put_64 (info->output_bfd, val, loc);
14148                   if (htab->opd_abi)
14149                     {
14150                       bfd_vma toc = elf_gp (ibfd);
14151                       bfd_put_64 (info->output_bfd, toc, loc + 8);
14152                     }
14153                 }
14154               else
14155                 {
14156                   Elf_Internal_Rela rela;
14157                   rela.r_offset = (ent->plt.offset
14158                                    + plt->output_offset
14159                                    + plt->output_section->vma);
14160                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14161                     {
14162                       if (htab->opd_abi)
14163                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14164                       else
14165                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14166                     }
14167                   else
14168                     {
14169                       if (htab->opd_abi)
14170                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14171                       else
14172                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14173                     }
14174                   rela.r_addend = val;
14175                   loc = relplt->contents + (relplt->reloc_count++
14176                                             * sizeof (Elf64_External_Rela));
14177                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14178                 }
14179             }
14180
14181       if (local_syms != NULL
14182           && symtab_hdr->contents != (unsigned char *) local_syms)
14183         {
14184           if (!info->keep_memory)
14185             free (local_syms);
14186           else
14187             symtab_hdr->contents = (unsigned char *) local_syms;
14188         }
14189     }
14190   return TRUE;
14191 }
14192
14193 /* Emit the static wrapper function preserving registers around a
14194    __tls_get_addr_opt call.  */
14195
14196 static bfd_boolean
14197 emit_tga_desc (struct ppc_link_hash_table *htab)
14198 {
14199   asection *stub_sec = htab->tga_group->stub_sec;
14200   unsigned int cfa_updt = 11 * 4;
14201   bfd_byte *p;
14202   bfd_vma to, from, delta;
14203
14204   BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14205               && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14206               && htab->tga_desc_fd->elf.root.u.def.value == 0);
14207   to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14208   from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14209   delta = to - from;
14210   if (delta + (1 << 25) >= 1 << 26)
14211     {
14212       _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14213       htab->stub_error = TRUE;
14214       return FALSE;
14215     }
14216
14217   p = stub_sec->contents;
14218   p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14219   bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14220   p += 4;
14221   p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14222   return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14223 }
14224
14225 /* Emit eh_frame describing the static wrapper function.  */
14226
14227 static bfd_byte *
14228 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14229 {
14230   unsigned int cfa_updt = 11 * 4;
14231   unsigned int i;
14232
14233   *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14234   *p++ = DW_CFA_def_cfa_offset;
14235   if (htab->opd_abi)
14236     {
14237       *p++ = 128;
14238       *p++ = 1;
14239     }
14240   else
14241     *p++ = 96;
14242   *p++ = DW_CFA_offset_extended_sf;
14243   *p++ = 65;
14244   *p++ = (-16 / 8) & 0x7f;
14245   for (i = 4; i < 12; i++)
14246     {
14247       *p++ = DW_CFA_offset + i;
14248       *p++ = (htab->opd_abi ? 13 : 12) - i;
14249     }
14250   *p++ = DW_CFA_advance_loc + 10;
14251   *p++ = DW_CFA_def_cfa_offset;
14252   *p++ = 0;
14253   for (i = 4; i < 12; i++)
14254     *p++ = DW_CFA_restore + i;
14255   *p++ = DW_CFA_advance_loc + 2;
14256   *p++ = DW_CFA_restore_extended;
14257   *p++ = 65;
14258   return p;
14259 }
14260
14261 /* Build all the stubs associated with the current output file.
14262    The stubs are kept in a hash table attached to the main linker
14263    hash table.  This function is called via gldelf64ppc_finish.  */
14264
14265 bfd_boolean
14266 ppc64_elf_build_stubs (struct bfd_link_info *info,
14267                        char **stats)
14268 {
14269   struct ppc_link_hash_table *htab = ppc_hash_table (info);
14270   struct map_stub *group;
14271   asection *stub_sec;
14272   bfd_byte *p;
14273   int stub_sec_count = 0;
14274
14275   if (htab == NULL)
14276     return FALSE;
14277
14278   /* Allocate memory to hold the linker stubs.  */
14279   for (group = htab->group; group != NULL; group = group->next)
14280     {
14281       group->eh_size = 0;
14282       group->lr_restore = 0;
14283       if ((stub_sec = group->stub_sec) != NULL
14284           && stub_sec->size != 0)
14285         {
14286           stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14287                                            stub_sec->size);
14288           if (stub_sec->contents == NULL)
14289             return FALSE;
14290           stub_sec->size = 0;
14291         }
14292     }
14293
14294   if (htab->glink != NULL && htab->glink->size != 0)
14295     {
14296       unsigned int indx;
14297       bfd_vma plt0;
14298
14299       /* Build the .glink plt call stub.  */
14300       if (htab->params->emit_stub_syms)
14301         {
14302           struct elf_link_hash_entry *h;
14303           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14304                                     TRUE, FALSE, FALSE);
14305           if (h == NULL)
14306             return FALSE;
14307           if (h->root.type == bfd_link_hash_new)
14308             {
14309               h->root.type = bfd_link_hash_defined;
14310               h->root.u.def.section = htab->glink;
14311               h->root.u.def.value = 8;
14312               h->ref_regular = 1;
14313               h->def_regular = 1;
14314               h->ref_regular_nonweak = 1;
14315               h->forced_local = 1;
14316               h->non_elf = 0;
14317               h->root.linker_def = 1;
14318             }
14319         }
14320       plt0 = (htab->elf.splt->output_section->vma
14321               + htab->elf.splt->output_offset
14322               - 16);
14323       if (info->emitrelocations)
14324         {
14325           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14326           if (r == NULL)
14327             return FALSE;
14328           r->r_offset = (htab->glink->output_offset
14329                          + htab->glink->output_section->vma);
14330           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14331           r->r_addend = plt0;
14332         }
14333       p = htab->glink->contents;
14334       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14335       bfd_put_64 (htab->glink->owner, plt0, p);
14336       p += 8;
14337       if (htab->opd_abi)
14338         {
14339           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14340           p += 4;
14341           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14342           p += 4;
14343           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14344           p += 4;
14345           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14346           p += 4;
14347           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14348           p += 4;
14349           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14350           p += 4;
14351           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14352           p += 4;
14353           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14354           p += 4;
14355           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14356           p += 4;
14357           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14358           p += 4;
14359         }
14360       else
14361         {
14362           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14363           p += 4;
14364           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14365           p += 4;
14366           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14367           p += 4;
14368           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14369           p += 4;
14370           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14371           p += 4;
14372           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14373           p += 4;
14374           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14375           p += 4;
14376           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14377           p += 4;
14378           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14379           p += 4;
14380           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14381           p += 4;
14382           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14383           p += 4;
14384           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14385           p += 4;
14386           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14387           p += 4;
14388         }
14389       bfd_put_32 (htab->glink->owner, BCTR, p);
14390       p += 4;
14391       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14392
14393       /* Build the .glink lazy link call stubs.  */
14394       indx = 0;
14395       while (p < htab->glink->contents + htab->glink->size)
14396         {
14397           if (htab->opd_abi)
14398             {
14399               if (indx < 0x8000)
14400                 {
14401                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14402                   p += 4;
14403                 }
14404               else
14405                 {
14406                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14407                   p += 4;
14408                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14409                               p);
14410                   p += 4;
14411                 }
14412             }
14413           bfd_put_32 (htab->glink->owner,
14414                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14415           indx++;
14416           p += 4;
14417         }
14418     }
14419
14420   if (htab->tga_group != NULL)
14421     {
14422       htab->tga_group->lr_restore = 23 * 4;
14423       htab->tga_group->stub_sec->size = 24 * 4;
14424       if (!emit_tga_desc (htab))
14425         return FALSE;
14426       if (htab->glink_eh_frame != NULL
14427           && htab->glink_eh_frame->size != 0)
14428         {
14429           size_t align = 4;
14430
14431           p = htab->glink_eh_frame->contents;
14432           p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14433           p += 17;
14434           htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14435         }
14436     }
14437
14438   /* Build .glink global entry stubs, and PLT relocs for globals.  */
14439   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14440
14441   if (!write_plt_relocs_for_local_syms (info))
14442     return FALSE;
14443
14444   if (htab->brlt != NULL && htab->brlt->size != 0)
14445     {
14446       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14447                                          htab->brlt->size);
14448       if (htab->brlt->contents == NULL)
14449         return FALSE;
14450     }
14451   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14452     {
14453       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14454                                             htab->relbrlt->size);
14455       if (htab->relbrlt->contents == NULL)
14456         return FALSE;
14457     }
14458
14459   /* Build the stubs as directed by the stub hash table.  */
14460   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14461
14462   for (group = htab->group; group != NULL; group = group->next)
14463     if (group->needs_save_res)
14464       group->stub_sec->size += htab->sfpr->size;
14465
14466   if (htab->relbrlt != NULL)
14467     htab->relbrlt->reloc_count = 0;
14468
14469   if (htab->params->plt_stub_align != 0)
14470     for (group = htab->group; group != NULL; group = group->next)
14471       if ((stub_sec = group->stub_sec) != NULL)
14472         {
14473           int align = abs (htab->params->plt_stub_align);
14474           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14475         }
14476
14477   for (group = htab->group; group != NULL; group = group->next)
14478     if (group->needs_save_res)
14479       {
14480         stub_sec = group->stub_sec;
14481         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14482                 htab->sfpr->contents, htab->sfpr->size);
14483         if (htab->params->emit_stub_syms)
14484           {
14485             unsigned int i;
14486
14487             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14488               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14489                 return FALSE;
14490           }
14491       }
14492
14493   if (htab->glink_eh_frame != NULL
14494       && htab->glink_eh_frame->size != 0)
14495     {
14496       bfd_vma val;
14497       size_t align = 4;
14498
14499       p = htab->glink_eh_frame->contents;
14500       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14501
14502       for (group = htab->group; group != NULL; group = group->next)
14503         if (group->eh_size != 0)
14504           {
14505             /* Offset to stub section.  */
14506             val = (group->stub_sec->output_section->vma
14507                    + group->stub_sec->output_offset);
14508             val -= (htab->glink_eh_frame->output_section->vma
14509                     + htab->glink_eh_frame->output_offset
14510                     + (p + 8 - htab->glink_eh_frame->contents));
14511             if (val + 0x80000000 > 0xffffffff)
14512               {
14513                 _bfd_error_handler
14514                   (_("%s offset too large for .eh_frame sdata4 encoding"),
14515                    group->stub_sec->name);
14516                 return FALSE;
14517               }
14518             bfd_put_32 (htab->elf.dynobj, val, p + 8);
14519             p += (group->eh_size + 17 + 3) & -4;
14520           }
14521       if (htab->glink != NULL && htab->glink->size != 0)
14522         {
14523           /* Offset to .glink.  */
14524           val = (htab->glink->output_section->vma
14525                  + htab->glink->output_offset
14526                  + 8);
14527           val -= (htab->glink_eh_frame->output_section->vma
14528                   + htab->glink_eh_frame->output_offset
14529                   + (p + 8 - htab->glink_eh_frame->contents));
14530           if (val + 0x80000000 > 0xffffffff)
14531             {
14532               _bfd_error_handler
14533                 (_("%s offset too large for .eh_frame sdata4 encoding"),
14534                  htab->glink->name);
14535               return FALSE;
14536             }
14537           bfd_put_32 (htab->elf.dynobj, val, p + 8);
14538           p += (24 + align - 1) & -align;
14539         }
14540     }
14541
14542   for (group = htab->group; group != NULL; group = group->next)
14543     if ((stub_sec = group->stub_sec) != NULL)
14544       {
14545         stub_sec_count += 1;
14546         if (stub_sec->rawsize != stub_sec->size
14547             && (htab->stub_iteration <= STUB_SHRINK_ITER
14548                 || stub_sec->rawsize < stub_sec->size))
14549           break;
14550       }
14551
14552   if (group != NULL)
14553     {
14554       htab->stub_error = TRUE;
14555       _bfd_error_handler (_("stubs don't match calculated size"));
14556     }
14557
14558   if (htab->stub_error)
14559     return FALSE;
14560
14561   if (stats != NULL)
14562     {
14563       char *groupmsg;
14564       if (asprintf (&groupmsg,
14565                     ngettext ("linker stubs in %u group\n",
14566                               "linker stubs in %u groups\n",
14567                               stub_sec_count),
14568                     stub_sec_count) < 0)
14569         *stats = NULL;
14570       else
14571         {
14572           if (asprintf (stats, _("%s"
14573                                  "  branch         %lu\n"
14574                                  "  branch toc adj %lu\n"
14575                                  "  branch notoc   %lu\n"
14576                                  "  branch both    %lu\n"
14577                                  "  long branch    %lu\n"
14578                                  "  long toc adj   %lu\n"
14579                                  "  long notoc     %lu\n"
14580                                  "  long both      %lu\n"
14581                                  "  plt call       %lu\n"
14582                                  "  plt call save  %lu\n"
14583                                  "  plt call notoc %lu\n"
14584                                  "  plt call both  %lu\n"
14585                                  "  global entry   %lu"),
14586                         groupmsg,
14587                         htab->stub_count[ppc_stub_long_branch - 1],
14588                         htab->stub_count[ppc_stub_long_branch_r2off - 1],
14589                         htab->stub_count[ppc_stub_long_branch_notoc - 1],
14590                         htab->stub_count[ppc_stub_long_branch_both - 1],
14591                         htab->stub_count[ppc_stub_plt_branch - 1],
14592                         htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14593                         htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14594                         htab->stub_count[ppc_stub_plt_branch_both - 1],
14595                         htab->stub_count[ppc_stub_plt_call - 1],
14596                         htab->stub_count[ppc_stub_plt_call_r2save - 1],
14597                         htab->stub_count[ppc_stub_plt_call_notoc - 1],
14598                         htab->stub_count[ppc_stub_plt_call_both - 1],
14599                         htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14600             *stats = NULL;
14601           free (groupmsg);
14602         }
14603     }
14604   return TRUE;
14605 }
14606
14607 /* What to do when ld finds relocations against symbols defined in
14608    discarded sections.  */
14609
14610 static unsigned int
14611 ppc64_elf_action_discarded (asection *sec)
14612 {
14613   if (strcmp (".opd", sec->name) == 0)
14614     return 0;
14615
14616   if (strcmp (".toc", sec->name) == 0)
14617     return 0;
14618
14619   if (strcmp (".toc1", sec->name) == 0)
14620     return 0;
14621
14622   return _bfd_elf_default_action_discarded (sec);
14623 }
14624
14625 /* These are the dynamic relocations supported by glibc.  */
14626
14627 static bfd_boolean
14628 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14629 {
14630   switch (r_type)
14631     {
14632     case R_PPC64_RELATIVE:
14633     case R_PPC64_NONE:
14634     case R_PPC64_ADDR64:
14635     case R_PPC64_GLOB_DAT:
14636     case R_PPC64_IRELATIVE:
14637     case R_PPC64_JMP_IREL:
14638     case R_PPC64_JMP_SLOT:
14639     case R_PPC64_DTPMOD64:
14640     case R_PPC64_DTPREL64:
14641     case R_PPC64_TPREL64:
14642     case R_PPC64_TPREL16_LO_DS:
14643     case R_PPC64_TPREL16_DS:
14644     case R_PPC64_TPREL16:
14645     case R_PPC64_TPREL16_LO:
14646     case R_PPC64_TPREL16_HI:
14647     case R_PPC64_TPREL16_HIGH:
14648     case R_PPC64_TPREL16_HA:
14649     case R_PPC64_TPREL16_HIGHA:
14650     case R_PPC64_TPREL16_HIGHER:
14651     case R_PPC64_TPREL16_HIGHEST:
14652     case R_PPC64_TPREL16_HIGHERA:
14653     case R_PPC64_TPREL16_HIGHESTA:
14654     case R_PPC64_ADDR16_LO_DS:
14655     case R_PPC64_ADDR16_LO:
14656     case R_PPC64_ADDR16_HI:
14657     case R_PPC64_ADDR16_HIGH:
14658     case R_PPC64_ADDR16_HA:
14659     case R_PPC64_ADDR16_HIGHA:
14660     case R_PPC64_REL30:
14661     case R_PPC64_COPY:
14662     case R_PPC64_UADDR64:
14663     case R_PPC64_UADDR32:
14664     case R_PPC64_ADDR32:
14665     case R_PPC64_ADDR24:
14666     case R_PPC64_ADDR16:
14667     case R_PPC64_UADDR16:
14668     case R_PPC64_ADDR16_DS:
14669     case R_PPC64_ADDR16_HIGHER:
14670     case R_PPC64_ADDR16_HIGHEST:
14671     case R_PPC64_ADDR16_HIGHERA:
14672     case R_PPC64_ADDR16_HIGHESTA:
14673     case R_PPC64_ADDR14:
14674     case R_PPC64_ADDR14_BRTAKEN:
14675     case R_PPC64_ADDR14_BRNTAKEN:
14676     case R_PPC64_REL32:
14677     case R_PPC64_REL64:
14678       return TRUE;
14679
14680     default:
14681       return FALSE;
14682     }
14683 }
14684
14685 /* The RELOCATE_SECTION function is called by the ELF backend linker
14686    to handle the relocations for a section.
14687
14688    The relocs are always passed as Rela structures; if the section
14689    actually uses Rel structures, the r_addend field will always be
14690    zero.
14691
14692    This function is responsible for adjust the section contents as
14693    necessary, and (if using Rela relocs and generating a
14694    relocatable output file) adjusting the reloc addend as
14695    necessary.
14696
14697    This function does not have to worry about setting the reloc
14698    address or the reloc symbol index.
14699
14700    LOCAL_SYMS is a pointer to the swapped in local symbols.
14701
14702    LOCAL_SECTIONS is an array giving the section in the input file
14703    corresponding to the st_shndx field of each local symbol.
14704
14705    The global hash table entry for the global symbols can be found
14706    via elf_sym_hashes (input_bfd).
14707
14708    When generating relocatable output, this function must handle
14709    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
14710    going to be the section symbol corresponding to the output
14711    section, which means that the addend must be adjusted
14712    accordingly.  */
14713
14714 static bfd_boolean
14715 ppc64_elf_relocate_section (bfd *output_bfd,
14716                             struct bfd_link_info *info,
14717                             bfd *input_bfd,
14718                             asection *input_section,
14719                             bfd_byte *contents,
14720                             Elf_Internal_Rela *relocs,
14721                             Elf_Internal_Sym *local_syms,
14722                             asection **local_sections)
14723 {
14724   struct ppc_link_hash_table *htab;
14725   Elf_Internal_Shdr *symtab_hdr;
14726   struct elf_link_hash_entry **sym_hashes;
14727   Elf_Internal_Rela *rel;
14728   Elf_Internal_Rela *wrel;
14729   Elf_Internal_Rela *relend;
14730   Elf_Internal_Rela outrel;
14731   bfd_byte *loc;
14732   struct got_entry **local_got_ents;
14733   bfd_vma TOCstart;
14734   bfd_boolean ret = TRUE;
14735   bfd_boolean is_opd;
14736   /* Assume 'at' branch hints.  */
14737   bfd_boolean is_isa_v2 = TRUE;
14738   bfd_boolean warned_dynamic = FALSE;
14739   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14740
14741   /* Initialize howto table if needed.  */
14742   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14743     ppc_howto_init ();
14744
14745   htab = ppc_hash_table (info);
14746   if (htab == NULL)
14747     return FALSE;
14748
14749   /* Don't relocate stub sections.  */
14750   if (input_section->owner == htab->params->stub_bfd)
14751     return TRUE;
14752
14753   if (!is_ppc64_elf (input_bfd))
14754     {
14755       bfd_set_error (bfd_error_wrong_format);
14756       return FALSE;
14757     }
14758
14759   local_got_ents = elf_local_got_ents (input_bfd);
14760   TOCstart = elf_gp (output_bfd);
14761   symtab_hdr = &elf_symtab_hdr (input_bfd);
14762   sym_hashes = elf_sym_hashes (input_bfd);
14763   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14764
14765   rel = wrel = relocs;
14766   relend = relocs + input_section->reloc_count;
14767   for (; rel < relend; wrel++, rel++)
14768     {
14769       enum elf_ppc64_reloc_type r_type;
14770       bfd_vma addend;
14771       bfd_reloc_status_type r;
14772       Elf_Internal_Sym *sym;
14773       asection *sec;
14774       struct elf_link_hash_entry *h_elf;
14775       struct ppc_link_hash_entry *h;
14776       struct ppc_link_hash_entry *fdh;
14777       const char *sym_name;
14778       unsigned long r_symndx, toc_symndx;
14779       bfd_vma toc_addend;
14780       unsigned char tls_mask, tls_gd, tls_type;
14781       unsigned char sym_type;
14782       bfd_vma relocation;
14783       bfd_boolean unresolved_reloc, save_unresolved_reloc;
14784       bfd_boolean warned;
14785       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14786       unsigned int insn;
14787       unsigned int mask;
14788       struct ppc_stub_hash_entry *stub_entry;
14789       bfd_vma max_br_offset;
14790       bfd_vma from;
14791       Elf_Internal_Rela orig_rel;
14792       reloc_howto_type *howto;
14793       struct reloc_howto_struct alt_howto;
14794       uint64_t pinsn;
14795       bfd_vma offset;
14796
14797     again:
14798       orig_rel = *rel;
14799
14800       r_type = ELF64_R_TYPE (rel->r_info);
14801       r_symndx = ELF64_R_SYM (rel->r_info);
14802
14803       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14804          symbol of the previous ADDR64 reloc.  The symbol gives us the
14805          proper TOC base to use.  */
14806       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14807           && wrel != relocs
14808           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14809           && is_opd)
14810         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14811
14812       sym = NULL;
14813       sec = NULL;
14814       h_elf = NULL;
14815       sym_name = NULL;
14816       unresolved_reloc = FALSE;
14817       warned = FALSE;
14818
14819       if (r_symndx < symtab_hdr->sh_info)
14820         {
14821           /* It's a local symbol.  */
14822           struct _opd_sec_data *opd;
14823
14824           sym = local_syms + r_symndx;
14825           sec = local_sections[r_symndx];
14826           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14827           sym_type = ELF64_ST_TYPE (sym->st_info);
14828           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14829           opd = get_opd_info (sec);
14830           if (opd != NULL && opd->adjust != NULL)
14831             {
14832               long adjust = opd->adjust[OPD_NDX (sym->st_value
14833                                                  + rel->r_addend)];
14834               if (adjust == -1)
14835                 relocation = 0;
14836               else
14837                 {
14838                   /* If this is a relocation against the opd section sym
14839                      and we have edited .opd, adjust the reloc addend so
14840                      that ld -r and ld --emit-relocs output is correct.
14841                      If it is a reloc against some other .opd symbol,
14842                      then the symbol value will be adjusted later.  */
14843                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14844                     rel->r_addend += adjust;
14845                   else
14846                     relocation += adjust;
14847                 }
14848             }
14849         }
14850       else
14851         {
14852           bfd_boolean ignored;
14853
14854           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14855                                    r_symndx, symtab_hdr, sym_hashes,
14856                                    h_elf, sec, relocation,
14857                                    unresolved_reloc, warned, ignored);
14858           sym_name = h_elf->root.root.string;
14859           sym_type = h_elf->type;
14860           if (sec != NULL
14861               && sec->owner == output_bfd
14862               && strcmp (sec->name, ".opd") == 0)
14863             {
14864               /* This is a symbol defined in a linker script.  All
14865                  such are defined in output sections, even those
14866                  defined by simple assignment from a symbol defined in
14867                  an input section.  Transfer the symbol to an
14868                  appropriate input .opd section, so that a branch to
14869                  this symbol will be mapped to the location specified
14870                  by the opd entry.  */
14871               struct bfd_link_order *lo;
14872               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14873                 if (lo->type == bfd_indirect_link_order)
14874                   {
14875                     asection *isec = lo->u.indirect.section;
14876                     if (h_elf->root.u.def.value >= isec->output_offset
14877                         && h_elf->root.u.def.value < (isec->output_offset
14878                                                       + isec->size))
14879                       {
14880                         h_elf->root.u.def.value -= isec->output_offset;
14881                         h_elf->root.u.def.section = isec;
14882                         sec = isec;
14883                         break;
14884                       }
14885                   }
14886             }
14887         }
14888       h = ppc_elf_hash_entry (h_elf);
14889
14890       if (sec != NULL && discarded_section (sec))
14891         {
14892           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14893                                input_bfd, input_section,
14894                                contents, rel->r_offset);
14895           wrel->r_offset = rel->r_offset;
14896           wrel->r_info = 0;
14897           wrel->r_addend = 0;
14898
14899           /* For ld -r, remove relocations in debug sections against
14900              symbols defined in discarded sections.  Not done for
14901              non-debug to preserve relocs in .eh_frame which the
14902              eh_frame editing code expects to be present.  */
14903           if (bfd_link_relocatable (info)
14904               && (input_section->flags & SEC_DEBUGGING))
14905             wrel--;
14906
14907           continue;
14908         }
14909
14910       if (bfd_link_relocatable (info))
14911         goto copy_reloc;
14912
14913       if (h != NULL && &h->elf == htab->elf.hgot)
14914         {
14915           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14916           sec = bfd_abs_section_ptr;
14917           unresolved_reloc = FALSE;
14918         }
14919
14920       /* TLS optimizations.  Replace instruction sequences and relocs
14921          based on information we collected in tls_optimize.  We edit
14922          RELOCS so that --emit-relocs will output something sensible
14923          for the final instruction stream.  */
14924       tls_mask = 0;
14925       tls_gd = 0;
14926       toc_symndx = 0;
14927       if (h != NULL)
14928         tls_mask = h->tls_mask;
14929       else if (local_got_ents != NULL)
14930         {
14931           struct plt_entry **local_plt = (struct plt_entry **)
14932             (local_got_ents + symtab_hdr->sh_info);
14933           unsigned char *lgot_masks = (unsigned char *)
14934             (local_plt + symtab_hdr->sh_info);
14935           tls_mask = lgot_masks[r_symndx];
14936         }
14937       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14938           && (r_type == R_PPC64_TLS
14939               || r_type == R_PPC64_TLSGD
14940               || r_type == R_PPC64_TLSLD))
14941         {
14942           /* Check for toc tls entries.  */
14943           unsigned char *toc_tls;
14944
14945           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14946                              &local_syms, rel, input_bfd))
14947             return FALSE;
14948
14949           if (toc_tls)
14950             tls_mask = *toc_tls;
14951         }
14952
14953       /* Check that tls relocs are used with tls syms, and non-tls
14954          relocs are used with non-tls syms.  */
14955       if (r_symndx != STN_UNDEF
14956           && r_type != R_PPC64_NONE
14957           && (h == NULL
14958               || h->elf.root.type == bfd_link_hash_defined
14959               || h->elf.root.type == bfd_link_hash_defweak)
14960           && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
14961         {
14962           if ((tls_mask & TLS_TLS) != 0
14963               && (r_type == R_PPC64_TLS
14964                   || r_type == R_PPC64_TLSGD
14965                   || r_type == R_PPC64_TLSLD))
14966             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
14967             ;
14968           else
14969             info->callbacks->einfo
14970               (!IS_PPC64_TLS_RELOC (r_type)
14971                /* xgettext:c-format */
14972                ? _("%H: %s used with TLS symbol `%pT'\n")
14973                /* xgettext:c-format */
14974                : _("%H: %s used with non-TLS symbol `%pT'\n"),
14975                input_bfd, input_section, rel->r_offset,
14976                ppc64_elf_howto_table[r_type]->name,
14977                sym_name);
14978         }
14979
14980       /* Ensure reloc mapping code below stays sane.  */
14981       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14982           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14983           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
14984           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14985           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14986           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14987           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
14988           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14989           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14990           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14991         abort ();
14992
14993       switch (r_type)
14994         {
14995         default:
14996           break;
14997
14998         case R_PPC64_LO_DS_OPT:
14999           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
15000           if ((insn & (0x3fu << 26)) != 58u << 26)
15001             abort ();
15002           insn += (14u << 26) - (58u << 26);
15003           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
15004           r_type = R_PPC64_TOC16_LO;
15005           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15006           break;
15007
15008         case R_PPC64_TOC16:
15009         case R_PPC64_TOC16_LO:
15010         case R_PPC64_TOC16_DS:
15011         case R_PPC64_TOC16_LO_DS:
15012           {
15013             /* Check for toc tls entries.  */
15014             unsigned char *toc_tls;
15015             int retval;
15016
15017             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15018                                    &local_syms, rel, input_bfd);
15019             if (retval == 0)
15020               return FALSE;
15021
15022             if (toc_tls)
15023               {
15024                 tls_mask = *toc_tls;
15025                 if (r_type == R_PPC64_TOC16_DS
15026                     || r_type == R_PPC64_TOC16_LO_DS)
15027                   {
15028                     if ((tls_mask & TLS_TLS) != 0
15029                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15030                       goto toctprel;
15031                   }
15032                 else
15033                   {
15034                     /* If we found a GD reloc pair, then we might be
15035                        doing a GD->IE transition.  */
15036                     if (retval == 2)
15037                       {
15038                         tls_gd = TLS_GDIE;
15039                         if ((tls_mask & TLS_TLS) != 0
15040                             && (tls_mask & TLS_GD) == 0)
15041                           goto tls_ldgd_opt;
15042                       }
15043                     else if (retval == 3)
15044                       {
15045                         if ((tls_mask & TLS_TLS) != 0
15046                             && (tls_mask & TLS_LD) == 0)
15047                           goto tls_ldgd_opt;
15048                       }
15049                   }
15050               }
15051           }
15052           break;
15053
15054         case R_PPC64_GOT_TPREL16_HI:
15055         case R_PPC64_GOT_TPREL16_HA:
15056           if ((tls_mask & TLS_TLS) != 0
15057               && (tls_mask & TLS_TPREL) == 0)
15058             {
15059               rel->r_offset -= d_offset;
15060               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15061               r_type = R_PPC64_NONE;
15062               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15063             }
15064           break;
15065
15066         case R_PPC64_GOT_TPREL16_DS:
15067         case R_PPC64_GOT_TPREL16_LO_DS:
15068           if ((tls_mask & TLS_TLS) != 0
15069               && (tls_mask & TLS_TPREL) == 0)
15070             {
15071             toctprel:
15072               insn = bfd_get_32 (input_bfd,
15073                                  contents + rel->r_offset - d_offset);
15074               insn &= 31 << 21;
15075               insn |= 0x3c0d0000;       /* addis 0,13,0 */
15076               bfd_put_32 (input_bfd, insn,
15077                           contents + rel->r_offset - d_offset);
15078               r_type = R_PPC64_TPREL16_HA;
15079               if (toc_symndx != 0)
15080                 {
15081                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15082                   rel->r_addend = toc_addend;
15083                   /* We changed the symbol.  Start over in order to
15084                      get h, sym, sec etc. right.  */
15085                   goto again;
15086                 }
15087               else
15088                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15089             }
15090           break;
15091
15092         case R_PPC64_GOT_TPREL34:
15093           if ((tls_mask & TLS_TLS) != 0
15094               && (tls_mask & TLS_TPREL) == 0)
15095             {
15096               /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
15097               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15098               pinsn <<= 32;
15099               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15100               pinsn += ((2ULL << 56) + (-1ULL << 52)
15101                         + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15102               bfd_put_32 (input_bfd, pinsn >> 32,
15103                           contents + rel->r_offset);
15104               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15105                           contents + rel->r_offset + 4);
15106               r_type = R_PPC64_TPREL34;
15107               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15108             }
15109           break;
15110
15111         case R_PPC64_TLS:
15112           if ((tls_mask & TLS_TLS) != 0
15113               && (tls_mask & TLS_TPREL) == 0)
15114             {
15115               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15116               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15117               if (insn == 0)
15118                 break;
15119               if ((rel->r_offset & 3) == 0)
15120                 {
15121                   bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15122                   /* Was PPC64_TLS which sits on insn boundary, now
15123                      PPC64_TPREL16_LO which is at low-order half-word.  */
15124                   rel->r_offset += d_offset;
15125                   r_type = R_PPC64_TPREL16_LO;
15126                   if (toc_symndx != 0)
15127                     {
15128                       rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15129                       rel->r_addend = toc_addend;
15130                       /* We changed the symbol.  Start over in order to
15131                          get h, sym, sec etc. right.  */
15132                       goto again;
15133                     }
15134                   else
15135                     rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15136                 }
15137               else if ((rel->r_offset & 3) == 1)
15138                 {
15139                   /* For pcrel IE to LE we already have the full
15140                      offset and thus don't need an addi here.  A nop
15141                      or mr will do.  */
15142                   if ((insn & (0x3fu << 26)) == 14 << 26)
15143                     {
15144                       /* Extract regs from addi rt,ra,si.  */
15145                       unsigned int rt = (insn >> 21) & 0x1f;
15146                       unsigned int ra = (insn >> 16) & 0x1f;
15147                       if (rt == ra)
15148                         insn = NOP;
15149                       else
15150                         {
15151                           /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
15152                           insn = (rt << 16) | (ra << 21) | (ra << 11);
15153                           insn |= (31u << 26) | (444u << 1);
15154                         }
15155                     }
15156                   bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15157                 }
15158             }
15159           break;
15160
15161         case R_PPC64_GOT_TLSGD16_HI:
15162         case R_PPC64_GOT_TLSGD16_HA:
15163           tls_gd = TLS_GDIE;
15164           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15165             goto tls_gdld_hi;
15166           break;
15167
15168         case R_PPC64_GOT_TLSLD16_HI:
15169         case R_PPC64_GOT_TLSLD16_HA:
15170           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15171             {
15172             tls_gdld_hi:
15173               if ((tls_mask & tls_gd) != 0)
15174                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15175                           + R_PPC64_GOT_TPREL16_DS);
15176               else
15177                 {
15178                   rel->r_offset -= d_offset;
15179                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15180                   r_type = R_PPC64_NONE;
15181                 }
15182               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15183             }
15184           break;
15185
15186         case R_PPC64_GOT_TLSGD16:
15187         case R_PPC64_GOT_TLSGD16_LO:
15188           tls_gd = TLS_GDIE;
15189           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15190             goto tls_ldgd_opt;
15191           break;
15192
15193         case R_PPC64_GOT_TLSLD16:
15194         case R_PPC64_GOT_TLSLD16_LO:
15195           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15196             {
15197               unsigned int insn1, insn2;
15198
15199             tls_ldgd_opt:
15200               offset = (bfd_vma) -1;
15201               /* If not using the newer R_PPC64_TLSGD/LD to mark
15202                  __tls_get_addr calls, we must trust that the call
15203                  stays with its arg setup insns, ie. that the next
15204                  reloc is the __tls_get_addr call associated with
15205                  the current reloc.  Edit both insns.  */
15206               if (input_section->nomark_tls_get_addr
15207                   && rel + 1 < relend
15208                   && branch_reloc_hash_match (input_bfd, rel + 1,
15209                                               htab->tls_get_addr_fd,
15210                                               htab->tga_desc_fd,
15211                                               htab->tls_get_addr,
15212                                               htab->tga_desc))
15213                 offset = rel[1].r_offset;
15214               /* We read the low GOT_TLS (or TOC16) insn because we
15215                  need to keep the destination reg.  It may be
15216                  something other than the usual r3, and moved to r3
15217                  before the call by intervening code.  */
15218               insn1 = bfd_get_32 (input_bfd,
15219                                   contents + rel->r_offset - d_offset);
15220               if ((tls_mask & tls_gd) != 0)
15221                 {
15222                   /* IE */
15223                   insn1 &= (0x1f << 21) | (0x1f << 16);
15224                   insn1 |= 58u << 26;   /* ld */
15225                   insn2 = 0x7c636a14;   /* add 3,3,13 */
15226                   if (offset != (bfd_vma) -1)
15227                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15228                   if (r_type == R_PPC64_TOC16
15229                       || r_type == R_PPC64_TOC16_LO)
15230                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15231                   else
15232                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15233                               + R_PPC64_GOT_TPREL16_DS);
15234                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15235                 }
15236               else
15237                 {
15238                   /* LE */
15239                   insn1 &= 0x1f << 21;
15240                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
15241                   insn2 = 0x38630000;   /* addi 3,3,0 */
15242                   if (tls_gd == 0)
15243                     {
15244                       /* Was an LD reloc.  */
15245                       r_symndx = STN_UNDEF;
15246                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15247                     }
15248                   else if (toc_symndx != 0)
15249                     {
15250                       r_symndx = toc_symndx;
15251                       rel->r_addend = toc_addend;
15252                     }
15253                   r_type = R_PPC64_TPREL16_HA;
15254                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15255                   if (offset != (bfd_vma) -1)
15256                     {
15257                       rel[1].r_info = ELF64_R_INFO (r_symndx,
15258                                                     R_PPC64_TPREL16_LO);
15259                       rel[1].r_offset = offset + d_offset;
15260                       rel[1].r_addend = rel->r_addend;
15261                     }
15262                 }
15263               bfd_put_32 (input_bfd, insn1,
15264                           contents + rel->r_offset - d_offset);
15265               if (offset != (bfd_vma) -1)
15266                 {
15267                   bfd_put_32 (input_bfd, insn2, contents + offset);
15268                   if (offset + 8 <= input_section->size)
15269                     {
15270                       insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15271                       if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15272                         bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15273                     }
15274                 }
15275               if ((tls_mask & tls_gd) == 0
15276                   && (tls_gd == 0 || toc_symndx != 0))
15277                 {
15278                   /* We changed the symbol.  Start over in order
15279                      to get h, sym, sec etc. right.  */
15280                   goto again;
15281                 }
15282             }
15283           break;
15284
15285         case R_PPC64_GOT_TLSGD34:
15286           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15287             {
15288               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15289               pinsn <<= 32;
15290               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15291               if ((tls_mask & TLS_GDIE) != 0)
15292                 {
15293                   /* IE, pla -> pld  */
15294                   pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15295                   r_type = R_PPC64_GOT_TPREL34;
15296                 }
15297               else
15298                 {
15299                   /* LE, pla pcrel -> paddi r13  */
15300                   pinsn += (-1ULL << 52) + (13ULL << 16);
15301                   r_type = R_PPC64_TPREL34;
15302                 }
15303               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15304               bfd_put_32 (input_bfd, pinsn >> 32,
15305                           contents + rel->r_offset);
15306               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15307                           contents + rel->r_offset + 4);
15308             }
15309           break;
15310
15311         case R_PPC64_GOT_TLSLD34:
15312           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15313             {
15314               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15315               pinsn <<= 32;
15316               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15317               pinsn += (-1ULL << 52) + (13ULL << 16);
15318               bfd_put_32 (input_bfd, pinsn >> 32,
15319                           contents + rel->r_offset);
15320               bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15321                           contents + rel->r_offset + 4);
15322               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15323               r_symndx = STN_UNDEF;
15324               r_type = R_PPC64_TPREL34;
15325               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15326               goto again;
15327             }
15328           break;
15329
15330         case R_PPC64_TLSGD:
15331           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15332               && rel + 1 < relend)
15333             {
15334               unsigned int insn2;
15335               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15336
15337               offset = rel->r_offset;
15338               if (is_plt_seq_reloc (r_type1))
15339                 {
15340                   bfd_put_32 (output_bfd, NOP, contents + offset);
15341                   if (r_type1 == R_PPC64_PLT_PCREL34
15342                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15343                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15344                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15345                   break;
15346                 }
15347
15348               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15349                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15350
15351               if ((tls_mask & TLS_GDIE) != 0)
15352                 {
15353                   /* IE */
15354                   r_type = R_PPC64_NONE;
15355                   insn2 = 0x7c636a14;   /* add 3,3,13 */
15356                 }
15357               else
15358                 {
15359                   /* LE */
15360                   if (toc_symndx != 0)
15361                     {
15362                       r_symndx = toc_symndx;
15363                       rel->r_addend = toc_addend;
15364                     }
15365                   if (r_type1 == R_PPC64_REL24_NOTOC
15366                       || r_type1 == R_PPC64_PLTCALL_NOTOC)
15367                     {
15368                       r_type = R_PPC64_NONE;
15369                       insn2 = NOP;
15370                     }
15371                   else
15372                     {
15373                       rel->r_offset = offset + d_offset;
15374                       r_type = R_PPC64_TPREL16_LO;
15375                       insn2 = 0x38630000;       /* addi 3,3,0 */
15376                     }
15377                 }
15378               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15379               /* Zap the reloc on the _tls_get_addr call too.  */
15380               BFD_ASSERT (offset == rel[1].r_offset);
15381               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15382               bfd_put_32 (input_bfd, insn2, contents + offset);
15383               if ((tls_mask & TLS_GDIE) == 0
15384                   && toc_symndx != 0
15385                   && r_type != R_PPC64_NONE)
15386                 goto again;
15387             }
15388           break;
15389
15390         case R_PPC64_TLSLD:
15391           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15392               && rel + 1 < relend)
15393             {
15394               unsigned int insn2;
15395               enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15396
15397               offset = rel->r_offset;
15398               if (is_plt_seq_reloc (r_type1))
15399                 {
15400                   bfd_put_32 (output_bfd, NOP, contents + offset);
15401                   if (r_type1 == R_PPC64_PLT_PCREL34
15402                       || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15403                     bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15404                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15405                   break;
15406                 }
15407
15408               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15409                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15410
15411               if (r_type1 == R_PPC64_REL24_NOTOC
15412                   || r_type1 == R_PPC64_PLTCALL_NOTOC)
15413                 {
15414                   r_type = R_PPC64_NONE;
15415                   insn2 = NOP;
15416                 }
15417               else
15418                 {
15419                   rel->r_offset = offset + d_offset;
15420                   r_symndx = STN_UNDEF;
15421                   r_type = R_PPC64_TPREL16_LO;
15422                   rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15423                   insn2 = 0x38630000;   /* addi 3,3,0 */
15424                 }
15425               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15426               /* Zap the reloc on the _tls_get_addr call too.  */
15427               BFD_ASSERT (offset == rel[1].r_offset);
15428               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15429               bfd_put_32 (input_bfd, insn2, contents + offset);
15430               if (r_type != R_PPC64_NONE)
15431                 goto again;
15432             }
15433           break;
15434
15435         case R_PPC64_DTPMOD64:
15436           if (rel + 1 < relend
15437               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15438               && rel[1].r_offset == rel->r_offset + 8)
15439             {
15440               if ((tls_mask & TLS_GD) == 0)
15441                 {
15442                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15443                   if ((tls_mask & TLS_GDIE) != 0)
15444                     r_type = R_PPC64_TPREL64;
15445                   else
15446                     {
15447                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15448                       r_type = R_PPC64_NONE;
15449                     }
15450                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15451                 }
15452             }
15453           else
15454             {
15455               if ((tls_mask & TLS_LD) == 0)
15456                 {
15457                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15458                   r_type = R_PPC64_NONE;
15459                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15460                 }
15461             }
15462           break;
15463
15464         case R_PPC64_TPREL64:
15465           if ((tls_mask & TLS_TPREL) == 0)
15466             {
15467               r_type = R_PPC64_NONE;
15468               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15469             }
15470           break;
15471
15472         case R_PPC64_ENTRY:
15473           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15474           if (!bfd_link_pic (info)
15475               && !info->traditional_format
15476               && relocation + 0x80008000 <= 0xffffffff)
15477             {
15478               unsigned int insn1, insn2;
15479
15480               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15481               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15482               if ((insn1 & ~0xfffc) == LD_R2_0R12
15483                   && insn2 == ADD_R2_R2_R12)
15484                 {
15485                   bfd_put_32 (input_bfd,
15486                               LIS_R2 + PPC_HA (relocation),
15487                               contents + rel->r_offset);
15488                   bfd_put_32 (input_bfd,
15489                               ADDI_R2_R2 + PPC_LO (relocation),
15490                               contents + rel->r_offset + 4);
15491                 }
15492             }
15493           else
15494             {
15495               relocation -= (rel->r_offset
15496                              + input_section->output_offset
15497                              + input_section->output_section->vma);
15498               if (relocation + 0x80008000 <= 0xffffffff)
15499                 {
15500                   unsigned int insn1, insn2;
15501
15502                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15503                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15504                   if ((insn1 & ~0xfffc) == LD_R2_0R12
15505                       && insn2 == ADD_R2_R2_R12)
15506                     {
15507                       bfd_put_32 (input_bfd,
15508                                   ADDIS_R2_R12 + PPC_HA (relocation),
15509                                   contents + rel->r_offset);
15510                       bfd_put_32 (input_bfd,
15511                                   ADDI_R2_R2 + PPC_LO (relocation),
15512                                   contents + rel->r_offset + 4);
15513                     }
15514                 }
15515             }
15516           break;
15517
15518         case R_PPC64_REL16_HA:
15519           /* If we are generating a non-PIC executable, edit
15520              .  0:      addis 2,12,.TOC.-0b@ha
15521              .          addi 2,2,.TOC.-0b@l
15522              used by ELFv2 global entry points to set up r2, to
15523              .          lis 2,.TOC.@ha
15524              .          addi 2,2,.TOC.@l
15525              if .TOC. is in range.  */
15526           if (!bfd_link_pic (info)
15527               && !info->traditional_format
15528               && !htab->opd_abi
15529               && rel->r_addend == d_offset
15530               && h != NULL && &h->elf == htab->elf.hgot
15531               && rel + 1 < relend
15532               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15533               && rel[1].r_offset == rel->r_offset + 4
15534               && rel[1].r_addend == rel->r_addend + 4
15535               && relocation + 0x80008000 <= 0xffffffff)
15536             {
15537               unsigned int insn1, insn2;
15538               offset = rel->r_offset - d_offset;
15539               insn1 = bfd_get_32 (input_bfd, contents + offset);
15540               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15541               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15542                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
15543                 {
15544                   r_type = R_PPC64_ADDR16_HA;
15545                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15546                   rel->r_addend -= d_offset;
15547                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15548                   rel[1].r_addend -= d_offset + 4;
15549                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15550                 }
15551             }
15552           break;
15553         }
15554
15555       /* Handle other relocations that tweak non-addend part of insn.  */
15556       insn = 0;
15557       max_br_offset = 1 << 25;
15558       addend = rel->r_addend;
15559       reloc_dest = DEST_NORMAL;
15560       switch (r_type)
15561         {
15562         default:
15563           break;
15564
15565         case R_PPC64_TOCSAVE:
15566           if (relocation + addend == (rel->r_offset
15567                                       + input_section->output_offset
15568                                       + input_section->output_section->vma)
15569               && tocsave_find (htab, NO_INSERT,
15570                                &local_syms, rel, input_bfd))
15571             {
15572               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15573               if (insn == NOP
15574                   || insn == CROR_151515 || insn == CROR_313131)
15575                 bfd_put_32 (input_bfd,
15576                             STD_R2_0R1 + STK_TOC (htab),
15577                             contents + rel->r_offset);
15578             }
15579           break;
15580
15581           /* Branch taken prediction relocations.  */
15582         case R_PPC64_ADDR14_BRTAKEN:
15583         case R_PPC64_REL14_BRTAKEN:
15584           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
15585           /* Fall through.  */
15586
15587           /* Branch not taken prediction relocations.  */
15588         case R_PPC64_ADDR14_BRNTAKEN:
15589         case R_PPC64_REL14_BRNTAKEN:
15590           insn |= bfd_get_32 (input_bfd,
15591                               contents + rel->r_offset) & ~(0x01 << 21);
15592           /* Fall through.  */
15593
15594         case R_PPC64_REL14:
15595           max_br_offset = 1 << 15;
15596           /* Fall through.  */
15597
15598         case R_PPC64_REL24:
15599         case R_PPC64_REL24_NOTOC:
15600         case R_PPC64_PLTCALL:
15601         case R_PPC64_PLTCALL_NOTOC:
15602           /* Calls to functions with a different TOC, such as calls to
15603              shared objects, need to alter the TOC pointer.  This is
15604              done using a linkage stub.  A REL24 branching to these
15605              linkage stubs needs to be followed by a nop, as the nop
15606              will be replaced with an instruction to restore the TOC
15607              base pointer.  */
15608           fdh = h;
15609           if (h != NULL
15610               && h->oh != NULL
15611               && h->oh->is_func_descriptor)
15612             fdh = ppc_follow_link (h->oh);
15613           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15614                                            htab);
15615           if ((r_type == R_PPC64_PLTCALL
15616                || r_type == R_PPC64_PLTCALL_NOTOC)
15617               && stub_entry != NULL
15618               && stub_entry->stub_type >= ppc_stub_plt_call
15619               && stub_entry->stub_type <= ppc_stub_plt_call_both)
15620             stub_entry = NULL;
15621
15622           if (stub_entry != NULL
15623               && ((stub_entry->stub_type >= ppc_stub_plt_call
15624                    && stub_entry->stub_type <= ppc_stub_plt_call_both)
15625                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15626                   || stub_entry->stub_type == ppc_stub_plt_branch_both
15627                   || stub_entry->stub_type == ppc_stub_long_branch_r2off
15628                   || stub_entry->stub_type == ppc_stub_long_branch_both))
15629             {
15630               bfd_boolean can_plt_call = FALSE;
15631
15632               if (stub_entry->stub_type == ppc_stub_plt_call
15633                   && !htab->opd_abi
15634                   && htab->params->plt_localentry0 != 0
15635                   && is_elfv2_localentry0 (&h->elf))
15636                 {
15637                   /* The function doesn't use or change r2.  */
15638                   can_plt_call = TRUE;
15639                 }
15640               else if (r_type == R_PPC64_REL24_NOTOC)
15641                 {
15642                   /* NOTOC calls don't need to restore r2.  */
15643                   can_plt_call = TRUE;
15644                 }
15645
15646               /* All of these stubs may modify r2, so there must be a
15647                  branch and link followed by a nop.  The nop is
15648                  replaced by an insn to restore r2.  */
15649               else if (rel->r_offset + 8 <= input_section->size)
15650                 {
15651                   unsigned long br;
15652
15653                   br = bfd_get_32 (input_bfd,
15654                                    contents + rel->r_offset);
15655                   if ((br & 1) != 0)
15656                     {
15657                       unsigned long nop;
15658
15659                       nop = bfd_get_32 (input_bfd,
15660                                         contents + rel->r_offset + 4);
15661                       if (nop == LD_R2_0R1 + STK_TOC (htab))
15662                         can_plt_call = TRUE;
15663                       else if (nop == NOP
15664                                || nop == CROR_151515
15665                                || nop == CROR_313131)
15666                         {
15667                           if (h != NULL
15668                               && is_tls_get_addr (&h->elf, htab)
15669                               && htab->params->tls_get_addr_opt)
15670                             {
15671                               /* Special stub used, leave nop alone.  */
15672                             }
15673                           else
15674                             bfd_put_32 (input_bfd,
15675                                         LD_R2_0R1 + STK_TOC (htab),
15676                                         contents + rel->r_offset + 4);
15677                           can_plt_call = TRUE;
15678                         }
15679                     }
15680                 }
15681
15682               if (!can_plt_call && h != NULL)
15683                 {
15684                   const char *name = h->elf.root.root.string;
15685
15686                   if (*name == '.')
15687                     ++name;
15688
15689                   if (strncmp (name, "__libc_start_main", 17) == 0
15690                       && (name[17] == 0 || name[17] == '@'))
15691                     {
15692                       /* Allow crt1 branch to go via a toc adjusting
15693                          stub.  Other calls that never return could do
15694                          the same, if we could detect such.  */
15695                       can_plt_call = TRUE;
15696                     }
15697                 }
15698
15699               if (!can_plt_call)
15700                 {
15701                   /* g++ as of 20130507 emits self-calls without a
15702                      following nop.  This is arguably wrong since we
15703                      have conflicting information.  On the one hand a
15704                      global symbol and on the other a local call
15705                      sequence, but don't error for this special case.
15706                      It isn't possible to cheaply verify we have
15707                      exactly such a call.  Allow all calls to the same
15708                      section.  */
15709                   asection *code_sec = sec;
15710
15711                   if (get_opd_info (sec) != NULL)
15712                     {
15713                       bfd_vma off = (relocation + addend
15714                                      - sec->output_section->vma
15715                                      - sec->output_offset);
15716
15717                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15718                     }
15719                   if (code_sec == input_section)
15720                     can_plt_call = TRUE;
15721                 }
15722
15723               if (!can_plt_call)
15724                 {
15725                   if (stub_entry->stub_type >= ppc_stub_plt_call
15726                       && stub_entry->stub_type <= ppc_stub_plt_call_both)
15727                     info->callbacks->einfo
15728                       /* xgettext:c-format */
15729                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
15730                          "(plt call stub)\n"),
15731                        input_bfd, input_section, rel->r_offset, sym_name);
15732                   else
15733                     info->callbacks->einfo
15734                       /* xgettext:c-format */
15735                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
15736                          "(toc save/adjust stub)\n"),
15737                        input_bfd, input_section, rel->r_offset, sym_name);
15738
15739                   bfd_set_error (bfd_error_bad_value);
15740                   ret = FALSE;
15741                 }
15742
15743               if (can_plt_call
15744                   && stub_entry->stub_type >= ppc_stub_plt_call
15745                   && stub_entry->stub_type <= ppc_stub_plt_call_both)
15746                 unresolved_reloc = FALSE;
15747             }
15748
15749           if ((stub_entry == NULL
15750                || stub_entry->stub_type == ppc_stub_long_branch
15751                || stub_entry->stub_type == ppc_stub_plt_branch)
15752               && get_opd_info (sec) != NULL)
15753             {
15754               /* The branch destination is the value of the opd entry. */
15755               bfd_vma off = (relocation + addend
15756                              - sec->output_section->vma
15757                              - sec->output_offset);
15758               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15759               if (dest != (bfd_vma) -1)
15760                 {
15761                   relocation = dest;
15762                   addend = 0;
15763                   reloc_dest = DEST_OPD;
15764                 }
15765             }
15766
15767           /* If the branch is out of reach we ought to have a long
15768              branch stub.  */
15769           from = (rel->r_offset
15770                   + input_section->output_offset
15771                   + input_section->output_section->vma);
15772
15773           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15774                                                   ? fdh->elf.other
15775                                                   : sym->st_other);
15776
15777           if (stub_entry != NULL
15778               && (stub_entry->stub_type == ppc_stub_long_branch
15779                   || stub_entry->stub_type == ppc_stub_plt_branch)
15780               && (r_type == R_PPC64_ADDR14_BRTAKEN
15781                   || r_type == R_PPC64_ADDR14_BRNTAKEN
15782                   || (relocation + addend - from + max_br_offset
15783                       < 2 * max_br_offset)))
15784             /* Don't use the stub if this branch is in range.  */
15785             stub_entry = NULL;
15786
15787           if (stub_entry != NULL
15788               && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15789                   || stub_entry->stub_type == ppc_stub_long_branch_both
15790                   || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15791                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
15792               && (r_type != R_PPC64_REL24_NOTOC
15793                   || ((fdh ? fdh->elf.other : sym->st_other)
15794                       & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15795               && (relocation + addend - from + max_br_offset
15796                   < 2 * max_br_offset))
15797             stub_entry = NULL;
15798
15799           if (stub_entry != NULL
15800               && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15801                   || stub_entry->stub_type == ppc_stub_long_branch_both
15802                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15803                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
15804               && r_type == R_PPC64_REL24_NOTOC
15805               && (relocation + addend - from + max_br_offset
15806                   < 2 * max_br_offset))
15807             stub_entry = NULL;
15808
15809           if (stub_entry != NULL)
15810             {
15811               /* Munge up the value and addend so that we call the stub
15812                  rather than the procedure directly.  */
15813               asection *stub_sec = stub_entry->group->stub_sec;
15814
15815               if (stub_entry->stub_type == ppc_stub_save_res)
15816                 relocation += (stub_sec->output_offset
15817                                + stub_sec->output_section->vma
15818                                + stub_sec->size - htab->sfpr->size
15819                                - htab->sfpr->output_offset
15820                                - htab->sfpr->output_section->vma);
15821               else
15822                 relocation = (stub_entry->stub_offset
15823                               + stub_sec->output_offset
15824                               + stub_sec->output_section->vma);
15825               addend = 0;
15826               reloc_dest = DEST_STUB;
15827
15828               if (((stub_entry->stub_type == ppc_stub_plt_call
15829                     && ALWAYS_EMIT_R2SAVE)
15830                    || stub_entry->stub_type == ppc_stub_plt_call_r2save
15831                    || stub_entry->stub_type == ppc_stub_plt_call_both)
15832                   && !(h != NULL
15833                        && is_tls_get_addr (&h->elf, htab)
15834                        && htab->params->tls_get_addr_opt)
15835                   && rel + 1 < relend
15836                   && rel[1].r_offset == rel->r_offset + 4
15837                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15838                 relocation += 4;
15839               else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15840                         || stub_entry->stub_type == ppc_stub_plt_branch_both
15841                         || stub_entry->stub_type == ppc_stub_plt_call_both)
15842                        && r_type == R_PPC64_REL24_NOTOC)
15843                 relocation += 4;
15844
15845               if (r_type == R_PPC64_REL24_NOTOC
15846                   && (stub_entry->stub_type == ppc_stub_plt_call_notoc
15847                       || stub_entry->stub_type == ppc_stub_plt_call_both))
15848                 htab->notoc_plt = 1;
15849             }
15850
15851           if (insn != 0)
15852             {
15853               if (is_isa_v2)
15854                 {
15855                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
15856                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
15857                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
15858                   if ((insn & (0x14 << 21)) == (0x04 << 21))
15859                     insn |= 0x02 << 21;
15860                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
15861                     insn |= 0x08 << 21;
15862                   else
15863                     break;
15864                 }
15865               else
15866                 {
15867                   /* Invert 'y' bit if not the default.  */
15868                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
15869                     insn ^= 0x01 << 21;
15870                 }
15871
15872               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15873             }
15874
15875           /* NOP out calls to undefined weak functions.
15876              We can thus call a weak function without first
15877              checking whether the function is defined.  */
15878           else if (h != NULL
15879                    && h->elf.root.type == bfd_link_hash_undefweak
15880                    && h->elf.dynindx == -1
15881                    && (r_type == R_PPC64_REL24
15882                        || r_type == R_PPC64_REL24_NOTOC)
15883                    && relocation == 0
15884                    && addend == 0)
15885             {
15886               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15887               goto copy_reloc;
15888             }
15889           break;
15890
15891         case R_PPC64_GOT16_DS:
15892           if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
15893             break;
15894           from = TOCstart + htab->sec_info[input_section->id].toc_off;
15895           if (relocation + addend - from + 0x8000 < 0x10000
15896               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15897             {
15898               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15899               if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
15900                 {
15901                   insn += (14u << 26) - (58u << 26);
15902                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15903                   r_type = R_PPC64_TOC16;
15904                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15905                 }
15906             }
15907           break;
15908
15909         case R_PPC64_GOT16_LO_DS:
15910         case R_PPC64_GOT16_HA:
15911           if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
15912             break;
15913           from = TOCstart + htab->sec_info[input_section->id].toc_off;
15914           if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
15915               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15916             {
15917               insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15918               if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
15919                 {
15920                   insn += (14u << 26) - (58u << 26);
15921                   bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
15922                   r_type = R_PPC64_TOC16_LO;
15923                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15924                 }
15925               else if ((insn & (0x3fu << 26)) == 15u << 26 /* addis */)
15926                 {
15927                   r_type = R_PPC64_TOC16_HA;
15928                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15929                 }
15930             }
15931           break;
15932
15933         case R_PPC64_GOT_PCREL34:
15934           if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
15935             break;
15936           from = (rel->r_offset
15937                   + input_section->output_section->vma
15938                   + input_section->output_offset);
15939           if (relocation - from + (1ULL << 33) < 1ULL << 34
15940               && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15941             {
15942               offset = rel->r_offset;
15943               pinsn = bfd_get_32 (input_bfd, contents + offset);
15944               pinsn <<= 32;
15945               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15946               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15947                    == ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
15948                 {
15949                   /* Replace with paddi.  */
15950                   pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
15951                   r_type = R_PPC64_PCREL34;
15952                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15953                   bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
15954                   bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
15955                   goto pcrelopt;
15956                 }
15957             }
15958           break;
15959
15960         case R_PPC64_PCREL34:
15961           if (SYMBOL_REFERENCES_LOCAL (info, &h->elf))
15962             {
15963               offset = rel->r_offset;
15964               pinsn = bfd_get_32 (input_bfd, contents + offset);
15965               pinsn <<= 32;
15966               pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
15967               if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
15968                    == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
15969                        | (14ULL << 26) /* paddi */))
15970                 {
15971                 pcrelopt:
15972                   if (rel + 1 < relend
15973                       && rel[1].r_offset == offset
15974                       && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT))
15975                     {
15976                       bfd_vma off2 = rel[1].r_addend;
15977                       if (off2 == 0)
15978                         /* zero means next insn.  */
15979                         off2 = 8;
15980                       off2 += offset;
15981                       if (off2 + 4 <= input_section->size)
15982                         {
15983                           uint64_t pinsn2;
15984                           bfd_signed_vma addend_off;
15985                           pinsn2 = bfd_get_32 (input_bfd, contents + off2);
15986                           pinsn2 <<= 32;
15987                           if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
15988                             {
15989                               if (off2 + 8 > input_section->size)
15990                                 break;
15991                               pinsn2 |= bfd_get_32 (input_bfd,
15992                                                     contents + off2 + 4);
15993                             }
15994                           if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
15995                             {
15996                               addend += addend_off;
15997                               rel->r_addend = addend;
15998                               bfd_put_32 (input_bfd, pinsn >> 32,
15999                                           contents + offset);
16000                               bfd_put_32 (input_bfd, pinsn,
16001                                           contents + offset + 4);
16002                               bfd_put_32 (input_bfd, pinsn2 >> 32,
16003                                           contents + off2);
16004                               if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16005                                 bfd_put_32 (input_bfd, pinsn2,
16006                                             contents + off2 + 4);
16007                             }
16008                         }
16009                     }
16010                 }
16011             }
16012           break;
16013         }
16014
16015       tls_type = 0;
16016       save_unresolved_reloc = unresolved_reloc;
16017       switch (r_type)
16018         {
16019         default:
16020           /* xgettext:c-format */
16021           _bfd_error_handler (_("%pB: %s unsupported"),
16022                               input_bfd, ppc64_elf_howto_table[r_type]->name);
16023
16024           bfd_set_error (bfd_error_bad_value);
16025           ret = FALSE;
16026           goto copy_reloc;
16027
16028         case R_PPC64_NONE:
16029         case R_PPC64_TLS:
16030         case R_PPC64_TLSGD:
16031         case R_PPC64_TLSLD:
16032         case R_PPC64_TOCSAVE:
16033         case R_PPC64_GNU_VTINHERIT:
16034         case R_PPC64_GNU_VTENTRY:
16035         case R_PPC64_ENTRY:
16036         case R_PPC64_PCREL_OPT:
16037           goto copy_reloc;
16038
16039           /* GOT16 relocations.  Like an ADDR16 using the symbol's
16040              address in the GOT as relocation value instead of the
16041              symbol's value itself.  Also, create a GOT entry for the
16042              symbol and put the symbol value there.  */
16043         case R_PPC64_GOT_TLSGD16:
16044         case R_PPC64_GOT_TLSGD16_LO:
16045         case R_PPC64_GOT_TLSGD16_HI:
16046         case R_PPC64_GOT_TLSGD16_HA:
16047         case R_PPC64_GOT_TLSGD34:
16048           tls_type = TLS_TLS | TLS_GD;
16049           goto dogot;
16050
16051         case R_PPC64_GOT_TLSLD16:
16052         case R_PPC64_GOT_TLSLD16_LO:
16053         case R_PPC64_GOT_TLSLD16_HI:
16054         case R_PPC64_GOT_TLSLD16_HA:
16055         case R_PPC64_GOT_TLSLD34:
16056           tls_type = TLS_TLS | TLS_LD;
16057           goto dogot;
16058
16059         case R_PPC64_GOT_TPREL16_DS:
16060         case R_PPC64_GOT_TPREL16_LO_DS:
16061         case R_PPC64_GOT_TPREL16_HI:
16062         case R_PPC64_GOT_TPREL16_HA:
16063         case R_PPC64_GOT_TPREL34:
16064           tls_type = TLS_TLS | TLS_TPREL;
16065           goto dogot;
16066
16067         case R_PPC64_GOT_DTPREL16_DS:
16068         case R_PPC64_GOT_DTPREL16_LO_DS:
16069         case R_PPC64_GOT_DTPREL16_HI:
16070         case R_PPC64_GOT_DTPREL16_HA:
16071         case R_PPC64_GOT_DTPREL34:
16072           tls_type = TLS_TLS | TLS_DTPREL;
16073           goto dogot;
16074
16075         case R_PPC64_GOT16:
16076         case R_PPC64_GOT16_LO:
16077         case R_PPC64_GOT16_HI:
16078         case R_PPC64_GOT16_HA:
16079         case R_PPC64_GOT16_DS:
16080         case R_PPC64_GOT16_LO_DS:
16081         case R_PPC64_GOT_PCREL34:
16082         dogot:
16083           {
16084             /* Relocation is to the entry for this symbol in the global
16085                offset table.  */
16086             asection *got;
16087             bfd_vma *offp;
16088             bfd_vma off;
16089             unsigned long indx = 0;
16090             struct got_entry *ent;
16091
16092             if (tls_type == (TLS_TLS | TLS_LD)
16093                 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16094               ent = ppc64_tlsld_got (input_bfd);
16095             else
16096               {
16097                 if (h != NULL)
16098                   {
16099                     if (!htab->elf.dynamic_sections_created
16100                         || h->elf.dynindx == -1
16101                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16102                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16103                       /* This is actually a static link, or it is a
16104                          -Bsymbolic link and the symbol is defined
16105                          locally, or the symbol was forced to be local
16106                          because of a version file.  */
16107                       ;
16108                     else
16109                       {
16110                         indx = h->elf.dynindx;
16111                         unresolved_reloc = FALSE;
16112                       }
16113                     ent = h->elf.got.glist;
16114                   }
16115                 else
16116                   {
16117                     if (local_got_ents == NULL)
16118                       abort ();
16119                     ent = local_got_ents[r_symndx];
16120                   }
16121
16122                 for (; ent != NULL; ent = ent->next)
16123                   if (ent->addend == orig_rel.r_addend
16124                       && ent->owner == input_bfd
16125                       && ent->tls_type == tls_type)
16126                     break;
16127               }
16128
16129             if (ent == NULL)
16130               abort ();
16131             if (ent->is_indirect)
16132               ent = ent->got.ent;
16133             offp = &ent->got.offset;
16134             got = ppc64_elf_tdata (ent->owner)->got;
16135             if (got == NULL)
16136               abort ();
16137
16138             /* The offset must always be a multiple of 8.  We use the
16139                least significant bit to record whether we have already
16140                processed this entry.  */
16141             off = *offp;
16142             if ((off & 1) != 0)
16143               off &= ~1;
16144             else
16145               {
16146                 /* Generate relocs for the dynamic linker, except in
16147                    the case of TLSLD where we'll use one entry per
16148                    module.  */
16149                 asection *relgot;
16150                 bfd_boolean ifunc;
16151
16152                 *offp = off | 1;
16153                 relgot = NULL;
16154                 ifunc = (h != NULL
16155                          ? h->elf.type == STT_GNU_IFUNC
16156                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16157                 if (ifunc)
16158                   {
16159                     relgot = htab->elf.irelplt;
16160                     if (indx == 0)
16161                       htab->local_ifunc_resolver = 1;
16162                     else if (is_static_defined (&h->elf))
16163                       htab->maybe_local_ifunc_resolver = 1;
16164                   }
16165                 else if (indx != 0
16166                          || (bfd_link_pic (info)
16167                              && (h == NULL
16168                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16169                              && !(tls_type != 0
16170                                   && bfd_link_executable (info)
16171                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
16172                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
16173                 if (relgot != NULL)
16174                   {
16175                     outrel.r_offset = (got->output_section->vma
16176                                        + got->output_offset
16177                                        + off);
16178                     outrel.r_addend = orig_rel.r_addend;
16179                     if (tls_type & (TLS_LD | TLS_GD))
16180                       {
16181                         outrel.r_addend = 0;
16182                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16183                         if (tls_type == (TLS_TLS | TLS_GD))
16184                           {
16185                             loc = relgot->contents;
16186                             loc += (relgot->reloc_count++
16187                                     * sizeof (Elf64_External_Rela));
16188                             bfd_elf64_swap_reloca_out (output_bfd,
16189                                                        &outrel, loc);
16190                             outrel.r_offset += 8;
16191                             outrel.r_addend = orig_rel.r_addend;
16192                             outrel.r_info
16193                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16194                           }
16195                       }
16196                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
16197                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16198                     else if (tls_type == (TLS_TLS | TLS_TPREL))
16199                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16200                     else if (indx != 0)
16201                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16202                     else
16203                       {
16204                         if (ifunc)
16205                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16206                         else
16207                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16208
16209                         /* Write the .got section contents for the sake
16210                            of prelink.  */
16211                         loc = got->contents + off;
16212                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16213                                     loc);
16214                       }
16215
16216                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16217                       {
16218                         outrel.r_addend += relocation;
16219                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16220                           {
16221                             if (htab->elf.tls_sec == NULL)
16222                               outrel.r_addend = 0;
16223                             else
16224                               outrel.r_addend -= htab->elf.tls_sec->vma;
16225                           }
16226                       }
16227                     loc = relgot->contents;
16228                     loc += (relgot->reloc_count++
16229                             * sizeof (Elf64_External_Rela));
16230                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16231                   }
16232
16233                 /* Init the .got section contents here if we're not
16234                    emitting a reloc.  */
16235                 else
16236                   {
16237                     relocation += orig_rel.r_addend;
16238                     if (tls_type != 0)
16239                       {
16240                         if (htab->elf.tls_sec == NULL)
16241                           relocation = 0;
16242                         else
16243                           {
16244                             if (tls_type & TLS_LD)
16245                               relocation = 0;
16246                             else
16247                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16248                             if (tls_type & TLS_TPREL)
16249                               relocation += DTP_OFFSET - TP_OFFSET;
16250                           }
16251
16252                         if (tls_type & (TLS_GD | TLS_LD))
16253                           {
16254                             bfd_put_64 (output_bfd, relocation,
16255                                         got->contents + off + 8);
16256                             relocation = 1;
16257                           }
16258                       }
16259                     bfd_put_64 (output_bfd, relocation,
16260                                 got->contents + off);
16261                   }
16262               }
16263
16264             if (off >= (bfd_vma) -2)
16265               abort ();
16266
16267             relocation = got->output_section->vma + got->output_offset + off;
16268             addend = 0;
16269             if (!(r_type == R_PPC64_GOT_PCREL34
16270                   || r_type == R_PPC64_GOT_TLSGD34
16271                   || r_type == R_PPC64_GOT_TLSLD34
16272                   || r_type == R_PPC64_GOT_TPREL34
16273                   || r_type == R_PPC64_GOT_DTPREL34))
16274               addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16275           }
16276           break;
16277
16278         case R_PPC64_PLT16_HA:
16279         case R_PPC64_PLT16_HI:
16280         case R_PPC64_PLT16_LO:
16281         case R_PPC64_PLT16_LO_DS:
16282         case R_PPC64_PLT_PCREL34:
16283         case R_PPC64_PLT_PCREL34_NOTOC:
16284         case R_PPC64_PLT32:
16285         case R_PPC64_PLT64:
16286         case R_PPC64_PLTSEQ:
16287         case R_PPC64_PLTSEQ_NOTOC:
16288         case R_PPC64_PLTCALL:
16289         case R_PPC64_PLTCALL_NOTOC:
16290           /* Relocation is to the entry for this symbol in the
16291              procedure linkage table.  */
16292           unresolved_reloc = TRUE;
16293           {
16294             struct plt_entry **plt_list = NULL;
16295             if (h != NULL)
16296               plt_list = &h->elf.plt.plist;
16297             else if (local_got_ents != NULL)
16298               {
16299                 struct plt_entry **local_plt = (struct plt_entry **)
16300                   (local_got_ents + symtab_hdr->sh_info);
16301                 plt_list = local_plt + r_symndx;
16302               }
16303             if (plt_list)
16304               {
16305                 struct plt_entry *ent;
16306
16307                 for (ent = *plt_list; ent != NULL; ent = ent->next)
16308                   if (ent->plt.offset != (bfd_vma) -1
16309                       && ent->addend == orig_rel.r_addend)
16310                     {
16311                       asection *plt;
16312                       bfd_vma got;
16313
16314                       plt = htab->elf.splt;
16315                       if (!htab->elf.dynamic_sections_created
16316                           || h == NULL
16317                           || h->elf.dynindx == -1)
16318                         {
16319                           if (h != NULL
16320                               ? h->elf.type == STT_GNU_IFUNC
16321                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16322                             plt = htab->elf.iplt;
16323                           else
16324                             plt = htab->pltlocal;
16325                         }
16326                       relocation = (plt->output_section->vma
16327                                     + plt->output_offset
16328                                     + ent->plt.offset);
16329                       if (r_type == R_PPC64_PLT16_HA
16330                           || r_type == R_PPC64_PLT16_HI
16331                           || r_type == R_PPC64_PLT16_LO
16332                           || r_type == R_PPC64_PLT16_LO_DS)
16333                         {
16334                           got = (elf_gp (output_bfd)
16335                                  + htab->sec_info[input_section->id].toc_off);
16336                           relocation -= got;
16337                         }
16338                       addend = 0;
16339                       unresolved_reloc = FALSE;
16340                       break;
16341                     }
16342               }
16343           }
16344           break;
16345
16346         case R_PPC64_TOC:
16347           /* Relocation value is TOC base.  */
16348           relocation = TOCstart;
16349           if (r_symndx == STN_UNDEF)
16350             relocation += htab->sec_info[input_section->id].toc_off;
16351           else if (unresolved_reloc)
16352             ;
16353           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16354             relocation += htab->sec_info[sec->id].toc_off;
16355           else
16356             unresolved_reloc = TRUE;
16357           goto dodyn;
16358
16359           /* TOC16 relocs.  We want the offset relative to the TOC base,
16360              which is the address of the start of the TOC plus 0x8000.
16361              The TOC consists of sections .got, .toc, .tocbss, and .plt,
16362              in this order.  */
16363         case R_PPC64_TOC16:
16364         case R_PPC64_TOC16_LO:
16365         case R_PPC64_TOC16_HI:
16366         case R_PPC64_TOC16_DS:
16367         case R_PPC64_TOC16_LO_DS:
16368         case R_PPC64_TOC16_HA:
16369           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
16370           if (h != NULL)
16371             goto dodyn;
16372           break;
16373
16374           /* Relocate against the beginning of the section.  */
16375         case R_PPC64_SECTOFF:
16376         case R_PPC64_SECTOFF_LO:
16377         case R_PPC64_SECTOFF_HI:
16378         case R_PPC64_SECTOFF_DS:
16379         case R_PPC64_SECTOFF_LO_DS:
16380         case R_PPC64_SECTOFF_HA:
16381           if (sec != NULL)
16382             addend -= sec->output_section->vma;
16383           break;
16384
16385         case R_PPC64_REL16:
16386         case R_PPC64_REL16_LO:
16387         case R_PPC64_REL16_HI:
16388         case R_PPC64_REL16_HA:
16389         case R_PPC64_REL16_HIGH:
16390         case R_PPC64_REL16_HIGHA:
16391         case R_PPC64_REL16_HIGHER:
16392         case R_PPC64_REL16_HIGHERA:
16393         case R_PPC64_REL16_HIGHEST:
16394         case R_PPC64_REL16_HIGHESTA:
16395         case R_PPC64_REL16_HIGHER34:
16396         case R_PPC64_REL16_HIGHERA34:
16397         case R_PPC64_REL16_HIGHEST34:
16398         case R_PPC64_REL16_HIGHESTA34:
16399         case R_PPC64_REL16DX_HA:
16400         case R_PPC64_REL14:
16401         case R_PPC64_REL14_BRNTAKEN:
16402         case R_PPC64_REL14_BRTAKEN:
16403         case R_PPC64_REL24:
16404         case R_PPC64_REL24_NOTOC:
16405         case R_PPC64_PCREL34:
16406         case R_PPC64_PCREL28:
16407           break;
16408
16409         case R_PPC64_TPREL16:
16410         case R_PPC64_TPREL16_LO:
16411         case R_PPC64_TPREL16_HI:
16412         case R_PPC64_TPREL16_HA:
16413         case R_PPC64_TPREL16_DS:
16414         case R_PPC64_TPREL16_LO_DS:
16415         case R_PPC64_TPREL16_HIGH:
16416         case R_PPC64_TPREL16_HIGHA:
16417         case R_PPC64_TPREL16_HIGHER:
16418         case R_PPC64_TPREL16_HIGHERA:
16419         case R_PPC64_TPREL16_HIGHEST:
16420         case R_PPC64_TPREL16_HIGHESTA:
16421         case R_PPC64_TPREL34:
16422           if (h != NULL
16423               && h->elf.root.type == bfd_link_hash_undefweak
16424               && h->elf.dynindx == -1)
16425             {
16426               /* Make this relocation against an undefined weak symbol
16427                  resolve to zero.  This is really just a tweak, since
16428                  code using weak externs ought to check that they are
16429                  defined before using them.  */
16430               bfd_byte *p = contents + rel->r_offset - d_offset;
16431
16432               insn = bfd_get_32 (input_bfd, p);
16433               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16434               if (insn != 0)
16435                 bfd_put_32 (input_bfd, insn, p);
16436               break;
16437             }
16438           if (htab->elf.tls_sec != NULL)
16439             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16440           /* The TPREL16 relocs shouldn't really be used in shared
16441              libs or with non-local symbols as that will result in
16442              DT_TEXTREL being set, but support them anyway.  */
16443           goto dodyn;
16444
16445         case R_PPC64_DTPREL16:
16446         case R_PPC64_DTPREL16_LO:
16447         case R_PPC64_DTPREL16_HI:
16448         case R_PPC64_DTPREL16_HA:
16449         case R_PPC64_DTPREL16_DS:
16450         case R_PPC64_DTPREL16_LO_DS:
16451         case R_PPC64_DTPREL16_HIGH:
16452         case R_PPC64_DTPREL16_HIGHA:
16453         case R_PPC64_DTPREL16_HIGHER:
16454         case R_PPC64_DTPREL16_HIGHERA:
16455         case R_PPC64_DTPREL16_HIGHEST:
16456         case R_PPC64_DTPREL16_HIGHESTA:
16457         case R_PPC64_DTPREL34:
16458           if (htab->elf.tls_sec != NULL)
16459             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16460           break;
16461
16462         case R_PPC64_ADDR64_LOCAL:
16463           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16464                                               ? h->elf.other
16465                                               : sym->st_other);
16466           break;
16467
16468         case R_PPC64_DTPMOD64:
16469           relocation = 1;
16470           addend = 0;
16471           goto dodyn;
16472
16473         case R_PPC64_TPREL64:
16474           if (htab->elf.tls_sec != NULL)
16475             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16476           goto dodyn;
16477
16478         case R_PPC64_DTPREL64:
16479           if (htab->elf.tls_sec != NULL)
16480             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16481           /* Fall through.  */
16482
16483           /* Relocations that may need to be propagated if this is a
16484              dynamic object.  */
16485         case R_PPC64_REL30:
16486         case R_PPC64_REL32:
16487         case R_PPC64_REL64:
16488         case R_PPC64_ADDR14:
16489         case R_PPC64_ADDR14_BRNTAKEN:
16490         case R_PPC64_ADDR14_BRTAKEN:
16491         case R_PPC64_ADDR16:
16492         case R_PPC64_ADDR16_DS:
16493         case R_PPC64_ADDR16_HA:
16494         case R_PPC64_ADDR16_HI:
16495         case R_PPC64_ADDR16_HIGH:
16496         case R_PPC64_ADDR16_HIGHA:
16497         case R_PPC64_ADDR16_HIGHER:
16498         case R_PPC64_ADDR16_HIGHERA:
16499         case R_PPC64_ADDR16_HIGHEST:
16500         case R_PPC64_ADDR16_HIGHESTA:
16501         case R_PPC64_ADDR16_LO:
16502         case R_PPC64_ADDR16_LO_DS:
16503         case R_PPC64_ADDR16_HIGHER34:
16504         case R_PPC64_ADDR16_HIGHERA34:
16505         case R_PPC64_ADDR16_HIGHEST34:
16506         case R_PPC64_ADDR16_HIGHESTA34:
16507         case R_PPC64_ADDR24:
16508         case R_PPC64_ADDR32:
16509         case R_PPC64_ADDR64:
16510         case R_PPC64_UADDR16:
16511         case R_PPC64_UADDR32:
16512         case R_PPC64_UADDR64:
16513         case R_PPC64_D34:
16514         case R_PPC64_D34_LO:
16515         case R_PPC64_D34_HI30:
16516         case R_PPC64_D34_HA30:
16517         case R_PPC64_D28:
16518         dodyn:
16519           if ((input_section->flags & SEC_ALLOC) == 0)
16520             break;
16521
16522           if (NO_OPD_RELOCS && is_opd)
16523             break;
16524
16525           if (bfd_link_pic (info)
16526               ? ((h == NULL
16527                   || h->dyn_relocs != NULL)
16528                  && ((h != NULL && pc_dynrelocs (h))
16529                      || must_be_dyn_reloc (info, r_type)))
16530               : (h != NULL
16531                  ? h->dyn_relocs != NULL
16532                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16533             {
16534               bfd_boolean skip, relocate;
16535               asection *sreloc;
16536               bfd_vma out_off;
16537               long indx = 0;
16538
16539               /* When generating a dynamic object, these relocations
16540                  are copied into the output file to be resolved at run
16541                  time.  */
16542
16543               skip = FALSE;
16544               relocate = FALSE;
16545
16546               out_off = _bfd_elf_section_offset (output_bfd, info,
16547                                                  input_section, rel->r_offset);
16548               if (out_off == (bfd_vma) -1)
16549                 skip = TRUE;
16550               else if (out_off == (bfd_vma) -2)
16551                 skip = TRUE, relocate = TRUE;
16552               out_off += (input_section->output_section->vma
16553                           + input_section->output_offset);
16554               outrel.r_offset = out_off;
16555               outrel.r_addend = rel->r_addend;
16556
16557               /* Optimize unaligned reloc use.  */
16558               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16559                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16560                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16561               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16562                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16563                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16564               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16565                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16566                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16567
16568               if (skip)
16569                 memset (&outrel, 0, sizeof outrel);
16570               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16571                        && !is_opd
16572                        && r_type != R_PPC64_TOC)
16573                 {
16574                   indx = h->elf.dynindx;
16575                   BFD_ASSERT (indx != -1);
16576                   outrel.r_info = ELF64_R_INFO (indx, r_type);
16577                 }
16578               else
16579                 {
16580                   /* This symbol is local, or marked to become local,
16581                      or this is an opd section reloc which must point
16582                      at a local function.  */
16583                   outrel.r_addend += relocation;
16584                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16585                     {
16586                       if (is_opd && h != NULL)
16587                         {
16588                           /* Lie about opd entries.  This case occurs
16589                              when building shared libraries and we
16590                              reference a function in another shared
16591                              lib.  The same thing happens for a weak
16592                              definition in an application that's
16593                              overridden by a strong definition in a
16594                              shared lib.  (I believe this is a generic
16595                              bug in binutils handling of weak syms.)
16596                              In these cases we won't use the opd
16597                              entry in this lib.  */
16598                           unresolved_reloc = FALSE;
16599                         }
16600                       if (!is_opd
16601                           && r_type == R_PPC64_ADDR64
16602                           && (h != NULL
16603                               ? h->elf.type == STT_GNU_IFUNC
16604                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16605                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16606                       else
16607                         {
16608                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16609
16610                           /* We need to relocate .opd contents for ld.so.
16611                              Prelink also wants simple and consistent rules
16612                              for relocs.  This make all RELATIVE relocs have
16613                              *r_offset equal to r_addend.  */
16614                           relocate = TRUE;
16615                         }
16616                     }
16617                   else
16618                     {
16619                       if (h != NULL
16620                           ? h->elf.type == STT_GNU_IFUNC
16621                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16622                         {
16623                           info->callbacks->einfo
16624                             /* xgettext:c-format */
16625                             (_("%H: %s for indirect "
16626                                "function `%pT' unsupported\n"),
16627                              input_bfd, input_section, rel->r_offset,
16628                              ppc64_elf_howto_table[r_type]->name,
16629                              sym_name);
16630                           ret = FALSE;
16631                         }
16632                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16633                         ;
16634                       else if (sec == NULL || sec->owner == NULL)
16635                         {
16636                           bfd_set_error (bfd_error_bad_value);
16637                           return FALSE;
16638                         }
16639                       else
16640                         {
16641                           asection *osec = sec->output_section;
16642
16643                           if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16644                             {
16645                               /* TLS symbol values are relative to the
16646                                  TLS segment.  Dynamic relocations for
16647                                  local TLS symbols therefore can't be
16648                                  reduced to a relocation against their
16649                                  section symbol because it holds the
16650                                  address of the section, not a value
16651                                  relative to the TLS segment.  We could
16652                                  change the .tdata dynamic section symbol
16653                                  to be zero value but STN_UNDEF works
16654                                  and is used elsewhere, eg. for TPREL64
16655                                  GOT relocs against local TLS symbols.  */
16656                               osec = htab->elf.tls_sec;
16657                               indx = 0;
16658                             }
16659                           else
16660                             {
16661                               indx = elf_section_data (osec)->dynindx;
16662                               if (indx == 0)
16663                                 {
16664                                   if ((osec->flags & SEC_READONLY) == 0
16665                                       && htab->elf.data_index_section != NULL)
16666                                     osec = htab->elf.data_index_section;
16667                                   else
16668                                     osec = htab->elf.text_index_section;
16669                                   indx = elf_section_data (osec)->dynindx;
16670                                 }
16671                               BFD_ASSERT (indx != 0);
16672                             }
16673
16674                           /* We are turning this relocation into one
16675                              against a section symbol, so subtract out
16676                              the output section's address but not the
16677                              offset of the input section in the output
16678                              section.  */
16679                           outrel.r_addend -= osec->vma;
16680                         }
16681
16682                       outrel.r_info = ELF64_R_INFO (indx, r_type);
16683                     }
16684                 }
16685
16686               sreloc = elf_section_data (input_section)->sreloc;
16687               if (h != NULL
16688                   ? h->elf.type == STT_GNU_IFUNC
16689                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16690                 {
16691                   sreloc = htab->elf.irelplt;
16692                   if (indx == 0)
16693                     htab->local_ifunc_resolver = 1;
16694                   else if (is_static_defined (&h->elf))
16695                     htab->maybe_local_ifunc_resolver = 1;
16696                 }
16697               if (sreloc == NULL)
16698                 abort ();
16699
16700               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16701                   >= sreloc->size)
16702                 abort ();
16703               loc = sreloc->contents;
16704               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16705               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16706
16707               if (!warned_dynamic
16708                   && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16709                 {
16710                   info->callbacks->einfo
16711                     /* xgettext:c-format */
16712                     (_("%X%P: %pB: %s against %pT "
16713                        "is not supported by glibc as a dynamic relocation\n"),
16714                      input_bfd,
16715                      ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16716                      sym_name);
16717                   warned_dynamic = TRUE;
16718                 }
16719
16720               /* If this reloc is against an external symbol, it will
16721                  be computed at runtime, so there's no need to do
16722                  anything now.  However, for the sake of prelink ensure
16723                  that the section contents are a known value.  */
16724               if (!relocate)
16725                 {
16726                   unresolved_reloc = FALSE;
16727                   /* The value chosen here is quite arbitrary as ld.so
16728                      ignores section contents except for the special
16729                      case of .opd where the contents might be accessed
16730                      before relocation.  Choose zero, as that won't
16731                      cause reloc overflow.  */
16732                   relocation = 0;
16733                   addend = 0;
16734                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16735                      to improve backward compatibility with older
16736                      versions of ld.  */
16737                   if (r_type == R_PPC64_ADDR64)
16738                     addend = outrel.r_addend;
16739                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
16740                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
16741                     addend = outrel.r_offset;
16742                 }
16743             }
16744           break;
16745
16746         case R_PPC64_COPY:
16747         case R_PPC64_GLOB_DAT:
16748         case R_PPC64_JMP_SLOT:
16749         case R_PPC64_JMP_IREL:
16750         case R_PPC64_RELATIVE:
16751           /* We shouldn't ever see these dynamic relocs in relocatable
16752              files.  */
16753           /* Fall through.  */
16754
16755         case R_PPC64_PLTGOT16:
16756         case R_PPC64_PLTGOT16_DS:
16757         case R_PPC64_PLTGOT16_HA:
16758         case R_PPC64_PLTGOT16_HI:
16759         case R_PPC64_PLTGOT16_LO:
16760         case R_PPC64_PLTGOT16_LO_DS:
16761         case R_PPC64_PLTREL32:
16762         case R_PPC64_PLTREL64:
16763           /* These ones haven't been implemented yet.  */
16764
16765           info->callbacks->einfo
16766             /* xgettext:c-format */
16767             (_("%P: %pB: %s is not supported for `%pT'\n"),
16768              input_bfd,
16769              ppc64_elf_howto_table[r_type]->name, sym_name);
16770
16771           bfd_set_error (bfd_error_invalid_operation);
16772           ret = FALSE;
16773           goto copy_reloc;
16774         }
16775
16776       /* Multi-instruction sequences that access the TOC can be
16777          optimized, eg. addis ra,r2,0; addi rb,ra,x;
16778          to             nop;           addi rb,r2,x;  */
16779       switch (r_type)
16780         {
16781         default:
16782           break;
16783
16784         case R_PPC64_GOT_TLSLD16_HI:
16785         case R_PPC64_GOT_TLSGD16_HI:
16786         case R_PPC64_GOT_TPREL16_HI:
16787         case R_PPC64_GOT_DTPREL16_HI:
16788         case R_PPC64_GOT16_HI:
16789         case R_PPC64_TOC16_HI:
16790           /* These relocs would only be useful if building up an
16791              offset to later add to r2, perhaps in an indexed
16792              addressing mode instruction.  Don't try to optimize.
16793              Unfortunately, the possibility of someone building up an
16794              offset like this or even with the HA relocs, means that
16795              we need to check the high insn when optimizing the low
16796              insn.  */
16797           break;
16798
16799         case R_PPC64_PLTCALL_NOTOC:
16800           if (!unresolved_reloc)
16801             htab->notoc_plt = 1;
16802           /* Fall through.  */
16803         case R_PPC64_PLTCALL:
16804           if (unresolved_reloc)
16805             {
16806               /* No plt entry.  Make this into a direct call.  */
16807               bfd_byte *p = contents + rel->r_offset;
16808               insn = bfd_get_32 (input_bfd, p);
16809               insn &= 1;
16810               bfd_put_32 (input_bfd, B_DOT | insn, p);
16811               if (r_type == R_PPC64_PLTCALL)
16812                 bfd_put_32 (input_bfd, NOP, p + 4);
16813               unresolved_reloc = save_unresolved_reloc;
16814               r_type = R_PPC64_REL24;
16815             }
16816           break;
16817
16818         case R_PPC64_PLTSEQ_NOTOC:
16819         case R_PPC64_PLTSEQ:
16820           if (unresolved_reloc)
16821             {
16822               unresolved_reloc = FALSE;
16823               goto nop_it;
16824             }
16825           break;
16826
16827         case R_PPC64_PLT_PCREL34_NOTOC:
16828           if (!unresolved_reloc)
16829             htab->notoc_plt = 1;
16830           /* Fall through.  */
16831         case R_PPC64_PLT_PCREL34:
16832           if (unresolved_reloc)
16833             {
16834               bfd_byte *p = contents + rel->r_offset;
16835               bfd_put_32 (input_bfd, PNOP >> 32, p);
16836               bfd_put_32 (input_bfd, PNOP, p + 4);
16837               unresolved_reloc = FALSE;
16838               goto copy_reloc;
16839             }
16840           break;
16841
16842         case R_PPC64_PLT16_HA:
16843           if (unresolved_reloc)
16844             {
16845               unresolved_reloc = FALSE;
16846               goto nop_it;
16847             }
16848           /* Fall through.  */
16849         case R_PPC64_GOT_TLSLD16_HA:
16850         case R_PPC64_GOT_TLSGD16_HA:
16851         case R_PPC64_GOT_TPREL16_HA:
16852         case R_PPC64_GOT_DTPREL16_HA:
16853         case R_PPC64_GOT16_HA:
16854         case R_PPC64_TOC16_HA:
16855           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16856               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16857             {
16858               bfd_byte *p;
16859             nop_it:
16860               p = contents + (rel->r_offset & ~3);
16861               bfd_put_32 (input_bfd, NOP, p);
16862               goto copy_reloc;
16863             }
16864           break;
16865
16866         case R_PPC64_PLT16_LO:
16867         case R_PPC64_PLT16_LO_DS:
16868           if (unresolved_reloc)
16869             {
16870               unresolved_reloc = FALSE;
16871               goto nop_it;
16872             }
16873           /* Fall through.  */
16874         case R_PPC64_GOT_TLSLD16_LO:
16875         case R_PPC64_GOT_TLSGD16_LO:
16876         case R_PPC64_GOT_TPREL16_LO_DS:
16877         case R_PPC64_GOT_DTPREL16_LO_DS:
16878         case R_PPC64_GOT16_LO:
16879         case R_PPC64_GOT16_LO_DS:
16880         case R_PPC64_TOC16_LO:
16881         case R_PPC64_TOC16_LO_DS:
16882           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16883               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16884             {
16885               bfd_byte *p = contents + (rel->r_offset & ~3);
16886               insn = bfd_get_32 (input_bfd, p);
16887               if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
16888                 {
16889                   /* Transform addic to addi when we change reg.  */
16890                   insn &= ~((0x3fu << 26) | (0x1f << 16));
16891                   insn |= (14u << 26) | (2 << 16);
16892                 }
16893               else
16894                 {
16895                   insn &= ~(0x1f << 16);
16896                   insn |= 2 << 16;
16897                 }
16898               bfd_put_32 (input_bfd, insn, p);
16899             }
16900           break;
16901
16902         case R_PPC64_TPREL16_HA:
16903           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16904             {
16905               bfd_byte *p = contents + (rel->r_offset & ~3);
16906               insn = bfd_get_32 (input_bfd, p);
16907               if ((insn & ((0x3fu << 26) | 0x1f << 16))
16908                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16909                 /* xgettext:c-format */
16910                 info->callbacks->minfo
16911                   (_("%H: warning: %s unexpected insn %#x.\n"),
16912                    input_bfd, input_section, rel->r_offset,
16913                    ppc64_elf_howto_table[r_type]->name, insn);
16914               else
16915                 {
16916                   bfd_put_32 (input_bfd, NOP, p);
16917                   goto copy_reloc;
16918                 }
16919             }
16920           break;
16921
16922         case R_PPC64_TPREL16_LO:
16923         case R_PPC64_TPREL16_LO_DS:
16924           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16925             {
16926               bfd_byte *p = contents + (rel->r_offset & ~3);
16927               insn = bfd_get_32 (input_bfd, p);
16928               insn &= ~(0x1f << 16);
16929               insn |= 13 << 16;
16930               bfd_put_32 (input_bfd, insn, p);
16931             }
16932           break;
16933         }
16934
16935       /* Do any further special processing.  */
16936       switch (r_type)
16937         {
16938         default:
16939           break;
16940
16941         case R_PPC64_REL16_HA:
16942         case R_PPC64_REL16_HIGHA:
16943         case R_PPC64_REL16_HIGHERA:
16944         case R_PPC64_REL16_HIGHESTA:
16945         case R_PPC64_REL16DX_HA:
16946         case R_PPC64_ADDR16_HA:
16947         case R_PPC64_ADDR16_HIGHA:
16948         case R_PPC64_ADDR16_HIGHERA:
16949         case R_PPC64_ADDR16_HIGHESTA:
16950         case R_PPC64_TOC16_HA:
16951         case R_PPC64_SECTOFF_HA:
16952         case R_PPC64_TPREL16_HA:
16953         case R_PPC64_TPREL16_HIGHA:
16954         case R_PPC64_TPREL16_HIGHERA:
16955         case R_PPC64_TPREL16_HIGHESTA:
16956         case R_PPC64_DTPREL16_HA:
16957         case R_PPC64_DTPREL16_HIGHA:
16958         case R_PPC64_DTPREL16_HIGHERA:
16959         case R_PPC64_DTPREL16_HIGHESTA:
16960           /* It's just possible that this symbol is a weak symbol
16961              that's not actually defined anywhere. In that case,
16962              'sec' would be NULL, and we should leave the symbol
16963              alone (it will be set to zero elsewhere in the link).  */
16964           if (sec == NULL)
16965             break;
16966           /* Fall through.  */
16967
16968         case R_PPC64_GOT16_HA:
16969         case R_PPC64_PLTGOT16_HA:
16970         case R_PPC64_PLT16_HA:
16971         case R_PPC64_GOT_TLSGD16_HA:
16972         case R_PPC64_GOT_TLSLD16_HA:
16973         case R_PPC64_GOT_TPREL16_HA:
16974         case R_PPC64_GOT_DTPREL16_HA:
16975           /* Add 0x10000 if sign bit in 0:15 is set.
16976              Bits 0:15 are not used.  */
16977           addend += 0x8000;
16978           break;
16979
16980         case R_PPC64_D34_HA30:
16981         case R_PPC64_ADDR16_HIGHERA34:
16982         case R_PPC64_ADDR16_HIGHESTA34:
16983         case R_PPC64_REL16_HIGHERA34:
16984         case R_PPC64_REL16_HIGHESTA34:
16985           if (sec != NULL)
16986             addend += 1ULL << 33;
16987           break;
16988
16989         case R_PPC64_ADDR16_DS:
16990         case R_PPC64_ADDR16_LO_DS:
16991         case R_PPC64_GOT16_DS:
16992         case R_PPC64_GOT16_LO_DS:
16993         case R_PPC64_PLT16_LO_DS:
16994         case R_PPC64_SECTOFF_DS:
16995         case R_PPC64_SECTOFF_LO_DS:
16996         case R_PPC64_TOC16_DS:
16997         case R_PPC64_TOC16_LO_DS:
16998         case R_PPC64_PLTGOT16_DS:
16999         case R_PPC64_PLTGOT16_LO_DS:
17000         case R_PPC64_GOT_TPREL16_DS:
17001         case R_PPC64_GOT_TPREL16_LO_DS:
17002         case R_PPC64_GOT_DTPREL16_DS:
17003         case R_PPC64_GOT_DTPREL16_LO_DS:
17004         case R_PPC64_TPREL16_DS:
17005         case R_PPC64_TPREL16_LO_DS:
17006         case R_PPC64_DTPREL16_DS:
17007         case R_PPC64_DTPREL16_LO_DS:
17008           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17009           mask = 3;
17010           /* If this reloc is against an lq, lxv, or stxv insn, then
17011              the value must be a multiple of 16.  This is somewhat of
17012              a hack, but the "correct" way to do this by defining _DQ
17013              forms of all the _DS relocs bloats all reloc switches in
17014              this file.  It doesn't make much sense to use these
17015              relocs in data, so testing the insn should be safe.  */
17016           if ((insn & (0x3fu << 26)) == (56u << 26)
17017               || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17018             mask = 15;
17019           relocation += addend;
17020           addend = insn & (mask ^ 3);
17021           if ((relocation & mask) != 0)
17022             {
17023               relocation ^= relocation & mask;
17024               info->callbacks->einfo
17025                 /* xgettext:c-format */
17026                 (_("%H: error: %s not a multiple of %u\n"),
17027                  input_bfd, input_section, rel->r_offset,
17028                  ppc64_elf_howto_table[r_type]->name,
17029                  mask + 1);
17030               bfd_set_error (bfd_error_bad_value);
17031               ret = FALSE;
17032               goto copy_reloc;
17033             }
17034           break;
17035         }
17036
17037       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17038          because such sections are not SEC_ALLOC and thus ld.so will
17039          not process them.  */
17040       howto = ppc64_elf_howto_table[(int) r_type];
17041       if (unresolved_reloc
17042           && !((input_section->flags & SEC_DEBUGGING) != 0
17043                && h->elf.def_dynamic)
17044           && _bfd_elf_section_offset (output_bfd, info, input_section,
17045                                       rel->r_offset) != (bfd_vma) -1)
17046         {
17047           info->callbacks->einfo
17048             /* xgettext:c-format */
17049             (_("%H: unresolvable %s against `%pT'\n"),
17050              input_bfd, input_section, rel->r_offset,
17051              howto->name,
17052              h->elf.root.root.string);
17053           ret = FALSE;
17054         }
17055
17056       /* 16-bit fields in insns mostly have signed values, but a
17057          few insns have 16-bit unsigned values.  Really, we should
17058          have different reloc types.  */
17059       if (howto->complain_on_overflow != complain_overflow_dont
17060           && howto->dst_mask == 0xffff
17061           && (input_section->flags & SEC_CODE) != 0)
17062         {
17063           enum complain_overflow complain = complain_overflow_signed;
17064
17065           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17066           if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17067             complain = complain_overflow_bitfield;
17068           else if (howto->rightshift == 0
17069                    ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17070                       || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17071                       || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17072                    : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17073                       || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17074                       || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17075             complain = complain_overflow_unsigned;
17076           if (howto->complain_on_overflow != complain)
17077             {
17078               alt_howto = *howto;
17079               alt_howto.complain_on_overflow = complain;
17080               howto = &alt_howto;
17081             }
17082         }
17083
17084       switch (r_type)
17085         {
17086           /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
17087         case R_PPC64_D34:
17088         case R_PPC64_D34_LO:
17089         case R_PPC64_D34_HI30:
17090         case R_PPC64_D34_HA30:
17091         case R_PPC64_PCREL34:
17092         case R_PPC64_GOT_PCREL34:
17093         case R_PPC64_TPREL34:
17094         case R_PPC64_DTPREL34:
17095         case R_PPC64_GOT_TLSGD34:
17096         case R_PPC64_GOT_TLSLD34:
17097         case R_PPC64_GOT_TPREL34:
17098         case R_PPC64_GOT_DTPREL34:
17099         case R_PPC64_PLT_PCREL34:
17100         case R_PPC64_PLT_PCREL34_NOTOC:
17101         case R_PPC64_D28:
17102         case R_PPC64_PCREL28:
17103           if (rel->r_offset + 8 > input_section->size)
17104             r = bfd_reloc_outofrange;
17105           else
17106             {
17107               relocation += addend;
17108               if (howto->pc_relative)
17109                 relocation -= (rel->r_offset
17110                                + input_section->output_offset
17111                                + input_section->output_section->vma);
17112               relocation >>= howto->rightshift;
17113
17114               pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17115               pinsn <<= 32;
17116               pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17117
17118               pinsn &= ~howto->dst_mask;
17119               pinsn |= (((relocation << 16) | (relocation & 0xffff))
17120                         & howto->dst_mask);
17121               bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17122               bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17123               r = bfd_reloc_ok;
17124               if (howto->complain_on_overflow == complain_overflow_signed
17125                   && (relocation + (1ULL << (howto->bitsize - 1))
17126                       >= 1ULL << howto->bitsize))
17127                 r = bfd_reloc_overflow;
17128             }
17129           break;
17130
17131         case R_PPC64_REL16DX_HA:
17132           if (rel->r_offset + 4 > input_section->size)
17133             r = bfd_reloc_outofrange;
17134           else
17135             {
17136               relocation += addend;
17137               relocation -= (rel->r_offset
17138                              + input_section->output_offset
17139                              + input_section->output_section->vma);
17140               relocation = (bfd_signed_vma) relocation >> 16;
17141               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17142               insn &= ~0x1fffc1;
17143               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17144               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17145               r = bfd_reloc_ok;
17146               if (relocation + 0x8000 > 0xffff)
17147                 r = bfd_reloc_overflow;
17148             }
17149           break;
17150
17151         default:
17152           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17153                                         contents, rel->r_offset,
17154                                         relocation, addend);
17155         }
17156
17157       if (r != bfd_reloc_ok)
17158         {
17159           char *more_info = NULL;
17160           const char *reloc_name = howto->name;
17161
17162           if (reloc_dest != DEST_NORMAL)
17163             {
17164               more_info = bfd_malloc (strlen (reloc_name) + 8);
17165               if (more_info != NULL)
17166                 {
17167                   strcpy (more_info, reloc_name);
17168                   strcat (more_info, (reloc_dest == DEST_OPD
17169                                       ? " (OPD)" : " (stub)"));
17170                   reloc_name = more_info;
17171                 }
17172             }
17173
17174           if (r == bfd_reloc_overflow)
17175             {
17176               /* On code like "if (foo) foo();" don't report overflow
17177                  on a branch to zero when foo is undefined.  */
17178               if (!warned
17179                   && (reloc_dest == DEST_STUB
17180                       || !(h != NULL
17181                            && (h->elf.root.type == bfd_link_hash_undefweak
17182                                || h->elf.root.type == bfd_link_hash_undefined)
17183                            && is_branch_reloc (r_type))))
17184                 info->callbacks->reloc_overflow (info, &h->elf.root,
17185                                                  sym_name, reloc_name,
17186                                                  orig_rel.r_addend,
17187                                                  input_bfd, input_section,
17188                                                  rel->r_offset);
17189             }
17190           else
17191             {
17192               info->callbacks->einfo
17193                 /* xgettext:c-format */
17194                 (_("%H: %s against `%pT': error %d\n"),
17195                  input_bfd, input_section, rel->r_offset,
17196                  reloc_name, sym_name, (int) r);
17197               ret = FALSE;
17198             }
17199           if (more_info != NULL)
17200             free (more_info);
17201         }
17202     copy_reloc:
17203       if (wrel != rel)
17204         *wrel = *rel;
17205     }
17206
17207   if (wrel != rel)
17208     {
17209       Elf_Internal_Shdr *rel_hdr;
17210       size_t deleted = rel - wrel;
17211
17212       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17213       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17214       if (rel_hdr->sh_size == 0)
17215         {
17216           /* It is too late to remove an empty reloc section.  Leave
17217              one NONE reloc.
17218              ??? What is wrong with an empty section???  */
17219           rel_hdr->sh_size = rel_hdr->sh_entsize;
17220           deleted -= 1;
17221         }
17222       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17223       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17224       input_section->reloc_count -= deleted;
17225     }
17226
17227   /* If we're emitting relocations, then shortly after this function
17228      returns, reloc offsets and addends for this section will be
17229      adjusted.  Worse, reloc symbol indices will be for the output
17230      file rather than the input.  Save a copy of the relocs for
17231      opd_entry_value.  */
17232   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17233     {
17234       bfd_size_type amt;
17235       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17236       rel = bfd_alloc (input_bfd, amt);
17237       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17238       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17239       if (rel == NULL)
17240         return FALSE;
17241       memcpy (rel, relocs, amt);
17242     }
17243   return ret;
17244 }
17245
17246 /* Adjust the value of any local symbols in opd sections.  */
17247
17248 static int
17249 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17250                               const char *name ATTRIBUTE_UNUSED,
17251                               Elf_Internal_Sym *elfsym,
17252                               asection *input_sec,
17253                               struct elf_link_hash_entry *h)
17254 {
17255   struct _opd_sec_data *opd;
17256   long adjust;
17257   bfd_vma value;
17258
17259   if (h != NULL)
17260     return 1;
17261
17262   opd = get_opd_info (input_sec);
17263   if (opd == NULL || opd->adjust == NULL)
17264     return 1;
17265
17266   value = elfsym->st_value - input_sec->output_offset;
17267   if (!bfd_link_relocatable (info))
17268     value -= input_sec->output_section->vma;
17269
17270   adjust = opd->adjust[OPD_NDX (value)];
17271   if (adjust == -1)
17272     return 2;
17273
17274   elfsym->st_value += adjust;
17275   return 1;
17276 }
17277
17278 /* Finish up dynamic symbol handling.  We set the contents of various
17279    dynamic sections here.  */
17280
17281 static bfd_boolean
17282 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17283                                  struct bfd_link_info *info,
17284                                  struct elf_link_hash_entry *h,
17285                                  Elf_Internal_Sym *sym)
17286 {
17287   struct ppc_link_hash_table *htab;
17288   struct plt_entry *ent;
17289
17290   htab = ppc_hash_table (info);
17291   if (htab == NULL)
17292     return FALSE;
17293
17294   if (!htab->opd_abi && !h->def_regular)
17295     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17296       if (ent->plt.offset != (bfd_vma) -1)
17297         {
17298           /* Mark the symbol as undefined, rather than as
17299              defined in glink.  Leave the value if there were
17300              any relocations where pointer equality matters
17301              (this is a clue for the dynamic linker, to make
17302              function pointer comparisons work between an
17303              application and shared library), otherwise set it
17304              to zero.  */
17305           sym->st_shndx = SHN_UNDEF;
17306           if (!h->pointer_equality_needed)
17307             sym->st_value = 0;
17308           else if (!h->ref_regular_nonweak)
17309             {
17310               /* This breaks function pointer comparisons, but
17311                  that is better than breaking tests for a NULL
17312                  function pointer.  */
17313               sym->st_value = 0;
17314             }
17315           break;
17316         }
17317
17318   if (h->needs_copy
17319       && (h->root.type == bfd_link_hash_defined
17320           || h->root.type == bfd_link_hash_defweak)
17321       && (h->root.u.def.section == htab->elf.sdynbss
17322           || h->root.u.def.section == htab->elf.sdynrelro))
17323     {
17324       /* This symbol needs a copy reloc.  Set it up.  */
17325       Elf_Internal_Rela rela;
17326       asection *srel;
17327       bfd_byte *loc;
17328
17329       if (h->dynindx == -1)
17330         abort ();
17331
17332       rela.r_offset = defined_sym_val (h);
17333       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17334       rela.r_addend = 0;
17335       if (h->root.u.def.section == htab->elf.sdynrelro)
17336         srel = htab->elf.sreldynrelro;
17337       else
17338         srel = htab->elf.srelbss;
17339       loc = srel->contents;
17340       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
17341       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
17342     }
17343
17344   return TRUE;
17345 }
17346
17347 /* Used to decide how to sort relocs in an optimal manner for the
17348    dynamic linker, before writing them out.  */
17349
17350 static enum elf_reloc_type_class
17351 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17352                             const asection *rel_sec,
17353                             const Elf_Internal_Rela *rela)
17354 {
17355   enum elf_ppc64_reloc_type r_type;
17356   struct ppc_link_hash_table *htab = ppc_hash_table (info);
17357
17358   if (rel_sec == htab->elf.irelplt)
17359     return reloc_class_ifunc;
17360
17361   r_type = ELF64_R_TYPE (rela->r_info);
17362   switch (r_type)
17363     {
17364     case R_PPC64_RELATIVE:
17365       return reloc_class_relative;
17366     case R_PPC64_JMP_SLOT:
17367       return reloc_class_plt;
17368     case R_PPC64_COPY:
17369       return reloc_class_copy;
17370     default:
17371       return reloc_class_normal;
17372     }
17373 }
17374
17375 /* Finish up the dynamic sections.  */
17376
17377 static bfd_boolean
17378 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17379                                    struct bfd_link_info *info)
17380 {
17381   struct ppc_link_hash_table *htab;
17382   bfd *dynobj;
17383   asection *sdyn;
17384
17385   htab = ppc_hash_table (info);
17386   if (htab == NULL)
17387     return FALSE;
17388
17389   dynobj = htab->elf.dynobj;
17390   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17391
17392   if (htab->elf.dynamic_sections_created)
17393     {
17394       Elf64_External_Dyn *dyncon, *dynconend;
17395
17396       if (sdyn == NULL || htab->elf.sgot == NULL)
17397         abort ();
17398
17399       dyncon = (Elf64_External_Dyn *) sdyn->contents;
17400       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
17401       for (; dyncon < dynconend; dyncon++)
17402         {
17403           Elf_Internal_Dyn dyn;
17404           asection *s;
17405
17406           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17407
17408           switch (dyn.d_tag)
17409             {
17410             default:
17411               continue;
17412
17413             case DT_PPC64_GLINK:
17414               s = htab->glink;
17415               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17416               /* We stupidly defined DT_PPC64_GLINK to be the start
17417                  of glink rather than the first entry point, which is
17418                  what ld.so needs, and now have a bigger stub to
17419                  support automatic multiple TOCs.  */
17420               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
17421               break;
17422
17423             case DT_PPC64_OPD:
17424               s = bfd_get_section_by_name (output_bfd, ".opd");
17425               if (s == NULL)
17426                 continue;
17427               dyn.d_un.d_ptr = s->vma;
17428               break;
17429
17430             case DT_PPC64_OPT:
17431               if ((htab->do_multi_toc && htab->multi_toc_needed)
17432                   || htab->notoc_plt)
17433                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
17434               if (htab->has_plt_localentry0)
17435                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
17436               break;
17437
17438             case DT_PPC64_OPDSZ:
17439               s = bfd_get_section_by_name (output_bfd, ".opd");
17440               if (s == NULL)
17441                 continue;
17442               dyn.d_un.d_val = s->size;
17443               break;
17444
17445             case DT_PLTGOT:
17446               s = htab->elf.splt;
17447               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17448               break;
17449
17450             case DT_JMPREL:
17451               s = htab->elf.srelplt;
17452               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17453               break;
17454
17455             case DT_PLTRELSZ:
17456               dyn.d_un.d_val = htab->elf.srelplt->size;
17457               break;
17458
17459             case DT_TEXTREL:
17460               if (htab->local_ifunc_resolver)
17461                 info->callbacks->einfo
17462                   (_("%X%P: text relocations and GNU indirect "
17463                      "functions will result in a segfault at runtime\n"));
17464               else if (htab->maybe_local_ifunc_resolver)
17465                 info->callbacks->einfo
17466                   (_("%P: warning: text relocations and GNU indirect "
17467                      "functions may result in a segfault at runtime\n"));
17468               continue;
17469             }
17470
17471           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
17472         }
17473     }
17474
17475   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17476       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
17477     {
17478       /* Fill in the first entry in the global offset table.
17479          We use it to hold the link-time TOCbase.  */
17480       bfd_put_64 (output_bfd,
17481                   elf_gp (output_bfd) + TOC_BASE_OFF,
17482                   htab->elf.sgot->contents);
17483
17484       /* Set .got entry size.  */
17485       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17486         = 8;
17487     }
17488
17489   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17490       && htab->elf.splt->output_section != bfd_abs_section_ptr)
17491     {
17492       /* Set .plt entry size.  */
17493       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
17494         = PLT_ENTRY_SIZE (htab);
17495     }
17496
17497   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17498      brlt ourselves if emitrelocations.  */
17499   if (htab->brlt != NULL
17500       && htab->brlt->reloc_count != 0
17501       && !_bfd_elf_link_output_relocs (output_bfd,
17502                                        htab->brlt,
17503                                        elf_section_data (htab->brlt)->rela.hdr,
17504                                        elf_section_data (htab->brlt)->relocs,
17505                                        NULL))
17506     return FALSE;
17507
17508   if (htab->glink != NULL
17509       && htab->glink->reloc_count != 0
17510       && !_bfd_elf_link_output_relocs (output_bfd,
17511                                        htab->glink,
17512                                        elf_section_data (htab->glink)->rela.hdr,
17513                                        elf_section_data (htab->glink)->relocs,
17514                                        NULL))
17515     return FALSE;
17516
17517
17518   if (htab->glink_eh_frame != NULL
17519       && htab->glink_eh_frame->size != 0
17520       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17521       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17522                                            htab->glink_eh_frame,
17523                                            htab->glink_eh_frame->contents))
17524     return FALSE;
17525
17526   /* We need to handle writing out multiple GOT sections ourselves,
17527      since we didn't add them to DYNOBJ.  We know dynobj is the first
17528      bfd.  */
17529   while ((dynobj = dynobj->link.next) != NULL)
17530     {
17531       asection *s;
17532
17533       if (!is_ppc64_elf (dynobj))
17534         continue;
17535
17536       s = ppc64_elf_tdata (dynobj)->got;
17537       if (s != NULL
17538           && s->size != 0
17539           && s->output_section != bfd_abs_section_ptr
17540           && !bfd_set_section_contents (output_bfd, s->output_section,
17541                                         s->contents, s->output_offset,
17542                                         s->size))
17543         return FALSE;
17544       s = ppc64_elf_tdata (dynobj)->relgot;
17545       if (s != NULL
17546           && s->size != 0
17547           && s->output_section != bfd_abs_section_ptr
17548           && !bfd_set_section_contents (output_bfd, s->output_section,
17549                                         s->contents, s->output_offset,
17550                                         s->size))
17551         return FALSE;
17552     }
17553
17554   return TRUE;
17555 }
17556
17557 #include "elf64-target.h"
17558
17559 /* FreeBSD support */
17560
17561 #undef  TARGET_LITTLE_SYM
17562 #undef  TARGET_LITTLE_NAME
17563
17564 #undef  TARGET_BIG_SYM
17565 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
17566 #undef  TARGET_BIG_NAME
17567 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17568
17569 #undef  ELF_OSABI
17570 #define ELF_OSABI       ELFOSABI_FREEBSD
17571
17572 #undef  elf64_bed
17573 #define elf64_bed       elf64_powerpc_fbsd_bed
17574
17575 #include "elf64-target.h"
This page took 1.084404 seconds and 4 git commands to generate.