Rearrange ppc_size_one_stub and correct _notoc stub examples
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2018 Free Software Foundation, Inc.
3    Written by Linus Nordberg, Swox AB <info@swox.com>,
4    based on elf32-ppc.c by Ian Lance Taylor.
5    Largely rewritten by Alan Modra.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License along
20    with this program; if not, write to the Free Software Foundation, Inc.,
21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM       powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME      "elf64-powerpcle"
61 #define TARGET_BIG_SYM          powerpc_elf64_vec
62 #define TARGET_BIG_NAME         "elf64-powerpc"
63 #define ELF_ARCH                bfd_arch_powerpc
64 #define ELF_TARGET_ID           PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE        EM_PPC64
66 #define ELF_MAXPAGESIZE         0x10000
67 #define ELF_COMMONPAGESIZE      0x1000
68 #define ELF_RELROPAGESIZE       ELF_MAXPAGESIZE
69 #define elf_info_to_howto       ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
82
83 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections             ppc64_elf_gc_sections
93
94 #define elf_backend_object_p                  ppc64_elf_object_p
95 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note           ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs              ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded          ppc64_elf_action_discarded
117 #define elf_backend_relocate_section          ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections          ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol              ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section         bfd_get_section_by_name
126
127 /* The name of the dynamic interpreter.  This is put in the .interp
128    section.  */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131 /* The size in bytes of an entry in the procedure linkage table.  */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
134
135 /* The initial size of the plt reserved for the dynamic linker.  */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
137
138 /* Offsets to some stack save slots.  */
139 #define STK_LR 16
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
142    CR save slot.  Used only by optimised __tls_get_addr call stub,
143    relying on __tls_get_addr_opt not saving CR..  */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
146 /* TOC base pointers offset from start of TOC.  */
147 #define TOC_BASE_OFF    0x8000
148 /* TOC base alignment.  */
149 #define TOC_BASE_ALIGN  256
150
151 /* Offset of tp and dtp pointers from start of TLS block.  */
152 #define TP_OFFSET       0x7000
153 #define DTP_OFFSET      0x8000
154
155 /* .plt call stub instructions.  The normal stub is like this, but
156    sometimes the .plt entry crosses a 64k boundary and we need to
157    insert an addi to adjust r11.  */
158 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
159 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
160 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
161 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
162 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
163 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
164 #define BCTR            0x4e800420      /* bctr                      */
165
166 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
167 #define ADDI_R12_R11    0x398b0000      /* addi %r12,%r11,off@l  */
168 #define ADDI_R12_R12    0x398c0000      /* addi %r12,%r12,off@l  */
169 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
170 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
171
172 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
173 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
174 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
175 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
176 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
177 #define BNECTR          0x4ca20420      /* bnectr+               */
178 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
179
180 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
181 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
182 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
183
184 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
185 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
186 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
187
188 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
189 #define LIS_R12         0x3d800000      /* lis %r12,xxx@ha        */
190 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
191 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
192 #define ADDIS_R12_R11   0x3d8b0000      /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0  0x658c0000      /* oris  %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0   0x618c0000      /* ori   %r12,%r12,xxx@l  */
196 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6      /* sldi  %r12,%r12,32     */
198 #define LDX_R12_R11_R12 0x7d8b602a      /* ldx   %r12,%r11,%r12   */
199 #define ADD_R12_R11_R12 0x7d8b6214      /* add   %r12,%r11,%r12   */
200
201 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
202 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
203   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
204                                         /* 0:                           */
205                                         /*  .quad plt0-1f               */
206                                         /* __glink:                     */
207 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
208 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
209                                         /* 1:                           */
210 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
211                                         /*  ld %2,(0b-1b)(%11)          */
212 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
213 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
214                                         /*  ld %12,0(%11)               */
215                                         /*  ld %2,8(%11)                */
216                                         /*  mtctr %12                   */
217                                         /*  ld %11,16(%11)              */
218                                         /*  bctr                        */
219 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
220 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
221 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
222 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
223 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
224
225 /* Pad with this.  */
226 #define NOP             0x60000000
227
228 /* Some other nops.  */
229 #define CROR_151515     0x4def7b82
230 #define CROR_313131     0x4ffffb82
231
232 /* .glink entries for the first 32k functions are two instructions.  */
233 #define LI_R0_0         0x38000000      /* li    %r0,0          */
234 #define B_DOT           0x48000000      /* b     .              */
235
236 /* After that, we need two instructions to load the index, followed by
237    a branch.  */
238 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
239 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
240
241 /* Instructions used by the save and restore reg functions.  */
242 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
243 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
244 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
245 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
246 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
247 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
248 #define LI_R12_0        0x39800000      /* li    %r12,0         */
249 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
250 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
251 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
252 #define BLR             0x4e800020      /* blr                  */
253
254 /* Since .opd is an array of descriptors and each entry will end up
255    with identical R_PPC64_RELATIVE relocs, there is really no need to
256    propagate .opd relocs;  The dynamic linker should be taught to
257    relocate .opd without reloc entries.  */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
260 #endif
261
262 #ifndef ARRAY_SIZE
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264 #endif
265
266 static inline int
267 abiversion (bfd *abfd)
268 {
269   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270 }
271
272 static inline void
273 set_abiversion (bfd *abfd, int ver)
274 {
275   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277 }
278 \f
279 /* Relocation HOWTO's.  */
280 /* Like other ELF RELA targets that don't apply multiple
281    field-altering relocations to the same localation, src_mask is
282    always zero and pcrel_offset is the same as pc_relative.
283    PowerPC can always use a zero bitpos, even when the field is not at
284    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
285    and bitpos=2 which matches the ABI description, or as we do here,
286    rightshift=0, bitsize=26 and bitpos=0.  */
287 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
288             complain, special_func)                             \
289   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,       \
290          complain_overflow_ ## complain, special_func,          \
291          #type, FALSE, 0, mask, pc_relative)
292
293 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
294
295 static reloc_howto_type ppc64_elf_howto_raw[] =
296 {
297   /* This reloc does nothing.  */
298   HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
299        bfd_elf_generic_reloc),
300
301   /* A standard 32 bit relocation.  */
302   HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
303        bfd_elf_generic_reloc),
304
305   /* An absolute 26 bit branch; the lower two bits must be zero.
306      FIXME: we don't check that, we just clear them.  */
307   HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
308        bfd_elf_generic_reloc),
309
310   /* A standard 16 bit relocation.  */
311   HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
312        bfd_elf_generic_reloc),
313
314   /* A 16 bit relocation without overflow.  */
315   HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
316        bfd_elf_generic_reloc),
317
318   /* Bits 16-31 of an address.  */
319   HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
320        bfd_elf_generic_reloc),
321
322   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323      bits, treated as a signed number, is negative.  */
324   HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
325        ppc64_elf_ha_reloc),
326
327   /* An absolute 16 bit branch; the lower two bits must be zero.
328      FIXME: we don't check that, we just clear them.  */
329   HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
330        ppc64_elf_branch_reloc),
331
332   /* An absolute 16 bit branch, for which bit 10 should be set to
333      indicate that the branch is expected to be taken.  The lower two
334      bits must be zero.  */
335   HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
336        ppc64_elf_brtaken_reloc),
337
338   /* An absolute 16 bit branch, for which bit 10 should be set to
339      indicate that the branch is not expected to be taken.  The lower
340      two bits must be zero.  */
341   HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
342        ppc64_elf_brtaken_reloc),
343
344   /* A relative 26 bit branch; the lower two bits must be zero.  */
345   HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
346        ppc64_elf_branch_reloc),
347
348   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
349   HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
350        ppc64_elf_branch_reloc),
351
352   /* A relative 16 bit branch; the lower two bits must be zero.  */
353   HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
354        ppc64_elf_branch_reloc),
355
356   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
357      the branch is expected to be taken.  The lower two bits must be
358      zero.  */
359   HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
360        ppc64_elf_brtaken_reloc),
361
362   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
363      the branch is not expected to be taken.  The lower two bits must
364      be zero.  */
365   HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
366        ppc64_elf_brtaken_reloc),
367
368   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
369      symbol.  */
370   HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
371        ppc64_elf_unhandled_reloc),
372
373   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
374      the symbol.  */
375   HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
376        ppc64_elf_unhandled_reloc),
377
378   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
379      the symbol.  */
380   HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
381        ppc64_elf_unhandled_reloc),
382
383   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
384      the symbol.  */
385   HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
386        ppc64_elf_unhandled_reloc),
387
388   /* This is used only by the dynamic linker.  The symbol should exist
389      both in the object being run and in some shared library.  The
390      dynamic linker copies the data addressed by the symbol from the
391      shared library into the object, because the object being
392      run has to have the data at some particular address.  */
393   HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
394        ppc64_elf_unhandled_reloc),
395
396   /* Like R_PPC64_ADDR64, but used when setting global offset table
397      entries.  */
398   HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
399        ppc64_elf_unhandled_reloc),
400
401   /* Created by the link editor.  Marks a procedure linkage table
402      entry for a symbol.  */
403   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
404        ppc64_elf_unhandled_reloc),
405
406   /* Used only by the dynamic linker.  When the object is run, this
407      doubleword64 is set to the load address of the object, plus the
408      addend.  */
409   HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
410        bfd_elf_generic_reloc),
411
412   /* Like R_PPC64_ADDR32, but may be unaligned.  */
413   HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
414        bfd_elf_generic_reloc),
415
416   /* Like R_PPC64_ADDR16, but may be unaligned.  */
417   HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
418        bfd_elf_generic_reloc),
419
420   /* 32-bit PC relative.  */
421   HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
422        bfd_elf_generic_reloc),
423
424   /* 32-bit relocation to the symbol's procedure linkage table.  */
425   HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
426        ppc64_elf_unhandled_reloc),
427
428   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
429      FIXME: R_PPC64_PLTREL32 not supported.  */
430   HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
431        ppc64_elf_unhandled_reloc),
432
433   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
434      the symbol.  */
435   HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
436        ppc64_elf_unhandled_reloc),
437
438   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
439      the symbol.  */
440   HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
441        ppc64_elf_unhandled_reloc),
442
443   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
444      the symbol.  */
445   HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
446        ppc64_elf_unhandled_reloc),
447
448   /* 16-bit section relative relocation.  */
449   HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
450        ppc64_elf_sectoff_reloc),
451
452   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
453   HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
454        ppc64_elf_sectoff_reloc),
455
456   /* 16-bit upper half section relative relocation.  */
457   HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
458        ppc64_elf_sectoff_reloc),
459
460   /* 16-bit upper half adjusted section relative relocation.  */
461   HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
462        ppc64_elf_sectoff_ha_reloc),
463
464   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
465   HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
466        bfd_elf_generic_reloc),
467
468   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
469
470   /* A standard 64-bit relocation.  */
471   HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
472        bfd_elf_generic_reloc),
473
474   /* The bits 32-47 of an address.  */
475   HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
476        bfd_elf_generic_reloc),
477
478   /* The bits 32-47 of an address, plus 1 if the contents of the low
479      16 bits, treated as a signed number, is negative.  */
480   HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
481        ppc64_elf_ha_reloc),
482
483   /* The bits 48-63 of an address.  */
484   HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
485        bfd_elf_generic_reloc),
486
487   /* The bits 48-63 of an address, plus 1 if the contents of the low
488      16 bits, treated as a signed number, is negative.  */
489   HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
490        ppc64_elf_ha_reloc),
491
492   /* Like ADDR64, but may be unaligned.  */
493   HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
494        bfd_elf_generic_reloc),
495
496   /* 64-bit relative relocation.  */
497   HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
498        bfd_elf_generic_reloc),
499
500   /* 64-bit relocation to the symbol's procedure linkage table.  */
501   HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
502        ppc64_elf_unhandled_reloc),
503
504   /* 64-bit PC relative relocation to the symbol's procedure linkage
505      table.  */
506   /* FIXME: R_PPC64_PLTREL64 not supported.  */
507   HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
508        ppc64_elf_unhandled_reloc),
509
510   /* 16 bit TOC-relative relocation.  */
511   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
512   HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
513        ppc64_elf_toc_reloc),
514
515   /* 16 bit TOC-relative relocation without overflow.  */
516   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
517   HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
518        ppc64_elf_toc_reloc),
519
520   /* 16 bit TOC-relative relocation, high 16 bits.  */
521   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
522   HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
523        ppc64_elf_toc_reloc),
524
525   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
526      contents of the low 16 bits, treated as a signed number, is
527      negative.  */
528   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
529   HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
530        ppc64_elf_toc_ha_reloc),
531
532   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
533   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
534   HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
535        ppc64_elf_toc64_reloc),
536
537   /* Like R_PPC64_GOT16, but also informs the link editor that the
538      value to relocate may (!) refer to a PLT entry which the link
539      editor (a) may replace with the symbol value.  If the link editor
540      is unable to fully resolve the symbol, it may (b) create a PLT
541      entry and store the address to the new PLT entry in the GOT.
542      This permits lazy resolution of function symbols at run time.
543      The link editor may also skip all of this and just (c) emit a
544      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
545   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
546     HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
547           ppc64_elf_unhandled_reloc),
548
549   /* Like R_PPC64_PLTGOT16, but without overflow.  */
550   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
551   HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
552        ppc64_elf_unhandled_reloc),
553
554   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
555   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
556   HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
557        ppc64_elf_unhandled_reloc),
558
559   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
560      1 if the contents of the low 16 bits, treated as a signed number,
561      is negative.  */
562   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
563   HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
564        ppc64_elf_unhandled_reloc),
565
566   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
567   HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
568        bfd_elf_generic_reloc),
569
570   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
571   HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
572        bfd_elf_generic_reloc),
573
574   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
575   HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
576        ppc64_elf_unhandled_reloc),
577
578   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
579   HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
580        ppc64_elf_unhandled_reloc),
581
582   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
583   HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
584        ppc64_elf_unhandled_reloc),
585
586   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
587   HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588        ppc64_elf_sectoff_reloc),
589
590   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
591   HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592        ppc64_elf_sectoff_reloc),
593
594   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
595   HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596        ppc64_elf_toc_reloc),
597
598   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
599   HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600        ppc64_elf_toc_reloc),
601
602   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
603   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
604   HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
605        ppc64_elf_unhandled_reloc),
606
607   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
608   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
609   HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
610        ppc64_elf_unhandled_reloc),
611
612   /* Marker relocs for TLS.  */
613   HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
614        bfd_elf_generic_reloc),
615
616   HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
617        bfd_elf_generic_reloc),
618
619   HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
620        bfd_elf_generic_reloc),
621
622   /* Marker reloc for optimizing r2 save in prologue rather than on
623      each plt call stub.  */
624   HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
625        bfd_elf_generic_reloc),
626
627   /* Marker relocs on inline plt call instructions.  */
628   HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
629        bfd_elf_generic_reloc),
630
631   HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
632        bfd_elf_generic_reloc),
633
634   /* Computes the load module index of the load module that contains the
635      definition of its TLS sym.  */
636   HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
637        ppc64_elf_unhandled_reloc),
638
639   /* Computes a dtv-relative displacement, the difference between the value
640      of sym+add and the base address of the thread-local storage block that
641      contains the definition of sym, minus 0x8000.  */
642   HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
643        ppc64_elf_unhandled_reloc),
644
645   /* A 16 bit dtprel reloc.  */
646   HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
647        ppc64_elf_unhandled_reloc),
648
649   /* Like DTPREL16, but no overflow.  */
650   HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
651        ppc64_elf_unhandled_reloc),
652
653   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
654   HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
655        ppc64_elf_unhandled_reloc),
656
657   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
658   HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
659        ppc64_elf_unhandled_reloc),
660
661   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
662   HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
663        ppc64_elf_unhandled_reloc),
664
665   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
666   HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
667        ppc64_elf_unhandled_reloc),
668
669   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
670   HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
671        ppc64_elf_unhandled_reloc),
672
673   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
674   HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
675        ppc64_elf_unhandled_reloc),
676
677   /* Like DTPREL16, but for insns with a DS field.  */
678   HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
679        ppc64_elf_unhandled_reloc),
680
681   /* Like DTPREL16_DS, but no overflow.  */
682   HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
683        ppc64_elf_unhandled_reloc),
684
685   /* Computes a tp-relative displacement, the difference between the value of
686      sym+add and the value of the thread pointer (r13).  */
687   HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
688        ppc64_elf_unhandled_reloc),
689
690   /* A 16 bit tprel reloc.  */
691   HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
692        ppc64_elf_unhandled_reloc),
693
694   /* Like TPREL16, but no overflow.  */
695   HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
696        ppc64_elf_unhandled_reloc),
697
698   /* Like TPREL16_LO, but next higher group of 16 bits.  */
699   HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
700        ppc64_elf_unhandled_reloc),
701
702   /* Like TPREL16_HI, but adjust for low 16 bits.  */
703   HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
704        ppc64_elf_unhandled_reloc),
705
706   /* Like TPREL16_HI, but next higher group of 16 bits.  */
707   HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
708        ppc64_elf_unhandled_reloc),
709
710   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
711   HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
712        ppc64_elf_unhandled_reloc),
713
714   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
715   HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
716        ppc64_elf_unhandled_reloc),
717
718   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
719   HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
720        ppc64_elf_unhandled_reloc),
721
722   /* Like TPREL16, but for insns with a DS field.  */
723   HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
724        ppc64_elf_unhandled_reloc),
725
726   /* Like TPREL16_DS, but no overflow.  */
727   HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
728        ppc64_elf_unhandled_reloc),
729
730   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
731      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
732      to the first entry relative to the TOC base (r2).  */
733   HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
734        ppc64_elf_unhandled_reloc),
735
736   /* Like GOT_TLSGD16, but no overflow.  */
737   HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
738        ppc64_elf_unhandled_reloc),
739
740   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
741   HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
742        ppc64_elf_unhandled_reloc),
743
744   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
745   HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
746        ppc64_elf_unhandled_reloc),
747
748   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
749      with values (sym+add)@dtpmod and zero, and computes the offset to the
750      first entry relative to the TOC base (r2).  */
751   HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
752        ppc64_elf_unhandled_reloc),
753
754   /* Like GOT_TLSLD16, but no overflow.  */
755   HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
756        ppc64_elf_unhandled_reloc),
757
758   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
759   HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
760        ppc64_elf_unhandled_reloc),
761
762   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
763   HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
764        ppc64_elf_unhandled_reloc),
765
766   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
767      the offset to the entry relative to the TOC base (r2).  */
768   HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
769        ppc64_elf_unhandled_reloc),
770
771   /* Like GOT_DTPREL16_DS, but no overflow.  */
772   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
773        ppc64_elf_unhandled_reloc),
774
775   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
776   HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
777        ppc64_elf_unhandled_reloc),
778
779   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
780   HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
781        ppc64_elf_unhandled_reloc),
782
783   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
784      offset to the entry relative to the TOC base (r2).  */
785   HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
786        ppc64_elf_unhandled_reloc),
787
788   /* Like GOT_TPREL16_DS, but no overflow.  */
789   HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
790        ppc64_elf_unhandled_reloc),
791
792   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
793   HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
794        ppc64_elf_unhandled_reloc),
795
796   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
797   HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
798        ppc64_elf_unhandled_reloc),
799
800   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
801        ppc64_elf_unhandled_reloc),
802
803   HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
804        bfd_elf_generic_reloc),
805
806   /* A 16 bit relative relocation.  */
807   HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
808        bfd_elf_generic_reloc),
809
810   /* A 16 bit relative relocation without overflow.  */
811   HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
812        bfd_elf_generic_reloc),
813
814   /* The high order 16 bits of a relative address.  */
815   HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
816        bfd_elf_generic_reloc),
817
818   /* The high order 16 bits of a relative address, plus 1 if the contents of
819      the low 16 bits, treated as a signed number, is negative.  */
820   HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
821        ppc64_elf_ha_reloc),
822
823   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
824   HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
825        ppc64_elf_ha_reloc),
826
827   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
828   HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
829        ppc64_elf_ha_reloc),
830
831   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
832   HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
833        bfd_elf_generic_reloc),
834
835   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
836   HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
837        ppc64_elf_ha_reloc),
838
839   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
840   HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
841        ppc64_elf_unhandled_reloc),
842
843   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
844   HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
845        ppc64_elf_unhandled_reloc),
846
847   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
848   HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
849        ppc64_elf_unhandled_reloc),
850
851   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
852   HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
853        ppc64_elf_unhandled_reloc),
854
855   /* Marker reloc on ELFv2 large-model function entry.  */
856   HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
857        bfd_elf_generic_reloc),
858
859   /* Like ADDR64, but use local entry point of function.  */
860   HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
861        bfd_elf_generic_reloc),
862
863   /* GNU extension to record C++ vtable hierarchy.  */
864   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
865        NULL),
866
867   /* GNU extension to record C++ vtable member usage.  */
868   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
869        NULL),
870 };
871
872 \f
873 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
874    be done.  */
875
876 static void
877 ppc_howto_init (void)
878 {
879   unsigned int i, type;
880
881   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
882     {
883       type = ppc64_elf_howto_raw[i].type;
884       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
885       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
886     }
887 }
888
889 static reloc_howto_type *
890 ppc64_elf_reloc_type_lookup (bfd *abfd,
891                              bfd_reloc_code_real_type code)
892 {
893   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
894
895   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
896     /* Initialize howto table if needed.  */
897     ppc_howto_init ();
898
899   switch (code)
900     {
901     default:
902       /* xgettext:c-format */
903       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
904                           (int) code);
905       bfd_set_error (bfd_error_bad_value);
906       return NULL;
907
908     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
909       break;
910     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
911       break;
912     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
913       break;
914     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
915       break;
916     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
917       break;
918     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
919       break;
920     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
921       break;
922     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
923       break;
924     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
925       break;
926     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
927       break;
928     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
929       break;
930     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
931       break;
932     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
933       break;
934     case BFD_RELOC_PPC64_REL24_NOTOC:           r = R_PPC64_REL24_NOTOC;
935       break;
936     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
937       break;
938     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
939       break;
940     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
941       break;
942     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
943       break;
944     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
945       break;
946     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
947       break;
948     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
949       break;
950     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
951       break;
952     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
953       break;
954     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
955       break;
956     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
957       break;
958     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
959       break;
960     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
961       break;
962     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
963       break;
964     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
965       break;
966     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
967       break;
968     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
969       break;
970     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
971       break;
972     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
973       break;
974     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
975       break;
976     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
977       break;
978     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
979       break;
980     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
981       break;
982     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
983       break;
984     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
985       break;
986     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
987       break;
988     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
989       break;
990     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
991       break;
992     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
993       break;
994     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
995       break;
996     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
997       break;
998     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
999       break;
1000     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
1001       break;
1002     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
1003       break;
1004     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
1005       break;
1006     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
1007       break;
1008     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
1009       break;
1010     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
1011       break;
1012     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
1013       break;
1014     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
1015       break;
1016     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
1017       break;
1018     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
1019       break;
1020     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
1021       break;
1022     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
1023       break;
1024     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
1025       break;
1026     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
1027       break;
1028     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
1029       break;
1030     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
1031       break;
1032     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
1033       break;
1034     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
1035       break;
1036     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
1037       break;
1038     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
1039       break;
1040     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
1041       break;
1042     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
1043       break;
1044     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
1045       break;
1046     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
1047       break;
1048     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
1049       break;
1050     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
1051       break;
1052     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
1053       break;
1054     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
1055       break;
1056     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
1057       break;
1058     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
1059       break;
1060     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
1061       break;
1062     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
1063       break;
1064     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
1065       break;
1066     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
1067       break;
1068     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
1069       break;
1070     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
1071       break;
1072     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
1073       break;
1074     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
1075       break;
1076     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
1077       break;
1078     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
1079       break;
1080     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
1081       break;
1082     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
1083       break;
1084     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
1085       break;
1086     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
1087       break;
1088     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
1089       break;
1090     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
1091       break;
1092     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
1093       break;
1094     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
1095       break;
1096     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
1097       break;
1098     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
1099       break;
1100     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
1101       break;
1102     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
1103       break;
1104     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
1105       break;
1106     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
1107       break;
1108     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
1109       break;
1110     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
1111       break;
1112     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
1113       break;
1114     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
1115       break;
1116     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
1117       break;
1118     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
1119       break;
1120     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
1121       break;
1122     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
1123       break;
1124     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
1125       break;
1126     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
1127       break;
1128     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
1129       break;
1130     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
1131       break;
1132     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
1133       break;
1134     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
1135       break;
1136     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
1137       break;
1138     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
1139       break;
1140     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
1141       break;
1142     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
1143       break;
1144     }
1145
1146   return ppc64_elf_howto_table[r];
1147 };
1148
1149 static reloc_howto_type *
1150 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1151                              const char *r_name)
1152 {
1153   unsigned int i;
1154
1155   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1156     if (ppc64_elf_howto_raw[i].name != NULL
1157         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1158       return &ppc64_elf_howto_raw[i];
1159
1160   return NULL;
1161 }
1162
1163 /* Set the howto pointer for a PowerPC ELF reloc.  */
1164
1165 static bfd_boolean
1166 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1167                          Elf_Internal_Rela *dst)
1168 {
1169   unsigned int type;
1170
1171   /* Initialize howto table if needed.  */
1172   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1173     ppc_howto_init ();
1174
1175   type = ELF64_R_TYPE (dst->r_info);
1176   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1177     {
1178       /* xgettext:c-format */
1179       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1180                           abfd, type);
1181       bfd_set_error (bfd_error_bad_value);
1182       return FALSE;
1183     }
1184   cache_ptr->howto = ppc64_elf_howto_table[type];
1185   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1186     {
1187       /* xgettext:c-format */
1188       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1189                           abfd, type);
1190       bfd_set_error (bfd_error_bad_value);
1191       return FALSE;
1192     }
1193
1194   return TRUE;
1195 }
1196
1197 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
1198
1199 static bfd_reloc_status_type
1200 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1201                     void *data, asection *input_section,
1202                     bfd *output_bfd, char **error_message)
1203 {
1204   enum elf_ppc64_reloc_type r_type;
1205   long insn;
1206   bfd_size_type octets;
1207   bfd_vma value;
1208
1209   /* If this is a relocatable link (output_bfd test tells us), just
1210      call the generic function.  Any adjustment will be done at final
1211      link time.  */
1212   if (output_bfd != NULL)
1213     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1214                                   input_section, output_bfd, error_message);
1215
1216   /* Adjust the addend for sign extension of the low 16 bits.
1217      We won't actually be using the low 16 bits, so trashing them
1218      doesn't matter.  */
1219   reloc_entry->addend += 0x8000;
1220   r_type = reloc_entry->howto->type;
1221   if (r_type != R_PPC64_REL16DX_HA)
1222     return bfd_reloc_continue;
1223
1224   value = 0;
1225   if (!bfd_is_com_section (symbol->section))
1226     value = symbol->value;
1227   value += (reloc_entry->addend
1228             + symbol->section->output_offset
1229             + symbol->section->output_section->vma);
1230   value -= (reloc_entry->address
1231             + input_section->output_offset
1232             + input_section->output_section->vma);
1233   value = (bfd_signed_vma) value >> 16;
1234
1235   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1236   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1237   insn &= ~0x1fffc1;
1238   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1239   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1240   if (value + 0x8000 > 0xffff)
1241     return bfd_reloc_overflow;
1242   return bfd_reloc_ok;
1243 }
1244
1245 static bfd_reloc_status_type
1246 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1247                         void *data, asection *input_section,
1248                         bfd *output_bfd, char **error_message)
1249 {
1250   if (output_bfd != NULL)
1251     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1252                                   input_section, output_bfd, error_message);
1253
1254   if (strcmp (symbol->section->name, ".opd") == 0
1255       && (symbol->section->owner->flags & DYNAMIC) == 0)
1256     {
1257       bfd_vma dest = opd_entry_value (symbol->section,
1258                                       symbol->value + reloc_entry->addend,
1259                                       NULL, NULL, FALSE);
1260       if (dest != (bfd_vma) -1)
1261         reloc_entry->addend = dest - (symbol->value
1262                                       + symbol->section->output_section->vma
1263                                       + symbol->section->output_offset);
1264     }
1265   else
1266     {
1267       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1268
1269       if (symbol->section->owner != abfd
1270           && symbol->section->owner != NULL
1271           && abiversion (symbol->section->owner) >= 2)
1272         {
1273           unsigned int i;
1274
1275           for (i = 0; i < symbol->section->owner->symcount; ++i)
1276             {
1277               asymbol *symdef = symbol->section->owner->outsymbols[i];
1278
1279               if (strcmp (symdef->name, symbol->name) == 0)
1280                 {
1281                   elfsym = (elf_symbol_type *) symdef;
1282                   break;
1283                 }
1284             }
1285         }
1286       reloc_entry->addend
1287         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1288     }
1289   return bfd_reloc_continue;
1290 }
1291
1292 static bfd_reloc_status_type
1293 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1294                          void *data, asection *input_section,
1295                          bfd *output_bfd, char **error_message)
1296 {
1297   long insn;
1298   enum elf_ppc64_reloc_type r_type;
1299   bfd_size_type octets;
1300   /* Assume 'at' branch hints.  */
1301   bfd_boolean is_isa_v2 = TRUE;
1302
1303   /* If this is a relocatable link (output_bfd test tells us), just
1304      call the generic function.  Any adjustment will be done at final
1305      link time.  */
1306   if (output_bfd != NULL)
1307     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1308                                   input_section, output_bfd, error_message);
1309
1310   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1311   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1312   insn &= ~(0x01 << 21);
1313   r_type = reloc_entry->howto->type;
1314   if (r_type == R_PPC64_ADDR14_BRTAKEN
1315       || r_type == R_PPC64_REL14_BRTAKEN)
1316     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
1317
1318   if (is_isa_v2)
1319     {
1320       /* Set 'a' bit.  This is 0b00010 in BO field for branch
1321          on CR(BI) insns (BO == 001at or 011at), and 0b01000
1322          for branch on CTR insns (BO == 1a00t or 1a01t).  */
1323       if ((insn & (0x14 << 21)) == (0x04 << 21))
1324         insn |= 0x02 << 21;
1325       else if ((insn & (0x14 << 21)) == (0x10 << 21))
1326         insn |= 0x08 << 21;
1327       else
1328         goto out;
1329     }
1330   else
1331     {
1332       bfd_vma target = 0;
1333       bfd_vma from;
1334
1335       if (!bfd_is_com_section (symbol->section))
1336         target = symbol->value;
1337       target += symbol->section->output_section->vma;
1338       target += symbol->section->output_offset;
1339       target += reloc_entry->addend;
1340
1341       from = (reloc_entry->address
1342               + input_section->output_offset
1343               + input_section->output_section->vma);
1344
1345       /* Invert 'y' bit if not the default.  */
1346       if ((bfd_signed_vma) (target - from) < 0)
1347         insn ^= 0x01 << 21;
1348     }
1349   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1350  out:
1351   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1352                                  input_section, output_bfd, error_message);
1353 }
1354
1355 static bfd_reloc_status_type
1356 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1357                          void *data, asection *input_section,
1358                          bfd *output_bfd, char **error_message)
1359 {
1360   /* If this is a relocatable link (output_bfd test tells us), just
1361      call the generic function.  Any adjustment will be done at final
1362      link time.  */
1363   if (output_bfd != NULL)
1364     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1365                                   input_section, output_bfd, error_message);
1366
1367   /* Subtract the symbol section base address.  */
1368   reloc_entry->addend -= symbol->section->output_section->vma;
1369   return bfd_reloc_continue;
1370 }
1371
1372 static bfd_reloc_status_type
1373 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1374                             void *data, asection *input_section,
1375                             bfd *output_bfd, char **error_message)
1376 {
1377   /* If this is a relocatable link (output_bfd test tells us), just
1378      call the generic function.  Any adjustment will be done at final
1379      link time.  */
1380   if (output_bfd != NULL)
1381     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1382                                   input_section, output_bfd, error_message);
1383
1384   /* Subtract the symbol section base address.  */
1385   reloc_entry->addend -= symbol->section->output_section->vma;
1386
1387   /* Adjust the addend for sign extension of the low 16 bits.  */
1388   reloc_entry->addend += 0x8000;
1389   return bfd_reloc_continue;
1390 }
1391
1392 static bfd_reloc_status_type
1393 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1394                      void *data, asection *input_section,
1395                      bfd *output_bfd, char **error_message)
1396 {
1397   bfd_vma TOCstart;
1398
1399   /* If this is a relocatable link (output_bfd test tells us), just
1400      call the generic function.  Any adjustment will be done at final
1401      link time.  */
1402   if (output_bfd != NULL)
1403     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1404                                   input_section, output_bfd, error_message);
1405
1406   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1407   if (TOCstart == 0)
1408     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1409
1410   /* Subtract the TOC base address.  */
1411   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1412   return bfd_reloc_continue;
1413 }
1414
1415 static bfd_reloc_status_type
1416 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1417                         void *data, asection *input_section,
1418                         bfd *output_bfd, char **error_message)
1419 {
1420   bfd_vma TOCstart;
1421
1422   /* If this is a relocatable link (output_bfd test tells us), just
1423      call the generic function.  Any adjustment will be done at final
1424      link time.  */
1425   if (output_bfd != NULL)
1426     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1427                                   input_section, output_bfd, error_message);
1428
1429   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1430   if (TOCstart == 0)
1431     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1432
1433   /* Subtract the TOC base address.  */
1434   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1435
1436   /* Adjust the addend for sign extension of the low 16 bits.  */
1437   reloc_entry->addend += 0x8000;
1438   return bfd_reloc_continue;
1439 }
1440
1441 static bfd_reloc_status_type
1442 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1443                        void *data, asection *input_section,
1444                        bfd *output_bfd, char **error_message)
1445 {
1446   bfd_vma TOCstart;
1447   bfd_size_type octets;
1448
1449   /* If this is a relocatable link (output_bfd test tells us), just
1450      call the generic function.  Any adjustment will be done at final
1451      link time.  */
1452   if (output_bfd != NULL)
1453     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1454                                   input_section, output_bfd, error_message);
1455
1456   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1457   if (TOCstart == 0)
1458     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1459
1460   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1461   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1462   return bfd_reloc_ok;
1463 }
1464
1465 static bfd_reloc_status_type
1466 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1467                            void *data, asection *input_section,
1468                            bfd *output_bfd, char **error_message)
1469 {
1470   /* If this is a relocatable link (output_bfd test tells us), just
1471      call the generic function.  Any adjustment will be done at final
1472      link time.  */
1473   if (output_bfd != NULL)
1474     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1475                                   input_section, output_bfd, error_message);
1476
1477   if (error_message != NULL)
1478     {
1479       static char buf[60];
1480       sprintf (buf, "generic linker can't handle %s",
1481                reloc_entry->howto->name);
1482       *error_message = buf;
1483     }
1484   return bfd_reloc_dangerous;
1485 }
1486
1487 /* Track GOT entries needed for a given symbol.  We might need more
1488    than one got entry per symbol.  */
1489 struct got_entry
1490 {
1491   struct got_entry *next;
1492
1493   /* The symbol addend that we'll be placing in the GOT.  */
1494   bfd_vma addend;
1495
1496   /* Unlike other ELF targets, we use separate GOT entries for the same
1497      symbol referenced from different input files.  This is to support
1498      automatic multiple TOC/GOT sections, where the TOC base can vary
1499      from one input file to another.  After partitioning into TOC groups
1500      we merge entries within the group.
1501
1502      Point to the BFD owning this GOT entry.  */
1503   bfd *owner;
1504
1505   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1506      TLS_TPREL or TLS_DTPREL for tls entries.  */
1507   unsigned char tls_type;
1508
1509   /* Non-zero if got.ent points to real entry.  */
1510   unsigned char is_indirect;
1511
1512   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
1513   union
1514   {
1515     bfd_signed_vma refcount;
1516     bfd_vma offset;
1517     struct got_entry *ent;
1518   } got;
1519 };
1520
1521 /* The same for PLT.  */
1522 struct plt_entry
1523 {
1524   struct plt_entry *next;
1525
1526   bfd_vma addend;
1527
1528   union
1529   {
1530     bfd_signed_vma refcount;
1531     bfd_vma offset;
1532   } plt;
1533 };
1534
1535 struct ppc64_elf_obj_tdata
1536 {
1537   struct elf_obj_tdata elf;
1538
1539   /* Shortcuts to dynamic linker sections.  */
1540   asection *got;
1541   asection *relgot;
1542
1543   /* Used during garbage collection.  We attach global symbols defined
1544      on removed .opd entries to this section so that the sym is removed.  */
1545   asection *deleted_section;
1546
1547   /* TLS local dynamic got entry handling.  Support for multiple GOT
1548      sections means we potentially need one of these for each input bfd.  */
1549   struct got_entry tlsld_got;
1550
1551   union
1552   {
1553     /* A copy of relocs before they are modified for --emit-relocs.  */
1554     Elf_Internal_Rela *relocs;
1555
1556     /* Section contents.  */
1557     bfd_byte *contents;
1558   } opd;
1559
1560   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1561      the reloc to be in the range -32768 to 32767.  */
1562   unsigned int has_small_toc_reloc : 1;
1563
1564   /* Set if toc/got ha relocs detected not using r2, or lo reloc
1565      instruction not one we handle.  */
1566   unsigned int unexpected_toc_insn : 1;
1567 };
1568
1569 #define ppc64_elf_tdata(bfd) \
1570   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1571
1572 #define ppc64_tlsld_got(bfd) \
1573   (&ppc64_elf_tdata (bfd)->tlsld_got)
1574
1575 #define is_ppc64_elf(bfd) \
1576   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1577    && elf_object_id (bfd) == PPC64_ELF_DATA)
1578
1579 /* Override the generic function because we store some extras.  */
1580
1581 static bfd_boolean
1582 ppc64_elf_mkobject (bfd *abfd)
1583 {
1584   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1585                                   PPC64_ELF_DATA);
1586 }
1587
1588 /* Fix bad default arch selected for a 64 bit input bfd when the
1589    default is 32 bit.  Also select arch based on apuinfo.  */
1590
1591 static bfd_boolean
1592 ppc64_elf_object_p (bfd *abfd)
1593 {
1594   if (!abfd->arch_info->the_default)
1595     return TRUE;
1596
1597   if (abfd->arch_info->bits_per_word == 32)
1598     {
1599       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1600
1601       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1602         {
1603           /* Relies on arch after 32 bit default being 64 bit default.  */
1604           abfd->arch_info = abfd->arch_info->next;
1605           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1606         }
1607     }
1608   return _bfd_elf_ppc_set_arch (abfd);
1609 }
1610
1611 /* Support for core dump NOTE sections.  */
1612
1613 static bfd_boolean
1614 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1615 {
1616   size_t offset, size;
1617
1618   if (note->descsz != 504)
1619     return FALSE;
1620
1621   /* pr_cursig */
1622   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1623
1624   /* pr_pid */
1625   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1626
1627   /* pr_reg */
1628   offset = 112;
1629   size = 384;
1630
1631   /* Make a ".reg/999" section.  */
1632   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1633                                           size, note->descpos + offset);
1634 }
1635
1636 static bfd_boolean
1637 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1638 {
1639   if (note->descsz != 136)
1640     return FALSE;
1641
1642   elf_tdata (abfd)->core->pid
1643     = bfd_get_32 (abfd, note->descdata + 24);
1644   elf_tdata (abfd)->core->program
1645     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1646   elf_tdata (abfd)->core->command
1647     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1648
1649   return TRUE;
1650 }
1651
1652 static char *
1653 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1654                            ...)
1655 {
1656   switch (note_type)
1657     {
1658     default:
1659       return NULL;
1660
1661     case NT_PRPSINFO:
1662       {
1663         char data[136] ATTRIBUTE_NONSTRING;
1664         va_list ap;
1665
1666         va_start (ap, note_type);
1667         memset (data, 0, sizeof (data));
1668         strncpy (data + 40, va_arg (ap, const char *), 16);
1669 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1670         DIAGNOSTIC_PUSH;
1671         /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1672            -Wstringop-truncation:
1673            https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1674          */
1675         DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1676 #endif
1677         strncpy (data + 56, va_arg (ap, const char *), 80);
1678 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1679         DIAGNOSTIC_POP;
1680 #endif
1681         va_end (ap);
1682         return elfcore_write_note (abfd, buf, bufsiz,
1683                                    "CORE", note_type, data, sizeof (data));
1684       }
1685
1686     case NT_PRSTATUS:
1687       {
1688         char data[504];
1689         va_list ap;
1690         long pid;
1691         int cursig;
1692         const void *greg;
1693
1694         va_start (ap, note_type);
1695         memset (data, 0, 112);
1696         pid = va_arg (ap, long);
1697         bfd_put_32 (abfd, pid, data + 32);
1698         cursig = va_arg (ap, int);
1699         bfd_put_16 (abfd, cursig, data + 12);
1700         greg = va_arg (ap, const void *);
1701         memcpy (data + 112, greg, 384);
1702         memset (data + 496, 0, 8);
1703         va_end (ap);
1704         return elfcore_write_note (abfd, buf, bufsiz,
1705                                    "CORE", note_type, data, sizeof (data));
1706       }
1707     }
1708 }
1709
1710 /* Add extra PPC sections.  */
1711
1712 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1713 {
1714   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
1715   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1716   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1717   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1718   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1719   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1720   { NULL,                     0,  0, 0,            0 }
1721 };
1722
1723 enum _ppc64_sec_type {
1724   sec_normal = 0,
1725   sec_opd = 1,
1726   sec_toc = 2
1727 };
1728
1729 struct _ppc64_elf_section_data
1730 {
1731   struct bfd_elf_section_data elf;
1732
1733   union
1734   {
1735     /* An array with one entry for each opd function descriptor,
1736        and some spares since opd entries may be either 16 or 24 bytes.  */
1737 #define OPD_NDX(OFF) ((OFF) >> 4)
1738     struct _opd_sec_data
1739     {
1740       /* Points to the function code section for local opd entries.  */
1741       asection **func_sec;
1742
1743       /* After editing .opd, adjust references to opd local syms.  */
1744       long *adjust;
1745     } opd;
1746
1747     /* An array for toc sections, indexed by offset/8.  */
1748     struct _toc_sec_data
1749     {
1750       /* Specifies the relocation symbol index used at a given toc offset.  */
1751       unsigned *symndx;
1752
1753       /* And the relocation addend.  */
1754       bfd_vma *add;
1755     } toc;
1756   } u;
1757
1758   enum _ppc64_sec_type sec_type:2;
1759
1760   /* Flag set when small branches are detected.  Used to
1761      select suitable defaults for the stub group size.  */
1762   unsigned int has_14bit_branch:1;
1763
1764   /* Flag set when PLTCALL relocs are detected.  */
1765   unsigned int has_pltcall:1;
1766 };
1767
1768 #define ppc64_elf_section_data(sec) \
1769   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
1770
1771 static bfd_boolean
1772 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
1773 {
1774   if (!sec->used_by_bfd)
1775     {
1776       struct _ppc64_elf_section_data *sdata;
1777       bfd_size_type amt = sizeof (*sdata);
1778
1779       sdata = bfd_zalloc (abfd, amt);
1780       if (sdata == NULL)
1781         return FALSE;
1782       sec->used_by_bfd = sdata;
1783     }
1784
1785   return _bfd_elf_new_section_hook (abfd, sec);
1786 }
1787
1788 static struct _opd_sec_data *
1789 get_opd_info (asection * sec)
1790 {
1791   if (sec != NULL
1792       && ppc64_elf_section_data (sec) != NULL
1793       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
1794     return &ppc64_elf_section_data (sec)->u.opd;
1795   return NULL;
1796 }
1797 \f
1798 /* Parameters for the qsort hook.  */
1799 static bfd_boolean synthetic_relocatable;
1800 static asection *synthetic_opd;
1801
1802 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
1803
1804 static int
1805 compare_symbols (const void *ap, const void *bp)
1806 {
1807   const asymbol *a = *(const asymbol **) ap;
1808   const asymbol *b = *(const asymbol **) bp;
1809
1810   /* Section symbols first.  */
1811   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
1812     return -1;
1813   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
1814     return 1;
1815
1816   /* then .opd symbols.  */
1817   if (synthetic_opd != NULL)
1818     {
1819       if (strcmp (a->section->name, ".opd") == 0
1820           && strcmp (b->section->name, ".opd") != 0)
1821         return -1;
1822       if (strcmp (a->section->name, ".opd") != 0
1823           && strcmp (b->section->name, ".opd") == 0)
1824         return 1;
1825     }
1826
1827   /* then other code symbols.  */
1828   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1829        == (SEC_CODE | SEC_ALLOC))
1830       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1831           != (SEC_CODE | SEC_ALLOC)))
1832     return -1;
1833
1834   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1835        != (SEC_CODE | SEC_ALLOC))
1836       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
1837           == (SEC_CODE | SEC_ALLOC)))
1838     return 1;
1839
1840   if (synthetic_relocatable)
1841     {
1842       if (a->section->id < b->section->id)
1843         return -1;
1844
1845       if (a->section->id > b->section->id)
1846         return 1;
1847     }
1848
1849   if (a->value + a->section->vma < b->value + b->section->vma)
1850     return -1;
1851
1852   if (a->value + a->section->vma > b->value + b->section->vma)
1853     return 1;
1854
1855   /* For syms with the same value, prefer strong dynamic global function
1856      syms over other syms.  */
1857   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
1858     return -1;
1859
1860   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
1861     return 1;
1862
1863   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
1864     return -1;
1865
1866   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
1867     return 1;
1868
1869   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
1870     return -1;
1871
1872   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
1873     return 1;
1874
1875   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
1876     return -1;
1877
1878   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
1879     return 1;
1880
1881   return a > b;
1882 }
1883
1884 /* Search SYMS for a symbol of the given VALUE.  */
1885
1886 static asymbol *
1887 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
1888 {
1889   long mid;
1890
1891   if (id == (unsigned) -1)
1892     {
1893       while (lo < hi)
1894         {
1895           mid = (lo + hi) >> 1;
1896           if (syms[mid]->value + syms[mid]->section->vma < value)
1897             lo = mid + 1;
1898           else if (syms[mid]->value + syms[mid]->section->vma > value)
1899             hi = mid;
1900           else
1901             return syms[mid];
1902         }
1903     }
1904   else
1905     {
1906       while (lo < hi)
1907         {
1908           mid = (lo + hi) >> 1;
1909           if (syms[mid]->section->id < id)
1910             lo = mid + 1;
1911           else if (syms[mid]->section->id > id)
1912             hi = mid;
1913           else if (syms[mid]->value < value)
1914             lo = mid + 1;
1915           else if (syms[mid]->value > value)
1916             hi = mid;
1917           else
1918             return syms[mid];
1919         }
1920     }
1921   return NULL;
1922 }
1923
1924 static bfd_boolean
1925 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
1926 {
1927   bfd_vma vma = *(bfd_vma *) ptr;
1928   return ((section->flags & SEC_ALLOC) != 0
1929           && section->vma <= vma
1930           && vma < section->vma + section->size);
1931 }
1932
1933 /* Create synthetic symbols, effectively restoring "dot-symbol" function
1934    entry syms.  Also generate @plt symbols for the glink branch table.
1935    Returns count of synthetic symbols in RET or -1 on error.  */
1936
1937 static long
1938 ppc64_elf_get_synthetic_symtab (bfd *abfd,
1939                                 long static_count, asymbol **static_syms,
1940                                 long dyn_count, asymbol **dyn_syms,
1941                                 asymbol **ret)
1942 {
1943   asymbol *s;
1944   size_t i, j, count;
1945   char *names;
1946   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
1947   asection *opd = NULL;
1948   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1949   asymbol **syms;
1950   int abi = abiversion (abfd);
1951
1952   *ret = NULL;
1953
1954   if (abi < 2)
1955     {
1956       opd = bfd_get_section_by_name (abfd, ".opd");
1957       if (opd == NULL && abi == 1)
1958         return 0;
1959     }
1960
1961   syms = NULL;
1962   codesecsym = 0;
1963   codesecsymend = 0;
1964   secsymend = 0;
1965   opdsymend = 0;
1966   symcount = 0;
1967   if (opd != NULL)
1968     {
1969       symcount = static_count;
1970       if (!relocatable)
1971         symcount += dyn_count;
1972       if (symcount == 0)
1973         return 0;
1974
1975       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
1976       if (syms == NULL)
1977         return -1;
1978
1979       if (!relocatable && static_count != 0 && dyn_count != 0)
1980         {
1981           /* Use both symbol tables.  */
1982           memcpy (syms, static_syms, static_count * sizeof (*syms));
1983           memcpy (syms + static_count, dyn_syms,
1984                   (dyn_count + 1) * sizeof (*syms));
1985         }
1986       else if (!relocatable && static_count == 0)
1987         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
1988       else
1989         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
1990
1991       /* Trim uninteresting symbols.  Interesting symbols are section,
1992          function, and notype symbols.  */
1993       for (i = 0, j = 0; i < symcount; ++i)
1994         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
1995                                | BSF_RELC | BSF_SRELC)) == 0)
1996           syms[j++] = syms[i];
1997       symcount = j;
1998
1999       synthetic_relocatable = relocatable;
2000       synthetic_opd = opd;
2001       qsort (syms, symcount, sizeof (*syms), compare_symbols);
2002
2003       if (!relocatable && symcount > 1)
2004         {
2005           /* Trim duplicate syms, since we may have merged the normal
2006              and dynamic symbols.  Actually, we only care about syms
2007              that have different values, so trim any with the same
2008              value.  Don't consider ifunc and ifunc resolver symbols
2009              duplicates however, because GDB wants to know whether a
2010              text symbol is an ifunc resolver.  */
2011           for (i = 1, j = 1; i < symcount; ++i)
2012             {
2013               const asymbol *s0 = syms[i - 1];
2014               const asymbol *s1 = syms[i];
2015
2016               if ((s0->value + s0->section->vma
2017                    != s1->value + s1->section->vma)
2018                   || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2019                       != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2020                 syms[j++] = syms[i];
2021             }
2022           symcount = j;
2023         }
2024
2025       i = 0;
2026       /* Note that here and in compare_symbols we can't compare opd and
2027          sym->section directly.  With separate debug info files, the
2028          symbols will be extracted from the debug file while abfd passed
2029          to this function is the real binary.  */
2030       if (strcmp (syms[i]->section->name, ".opd") == 0)
2031         ++i;
2032       codesecsym = i;
2033
2034       for (; i < symcount; ++i)
2035         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2036                                          | SEC_THREAD_LOCAL))
2037              != (SEC_CODE | SEC_ALLOC))
2038             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2039           break;
2040       codesecsymend = i;
2041
2042       for (; i < symcount; ++i)
2043         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2044           break;
2045       secsymend = i;
2046
2047       for (; i < symcount; ++i)
2048         if (strcmp (syms[i]->section->name, ".opd") != 0)
2049           break;
2050       opdsymend = i;
2051
2052       for (; i < symcount; ++i)
2053         if (((syms[i]->section->flags
2054               & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2055             != (SEC_CODE | SEC_ALLOC))
2056           break;
2057       symcount = i;
2058     }
2059   count = 0;
2060
2061   if (relocatable)
2062     {
2063       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2064       arelent *r;
2065       size_t size;
2066       size_t relcount;
2067
2068       if (opdsymend == secsymend)
2069         goto done;
2070
2071       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2072       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2073       if (relcount == 0)
2074         goto done;
2075
2076       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2077         {
2078           count = -1;
2079           goto done;
2080         }
2081
2082       size = 0;
2083       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2084         {
2085           asymbol *sym;
2086
2087           while (r < opd->relocation + relcount
2088                  && r->address < syms[i]->value + opd->vma)
2089             ++r;
2090
2091           if (r == opd->relocation + relcount)
2092             break;
2093
2094           if (r->address != syms[i]->value + opd->vma)
2095             continue;
2096
2097           if (r->howto->type != R_PPC64_ADDR64)
2098             continue;
2099
2100           sym = *r->sym_ptr_ptr;
2101           if (!sym_exists_at (syms, opdsymend, symcount,
2102                               sym->section->id, sym->value + r->addend))
2103             {
2104               ++count;
2105               size += sizeof (asymbol);
2106               size += strlen (syms[i]->name) + 2;
2107             }
2108         }
2109
2110       if (size == 0)
2111         goto done;
2112       s = *ret = bfd_malloc (size);
2113       if (s == NULL)
2114         {
2115           count = -1;
2116           goto done;
2117         }
2118
2119       names = (char *) (s + count);
2120
2121       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2122         {
2123           asymbol *sym;
2124
2125           while (r < opd->relocation + relcount
2126                  && r->address < syms[i]->value + opd->vma)
2127             ++r;
2128
2129           if (r == opd->relocation + relcount)
2130             break;
2131
2132           if (r->address != syms[i]->value + opd->vma)
2133             continue;
2134
2135           if (r->howto->type != R_PPC64_ADDR64)
2136             continue;
2137
2138           sym = *r->sym_ptr_ptr;
2139           if (!sym_exists_at (syms, opdsymend, symcount,
2140                               sym->section->id, sym->value + r->addend))
2141             {
2142               size_t len;
2143
2144               *s = *syms[i];
2145               s->flags |= BSF_SYNTHETIC;
2146               s->section = sym->section;
2147               s->value = sym->value + r->addend;
2148               s->name = names;
2149               *names++ = '.';
2150               len = strlen (syms[i]->name);
2151               memcpy (names, syms[i]->name, len + 1);
2152               names += len + 1;
2153               /* Have udata.p point back to the original symbol this
2154                  synthetic symbol was derived from.  */
2155               s->udata.p = syms[i];
2156               s++;
2157             }
2158         }
2159     }
2160   else
2161     {
2162       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2163       bfd_byte *contents = NULL;
2164       size_t size;
2165       size_t plt_count = 0;
2166       bfd_vma glink_vma = 0, resolv_vma = 0;
2167       asection *dynamic, *glink = NULL, *relplt = NULL;
2168       arelent *p;
2169
2170       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2171         {
2172         free_contents_and_exit_err:
2173           count = -1;
2174         free_contents_and_exit:
2175           if (contents)
2176             free (contents);
2177           goto done;
2178         }
2179
2180       size = 0;
2181       for (i = secsymend; i < opdsymend; ++i)
2182         {
2183           bfd_vma ent;
2184
2185           /* Ignore bogus symbols.  */
2186           if (syms[i]->value > opd->size - 8)
2187             continue;
2188
2189           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2190           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2191             {
2192               ++count;
2193               size += sizeof (asymbol);
2194               size += strlen (syms[i]->name) + 2;
2195             }
2196         }
2197
2198       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
2199       if (dyn_count != 0
2200           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2201         {
2202           bfd_byte *dynbuf, *extdyn, *extdynend;
2203           size_t extdynsize;
2204           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2205
2206           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2207             goto free_contents_and_exit_err;
2208
2209           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2210           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2211
2212           extdyn = dynbuf;
2213           extdynend = extdyn + dynamic->size;
2214           for (; extdyn < extdynend; extdyn += extdynsize)
2215             {
2216               Elf_Internal_Dyn dyn;
2217               (*swap_dyn_in) (abfd, extdyn, &dyn);
2218
2219               if (dyn.d_tag == DT_NULL)
2220                 break;
2221
2222               if (dyn.d_tag == DT_PPC64_GLINK)
2223                 {
2224                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2225                      See comment in ppc64_elf_finish_dynamic_sections. */
2226                   glink_vma = dyn.d_un.d_val + 8 * 4;
2227                   /* The .glink section usually does not survive the final
2228                      link; search for the section (usually .text) where the
2229                      glink stubs now reside.  */
2230                   glink = bfd_sections_find_if (abfd, section_covers_vma,
2231                                                 &glink_vma);
2232                   break;
2233                 }
2234             }
2235
2236           free (dynbuf);
2237         }
2238
2239       if (glink != NULL)
2240         {
2241           /* Determine __glink trampoline by reading the relative branch
2242              from the first glink stub.  */
2243           bfd_byte buf[4];
2244           unsigned int off = 0;
2245
2246           while (bfd_get_section_contents (abfd, glink, buf,
2247                                            glink_vma + off - glink->vma, 4))
2248             {
2249               unsigned int insn = bfd_get_32 (abfd, buf);
2250               insn ^= B_DOT;
2251               if ((insn & ~0x3fffffc) == 0)
2252                 {
2253                   resolv_vma
2254                     = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2255                   break;
2256                 }
2257               off += 4;
2258               if (off > 4)
2259                 break;
2260             }
2261
2262           if (resolv_vma)
2263             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2264
2265           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2266           if (relplt != NULL)
2267             {
2268               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2269               if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
2270                 goto free_contents_and_exit_err;
2271
2272               plt_count = relplt->size / sizeof (Elf64_External_Rela);
2273               size += plt_count * sizeof (asymbol);
2274
2275               p = relplt->relocation;
2276               for (i = 0; i < plt_count; i++, p++)
2277                 {
2278                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2279                   if (p->addend != 0)
2280                     size += sizeof ("+0x") - 1 + 16;
2281                 }
2282             }
2283         }
2284
2285       if (size == 0)
2286         goto free_contents_and_exit;
2287       s = *ret = bfd_malloc (size);
2288       if (s == NULL)
2289         goto free_contents_and_exit_err;
2290
2291       names = (char *) (s + count + plt_count + (resolv_vma != 0));
2292
2293       for (i = secsymend; i < opdsymend; ++i)
2294         {
2295           bfd_vma ent;
2296
2297           if (syms[i]->value > opd->size - 8)
2298             continue;
2299
2300           ent = bfd_get_64 (abfd, contents + syms[i]->value);
2301           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2302             {
2303               size_t lo, hi;
2304               size_t len;
2305               asection *sec = abfd->sections;
2306
2307               *s = *syms[i];
2308               lo = codesecsym;
2309               hi = codesecsymend;
2310               while (lo < hi)
2311                 {
2312                   size_t mid = (lo + hi) >> 1;
2313                   if (syms[mid]->section->vma < ent)
2314                     lo = mid + 1;
2315                   else if (syms[mid]->section->vma > ent)
2316                     hi = mid;
2317                   else
2318                     {
2319                       sec = syms[mid]->section;
2320                       break;
2321                     }
2322                 }
2323
2324               if (lo >= hi && lo > codesecsym)
2325                 sec = syms[lo - 1]->section;
2326
2327               for (; sec != NULL; sec = sec->next)
2328                 {
2329                   if (sec->vma > ent)
2330                     break;
2331                   /* SEC_LOAD may not be set if SEC is from a separate debug
2332                      info file.  */
2333                   if ((sec->flags & SEC_ALLOC) == 0)
2334                     break;
2335                   if ((sec->flags & SEC_CODE) != 0)
2336                     s->section = sec;
2337                 }
2338               s->flags |= BSF_SYNTHETIC;
2339               s->value = ent - s->section->vma;
2340               s->name = names;
2341               *names++ = '.';
2342               len = strlen (syms[i]->name);
2343               memcpy (names, syms[i]->name, len + 1);
2344               names += len + 1;
2345               /* Have udata.p point back to the original symbol this
2346                  synthetic symbol was derived from.  */
2347               s->udata.p = syms[i];
2348               s++;
2349             }
2350         }
2351       free (contents);
2352
2353       if (glink != NULL && relplt != NULL)
2354         {
2355           if (resolv_vma)
2356             {
2357               /* Add a symbol for the main glink trampoline.  */
2358               memset (s, 0, sizeof *s);
2359               s->the_bfd = abfd;
2360               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2361               s->section = glink;
2362               s->value = resolv_vma - glink->vma;
2363               s->name = names;
2364               memcpy (names, "__glink_PLTresolve",
2365                       sizeof ("__glink_PLTresolve"));
2366               names += sizeof ("__glink_PLTresolve");
2367               s++;
2368               count++;
2369             }
2370
2371           /* FIXME: It would be very much nicer to put sym@plt on the
2372              stub rather than on the glink branch table entry.  The
2373              objdump disassembler would then use a sensible symbol
2374              name on plt calls.  The difficulty in doing so is
2375              a) finding the stubs, and,
2376              b) matching stubs against plt entries, and,
2377              c) there can be multiple stubs for a given plt entry.
2378
2379              Solving (a) could be done by code scanning, but older
2380              ppc64 binaries used different stubs to current code.
2381              (b) is the tricky one since you need to known the toc
2382              pointer for at least one function that uses a pic stub to
2383              be able to calculate the plt address referenced.
2384              (c) means gdb would need to set multiple breakpoints (or
2385              find the glink branch itself) when setting breakpoints
2386              for pending shared library loads.  */
2387           p = relplt->relocation;
2388           for (i = 0; i < plt_count; i++, p++)
2389             {
2390               size_t len;
2391
2392               *s = **p->sym_ptr_ptr;
2393               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
2394                  we are defining a symbol, ensure one of them is set.  */
2395               if ((s->flags & BSF_LOCAL) == 0)
2396                 s->flags |= BSF_GLOBAL;
2397               s->flags |= BSF_SYNTHETIC;
2398               s->section = glink;
2399               s->value = glink_vma - glink->vma;
2400               s->name = names;
2401               s->udata.p = NULL;
2402               len = strlen ((*p->sym_ptr_ptr)->name);
2403               memcpy (names, (*p->sym_ptr_ptr)->name, len);
2404               names += len;
2405               if (p->addend != 0)
2406                 {
2407                   memcpy (names, "+0x", sizeof ("+0x") - 1);
2408                   names += sizeof ("+0x") - 1;
2409                   bfd_sprintf_vma (abfd, names, p->addend);
2410                   names += strlen (names);
2411                 }
2412               memcpy (names, "@plt", sizeof ("@plt"));
2413               names += sizeof ("@plt");
2414               s++;
2415               if (abi < 2)
2416                 {
2417                   glink_vma += 8;
2418                   if (i >= 0x8000)
2419                     glink_vma += 4;
2420                 }
2421               else
2422                 glink_vma += 4;
2423             }
2424           count += plt_count;
2425         }
2426     }
2427
2428  done:
2429   free (syms);
2430   return count;
2431 }
2432 \f
2433 /* The following functions are specific to the ELF linker, while
2434    functions above are used generally.  Those named ppc64_elf_* are
2435    called by the main ELF linker code.  They appear in this file more
2436    or less in the order in which they are called.  eg.
2437    ppc64_elf_check_relocs is called early in the link process,
2438    ppc64_elf_finish_dynamic_sections is one of the last functions
2439    called.
2440
2441    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2442    functions have both a function code symbol and a function descriptor
2443    symbol.  A call to foo in a relocatable object file looks like:
2444
2445    .            .text
2446    .    x:
2447    .            bl      .foo
2448    .            nop
2449
2450    The function definition in another object file might be:
2451
2452    .            .section .opd
2453    .    foo:    .quad   .foo
2454    .            .quad   .TOC.@tocbase
2455    .            .quad   0
2456    .
2457    .            .text
2458    .    .foo:   blr
2459
2460    When the linker resolves the call during a static link, the branch
2461    unsurprisingly just goes to .foo and the .opd information is unused.
2462    If the function definition is in a shared library, things are a little
2463    different:  The call goes via a plt call stub, the opd information gets
2464    copied to the plt, and the linker patches the nop.
2465
2466    .    x:
2467    .            bl      .foo_stub
2468    .            ld      2,40(1)
2469    .
2470    .
2471    .    .foo_stub:
2472    .            std     2,40(1)                 # in practice, the call stub
2473    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
2474    .            addi    11,11,Lfoo@toc@l        # this is the general idea
2475    .            ld      12,0(11)
2476    .            ld      2,8(11)
2477    .            mtctr   12
2478    .            ld      11,16(11)
2479    .            bctr
2480    .
2481    .            .section .plt
2482    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
2483
2484    The "reloc ()" notation is supposed to indicate that the linker emits
2485    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
2486    copying.
2487
2488    What are the difficulties here?  Well, firstly, the relocations
2489    examined by the linker in check_relocs are against the function code
2490    sym .foo, while the dynamic relocation in the plt is emitted against
2491    the function descriptor symbol, foo.  Somewhere along the line, we need
2492    to carefully copy dynamic link information from one symbol to the other.
2493    Secondly, the generic part of the elf linker will make .foo a dynamic
2494    symbol as is normal for most other backends.  We need foo dynamic
2495    instead, at least for an application final link.  However, when
2496    creating a shared library containing foo, we need to have both symbols
2497    dynamic so that references to .foo are satisfied during the early
2498    stages of linking.  Otherwise the linker might decide to pull in a
2499    definition from some other object, eg. a static library.
2500
2501    Update: As of August 2004, we support a new convention.  Function
2502    calls may use the function descriptor symbol, ie. "bl foo".  This
2503    behaves exactly as "bl .foo".  */
2504
2505 /* Of those relocs that might be copied as dynamic relocs, this
2506    function selects those that must be copied when linking a shared
2507    library or PIE, even when the symbol is local.  */
2508
2509 static int
2510 must_be_dyn_reloc (struct bfd_link_info *info,
2511                    enum elf_ppc64_reloc_type r_type)
2512 {
2513   switch (r_type)
2514     {
2515     default:
2516       /* Only relative relocs can be resolved when the object load
2517          address isn't fixed.  DTPREL64 is excluded because the
2518          dynamic linker needs to differentiate global dynamic from
2519          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
2520       return 1;
2521
2522     case R_PPC64_REL32:
2523     case R_PPC64_REL64:
2524     case R_PPC64_REL30:
2525       return 0;
2526
2527     case R_PPC64_TPREL16:
2528     case R_PPC64_TPREL16_LO:
2529     case R_PPC64_TPREL16_HI:
2530     case R_PPC64_TPREL16_HA:
2531     case R_PPC64_TPREL16_DS:
2532     case R_PPC64_TPREL16_LO_DS:
2533     case R_PPC64_TPREL16_HIGH:
2534     case R_PPC64_TPREL16_HIGHA:
2535     case R_PPC64_TPREL16_HIGHER:
2536     case R_PPC64_TPREL16_HIGHERA:
2537     case R_PPC64_TPREL16_HIGHEST:
2538     case R_PPC64_TPREL16_HIGHESTA:
2539     case R_PPC64_TPREL64:
2540       /* These relocations are relative but in a shared library the
2541          linker doesn't know the thread pointer base.  */
2542       return bfd_link_dll (info);
2543     }
2544 }
2545
2546 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2547    copying dynamic variables from a shared lib into an app's dynbss
2548    section, and instead use a dynamic relocation to point into the
2549    shared lib.  With code that gcc generates, it's vital that this be
2550    enabled;  In the PowerPC64 ABI, the address of a function is actually
2551    the address of a function descriptor, which resides in the .opd
2552    section.  gcc uses the descriptor directly rather than going via the
2553    GOT as some other ABI's do, which means that initialized function
2554    pointers must reference the descriptor.  Thus, a function pointer
2555    initialized to the address of a function in a shared library will
2556    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
2557    redefines the function descriptor symbol to point to the copy.  This
2558    presents a problem as a plt entry for that function is also
2559    initialized from the function descriptor symbol and the copy reloc
2560    may not be initialized first.  */
2561 #define ELIMINATE_COPY_RELOCS 1
2562
2563 /* Section name for stubs is the associated section name plus this
2564    string.  */
2565 #define STUB_SUFFIX ".stub"
2566
2567 /* Linker stubs.
2568    ppc_stub_long_branch:
2569    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2570    destination, but a 24 bit branch in a stub section will reach.
2571    .    b       dest
2572
2573    ppc_stub_plt_branch:
2574    Similar to the above, but a 24 bit branch in the stub section won't
2575    reach its destination.
2576    .    addis   %r11,%r2,xxx@toc@ha
2577    .    ld      %r12,xxx@toc@l(%r11)
2578    .    mtctr   %r12
2579    .    bctr
2580
2581    ppc_stub_plt_call:
2582    Used to call a function in a shared library.  If it so happens that
2583    the plt entry referenced crosses a 64k boundary, then an extra
2584    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2585    ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2586    .    addis   %r11,%r2,xxx@toc@ha
2587    .    ld      %r12,xxx+0@toc@l(%r11)
2588    .    mtctr   %r12
2589    .    ld      %r2,xxx+8@toc@l(%r11)
2590    .    ld      %r11,xxx+16@toc@l(%r11)
2591    .    bctr
2592
2593    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2594    code to adjust the value and save r2 to support multiple toc sections.
2595    A ppc_stub_long_branch with an r2 offset looks like:
2596    .    std     %r2,40(%r1)
2597    .    addis   %r2,%r2,off@ha
2598    .    addi    %r2,%r2,off@l
2599    .    b       dest
2600
2601    A ppc_stub_plt_branch with an r2 offset looks like:
2602    .    std     %r2,40(%r1)
2603    .    addis   %r11,%r2,xxx@toc@ha
2604    .    ld      %r12,xxx@toc@l(%r11)
2605    .    addis   %r2,%r2,off@ha
2606    .    addi    %r2,%r2,off@l
2607    .    mtctr   %r12
2608    .    bctr
2609
2610    All of the above stubs are shown as their ELFv1 variants.  ELFv2
2611    variants exist too, simpler for plt calls since a new toc pointer
2612    and static chain are not loaded by the stub.  In addition, ELFv2
2613    has some more complex stubs to handle calls marked with NOTOC
2614    relocs from functions where r2 is not a valid toc pointer.  These
2615    come in two flavours, the ones shown below, and _both variants that
2616    start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2617    one call is from a function where r2 is used as the toc pointer but
2618    needs a toc adjusting stub for small-model multi-toc, and another
2619    call is from a function where r2 is not valid.
2620    ppc_stub_long_branch_notoc:
2621    .    mflr    %r12
2622    .    bcl     20,31,1f
2623    .  1:
2624    .    mflr    %r11
2625    .    mtlr    %r12
2626    .    addis   %r12,%r11,dest-1b@ha
2627    .    addi    %r12,%r12,dest-1b@l
2628    .    b       dest
2629
2630    ppc_stub_plt_branch_notoc:
2631    .    mflr    %r12
2632    .    bcl     20,31,1f
2633    .  1:
2634    .    mflr    %r11
2635    .    mtlr    %r12
2636    .    lis     %r12,xxx-1b@highest
2637    .    ori     %r12,%r12,xxx-1b@higher
2638    .    sldi    %r12,%r12,32
2639    .    oris    %r12,%r12,xxx-1b@high
2640    .    ori     %r12,%r12,xxx-1b@l
2641    .    add     %r12,%r11,%r12
2642    .    mtctr   %r12
2643    .    bctr
2644
2645    ppc_stub_plt_call_notoc:
2646    .    mflr    %r12
2647    .    bcl     20,31,1f
2648    .  1:
2649    .    mflr    %r11
2650    .    mtlr    %r12
2651    .    lis     %r12,xxx-1b@highest
2652    .    ori     %r12,%r12,xxx-1b@higher
2653    .    sldi    %r12,%r12,32
2654    .    oris    %r12,%r12,xxx-1b@high
2655    .    ori     %r12,%r12,xxx-1b@l
2656    .    ldx     %r12,%r11,%r12
2657    .    mtctr   %r12
2658    .    bctr
2659
2660    In cases where the high instructions would add zero, they are
2661    omitted and following instructions modified in some cases.
2662
2663    For a given stub group (a set of sections all using the same toc
2664    pointer value) there will be just one stub type used for any
2665    particular function symbol.  For example, if printf is called from
2666    code with the tocsave optimization (ie. r2 saved in function
2667    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2668    and from other code without the tocsave optimization requiring a
2669    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2670    type will be created.  Calls with the tocsave optimization will
2671    enter this stub after the instruction saving r2.  A similar
2672    situation exists when calls are marked with R_PPC64_REL24_NOTOC
2673    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
2674    to call an external function like printf.  If other calls to printf
2675    require a ppc_stub_plt_call linkage stub then a single
2676    ppc_stub_plt_call_notoc linkage stub will be used for both types of
2677    call.  If other calls to printf require a ppc_stub_plt_call_r2save
2678    linkage stub then a single ppc_stub_plt_call_both linkage stub will
2679    be created and calls not requiring r2 to be saved will enter the
2680    stub after the r2 save instruction.  There is an analogous
2681    hierarchy of long branch and plt branch stubs for local call
2682    linkage.  */
2683
2684 enum ppc_stub_type
2685 {
2686   ppc_stub_none,
2687   ppc_stub_long_branch,
2688   ppc_stub_long_branch_r2off,
2689   ppc_stub_long_branch_notoc,
2690   ppc_stub_long_branch_both, /* r2off and notoc variants both needed.  */
2691   ppc_stub_plt_branch,
2692   ppc_stub_plt_branch_r2off,
2693   ppc_stub_plt_branch_notoc,
2694   ppc_stub_plt_branch_both,
2695   ppc_stub_plt_call,
2696   ppc_stub_plt_call_r2save,
2697   ppc_stub_plt_call_notoc,
2698   ppc_stub_plt_call_both,
2699   ppc_stub_global_entry,
2700   ppc_stub_save_res
2701 };
2702
2703 /* Information on stub grouping.  */
2704 struct map_stub
2705 {
2706   /* The stub section.  */
2707   asection *stub_sec;
2708   /* This is the section to which stubs in the group will be attached.  */
2709   asection *link_sec;
2710   /* Next group.  */
2711   struct map_stub *next;
2712   /* Whether to emit a copy of register save/restore functions in this
2713      group.  */
2714   int needs_save_res;
2715   /* Current offset within stubs after the insn restoring lr in a
2716      _notoc or _both stub using bcl for pc-relative addressing, or
2717      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
2718   unsigned int lr_restore;
2719   /* Accumulated size of EH info emitted to describe return address
2720      if stubs modify lr.  Does not include 17 byte FDE header.  */
2721   unsigned int eh_size;
2722   /* Offset in glink_eh_frame to the start of EH info for this group.  */
2723   unsigned int eh_base;
2724 };
2725
2726 struct ppc_stub_hash_entry
2727 {
2728   /* Base hash table entry structure.  */
2729   struct bfd_hash_entry root;
2730
2731   enum ppc_stub_type stub_type;
2732
2733   /* Group information.  */
2734   struct map_stub *group;
2735
2736   /* Offset within stub_sec of the beginning of this stub.  */
2737   bfd_vma stub_offset;
2738
2739   /* Given the symbol's value and its section we can determine its final
2740      value when building the stubs (so the stub knows where to jump.  */
2741   bfd_vma target_value;
2742   asection *target_section;
2743
2744   /* The symbol table entry, if any, that this was derived from.  */
2745   struct ppc_link_hash_entry *h;
2746   struct plt_entry *plt_ent;
2747
2748   /* Symbol type.  */
2749   unsigned char symtype;
2750
2751   /* Symbol st_other.  */
2752   unsigned char other;
2753 };
2754
2755 struct ppc_branch_hash_entry
2756 {
2757   /* Base hash table entry structure.  */
2758   struct bfd_hash_entry root;
2759
2760   /* Offset within branch lookup table.  */
2761   unsigned int offset;
2762
2763   /* Generation marker.  */
2764   unsigned int iter;
2765 };
2766
2767 /* Used to track dynamic relocations for local symbols.  */
2768 struct ppc_dyn_relocs
2769 {
2770   struct ppc_dyn_relocs *next;
2771
2772   /* The input section of the reloc.  */
2773   asection *sec;
2774
2775   /* Total number of relocs copied for the input section.  */
2776   unsigned int count : 31;
2777
2778   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
2779   unsigned int ifunc : 1;
2780 };
2781
2782 struct ppc_link_hash_entry
2783 {
2784   struct elf_link_hash_entry elf;
2785
2786   union
2787   {
2788     /* A pointer to the most recently used stub hash entry against this
2789        symbol.  */
2790     struct ppc_stub_hash_entry *stub_cache;
2791
2792     /* A pointer to the next symbol starting with a '.'  */
2793     struct ppc_link_hash_entry *next_dot_sym;
2794   } u;
2795
2796   /* Track dynamic relocs copied for this symbol.  */
2797   struct elf_dyn_relocs *dyn_relocs;
2798
2799   /* Link between function code and descriptor symbols.  */
2800   struct ppc_link_hash_entry *oh;
2801
2802   /* Flag function code and descriptor symbols.  */
2803   unsigned int is_func:1;
2804   unsigned int is_func_descriptor:1;
2805   unsigned int fake:1;
2806
2807   /* Whether global opd/toc sym has been adjusted or not.
2808      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
2809      should be set for all globals defined in any opd/toc section.  */
2810   unsigned int adjust_done:1;
2811
2812   /* Set if this is an out-of-line register save/restore function,
2813      with non-standard calling convention.  */
2814   unsigned int save_res:1;
2815
2816   /* Set if a duplicate symbol with non-zero localentry is detected,
2817      even when the duplicate symbol does not provide a definition.  */
2818   unsigned int non_zero_localentry:1;
2819
2820   /* Contexts in which symbol is used in the GOT (or TOC).
2821      Bits are or'd into the mask as the corresponding relocs are
2822      encountered during check_relocs, with TLS_TLS being set when any
2823      of the other TLS bits are set.  tls_optimize clears bits when
2824      optimizing to indicate the corresponding GOT entry type is not
2825      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
2826      set TLS_TPRELGD when a GD reloc turns into a TPREL one.  We use a
2827      separate flag rather than setting TPREL just for convenience in
2828      distinguishing the two cases.
2829      These flags are also kept for local symbols.  */
2830 #define TLS_TLS          1      /* Any TLS reloc.  */
2831 #define TLS_GD           2      /* GD reloc. */
2832 #define TLS_LD           4      /* LD reloc. */
2833 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
2834 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
2835 #define TLS_MARK        32      /* __tls_get_addr call marked. */
2836 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
2837 #define TLS_EXPLICIT   128      /* Marks TOC section TLS relocs. */
2838   unsigned char tls_mask;
2839
2840   /* The above field is also used to mark function symbols.  In which
2841      case TLS_TLS will be 0.  */
2842 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
2843 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
2844 #define NON_GOT        256      /* local symbol plt, not stored.  */
2845 };
2846
2847 /* ppc64 ELF linker hash table.  */
2848
2849 struct ppc_link_hash_table
2850 {
2851   struct elf_link_hash_table elf;
2852
2853   /* The stub hash table.  */
2854   struct bfd_hash_table stub_hash_table;
2855
2856   /* Another hash table for plt_branch stubs.  */
2857   struct bfd_hash_table branch_hash_table;
2858
2859   /* Hash table for function prologue tocsave.  */
2860   htab_t tocsave_htab;
2861
2862   /* Various options and other info passed from the linker.  */
2863   struct ppc64_elf_params *params;
2864
2865   /* The size of sec_info below.  */
2866   unsigned int sec_info_arr_size;
2867
2868   /* Per-section array of extra section info.  Done this way rather
2869      than as part of ppc64_elf_section_data so we have the info for
2870      non-ppc64 sections.  */
2871   struct
2872   {
2873     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
2874     bfd_vma toc_off;
2875
2876     union
2877     {
2878       /* The section group that this section belongs to.  */
2879       struct map_stub *group;
2880       /* A temp section list pointer.  */
2881       asection *list;
2882     } u;
2883   } *sec_info;
2884
2885   /* Linked list of groups.  */
2886   struct map_stub *group;
2887
2888   /* Temp used when calculating TOC pointers.  */
2889   bfd_vma toc_curr;
2890   bfd *toc_bfd;
2891   asection *toc_first_sec;
2892
2893   /* Used when adding symbols.  */
2894   struct ppc_link_hash_entry *dot_syms;
2895
2896   /* Shortcuts to get to dynamic linker sections.  */
2897   asection *glink;
2898   asection *global_entry;
2899   asection *sfpr;
2900   asection *pltlocal;
2901   asection *relpltlocal;
2902   asection *brlt;
2903   asection *relbrlt;
2904   asection *glink_eh_frame;
2905
2906   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
2907   struct ppc_link_hash_entry *tls_get_addr;
2908   struct ppc_link_hash_entry *tls_get_addr_fd;
2909
2910   /* The size of reliplt used by got entry relocs.  */
2911   bfd_size_type got_reli_size;
2912
2913   /* Statistics.  */
2914   unsigned long stub_count[ppc_stub_global_entry];
2915
2916   /* Number of stubs against global syms.  */
2917   unsigned long stub_globals;
2918
2919   /* Set if we're linking code with function descriptors.  */
2920   unsigned int opd_abi:1;
2921
2922   /* Support for multiple toc sections.  */
2923   unsigned int do_multi_toc:1;
2924   unsigned int multi_toc_needed:1;
2925   unsigned int second_toc_pass:1;
2926   unsigned int do_toc_opt:1;
2927
2928   /* Set if tls optimization is enabled.  */
2929   unsigned int do_tls_opt:1;
2930
2931   /* Set if inline plt calls should be converted to direct calls.  */
2932   unsigned int can_convert_all_inline_plt:1;
2933
2934   /* Set on error.  */
2935   unsigned int stub_error:1;
2936
2937   /* Whether func_desc_adjust needs to be run over symbols.  */
2938   unsigned int need_func_desc_adj:1;
2939
2940   /* Whether there exist local gnu indirect function resolvers,
2941      referenced by dynamic relocations.  */
2942   unsigned int local_ifunc_resolver:1;
2943   unsigned int maybe_local_ifunc_resolver:1;
2944
2945   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
2946   unsigned int has_plt_localentry0:1;
2947
2948   /* Incremented every time we size stubs.  */
2949   unsigned int stub_iteration;
2950
2951   /* Small local sym cache.  */
2952   struct sym_cache sym_cache;
2953 };
2954
2955 /* Rename some of the generic section flags to better document how they
2956    are used here.  */
2957
2958 /* Nonzero if this section has TLS related relocations.  */
2959 #define has_tls_reloc sec_flg0
2960
2961 /* Nonzero if this section has an old-style call to __tls_get_addr.  */
2962 #define has_tls_get_addr_call sec_flg1
2963
2964 /* Nonzero if this section has any toc or got relocs.  */
2965 #define has_toc_reloc sec_flg2
2966
2967 /* Nonzero if this section has a call to another section that uses
2968    the toc or got.  */
2969 #define makes_toc_func_call sec_flg3
2970
2971 /* Recursion protection when determining above flag.  */
2972 #define call_check_in_progress sec_flg4
2973 #define call_check_done sec_flg5
2974
2975 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
2976
2977 #define ppc_hash_table(p) \
2978   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2979   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
2980
2981 #define ppc_stub_hash_lookup(table, string, create, copy) \
2982   ((struct ppc_stub_hash_entry *) \
2983    bfd_hash_lookup ((table), (string), (create), (copy)))
2984
2985 #define ppc_branch_hash_lookup(table, string, create, copy) \
2986   ((struct ppc_branch_hash_entry *) \
2987    bfd_hash_lookup ((table), (string), (create), (copy)))
2988
2989 /* Create an entry in the stub hash table.  */
2990
2991 static struct bfd_hash_entry *
2992 stub_hash_newfunc (struct bfd_hash_entry *entry,
2993                    struct bfd_hash_table *table,
2994                    const char *string)
2995 {
2996   /* Allocate the structure if it has not already been allocated by a
2997      subclass.  */
2998   if (entry == NULL)
2999     {
3000       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3001       if (entry == NULL)
3002         return entry;
3003     }
3004
3005   /* Call the allocation method of the superclass.  */
3006   entry = bfd_hash_newfunc (entry, table, string);
3007   if (entry != NULL)
3008     {
3009       struct ppc_stub_hash_entry *eh;
3010
3011       /* Initialize the local fields.  */
3012       eh = (struct ppc_stub_hash_entry *) entry;
3013       eh->stub_type = ppc_stub_none;
3014       eh->group = NULL;
3015       eh->stub_offset = 0;
3016       eh->target_value = 0;
3017       eh->target_section = NULL;
3018       eh->h = NULL;
3019       eh->plt_ent = NULL;
3020       eh->other = 0;
3021     }
3022
3023   return entry;
3024 }
3025
3026 /* Create an entry in the branch hash table.  */
3027
3028 static struct bfd_hash_entry *
3029 branch_hash_newfunc (struct bfd_hash_entry *entry,
3030                      struct bfd_hash_table *table,
3031                      const char *string)
3032 {
3033   /* Allocate the structure if it has not already been allocated by a
3034      subclass.  */
3035   if (entry == NULL)
3036     {
3037       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3038       if (entry == NULL)
3039         return entry;
3040     }
3041
3042   /* Call the allocation method of the superclass.  */
3043   entry = bfd_hash_newfunc (entry, table, string);
3044   if (entry != NULL)
3045     {
3046       struct ppc_branch_hash_entry *eh;
3047
3048       /* Initialize the local fields.  */
3049       eh = (struct ppc_branch_hash_entry *) entry;
3050       eh->offset = 0;
3051       eh->iter = 0;
3052     }
3053
3054   return entry;
3055 }
3056
3057 /* Create an entry in a ppc64 ELF linker hash table.  */
3058
3059 static struct bfd_hash_entry *
3060 link_hash_newfunc (struct bfd_hash_entry *entry,
3061                    struct bfd_hash_table *table,
3062                    const char *string)
3063 {
3064   /* Allocate the structure if it has not already been allocated by a
3065      subclass.  */
3066   if (entry == NULL)
3067     {
3068       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3069       if (entry == NULL)
3070         return entry;
3071     }
3072
3073   /* Call the allocation method of the superclass.  */
3074   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3075   if (entry != NULL)
3076     {
3077       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3078
3079       memset (&eh->u.stub_cache, 0,
3080               (sizeof (struct ppc_link_hash_entry)
3081                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3082
3083       /* When making function calls, old ABI code references function entry
3084          points (dot symbols), while new ABI code references the function
3085          descriptor symbol.  We need to make any combination of reference and
3086          definition work together, without breaking archive linking.
3087
3088          For a defined function "foo" and an undefined call to "bar":
3089          An old object defines "foo" and ".foo", references ".bar" (possibly
3090          "bar" too).
3091          A new object defines "foo" and references "bar".
3092
3093          A new object thus has no problem with its undefined symbols being
3094          satisfied by definitions in an old object.  On the other hand, the
3095          old object won't have ".bar" satisfied by a new object.
3096
3097          Keep a list of newly added dot-symbols.  */
3098
3099       if (string[0] == '.')
3100         {
3101           struct ppc_link_hash_table *htab;
3102
3103           htab = (struct ppc_link_hash_table *) table;
3104           eh->u.next_dot_sym = htab->dot_syms;
3105           htab->dot_syms = eh;
3106         }
3107     }
3108
3109   return entry;
3110 }
3111
3112 struct tocsave_entry
3113 {
3114   asection *sec;
3115   bfd_vma offset;
3116 };
3117
3118 static hashval_t
3119 tocsave_htab_hash (const void *p)
3120 {
3121   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3122   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3123 }
3124
3125 static int
3126 tocsave_htab_eq (const void *p1, const void *p2)
3127 {
3128   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3129   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3130   return e1->sec == e2->sec && e1->offset == e2->offset;
3131 }
3132
3133 /* Destroy a ppc64 ELF linker hash table.  */
3134
3135 static void
3136 ppc64_elf_link_hash_table_free (bfd *obfd)
3137 {
3138   struct ppc_link_hash_table *htab;
3139
3140   htab = (struct ppc_link_hash_table *) obfd->link.hash;
3141   if (htab->tocsave_htab)
3142     htab_delete (htab->tocsave_htab);
3143   bfd_hash_table_free (&htab->branch_hash_table);
3144   bfd_hash_table_free (&htab->stub_hash_table);
3145   _bfd_elf_link_hash_table_free (obfd);
3146 }
3147
3148 /* Create a ppc64 ELF linker hash table.  */
3149
3150 static struct bfd_link_hash_table *
3151 ppc64_elf_link_hash_table_create (bfd *abfd)
3152 {
3153   struct ppc_link_hash_table *htab;
3154   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3155
3156   htab = bfd_zmalloc (amt);
3157   if (htab == NULL)
3158     return NULL;
3159
3160   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3161                                       sizeof (struct ppc_link_hash_entry),
3162                                       PPC64_ELF_DATA))
3163     {
3164       free (htab);
3165       return NULL;
3166     }
3167
3168   /* Init the stub hash table too.  */
3169   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3170                             sizeof (struct ppc_stub_hash_entry)))
3171     {
3172       _bfd_elf_link_hash_table_free (abfd);
3173       return NULL;
3174     }
3175
3176   /* And the branch hash table.  */
3177   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3178                             sizeof (struct ppc_branch_hash_entry)))
3179     {
3180       bfd_hash_table_free (&htab->stub_hash_table);
3181       _bfd_elf_link_hash_table_free (abfd);
3182       return NULL;
3183     }
3184
3185   htab->tocsave_htab = htab_try_create (1024,
3186                                         tocsave_htab_hash,
3187                                         tocsave_htab_eq,
3188                                         NULL);
3189   if (htab->tocsave_htab == NULL)
3190     {
3191       ppc64_elf_link_hash_table_free (abfd);
3192       return NULL;
3193     }
3194   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3195
3196   /* Initializing two fields of the union is just cosmetic.  We really
3197      only care about glist, but when compiled on a 32-bit host the
3198      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
3199      debugger inspection of these fields look nicer.  */
3200   htab->elf.init_got_refcount.refcount = 0;
3201   htab->elf.init_got_refcount.glist = NULL;
3202   htab->elf.init_plt_refcount.refcount = 0;
3203   htab->elf.init_plt_refcount.glist = NULL;
3204   htab->elf.init_got_offset.offset = 0;
3205   htab->elf.init_got_offset.glist = NULL;
3206   htab->elf.init_plt_offset.offset = 0;
3207   htab->elf.init_plt_offset.glist = NULL;
3208
3209   return &htab->elf.root;
3210 }
3211
3212 /* Create sections for linker generated code.  */
3213
3214 static bfd_boolean
3215 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3216 {
3217   struct ppc_link_hash_table *htab;
3218   flagword flags;
3219
3220   htab = ppc_hash_table (info);
3221
3222   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3223            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3224   if (htab->params->save_restore_funcs)
3225     {
3226       /* Create .sfpr for code to save and restore fp regs.  */
3227       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3228                                                        flags);
3229       if (htab->sfpr == NULL
3230           || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3231         return FALSE;
3232     }
3233
3234   if (bfd_link_relocatable (info))
3235     return TRUE;
3236
3237   /* Create .glink for lazy dynamic linking support.  */
3238   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3239                                                     flags);
3240   if (htab->glink == NULL
3241       || !bfd_set_section_alignment (dynobj, htab->glink, 3))
3242     return FALSE;
3243
3244   /* The part of .glink used by global entry stubs, separate so that
3245      it can be aligned appropriately without affecting htab->glink.  */
3246   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3247                                                            flags);
3248   if (htab->global_entry == NULL
3249       || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
3250     return FALSE;
3251
3252   if (!info->no_ld_generated_unwind_info)
3253     {
3254       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3255                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3256       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3257                                                                  ".eh_frame",
3258                                                                  flags);
3259       if (htab->glink_eh_frame == NULL
3260           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
3261         return FALSE;
3262     }
3263
3264   flags = SEC_ALLOC | SEC_LINKER_CREATED;
3265   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3266   if (htab->elf.iplt == NULL
3267       || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
3268     return FALSE;
3269
3270   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3271            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3272   htab->elf.irelplt
3273     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3274   if (htab->elf.irelplt == NULL
3275       || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
3276     return FALSE;
3277
3278   /* Create branch lookup table for plt_branch stubs.  */
3279   flags = (SEC_ALLOC | SEC_LOAD
3280            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3281   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3282                                                    flags);
3283   if (htab->brlt == NULL
3284       || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
3285     return FALSE;
3286
3287   /* Local plt entries, put in .branch_lt but a separate section for
3288      convenience.  */
3289   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3290                                                        flags);
3291   if (htab->pltlocal == NULL
3292       || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
3293     return FALSE;
3294
3295   if (!bfd_link_pic (info))
3296     return TRUE;
3297
3298   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3299            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3300   htab->relbrlt
3301     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3302   if (htab->relbrlt == NULL
3303       || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3304     return FALSE;
3305
3306   htab->relpltlocal
3307     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3308   if (htab->relpltlocal == NULL
3309       || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
3310     return FALSE;
3311
3312   return TRUE;
3313 }
3314
3315 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
3316
3317 bfd_boolean
3318 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3319                          struct ppc64_elf_params *params)
3320 {
3321   struct ppc_link_hash_table *htab;
3322
3323   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3324
3325 /* Always hook our dynamic sections into the first bfd, which is the
3326    linker created stub bfd.  This ensures that the GOT header is at
3327    the start of the output TOC section.  */
3328   htab = ppc_hash_table (info);
3329   htab->elf.dynobj = params->stub_bfd;
3330   htab->params = params;
3331
3332   return create_linkage_sections (htab->elf.dynobj, info);
3333 }
3334
3335 /* Build a name for an entry in the stub hash table.  */
3336
3337 static char *
3338 ppc_stub_name (const asection *input_section,
3339                const asection *sym_sec,
3340                const struct ppc_link_hash_entry *h,
3341                const Elf_Internal_Rela *rel)
3342 {
3343   char *stub_name;
3344   ssize_t len;
3345
3346   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3347      offsets from a sym as a branch target?  In fact, we could
3348      probably assume the addend is always zero.  */
3349   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3350
3351   if (h)
3352     {
3353       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3354       stub_name = bfd_malloc (len);
3355       if (stub_name == NULL)
3356         return stub_name;
3357
3358       len = sprintf (stub_name, "%08x.%s+%x",
3359                      input_section->id & 0xffffffff,
3360                      h->elf.root.root.string,
3361                      (int) rel->r_addend & 0xffffffff);
3362     }
3363   else
3364     {
3365       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3366       stub_name = bfd_malloc (len);
3367       if (stub_name == NULL)
3368         return stub_name;
3369
3370       len = sprintf (stub_name, "%08x.%x:%x+%x",
3371                      input_section->id & 0xffffffff,
3372                      sym_sec->id & 0xffffffff,
3373                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3374                      (int) rel->r_addend & 0xffffffff);
3375     }
3376   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3377     stub_name[len - 2] = 0;
3378   return stub_name;
3379 }
3380
3381 /* Look up an entry in the stub hash.  Stub entries are cached because
3382    creating the stub name takes a bit of time.  */
3383
3384 static struct ppc_stub_hash_entry *
3385 ppc_get_stub_entry (const asection *input_section,
3386                     const asection *sym_sec,
3387                     struct ppc_link_hash_entry *h,
3388                     const Elf_Internal_Rela *rel,
3389                     struct ppc_link_hash_table *htab)
3390 {
3391   struct ppc_stub_hash_entry *stub_entry;
3392   struct map_stub *group;
3393
3394   /* If this input section is part of a group of sections sharing one
3395      stub section, then use the id of the first section in the group.
3396      Stub names need to include a section id, as there may well be
3397      more than one stub used to reach say, printf, and we need to
3398      distinguish between them.  */
3399   group = htab->sec_info[input_section->id].u.group;
3400   if (group == NULL)
3401     return NULL;
3402
3403   if (h != NULL && h->u.stub_cache != NULL
3404       && h->u.stub_cache->h == h
3405       && h->u.stub_cache->group == group)
3406     {
3407       stub_entry = h->u.stub_cache;
3408     }
3409   else
3410     {
3411       char *stub_name;
3412
3413       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3414       if (stub_name == NULL)
3415         return NULL;
3416
3417       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3418                                          stub_name, FALSE, FALSE);
3419       if (h != NULL)
3420         h->u.stub_cache = stub_entry;
3421
3422       free (stub_name);
3423     }
3424
3425   return stub_entry;
3426 }
3427
3428 /* Add a new stub entry to the stub hash.  Not all fields of the new
3429    stub entry are initialised.  */
3430
3431 static struct ppc_stub_hash_entry *
3432 ppc_add_stub (const char *stub_name,
3433               asection *section,
3434               struct bfd_link_info *info)
3435 {
3436   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3437   struct map_stub *group;
3438   asection *link_sec;
3439   asection *stub_sec;
3440   struct ppc_stub_hash_entry *stub_entry;
3441
3442   group = htab->sec_info[section->id].u.group;
3443   link_sec = group->link_sec;
3444   stub_sec = group->stub_sec;
3445   if (stub_sec == NULL)
3446     {
3447       size_t namelen;
3448       bfd_size_type len;
3449       char *s_name;
3450
3451       namelen = strlen (link_sec->name);
3452       len = namelen + sizeof (STUB_SUFFIX);
3453       s_name = bfd_alloc (htab->params->stub_bfd, len);
3454       if (s_name == NULL)
3455         return NULL;
3456
3457       memcpy (s_name, link_sec->name, namelen);
3458       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3459       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3460       if (stub_sec == NULL)
3461         return NULL;
3462       group->stub_sec = stub_sec;
3463     }
3464
3465   /* Enter this entry into the linker stub hash table.  */
3466   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3467                                      TRUE, FALSE);
3468   if (stub_entry == NULL)
3469     {
3470       /* xgettext:c-format */
3471       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3472                           section->owner, stub_name);
3473       return NULL;
3474     }
3475
3476   stub_entry->group = group;
3477   stub_entry->stub_offset = 0;
3478   return stub_entry;
3479 }
3480
3481 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3482    not already done.  */
3483
3484 static bfd_boolean
3485 create_got_section (bfd *abfd, struct bfd_link_info *info)
3486 {
3487   asection *got, *relgot;
3488   flagword flags;
3489   struct ppc_link_hash_table *htab = ppc_hash_table (info);
3490
3491   if (!is_ppc64_elf (abfd))
3492     return FALSE;
3493   if (htab == NULL)
3494     return FALSE;
3495
3496   if (!htab->elf.sgot
3497       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3498     return FALSE;
3499
3500   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3501            | SEC_LINKER_CREATED);
3502
3503   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3504   if (!got
3505       || !bfd_set_section_alignment (abfd, got, 3))
3506     return FALSE;
3507
3508   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3509                                                flags | SEC_READONLY);
3510   if (!relgot
3511       || !bfd_set_section_alignment (abfd, relgot, 3))
3512     return FALSE;
3513
3514   ppc64_elf_tdata (abfd)->got = got;
3515   ppc64_elf_tdata (abfd)->relgot = relgot;
3516   return TRUE;
3517 }
3518
3519 /* Follow indirect and warning symbol links.  */
3520
3521 static inline struct bfd_link_hash_entry *
3522 follow_link (struct bfd_link_hash_entry *h)
3523 {
3524   while (h->type == bfd_link_hash_indirect
3525          || h->type == bfd_link_hash_warning)
3526     h = h->u.i.link;
3527   return h;
3528 }
3529
3530 static inline struct elf_link_hash_entry *
3531 elf_follow_link (struct elf_link_hash_entry *h)
3532 {
3533   return (struct elf_link_hash_entry *) follow_link (&h->root);
3534 }
3535
3536 static inline struct ppc_link_hash_entry *
3537 ppc_follow_link (struct ppc_link_hash_entry *h)
3538 {
3539   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
3540 }
3541
3542 /* Merge PLT info on FROM with that on TO.  */
3543
3544 static void
3545 move_plt_plist (struct ppc_link_hash_entry *from,
3546                 struct ppc_link_hash_entry *to)
3547 {
3548   if (from->elf.plt.plist != NULL)
3549     {
3550       if (to->elf.plt.plist != NULL)
3551         {
3552           struct plt_entry **entp;
3553           struct plt_entry *ent;
3554
3555           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3556             {
3557               struct plt_entry *dent;
3558
3559               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3560                 if (dent->addend == ent->addend)
3561                   {
3562                     dent->plt.refcount += ent->plt.refcount;
3563                     *entp = ent->next;
3564                     break;
3565                   }
3566               if (dent == NULL)
3567                 entp = &ent->next;
3568             }
3569           *entp = to->elf.plt.plist;
3570         }
3571
3572       to->elf.plt.plist = from->elf.plt.plist;
3573       from->elf.plt.plist = NULL;
3574     }
3575 }
3576
3577 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3578
3579 static void
3580 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3581                                 struct elf_link_hash_entry *dir,
3582                                 struct elf_link_hash_entry *ind)
3583 {
3584   struct ppc_link_hash_entry *edir, *eind;
3585
3586   edir = (struct ppc_link_hash_entry *) dir;
3587   eind = (struct ppc_link_hash_entry *) ind;
3588
3589   edir->is_func |= eind->is_func;
3590   edir->is_func_descriptor |= eind->is_func_descriptor;
3591   edir->tls_mask |= eind->tls_mask;
3592   if (eind->oh != NULL)
3593     edir->oh = ppc_follow_link (eind->oh);
3594
3595   if (edir->elf.versioned != versioned_hidden)
3596     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3597   edir->elf.ref_regular |= eind->elf.ref_regular;
3598   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3599   edir->elf.non_got_ref |= eind->elf.non_got_ref;
3600   edir->elf.needs_plt |= eind->elf.needs_plt;
3601   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3602
3603   /* If we were called to copy over info for a weak sym, don't copy
3604      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
3605      in order to simplify readonly_dynrelocs and save a field in the
3606      symbol hash entry, but that means dyn_relocs can't be used in any
3607      tests about a specific symbol, or affect other symbol flags which
3608      are then tested.  */
3609   if (eind->elf.root.type != bfd_link_hash_indirect)
3610     return;
3611
3612   /* Copy over any dynamic relocs we may have on the indirect sym.  */
3613   if (eind->dyn_relocs != NULL)
3614     {
3615       if (edir->dyn_relocs != NULL)
3616         {
3617           struct elf_dyn_relocs **pp;
3618           struct elf_dyn_relocs *p;
3619
3620           /* Add reloc counts against the indirect sym to the direct sym
3621              list.  Merge any entries against the same section.  */
3622           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3623             {
3624               struct elf_dyn_relocs *q;
3625
3626               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3627                 if (q->sec == p->sec)
3628                   {
3629                     q->pc_count += p->pc_count;
3630                     q->count += p->count;
3631                     *pp = p->next;
3632                     break;
3633                   }
3634               if (q == NULL)
3635                 pp = &p->next;
3636             }
3637           *pp = edir->dyn_relocs;
3638         }
3639
3640       edir->dyn_relocs = eind->dyn_relocs;
3641       eind->dyn_relocs = NULL;
3642     }
3643
3644   /* Copy over got entries that we may have already seen to the
3645      symbol which just became indirect.  */
3646   if (eind->elf.got.glist != NULL)
3647     {
3648       if (edir->elf.got.glist != NULL)
3649         {
3650           struct got_entry **entp;
3651           struct got_entry *ent;
3652
3653           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3654             {
3655               struct got_entry *dent;
3656
3657               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3658                 if (dent->addend == ent->addend
3659                     && dent->owner == ent->owner
3660                     && dent->tls_type == ent->tls_type)
3661                   {
3662                     dent->got.refcount += ent->got.refcount;
3663                     *entp = ent->next;
3664                     break;
3665                   }
3666               if (dent == NULL)
3667                 entp = &ent->next;
3668             }
3669           *entp = edir->elf.got.glist;
3670         }
3671
3672       edir->elf.got.glist = eind->elf.got.glist;
3673       eind->elf.got.glist = NULL;
3674     }
3675
3676   /* And plt entries.  */
3677   move_plt_plist (eind, edir);
3678
3679   if (eind->elf.dynindx != -1)
3680     {
3681       if (edir->elf.dynindx != -1)
3682         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3683                                 edir->elf.dynstr_index);
3684       edir->elf.dynindx = eind->elf.dynindx;
3685       edir->elf.dynstr_index = eind->elf.dynstr_index;
3686       eind->elf.dynindx = -1;
3687       eind->elf.dynstr_index = 0;
3688     }
3689 }
3690
3691 /* Find the function descriptor hash entry from the given function code
3692    hash entry FH.  Link the entries via their OH fields.  */
3693
3694 static struct ppc_link_hash_entry *
3695 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3696 {
3697   struct ppc_link_hash_entry *fdh = fh->oh;
3698
3699   if (fdh == NULL)
3700     {
3701       const char *fd_name = fh->elf.root.root.string + 1;
3702
3703       fdh = (struct ppc_link_hash_entry *)
3704         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3705       if (fdh == NULL)
3706         return fdh;
3707
3708       fdh->is_func_descriptor = 1;
3709       fdh->oh = fh;
3710       fh->is_func = 1;
3711       fh->oh = fdh;
3712     }
3713
3714   fdh = ppc_follow_link (fdh);
3715   fdh->is_func_descriptor = 1;
3716   fdh->oh = fh;
3717   return fdh;
3718 }
3719
3720 /* Make a fake function descriptor sym for the undefined code sym FH.  */
3721
3722 static struct ppc_link_hash_entry *
3723 make_fdh (struct bfd_link_info *info,
3724           struct ppc_link_hash_entry *fh)
3725 {
3726   bfd *abfd = fh->elf.root.u.undef.abfd;
3727   struct bfd_link_hash_entry *bh = NULL;
3728   struct ppc_link_hash_entry *fdh;
3729   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
3730                     ? BSF_WEAK
3731                     : BSF_GLOBAL);
3732
3733   if (!_bfd_generic_link_add_one_symbol (info, abfd,
3734                                          fh->elf.root.root.string + 1,
3735                                          flags, bfd_und_section_ptr, 0,
3736                                          NULL, FALSE, FALSE, &bh))
3737     return NULL;
3738
3739   fdh = (struct ppc_link_hash_entry *) bh;
3740   fdh->elf.non_elf = 0;
3741   fdh->fake = 1;
3742   fdh->is_func_descriptor = 1;
3743   fdh->oh = fh;
3744   fh->is_func = 1;
3745   fh->oh = fdh;
3746   return fdh;
3747 }
3748
3749 /* Fix function descriptor symbols defined in .opd sections to be
3750    function type.  */
3751
3752 static bfd_boolean
3753 ppc64_elf_add_symbol_hook (bfd *ibfd,
3754                            struct bfd_link_info *info,
3755                            Elf_Internal_Sym *isym,
3756                            const char **name,
3757                            flagword *flags ATTRIBUTE_UNUSED,
3758                            asection **sec,
3759                            bfd_vma *value)
3760 {
3761   if (*sec != NULL
3762       && strcmp ((*sec)->name, ".opd") == 0)
3763     {
3764       asection *code_sec;
3765
3766       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3767             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
3768         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
3769
3770       /* If the symbol is a function defined in .opd, and the function
3771          code is in a discarded group, let it appear to be undefined.  */
3772       if (!bfd_link_relocatable (info)
3773           && (*sec)->reloc_count != 0
3774           && opd_entry_value (*sec, *value, &code_sec, NULL,
3775                               FALSE) != (bfd_vma) -1
3776           && discarded_section (code_sec))
3777         {
3778           *sec = bfd_und_section_ptr;
3779           isym->st_shndx = SHN_UNDEF;
3780         }
3781     }
3782   else if (*sec != NULL
3783            && strcmp ((*sec)->name, ".toc") == 0
3784            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
3785     {
3786       struct ppc_link_hash_table *htab = ppc_hash_table (info);
3787       if (htab != NULL)
3788         htab->params->object_in_toc = 1;
3789     }
3790
3791   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3792     {
3793       if (abiversion (ibfd) == 0)
3794         set_abiversion (ibfd, 2);
3795       else if (abiversion (ibfd) == 1)
3796         {
3797           _bfd_error_handler (_("symbol '%s' has invalid st_other"
3798                                 " for ABI version 1"), *name);
3799           bfd_set_error (bfd_error_bad_value);
3800           return FALSE;
3801         }
3802     }
3803
3804   return TRUE;
3805 }
3806
3807 /* Merge non-visibility st_other attributes: local entry point.  */
3808
3809 static void
3810 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
3811                                   const Elf_Internal_Sym *isym,
3812                                   bfd_boolean definition,
3813                                   bfd_boolean dynamic)
3814 {
3815   if (definition && (!dynamic || !h->def_regular))
3816     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
3817                 | ELF_ST_VISIBILITY (h->other));
3818 }
3819
3820 /* Hook called on merging a symbol.  We use this to clear "fake" since
3821    we now have a real symbol.  */
3822
3823 static bfd_boolean
3824 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
3825                         const Elf_Internal_Sym *isym,
3826                         asection **psec ATTRIBUTE_UNUSED,
3827                         bfd_boolean newdef ATTRIBUTE_UNUSED,
3828                         bfd_boolean olddef ATTRIBUTE_UNUSED,
3829                         bfd *oldbfd ATTRIBUTE_UNUSED,
3830                         const asection *oldsec ATTRIBUTE_UNUSED)
3831 {
3832   ((struct ppc_link_hash_entry *) h)->fake = 0;
3833   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
3834     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
3835   return TRUE;
3836 }
3837
3838 /* This function makes an old ABI object reference to ".bar" cause the
3839    inclusion of a new ABI object archive that defines "bar".
3840    NAME is a symbol defined in an archive.  Return a symbol in the hash
3841    table that might be satisfied by the archive symbols.  */
3842
3843 static struct elf_link_hash_entry *
3844 ppc64_elf_archive_symbol_lookup (bfd *abfd,
3845                                  struct bfd_link_info *info,
3846                                  const char *name)
3847 {
3848   struct elf_link_hash_entry *h;
3849   char *dot_name;
3850   size_t len;
3851
3852   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
3853   if (h != NULL
3854       /* Don't return this sym if it is a fake function descriptor
3855          created by add_symbol_adjust.  */
3856       && !((struct ppc_link_hash_entry *) h)->fake)
3857     return h;
3858
3859   if (name[0] == '.')
3860     return h;
3861
3862   len = strlen (name);
3863   dot_name = bfd_alloc (abfd, len + 2);
3864   if (dot_name == NULL)
3865     return (struct elf_link_hash_entry *) -1;
3866   dot_name[0] = '.';
3867   memcpy (dot_name + 1, name, len + 1);
3868   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
3869   bfd_release (abfd, dot_name);
3870   return h;
3871 }
3872
3873 /* This function satisfies all old ABI object references to ".bar" if a
3874    new ABI object defines "bar".  Well, at least, undefined dot symbols
3875    are made weak.  This stops later archive searches from including an
3876    object if we already have a function descriptor definition.  It also
3877    prevents the linker complaining about undefined symbols.
3878    We also check and correct mismatched symbol visibility here.  The
3879    most restrictive visibility of the function descriptor and the
3880    function entry symbol is used.  */
3881
3882 static bfd_boolean
3883 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
3884 {
3885   struct ppc_link_hash_table *htab;
3886   struct ppc_link_hash_entry *fdh;
3887
3888   if (eh->elf.root.type == bfd_link_hash_warning)
3889     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
3890
3891   if (eh->elf.root.type == bfd_link_hash_indirect)
3892     return TRUE;
3893
3894   if (eh->elf.root.root.string[0] != '.')
3895     abort ();
3896
3897   htab = ppc_hash_table (info);
3898   if (htab == NULL)
3899     return FALSE;
3900
3901   fdh = lookup_fdh (eh, htab);
3902   if (fdh == NULL
3903       && !bfd_link_relocatable (info)
3904       && (eh->elf.root.type == bfd_link_hash_undefined
3905           || eh->elf.root.type == bfd_link_hash_undefweak)
3906       && eh->elf.ref_regular)
3907     {
3908       /* Make an undefined function descriptor sym, in order to
3909          pull in an --as-needed shared lib.  Archives are handled
3910          elsewhere.  */
3911       fdh = make_fdh (info, eh);
3912       if (fdh == NULL)
3913         return FALSE;
3914     }
3915
3916   if (fdh != NULL)
3917     {
3918       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
3919       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
3920
3921       /* Make both descriptor and entry symbol have the most
3922          constraining visibility of either symbol.  */
3923       if (entry_vis < descr_vis)
3924         fdh->elf.other += entry_vis - descr_vis;
3925       else if (entry_vis > descr_vis)
3926         eh->elf.other += descr_vis - entry_vis;
3927
3928       /* Propagate reference flags from entry symbol to function
3929          descriptor symbol.  */
3930       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
3931       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
3932       fdh->elf.ref_regular |= eh->elf.ref_regular;
3933       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
3934
3935       if (!fdh->elf.forced_local
3936           && fdh->elf.dynindx == -1
3937           && fdh->elf.versioned != versioned_hidden
3938           && (bfd_link_dll (info)
3939               || fdh->elf.def_dynamic
3940               || fdh->elf.ref_dynamic)
3941           && (eh->elf.ref_regular
3942               || eh->elf.def_regular))
3943         {
3944           if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
3945             return FALSE;
3946         }
3947     }
3948
3949   return TRUE;
3950 }
3951
3952 /* Set up opd section info and abiversion for IBFD, and process list
3953    of dot-symbols we made in link_hash_newfunc.  */
3954
3955 static bfd_boolean
3956 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
3957 {
3958   struct ppc_link_hash_table *htab;
3959   struct ppc_link_hash_entry **p, *eh;
3960   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
3961
3962   if (opd != NULL && opd->size != 0)
3963     {
3964       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
3965       ppc64_elf_section_data (opd)->sec_type = sec_opd;
3966
3967       if (abiversion (ibfd) == 0)
3968         set_abiversion (ibfd, 1);
3969       else if (abiversion (ibfd) >= 2)
3970         {
3971           /* xgettext:c-format */
3972           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
3973                               ibfd, abiversion (ibfd));
3974           bfd_set_error (bfd_error_bad_value);
3975           return FALSE;
3976         }
3977     }
3978
3979   if (is_ppc64_elf (info->output_bfd))
3980     {
3981       /* For input files without an explicit abiversion in e_flags
3982          we should have flagged any with symbol st_other bits set
3983          as ELFv1 and above flagged those with .opd as ELFv2.
3984          Set the output abiversion if not yet set, and for any input
3985          still ambiguous, take its abiversion from the output.
3986          Differences in ABI are reported later.  */
3987       if (abiversion (info->output_bfd) == 0)
3988         set_abiversion (info->output_bfd, abiversion (ibfd));
3989       else if (abiversion (ibfd) == 0)
3990         set_abiversion (ibfd, abiversion (info->output_bfd));
3991     }
3992
3993   htab = ppc_hash_table (info);
3994   if (htab == NULL)
3995     return TRUE;
3996
3997   if (opd != NULL && opd->size != 0
3998       && (ibfd->flags & DYNAMIC) == 0
3999       && (opd->flags & SEC_RELOC) != 0
4000       && opd->reloc_count != 0
4001       && !bfd_is_abs_section (opd->output_section)
4002       && info->gc_sections)
4003     {
4004       /* Garbage collection needs some extra help with .opd sections.
4005          We don't want to necessarily keep everything referenced by
4006          relocs in .opd, as that would keep all functions.  Instead,
4007          if we reference an .opd symbol (a function descriptor), we
4008          want to keep the function code symbol's section.  This is
4009          easy for global symbols, but for local syms we need to keep
4010          information about the associated function section.  */
4011       bfd_size_type amt;
4012       asection **opd_sym_map;
4013       Elf_Internal_Shdr *symtab_hdr;
4014       Elf_Internal_Rela *relocs, *rel_end, *rel;
4015
4016       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4017       opd_sym_map = bfd_zalloc (ibfd, amt);
4018       if (opd_sym_map == NULL)
4019         return FALSE;
4020       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4021       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4022                                           info->keep_memory);
4023       if (relocs == NULL)
4024         return FALSE;
4025       symtab_hdr = &elf_symtab_hdr (ibfd);
4026       rel_end = relocs + opd->reloc_count - 1;
4027       for (rel = relocs; rel < rel_end; rel++)
4028         {
4029           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4030           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4031
4032           if (r_type == R_PPC64_ADDR64
4033               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4034               && r_symndx < symtab_hdr->sh_info)
4035             {
4036               Elf_Internal_Sym *isym;
4037               asection *s;
4038
4039               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
4040               if (isym == NULL)
4041                 {
4042                   if (elf_section_data (opd)->relocs != relocs)
4043                     free (relocs);
4044                   return FALSE;
4045                 }
4046
4047               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4048               if (s != NULL && s != opd)
4049                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4050             }
4051         }
4052       if (elf_section_data (opd)->relocs != relocs)
4053         free (relocs);
4054     }
4055
4056   p = &htab->dot_syms;
4057   while ((eh = *p) != NULL)
4058     {
4059       *p = NULL;
4060       if (&eh->elf == htab->elf.hgot)
4061         ;
4062       else if (htab->elf.hgot == NULL
4063                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4064         htab->elf.hgot = &eh->elf;
4065       else if (abiversion (ibfd) <= 1)
4066         {
4067           htab->need_func_desc_adj = 1;
4068           if (!add_symbol_adjust (eh, info))
4069             return FALSE;
4070         }
4071       p = &eh->u.next_dot_sym;
4072     }
4073   return TRUE;
4074 }
4075
4076 /* Undo hash table changes when an --as-needed input file is determined
4077    not to be needed.  */
4078
4079 static bfd_boolean
4080 ppc64_elf_notice_as_needed (bfd *ibfd,
4081                             struct bfd_link_info *info,
4082                             enum notice_asneeded_action act)
4083 {
4084   if (act == notice_not_needed)
4085     {
4086       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4087
4088       if (htab == NULL)
4089         return FALSE;
4090
4091       htab->dot_syms = NULL;
4092     }
4093   return _bfd_elf_notice_as_needed (ibfd, info, act);
4094 }
4095
4096 /* If --just-symbols against a final linked binary, then assume we need
4097    toc adjusting stubs when calling functions defined there.  */
4098
4099 static void
4100 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4101 {
4102   if ((sec->flags & SEC_CODE) != 0
4103       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4104       && is_ppc64_elf (sec->owner))
4105     {
4106       if (abiversion (sec->owner) >= 2
4107           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4108         sec->has_toc_reloc = 1;
4109     }
4110   _bfd_elf_link_just_syms (sec, info);
4111 }
4112
4113 static struct plt_entry **
4114 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4115                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4116 {
4117   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4118   struct plt_entry **local_plt;
4119   unsigned char *local_got_tls_masks;
4120
4121   if (local_got_ents == NULL)
4122     {
4123       bfd_size_type size = symtab_hdr->sh_info;
4124
4125       size *= (sizeof (*local_got_ents)
4126                + sizeof (*local_plt)
4127                + sizeof (*local_got_tls_masks));
4128       local_got_ents = bfd_zalloc (abfd, size);
4129       if (local_got_ents == NULL)
4130         return NULL;
4131       elf_local_got_ents (abfd) = local_got_ents;
4132     }
4133
4134   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4135     {
4136       struct got_entry *ent;
4137
4138       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4139         if (ent->addend == r_addend
4140             && ent->owner == abfd
4141             && ent->tls_type == tls_type)
4142           break;
4143       if (ent == NULL)
4144         {
4145           bfd_size_type amt = sizeof (*ent);
4146           ent = bfd_alloc (abfd, amt);
4147           if (ent == NULL)
4148             return FALSE;
4149           ent->next = local_got_ents[r_symndx];
4150           ent->addend = r_addend;
4151           ent->owner = abfd;
4152           ent->tls_type = tls_type;
4153           ent->is_indirect = FALSE;
4154           ent->got.refcount = 0;
4155           local_got_ents[r_symndx] = ent;
4156         }
4157       ent->got.refcount += 1;
4158     }
4159
4160   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4161   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4162   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4163
4164   return local_plt + r_symndx;
4165 }
4166
4167 static bfd_boolean
4168 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4169 {
4170   struct plt_entry *ent;
4171
4172   for (ent = *plist; ent != NULL; ent = ent->next)
4173     if (ent->addend == addend)
4174       break;
4175   if (ent == NULL)
4176     {
4177       bfd_size_type amt = sizeof (*ent);
4178       ent = bfd_alloc (abfd, amt);
4179       if (ent == NULL)
4180         return FALSE;
4181       ent->next = *plist;
4182       ent->addend = addend;
4183       ent->plt.refcount = 0;
4184       *plist = ent;
4185     }
4186   ent->plt.refcount += 1;
4187   return TRUE;
4188 }
4189
4190 static bfd_boolean
4191 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4192 {
4193   return (r_type == R_PPC64_REL24
4194           || r_type == R_PPC64_REL24_NOTOC
4195           || r_type == R_PPC64_REL14
4196           || r_type == R_PPC64_REL14_BRTAKEN
4197           || r_type == R_PPC64_REL14_BRNTAKEN
4198           || r_type == R_PPC64_ADDR24
4199           || r_type == R_PPC64_ADDR14
4200           || r_type == R_PPC64_ADDR14_BRTAKEN
4201           || r_type == R_PPC64_ADDR14_BRNTAKEN
4202           || r_type == R_PPC64_PLTCALL);
4203 }
4204
4205 /* Relocs on inline plt call sequence insns prior to the call.  */
4206
4207 static bfd_boolean
4208 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4209 {
4210   return (r_type == R_PPC64_PLT16_HA
4211           || r_type == R_PPC64_PLT16_HI
4212           || r_type == R_PPC64_PLT16_LO
4213           || r_type == R_PPC64_PLT16_LO_DS
4214           || r_type == R_PPC64_PLTSEQ);
4215 }
4216
4217 /* Look through the relocs for a section during the first phase, and
4218    calculate needed space in the global offset table, procedure
4219    linkage table, and dynamic reloc sections.  */
4220
4221 static bfd_boolean
4222 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4223                         asection *sec, const Elf_Internal_Rela *relocs)
4224 {
4225   struct ppc_link_hash_table *htab;
4226   Elf_Internal_Shdr *symtab_hdr;
4227   struct elf_link_hash_entry **sym_hashes;
4228   const Elf_Internal_Rela *rel;
4229   const Elf_Internal_Rela *rel_end;
4230   asection *sreloc;
4231   struct elf_link_hash_entry *tga, *dottga;
4232   bfd_boolean is_opd;
4233
4234   if (bfd_link_relocatable (info))
4235     return TRUE;
4236
4237   /* Don't do anything special with non-loaded, non-alloced sections.
4238      In particular, any relocs in such sections should not affect GOT
4239      and PLT reference counting (ie. we don't allow them to create GOT
4240      or PLT entries), there's no possibility or desire to optimize TLS
4241      relocs, and there's not much point in propagating relocs to shared
4242      libs that the dynamic linker won't relocate.  */
4243   if ((sec->flags & SEC_ALLOC) == 0)
4244     return TRUE;
4245
4246   BFD_ASSERT (is_ppc64_elf (abfd));
4247
4248   htab = ppc_hash_table (info);
4249   if (htab == NULL)
4250     return FALSE;
4251
4252   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4253                               FALSE, FALSE, TRUE);
4254   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4255                                  FALSE, FALSE, TRUE);
4256   symtab_hdr = &elf_symtab_hdr (abfd);
4257   sym_hashes = elf_sym_hashes (abfd);
4258   sreloc = NULL;
4259   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4260   rel_end = relocs + sec->reloc_count;
4261   for (rel = relocs; rel < rel_end; rel++)
4262     {
4263       unsigned long r_symndx;
4264       struct elf_link_hash_entry *h;
4265       enum elf_ppc64_reloc_type r_type;
4266       int tls_type;
4267       struct _ppc64_elf_section_data *ppc64_sec;
4268       struct plt_entry **ifunc, **plt_list;
4269
4270       r_symndx = ELF64_R_SYM (rel->r_info);
4271       if (r_symndx < symtab_hdr->sh_info)
4272         h = NULL;
4273       else
4274         {
4275           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4276           h = elf_follow_link (h);
4277
4278           if (h == htab->elf.hgot)
4279             sec->has_toc_reloc = 1;
4280         }
4281
4282       tls_type = 0;
4283       ifunc = NULL;
4284       if (h != NULL)
4285         {
4286           if (h->type == STT_GNU_IFUNC)
4287             {
4288               h->needs_plt = 1;
4289               ifunc = &h->plt.plist;
4290             }
4291         }
4292       else
4293         {
4294           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4295                                                           abfd, r_symndx);
4296           if (isym == NULL)
4297             return FALSE;
4298
4299           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4300             {
4301               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4302                                              rel->r_addend,
4303                                              NON_GOT | PLT_IFUNC);
4304               if (ifunc == NULL)
4305                 return FALSE;
4306             }
4307         }
4308
4309       r_type = ELF64_R_TYPE (rel->r_info);
4310       switch (r_type)
4311         {
4312         case R_PPC64_TLSGD:
4313         case R_PPC64_TLSLD:
4314           /* These special tls relocs tie a call to __tls_get_addr with
4315              its parameter symbol.  */
4316           if (h != NULL)
4317             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
4318           else
4319             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4320                                         rel->r_addend,
4321                                         NON_GOT | TLS_TLS | TLS_MARK))
4322               return FALSE;
4323           sec->has_tls_reloc = 1;
4324           break;
4325
4326         case R_PPC64_GOT_TLSLD16:
4327         case R_PPC64_GOT_TLSLD16_LO:
4328         case R_PPC64_GOT_TLSLD16_HI:
4329         case R_PPC64_GOT_TLSLD16_HA:
4330           tls_type = TLS_TLS | TLS_LD;
4331           goto dogottls;
4332
4333         case R_PPC64_GOT_TLSGD16:
4334         case R_PPC64_GOT_TLSGD16_LO:
4335         case R_PPC64_GOT_TLSGD16_HI:
4336         case R_PPC64_GOT_TLSGD16_HA:
4337           tls_type = TLS_TLS | TLS_GD;
4338           goto dogottls;
4339
4340         case R_PPC64_GOT_TPREL16_DS:
4341         case R_PPC64_GOT_TPREL16_LO_DS:
4342         case R_PPC64_GOT_TPREL16_HI:
4343         case R_PPC64_GOT_TPREL16_HA:
4344           if (bfd_link_dll (info))
4345             info->flags |= DF_STATIC_TLS;
4346           tls_type = TLS_TLS | TLS_TPREL;
4347           goto dogottls;
4348
4349         case R_PPC64_GOT_DTPREL16_DS:
4350         case R_PPC64_GOT_DTPREL16_LO_DS:
4351         case R_PPC64_GOT_DTPREL16_HI:
4352         case R_PPC64_GOT_DTPREL16_HA:
4353           tls_type = TLS_TLS | TLS_DTPREL;
4354         dogottls:
4355           sec->has_tls_reloc = 1;
4356           /* Fall through */
4357
4358         case R_PPC64_GOT16:
4359         case R_PPC64_GOT16_DS:
4360         case R_PPC64_GOT16_HA:
4361         case R_PPC64_GOT16_HI:
4362         case R_PPC64_GOT16_LO:
4363         case R_PPC64_GOT16_LO_DS:
4364           /* This symbol requires a global offset table entry.  */
4365           sec->has_toc_reloc = 1;
4366           if (r_type == R_PPC64_GOT_TLSLD16
4367               || r_type == R_PPC64_GOT_TLSGD16
4368               || r_type == R_PPC64_GOT_TPREL16_DS
4369               || r_type == R_PPC64_GOT_DTPREL16_DS
4370               || r_type == R_PPC64_GOT16
4371               || r_type == R_PPC64_GOT16_DS)
4372             {
4373               htab->do_multi_toc = 1;
4374               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4375             }
4376
4377           if (ppc64_elf_tdata (abfd)->got == NULL
4378               && !create_got_section (abfd, info))
4379             return FALSE;
4380
4381           if (h != NULL)
4382             {
4383               struct ppc_link_hash_entry *eh;
4384               struct got_entry *ent;
4385
4386               eh = (struct ppc_link_hash_entry *) h;
4387               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4388                 if (ent->addend == rel->r_addend
4389                     && ent->owner == abfd
4390                     && ent->tls_type == tls_type)
4391                   break;
4392               if (ent == NULL)
4393                 {
4394                   bfd_size_type amt = sizeof (*ent);
4395                   ent = bfd_alloc (abfd, amt);
4396                   if (ent == NULL)
4397                     return FALSE;
4398                   ent->next = eh->elf.got.glist;
4399                   ent->addend = rel->r_addend;
4400                   ent->owner = abfd;
4401                   ent->tls_type = tls_type;
4402                   ent->is_indirect = FALSE;
4403                   ent->got.refcount = 0;
4404                   eh->elf.got.glist = ent;
4405                 }
4406               ent->got.refcount += 1;
4407               eh->tls_mask |= tls_type;
4408             }
4409           else
4410             /* This is a global offset table entry for a local symbol.  */
4411             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4412                                         rel->r_addend, tls_type))
4413               return FALSE;
4414
4415           /* We may also need a plt entry if the symbol turns out to be
4416              an ifunc.  */
4417           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
4418             {
4419               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4420                 return FALSE;
4421             }
4422           break;
4423
4424         case R_PPC64_PLT16_HA:
4425         case R_PPC64_PLT16_HI:
4426         case R_PPC64_PLT16_LO:
4427         case R_PPC64_PLT16_LO_DS:
4428         case R_PPC64_PLT32:
4429         case R_PPC64_PLT64:
4430           /* This symbol requires a procedure linkage table entry.  */
4431           plt_list = ifunc;
4432           if (h != NULL)
4433             {
4434               h->needs_plt = 1;
4435               if (h->root.root.string[0] == '.'
4436                   && h->root.root.string[1] != '\0')
4437                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4438               ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
4439               plt_list = &h->plt.plist;
4440             }
4441           if (plt_list == NULL)
4442             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4443                                               rel->r_addend,
4444                                               NON_GOT | PLT_KEEP);
4445           if (!update_plt_info (abfd, plt_list, rel->r_addend))
4446             return FALSE;
4447           break;
4448
4449           /* The following relocations don't need to propagate the
4450              relocation if linking a shared object since they are
4451              section relative.  */
4452         case R_PPC64_SECTOFF:
4453         case R_PPC64_SECTOFF_LO:
4454         case R_PPC64_SECTOFF_HI:
4455         case R_PPC64_SECTOFF_HA:
4456         case R_PPC64_SECTOFF_DS:
4457         case R_PPC64_SECTOFF_LO_DS:
4458         case R_PPC64_DTPREL16:
4459         case R_PPC64_DTPREL16_LO:
4460         case R_PPC64_DTPREL16_HI:
4461         case R_PPC64_DTPREL16_HA:
4462         case R_PPC64_DTPREL16_DS:
4463         case R_PPC64_DTPREL16_LO_DS:
4464         case R_PPC64_DTPREL16_HIGH:
4465         case R_PPC64_DTPREL16_HIGHA:
4466         case R_PPC64_DTPREL16_HIGHER:
4467         case R_PPC64_DTPREL16_HIGHERA:
4468         case R_PPC64_DTPREL16_HIGHEST:
4469         case R_PPC64_DTPREL16_HIGHESTA:
4470           break;
4471
4472           /* Nor do these.  */
4473         case R_PPC64_REL16:
4474         case R_PPC64_REL16_LO:
4475         case R_PPC64_REL16_HI:
4476         case R_PPC64_REL16_HA:
4477         case R_PPC64_REL16DX_HA:
4478           break;
4479
4480           /* Not supported as a dynamic relocation.  */
4481         case R_PPC64_ADDR64_LOCAL:
4482           if (bfd_link_pic (info))
4483             {
4484               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4485                 ppc_howto_init ();
4486               /* xgettext:c-format */
4487               info->callbacks->einfo (_("%H: %s reloc unsupported "
4488                                         "in shared libraries and PIEs\n"),
4489                                       abfd, sec, rel->r_offset,
4490                                       ppc64_elf_howto_table[r_type]->name);
4491               bfd_set_error (bfd_error_bad_value);
4492               return FALSE;
4493             }
4494           break;
4495
4496         case R_PPC64_TOC16:
4497         case R_PPC64_TOC16_DS:
4498           htab->do_multi_toc = 1;
4499           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4500           /* Fall through.  */
4501         case R_PPC64_TOC16_LO:
4502         case R_PPC64_TOC16_HI:
4503         case R_PPC64_TOC16_HA:
4504         case R_PPC64_TOC16_LO_DS:
4505           sec->has_toc_reloc = 1;
4506           break;
4507
4508           /* Marker reloc.  */
4509         case R_PPC64_ENTRY:
4510           break;
4511
4512           /* This relocation describes the C++ object vtable hierarchy.
4513              Reconstruct it for later use during GC.  */
4514         case R_PPC64_GNU_VTINHERIT:
4515           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4516             return FALSE;
4517           break;
4518
4519           /* This relocation describes which C++ vtable entries are actually
4520              used.  Record for later use during GC.  */
4521         case R_PPC64_GNU_VTENTRY:
4522           BFD_ASSERT (h != NULL);
4523           if (h != NULL
4524               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4525             return FALSE;
4526           break;
4527
4528         case R_PPC64_REL14:
4529         case R_PPC64_REL14_BRTAKEN:
4530         case R_PPC64_REL14_BRNTAKEN:
4531           {
4532             asection *dest = NULL;
4533
4534             /* Heuristic: If jumping outside our section, chances are
4535                we are going to need a stub.  */
4536             if (h != NULL)
4537               {
4538                 /* If the sym is weak it may be overridden later, so
4539                    don't assume we know where a weak sym lives.  */
4540                 if (h->root.type == bfd_link_hash_defined)
4541                   dest = h->root.u.def.section;
4542               }
4543             else
4544               {
4545                 Elf_Internal_Sym *isym;
4546
4547                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4548                                               abfd, r_symndx);
4549                 if (isym == NULL)
4550                   return FALSE;
4551
4552                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4553               }
4554
4555             if (dest != sec)
4556               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4557           }
4558           goto rel24;
4559
4560         case R_PPC64_PLTCALL:
4561           ppc64_elf_section_data (sec)->has_pltcall = 1;
4562           /* Fall through.  */
4563
4564         case R_PPC64_REL24:
4565         case R_PPC64_REL24_NOTOC:
4566         rel24:
4567           plt_list = ifunc;
4568           if (h != NULL)
4569             {
4570               h->needs_plt = 1;
4571               if (h->root.root.string[0] == '.'
4572                   && h->root.root.string[1] != '\0')
4573                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4574
4575               if (h == tga || h == dottga)
4576                 {
4577                   sec->has_tls_reloc = 1;
4578                   if (rel != relocs
4579                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4580                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4581                     /* We have a new-style __tls_get_addr call with
4582                        a marker reloc.  */
4583                     ;
4584                   else
4585                     /* Mark this section as having an old-style call.  */
4586                     sec->has_tls_get_addr_call = 1;
4587                 }
4588               plt_list = &h->plt.plist;
4589             }
4590
4591           /* We may need a .plt entry if the function this reloc
4592              refers to is in a shared lib.  */
4593           if (plt_list
4594               && !update_plt_info (abfd, plt_list, rel->r_addend))
4595             return FALSE;
4596           break;
4597
4598         case R_PPC64_ADDR14:
4599         case R_PPC64_ADDR14_BRNTAKEN:
4600         case R_PPC64_ADDR14_BRTAKEN:
4601         case R_PPC64_ADDR24:
4602           goto dodyn;
4603
4604         case R_PPC64_TPREL64:
4605           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4606           if (bfd_link_dll (info))
4607             info->flags |= DF_STATIC_TLS;
4608           goto dotlstoc;
4609
4610         case R_PPC64_DTPMOD64:
4611           if (rel + 1 < rel_end
4612               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4613               && rel[1].r_offset == rel->r_offset + 8)
4614             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4615           else
4616             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4617           goto dotlstoc;
4618
4619         case R_PPC64_DTPREL64:
4620           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4621           if (rel != relocs
4622               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4623               && rel[-1].r_offset == rel->r_offset - 8)
4624             /* This is the second reloc of a dtpmod, dtprel pair.
4625                Don't mark with TLS_DTPREL.  */
4626             goto dodyn;
4627
4628         dotlstoc:
4629           sec->has_tls_reloc = 1;
4630           if (h != NULL)
4631             {
4632               struct ppc_link_hash_entry *eh;
4633               eh = (struct ppc_link_hash_entry *) h;
4634               eh->tls_mask |= tls_type;
4635             }
4636           else
4637             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4638                                         rel->r_addend, tls_type))
4639               return FALSE;
4640
4641           ppc64_sec = ppc64_elf_section_data (sec);
4642           if (ppc64_sec->sec_type != sec_toc)
4643             {
4644               bfd_size_type amt;
4645
4646               /* One extra to simplify get_tls_mask.  */
4647               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
4648               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
4649               if (ppc64_sec->u.toc.symndx == NULL)
4650                 return FALSE;
4651               amt = sec->size * sizeof (bfd_vma) / 8;
4652               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
4653               if (ppc64_sec->u.toc.add == NULL)
4654                 return FALSE;
4655               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4656               ppc64_sec->sec_type = sec_toc;
4657             }
4658           BFD_ASSERT (rel->r_offset % 8 == 0);
4659           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
4660           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
4661
4662           /* Mark the second slot of a GD or LD entry.
4663              -1 to indicate GD and -2 to indicate LD.  */
4664           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4665             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
4666           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4667             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
4668           goto dodyn;
4669
4670         case R_PPC64_TPREL16:
4671         case R_PPC64_TPREL16_LO:
4672         case R_PPC64_TPREL16_HI:
4673         case R_PPC64_TPREL16_HA:
4674         case R_PPC64_TPREL16_DS:
4675         case R_PPC64_TPREL16_LO_DS:
4676         case R_PPC64_TPREL16_HIGH:
4677         case R_PPC64_TPREL16_HIGHA:
4678         case R_PPC64_TPREL16_HIGHER:
4679         case R_PPC64_TPREL16_HIGHERA:
4680         case R_PPC64_TPREL16_HIGHEST:
4681         case R_PPC64_TPREL16_HIGHESTA:
4682           if (bfd_link_dll (info))
4683             info->flags |= DF_STATIC_TLS;
4684           goto dodyn;
4685
4686         case R_PPC64_ADDR64:
4687           if (is_opd
4688               && rel + 1 < rel_end
4689               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4690             {
4691               if (h != NULL)
4692                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4693             }
4694           /* Fall through.  */
4695
4696         case R_PPC64_ADDR16:
4697         case R_PPC64_ADDR16_DS:
4698         case R_PPC64_ADDR16_HA:
4699         case R_PPC64_ADDR16_HI:
4700         case R_PPC64_ADDR16_HIGH:
4701         case R_PPC64_ADDR16_HIGHA:
4702         case R_PPC64_ADDR16_HIGHER:
4703         case R_PPC64_ADDR16_HIGHERA:
4704         case R_PPC64_ADDR16_HIGHEST:
4705         case R_PPC64_ADDR16_HIGHESTA:
4706         case R_PPC64_ADDR16_LO:
4707         case R_PPC64_ADDR16_LO_DS:
4708           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
4709               && rel->r_addend == 0)
4710             {
4711               /* We may need a .plt entry if this reloc refers to a
4712                  function in a shared lib.  */
4713               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
4714                 return FALSE;
4715               h->pointer_equality_needed = 1;
4716             }
4717           /* Fall through.  */
4718
4719         case R_PPC64_REL30:
4720         case R_PPC64_REL32:
4721         case R_PPC64_REL64:
4722         case R_PPC64_ADDR32:
4723         case R_PPC64_UADDR16:
4724         case R_PPC64_UADDR32:
4725         case R_PPC64_UADDR64:
4726         case R_PPC64_TOC:
4727           if (h != NULL && !bfd_link_pic (info))
4728             /* We may need a copy reloc.  */
4729             h->non_got_ref = 1;
4730
4731           /* Don't propagate .opd relocs.  */
4732           if (NO_OPD_RELOCS && is_opd)
4733             break;
4734
4735           /* If we are creating a shared library, and this is a reloc
4736              against a global symbol, or a non PC relative reloc
4737              against a local symbol, then we need to copy the reloc
4738              into the shared library.  However, if we are linking with
4739              -Bsymbolic, we do not need to copy a reloc against a
4740              global symbol which is defined in an object we are
4741              including in the link (i.e., DEF_REGULAR is set).  At
4742              this point we have not seen all the input files, so it is
4743              possible that DEF_REGULAR is not set now but will be set
4744              later (it is never cleared).  In case of a weak definition,
4745              DEF_REGULAR may be cleared later by a strong definition in
4746              a shared library.  We account for that possibility below by
4747              storing information in the dyn_relocs field of the hash
4748              table entry.  A similar situation occurs when creating
4749              shared libraries and symbol visibility changes render the
4750              symbol local.
4751
4752              If on the other hand, we are creating an executable, we
4753              may need to keep relocations for symbols satisfied by a
4754              dynamic library if we manage to avoid copy relocs for the
4755              symbol.  */
4756         dodyn:
4757           if ((bfd_link_pic (info)
4758                && (must_be_dyn_reloc (info, r_type)
4759                    || (h != NULL
4760                        && (!SYMBOLIC_BIND (info, h)
4761                            || h->root.type == bfd_link_hash_defweak
4762                            || !h->def_regular))))
4763               || (ELIMINATE_COPY_RELOCS
4764                   && !bfd_link_pic (info)
4765                   && h != NULL
4766                   && (h->root.type == bfd_link_hash_defweak
4767                       || !h->def_regular))
4768               || (!bfd_link_pic (info)
4769                   && ifunc != NULL))
4770             {
4771               /* We must copy these reloc types into the output file.
4772                  Create a reloc section in dynobj and make room for
4773                  this reloc.  */
4774               if (sreloc == NULL)
4775                 {
4776                   sreloc = _bfd_elf_make_dynamic_reloc_section
4777                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
4778
4779                   if (sreloc == NULL)
4780                     return FALSE;
4781                 }
4782
4783               /* If this is a global symbol, we count the number of
4784                  relocations we need for this symbol.  */
4785               if (h != NULL)
4786                 {
4787                   struct elf_dyn_relocs *p;
4788                   struct elf_dyn_relocs **head;
4789
4790                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4791                   p = *head;
4792                   if (p == NULL || p->sec != sec)
4793                     {
4794                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4795                       if (p == NULL)
4796                         return FALSE;
4797                       p->next = *head;
4798                       *head = p;
4799                       p->sec = sec;
4800                       p->count = 0;
4801                       p->pc_count = 0;
4802                     }
4803                   p->count += 1;
4804                   if (!must_be_dyn_reloc (info, r_type))
4805                     p->pc_count += 1;
4806                 }
4807               else
4808                 {
4809                   /* Track dynamic relocs needed for local syms too.
4810                      We really need local syms available to do this
4811                      easily.  Oh well.  */
4812                   struct ppc_dyn_relocs *p;
4813                   struct ppc_dyn_relocs **head;
4814                   bfd_boolean is_ifunc;
4815                   asection *s;
4816                   void *vpp;
4817                   Elf_Internal_Sym *isym;
4818
4819                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4820                                                 abfd, r_symndx);
4821                   if (isym == NULL)
4822                     return FALSE;
4823
4824                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
4825                   if (s == NULL)
4826                     s = sec;
4827
4828                   vpp = &elf_section_data (s)->local_dynrel;
4829                   head = (struct ppc_dyn_relocs **) vpp;
4830                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
4831                   p = *head;
4832                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
4833                     p = p->next;
4834                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
4835                     {
4836                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4837                       if (p == NULL)
4838                         return FALSE;
4839                       p->next = *head;
4840                       *head = p;
4841                       p->sec = sec;
4842                       p->ifunc = is_ifunc;
4843                       p->count = 0;
4844                     }
4845                   p->count += 1;
4846                 }
4847             }
4848           break;
4849
4850         default:
4851           break;
4852         }
4853     }
4854
4855   return TRUE;
4856 }
4857
4858 /* Merge backend specific data from an object file to the output
4859    object file when linking.  */
4860
4861 static bfd_boolean
4862 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4863 {
4864   bfd *obfd = info->output_bfd;
4865   unsigned long iflags, oflags;
4866
4867   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
4868     return TRUE;
4869
4870   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
4871     return TRUE;
4872
4873   if (!_bfd_generic_verify_endian_match (ibfd, info))
4874     return FALSE;
4875
4876   iflags = elf_elfheader (ibfd)->e_flags;
4877   oflags = elf_elfheader (obfd)->e_flags;
4878
4879   if (iflags & ~EF_PPC64_ABI)
4880     {
4881       _bfd_error_handler
4882         /* xgettext:c-format */
4883         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
4884       bfd_set_error (bfd_error_bad_value);
4885       return FALSE;
4886     }
4887   else if (iflags != oflags && iflags != 0)
4888     {
4889       _bfd_error_handler
4890         /* xgettext:c-format */
4891         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
4892          ibfd, iflags, oflags);
4893       bfd_set_error (bfd_error_bad_value);
4894       return FALSE;
4895     }
4896
4897   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
4898     return FALSE;
4899
4900   /* Merge Tag_compatibility attributes and any common GNU ones.  */
4901   return _bfd_elf_merge_object_attributes (ibfd, info);
4902 }
4903
4904 static bfd_boolean
4905 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
4906 {
4907   /* Print normal ELF private data.  */
4908   _bfd_elf_print_private_bfd_data (abfd, ptr);
4909
4910   if (elf_elfheader (abfd)->e_flags != 0)
4911     {
4912       FILE *file = ptr;
4913
4914       fprintf (file, _("private flags = 0x%lx:"),
4915                elf_elfheader (abfd)->e_flags);
4916
4917       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
4918         fprintf (file, _(" [abiv%ld]"),
4919                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
4920       fputc ('\n', file);
4921     }
4922
4923   return TRUE;
4924 }
4925
4926 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
4927    of the code entry point, and its section, which must be in the same
4928    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
4929
4930 static bfd_vma
4931 opd_entry_value (asection *opd_sec,
4932                  bfd_vma offset,
4933                  asection **code_sec,
4934                  bfd_vma *code_off,
4935                  bfd_boolean in_code_sec)
4936 {
4937   bfd *opd_bfd = opd_sec->owner;
4938   Elf_Internal_Rela *relocs;
4939   Elf_Internal_Rela *lo, *hi, *look;
4940   bfd_vma val;
4941
4942   /* No relocs implies we are linking a --just-symbols object, or looking
4943      at a final linked executable with addr2line or somesuch.  */
4944   if (opd_sec->reloc_count == 0)
4945     {
4946       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
4947
4948       if (contents == NULL)
4949         {
4950           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
4951             return (bfd_vma) -1;
4952           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
4953         }
4954
4955       /* PR 17512: file: 64b9dfbb.  */
4956       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
4957         return (bfd_vma) -1;
4958
4959       val = bfd_get_64 (opd_bfd, contents + offset);
4960       if (code_sec != NULL)
4961         {
4962           asection *sec, *likely = NULL;
4963
4964           if (in_code_sec)
4965             {
4966               sec = *code_sec;
4967               if (sec->vma <= val
4968                   && val < sec->vma + sec->size)
4969                 likely = sec;
4970               else
4971                 val = -1;
4972             }
4973           else
4974             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4975               if (sec->vma <= val
4976                   && (sec->flags & SEC_LOAD) != 0
4977                   && (sec->flags & SEC_ALLOC) != 0)
4978                 likely = sec;
4979           if (likely != NULL)
4980             {
4981               *code_sec = likely;
4982               if (code_off != NULL)
4983                 *code_off = val - likely->vma;
4984             }
4985         }
4986       return val;
4987     }
4988
4989   BFD_ASSERT (is_ppc64_elf (opd_bfd));
4990
4991   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
4992   if (relocs == NULL)
4993     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4994   /* PR 17512: file: df8e1fd6.  */
4995   if (relocs == NULL)
4996     return (bfd_vma) -1;
4997
4998   /* Go find the opd reloc at the sym address.  */
4999   lo = relocs;
5000   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5001   val = (bfd_vma) -1;
5002   while (lo < hi)
5003     {
5004       look = lo + (hi - lo) / 2;
5005       if (look->r_offset < offset)
5006         lo = look + 1;
5007       else if (look->r_offset > offset)
5008         hi = look;
5009       else
5010         {
5011           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5012
5013           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5014               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5015             {
5016               unsigned long symndx = ELF64_R_SYM (look->r_info);
5017               asection *sec = NULL;
5018
5019               if (symndx >= symtab_hdr->sh_info
5020                   && elf_sym_hashes (opd_bfd) != NULL)
5021                 {
5022                   struct elf_link_hash_entry **sym_hashes;
5023                   struct elf_link_hash_entry *rh;
5024
5025                   sym_hashes = elf_sym_hashes (opd_bfd);
5026                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
5027                   if (rh != NULL)
5028                     {
5029                       rh = elf_follow_link (rh);
5030                       if (rh->root.type != bfd_link_hash_defined
5031                           && rh->root.type != bfd_link_hash_defweak)
5032                         break;
5033                       if (rh->root.u.def.section->owner == opd_bfd)
5034                         {
5035                           val = rh->root.u.def.value;
5036                           sec = rh->root.u.def.section;
5037                         }
5038                     }
5039                 }
5040
5041               if (sec == NULL)
5042                 {
5043                   Elf_Internal_Sym *sym;
5044
5045                   if (symndx < symtab_hdr->sh_info)
5046                     {
5047                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5048                       if (sym == NULL)
5049                         {
5050                           size_t symcnt = symtab_hdr->sh_info;
5051                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5052                                                       symcnt, 0,
5053                                                       NULL, NULL, NULL);
5054                           if (sym == NULL)
5055                             break;
5056                           symtab_hdr->contents = (bfd_byte *) sym;
5057                         }
5058                       sym += symndx;
5059                     }
5060                   else
5061                     {
5062                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5063                                                   1, symndx,
5064                                                   NULL, NULL, NULL);
5065                       if (sym == NULL)
5066                         break;
5067                     }
5068                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5069                   if (sec == NULL)
5070                     break;
5071                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5072                   val = sym->st_value;
5073                 }
5074
5075               val += look->r_addend;
5076               if (code_off != NULL)
5077                 *code_off = val;
5078               if (code_sec != NULL)
5079                 {
5080                   if (in_code_sec && *code_sec != sec)
5081                     return -1;
5082                   else
5083                     *code_sec = sec;
5084                 }
5085               if (sec->output_section != NULL)
5086                 val += sec->output_section->vma + sec->output_offset;
5087             }
5088           break;
5089         }
5090     }
5091
5092   return val;
5093 }
5094
5095 /* If the ELF symbol SYM might be a function in SEC, return the
5096    function size and set *CODE_OFF to the function's entry point,
5097    otherwise return zero.  */
5098
5099 static bfd_size_type
5100 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5101                               bfd_vma *code_off)
5102 {
5103   bfd_size_type size;
5104
5105   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5106                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5107     return 0;
5108
5109   size = 0;
5110   if (!(sym->flags & BSF_SYNTHETIC))
5111     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5112
5113   if (strcmp (sym->section->name, ".opd") == 0)
5114     {
5115       struct _opd_sec_data *opd = get_opd_info (sym->section);
5116       bfd_vma symval = sym->value;
5117
5118       if (opd != NULL
5119           && opd->adjust != NULL
5120           && elf_section_data (sym->section)->relocs != NULL)
5121         {
5122           /* opd_entry_value will use cached relocs that have been
5123              adjusted, but with raw symbols.  That means both local
5124              and global symbols need adjusting.  */
5125           long adjust = opd->adjust[OPD_NDX (symval)];
5126           if (adjust == -1)
5127             return 0;
5128           symval += adjust;
5129         }
5130
5131       if (opd_entry_value (sym->section, symval,
5132                            &sec, code_off, TRUE) == (bfd_vma) -1)
5133         return 0;
5134       /* An old ABI binary with dot-syms has a size of 24 on the .opd
5135          symbol.  This size has nothing to do with the code size of the
5136          function, which is what we're supposed to return, but the
5137          code size isn't available without looking up the dot-sym.
5138          However, doing that would be a waste of time particularly
5139          since elf_find_function will look at the dot-sym anyway.
5140          Now, elf_find_function will keep the largest size of any
5141          function sym found at the code address of interest, so return
5142          1 here to avoid it incorrectly caching a larger function size
5143          for a small function.  This does mean we return the wrong
5144          size for a new-ABI function of size 24, but all that does is
5145          disable caching for such functions.  */
5146       if (size == 24)
5147         size = 1;
5148     }
5149   else
5150     {
5151       if (sym->section != sec)
5152         return 0;
5153       *code_off = sym->value;
5154     }
5155   if (size == 0)
5156     size = 1;
5157   return size;
5158 }
5159
5160 /* Return true if symbol is a strong function defined in an ELFv2
5161    object with st_other localentry bits of zero, ie. its local entry
5162    point coincides with its global entry point.  */
5163
5164 static bfd_boolean
5165 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5166 {
5167   return (h != NULL
5168           && h->type == STT_FUNC
5169           && h->root.type == bfd_link_hash_defined
5170           && (STO_PPC64_LOCAL_MASK & h->other) == 0
5171           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
5172           && is_ppc64_elf (h->root.u.def.section->owner)
5173           && abiversion (h->root.u.def.section->owner) >= 2);
5174 }
5175
5176 /* Return true if symbol is defined in a regular object file.  */
5177
5178 static bfd_boolean
5179 is_static_defined (struct elf_link_hash_entry *h)
5180 {
5181   return ((h->root.type == bfd_link_hash_defined
5182            || h->root.type == bfd_link_hash_defweak)
5183           && h->root.u.def.section != NULL
5184           && h->root.u.def.section->output_section != NULL);
5185 }
5186
5187 /* If FDH is a function descriptor symbol, return the associated code
5188    entry symbol if it is defined.  Return NULL otherwise.  */
5189
5190 static struct ppc_link_hash_entry *
5191 defined_code_entry (struct ppc_link_hash_entry *fdh)
5192 {
5193   if (fdh->is_func_descriptor)
5194     {
5195       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5196       if (fh->elf.root.type == bfd_link_hash_defined
5197           || fh->elf.root.type == bfd_link_hash_defweak)
5198         return fh;
5199     }
5200   return NULL;
5201 }
5202
5203 /* If FH is a function code entry symbol, return the associated
5204    function descriptor symbol if it is defined.  Return NULL otherwise.  */
5205
5206 static struct ppc_link_hash_entry *
5207 defined_func_desc (struct ppc_link_hash_entry *fh)
5208 {
5209   if (fh->oh != NULL
5210       && fh->oh->is_func_descriptor)
5211     {
5212       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5213       if (fdh->elf.root.type == bfd_link_hash_defined
5214           || fdh->elf.root.type == bfd_link_hash_defweak)
5215         return fdh;
5216     }
5217   return NULL;
5218 }
5219
5220 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5221
5222 /* Garbage collect sections, after first dealing with dot-symbols.  */
5223
5224 static bfd_boolean
5225 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5226 {
5227   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5228
5229   if (htab != NULL && htab->need_func_desc_adj)
5230     {
5231       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5232       htab->need_func_desc_adj = 0;
5233     }
5234   return bfd_elf_gc_sections (abfd, info);
5235 }
5236
5237 /* Mark all our entry sym sections, both opd and code section.  */
5238
5239 static void
5240 ppc64_elf_gc_keep (struct bfd_link_info *info)
5241 {
5242   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5243   struct bfd_sym_chain *sym;
5244
5245   if (htab == NULL)
5246     return;
5247
5248   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5249     {
5250       struct ppc_link_hash_entry *eh, *fh;
5251       asection *sec;
5252
5253       eh = (struct ppc_link_hash_entry *)
5254         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5255       if (eh == NULL)
5256         continue;
5257       if (eh->elf.root.type != bfd_link_hash_defined
5258           && eh->elf.root.type != bfd_link_hash_defweak)
5259         continue;
5260
5261       fh = defined_code_entry (eh);
5262       if (fh != NULL)
5263         {
5264           sec = fh->elf.root.u.def.section;
5265           sec->flags |= SEC_KEEP;
5266         }
5267       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5268                && opd_entry_value (eh->elf.root.u.def.section,
5269                                    eh->elf.root.u.def.value,
5270                                    &sec, NULL, FALSE) != (bfd_vma) -1)
5271         sec->flags |= SEC_KEEP;
5272
5273       sec = eh->elf.root.u.def.section;
5274       sec->flags |= SEC_KEEP;
5275     }
5276 }
5277
5278 /* Mark sections containing dynamically referenced symbols.  When
5279    building shared libraries, we must assume that any visible symbol is
5280    referenced.  */
5281
5282 static bfd_boolean
5283 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5284 {
5285   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5286   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5287   struct ppc_link_hash_entry *fdh;
5288   struct bfd_elf_dynamic_list *d = info->dynamic_list;
5289
5290   /* Dynamic linking info is on the func descriptor sym.  */
5291   fdh = defined_func_desc (eh);
5292   if (fdh != NULL)
5293     eh = fdh;
5294
5295   if ((eh->elf.root.type == bfd_link_hash_defined
5296        || eh->elf.root.type == bfd_link_hash_defweak)
5297       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5298           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5299               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5300               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5301               && (!bfd_link_executable (info)
5302                   || info->gc_keep_exported
5303                   || info->export_dynamic
5304                   || (eh->elf.dynamic
5305                       && d != NULL
5306                       && (*d->match) (&d->head, NULL,
5307                                       eh->elf.root.root.string)))
5308               && (eh->elf.versioned >= versioned
5309                   || !bfd_hide_sym_by_version (info->version_info,
5310                                                eh->elf.root.root.string)))))
5311     {
5312       asection *code_sec;
5313       struct ppc_link_hash_entry *fh;
5314
5315       eh->elf.root.u.def.section->flags |= SEC_KEEP;
5316
5317       /* Function descriptor syms cause the associated
5318          function code sym section to be marked.  */
5319       fh = defined_code_entry (eh);
5320       if (fh != NULL)
5321         {
5322           code_sec = fh->elf.root.u.def.section;
5323           code_sec->flags |= SEC_KEEP;
5324         }
5325       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5326                && opd_entry_value (eh->elf.root.u.def.section,
5327                                    eh->elf.root.u.def.value,
5328                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
5329         code_sec->flags |= SEC_KEEP;
5330     }
5331
5332   return TRUE;
5333 }
5334
5335 /* Return the section that should be marked against GC for a given
5336    relocation.  */
5337
5338 static asection *
5339 ppc64_elf_gc_mark_hook (asection *sec,
5340                         struct bfd_link_info *info,
5341                         Elf_Internal_Rela *rel,
5342                         struct elf_link_hash_entry *h,
5343                         Elf_Internal_Sym *sym)
5344 {
5345   asection *rsec;
5346
5347   /* Syms return NULL if we're marking .opd, so we avoid marking all
5348      function sections, as all functions are referenced in .opd.  */
5349   rsec = NULL;
5350   if (get_opd_info (sec) != NULL)
5351     return rsec;
5352
5353   if (h != NULL)
5354     {
5355       enum elf_ppc64_reloc_type r_type;
5356       struct ppc_link_hash_entry *eh, *fh, *fdh;
5357
5358       r_type = ELF64_R_TYPE (rel->r_info);
5359       switch (r_type)
5360         {
5361         case R_PPC64_GNU_VTINHERIT:
5362         case R_PPC64_GNU_VTENTRY:
5363           break;
5364
5365         default:
5366           switch (h->root.type)
5367             {
5368             case bfd_link_hash_defined:
5369             case bfd_link_hash_defweak:
5370               eh = (struct ppc_link_hash_entry *) h;
5371               fdh = defined_func_desc (eh);
5372               if (fdh != NULL)
5373                 {
5374                   /* -mcall-aixdesc code references the dot-symbol on
5375                      a call reloc.  Mark the function descriptor too
5376                      against garbage collection.  */
5377                   fdh->elf.mark = 1;
5378                   if (fdh->elf.is_weakalias)
5379                     weakdef (&fdh->elf)->mark = 1;
5380                   eh = fdh;
5381                 }
5382
5383               /* Function descriptor syms cause the associated
5384                  function code sym section to be marked.  */
5385               fh = defined_code_entry (eh);
5386               if (fh != NULL)
5387                 {
5388                   /* They also mark their opd section.  */
5389                   eh->elf.root.u.def.section->gc_mark = 1;
5390
5391                   rsec = fh->elf.root.u.def.section;
5392                 }
5393               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5394                        && opd_entry_value (eh->elf.root.u.def.section,
5395                                            eh->elf.root.u.def.value,
5396                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
5397                 eh->elf.root.u.def.section->gc_mark = 1;
5398               else
5399                 rsec = h->root.u.def.section;
5400               break;
5401
5402             case bfd_link_hash_common:
5403               rsec = h->root.u.c.p->section;
5404               break;
5405
5406             default:
5407               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5408             }
5409         }
5410     }
5411   else
5412     {
5413       struct _opd_sec_data *opd;
5414
5415       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5416       opd = get_opd_info (rsec);
5417       if (opd != NULL && opd->func_sec != NULL)
5418         {
5419           rsec->gc_mark = 1;
5420
5421           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5422         }
5423     }
5424
5425   return rsec;
5426 }
5427
5428 /* The maximum size of .sfpr.  */
5429 #define SFPR_MAX (218*4)
5430
5431 struct sfpr_def_parms
5432 {
5433   const char name[12];
5434   unsigned char lo, hi;
5435   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5436   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5437 };
5438
5439 /* Auto-generate _save*, _rest* functions in .sfpr.
5440    If STUB_SEC is non-null, define alias symbols in STUB_SEC
5441    instead.  */
5442
5443 static bfd_boolean
5444 sfpr_define (struct bfd_link_info *info,
5445              const struct sfpr_def_parms *parm,
5446              asection *stub_sec)
5447 {
5448   struct ppc_link_hash_table *htab = ppc_hash_table (info);
5449   unsigned int i;
5450   size_t len = strlen (parm->name);
5451   bfd_boolean writing = FALSE;
5452   char sym[16];
5453
5454   if (htab == NULL)
5455     return FALSE;
5456
5457   memcpy (sym, parm->name, len);
5458   sym[len + 2] = 0;
5459
5460   for (i = parm->lo; i <= parm->hi; i++)
5461     {
5462       struct ppc_link_hash_entry *h;
5463
5464       sym[len + 0] = i / 10 + '0';
5465       sym[len + 1] = i % 10 + '0';
5466       h = (struct ppc_link_hash_entry *)
5467         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
5468       if (stub_sec != NULL)
5469         {
5470           if (h != NULL
5471               && h->elf.root.type == bfd_link_hash_defined
5472               && h->elf.root.u.def.section == htab->sfpr)
5473             {
5474               struct elf_link_hash_entry *s;
5475               char buf[32];
5476               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5477               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5478               if (s == NULL)
5479                 return FALSE;
5480               if (s->root.type == bfd_link_hash_new
5481                   || (s->root.type = bfd_link_hash_defined
5482                       && s->root.u.def.section == stub_sec))
5483                 {
5484                   s->root.type = bfd_link_hash_defined;
5485                   s->root.u.def.section = stub_sec;
5486                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5487                                          + h->elf.root.u.def.value);
5488                   s->ref_regular = 1;
5489                   s->def_regular = 1;
5490                   s->ref_regular_nonweak = 1;
5491                   s->forced_local = 1;
5492                   s->non_elf = 0;
5493                   s->root.linker_def = 1;
5494                 }
5495             }
5496           continue;
5497         }
5498       if (h != NULL)
5499         {
5500           h->save_res = 1;
5501           if (!h->elf.def_regular)
5502             {
5503               h->elf.root.type = bfd_link_hash_defined;
5504               h->elf.root.u.def.section = htab->sfpr;
5505               h->elf.root.u.def.value = htab->sfpr->size;
5506               h->elf.type = STT_FUNC;
5507               h->elf.def_regular = 1;
5508               h->elf.non_elf = 0;
5509               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5510               writing = TRUE;
5511               if (htab->sfpr->contents == NULL)
5512                 {
5513                   htab->sfpr->contents
5514                     = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5515                   if (htab->sfpr->contents == NULL)
5516                     return FALSE;
5517                 }
5518             }
5519         }
5520       if (writing)
5521         {
5522           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5523           if (i != parm->hi)
5524             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5525           else
5526             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5527           htab->sfpr->size = p - htab->sfpr->contents;
5528         }
5529     }
5530
5531   return TRUE;
5532 }
5533
5534 static bfd_byte *
5535 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5536 {
5537   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5538   return p + 4;
5539 }
5540
5541 static bfd_byte *
5542 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5543 {
5544   p = savegpr0 (abfd, p, r);
5545   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5546   p = p + 4;
5547   bfd_put_32 (abfd, BLR, p);
5548   return p + 4;
5549 }
5550
5551 static bfd_byte *
5552 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5553 {
5554   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5555   return p + 4;
5556 }
5557
5558 static bfd_byte *
5559 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5560 {
5561   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5562   p = p + 4;
5563   p = restgpr0 (abfd, p, r);
5564   bfd_put_32 (abfd, MTLR_R0, p);
5565   p = p + 4;
5566   if (r == 29)
5567     {
5568       p = restgpr0 (abfd, p, 30);
5569       p = restgpr0 (abfd, p, 31);
5570     }
5571   bfd_put_32 (abfd, BLR, p);
5572   return p + 4;
5573 }
5574
5575 static bfd_byte *
5576 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5577 {
5578   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5579   return p + 4;
5580 }
5581
5582 static bfd_byte *
5583 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5584 {
5585   p = savegpr1 (abfd, p, r);
5586   bfd_put_32 (abfd, BLR, p);
5587   return p + 4;
5588 }
5589
5590 static bfd_byte *
5591 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5592 {
5593   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5594   return p + 4;
5595 }
5596
5597 static bfd_byte *
5598 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5599 {
5600   p = restgpr1 (abfd, p, r);
5601   bfd_put_32 (abfd, BLR, p);
5602   return p + 4;
5603 }
5604
5605 static bfd_byte *
5606 savefpr (bfd *abfd, bfd_byte *p, int r)
5607 {
5608   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5609   return p + 4;
5610 }
5611
5612 static bfd_byte *
5613 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5614 {
5615   p = savefpr (abfd, p, r);
5616   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
5617   p = p + 4;
5618   bfd_put_32 (abfd, BLR, p);
5619   return p + 4;
5620 }
5621
5622 static bfd_byte *
5623 restfpr (bfd *abfd, bfd_byte *p, int r)
5624 {
5625   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5626   return p + 4;
5627 }
5628
5629 static bfd_byte *
5630 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5631 {
5632   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
5633   p = p + 4;
5634   p = restfpr (abfd, p, r);
5635   bfd_put_32 (abfd, MTLR_R0, p);
5636   p = p + 4;
5637   if (r == 29)
5638     {
5639       p = restfpr (abfd, p, 30);
5640       p = restfpr (abfd, p, 31);
5641     }
5642   bfd_put_32 (abfd, BLR, p);
5643   return p + 4;
5644 }
5645
5646 static bfd_byte *
5647 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5648 {
5649   p = savefpr (abfd, p, r);
5650   bfd_put_32 (abfd, BLR, p);
5651   return p + 4;
5652 }
5653
5654 static bfd_byte *
5655 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5656 {
5657   p = restfpr (abfd, p, r);
5658   bfd_put_32 (abfd, BLR, p);
5659   return p + 4;
5660 }
5661
5662 static bfd_byte *
5663 savevr (bfd *abfd, bfd_byte *p, int r)
5664 {
5665   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5666   p = p + 4;
5667   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5668   return p + 4;
5669 }
5670
5671 static bfd_byte *
5672 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5673 {
5674   p = savevr (abfd, p, r);
5675   bfd_put_32 (abfd, BLR, p);
5676   return p + 4;
5677 }
5678
5679 static bfd_byte *
5680 restvr (bfd *abfd, bfd_byte *p, int r)
5681 {
5682   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5683   p = p + 4;
5684   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5685   return p + 4;
5686 }
5687
5688 static bfd_byte *
5689 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5690 {
5691   p = restvr (abfd, p, r);
5692   bfd_put_32 (abfd, BLR, p);
5693   return p + 4;
5694 }
5695
5696 /* Called via elf_link_hash_traverse to transfer dynamic linking
5697    information on function code symbol entries to their corresponding
5698    function descriptor symbol entries.  */
5699
5700 static bfd_boolean
5701 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5702 {
5703   struct bfd_link_info *info;
5704   struct ppc_link_hash_table *htab;
5705   struct ppc_link_hash_entry *fh;
5706   struct ppc_link_hash_entry *fdh;
5707   bfd_boolean force_local;
5708
5709   fh = (struct ppc_link_hash_entry *) h;
5710   if (fh->elf.root.type == bfd_link_hash_indirect)
5711     return TRUE;
5712
5713   if (!fh->is_func)
5714     return TRUE;
5715
5716   if (fh->elf.root.root.string[0] != '.'
5717       || fh->elf.root.root.string[1] == '\0')
5718     return TRUE;
5719
5720   info = inf;
5721   htab = ppc_hash_table (info);
5722   if (htab == NULL)
5723     return FALSE;
5724
5725   /* Find the corresponding function descriptor symbol.  */
5726   fdh = lookup_fdh (fh, htab);
5727
5728   /* Resolve undefined references to dot-symbols as the value
5729      in the function descriptor, if we have one in a regular object.
5730      This is to satisfy cases like ".quad .foo".  Calls to functions
5731      in dynamic objects are handled elsewhere.  */
5732   if ((fh->elf.root.type == bfd_link_hash_undefined
5733        || fh->elf.root.type == bfd_link_hash_undefweak)
5734       && (fdh->elf.root.type == bfd_link_hash_defined
5735           || fdh->elf.root.type == bfd_link_hash_defweak)
5736       && get_opd_info (fdh->elf.root.u.def.section) != NULL
5737       && opd_entry_value (fdh->elf.root.u.def.section,
5738                           fdh->elf.root.u.def.value,
5739                           &fh->elf.root.u.def.section,
5740                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
5741     {
5742       fh->elf.root.type = fdh->elf.root.type;
5743       fh->elf.forced_local = 1;
5744       fh->elf.def_regular = fdh->elf.def_regular;
5745       fh->elf.def_dynamic = fdh->elf.def_dynamic;
5746     }
5747
5748   if (!fh->elf.dynamic)
5749     {
5750       struct plt_entry *ent;
5751
5752       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5753         if (ent->plt.refcount > 0)
5754           break;
5755       if (ent == NULL)
5756         return TRUE;
5757     }
5758
5759   /* Create a descriptor as undefined if necessary.  */
5760   if (fdh == NULL
5761       && !bfd_link_executable (info)
5762       && (fh->elf.root.type == bfd_link_hash_undefined
5763           || fh->elf.root.type == bfd_link_hash_undefweak))
5764     {
5765       fdh = make_fdh (info, fh);
5766       if (fdh == NULL)
5767         return FALSE;
5768     }
5769
5770   /* We can't support overriding of symbols on a fake descriptor.  */
5771   if (fdh != NULL
5772       && fdh->fake
5773       && (fh->elf.root.type == bfd_link_hash_defined
5774           || fh->elf.root.type == bfd_link_hash_defweak))
5775     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5776
5777   /* Transfer dynamic linking information to the function descriptor.  */
5778   if (fdh != NULL)
5779     {
5780       fdh->elf.ref_regular |= fh->elf.ref_regular;
5781       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5782       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5783       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5784       fdh->elf.dynamic |= fh->elf.dynamic;
5785       fdh->elf.needs_plt |= (fh->elf.needs_plt
5786                              || fh->elf.type == STT_FUNC
5787                              || fh->elf.type == STT_GNU_IFUNC);
5788       move_plt_plist (fh, fdh);
5789
5790       if (!fdh->elf.forced_local
5791           && fh->elf.dynindx != -1)
5792         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5793           return FALSE;
5794     }
5795
5796   /* Now that the info is on the function descriptor, clear the
5797      function code sym info.  Any function code syms for which we
5798      don't have a definition in a regular file, we force local.
5799      This prevents a shared library from exporting syms that have
5800      been imported from another library.  Function code syms that
5801      are really in the library we must leave global to prevent the
5802      linker dragging in a definition from a static library.  */
5803   force_local = (!fh->elf.def_regular
5804                  || fdh == NULL
5805                  || !fdh->elf.def_regular
5806                  || fdh->elf.forced_local);
5807   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5808
5809   return TRUE;
5810 }
5811
5812 static const struct sfpr_def_parms save_res_funcs[] =
5813   {
5814     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5815     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5816     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5817     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5818     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5819     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5820     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5821     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5822     { "._savef", 14, 31, savefpr, savefpr1_tail },
5823     { "._restf", 14, 31, restfpr, restfpr1_tail },
5824     { "_savevr_", 20, 31, savevr, savevr_tail },
5825     { "_restvr_", 20, 31, restvr, restvr_tail }
5826   };
5827
5828 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
5829    this hook to a) provide some gcc support functions, and b) transfer
5830    dynamic linking information gathered so far on function code symbol
5831    entries, to their corresponding function descriptor symbol entries.  */
5832
5833 static bfd_boolean
5834 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5835                             struct bfd_link_info *info)
5836 {
5837   struct ppc_link_hash_table *htab;
5838
5839   htab = ppc_hash_table (info);
5840   if (htab == NULL)
5841     return FALSE;
5842
5843   /* Provide any missing _save* and _rest* functions.  */
5844   if (htab->sfpr != NULL)
5845     {
5846       unsigned int i;
5847
5848       htab->sfpr->size = 0;
5849       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
5850         if (!sfpr_define (info, &save_res_funcs[i], NULL))
5851           return FALSE;
5852       if (htab->sfpr->size == 0)
5853         htab->sfpr->flags |= SEC_EXCLUDE;
5854     }
5855
5856   if (bfd_link_relocatable (info))
5857     return TRUE;
5858
5859   if (htab->elf.hgot != NULL)
5860     {
5861       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
5862       /* Make .TOC. defined so as to prevent it being made dynamic.
5863          The wrong value here is fixed later in ppc64_elf_set_toc.  */
5864       if (!htab->elf.hgot->def_regular
5865           || htab->elf.hgot->root.type != bfd_link_hash_defined)
5866         {
5867           htab->elf.hgot->root.type = bfd_link_hash_defined;
5868           htab->elf.hgot->root.u.def.value = 0;
5869           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
5870           htab->elf.hgot->def_regular = 1;
5871           htab->elf.hgot->root.linker_def = 1;
5872         }
5873       htab->elf.hgot->type = STT_OBJECT;
5874       htab->elf.hgot->other
5875         = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5876     }
5877
5878   if (htab->need_func_desc_adj)
5879     {
5880       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5881       htab->need_func_desc_adj = 0;
5882     }
5883
5884   return TRUE;
5885 }
5886
5887 /* Find dynamic relocs for H that apply to read-only sections.  */
5888
5889 static asection *
5890 readonly_dynrelocs (struct elf_link_hash_entry *h)
5891 {
5892   struct ppc_link_hash_entry *eh;
5893   struct elf_dyn_relocs *p;
5894
5895   eh = (struct ppc_link_hash_entry *) h;
5896   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5897     {
5898       asection *s = p->sec->output_section;
5899
5900       if (s != NULL && (s->flags & SEC_READONLY) != 0)
5901         return p->sec;
5902     }
5903   return NULL;
5904 }
5905
5906 /* Return true if we have dynamic relocs against H or any of its weak
5907    aliases, that apply to read-only sections.  Cannot be used after
5908    size_dynamic_sections.  */
5909
5910 static bfd_boolean
5911 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
5912 {
5913   struct ppc_link_hash_entry *eh;
5914
5915   eh = (struct ppc_link_hash_entry *) h;
5916   do
5917     {
5918       if (readonly_dynrelocs (&eh->elf))
5919         return TRUE;
5920       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
5921     }
5922   while (eh != NULL && &eh->elf != h);
5923
5924   return FALSE;
5925 }
5926
5927 /* Return whether EH has pc-relative dynamic relocs.  */
5928
5929 static bfd_boolean
5930 pc_dynrelocs (struct ppc_link_hash_entry *eh)
5931 {
5932   struct elf_dyn_relocs *p;
5933
5934   for (p = eh->dyn_relocs; p != NULL; p = p->next)
5935     if (p->pc_count != 0)
5936       return TRUE;
5937   return FALSE;
5938 }
5939
5940 /* Return true if a global entry stub will be created for H.  Valid
5941    for ELFv2 before plt entries have been allocated.  */
5942
5943 static bfd_boolean
5944 global_entry_stub (struct elf_link_hash_entry *h)
5945 {
5946   struct plt_entry *pent;
5947
5948   if (!h->pointer_equality_needed
5949       || h->def_regular)
5950     return FALSE;
5951
5952   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5953     if (pent->plt.refcount > 0
5954         && pent->addend == 0)
5955       return TRUE;
5956
5957   return FALSE;
5958 }
5959
5960 /* Adjust a symbol defined by a dynamic object and referenced by a
5961    regular object.  The current definition is in some section of the
5962    dynamic object, but we're not including those sections.  We have to
5963    change the definition to something the rest of the link can
5964    understand.  */
5965
5966 static bfd_boolean
5967 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5968                                  struct elf_link_hash_entry *h)
5969 {
5970   struct ppc_link_hash_table *htab;
5971   asection *s, *srel;
5972
5973   htab = ppc_hash_table (info);
5974   if (htab == NULL)
5975     return FALSE;
5976
5977   /* Deal with function syms.  */
5978   if (h->type == STT_FUNC
5979       || h->type == STT_GNU_IFUNC
5980       || h->needs_plt)
5981     {
5982       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
5983                            || SYMBOL_CALLS_LOCAL (info, h)
5984                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
5985       /* Discard dyn_relocs when non-pic if we've decided that a
5986          function symbol is local and not an ifunc.  We keep dynamic
5987          relocs for ifuncs when local rather than always emitting a
5988          plt call stub for them and defining the symbol on the call
5989          stub.  We can't do that for ELFv1 anyway (a function symbol
5990          is defined on a descriptor, not code) and it can be faster at
5991          run-time due to not needing to bounce through a stub.  The
5992          dyn_relocs for ifuncs will be applied even in a static
5993          executable.  */
5994       if (!bfd_link_pic (info)
5995           && h->type != STT_GNU_IFUNC
5996           && local)
5997         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
5998
5999       /* Clear procedure linkage table information for any symbol that
6000          won't need a .plt entry.  */
6001       struct plt_entry *ent;
6002       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6003         if (ent->plt.refcount > 0)
6004           break;
6005       if (ent == NULL
6006           || (h->type != STT_GNU_IFUNC
6007               && local
6008               && (htab->can_convert_all_inline_plt
6009                   || (((struct ppc_link_hash_entry *) h)->tls_mask
6010                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6011         {
6012           h->plt.plist = NULL;
6013           h->needs_plt = 0;
6014           h->pointer_equality_needed = 0;
6015         }
6016       else if (abiversion (info->output_bfd) >= 2)
6017         {
6018           /* Taking a function's address in a read/write section
6019              doesn't require us to define the function symbol in the
6020              executable on a global entry stub.  A dynamic reloc can
6021              be used instead.  The reason we prefer a few more dynamic
6022              relocs is that calling via a global entry stub costs a
6023              few more instructions, and pointer_equality_needed causes
6024              extra work in ld.so when resolving these symbols.  */
6025           if (global_entry_stub (h))
6026             {
6027               if (!readonly_dynrelocs (h))
6028                 {
6029                   h->pointer_equality_needed = 0;
6030                   /* If we haven't seen a branch reloc and the symbol
6031                      isn't an ifunc then we don't need a plt entry.  */
6032                   if (!h->needs_plt)
6033                     h->plt.plist = NULL;
6034                 }
6035               else if (!bfd_link_pic (info))
6036                 /* We are going to be defining the function symbol on the
6037                    plt stub, so no dyn_relocs needed when non-pic.  */
6038                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6039             }
6040
6041           /* ELFv2 function symbols can't have copy relocs.  */
6042           return TRUE;
6043         }
6044       else if (!h->needs_plt
6045                && !readonly_dynrelocs (h))
6046         {
6047           /* If we haven't seen a branch reloc and the symbol isn't an
6048              ifunc then we don't need a plt entry.  */
6049           h->plt.plist = NULL;
6050           h->pointer_equality_needed = 0;
6051           return TRUE;
6052         }
6053     }
6054   else
6055     h->plt.plist = NULL;
6056
6057   /* If this is a weak symbol, and there is a real definition, the
6058      processor independent code will have arranged for us to see the
6059      real definition first, and we can just use the same value.  */
6060   if (h->is_weakalias)
6061     {
6062       struct elf_link_hash_entry *def = weakdef (h);
6063       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6064       h->root.u.def.section = def->root.u.def.section;
6065       h->root.u.def.value = def->root.u.def.value;
6066       if (def->root.u.def.section == htab->elf.sdynbss
6067           || def->root.u.def.section == htab->elf.sdynrelro)
6068         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6069       return TRUE;
6070     }
6071
6072   /* If we are creating a shared library, we must presume that the
6073      only references to the symbol are via the global offset table.
6074      For such cases we need not do anything here; the relocations will
6075      be handled correctly by relocate_section.  */
6076   if (bfd_link_pic (info))
6077     return TRUE;
6078
6079   /* If there are no references to this symbol that do not use the
6080      GOT, we don't need to generate a copy reloc.  */
6081   if (!h->non_got_ref)
6082     return TRUE;
6083
6084   /* Don't generate a copy reloc for symbols defined in the executable.  */
6085   if (!h->def_dynamic || !h->ref_regular || h->def_regular
6086
6087       /* If -z nocopyreloc was given, don't generate them either.  */
6088       || info->nocopyreloc
6089
6090       /* If we don't find any dynamic relocs in read-only sections, then
6091          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
6092       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
6093
6094       /* Protected variables do not work with .dynbss.  The copy in
6095          .dynbss won't be used by the shared library with the protected
6096          definition for the variable.  Text relocations are preferable
6097          to an incorrect program.  */
6098       || h->protected_def)
6099     return TRUE;
6100
6101   if (h->plt.plist != NULL)
6102     {
6103       /* We should never get here, but unfortunately there are versions
6104          of gcc out there that improperly (for this ABI) put initialized
6105          function pointers, vtable refs and suchlike in read-only
6106          sections.  Allow them to proceed, but warn that this might
6107          break at runtime.  */
6108       info->callbacks->einfo
6109         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6110            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6111          h->root.root.string);
6112     }
6113
6114   /* This is a reference to a symbol defined by a dynamic object which
6115      is not a function.  */
6116
6117   /* We must allocate the symbol in our .dynbss section, which will
6118      become part of the .bss section of the executable.  There will be
6119      an entry for this symbol in the .dynsym section.  The dynamic
6120      object will contain position independent code, so all references
6121      from the dynamic object to this symbol will go through the global
6122      offset table.  The dynamic linker will use the .dynsym entry to
6123      determine the address it must put in the global offset table, so
6124      both the dynamic object and the regular object will refer to the
6125      same memory location for the variable.  */
6126   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6127     {
6128       s = htab->elf.sdynrelro;
6129       srel = htab->elf.sreldynrelro;
6130     }
6131   else
6132     {
6133       s = htab->elf.sdynbss;
6134       srel = htab->elf.srelbss;
6135     }
6136   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6137     {
6138       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6139          linker to copy the initial value out of the dynamic object
6140          and into the runtime process image.  */
6141       srel->size += sizeof (Elf64_External_Rela);
6142       h->needs_copy = 1;
6143     }
6144
6145   /* We no longer want dyn_relocs.  */
6146   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
6147   return _bfd_elf_adjust_dynamic_copy (info, h, s);
6148 }
6149
6150 /* If given a function descriptor symbol, hide both the function code
6151    sym and the descriptor.  */
6152 static void
6153 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6154                        struct elf_link_hash_entry *h,
6155                        bfd_boolean force_local)
6156 {
6157   struct ppc_link_hash_entry *eh;
6158   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6159
6160   eh = (struct ppc_link_hash_entry *) h;
6161   if (eh->is_func_descriptor)
6162     {
6163       struct ppc_link_hash_entry *fh = eh->oh;
6164
6165       if (fh == NULL)
6166         {
6167           const char *p, *q;
6168           struct elf_link_hash_table *htab = elf_hash_table (info);
6169           char save;
6170
6171           /* We aren't supposed to use alloca in BFD because on
6172              systems which do not have alloca the version in libiberty
6173              calls xmalloc, which might cause the program to crash
6174              when it runs out of memory.  This function doesn't have a
6175              return status, so there's no way to gracefully return an
6176              error.  So cheat.  We know that string[-1] can be safely
6177              accessed;  It's either a string in an ELF string table,
6178              or allocated in an objalloc structure.  */
6179
6180           p = eh->elf.root.root.string - 1;
6181           save = *p;
6182           *(char *) p = '.';
6183           fh = (struct ppc_link_hash_entry *)
6184             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6185           *(char *) p = save;
6186
6187           /* Unfortunately, if it so happens that the string we were
6188              looking for was allocated immediately before this string,
6189              then we overwrote the string terminator.  That's the only
6190              reason the lookup should fail.  */
6191           if (fh == NULL)
6192             {
6193               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6194               while (q >= eh->elf.root.root.string && *q == *p)
6195                 --q, --p;
6196               if (q < eh->elf.root.root.string && *p == '.')
6197                 fh = (struct ppc_link_hash_entry *)
6198                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
6199             }
6200           if (fh != NULL)
6201             {
6202               eh->oh = fh;
6203               fh->oh = eh;
6204             }
6205         }
6206       if (fh != NULL)
6207         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6208     }
6209 }
6210
6211 static bfd_boolean
6212 get_sym_h (struct elf_link_hash_entry **hp,
6213            Elf_Internal_Sym **symp,
6214            asection **symsecp,
6215            unsigned char **tls_maskp,
6216            Elf_Internal_Sym **locsymsp,
6217            unsigned long r_symndx,
6218            bfd *ibfd)
6219 {
6220   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6221
6222   if (r_symndx >= symtab_hdr->sh_info)
6223     {
6224       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6225       struct elf_link_hash_entry *h;
6226
6227       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6228       h = elf_follow_link (h);
6229
6230       if (hp != NULL)
6231         *hp = h;
6232
6233       if (symp != NULL)
6234         *symp = NULL;
6235
6236       if (symsecp != NULL)
6237         {
6238           asection *symsec = NULL;
6239           if (h->root.type == bfd_link_hash_defined
6240               || h->root.type == bfd_link_hash_defweak)
6241             symsec = h->root.u.def.section;
6242           *symsecp = symsec;
6243         }
6244
6245       if (tls_maskp != NULL)
6246         {
6247           struct ppc_link_hash_entry *eh;
6248
6249           eh = (struct ppc_link_hash_entry *) h;
6250           *tls_maskp = &eh->tls_mask;
6251         }
6252     }
6253   else
6254     {
6255       Elf_Internal_Sym *sym;
6256       Elf_Internal_Sym *locsyms = *locsymsp;
6257
6258       if (locsyms == NULL)
6259         {
6260           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6261           if (locsyms == NULL)
6262             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6263                                             symtab_hdr->sh_info,
6264                                             0, NULL, NULL, NULL);
6265           if (locsyms == NULL)
6266             return FALSE;
6267           *locsymsp = locsyms;
6268         }
6269       sym = locsyms + r_symndx;
6270
6271       if (hp != NULL)
6272         *hp = NULL;
6273
6274       if (symp != NULL)
6275         *symp = sym;
6276
6277       if (symsecp != NULL)
6278         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6279
6280       if (tls_maskp != NULL)
6281         {
6282           struct got_entry **lgot_ents;
6283           unsigned char *tls_mask;
6284
6285           tls_mask = NULL;
6286           lgot_ents = elf_local_got_ents (ibfd);
6287           if (lgot_ents != NULL)
6288             {
6289               struct plt_entry **local_plt = (struct plt_entry **)
6290                 (lgot_ents + symtab_hdr->sh_info);
6291               unsigned char *lgot_masks = (unsigned char *)
6292                 (local_plt + symtab_hdr->sh_info);
6293               tls_mask = &lgot_masks[r_symndx];
6294             }
6295           *tls_maskp = tls_mask;
6296         }
6297     }
6298   return TRUE;
6299 }
6300
6301 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
6302    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6303    type suitable for optimization, and 1 otherwise.  */
6304
6305 static int
6306 get_tls_mask (unsigned char **tls_maskp,
6307               unsigned long *toc_symndx,
6308               bfd_vma *toc_addend,
6309               Elf_Internal_Sym **locsymsp,
6310               const Elf_Internal_Rela *rel,
6311               bfd *ibfd)
6312 {
6313   unsigned long r_symndx;
6314   int next_r;
6315   struct elf_link_hash_entry *h;
6316   Elf_Internal_Sym *sym;
6317   asection *sec;
6318   bfd_vma off;
6319
6320   r_symndx = ELF64_R_SYM (rel->r_info);
6321   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6322     return 0;
6323
6324   if ((*tls_maskp != NULL
6325        && (**tls_maskp & TLS_TLS) != 0
6326        && **tls_maskp != (TLS_TLS | TLS_MARK))
6327       || sec == NULL
6328       || ppc64_elf_section_data (sec) == NULL
6329       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6330     return 1;
6331
6332   /* Look inside a TOC section too.  */
6333   if (h != NULL)
6334     {
6335       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6336       off = h->root.u.def.value;
6337     }
6338   else
6339     off = sym->st_value;
6340   off += rel->r_addend;
6341   BFD_ASSERT (off % 8 == 0);
6342   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6343   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6344   if (toc_symndx != NULL)
6345     *toc_symndx = r_symndx;
6346   if (toc_addend != NULL)
6347     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6348   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6349     return 0;
6350   if ((h == NULL || is_static_defined (h))
6351       && (next_r == -1 || next_r == -2))
6352     return 1 - next_r;
6353   return 1;
6354 }
6355
6356 /* Find (or create) an entry in the tocsave hash table.  */
6357
6358 static struct tocsave_entry *
6359 tocsave_find (struct ppc_link_hash_table *htab,
6360               enum insert_option insert,
6361               Elf_Internal_Sym **local_syms,
6362               const Elf_Internal_Rela *irela,
6363               bfd *ibfd)
6364 {
6365   unsigned long r_indx;
6366   struct elf_link_hash_entry *h;
6367   Elf_Internal_Sym *sym;
6368   struct tocsave_entry ent, *p;
6369   hashval_t hash;
6370   struct tocsave_entry **slot;
6371
6372   r_indx = ELF64_R_SYM (irela->r_info);
6373   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6374     return NULL;
6375   if (ent.sec == NULL || ent.sec->output_section == NULL)
6376     {
6377       _bfd_error_handler
6378         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6379       return NULL;
6380     }
6381
6382   if (h != NULL)
6383     ent.offset = h->root.u.def.value;
6384   else
6385     ent.offset = sym->st_value;
6386   ent.offset += irela->r_addend;
6387
6388   hash = tocsave_htab_hash (&ent);
6389   slot = ((struct tocsave_entry **)
6390           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6391   if (slot == NULL)
6392     return NULL;
6393
6394   if (*slot == NULL)
6395     {
6396       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6397       if (p == NULL)
6398         return NULL;
6399       *p = ent;
6400       *slot = p;
6401     }
6402   return *slot;
6403 }
6404
6405 /* Adjust all global syms defined in opd sections.  In gcc generated
6406    code for the old ABI, these will already have been done.  */
6407
6408 static bfd_boolean
6409 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6410 {
6411   struct ppc_link_hash_entry *eh;
6412   asection *sym_sec;
6413   struct _opd_sec_data *opd;
6414
6415   if (h->root.type == bfd_link_hash_indirect)
6416     return TRUE;
6417
6418   if (h->root.type != bfd_link_hash_defined
6419       && h->root.type != bfd_link_hash_defweak)
6420     return TRUE;
6421
6422   eh = (struct ppc_link_hash_entry *) h;
6423   if (eh->adjust_done)
6424     return TRUE;
6425
6426   sym_sec = eh->elf.root.u.def.section;
6427   opd = get_opd_info (sym_sec);
6428   if (opd != NULL && opd->adjust != NULL)
6429     {
6430       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6431       if (adjust == -1)
6432         {
6433           /* This entry has been deleted.  */
6434           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6435           if (dsec == NULL)
6436             {
6437               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6438                 if (discarded_section (dsec))
6439                   {
6440                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6441                     break;
6442                   }
6443             }
6444           eh->elf.root.u.def.value = 0;
6445           eh->elf.root.u.def.section = dsec;
6446         }
6447       else
6448         eh->elf.root.u.def.value += adjust;
6449       eh->adjust_done = 1;
6450     }
6451   return TRUE;
6452 }
6453
6454 /* Handles decrementing dynamic reloc counts for the reloc specified by
6455    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
6456    have already been determined.  */
6457
6458 static bfd_boolean
6459 dec_dynrel_count (bfd_vma r_info,
6460                   asection *sec,
6461                   struct bfd_link_info *info,
6462                   Elf_Internal_Sym **local_syms,
6463                   struct elf_link_hash_entry *h,
6464                   Elf_Internal_Sym *sym)
6465 {
6466   enum elf_ppc64_reloc_type r_type;
6467   asection *sym_sec = NULL;
6468
6469   /* Can this reloc be dynamic?  This switch, and later tests here
6470      should be kept in sync with the code in check_relocs.  */
6471   r_type = ELF64_R_TYPE (r_info);
6472   switch (r_type)
6473     {
6474     default:
6475       return TRUE;
6476
6477     case R_PPC64_TPREL16:
6478     case R_PPC64_TPREL16_LO:
6479     case R_PPC64_TPREL16_HI:
6480     case R_PPC64_TPREL16_HA:
6481     case R_PPC64_TPREL16_DS:
6482     case R_PPC64_TPREL16_LO_DS:
6483     case R_PPC64_TPREL16_HIGH:
6484     case R_PPC64_TPREL16_HIGHA:
6485     case R_PPC64_TPREL16_HIGHER:
6486     case R_PPC64_TPREL16_HIGHERA:
6487     case R_PPC64_TPREL16_HIGHEST:
6488     case R_PPC64_TPREL16_HIGHESTA:
6489     case R_PPC64_TPREL64:
6490     case R_PPC64_DTPMOD64:
6491     case R_PPC64_DTPREL64:
6492     case R_PPC64_ADDR64:
6493     case R_PPC64_REL30:
6494     case R_PPC64_REL32:
6495     case R_PPC64_REL64:
6496     case R_PPC64_ADDR14:
6497     case R_PPC64_ADDR14_BRNTAKEN:
6498     case R_PPC64_ADDR14_BRTAKEN:
6499     case R_PPC64_ADDR16:
6500     case R_PPC64_ADDR16_DS:
6501     case R_PPC64_ADDR16_HA:
6502     case R_PPC64_ADDR16_HI:
6503     case R_PPC64_ADDR16_HIGH:
6504     case R_PPC64_ADDR16_HIGHA:
6505     case R_PPC64_ADDR16_HIGHER:
6506     case R_PPC64_ADDR16_HIGHERA:
6507     case R_PPC64_ADDR16_HIGHEST:
6508     case R_PPC64_ADDR16_HIGHESTA:
6509     case R_PPC64_ADDR16_LO:
6510     case R_PPC64_ADDR16_LO_DS:
6511     case R_PPC64_ADDR24:
6512     case R_PPC64_ADDR32:
6513     case R_PPC64_UADDR16:
6514     case R_PPC64_UADDR32:
6515     case R_PPC64_UADDR64:
6516     case R_PPC64_TOC:
6517       break;
6518     }
6519
6520   if (local_syms != NULL)
6521     {
6522       unsigned long r_symndx;
6523       bfd *ibfd = sec->owner;
6524
6525       r_symndx = ELF64_R_SYM (r_info);
6526       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6527         return FALSE;
6528     }
6529
6530   if ((bfd_link_pic (info)
6531        && (must_be_dyn_reloc (info, r_type)
6532            || (h != NULL
6533                && (!SYMBOLIC_BIND (info, h)
6534                    || h->root.type == bfd_link_hash_defweak
6535                    || !h->def_regular))))
6536       || (ELIMINATE_COPY_RELOCS
6537           && !bfd_link_pic (info)
6538           && h != NULL
6539           && (h->root.type == bfd_link_hash_defweak
6540               || !h->def_regular)))
6541     ;
6542   else
6543     return TRUE;
6544
6545   if (h != NULL)
6546     {
6547       struct elf_dyn_relocs *p;
6548       struct elf_dyn_relocs **pp;
6549       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6550
6551       /* elf_gc_sweep may have already removed all dyn relocs associated
6552          with local syms for a given section.  Also, symbol flags are
6553          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
6554          report a dynreloc miscount.  */
6555       if (*pp == NULL && info->gc_sections)
6556         return TRUE;
6557
6558       while ((p = *pp) != NULL)
6559         {
6560           if (p->sec == sec)
6561             {
6562               if (!must_be_dyn_reloc (info, r_type))
6563                 p->pc_count -= 1;
6564               p->count -= 1;
6565               if (p->count == 0)
6566                 *pp = p->next;
6567               return TRUE;
6568             }
6569           pp = &p->next;
6570         }
6571     }
6572   else
6573     {
6574       struct ppc_dyn_relocs *p;
6575       struct ppc_dyn_relocs **pp;
6576       void *vpp;
6577       bfd_boolean is_ifunc;
6578
6579       if (local_syms == NULL)
6580         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6581       if (sym_sec == NULL)
6582         sym_sec = sec;
6583
6584       vpp = &elf_section_data (sym_sec)->local_dynrel;
6585       pp = (struct ppc_dyn_relocs **) vpp;
6586
6587       if (*pp == NULL && info->gc_sections)
6588         return TRUE;
6589
6590       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
6591       while ((p = *pp) != NULL)
6592         {
6593           if (p->sec == sec && p->ifunc == is_ifunc)
6594             {
6595               p->count -= 1;
6596               if (p->count == 0)
6597                 *pp = p->next;
6598               return TRUE;
6599             }
6600           pp = &p->next;
6601         }
6602     }
6603
6604   /* xgettext:c-format */
6605   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
6606                       sec->owner, sec);
6607   bfd_set_error (bfd_error_bad_value);
6608   return FALSE;
6609 }
6610
6611 /* Remove unused Official Procedure Descriptor entries.  Currently we
6612    only remove those associated with functions in discarded link-once
6613    sections, or weakly defined functions that have been overridden.  It
6614    would be possible to remove many more entries for statically linked
6615    applications.  */
6616
6617 bfd_boolean
6618 ppc64_elf_edit_opd (struct bfd_link_info *info)
6619 {
6620   bfd *ibfd;
6621   bfd_boolean some_edited = FALSE;
6622   asection *need_pad = NULL;
6623   struct ppc_link_hash_table *htab;
6624
6625   htab = ppc_hash_table (info);
6626   if (htab == NULL)
6627     return FALSE;
6628
6629   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6630     {
6631       asection *sec;
6632       Elf_Internal_Rela *relstart, *rel, *relend;
6633       Elf_Internal_Shdr *symtab_hdr;
6634       Elf_Internal_Sym *local_syms;
6635       struct _opd_sec_data *opd;
6636       bfd_boolean need_edit, add_aux_fields, broken;
6637       bfd_size_type cnt_16b = 0;
6638
6639       if (!is_ppc64_elf (ibfd))
6640         continue;
6641
6642       sec = bfd_get_section_by_name (ibfd, ".opd");
6643       if (sec == NULL || sec->size == 0)
6644         continue;
6645
6646       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6647         continue;
6648
6649       if (sec->output_section == bfd_abs_section_ptr)
6650         continue;
6651
6652       /* Look through the section relocs.  */
6653       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6654         continue;
6655
6656       local_syms = NULL;
6657       symtab_hdr = &elf_symtab_hdr (ibfd);
6658
6659       /* Read the relocations.  */
6660       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6661                                             info->keep_memory);
6662       if (relstart == NULL)
6663         return FALSE;
6664
6665       /* First run through the relocs to check they are sane, and to
6666          determine whether we need to edit this opd section.  */
6667       need_edit = FALSE;
6668       broken = FALSE;
6669       need_pad = sec;
6670       relend = relstart + sec->reloc_count;
6671       for (rel = relstart; rel < relend; )
6672         {
6673           enum elf_ppc64_reloc_type r_type;
6674           unsigned long r_symndx;
6675           asection *sym_sec;
6676           struct elf_link_hash_entry *h;
6677           Elf_Internal_Sym *sym;
6678           bfd_vma offset;
6679
6680           /* .opd contains an array of 16 or 24 byte entries.  We're
6681              only interested in the reloc pointing to a function entry
6682              point.  */
6683           offset = rel->r_offset;
6684           if (rel + 1 == relend
6685               || rel[1].r_offset != offset + 8)
6686             {
6687               /* If someone messes with .opd alignment then after a
6688                  "ld -r" we might have padding in the middle of .opd.
6689                  Also, there's nothing to prevent someone putting
6690                  something silly in .opd with the assembler.  No .opd
6691                  optimization for them!  */
6692             broken_opd:
6693               _bfd_error_handler
6694                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
6695               broken = TRUE;
6696               break;
6697             }
6698
6699           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6700               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6701             {
6702               _bfd_error_handler
6703                 /* xgettext:c-format */
6704                 (_("%pB: unexpected reloc type %u in .opd section"),
6705                  ibfd, r_type);
6706               broken = TRUE;
6707               break;
6708             }
6709
6710           r_symndx = ELF64_R_SYM (rel->r_info);
6711           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6712                           r_symndx, ibfd))
6713             goto error_ret;
6714
6715           if (sym_sec == NULL || sym_sec->owner == NULL)
6716             {
6717               const char *sym_name;
6718               if (h != NULL)
6719                 sym_name = h->root.root.string;
6720               else
6721                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6722                                              sym_sec);
6723
6724               _bfd_error_handler
6725                 /* xgettext:c-format */
6726                 (_("%pB: undefined sym `%s' in .opd section"),
6727                  ibfd, sym_name);
6728               broken = TRUE;
6729               break;
6730             }
6731
6732           /* opd entries are always for functions defined in the
6733              current input bfd.  If the symbol isn't defined in the
6734              input bfd, then we won't be using the function in this
6735              bfd;  It must be defined in a linkonce section in another
6736              bfd, or is weak.  It's also possible that we are
6737              discarding the function due to a linker script /DISCARD/,
6738              which we test for via the output_section.  */
6739           if (sym_sec->owner != ibfd
6740               || sym_sec->output_section == bfd_abs_section_ptr)
6741             need_edit = TRUE;
6742
6743           rel += 2;
6744           if (rel + 1 == relend
6745               || (rel + 2 < relend
6746                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
6747             ++rel;
6748
6749           if (rel == relend)
6750             {
6751               if (sec->size == offset + 24)
6752                 {
6753                   need_pad = NULL;
6754                   break;
6755                 }
6756               if (sec->size == offset + 16)
6757                 {
6758                   cnt_16b++;
6759                   break;
6760                 }
6761               goto broken_opd;
6762             }
6763           else if (rel + 1 < relend
6764                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6765                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6766             {
6767               if (rel[0].r_offset == offset + 16)
6768                 cnt_16b++;
6769               else if (rel[0].r_offset != offset + 24)
6770                 goto broken_opd;
6771             }
6772           else
6773             goto broken_opd;
6774         }
6775
6776       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
6777
6778       if (!broken && (need_edit || add_aux_fields))
6779         {
6780           Elf_Internal_Rela *write_rel;
6781           Elf_Internal_Shdr *rel_hdr;
6782           bfd_byte *rptr, *wptr;
6783           bfd_byte *new_contents;
6784           bfd_size_type amt;
6785
6786           new_contents = NULL;
6787           amt = OPD_NDX (sec->size) * sizeof (long);
6788           opd = &ppc64_elf_section_data (sec)->u.opd;
6789           opd->adjust = bfd_zalloc (sec->owner, amt);
6790           if (opd->adjust == NULL)
6791             return FALSE;
6792
6793           /* This seems a waste of time as input .opd sections are all
6794              zeros as generated by gcc, but I suppose there's no reason
6795              this will always be so.  We might start putting something in
6796              the third word of .opd entries.  */
6797           if ((sec->flags & SEC_IN_MEMORY) == 0)
6798             {
6799               bfd_byte *loc;
6800               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6801                 {
6802                   if (loc != NULL)
6803                     free (loc);
6804                 error_ret:
6805                   if (local_syms != NULL
6806                       && symtab_hdr->contents != (unsigned char *) local_syms)
6807                     free (local_syms);
6808                   if (elf_section_data (sec)->relocs != relstart)
6809                     free (relstart);
6810                   return FALSE;
6811                 }
6812               sec->contents = loc;
6813               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6814             }
6815
6816           elf_section_data (sec)->relocs = relstart;
6817
6818           new_contents = sec->contents;
6819           if (add_aux_fields)
6820             {
6821               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6822               if (new_contents == NULL)
6823                 return FALSE;
6824               need_pad = NULL;
6825             }
6826           wptr = new_contents;
6827           rptr = sec->contents;
6828           write_rel = relstart;
6829           for (rel = relstart; rel < relend; )
6830             {
6831               unsigned long r_symndx;
6832               asection *sym_sec;
6833               struct elf_link_hash_entry *h;
6834               struct ppc_link_hash_entry *fdh = NULL;
6835               Elf_Internal_Sym *sym;
6836               long opd_ent_size;
6837               Elf_Internal_Rela *next_rel;
6838               bfd_boolean skip;
6839
6840               r_symndx = ELF64_R_SYM (rel->r_info);
6841               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6842                               r_symndx, ibfd))
6843                 goto error_ret;
6844
6845               next_rel = rel + 2;
6846               if (next_rel + 1 == relend
6847                   || (next_rel + 2 < relend
6848                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
6849                 ++next_rel;
6850
6851               /* See if the .opd entry is full 24 byte or
6852                  16 byte (with fd_aux entry overlapped with next
6853                  fd_func).  */
6854               opd_ent_size = 24;
6855               if (next_rel == relend)
6856                 {
6857                   if (sec->size == rel->r_offset + 16)
6858                     opd_ent_size = 16;
6859                 }
6860               else if (next_rel->r_offset == rel->r_offset + 16)
6861                 opd_ent_size = 16;
6862
6863               if (h != NULL
6864                   && h->root.root.string[0] == '.')
6865                 {
6866                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
6867                   if (fdh != NULL)
6868                     {
6869                       fdh = ppc_follow_link (fdh);
6870                       if (fdh->elf.root.type != bfd_link_hash_defined
6871                           && fdh->elf.root.type != bfd_link_hash_defweak)
6872                         fdh = NULL;
6873                     }
6874                 }
6875
6876               skip = (sym_sec->owner != ibfd
6877                       || sym_sec->output_section == bfd_abs_section_ptr);
6878               if (skip)
6879                 {
6880                   if (fdh != NULL && sym_sec->owner == ibfd)
6881                     {
6882                       /* Arrange for the function descriptor sym
6883                          to be dropped.  */
6884                       fdh->elf.root.u.def.value = 0;
6885                       fdh->elf.root.u.def.section = sym_sec;
6886                     }
6887                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
6888
6889                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
6890                     rel = next_rel;
6891                   else
6892                     while (1)
6893                       {
6894                         if (!dec_dynrel_count (rel->r_info, sec, info,
6895                                                NULL, h, sym))
6896                           goto error_ret;
6897
6898                         if (++rel == next_rel)
6899                           break;
6900
6901                         r_symndx = ELF64_R_SYM (rel->r_info);
6902                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6903                                         r_symndx, ibfd))
6904                           goto error_ret;
6905                       }
6906                 }
6907               else
6908                 {
6909                   /* We'll be keeping this opd entry.  */
6910                   long adjust;
6911
6912                   if (fdh != NULL)
6913                     {
6914                       /* Redefine the function descriptor symbol to
6915                          this location in the opd section.  It is
6916                          necessary to update the value here rather
6917                          than using an array of adjustments as we do
6918                          for local symbols, because various places
6919                          in the generic ELF code use the value
6920                          stored in u.def.value.  */
6921                       fdh->elf.root.u.def.value = wptr - new_contents;
6922                       fdh->adjust_done = 1;
6923                     }
6924
6925                   /* Local syms are a bit tricky.  We could
6926                      tweak them as they can be cached, but
6927                      we'd need to look through the local syms
6928                      for the function descriptor sym which we
6929                      don't have at the moment.  So keep an
6930                      array of adjustments.  */
6931                   adjust = (wptr - new_contents) - (rptr - sec->contents);
6932                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
6933
6934                   if (wptr != rptr)
6935                     memcpy (wptr, rptr, opd_ent_size);
6936                   wptr += opd_ent_size;
6937                   if (add_aux_fields && opd_ent_size == 16)
6938                     {
6939                       memset (wptr, '\0', 8);
6940                       wptr += 8;
6941                     }
6942
6943                   /* We need to adjust any reloc offsets to point to the
6944                      new opd entries.  */
6945                   for ( ; rel != next_rel; ++rel)
6946                     {
6947                       rel->r_offset += adjust;
6948                       if (write_rel != rel)
6949                         memcpy (write_rel, rel, sizeof (*rel));
6950                       ++write_rel;
6951                     }
6952                 }
6953
6954               rptr += opd_ent_size;
6955             }
6956
6957           sec->size = wptr - new_contents;
6958           sec->reloc_count = write_rel - relstart;
6959           if (add_aux_fields)
6960             {
6961               free (sec->contents);
6962               sec->contents = new_contents;
6963             }
6964
6965           /* Fudge the header size too, as this is used later in
6966              elf_bfd_final_link if we are emitting relocs.  */
6967           rel_hdr = _bfd_elf_single_rel_hdr (sec);
6968           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
6969           some_edited = TRUE;
6970         }
6971       else if (elf_section_data (sec)->relocs != relstart)
6972         free (relstart);
6973
6974       if (local_syms != NULL
6975           && symtab_hdr->contents != (unsigned char *) local_syms)
6976         {
6977           if (!info->keep_memory)
6978             free (local_syms);
6979           else
6980             symtab_hdr->contents = (unsigned char *) local_syms;
6981         }
6982     }
6983
6984   if (some_edited)
6985     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6986
6987   /* If we are doing a final link and the last .opd entry is just 16 byte
6988      long, add a 8 byte padding after it.  */
6989   if (need_pad != NULL && !bfd_link_relocatable (info))
6990     {
6991       bfd_byte *p;
6992
6993       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6994         {
6995           BFD_ASSERT (need_pad->size > 0);
6996
6997           p = bfd_malloc (need_pad->size + 8);
6998           if (p == NULL)
6999             return FALSE;
7000
7001           if (!bfd_get_section_contents (need_pad->owner, need_pad,
7002                                          p, 0, need_pad->size))
7003             return FALSE;
7004
7005           need_pad->contents = p;
7006           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7007         }
7008       else
7009         {
7010           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7011           if (p == NULL)
7012             return FALSE;
7013
7014           need_pad->contents = p;
7015         }
7016
7017       memset (need_pad->contents + need_pad->size, 0, 8);
7018       need_pad->size += 8;
7019     }
7020
7021   return TRUE;
7022 }
7023
7024 /* Analyze inline PLT call relocations to see whether calls to locally
7025    defined functions can be converted to direct calls.  */
7026
7027 bfd_boolean
7028 ppc64_elf_inline_plt (struct bfd_link_info *info)
7029 {
7030   struct ppc_link_hash_table *htab;
7031   bfd *ibfd;
7032   asection *sec;
7033   bfd_vma low_vma, high_vma, limit;
7034
7035   htab = ppc_hash_table (info);
7036   if (htab == NULL)
7037     return FALSE;
7038
7039   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
7040      reduced somewhat to cater for possible stubs that might be added
7041      between the call and its destination.  */
7042   if (htab->params->group_size < 0)
7043     {
7044       limit = -htab->params->group_size;
7045       if (limit == 1)
7046         limit = 0x1e00000;
7047     }
7048   else
7049     {
7050       limit = htab->params->group_size;
7051       if (limit == 1)
7052         limit = 0x1c00000;
7053     }
7054
7055   low_vma = -1;
7056   high_vma = 0;
7057   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7058     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7059       {
7060         if (low_vma > sec->vma)
7061           low_vma = sec->vma;
7062         if (high_vma < sec->vma + sec->size)
7063           high_vma = sec->vma + sec->size;
7064       }
7065
7066   /* If a "bl" can reach anywhere in local code sections, then we can
7067      convert all inline PLT sequences to direct calls when the symbol
7068      is local.  */
7069   if (high_vma - low_vma < limit)
7070     {
7071       htab->can_convert_all_inline_plt = 1;
7072       return TRUE;
7073     }
7074
7075   /* Otherwise, go looking through relocs for cases where a direct
7076      call won't reach.  Mark the symbol on any such reloc to disable
7077      the optimization and keep the PLT entry as it seems likely that
7078      this will be better than creating trampolines.  Note that this
7079      will disable the optimization for all inline PLT calls to a
7080      particular symbol, not just those that won't reach.  The
7081      difficulty in doing a more precise optimization is that the
7082      linker needs to make a decision depending on whether a
7083      particular R_PPC64_PLTCALL insn can be turned into a direct
7084      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7085      the sequence, and there is nothing that ties those relocs
7086      together except their symbol.  */
7087
7088   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7089     {
7090       Elf_Internal_Shdr *symtab_hdr;
7091       Elf_Internal_Sym *local_syms;
7092
7093       if (!is_ppc64_elf (ibfd))
7094         continue;
7095
7096       local_syms = NULL;
7097       symtab_hdr = &elf_symtab_hdr (ibfd);
7098
7099       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7100         if (ppc64_elf_section_data (sec)->has_pltcall
7101             && !bfd_is_abs_section (sec->output_section))
7102           {
7103             Elf_Internal_Rela *relstart, *rel, *relend;
7104
7105             /* Read the relocations.  */
7106             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7107                                                   info->keep_memory);
7108             if (relstart == NULL)
7109               return FALSE;
7110
7111             relend = relstart + sec->reloc_count;
7112             for (rel = relstart; rel < relend; )
7113               {
7114                 enum elf_ppc64_reloc_type r_type;
7115                 unsigned long r_symndx;
7116                 asection *sym_sec;
7117                 struct elf_link_hash_entry *h;
7118                 Elf_Internal_Sym *sym;
7119                 unsigned char *tls_maskp;
7120
7121                 r_type = ELF64_R_TYPE (rel->r_info);
7122                 if (r_type != R_PPC64_PLTCALL)
7123                   continue;
7124
7125                 r_symndx = ELF64_R_SYM (rel->r_info);
7126                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7127                                 r_symndx, ibfd))
7128                   {
7129                     if (elf_section_data (sec)->relocs != relstart)
7130                       free (relstart);
7131                     if (local_syms != NULL
7132                         && symtab_hdr->contents != (bfd_byte *) local_syms)
7133                       free (local_syms);
7134                     return FALSE;
7135                   }
7136
7137                 if (sym_sec != NULL && sym_sec->output_section != NULL)
7138                   {
7139                     bfd_vma from, to;
7140                     if (h != NULL)
7141                       to = h->root.u.def.value;
7142                     else
7143                       to = sym->st_value;
7144                     to += (rel->r_addend
7145                            + sym_sec->output_offset
7146                            + sym_sec->output_section->vma);
7147                     from = (rel->r_offset
7148                             + sec->output_offset
7149                             + sec->output_section->vma);
7150                     if (to - from + limit < 2 * limit)
7151                       *tls_maskp &= ~PLT_KEEP;
7152                   }
7153               }
7154             if (elf_section_data (sec)->relocs != relstart)
7155               free (relstart);
7156           }
7157
7158       if (local_syms != NULL
7159           && symtab_hdr->contents != (unsigned char *) local_syms)
7160         {
7161           if (!info->keep_memory)
7162             free (local_syms);
7163           else
7164             symtab_hdr->contents = (unsigned char *) local_syms;
7165         }
7166     }
7167
7168   return TRUE;
7169 }
7170
7171 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
7172
7173 asection *
7174 ppc64_elf_tls_setup (struct bfd_link_info *info)
7175 {
7176   struct ppc_link_hash_table *htab;
7177
7178   htab = ppc_hash_table (info);
7179   if (htab == NULL)
7180     return NULL;
7181
7182   if (abiversion (info->output_bfd) == 1)
7183     htab->opd_abi = 1;
7184
7185   if (htab->params->no_multi_toc)
7186     htab->do_multi_toc = 0;
7187   else if (!htab->do_multi_toc)
7188     htab->params->no_multi_toc = 1;
7189
7190   /* Default to --no-plt-localentry, as this option can cause problems
7191      with symbol interposition.  For example, glibc libpthread.so and
7192      libc.so duplicate many pthread symbols, with a fallback
7193      implementation in libc.so.  In some cases the fallback does more
7194      work than the pthread implementation.  __pthread_condattr_destroy
7195      is one such symbol: the libpthread.so implementation is
7196      localentry:0 while the libc.so implementation is localentry:8.
7197      An app that "cleverly" uses dlopen to only load necessary
7198      libraries at runtime may omit loading libpthread.so when not
7199      running multi-threaded, which then results in the libc.so
7200      fallback symbols being used and ld.so complaining.  Now there
7201      are workarounds in ld (see non_zero_localentry) to detect the
7202      pthread situation, but that may not be the only case where
7203      --plt-localentry can cause trouble.  */
7204   if (htab->params->plt_localentry0 < 0)
7205     htab->params->plt_localentry0 = 0;
7206   if (htab->params->plt_localentry0
7207       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7208                                FALSE, FALSE, FALSE) == NULL)
7209     _bfd_error_handler
7210       (_("warning: --plt-localentry is especially dangerous without "
7211          "ld.so support to detect ABI violations"));
7212
7213   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7214                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7215                                               FALSE, FALSE, TRUE));
7216   /* Move dynamic linking info to the function descriptor sym.  */
7217   if (htab->tls_get_addr != NULL)
7218     func_desc_adjust (&htab->tls_get_addr->elf, info);
7219   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7220                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7221                                                  FALSE, FALSE, TRUE));
7222   if (htab->params->tls_get_addr_opt)
7223     {
7224       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7225
7226       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7227                                   FALSE, FALSE, TRUE);
7228       if (opt != NULL)
7229         func_desc_adjust (opt, info);
7230       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7231                                      FALSE, FALSE, TRUE);
7232       if (opt_fd != NULL
7233           && (opt_fd->root.type == bfd_link_hash_defined
7234               || opt_fd->root.type == bfd_link_hash_defweak))
7235         {
7236           /* If glibc supports an optimized __tls_get_addr call stub,
7237              signalled by the presence of __tls_get_addr_opt, and we'll
7238              be calling __tls_get_addr via a plt call stub, then
7239              make __tls_get_addr point to __tls_get_addr_opt.  */
7240           tga_fd = &htab->tls_get_addr_fd->elf;
7241           if (htab->elf.dynamic_sections_created
7242               && tga_fd != NULL
7243               && (tga_fd->type == STT_FUNC
7244                   || tga_fd->needs_plt)
7245               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7246                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
7247             {
7248               struct plt_entry *ent;
7249
7250               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7251                 if (ent->plt.refcount > 0)
7252                   break;
7253               if (ent != NULL)
7254                 {
7255                   tga_fd->root.type = bfd_link_hash_indirect;
7256                   tga_fd->root.u.i.link = &opt_fd->root;
7257                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7258                   opt_fd->mark = 1;
7259                   if (opt_fd->dynindx != -1)
7260                     {
7261                       /* Use __tls_get_addr_opt in dynamic relocations.  */
7262                       opt_fd->dynindx = -1;
7263                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7264                                               opt_fd->dynstr_index);
7265                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7266                         return NULL;
7267                     }
7268                   htab->tls_get_addr_fd
7269                     = (struct ppc_link_hash_entry *) opt_fd;
7270                   tga = &htab->tls_get_addr->elf;
7271                   if (opt != NULL && tga != NULL)
7272                     {
7273                       tga->root.type = bfd_link_hash_indirect;
7274                       tga->root.u.i.link = &opt->root;
7275                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
7276                       opt->mark = 1;
7277                       _bfd_elf_link_hash_hide_symbol (info, opt,
7278                                                       tga->forced_local);
7279                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7280                     }
7281                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7282                   htab->tls_get_addr_fd->is_func_descriptor = 1;
7283                   if (htab->tls_get_addr != NULL)
7284                     {
7285                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7286                       htab->tls_get_addr->is_func = 1;
7287                     }
7288                 }
7289             }
7290         }
7291       else if (htab->params->tls_get_addr_opt < 0)
7292         htab->params->tls_get_addr_opt = 0;
7293     }
7294   return _bfd_elf_tls_setup (info->output_bfd, info);
7295 }
7296
7297 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7298    HASH1 or HASH2.  */
7299
7300 static bfd_boolean
7301 branch_reloc_hash_match (const bfd *ibfd,
7302                          const Elf_Internal_Rela *rel,
7303                          const struct ppc_link_hash_entry *hash1,
7304                          const struct ppc_link_hash_entry *hash2)
7305 {
7306   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7307   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7308   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7309
7310   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7311     {
7312       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7313       struct elf_link_hash_entry *h;
7314
7315       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7316       h = elf_follow_link (h);
7317       if (h == &hash1->elf || h == &hash2->elf)
7318         return TRUE;
7319     }
7320   return FALSE;
7321 }
7322
7323 /* Run through all the TLS relocs looking for optimization
7324    opportunities.  The linker has been hacked (see ppc64elf.em) to do
7325    a preliminary section layout so that we know the TLS segment
7326    offsets.  We can't optimize earlier because some optimizations need
7327    to know the tp offset, and we need to optimize before allocating
7328    dynamic relocations.  */
7329
7330 bfd_boolean
7331 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7332 {
7333   bfd *ibfd;
7334   asection *sec;
7335   struct ppc_link_hash_table *htab;
7336   unsigned char *toc_ref;
7337   int pass;
7338
7339   if (!bfd_link_executable (info))
7340     return TRUE;
7341
7342   htab = ppc_hash_table (info);
7343   if (htab == NULL)
7344     return FALSE;
7345
7346   /* Make two passes over the relocs.  On the first pass, mark toc
7347      entries involved with tls relocs, and check that tls relocs
7348      involved in setting up a tls_get_addr call are indeed followed by
7349      such a call.  If they are not, we can't do any tls optimization.
7350      On the second pass twiddle tls_mask flags to notify
7351      relocate_section that optimization can be done, and adjust got
7352      and plt refcounts.  */
7353   toc_ref = NULL;
7354   for (pass = 0; pass < 2; ++pass)
7355     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7356       {
7357         Elf_Internal_Sym *locsyms = NULL;
7358         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7359
7360         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7361           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7362             {
7363               Elf_Internal_Rela *relstart, *rel, *relend;
7364               bfd_boolean found_tls_get_addr_arg = 0;
7365
7366               /* Read the relocations.  */
7367               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7368                                                     info->keep_memory);
7369               if (relstart == NULL)
7370                 {
7371                   free (toc_ref);
7372                   return FALSE;
7373                 }
7374
7375               relend = relstart + sec->reloc_count;
7376               for (rel = relstart; rel < relend; rel++)
7377                 {
7378                   enum elf_ppc64_reloc_type r_type;
7379                   unsigned long r_symndx;
7380                   struct elf_link_hash_entry *h;
7381                   Elf_Internal_Sym *sym;
7382                   asection *sym_sec;
7383                   unsigned char *tls_mask;
7384                   unsigned char tls_set, tls_clear, tls_type = 0;
7385                   bfd_vma value;
7386                   bfd_boolean ok_tprel, is_local;
7387                   long toc_ref_index = 0;
7388                   int expecting_tls_get_addr = 0;
7389                   bfd_boolean ret = FALSE;
7390
7391                   r_symndx = ELF64_R_SYM (rel->r_info);
7392                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7393                                   r_symndx, ibfd))
7394                     {
7395                     err_free_rel:
7396                       if (elf_section_data (sec)->relocs != relstart)
7397                         free (relstart);
7398                       if (toc_ref != NULL)
7399                         free (toc_ref);
7400                       if (locsyms != NULL
7401                           && (elf_symtab_hdr (ibfd).contents
7402                               != (unsigned char *) locsyms))
7403                         free (locsyms);
7404                       return ret;
7405                     }
7406
7407                   if (h != NULL)
7408                     {
7409                       if (h->root.type == bfd_link_hash_defined
7410                           || h->root.type == bfd_link_hash_defweak)
7411                         value = h->root.u.def.value;
7412                       else if (h->root.type == bfd_link_hash_undefweak)
7413                         value = 0;
7414                       else
7415                         {
7416                           found_tls_get_addr_arg = 0;
7417                           continue;
7418                         }
7419                     }
7420                   else
7421                     /* Symbols referenced by TLS relocs must be of type
7422                        STT_TLS.  So no need for .opd local sym adjust.  */
7423                     value = sym->st_value;
7424
7425                   ok_tprel = FALSE;
7426                   is_local = FALSE;
7427                   if (h == NULL
7428                       || !h->def_dynamic)
7429                     {
7430                       is_local = TRUE;
7431                       if (h != NULL
7432                           && h->root.type == bfd_link_hash_undefweak)
7433                         ok_tprel = TRUE;
7434                       else if (sym_sec != NULL
7435                                && sym_sec->output_section != NULL)
7436                         {
7437                           value += sym_sec->output_offset;
7438                           value += sym_sec->output_section->vma;
7439                           value -= htab->elf.tls_sec->vma;
7440                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7441                                       < (bfd_vma) 1 << 32);
7442                         }
7443                     }
7444
7445                   r_type = ELF64_R_TYPE (rel->r_info);
7446                   /* If this section has old-style __tls_get_addr calls
7447                      without marker relocs, then check that each
7448                      __tls_get_addr call reloc is preceded by a reloc
7449                      that conceivably belongs to the __tls_get_addr arg
7450                      setup insn.  If we don't find matching arg setup
7451                      relocs, don't do any tls optimization.  */
7452                   if (pass == 0
7453                       && sec->has_tls_get_addr_call
7454                       && h != NULL
7455                       && (h == &htab->tls_get_addr->elf
7456                           || h == &htab->tls_get_addr_fd->elf)
7457                       && !found_tls_get_addr_arg
7458                       && is_branch_reloc (r_type))
7459                     {
7460                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7461                                                 "TLS optimization disabled\n"),
7462                                               ibfd, sec, rel->r_offset);
7463                       ret = TRUE;
7464                       goto err_free_rel;
7465                     }
7466
7467                   found_tls_get_addr_arg = 0;
7468                   switch (r_type)
7469                     {
7470                     case R_PPC64_GOT_TLSLD16:
7471                     case R_PPC64_GOT_TLSLD16_LO:
7472                       expecting_tls_get_addr = 1;
7473                       found_tls_get_addr_arg = 1;
7474                       /* Fall through.  */
7475
7476                     case R_PPC64_GOT_TLSLD16_HI:
7477                     case R_PPC64_GOT_TLSLD16_HA:
7478                       /* These relocs should never be against a symbol
7479                          defined in a shared lib.  Leave them alone if
7480                          that turns out to be the case.  */
7481                       if (!is_local)
7482                         continue;
7483
7484                       /* LD -> LE */
7485                       tls_set = 0;
7486                       tls_clear = TLS_LD;
7487                       tls_type = TLS_TLS | TLS_LD;
7488                       break;
7489
7490                     case R_PPC64_GOT_TLSGD16:
7491                     case R_PPC64_GOT_TLSGD16_LO:
7492                       expecting_tls_get_addr = 1;
7493                       found_tls_get_addr_arg = 1;
7494                       /* Fall through. */
7495
7496                     case R_PPC64_GOT_TLSGD16_HI:
7497                     case R_PPC64_GOT_TLSGD16_HA:
7498                       if (ok_tprel)
7499                         /* GD -> LE */
7500                         tls_set = 0;
7501                       else
7502                         /* GD -> IE */
7503                         tls_set = TLS_TLS | TLS_TPRELGD;
7504                       tls_clear = TLS_GD;
7505                       tls_type = TLS_TLS | TLS_GD;
7506                       break;
7507
7508                     case R_PPC64_GOT_TPREL16_DS:
7509                     case R_PPC64_GOT_TPREL16_LO_DS:
7510                     case R_PPC64_GOT_TPREL16_HI:
7511                     case R_PPC64_GOT_TPREL16_HA:
7512                       if (ok_tprel)
7513                         {
7514                           /* IE -> LE */
7515                           tls_set = 0;
7516                           tls_clear = TLS_TPREL;
7517                           tls_type = TLS_TLS | TLS_TPREL;
7518                           break;
7519                         }
7520                       continue;
7521
7522                     case R_PPC64_TLSGD:
7523                     case R_PPC64_TLSLD:
7524                       if (rel + 1 < relend
7525                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
7526                         {
7527                           if (pass != 0
7528                               && (ELF64_R_TYPE (rel[1].r_info)
7529                                   != R_PPC64_PLTSEQ))
7530                             {
7531                               r_symndx = ELF64_R_SYM (rel[1].r_info);
7532                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
7533                                               r_symndx, ibfd))
7534                                 goto err_free_rel;
7535                               if (h != NULL)
7536                                 {
7537                                   struct plt_entry *ent = NULL;
7538
7539                                   for (ent = h->plt.plist;
7540                                        ent != NULL;
7541                                        ent = ent->next)
7542                                     if (ent->addend == rel[1].r_addend)
7543                                       break;
7544
7545                                   if (ent != NULL
7546                                       && ent->plt.refcount > 0)
7547                                     ent->plt.refcount -= 1;
7548                                 }
7549                             }
7550                           continue;
7551                         }
7552                       found_tls_get_addr_arg = 1;
7553                       /* Fall through.  */
7554
7555                     case R_PPC64_TLS:
7556                     case R_PPC64_TOC16:
7557                     case R_PPC64_TOC16_LO:
7558                       if (sym_sec == NULL || sym_sec != toc)
7559                         continue;
7560
7561                       /* Mark this toc entry as referenced by a TLS
7562                          code sequence.  We can do that now in the
7563                          case of R_PPC64_TLS, and after checking for
7564                          tls_get_addr for the TOC16 relocs.  */
7565                       if (toc_ref == NULL)
7566                         toc_ref
7567                           = bfd_zmalloc (toc->output_section->rawsize / 8);
7568                       if (toc_ref == NULL)
7569                         goto err_free_rel;
7570
7571                       if (h != NULL)
7572                         value = h->root.u.def.value;
7573                       else
7574                         value = sym->st_value;
7575                       value += rel->r_addend;
7576                       if (value % 8 != 0)
7577                         continue;
7578                       BFD_ASSERT (value < toc->size
7579                                   && toc->output_offset % 8 == 0);
7580                       toc_ref_index = (value + toc->output_offset) / 8;
7581                       if (r_type == R_PPC64_TLS
7582                           || r_type == R_PPC64_TLSGD
7583                           || r_type == R_PPC64_TLSLD)
7584                         {
7585                           toc_ref[toc_ref_index] = 1;
7586                           continue;
7587                         }
7588
7589                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
7590                         continue;
7591
7592                       tls_set = 0;
7593                       tls_clear = 0;
7594                       expecting_tls_get_addr = 2;
7595                       break;
7596
7597                     case R_PPC64_TPREL64:
7598                       if (pass == 0
7599                           || sec != toc
7600                           || toc_ref == NULL
7601                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7602                         continue;
7603                       if (ok_tprel)
7604                         {
7605                           /* IE -> LE */
7606                           tls_set = TLS_EXPLICIT;
7607                           tls_clear = TLS_TPREL;
7608                           break;
7609                         }
7610                       continue;
7611
7612                     case R_PPC64_DTPMOD64:
7613                       if (pass == 0
7614                           || sec != toc
7615                           || toc_ref == NULL
7616                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7617                         continue;
7618                       if (rel + 1 < relend
7619                           && (rel[1].r_info
7620                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7621                           && rel[1].r_offset == rel->r_offset + 8)
7622                         {
7623                           if (ok_tprel)
7624                             /* GD -> LE */
7625                             tls_set = TLS_EXPLICIT | TLS_GD;
7626                           else
7627                             /* GD -> IE */
7628                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7629                           tls_clear = TLS_GD;
7630                         }
7631                       else
7632                         {
7633                           if (!is_local)
7634                             continue;
7635
7636                           /* LD -> LE */
7637                           tls_set = TLS_EXPLICIT;
7638                           tls_clear = TLS_LD;
7639                         }
7640                       break;
7641
7642                     default:
7643                       continue;
7644                     }
7645
7646                   if (pass == 0)
7647                     {
7648                       if (!expecting_tls_get_addr
7649                           || !sec->has_tls_get_addr_call)
7650                         continue;
7651
7652                       if (rel + 1 < relend
7653                           && branch_reloc_hash_match (ibfd, rel + 1,
7654                                                       htab->tls_get_addr,
7655                                                       htab->tls_get_addr_fd))
7656                         {
7657                           if (expecting_tls_get_addr == 2)
7658                             {
7659                               /* Check for toc tls entries.  */
7660                               unsigned char *toc_tls;
7661                               int retval;
7662
7663                               retval = get_tls_mask (&toc_tls, NULL, NULL,
7664                                                      &locsyms,
7665                                                      rel, ibfd);
7666                               if (retval == 0)
7667                                 goto err_free_rel;
7668                               if (toc_tls != NULL)
7669                                 {
7670                                   if ((*toc_tls & TLS_TLS) != 0
7671                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
7672                                     found_tls_get_addr_arg = 1;
7673                                   if (retval > 1)
7674                                     toc_ref[toc_ref_index] = 1;
7675                                 }
7676                             }
7677                           continue;
7678                         }
7679
7680                       /* Uh oh, we didn't find the expected call.  We
7681                          could just mark this symbol to exclude it
7682                          from tls optimization but it's safer to skip
7683                          the entire optimization.  */
7684                       /* xgettext:c-format */
7685                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7686                                                 "TLS optimization disabled\n"),
7687                                               ibfd, sec, rel->r_offset);
7688                       ret = TRUE;
7689                       goto err_free_rel;
7690                     }
7691
7692                   /* If we don't have old-style __tls_get_addr calls
7693                      without TLSGD/TLSLD marker relocs, and we haven't
7694                      found a new-style __tls_get_addr call with a
7695                      marker for this symbol, then we either have a
7696                      broken object file or an -mlongcall style
7697                      indirect call to __tls_get_addr without a marker.
7698                      Disable optimization in this case.  */
7699                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
7700                       && (tls_set & TLS_EXPLICIT) == 0
7701                       && !sec->has_tls_get_addr_call
7702                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
7703                           != (TLS_TLS | TLS_MARK)))
7704                     continue;
7705
7706                   if (expecting_tls_get_addr)
7707                     {
7708                       struct plt_entry *ent = NULL;
7709
7710                       if (htab->tls_get_addr != NULL)
7711                         for (ent = htab->tls_get_addr->elf.plt.plist;
7712                              ent != NULL;
7713                              ent = ent->next)
7714                           if (ent->addend == 0)
7715                             break;
7716
7717                       if (ent == NULL && htab->tls_get_addr_fd != NULL)
7718                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7719                              ent != NULL;
7720                              ent = ent->next)
7721                           if (ent->addend == 0)
7722                             break;
7723
7724                       if (ent != NULL
7725                           && ent->plt.refcount > 0)
7726                         ent->plt.refcount -= 1;
7727                     }
7728
7729                   if (tls_clear == 0)
7730                     continue;
7731
7732                   if ((tls_set & TLS_EXPLICIT) == 0)
7733                     {
7734                       struct got_entry *ent;
7735
7736                       /* Adjust got entry for this reloc.  */
7737                       if (h != NULL)
7738                         ent = h->got.glist;
7739                       else
7740                         ent = elf_local_got_ents (ibfd)[r_symndx];
7741
7742                       for (; ent != NULL; ent = ent->next)
7743                         if (ent->addend == rel->r_addend
7744                             && ent->owner == ibfd
7745                             && ent->tls_type == tls_type)
7746                           break;
7747                       if (ent == NULL)
7748                         abort ();
7749
7750                       if (tls_set == 0)
7751                         {
7752                           /* We managed to get rid of a got entry.  */
7753                           if (ent->got.refcount > 0)
7754                             ent->got.refcount -= 1;
7755                         }
7756                     }
7757                   else
7758                     {
7759                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
7760                          we'll lose one or two dyn relocs.  */
7761                       if (!dec_dynrel_count (rel->r_info, sec, info,
7762                                              NULL, h, sym))
7763                         return FALSE;
7764
7765                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
7766                         {
7767                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7768                                                  NULL, h, sym))
7769                             return FALSE;
7770                         }
7771                     }
7772
7773                   *tls_mask |= tls_set;
7774                   *tls_mask &= ~tls_clear;
7775                 }
7776
7777               if (elf_section_data (sec)->relocs != relstart)
7778                 free (relstart);
7779             }
7780
7781         if (locsyms != NULL
7782             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7783           {
7784             if (!info->keep_memory)
7785               free (locsyms);
7786             else
7787               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7788           }
7789       }
7790
7791   if (toc_ref != NULL)
7792     free (toc_ref);
7793   htab->do_tls_opt = 1;
7794   return TRUE;
7795 }
7796
7797 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7798    the values of any global symbols in a toc section that has been
7799    edited.  Globals in toc sections should be a rarity, so this function
7800    sets a flag if any are found in toc sections other than the one just
7801    edited, so that further hash table traversals can be avoided.  */
7802
7803 struct adjust_toc_info
7804 {
7805   asection *toc;
7806   unsigned long *skip;
7807   bfd_boolean global_toc_syms;
7808 };
7809
7810 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7811
7812 static bfd_boolean
7813 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7814 {
7815   struct ppc_link_hash_entry *eh;
7816   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7817   unsigned long i;
7818
7819   if (h->root.type != bfd_link_hash_defined
7820       && h->root.type != bfd_link_hash_defweak)
7821     return TRUE;
7822
7823   eh = (struct ppc_link_hash_entry *) h;
7824   if (eh->adjust_done)
7825     return TRUE;
7826
7827   if (eh->elf.root.u.def.section == toc_inf->toc)
7828     {
7829       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7830         i = toc_inf->toc->rawsize >> 3;
7831       else
7832         i = eh->elf.root.u.def.value >> 3;
7833
7834       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7835         {
7836           _bfd_error_handler
7837             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7838           do
7839             ++i;
7840           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7841           eh->elf.root.u.def.value = (bfd_vma) i << 3;
7842         }
7843
7844       eh->elf.root.u.def.value -= toc_inf->skip[i];
7845       eh->adjust_done = 1;
7846     }
7847   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7848     toc_inf->global_toc_syms = TRUE;
7849
7850   return TRUE;
7851 }
7852
7853 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
7854    on a _LO variety toc/got reloc.  */
7855
7856 static bfd_boolean
7857 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
7858 {
7859   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7860           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7861           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7862           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7863           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7864           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7865           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7866           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7867           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7868           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7869           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7870           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7871           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7872           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7873           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7874           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7875           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7876               /* Exclude lfqu by testing reloc.  If relocs are ever
7877                  defined for the reduced D field in psq_lu then those
7878                  will need testing too.  */
7879               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7880           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7881               && (insn & 1) == 0)
7882           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7883           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7884               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
7885               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
7886           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7887               && (insn & 1) == 0));
7888 }
7889
7890 /* Examine all relocs referencing .toc sections in order to remove
7891    unused .toc entries.  */
7892
7893 bfd_boolean
7894 ppc64_elf_edit_toc (struct bfd_link_info *info)
7895 {
7896   bfd *ibfd;
7897   struct adjust_toc_info toc_inf;
7898   struct ppc_link_hash_table *htab = ppc_hash_table (info);
7899
7900   htab->do_toc_opt = 1;
7901   toc_inf.global_toc_syms = TRUE;
7902   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7903     {
7904       asection *toc, *sec;
7905       Elf_Internal_Shdr *symtab_hdr;
7906       Elf_Internal_Sym *local_syms;
7907       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7908       unsigned long *skip, *drop;
7909       unsigned char *used;
7910       unsigned char *keep, last, some_unused;
7911
7912       if (!is_ppc64_elf (ibfd))
7913         continue;
7914
7915       toc = bfd_get_section_by_name (ibfd, ".toc");
7916       if (toc == NULL
7917           || toc->size == 0
7918           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
7919           || discarded_section (toc))
7920         continue;
7921
7922       toc_relocs = NULL;
7923       local_syms = NULL;
7924       symtab_hdr = &elf_symtab_hdr (ibfd);
7925
7926       /* Look at sections dropped from the final link.  */
7927       skip = NULL;
7928       relstart = NULL;
7929       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7930         {
7931           if (sec->reloc_count == 0
7932               || !discarded_section (sec)
7933               || get_opd_info (sec)
7934               || (sec->flags & SEC_ALLOC) == 0
7935               || (sec->flags & SEC_DEBUGGING) != 0)
7936             continue;
7937
7938           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7939           if (relstart == NULL)
7940             goto error_ret;
7941
7942           /* Run through the relocs to see which toc entries might be
7943              unused.  */
7944           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7945             {
7946               enum elf_ppc64_reloc_type r_type;
7947               unsigned long r_symndx;
7948               asection *sym_sec;
7949               struct elf_link_hash_entry *h;
7950               Elf_Internal_Sym *sym;
7951               bfd_vma val;
7952
7953               r_type = ELF64_R_TYPE (rel->r_info);
7954               switch (r_type)
7955                 {
7956                 default:
7957                   continue;
7958
7959                 case R_PPC64_TOC16:
7960                 case R_PPC64_TOC16_LO:
7961                 case R_PPC64_TOC16_HI:
7962                 case R_PPC64_TOC16_HA:
7963                 case R_PPC64_TOC16_DS:
7964                 case R_PPC64_TOC16_LO_DS:
7965                   break;
7966                 }
7967
7968               r_symndx = ELF64_R_SYM (rel->r_info);
7969               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7970                               r_symndx, ibfd))
7971                 goto error_ret;
7972
7973               if (sym_sec != toc)
7974                 continue;
7975
7976               if (h != NULL)
7977                 val = h->root.u.def.value;
7978               else
7979                 val = sym->st_value;
7980               val += rel->r_addend;
7981
7982               if (val >= toc->size)
7983                 continue;
7984
7985               /* Anything in the toc ought to be aligned to 8 bytes.
7986                  If not, don't mark as unused.  */
7987               if (val & 7)
7988                 continue;
7989
7990               if (skip == NULL)
7991                 {
7992                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
7993                   if (skip == NULL)
7994                     goto error_ret;
7995                 }
7996
7997               skip[val >> 3] = ref_from_discarded;
7998             }
7999
8000           if (elf_section_data (sec)->relocs != relstart)
8001             free (relstart);
8002         }
8003
8004       /* For largetoc loads of address constants, we can convert
8005          .  addis rx,2,addr@got@ha
8006          .  ld ry,addr@got@l(rx)
8007          to
8008          .  addis rx,2,addr@toc@ha
8009          .  addi ry,rx,addr@toc@l
8010          when addr is within 2G of the toc pointer.  This then means
8011          that the word storing "addr" in the toc is no longer needed.  */
8012
8013       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8014           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8015           && toc->reloc_count != 0)
8016         {
8017           /* Read toc relocs.  */
8018           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8019                                                   info->keep_memory);
8020           if (toc_relocs == NULL)
8021             goto error_ret;
8022
8023           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8024             {
8025               enum elf_ppc64_reloc_type r_type;
8026               unsigned long r_symndx;
8027               asection *sym_sec;
8028               struct elf_link_hash_entry *h;
8029               Elf_Internal_Sym *sym;
8030               bfd_vma val, addr;
8031
8032               r_type = ELF64_R_TYPE (rel->r_info);
8033               if (r_type != R_PPC64_ADDR64)
8034                 continue;
8035
8036               r_symndx = ELF64_R_SYM (rel->r_info);
8037               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8038                               r_symndx, ibfd))
8039                 goto error_ret;
8040
8041               if (sym_sec == NULL
8042                   || sym_sec->output_section == NULL
8043                   || discarded_section (sym_sec))
8044                 continue;
8045
8046               if (!SYMBOL_REFERENCES_LOCAL (info, h))
8047                 continue;
8048
8049               if (h != NULL)
8050                 {
8051                   if (h->type == STT_GNU_IFUNC)
8052                     continue;
8053                   val = h->root.u.def.value;
8054                 }
8055               else
8056                 {
8057                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8058                     continue;
8059                   val = sym->st_value;
8060                 }
8061               val += rel->r_addend;
8062               val += sym_sec->output_section->vma + sym_sec->output_offset;
8063
8064               /* We don't yet know the exact toc pointer value, but we
8065                  know it will be somewhere in the toc section.  Don't
8066                  optimize if the difference from any possible toc
8067                  pointer is outside [ff..f80008000, 7fff7fff].  */
8068               addr = toc->output_section->vma + TOC_BASE_OFF;
8069               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8070                 continue;
8071
8072               addr = toc->output_section->vma + toc->output_section->rawsize;
8073               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8074                 continue;
8075
8076               if (skip == NULL)
8077                 {
8078                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8079                   if (skip == NULL)
8080                     goto error_ret;
8081                 }
8082
8083               skip[rel->r_offset >> 3]
8084                 |= can_optimize | ((rel - toc_relocs) << 2);
8085             }
8086         }
8087
8088       if (skip == NULL)
8089         continue;
8090
8091       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8092       if (used == NULL)
8093         {
8094         error_ret:
8095           if (local_syms != NULL
8096               && symtab_hdr->contents != (unsigned char *) local_syms)
8097             free (local_syms);
8098           if (sec != NULL
8099               && relstart != NULL
8100               && elf_section_data (sec)->relocs != relstart)
8101             free (relstart);
8102           if (toc_relocs != NULL
8103               && elf_section_data (toc)->relocs != toc_relocs)
8104             free (toc_relocs);
8105           if (skip != NULL)
8106             free (skip);
8107           return FALSE;
8108         }
8109
8110       /* Now check all kept sections that might reference the toc.
8111          Check the toc itself last.  */
8112       for (sec = (ibfd->sections == toc && toc->next ? toc->next
8113                   : ibfd->sections);
8114            sec != NULL;
8115            sec = (sec == toc ? NULL
8116                   : sec->next == NULL ? toc
8117                   : sec->next == toc && toc->next ? toc->next
8118                   : sec->next))
8119         {
8120           int repeat;
8121
8122           if (sec->reloc_count == 0
8123               || discarded_section (sec)
8124               || get_opd_info (sec)
8125               || (sec->flags & SEC_ALLOC) == 0
8126               || (sec->flags & SEC_DEBUGGING) != 0)
8127             continue;
8128
8129           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8130                                                 info->keep_memory);
8131           if (relstart == NULL)
8132             {
8133               free (used);
8134               goto error_ret;
8135             }
8136
8137           /* Mark toc entries referenced as used.  */
8138           do
8139             {
8140               repeat = 0;
8141               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8142                 {
8143                   enum elf_ppc64_reloc_type r_type;
8144                   unsigned long r_symndx;
8145                   asection *sym_sec;
8146                   struct elf_link_hash_entry *h;
8147                   Elf_Internal_Sym *sym;
8148                   bfd_vma val;
8149                   enum {no_check, check_lo, check_ha} insn_check;
8150
8151                   r_type = ELF64_R_TYPE (rel->r_info);
8152                   switch (r_type)
8153                     {
8154                     default:
8155                       insn_check = no_check;
8156                       break;
8157
8158                     case R_PPC64_GOT_TLSLD16_HA:
8159                     case R_PPC64_GOT_TLSGD16_HA:
8160                     case R_PPC64_GOT_TPREL16_HA:
8161                     case R_PPC64_GOT_DTPREL16_HA:
8162                     case R_PPC64_GOT16_HA:
8163                     case R_PPC64_TOC16_HA:
8164                       insn_check = check_ha;
8165                       break;
8166
8167                     case R_PPC64_GOT_TLSLD16_LO:
8168                     case R_PPC64_GOT_TLSGD16_LO:
8169                     case R_PPC64_GOT_TPREL16_LO_DS:
8170                     case R_PPC64_GOT_DTPREL16_LO_DS:
8171                     case R_PPC64_GOT16_LO:
8172                     case R_PPC64_GOT16_LO_DS:
8173                     case R_PPC64_TOC16_LO:
8174                     case R_PPC64_TOC16_LO_DS:
8175                       insn_check = check_lo;
8176                       break;
8177                     }
8178
8179                   if (insn_check != no_check)
8180                     {
8181                       bfd_vma off = rel->r_offset & ~3;
8182                       unsigned char buf[4];
8183                       unsigned int insn;
8184
8185                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8186                         {
8187                           free (used);
8188                           goto error_ret;
8189                         }
8190                       insn = bfd_get_32 (ibfd, buf);
8191                       if (insn_check == check_lo
8192                           ? !ok_lo_toc_insn (insn, r_type)
8193                           : ((insn & ((0x3f << 26) | 0x1f << 16))
8194                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8195                         {
8196                           char str[12];
8197
8198                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8199                           sprintf (str, "%#08x", insn);
8200                           info->callbacks->einfo
8201                             /* xgettext:c-format */
8202                             (_("%H: toc optimization is not supported for"
8203                                " %s instruction\n"),
8204                              ibfd, sec, rel->r_offset & ~3, str);
8205                         }
8206                     }
8207
8208                   switch (r_type)
8209                     {
8210                     case R_PPC64_TOC16:
8211                     case R_PPC64_TOC16_LO:
8212                     case R_PPC64_TOC16_HI:
8213                     case R_PPC64_TOC16_HA:
8214                     case R_PPC64_TOC16_DS:
8215                     case R_PPC64_TOC16_LO_DS:
8216                       /* In case we're taking addresses of toc entries.  */
8217                     case R_PPC64_ADDR64:
8218                       break;
8219
8220                     default:
8221                       continue;
8222                     }
8223
8224                   r_symndx = ELF64_R_SYM (rel->r_info);
8225                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8226                                   r_symndx, ibfd))
8227                     {
8228                       free (used);
8229                       goto error_ret;
8230                     }
8231
8232                   if (sym_sec != toc)
8233                     continue;
8234
8235                   if (h != NULL)
8236                     val = h->root.u.def.value;
8237                   else
8238                     val = sym->st_value;
8239                   val += rel->r_addend;
8240
8241                   if (val >= toc->size)
8242                     continue;
8243
8244                   if ((skip[val >> 3] & can_optimize) != 0)
8245                     {
8246                       bfd_vma off;
8247                       unsigned char opc;
8248
8249                       switch (r_type)
8250                         {
8251                         case R_PPC64_TOC16_HA:
8252                           break;
8253
8254                         case R_PPC64_TOC16_LO_DS:
8255                           off = rel->r_offset;
8256                           off += (bfd_big_endian (ibfd) ? -2 : 3);
8257                           if (!bfd_get_section_contents (ibfd, sec, &opc,
8258                                                          off, 1))
8259                             {
8260                               free (used);
8261                               goto error_ret;
8262                             }
8263                           if ((opc & (0x3f << 2)) == (58u << 2))
8264                             break;
8265                           /* Fall through.  */
8266
8267                         default:
8268                           /* Wrong sort of reloc, or not a ld.  We may
8269                              as well clear ref_from_discarded too.  */
8270                           skip[val >> 3] = 0;
8271                         }
8272                     }
8273
8274                   if (sec != toc)
8275                     used[val >> 3] = 1;
8276                   /* For the toc section, we only mark as used if this
8277                      entry itself isn't unused.  */
8278                   else if ((used[rel->r_offset >> 3]
8279                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8280                            && !used[val >> 3])
8281                     {
8282                       /* Do all the relocs again, to catch reference
8283                          chains.  */
8284                       repeat = 1;
8285                       used[val >> 3] = 1;
8286                     }
8287                 }
8288             }
8289           while (repeat);
8290
8291           if (elf_section_data (sec)->relocs != relstart)
8292             free (relstart);
8293         }
8294
8295       /* Merge the used and skip arrays.  Assume that TOC
8296          doublewords not appearing as either used or unused belong
8297          to an entry more than one doubleword in size.  */
8298       for (drop = skip, keep = used, last = 0, some_unused = 0;
8299            drop < skip + (toc->size + 7) / 8;
8300            ++drop, ++keep)
8301         {
8302           if (*keep)
8303             {
8304               *drop &= ~ref_from_discarded;
8305               if ((*drop & can_optimize) != 0)
8306                 some_unused = 1;
8307               last = 0;
8308             }
8309           else if ((*drop & ref_from_discarded) != 0)
8310             {
8311               some_unused = 1;
8312               last = ref_from_discarded;
8313             }
8314           else
8315             *drop = last;
8316         }
8317
8318       free (used);
8319
8320       if (some_unused)
8321         {
8322           bfd_byte *contents, *src;
8323           unsigned long off;
8324           Elf_Internal_Sym *sym;
8325           bfd_boolean local_toc_syms = FALSE;
8326
8327           /* Shuffle the toc contents, and at the same time convert the
8328              skip array from booleans into offsets.  */
8329           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8330             goto error_ret;
8331
8332           elf_section_data (toc)->this_hdr.contents = contents;
8333
8334           for (src = contents, off = 0, drop = skip;
8335                src < contents + toc->size;
8336                src += 8, ++drop)
8337             {
8338               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8339                 off += 8;
8340               else if (off != 0)
8341                 {
8342                   *drop = off;
8343                   memcpy (src - off, src, 8);
8344                 }
8345             }
8346           *drop = off;
8347           toc->rawsize = toc->size;
8348           toc->size = src - contents - off;
8349
8350           /* Adjust addends for relocs against the toc section sym,
8351              and optimize any accesses we can.  */
8352           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8353             {
8354               if (sec->reloc_count == 0
8355                   || discarded_section (sec))
8356                 continue;
8357
8358               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8359                                                     info->keep_memory);
8360               if (relstart == NULL)
8361                 goto error_ret;
8362
8363               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8364                 {
8365                   enum elf_ppc64_reloc_type r_type;
8366                   unsigned long r_symndx;
8367                   asection *sym_sec;
8368                   struct elf_link_hash_entry *h;
8369                   bfd_vma val;
8370
8371                   r_type = ELF64_R_TYPE (rel->r_info);
8372                   switch (r_type)
8373                     {
8374                     default:
8375                       continue;
8376
8377                     case R_PPC64_TOC16:
8378                     case R_PPC64_TOC16_LO:
8379                     case R_PPC64_TOC16_HI:
8380                     case R_PPC64_TOC16_HA:
8381                     case R_PPC64_TOC16_DS:
8382                     case R_PPC64_TOC16_LO_DS:
8383                     case R_PPC64_ADDR64:
8384                       break;
8385                     }
8386
8387                   r_symndx = ELF64_R_SYM (rel->r_info);
8388                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8389                                   r_symndx, ibfd))
8390                     goto error_ret;
8391
8392                   if (sym_sec != toc)
8393                     continue;
8394
8395                   if (h != NULL)
8396                     val = h->root.u.def.value;
8397                   else
8398                     {
8399                       val = sym->st_value;
8400                       if (val != 0)
8401                         local_toc_syms = TRUE;
8402                     }
8403
8404                   val += rel->r_addend;
8405
8406                   if (val > toc->rawsize)
8407                     val = toc->rawsize;
8408                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
8409                     continue;
8410                   else if ((skip[val >> 3] & can_optimize) != 0)
8411                     {
8412                       Elf_Internal_Rela *tocrel
8413                         = toc_relocs + (skip[val >> 3] >> 2);
8414                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8415
8416                       switch (r_type)
8417                         {
8418                         case R_PPC64_TOC16_HA:
8419                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8420                           break;
8421
8422                         case R_PPC64_TOC16_LO_DS:
8423                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8424                           break;
8425
8426                         default:
8427                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8428                             ppc_howto_init ();
8429                           info->callbacks->einfo
8430                             /* xgettext:c-format */
8431                             (_("%H: %s references "
8432                                "optimized away TOC entry\n"),
8433                              ibfd, sec, rel->r_offset,
8434                              ppc64_elf_howto_table[r_type]->name);
8435                           bfd_set_error (bfd_error_bad_value);
8436                           goto error_ret;
8437                         }
8438                       rel->r_addend = tocrel->r_addend;
8439                       elf_section_data (sec)->relocs = relstart;
8440                       continue;
8441                     }
8442
8443                   if (h != NULL || sym->st_value != 0)
8444                     continue;
8445
8446                   rel->r_addend -= skip[val >> 3];
8447                   elf_section_data (sec)->relocs = relstart;
8448                 }
8449
8450               if (elf_section_data (sec)->relocs != relstart)
8451                 free (relstart);
8452             }
8453
8454           /* We shouldn't have local or global symbols defined in the TOC,
8455              but handle them anyway.  */
8456           if (local_syms != NULL)
8457             for (sym = local_syms;
8458                  sym < local_syms + symtab_hdr->sh_info;
8459                  ++sym)
8460               if (sym->st_value != 0
8461                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8462                 {
8463                   unsigned long i;
8464
8465                   if (sym->st_value > toc->rawsize)
8466                     i = toc->rawsize >> 3;
8467                   else
8468                     i = sym->st_value >> 3;
8469
8470                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8471                     {
8472                       if (local_toc_syms)
8473                         _bfd_error_handler
8474                           (_("%s defined on removed toc entry"),
8475                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8476                       do
8477                         ++i;
8478                       while ((skip[i] & (ref_from_discarded | can_optimize)));
8479                       sym->st_value = (bfd_vma) i << 3;
8480                     }
8481
8482                   sym->st_value -= skip[i];
8483                   symtab_hdr->contents = (unsigned char *) local_syms;
8484                 }
8485
8486           /* Adjust any global syms defined in this toc input section.  */
8487           if (toc_inf.global_toc_syms)
8488             {
8489               toc_inf.toc = toc;
8490               toc_inf.skip = skip;
8491               toc_inf.global_toc_syms = FALSE;
8492               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8493                                       &toc_inf);
8494             }
8495
8496           if (toc->reloc_count != 0)
8497             {
8498               Elf_Internal_Shdr *rel_hdr;
8499               Elf_Internal_Rela *wrel;
8500               bfd_size_type sz;
8501
8502               /* Remove unused toc relocs, and adjust those we keep.  */
8503               if (toc_relocs == NULL)
8504                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8505                                                         info->keep_memory);
8506               if (toc_relocs == NULL)
8507                 goto error_ret;
8508
8509               wrel = toc_relocs;
8510               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8511                 if ((skip[rel->r_offset >> 3]
8512                      & (ref_from_discarded | can_optimize)) == 0)
8513                   {
8514                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8515                     wrel->r_info = rel->r_info;
8516                     wrel->r_addend = rel->r_addend;
8517                     ++wrel;
8518                   }
8519                 else if (!dec_dynrel_count (rel->r_info, toc, info,
8520                                             &local_syms, NULL, NULL))
8521                   goto error_ret;
8522
8523               elf_section_data (toc)->relocs = toc_relocs;
8524               toc->reloc_count = wrel - toc_relocs;
8525               rel_hdr = _bfd_elf_single_rel_hdr (toc);
8526               sz = rel_hdr->sh_entsize;
8527               rel_hdr->sh_size = toc->reloc_count * sz;
8528             }
8529         }
8530       else if (toc_relocs != NULL
8531                && elf_section_data (toc)->relocs != toc_relocs)
8532         free (toc_relocs);
8533
8534       if (local_syms != NULL
8535           && symtab_hdr->contents != (unsigned char *) local_syms)
8536         {
8537           if (!info->keep_memory)
8538             free (local_syms);
8539           else
8540             symtab_hdr->contents = (unsigned char *) local_syms;
8541         }
8542       free (skip);
8543     }
8544
8545   return TRUE;
8546 }
8547
8548 /* Return true iff input section I references the TOC using
8549    instructions limited to +/-32k offsets.  */
8550
8551 bfd_boolean
8552 ppc64_elf_has_small_toc_reloc (asection *i)
8553 {
8554   return (is_ppc64_elf (i->owner)
8555           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8556 }
8557
8558 /* Allocate space for one GOT entry.  */
8559
8560 static void
8561 allocate_got (struct elf_link_hash_entry *h,
8562               struct bfd_link_info *info,
8563               struct got_entry *gent)
8564 {
8565   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8566   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8567   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8568                  ? 16 : 8);
8569   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8570                   ? 2 : 1) * sizeof (Elf64_External_Rela);
8571   asection *got = ppc64_elf_tdata (gent->owner)->got;
8572
8573   gent->got.offset = got->size;
8574   got->size += entsize;
8575
8576   if (h->type == STT_GNU_IFUNC)
8577     {
8578       htab->elf.irelplt->size += rentsize;
8579       htab->got_reli_size += rentsize;
8580     }
8581   else if (((bfd_link_pic (info)
8582              && !((gent->tls_type & TLS_TPREL) != 0
8583                   && bfd_link_executable (info)
8584                   && SYMBOL_REFERENCES_LOCAL (info, h)))
8585             || (htab->elf.dynamic_sections_created
8586                 && h->dynindx != -1
8587                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
8588            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8589     {
8590       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8591       relgot->size += rentsize;
8592     }
8593 }
8594
8595 /* This function merges got entries in the same toc group.  */
8596
8597 static void
8598 merge_got_entries (struct got_entry **pent)
8599 {
8600   struct got_entry *ent, *ent2;
8601
8602   for (ent = *pent; ent != NULL; ent = ent->next)
8603     if (!ent->is_indirect)
8604       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8605         if (!ent2->is_indirect
8606             && ent2->addend == ent->addend
8607             && ent2->tls_type == ent->tls_type
8608             && elf_gp (ent2->owner) == elf_gp (ent->owner))
8609           {
8610             ent2->is_indirect = TRUE;
8611             ent2->got.ent = ent;
8612           }
8613 }
8614
8615 /* If H is undefined, make it dynamic if that makes sense.  */
8616
8617 static bfd_boolean
8618 ensure_undef_dynamic (struct bfd_link_info *info,
8619                       struct elf_link_hash_entry *h)
8620 {
8621   struct elf_link_hash_table *htab = elf_hash_table (info);
8622
8623   if (htab->dynamic_sections_created
8624       && ((info->dynamic_undefined_weak != 0
8625            && h->root.type == bfd_link_hash_undefweak)
8626           || h->root.type == bfd_link_hash_undefined)
8627       && h->dynindx == -1
8628       && !h->forced_local
8629       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
8630     return bfd_elf_link_record_dynamic_symbol (info, h);
8631   return TRUE;
8632 }
8633
8634 /* Allocate space in .plt, .got and associated reloc sections for
8635    dynamic relocs.  */
8636
8637 static bfd_boolean
8638 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8639 {
8640   struct bfd_link_info *info;
8641   struct ppc_link_hash_table *htab;
8642   asection *s;
8643   struct ppc_link_hash_entry *eh;
8644   struct got_entry **pgent, *gent;
8645
8646   if (h->root.type == bfd_link_hash_indirect)
8647     return TRUE;
8648
8649   info = (struct bfd_link_info *) inf;
8650   htab = ppc_hash_table (info);
8651   if (htab == NULL)
8652     return FALSE;
8653
8654   eh = (struct ppc_link_hash_entry *) h;
8655   /* Run through the TLS GD got entries first if we're changing them
8656      to TPREL.  */
8657   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
8658     for (gent = h->got.glist; gent != NULL; gent = gent->next)
8659       if (gent->got.refcount > 0
8660           && (gent->tls_type & TLS_GD) != 0)
8661         {
8662           /* This was a GD entry that has been converted to TPREL.  If
8663              there happens to be a TPREL entry we can use that one.  */
8664           struct got_entry *ent;
8665           for (ent = h->got.glist; ent != NULL; ent = ent->next)
8666             if (ent->got.refcount > 0
8667                 && (ent->tls_type & TLS_TPREL) != 0
8668                 && ent->addend == gent->addend
8669                 && ent->owner == gent->owner)
8670               {
8671                 gent->got.refcount = 0;
8672                 break;
8673               }
8674
8675           /* If not, then we'll be using our own TPREL entry.  */
8676           if (gent->got.refcount != 0)
8677             gent->tls_type = TLS_TLS | TLS_TPREL;
8678         }
8679
8680   /* Remove any list entry that won't generate a word in the GOT before
8681      we call merge_got_entries.  Otherwise we risk merging to empty
8682      entries.  */
8683   pgent = &h->got.glist;
8684   while ((gent = *pgent) != NULL)
8685     if (gent->got.refcount > 0)
8686       {
8687         if ((gent->tls_type & TLS_LD) != 0
8688             && !h->def_dynamic)
8689           {
8690             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8691             *pgent = gent->next;
8692           }
8693         else
8694           pgent = &gent->next;
8695       }
8696     else
8697       *pgent = gent->next;
8698
8699   if (!htab->do_multi_toc)
8700     merge_got_entries (&h->got.glist);
8701
8702   for (gent = h->got.glist; gent != NULL; gent = gent->next)
8703     if (!gent->is_indirect)
8704       {
8705         /* Make sure this symbol is output as a dynamic symbol.  */
8706         if (!ensure_undef_dynamic (info, h))
8707           return FALSE;
8708
8709         if (!is_ppc64_elf (gent->owner))
8710           abort ();
8711
8712         allocate_got (h, info, gent);
8713       }
8714
8715   /* If no dynamic sections we can't have dynamic relocs, except for
8716      IFUNCs which are handled even in static executables.  */
8717   if (!htab->elf.dynamic_sections_created
8718       && h->type != STT_GNU_IFUNC)
8719     eh->dyn_relocs = NULL;
8720
8721   /* Discard relocs on undefined symbols that must be local.  */
8722   else if (h->root.type == bfd_link_hash_undefined
8723            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8724     eh->dyn_relocs = NULL;
8725
8726   /* Also discard relocs on undefined weak syms with non-default
8727      visibility, or when dynamic_undefined_weak says so.  */
8728   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8729     eh->dyn_relocs = NULL;
8730
8731   if (eh->dyn_relocs != NULL)
8732     {
8733       struct elf_dyn_relocs *p, **pp;
8734
8735       /* In the shared -Bsymbolic case, discard space allocated for
8736          dynamic pc-relative relocs against symbols which turn out to
8737          be defined in regular objects.  For the normal shared case,
8738          discard space for relocs that have become local due to symbol
8739          visibility changes.  */
8740
8741       if (bfd_link_pic (info))
8742         {
8743           /* Relocs that use pc_count are those that appear on a call
8744              insn, or certain REL relocs (see must_be_dyn_reloc) that
8745              can be generated via assembly.  We want calls to
8746              protected symbols to resolve directly to the function
8747              rather than going via the plt.  If people want function
8748              pointer comparisons to work as expected then they should
8749              avoid writing weird assembly.  */
8750           if (SYMBOL_CALLS_LOCAL (info, h))
8751             {
8752               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8753                 {
8754                   p->count -= p->pc_count;
8755                   p->pc_count = 0;
8756                   if (p->count == 0)
8757                     *pp = p->next;
8758                   else
8759                     pp = &p->next;
8760                 }
8761             }
8762
8763           if (eh->dyn_relocs != NULL)
8764             {
8765               /* Make sure this symbol is output as a dynamic symbol.  */
8766               if (!ensure_undef_dynamic (info, h))
8767                 return FALSE;
8768             }
8769         }
8770       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
8771         {
8772           /* For the non-pic case, discard space for relocs against
8773              symbols which turn out to need copy relocs or are not
8774              dynamic.  */
8775           if (h->dynamic_adjusted
8776               && !h->def_regular
8777               && !ELF_COMMON_DEF_P (h))
8778             {
8779               /* Make sure this symbol is output as a dynamic symbol.  */
8780               if (!ensure_undef_dynamic (info, h))
8781                 return FALSE;
8782
8783               if (h->dynindx == -1)
8784                 eh->dyn_relocs = NULL;
8785             }
8786           else
8787             eh->dyn_relocs = NULL;
8788         }
8789
8790       /* Finally, allocate space.  */
8791       for (p = eh->dyn_relocs; p != NULL; p = p->next)
8792         {
8793           asection *sreloc = elf_section_data (p->sec)->sreloc;
8794           if (eh->elf.type == STT_GNU_IFUNC)
8795             sreloc = htab->elf.irelplt;
8796           sreloc->size += p->count * sizeof (Elf64_External_Rela);
8797         }
8798     }
8799
8800   /* We might need a PLT entry when the symbol
8801      a) is dynamic, or
8802      b) is an ifunc, or
8803      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
8804      d) has plt16 relocs and we are linking statically.  */
8805   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
8806       || h->type == STT_GNU_IFUNC
8807       || (h->needs_plt && h->dynamic_adjusted)
8808       || (h->needs_plt
8809           && h->def_regular
8810           && !htab->elf.dynamic_sections_created
8811           && !htab->can_convert_all_inline_plt
8812           && (((struct ppc_link_hash_entry *) h)->tls_mask
8813               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
8814     {
8815       struct plt_entry *pent;
8816       bfd_boolean doneone = FALSE;
8817       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8818         if (pent->plt.refcount > 0)
8819           {
8820             if (!htab->elf.dynamic_sections_created
8821                 || h->dynindx == -1)
8822               {
8823                 if (h->type == STT_GNU_IFUNC)
8824                   {
8825                     s = htab->elf.iplt;
8826                     pent->plt.offset = s->size;
8827                     s->size += PLT_ENTRY_SIZE (htab);
8828                     s = htab->elf.irelplt;
8829                   }
8830                 else
8831                   {
8832                     s = htab->pltlocal;
8833                     pent->plt.offset = s->size;
8834                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
8835                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
8836                   }
8837               }
8838             else
8839               {
8840                 /* If this is the first .plt entry, make room for the special
8841                    first entry.  */
8842                 s = htab->elf.splt;
8843                 if (s->size == 0)
8844                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
8845
8846                 pent->plt.offset = s->size;
8847
8848                 /* Make room for this entry.  */
8849                 s->size += PLT_ENTRY_SIZE (htab);
8850
8851                 /* Make room for the .glink code.  */
8852                 s = htab->glink;
8853                 if (s->size == 0)
8854                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
8855                 if (htab->opd_abi)
8856                   {
8857                     /* We need bigger stubs past index 32767.  */
8858                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
8859                       s->size += 4;
8860                     s->size += 2*4;
8861                   }
8862                 else
8863                   s->size += 4;
8864
8865                 /* We also need to make an entry in the .rela.plt section.  */
8866                 s = htab->elf.srelplt;
8867               }
8868             if (s != NULL)
8869               s->size += sizeof (Elf64_External_Rela);
8870             doneone = TRUE;
8871           }
8872         else
8873           pent->plt.offset = (bfd_vma) -1;
8874       if (!doneone)
8875         {
8876           h->plt.plist = NULL;
8877           h->needs_plt = 0;
8878         }
8879     }
8880   else
8881     {
8882       h->plt.plist = NULL;
8883       h->needs_plt = 0;
8884     }
8885
8886   return TRUE;
8887 }
8888
8889 #define PPC_LO(v) ((v) & 0xffff)
8890 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8891 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8892
8893 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
8894    to set up space for global entry stubs.  These are put in glink,
8895    after the branch table.  */
8896
8897 static bfd_boolean
8898 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
8899 {
8900   struct bfd_link_info *info;
8901   struct ppc_link_hash_table *htab;
8902   struct plt_entry *pent;
8903   asection *s, *plt;
8904
8905   if (h->root.type == bfd_link_hash_indirect)
8906     return TRUE;
8907
8908   if (!h->pointer_equality_needed)
8909     return TRUE;
8910
8911   if (h->def_regular)
8912     return TRUE;
8913
8914   info = inf;
8915   htab = ppc_hash_table (info);
8916   if (htab == NULL)
8917     return FALSE;
8918
8919   s = htab->global_entry;
8920   plt = htab->elf.splt;
8921   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8922     if (pent->plt.offset != (bfd_vma) -1
8923         && pent->addend == 0)
8924       {
8925         /* For ELFv2, if this symbol is not defined in a regular file
8926            and we are not generating a shared library or pie, then we
8927            need to define the symbol in the executable on a call stub.
8928            This is to avoid text relocations.  */
8929         bfd_vma off, stub_align, stub_off, stub_size;
8930         unsigned int align_power;
8931
8932         stub_size = 16;
8933         stub_off = s->size;
8934         if (htab->params->plt_stub_align >= 0)
8935           align_power = htab->params->plt_stub_align;
8936         else
8937           align_power = -htab->params->plt_stub_align;
8938         /* Setting section alignment is delayed until we know it is
8939            non-empty.  Otherwise the .text output section will be
8940            aligned at least to plt_stub_align even when no global
8941            entry stubs are needed.  */
8942         if (s->alignment_power < align_power)
8943           s->alignment_power = align_power;
8944         stub_align = (bfd_vma) 1 << align_power;
8945         if (htab->params->plt_stub_align >= 0
8946             || ((((stub_off + stub_size - 1) & -stub_align)
8947                  - (stub_off & -stub_align))
8948                 > ((stub_size - 1) & -stub_align)))
8949           stub_off = (stub_off + stub_align - 1) & -stub_align;
8950         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
8951         off -= stub_off + s->output_offset + s->output_section->vma;
8952         /* Note that for --plt-stub-align negative we have a possible
8953            dependency between stub offset and size.  Break that
8954            dependency by assuming the max stub size when calculating
8955            the stub offset.  */
8956         if (PPC_HA (off) == 0)
8957           stub_size -= 4;
8958         h->root.type = bfd_link_hash_defined;
8959         h->root.u.def.section = s;
8960         h->root.u.def.value = stub_off;
8961         s->size = stub_off + stub_size;
8962         break;
8963       }
8964   return TRUE;
8965 }
8966
8967 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
8968    read-only sections.  */
8969
8970 static bfd_boolean
8971 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
8972 {
8973   asection *sec;
8974
8975   if (h->root.type == bfd_link_hash_indirect)
8976     return TRUE;
8977
8978   sec = readonly_dynrelocs (h);
8979   if (sec != NULL)
8980     {
8981       struct bfd_link_info *info = (struct bfd_link_info *) inf;
8982
8983       info->flags |= DF_TEXTREL;
8984       info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
8985                                 " in read-only section `%pA'\n"),
8986                               sec->owner, h->root.root.string, sec);
8987
8988       /* Not an error, just cut short the traversal.  */
8989       return FALSE;
8990     }
8991   return TRUE;
8992 }
8993
8994 /* Set the sizes of the dynamic sections.  */
8995
8996 static bfd_boolean
8997 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
8998                                  struct bfd_link_info *info)
8999 {
9000   struct ppc_link_hash_table *htab;
9001   bfd *dynobj;
9002   asection *s;
9003   bfd_boolean relocs;
9004   bfd *ibfd;
9005   struct got_entry *first_tlsld;
9006
9007   htab = ppc_hash_table (info);
9008   if (htab == NULL)
9009     return FALSE;
9010
9011   dynobj = htab->elf.dynobj;
9012   if (dynobj == NULL)
9013     abort ();
9014
9015   if (htab->elf.dynamic_sections_created)
9016     {
9017       /* Set the contents of the .interp section to the interpreter.  */
9018       if (bfd_link_executable (info) && !info->nointerp)
9019         {
9020           s = bfd_get_linker_section (dynobj, ".interp");
9021           if (s == NULL)
9022             abort ();
9023           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9024           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9025         }
9026     }
9027
9028   /* Set up .got offsets for local syms, and space for local dynamic
9029      relocs.  */
9030   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9031     {
9032       struct got_entry **lgot_ents;
9033       struct got_entry **end_lgot_ents;
9034       struct plt_entry **local_plt;
9035       struct plt_entry **end_local_plt;
9036       unsigned char *lgot_masks;
9037       bfd_size_type locsymcount;
9038       Elf_Internal_Shdr *symtab_hdr;
9039
9040       if (!is_ppc64_elf (ibfd))
9041         continue;
9042
9043       for (s = ibfd->sections; s != NULL; s = s->next)
9044         {
9045           struct ppc_dyn_relocs *p;
9046
9047           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9048             {
9049               if (!bfd_is_abs_section (p->sec)
9050                   && bfd_is_abs_section (p->sec->output_section))
9051                 {
9052                   /* Input section has been discarded, either because
9053                      it is a copy of a linkonce section or due to
9054                      linker script /DISCARD/, so we'll be discarding
9055                      the relocs too.  */
9056                 }
9057               else if (p->count != 0)
9058                 {
9059                   asection *srel = elf_section_data (p->sec)->sreloc;
9060                   if (p->ifunc)
9061                     srel = htab->elf.irelplt;
9062                   srel->size += p->count * sizeof (Elf64_External_Rela);
9063                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9064                     info->flags |= DF_TEXTREL;
9065                 }
9066             }
9067         }
9068
9069       lgot_ents = elf_local_got_ents (ibfd);
9070       if (!lgot_ents)
9071         continue;
9072
9073       symtab_hdr = &elf_symtab_hdr (ibfd);
9074       locsymcount = symtab_hdr->sh_info;
9075       end_lgot_ents = lgot_ents + locsymcount;
9076       local_plt = (struct plt_entry **) end_lgot_ents;
9077       end_local_plt = local_plt + locsymcount;
9078       lgot_masks = (unsigned char *) end_local_plt;
9079       s = ppc64_elf_tdata (ibfd)->got;
9080       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9081         {
9082           struct got_entry **pent, *ent;
9083
9084           pent = lgot_ents;
9085           while ((ent = *pent) != NULL)
9086             if (ent->got.refcount > 0)
9087               {
9088                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9089                   {
9090                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
9091                     *pent = ent->next;
9092                   }
9093                 else
9094                   {
9095                     unsigned int ent_size = 8;
9096                     unsigned int rel_size = sizeof (Elf64_External_Rela);
9097
9098                     ent->got.offset = s->size;
9099                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9100                       {
9101                         ent_size *= 2;
9102                         rel_size *= 2;
9103                       }
9104                     s->size += ent_size;
9105                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9106                       {
9107                         htab->elf.irelplt->size += rel_size;
9108                         htab->got_reli_size += rel_size;
9109                       }
9110                     else if (bfd_link_pic (info)
9111                              && !((ent->tls_type & TLS_TPREL) != 0
9112                                   && bfd_link_executable (info)))
9113                       {
9114                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9115                         srel->size += rel_size;
9116                       }
9117                     pent = &ent->next;
9118                   }
9119               }
9120             else
9121               *pent = ent->next;
9122         }
9123
9124       /* Allocate space for plt calls to local syms.  */
9125       lgot_masks = (unsigned char *) end_local_plt;
9126       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
9127         {
9128           struct plt_entry *ent;
9129
9130           for (ent = *local_plt; ent != NULL; ent = ent->next)
9131             if (ent->plt.refcount > 0)
9132               {
9133                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
9134                   {
9135                     s = htab->elf.iplt;
9136                     ent->plt.offset = s->size;
9137                     s->size += PLT_ENTRY_SIZE (htab);
9138                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
9139                   }
9140                 else if (htab->can_convert_all_inline_plt
9141                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
9142                   ent->plt.offset = (bfd_vma) -1;
9143                 else
9144                   {
9145                     s = htab->pltlocal;
9146                     ent->plt.offset = s->size;
9147                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9148                     if (bfd_link_pic (info))
9149                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
9150                   }
9151               }
9152             else
9153               ent->plt.offset = (bfd_vma) -1;
9154         }
9155     }
9156
9157   /* Allocate global sym .plt and .got entries, and space for global
9158      sym dynamic relocs.  */
9159   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9160
9161   if (!htab->opd_abi && !bfd_link_pic (info))
9162     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
9163
9164   first_tlsld = NULL;
9165   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9166     {
9167       struct got_entry *ent;
9168
9169       if (!is_ppc64_elf (ibfd))
9170         continue;
9171
9172       ent = ppc64_tlsld_got (ibfd);
9173       if (ent->got.refcount > 0)
9174         {
9175           if (!htab->do_multi_toc && first_tlsld != NULL)
9176             {
9177               ent->is_indirect = TRUE;
9178               ent->got.ent = first_tlsld;
9179             }
9180           else
9181             {
9182               if (first_tlsld == NULL)
9183                 first_tlsld = ent;
9184               s = ppc64_elf_tdata (ibfd)->got;
9185               ent->got.offset = s->size;
9186               ent->owner = ibfd;
9187               s->size += 16;
9188               if (bfd_link_pic (info))
9189                 {
9190                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9191                   srel->size += sizeof (Elf64_External_Rela);
9192                 }
9193             }
9194         }
9195       else
9196         ent->got.offset = (bfd_vma) -1;
9197     }
9198
9199   /* We now have determined the sizes of the various dynamic sections.
9200      Allocate memory for them.  */
9201   relocs = FALSE;
9202   for (s = dynobj->sections; s != NULL; s = s->next)
9203     {
9204       if ((s->flags & SEC_LINKER_CREATED) == 0)
9205         continue;
9206
9207       if (s == htab->brlt || s == htab->relbrlt)
9208         /* These haven't been allocated yet;  don't strip.  */
9209         continue;
9210       else if (s == htab->elf.sgot
9211                || s == htab->elf.splt
9212                || s == htab->elf.iplt
9213                || s == htab->pltlocal
9214                || s == htab->glink
9215                || s == htab->global_entry
9216                || s == htab->elf.sdynbss
9217                || s == htab->elf.sdynrelro)
9218         {
9219           /* Strip this section if we don't need it; see the
9220              comment below.  */
9221         }
9222       else if (s == htab->glink_eh_frame)
9223         {
9224           if (!bfd_is_abs_section (s->output_section))
9225             /* Not sized yet.  */
9226             continue;
9227         }
9228       else if (CONST_STRNEQ (s->name, ".rela"))
9229         {
9230           if (s->size != 0)
9231             {
9232               if (s != htab->elf.srelplt)
9233                 relocs = TRUE;
9234
9235               /* We use the reloc_count field as a counter if we need
9236                  to copy relocs into the output file.  */
9237               s->reloc_count = 0;
9238             }
9239         }
9240       else
9241         {
9242           /* It's not one of our sections, so don't allocate space.  */
9243           continue;
9244         }
9245
9246       if (s->size == 0)
9247         {
9248           /* If we don't need this section, strip it from the
9249              output file.  This is mostly to handle .rela.bss and
9250              .rela.plt.  We must create both sections in
9251              create_dynamic_sections, because they must be created
9252              before the linker maps input sections to output
9253              sections.  The linker does that before
9254              adjust_dynamic_symbol is called, and it is that
9255              function which decides whether anything needs to go
9256              into these sections.  */
9257           s->flags |= SEC_EXCLUDE;
9258           continue;
9259         }
9260
9261       if (bfd_is_abs_section (s->output_section))
9262         _bfd_error_handler (_("warning: discarding dynamic section %s"),
9263                             s->name);
9264
9265       if ((s->flags & SEC_HAS_CONTENTS) == 0)
9266         continue;
9267
9268       /* Allocate memory for the section contents.  We use bfd_zalloc
9269          here in case unused entries are not reclaimed before the
9270          section's contents are written out.  This should not happen,
9271          but this way if it does we get a R_PPC64_NONE reloc in .rela
9272          sections instead of garbage.
9273          We also rely on the section contents being zero when writing
9274          the GOT and .dynrelro.  */
9275       s->contents = bfd_zalloc (dynobj, s->size);
9276       if (s->contents == NULL)
9277         return FALSE;
9278     }
9279
9280   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9281     {
9282       if (!is_ppc64_elf (ibfd))
9283         continue;
9284
9285       s = ppc64_elf_tdata (ibfd)->got;
9286       if (s != NULL && s != htab->elf.sgot)
9287         {
9288           if (s->size == 0)
9289             s->flags |= SEC_EXCLUDE;
9290           else
9291             {
9292               s->contents = bfd_zalloc (ibfd, s->size);
9293               if (s->contents == NULL)
9294                 return FALSE;
9295             }
9296         }
9297       s = ppc64_elf_tdata (ibfd)->relgot;
9298       if (s != NULL)
9299         {
9300           if (s->size == 0)
9301             s->flags |= SEC_EXCLUDE;
9302           else
9303             {
9304               s->contents = bfd_zalloc (ibfd, s->size);
9305               if (s->contents == NULL)
9306                 return FALSE;
9307               relocs = TRUE;
9308               s->reloc_count = 0;
9309             }
9310         }
9311     }
9312
9313   if (htab->elf.dynamic_sections_created)
9314     {
9315       bfd_boolean tls_opt;
9316
9317       /* Add some entries to the .dynamic section.  We fill in the
9318          values later, in ppc64_elf_finish_dynamic_sections, but we
9319          must add the entries now so that we get the correct size for
9320          the .dynamic section.  The DT_DEBUG entry is filled in by the
9321          dynamic linker and used by the debugger.  */
9322 #define add_dynamic_entry(TAG, VAL) \
9323   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9324
9325       if (bfd_link_executable (info))
9326         {
9327           if (!add_dynamic_entry (DT_DEBUG, 0))
9328             return FALSE;
9329         }
9330
9331       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
9332         {
9333           if (!add_dynamic_entry (DT_PLTGOT, 0)
9334               || !add_dynamic_entry (DT_PLTRELSZ, 0)
9335               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9336               || !add_dynamic_entry (DT_JMPREL, 0)
9337               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9338             return FALSE;
9339         }
9340
9341       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9342         {
9343           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9344               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9345             return FALSE;
9346         }
9347
9348       tls_opt = (htab->params->tls_get_addr_opt
9349                  && htab->tls_get_addr_fd != NULL
9350                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
9351       if (tls_opt || !htab->opd_abi)
9352         {
9353           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
9354             return FALSE;
9355         }
9356
9357       if (relocs)
9358         {
9359           if (!add_dynamic_entry (DT_RELA, 0)
9360               || !add_dynamic_entry (DT_RELASZ, 0)
9361               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9362             return FALSE;
9363
9364           /* If any dynamic relocs apply to a read-only section,
9365              then we need a DT_TEXTREL entry.  */
9366           if ((info->flags & DF_TEXTREL) == 0)
9367             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
9368
9369           if ((info->flags & DF_TEXTREL) != 0)
9370             {
9371               if (!add_dynamic_entry (DT_TEXTREL, 0))
9372                 return FALSE;
9373             }
9374         }
9375     }
9376 #undef add_dynamic_entry
9377
9378   return TRUE;
9379 }
9380
9381 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
9382
9383 static bfd_boolean
9384 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
9385 {
9386   if (h->plt.plist != NULL
9387       && !h->def_regular
9388       && !h->pointer_equality_needed)
9389     return FALSE;
9390
9391   return _bfd_elf_hash_symbol (h);
9392 }
9393
9394 /* Determine the type of stub needed, if any, for a call.  */
9395
9396 static inline enum ppc_stub_type
9397 ppc_type_of_stub (asection *input_sec,
9398                   const Elf_Internal_Rela *rel,
9399                   struct ppc_link_hash_entry **hash,
9400                   struct plt_entry **plt_ent,
9401                   bfd_vma destination,
9402                   unsigned long local_off)
9403 {
9404   struct ppc_link_hash_entry *h = *hash;
9405   bfd_vma location;
9406   bfd_vma branch_offset;
9407   bfd_vma max_branch_offset;
9408   enum elf_ppc64_reloc_type r_type;
9409
9410   if (h != NULL)
9411     {
9412       struct plt_entry *ent;
9413       struct ppc_link_hash_entry *fdh = h;
9414       if (h->oh != NULL
9415           && h->oh->is_func_descriptor)
9416         {
9417           fdh = ppc_follow_link (h->oh);
9418           *hash = fdh;
9419         }
9420
9421       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9422         if (ent->addend == rel->r_addend
9423             && ent->plt.offset != (bfd_vma) -1)
9424           {
9425             *plt_ent = ent;
9426             return ppc_stub_plt_call;
9427           }
9428
9429       /* Here, we know we don't have a plt entry.  If we don't have a
9430          either a defined function descriptor or a defined entry symbol
9431          in a regular object file, then it is pointless trying to make
9432          any other type of stub.  */
9433       if (!is_static_defined (&fdh->elf)
9434           && !is_static_defined (&h->elf))
9435         return ppc_stub_none;
9436     }
9437   else if (elf_local_got_ents (input_sec->owner) != NULL)
9438     {
9439       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9440       struct plt_entry **local_plt = (struct plt_entry **)
9441         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9442       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9443
9444       if (local_plt[r_symndx] != NULL)
9445         {
9446           struct plt_entry *ent;
9447
9448           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9449             if (ent->addend == rel->r_addend
9450                 && ent->plt.offset != (bfd_vma) -1)
9451               {
9452                 *plt_ent = ent;
9453                 return ppc_stub_plt_call;
9454               }
9455         }
9456     }
9457
9458   /* Determine where the call point is.  */
9459   location = (input_sec->output_offset
9460               + input_sec->output_section->vma
9461               + rel->r_offset);
9462
9463   branch_offset = destination - location;
9464   r_type = ELF64_R_TYPE (rel->r_info);
9465
9466   /* Determine if a long branch stub is needed.  */
9467   max_branch_offset = 1 << 25;
9468   if (r_type == R_PPC64_REL14
9469       || r_type == R_PPC64_REL14_BRTAKEN
9470       || r_type == R_PPC64_REL14_BRNTAKEN)
9471     max_branch_offset = 1 << 15;
9472
9473   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9474     /* We need a stub.  Figure out whether a long_branch or plt_branch
9475        is needed later.  */
9476     return ppc_stub_long_branch;
9477
9478   return ppc_stub_none;
9479 }
9480
9481 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
9482    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
9483    .    mflr    %r12
9484    .    bcl     20,31,1f
9485    .1:  mflr    %r11
9486    .    mtlr    %r12
9487    .    lis     %r12,xxx-1b@highest
9488    .    ori     %r12,%r12,xxx-1b@higher
9489    .    sldi    %r12,%r12,32
9490    .    oris    %r12,%r12,xxx-1b@high
9491    .    ori     %r12,%r12,xxx-1b@l
9492    .    add/ldx %r12,%r11,%r12  */
9493
9494 static bfd_byte *
9495 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
9496 {
9497   bfd_put_32 (abfd, MFLR_R12, p);
9498   p += 4;
9499   bfd_put_32 (abfd, BCL_20_31, p);
9500   p += 4;
9501   bfd_put_32 (abfd, MFLR_R11, p);
9502   p += 4;
9503   bfd_put_32 (abfd, MTLR_R12, p);
9504   p += 4;
9505   if (off + 0x8000 < 0x10000)
9506     {
9507       if (load)
9508         bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
9509       else
9510         bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
9511       p += 4;
9512     }
9513   else if (off + 0x80008000ULL < 0x100000000ULL)
9514     {
9515       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
9516       p += 4;
9517       if (load)
9518         bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
9519       else
9520         bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
9521       p += 4;
9522     }
9523   else
9524     {
9525       if (off + 0x800000000000ULL < 0x1000000000000ULL)
9526         {
9527           bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
9528           p += 4;
9529         }
9530       else
9531         {
9532           bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
9533           p += 4;
9534           if (((off >> 32) & 0xffff) != 0)
9535             {
9536               bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
9537               p += 4;
9538             }
9539         }
9540       if (((off >> 32) & 0xffffffffULL) != 0)
9541         {
9542           bfd_put_32 (abfd, SLDI_R12_R12_32, p);
9543           p += 4;
9544         }
9545       if (PPC_HI (off) != 0)
9546         {
9547           bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
9548           p += 4;
9549         }
9550       if (PPC_LO (off) != 0)
9551         {
9552           bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
9553           p += 4;
9554         }
9555       if (load)
9556         bfd_put_32 (abfd, LDX_R12_R11_R12, p);
9557       else
9558         bfd_put_32 (abfd, ADD_R12_R11_R12, p);
9559       p += 4;
9560     }
9561   return p;
9562 }
9563
9564 static unsigned int
9565 size_offset (bfd_vma off)
9566 {
9567   unsigned int size;
9568   if (off + 0x8000 < 0x10000)
9569     size = 4;
9570   else if (off + 0x80008000ULL < 0x100000000ULL)
9571     size = 8;
9572   else
9573     {
9574       if (off + 0x800000000000ULL < 0x1000000000000ULL)
9575         size = 4;
9576       else
9577         {
9578           size = 4;
9579           if (((off >> 32) & 0xffff) != 0)
9580             size += 4;
9581         }
9582       if (((off >> 32) & 0xffffffffULL) != 0)
9583         size += 4;
9584       if (PPC_HI (off) != 0)
9585         size += 4;
9586       if (PPC_LO (off) != 0)
9587         size += 4;
9588       size += 4;
9589     }
9590   return size + 16;
9591 }
9592
9593 /* Emit .eh_frame opcode to advance pc by DELTA.  */
9594
9595 static bfd_byte *
9596 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
9597 {
9598   delta /= 4;
9599   if (delta < 64)
9600     *eh++ = DW_CFA_advance_loc + delta;
9601   else if (delta < 256)
9602     {
9603       *eh++ = DW_CFA_advance_loc1;
9604       *eh++ = delta;
9605     }
9606   else if (delta < 65536)
9607     {
9608       *eh++ = DW_CFA_advance_loc2;
9609       bfd_put_16 (abfd, delta, eh);
9610       eh += 2;
9611     }
9612   else
9613     {
9614       *eh++ = DW_CFA_advance_loc4;
9615       bfd_put_32 (abfd, delta, eh);
9616       eh += 4;
9617     }
9618   return eh;
9619 }
9620
9621 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
9622
9623 static unsigned int
9624 eh_advance_size (unsigned int delta)
9625 {
9626   if (delta < 64 * 4)
9627     /* DW_CFA_advance_loc+[1..63].  */
9628     return 1;
9629   if (delta < 256 * 4)
9630     /* DW_CFA_advance_loc1, byte.  */
9631     return 2;
9632   if (delta < 65536 * 4)
9633     /* DW_CFA_advance_loc2, 2 bytes.  */
9634     return 3;
9635   /* DW_CFA_advance_loc4, 4 bytes.  */
9636   return 5;
9637 }
9638
9639 /* With power7 weakly ordered memory model, it is possible for ld.so
9640    to update a plt entry in one thread and have another thread see a
9641    stale zero toc entry.  To avoid this we need some sort of acquire
9642    barrier in the call stub.  One solution is to make the load of the
9643    toc word seem to appear to depend on the load of the function entry
9644    word.  Another solution is to test for r2 being zero, and branch to
9645    the appropriate glink entry if so.
9646
9647    .    fake dep barrier        compare
9648    .    ld 12,xxx(2)            ld 12,xxx(2)
9649    .    mtctr 12                mtctr 12
9650    .    xor 11,12,12            ld 2,xxx+8(2)
9651    .    add 2,2,11              cmpldi 2,0
9652    .    ld 2,xxx+8(2)           bnectr+
9653    .    bctr                    b <glink_entry>
9654
9655    The solution involving the compare turns out to be faster, so
9656    that's what we use unless the branch won't reach.  */
9657
9658 #define ALWAYS_USE_FAKE_DEP 0
9659 #define ALWAYS_EMIT_R2SAVE 0
9660
9661 static inline unsigned int
9662 plt_stub_size (struct ppc_link_hash_table *htab,
9663                struct ppc_stub_hash_entry *stub_entry,
9664                bfd_vma off)
9665 {
9666   unsigned size;
9667
9668   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
9669     {
9670       size = 8 + size_offset (off - 8);
9671       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
9672         size += 4;
9673       return size;
9674     }
9675
9676   size = 12;
9677   if (ALWAYS_EMIT_R2SAVE
9678       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9679     size += 4;
9680   if (PPC_HA (off) != 0)
9681     size += 4;
9682   if (htab->opd_abi)
9683     {
9684       size += 4;
9685       if (htab->params->plt_static_chain)
9686         size += 4;
9687       if (htab->params->plt_thread_safe
9688           && htab->elf.dynamic_sections_created
9689           && stub_entry->h != NULL
9690           && stub_entry->h->elf.dynindx != -1)
9691         size += 8;
9692       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
9693         size += 4;
9694     }
9695   if (stub_entry->h != NULL
9696       && (stub_entry->h == htab->tls_get_addr_fd
9697           || stub_entry->h == htab->tls_get_addr)
9698       && htab->params->tls_get_addr_opt)
9699     {
9700       size += 7 * 4;
9701       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
9702         size += 6 * 4;
9703     }
9704   return size;
9705 }
9706
9707 /* Depending on the sign of plt_stub_align:
9708    If positive, return the padding to align to a 2**plt_stub_align
9709    boundary.
9710    If negative, if this stub would cross fewer 2**plt_stub_align
9711    boundaries if we align, then return the padding needed to do so.  */
9712
9713 static inline unsigned int
9714 plt_stub_pad (struct ppc_link_hash_table *htab,
9715               struct ppc_stub_hash_entry *stub_entry,
9716               bfd_vma plt_off)
9717 {
9718   int stub_align;
9719   unsigned stub_size;
9720   bfd_vma stub_off = stub_entry->group->stub_sec->size;
9721
9722   if (htab->params->plt_stub_align >= 0)
9723     {
9724       stub_align = 1 << htab->params->plt_stub_align;
9725       if ((stub_off & (stub_align - 1)) != 0)
9726         return stub_align - (stub_off & (stub_align - 1));
9727       return 0;
9728     }
9729
9730   stub_align = 1 << -htab->params->plt_stub_align;
9731   stub_size = plt_stub_size (htab, stub_entry, plt_off);
9732   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9733       > ((stub_size - 1) & -stub_align))
9734     return stub_align - (stub_off & (stub_align - 1));
9735   return 0;
9736 }
9737
9738 /* Build a .plt call stub.  */
9739
9740 static inline bfd_byte *
9741 build_plt_stub (struct ppc_link_hash_table *htab,
9742                 struct ppc_stub_hash_entry *stub_entry,
9743                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9744 {
9745   bfd *obfd = htab->params->stub_bfd;
9746   bfd_boolean plt_load_toc = htab->opd_abi;
9747   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
9748   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
9749                                  && htab->elf.dynamic_sections_created
9750                                  && stub_entry->h != NULL
9751                                  && stub_entry->h->elf.dynindx != -1);
9752   bfd_boolean use_fake_dep = plt_thread_safe;
9753   bfd_vma cmp_branch_off = 0;
9754
9755   if (!ALWAYS_USE_FAKE_DEP
9756       && plt_load_toc
9757       && plt_thread_safe
9758       && !((stub_entry->h == htab->tls_get_addr_fd
9759             || stub_entry->h == htab->tls_get_addr)
9760            && htab->params->tls_get_addr_opt))
9761     {
9762       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9763       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
9764                           / PLT_ENTRY_SIZE (htab));
9765       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
9766       bfd_vma to, from;
9767
9768       if (pltindex > 32768)
9769         glinkoff += (pltindex - 32768) * 4;
9770       to = (glinkoff
9771             + htab->glink->output_offset
9772             + htab->glink->output_section->vma);
9773       from = (p - stub_entry->group->stub_sec->contents
9774               + 4 * (ALWAYS_EMIT_R2SAVE
9775                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9776               + 4 * (PPC_HA (offset) != 0)
9777               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
9778                      != PPC_HA (offset))
9779               + 4 * (plt_static_chain != 0)
9780               + 20
9781               + stub_entry->group->stub_sec->output_offset
9782               + stub_entry->group->stub_sec->output_section->vma);
9783       cmp_branch_off = to - from;
9784       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
9785     }
9786
9787   if (PPC_HA (offset) != 0)
9788     {
9789       if (r != NULL)
9790         {
9791           if (ALWAYS_EMIT_R2SAVE
9792               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9793             r[0].r_offset += 4;
9794           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9795           r[1].r_offset = r[0].r_offset + 4;
9796           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9797           r[1].r_addend = r[0].r_addend;
9798           if (plt_load_toc)
9799             {
9800               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9801                 {
9802                   r[2].r_offset = r[1].r_offset + 4;
9803                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9804                   r[2].r_addend = r[0].r_addend;
9805                 }
9806               else
9807                 {
9808                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
9809                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9810                   r[2].r_addend = r[0].r_addend + 8;
9811                   if (plt_static_chain)
9812                     {
9813                       r[3].r_offset = r[2].r_offset + 4;
9814                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9815                       r[3].r_addend = r[0].r_addend + 16;
9816                     }
9817                 }
9818             }
9819         }
9820       if (ALWAYS_EMIT_R2SAVE
9821           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9822         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
9823       if (plt_load_toc)
9824         {
9825           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
9826           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
9827         }
9828       else
9829         {
9830           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9831           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
9832         }
9833       if (plt_load_toc
9834           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9835         {
9836           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
9837           offset = 0;
9838         }
9839       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
9840       if (plt_load_toc)
9841         {
9842           if (use_fake_dep)
9843             {
9844               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
9845               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
9846             }
9847           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9848           if (plt_static_chain)
9849             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
9850         }
9851     }
9852   else
9853     {
9854       if (r != NULL)
9855         {
9856           if (ALWAYS_EMIT_R2SAVE
9857               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9858             r[0].r_offset += 4;
9859           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9860           if (plt_load_toc)
9861             {
9862               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9863                 {
9864                   r[1].r_offset = r[0].r_offset + 4;
9865                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9866                   r[1].r_addend = r[0].r_addend;
9867                 }
9868               else
9869                 {
9870                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
9871                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9872                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9873                   if (plt_static_chain)
9874                     {
9875                       r[2].r_offset = r[1].r_offset + 4;
9876                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9877                       r[2].r_addend = r[0].r_addend + 8;
9878                     }
9879                 }
9880             }
9881         }
9882       if (ALWAYS_EMIT_R2SAVE
9883           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
9884         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
9885       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
9886       if (plt_load_toc
9887           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9888         {
9889           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
9890           offset = 0;
9891         }
9892       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
9893       if (plt_load_toc)
9894         {
9895           if (use_fake_dep)
9896             {
9897               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
9898               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
9899             }
9900           if (plt_static_chain)
9901             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9902           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9903         }
9904     }
9905   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
9906     {
9907       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
9908       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
9909       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
9910     }
9911   else
9912     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
9913   return p;
9914 }
9915
9916 /* Build a special .plt call stub for __tls_get_addr.  */
9917
9918 #define LD_R11_0R3      0xe9630000
9919 #define LD_R12_0R3      0xe9830000
9920 #define MR_R0_R3        0x7c601b78
9921 #define CMPDI_R11_0     0x2c2b0000
9922 #define ADD_R3_R12_R13  0x7c6c6a14
9923 #define BEQLR           0x4d820020
9924 #define MR_R3_R0        0x7c030378
9925 #define STD_R11_0R1     0xf9610000
9926 #define BCTRL           0x4e800421
9927 #define LD_R11_0R1      0xe9610000
9928 #define MTLR_R11        0x7d6803a6
9929
9930 static inline bfd_byte *
9931 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
9932                          struct ppc_stub_hash_entry *stub_entry,
9933                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9934 {
9935   bfd *obfd = htab->params->stub_bfd;
9936   bfd_byte *loc = p;
9937
9938   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
9939   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
9940   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
9941   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
9942   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
9943   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
9944   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
9945   if (r != NULL)
9946     r[0].r_offset += 7 * 4;
9947   if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
9948     return build_plt_stub (htab, stub_entry, p, offset, r);
9949
9950   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
9951   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9952
9953   if (r != NULL)
9954     r[0].r_offset += 2 * 4;
9955   p = build_plt_stub (htab, stub_entry, p, offset, r);
9956   bfd_put_32 (obfd, BCTRL, p - 4);
9957
9958   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
9959   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
9960   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
9961   bfd_put_32 (obfd, BLR, p),                    p += 4;
9962
9963   if (htab->glink_eh_frame != NULL
9964       && htab->glink_eh_frame->size != 0)
9965     {
9966       bfd_byte *base, *eh;
9967       unsigned int lr_used, delta;
9968
9969       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
9970       eh = base + stub_entry->group->eh_size;
9971       lr_used = stub_entry->stub_offset + (p - 20 - loc);
9972       delta = lr_used - stub_entry->group->lr_restore;
9973       stub_entry->group->lr_restore = lr_used + 16;
9974       eh = eh_advance (htab->elf.dynobj, eh, delta);
9975       *eh++ = DW_CFA_offset_extended_sf;
9976       *eh++ = 65;
9977       *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
9978       *eh++ = DW_CFA_advance_loc + 4;
9979       *eh++ = DW_CFA_restore_extended;
9980       *eh++ = 65;
9981       stub_entry->group->eh_size = eh - base;
9982     }
9983   return p;
9984 }
9985
9986 static Elf_Internal_Rela *
9987 get_relocs (asection *sec, int count)
9988 {
9989   Elf_Internal_Rela *relocs;
9990   struct bfd_elf_section_data *elfsec_data;
9991
9992   elfsec_data = elf_section_data (sec);
9993   relocs = elfsec_data->relocs;
9994   if (relocs == NULL)
9995     {
9996       bfd_size_type relsize;
9997       relsize = sec->reloc_count * sizeof (*relocs);
9998       relocs = bfd_alloc (sec->owner, relsize);
9999       if (relocs == NULL)
10000         return NULL;
10001       elfsec_data->relocs = relocs;
10002       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10003                                           sizeof (Elf_Internal_Shdr));
10004       if (elfsec_data->rela.hdr == NULL)
10005         return NULL;
10006       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10007                                         * sizeof (Elf64_External_Rela));
10008       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10009       sec->reloc_count = 0;
10010     }
10011   relocs += sec->reloc_count;
10012   sec->reloc_count += count;
10013   return relocs;
10014 }
10015
10016 static bfd_vma
10017 get_r2off (struct bfd_link_info *info,
10018            struct ppc_stub_hash_entry *stub_entry)
10019 {
10020   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10021   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10022
10023   if (r2off == 0)
10024     {
10025       /* Support linking -R objects.  Get the toc pointer from the
10026          opd entry.  */
10027       char buf[8];
10028       if (!htab->opd_abi)
10029         return r2off;
10030       asection *opd = stub_entry->h->elf.root.u.def.section;
10031       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10032
10033       if (strcmp (opd->name, ".opd") != 0
10034           || opd->reloc_count != 0)
10035         {
10036           info->callbacks->einfo
10037             (_("%P: cannot find opd entry toc for `%pT'\n"),
10038              stub_entry->h->elf.root.root.string);
10039           bfd_set_error (bfd_error_bad_value);
10040           return (bfd_vma) -1;
10041         }
10042       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10043         return (bfd_vma) -1;
10044       r2off = bfd_get_64 (opd->owner, buf);
10045       r2off -= elf_gp (info->output_bfd);
10046     }
10047   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10048   return r2off;
10049 }
10050
10051 static bfd_boolean
10052 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10053 {
10054   struct ppc_stub_hash_entry *stub_entry;
10055   struct ppc_branch_hash_entry *br_entry;
10056   struct bfd_link_info *info;
10057   struct ppc_link_hash_table *htab;
10058   bfd_byte *loc;
10059   bfd_byte *p;
10060   bfd_vma targ, off;
10061   Elf_Internal_Rela *r;
10062   asection *plt;
10063
10064   /* Massage our args to the form they really have.  */
10065   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10066   info = in_arg;
10067
10068   htab = ppc_hash_table (info);
10069   if (htab == NULL)
10070     return FALSE;
10071
10072   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
10073   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10074
10075   htab->stub_count[stub_entry->stub_type - 1] += 1;
10076   switch (stub_entry->stub_type)
10077     {
10078     case ppc_stub_long_branch:
10079     case ppc_stub_long_branch_r2off:
10080       /* Branches are relative.  This is where we are going to.  */
10081       targ = (stub_entry->target_value
10082               + stub_entry->target_section->output_offset
10083               + stub_entry->target_section->output_section->vma);
10084       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10085
10086       /* And this is where we are coming from.  */
10087       off = (stub_entry->stub_offset
10088              + stub_entry->group->stub_sec->output_offset
10089              + stub_entry->group->stub_sec->output_section->vma);
10090       off = targ - off;
10091
10092       p = loc;
10093       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10094         {
10095           bfd_vma r2off = get_r2off (info, stub_entry);
10096
10097           if (r2off == (bfd_vma) -1)
10098             {
10099               htab->stub_error = TRUE;
10100               return FALSE;
10101             }
10102           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10103           p += 4;
10104           if (PPC_HA (r2off) != 0)
10105             {
10106               bfd_put_32 (htab->params->stub_bfd,
10107                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10108               p += 4;
10109             }
10110           if (PPC_LO (r2off) != 0)
10111             {
10112               bfd_put_32 (htab->params->stub_bfd,
10113                           ADDI_R2_R2 | PPC_LO (r2off), p);
10114               p += 4;
10115             }
10116           off -= p - loc;
10117         }
10118       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10119       p += 4;
10120
10121       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10122         {
10123           _bfd_error_handler
10124             (_("long branch stub `%s' offset overflow"),
10125              stub_entry->root.string);
10126           htab->stub_error = TRUE;
10127           return FALSE;
10128         }
10129
10130       if (info->emitrelocations)
10131         {
10132           r = get_relocs (stub_entry->group->stub_sec, 1);
10133           if (r == NULL)
10134             return FALSE;
10135           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10136           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10137           r->r_addend = targ;
10138           if (stub_entry->h != NULL)
10139             {
10140               struct elf_link_hash_entry **hashes;
10141               unsigned long symndx;
10142               struct ppc_link_hash_entry *h;
10143
10144               hashes = elf_sym_hashes (htab->params->stub_bfd);
10145               if (hashes == NULL)
10146                 {
10147                   bfd_size_type hsize;
10148
10149                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10150                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10151                   if (hashes == NULL)
10152                     return FALSE;
10153                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10154                   htab->stub_globals = 1;
10155                 }
10156               symndx = htab->stub_globals++;
10157               h = stub_entry->h;
10158               hashes[symndx] = &h->elf;
10159               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10160               if (h->oh != NULL && h->oh->is_func)
10161                 h = ppc_follow_link (h->oh);
10162               if (h->elf.root.u.def.section != stub_entry->target_section)
10163                 /* H is an opd symbol.  The addend must be zero.  */
10164                 r->r_addend = 0;
10165               else
10166                 {
10167                   off = (h->elf.root.u.def.value
10168                          + h->elf.root.u.def.section->output_offset
10169                          + h->elf.root.u.def.section->output_section->vma);
10170                   r->r_addend -= off;
10171                 }
10172             }
10173         }
10174       break;
10175
10176     case ppc_stub_plt_branch:
10177     case ppc_stub_plt_branch_r2off:
10178       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10179                                          stub_entry->root.string + 9,
10180                                          FALSE, FALSE);
10181       if (br_entry == NULL)
10182         {
10183           _bfd_error_handler (_("can't find branch stub `%s'"),
10184                               stub_entry->root.string);
10185           htab->stub_error = TRUE;
10186           return FALSE;
10187         }
10188
10189       targ = (stub_entry->target_value
10190               + stub_entry->target_section->output_offset
10191               + stub_entry->target_section->output_section->vma);
10192       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10193         targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10194
10195       bfd_put_64 (htab->brlt->owner, targ,
10196                   htab->brlt->contents + br_entry->offset);
10197
10198       if (br_entry->iter == htab->stub_iteration)
10199         {
10200           br_entry->iter = 0;
10201
10202           if (htab->relbrlt != NULL)
10203             {
10204               /* Create a reloc for the branch lookup table entry.  */
10205               Elf_Internal_Rela rela;
10206               bfd_byte *rl;
10207
10208               rela.r_offset = (br_entry->offset
10209                                + htab->brlt->output_offset
10210                                + htab->brlt->output_section->vma);
10211               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10212               rela.r_addend = targ;
10213
10214               rl = htab->relbrlt->contents;
10215               rl += (htab->relbrlt->reloc_count++
10216                      * sizeof (Elf64_External_Rela));
10217               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10218             }
10219           else if (info->emitrelocations)
10220             {
10221               r = get_relocs (htab->brlt, 1);
10222               if (r == NULL)
10223                 return FALSE;
10224               /* brlt, being SEC_LINKER_CREATED does not go through the
10225                  normal reloc processing.  Symbols and offsets are not
10226                  translated from input file to output file form, so
10227                  set up the offset per the output file.  */
10228               r->r_offset = (br_entry->offset
10229                              + htab->brlt->output_offset
10230                              + htab->brlt->output_section->vma);
10231               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10232               r->r_addend = targ;
10233             }
10234         }
10235
10236       targ = (br_entry->offset
10237               + htab->brlt->output_offset
10238               + htab->brlt->output_section->vma);
10239
10240       off = (elf_gp (info->output_bfd)
10241              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10242       off = targ - off;
10243
10244       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10245         {
10246           info->callbacks->einfo
10247             (_("%P: linkage table error against `%pT'\n"),
10248              stub_entry->root.string);
10249           bfd_set_error (bfd_error_bad_value);
10250           htab->stub_error = TRUE;
10251           return FALSE;
10252         }
10253
10254       if (info->emitrelocations)
10255         {
10256           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
10257           if (r == NULL)
10258             return FALSE;
10259           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10260           if (bfd_big_endian (info->output_bfd))
10261             r[0].r_offset += 2;
10262           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10263             r[0].r_offset += 4;
10264           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10265           r[0].r_addend = targ;
10266           if (PPC_HA (off) != 0)
10267             {
10268               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10269               r[1].r_offset = r[0].r_offset + 4;
10270               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10271               r[1].r_addend = r[0].r_addend;
10272             }
10273         }
10274
10275       p = loc;
10276       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10277         {
10278           if (PPC_HA (off) != 0)
10279             {
10280               bfd_put_32 (htab->params->stub_bfd,
10281                           ADDIS_R12_R2 | PPC_HA (off), p);
10282               p += 4;
10283               bfd_put_32 (htab->params->stub_bfd,
10284                           LD_R12_0R12 | PPC_LO (off), p);
10285             }
10286           else
10287             bfd_put_32 (htab->params->stub_bfd,
10288                         LD_R12_0R2 | PPC_LO (off), p);
10289         }
10290       else
10291         {
10292           bfd_vma r2off = get_r2off (info, stub_entry);
10293
10294           if (r2off == (bfd_vma) -1)
10295             {
10296               htab->stub_error = TRUE;
10297               return FALSE;
10298             }
10299
10300           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10301           p += 4;
10302           if (PPC_HA (off) != 0)
10303             {
10304               bfd_put_32 (htab->params->stub_bfd,
10305                           ADDIS_R12_R2 | PPC_HA (off), p);
10306               p += 4;
10307               bfd_put_32 (htab->params->stub_bfd,
10308                           LD_R12_0R12 | PPC_LO (off), p);
10309             }
10310           else
10311             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
10312
10313           if (PPC_HA (r2off) != 0)
10314             {
10315               p += 4;
10316               bfd_put_32 (htab->params->stub_bfd,
10317                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10318             }
10319           if (PPC_LO (r2off) != 0)
10320             {
10321               p += 4;
10322               bfd_put_32 (htab->params->stub_bfd,
10323                           ADDI_R2_R2 | PPC_LO (r2off), p);
10324             }
10325         }
10326       p += 4;
10327       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10328       p += 4;
10329       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10330       p += 4;
10331       break;
10332
10333     case ppc_stub_long_branch_notoc:
10334     case ppc_stub_long_branch_both:
10335     case ppc_stub_plt_branch_notoc:
10336     case ppc_stub_plt_branch_both:
10337     case ppc_stub_plt_call_notoc:
10338     case ppc_stub_plt_call_both:
10339       p = loc;
10340       off = (stub_entry->stub_offset
10341              + stub_entry->group->stub_sec->output_offset
10342              + stub_entry->group->stub_sec->output_section->vma);
10343       if (stub_entry->stub_type == ppc_stub_long_branch_both
10344           || stub_entry->stub_type == ppc_stub_plt_branch_both
10345           || stub_entry->stub_type == ppc_stub_plt_call_both)
10346         {
10347           off += 4;
10348           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10349           p += 4;
10350         }
10351       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10352         {
10353           targ = stub_entry->plt_ent->plt.offset & ~1;
10354           if (targ >= (bfd_vma) -2)
10355             abort ();
10356
10357           plt = htab->elf.splt;
10358           if (!htab->elf.dynamic_sections_created
10359               || stub_entry->h == NULL
10360               || stub_entry->h->elf.dynindx == -1)
10361             {
10362               if (stub_entry->symtype == STT_GNU_IFUNC)
10363                 plt = htab->elf.iplt;
10364               else
10365                 plt = htab->pltlocal;
10366             }
10367           targ += plt->output_offset + plt->output_section->vma;
10368         }
10369       else
10370         targ = (stub_entry->target_value
10371                 + stub_entry->target_section->output_offset
10372                 + stub_entry->target_section->output_section->vma);
10373       off = targ - off;
10374
10375       /* The notoc stubs calculate their target (either a PLT entry or
10376          the global entry point of a function) relative to the PC
10377          returned by the "bcl" two instructions past the start of the
10378          sequence emitted by build_offset.  The offset is therefore 8
10379          less than calculated from the start of the sequence.  */
10380       off -= 8;
10381       p = build_offset (htab->params->stub_bfd, p, off,
10382                         stub_entry->stub_type >= ppc_stub_plt_call_notoc);
10383       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
10384         {
10385           bfd_vma from;
10386           from = (stub_entry->stub_offset
10387                   + stub_entry->group->stub_sec->output_offset
10388                   + stub_entry->group->stub_sec->output_section->vma
10389                   + (p - loc));
10390           bfd_put_32 (htab->params->stub_bfd,
10391                       B_DOT | ((targ - from) & 0x3fffffc), p);
10392         }
10393       else
10394         {
10395           bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
10396           p += 4;
10397           bfd_put_32 (htab->params->stub_bfd, BCTR, p);
10398         }
10399       p += 4;
10400
10401       if (htab->glink_eh_frame != NULL
10402         && htab->glink_eh_frame->size != 0)
10403         {
10404           bfd_byte *base, *eh;
10405           unsigned int lr_used, delta;
10406
10407           base = (htab->glink_eh_frame->contents
10408                   + stub_entry->group->eh_base + 17);
10409           eh = base + stub_entry->group->eh_size;
10410           lr_used = stub_entry->stub_offset + 8;
10411           if (stub_entry->stub_type == ppc_stub_long_branch_both
10412               || stub_entry->stub_type == ppc_stub_plt_branch_both
10413               || stub_entry->stub_type == ppc_stub_plt_call_both)
10414             lr_used += 4;
10415           delta = lr_used - stub_entry->group->lr_restore;
10416           stub_entry->group->lr_restore = lr_used + 8;
10417           eh = eh_advance (htab->elf.dynobj, eh, delta);
10418           *eh++ = DW_CFA_register;
10419           *eh++ = 65;
10420           *eh++ = 12;
10421           *eh++ = DW_CFA_advance_loc + 2;
10422           *eh++ = DW_CFA_restore_extended;
10423           *eh++ = 65;
10424           stub_entry->group->eh_size = eh - base;
10425         }
10426       break;
10427
10428     case ppc_stub_plt_call:
10429     case ppc_stub_plt_call_r2save:
10430       if (stub_entry->h != NULL
10431           && stub_entry->h->is_func_descriptor
10432           && stub_entry->h->oh != NULL)
10433         {
10434           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10435
10436           /* If the old-ABI "dot-symbol" is undefined make it weak so
10437              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
10438           if (fh->elf.root.type == bfd_link_hash_undefined
10439               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
10440                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
10441             fh->elf.root.type = bfd_link_hash_undefweak;
10442         }
10443
10444       /* Now build the stub.  */
10445       targ = stub_entry->plt_ent->plt.offset & ~1;
10446       if (targ >= (bfd_vma) -2)
10447         abort ();
10448
10449       plt = htab->elf.splt;
10450       if (!htab->elf.dynamic_sections_created
10451           || stub_entry->h == NULL
10452           || stub_entry->h->elf.dynindx == -1)
10453         {
10454           if (stub_entry->symtype == STT_GNU_IFUNC)
10455             plt = htab->elf.iplt;
10456           else
10457             plt = htab->pltlocal;
10458         }
10459       targ += plt->output_offset + plt->output_section->vma;
10460
10461       off = (elf_gp (info->output_bfd)
10462              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10463       off = targ - off;
10464
10465       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10466         {
10467           info->callbacks->einfo
10468             /* xgettext:c-format */
10469             (_("%P: linkage table error against `%pT'\n"),
10470              stub_entry->h != NULL
10471              ? stub_entry->h->elf.root.root.string
10472              : "<local sym>");
10473           bfd_set_error (bfd_error_bad_value);
10474           htab->stub_error = TRUE;
10475           return FALSE;
10476         }
10477
10478       r = NULL;
10479       if (info->emitrelocations)
10480         {
10481           r = get_relocs (stub_entry->group->stub_sec,
10482                           ((PPC_HA (off) != 0)
10483                            + (htab->opd_abi
10484                               ? 2 + (htab->params->plt_static_chain
10485                                      && PPC_HA (off + 16) == PPC_HA (off))
10486                               : 1)));
10487           if (r == NULL)
10488             return FALSE;
10489           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
10490           if (bfd_big_endian (info->output_bfd))
10491             r[0].r_offset += 2;
10492           r[0].r_addend = targ;
10493         }
10494       if (stub_entry->h != NULL
10495           && (stub_entry->h == htab->tls_get_addr_fd
10496               || stub_entry->h == htab->tls_get_addr)
10497           && htab->params->tls_get_addr_opt)
10498         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10499       else
10500         p = build_plt_stub (htab, stub_entry, loc, off, r);
10501       break;
10502
10503     case ppc_stub_save_res:
10504       return TRUE;
10505
10506     default:
10507       BFD_FAIL ();
10508       return FALSE;
10509     }
10510
10511   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
10512
10513   if (htab->params->emit_stub_syms)
10514     {
10515       struct elf_link_hash_entry *h;
10516       size_t len1, len2;
10517       char *name;
10518       const char *const stub_str[] = { "long_branch",
10519                                        "long_branch",
10520                                        "long_branch",
10521                                        "long_branch",
10522                                        "plt_branch",
10523                                        "plt_branch",
10524                                        "plt_branch",
10525                                        "plt_branch",
10526                                        "plt_call",
10527                                        "plt_call",
10528                                        "plt_call",
10529                                        "plt_call" };
10530
10531       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10532       len2 = strlen (stub_entry->root.string);
10533       name = bfd_malloc (len1 + len2 + 2);
10534       if (name == NULL)
10535         return FALSE;
10536       memcpy (name, stub_entry->root.string, 9);
10537       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10538       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10539       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10540       if (h == NULL)
10541         return FALSE;
10542       if (h->root.type == bfd_link_hash_new)
10543         {
10544           h->root.type = bfd_link_hash_defined;
10545           h->root.u.def.section = stub_entry->group->stub_sec;
10546           h->root.u.def.value = stub_entry->stub_offset;
10547           h->ref_regular = 1;
10548           h->def_regular = 1;
10549           h->ref_regular_nonweak = 1;
10550           h->forced_local = 1;
10551           h->non_elf = 0;
10552           h->root.linker_def = 1;
10553         }
10554     }
10555
10556   return TRUE;
10557 }
10558
10559 /* As above, but don't actually build the stub.  Just bump offset so
10560    we know stub section sizes, and select plt_branch stubs where
10561    long_branch stubs won't do.  */
10562
10563 static bfd_boolean
10564 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10565 {
10566   struct ppc_stub_hash_entry *stub_entry;
10567   struct bfd_link_info *info;
10568   struct ppc_link_hash_table *htab;
10569   asection *plt;
10570   bfd_vma targ, off, r2off;
10571   unsigned int size, extra, lr_used, delta;
10572
10573   /* Massage our args to the form they really have.  */
10574   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10575   info = in_arg;
10576
10577   htab = ppc_hash_table (info);
10578   if (htab == NULL)
10579     return FALSE;
10580
10581   /* Make a note of the offset within the stubs for this entry.  */
10582   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10583
10584   if (stub_entry->h != NULL
10585       && stub_entry->h->save_res
10586       && stub_entry->h->elf.root.type == bfd_link_hash_defined
10587       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
10588     {
10589       /* Don't make stubs to out-of-line register save/restore
10590          functions.  Instead, emit copies of the functions.  */
10591       stub_entry->group->needs_save_res = 1;
10592       stub_entry->stub_type = ppc_stub_save_res;
10593       return TRUE;
10594     }
10595
10596   switch (stub_entry->stub_type)
10597     {
10598     case ppc_stub_plt_branch:
10599     case ppc_stub_plt_branch_r2off:
10600       /* Reset the stub type from the plt branch variant in case we now
10601          can reach with a shorter stub.  */
10602       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10603       /* Fall through.  */
10604     case ppc_stub_long_branch:
10605     case ppc_stub_long_branch_r2off:
10606       targ = (stub_entry->target_value
10607               + stub_entry->target_section->output_offset
10608               + stub_entry->target_section->output_section->vma);
10609       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10610       off = (stub_entry->stub_offset
10611              + stub_entry->group->stub_sec->output_offset
10612              + stub_entry->group->stub_sec->output_section->vma);
10613
10614       size = 4;
10615       r2off = 0;
10616       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10617         {
10618           r2off = get_r2off (info, stub_entry);
10619           if (r2off == (bfd_vma) -1)
10620             {
10621               htab->stub_error = TRUE;
10622               return FALSE;
10623             }
10624           size = 8;
10625           if (PPC_HA (r2off) != 0)
10626             size += 4;
10627           if (PPC_LO (r2off) != 0)
10628             size += 4;
10629           off += size - 4;
10630         }
10631       off = targ - off;
10632
10633       /* If the branch offset is too big, use a ppc_stub_plt_branch.
10634          Do the same for -R objects without function descriptors.  */
10635       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
10636            && r2off == 0
10637            && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
10638           || off + (1 << 25) >= (bfd_vma) (1 << 26))
10639         {
10640           struct ppc_branch_hash_entry *br_entry;
10641
10642           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10643                                              stub_entry->root.string + 9,
10644                                              TRUE, FALSE);
10645           if (br_entry == NULL)
10646             {
10647               _bfd_error_handler (_("can't build branch stub `%s'"),
10648                                   stub_entry->root.string);
10649               htab->stub_error = TRUE;
10650               return FALSE;
10651             }
10652
10653           if (br_entry->iter != htab->stub_iteration)
10654             {
10655               br_entry->iter = htab->stub_iteration;
10656               br_entry->offset = htab->brlt->size;
10657               htab->brlt->size += 8;
10658
10659               if (htab->relbrlt != NULL)
10660                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10661               else if (info->emitrelocations)
10662                 {
10663                   htab->brlt->reloc_count += 1;
10664                   htab->brlt->flags |= SEC_RELOC;
10665                 }
10666             }
10667
10668           targ = (br_entry->offset
10669                   + htab->brlt->output_offset
10670                   + htab->brlt->output_section->vma);
10671           off = (elf_gp (info->output_bfd)
10672                  + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10673           off = targ - off;
10674
10675           if (info->emitrelocations)
10676             {
10677               stub_entry->group->stub_sec->reloc_count
10678                 += 1 + (PPC_HA (off) != 0);
10679               stub_entry->group->stub_sec->flags |= SEC_RELOC;
10680             }
10681
10682           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10683           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10684             {
10685               size = 12;
10686               if (PPC_HA (off) != 0)
10687                 size = 16;
10688             }
10689           else
10690             {
10691               size = 16;
10692               if (PPC_HA (off) != 0)
10693                 size += 4;
10694
10695               if (PPC_HA (r2off) != 0)
10696                 size += 4;
10697               if (PPC_LO (r2off) != 0)
10698                 size += 4;
10699             }
10700         }
10701       else if (info->emitrelocations)
10702         {
10703           stub_entry->group->stub_sec->reloc_count += 1;
10704           stub_entry->group->stub_sec->flags |= SEC_RELOC;
10705         }
10706       break;
10707
10708     case ppc_stub_plt_branch_notoc:
10709     case ppc_stub_plt_branch_both:
10710       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10711       /* Fall through.  */
10712     case ppc_stub_long_branch_notoc:
10713     case ppc_stub_long_branch_both:
10714       off = (stub_entry->stub_offset
10715              + stub_entry->group->stub_sec->output_offset
10716              + stub_entry->group->stub_sec->output_section->vma);
10717       size = 0;
10718       if (stub_entry->stub_type == ppc_stub_long_branch_both)
10719         size = 4;
10720       off += size;
10721       targ = (stub_entry->target_value
10722               + stub_entry->target_section->output_offset
10723               + stub_entry->target_section->output_section->vma);
10724       off = targ - off;
10725
10726       extra = size_offset (off - 8);
10727       /* Include branch insn plus those in the offset sequence.  */
10728       size += 4 + extra;
10729       /* The branch insn is at the end, or "extra" bytes along.  So
10730          its offset will be "extra" bytes less that that already
10731          calculated.  */
10732       off -= extra;
10733
10734       /* After the bcl, lr has been modified so we need to emit
10735          .eh_frame info saying the return address is in r12.  */
10736       lr_used = stub_entry->stub_offset + 8;
10737       if (stub_entry->stub_type == ppc_stub_long_branch_both)
10738         lr_used += 4;
10739       /* The eh_frame info will consist of a DW_CFA_advance_loc or
10740          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10741          DW_CFA_restore_extended 65.  */
10742       delta = lr_used - stub_entry->group->lr_restore;
10743       stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10744       stub_entry->group->lr_restore = lr_used + 8;
10745
10746       /* If the branch can't reach, use a plt_branch.  */
10747       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10748         {
10749           stub_entry->stub_type += (ppc_stub_plt_branch_notoc
10750                                     - ppc_stub_long_branch_notoc);
10751           size += 4;
10752         }
10753       break;
10754
10755     case ppc_stub_plt_call_notoc:
10756     case ppc_stub_plt_call_both:
10757       off = (stub_entry->stub_offset
10758              + stub_entry->group->stub_sec->output_offset
10759              + stub_entry->group->stub_sec->output_section->vma);
10760       if (stub_entry->stub_type == ppc_stub_plt_call_both)
10761         off += 4;
10762       targ = stub_entry->plt_ent->plt.offset & ~1;
10763       if (targ >= (bfd_vma) -2)
10764         abort ();
10765
10766       plt = htab->elf.splt;
10767       if (!htab->elf.dynamic_sections_created
10768           || stub_entry->h == NULL
10769           || stub_entry->h->elf.dynindx == -1)
10770         {
10771           if (stub_entry->symtype == STT_GNU_IFUNC)
10772             plt = htab->elf.iplt;
10773           else
10774             plt = htab->pltlocal;
10775         }
10776       targ += plt->output_offset + plt->output_section->vma;
10777       off = targ - off;
10778
10779       if (htab->params->plt_stub_align != 0)
10780         {
10781           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10782
10783           stub_entry->group->stub_sec->size += pad;
10784           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10785           off -= pad;
10786         }
10787
10788       size = plt_stub_size (htab, stub_entry, off);
10789
10790       /* After the bcl, lr has been modified so we need to emit
10791          .eh_frame info saying the return address is in r12.  */
10792       lr_used = stub_entry->stub_offset + 8;
10793       if (stub_entry->stub_type == ppc_stub_plt_call_both)
10794         lr_used += 4;
10795       /* The eh_frame info will consist of a DW_CFA_advance_loc or
10796          variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
10797          DW_CFA_restore_extended 65.  */
10798       delta = lr_used - stub_entry->group->lr_restore;
10799       stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10800       stub_entry->group->lr_restore = lr_used + 8;
10801       break;
10802
10803     case ppc_stub_plt_call:
10804     case ppc_stub_plt_call_r2save:
10805       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10806       if (targ >= (bfd_vma) -2)
10807         abort ();
10808       plt = htab->elf.splt;
10809       if (!htab->elf.dynamic_sections_created
10810           || stub_entry->h == NULL
10811           || stub_entry->h->elf.dynindx == -1)
10812         {
10813           if (stub_entry->symtype == STT_GNU_IFUNC)
10814             plt = htab->elf.iplt;
10815           else
10816             plt = htab->pltlocal;
10817         }
10818       targ += plt->output_offset + plt->output_section->vma;
10819
10820       off = (elf_gp (info->output_bfd)
10821              + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10822       off = targ - off;
10823
10824       if (htab->params->plt_stub_align != 0)
10825         {
10826           unsigned pad = plt_stub_pad (htab, stub_entry, off);
10827
10828           stub_entry->group->stub_sec->size += pad;
10829           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10830         }
10831
10832       if (info->emitrelocations)
10833         {
10834           stub_entry->group->stub_sec->reloc_count
10835             += ((PPC_HA (off) != 0)
10836                 + (htab->opd_abi
10837                    ? 2 + (htab->params->plt_static_chain
10838                           && PPC_HA (off + 16) == PPC_HA (off))
10839                    : 1));
10840           stub_entry->group->stub_sec->flags |= SEC_RELOC;
10841         }
10842
10843       size = plt_stub_size (htab, stub_entry, off);
10844
10845       if (stub_entry->h != NULL
10846           && (stub_entry->h == htab->tls_get_addr_fd
10847               || stub_entry->h == htab->tls_get_addr)
10848           && htab->params->tls_get_addr_opt
10849           && stub_entry->stub_type == ppc_stub_plt_call_r2save)
10850         {
10851           /* After the bctrl, lr has been modified so we need to
10852              emit .eh_frame info saying the return address is
10853              on the stack.  In fact we put the EH info specifying
10854              that the return address is on the stack *at* the
10855              call rather than after it, because the EH info for a
10856              call needs to be specified by that point.
10857              See libgcc/unwind-dw2.c execute_cfa_program.  */
10858           lr_used = stub_entry->stub_offset + size - 20;
10859           /* The eh_frame info will consist of a DW_CFA_advance_loc
10860              or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
10861              DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
10862           delta = lr_used - stub_entry->group->lr_restore;
10863           stub_entry->group->eh_size += eh_advance_size (delta) + 6;
10864           stub_entry->group->lr_restore = size - 4;
10865         }
10866       break;
10867
10868     default:
10869       BFD_FAIL ();
10870       return FALSE;
10871     }
10872
10873   stub_entry->group->stub_sec->size += size;
10874   return TRUE;
10875 }
10876
10877 /* Set up various things so that we can make a list of input sections
10878    for each output section included in the link.  Returns -1 on error,
10879    0 when no stubs will be needed, and 1 on success.  */
10880
10881 int
10882 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
10883 {
10884   unsigned int id;
10885   bfd_size_type amt;
10886   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10887
10888   if (htab == NULL)
10889     return -1;
10890
10891   htab->sec_info_arr_size = _bfd_section_id;
10892   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
10893   htab->sec_info = bfd_zmalloc (amt);
10894   if (htab->sec_info == NULL)
10895     return -1;
10896
10897   /* Set toc_off for com, und, abs and ind sections.  */
10898   for (id = 0; id < 3; id++)
10899     htab->sec_info[id].toc_off = TOC_BASE_OFF;
10900
10901   return 1;
10902 }
10903
10904 /* Set up for first pass at multitoc partitioning.  */
10905
10906 void
10907 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10908 {
10909   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10910
10911   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10912   htab->toc_bfd = NULL;
10913   htab->toc_first_sec = NULL;
10914 }
10915
10916 /* The linker repeatedly calls this function for each TOC input section
10917    and linker generated GOT section.  Group input bfds such that the toc
10918    within a group is less than 64k in size.  */
10919
10920 bfd_boolean
10921 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10922 {
10923   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10924   bfd_vma addr, off, limit;
10925
10926   if (htab == NULL)
10927     return FALSE;
10928
10929   if (!htab->second_toc_pass)
10930     {
10931       /* Keep track of the first .toc or .got section for this input bfd.  */
10932       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10933
10934       if (new_bfd)
10935         {
10936           htab->toc_bfd = isec->owner;
10937           htab->toc_first_sec = isec;
10938         }
10939
10940       addr = isec->output_offset + isec->output_section->vma;
10941       off = addr - htab->toc_curr;
10942       limit = 0x80008000;
10943       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10944         limit = 0x10000;
10945       if (off + isec->size > limit)
10946         {
10947           addr = (htab->toc_first_sec->output_offset
10948                   + htab->toc_first_sec->output_section->vma);
10949           htab->toc_curr = addr;
10950           htab->toc_curr &= -TOC_BASE_ALIGN;
10951         }
10952
10953       /* toc_curr is the base address of this toc group.  Set elf_gp
10954          for the input section to be the offset relative to the
10955          output toc base plus 0x8000.  Making the input elf_gp an
10956          offset allows us to move the toc as a whole without
10957          recalculating input elf_gp.  */
10958       off = htab->toc_curr - elf_gp (info->output_bfd);
10959       off += TOC_BASE_OFF;
10960
10961       /* Die if someone uses a linker script that doesn't keep input
10962          file .toc and .got together.  */
10963       if (new_bfd
10964           && elf_gp (isec->owner) != 0
10965           && elf_gp (isec->owner) != off)
10966         return FALSE;
10967
10968       elf_gp (isec->owner) = off;
10969       return TRUE;
10970     }
10971
10972   /* During the second pass toc_first_sec points to the start of
10973      a toc group, and toc_curr is used to track the old elf_gp.
10974      We use toc_bfd to ensure we only look at each bfd once.  */
10975   if (htab->toc_bfd == isec->owner)
10976     return TRUE;
10977   htab->toc_bfd = isec->owner;
10978
10979   if (htab->toc_first_sec == NULL
10980       || htab->toc_curr != elf_gp (isec->owner))
10981     {
10982       htab->toc_curr = elf_gp (isec->owner);
10983       htab->toc_first_sec = isec;
10984     }
10985   addr = (htab->toc_first_sec->output_offset
10986           + htab->toc_first_sec->output_section->vma);
10987   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
10988   elf_gp (isec->owner) = off;
10989
10990   return TRUE;
10991 }
10992
10993 /* Called via elf_link_hash_traverse to merge GOT entries for global
10994    symbol H.  */
10995
10996 static bfd_boolean
10997 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10998 {
10999   if (h->root.type == bfd_link_hash_indirect)
11000     return TRUE;
11001
11002   merge_got_entries (&h->got.glist);
11003
11004   return TRUE;
11005 }
11006
11007 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11008    symbol H.  */
11009
11010 static bfd_boolean
11011 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11012 {
11013   struct got_entry *gent;
11014
11015   if (h->root.type == bfd_link_hash_indirect)
11016     return TRUE;
11017
11018   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11019     if (!gent->is_indirect)
11020       allocate_got (h, (struct bfd_link_info *) inf, gent);
11021   return TRUE;
11022 }
11023
11024 /* Called on the first multitoc pass after the last call to
11025    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11026    entries.  */
11027
11028 bfd_boolean
11029 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11030 {
11031   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11032   struct bfd *ibfd, *ibfd2;
11033   bfd_boolean done_something;
11034
11035   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11036
11037   if (!htab->do_multi_toc)
11038     return FALSE;
11039
11040   /* Merge global sym got entries within a toc group.  */
11041   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11042
11043   /* And tlsld_got.  */
11044   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11045     {
11046       struct got_entry *ent, *ent2;
11047
11048       if (!is_ppc64_elf (ibfd))
11049         continue;
11050
11051       ent = ppc64_tlsld_got (ibfd);
11052       if (!ent->is_indirect
11053           && ent->got.offset != (bfd_vma) -1)
11054         {
11055           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11056             {
11057               if (!is_ppc64_elf (ibfd2))
11058                 continue;
11059
11060               ent2 = ppc64_tlsld_got (ibfd2);
11061               if (!ent2->is_indirect
11062                   && ent2->got.offset != (bfd_vma) -1
11063                   && elf_gp (ibfd2) == elf_gp (ibfd))
11064                 {
11065                   ent2->is_indirect = TRUE;
11066                   ent2->got.ent = ent;
11067                 }
11068             }
11069         }
11070     }
11071
11072   /* Zap sizes of got sections.  */
11073   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11074   htab->elf.irelplt->size -= htab->got_reli_size;
11075   htab->got_reli_size = 0;
11076
11077   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11078     {
11079       asection *got, *relgot;
11080
11081       if (!is_ppc64_elf (ibfd))
11082         continue;
11083
11084       got = ppc64_elf_tdata (ibfd)->got;
11085       if (got != NULL)
11086         {
11087           got->rawsize = got->size;
11088           got->size = 0;
11089           relgot = ppc64_elf_tdata (ibfd)->relgot;
11090           relgot->rawsize = relgot->size;
11091           relgot->size = 0;
11092         }
11093     }
11094
11095   /* Now reallocate the got, local syms first.  We don't need to
11096      allocate section contents again since we never increase size.  */
11097   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11098     {
11099       struct got_entry **lgot_ents;
11100       struct got_entry **end_lgot_ents;
11101       struct plt_entry **local_plt;
11102       struct plt_entry **end_local_plt;
11103       unsigned char *lgot_masks;
11104       bfd_size_type locsymcount;
11105       Elf_Internal_Shdr *symtab_hdr;
11106       asection *s;
11107
11108       if (!is_ppc64_elf (ibfd))
11109         continue;
11110
11111       lgot_ents = elf_local_got_ents (ibfd);
11112       if (!lgot_ents)
11113         continue;
11114
11115       symtab_hdr = &elf_symtab_hdr (ibfd);
11116       locsymcount = symtab_hdr->sh_info;
11117       end_lgot_ents = lgot_ents + locsymcount;
11118       local_plt = (struct plt_entry **) end_lgot_ents;
11119       end_local_plt = local_plt + locsymcount;
11120       lgot_masks = (unsigned char *) end_local_plt;
11121       s = ppc64_elf_tdata (ibfd)->got;
11122       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11123         {
11124           struct got_entry *ent;
11125
11126           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11127             {
11128               unsigned int ent_size = 8;
11129               unsigned int rel_size = sizeof (Elf64_External_Rela);
11130
11131               ent->got.offset = s->size;
11132               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11133                 {
11134                   ent_size *= 2;
11135                   rel_size *= 2;
11136                 }
11137               s->size += ent_size;
11138               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11139                 {
11140                   htab->elf.irelplt->size += rel_size;
11141                   htab->got_reli_size += rel_size;
11142                 }
11143               else if (bfd_link_pic (info)
11144                        && !((ent->tls_type & TLS_TPREL) != 0
11145                             && bfd_link_executable (info)))
11146                 {
11147                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11148                   srel->size += rel_size;
11149                 }
11150             }
11151         }
11152     }
11153
11154   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11155
11156   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11157     {
11158       struct got_entry *ent;
11159
11160       if (!is_ppc64_elf (ibfd))
11161         continue;
11162
11163       ent = ppc64_tlsld_got (ibfd);
11164       if (!ent->is_indirect
11165           && ent->got.offset != (bfd_vma) -1)
11166         {
11167           asection *s = ppc64_elf_tdata (ibfd)->got;
11168           ent->got.offset = s->size;
11169           s->size += 16;
11170           if (bfd_link_pic (info))
11171             {
11172               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11173               srel->size += sizeof (Elf64_External_Rela);
11174             }
11175         }
11176     }
11177
11178   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11179   if (!done_something)
11180     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11181       {
11182         asection *got;
11183
11184         if (!is_ppc64_elf (ibfd))
11185           continue;
11186
11187         got = ppc64_elf_tdata (ibfd)->got;
11188         if (got != NULL)
11189           {
11190             done_something = got->rawsize != got->size;
11191             if (done_something)
11192               break;
11193           }
11194       }
11195
11196   if (done_something)
11197     (*htab->params->layout_sections_again) ();
11198
11199   /* Set up for second pass over toc sections to recalculate elf_gp
11200      on input sections.  */
11201   htab->toc_bfd = NULL;
11202   htab->toc_first_sec = NULL;
11203   htab->second_toc_pass = TRUE;
11204   return done_something;
11205 }
11206
11207 /* Called after second pass of multitoc partitioning.  */
11208
11209 void
11210 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11211 {
11212   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11213
11214   /* After the second pass, toc_curr tracks the TOC offset used
11215      for code sections below in ppc64_elf_next_input_section.  */
11216   htab->toc_curr = TOC_BASE_OFF;
11217 }
11218
11219 /* No toc references were found in ISEC.  If the code in ISEC makes no
11220    calls, then there's no need to use toc adjusting stubs when branching
11221    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11222    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11223    needed, and 2 if a cyclical call-graph was found but no other reason
11224    for a stub was detected.  If called from the top level, a return of
11225    2 means the same as a return of 0.  */
11226
11227 static int
11228 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11229 {
11230   int ret;
11231
11232   /* Mark this section as checked.  */
11233   isec->call_check_done = 1;
11234
11235   /* We know none of our code bearing sections will need toc stubs.  */
11236   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11237     return 0;
11238
11239   if (isec->size == 0)
11240     return 0;
11241
11242   if (isec->output_section == NULL)
11243     return 0;
11244
11245   ret = 0;
11246   if (isec->reloc_count != 0)
11247     {
11248       Elf_Internal_Rela *relstart, *rel;
11249       Elf_Internal_Sym *local_syms;
11250       struct ppc_link_hash_table *htab;
11251
11252       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11253                                             info->keep_memory);
11254       if (relstart == NULL)
11255         return -1;
11256
11257       /* Look for branches to outside of this section.  */
11258       local_syms = NULL;
11259       htab = ppc_hash_table (info);
11260       if (htab == NULL)
11261         return -1;
11262
11263       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11264         {
11265           enum elf_ppc64_reloc_type r_type;
11266           unsigned long r_symndx;
11267           struct elf_link_hash_entry *h;
11268           struct ppc_link_hash_entry *eh;
11269           Elf_Internal_Sym *sym;
11270           asection *sym_sec;
11271           struct _opd_sec_data *opd;
11272           bfd_vma sym_value;
11273           bfd_vma dest;
11274
11275           r_type = ELF64_R_TYPE (rel->r_info);
11276           if (r_type != R_PPC64_REL24
11277               && r_type != R_PPC64_REL24_NOTOC
11278               && r_type != R_PPC64_REL14
11279               && r_type != R_PPC64_REL14_BRTAKEN
11280               && r_type != R_PPC64_REL14_BRNTAKEN
11281               && r_type != R_PPC64_PLTCALL)
11282             continue;
11283
11284           r_symndx = ELF64_R_SYM (rel->r_info);
11285           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11286                           isec->owner))
11287             {
11288               ret = -1;
11289               break;
11290             }
11291
11292           /* Calls to dynamic lib functions go through a plt call stub
11293              that uses r2.  */
11294           eh = (struct ppc_link_hash_entry *) h;
11295           if (eh != NULL
11296               && (eh->elf.plt.plist != NULL
11297                   || (eh->oh != NULL
11298                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11299             {
11300               ret = 1;
11301               break;
11302             }
11303
11304           if (sym_sec == NULL)
11305             /* Ignore other undefined symbols.  */
11306             continue;
11307
11308           /* Assume branches to other sections not included in the
11309              link need stubs too, to cover -R and absolute syms.  */
11310           if (sym_sec->output_section == NULL)
11311             {
11312               ret = 1;
11313               break;
11314             }
11315
11316           if (h == NULL)
11317             sym_value = sym->st_value;
11318           else
11319             {
11320               if (h->root.type != bfd_link_hash_defined
11321                   && h->root.type != bfd_link_hash_defweak)
11322                 abort ();
11323               sym_value = h->root.u.def.value;
11324             }
11325           sym_value += rel->r_addend;
11326
11327           /* If this branch reloc uses an opd sym, find the code section.  */
11328           opd = get_opd_info (sym_sec);
11329           if (opd != NULL)
11330             {
11331               if (h == NULL && opd->adjust != NULL)
11332                 {
11333                   long adjust;
11334
11335                   adjust = opd->adjust[OPD_NDX (sym_value)];
11336                   if (adjust == -1)
11337                     /* Assume deleted functions won't ever be called.  */
11338                     continue;
11339                   sym_value += adjust;
11340                 }
11341
11342               dest = opd_entry_value (sym_sec, sym_value,
11343                                       &sym_sec, NULL, FALSE);
11344               if (dest == (bfd_vma) -1)
11345                 continue;
11346             }
11347           else
11348             dest = (sym_value
11349                     + sym_sec->output_offset
11350                     + sym_sec->output_section->vma);
11351
11352           /* Ignore branch to self.  */
11353           if (sym_sec == isec)
11354             continue;
11355
11356           /* If the called function uses the toc, we need a stub.  */
11357           if (sym_sec->has_toc_reloc
11358               || sym_sec->makes_toc_func_call)
11359             {
11360               ret = 1;
11361               break;
11362             }
11363
11364           /* Assume any branch that needs a long branch stub might in fact
11365              need a plt_branch stub.  A plt_branch stub uses r2.  */
11366           else if (dest - (isec->output_offset
11367                            + isec->output_section->vma
11368                            + rel->r_offset) + (1 << 25)
11369                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11370                                                              ? h->other
11371                                                              : sym->st_other))
11372             {
11373               ret = 1;
11374               break;
11375             }
11376
11377           /* If calling back to a section in the process of being
11378              tested, we can't say for sure that no toc adjusting stubs
11379              are needed, so don't return zero.  */
11380           else if (sym_sec->call_check_in_progress)
11381             ret = 2;
11382
11383           /* Branches to another section that itself doesn't have any TOC
11384              references are OK.  Recursively call ourselves to check.  */
11385           else if (!sym_sec->call_check_done)
11386             {
11387               int recur;
11388
11389               /* Mark current section as indeterminate, so that other
11390                  sections that call back to current won't be marked as
11391                  known.  */
11392               isec->call_check_in_progress = 1;
11393               recur = toc_adjusting_stub_needed (info, sym_sec);
11394               isec->call_check_in_progress = 0;
11395
11396               if (recur != 0)
11397                 {
11398                   ret = recur;
11399                   if (recur != 2)
11400                     break;
11401                 }
11402             }
11403         }
11404
11405       if (local_syms != NULL
11406           && (elf_symtab_hdr (isec->owner).contents
11407               != (unsigned char *) local_syms))
11408         free (local_syms);
11409       if (elf_section_data (isec)->relocs != relstart)
11410         free (relstart);
11411     }
11412
11413   if ((ret & 1) == 0
11414       && isec->map_head.s != NULL
11415       && (strcmp (isec->output_section->name, ".init") == 0
11416           || strcmp (isec->output_section->name, ".fini") == 0))
11417     {
11418       if (isec->map_head.s->has_toc_reloc
11419           || isec->map_head.s->makes_toc_func_call)
11420         ret = 1;
11421       else if (!isec->map_head.s->call_check_done)
11422         {
11423           int recur;
11424           isec->call_check_in_progress = 1;
11425           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11426           isec->call_check_in_progress = 0;
11427           if (recur != 0)
11428             ret = recur;
11429         }
11430     }
11431
11432   if (ret == 1)
11433     isec->makes_toc_func_call = 1;
11434
11435   return ret;
11436 }
11437
11438 /* The linker repeatedly calls this function for each input section,
11439    in the order that input sections are linked into output sections.
11440    Build lists of input sections to determine groupings between which
11441    we may insert linker stubs.  */
11442
11443 bfd_boolean
11444 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11445 {
11446   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11447
11448   if (htab == NULL)
11449     return FALSE;
11450
11451   if ((isec->output_section->flags & SEC_CODE) != 0
11452       && isec->output_section->id < htab->sec_info_arr_size)
11453     {
11454       /* This happens to make the list in reverse order,
11455          which is what we want.  */
11456       htab->sec_info[isec->id].u.list
11457         = htab->sec_info[isec->output_section->id].u.list;
11458       htab->sec_info[isec->output_section->id].u.list = isec;
11459     }
11460
11461   if (htab->multi_toc_needed)
11462     {
11463       /* Analyse sections that aren't already flagged as needing a
11464          valid toc pointer.  Exclude .fixup for the linux kernel.
11465          .fixup contains branches, but only back to the function that
11466          hit an exception.  */
11467       if (!(isec->has_toc_reloc
11468             || (isec->flags & SEC_CODE) == 0
11469             || strcmp (isec->name, ".fixup") == 0
11470             || isec->call_check_done))
11471         {
11472           if (toc_adjusting_stub_needed (info, isec) < 0)
11473             return FALSE;
11474         }
11475       /* Make all sections use the TOC assigned for this object file.
11476          This will be wrong for pasted sections;  We fix that in
11477          check_pasted_section().  */
11478       if (elf_gp (isec->owner) != 0)
11479         htab->toc_curr = elf_gp (isec->owner);
11480     }
11481
11482   htab->sec_info[isec->id].toc_off = htab->toc_curr;
11483   return TRUE;
11484 }
11485
11486 /* Check that all .init and .fini sections use the same toc, if they
11487    have toc relocs.  */
11488
11489 static bfd_boolean
11490 check_pasted_section (struct bfd_link_info *info, const char *name)
11491 {
11492   asection *o = bfd_get_section_by_name (info->output_bfd, name);
11493
11494   if (o != NULL)
11495     {
11496       struct ppc_link_hash_table *htab = ppc_hash_table (info);
11497       bfd_vma toc_off = 0;
11498       asection *i;
11499
11500       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11501         if (i->has_toc_reloc)
11502           {
11503             if (toc_off == 0)
11504               toc_off = htab->sec_info[i->id].toc_off;
11505             else if (toc_off != htab->sec_info[i->id].toc_off)
11506               return FALSE;
11507           }
11508
11509       if (toc_off == 0)
11510         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11511           if (i->makes_toc_func_call)
11512             {
11513               toc_off = htab->sec_info[i->id].toc_off;
11514               break;
11515             }
11516
11517       /* Make sure the whole pasted function uses the same toc offset.  */
11518       if (toc_off != 0)
11519         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11520           htab->sec_info[i->id].toc_off = toc_off;
11521     }
11522   return TRUE;
11523 }
11524
11525 bfd_boolean
11526 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11527 {
11528   return (check_pasted_section (info, ".init")
11529           & check_pasted_section (info, ".fini"));
11530 }
11531
11532 /* See whether we can group stub sections together.  Grouping stub
11533    sections may result in fewer stubs.  More importantly, we need to
11534    put all .init* and .fini* stubs at the beginning of the .init or
11535    .fini output sections respectively, because glibc splits the
11536    _init and _fini functions into multiple parts.  Putting a stub in
11537    the middle of a function is not a good idea.  */
11538
11539 static bfd_boolean
11540 group_sections (struct bfd_link_info *info,
11541                 bfd_size_type stub_group_size,
11542                 bfd_boolean stubs_always_before_branch)
11543 {
11544   struct ppc_link_hash_table *htab;
11545   asection *osec;
11546   bfd_boolean suppress_size_errors;
11547
11548   htab = ppc_hash_table (info);
11549   if (htab == NULL)
11550     return FALSE;
11551
11552   suppress_size_errors = FALSE;
11553   if (stub_group_size == 1)
11554     {
11555       /* Default values.  */
11556       if (stubs_always_before_branch)
11557         stub_group_size = 0x1e00000;
11558       else
11559         stub_group_size = 0x1c00000;
11560       suppress_size_errors = TRUE;
11561     }
11562
11563   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
11564     {
11565       asection *tail;
11566
11567       if (osec->id >= htab->sec_info_arr_size)
11568         continue;
11569
11570       tail = htab->sec_info[osec->id].u.list;
11571       while (tail != NULL)
11572         {
11573           asection *curr;
11574           asection *prev;
11575           bfd_size_type total;
11576           bfd_boolean big_sec;
11577           bfd_vma curr_toc;
11578           struct map_stub *group;
11579           bfd_size_type group_size;
11580
11581           curr = tail;
11582           total = tail->size;
11583           group_size = (ppc64_elf_section_data (tail) != NULL
11584                         && ppc64_elf_section_data (tail)->has_14bit_branch
11585                         ? stub_group_size >> 10 : stub_group_size);
11586
11587           big_sec = total > group_size;
11588           if (big_sec && !suppress_size_errors)
11589             /* xgettext:c-format */
11590             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
11591                                 tail->owner, tail);
11592           curr_toc = htab->sec_info[tail->id].toc_off;
11593
11594           while ((prev = htab->sec_info[curr->id].u.list) != NULL
11595                  && ((total += curr->output_offset - prev->output_offset)
11596                      < (ppc64_elf_section_data (prev) != NULL
11597                         && ppc64_elf_section_data (prev)->has_14bit_branch
11598                         ? (group_size = stub_group_size >> 10) : group_size))
11599                  && htab->sec_info[prev->id].toc_off == curr_toc)
11600             curr = prev;
11601
11602           /* OK, the size from the start of CURR to the end is less
11603              than group_size and thus can be handled by one stub
11604              section.  (or the tail section is itself larger than
11605              group_size, in which case we may be toast.)  We should
11606              really be keeping track of the total size of stubs added
11607              here, as stubs contribute to the final output section
11608              size.  That's a little tricky, and this way will only
11609              break if stubs added make the total size more than 2^25,
11610              ie. for the default stub_group_size, if stubs total more
11611              than 2097152 bytes, or nearly 75000 plt call stubs.  */
11612           group = bfd_alloc (curr->owner, sizeof (*group));
11613           if (group == NULL)
11614             return FALSE;
11615           group->link_sec = curr;
11616           group->stub_sec = NULL;
11617           group->needs_save_res = 0;
11618           group->lr_restore = 0;
11619           group->eh_size = 0;
11620           group->eh_base = 0;
11621           group->next = htab->group;
11622           htab->group = group;
11623           do
11624             {
11625               prev = htab->sec_info[tail->id].u.list;
11626               /* Set up this stub group.  */
11627               htab->sec_info[tail->id].u.group = group;
11628             }
11629           while (tail != curr && (tail = prev) != NULL);
11630
11631           /* But wait, there's more!  Input sections up to group_size
11632              bytes before the stub section can be handled by it too.
11633              Don't do this if we have a really large section after the
11634              stubs, as adding more stubs increases the chance that
11635              branches may not reach into the stub section.  */
11636           if (!stubs_always_before_branch && !big_sec)
11637             {
11638               total = 0;
11639               while (prev != NULL
11640                      && ((total += tail->output_offset - prev->output_offset)
11641                          < (ppc64_elf_section_data (prev) != NULL
11642                             && ppc64_elf_section_data (prev)->has_14bit_branch
11643                             ? (group_size = stub_group_size >> 10)
11644                             : group_size))
11645                      && htab->sec_info[prev->id].toc_off == curr_toc)
11646                 {
11647                   tail = prev;
11648                   prev = htab->sec_info[tail->id].u.list;
11649                   htab->sec_info[tail->id].u.group = group;
11650                 }
11651             }
11652           tail = prev;
11653         }
11654     }
11655   return TRUE;
11656 }
11657
11658 static const unsigned char glink_eh_frame_cie[] =
11659 {
11660   0, 0, 0, 16,                          /* length.  */
11661   0, 0, 0, 0,                           /* id.  */
11662   1,                                    /* CIE version.  */
11663   'z', 'R', 0,                          /* Augmentation string.  */
11664   4,                                    /* Code alignment.  */
11665   0x78,                                 /* Data alignment.  */
11666   65,                                   /* RA reg.  */
11667   1,                                    /* Augmentation size.  */
11668   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
11669   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
11670 };
11671
11672 /* Stripping output sections is normally done before dynamic section
11673    symbols have been allocated.  This function is called later, and
11674    handles cases like htab->brlt which is mapped to its own output
11675    section.  */
11676
11677 static void
11678 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11679 {
11680   if (isec->size == 0
11681       && isec->output_section->size == 0
11682       && !(isec->output_section->flags & SEC_KEEP)
11683       && !bfd_section_removed_from_list (info->output_bfd,
11684                                          isec->output_section)
11685       && elf_section_data (isec->output_section)->dynindx == 0)
11686     {
11687       isec->output_section->flags |= SEC_EXCLUDE;
11688       bfd_section_list_remove (info->output_bfd, isec->output_section);
11689       info->output_bfd->section_count--;
11690     }
11691 }
11692
11693 /* Determine and set the size of the stub section for a final link.
11694
11695    The basic idea here is to examine all the relocations looking for
11696    PC-relative calls to a target that is unreachable with a "bl"
11697    instruction.  */
11698
11699 bfd_boolean
11700 ppc64_elf_size_stubs (struct bfd_link_info *info)
11701 {
11702   bfd_size_type stub_group_size;
11703   bfd_boolean stubs_always_before_branch;
11704   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11705
11706   if (htab == NULL)
11707     return FALSE;
11708
11709   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
11710     htab->params->plt_thread_safe = 1;
11711   if (!htab->opd_abi)
11712     htab->params->plt_thread_safe = 0;
11713   else if (htab->params->plt_thread_safe == -1)
11714     {
11715       static const char *const thread_starter[] =
11716         {
11717           "pthread_create",
11718           /* libstdc++ */
11719           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11720           /* librt */
11721           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11722           "mq_notify", "create_timer",
11723           /* libanl */
11724           "getaddrinfo_a",
11725           /* libgomp */
11726           "GOMP_parallel",
11727           "GOMP_parallel_start",
11728           "GOMP_parallel_loop_static",
11729           "GOMP_parallel_loop_static_start",
11730           "GOMP_parallel_loop_dynamic",
11731           "GOMP_parallel_loop_dynamic_start",
11732           "GOMP_parallel_loop_guided",
11733           "GOMP_parallel_loop_guided_start",
11734           "GOMP_parallel_loop_runtime",
11735           "GOMP_parallel_loop_runtime_start",
11736           "GOMP_parallel_sections",
11737           "GOMP_parallel_sections_start",
11738           /* libgo */
11739           "__go_go",
11740         };
11741       unsigned i;
11742
11743       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
11744         {
11745           struct elf_link_hash_entry *h;
11746           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11747                                     FALSE, FALSE, TRUE);
11748           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
11749           if (htab->params->plt_thread_safe)
11750             break;
11751         }
11752     }
11753   stubs_always_before_branch = htab->params->group_size < 0;
11754   if (htab->params->group_size < 0)
11755     stub_group_size = -htab->params->group_size;
11756   else
11757     stub_group_size = htab->params->group_size;
11758
11759   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
11760     return FALSE;
11761
11762 #define STUB_SHRINK_ITER 20
11763   /* Loop until no stubs added.  After iteration 20 of this loop we may
11764      exit on a stub section shrinking.  This is to break out of a
11765      pathological case where adding stubs on one iteration decreases
11766      section gaps (perhaps due to alignment), which then requires
11767      fewer or smaller stubs on the next iteration.  */
11768
11769   while (1)
11770     {
11771       bfd *input_bfd;
11772       unsigned int bfd_indx;
11773       struct map_stub *group;
11774
11775       htab->stub_iteration += 1;
11776
11777       for (input_bfd = info->input_bfds, bfd_indx = 0;
11778            input_bfd != NULL;
11779            input_bfd = input_bfd->link.next, bfd_indx++)
11780         {
11781           Elf_Internal_Shdr *symtab_hdr;
11782           asection *section;
11783           Elf_Internal_Sym *local_syms = NULL;
11784
11785           if (!is_ppc64_elf (input_bfd))
11786             continue;
11787
11788           /* We'll need the symbol table in a second.  */
11789           symtab_hdr = &elf_symtab_hdr (input_bfd);
11790           if (symtab_hdr->sh_info == 0)
11791             continue;
11792
11793           /* Walk over each section attached to the input bfd.  */
11794           for (section = input_bfd->sections;
11795                section != NULL;
11796                section = section->next)
11797             {
11798               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11799
11800               /* If there aren't any relocs, then there's nothing more
11801                  to do.  */
11802               if ((section->flags & SEC_RELOC) == 0
11803                   || (section->flags & SEC_ALLOC) == 0
11804                   || (section->flags & SEC_LOAD) == 0
11805                   || (section->flags & SEC_CODE) == 0
11806                   || section->reloc_count == 0)
11807                 continue;
11808
11809               /* If this section is a link-once section that will be
11810                  discarded, then don't create any stubs.  */
11811               if (section->output_section == NULL
11812                   || section->output_section->owner != info->output_bfd)
11813                 continue;
11814
11815               /* Get the relocs.  */
11816               internal_relocs
11817                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11818                                              info->keep_memory);
11819               if (internal_relocs == NULL)
11820                 goto error_ret_free_local;
11821
11822               /* Now examine each relocation.  */
11823               irela = internal_relocs;
11824               irelaend = irela + section->reloc_count;
11825               for (; irela < irelaend; irela++)
11826                 {
11827                   enum elf_ppc64_reloc_type r_type;
11828                   unsigned int r_indx;
11829                   enum ppc_stub_type stub_type;
11830                   struct ppc_stub_hash_entry *stub_entry;
11831                   asection *sym_sec, *code_sec;
11832                   bfd_vma sym_value, code_value;
11833                   bfd_vma destination;
11834                   unsigned long local_off;
11835                   bfd_boolean ok_dest;
11836                   struct ppc_link_hash_entry *hash;
11837                   struct ppc_link_hash_entry *fdh;
11838                   struct elf_link_hash_entry *h;
11839                   Elf_Internal_Sym *sym;
11840                   char *stub_name;
11841                   const asection *id_sec;
11842                   struct _opd_sec_data *opd;
11843                   struct plt_entry *plt_ent;
11844
11845                   r_type = ELF64_R_TYPE (irela->r_info);
11846                   r_indx = ELF64_R_SYM (irela->r_info);
11847
11848                   if (r_type >= R_PPC64_max)
11849                     {
11850                       bfd_set_error (bfd_error_bad_value);
11851                       goto error_ret_free_internal;
11852                     }
11853
11854                   /* Only look for stubs on branch instructions.  */
11855                   if (r_type != R_PPC64_REL24
11856                       && r_type != R_PPC64_REL24_NOTOC
11857                       && r_type != R_PPC64_REL14
11858                       && r_type != R_PPC64_REL14_BRTAKEN
11859                       && r_type != R_PPC64_REL14_BRNTAKEN)
11860                     continue;
11861
11862                   /* Now determine the call target, its name, value,
11863                      section.  */
11864                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11865                                   r_indx, input_bfd))
11866                     goto error_ret_free_internal;
11867                   hash = (struct ppc_link_hash_entry *) h;
11868
11869                   ok_dest = FALSE;
11870                   fdh = NULL;
11871                   sym_value = 0;
11872                   if (hash == NULL)
11873                     {
11874                       sym_value = sym->st_value;
11875                       if (sym_sec != NULL
11876                           && sym_sec->output_section != NULL)
11877                         ok_dest = TRUE;
11878                     }
11879                   else if (hash->elf.root.type == bfd_link_hash_defined
11880                            || hash->elf.root.type == bfd_link_hash_defweak)
11881                     {
11882                       sym_value = hash->elf.root.u.def.value;
11883                       if (sym_sec->output_section != NULL)
11884                         ok_dest = TRUE;
11885                     }
11886                   else if (hash->elf.root.type == bfd_link_hash_undefweak
11887                            || hash->elf.root.type == bfd_link_hash_undefined)
11888                     {
11889                       /* Recognise an old ABI func code entry sym, and
11890                          use the func descriptor sym instead if it is
11891                          defined.  */
11892                       if (hash->elf.root.root.string[0] == '.'
11893                           && hash->oh != NULL)
11894                         {
11895                           fdh = ppc_follow_link (hash->oh);
11896                           if (fdh->elf.root.type == bfd_link_hash_defined
11897                               || fdh->elf.root.type == bfd_link_hash_defweak)
11898                             {
11899                               sym_sec = fdh->elf.root.u.def.section;
11900                               sym_value = fdh->elf.root.u.def.value;
11901                               if (sym_sec->output_section != NULL)
11902                                 ok_dest = TRUE;
11903                             }
11904                           else
11905                             fdh = NULL;
11906                         }
11907                     }
11908                   else
11909                     {
11910                       bfd_set_error (bfd_error_bad_value);
11911                       goto error_ret_free_internal;
11912                     }
11913
11914                   destination = 0;
11915                   local_off = 0;
11916                   if (ok_dest)
11917                     {
11918                       sym_value += irela->r_addend;
11919                       destination = (sym_value
11920                                      + sym_sec->output_offset
11921                                      + sym_sec->output_section->vma);
11922                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11923                                                             ? hash->elf.other
11924                                                             : sym->st_other);
11925                     }
11926
11927                   code_sec = sym_sec;
11928                   code_value = sym_value;
11929                   opd = get_opd_info (sym_sec);
11930                   if (opd != NULL)
11931                     {
11932                       bfd_vma dest;
11933
11934                       if (hash == NULL && opd->adjust != NULL)
11935                         {
11936                           long adjust = opd->adjust[OPD_NDX (sym_value)];
11937                           if (adjust == -1)
11938                             continue;
11939                           code_value += adjust;
11940                           sym_value += adjust;
11941                         }
11942                       dest = opd_entry_value (sym_sec, sym_value,
11943                                               &code_sec, &code_value, FALSE);
11944                       if (dest != (bfd_vma) -1)
11945                         {
11946                           destination = dest;
11947                           if (fdh != NULL)
11948                             {
11949                               /* Fixup old ABI sym to point at code
11950                                  entry.  */
11951                               hash->elf.root.type = bfd_link_hash_defweak;
11952                               hash->elf.root.u.def.section = code_sec;
11953                               hash->elf.root.u.def.value = code_value;
11954                             }
11955                         }
11956                     }
11957
11958                   /* Determine what (if any) linker stub is needed.  */
11959                   plt_ent = NULL;
11960                   stub_type = ppc_type_of_stub (section, irela, &hash,
11961                                                 &plt_ent, destination,
11962                                                 local_off);
11963
11964                   if (r_type == R_PPC64_REL24_NOTOC)
11965                     {
11966                       if (stub_type == ppc_stub_plt_call)
11967                         stub_type = ppc_stub_plt_call_notoc;
11968                       else if (stub_type == ppc_stub_long_branch
11969                                || (code_sec != NULL
11970                                    && code_sec->output_section != NULL
11971                                    && (((hash ? hash->elf.other : sym->st_other)
11972                                         & STO_PPC64_LOCAL_MASK)
11973                                        != 1 << STO_PPC64_LOCAL_BIT)))
11974                         stub_type = ppc_stub_long_branch_notoc;
11975                     }
11976                   else if (stub_type != ppc_stub_plt_call)
11977                     {
11978                       /* Check whether we need a TOC adjusting stub.
11979                          Since the linker pastes together pieces from
11980                          different object files when creating the
11981                          _init and _fini functions, it may be that a
11982                          call to what looks like a local sym is in
11983                          fact a call needing a TOC adjustment.  */
11984                       if ((code_sec != NULL
11985                            && code_sec->output_section != NULL
11986                            && (htab->sec_info[code_sec->id].toc_off
11987                                != htab->sec_info[section->id].toc_off)
11988                            && (code_sec->has_toc_reloc
11989                                || code_sec->makes_toc_func_call))
11990                           || (((hash ? hash->elf.other : sym->st_other)
11991                                & STO_PPC64_LOCAL_MASK)
11992                               == 1 << STO_PPC64_LOCAL_BIT))
11993                         stub_type = ppc_stub_long_branch_r2off;
11994                     }
11995
11996                   if (stub_type == ppc_stub_none)
11997                     continue;
11998
11999                   /* __tls_get_addr calls might be eliminated.  */
12000                   if (stub_type != ppc_stub_plt_call
12001                       && stub_type != ppc_stub_plt_call_notoc
12002                       && hash != NULL
12003                       && (hash == htab->tls_get_addr
12004                           || hash == htab->tls_get_addr_fd)
12005                       && section->has_tls_reloc
12006                       && irela != internal_relocs)
12007                     {
12008                       /* Get tls info.  */
12009                       unsigned char *tls_mask;
12010
12011                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12012                                          irela - 1, input_bfd))
12013                         goto error_ret_free_internal;
12014                       if ((*tls_mask & TLS_TLS) != 0)
12015                         continue;
12016                     }
12017
12018                   if (stub_type == ppc_stub_plt_call)
12019                     {
12020                       if (!htab->opd_abi
12021                           && htab->params->plt_localentry0 != 0
12022                           && is_elfv2_localentry0 (&hash->elf))
12023                         htab->has_plt_localentry0 = 1;
12024                       else if (irela + 1 < irelaend
12025                                && irela[1].r_offset == irela->r_offset + 4
12026                                && (ELF64_R_TYPE (irela[1].r_info)
12027                                    == R_PPC64_TOCSAVE))
12028                         {
12029                           if (!tocsave_find (htab, INSERT,
12030                                              &local_syms, irela + 1, input_bfd))
12031                             goto error_ret_free_internal;
12032                         }
12033                       else
12034                         stub_type = ppc_stub_plt_call_r2save;
12035                     }
12036
12037                   /* Support for grouping stub sections.  */
12038                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12039
12040                   /* Get the name of this stub.  */
12041                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12042                   if (!stub_name)
12043                     goto error_ret_free_internal;
12044
12045                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12046                                                      stub_name, FALSE, FALSE);
12047                   if (stub_entry != NULL)
12048                     {
12049                       enum ppc_stub_type old_type;
12050                       /* A stub has already been created, but it may
12051                          not be the required type.  We shouldn't be
12052                          transitioning from plt_call to long_branch
12053                          stubs or vice versa, but we might be
12054                          upgrading from plt_call to plt_call_r2save or
12055                          from long_branch to long_branch_r2off.  */
12056                       free (stub_name);
12057                       old_type = stub_entry->stub_type;
12058                       switch (old_type)
12059                         {
12060                         default:
12061                           abort ();
12062
12063                         case ppc_stub_save_res:
12064                           continue;
12065
12066                         case ppc_stub_plt_call:
12067                         case ppc_stub_plt_call_r2save:
12068                         case ppc_stub_plt_call_notoc:
12069                         case ppc_stub_plt_call_both:
12070                           if (stub_type == ppc_stub_plt_call)
12071                             continue;
12072                           else if (stub_type == ppc_stub_plt_call_r2save)
12073                             {
12074                               if (old_type == ppc_stub_plt_call_notoc)
12075                                 stub_type = ppc_stub_plt_call_both;
12076                             }
12077                           else if (stub_type == ppc_stub_plt_call_notoc)
12078                             {
12079                               if (old_type == ppc_stub_plt_call_r2save)
12080                                 stub_type = ppc_stub_plt_call_both;
12081                             }
12082                           else
12083                             abort ();
12084                           break;
12085
12086                         case ppc_stub_plt_branch:
12087                         case ppc_stub_plt_branch_r2off:
12088                         case ppc_stub_plt_branch_notoc:
12089                         case ppc_stub_plt_branch_both:
12090                           old_type += (ppc_stub_long_branch
12091                                        - ppc_stub_plt_branch);
12092                           /* Fall through.  */
12093                         case ppc_stub_long_branch:
12094                         case ppc_stub_long_branch_r2off:
12095                         case ppc_stub_long_branch_notoc:
12096                         case ppc_stub_long_branch_both:
12097                           if (stub_type == ppc_stub_long_branch)
12098                             continue;
12099                           else if (stub_type == ppc_stub_long_branch_r2off)
12100                             {
12101                               if (old_type == ppc_stub_long_branch_notoc)
12102                                 stub_type = ppc_stub_long_branch_both;
12103                             }
12104                           else if (stub_type == ppc_stub_long_branch_notoc)
12105                             {
12106                               if (old_type == ppc_stub_long_branch_r2off)
12107                                 stub_type = ppc_stub_long_branch_both;
12108                             }
12109                           else
12110                             abort ();
12111                           break;
12112                         }
12113                       if (old_type < stub_type)
12114                         stub_entry->stub_type = stub_type;
12115                       continue;
12116                     }
12117
12118                   stub_entry = ppc_add_stub (stub_name, section, info);
12119                   if (stub_entry == NULL)
12120                     {
12121                       free (stub_name);
12122                     error_ret_free_internal:
12123                       if (elf_section_data (section)->relocs == NULL)
12124                         free (internal_relocs);
12125                     error_ret_free_local:
12126                       if (local_syms != NULL
12127                           && (symtab_hdr->contents
12128                               != (unsigned char *) local_syms))
12129                         free (local_syms);
12130                       return FALSE;
12131                     }
12132
12133                   stub_entry->stub_type = stub_type;
12134                   if (stub_type >= ppc_stub_plt_call
12135                       && stub_type <= ppc_stub_plt_call_both)
12136                     {
12137                       stub_entry->target_value = sym_value;
12138                       stub_entry->target_section = sym_sec;
12139                     }
12140                   else
12141                     {
12142                       stub_entry->target_value = code_value;
12143                       stub_entry->target_section = code_sec;
12144                     }
12145                   stub_entry->h = hash;
12146                   stub_entry->plt_ent = plt_ent;
12147                   stub_entry->symtype
12148                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12149                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12150
12151                   if (stub_entry->h != NULL)
12152                     htab->stub_globals += 1;
12153                 }
12154
12155               /* We're done with the internal relocs, free them.  */
12156               if (elf_section_data (section)->relocs != internal_relocs)
12157                 free (internal_relocs);
12158             }
12159
12160           if (local_syms != NULL
12161               && symtab_hdr->contents != (unsigned char *) local_syms)
12162             {
12163               if (!info->keep_memory)
12164                 free (local_syms);
12165               else
12166                 symtab_hdr->contents = (unsigned char *) local_syms;
12167             }
12168         }
12169
12170       /* We may have added some stubs.  Find out the new size of the
12171          stub sections.  */
12172       for (group = htab->group; group != NULL; group = group->next)
12173         {
12174           group->lr_restore = 0;
12175           group->eh_size = 0;
12176           if (group->stub_sec != NULL)
12177             {
12178               asection *stub_sec = group->stub_sec;
12179
12180               if (htab->stub_iteration <= STUB_SHRINK_ITER
12181                   || stub_sec->rawsize < stub_sec->size)
12182                 /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12183                 stub_sec->rawsize = stub_sec->size;
12184               stub_sec->size = 0;
12185               stub_sec->reloc_count = 0;
12186               stub_sec->flags &= ~SEC_RELOC;
12187             }
12188         }
12189
12190       if (htab->stub_iteration <= STUB_SHRINK_ITER
12191           || htab->brlt->rawsize < htab->brlt->size)
12192         htab->brlt->rawsize = htab->brlt->size;
12193       htab->brlt->size = 0;
12194       htab->brlt->reloc_count = 0;
12195       htab->brlt->flags &= ~SEC_RELOC;
12196       if (htab->relbrlt != NULL)
12197         htab->relbrlt->size = 0;
12198
12199       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12200
12201       for (group = htab->group; group != NULL; group = group->next)
12202         if (group->needs_save_res)
12203           group->stub_sec->size += htab->sfpr->size;
12204
12205       if (info->emitrelocations
12206           && htab->glink != NULL && htab->glink->size != 0)
12207         {
12208           htab->glink->reloc_count = 1;
12209           htab->glink->flags |= SEC_RELOC;
12210         }
12211
12212       if (htab->glink_eh_frame != NULL
12213           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12214           && htab->glink_eh_frame->output_section->size > 8)
12215         {
12216           size_t size = 0, align = 4;
12217
12218           for (group = htab->group; group != NULL; group = group->next)
12219             if (group->eh_size != 0)
12220               size += (group->eh_size + 17 + align - 1) & -align;
12221           if (htab->glink != NULL && htab->glink->size != 0)
12222             size += (24 + align - 1) & -align;
12223           if (size != 0)
12224             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12225           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12226           size = (size + align - 1) & -align;
12227           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12228           htab->glink_eh_frame->size = size;
12229         }
12230
12231       if (htab->params->plt_stub_align != 0)
12232         for (group = htab->group; group != NULL; group = group->next)
12233           if (group->stub_sec != NULL)
12234             {
12235               int align = abs (htab->params->plt_stub_align);
12236               group->stub_sec->size
12237                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12238             }
12239
12240       for (group = htab->group; group != NULL; group = group->next)
12241         if (group->stub_sec != NULL
12242             && group->stub_sec->rawsize != group->stub_sec->size
12243             && (htab->stub_iteration <= STUB_SHRINK_ITER
12244                 || group->stub_sec->rawsize < group->stub_sec->size))
12245           break;
12246
12247       if (group == NULL
12248           && (htab->brlt->rawsize == htab->brlt->size
12249               || (htab->stub_iteration > STUB_SHRINK_ITER
12250                   && htab->brlt->rawsize > htab->brlt->size))
12251           && (htab->glink_eh_frame == NULL
12252               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12253         break;
12254
12255       /* Ask the linker to do its stuff.  */
12256       (*htab->params->layout_sections_again) ();
12257     }
12258
12259   if (htab->glink_eh_frame != NULL
12260       && htab->glink_eh_frame->size != 0)
12261     {
12262       bfd_vma val;
12263       bfd_byte *p, *last_fde;
12264       size_t last_fde_len, size, align, pad;
12265       struct map_stub *group;
12266
12267       /* It is necessary to at least have a rough outline of the
12268          linker generated CIEs and FDEs written before
12269          bfd_elf_discard_info is run, in order for these FDEs to be
12270          indexed in .eh_frame_hdr.  */
12271       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12272       if (p == NULL)
12273         return FALSE;
12274       htab->glink_eh_frame->contents = p;
12275       last_fde = p;
12276       align = 4;
12277
12278       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12279       /* CIE length (rewrite in case little-endian).  */
12280       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12281       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12282       p += last_fde_len + 4;
12283
12284       for (group = htab->group; group != NULL; group = group->next)
12285         if (group->eh_size != 0)
12286           {
12287             group->eh_base = p - htab->glink_eh_frame->contents;
12288             last_fde = p;
12289             last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
12290             /* FDE length.  */
12291             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12292             p += 4;
12293             /* CIE pointer.  */
12294             val = p - htab->glink_eh_frame->contents;
12295             bfd_put_32 (htab->elf.dynobj, val, p);
12296             p += 4;
12297             /* Offset to stub section, written later.  */
12298             p += 4;
12299             /* stub section size.  */
12300             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12301             p += 4;
12302             /* Augmentation.  */
12303             p += 1;
12304             /* Make sure we don't have all nops.  This is enough for
12305                elf-eh-frame.c to detect the last non-nop opcode.  */
12306             p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
12307             p = last_fde + last_fde_len + 4;
12308           }
12309       if (htab->glink != NULL && htab->glink->size != 0)
12310         {
12311           last_fde = p;
12312           last_fde_len = ((24 + align - 1) & -align) - 4;
12313           /* FDE length.  */
12314           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12315           p += 4;
12316           /* CIE pointer.  */
12317           val = p - htab->glink_eh_frame->contents;
12318           bfd_put_32 (htab->elf.dynobj, val, p);
12319           p += 4;
12320           /* Offset to .glink, written later.  */
12321           p += 4;
12322           /* .glink size.  */
12323           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12324           p += 4;
12325           /* Augmentation.  */
12326           p += 1;
12327
12328           *p++ = DW_CFA_advance_loc + 1;
12329           *p++ = DW_CFA_register;
12330           *p++ = 65;
12331           *p++ = htab->opd_abi ? 12 : 0;
12332           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12333           *p++ = DW_CFA_restore_extended;
12334           *p++ = 65;
12335           p += ((24 + align - 1) & -align) - 24;
12336         }
12337       /* Subsume any padding into the last FDE if user .eh_frame
12338          sections are aligned more than glink_eh_frame.  Otherwise any
12339          zero padding will be seen as a terminator.  */
12340       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12341       size = p - htab->glink_eh_frame->contents;
12342       pad = ((size + align - 1) & -align) - size;
12343       htab->glink_eh_frame->size = size + pad;
12344       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12345     }
12346
12347   maybe_strip_output (info, htab->brlt);
12348   if (htab->glink_eh_frame != NULL)
12349     maybe_strip_output (info, htab->glink_eh_frame);
12350
12351   return TRUE;
12352 }
12353
12354 /* Called after we have determined section placement.  If sections
12355    move, we'll be called again.  Provide a value for TOCstart.  */
12356
12357 bfd_vma
12358 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12359 {
12360   asection *s;
12361   bfd_vma TOCstart, adjust;
12362
12363   if (info != NULL)
12364     {
12365       struct elf_link_hash_entry *h;
12366       struct elf_link_hash_table *htab = elf_hash_table (info);
12367
12368       if (is_elf_hash_table (htab)
12369           && htab->hgot != NULL)
12370         h = htab->hgot;
12371       else
12372         {
12373           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12374           if (is_elf_hash_table (htab))
12375             htab->hgot = h;
12376         }
12377       if (h != NULL
12378           && h->root.type == bfd_link_hash_defined
12379           && !h->root.linker_def
12380           && (!is_elf_hash_table (htab)
12381               || h->def_regular))
12382         {
12383           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12384                       + h->root.u.def.section->output_offset
12385                       + h->root.u.def.section->output_section->vma);
12386           _bfd_set_gp_value (obfd, TOCstart);
12387           return TOCstart;
12388         }
12389     }
12390
12391   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12392      order.  The TOC starts where the first of these sections starts.  */
12393   s = bfd_get_section_by_name (obfd, ".got");
12394   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12395     s = bfd_get_section_by_name (obfd, ".toc");
12396   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12397     s = bfd_get_section_by_name (obfd, ".tocbss");
12398   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12399     s = bfd_get_section_by_name (obfd, ".plt");
12400   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12401     {
12402       /* This may happen for
12403          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12404          .toc directive
12405          o  bad linker script
12406          o --gc-sections and empty TOC sections
12407
12408          FIXME: Warn user?  */
12409
12410       /* Look for a likely section.  We probably won't even be
12411          using TOCstart.  */
12412       for (s = obfd->sections; s != NULL; s = s->next)
12413         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12414                          | SEC_EXCLUDE))
12415             == (SEC_ALLOC | SEC_SMALL_DATA))
12416           break;
12417       if (s == NULL)
12418         for (s = obfd->sections; s != NULL; s = s->next)
12419           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12420               == (SEC_ALLOC | SEC_SMALL_DATA))
12421             break;
12422       if (s == NULL)
12423         for (s = obfd->sections; s != NULL; s = s->next)
12424           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12425               == SEC_ALLOC)
12426             break;
12427       if (s == NULL)
12428         for (s = obfd->sections; s != NULL; s = s->next)
12429           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12430             break;
12431     }
12432
12433   TOCstart = 0;
12434   if (s != NULL)
12435     TOCstart = s->output_section->vma + s->output_offset;
12436
12437   /* Force alignment.  */
12438   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12439   TOCstart -= adjust;
12440   _bfd_set_gp_value (obfd, TOCstart);
12441
12442   if (info != NULL && s != NULL)
12443     {
12444       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12445
12446       if (htab != NULL)
12447         {
12448           if (htab->elf.hgot != NULL)
12449             {
12450               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12451               htab->elf.hgot->root.u.def.section = s;
12452             }
12453         }
12454       else
12455         {
12456           struct bfd_link_hash_entry *bh = NULL;
12457           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12458                                             s, TOC_BASE_OFF - adjust,
12459                                             NULL, FALSE, FALSE, &bh);
12460         }
12461     }
12462   return TOCstart;
12463 }
12464
12465 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12466    write out any global entry stubs, and PLT relocations.  */
12467
12468 static bfd_boolean
12469 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
12470 {
12471   struct bfd_link_info *info;
12472   struct ppc_link_hash_table *htab;
12473   struct plt_entry *ent;
12474   asection *s;
12475
12476   if (h->root.type == bfd_link_hash_indirect)
12477     return TRUE;
12478
12479   info = inf;
12480   htab = ppc_hash_table (info);
12481   if (htab == NULL)
12482     return FALSE;
12483
12484   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12485     if (ent->plt.offset != (bfd_vma) -1)
12486       {
12487         /* This symbol has an entry in the procedure linkage
12488            table.  Set it up.  */
12489         Elf_Internal_Rela rela;
12490         asection *plt, *relplt;
12491         bfd_byte *loc;
12492
12493         if (!htab->elf.dynamic_sections_created
12494             || h->dynindx == -1)
12495           {
12496             if (!(h->def_regular
12497                   && (h->root.type == bfd_link_hash_defined
12498                       || h->root.type == bfd_link_hash_defweak)))
12499               continue;
12500             if (h->type == STT_GNU_IFUNC)
12501               {
12502                 plt = htab->elf.iplt;
12503                 relplt = htab->elf.irelplt;
12504                 htab->local_ifunc_resolver = 1;
12505                 if (htab->opd_abi)
12506                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12507                 else
12508                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12509               }
12510             else
12511               {
12512                 plt = htab->pltlocal;
12513                 if (bfd_link_pic (info))
12514                   {
12515                     relplt = htab->relpltlocal;
12516                     if (htab->opd_abi)
12517                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12518                     else
12519                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12520                   }
12521                 else
12522                   relplt = NULL;
12523               }
12524             rela.r_addend = (h->root.u.def.value
12525                              + h->root.u.def.section->output_offset
12526                              + h->root.u.def.section->output_section->vma
12527                              + ent->addend);
12528
12529             if (relplt == NULL)
12530               {
12531                 loc = plt->contents + ent->plt.offset;
12532                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
12533                 if (htab->opd_abi)
12534                   {
12535                     bfd_vma toc = elf_gp (info->output_bfd);
12536                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
12537                     bfd_put_64 (info->output_bfd, toc, loc + 8);
12538                   }
12539               }
12540             else
12541               {
12542                 rela.r_offset = (plt->output_section->vma
12543                                  + plt->output_offset
12544                                  + ent->plt.offset);
12545                 loc = relplt->contents + (relplt->reloc_count++
12546                                           * sizeof (Elf64_External_Rela));
12547                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12548               }
12549           }
12550         else
12551           {
12552             rela.r_offset = (htab->elf.splt->output_section->vma
12553                              + htab->elf.splt->output_offset
12554                              + ent->plt.offset);
12555             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
12556             rela.r_addend = ent->addend;
12557             loc = (htab->elf.srelplt->contents
12558                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
12559                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
12560             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
12561               htab->maybe_local_ifunc_resolver = 1;
12562             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12563           }
12564       }
12565
12566   if (!h->pointer_equality_needed)
12567     return TRUE;
12568
12569   if (h->def_regular)
12570     return TRUE;
12571
12572   s = htab->global_entry;
12573   if (s == NULL || s->size == 0)
12574     return TRUE;
12575
12576   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
12577     if (ent->plt.offset != (bfd_vma) -1
12578         && ent->addend == 0)
12579       {
12580         bfd_byte *p;
12581         asection *plt;
12582         bfd_vma off;
12583
12584         p = s->contents + h->root.u.def.value;
12585         plt = htab->elf.splt;
12586         if (!htab->elf.dynamic_sections_created
12587             || h->dynindx == -1)
12588           {
12589             if (h->type == STT_GNU_IFUNC)
12590               plt = htab->elf.iplt;
12591             else
12592               plt = htab->pltlocal;
12593           }
12594         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
12595         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12596
12597         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12598           {
12599             info->callbacks->einfo
12600               (_("%P: linkage table error against `%pT'\n"),
12601                h->root.root.string);
12602             bfd_set_error (bfd_error_bad_value);
12603             htab->stub_error = TRUE;
12604           }
12605
12606         htab->stub_count[ppc_stub_global_entry - 1] += 1;
12607         if (htab->params->emit_stub_syms)
12608           {
12609             size_t len = strlen (h->root.root.string);
12610             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12611
12612             if (name == NULL)
12613               return FALSE;
12614
12615             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12616             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12617             if (h == NULL)
12618               return FALSE;
12619             if (h->root.type == bfd_link_hash_new)
12620               {
12621                 h->root.type = bfd_link_hash_defined;
12622                 h->root.u.def.section = s;
12623                 h->root.u.def.value = p - s->contents;
12624                 h->ref_regular = 1;
12625                 h->def_regular = 1;
12626                 h->ref_regular_nonweak = 1;
12627                 h->forced_local = 1;
12628                 h->non_elf = 0;
12629                 h->root.linker_def = 1;
12630               }
12631           }
12632
12633         if (PPC_HA (off) != 0)
12634           {
12635             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12636             p += 4;
12637           }
12638         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12639         p += 4;
12640         bfd_put_32 (s->owner, MTCTR_R12, p);
12641         p += 4;
12642         bfd_put_32 (s->owner, BCTR, p);
12643         break;
12644       }
12645   return TRUE;
12646 }
12647
12648 /* Write PLT relocs for locals.  */
12649
12650 static bfd_boolean
12651 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
12652 {
12653   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12654   bfd *ibfd;
12655
12656   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12657     {
12658       struct got_entry **lgot_ents, **end_lgot_ents;
12659       struct plt_entry **local_plt, **lplt, **end_local_plt;
12660       Elf_Internal_Shdr *symtab_hdr;
12661       bfd_size_type locsymcount;
12662       Elf_Internal_Sym *local_syms = NULL;
12663       struct plt_entry *ent;
12664
12665       if (!is_ppc64_elf (ibfd))
12666         continue;
12667
12668       lgot_ents = elf_local_got_ents (ibfd);
12669       if (!lgot_ents)
12670         continue;
12671
12672       symtab_hdr = &elf_symtab_hdr (ibfd);
12673       locsymcount = symtab_hdr->sh_info;
12674       end_lgot_ents = lgot_ents + locsymcount;
12675       local_plt = (struct plt_entry **) end_lgot_ents;
12676       end_local_plt = local_plt + locsymcount;
12677       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
12678         for (ent = *lplt; ent != NULL; ent = ent->next)
12679           if (ent->plt.offset != (bfd_vma) -1)
12680             {
12681               Elf_Internal_Sym *sym;
12682               asection *sym_sec;
12683               asection *plt, *relplt;
12684               bfd_byte *loc;
12685               bfd_vma val;
12686
12687               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
12688                               lplt - local_plt, ibfd))
12689                 {
12690                   if (local_syms != NULL
12691                       && symtab_hdr->contents != (unsigned char *) local_syms)
12692                     free (local_syms);
12693                   return FALSE;
12694                 }
12695
12696               val = sym->st_value + ent->addend;
12697               val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
12698               if (sym_sec != NULL && sym_sec->output_section != NULL)
12699                 val += sym_sec->output_offset + sym_sec->output_section->vma;
12700
12701               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12702                 {
12703                   htab->local_ifunc_resolver = 1;
12704                   plt = htab->elf.iplt;
12705                   relplt = htab->elf.irelplt;
12706                 }
12707               else
12708                 {
12709                   plt = htab->pltlocal;
12710                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
12711                 }
12712
12713               if (relplt == NULL)
12714                 {
12715                   loc = plt->contents + ent->plt.offset;
12716                   bfd_put_64 (info->output_bfd, val, loc);
12717                   if (htab->opd_abi)
12718                     {
12719                       bfd_vma toc = elf_gp (ibfd);
12720                       bfd_put_64 (info->output_bfd, toc, loc + 8);
12721                     }
12722                 }
12723               else
12724                 {
12725                   Elf_Internal_Rela rela;
12726                   rela.r_offset = (ent->plt.offset
12727                                    + plt->output_offset
12728                                    + plt->output_section->vma);
12729                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12730                     {
12731                       if (htab->opd_abi)
12732                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
12733                       else
12734                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12735                     }
12736                   else
12737                     {
12738                       if (htab->opd_abi)
12739                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
12740                       else
12741                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12742                     }
12743                   rela.r_addend = val;
12744                   loc = relplt->contents + (relplt->reloc_count++
12745                                             * sizeof (Elf64_External_Rela));
12746                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
12747                 }
12748             }
12749
12750       if (local_syms != NULL
12751           && symtab_hdr->contents != (unsigned char *) local_syms)
12752         {
12753           if (!info->keep_memory)
12754             free (local_syms);
12755           else
12756             symtab_hdr->contents = (unsigned char *) local_syms;
12757         }
12758     }
12759   return TRUE;
12760 }
12761
12762 /* Build all the stubs associated with the current output file.
12763    The stubs are kept in a hash table attached to the main linker
12764    hash table.  This function is called via gldelf64ppc_finish.  */
12765
12766 bfd_boolean
12767 ppc64_elf_build_stubs (struct bfd_link_info *info,
12768                        char **stats)
12769 {
12770   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12771   struct map_stub *group;
12772   asection *stub_sec;
12773   bfd_byte *p;
12774   int stub_sec_count = 0;
12775
12776   if (htab == NULL)
12777     return FALSE;
12778
12779   /* Allocate memory to hold the linker stubs.  */
12780   for (group = htab->group; group != NULL; group = group->next)
12781     {
12782       group->eh_size = 0;
12783       group->lr_restore = 0;
12784       if ((stub_sec = group->stub_sec) != NULL
12785           && stub_sec->size != 0)
12786         {
12787           stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
12788                                            stub_sec->size);
12789           if (stub_sec->contents == NULL)
12790             return FALSE;
12791           stub_sec->size = 0;
12792         }
12793     }
12794
12795   if (htab->glink != NULL && htab->glink->size != 0)
12796     {
12797       unsigned int indx;
12798       bfd_vma plt0;
12799
12800       /* Build the .glink plt call stub.  */
12801       if (htab->params->emit_stub_syms)
12802         {
12803           struct elf_link_hash_entry *h;
12804           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12805                                     TRUE, FALSE, FALSE);
12806           if (h == NULL)
12807             return FALSE;
12808           if (h->root.type == bfd_link_hash_new)
12809             {
12810               h->root.type = bfd_link_hash_defined;
12811               h->root.u.def.section = htab->glink;
12812               h->root.u.def.value = 8;
12813               h->ref_regular = 1;
12814               h->def_regular = 1;
12815               h->ref_regular_nonweak = 1;
12816               h->forced_local = 1;
12817               h->non_elf = 0;
12818               h->root.linker_def = 1;
12819             }
12820         }
12821       plt0 = (htab->elf.splt->output_section->vma
12822               + htab->elf.splt->output_offset
12823               - 16);
12824       if (info->emitrelocations)
12825         {
12826           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12827           if (r == NULL)
12828             return FALSE;
12829           r->r_offset = (htab->glink->output_offset
12830                          + htab->glink->output_section->vma);
12831           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12832           r->r_addend = plt0;
12833         }
12834       p = htab->glink->contents;
12835       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12836       bfd_put_64 (htab->glink->owner, plt0, p);
12837       p += 8;
12838       if (htab->opd_abi)
12839         {
12840           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12841           p += 4;
12842           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12843           p += 4;
12844           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12845           p += 4;
12846           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12847           p += 4;
12848           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12849           p += 4;
12850           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12851           p += 4;
12852           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12853           p += 4;
12854           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12855           p += 4;
12856           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12857           p += 4;
12858           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12859           p += 4;
12860         }
12861       else
12862         {
12863           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
12864           p += 4;
12865           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12866           p += 4;
12867           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12868           p += 4;
12869           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
12870           p += 4;
12871           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12872           p += 4;
12873           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
12874           p += 4;
12875           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
12876           p += 4;
12877           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12878           p += 4;
12879           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
12880           p += 4;
12881           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12882           p += 4;
12883           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
12884           p += 4;
12885           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12886           p += 4;
12887           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
12888           p += 4;
12889         }
12890       bfd_put_32 (htab->glink->owner, BCTR, p);
12891       p += 4;
12892       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
12893
12894       /* Build the .glink lazy link call stubs.  */
12895       indx = 0;
12896       while (p < htab->glink->contents + htab->glink->size)
12897         {
12898           if (htab->opd_abi)
12899             {
12900               if (indx < 0x8000)
12901                 {
12902                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12903                   p += 4;
12904                 }
12905               else
12906                 {
12907                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12908                   p += 4;
12909                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
12910                               p);
12911                   p += 4;
12912                 }
12913             }
12914           bfd_put_32 (htab->glink->owner,
12915                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12916           indx++;
12917           p += 4;
12918         }
12919     }
12920
12921   /* Build .glink global entry stubs, and PLT relocs for globals.  */
12922   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
12923
12924   if (!write_plt_relocs_for_local_syms (info))
12925     return FALSE;
12926
12927   if (htab->brlt != NULL && htab->brlt->size != 0)
12928     {
12929       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12930                                          htab->brlt->size);
12931       if (htab->brlt->contents == NULL)
12932         return FALSE;
12933     }
12934   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12935     {
12936       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12937                                             htab->relbrlt->size);
12938       if (htab->relbrlt->contents == NULL)
12939         return FALSE;
12940     }
12941
12942   /* Build the stubs as directed by the stub hash table.  */
12943   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12944
12945   for (group = htab->group; group != NULL; group = group->next)
12946     if (group->needs_save_res)
12947       group->stub_sec->size += htab->sfpr->size;
12948
12949   if (htab->relbrlt != NULL)
12950     htab->relbrlt->reloc_count = 0;
12951
12952   if (htab->params->plt_stub_align != 0)
12953     for (group = htab->group; group != NULL; group = group->next)
12954       if ((stub_sec = group->stub_sec) != NULL)
12955         {
12956           int align = abs (htab->params->plt_stub_align);
12957           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
12958         }
12959
12960   for (group = htab->group; group != NULL; group = group->next)
12961     if (group->needs_save_res)
12962       {
12963         stub_sec = group->stub_sec;
12964         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
12965                 htab->sfpr->contents, htab->sfpr->size);
12966         if (htab->params->emit_stub_syms)
12967           {
12968             unsigned int i;
12969
12970             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
12971               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
12972                 return FALSE;
12973           }
12974       }
12975
12976   if (htab->glink_eh_frame != NULL
12977       && htab->glink_eh_frame->size != 0)
12978     {
12979       bfd_vma val;
12980       size_t align = 4;
12981
12982       p = htab->glink_eh_frame->contents;
12983       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12984
12985       for (group = htab->group; group != NULL; group = group->next)
12986         if (group->eh_size != 0)
12987           {
12988             /* Offset to stub section.  */
12989             val = (group->stub_sec->output_section->vma
12990                    + group->stub_sec->output_offset);
12991             val -= (htab->glink_eh_frame->output_section->vma
12992                     + htab->glink_eh_frame->output_offset
12993                     + (p + 8 - htab->glink_eh_frame->contents));
12994             if (val + 0x80000000 > 0xffffffff)
12995               {
12996                 _bfd_error_handler
12997                   (_("%s offset too large for .eh_frame sdata4 encoding"),
12998                    group->stub_sec->name);
12999                 return FALSE;
13000               }
13001             bfd_put_32 (htab->elf.dynobj, val, p + 8);
13002             p += (group->eh_size + 17 + 3) & -4;
13003           }
13004       if (htab->glink != NULL && htab->glink->size != 0)
13005         {
13006           /* Offset to .glink.  */
13007           val = (htab->glink->output_section->vma
13008                  + htab->glink->output_offset
13009                  + 8);
13010           val -= (htab->glink_eh_frame->output_section->vma
13011                   + htab->glink_eh_frame->output_offset
13012                   + (p + 8 - htab->glink_eh_frame->contents));
13013           if (val + 0x80000000 > 0xffffffff)
13014             {
13015               _bfd_error_handler
13016                 (_("%s offset too large for .eh_frame sdata4 encoding"),
13017                  htab->glink->name);
13018               return FALSE;
13019             }
13020           bfd_put_32 (htab->elf.dynobj, val, p + 8);
13021           p += (24 + align - 1) & -align;
13022         }
13023     }
13024
13025   for (group = htab->group; group != NULL; group = group->next)
13026     if ((stub_sec = group->stub_sec) != NULL)
13027       {
13028         stub_sec_count += 1;
13029         if (stub_sec->rawsize != stub_sec->size
13030             && (htab->stub_iteration <= STUB_SHRINK_ITER
13031                 || stub_sec->rawsize < stub_sec->size))
13032           break;
13033       }
13034
13035   if (group != NULL)
13036     {
13037       htab->stub_error = TRUE;
13038       _bfd_error_handler (_("stubs don't match calculated size"));
13039     }
13040
13041   if (htab->stub_error)
13042     return FALSE;
13043
13044   if (stats != NULL)
13045     {
13046       size_t len;
13047       *stats = bfd_malloc (500);
13048       if (*stats == NULL)
13049         return FALSE;
13050
13051       len = sprintf (*stats,
13052                      ngettext ("linker stubs in %u group\n",
13053                                "linker stubs in %u groups\n",
13054                                stub_sec_count),
13055                      stub_sec_count);
13056       sprintf (*stats + len, _("  branch         %lu\n"
13057                                "  branch toc adj %lu\n"
13058                                "  branch notoc   %lu\n"
13059                                "  branch both    %lu\n"
13060                                "  long branch    %lu\n"
13061                                "  long toc adj   %lu\n"
13062                                "  long notoc     %lu\n"
13063                                "  long both      %lu\n"
13064                                "  plt call       %lu\n"
13065                                "  plt call save  %lu\n"
13066                                "  plt call notoc %lu\n"
13067                                "  plt call both  %lu\n"
13068                                "  global entry   %lu"),
13069                htab->stub_count[ppc_stub_long_branch - 1],
13070                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13071                htab->stub_count[ppc_stub_long_branch_notoc - 1],
13072                htab->stub_count[ppc_stub_long_branch_both - 1],
13073                htab->stub_count[ppc_stub_plt_branch - 1],
13074                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13075                htab->stub_count[ppc_stub_plt_branch_notoc - 1],
13076                htab->stub_count[ppc_stub_plt_branch_both - 1],
13077                htab->stub_count[ppc_stub_plt_call - 1],
13078                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13079                htab->stub_count[ppc_stub_plt_call_notoc - 1],
13080                htab->stub_count[ppc_stub_plt_call_both - 1],
13081                htab->stub_count[ppc_stub_global_entry - 1]);
13082     }
13083   return TRUE;
13084 }
13085
13086 /* What to do when ld finds relocations against symbols defined in
13087    discarded sections.  */
13088
13089 static unsigned int
13090 ppc64_elf_action_discarded (asection *sec)
13091 {
13092   if (strcmp (".opd", sec->name) == 0)
13093     return 0;
13094
13095   if (strcmp (".toc", sec->name) == 0)
13096     return 0;
13097
13098   if (strcmp (".toc1", sec->name) == 0)
13099     return 0;
13100
13101   return _bfd_elf_default_action_discarded (sec);
13102 }
13103
13104 /* The RELOCATE_SECTION function is called by the ELF backend linker
13105    to handle the relocations for a section.
13106
13107    The relocs are always passed as Rela structures; if the section
13108    actually uses Rel structures, the r_addend field will always be
13109    zero.
13110
13111    This function is responsible for adjust the section contents as
13112    necessary, and (if using Rela relocs and generating a
13113    relocatable output file) adjusting the reloc addend as
13114    necessary.
13115
13116    This function does not have to worry about setting the reloc
13117    address or the reloc symbol index.
13118
13119    LOCAL_SYMS is a pointer to the swapped in local symbols.
13120
13121    LOCAL_SECTIONS is an array giving the section in the input file
13122    corresponding to the st_shndx field of each local symbol.
13123
13124    The global hash table entry for the global symbols can be found
13125    via elf_sym_hashes (input_bfd).
13126
13127    When generating relocatable output, this function must handle
13128    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13129    going to be the section symbol corresponding to the output
13130    section, which means that the addend must be adjusted
13131    accordingly.  */
13132
13133 static bfd_boolean
13134 ppc64_elf_relocate_section (bfd *output_bfd,
13135                             struct bfd_link_info *info,
13136                             bfd *input_bfd,
13137                             asection *input_section,
13138                             bfd_byte *contents,
13139                             Elf_Internal_Rela *relocs,
13140                             Elf_Internal_Sym *local_syms,
13141                             asection **local_sections)
13142 {
13143   struct ppc_link_hash_table *htab;
13144   Elf_Internal_Shdr *symtab_hdr;
13145   struct elf_link_hash_entry **sym_hashes;
13146   Elf_Internal_Rela *rel;
13147   Elf_Internal_Rela *wrel;
13148   Elf_Internal_Rela *relend;
13149   Elf_Internal_Rela outrel;
13150   bfd_byte *loc;
13151   struct got_entry **local_got_ents;
13152   bfd_vma TOCstart;
13153   bfd_boolean ret = TRUE;
13154   bfd_boolean is_opd;
13155   /* Assume 'at' branch hints.  */
13156   bfd_boolean is_isa_v2 = TRUE;
13157   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13158
13159   /* Initialize howto table if needed.  */
13160   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13161     ppc_howto_init ();
13162
13163   htab = ppc_hash_table (info);
13164   if (htab == NULL)
13165     return FALSE;
13166
13167   /* Don't relocate stub sections.  */
13168   if (input_section->owner == htab->params->stub_bfd)
13169     return TRUE;
13170
13171   BFD_ASSERT (is_ppc64_elf (input_bfd));
13172
13173   local_got_ents = elf_local_got_ents (input_bfd);
13174   TOCstart = elf_gp (output_bfd);
13175   symtab_hdr = &elf_symtab_hdr (input_bfd);
13176   sym_hashes = elf_sym_hashes (input_bfd);
13177   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13178
13179   rel = wrel = relocs;
13180   relend = relocs + input_section->reloc_count;
13181   for (; rel < relend; wrel++, rel++)
13182     {
13183       enum elf_ppc64_reloc_type r_type;
13184       bfd_vma addend;
13185       bfd_reloc_status_type r;
13186       Elf_Internal_Sym *sym;
13187       asection *sec;
13188       struct elf_link_hash_entry *h_elf;
13189       struct ppc_link_hash_entry *h;
13190       struct ppc_link_hash_entry *fdh;
13191       const char *sym_name;
13192       unsigned long r_symndx, toc_symndx;
13193       bfd_vma toc_addend;
13194       unsigned char tls_mask, tls_gd, tls_type;
13195       unsigned char sym_type;
13196       bfd_vma relocation;
13197       bfd_boolean unresolved_reloc, save_unresolved_reloc;
13198       bfd_boolean warned;
13199       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13200       unsigned int insn;
13201       unsigned int mask;
13202       struct ppc_stub_hash_entry *stub_entry;
13203       bfd_vma max_br_offset;
13204       bfd_vma from;
13205       Elf_Internal_Rela orig_rel;
13206       reloc_howto_type *howto;
13207       struct reloc_howto_struct alt_howto;
13208
13209     again:
13210       orig_rel = *rel;
13211
13212       r_type = ELF64_R_TYPE (rel->r_info);
13213       r_symndx = ELF64_R_SYM (rel->r_info);
13214
13215       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13216          symbol of the previous ADDR64 reloc.  The symbol gives us the
13217          proper TOC base to use.  */
13218       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13219           && wrel != relocs
13220           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13221           && is_opd)
13222         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13223
13224       sym = NULL;
13225       sec = NULL;
13226       h_elf = NULL;
13227       sym_name = NULL;
13228       unresolved_reloc = FALSE;
13229       warned = FALSE;
13230
13231       if (r_symndx < symtab_hdr->sh_info)
13232         {
13233           /* It's a local symbol.  */
13234           struct _opd_sec_data *opd;
13235
13236           sym = local_syms + r_symndx;
13237           sec = local_sections[r_symndx];
13238           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13239           sym_type = ELF64_ST_TYPE (sym->st_info);
13240           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13241           opd = get_opd_info (sec);
13242           if (opd != NULL && opd->adjust != NULL)
13243             {
13244               long adjust = opd->adjust[OPD_NDX (sym->st_value
13245                                                  + rel->r_addend)];
13246               if (adjust == -1)
13247                 relocation = 0;
13248               else
13249                 {
13250                   /* If this is a relocation against the opd section sym
13251                      and we have edited .opd, adjust the reloc addend so
13252                      that ld -r and ld --emit-relocs output is correct.
13253                      If it is a reloc against some other .opd symbol,
13254                      then the symbol value will be adjusted later.  */
13255                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13256                     rel->r_addend += adjust;
13257                   else
13258                     relocation += adjust;
13259                 }
13260             }
13261         }
13262       else
13263         {
13264           bfd_boolean ignored;
13265
13266           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13267                                    r_symndx, symtab_hdr, sym_hashes,
13268                                    h_elf, sec, relocation,
13269                                    unresolved_reloc, warned, ignored);
13270           sym_name = h_elf->root.root.string;
13271           sym_type = h_elf->type;
13272           if (sec != NULL
13273               && sec->owner == output_bfd
13274               && strcmp (sec->name, ".opd") == 0)
13275             {
13276               /* This is a symbol defined in a linker script.  All
13277                  such are defined in output sections, even those
13278                  defined by simple assignment from a symbol defined in
13279                  an input section.  Transfer the symbol to an
13280                  appropriate input .opd section, so that a branch to
13281                  this symbol will be mapped to the location specified
13282                  by the opd entry.  */
13283               struct bfd_link_order *lo;
13284               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13285                 if (lo->type == bfd_indirect_link_order)
13286                   {
13287                     asection *isec = lo->u.indirect.section;
13288                     if (h_elf->root.u.def.value >= isec->output_offset
13289                         && h_elf->root.u.def.value < (isec->output_offset
13290                                                       + isec->size))
13291                       {
13292                         h_elf->root.u.def.value -= isec->output_offset;
13293                         h_elf->root.u.def.section = isec;
13294                         sec = isec;
13295                         break;
13296                       }
13297                   }
13298             }
13299         }
13300       h = (struct ppc_link_hash_entry *) h_elf;
13301
13302       if (sec != NULL && discarded_section (sec))
13303         {
13304           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13305                                input_bfd, input_section,
13306                                contents + rel->r_offset);
13307           wrel->r_offset = rel->r_offset;
13308           wrel->r_info = 0;
13309           wrel->r_addend = 0;
13310
13311           /* For ld -r, remove relocations in debug sections against
13312              symbols defined in discarded sections.  Not done for
13313              non-debug to preserve relocs in .eh_frame which the
13314              eh_frame editing code expects to be present.  */
13315           if (bfd_link_relocatable (info)
13316               && (input_section->flags & SEC_DEBUGGING))
13317             wrel--;
13318
13319           continue;
13320         }
13321
13322       if (bfd_link_relocatable (info))
13323         goto copy_reloc;
13324
13325       if (h != NULL && &h->elf == htab->elf.hgot)
13326         {
13327           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13328           sec = bfd_abs_section_ptr;
13329           unresolved_reloc = FALSE;
13330         }
13331
13332       /* TLS optimizations.  Replace instruction sequences and relocs
13333          based on information we collected in tls_optimize.  We edit
13334          RELOCS so that --emit-relocs will output something sensible
13335          for the final instruction stream.  */
13336       tls_mask = 0;
13337       tls_gd = 0;
13338       toc_symndx = 0;
13339       if (h != NULL)
13340         tls_mask = h->tls_mask;
13341       else if (local_got_ents != NULL)
13342         {
13343           struct plt_entry **local_plt = (struct plt_entry **)
13344             (local_got_ents + symtab_hdr->sh_info);
13345           unsigned char *lgot_masks = (unsigned char *)
13346             (local_plt + symtab_hdr->sh_info);
13347           tls_mask = lgot_masks[r_symndx];
13348         }
13349       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13350           && (r_type == R_PPC64_TLS
13351               || r_type == R_PPC64_TLSGD
13352               || r_type == R_PPC64_TLSLD))
13353         {
13354           /* Check for toc tls entries.  */
13355           unsigned char *toc_tls;
13356
13357           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13358                              &local_syms, rel, input_bfd))
13359             return FALSE;
13360
13361           if (toc_tls)
13362             tls_mask = *toc_tls;
13363         }
13364
13365       /* Check that tls relocs are used with tls syms, and non-tls
13366          relocs are used with non-tls syms.  */
13367       if (r_symndx != STN_UNDEF
13368           && r_type != R_PPC64_NONE
13369           && (h == NULL
13370               || h->elf.root.type == bfd_link_hash_defined
13371               || h->elf.root.type == bfd_link_hash_defweak)
13372           && (IS_PPC64_TLS_RELOC (r_type)
13373               != (sym_type == STT_TLS
13374                   || (sym_type == STT_SECTION
13375                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13376         {
13377           if ((tls_mask & TLS_TLS) != 0
13378               && (r_type == R_PPC64_TLS
13379                   || r_type == R_PPC64_TLSGD
13380                   || r_type == R_PPC64_TLSLD))
13381             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13382             ;
13383           else
13384             info->callbacks->einfo
13385               (!IS_PPC64_TLS_RELOC (r_type)
13386                /* xgettext:c-format */
13387                ? _("%H: %s used with TLS symbol `%pT'\n")
13388                /* xgettext:c-format */
13389                : _("%H: %s used with non-TLS symbol `%pT'\n"),
13390                input_bfd, input_section, rel->r_offset,
13391                ppc64_elf_howto_table[r_type]->name,
13392                sym_name);
13393         }
13394
13395       /* Ensure reloc mapping code below stays sane.  */
13396       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13397           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13398           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13399           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13400           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13401           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13402           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13403           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13404           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13405           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13406         abort ();
13407
13408       switch (r_type)
13409         {
13410         default:
13411           break;
13412
13413         case R_PPC64_LO_DS_OPT:
13414           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13415           if ((insn & (0x3f << 26)) != 58u << 26)
13416             abort ();
13417           insn += (14u << 26) - (58u << 26);
13418           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13419           r_type = R_PPC64_TOC16_LO;
13420           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13421           break;
13422
13423         case R_PPC64_TOC16:
13424         case R_PPC64_TOC16_LO:
13425         case R_PPC64_TOC16_DS:
13426         case R_PPC64_TOC16_LO_DS:
13427           {
13428             /* Check for toc tls entries.  */
13429             unsigned char *toc_tls;
13430             int retval;
13431
13432             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13433                                    &local_syms, rel, input_bfd);
13434             if (retval == 0)
13435               return FALSE;
13436
13437             if (toc_tls)
13438               {
13439                 tls_mask = *toc_tls;
13440                 if (r_type == R_PPC64_TOC16_DS
13441                     || r_type == R_PPC64_TOC16_LO_DS)
13442                   {
13443                     if ((tls_mask & TLS_TLS) != 0
13444                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13445                       goto toctprel;
13446                   }
13447                 else
13448                   {
13449                     /* If we found a GD reloc pair, then we might be
13450                        doing a GD->IE transition.  */
13451                     if (retval == 2)
13452                       {
13453                         tls_gd = TLS_TPRELGD;
13454                         if ((tls_mask & TLS_TLS) != 0
13455                             && (tls_mask & TLS_GD) == 0)
13456                           goto tls_ldgd_opt;
13457                       }
13458                     else if (retval == 3)
13459                       {
13460                         if ((tls_mask & TLS_TLS) != 0
13461                             && (tls_mask & TLS_LD) == 0)
13462                           goto tls_ldgd_opt;
13463                       }
13464                   }
13465               }
13466           }
13467           break;
13468
13469         case R_PPC64_GOT_TPREL16_HI:
13470         case R_PPC64_GOT_TPREL16_HA:
13471           if ((tls_mask & TLS_TLS) != 0
13472               && (tls_mask & TLS_TPREL) == 0)
13473             {
13474               rel->r_offset -= d_offset;
13475               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13476               r_type = R_PPC64_NONE;
13477               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13478             }
13479           break;
13480
13481         case R_PPC64_GOT_TPREL16_DS:
13482         case R_PPC64_GOT_TPREL16_LO_DS:
13483           if ((tls_mask & TLS_TLS) != 0
13484               && (tls_mask & TLS_TPREL) == 0)
13485             {
13486             toctprel:
13487               insn = bfd_get_32 (input_bfd,
13488                                  contents + rel->r_offset - d_offset);
13489               insn &= 31 << 21;
13490               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13491               bfd_put_32 (input_bfd, insn,
13492                           contents + rel->r_offset - d_offset);
13493               r_type = R_PPC64_TPREL16_HA;
13494               if (toc_symndx != 0)
13495                 {
13496                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13497                   rel->r_addend = toc_addend;
13498                   /* We changed the symbol.  Start over in order to
13499                      get h, sym, sec etc. right.  */
13500                   goto again;
13501                 }
13502               else
13503                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13504             }
13505           break;
13506
13507         case R_PPC64_TLS:
13508           if ((tls_mask & TLS_TLS) != 0
13509               && (tls_mask & TLS_TPREL) == 0)
13510             {
13511               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13512               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13513               if (insn == 0)
13514                 abort ();
13515               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13516               /* Was PPC64_TLS which sits on insn boundary, now
13517                  PPC64_TPREL16_LO which is at low-order half-word.  */
13518               rel->r_offset += d_offset;
13519               r_type = R_PPC64_TPREL16_LO;
13520               if (toc_symndx != 0)
13521                 {
13522                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13523                   rel->r_addend = toc_addend;
13524                   /* We changed the symbol.  Start over in order to
13525                      get h, sym, sec etc. right.  */
13526                   goto again;
13527                 }
13528               else
13529                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13530             }
13531           break;
13532
13533         case R_PPC64_GOT_TLSGD16_HI:
13534         case R_PPC64_GOT_TLSGD16_HA:
13535           tls_gd = TLS_TPRELGD;
13536           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13537             goto tls_gdld_hi;
13538           break;
13539
13540         case R_PPC64_GOT_TLSLD16_HI:
13541         case R_PPC64_GOT_TLSLD16_HA:
13542           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13543             {
13544             tls_gdld_hi:
13545               if ((tls_mask & tls_gd) != 0)
13546                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13547                           + R_PPC64_GOT_TPREL16_DS);
13548               else
13549                 {
13550                   rel->r_offset -= d_offset;
13551                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13552                   r_type = R_PPC64_NONE;
13553                 }
13554               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13555             }
13556           break;
13557
13558         case R_PPC64_GOT_TLSGD16:
13559         case R_PPC64_GOT_TLSGD16_LO:
13560           tls_gd = TLS_TPRELGD;
13561           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13562             goto tls_ldgd_opt;
13563           break;
13564
13565         case R_PPC64_GOT_TLSLD16:
13566         case R_PPC64_GOT_TLSLD16_LO:
13567           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13568             {
13569               unsigned int insn1, insn2;
13570               bfd_vma offset;
13571
13572             tls_ldgd_opt:
13573               offset = (bfd_vma) -1;
13574               /* If not using the newer R_PPC64_TLSGD/LD to mark
13575                  __tls_get_addr calls, we must trust that the call
13576                  stays with its arg setup insns, ie. that the next
13577                  reloc is the __tls_get_addr call associated with
13578                  the current reloc.  Edit both insns.  */
13579               if (input_section->has_tls_get_addr_call
13580                   && rel + 1 < relend
13581                   && branch_reloc_hash_match (input_bfd, rel + 1,
13582                                               htab->tls_get_addr,
13583                                               htab->tls_get_addr_fd))
13584                 offset = rel[1].r_offset;
13585               /* We read the low GOT_TLS (or TOC16) insn because we
13586                  need to keep the destination reg.  It may be
13587                  something other than the usual r3, and moved to r3
13588                  before the call by intervening code.  */
13589               insn1 = bfd_get_32 (input_bfd,
13590                                   contents + rel->r_offset - d_offset);
13591               if ((tls_mask & tls_gd) != 0)
13592                 {
13593                   /* IE */
13594                   insn1 &= (0x1f << 21) | (0x1f << 16);
13595                   insn1 |= 58 << 26;    /* ld */
13596                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13597                   if (offset != (bfd_vma) -1)
13598                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13599                   if ((tls_mask & TLS_EXPLICIT) == 0)
13600                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13601                               + R_PPC64_GOT_TPREL16_DS);
13602                   else
13603                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13604                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13605                 }
13606               else
13607                 {
13608                   /* LE */
13609                   insn1 &= 0x1f << 21;
13610                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13611                   insn2 = 0x38630000;   /* addi 3,3,0 */
13612                   if (tls_gd == 0)
13613                     {
13614                       /* Was an LD reloc.  */
13615                       if (toc_symndx)
13616                         sec = local_sections[toc_symndx];
13617                       for (r_symndx = 0;
13618                            r_symndx < symtab_hdr->sh_info;
13619                            r_symndx++)
13620                         if (local_sections[r_symndx] == sec)
13621                           break;
13622                       if (r_symndx >= symtab_hdr->sh_info)
13623                         r_symndx = STN_UNDEF;
13624                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13625                       if (r_symndx != STN_UNDEF)
13626                         rel->r_addend -= (local_syms[r_symndx].st_value
13627                                           + sec->output_offset
13628                                           + sec->output_section->vma);
13629                     }
13630                   else if (toc_symndx != 0)
13631                     {
13632                       r_symndx = toc_symndx;
13633                       rel->r_addend = toc_addend;
13634                     }
13635                   r_type = R_PPC64_TPREL16_HA;
13636                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13637                   if (offset != (bfd_vma) -1)
13638                     {
13639                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13640                                                     R_PPC64_TPREL16_LO);
13641                       rel[1].r_offset = offset + d_offset;
13642                       rel[1].r_addend = rel->r_addend;
13643                     }
13644                 }
13645               bfd_put_32 (input_bfd, insn1,
13646                           contents + rel->r_offset - d_offset);
13647               if (offset != (bfd_vma) -1)
13648                 bfd_put_32 (input_bfd, insn2, contents + offset);
13649               if ((tls_mask & tls_gd) == 0
13650                   && (tls_gd == 0 || toc_symndx != 0))
13651                 {
13652                   /* We changed the symbol.  Start over in order
13653                      to get h, sym, sec etc. right.  */
13654                   goto again;
13655                 }
13656             }
13657           break;
13658
13659         case R_PPC64_TLSGD:
13660           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
13661               && rel + 1 < relend)
13662             {
13663               unsigned int insn2;
13664               bfd_vma offset = rel->r_offset;
13665
13666               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13667                 {
13668                   bfd_put_32 (output_bfd, NOP, contents + offset);
13669                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13670                   break;
13671                 }
13672
13673               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13674                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13675
13676               if ((tls_mask & TLS_TPRELGD) != 0)
13677                 {
13678                   /* IE */
13679                   r_type = R_PPC64_NONE;
13680                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13681                 }
13682               else
13683                 {
13684                   /* LE */
13685                   if (toc_symndx != 0)
13686                     {
13687                       r_symndx = toc_symndx;
13688                       rel->r_addend = toc_addend;
13689                     }
13690                   r_type = R_PPC64_TPREL16_LO;
13691                   rel->r_offset = offset + d_offset;
13692                   insn2 = 0x38630000;   /* addi 3,3,0 */
13693                 }
13694               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13695               /* Zap the reloc on the _tls_get_addr call too.  */
13696               BFD_ASSERT (offset == rel[1].r_offset);
13697               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13698               bfd_put_32 (input_bfd, insn2, contents + offset);
13699               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13700                 goto again;
13701             }
13702           break;
13703
13704         case R_PPC64_TLSLD:
13705           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
13706               && rel + 1 < relend)
13707             {
13708               unsigned int insn2;
13709               bfd_vma offset = rel->r_offset;
13710
13711               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
13712                 {
13713                   bfd_put_32 (output_bfd, NOP, contents + offset);
13714                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13715                   break;
13716                 }
13717
13718               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
13719                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
13720
13721               if (toc_symndx)
13722                 sec = local_sections[toc_symndx];
13723               for (r_symndx = 0;
13724                    r_symndx < symtab_hdr->sh_info;
13725                    r_symndx++)
13726                 if (local_sections[r_symndx] == sec)
13727                   break;
13728               if (r_symndx >= symtab_hdr->sh_info)
13729                 r_symndx = STN_UNDEF;
13730               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13731               if (r_symndx != STN_UNDEF)
13732                 rel->r_addend -= (local_syms[r_symndx].st_value
13733                                   + sec->output_offset
13734                                   + sec->output_section->vma);
13735
13736               r_type = R_PPC64_TPREL16_LO;
13737               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13738               rel->r_offset = offset + d_offset;
13739               /* Zap the reloc on the _tls_get_addr call too.  */
13740               BFD_ASSERT (offset == rel[1].r_offset);
13741               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13742               insn2 = 0x38630000;       /* addi 3,3,0 */
13743               bfd_put_32 (input_bfd, insn2, contents + offset);
13744               goto again;
13745             }
13746           break;
13747
13748         case R_PPC64_DTPMOD64:
13749           if (rel + 1 < relend
13750               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13751               && rel[1].r_offset == rel->r_offset + 8)
13752             {
13753               if ((tls_mask & TLS_GD) == 0)
13754                 {
13755                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13756                   if ((tls_mask & TLS_TPRELGD) != 0)
13757                     r_type = R_PPC64_TPREL64;
13758                   else
13759                     {
13760                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13761                       r_type = R_PPC64_NONE;
13762                     }
13763                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13764                 }
13765             }
13766           else
13767             {
13768               if ((tls_mask & TLS_LD) == 0)
13769                 {
13770                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13771                   r_type = R_PPC64_NONE;
13772                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13773                 }
13774             }
13775           break;
13776
13777         case R_PPC64_TPREL64:
13778           if ((tls_mask & TLS_TPREL) == 0)
13779             {
13780               r_type = R_PPC64_NONE;
13781               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13782             }
13783           break;
13784
13785         case R_PPC64_ENTRY:
13786           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13787           if (!bfd_link_pic (info)
13788               && !info->traditional_format
13789               && relocation + 0x80008000 <= 0xffffffff)
13790             {
13791               unsigned int insn1, insn2;
13792
13793               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13794               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13795               if ((insn1 & ~0xfffc) == LD_R2_0R12
13796                   && insn2 == ADD_R2_R2_R12)
13797                 {
13798                   bfd_put_32 (input_bfd,
13799                               LIS_R2 + PPC_HA (relocation),
13800                               contents + rel->r_offset);
13801                   bfd_put_32 (input_bfd,
13802                               ADDI_R2_R2 + PPC_LO (relocation),
13803                               contents + rel->r_offset + 4);
13804                 }
13805             }
13806           else
13807             {
13808               relocation -= (rel->r_offset
13809                              + input_section->output_offset
13810                              + input_section->output_section->vma);
13811               if (relocation + 0x80008000 <= 0xffffffff)
13812                 {
13813                   unsigned int insn1, insn2;
13814
13815                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13816                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13817                   if ((insn1 & ~0xfffc) == LD_R2_0R12
13818                       && insn2 == ADD_R2_R2_R12)
13819                     {
13820                       bfd_put_32 (input_bfd,
13821                                   ADDIS_R2_R12 + PPC_HA (relocation),
13822                                   contents + rel->r_offset);
13823                       bfd_put_32 (input_bfd,
13824                                   ADDI_R2_R2 + PPC_LO (relocation),
13825                                   contents + rel->r_offset + 4);
13826                     }
13827                 }
13828             }
13829           break;
13830
13831         case R_PPC64_REL16_HA:
13832           /* If we are generating a non-PIC executable, edit
13833              .  0:      addis 2,12,.TOC.-0b@ha
13834              .          addi 2,2,.TOC.-0b@l
13835              used by ELFv2 global entry points to set up r2, to
13836              .          lis 2,.TOC.@ha
13837              .          addi 2,2,.TOC.@l
13838              if .TOC. is in range.  */
13839           if (!bfd_link_pic (info)
13840               && !info->traditional_format
13841               && !htab->opd_abi
13842               && rel->r_addend == d_offset
13843               && h != NULL && &h->elf == htab->elf.hgot
13844               && rel + 1 < relend
13845               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
13846               && rel[1].r_offset == rel->r_offset + 4
13847               && rel[1].r_addend == rel->r_addend + 4
13848               && relocation + 0x80008000 <= 0xffffffff)
13849             {
13850               unsigned int insn1, insn2;
13851               bfd_vma offset = rel->r_offset - d_offset;
13852               insn1 = bfd_get_32 (input_bfd, contents + offset);
13853               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
13854               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
13855                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
13856                 {
13857                   r_type = R_PPC64_ADDR16_HA;
13858                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13859                   rel->r_addend -= d_offset;
13860                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
13861                   rel[1].r_addend -= d_offset + 4;
13862                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
13863                 }
13864             }
13865           break;
13866         }
13867
13868       /* Handle other relocations that tweak non-addend part of insn.  */
13869       insn = 0;
13870       max_br_offset = 1 << 25;
13871       addend = rel->r_addend;
13872       reloc_dest = DEST_NORMAL;
13873       switch (r_type)
13874         {
13875         default:
13876           break;
13877
13878         case R_PPC64_TOCSAVE:
13879           if (relocation + addend == (rel->r_offset
13880                                       + input_section->output_offset
13881                                       + input_section->output_section->vma)
13882               && tocsave_find (htab, NO_INSERT,
13883                                &local_syms, rel, input_bfd))
13884             {
13885               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13886               if (insn == NOP
13887                   || insn == CROR_151515 || insn == CROR_313131)
13888                 bfd_put_32 (input_bfd,
13889                             STD_R2_0R1 + STK_TOC (htab),
13890                             contents + rel->r_offset);
13891             }
13892           break;
13893
13894           /* Branch taken prediction relocations.  */
13895         case R_PPC64_ADDR14_BRTAKEN:
13896         case R_PPC64_REL14_BRTAKEN:
13897           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
13898           /* Fall through.  */
13899
13900           /* Branch not taken prediction relocations.  */
13901         case R_PPC64_ADDR14_BRNTAKEN:
13902         case R_PPC64_REL14_BRNTAKEN:
13903           insn |= bfd_get_32 (input_bfd,
13904                               contents + rel->r_offset) & ~(0x01 << 21);
13905           /* Fall through.  */
13906
13907         case R_PPC64_REL14:
13908           max_br_offset = 1 << 15;
13909           /* Fall through.  */
13910
13911         case R_PPC64_REL24:
13912         case R_PPC64_REL24_NOTOC:
13913         case R_PPC64_PLTCALL:
13914           /* Calls to functions with a different TOC, such as calls to
13915              shared objects, need to alter the TOC pointer.  This is
13916              done using a linkage stub.  A REL24 branching to these
13917              linkage stubs needs to be followed by a nop, as the nop
13918              will be replaced with an instruction to restore the TOC
13919              base pointer.  */
13920           fdh = h;
13921           if (h != NULL
13922               && h->oh != NULL
13923               && h->oh->is_func_descriptor)
13924             fdh = ppc_follow_link (h->oh);
13925           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13926                                            htab);
13927           if (r_type == R_PPC64_PLTCALL
13928               && stub_entry != NULL
13929               && stub_entry->stub_type >= ppc_stub_plt_call
13930               && stub_entry->stub_type <= ppc_stub_plt_call_both)
13931             stub_entry = NULL;
13932
13933           if (stub_entry != NULL
13934               && ((stub_entry->stub_type >= ppc_stub_plt_call
13935                    && stub_entry->stub_type <= ppc_stub_plt_call_both)
13936                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13937                   || stub_entry->stub_type == ppc_stub_plt_branch_both
13938                   || stub_entry->stub_type == ppc_stub_long_branch_r2off
13939                   || stub_entry->stub_type == ppc_stub_long_branch_both))
13940             {
13941               bfd_boolean can_plt_call = FALSE;
13942
13943               if (stub_entry->stub_type == ppc_stub_plt_call
13944                   && !htab->opd_abi
13945                   && htab->params->plt_localentry0 != 0
13946                   && is_elfv2_localentry0 (&h->elf))
13947                 {
13948                   /* The function doesn't use or change r2.  */
13949                   can_plt_call = TRUE;
13950                 }
13951               else if (r_type == R_PPC64_REL24_NOTOC)
13952                 {
13953                   /* NOTOC calls don't need to restore r2.  */
13954                   can_plt_call = TRUE;
13955                 }
13956
13957               /* All of these stubs may modify r2, so there must be a
13958                  branch and link followed by a nop.  The nop is
13959                  replaced by an insn to restore r2.  */
13960               else if (rel->r_offset + 8 <= input_section->size)
13961                 {
13962                   unsigned long br;
13963
13964                   br = bfd_get_32 (input_bfd,
13965                                    contents + rel->r_offset);
13966                   if ((br & 1) != 0)
13967                     {
13968                       unsigned long nop;
13969
13970                       nop = bfd_get_32 (input_bfd,
13971                                         contents + rel->r_offset + 4);
13972                       if (nop == LD_R2_0R1 + STK_TOC (htab))
13973                         can_plt_call = TRUE;
13974                       else if (nop == NOP
13975                                || nop == CROR_151515
13976                                || nop == CROR_313131)
13977                         {
13978                           if (h != NULL
13979                               && (h == htab->tls_get_addr_fd
13980                                   || h == htab->tls_get_addr)
13981                               && htab->params->tls_get_addr_opt)
13982                             {
13983                               /* Special stub used, leave nop alone.  */
13984                             }
13985                           else
13986                             bfd_put_32 (input_bfd,
13987                                         LD_R2_0R1 + STK_TOC (htab),
13988                                         contents + rel->r_offset + 4);
13989                           can_plt_call = TRUE;
13990                         }
13991                     }
13992                 }
13993
13994               if (!can_plt_call && h != NULL)
13995                 {
13996                   const char *name = h->elf.root.root.string;
13997
13998                   if (*name == '.')
13999                     ++name;
14000
14001                   if (strncmp (name, "__libc_start_main", 17) == 0
14002                       && (name[17] == 0 || name[17] == '@'))
14003                     {
14004                       /* Allow crt1 branch to go via a toc adjusting
14005                          stub.  Other calls that never return could do
14006                          the same, if we could detect such.  */
14007                       can_plt_call = TRUE;
14008                     }
14009                 }
14010
14011               if (!can_plt_call)
14012                 {
14013                   /* g++ as of 20130507 emits self-calls without a
14014                      following nop.  This is arguably wrong since we
14015                      have conflicting information.  On the one hand a
14016                      global symbol and on the other a local call
14017                      sequence, but don't error for this special case.
14018                      It isn't possible to cheaply verify we have
14019                      exactly such a call.  Allow all calls to the same
14020                      section.  */
14021                   asection *code_sec = sec;
14022
14023                   if (get_opd_info (sec) != NULL)
14024                     {
14025                       bfd_vma off = (relocation + addend
14026                                      - sec->output_section->vma
14027                                      - sec->output_offset);
14028
14029                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14030                     }
14031                   if (code_sec == input_section)
14032                     can_plt_call = TRUE;
14033                 }
14034
14035               if (!can_plt_call)
14036                 {
14037                   if (stub_entry->stub_type >= ppc_stub_plt_call
14038                       && stub_entry->stub_type <= ppc_stub_plt_call_both)
14039                     info->callbacks->einfo
14040                       /* xgettext:c-format */
14041                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14042                          "(plt call stub)\n"),
14043                        input_bfd, input_section, rel->r_offset, sym_name);
14044                   else
14045                     info->callbacks->einfo
14046                       /* xgettext:c-format */
14047                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14048                          "(toc save/adjust stub)\n"),
14049                        input_bfd, input_section, rel->r_offset, sym_name);
14050
14051                   bfd_set_error (bfd_error_bad_value);
14052                   ret = FALSE;
14053                 }
14054
14055               if (can_plt_call
14056                   && stub_entry->stub_type >= ppc_stub_plt_call
14057                   && stub_entry->stub_type <= ppc_stub_plt_call_both)
14058                 unresolved_reloc = FALSE;
14059             }
14060
14061           if ((stub_entry == NULL
14062                || stub_entry->stub_type == ppc_stub_long_branch
14063                || stub_entry->stub_type == ppc_stub_plt_branch)
14064               && get_opd_info (sec) != NULL)
14065             {
14066               /* The branch destination is the value of the opd entry. */
14067               bfd_vma off = (relocation + addend
14068                              - sec->output_section->vma
14069                              - sec->output_offset);
14070               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14071               if (dest != (bfd_vma) -1)
14072                 {
14073                   relocation = dest;
14074                   addend = 0;
14075                   reloc_dest = DEST_OPD;
14076                 }
14077             }
14078
14079           /* If the branch is out of reach we ought to have a long
14080              branch stub.  */
14081           from = (rel->r_offset
14082                   + input_section->output_offset
14083                   + input_section->output_section->vma);
14084
14085           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14086                                                   ? fdh->elf.other
14087                                                   : sym->st_other);
14088
14089           if (stub_entry != NULL
14090               && (stub_entry->stub_type == ppc_stub_long_branch
14091                   || stub_entry->stub_type == ppc_stub_plt_branch)
14092               && (r_type == R_PPC64_ADDR14_BRTAKEN
14093                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14094                   || (relocation + addend - from + max_br_offset
14095                       < 2 * max_br_offset)))
14096             /* Don't use the stub if this branch is in range.  */
14097             stub_entry = NULL;
14098
14099           if (stub_entry != NULL
14100               && (stub_entry->stub_type == ppc_stub_long_branch_notoc
14101                   || stub_entry->stub_type == ppc_stub_long_branch_both
14102                   || stub_entry->stub_type == ppc_stub_plt_branch_notoc
14103                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14104               && (r_type != R_PPC64_REL24_NOTOC
14105                   || ((fdh ? fdh->elf.other : sym->st_other)
14106                       & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
14107               && (relocation + addend - from + max_br_offset
14108                   < 2 * max_br_offset))
14109             stub_entry = NULL;
14110
14111           if (stub_entry != NULL
14112               && (stub_entry->stub_type == ppc_stub_long_branch_r2off
14113                   || stub_entry->stub_type == ppc_stub_long_branch_both
14114                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14115                   || stub_entry->stub_type == ppc_stub_plt_branch_both)
14116               && r_type == R_PPC64_REL24_NOTOC
14117               && (relocation + addend - from + max_br_offset
14118                   < 2 * max_br_offset))
14119             stub_entry = NULL;
14120
14121           if (stub_entry != NULL)
14122             {
14123               /* Munge up the value and addend so that we call the stub
14124                  rather than the procedure directly.  */
14125               asection *stub_sec = stub_entry->group->stub_sec;
14126
14127               if (stub_entry->stub_type == ppc_stub_save_res)
14128                 relocation += (stub_sec->output_offset
14129                                + stub_sec->output_section->vma
14130                                + stub_sec->size - htab->sfpr->size
14131                                - htab->sfpr->output_offset
14132                                - htab->sfpr->output_section->vma);
14133               else
14134                 relocation = (stub_entry->stub_offset
14135                               + stub_sec->output_offset
14136                               + stub_sec->output_section->vma);
14137               addend = 0;
14138               reloc_dest = DEST_STUB;
14139
14140               if (((stub_entry->stub_type == ppc_stub_plt_call
14141                     && ALWAYS_EMIT_R2SAVE)
14142                    || stub_entry->stub_type == ppc_stub_plt_call_r2save
14143                    || stub_entry->stub_type == ppc_stub_plt_call_both)
14144                   && !(h != NULL
14145                        && (h == htab->tls_get_addr_fd
14146                            || h == htab->tls_get_addr)
14147                        && htab->params->tls_get_addr_opt)
14148                   && rel + 1 < relend
14149                   && rel[1].r_offset == rel->r_offset + 4
14150                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14151                 relocation += 4;
14152               else if ((stub_entry->stub_type == ppc_stub_long_branch_both
14153                         || stub_entry->stub_type == ppc_stub_plt_branch_both
14154                         || stub_entry->stub_type == ppc_stub_plt_call_both)
14155                        && r_type == R_PPC64_REL24_NOTOC)
14156                 relocation += 4;
14157             }
14158
14159           if (insn != 0)
14160             {
14161               if (is_isa_v2)
14162                 {
14163                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14164                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14165                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14166                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14167                     insn |= 0x02 << 21;
14168                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14169                     insn |= 0x08 << 21;
14170                   else
14171                     break;
14172                 }
14173               else
14174                 {
14175                   /* Invert 'y' bit if not the default.  */
14176                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14177                     insn ^= 0x01 << 21;
14178                 }
14179
14180               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14181             }
14182
14183           /* NOP out calls to undefined weak functions.
14184              We can thus call a weak function without first
14185              checking whether the function is defined.  */
14186           else if (h != NULL
14187                    && h->elf.root.type == bfd_link_hash_undefweak
14188                    && h->elf.dynindx == -1
14189                    && (r_type == R_PPC64_REL24
14190                        || r_type == R_PPC64_REL24_NOTOC)
14191                    && relocation == 0
14192                    && addend == 0)
14193             {
14194               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14195               goto copy_reloc;
14196             }
14197           break;
14198         }
14199
14200       /* Set `addend'.  */
14201       tls_type = 0;
14202       save_unresolved_reloc = unresolved_reloc;
14203       switch (r_type)
14204         {
14205         default:
14206           /* xgettext:c-format */
14207           _bfd_error_handler (_("%pB: %s unsupported"),
14208                               input_bfd, ppc64_elf_howto_table[r_type]->name);
14209
14210           bfd_set_error (bfd_error_bad_value);
14211           ret = FALSE;
14212           goto copy_reloc;
14213
14214         case R_PPC64_NONE:
14215         case R_PPC64_TLS:
14216         case R_PPC64_TLSGD:
14217         case R_PPC64_TLSLD:
14218         case R_PPC64_TOCSAVE:
14219         case R_PPC64_GNU_VTINHERIT:
14220         case R_PPC64_GNU_VTENTRY:
14221         case R_PPC64_ENTRY:
14222           goto copy_reloc;
14223
14224           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14225              address in the GOT as relocation value instead of the
14226              symbol's value itself.  Also, create a GOT entry for the
14227              symbol and put the symbol value there.  */
14228         case R_PPC64_GOT_TLSGD16:
14229         case R_PPC64_GOT_TLSGD16_LO:
14230         case R_PPC64_GOT_TLSGD16_HI:
14231         case R_PPC64_GOT_TLSGD16_HA:
14232           tls_type = TLS_TLS | TLS_GD;
14233           goto dogot;
14234
14235         case R_PPC64_GOT_TLSLD16:
14236         case R_PPC64_GOT_TLSLD16_LO:
14237         case R_PPC64_GOT_TLSLD16_HI:
14238         case R_PPC64_GOT_TLSLD16_HA:
14239           tls_type = TLS_TLS | TLS_LD;
14240           goto dogot;
14241
14242         case R_PPC64_GOT_TPREL16_DS:
14243         case R_PPC64_GOT_TPREL16_LO_DS:
14244         case R_PPC64_GOT_TPREL16_HI:
14245         case R_PPC64_GOT_TPREL16_HA:
14246           tls_type = TLS_TLS | TLS_TPREL;
14247           goto dogot;
14248
14249         case R_PPC64_GOT_DTPREL16_DS:
14250         case R_PPC64_GOT_DTPREL16_LO_DS:
14251         case R_PPC64_GOT_DTPREL16_HI:
14252         case R_PPC64_GOT_DTPREL16_HA:
14253           tls_type = TLS_TLS | TLS_DTPREL;
14254           goto dogot;
14255
14256         case R_PPC64_GOT16:
14257         case R_PPC64_GOT16_LO:
14258         case R_PPC64_GOT16_HI:
14259         case R_PPC64_GOT16_HA:
14260         case R_PPC64_GOT16_DS:
14261         case R_PPC64_GOT16_LO_DS:
14262         dogot:
14263           {
14264             /* Relocation is to the entry for this symbol in the global
14265                offset table.  */
14266             asection *got;
14267             bfd_vma *offp;
14268             bfd_vma off;
14269             unsigned long indx = 0;
14270             struct got_entry *ent;
14271
14272             if (tls_type == (TLS_TLS | TLS_LD)
14273                 && (h == NULL
14274                     || !h->elf.def_dynamic))
14275               ent = ppc64_tlsld_got (input_bfd);
14276             else
14277               {
14278                 if (h != NULL)
14279                   {
14280                     if (!htab->elf.dynamic_sections_created
14281                         || h->elf.dynindx == -1
14282                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14283                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14284                       /* This is actually a static link, or it is a
14285                          -Bsymbolic link and the symbol is defined
14286                          locally, or the symbol was forced to be local
14287                          because of a version file.  */
14288                       ;
14289                     else
14290                       {
14291                         indx = h->elf.dynindx;
14292                         unresolved_reloc = FALSE;
14293                       }
14294                     ent = h->elf.got.glist;
14295                   }
14296                 else
14297                   {
14298                     if (local_got_ents == NULL)
14299                       abort ();
14300                     ent = local_got_ents[r_symndx];
14301                   }
14302
14303                 for (; ent != NULL; ent = ent->next)
14304                   if (ent->addend == orig_rel.r_addend
14305                       && ent->owner == input_bfd
14306                       && ent->tls_type == tls_type)
14307                     break;
14308               }
14309
14310             if (ent == NULL)
14311               abort ();
14312             if (ent->is_indirect)
14313               ent = ent->got.ent;
14314             offp = &ent->got.offset;
14315             got = ppc64_elf_tdata (ent->owner)->got;
14316             if (got == NULL)
14317               abort ();
14318
14319             /* The offset must always be a multiple of 8.  We use the
14320                least significant bit to record whether we have already
14321                processed this entry.  */
14322             off = *offp;
14323             if ((off & 1) != 0)
14324               off &= ~1;
14325             else
14326               {
14327                 /* Generate relocs for the dynamic linker, except in
14328                    the case of TLSLD where we'll use one entry per
14329                    module.  */
14330                 asection *relgot;
14331                 bfd_boolean ifunc;
14332
14333                 *offp = off | 1;
14334                 relgot = NULL;
14335                 ifunc = (h != NULL
14336                          ? h->elf.type == STT_GNU_IFUNC
14337                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14338                 if (ifunc)
14339                   {
14340                     relgot = htab->elf.irelplt;
14341                     if (indx == 0)
14342                       htab->local_ifunc_resolver = 1;
14343                     else if (is_static_defined (&h->elf))
14344                       htab->maybe_local_ifunc_resolver = 1;
14345                   }
14346                 else if (indx != 0
14347                          || (bfd_link_pic (info)
14348                              && (h == NULL
14349                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14350                                  || (tls_type == (TLS_TLS | TLS_LD)
14351                                      && !h->elf.def_dynamic))
14352                              && !(tls_type == (TLS_TLS | TLS_TPREL)
14353                                   && bfd_link_executable (info)
14354                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14355                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14356                 if (relgot != NULL)
14357                   {
14358                     outrel.r_offset = (got->output_section->vma
14359                                        + got->output_offset
14360                                        + off);
14361                     outrel.r_addend = addend;
14362                     if (tls_type & (TLS_LD | TLS_GD))
14363                       {
14364                         outrel.r_addend = 0;
14365                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14366                         if (tls_type == (TLS_TLS | TLS_GD))
14367                           {
14368                             loc = relgot->contents;
14369                             loc += (relgot->reloc_count++
14370                                     * sizeof (Elf64_External_Rela));
14371                             bfd_elf64_swap_reloca_out (output_bfd,
14372                                                        &outrel, loc);
14373                             outrel.r_offset += 8;
14374                             outrel.r_addend = addend;
14375                             outrel.r_info
14376                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14377                           }
14378                       }
14379                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14380                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14381                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14382                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14383                     else if (indx != 0)
14384                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14385                     else
14386                       {
14387                         if (ifunc)
14388                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14389                         else
14390                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14391
14392                         /* Write the .got section contents for the sake
14393                            of prelink.  */
14394                         loc = got->contents + off;
14395                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14396                                     loc);
14397                       }
14398
14399                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14400                       {
14401                         outrel.r_addend += relocation;
14402                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14403                           {
14404                             if (htab->elf.tls_sec == NULL)
14405                               outrel.r_addend = 0;
14406                             else
14407                               outrel.r_addend -= htab->elf.tls_sec->vma;
14408                           }
14409                       }
14410                     loc = relgot->contents;
14411                     loc += (relgot->reloc_count++
14412                             * sizeof (Elf64_External_Rela));
14413                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14414                   }
14415
14416                 /* Init the .got section contents here if we're not
14417                    emitting a reloc.  */
14418                 else
14419                   {
14420                     relocation += addend;
14421                     if (tls_type != 0)
14422                       {
14423                         if (htab->elf.tls_sec == NULL)
14424                           relocation = 0;
14425                         else
14426                           {
14427                             if (tls_type & TLS_LD)
14428                               relocation = 0;
14429                             else
14430                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14431                             if (tls_type & TLS_TPREL)
14432                               relocation += DTP_OFFSET - TP_OFFSET;
14433                           }
14434
14435                         if (tls_type & (TLS_GD | TLS_LD))
14436                           {
14437                             bfd_put_64 (output_bfd, relocation,
14438                                         got->contents + off + 8);
14439                             relocation = 1;
14440                           }
14441                       }
14442                     bfd_put_64 (output_bfd, relocation,
14443                                 got->contents + off);
14444                   }
14445               }
14446
14447             if (off >= (bfd_vma) -2)
14448               abort ();
14449
14450             relocation = got->output_section->vma + got->output_offset + off;
14451             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14452           }
14453           break;
14454
14455         case R_PPC64_PLT16_HA:
14456         case R_PPC64_PLT16_HI:
14457         case R_PPC64_PLT16_LO:
14458         case R_PPC64_PLT16_LO_DS:
14459         case R_PPC64_PLT32:
14460         case R_PPC64_PLT64:
14461         case R_PPC64_PLTSEQ:
14462         case R_PPC64_PLTCALL:
14463           /* Relocation is to the entry for this symbol in the
14464              procedure linkage table.  */
14465           unresolved_reloc = TRUE;
14466           {
14467             struct plt_entry **plt_list = NULL;
14468             if (h != NULL)
14469               plt_list = &h->elf.plt.plist;
14470             else if (local_got_ents != NULL)
14471               {
14472                 struct plt_entry **local_plt = (struct plt_entry **)
14473                   (local_got_ents + symtab_hdr->sh_info);
14474                 plt_list = local_plt + r_symndx;
14475               }
14476             if (plt_list)
14477               {
14478                 struct plt_entry *ent;
14479
14480                 for (ent = *plt_list; ent != NULL; ent = ent->next)
14481                   if (ent->plt.offset != (bfd_vma) -1
14482                       && ent->addend == orig_rel.r_addend)
14483                     {
14484                       asection *plt;
14485                       bfd_vma got;
14486
14487                       plt = htab->elf.splt;
14488                       if (!htab->elf.dynamic_sections_created
14489                           || h == NULL
14490                           || h->elf.dynindx == -1)
14491                         {
14492                           if (h != NULL
14493                               ? h->elf.type == STT_GNU_IFUNC
14494                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14495                             plt = htab->elf.iplt;
14496                           else
14497                             plt = htab->pltlocal;
14498                         }
14499                       relocation = (plt->output_section->vma
14500                                     + plt->output_offset
14501                                     + ent->plt.offset);
14502                       if (r_type == R_PPC64_PLT16_HA
14503                           || r_type == R_PPC64_PLT16_HI
14504                           || r_type == R_PPC64_PLT16_LO
14505                           || r_type == R_PPC64_PLT16_LO_DS)
14506                         {
14507                           got = (elf_gp (output_bfd)
14508                                  + htab->sec_info[input_section->id].toc_off);
14509                           relocation -= got;
14510                         }
14511                       addend = 0;
14512                       unresolved_reloc = FALSE;
14513                       break;
14514                     }
14515               }
14516           }
14517           break;
14518
14519         case R_PPC64_TOC:
14520           /* Relocation value is TOC base.  */
14521           relocation = TOCstart;
14522           if (r_symndx == STN_UNDEF)
14523             relocation += htab->sec_info[input_section->id].toc_off;
14524           else if (unresolved_reloc)
14525             ;
14526           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14527             relocation += htab->sec_info[sec->id].toc_off;
14528           else
14529             unresolved_reloc = TRUE;
14530           goto dodyn;
14531
14532           /* TOC16 relocs.  We want the offset relative to the TOC base,
14533              which is the address of the start of the TOC plus 0x8000.
14534              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14535              in this order.  */
14536         case R_PPC64_TOC16:
14537         case R_PPC64_TOC16_LO:
14538         case R_PPC64_TOC16_HI:
14539         case R_PPC64_TOC16_DS:
14540         case R_PPC64_TOC16_LO_DS:
14541         case R_PPC64_TOC16_HA:
14542           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14543           break;
14544
14545           /* Relocate against the beginning of the section.  */
14546         case R_PPC64_SECTOFF:
14547         case R_PPC64_SECTOFF_LO:
14548         case R_PPC64_SECTOFF_HI:
14549         case R_PPC64_SECTOFF_DS:
14550         case R_PPC64_SECTOFF_LO_DS:
14551         case R_PPC64_SECTOFF_HA:
14552           if (sec != NULL)
14553             addend -= sec->output_section->vma;
14554           break;
14555
14556         case R_PPC64_REL16:
14557         case R_PPC64_REL16_LO:
14558         case R_PPC64_REL16_HI:
14559         case R_PPC64_REL16_HA:
14560         case R_PPC64_REL16DX_HA:
14561           break;
14562
14563         case R_PPC64_REL14:
14564         case R_PPC64_REL14_BRNTAKEN:
14565         case R_PPC64_REL14_BRTAKEN:
14566         case R_PPC64_REL24:
14567         case R_PPC64_REL24_NOTOC:
14568           break;
14569
14570         case R_PPC64_TPREL16:
14571         case R_PPC64_TPREL16_LO:
14572         case R_PPC64_TPREL16_HI:
14573         case R_PPC64_TPREL16_HA:
14574         case R_PPC64_TPREL16_DS:
14575         case R_PPC64_TPREL16_LO_DS:
14576         case R_PPC64_TPREL16_HIGH:
14577         case R_PPC64_TPREL16_HIGHA:
14578         case R_PPC64_TPREL16_HIGHER:
14579         case R_PPC64_TPREL16_HIGHERA:
14580         case R_PPC64_TPREL16_HIGHEST:
14581         case R_PPC64_TPREL16_HIGHESTA:
14582           if (h != NULL
14583               && h->elf.root.type == bfd_link_hash_undefweak
14584               && h->elf.dynindx == -1)
14585             {
14586               /* Make this relocation against an undefined weak symbol
14587                  resolve to zero.  This is really just a tweak, since
14588                  code using weak externs ought to check that they are
14589                  defined before using them.  */
14590               bfd_byte *p = contents + rel->r_offset - d_offset;
14591
14592               insn = bfd_get_32 (input_bfd, p);
14593               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14594               if (insn != 0)
14595                 bfd_put_32 (input_bfd, insn, p);
14596               break;
14597             }
14598           if (htab->elf.tls_sec != NULL)
14599             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14600           /* The TPREL16 relocs shouldn't really be used in shared
14601              libs or with non-local symbols as that will result in
14602              DT_TEXTREL being set, but support them anyway.  */
14603           goto dodyn;
14604
14605         case R_PPC64_DTPREL16:
14606         case R_PPC64_DTPREL16_LO:
14607         case R_PPC64_DTPREL16_HI:
14608         case R_PPC64_DTPREL16_HA:
14609         case R_PPC64_DTPREL16_DS:
14610         case R_PPC64_DTPREL16_LO_DS:
14611         case R_PPC64_DTPREL16_HIGH:
14612         case R_PPC64_DTPREL16_HIGHA:
14613         case R_PPC64_DTPREL16_HIGHER:
14614         case R_PPC64_DTPREL16_HIGHERA:
14615         case R_PPC64_DTPREL16_HIGHEST:
14616         case R_PPC64_DTPREL16_HIGHESTA:
14617           if (htab->elf.tls_sec != NULL)
14618             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14619           break;
14620
14621         case R_PPC64_ADDR64_LOCAL:
14622           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14623                                               ? h->elf.other
14624                                               : sym->st_other);
14625           break;
14626
14627         case R_PPC64_DTPMOD64:
14628           relocation = 1;
14629           addend = 0;
14630           goto dodyn;
14631
14632         case R_PPC64_TPREL64:
14633           if (htab->elf.tls_sec != NULL)
14634             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14635           goto dodyn;
14636
14637         case R_PPC64_DTPREL64:
14638           if (htab->elf.tls_sec != NULL)
14639             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14640           /* Fall through.  */
14641
14642           /* Relocations that may need to be propagated if this is a
14643              dynamic object.  */
14644         case R_PPC64_REL30:
14645         case R_PPC64_REL32:
14646         case R_PPC64_REL64:
14647         case R_PPC64_ADDR14:
14648         case R_PPC64_ADDR14_BRNTAKEN:
14649         case R_PPC64_ADDR14_BRTAKEN:
14650         case R_PPC64_ADDR16:
14651         case R_PPC64_ADDR16_DS:
14652         case R_PPC64_ADDR16_HA:
14653         case R_PPC64_ADDR16_HI:
14654         case R_PPC64_ADDR16_HIGH:
14655         case R_PPC64_ADDR16_HIGHA:
14656         case R_PPC64_ADDR16_HIGHER:
14657         case R_PPC64_ADDR16_HIGHERA:
14658         case R_PPC64_ADDR16_HIGHEST:
14659         case R_PPC64_ADDR16_HIGHESTA:
14660         case R_PPC64_ADDR16_LO:
14661         case R_PPC64_ADDR16_LO_DS:
14662         case R_PPC64_ADDR24:
14663         case R_PPC64_ADDR32:
14664         case R_PPC64_ADDR64:
14665         case R_PPC64_UADDR16:
14666         case R_PPC64_UADDR32:
14667         case R_PPC64_UADDR64:
14668         dodyn:
14669           if ((input_section->flags & SEC_ALLOC) == 0)
14670             break;
14671
14672           if (NO_OPD_RELOCS && is_opd)
14673             break;
14674
14675           if (bfd_link_pic (info)
14676               ? ((h == NULL
14677                   || h->dyn_relocs != NULL)
14678                  && ((h != NULL && pc_dynrelocs (h))
14679                      || must_be_dyn_reloc (info, r_type)))
14680               : (h != NULL
14681                  ? h->dyn_relocs != NULL
14682                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14683             {
14684               bfd_boolean skip, relocate;
14685               asection *sreloc;
14686               bfd_vma out_off;
14687               long indx = 0;
14688
14689               /* When generating a dynamic object, these relocations
14690                  are copied into the output file to be resolved at run
14691                  time.  */
14692
14693               skip = FALSE;
14694               relocate = FALSE;
14695
14696               out_off = _bfd_elf_section_offset (output_bfd, info,
14697                                                  input_section, rel->r_offset);
14698               if (out_off == (bfd_vma) -1)
14699                 skip = TRUE;
14700               else if (out_off == (bfd_vma) -2)
14701                 skip = TRUE, relocate = TRUE;
14702               out_off += (input_section->output_section->vma
14703                           + input_section->output_offset);
14704               outrel.r_offset = out_off;
14705               outrel.r_addend = rel->r_addend;
14706
14707               /* Optimize unaligned reloc use.  */
14708               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14709                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14710                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14711               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14712                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14713                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14714               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14715                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14716                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14717
14718               if (skip)
14719                 memset (&outrel, 0, sizeof outrel);
14720               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14721                        && !is_opd
14722                        && r_type != R_PPC64_TOC)
14723                 {
14724                   indx = h->elf.dynindx;
14725                   BFD_ASSERT (indx != -1);
14726                   outrel.r_info = ELF64_R_INFO (indx, r_type);
14727                 }
14728               else
14729                 {
14730                   /* This symbol is local, or marked to become local,
14731                      or this is an opd section reloc which must point
14732                      at a local function.  */
14733                   outrel.r_addend += relocation;
14734                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14735                     {
14736                       if (is_opd && h != NULL)
14737                         {
14738                           /* Lie about opd entries.  This case occurs
14739                              when building shared libraries and we
14740                              reference a function in another shared
14741                              lib.  The same thing happens for a weak
14742                              definition in an application that's
14743                              overridden by a strong definition in a
14744                              shared lib.  (I believe this is a generic
14745                              bug in binutils handling of weak syms.)
14746                              In these cases we won't use the opd
14747                              entry in this lib.  */
14748                           unresolved_reloc = FALSE;
14749                         }
14750                       if (!is_opd
14751                           && r_type == R_PPC64_ADDR64
14752                           && (h != NULL
14753                               ? h->elf.type == STT_GNU_IFUNC
14754                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14755                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14756                       else
14757                         {
14758                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14759
14760                           /* We need to relocate .opd contents for ld.so.
14761                              Prelink also wants simple and consistent rules
14762                              for relocs.  This make all RELATIVE relocs have
14763                              *r_offset equal to r_addend.  */
14764                           relocate = TRUE;
14765                         }
14766                     }
14767                   else
14768                     {
14769                       if (h != NULL
14770                           ? h->elf.type == STT_GNU_IFUNC
14771                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14772                         {
14773                           info->callbacks->einfo
14774                             /* xgettext:c-format */
14775                             (_("%H: %s for indirect "
14776                                "function `%pT' unsupported\n"),
14777                              input_bfd, input_section, rel->r_offset,
14778                              ppc64_elf_howto_table[r_type]->name,
14779                              sym_name);
14780                           ret = FALSE;
14781                         }
14782                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14783                         ;
14784                       else if (sec == NULL || sec->owner == NULL)
14785                         {
14786                           bfd_set_error (bfd_error_bad_value);
14787                           return FALSE;
14788                         }
14789                       else
14790                         {
14791                           asection *osec;
14792
14793                           osec = sec->output_section;
14794                           indx = elf_section_data (osec)->dynindx;
14795
14796                           if (indx == 0)
14797                             {
14798                               if ((osec->flags & SEC_READONLY) == 0
14799                                   && htab->elf.data_index_section != NULL)
14800                                 osec = htab->elf.data_index_section;
14801                               else
14802                                 osec = htab->elf.text_index_section;
14803                               indx = elf_section_data (osec)->dynindx;
14804                             }
14805                           BFD_ASSERT (indx != 0);
14806
14807                           /* We are turning this relocation into one
14808                              against a section symbol, so subtract out
14809                              the output section's address but not the
14810                              offset of the input section in the output
14811                              section.  */
14812                           outrel.r_addend -= osec->vma;
14813                         }
14814
14815                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14816                     }
14817                 }
14818
14819               sreloc = elf_section_data (input_section)->sreloc;
14820               if (h != NULL
14821                   ? h->elf.type == STT_GNU_IFUNC
14822                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14823                 {
14824                   sreloc = htab->elf.irelplt;
14825                   if (indx == 0)
14826                     htab->local_ifunc_resolver = 1;
14827                   else if (is_static_defined (&h->elf))
14828                     htab->maybe_local_ifunc_resolver = 1;
14829                 }
14830               if (sreloc == NULL)
14831                 abort ();
14832
14833               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14834                   >= sreloc->size)
14835                 abort ();
14836               loc = sreloc->contents;
14837               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14838               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14839
14840               /* If this reloc is against an external symbol, it will
14841                  be computed at runtime, so there's no need to do
14842                  anything now.  However, for the sake of prelink ensure
14843                  that the section contents are a known value.  */
14844               if (!relocate)
14845                 {
14846                   unresolved_reloc = FALSE;
14847                   /* The value chosen here is quite arbitrary as ld.so
14848                      ignores section contents except for the special
14849                      case of .opd where the contents might be accessed
14850                      before relocation.  Choose zero, as that won't
14851                      cause reloc overflow.  */
14852                   relocation = 0;
14853                   addend = 0;
14854                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14855                      to improve backward compatibility with older
14856                      versions of ld.  */
14857                   if (r_type == R_PPC64_ADDR64)
14858                     addend = outrel.r_addend;
14859                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
14860                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
14861                     addend = outrel.r_offset;
14862                 }
14863             }
14864           break;
14865
14866         case R_PPC64_COPY:
14867         case R_PPC64_GLOB_DAT:
14868         case R_PPC64_JMP_SLOT:
14869         case R_PPC64_JMP_IREL:
14870         case R_PPC64_RELATIVE:
14871           /* We shouldn't ever see these dynamic relocs in relocatable
14872              files.  */
14873           /* Fall through.  */
14874
14875         case R_PPC64_PLTGOT16:
14876         case R_PPC64_PLTGOT16_DS:
14877         case R_PPC64_PLTGOT16_HA:
14878         case R_PPC64_PLTGOT16_HI:
14879         case R_PPC64_PLTGOT16_LO:
14880         case R_PPC64_PLTGOT16_LO_DS:
14881         case R_PPC64_PLTREL32:
14882         case R_PPC64_PLTREL64:
14883           /* These ones haven't been implemented yet.  */
14884
14885           info->callbacks->einfo
14886             /* xgettext:c-format */
14887             (_("%P: %pB: %s is not supported for `%pT'\n"),
14888              input_bfd,
14889              ppc64_elf_howto_table[r_type]->name, sym_name);
14890
14891           bfd_set_error (bfd_error_invalid_operation);
14892           ret = FALSE;
14893           goto copy_reloc;
14894         }
14895
14896       /* Multi-instruction sequences that access the TOC can be
14897          optimized, eg. addis ra,r2,0; addi rb,ra,x;
14898          to             nop;           addi rb,r2,x;  */
14899       switch (r_type)
14900         {
14901         default:
14902           break;
14903
14904         case R_PPC64_GOT_TLSLD16_HI:
14905         case R_PPC64_GOT_TLSGD16_HI:
14906         case R_PPC64_GOT_TPREL16_HI:
14907         case R_PPC64_GOT_DTPREL16_HI:
14908         case R_PPC64_GOT16_HI:
14909         case R_PPC64_TOC16_HI:
14910           /* These relocs would only be useful if building up an
14911              offset to later add to r2, perhaps in an indexed
14912              addressing mode instruction.  Don't try to optimize.
14913              Unfortunately, the possibility of someone building up an
14914              offset like this or even with the HA relocs, means that
14915              we need to check the high insn when optimizing the low
14916              insn.  */
14917           break;
14918
14919         case R_PPC64_PLTCALL:
14920           if (unresolved_reloc)
14921             {
14922               /* No plt entry.  Make this into a direct call.  */
14923               bfd_byte *p = contents + rel->r_offset;
14924               insn = bfd_get_32 (input_bfd, p);
14925               insn &= 1;
14926               bfd_put_32 (input_bfd, B_DOT | insn, p);
14927               bfd_put_32 (input_bfd, NOP, p + 4);
14928               unresolved_reloc = save_unresolved_reloc;
14929               r_type = R_PPC64_REL24;
14930             }
14931           break;
14932
14933         case R_PPC64_PLTSEQ:
14934           if (unresolved_reloc)
14935             {
14936               unresolved_reloc = FALSE;
14937               goto nop_it;
14938             }
14939           break;
14940
14941         case R_PPC64_PLT16_HA:
14942           if (unresolved_reloc)
14943             {
14944               unresolved_reloc = FALSE;
14945               goto nop_it;
14946             }
14947           /* Fall through.  */
14948         case R_PPC64_GOT_TLSLD16_HA:
14949         case R_PPC64_GOT_TLSGD16_HA:
14950         case R_PPC64_GOT_TPREL16_HA:
14951         case R_PPC64_GOT_DTPREL16_HA:
14952         case R_PPC64_GOT16_HA:
14953         case R_PPC64_TOC16_HA:
14954           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14955               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14956             {
14957               bfd_byte *p;
14958             nop_it:
14959               p = contents + (rel->r_offset & ~3);
14960               bfd_put_32 (input_bfd, NOP, p);
14961               goto copy_reloc;
14962             }
14963           break;
14964
14965         case R_PPC64_PLT16_LO:
14966         case R_PPC64_PLT16_LO_DS:
14967           if (unresolved_reloc)
14968             {
14969               unresolved_reloc = FALSE;
14970               goto nop_it;
14971             }
14972           /* Fall through.  */
14973         case R_PPC64_GOT_TLSLD16_LO:
14974         case R_PPC64_GOT_TLSGD16_LO:
14975         case R_PPC64_GOT_TPREL16_LO_DS:
14976         case R_PPC64_GOT_DTPREL16_LO_DS:
14977         case R_PPC64_GOT16_LO:
14978         case R_PPC64_GOT16_LO_DS:
14979         case R_PPC64_TOC16_LO:
14980         case R_PPC64_TOC16_LO_DS:
14981           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14982               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14983             {
14984               bfd_byte *p = contents + (rel->r_offset & ~3);
14985               insn = bfd_get_32 (input_bfd, p);
14986               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14987                 {
14988                   /* Transform addic to addi when we change reg.  */
14989                   insn &= ~((0x3f << 26) | (0x1f << 16));
14990                   insn |= (14u << 26) | (2 << 16);
14991                 }
14992               else
14993                 {
14994                   insn &= ~(0x1f << 16);
14995                   insn |= 2 << 16;
14996                 }
14997               bfd_put_32 (input_bfd, insn, p);
14998             }
14999           break;
15000
15001         case R_PPC64_TPREL16_HA:
15002           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15003             {
15004               bfd_byte *p = contents + (rel->r_offset & ~3);
15005               insn = bfd_get_32 (input_bfd, p);
15006               if ((insn & ((0x3f << 26) | 0x1f << 16))
15007                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15008                 /* xgettext:c-format */
15009                 info->callbacks->minfo
15010                   (_("%H: warning: %s unexpected insn %#x.\n"),
15011                    input_bfd, input_section, rel->r_offset,
15012                    ppc64_elf_howto_table[r_type]->name, insn);
15013               else
15014                 {
15015                   bfd_put_32 (input_bfd, NOP, p);
15016                   goto copy_reloc;
15017                 }
15018             }
15019           break;
15020
15021         case R_PPC64_TPREL16_LO:
15022         case R_PPC64_TPREL16_LO_DS:
15023           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15024             {
15025               bfd_byte *p = contents + (rel->r_offset & ~3);
15026               insn = bfd_get_32 (input_bfd, p);
15027               insn &= ~(0x1f << 16);
15028               insn |= 13 << 16;
15029               bfd_put_32 (input_bfd, insn, p);
15030             }
15031           break;
15032         }
15033
15034       /* Do any further special processing.  */
15035       switch (r_type)
15036         {
15037         default:
15038           break;
15039
15040         case R_PPC64_REL16_HA:
15041         case R_PPC64_REL16DX_HA:
15042         case R_PPC64_ADDR16_HA:
15043         case R_PPC64_ADDR16_HIGHA:
15044         case R_PPC64_ADDR16_HIGHERA:
15045         case R_PPC64_ADDR16_HIGHESTA:
15046         case R_PPC64_TOC16_HA:
15047         case R_PPC64_SECTOFF_HA:
15048         case R_PPC64_TPREL16_HA:
15049         case R_PPC64_TPREL16_HIGHA:
15050         case R_PPC64_TPREL16_HIGHERA:
15051         case R_PPC64_TPREL16_HIGHESTA:
15052         case R_PPC64_DTPREL16_HA:
15053         case R_PPC64_DTPREL16_HIGHA:
15054         case R_PPC64_DTPREL16_HIGHERA:
15055         case R_PPC64_DTPREL16_HIGHESTA:
15056           /* It's just possible that this symbol is a weak symbol
15057              that's not actually defined anywhere. In that case,
15058              'sec' would be NULL, and we should leave the symbol
15059              alone (it will be set to zero elsewhere in the link).  */
15060           if (sec == NULL)
15061             break;
15062           /* Fall through.  */
15063
15064         case R_PPC64_GOT16_HA:
15065         case R_PPC64_PLTGOT16_HA:
15066         case R_PPC64_PLT16_HA:
15067         case R_PPC64_GOT_TLSGD16_HA:
15068         case R_PPC64_GOT_TLSLD16_HA:
15069         case R_PPC64_GOT_TPREL16_HA:
15070         case R_PPC64_GOT_DTPREL16_HA:
15071           /* Add 0x10000 if sign bit in 0:15 is set.
15072              Bits 0:15 are not used.  */
15073           addend += 0x8000;
15074           break;
15075
15076         case R_PPC64_ADDR16_DS:
15077         case R_PPC64_ADDR16_LO_DS:
15078         case R_PPC64_GOT16_DS:
15079         case R_PPC64_GOT16_LO_DS:
15080         case R_PPC64_PLT16_LO_DS:
15081         case R_PPC64_SECTOFF_DS:
15082         case R_PPC64_SECTOFF_LO_DS:
15083         case R_PPC64_TOC16_DS:
15084         case R_PPC64_TOC16_LO_DS:
15085         case R_PPC64_PLTGOT16_DS:
15086         case R_PPC64_PLTGOT16_LO_DS:
15087         case R_PPC64_GOT_TPREL16_DS:
15088         case R_PPC64_GOT_TPREL16_LO_DS:
15089         case R_PPC64_GOT_DTPREL16_DS:
15090         case R_PPC64_GOT_DTPREL16_LO_DS:
15091         case R_PPC64_TPREL16_DS:
15092         case R_PPC64_TPREL16_LO_DS:
15093         case R_PPC64_DTPREL16_DS:
15094         case R_PPC64_DTPREL16_LO_DS:
15095           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15096           mask = 3;
15097           /* If this reloc is against an lq, lxv, or stxv insn, then
15098              the value must be a multiple of 16.  This is somewhat of
15099              a hack, but the "correct" way to do this by defining _DQ
15100              forms of all the _DS relocs bloats all reloc switches in
15101              this file.  It doesn't make much sense to use these
15102              relocs in data, so testing the insn should be safe.  */
15103           if ((insn & (0x3f << 26)) == (56u << 26)
15104               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15105             mask = 15;
15106           relocation += addend;
15107           addend = insn & (mask ^ 3);
15108           if ((relocation & mask) != 0)
15109             {
15110               relocation ^= relocation & mask;
15111               info->callbacks->einfo
15112                 /* xgettext:c-format */
15113                 (_("%H: error: %s not a multiple of %u\n"),
15114                  input_bfd, input_section, rel->r_offset,
15115                  ppc64_elf_howto_table[r_type]->name,
15116                  mask + 1);
15117               bfd_set_error (bfd_error_bad_value);
15118               ret = FALSE;
15119               goto copy_reloc;
15120             }
15121           break;
15122         }
15123
15124       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15125          because such sections are not SEC_ALLOC and thus ld.so will
15126          not process them.  */
15127       howto = ppc64_elf_howto_table[(int) r_type];
15128       if (unresolved_reloc
15129           && !((input_section->flags & SEC_DEBUGGING) != 0
15130                && h->elf.def_dynamic)
15131           && _bfd_elf_section_offset (output_bfd, info, input_section,
15132                                       rel->r_offset) != (bfd_vma) -1)
15133         {
15134           info->callbacks->einfo
15135             /* xgettext:c-format */
15136             (_("%H: unresolvable %s against `%pT'\n"),
15137              input_bfd, input_section, rel->r_offset,
15138              howto->name,
15139              h->elf.root.root.string);
15140           ret = FALSE;
15141         }
15142
15143       /* 16-bit fields in insns mostly have signed values, but a
15144          few insns have 16-bit unsigned values.  Really, we should
15145          have different reloc types.  */
15146       if (howto->complain_on_overflow != complain_overflow_dont
15147           && howto->dst_mask == 0xffff
15148           && (input_section->flags & SEC_CODE) != 0)
15149         {
15150           enum complain_overflow complain = complain_overflow_signed;
15151
15152           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15153           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15154             complain = complain_overflow_bitfield;
15155           else if (howto->rightshift == 0
15156                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15157                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15158                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15159                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15160                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15161                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15162             complain = complain_overflow_unsigned;
15163           if (howto->complain_on_overflow != complain)
15164             {
15165               alt_howto = *howto;
15166               alt_howto.complain_on_overflow = complain;
15167               howto = &alt_howto;
15168             }
15169         }
15170
15171       if (r_type == R_PPC64_REL16DX_HA)
15172         {
15173           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15174           if (rel->r_offset + 4 > input_section->size)
15175             r = bfd_reloc_outofrange;
15176           else
15177             {
15178               relocation += addend;
15179               relocation -= (rel->r_offset
15180                              + input_section->output_offset
15181                              + input_section->output_section->vma);
15182               relocation = (bfd_signed_vma) relocation >> 16;
15183               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15184               insn &= ~0x1fffc1;
15185               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15186               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15187               r = bfd_reloc_ok;
15188               if (relocation + 0x8000 > 0xffff)
15189                 r = bfd_reloc_overflow;
15190             }
15191         }
15192       else
15193         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15194                                       rel->r_offset, relocation, addend);
15195
15196       if (r != bfd_reloc_ok)
15197         {
15198           char *more_info = NULL;
15199           const char *reloc_name = howto->name;
15200
15201           if (reloc_dest != DEST_NORMAL)
15202             {
15203               more_info = bfd_malloc (strlen (reloc_name) + 8);
15204               if (more_info != NULL)
15205                 {
15206                   strcpy (more_info, reloc_name);
15207                   strcat (more_info, (reloc_dest == DEST_OPD
15208                                       ? " (OPD)" : " (stub)"));
15209                   reloc_name = more_info;
15210                 }
15211             }
15212
15213           if (r == bfd_reloc_overflow)
15214             {
15215               /* On code like "if (foo) foo();" don't report overflow
15216                  on a branch to zero when foo is undefined.  */
15217               if (!warned
15218                   && (reloc_dest == DEST_STUB
15219                       || !(h != NULL
15220                            && (h->elf.root.type == bfd_link_hash_undefweak
15221                                || h->elf.root.type == bfd_link_hash_undefined)
15222                            && is_branch_reloc (r_type))))
15223                 info->callbacks->reloc_overflow (info, &h->elf.root,
15224                                                  sym_name, reloc_name,
15225                                                  orig_rel.r_addend,
15226                                                  input_bfd, input_section,
15227                                                  rel->r_offset);
15228             }
15229           else
15230             {
15231               info->callbacks->einfo
15232                 /* xgettext:c-format */
15233                 (_("%H: %s against `%pT': error %d\n"),
15234                  input_bfd, input_section, rel->r_offset,
15235                  reloc_name, sym_name, (int) r);
15236               ret = FALSE;
15237             }
15238           if (more_info != NULL)
15239             free (more_info);
15240         }
15241     copy_reloc:
15242       if (wrel != rel)
15243         *wrel = *rel;
15244     }
15245
15246   if (wrel != rel)
15247     {
15248       Elf_Internal_Shdr *rel_hdr;
15249       size_t deleted = rel - wrel;
15250
15251       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15252       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15253       if (rel_hdr->sh_size == 0)
15254         {
15255           /* It is too late to remove an empty reloc section.  Leave
15256              one NONE reloc.
15257              ??? What is wrong with an empty section???  */
15258           rel_hdr->sh_size = rel_hdr->sh_entsize;
15259           deleted -= 1;
15260         }
15261       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15262       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15263       input_section->reloc_count -= deleted;
15264     }
15265
15266   /* If we're emitting relocations, then shortly after this function
15267      returns, reloc offsets and addends for this section will be
15268      adjusted.  Worse, reloc symbol indices will be for the output
15269      file rather than the input.  Save a copy of the relocs for
15270      opd_entry_value.  */
15271   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15272     {
15273       bfd_size_type amt;
15274       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15275       rel = bfd_alloc (input_bfd, amt);
15276       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15277       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15278       if (rel == NULL)
15279         return FALSE;
15280       memcpy (rel, relocs, amt);
15281     }
15282   return ret;
15283 }
15284
15285 /* Adjust the value of any local symbols in opd sections.  */
15286
15287 static int
15288 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15289                               const char *name ATTRIBUTE_UNUSED,
15290                               Elf_Internal_Sym *elfsym,
15291                               asection *input_sec,
15292                               struct elf_link_hash_entry *h)
15293 {
15294   struct _opd_sec_data *opd;
15295   long adjust;
15296   bfd_vma value;
15297
15298   if (h != NULL)
15299     return 1;
15300
15301   opd = get_opd_info (input_sec);
15302   if (opd == NULL || opd->adjust == NULL)
15303     return 1;
15304
15305   value = elfsym->st_value - input_sec->output_offset;
15306   if (!bfd_link_relocatable (info))
15307     value -= input_sec->output_section->vma;
15308
15309   adjust = opd->adjust[OPD_NDX (value)];
15310   if (adjust == -1)
15311     return 2;
15312
15313   elfsym->st_value += adjust;
15314   return 1;
15315 }
15316
15317 /* Finish up dynamic symbol handling.  We set the contents of various
15318    dynamic sections here.  */
15319
15320 static bfd_boolean
15321 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15322                                  struct bfd_link_info *info,
15323                                  struct elf_link_hash_entry *h,
15324                                  Elf_Internal_Sym *sym)
15325 {
15326   struct ppc_link_hash_table *htab;
15327   struct plt_entry *ent;
15328
15329   htab = ppc_hash_table (info);
15330   if (htab == NULL)
15331     return FALSE;
15332
15333   if (!htab->opd_abi && !h->def_regular)
15334     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15335       if (ent->plt.offset != (bfd_vma) -1)
15336         {
15337           /* Mark the symbol as undefined, rather than as
15338              defined in glink.  Leave the value if there were
15339              any relocations where pointer equality matters
15340              (this is a clue for the dynamic linker, to make
15341              function pointer comparisons work between an
15342              application and shared library), otherwise set it
15343              to zero.  */
15344           sym->st_shndx = SHN_UNDEF;
15345           if (!h->pointer_equality_needed)
15346             sym->st_value = 0;
15347           else if (!h->ref_regular_nonweak)
15348             {
15349               /* This breaks function pointer comparisons, but
15350                  that is better than breaking tests for a NULL
15351                  function pointer.  */
15352               sym->st_value = 0;
15353             }
15354           break;
15355         }
15356
15357   if (h->needs_copy)
15358     {
15359       /* This symbol needs a copy reloc.  Set it up.  */
15360       Elf_Internal_Rela rela;
15361       asection *srel;
15362       bfd_byte *loc;
15363
15364       if (h->dynindx == -1
15365           || (h->root.type != bfd_link_hash_defined
15366               && h->root.type != bfd_link_hash_defweak)
15367           || htab->elf.srelbss == NULL
15368           || htab->elf.sreldynrelro == NULL)
15369         abort ();
15370
15371       rela.r_offset = (h->root.u.def.value
15372                        + h->root.u.def.section->output_section->vma
15373                        + h->root.u.def.section->output_offset);
15374       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15375       rela.r_addend = 0;
15376       if (h->root.u.def.section == htab->elf.sdynrelro)
15377         srel = htab->elf.sreldynrelro;
15378       else
15379         srel = htab->elf.srelbss;
15380       loc = srel->contents;
15381       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15382       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15383     }
15384
15385   return TRUE;
15386 }
15387
15388 /* Used to decide how to sort relocs in an optimal manner for the
15389    dynamic linker, before writing them out.  */
15390
15391 static enum elf_reloc_type_class
15392 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15393                             const asection *rel_sec,
15394                             const Elf_Internal_Rela *rela)
15395 {
15396   enum elf_ppc64_reloc_type r_type;
15397   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15398
15399   if (rel_sec == htab->elf.irelplt)
15400     return reloc_class_ifunc;
15401
15402   r_type = ELF64_R_TYPE (rela->r_info);
15403   switch (r_type)
15404     {
15405     case R_PPC64_RELATIVE:
15406       return reloc_class_relative;
15407     case R_PPC64_JMP_SLOT:
15408       return reloc_class_plt;
15409     case R_PPC64_COPY:
15410       return reloc_class_copy;
15411     default:
15412       return reloc_class_normal;
15413     }
15414 }
15415
15416 /* Finish up the dynamic sections.  */
15417
15418 static bfd_boolean
15419 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15420                                    struct bfd_link_info *info)
15421 {
15422   struct ppc_link_hash_table *htab;
15423   bfd *dynobj;
15424   asection *sdyn;
15425
15426   htab = ppc_hash_table (info);
15427   if (htab == NULL)
15428     return FALSE;
15429
15430   dynobj = htab->elf.dynobj;
15431   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15432
15433   if (htab->elf.dynamic_sections_created)
15434     {
15435       Elf64_External_Dyn *dyncon, *dynconend;
15436
15437       if (sdyn == NULL || htab->elf.sgot == NULL)
15438         abort ();
15439
15440       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15441       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15442       for (; dyncon < dynconend; dyncon++)
15443         {
15444           Elf_Internal_Dyn dyn;
15445           asection *s;
15446
15447           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15448
15449           switch (dyn.d_tag)
15450             {
15451             default:
15452               continue;
15453
15454             case DT_PPC64_GLINK:
15455               s = htab->glink;
15456               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15457               /* We stupidly defined DT_PPC64_GLINK to be the start
15458                  of glink rather than the first entry point, which is
15459                  what ld.so needs, and now have a bigger stub to
15460                  support automatic multiple TOCs.  */
15461               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15462               break;
15463
15464             case DT_PPC64_OPD:
15465               s = bfd_get_section_by_name (output_bfd, ".opd");
15466               if (s == NULL)
15467                 continue;
15468               dyn.d_un.d_ptr = s->vma;
15469               break;
15470
15471             case DT_PPC64_OPT:
15472               if (htab->do_multi_toc && htab->multi_toc_needed)
15473                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15474               if (htab->has_plt_localentry0)
15475                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15476               break;
15477
15478             case DT_PPC64_OPDSZ:
15479               s = bfd_get_section_by_name (output_bfd, ".opd");
15480               if (s == NULL)
15481                 continue;
15482               dyn.d_un.d_val = s->size;
15483               break;
15484
15485             case DT_PLTGOT:
15486               s = htab->elf.splt;
15487               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15488               break;
15489
15490             case DT_JMPREL:
15491               s = htab->elf.srelplt;
15492               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15493               break;
15494
15495             case DT_PLTRELSZ:
15496               dyn.d_un.d_val = htab->elf.srelplt->size;
15497               break;
15498
15499             case DT_TEXTREL:
15500               if (htab->local_ifunc_resolver)
15501                 info->callbacks->einfo
15502                   (_("%X%P: text relocations and GNU indirect "
15503                      "functions will result in a segfault at runtime\n"));
15504               else if (htab->maybe_local_ifunc_resolver)
15505                 info->callbacks->einfo
15506                   (_("%P: warning: text relocations and GNU indirect "
15507                      "functions may result in a segfault at runtime\n"));
15508               continue;
15509             }
15510
15511           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15512         }
15513     }
15514
15515   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15516       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15517     {
15518       /* Fill in the first entry in the global offset table.
15519          We use it to hold the link-time TOCbase.  */
15520       bfd_put_64 (output_bfd,
15521                   elf_gp (output_bfd) + TOC_BASE_OFF,
15522                   htab->elf.sgot->contents);
15523
15524       /* Set .got entry size.  */
15525       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
15526         = 8;
15527     }
15528
15529   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15530       && htab->elf.splt->output_section != bfd_abs_section_ptr)
15531     {
15532       /* Set .plt entry size.  */
15533       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15534         = PLT_ENTRY_SIZE (htab);
15535     }
15536
15537   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15538      brlt ourselves if emitrelocations.  */
15539   if (htab->brlt != NULL
15540       && htab->brlt->reloc_count != 0
15541       && !_bfd_elf_link_output_relocs (output_bfd,
15542                                        htab->brlt,
15543                                        elf_section_data (htab->brlt)->rela.hdr,
15544                                        elf_section_data (htab->brlt)->relocs,
15545                                        NULL))
15546     return FALSE;
15547
15548   if (htab->glink != NULL
15549       && htab->glink->reloc_count != 0
15550       && !_bfd_elf_link_output_relocs (output_bfd,
15551                                        htab->glink,
15552                                        elf_section_data (htab->glink)->rela.hdr,
15553                                        elf_section_data (htab->glink)->relocs,
15554                                        NULL))
15555     return FALSE;
15556
15557
15558   if (htab->glink_eh_frame != NULL
15559       && htab->glink_eh_frame->size != 0
15560       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15561       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15562                                            htab->glink_eh_frame,
15563                                            htab->glink_eh_frame->contents))
15564     return FALSE;
15565
15566   /* We need to handle writing out multiple GOT sections ourselves,
15567      since we didn't add them to DYNOBJ.  We know dynobj is the first
15568      bfd.  */
15569   while ((dynobj = dynobj->link.next) != NULL)
15570     {
15571       asection *s;
15572
15573       if (!is_ppc64_elf (dynobj))
15574         continue;
15575
15576       s = ppc64_elf_tdata (dynobj)->got;
15577       if (s != NULL
15578           && s->size != 0
15579           && s->output_section != bfd_abs_section_ptr
15580           && !bfd_set_section_contents (output_bfd, s->output_section,
15581                                         s->contents, s->output_offset,
15582                                         s->size))
15583         return FALSE;
15584       s = ppc64_elf_tdata (dynobj)->relgot;
15585       if (s != NULL
15586           && s->size != 0
15587           && s->output_section != bfd_abs_section_ptr
15588           && !bfd_set_section_contents (output_bfd, s->output_section,
15589                                         s->contents, s->output_offset,
15590                                         s->size))
15591         return FALSE;
15592     }
15593
15594   return TRUE;
15595 }
15596
15597 #include "elf64-target.h"
15598
15599 /* FreeBSD support */
15600
15601 #undef  TARGET_LITTLE_SYM
15602 #undef  TARGET_LITTLE_NAME
15603
15604 #undef  TARGET_BIG_SYM
15605 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15606 #undef  TARGET_BIG_NAME
15607 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15608
15609 #undef  ELF_OSABI
15610 #define ELF_OSABI       ELFOSABI_FREEBSD
15611
15612 #undef  elf64_bed
15613 #define elf64_bed       elf64_powerpc_fbsd_bed
15614
15615 #include "elf64-target.h"