Inline PLT call optimization
[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      0x10000
68 #define elf_info_to_howto       ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_want_dynrelro 1
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_dtrel_excludes_plt 1
80 #define elf_backend_default_execstack 0
81
82 #define bfd_elf64_mkobject                    ppc64_elf_mkobject
83 #define bfd_elf64_bfd_reloc_type_lookup       ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_reloc_name_lookup       ppc64_elf_reloc_name_lookup
85 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
86 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
87 #define bfd_elf64_new_section_hook            ppc64_elf_new_section_hook
88 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
89 #define bfd_elf64_get_synthetic_symtab        ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms          ppc64_elf_link_just_syms
91 #define bfd_elf64_bfd_gc_sections             ppc64_elf_gc_sections
92
93 #define elf_backend_object_p                  ppc64_elf_object_p
94 #define elf_backend_grok_prstatus             ppc64_elf_grok_prstatus
95 #define elf_backend_grok_psinfo               ppc64_elf_grok_psinfo
96 #define elf_backend_write_core_note           ppc64_elf_write_core_note
97 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
98 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
99 #define elf_backend_add_symbol_hook           ppc64_elf_add_symbol_hook
100 #define elf_backend_check_directives          ppc64_elf_before_check_relocs
101 #define elf_backend_notice_as_needed          ppc64_elf_notice_as_needed
102 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
103 #define elf_backend_check_relocs              ppc64_elf_check_relocs
104 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
105 #define elf_backend_gc_keep                   ppc64_elf_gc_keep
106 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
107 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
108 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
109 #define elf_backend_hide_symbol               ppc64_elf_hide_symbol
110 #define elf_backend_maybe_function_sym        ppc64_elf_maybe_function_sym
111 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
112 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
113 #define elf_backend_hash_symbol               ppc64_elf_hash_symbol
114 #define elf_backend_init_index_section        _bfd_elf_init_2_index_sections
115 #define elf_backend_action_discarded          ppc64_elf_action_discarded
116 #define elf_backend_relocate_section          ppc64_elf_relocate_section
117 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
118 #define elf_backend_reloc_type_class          ppc64_elf_reloc_type_class
119 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
120 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
121 #define elf_backend_special_sections          ppc64_elf_special_sections
122 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
123 #define elf_backend_merge_symbol              ppc64_elf_merge_symbol
124 #define elf_backend_get_reloc_section         bfd_get_section_by_name
125
126 /* The name of the dynamic interpreter.  This is put in the .interp
127    section.  */
128 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130 /* The size in bytes of an entry in the procedure linkage table.  */
131 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
132 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
133
134 /* The initial size of the plt reserved for the dynamic linker.  */
135 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
136
137 /* Offsets to some stack save slots.  */
138 #define STK_LR 16
139 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
140 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
141    CR save slot.  Used only by optimised __tls_get_addr call stub,
142    relying on __tls_get_addr_opt not saving CR..  */
143 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
144
145 /* TOC base pointers offset from start of TOC.  */
146 #define TOC_BASE_OFF    0x8000
147 /* TOC base alignment.  */
148 #define TOC_BASE_ALIGN  256
149
150 /* Offset of tp and dtp pointers from start of TLS block.  */
151 #define TP_OFFSET       0x7000
152 #define DTP_OFFSET      0x8000
153
154 /* .plt call stub instructions.  The normal stub is like this, but
155    sometimes the .plt entry crosses a 64k boundary and we need to
156    insert an addi to adjust r11.  */
157 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
158 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
159 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
160 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
161 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
162 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
163 #define BCTR            0x4e800420      /* bctr                      */
164
165 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
166 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
167 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
168
169 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
170 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
171 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
172 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
173 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
174 #define BNECTR          0x4ca20420      /* bnectr+               */
175 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
176
177 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
178 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
179 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
180
181 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
182 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
183 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
184
185 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
186 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
187 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
188 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
189 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
190
191 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
192 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
193   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
194                                         /* 0:                           */
195                                         /*  .quad plt0-1f               */
196                                         /* __glink:                     */
197 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
198 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
199                                         /* 1:                           */
200 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
201                                         /*  ld %2,(0b-1b)(%11)          */
202 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
203 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
204                                         /*  ld %12,0(%11)               */
205                                         /*  ld %2,8(%11)                */
206                                         /*  mtctr %12                   */
207                                         /*  ld %11,16(%11)              */
208                                         /*  bctr                        */
209 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
210 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
211 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
212 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
213 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
214
215 /* Pad with this.  */
216 #define NOP             0x60000000
217
218 /* Some other nops.  */
219 #define CROR_151515     0x4def7b82
220 #define CROR_313131     0x4ffffb82
221
222 /* .glink entries for the first 32k functions are two instructions.  */
223 #define LI_R0_0         0x38000000      /* li    %r0,0          */
224 #define B_DOT           0x48000000      /* b     .              */
225
226 /* After that, we need two instructions to load the index, followed by
227    a branch.  */
228 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
229 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
230
231 /* Instructions used by the save and restore reg functions.  */
232 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
233 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
234 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
235 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
236 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
237 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
238 #define LI_R12_0        0x39800000      /* li    %r12,0         */
239 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
240 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
241 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
242 #define BLR             0x4e800020      /* blr                  */
243
244 /* Since .opd is an array of descriptors and each entry will end up
245    with identical R_PPC64_RELATIVE relocs, there is really no need to
246    propagate .opd relocs;  The dynamic linker should be taught to
247    relocate .opd without reloc entries.  */
248 #ifndef NO_OPD_RELOCS
249 #define NO_OPD_RELOCS 0
250 #endif
251
252 #ifndef ARRAY_SIZE
253 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
254 #endif
255
256 static inline int
257 abiversion (bfd *abfd)
258 {
259   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
260 }
261
262 static inline void
263 set_abiversion (bfd *abfd, int ver)
264 {
265   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
266   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
267 }
268 \f
269 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
270
271 /* Relocation HOWTO's.  */
272 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
273
274 static reloc_howto_type ppc64_elf_howto_raw[] =
275 {
276   /* This reloc does nothing.  */
277   HOWTO (R_PPC64_NONE,          /* type */
278          0,                     /* rightshift */
279          3,                     /* size (0 = byte, 1 = short, 2 = long) */
280          0,                     /* bitsize */
281          FALSE,                 /* pc_relative */
282          0,                     /* bitpos */
283          complain_overflow_dont, /* complain_on_overflow */
284          bfd_elf_generic_reloc, /* special_function */
285          "R_PPC64_NONE",        /* name */
286          FALSE,                 /* partial_inplace */
287          0,                     /* src_mask */
288          0,                     /* dst_mask */
289          FALSE),                /* pcrel_offset */
290
291   /* A standard 32 bit relocation.  */
292   HOWTO (R_PPC64_ADDR32,        /* type */
293          0,                     /* rightshift */
294          2,                     /* size (0 = byte, 1 = short, 2 = long) */
295          32,                    /* bitsize */
296          FALSE,                 /* pc_relative */
297          0,                     /* bitpos */
298          complain_overflow_bitfield, /* complain_on_overflow */
299          bfd_elf_generic_reloc, /* special_function */
300          "R_PPC64_ADDR32",      /* name */
301          FALSE,                 /* partial_inplace */
302          0,                     /* src_mask */
303          0xffffffff,            /* dst_mask */
304          FALSE),                /* pcrel_offset */
305
306   /* An absolute 26 bit branch; the lower two bits must be zero.
307      FIXME: we don't check that, we just clear them.  */
308   HOWTO (R_PPC64_ADDR24,        /* type */
309          0,                     /* rightshift */
310          2,                     /* size (0 = byte, 1 = short, 2 = long) */
311          26,                    /* bitsize */
312          FALSE,                 /* pc_relative */
313          0,                     /* bitpos */
314          complain_overflow_bitfield, /* complain_on_overflow */
315          bfd_elf_generic_reloc, /* special_function */
316          "R_PPC64_ADDR24",      /* name */
317          FALSE,                 /* partial_inplace */
318          0,                     /* src_mask */
319          0x03fffffc,            /* dst_mask */
320          FALSE),                /* pcrel_offset */
321
322   /* A standard 16 bit relocation.  */
323   HOWTO (R_PPC64_ADDR16,        /* type */
324          0,                     /* rightshift */
325          1,                     /* size (0 = byte, 1 = short, 2 = long) */
326          16,                    /* bitsize */
327          FALSE,                 /* pc_relative */
328          0,                     /* bitpos */
329          complain_overflow_bitfield, /* complain_on_overflow */
330          bfd_elf_generic_reloc, /* special_function */
331          "R_PPC64_ADDR16",      /* name */
332          FALSE,                 /* partial_inplace */
333          0,                     /* src_mask */
334          0xffff,                /* dst_mask */
335          FALSE),                /* pcrel_offset */
336
337   /* A 16 bit relocation without overflow.  */
338   HOWTO (R_PPC64_ADDR16_LO,     /* type */
339          0,                     /* rightshift */
340          1,                     /* size (0 = byte, 1 = short, 2 = long) */
341          16,                    /* bitsize */
342          FALSE,                 /* pc_relative */
343          0,                     /* bitpos */
344          complain_overflow_dont,/* complain_on_overflow */
345          bfd_elf_generic_reloc, /* special_function */
346          "R_PPC64_ADDR16_LO",   /* name */
347          FALSE,                 /* partial_inplace */
348          0,                     /* src_mask */
349          0xffff,                /* dst_mask */
350          FALSE),                /* pcrel_offset */
351
352   /* Bits 16-31 of an address.  */
353   HOWTO (R_PPC64_ADDR16_HI,     /* type */
354          16,                    /* rightshift */
355          1,                     /* size (0 = byte, 1 = short, 2 = long) */
356          16,                    /* bitsize */
357          FALSE,                 /* pc_relative */
358          0,                     /* bitpos */
359          complain_overflow_signed, /* complain_on_overflow */
360          bfd_elf_generic_reloc, /* special_function */
361          "R_PPC64_ADDR16_HI",   /* name */
362          FALSE,                 /* partial_inplace */
363          0,                     /* src_mask */
364          0xffff,                /* dst_mask */
365          FALSE),                /* pcrel_offset */
366
367   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
368      bits, treated as a signed number, is negative.  */
369   HOWTO (R_PPC64_ADDR16_HA,     /* type */
370          16,                    /* rightshift */
371          1,                     /* size (0 = byte, 1 = short, 2 = long) */
372          16,                    /* bitsize */
373          FALSE,                 /* pc_relative */
374          0,                     /* bitpos */
375          complain_overflow_signed, /* complain_on_overflow */
376          ppc64_elf_ha_reloc,    /* special_function */
377          "R_PPC64_ADDR16_HA",   /* name */
378          FALSE,                 /* partial_inplace */
379          0,                     /* src_mask */
380          0xffff,                /* dst_mask */
381          FALSE),                /* pcrel_offset */
382
383   /* An absolute 16 bit branch; the lower two bits must be zero.
384      FIXME: we don't check that, we just clear them.  */
385   HOWTO (R_PPC64_ADDR14,        /* type */
386          0,                     /* rightshift */
387          2,                     /* size (0 = byte, 1 = short, 2 = long) */
388          16,                    /* bitsize */
389          FALSE,                 /* pc_relative */
390          0,                     /* bitpos */
391          complain_overflow_signed, /* complain_on_overflow */
392          ppc64_elf_branch_reloc, /* special_function */
393          "R_PPC64_ADDR14",      /* name */
394          FALSE,                 /* partial_inplace */
395          0,                     /* src_mask */
396          0x0000fffc,            /* dst_mask */
397          FALSE),                /* pcrel_offset */
398
399   /* An absolute 16 bit branch, for which bit 10 should be set to
400      indicate that the branch is expected to be taken.  The lower two
401      bits must be zero.  */
402   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
403          0,                     /* rightshift */
404          2,                     /* size (0 = byte, 1 = short, 2 = long) */
405          16,                    /* bitsize */
406          FALSE,                 /* pc_relative */
407          0,                     /* bitpos */
408          complain_overflow_signed, /* complain_on_overflow */
409          ppc64_elf_brtaken_reloc, /* special_function */
410          "R_PPC64_ADDR14_BRTAKEN",/* name */
411          FALSE,                 /* partial_inplace */
412          0,                     /* src_mask */
413          0x0000fffc,            /* dst_mask */
414          FALSE),                /* pcrel_offset */
415
416   /* An absolute 16 bit branch, for which bit 10 should be set to
417      indicate that the branch is not expected to be taken.  The lower
418      two bits must be zero.  */
419   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
420          0,                     /* rightshift */
421          2,                     /* size (0 = byte, 1 = short, 2 = long) */
422          16,                    /* bitsize */
423          FALSE,                 /* pc_relative */
424          0,                     /* bitpos */
425          complain_overflow_signed, /* complain_on_overflow */
426          ppc64_elf_brtaken_reloc, /* special_function */
427          "R_PPC64_ADDR14_BRNTAKEN",/* name */
428          FALSE,                 /* partial_inplace */
429          0,                     /* src_mask */
430          0x0000fffc,            /* dst_mask */
431          FALSE),                /* pcrel_offset */
432
433   /* A relative 26 bit branch; the lower two bits must be zero.  */
434   HOWTO (R_PPC64_REL24,         /* type */
435          0,                     /* rightshift */
436          2,                     /* size (0 = byte, 1 = short, 2 = long) */
437          26,                    /* bitsize */
438          TRUE,                  /* pc_relative */
439          0,                     /* bitpos */
440          complain_overflow_signed, /* complain_on_overflow */
441          ppc64_elf_branch_reloc, /* special_function */
442          "R_PPC64_REL24",       /* name */
443          FALSE,                 /* partial_inplace */
444          0,                     /* src_mask */
445          0x03fffffc,            /* dst_mask */
446          TRUE),                 /* pcrel_offset */
447
448   /* A relative 16 bit branch; the lower two bits must be zero.  */
449   HOWTO (R_PPC64_REL14,         /* type */
450          0,                     /* rightshift */
451          2,                     /* size (0 = byte, 1 = short, 2 = long) */
452          16,                    /* bitsize */
453          TRUE,                  /* pc_relative */
454          0,                     /* bitpos */
455          complain_overflow_signed, /* complain_on_overflow */
456          ppc64_elf_branch_reloc, /* special_function */
457          "R_PPC64_REL14",       /* name */
458          FALSE,                 /* partial_inplace */
459          0,                     /* src_mask */
460          0x0000fffc,            /* dst_mask */
461          TRUE),                 /* pcrel_offset */
462
463   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
464      the branch is expected to be taken.  The lower two bits must be
465      zero.  */
466   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
467          0,                     /* rightshift */
468          2,                     /* size (0 = byte, 1 = short, 2 = long) */
469          16,                    /* bitsize */
470          TRUE,                  /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_signed, /* complain_on_overflow */
473          ppc64_elf_brtaken_reloc, /* special_function */
474          "R_PPC64_REL14_BRTAKEN", /* name */
475          FALSE,                 /* partial_inplace */
476          0,                     /* src_mask */
477          0x0000fffc,            /* dst_mask */
478          TRUE),                 /* pcrel_offset */
479
480   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
481      the branch is not expected to be taken.  The lower two bits must
482      be zero.  */
483   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
484          0,                     /* rightshift */
485          2,                     /* size (0 = byte, 1 = short, 2 = long) */
486          16,                    /* bitsize */
487          TRUE,                  /* pc_relative */
488          0,                     /* bitpos */
489          complain_overflow_signed, /* complain_on_overflow */
490          ppc64_elf_brtaken_reloc, /* special_function */
491          "R_PPC64_REL14_BRNTAKEN",/* name */
492          FALSE,                 /* partial_inplace */
493          0,                     /* src_mask */
494          0x0000fffc,            /* dst_mask */
495          TRUE),                 /* pcrel_offset */
496
497   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
498      symbol.  */
499   HOWTO (R_PPC64_GOT16,         /* type */
500          0,                     /* rightshift */
501          1,                     /* size (0 = byte, 1 = short, 2 = long) */
502          16,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_signed, /* complain_on_overflow */
506          ppc64_elf_unhandled_reloc, /* special_function */
507          "R_PPC64_GOT16",       /* name */
508          FALSE,                 /* partial_inplace */
509          0,                     /* src_mask */
510          0xffff,                /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
514      the symbol.  */
515   HOWTO (R_PPC64_GOT16_LO,      /* type */
516          0,                     /* rightshift */
517          1,                     /* size (0 = byte, 1 = short, 2 = long) */
518          16,                    /* bitsize */
519          FALSE,                 /* pc_relative */
520          0,                     /* bitpos */
521          complain_overflow_dont, /* complain_on_overflow */
522          ppc64_elf_unhandled_reloc, /* special_function */
523          "R_PPC64_GOT16_LO",    /* name */
524          FALSE,                 /* partial_inplace */
525          0,                     /* src_mask */
526          0xffff,                /* dst_mask */
527          FALSE),                /* pcrel_offset */
528
529   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
530      the symbol.  */
531   HOWTO (R_PPC64_GOT16_HI,      /* type */
532          16,                    /* rightshift */
533          1,                     /* size (0 = byte, 1 = short, 2 = long) */
534          16,                    /* bitsize */
535          FALSE,                 /* pc_relative */
536          0,                     /* bitpos */
537          complain_overflow_signed,/* complain_on_overflow */
538          ppc64_elf_unhandled_reloc, /* special_function */
539          "R_PPC64_GOT16_HI",    /* name */
540          FALSE,                 /* partial_inplace */
541          0,                     /* src_mask */
542          0xffff,                /* dst_mask */
543          FALSE),                /* pcrel_offset */
544
545   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
546      the symbol.  */
547   HOWTO (R_PPC64_GOT16_HA,      /* type */
548          16,                    /* rightshift */
549          1,                     /* size (0 = byte, 1 = short, 2 = long) */
550          16,                    /* bitsize */
551          FALSE,                 /* pc_relative */
552          0,                     /* bitpos */
553          complain_overflow_signed,/* complain_on_overflow */
554          ppc64_elf_unhandled_reloc, /* special_function */
555          "R_PPC64_GOT16_HA",    /* name */
556          FALSE,                 /* partial_inplace */
557          0,                     /* src_mask */
558          0xffff,                /* dst_mask */
559          FALSE),                /* pcrel_offset */
560
561   /* This is used only by the dynamic linker.  The symbol should exist
562      both in the object being run and in some shared library.  The
563      dynamic linker copies the data addressed by the symbol from the
564      shared library into the object, because the object being
565      run has to have the data at some particular address.  */
566   HOWTO (R_PPC64_COPY,          /* type */
567          0,                     /* rightshift */
568          0,                     /* this one is variable size */
569          0,                     /* bitsize */
570          FALSE,                 /* pc_relative */
571          0,                     /* bitpos */
572          complain_overflow_dont, /* complain_on_overflow */
573          ppc64_elf_unhandled_reloc, /* special_function */
574          "R_PPC64_COPY",        /* name */
575          FALSE,                 /* partial_inplace */
576          0,                     /* src_mask */
577          0,                     /* dst_mask */
578          FALSE),                /* pcrel_offset */
579
580   /* Like R_PPC64_ADDR64, but used when setting global offset table
581      entries.  */
582   HOWTO (R_PPC64_GLOB_DAT,      /* type */
583          0,                     /* rightshift */
584          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
585          64,                    /* bitsize */
586          FALSE,                 /* pc_relative */
587          0,                     /* bitpos */
588          complain_overflow_dont, /* complain_on_overflow */
589          ppc64_elf_unhandled_reloc,  /* special_function */
590          "R_PPC64_GLOB_DAT",    /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          ONES (64),             /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   /* Created by the link editor.  Marks a procedure linkage table
597      entry for a symbol.  */
598   HOWTO (R_PPC64_JMP_SLOT,      /* type */
599          0,                     /* rightshift */
600          0,                     /* size (0 = byte, 1 = short, 2 = long) */
601          0,                     /* bitsize */
602          FALSE,                 /* pc_relative */
603          0,                     /* bitpos */
604          complain_overflow_dont, /* complain_on_overflow */
605          ppc64_elf_unhandled_reloc, /* special_function */
606          "R_PPC64_JMP_SLOT",    /* name */
607          FALSE,                 /* partial_inplace */
608          0,                     /* src_mask */
609          0,                     /* dst_mask */
610          FALSE),                /* pcrel_offset */
611
612   /* Used only by the dynamic linker.  When the object is run, this
613      doubleword64 is set to the load address of the object, plus the
614      addend.  */
615   HOWTO (R_PPC64_RELATIVE,      /* type */
616          0,                     /* rightshift */
617          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
618          64,                    /* bitsize */
619          FALSE,                 /* pc_relative */
620          0,                     /* bitpos */
621          complain_overflow_dont, /* complain_on_overflow */
622          bfd_elf_generic_reloc, /* special_function */
623          "R_PPC64_RELATIVE",    /* name */
624          FALSE,                 /* partial_inplace */
625          0,                     /* src_mask */
626          ONES (64),             /* dst_mask */
627          FALSE),                /* pcrel_offset */
628
629   /* Like R_PPC64_ADDR32, but may be unaligned.  */
630   HOWTO (R_PPC64_UADDR32,       /* type */
631          0,                     /* rightshift */
632          2,                     /* size (0 = byte, 1 = short, 2 = long) */
633          32,                    /* bitsize */
634          FALSE,                 /* pc_relative */
635          0,                     /* bitpos */
636          complain_overflow_bitfield, /* complain_on_overflow */
637          bfd_elf_generic_reloc, /* special_function */
638          "R_PPC64_UADDR32",     /* name */
639          FALSE,                 /* partial_inplace */
640          0,                     /* src_mask */
641          0xffffffff,            /* dst_mask */
642          FALSE),                /* pcrel_offset */
643
644   /* Like R_PPC64_ADDR16, but may be unaligned.  */
645   HOWTO (R_PPC64_UADDR16,       /* type */
646          0,                     /* rightshift */
647          1,                     /* size (0 = byte, 1 = short, 2 = long) */
648          16,                    /* bitsize */
649          FALSE,                 /* pc_relative */
650          0,                     /* bitpos */
651          complain_overflow_bitfield, /* complain_on_overflow */
652          bfd_elf_generic_reloc, /* special_function */
653          "R_PPC64_UADDR16",     /* name */
654          FALSE,                 /* partial_inplace */
655          0,                     /* src_mask */
656          0xffff,                /* dst_mask */
657          FALSE),                /* pcrel_offset */
658
659   /* 32-bit PC relative.  */
660   HOWTO (R_PPC64_REL32,         /* type */
661          0,                     /* rightshift */
662          2,                     /* size (0 = byte, 1 = short, 2 = long) */
663          32,                    /* bitsize */
664          TRUE,                  /* pc_relative */
665          0,                     /* bitpos */
666          complain_overflow_signed, /* complain_on_overflow */
667          bfd_elf_generic_reloc, /* special_function */
668          "R_PPC64_REL32",       /* name */
669          FALSE,                 /* partial_inplace */
670          0,                     /* src_mask */
671          0xffffffff,            /* dst_mask */
672          TRUE),                 /* pcrel_offset */
673
674   /* 32-bit relocation to the symbol's procedure linkage table.  */
675   HOWTO (R_PPC64_PLT32,         /* type */
676          0,                     /* rightshift */
677          2,                     /* size (0 = byte, 1 = short, 2 = long) */
678          32,                    /* bitsize */
679          FALSE,                 /* pc_relative */
680          0,                     /* bitpos */
681          complain_overflow_bitfield, /* complain_on_overflow */
682          ppc64_elf_unhandled_reloc, /* special_function */
683          "R_PPC64_PLT32",       /* name */
684          FALSE,                 /* partial_inplace */
685          0,                     /* src_mask */
686          0xffffffff,            /* dst_mask */
687          FALSE),                /* pcrel_offset */
688
689   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
690      FIXME: R_PPC64_PLTREL32 not supported.  */
691   HOWTO (R_PPC64_PLTREL32,      /* type */
692          0,                     /* rightshift */
693          2,                     /* size (0 = byte, 1 = short, 2 = long) */
694          32,                    /* bitsize */
695          TRUE,                  /* pc_relative */
696          0,                     /* bitpos */
697          complain_overflow_signed, /* complain_on_overflow */
698          ppc64_elf_unhandled_reloc, /* special_function */
699          "R_PPC64_PLTREL32",    /* name */
700          FALSE,                 /* partial_inplace */
701          0,                     /* src_mask */
702          0xffffffff,            /* dst_mask */
703          TRUE),                 /* pcrel_offset */
704
705   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
706      the symbol.  */
707   HOWTO (R_PPC64_PLT16_LO,      /* type */
708          0,                     /* rightshift */
709          1,                     /* size (0 = byte, 1 = short, 2 = long) */
710          16,                    /* bitsize */
711          FALSE,                 /* pc_relative */
712          0,                     /* bitpos */
713          complain_overflow_dont, /* complain_on_overflow */
714          ppc64_elf_unhandled_reloc, /* special_function */
715          "R_PPC64_PLT16_LO",    /* name */
716          FALSE,                 /* partial_inplace */
717          0,                     /* src_mask */
718          0xffff,                /* dst_mask */
719          FALSE),                /* pcrel_offset */
720
721   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
722      the symbol.  */
723   HOWTO (R_PPC64_PLT16_HI,      /* type */
724          16,                    /* rightshift */
725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
726          16,                    /* bitsize */
727          FALSE,                 /* pc_relative */
728          0,                     /* bitpos */
729          complain_overflow_signed, /* complain_on_overflow */
730          ppc64_elf_unhandled_reloc, /* special_function */
731          "R_PPC64_PLT16_HI",    /* name */
732          FALSE,                 /* partial_inplace */
733          0,                     /* src_mask */
734          0xffff,                /* dst_mask */
735          FALSE),                /* pcrel_offset */
736
737   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
738      the symbol.  */
739   HOWTO (R_PPC64_PLT16_HA,      /* type */
740          16,                    /* rightshift */
741          1,                     /* size (0 = byte, 1 = short, 2 = long) */
742          16,                    /* bitsize */
743          FALSE,                 /* pc_relative */
744          0,                     /* bitpos */
745          complain_overflow_signed, /* complain_on_overflow */
746          ppc64_elf_unhandled_reloc, /* special_function */
747          "R_PPC64_PLT16_HA",    /* name */
748          FALSE,                 /* partial_inplace */
749          0,                     /* src_mask */
750          0xffff,                /* dst_mask */
751          FALSE),                /* pcrel_offset */
752
753   /* 16-bit section relative relocation.  */
754   HOWTO (R_PPC64_SECTOFF,       /* type */
755          0,                     /* rightshift */
756          1,                     /* size (0 = byte, 1 = short, 2 = long) */
757          16,                    /* bitsize */
758          FALSE,                 /* pc_relative */
759          0,                     /* bitpos */
760          complain_overflow_signed, /* complain_on_overflow */
761          ppc64_elf_sectoff_reloc, /* special_function */
762          "R_PPC64_SECTOFF",     /* name */
763          FALSE,                 /* partial_inplace */
764          0,                     /* src_mask */
765          0xffff,                /* dst_mask */
766          FALSE),                /* pcrel_offset */
767
768   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
769   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
770          0,                     /* rightshift */
771          1,                     /* size (0 = byte, 1 = short, 2 = long) */
772          16,                    /* bitsize */
773          FALSE,                 /* pc_relative */
774          0,                     /* bitpos */
775          complain_overflow_dont, /* complain_on_overflow */
776          ppc64_elf_sectoff_reloc, /* special_function */
777          "R_PPC64_SECTOFF_LO",  /* name */
778          FALSE,                 /* partial_inplace */
779          0,                     /* src_mask */
780          0xffff,                /* dst_mask */
781          FALSE),                /* pcrel_offset */
782
783   /* 16-bit upper half section relative relocation.  */
784   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
785          16,                    /* rightshift */
786          1,                     /* size (0 = byte, 1 = short, 2 = long) */
787          16,                    /* bitsize */
788          FALSE,                 /* pc_relative */
789          0,                     /* bitpos */
790          complain_overflow_signed, /* complain_on_overflow */
791          ppc64_elf_sectoff_reloc, /* special_function */
792          "R_PPC64_SECTOFF_HI",  /* name */
793          FALSE,                 /* partial_inplace */
794          0,                     /* src_mask */
795          0xffff,                /* dst_mask */
796          FALSE),                /* pcrel_offset */
797
798   /* 16-bit upper half adjusted section relative relocation.  */
799   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
800          16,                    /* rightshift */
801          1,                     /* size (0 = byte, 1 = short, 2 = long) */
802          16,                    /* bitsize */
803          FALSE,                 /* pc_relative */
804          0,                     /* bitpos */
805          complain_overflow_signed, /* complain_on_overflow */
806          ppc64_elf_sectoff_ha_reloc, /* special_function */
807          "R_PPC64_SECTOFF_HA",  /* name */
808          FALSE,                 /* partial_inplace */
809          0,                     /* src_mask */
810          0xffff,                /* dst_mask */
811          FALSE),                /* pcrel_offset */
812
813   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
814   HOWTO (R_PPC64_REL30,         /* type */
815          2,                     /* rightshift */
816          2,                     /* size (0 = byte, 1 = short, 2 = long) */
817          30,                    /* bitsize */
818          TRUE,                  /* pc_relative */
819          0,                     /* bitpos */
820          complain_overflow_dont, /* complain_on_overflow */
821          bfd_elf_generic_reloc, /* special_function */
822          "R_PPC64_REL30",       /* name */
823          FALSE,                 /* partial_inplace */
824          0,                     /* src_mask */
825          0xfffffffc,            /* dst_mask */
826          TRUE),                 /* pcrel_offset */
827
828   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
829
830   /* A standard 64-bit relocation.  */
831   HOWTO (R_PPC64_ADDR64,        /* type */
832          0,                     /* rightshift */
833          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
834          64,                    /* bitsize */
835          FALSE,                 /* pc_relative */
836          0,                     /* bitpos */
837          complain_overflow_dont, /* complain_on_overflow */
838          bfd_elf_generic_reloc, /* special_function */
839          "R_PPC64_ADDR64",      /* name */
840          FALSE,                 /* partial_inplace */
841          0,                     /* src_mask */
842          ONES (64),             /* dst_mask */
843          FALSE),                /* pcrel_offset */
844
845   /* The bits 32-47 of an address.  */
846   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
847          32,                    /* rightshift */
848          1,                     /* size (0 = byte, 1 = short, 2 = long) */
849          16,                    /* bitsize */
850          FALSE,                 /* pc_relative */
851          0,                     /* bitpos */
852          complain_overflow_dont, /* complain_on_overflow */
853          bfd_elf_generic_reloc, /* special_function */
854          "R_PPC64_ADDR16_HIGHER", /* name */
855          FALSE,                 /* partial_inplace */
856          0,                     /* src_mask */
857          0xffff,                /* dst_mask */
858          FALSE),                /* pcrel_offset */
859
860   /* The bits 32-47 of an address, plus 1 if the contents of the low
861      16 bits, treated as a signed number, is negative.  */
862   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
863          32,                    /* rightshift */
864          1,                     /* size (0 = byte, 1 = short, 2 = long) */
865          16,                    /* bitsize */
866          FALSE,                 /* pc_relative */
867          0,                     /* bitpos */
868          complain_overflow_dont, /* complain_on_overflow */
869          ppc64_elf_ha_reloc,    /* special_function */
870          "R_PPC64_ADDR16_HIGHERA", /* name */
871          FALSE,                 /* partial_inplace */
872          0,                     /* src_mask */
873          0xffff,                /* dst_mask */
874          FALSE),                /* pcrel_offset */
875
876   /* The bits 48-63 of an address.  */
877   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
878          48,                    /* rightshift */
879          1,                     /* size (0 = byte, 1 = short, 2 = long) */
880          16,                    /* bitsize */
881          FALSE,                 /* pc_relative */
882          0,                     /* bitpos */
883          complain_overflow_dont, /* complain_on_overflow */
884          bfd_elf_generic_reloc, /* special_function */
885          "R_PPC64_ADDR16_HIGHEST", /* name */
886          FALSE,                 /* partial_inplace */
887          0,                     /* src_mask */
888          0xffff,                /* dst_mask */
889          FALSE),                /* pcrel_offset */
890
891   /* The bits 48-63 of an address, plus 1 if the contents of the low
892      16 bits, treated as a signed number, is negative.  */
893   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
894          48,                    /* rightshift */
895          1,                     /* size (0 = byte, 1 = short, 2 = long) */
896          16,                    /* bitsize */
897          FALSE,                 /* pc_relative */
898          0,                     /* bitpos */
899          complain_overflow_dont, /* complain_on_overflow */
900          ppc64_elf_ha_reloc,    /* special_function */
901          "R_PPC64_ADDR16_HIGHESTA", /* name */
902          FALSE,                 /* partial_inplace */
903          0,                     /* src_mask */
904          0xffff,                /* dst_mask */
905          FALSE),                /* pcrel_offset */
906
907   /* Like ADDR64, but may be unaligned.  */
908   HOWTO (R_PPC64_UADDR64,       /* type */
909          0,                     /* rightshift */
910          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
911          64,                    /* bitsize */
912          FALSE,                 /* pc_relative */
913          0,                     /* bitpos */
914          complain_overflow_dont, /* complain_on_overflow */
915          bfd_elf_generic_reloc, /* special_function */
916          "R_PPC64_UADDR64",     /* name */
917          FALSE,                 /* partial_inplace */
918          0,                     /* src_mask */
919          ONES (64),             /* dst_mask */
920          FALSE),                /* pcrel_offset */
921
922   /* 64-bit relative relocation.  */
923   HOWTO (R_PPC64_REL64,         /* type */
924          0,                     /* rightshift */
925          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
926          64,                    /* bitsize */
927          TRUE,                  /* pc_relative */
928          0,                     /* bitpos */
929          complain_overflow_dont, /* complain_on_overflow */
930          bfd_elf_generic_reloc, /* special_function */
931          "R_PPC64_REL64",       /* name */
932          FALSE,                 /* partial_inplace */
933          0,                     /* src_mask */
934          ONES (64),             /* dst_mask */
935          TRUE),                 /* pcrel_offset */
936
937   /* 64-bit relocation to the symbol's procedure linkage table.  */
938   HOWTO (R_PPC64_PLT64,         /* type */
939          0,                     /* rightshift */
940          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
941          64,                    /* bitsize */
942          FALSE,                 /* pc_relative */
943          0,                     /* bitpos */
944          complain_overflow_dont, /* complain_on_overflow */
945          ppc64_elf_unhandled_reloc, /* special_function */
946          "R_PPC64_PLT64",       /* name */
947          FALSE,                 /* partial_inplace */
948          0,                     /* src_mask */
949          ONES (64),             /* dst_mask */
950          FALSE),                /* pcrel_offset */
951
952   /* 64-bit PC relative relocation to the symbol's procedure linkage
953      table.  */
954   /* FIXME: R_PPC64_PLTREL64 not supported.  */
955   HOWTO (R_PPC64_PLTREL64,      /* type */
956          0,                     /* rightshift */
957          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
958          64,                    /* bitsize */
959          TRUE,                  /* pc_relative */
960          0,                     /* bitpos */
961          complain_overflow_dont, /* complain_on_overflow */
962          ppc64_elf_unhandled_reloc, /* special_function */
963          "R_PPC64_PLTREL64",    /* name */
964          FALSE,                 /* partial_inplace */
965          0,                     /* src_mask */
966          ONES (64),             /* dst_mask */
967          TRUE),                 /* pcrel_offset */
968
969   /* 16 bit TOC-relative relocation.  */
970
971   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
972   HOWTO (R_PPC64_TOC16,         /* type */
973          0,                     /* rightshift */
974          1,                     /* size (0 = byte, 1 = short, 2 = long) */
975          16,                    /* bitsize */
976          FALSE,                 /* pc_relative */
977          0,                     /* bitpos */
978          complain_overflow_signed, /* complain_on_overflow */
979          ppc64_elf_toc_reloc,   /* special_function */
980          "R_PPC64_TOC16",       /* name */
981          FALSE,                 /* partial_inplace */
982          0,                     /* src_mask */
983          0xffff,                /* dst_mask */
984          FALSE),                /* pcrel_offset */
985
986   /* 16 bit TOC-relative relocation without overflow.  */
987
988   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
989   HOWTO (R_PPC64_TOC16_LO,      /* type */
990          0,                     /* rightshift */
991          1,                     /* size (0 = byte, 1 = short, 2 = long) */
992          16,                    /* bitsize */
993          FALSE,                 /* pc_relative */
994          0,                     /* bitpos */
995          complain_overflow_dont, /* complain_on_overflow */
996          ppc64_elf_toc_reloc,   /* special_function */
997          "R_PPC64_TOC16_LO",    /* name */
998          FALSE,                 /* partial_inplace */
999          0,                     /* src_mask */
1000          0xffff,                /* dst_mask */
1001          FALSE),                /* pcrel_offset */
1002
1003   /* 16 bit TOC-relative relocation, high 16 bits.  */
1004
1005   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
1006   HOWTO (R_PPC64_TOC16_HI,      /* type */
1007          16,                    /* rightshift */
1008          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1009          16,                    /* bitsize */
1010          FALSE,                 /* pc_relative */
1011          0,                     /* bitpos */
1012          complain_overflow_signed, /* complain_on_overflow */
1013          ppc64_elf_toc_reloc,   /* special_function */
1014          "R_PPC64_TOC16_HI",    /* name */
1015          FALSE,                 /* partial_inplace */
1016          0,                     /* src_mask */
1017          0xffff,                /* dst_mask */
1018          FALSE),                /* pcrel_offset */
1019
1020   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1021      contents of the low 16 bits, treated as a signed number, is
1022      negative.  */
1023
1024   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1025   HOWTO (R_PPC64_TOC16_HA,      /* type */
1026          16,                    /* rightshift */
1027          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1028          16,                    /* bitsize */
1029          FALSE,                 /* pc_relative */
1030          0,                     /* bitpos */
1031          complain_overflow_signed, /* complain_on_overflow */
1032          ppc64_elf_toc_ha_reloc, /* special_function */
1033          "R_PPC64_TOC16_HA",    /* name */
1034          FALSE,                 /* partial_inplace */
1035          0,                     /* src_mask */
1036          0xffff,                /* dst_mask */
1037          FALSE),                /* pcrel_offset */
1038
1039   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1040
1041   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1042   HOWTO (R_PPC64_TOC,           /* type */
1043          0,                     /* rightshift */
1044          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1045          64,                    /* bitsize */
1046          FALSE,                 /* pc_relative */
1047          0,                     /* bitpos */
1048          complain_overflow_dont, /* complain_on_overflow */
1049          ppc64_elf_toc64_reloc, /* special_function */
1050          "R_PPC64_TOC",         /* name */
1051          FALSE,                 /* partial_inplace */
1052          0,                     /* src_mask */
1053          ONES (64),             /* dst_mask */
1054          FALSE),                /* pcrel_offset */
1055
1056   /* Like R_PPC64_GOT16, but also informs the link editor that the
1057      value to relocate may (!) refer to a PLT entry which the link
1058      editor (a) may replace with the symbol value.  If the link editor
1059      is unable to fully resolve the symbol, it may (b) create a PLT
1060      entry and store the address to the new PLT entry in the GOT.
1061      This permits lazy resolution of function symbols at run time.
1062      The link editor may also skip all of this and just (c) emit a
1063      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1064   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1065     HOWTO (R_PPC64_PLTGOT16,    /* type */
1066          0,                     /* rightshift */
1067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1068          16,                    /* bitsize */
1069          FALSE,                 /* pc_relative */
1070          0,                     /* bitpos */
1071          complain_overflow_signed, /* complain_on_overflow */
1072          ppc64_elf_unhandled_reloc, /* special_function */
1073          "R_PPC64_PLTGOT16",    /* name */
1074          FALSE,                 /* partial_inplace */
1075          0,                     /* src_mask */
1076          0xffff,                /* dst_mask */
1077          FALSE),                /* pcrel_offset */
1078
1079   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1080   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1081   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1082          0,                     /* rightshift */
1083          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1084          16,                    /* bitsize */
1085          FALSE,                 /* pc_relative */
1086          0,                     /* bitpos */
1087          complain_overflow_dont, /* complain_on_overflow */
1088          ppc64_elf_unhandled_reloc, /* special_function */
1089          "R_PPC64_PLTGOT16_LO", /* name */
1090          FALSE,                 /* partial_inplace */
1091          0,                     /* src_mask */
1092          0xffff,                /* dst_mask */
1093          FALSE),                /* pcrel_offset */
1094
1095   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1096   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1097   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1098          16,                    /* rightshift */
1099          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1100          16,                    /* bitsize */
1101          FALSE,                 /* pc_relative */
1102          0,                     /* bitpos */
1103          complain_overflow_signed, /* complain_on_overflow */
1104          ppc64_elf_unhandled_reloc, /* special_function */
1105          "R_PPC64_PLTGOT16_HI", /* name */
1106          FALSE,                 /* partial_inplace */
1107          0,                     /* src_mask */
1108          0xffff,                /* dst_mask */
1109          FALSE),                /* pcrel_offset */
1110
1111   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1112      1 if the contents of the low 16 bits, treated as a signed number,
1113      is negative.  */
1114   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1115   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1116          16,                    /* rightshift */
1117          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1118          16,                    /* bitsize */
1119          FALSE,                 /* pc_relative */
1120          0,                     /* bitpos */
1121          complain_overflow_signed, /* complain_on_overflow */
1122          ppc64_elf_unhandled_reloc, /* special_function */
1123          "R_PPC64_PLTGOT16_HA", /* name */
1124          FALSE,                 /* partial_inplace */
1125          0,                     /* src_mask */
1126          0xffff,                /* dst_mask */
1127          FALSE),                /* pcrel_offset */
1128
1129   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1130   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1131          0,                     /* rightshift */
1132          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1133          16,                    /* bitsize */
1134          FALSE,                 /* pc_relative */
1135          0,                     /* bitpos */
1136          complain_overflow_signed, /* complain_on_overflow */
1137          bfd_elf_generic_reloc, /* special_function */
1138          "R_PPC64_ADDR16_DS",   /* name */
1139          FALSE,                 /* partial_inplace */
1140          0,                     /* src_mask */
1141          0xfffc,                /* dst_mask */
1142          FALSE),                /* pcrel_offset */
1143
1144   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1145   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1146          0,                     /* rightshift */
1147          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1148          16,                    /* bitsize */
1149          FALSE,                 /* pc_relative */
1150          0,                     /* bitpos */
1151          complain_overflow_dont,/* complain_on_overflow */
1152          bfd_elf_generic_reloc, /* special_function */
1153          "R_PPC64_ADDR16_LO_DS",/* name */
1154          FALSE,                 /* partial_inplace */
1155          0,                     /* src_mask */
1156          0xfffc,                /* dst_mask */
1157          FALSE),                /* pcrel_offset */
1158
1159   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1160   HOWTO (R_PPC64_GOT16_DS,      /* type */
1161          0,                     /* rightshift */
1162          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1163          16,                    /* bitsize */
1164          FALSE,                 /* pc_relative */
1165          0,                     /* bitpos */
1166          complain_overflow_signed, /* complain_on_overflow */
1167          ppc64_elf_unhandled_reloc, /* special_function */
1168          "R_PPC64_GOT16_DS",    /* name */
1169          FALSE,                 /* partial_inplace */
1170          0,                     /* src_mask */
1171          0xfffc,                /* dst_mask */
1172          FALSE),                /* pcrel_offset */
1173
1174   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1175   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1176          0,                     /* rightshift */
1177          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1178          16,                    /* bitsize */
1179          FALSE,                 /* pc_relative */
1180          0,                     /* bitpos */
1181          complain_overflow_dont, /* complain_on_overflow */
1182          ppc64_elf_unhandled_reloc, /* special_function */
1183          "R_PPC64_GOT16_LO_DS", /* name */
1184          FALSE,                 /* partial_inplace */
1185          0,                     /* src_mask */
1186          0xfffc,                /* dst_mask */
1187          FALSE),                /* pcrel_offset */
1188
1189   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1190   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1191          0,                     /* rightshift */
1192          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1193          16,                    /* bitsize */
1194          FALSE,                 /* pc_relative */
1195          0,                     /* bitpos */
1196          complain_overflow_dont, /* complain_on_overflow */
1197          ppc64_elf_unhandled_reloc, /* special_function */
1198          "R_PPC64_PLT16_LO_DS", /* name */
1199          FALSE,                 /* partial_inplace */
1200          0,                     /* src_mask */
1201          0xfffc,                /* dst_mask */
1202          FALSE),                /* pcrel_offset */
1203
1204   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1205   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1206          0,                     /* rightshift */
1207          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1208          16,                    /* bitsize */
1209          FALSE,                 /* pc_relative */
1210          0,                     /* bitpos */
1211          complain_overflow_signed, /* complain_on_overflow */
1212          ppc64_elf_sectoff_reloc, /* special_function */
1213          "R_PPC64_SECTOFF_DS",  /* name */
1214          FALSE,                 /* partial_inplace */
1215          0,                     /* src_mask */
1216          0xfffc,                /* dst_mask */
1217          FALSE),                /* pcrel_offset */
1218
1219   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1220   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1221          0,                     /* rightshift */
1222          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1223          16,                    /* bitsize */
1224          FALSE,                 /* pc_relative */
1225          0,                     /* bitpos */
1226          complain_overflow_dont, /* complain_on_overflow */
1227          ppc64_elf_sectoff_reloc, /* special_function */
1228          "R_PPC64_SECTOFF_LO_DS",/* name */
1229          FALSE,                 /* partial_inplace */
1230          0,                     /* src_mask */
1231          0xfffc,                /* dst_mask */
1232          FALSE),                /* pcrel_offset */
1233
1234   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1235   HOWTO (R_PPC64_TOC16_DS,      /* type */
1236          0,                     /* rightshift */
1237          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1238          16,                    /* bitsize */
1239          FALSE,                 /* pc_relative */
1240          0,                     /* bitpos */
1241          complain_overflow_signed, /* complain_on_overflow */
1242          ppc64_elf_toc_reloc,   /* special_function */
1243          "R_PPC64_TOC16_DS",    /* name */
1244          FALSE,                 /* partial_inplace */
1245          0,                     /* src_mask */
1246          0xfffc,                /* dst_mask */
1247          FALSE),                /* pcrel_offset */
1248
1249   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1250   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1251          0,                     /* rightshift */
1252          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1253          16,                    /* bitsize */
1254          FALSE,                 /* pc_relative */
1255          0,                     /* bitpos */
1256          complain_overflow_dont, /* complain_on_overflow */
1257          ppc64_elf_toc_reloc,   /* special_function */
1258          "R_PPC64_TOC16_LO_DS", /* name */
1259          FALSE,                 /* partial_inplace */
1260          0,                     /* src_mask */
1261          0xfffc,                /* dst_mask */
1262          FALSE),                /* pcrel_offset */
1263
1264   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1265   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1266   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1267          0,                     /* rightshift */
1268          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1269          16,                    /* bitsize */
1270          FALSE,                 /* pc_relative */
1271          0,                     /* bitpos */
1272          complain_overflow_signed, /* complain_on_overflow */
1273          ppc64_elf_unhandled_reloc, /* special_function */
1274          "R_PPC64_PLTGOT16_DS", /* name */
1275          FALSE,                 /* partial_inplace */
1276          0,                     /* src_mask */
1277          0xfffc,                /* dst_mask */
1278          FALSE),                /* pcrel_offset */
1279
1280   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1281   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1282   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1283          0,                     /* rightshift */
1284          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1285          16,                    /* bitsize */
1286          FALSE,                 /* pc_relative */
1287          0,                     /* bitpos */
1288          complain_overflow_dont, /* complain_on_overflow */
1289          ppc64_elf_unhandled_reloc, /* special_function */
1290          "R_PPC64_PLTGOT16_LO_DS",/* name */
1291          FALSE,                 /* partial_inplace */
1292          0,                     /* src_mask */
1293          0xfffc,                /* dst_mask */
1294          FALSE),                /* pcrel_offset */
1295
1296   /* Marker relocs for TLS.  */
1297   HOWTO (R_PPC64_TLS,
1298          0,                     /* rightshift */
1299          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1300          32,                    /* bitsize */
1301          FALSE,                 /* pc_relative */
1302          0,                     /* bitpos */
1303          complain_overflow_dont, /* complain_on_overflow */
1304          bfd_elf_generic_reloc, /* special_function */
1305          "R_PPC64_TLS",         /* name */
1306          FALSE,                 /* partial_inplace */
1307          0,                     /* src_mask */
1308          0,                     /* dst_mask */
1309          FALSE),                /* pcrel_offset */
1310
1311   HOWTO (R_PPC64_TLSGD,
1312          0,                     /* rightshift */
1313          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1314          32,                    /* bitsize */
1315          FALSE,                 /* pc_relative */
1316          0,                     /* bitpos */
1317          complain_overflow_dont, /* complain_on_overflow */
1318          bfd_elf_generic_reloc, /* special_function */
1319          "R_PPC64_TLSGD",       /* name */
1320          FALSE,                 /* partial_inplace */
1321          0,                     /* src_mask */
1322          0,                     /* dst_mask */
1323          FALSE),                /* pcrel_offset */
1324
1325   HOWTO (R_PPC64_TLSLD,
1326          0,                     /* rightshift */
1327          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1328          32,                    /* bitsize */
1329          FALSE,                 /* pc_relative */
1330          0,                     /* bitpos */
1331          complain_overflow_dont, /* complain_on_overflow */
1332          bfd_elf_generic_reloc, /* special_function */
1333          "R_PPC64_TLSLD",       /* name */
1334          FALSE,                 /* partial_inplace */
1335          0,                     /* src_mask */
1336          0,                     /* dst_mask */
1337          FALSE),                /* pcrel_offset */
1338
1339   /* Marker reloc for optimizing r2 save in prologue rather than on
1340      each plt call stub.  */
1341   HOWTO (R_PPC64_TOCSAVE,
1342          0,                     /* rightshift */
1343          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1344          32,                    /* bitsize */
1345          FALSE,                 /* pc_relative */
1346          0,                     /* bitpos */
1347          complain_overflow_dont, /* complain_on_overflow */
1348          bfd_elf_generic_reloc, /* special_function */
1349          "R_PPC64_TOCSAVE",     /* name */
1350          FALSE,                 /* partial_inplace */
1351          0,                     /* src_mask */
1352          0,                     /* dst_mask */
1353          FALSE),                /* pcrel_offset */
1354
1355   /* Marker relocs on inline plt call instructions.  */
1356   HOWTO (R_PPC64_PLTSEQ,
1357          0,                     /* rightshift */
1358          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1359          32,                    /* bitsize */
1360          FALSE,                 /* pc_relative */
1361          0,                     /* bitpos */
1362          complain_overflow_dont, /* complain_on_overflow */
1363          bfd_elf_generic_reloc, /* special_function */
1364          "R_PPC64_PLTSEQ",      /* name */
1365          FALSE,                 /* partial_inplace */
1366          0,                     /* src_mask */
1367          0,                     /* dst_mask */
1368          FALSE),                /* pcrel_offset */
1369
1370   HOWTO (R_PPC64_PLTCALL,
1371          0,                     /* rightshift */
1372          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1373          32,                    /* bitsize */
1374          FALSE,                 /* pc_relative */
1375          0,                     /* bitpos */
1376          complain_overflow_dont, /* complain_on_overflow */
1377          bfd_elf_generic_reloc, /* special_function */
1378          "R_PPC64_PLTCALL",     /* name */
1379          FALSE,                 /* partial_inplace */
1380          0,                     /* src_mask */
1381          0,                     /* dst_mask */
1382          FALSE),                /* pcrel_offset */
1383
1384   /* Computes the load module index of the load module that contains the
1385      definition of its TLS sym.  */
1386   HOWTO (R_PPC64_DTPMOD64,
1387          0,                     /* rightshift */
1388          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1389          64,                    /* bitsize */
1390          FALSE,                 /* pc_relative */
1391          0,                     /* bitpos */
1392          complain_overflow_dont, /* complain_on_overflow */
1393          ppc64_elf_unhandled_reloc, /* special_function */
1394          "R_PPC64_DTPMOD64",    /* name */
1395          FALSE,                 /* partial_inplace */
1396          0,                     /* src_mask */
1397          ONES (64),             /* dst_mask */
1398          FALSE),                /* pcrel_offset */
1399
1400   /* Computes a dtv-relative displacement, the difference between the value
1401      of sym+add and the base address of the thread-local storage block that
1402      contains the definition of sym, minus 0x8000.  */
1403   HOWTO (R_PPC64_DTPREL64,
1404          0,                     /* rightshift */
1405          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1406          64,                    /* bitsize */
1407          FALSE,                 /* pc_relative */
1408          0,                     /* bitpos */
1409          complain_overflow_dont, /* complain_on_overflow */
1410          ppc64_elf_unhandled_reloc, /* special_function */
1411          "R_PPC64_DTPREL64",    /* name */
1412          FALSE,                 /* partial_inplace */
1413          0,                     /* src_mask */
1414          ONES (64),             /* dst_mask */
1415          FALSE),                /* pcrel_offset */
1416
1417   /* A 16 bit dtprel reloc.  */
1418   HOWTO (R_PPC64_DTPREL16,
1419          0,                     /* rightshift */
1420          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1421          16,                    /* bitsize */
1422          FALSE,                 /* pc_relative */
1423          0,                     /* bitpos */
1424          complain_overflow_signed, /* complain_on_overflow */
1425          ppc64_elf_unhandled_reloc, /* special_function */
1426          "R_PPC64_DTPREL16",    /* name */
1427          FALSE,                 /* partial_inplace */
1428          0,                     /* src_mask */
1429          0xffff,                /* dst_mask */
1430          FALSE),                /* pcrel_offset */
1431
1432   /* Like DTPREL16, but no overflow.  */
1433   HOWTO (R_PPC64_DTPREL16_LO,
1434          0,                     /* rightshift */
1435          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1436          16,                    /* bitsize */
1437          FALSE,                 /* pc_relative */
1438          0,                     /* bitpos */
1439          complain_overflow_dont, /* complain_on_overflow */
1440          ppc64_elf_unhandled_reloc, /* special_function */
1441          "R_PPC64_DTPREL16_LO", /* name */
1442          FALSE,                 /* partial_inplace */
1443          0,                     /* src_mask */
1444          0xffff,                /* dst_mask */
1445          FALSE),                /* pcrel_offset */
1446
1447   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1448   HOWTO (R_PPC64_DTPREL16_HI,
1449          16,                    /* rightshift */
1450          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1451          16,                    /* bitsize */
1452          FALSE,                 /* pc_relative */
1453          0,                     /* bitpos */
1454          complain_overflow_signed, /* complain_on_overflow */
1455          ppc64_elf_unhandled_reloc, /* special_function */
1456          "R_PPC64_DTPREL16_HI", /* name */
1457          FALSE,                 /* partial_inplace */
1458          0,                     /* src_mask */
1459          0xffff,                /* dst_mask */
1460          FALSE),                /* pcrel_offset */
1461
1462   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1463   HOWTO (R_PPC64_DTPREL16_HA,
1464          16,                    /* rightshift */
1465          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1466          16,                    /* bitsize */
1467          FALSE,                 /* pc_relative */
1468          0,                     /* bitpos */
1469          complain_overflow_signed, /* complain_on_overflow */
1470          ppc64_elf_unhandled_reloc, /* special_function */
1471          "R_PPC64_DTPREL16_HA", /* name */
1472          FALSE,                 /* partial_inplace */
1473          0,                     /* src_mask */
1474          0xffff,                /* dst_mask */
1475          FALSE),                /* pcrel_offset */
1476
1477   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1478   HOWTO (R_PPC64_DTPREL16_HIGHER,
1479          32,                    /* rightshift */
1480          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1481          16,                    /* bitsize */
1482          FALSE,                 /* pc_relative */
1483          0,                     /* bitpos */
1484          complain_overflow_dont, /* complain_on_overflow */
1485          ppc64_elf_unhandled_reloc, /* special_function */
1486          "R_PPC64_DTPREL16_HIGHER", /* name */
1487          FALSE,                 /* partial_inplace */
1488          0,                     /* src_mask */
1489          0xffff,                /* dst_mask */
1490          FALSE),                /* pcrel_offset */
1491
1492   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1493   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1494          32,                    /* rightshift */
1495          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1496          16,                    /* bitsize */
1497          FALSE,                 /* pc_relative */
1498          0,                     /* bitpos */
1499          complain_overflow_dont, /* complain_on_overflow */
1500          ppc64_elf_unhandled_reloc, /* special_function */
1501          "R_PPC64_DTPREL16_HIGHERA", /* name */
1502          FALSE,                 /* partial_inplace */
1503          0,                     /* src_mask */
1504          0xffff,                /* dst_mask */
1505          FALSE),                /* pcrel_offset */
1506
1507   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1508   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1509          48,                    /* rightshift */
1510          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1511          16,                    /* bitsize */
1512          FALSE,                 /* pc_relative */
1513          0,                     /* bitpos */
1514          complain_overflow_dont, /* complain_on_overflow */
1515          ppc64_elf_unhandled_reloc, /* special_function */
1516          "R_PPC64_DTPREL16_HIGHEST", /* name */
1517          FALSE,                 /* partial_inplace */
1518          0,                     /* src_mask */
1519          0xffff,                /* dst_mask */
1520          FALSE),                /* pcrel_offset */
1521
1522   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1523   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1524          48,                    /* rightshift */
1525          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1526          16,                    /* bitsize */
1527          FALSE,                 /* pc_relative */
1528          0,                     /* bitpos */
1529          complain_overflow_dont, /* complain_on_overflow */
1530          ppc64_elf_unhandled_reloc, /* special_function */
1531          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1532          FALSE,                 /* partial_inplace */
1533          0,                     /* src_mask */
1534          0xffff,                /* dst_mask */
1535          FALSE),                /* pcrel_offset */
1536
1537   /* Like DTPREL16, but for insns with a DS field.  */
1538   HOWTO (R_PPC64_DTPREL16_DS,
1539          0,                     /* rightshift */
1540          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1541          16,                    /* bitsize */
1542          FALSE,                 /* pc_relative */
1543          0,                     /* bitpos */
1544          complain_overflow_signed, /* complain_on_overflow */
1545          ppc64_elf_unhandled_reloc, /* special_function */
1546          "R_PPC64_DTPREL16_DS", /* name */
1547          FALSE,                 /* partial_inplace */
1548          0,                     /* src_mask */
1549          0xfffc,                /* dst_mask */
1550          FALSE),                /* pcrel_offset */
1551
1552   /* Like DTPREL16_DS, but no overflow.  */
1553   HOWTO (R_PPC64_DTPREL16_LO_DS,
1554          0,                     /* rightshift */
1555          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1556          16,                    /* bitsize */
1557          FALSE,                 /* pc_relative */
1558          0,                     /* bitpos */
1559          complain_overflow_dont, /* complain_on_overflow */
1560          ppc64_elf_unhandled_reloc, /* special_function */
1561          "R_PPC64_DTPREL16_LO_DS", /* name */
1562          FALSE,                 /* partial_inplace */
1563          0,                     /* src_mask */
1564          0xfffc,                /* dst_mask */
1565          FALSE),                /* pcrel_offset */
1566
1567   /* Computes a tp-relative displacement, the difference between the value of
1568      sym+add and the value of the thread pointer (r13).  */
1569   HOWTO (R_PPC64_TPREL64,
1570          0,                     /* rightshift */
1571          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1572          64,                    /* bitsize */
1573          FALSE,                 /* pc_relative */
1574          0,                     /* bitpos */
1575          complain_overflow_dont, /* complain_on_overflow */
1576          ppc64_elf_unhandled_reloc, /* special_function */
1577          "R_PPC64_TPREL64",     /* name */
1578          FALSE,                 /* partial_inplace */
1579          0,                     /* src_mask */
1580          ONES (64),             /* dst_mask */
1581          FALSE),                /* pcrel_offset */
1582
1583   /* A 16 bit tprel reloc.  */
1584   HOWTO (R_PPC64_TPREL16,
1585          0,                     /* rightshift */
1586          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1587          16,                    /* bitsize */
1588          FALSE,                 /* pc_relative */
1589          0,                     /* bitpos */
1590          complain_overflow_signed, /* complain_on_overflow */
1591          ppc64_elf_unhandled_reloc, /* special_function */
1592          "R_PPC64_TPREL16",     /* name */
1593          FALSE,                 /* partial_inplace */
1594          0,                     /* src_mask */
1595          0xffff,                /* dst_mask */
1596          FALSE),                /* pcrel_offset */
1597
1598   /* Like TPREL16, but no overflow.  */
1599   HOWTO (R_PPC64_TPREL16_LO,
1600          0,                     /* rightshift */
1601          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1602          16,                    /* bitsize */
1603          FALSE,                 /* pc_relative */
1604          0,                     /* bitpos */
1605          complain_overflow_dont, /* complain_on_overflow */
1606          ppc64_elf_unhandled_reloc, /* special_function */
1607          "R_PPC64_TPREL16_LO",  /* name */
1608          FALSE,                 /* partial_inplace */
1609          0,                     /* src_mask */
1610          0xffff,                /* dst_mask */
1611          FALSE),                /* pcrel_offset */
1612
1613   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1614   HOWTO (R_PPC64_TPREL16_HI,
1615          16,                    /* rightshift */
1616          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1617          16,                    /* bitsize */
1618          FALSE,                 /* pc_relative */
1619          0,                     /* bitpos */
1620          complain_overflow_signed, /* complain_on_overflow */
1621          ppc64_elf_unhandled_reloc, /* special_function */
1622          "R_PPC64_TPREL16_HI",  /* name */
1623          FALSE,                 /* partial_inplace */
1624          0,                     /* src_mask */
1625          0xffff,                /* dst_mask */
1626          FALSE),                /* pcrel_offset */
1627
1628   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1629   HOWTO (R_PPC64_TPREL16_HA,
1630          16,                    /* rightshift */
1631          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1632          16,                    /* bitsize */
1633          FALSE,                 /* pc_relative */
1634          0,                     /* bitpos */
1635          complain_overflow_signed, /* complain_on_overflow */
1636          ppc64_elf_unhandled_reloc, /* special_function */
1637          "R_PPC64_TPREL16_HA",  /* name */
1638          FALSE,                 /* partial_inplace */
1639          0,                     /* src_mask */
1640          0xffff,                /* dst_mask */
1641          FALSE),                /* pcrel_offset */
1642
1643   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1644   HOWTO (R_PPC64_TPREL16_HIGHER,
1645          32,                    /* rightshift */
1646          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1647          16,                    /* bitsize */
1648          FALSE,                 /* pc_relative */
1649          0,                     /* bitpos */
1650          complain_overflow_dont, /* complain_on_overflow */
1651          ppc64_elf_unhandled_reloc, /* special_function */
1652          "R_PPC64_TPREL16_HIGHER",      /* name */
1653          FALSE,                 /* partial_inplace */
1654          0,                     /* src_mask */
1655          0xffff,                /* dst_mask */
1656          FALSE),                /* pcrel_offset */
1657
1658   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1659   HOWTO (R_PPC64_TPREL16_HIGHERA,
1660          32,                    /* rightshift */
1661          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1662          16,                    /* bitsize */
1663          FALSE,                 /* pc_relative */
1664          0,                     /* bitpos */
1665          complain_overflow_dont, /* complain_on_overflow */
1666          ppc64_elf_unhandled_reloc, /* special_function */
1667          "R_PPC64_TPREL16_HIGHERA", /* name */
1668          FALSE,                 /* partial_inplace */
1669          0,                     /* src_mask */
1670          0xffff,                /* dst_mask */
1671          FALSE),                /* pcrel_offset */
1672
1673   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1674   HOWTO (R_PPC64_TPREL16_HIGHEST,
1675          48,                    /* rightshift */
1676          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1677          16,                    /* bitsize */
1678          FALSE,                 /* pc_relative */
1679          0,                     /* bitpos */
1680          complain_overflow_dont, /* complain_on_overflow */
1681          ppc64_elf_unhandled_reloc, /* special_function */
1682          "R_PPC64_TPREL16_HIGHEST", /* name */
1683          FALSE,                 /* partial_inplace */
1684          0,                     /* src_mask */
1685          0xffff,                /* dst_mask */
1686          FALSE),                /* pcrel_offset */
1687
1688   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1689   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1690          48,                    /* rightshift */
1691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1692          16,                    /* bitsize */
1693          FALSE,                 /* pc_relative */
1694          0,                     /* bitpos */
1695          complain_overflow_dont, /* complain_on_overflow */
1696          ppc64_elf_unhandled_reloc, /* special_function */
1697          "R_PPC64_TPREL16_HIGHESTA", /* name */
1698          FALSE,                 /* partial_inplace */
1699          0,                     /* src_mask */
1700          0xffff,                /* dst_mask */
1701          FALSE),                /* pcrel_offset */
1702
1703   /* Like TPREL16, but for insns with a DS field.  */
1704   HOWTO (R_PPC64_TPREL16_DS,
1705          0,                     /* rightshift */
1706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1707          16,                    /* bitsize */
1708          FALSE,                 /* pc_relative */
1709          0,                     /* bitpos */
1710          complain_overflow_signed, /* complain_on_overflow */
1711          ppc64_elf_unhandled_reloc, /* special_function */
1712          "R_PPC64_TPREL16_DS",  /* name */
1713          FALSE,                 /* partial_inplace */
1714          0,                     /* src_mask */
1715          0xfffc,                /* dst_mask */
1716          FALSE),                /* pcrel_offset */
1717
1718   /* Like TPREL16_DS, but no overflow.  */
1719   HOWTO (R_PPC64_TPREL16_LO_DS,
1720          0,                     /* rightshift */
1721          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1722          16,                    /* bitsize */
1723          FALSE,                 /* pc_relative */
1724          0,                     /* bitpos */
1725          complain_overflow_dont, /* complain_on_overflow */
1726          ppc64_elf_unhandled_reloc, /* special_function */
1727          "R_PPC64_TPREL16_LO_DS", /* name */
1728          FALSE,                 /* partial_inplace */
1729          0,                     /* src_mask */
1730          0xfffc,                /* dst_mask */
1731          FALSE),                /* pcrel_offset */
1732
1733   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1734      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1735      to the first entry relative to the TOC base (r2).  */
1736   HOWTO (R_PPC64_GOT_TLSGD16,
1737          0,                     /* rightshift */
1738          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1739          16,                    /* bitsize */
1740          FALSE,                 /* pc_relative */
1741          0,                     /* bitpos */
1742          complain_overflow_signed, /* complain_on_overflow */
1743          ppc64_elf_unhandled_reloc, /* special_function */
1744          "R_PPC64_GOT_TLSGD16", /* name */
1745          FALSE,                 /* partial_inplace */
1746          0,                     /* src_mask */
1747          0xffff,                /* dst_mask */
1748          FALSE),                /* pcrel_offset */
1749
1750   /* Like GOT_TLSGD16, but no overflow.  */
1751   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1752          0,                     /* rightshift */
1753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1754          16,                    /* bitsize */
1755          FALSE,                 /* pc_relative */
1756          0,                     /* bitpos */
1757          complain_overflow_dont, /* complain_on_overflow */
1758          ppc64_elf_unhandled_reloc, /* special_function */
1759          "R_PPC64_GOT_TLSGD16_LO", /* name */
1760          FALSE,                 /* partial_inplace */
1761          0,                     /* src_mask */
1762          0xffff,                /* dst_mask */
1763          FALSE),                /* pcrel_offset */
1764
1765   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1766   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1767          16,                    /* rightshift */
1768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1769          16,                    /* bitsize */
1770          FALSE,                 /* pc_relative */
1771          0,                     /* bitpos */
1772          complain_overflow_signed, /* complain_on_overflow */
1773          ppc64_elf_unhandled_reloc, /* special_function */
1774          "R_PPC64_GOT_TLSGD16_HI", /* name */
1775          FALSE,                 /* partial_inplace */
1776          0,                     /* src_mask */
1777          0xffff,                /* dst_mask */
1778          FALSE),                /* pcrel_offset */
1779
1780   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1781   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1782          16,                    /* rightshift */
1783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1784          16,                    /* bitsize */
1785          FALSE,                 /* pc_relative */
1786          0,                     /* bitpos */
1787          complain_overflow_signed, /* complain_on_overflow */
1788          ppc64_elf_unhandled_reloc, /* special_function */
1789          "R_PPC64_GOT_TLSGD16_HA", /* name */
1790          FALSE,                 /* partial_inplace */
1791          0,                     /* src_mask */
1792          0xffff,                /* dst_mask */
1793          FALSE),                /* pcrel_offset */
1794
1795   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1796      with values (sym+add)@dtpmod and zero, and computes the offset to the
1797      first entry relative to the TOC base (r2).  */
1798   HOWTO (R_PPC64_GOT_TLSLD16,
1799          0,                     /* rightshift */
1800          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1801          16,                    /* bitsize */
1802          FALSE,                 /* pc_relative */
1803          0,                     /* bitpos */
1804          complain_overflow_signed, /* complain_on_overflow */
1805          ppc64_elf_unhandled_reloc, /* special_function */
1806          "R_PPC64_GOT_TLSLD16", /* name */
1807          FALSE,                 /* partial_inplace */
1808          0,                     /* src_mask */
1809          0xffff,                /* dst_mask */
1810          FALSE),                /* pcrel_offset */
1811
1812   /* Like GOT_TLSLD16, but no overflow.  */
1813   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1814          0,                     /* rightshift */
1815          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1816          16,                    /* bitsize */
1817          FALSE,                 /* pc_relative */
1818          0,                     /* bitpos */
1819          complain_overflow_dont, /* complain_on_overflow */
1820          ppc64_elf_unhandled_reloc, /* special_function */
1821          "R_PPC64_GOT_TLSLD16_LO", /* name */
1822          FALSE,                 /* partial_inplace */
1823          0,                     /* src_mask */
1824          0xffff,                /* dst_mask */
1825          FALSE),                /* pcrel_offset */
1826
1827   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1828   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1829          16,                    /* rightshift */
1830          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1831          16,                    /* bitsize */
1832          FALSE,                 /* pc_relative */
1833          0,                     /* bitpos */
1834          complain_overflow_signed, /* complain_on_overflow */
1835          ppc64_elf_unhandled_reloc, /* special_function */
1836          "R_PPC64_GOT_TLSLD16_HI", /* name */
1837          FALSE,                 /* partial_inplace */
1838          0,                     /* src_mask */
1839          0xffff,                /* dst_mask */
1840          FALSE),                /* pcrel_offset */
1841
1842   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1843   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1844          16,                    /* rightshift */
1845          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1846          16,                    /* bitsize */
1847          FALSE,                 /* pc_relative */
1848          0,                     /* bitpos */
1849          complain_overflow_signed, /* complain_on_overflow */
1850          ppc64_elf_unhandled_reloc, /* special_function */
1851          "R_PPC64_GOT_TLSLD16_HA", /* name */
1852          FALSE,                 /* partial_inplace */
1853          0,                     /* src_mask */
1854          0xffff,                /* dst_mask */
1855          FALSE),                /* pcrel_offset */
1856
1857   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1858      the offset to the entry relative to the TOC base (r2).  */
1859   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1860          0,                     /* rightshift */
1861          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1862          16,                    /* bitsize */
1863          FALSE,                 /* pc_relative */
1864          0,                     /* bitpos */
1865          complain_overflow_signed, /* complain_on_overflow */
1866          ppc64_elf_unhandled_reloc, /* special_function */
1867          "R_PPC64_GOT_DTPREL16_DS", /* name */
1868          FALSE,                 /* partial_inplace */
1869          0,                     /* src_mask */
1870          0xfffc,                /* dst_mask */
1871          FALSE),                /* pcrel_offset */
1872
1873   /* Like GOT_DTPREL16_DS, but no overflow.  */
1874   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1875          0,                     /* rightshift */
1876          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1877          16,                    /* bitsize */
1878          FALSE,                 /* pc_relative */
1879          0,                     /* bitpos */
1880          complain_overflow_dont, /* complain_on_overflow */
1881          ppc64_elf_unhandled_reloc, /* special_function */
1882          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1883          FALSE,                 /* partial_inplace */
1884          0,                     /* src_mask */
1885          0xfffc,                /* dst_mask */
1886          FALSE),                /* pcrel_offset */
1887
1888   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1889   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1890          16,                    /* rightshift */
1891          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1892          16,                    /* bitsize */
1893          FALSE,                 /* pc_relative */
1894          0,                     /* bitpos */
1895          complain_overflow_signed, /* complain_on_overflow */
1896          ppc64_elf_unhandled_reloc, /* special_function */
1897          "R_PPC64_GOT_DTPREL16_HI", /* name */
1898          FALSE,                 /* partial_inplace */
1899          0,                     /* src_mask */
1900          0xffff,                /* dst_mask */
1901          FALSE),                /* pcrel_offset */
1902
1903   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1904   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1905          16,                    /* rightshift */
1906          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1907          16,                    /* bitsize */
1908          FALSE,                 /* pc_relative */
1909          0,                     /* bitpos */
1910          complain_overflow_signed, /* complain_on_overflow */
1911          ppc64_elf_unhandled_reloc, /* special_function */
1912          "R_PPC64_GOT_DTPREL16_HA", /* name */
1913          FALSE,                 /* partial_inplace */
1914          0,                     /* src_mask */
1915          0xffff,                /* dst_mask */
1916          FALSE),                /* pcrel_offset */
1917
1918   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1919      offset to the entry relative to the TOC base (r2).  */
1920   HOWTO (R_PPC64_GOT_TPREL16_DS,
1921          0,                     /* rightshift */
1922          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1923          16,                    /* bitsize */
1924          FALSE,                 /* pc_relative */
1925          0,                     /* bitpos */
1926          complain_overflow_signed, /* complain_on_overflow */
1927          ppc64_elf_unhandled_reloc, /* special_function */
1928          "R_PPC64_GOT_TPREL16_DS", /* name */
1929          FALSE,                 /* partial_inplace */
1930          0,                     /* src_mask */
1931          0xfffc,                /* dst_mask */
1932          FALSE),                /* pcrel_offset */
1933
1934   /* Like GOT_TPREL16_DS, but no overflow.  */
1935   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1936          0,                     /* rightshift */
1937          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1938          16,                    /* bitsize */
1939          FALSE,                 /* pc_relative */
1940          0,                     /* bitpos */
1941          complain_overflow_dont, /* complain_on_overflow */
1942          ppc64_elf_unhandled_reloc, /* special_function */
1943          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1944          FALSE,                 /* partial_inplace */
1945          0,                     /* src_mask */
1946          0xfffc,                /* dst_mask */
1947          FALSE),                /* pcrel_offset */
1948
1949   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1950   HOWTO (R_PPC64_GOT_TPREL16_HI,
1951          16,                    /* rightshift */
1952          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1953          16,                    /* bitsize */
1954          FALSE,                 /* pc_relative */
1955          0,                     /* bitpos */
1956          complain_overflow_signed, /* complain_on_overflow */
1957          ppc64_elf_unhandled_reloc, /* special_function */
1958          "R_PPC64_GOT_TPREL16_HI", /* name */
1959          FALSE,                 /* partial_inplace */
1960          0,                     /* src_mask */
1961          0xffff,                /* dst_mask */
1962          FALSE),                /* pcrel_offset */
1963
1964   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1965   HOWTO (R_PPC64_GOT_TPREL16_HA,
1966          16,                    /* rightshift */
1967          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1968          16,                    /* bitsize */
1969          FALSE,                 /* pc_relative */
1970          0,                     /* bitpos */
1971          complain_overflow_signed, /* complain_on_overflow */
1972          ppc64_elf_unhandled_reloc, /* special_function */
1973          "R_PPC64_GOT_TPREL16_HA", /* name */
1974          FALSE,                 /* partial_inplace */
1975          0,                     /* src_mask */
1976          0xffff,                /* dst_mask */
1977          FALSE),                /* pcrel_offset */
1978
1979   HOWTO (R_PPC64_JMP_IREL,      /* type */
1980          0,                     /* rightshift */
1981          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1982          0,                     /* bitsize */
1983          FALSE,                 /* pc_relative */
1984          0,                     /* bitpos */
1985          complain_overflow_dont, /* complain_on_overflow */
1986          ppc64_elf_unhandled_reloc, /* special_function */
1987          "R_PPC64_JMP_IREL",    /* name */
1988          FALSE,                 /* partial_inplace */
1989          0,                     /* src_mask */
1990          0,                     /* dst_mask */
1991          FALSE),                /* pcrel_offset */
1992
1993   HOWTO (R_PPC64_IRELATIVE,     /* type */
1994          0,                     /* rightshift */
1995          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1996          64,                    /* bitsize */
1997          FALSE,                 /* pc_relative */
1998          0,                     /* bitpos */
1999          complain_overflow_dont, /* complain_on_overflow */
2000          bfd_elf_generic_reloc, /* special_function */
2001          "R_PPC64_IRELATIVE",   /* name */
2002          FALSE,                 /* partial_inplace */
2003          0,                     /* src_mask */
2004          ONES (64),             /* dst_mask */
2005          FALSE),                /* pcrel_offset */
2006
2007   /* A 16 bit relative relocation.  */
2008   HOWTO (R_PPC64_REL16,         /* type */
2009          0,                     /* rightshift */
2010          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2011          16,                    /* bitsize */
2012          TRUE,                  /* pc_relative */
2013          0,                     /* bitpos */
2014          complain_overflow_signed, /* complain_on_overflow */
2015          bfd_elf_generic_reloc, /* special_function */
2016          "R_PPC64_REL16",       /* name */
2017          FALSE,                 /* partial_inplace */
2018          0,                     /* src_mask */
2019          0xffff,                /* dst_mask */
2020          TRUE),                 /* pcrel_offset */
2021
2022   /* A 16 bit relative relocation without overflow.  */
2023   HOWTO (R_PPC64_REL16_LO,      /* type */
2024          0,                     /* rightshift */
2025          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2026          16,                    /* bitsize */
2027          TRUE,                  /* pc_relative */
2028          0,                     /* bitpos */
2029          complain_overflow_dont,/* complain_on_overflow */
2030          bfd_elf_generic_reloc, /* special_function */
2031          "R_PPC64_REL16_LO",    /* name */
2032          FALSE,                 /* partial_inplace */
2033          0,                     /* src_mask */
2034          0xffff,                /* dst_mask */
2035          TRUE),                 /* pcrel_offset */
2036
2037   /* The high order 16 bits of a relative address.  */
2038   HOWTO (R_PPC64_REL16_HI,      /* type */
2039          16,                    /* rightshift */
2040          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2041          16,                    /* bitsize */
2042          TRUE,                  /* pc_relative */
2043          0,                     /* bitpos */
2044          complain_overflow_signed, /* complain_on_overflow */
2045          bfd_elf_generic_reloc, /* special_function */
2046          "R_PPC64_REL16_HI",    /* name */
2047          FALSE,                 /* partial_inplace */
2048          0,                     /* src_mask */
2049          0xffff,                /* dst_mask */
2050          TRUE),                 /* pcrel_offset */
2051
2052   /* The high order 16 bits of a relative address, plus 1 if the contents of
2053      the low 16 bits, treated as a signed number, is negative.  */
2054   HOWTO (R_PPC64_REL16_HA,      /* type */
2055          16,                    /* rightshift */
2056          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2057          16,                    /* bitsize */
2058          TRUE,                  /* pc_relative */
2059          0,                     /* bitpos */
2060          complain_overflow_signed, /* complain_on_overflow */
2061          ppc64_elf_ha_reloc,    /* special_function */
2062          "R_PPC64_REL16_HA",    /* name */
2063          FALSE,                 /* partial_inplace */
2064          0,                     /* src_mask */
2065          0xffff,                /* dst_mask */
2066          TRUE),                 /* pcrel_offset */
2067
2068   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2069   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2070          16,                    /* rightshift */
2071          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2072          16,                    /* bitsize */
2073          TRUE,                  /* pc_relative */
2074          0,                     /* bitpos */
2075          complain_overflow_signed, /* complain_on_overflow */
2076          ppc64_elf_ha_reloc,    /* special_function */
2077          "R_PPC64_REL16DX_HA",  /* name */
2078          FALSE,                 /* partial_inplace */
2079          0,                     /* src_mask */
2080          0x1fffc1,              /* dst_mask */
2081          TRUE),                 /* pcrel_offset */
2082
2083   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
2084   HOWTO (R_PPC64_16DX_HA,       /* type */
2085          16,                    /* rightshift */
2086          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2087          16,                    /* bitsize */
2088          FALSE,                 /* pc_relative */
2089          0,                     /* bitpos */
2090          complain_overflow_signed, /* complain_on_overflow */
2091          ppc64_elf_ha_reloc,    /* special_function */
2092          "R_PPC64_16DX_HA",     /* name */
2093          FALSE,                 /* partial_inplace */
2094          0,                     /* src_mask */
2095          0x1fffc1,              /* dst_mask */
2096          FALSE),                /* pcrel_offset */
2097
2098   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2099   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2100          16,                    /* rightshift */
2101          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2102          16,                    /* bitsize */
2103          FALSE,                 /* pc_relative */
2104          0,                     /* bitpos */
2105          complain_overflow_dont, /* complain_on_overflow */
2106          bfd_elf_generic_reloc, /* special_function */
2107          "R_PPC64_ADDR16_HIGH", /* name */
2108          FALSE,                 /* partial_inplace */
2109          0,                     /* src_mask */
2110          0xffff,                /* dst_mask */
2111          FALSE),                /* pcrel_offset */
2112
2113   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2114   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2115          16,                    /* rightshift */
2116          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2117          16,                    /* bitsize */
2118          FALSE,                 /* pc_relative */
2119          0,                     /* bitpos */
2120          complain_overflow_dont, /* complain_on_overflow */
2121          ppc64_elf_ha_reloc,    /* special_function */
2122          "R_PPC64_ADDR16_HIGHA",        /* name */
2123          FALSE,                 /* partial_inplace */
2124          0,                     /* src_mask */
2125          0xffff,                /* dst_mask */
2126          FALSE),                /* pcrel_offset */
2127
2128   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2129   HOWTO (R_PPC64_DTPREL16_HIGH,
2130          16,                    /* rightshift */
2131          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2132          16,                    /* bitsize */
2133          FALSE,                 /* pc_relative */
2134          0,                     /* bitpos */
2135          complain_overflow_dont, /* complain_on_overflow */
2136          ppc64_elf_unhandled_reloc, /* special_function */
2137          "R_PPC64_DTPREL16_HIGH", /* name */
2138          FALSE,                 /* partial_inplace */
2139          0,                     /* src_mask */
2140          0xffff,                /* dst_mask */
2141          FALSE),                /* pcrel_offset */
2142
2143   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2144   HOWTO (R_PPC64_DTPREL16_HIGHA,
2145          16,                    /* rightshift */
2146          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2147          16,                    /* bitsize */
2148          FALSE,                 /* pc_relative */
2149          0,                     /* bitpos */
2150          complain_overflow_dont, /* complain_on_overflow */
2151          ppc64_elf_unhandled_reloc, /* special_function */
2152          "R_PPC64_DTPREL16_HIGHA", /* name */
2153          FALSE,                 /* partial_inplace */
2154          0,                     /* src_mask */
2155          0xffff,                /* dst_mask */
2156          FALSE),                /* pcrel_offset */
2157
2158   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2159   HOWTO (R_PPC64_TPREL16_HIGH,
2160          16,                    /* rightshift */
2161          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2162          16,                    /* bitsize */
2163          FALSE,                 /* pc_relative */
2164          0,                     /* bitpos */
2165          complain_overflow_dont, /* complain_on_overflow */
2166          ppc64_elf_unhandled_reloc, /* special_function */
2167          "R_PPC64_TPREL16_HIGH",        /* name */
2168          FALSE,                 /* partial_inplace */
2169          0,                     /* src_mask */
2170          0xffff,                /* dst_mask */
2171          FALSE),                /* pcrel_offset */
2172
2173   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2174   HOWTO (R_PPC64_TPREL16_HIGHA,
2175          16,                    /* rightshift */
2176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2177          16,                    /* bitsize */
2178          FALSE,                 /* pc_relative */
2179          0,                     /* bitpos */
2180          complain_overflow_dont, /* complain_on_overflow */
2181          ppc64_elf_unhandled_reloc, /* special_function */
2182          "R_PPC64_TPREL16_HIGHA",       /* name */
2183          FALSE,                 /* partial_inplace */
2184          0,                     /* src_mask */
2185          0xffff,                /* dst_mask */
2186          FALSE),                /* pcrel_offset */
2187
2188   /* Marker reloc on ELFv2 large-model function entry.  */
2189   HOWTO (R_PPC64_ENTRY,
2190          0,                     /* rightshift */
2191          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2192          32,                    /* bitsize */
2193          FALSE,                 /* pc_relative */
2194          0,                     /* bitpos */
2195          complain_overflow_dont, /* complain_on_overflow */
2196          bfd_elf_generic_reloc, /* special_function */
2197          "R_PPC64_ENTRY",       /* name */
2198          FALSE,                 /* partial_inplace */
2199          0,                     /* src_mask */
2200          0,                     /* dst_mask */
2201          FALSE),                /* pcrel_offset */
2202
2203   /* Like ADDR64, but use local entry point of function.  */
2204   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2205          0,                     /* rightshift */
2206          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2207          64,                    /* bitsize */
2208          FALSE,                 /* pc_relative */
2209          0,                     /* bitpos */
2210          complain_overflow_dont, /* complain_on_overflow */
2211          bfd_elf_generic_reloc, /* special_function */
2212          "R_PPC64_ADDR64_LOCAL", /* name */
2213          FALSE,                 /* partial_inplace */
2214          0,                     /* src_mask */
2215          ONES (64),             /* dst_mask */
2216          FALSE),                /* pcrel_offset */
2217
2218   /* GNU extension to record C++ vtable hierarchy.  */
2219   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2220          0,                     /* rightshift */
2221          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2222          0,                     /* bitsize */
2223          FALSE,                 /* pc_relative */
2224          0,                     /* bitpos */
2225          complain_overflow_dont, /* complain_on_overflow */
2226          NULL,                  /* special_function */
2227          "R_PPC64_GNU_VTINHERIT", /* name */
2228          FALSE,                 /* partial_inplace */
2229          0,                     /* src_mask */
2230          0,                     /* dst_mask */
2231          FALSE),                /* pcrel_offset */
2232
2233   /* GNU extension to record C++ vtable member usage.  */
2234   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2235          0,                     /* rightshift */
2236          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2237          0,                     /* bitsize */
2238          FALSE,                 /* pc_relative */
2239          0,                     /* bitpos */
2240          complain_overflow_dont, /* complain_on_overflow */
2241          NULL,                  /* special_function */
2242          "R_PPC64_GNU_VTENTRY", /* name */
2243          FALSE,                 /* partial_inplace */
2244          0,                     /* src_mask */
2245          0,                     /* dst_mask */
2246          FALSE),                /* pcrel_offset */
2247 };
2248
2249 \f
2250 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2251    be done.  */
2252
2253 static void
2254 ppc_howto_init (void)
2255 {
2256   unsigned int i, type;
2257
2258   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2259     {
2260       type = ppc64_elf_howto_raw[i].type;
2261       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2262       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2263     }
2264 }
2265
2266 static reloc_howto_type *
2267 ppc64_elf_reloc_type_lookup (bfd *abfd,
2268                              bfd_reloc_code_real_type code)
2269 {
2270   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2271
2272   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2273     /* Initialize howto table if needed.  */
2274     ppc_howto_init ();
2275
2276   switch (code)
2277     {
2278     default:
2279       /* xgettext:c-format */
2280       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2281       bfd_set_error (bfd_error_bad_value);
2282       return NULL;
2283
2284     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2285       break;
2286     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2287       break;
2288     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2289       break;
2290     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2291       break;
2292     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2293       break;
2294     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2295       break;
2296     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2297       break;
2298     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2299       break;
2300     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2301       break;
2302     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2303       break;
2304     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2305       break;
2306     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2307       break;
2308     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2309       break;
2310     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2311       break;
2312     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2313       break;
2314     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2315       break;
2316     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2317       break;
2318     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2319       break;
2320     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2321       break;
2322     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2323       break;
2324     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2325       break;
2326     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2327       break;
2328     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2329       break;
2330     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2331       break;
2332     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2333       break;
2334     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2335       break;
2336     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2337       break;
2338     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2339       break;
2340     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2341       break;
2342     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2343       break;
2344     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2345       break;
2346     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2347       break;
2348     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2349       break;
2350     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2351       break;
2352     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2353       break;
2354     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2355       break;
2356     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2357       break;
2358     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2359       break;
2360     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2361       break;
2362     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2363       break;
2364     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2365       break;
2366     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2367       break;
2368     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2369       break;
2370     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2371       break;
2372     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2373       break;
2374     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2375       break;
2376     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2377       break;
2378     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2379       break;
2380     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2381       break;
2382     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2383       break;
2384     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2385       break;
2386     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2387       break;
2388     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2389       break;
2390     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2391       break;
2392     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2393       break;
2394     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2395       break;
2396     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2397       break;
2398     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2399       break;
2400     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2401       break;
2402     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2403       break;
2404     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2405       break;
2406     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2407       break;
2408     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2409       break;
2410     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2411       break;
2412     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2413       break;
2414     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2415       break;
2416     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2417       break;
2418     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2419       break;
2420     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2421       break;
2422     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2423       break;
2424     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2425       break;
2426     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2427       break;
2428     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2429       break;
2430     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2431       break;
2432     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2433       break;
2434     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2435       break;
2436     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2437       break;
2438     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2439       break;
2440     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2441       break;
2442     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2443       break;
2444     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2445       break;
2446     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2447       break;
2448     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2449       break;
2450     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2451       break;
2452     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2453       break;
2454     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2455       break;
2456     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2457       break;
2458     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2459       break;
2460     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2461       break;
2462     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2463       break;
2464     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2465       break;
2466     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2467       break;
2468     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2469       break;
2470     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2471       break;
2472     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2473       break;
2474     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2475       break;
2476     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2477       break;
2478     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2479       break;
2480     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2481       break;
2482     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2483       break;
2484     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2485       break;
2486     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2487       break;
2488     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2489       break;
2490     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2491       break;
2492     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2493       break;
2494     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2495       break;
2496     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2497       break;
2498     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2499       break;
2500     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2501       break;
2502     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2503       break;
2504     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2505       break;
2506     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
2507       break;
2508     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2509       break;
2510     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2511       break;
2512     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2513       break;
2514     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2515       break;
2516     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2517       break;
2518     }
2519
2520   return ppc64_elf_howto_table[r];
2521 };
2522
2523 static reloc_howto_type *
2524 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2525                              const char *r_name)
2526 {
2527   unsigned int i;
2528
2529   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2530     if (ppc64_elf_howto_raw[i].name != NULL
2531         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2532       return &ppc64_elf_howto_raw[i];
2533
2534   
2535   return NULL;
2536 }
2537
2538 /* Set the howto pointer for a PowerPC ELF reloc.  */
2539
2540 static bfd_boolean
2541 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2542                          Elf_Internal_Rela *dst)
2543 {
2544   unsigned int type;
2545
2546   /* Initialize howto table if needed.  */
2547   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2548     ppc_howto_init ();
2549
2550   type = ELF64_R_TYPE (dst->r_info);
2551   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2552     {
2553       /* xgettext:c-format */
2554       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2555                           abfd, type);
2556       bfd_set_error (bfd_error_bad_value);
2557       return FALSE;
2558     }
2559   cache_ptr->howto = ppc64_elf_howto_table[type];
2560   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2561     {
2562       /* xgettext:c-format */
2563       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2564                           abfd, type);
2565       bfd_set_error (bfd_error_bad_value);
2566       return FALSE;
2567     }
2568   
2569   return TRUE;
2570 }
2571
2572 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2573
2574 static bfd_reloc_status_type
2575 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2576                     void *data, asection *input_section,
2577                     bfd *output_bfd, char **error_message)
2578 {
2579   enum elf_ppc64_reloc_type r_type;
2580   long insn;
2581   bfd_size_type octets;
2582   bfd_vma value;
2583
2584   /* If this is a relocatable link (output_bfd test tells us), just
2585      call the generic function.  Any adjustment will be done at final
2586      link time.  */
2587   if (output_bfd != NULL)
2588     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2589                                   input_section, output_bfd, error_message);
2590
2591   /* Adjust the addend for sign extension of the low 16 bits.
2592      We won't actually be using the low 16 bits, so trashing them
2593      doesn't matter.  */
2594   reloc_entry->addend += 0x8000;
2595   r_type = reloc_entry->howto->type;
2596   if (r_type != R_PPC64_REL16DX_HA)
2597     return bfd_reloc_continue;
2598
2599   value = 0;
2600   if (!bfd_is_com_section (symbol->section))
2601     value = symbol->value;
2602   value += (reloc_entry->addend
2603             + symbol->section->output_offset
2604             + symbol->section->output_section->vma);
2605   value -= (reloc_entry->address
2606             + input_section->output_offset
2607             + input_section->output_section->vma);
2608   value = (bfd_signed_vma) value >> 16;
2609
2610   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2611   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2612   insn &= ~0x1fffc1;
2613   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2614   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2615   if (value + 0x8000 > 0xffff)
2616     return bfd_reloc_overflow;
2617   return bfd_reloc_ok;
2618 }
2619
2620 static bfd_reloc_status_type
2621 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2622                         void *data, asection *input_section,
2623                         bfd *output_bfd, char **error_message)
2624 {
2625   if (output_bfd != NULL)
2626     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2627                                   input_section, output_bfd, error_message);
2628
2629   if (strcmp (symbol->section->name, ".opd") == 0
2630       && (symbol->section->owner->flags & DYNAMIC) == 0)
2631     {
2632       bfd_vma dest = opd_entry_value (symbol->section,
2633                                       symbol->value + reloc_entry->addend,
2634                                       NULL, NULL, FALSE);
2635       if (dest != (bfd_vma) -1)
2636         reloc_entry->addend = dest - (symbol->value
2637                                       + symbol->section->output_section->vma
2638                                       + symbol->section->output_offset);
2639     }
2640   else
2641     {
2642       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2643
2644       if (symbol->section->owner != abfd
2645           && symbol->section->owner != NULL
2646           && abiversion (symbol->section->owner) >= 2)
2647         {
2648           unsigned int i;
2649
2650           for (i = 0; i < symbol->section->owner->symcount; ++i)
2651             {
2652               asymbol *symdef = symbol->section->owner->outsymbols[i];
2653
2654               if (strcmp (symdef->name, symbol->name) == 0)
2655                 {
2656                   elfsym = (elf_symbol_type *) symdef;
2657                   break;
2658                 }
2659             }
2660         }
2661       reloc_entry->addend
2662         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2663     }
2664   return bfd_reloc_continue;
2665 }
2666
2667 static bfd_reloc_status_type
2668 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2669                          void *data, asection *input_section,
2670                          bfd *output_bfd, char **error_message)
2671 {
2672   long insn;
2673   enum elf_ppc64_reloc_type r_type;
2674   bfd_size_type octets;
2675   /* Assume 'at' branch hints.  */
2676   bfd_boolean is_isa_v2 = TRUE;
2677
2678   /* If this is a relocatable link (output_bfd test tells us), just
2679      call the generic function.  Any adjustment will be done at final
2680      link time.  */
2681   if (output_bfd != NULL)
2682     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2683                                   input_section, output_bfd, error_message);
2684
2685   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2686   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2687   insn &= ~(0x01 << 21);
2688   r_type = reloc_entry->howto->type;
2689   if (r_type == R_PPC64_ADDR14_BRTAKEN
2690       || r_type == R_PPC64_REL14_BRTAKEN)
2691     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2692
2693   if (is_isa_v2)
2694     {
2695       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2696          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2697          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2698       if ((insn & (0x14 << 21)) == (0x04 << 21))
2699         insn |= 0x02 << 21;
2700       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2701         insn |= 0x08 << 21;
2702       else
2703         goto out;
2704     }
2705   else
2706     {
2707       bfd_vma target = 0;
2708       bfd_vma from;
2709
2710       if (!bfd_is_com_section (symbol->section))
2711         target = symbol->value;
2712       target += symbol->section->output_section->vma;
2713       target += symbol->section->output_offset;
2714       target += reloc_entry->addend;
2715
2716       from = (reloc_entry->address
2717               + input_section->output_offset
2718               + input_section->output_section->vma);
2719
2720       /* Invert 'y' bit if not the default.  */
2721       if ((bfd_signed_vma) (target - from) < 0)
2722         insn ^= 0x01 << 21;
2723     }
2724   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2725  out:
2726   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2727                                  input_section, output_bfd, error_message);
2728 }
2729
2730 static bfd_reloc_status_type
2731 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2732                          void *data, asection *input_section,
2733                          bfd *output_bfd, char **error_message)
2734 {
2735   /* If this is a relocatable link (output_bfd test tells us), just
2736      call the generic function.  Any adjustment will be done at final
2737      link time.  */
2738   if (output_bfd != NULL)
2739     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2740                                   input_section, output_bfd, error_message);
2741
2742   /* Subtract the symbol section base address.  */
2743   reloc_entry->addend -= symbol->section->output_section->vma;
2744   return bfd_reloc_continue;
2745 }
2746
2747 static bfd_reloc_status_type
2748 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2749                             void *data, asection *input_section,
2750                             bfd *output_bfd, char **error_message)
2751 {
2752   /* If this is a relocatable link (output_bfd test tells us), just
2753      call the generic function.  Any adjustment will be done at final
2754      link time.  */
2755   if (output_bfd != NULL)
2756     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2757                                   input_section, output_bfd, error_message);
2758
2759   /* Subtract the symbol section base address.  */
2760   reloc_entry->addend -= symbol->section->output_section->vma;
2761
2762   /* Adjust the addend for sign extension of the low 16 bits.  */
2763   reloc_entry->addend += 0x8000;
2764   return bfd_reloc_continue;
2765 }
2766
2767 static bfd_reloc_status_type
2768 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2769                      void *data, asection *input_section,
2770                      bfd *output_bfd, char **error_message)
2771 {
2772   bfd_vma TOCstart;
2773
2774   /* If this is a relocatable link (output_bfd test tells us), just
2775      call the generic function.  Any adjustment will be done at final
2776      link time.  */
2777   if (output_bfd != NULL)
2778     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2779                                   input_section, output_bfd, error_message);
2780
2781   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2782   if (TOCstart == 0)
2783     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2784
2785   /* Subtract the TOC base address.  */
2786   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2787   return bfd_reloc_continue;
2788 }
2789
2790 static bfd_reloc_status_type
2791 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2792                         void *data, asection *input_section,
2793                         bfd *output_bfd, char **error_message)
2794 {
2795   bfd_vma TOCstart;
2796
2797   /* If this is a relocatable link (output_bfd test tells us), just
2798      call the generic function.  Any adjustment will be done at final
2799      link time.  */
2800   if (output_bfd != NULL)
2801     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2802                                   input_section, output_bfd, error_message);
2803
2804   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2805   if (TOCstart == 0)
2806     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2807
2808   /* Subtract the TOC base address.  */
2809   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2810
2811   /* Adjust the addend for sign extension of the low 16 bits.  */
2812   reloc_entry->addend += 0x8000;
2813   return bfd_reloc_continue;
2814 }
2815
2816 static bfd_reloc_status_type
2817 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2818                        void *data, asection *input_section,
2819                        bfd *output_bfd, char **error_message)
2820 {
2821   bfd_vma TOCstart;
2822   bfd_size_type octets;
2823
2824   /* If this is a relocatable link (output_bfd test tells us), just
2825      call the generic function.  Any adjustment will be done at final
2826      link time.  */
2827   if (output_bfd != NULL)
2828     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2829                                   input_section, output_bfd, error_message);
2830
2831   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2832   if (TOCstart == 0)
2833     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2834
2835   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2836   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2837   return bfd_reloc_ok;
2838 }
2839
2840 static bfd_reloc_status_type
2841 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2842                            void *data, asection *input_section,
2843                            bfd *output_bfd, char **error_message)
2844 {
2845   /* If this is a relocatable link (output_bfd test tells us), just
2846      call the generic function.  Any adjustment will be done at final
2847      link time.  */
2848   if (output_bfd != NULL)
2849     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2850                                   input_section, output_bfd, error_message);
2851
2852   if (error_message != NULL)
2853     {
2854       static char buf[60];
2855       sprintf (buf, "generic linker can't handle %s",
2856                reloc_entry->howto->name);
2857       *error_message = buf;
2858     }
2859   return bfd_reloc_dangerous;
2860 }
2861
2862 /* Track GOT entries needed for a given symbol.  We might need more
2863    than one got entry per symbol.  */
2864 struct got_entry
2865 {
2866   struct got_entry *next;
2867
2868   /* The symbol addend that we'll be placing in the GOT.  */
2869   bfd_vma addend;
2870
2871   /* Unlike other ELF targets, we use separate GOT entries for the same
2872      symbol referenced from different input files.  This is to support
2873      automatic multiple TOC/GOT sections, where the TOC base can vary
2874      from one input file to another.  After partitioning into TOC groups
2875      we merge entries within the group.
2876
2877      Point to the BFD owning this GOT entry.  */
2878   bfd *owner;
2879
2880   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2881      TLS_TPREL or TLS_DTPREL for tls entries.  */
2882   unsigned char tls_type;
2883
2884   /* Non-zero if got.ent points to real entry.  */
2885   unsigned char is_indirect;
2886
2887   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2888   union
2889     {
2890       bfd_signed_vma refcount;
2891       bfd_vma offset;
2892       struct got_entry *ent;
2893     } got;
2894 };
2895
2896 /* The same for PLT.  */
2897 struct plt_entry
2898 {
2899   struct plt_entry *next;
2900
2901   bfd_vma addend;
2902
2903   union
2904     {
2905       bfd_signed_vma refcount;
2906       bfd_vma offset;
2907     } plt;
2908 };
2909
2910 struct ppc64_elf_obj_tdata
2911 {
2912   struct elf_obj_tdata elf;
2913
2914   /* Shortcuts to dynamic linker sections.  */
2915   asection *got;
2916   asection *relgot;
2917
2918   /* Used during garbage collection.  We attach global symbols defined
2919      on removed .opd entries to this section so that the sym is removed.  */
2920   asection *deleted_section;
2921
2922   /* TLS local dynamic got entry handling.  Support for multiple GOT
2923      sections means we potentially need one of these for each input bfd.  */
2924   struct got_entry tlsld_got;
2925
2926   union {
2927     /* A copy of relocs before they are modified for --emit-relocs.  */
2928     Elf_Internal_Rela *relocs;
2929
2930     /* Section contents.  */
2931     bfd_byte *contents;
2932   } opd;
2933
2934   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2935      the reloc to be in the range -32768 to 32767.  */
2936   unsigned int has_small_toc_reloc : 1;
2937
2938   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2939      instruction not one we handle.  */
2940   unsigned int unexpected_toc_insn : 1;
2941 };
2942
2943 #define ppc64_elf_tdata(bfd) \
2944   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2945
2946 #define ppc64_tlsld_got(bfd) \
2947   (&ppc64_elf_tdata (bfd)->tlsld_got)
2948
2949 #define is_ppc64_elf(bfd) \
2950   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2951    && elf_object_id (bfd) == PPC64_ELF_DATA)
2952
2953 /* Override the generic function because we store some extras.  */
2954
2955 static bfd_boolean
2956 ppc64_elf_mkobject (bfd *abfd)
2957 {
2958   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2959                                   PPC64_ELF_DATA);
2960 }
2961
2962 /* Fix bad default arch selected for a 64 bit input bfd when the
2963    default is 32 bit.  Also select arch based on apuinfo.  */
2964
2965 static bfd_boolean
2966 ppc64_elf_object_p (bfd *abfd)
2967 {
2968   if (!abfd->arch_info->the_default)
2969     return TRUE;
2970
2971   if (abfd->arch_info->bits_per_word == 32)
2972     {
2973       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2974
2975       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2976         {
2977           /* Relies on arch after 32 bit default being 64 bit default.  */
2978           abfd->arch_info = abfd->arch_info->next;
2979           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2980         }
2981     }
2982   return _bfd_elf_ppc_set_arch (abfd);
2983 }
2984
2985 /* Support for core dump NOTE sections.  */
2986
2987 static bfd_boolean
2988 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2989 {
2990   size_t offset, size;
2991
2992   if (note->descsz != 504)
2993     return FALSE;
2994
2995   /* pr_cursig */
2996   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2997
2998   /* pr_pid */
2999   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3000
3001   /* pr_reg */
3002   offset = 112;
3003   size = 384;
3004
3005   /* Make a ".reg/999" section.  */
3006   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3007                                           size, note->descpos + offset);
3008 }
3009
3010 static bfd_boolean
3011 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3012 {
3013   if (note->descsz != 136)
3014     return FALSE;
3015
3016   elf_tdata (abfd)->core->pid
3017     = bfd_get_32 (abfd, note->descdata + 24);
3018   elf_tdata (abfd)->core->program
3019     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3020   elf_tdata (abfd)->core->command
3021     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3022
3023   return TRUE;
3024 }
3025
3026 static char *
3027 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3028                            ...)
3029 {
3030   switch (note_type)
3031     {
3032     default:
3033       return NULL;
3034
3035     case NT_PRPSINFO:
3036       {
3037         char data[136];
3038         va_list ap;
3039
3040         va_start (ap, note_type);
3041         memset (data, 0, sizeof (data));
3042         strncpy (data + 40, va_arg (ap, const char *), 16);
3043         strncpy (data + 56, va_arg (ap, const char *), 80);
3044         va_end (ap);
3045         return elfcore_write_note (abfd, buf, bufsiz,
3046                                    "CORE", note_type, data, sizeof (data));
3047       }
3048
3049     case NT_PRSTATUS:
3050       {
3051         char data[504];
3052         va_list ap;
3053         long pid;
3054         int cursig;
3055         const void *greg;
3056
3057         va_start (ap, note_type);
3058         memset (data, 0, 112);
3059         pid = va_arg (ap, long);
3060         bfd_put_32 (abfd, pid, data + 32);
3061         cursig = va_arg (ap, int);
3062         bfd_put_16 (abfd, cursig, data + 12);
3063         greg = va_arg (ap, const void *);
3064         memcpy (data + 112, greg, 384);
3065         memset (data + 496, 0, 8);
3066         va_end (ap);
3067         return elfcore_write_note (abfd, buf, bufsiz,
3068                                    "CORE", note_type, data, sizeof (data));
3069       }
3070     }
3071 }
3072
3073 /* Add extra PPC sections.  */
3074
3075 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3076 {
3077   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3078   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3079   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3080   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3081   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3083   { NULL,                     0,  0, 0,            0 }
3084 };
3085
3086 enum _ppc64_sec_type {
3087   sec_normal = 0,
3088   sec_opd = 1,
3089   sec_toc = 2
3090 };
3091
3092 struct _ppc64_elf_section_data
3093 {
3094   struct bfd_elf_section_data elf;
3095
3096   union
3097   {
3098     /* An array with one entry for each opd function descriptor,
3099        and some spares since opd entries may be either 16 or 24 bytes.  */
3100 #define OPD_NDX(OFF) ((OFF) >> 4)
3101     struct _opd_sec_data
3102     {
3103       /* Points to the function code section for local opd entries.  */
3104       asection **func_sec;
3105
3106       /* After editing .opd, adjust references to opd local syms.  */
3107       long *adjust;
3108     } opd;
3109
3110     /* An array for toc sections, indexed by offset/8.  */
3111     struct _toc_sec_data
3112     {
3113       /* Specifies the relocation symbol index used at a given toc offset.  */
3114       unsigned *symndx;
3115
3116       /* And the relocation addend.  */
3117       bfd_vma *add;
3118     } toc;
3119   } u;
3120
3121   enum _ppc64_sec_type sec_type:2;
3122
3123   /* Flag set when small branches are detected.  Used to
3124      select suitable defaults for the stub group size.  */
3125   unsigned int has_14bit_branch:1;
3126
3127   /* Flag set when PLTCALL relocs are detected.  */
3128   unsigned int has_pltcall:1;
3129 };
3130
3131 #define ppc64_elf_section_data(sec) \
3132   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3133
3134 static bfd_boolean
3135 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3136 {
3137   if (!sec->used_by_bfd)
3138     {
3139       struct _ppc64_elf_section_data *sdata;
3140       bfd_size_type amt = sizeof (*sdata);
3141
3142       sdata = bfd_zalloc (abfd, amt);
3143       if (sdata == NULL)
3144         return FALSE;
3145       sec->used_by_bfd = sdata;
3146     }
3147
3148   return _bfd_elf_new_section_hook (abfd, sec);
3149 }
3150
3151 static struct _opd_sec_data *
3152 get_opd_info (asection * sec)
3153 {
3154   if (sec != NULL
3155       && ppc64_elf_section_data (sec) != NULL
3156       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3157     return &ppc64_elf_section_data (sec)->u.opd;
3158   return NULL;
3159 }
3160 \f
3161 /* Parameters for the qsort hook.  */
3162 static bfd_boolean synthetic_relocatable;
3163 static asection *synthetic_opd;
3164
3165 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3166
3167 static int
3168 compare_symbols (const void *ap, const void *bp)
3169 {
3170   const asymbol *a = * (const asymbol **) ap;
3171   const asymbol *b = * (const asymbol **) bp;
3172
3173   /* Section symbols first.  */
3174   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3175     return -1;
3176   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3177     return 1;
3178
3179   /* then .opd symbols.  */
3180   if (synthetic_opd != NULL)
3181     {
3182       if (strcmp (a->section->name, ".opd") == 0
3183           && strcmp (b->section->name, ".opd") != 0)
3184         return -1;
3185       if (strcmp (a->section->name, ".opd") != 0
3186           && strcmp (b->section->name, ".opd") == 0)
3187         return 1;
3188     }
3189
3190   /* then other code symbols.  */
3191   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3192       == (SEC_CODE | SEC_ALLOC)
3193       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3194          != (SEC_CODE | SEC_ALLOC))
3195     return -1;
3196
3197   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3198       != (SEC_CODE | SEC_ALLOC)
3199       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3200          == (SEC_CODE | SEC_ALLOC))
3201     return 1;
3202
3203   if (synthetic_relocatable)
3204     {
3205       if (a->section->id < b->section->id)
3206         return -1;
3207
3208       if (a->section->id > b->section->id)
3209         return 1;
3210     }
3211
3212   if (a->value + a->section->vma < b->value + b->section->vma)
3213     return -1;
3214
3215   if (a->value + a->section->vma > b->value + b->section->vma)
3216     return 1;
3217
3218   /* For syms with the same value, prefer strong dynamic global function
3219      syms over other syms.  */
3220   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3221     return -1;
3222
3223   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3224     return 1;
3225
3226   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3227     return -1;
3228
3229   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3230     return 1;
3231
3232   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3233     return -1;
3234
3235   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3236     return 1;
3237
3238   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3239     return -1;
3240
3241   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3242     return 1;
3243
3244   return a > b;
3245 }
3246
3247 /* Search SYMS for a symbol of the given VALUE.  */
3248
3249 static asymbol *
3250 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3251 {
3252   long mid;
3253
3254   if (id == (unsigned) -1)
3255     {
3256       while (lo < hi)
3257         {
3258           mid = (lo + hi) >> 1;
3259           if (syms[mid]->value + syms[mid]->section->vma < value)
3260             lo = mid + 1;
3261           else if (syms[mid]->value + syms[mid]->section->vma > value)
3262             hi = mid;
3263           else
3264             return syms[mid];
3265         }
3266     }
3267   else
3268     {
3269       while (lo < hi)
3270         {
3271           mid = (lo + hi) >> 1;
3272           if (syms[mid]->section->id < id)
3273             lo = mid + 1;
3274           else if (syms[mid]->section->id > id)
3275             hi = mid;
3276           else if (syms[mid]->value < value)
3277             lo = mid + 1;
3278           else if (syms[mid]->value > value)
3279             hi = mid;
3280           else
3281             return syms[mid];
3282         }
3283     }
3284   return NULL;
3285 }
3286
3287 static bfd_boolean
3288 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3289 {
3290   bfd_vma vma = *(bfd_vma *) ptr;
3291   return ((section->flags & SEC_ALLOC) != 0
3292           && section->vma <= vma
3293           && vma < section->vma + section->size);
3294 }
3295
3296 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3297    entry syms.  Also generate @plt symbols for the glink branch table.
3298    Returns count of synthetic symbols in RET or -1 on error.  */
3299
3300 static long
3301 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3302                                 long static_count, asymbol **static_syms,
3303                                 long dyn_count, asymbol **dyn_syms,
3304                                 asymbol **ret)
3305 {
3306   asymbol *s;
3307   size_t i, j, count;
3308   char *names;
3309   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3310   asection *opd = NULL;
3311   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3312   asymbol **syms;
3313   int abi = abiversion (abfd);
3314
3315   *ret = NULL;
3316
3317   if (abi < 2)
3318     {
3319       opd = bfd_get_section_by_name (abfd, ".opd");
3320       if (opd == NULL && abi == 1)
3321         return 0;
3322     }
3323
3324   syms = NULL;
3325   codesecsym = 0;
3326   codesecsymend = 0;
3327   secsymend = 0;
3328   opdsymend = 0;
3329   symcount = 0;
3330   if (opd != NULL)
3331     {
3332       symcount = static_count;
3333       if (!relocatable)
3334         symcount += dyn_count;
3335       if (symcount == 0)
3336         return 0;
3337
3338       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3339       if (syms == NULL)
3340         return -1;
3341
3342       if (!relocatable && static_count != 0 && dyn_count != 0)
3343         {
3344           /* Use both symbol tables.  */
3345           memcpy (syms, static_syms, static_count * sizeof (*syms));
3346           memcpy (syms + static_count, dyn_syms,
3347                   (dyn_count + 1) * sizeof (*syms));
3348         }
3349       else if (!relocatable && static_count == 0)
3350         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3351       else
3352         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3353
3354       /* Trim uninteresting symbols.  Interesting symbols are section,
3355          function, and notype symbols.  */
3356       for (i = 0, j = 0; i < symcount; ++i)
3357         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3358                                | BSF_RELC | BSF_SRELC)) == 0)
3359           syms[j++] = syms[i];
3360       symcount = j;
3361
3362       synthetic_relocatable = relocatable;
3363       synthetic_opd = opd;
3364       qsort (syms, symcount, sizeof (*syms), compare_symbols);
3365
3366       if (!relocatable && symcount > 1)
3367         {
3368           /* Trim duplicate syms, since we may have merged the normal and
3369              dynamic symbols.  Actually, we only care about syms that have
3370              different values, so trim any with the same value.  */
3371           for (i = 1, j = 1; i < symcount; ++i)
3372             if (syms[i - 1]->value + syms[i - 1]->section->vma
3373                 != syms[i]->value + syms[i]->section->vma)
3374               syms[j++] = syms[i];
3375           symcount = j;
3376         }
3377
3378       i = 0;
3379       /* Note that here and in compare_symbols we can't compare opd and
3380          sym->section directly.  With separate debug info files, the
3381          symbols will be extracted from the debug file while abfd passed
3382          to this function is the real binary.  */
3383       if (strcmp (syms[i]->section->name, ".opd") == 0)
3384         ++i;
3385       codesecsym = i;
3386
3387       for (; i < symcount; ++i)
3388         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3389                                          | SEC_THREAD_LOCAL))
3390              != (SEC_CODE | SEC_ALLOC))
3391             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3392           break;
3393       codesecsymend = i;
3394
3395       for (; i < symcount; ++i)
3396         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3397           break;
3398       secsymend = i;
3399
3400       for (; i < symcount; ++i)
3401         if (strcmp (syms[i]->section->name, ".opd") != 0)
3402           break;
3403       opdsymend = i;
3404
3405       for (; i < symcount; ++i)
3406         if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3407             != (SEC_CODE | SEC_ALLOC))
3408           break;
3409       symcount = i;
3410     }
3411   count = 0;
3412
3413   if (relocatable)
3414     {
3415       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3416       arelent *r;
3417       size_t size;
3418       size_t relcount;
3419
3420       if (opdsymend == secsymend)
3421         goto done;
3422
3423       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3424       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3425       if (relcount == 0)
3426         goto done;
3427
3428       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3429         {
3430           count = -1;
3431           goto done;
3432         }
3433
3434       size = 0;
3435       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3436         {
3437           asymbol *sym;
3438
3439           while (r < opd->relocation + relcount
3440                  && r->address < syms[i]->value + opd->vma)
3441             ++r;
3442
3443           if (r == opd->relocation + relcount)
3444             break;
3445
3446           if (r->address != syms[i]->value + opd->vma)
3447             continue;
3448
3449           if (r->howto->type != R_PPC64_ADDR64)
3450             continue;
3451
3452           sym = *r->sym_ptr_ptr;
3453           if (!sym_exists_at (syms, opdsymend, symcount,
3454                               sym->section->id, sym->value + r->addend))
3455             {
3456               ++count;
3457               size += sizeof (asymbol);
3458               size += strlen (syms[i]->name) + 2;
3459             }
3460         }
3461
3462       if (size == 0)
3463         goto done;
3464       s = *ret = bfd_malloc (size);
3465       if (s == NULL)
3466         {
3467           count = -1;
3468           goto done;
3469         }
3470
3471       names = (char *) (s + count);
3472
3473       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3474         {
3475           asymbol *sym;
3476
3477           while (r < opd->relocation + relcount
3478                  && r->address < syms[i]->value + opd->vma)
3479             ++r;
3480
3481           if (r == opd->relocation + relcount)
3482             break;
3483
3484           if (r->address != syms[i]->value + opd->vma)
3485             continue;
3486
3487           if (r->howto->type != R_PPC64_ADDR64)
3488             continue;
3489
3490           sym = *r->sym_ptr_ptr;
3491           if (!sym_exists_at (syms, opdsymend, symcount,
3492                               sym->section->id, sym->value + r->addend))
3493             {
3494               size_t len;
3495
3496               *s = *syms[i];
3497               s->flags |= BSF_SYNTHETIC;
3498               s->section = sym->section;
3499               s->value = sym->value + r->addend;
3500               s->name = names;
3501               *names++ = '.';
3502               len = strlen (syms[i]->name);
3503               memcpy (names, syms[i]->name, len + 1);
3504               names += len + 1;
3505               /* Have udata.p point back to the original symbol this
3506                  synthetic symbol was derived from.  */
3507               s->udata.p = syms[i];
3508               s++;
3509             }
3510         }
3511     }
3512   else
3513     {
3514       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3515       bfd_byte *contents = NULL;
3516       size_t size;
3517       size_t plt_count = 0;
3518       bfd_vma glink_vma = 0, resolv_vma = 0;
3519       asection *dynamic, *glink = NULL, *relplt = NULL;
3520       arelent *p;
3521
3522       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3523         {
3524         free_contents_and_exit_err:
3525           count = -1;
3526         free_contents_and_exit:
3527           if (contents)
3528             free (contents);
3529           goto done;
3530         }
3531
3532       size = 0;
3533       for (i = secsymend; i < opdsymend; ++i)
3534         {
3535           bfd_vma ent;
3536
3537           /* Ignore bogus symbols.  */
3538           if (syms[i]->value > opd->size - 8)
3539             continue;
3540
3541           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3542           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3543             {
3544               ++count;
3545               size += sizeof (asymbol);
3546               size += strlen (syms[i]->name) + 2;
3547             }
3548         }
3549
3550       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3551       if (dyn_count != 0
3552           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3553         {
3554           bfd_byte *dynbuf, *extdyn, *extdynend;
3555           size_t extdynsize;
3556           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3557
3558           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3559             goto free_contents_and_exit_err;
3560
3561           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3562           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3563
3564           extdyn = dynbuf;
3565           extdynend = extdyn + dynamic->size;
3566           for (; extdyn < extdynend; extdyn += extdynsize)
3567             {
3568               Elf_Internal_Dyn dyn;
3569               (*swap_dyn_in) (abfd, extdyn, &dyn);
3570
3571               if (dyn.d_tag == DT_NULL)
3572                 break;
3573
3574               if (dyn.d_tag == DT_PPC64_GLINK)
3575                 {
3576                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3577                      See comment in ppc64_elf_finish_dynamic_sections. */
3578                   glink_vma = dyn.d_un.d_val + 8 * 4;
3579                   /* The .glink section usually does not survive the final
3580                      link; search for the section (usually .text) where the
3581                      glink stubs now reside.  */
3582                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3583                                                 &glink_vma);
3584                   break;
3585                 }
3586             }
3587
3588           free (dynbuf);
3589         }
3590
3591       if (glink != NULL)
3592         {
3593           /* Determine __glink trampoline by reading the relative branch
3594              from the first glink stub.  */
3595           bfd_byte buf[4];
3596           unsigned int off = 0;
3597
3598           while (bfd_get_section_contents (abfd, glink, buf,
3599                                            glink_vma + off - glink->vma, 4))
3600             {
3601               unsigned int insn = bfd_get_32 (abfd, buf);
3602               insn ^= B_DOT;
3603               if ((insn & ~0x3fffffc) == 0)
3604                 {
3605                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3606                   break;
3607                 }
3608               off += 4;
3609               if (off > 4)
3610                 break;
3611             }
3612
3613           if (resolv_vma)
3614             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3615
3616           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3617           if (relplt != NULL)
3618             {
3619               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3620               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3621                 goto free_contents_and_exit_err;
3622
3623               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3624               size += plt_count * sizeof (asymbol);
3625
3626               p = relplt->relocation;
3627               for (i = 0; i < plt_count; i++, p++)
3628                 {
3629                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3630                   if (p->addend != 0)
3631                     size += sizeof ("+0x") - 1 + 16;
3632                 }
3633             }
3634         }
3635
3636       if (size == 0)
3637         goto free_contents_and_exit;
3638       s = *ret = bfd_malloc (size);
3639       if (s == NULL)
3640         goto free_contents_and_exit_err;
3641
3642       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3643
3644       for (i = secsymend; i < opdsymend; ++i)
3645         {
3646           bfd_vma ent;
3647
3648           if (syms[i]->value > opd->size - 8)
3649             continue;
3650
3651           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3652           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3653             {
3654               size_t lo, hi;
3655               size_t len;
3656               asection *sec = abfd->sections;
3657
3658               *s = *syms[i];
3659               lo = codesecsym;
3660               hi = codesecsymend;
3661               while (lo < hi)
3662                 {
3663                   size_t mid = (lo + hi) >> 1;
3664                   if (syms[mid]->section->vma < ent)
3665                     lo = mid + 1;
3666                   else if (syms[mid]->section->vma > ent)
3667                     hi = mid;
3668                   else
3669                     {
3670                       sec = syms[mid]->section;
3671                       break;
3672                     }
3673                 }
3674
3675               if (lo >= hi && lo > codesecsym)
3676                 sec = syms[lo - 1]->section;
3677
3678               for (; sec != NULL; sec = sec->next)
3679                 {
3680                   if (sec->vma > ent)
3681                     break;
3682                   /* SEC_LOAD may not be set if SEC is from a separate debug
3683                      info file.  */
3684                   if ((sec->flags & SEC_ALLOC) == 0)
3685                     break;
3686                   if ((sec->flags & SEC_CODE) != 0)
3687                     s->section = sec;
3688                 }
3689               s->flags |= BSF_SYNTHETIC;
3690               s->value = ent - s->section->vma;
3691               s->name = names;
3692               *names++ = '.';
3693               len = strlen (syms[i]->name);
3694               memcpy (names, syms[i]->name, len + 1);
3695               names += len + 1;
3696               /* Have udata.p point back to the original symbol this
3697                  synthetic symbol was derived from.  */
3698               s->udata.p = syms[i];
3699               s++;
3700             }
3701         }
3702       free (contents);
3703
3704       if (glink != NULL && relplt != NULL)
3705         {
3706           if (resolv_vma)
3707             {
3708               /* Add a symbol for the main glink trampoline.  */
3709               memset (s, 0, sizeof *s);
3710               s->the_bfd = abfd;
3711               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3712               s->section = glink;
3713               s->value = resolv_vma - glink->vma;
3714               s->name = names;
3715               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3716               names += sizeof ("__glink_PLTresolve");
3717               s++;
3718               count++;
3719             }
3720
3721           /* FIXME: It would be very much nicer to put sym@plt on the
3722              stub rather than on the glink branch table entry.  The
3723              objdump disassembler would then use a sensible symbol
3724              name on plt calls.  The difficulty in doing so is
3725              a) finding the stubs, and,
3726              b) matching stubs against plt entries, and,
3727              c) there can be multiple stubs for a given plt entry.
3728
3729              Solving (a) could be done by code scanning, but older
3730              ppc64 binaries used different stubs to current code.
3731              (b) is the tricky one since you need to known the toc
3732              pointer for at least one function that uses a pic stub to
3733              be able to calculate the plt address referenced.
3734              (c) means gdb would need to set multiple breakpoints (or
3735              find the glink branch itself) when setting breakpoints
3736              for pending shared library loads.  */
3737           p = relplt->relocation;
3738           for (i = 0; i < plt_count; i++, p++)
3739             {
3740               size_t len;
3741
3742               *s = **p->sym_ptr_ptr;
3743               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3744                  we are defining a symbol, ensure one of them is set.  */
3745               if ((s->flags & BSF_LOCAL) == 0)
3746                 s->flags |= BSF_GLOBAL;
3747               s->flags |= BSF_SYNTHETIC;
3748               s->section = glink;
3749               s->value = glink_vma - glink->vma;
3750               s->name = names;
3751               s->udata.p = NULL;
3752               len = strlen ((*p->sym_ptr_ptr)->name);
3753               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3754               names += len;
3755               if (p->addend != 0)
3756                 {
3757                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3758                   names += sizeof ("+0x") - 1;
3759                   bfd_sprintf_vma (abfd, names, p->addend);
3760                   names += strlen (names);
3761                 }
3762               memcpy (names, "@plt", sizeof ("@plt"));
3763               names += sizeof ("@plt");
3764               s++;
3765               if (abi < 2)
3766                 {
3767                   glink_vma += 8;
3768                   if (i >= 0x8000)
3769                     glink_vma += 4;
3770                 }
3771               else
3772                 glink_vma += 4;
3773             }
3774           count += plt_count;
3775         }
3776     }
3777
3778  done:
3779   free (syms);
3780   return count;
3781 }
3782 \f
3783 /* The following functions are specific to the ELF linker, while
3784    functions above are used generally.  Those named ppc64_elf_* are
3785    called by the main ELF linker code.  They appear in this file more
3786    or less in the order in which they are called.  eg.
3787    ppc64_elf_check_relocs is called early in the link process,
3788    ppc64_elf_finish_dynamic_sections is one of the last functions
3789    called.
3790
3791    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3792    functions have both a function code symbol and a function descriptor
3793    symbol.  A call to foo in a relocatable object file looks like:
3794
3795    .            .text
3796    .    x:
3797    .            bl      .foo
3798    .            nop
3799
3800    The function definition in another object file might be:
3801
3802    .            .section .opd
3803    .    foo:    .quad   .foo
3804    .            .quad   .TOC.@tocbase
3805    .            .quad   0
3806    .
3807    .            .text
3808    .    .foo:   blr
3809
3810    When the linker resolves the call during a static link, the branch
3811    unsurprisingly just goes to .foo and the .opd information is unused.
3812    If the function definition is in a shared library, things are a little
3813    different:  The call goes via a plt call stub, the opd information gets
3814    copied to the plt, and the linker patches the nop.
3815
3816    .    x:
3817    .            bl      .foo_stub
3818    .            ld      2,40(1)
3819    .
3820    .
3821    .    .foo_stub:
3822    .            std     2,40(1)                 # in practice, the call stub
3823    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3824    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3825    .            ld      12,0(11)
3826    .            ld      2,8(11)
3827    .            mtctr   12
3828    .            ld      11,16(11)
3829    .            bctr
3830    .
3831    .            .section .plt
3832    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3833
3834    The "reloc ()" notation is supposed to indicate that the linker emits
3835    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3836    copying.
3837
3838    What are the difficulties here?  Well, firstly, the relocations
3839    examined by the linker in check_relocs are against the function code
3840    sym .foo, while the dynamic relocation in the plt is emitted against
3841    the function descriptor symbol, foo.  Somewhere along the line, we need
3842    to carefully copy dynamic link information from one symbol to the other.
3843    Secondly, the generic part of the elf linker will make .foo a dynamic
3844    symbol as is normal for most other backends.  We need foo dynamic
3845    instead, at least for an application final link.  However, when
3846    creating a shared library containing foo, we need to have both symbols
3847    dynamic so that references to .foo are satisfied during the early
3848    stages of linking.  Otherwise the linker might decide to pull in a
3849    definition from some other object, eg. a static library.
3850
3851    Update: As of August 2004, we support a new convention.  Function
3852    calls may use the function descriptor symbol, ie. "bl foo".  This
3853    behaves exactly as "bl .foo".  */
3854
3855 /* Of those relocs that might be copied as dynamic relocs, this
3856    function selects those that must be copied when linking a shared
3857    library or PIE, even when the symbol is local.  */
3858
3859 static int
3860 must_be_dyn_reloc (struct bfd_link_info *info,
3861                    enum elf_ppc64_reloc_type r_type)
3862 {
3863   switch (r_type)
3864     {
3865     default:
3866       /* Only relative relocs can be resolved when the object load
3867          address isn't fixed.  DTPREL64 is excluded because the
3868          dynamic linker needs to differentiate global dynamic from
3869          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
3870       return 1;
3871
3872     case R_PPC64_REL32:
3873     case R_PPC64_REL64:
3874     case R_PPC64_REL30:
3875       return 0;
3876
3877     case R_PPC64_TPREL16:
3878     case R_PPC64_TPREL16_LO:
3879     case R_PPC64_TPREL16_HI:
3880     case R_PPC64_TPREL16_HA:
3881     case R_PPC64_TPREL16_DS:
3882     case R_PPC64_TPREL16_LO_DS:
3883     case R_PPC64_TPREL16_HIGH:
3884     case R_PPC64_TPREL16_HIGHA:
3885     case R_PPC64_TPREL16_HIGHER:
3886     case R_PPC64_TPREL16_HIGHERA:
3887     case R_PPC64_TPREL16_HIGHEST:
3888     case R_PPC64_TPREL16_HIGHESTA:
3889     case R_PPC64_TPREL64:
3890       /* These relocations are relative but in a shared library the
3891          linker doesn't know the thread pointer base.  */
3892       return bfd_link_dll (info);
3893     }
3894 }
3895
3896 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3897    copying dynamic variables from a shared lib into an app's dynbss
3898    section, and instead use a dynamic relocation to point into the
3899    shared lib.  With code that gcc generates, it's vital that this be
3900    enabled;  In the PowerPC64 ABI, the address of a function is actually
3901    the address of a function descriptor, which resides in the .opd
3902    section.  gcc uses the descriptor directly rather than going via the
3903    GOT as some other ABI's do, which means that initialized function
3904    pointers must reference the descriptor.  Thus, a function pointer
3905    initialized to the address of a function in a shared library will
3906    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3907    redefines the function descriptor symbol to point to the copy.  This
3908    presents a problem as a plt entry for that function is also
3909    initialized from the function descriptor symbol and the copy reloc
3910    may not be initialized first.  */
3911 #define ELIMINATE_COPY_RELOCS 1
3912
3913 /* Section name for stubs is the associated section name plus this
3914    string.  */
3915 #define STUB_SUFFIX ".stub"
3916
3917 /* Linker stubs.
3918    ppc_stub_long_branch:
3919    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3920    destination, but a 24 bit branch in a stub section will reach.
3921    .    b       dest
3922
3923    ppc_stub_plt_branch:
3924    Similar to the above, but a 24 bit branch in the stub section won't
3925    reach its destination.
3926    .    addis   %r11,%r2,xxx@toc@ha
3927    .    ld      %r12,xxx@toc@l(%r11)
3928    .    mtctr   %r12
3929    .    bctr
3930
3931    ppc_stub_plt_call:
3932    Used to call a function in a shared library.  If it so happens that
3933    the plt entry referenced crosses a 64k boundary, then an extra
3934    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3935    .    std     %r2,40(%r1)
3936    .    addis   %r11,%r2,xxx@toc@ha
3937    .    ld      %r12,xxx+0@toc@l(%r11)
3938    .    mtctr   %r12
3939    .    ld      %r2,xxx+8@toc@l(%r11)
3940    .    ld      %r11,xxx+16@toc@l(%r11)
3941    .    bctr
3942
3943    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3944    code to adjust the value and save r2 to support multiple toc sections.
3945    A ppc_stub_long_branch with an r2 offset looks like:
3946    .    std     %r2,40(%r1)
3947    .    addis   %r2,%r2,off@ha
3948    .    addi    %r2,%r2,off@l
3949    .    b       dest
3950
3951    A ppc_stub_plt_branch with an r2 offset looks like:
3952    .    std     %r2,40(%r1)
3953    .    addis   %r11,%r2,xxx@toc@ha
3954    .    ld      %r12,xxx@toc@l(%r11)
3955    .    addis   %r2,%r2,off@ha
3956    .    addi    %r2,%r2,off@l
3957    .    mtctr   %r12
3958    .    bctr
3959
3960    In cases where the "addis" instruction would add zero, the "addis" is
3961    omitted and following instructions modified slightly in some cases.
3962 */
3963
3964 enum ppc_stub_type {
3965   ppc_stub_none,
3966   ppc_stub_long_branch,
3967   ppc_stub_long_branch_r2off,
3968   ppc_stub_plt_branch,
3969   ppc_stub_plt_branch_r2off,
3970   ppc_stub_plt_call,
3971   ppc_stub_plt_call_r2save,
3972   ppc_stub_global_entry,
3973   ppc_stub_save_res
3974 };
3975
3976 /* Information on stub grouping.  */
3977 struct map_stub
3978 {
3979   /* The stub section.  */
3980   asection *stub_sec;
3981   /* This is the section to which stubs in the group will be attached.  */
3982   asection *link_sec;
3983   /* Next group.  */
3984   struct map_stub *next;
3985   /* Whether to emit a copy of register save/restore functions in this
3986      group.  */
3987   int needs_save_res;
3988   /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3989      or -1u if no such stub with bctrl exists.  */
3990   unsigned int tls_get_addr_opt_bctrl;
3991 };
3992
3993 struct ppc_stub_hash_entry {
3994
3995   /* Base hash table entry structure.  */
3996   struct bfd_hash_entry root;
3997
3998   enum ppc_stub_type stub_type;
3999
4000   /* Group information.  */
4001   struct map_stub *group;
4002
4003   /* Offset within stub_sec of the beginning of this stub.  */
4004   bfd_vma stub_offset;
4005
4006   /* Given the symbol's value and its section we can determine its final
4007      value when building the stubs (so the stub knows where to jump.  */
4008   bfd_vma target_value;
4009   asection *target_section;
4010
4011   /* The symbol table entry, if any, that this was derived from.  */
4012   struct ppc_link_hash_entry *h;
4013   struct plt_entry *plt_ent;
4014
4015   /* Symbol type.  */
4016   unsigned char symtype;
4017
4018   /* Symbol st_other.  */
4019   unsigned char other;
4020 };
4021
4022 struct ppc_branch_hash_entry {
4023
4024   /* Base hash table entry structure.  */
4025   struct bfd_hash_entry root;
4026
4027   /* Offset within branch lookup table.  */
4028   unsigned int offset;
4029
4030   /* Generation marker.  */
4031   unsigned int iter;
4032 };
4033
4034 /* Used to track dynamic relocations for local symbols.  */
4035 struct ppc_dyn_relocs
4036 {
4037   struct ppc_dyn_relocs *next;
4038
4039   /* The input section of the reloc.  */
4040   asection *sec;
4041
4042   /* Total number of relocs copied for the input section.  */
4043   unsigned int count : 31;
4044
4045   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
4046   unsigned int ifunc : 1;
4047 };
4048
4049 struct ppc_link_hash_entry
4050 {
4051   struct elf_link_hash_entry elf;
4052
4053   union {
4054     /* A pointer to the most recently used stub hash entry against this
4055        symbol.  */
4056     struct ppc_stub_hash_entry *stub_cache;
4057
4058     /* A pointer to the next symbol starting with a '.'  */
4059     struct ppc_link_hash_entry *next_dot_sym;
4060   } u;
4061
4062   /* Track dynamic relocs copied for this symbol.  */
4063   struct elf_dyn_relocs *dyn_relocs;
4064
4065   /* Link between function code and descriptor symbols.  */
4066   struct ppc_link_hash_entry *oh;
4067
4068   /* Flag function code and descriptor symbols.  */
4069   unsigned int is_func:1;
4070   unsigned int is_func_descriptor:1;
4071   unsigned int fake:1;
4072
4073   /* Whether global opd/toc sym has been adjusted or not.
4074      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4075      should be set for all globals defined in any opd/toc section.  */
4076   unsigned int adjust_done:1;
4077
4078   /* Set if this is an out-of-line register save/restore function,
4079      with non-standard calling convention.  */
4080   unsigned int save_res:1;
4081
4082   /* Set if a duplicate symbol with non-zero localentry is detected,
4083      even when the duplicate symbol does not provide a definition.  */
4084   unsigned int non_zero_localentry:1;
4085
4086   /* Contexts in which symbol is used in the GOT (or TOC).
4087      Bits are or'd into the mask as the corresponding relocs are
4088      encountered during check_relocs, with TLS_TLS being set when any
4089      of the other TLS bits are set.  tls_optimize clears bits when
4090      optimizing to indicate the corresponding GOT entry type is not
4091      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
4092      set TLS_TPRELGD when a GD reloc turns into a TPREL one.  We use a
4093      separate flag rather than setting TPREL just for convenience in
4094      distinguishing the two cases.
4095      These flags are also kept for local symbols.  */
4096 #define TLS_TLS          1      /* Any TLS reloc.  */
4097 #define TLS_GD           2      /* GD reloc. */
4098 #define TLS_LD           4      /* LD reloc. */
4099 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
4100 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
4101 #define TLS_MARK        32      /* __tls_get_addr call marked. */
4102 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
4103 #define TLS_EXPLICIT   128      /* Marks TOC section TLS relocs. */
4104   unsigned char tls_mask;
4105
4106   /* The above field is also used to mark function symbols.  In which
4107      case TLS_TLS will be 0.  */
4108 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
4109 #define PLT_KEEP         4      /* inline plt call requires plt entry.  */
4110 #define NON_GOT        256      /* local symbol plt, not stored.  */
4111 };
4112
4113 /* ppc64 ELF linker hash table.  */
4114
4115 struct ppc_link_hash_table
4116 {
4117   struct elf_link_hash_table elf;
4118
4119   /* The stub hash table.  */
4120   struct bfd_hash_table stub_hash_table;
4121
4122   /* Another hash table for plt_branch stubs.  */
4123   struct bfd_hash_table branch_hash_table;
4124
4125   /* Hash table for function prologue tocsave.  */
4126   htab_t tocsave_htab;
4127
4128   /* Various options and other info passed from the linker.  */
4129   struct ppc64_elf_params *params;
4130
4131   /* The size of sec_info below.  */
4132   unsigned int sec_info_arr_size;
4133
4134   /* Per-section array of extra section info.  Done this way rather
4135      than as part of ppc64_elf_section_data so we have the info for
4136      non-ppc64 sections.  */
4137   struct
4138   {
4139     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4140     bfd_vma toc_off;
4141
4142     union
4143     {
4144       /* The section group that this section belongs to.  */
4145       struct map_stub *group;
4146       /* A temp section list pointer.  */
4147       asection *list;
4148     } u;
4149   } *sec_info;
4150
4151   /* Linked list of groups.  */
4152   struct map_stub *group;
4153
4154   /* Temp used when calculating TOC pointers.  */
4155   bfd_vma toc_curr;
4156   bfd *toc_bfd;
4157   asection *toc_first_sec;
4158
4159   /* Used when adding symbols.  */
4160   struct ppc_link_hash_entry *dot_syms;
4161
4162   /* Shortcuts to get to dynamic linker sections.  */
4163   asection *glink;
4164   asection *global_entry;
4165   asection *sfpr;
4166   asection *pltlocal;
4167   asection *relpltlocal;
4168   asection *brlt;
4169   asection *relbrlt;
4170   asection *glink_eh_frame;
4171
4172   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4173   struct ppc_link_hash_entry *tls_get_addr;
4174   struct ppc_link_hash_entry *tls_get_addr_fd;
4175
4176   /* The size of reliplt used by got entry relocs.  */
4177   bfd_size_type got_reli_size;
4178
4179   /* Statistics.  */
4180   unsigned long stub_count[ppc_stub_global_entry];
4181
4182   /* Number of stubs against global syms.  */
4183   unsigned long stub_globals;
4184
4185   /* Set if we're linking code with function descriptors.  */
4186   unsigned int opd_abi:1;
4187
4188   /* Support for multiple toc sections.  */
4189   unsigned int do_multi_toc:1;
4190   unsigned int multi_toc_needed:1;
4191   unsigned int second_toc_pass:1;
4192   unsigned int do_toc_opt:1;
4193
4194   /* Set if tls optimization is enabled.  */
4195   unsigned int do_tls_opt:1;
4196
4197   /* Set if inline plt calls should be converted to direct calls.  */
4198   unsigned int can_convert_all_inline_plt:1;
4199
4200   /* Set on error.  */
4201   unsigned int stub_error:1;
4202
4203   /* Whether func_desc_adjust needs to be run over symbols.  */
4204   unsigned int need_func_desc_adj:1;
4205
4206   /* Whether there exist local gnu indirect function resolvers,
4207      referenced by dynamic relocations.  */
4208   unsigned int local_ifunc_resolver:1;
4209   unsigned int maybe_local_ifunc_resolver:1;
4210
4211   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
4212   unsigned int has_plt_localentry0:1;
4213
4214   /* Incremented every time we size stubs.  */
4215   unsigned int stub_iteration;
4216
4217   /* Small local sym cache.  */
4218   struct sym_cache sym_cache;
4219 };
4220
4221 /* Rename some of the generic section flags to better document how they
4222    are used here.  */
4223
4224 /* Nonzero if this section has TLS related relocations.  */
4225 #define has_tls_reloc sec_flg0
4226
4227 /* Nonzero if this section has an old-style call to __tls_get_addr.  */
4228 #define has_tls_get_addr_call sec_flg1
4229
4230 /* Nonzero if this section has any toc or got relocs.  */
4231 #define has_toc_reloc sec_flg2
4232
4233 /* Nonzero if this section has a call to another section that uses
4234    the toc or got.  */
4235 #define makes_toc_func_call sec_flg3
4236
4237 /* Recursion protection when determining above flag.  */
4238 #define call_check_in_progress sec_flg4
4239 #define call_check_done sec_flg5
4240
4241 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4242
4243 #define ppc_hash_table(p) \
4244   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4245   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4246
4247 #define ppc_stub_hash_lookup(table, string, create, copy) \
4248   ((struct ppc_stub_hash_entry *) \
4249    bfd_hash_lookup ((table), (string), (create), (copy)))
4250
4251 #define ppc_branch_hash_lookup(table, string, create, copy) \
4252   ((struct ppc_branch_hash_entry *) \
4253    bfd_hash_lookup ((table), (string), (create), (copy)))
4254
4255 /* Create an entry in the stub hash table.  */
4256
4257 static struct bfd_hash_entry *
4258 stub_hash_newfunc (struct bfd_hash_entry *entry,
4259                    struct bfd_hash_table *table,
4260                    const char *string)
4261 {
4262   /* Allocate the structure if it has not already been allocated by a
4263      subclass.  */
4264   if (entry == NULL)
4265     {
4266       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4267       if (entry == NULL)
4268         return entry;
4269     }
4270
4271   /* Call the allocation method of the superclass.  */
4272   entry = bfd_hash_newfunc (entry, table, string);
4273   if (entry != NULL)
4274     {
4275       struct ppc_stub_hash_entry *eh;
4276
4277       /* Initialize the local fields.  */
4278       eh = (struct ppc_stub_hash_entry *) entry;
4279       eh->stub_type = ppc_stub_none;
4280       eh->group = NULL;
4281       eh->stub_offset = 0;
4282       eh->target_value = 0;
4283       eh->target_section = NULL;
4284       eh->h = NULL;
4285       eh->plt_ent = NULL;
4286       eh->other = 0;
4287     }
4288
4289   return entry;
4290 }
4291
4292 /* Create an entry in the branch hash table.  */
4293
4294 static struct bfd_hash_entry *
4295 branch_hash_newfunc (struct bfd_hash_entry *entry,
4296                      struct bfd_hash_table *table,
4297                      const char *string)
4298 {
4299   /* Allocate the structure if it has not already been allocated by a
4300      subclass.  */
4301   if (entry == NULL)
4302     {
4303       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4304       if (entry == NULL)
4305         return entry;
4306     }
4307
4308   /* Call the allocation method of the superclass.  */
4309   entry = bfd_hash_newfunc (entry, table, string);
4310   if (entry != NULL)
4311     {
4312       struct ppc_branch_hash_entry *eh;
4313
4314       /* Initialize the local fields.  */
4315       eh = (struct ppc_branch_hash_entry *) entry;
4316       eh->offset = 0;
4317       eh->iter = 0;
4318     }
4319
4320   return entry;
4321 }
4322
4323 /* Create an entry in a ppc64 ELF linker hash table.  */
4324
4325 static struct bfd_hash_entry *
4326 link_hash_newfunc (struct bfd_hash_entry *entry,
4327                    struct bfd_hash_table *table,
4328                    const char *string)
4329 {
4330   /* Allocate the structure if it has not already been allocated by a
4331      subclass.  */
4332   if (entry == NULL)
4333     {
4334       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4335       if (entry == NULL)
4336         return entry;
4337     }
4338
4339   /* Call the allocation method of the superclass.  */
4340   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4341   if (entry != NULL)
4342     {
4343       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4344
4345       memset (&eh->u.stub_cache, 0,
4346               (sizeof (struct ppc_link_hash_entry)
4347                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4348
4349       /* When making function calls, old ABI code references function entry
4350          points (dot symbols), while new ABI code references the function
4351          descriptor symbol.  We need to make any combination of reference and
4352          definition work together, without breaking archive linking.
4353
4354          For a defined function "foo" and an undefined call to "bar":
4355          An old object defines "foo" and ".foo", references ".bar" (possibly
4356          "bar" too).
4357          A new object defines "foo" and references "bar".
4358
4359          A new object thus has no problem with its undefined symbols being
4360          satisfied by definitions in an old object.  On the other hand, the
4361          old object won't have ".bar" satisfied by a new object.
4362
4363          Keep a list of newly added dot-symbols.  */
4364
4365       if (string[0] == '.')
4366         {
4367           struct ppc_link_hash_table *htab;
4368
4369           htab = (struct ppc_link_hash_table *) table;
4370           eh->u.next_dot_sym = htab->dot_syms;
4371           htab->dot_syms = eh;
4372         }
4373     }
4374
4375   return entry;
4376 }
4377
4378 struct tocsave_entry {
4379   asection *sec;
4380   bfd_vma offset;
4381 };
4382
4383 static hashval_t
4384 tocsave_htab_hash (const void *p)
4385 {
4386   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4387   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4388 }
4389
4390 static int
4391 tocsave_htab_eq (const void *p1, const void *p2)
4392 {
4393   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4394   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4395   return e1->sec == e2->sec && e1->offset == e2->offset;
4396 }
4397
4398 /* Destroy a ppc64 ELF linker hash table.  */
4399
4400 static void
4401 ppc64_elf_link_hash_table_free (bfd *obfd)
4402 {
4403   struct ppc_link_hash_table *htab;
4404
4405   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4406   if (htab->tocsave_htab)
4407     htab_delete (htab->tocsave_htab);
4408   bfd_hash_table_free (&htab->branch_hash_table);
4409   bfd_hash_table_free (&htab->stub_hash_table);
4410   _bfd_elf_link_hash_table_free (obfd);
4411 }
4412
4413 /* Create a ppc64 ELF linker hash table.  */
4414
4415 static struct bfd_link_hash_table *
4416 ppc64_elf_link_hash_table_create (bfd *abfd)
4417 {
4418   struct ppc_link_hash_table *htab;
4419   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4420
4421   htab = bfd_zmalloc (amt);
4422   if (htab == NULL)
4423     return NULL;
4424
4425   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4426                                       sizeof (struct ppc_link_hash_entry),
4427                                       PPC64_ELF_DATA))
4428     {
4429       free (htab);
4430       return NULL;
4431     }
4432
4433   /* Init the stub hash table too.  */
4434   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4435                             sizeof (struct ppc_stub_hash_entry)))
4436     {
4437       _bfd_elf_link_hash_table_free (abfd);
4438       return NULL;
4439     }
4440
4441   /* And the branch hash table.  */
4442   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4443                             sizeof (struct ppc_branch_hash_entry)))
4444     {
4445       bfd_hash_table_free (&htab->stub_hash_table);
4446       _bfd_elf_link_hash_table_free (abfd);
4447       return NULL;
4448     }
4449
4450   htab->tocsave_htab = htab_try_create (1024,
4451                                         tocsave_htab_hash,
4452                                         tocsave_htab_eq,
4453                                         NULL);
4454   if (htab->tocsave_htab == NULL)
4455     {
4456       ppc64_elf_link_hash_table_free (abfd);
4457       return NULL;
4458     }
4459   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4460
4461   /* Initializing two fields of the union is just cosmetic.  We really
4462      only care about glist, but when compiled on a 32-bit host the
4463      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4464      debugger inspection of these fields look nicer.  */
4465   htab->elf.init_got_refcount.refcount = 0;
4466   htab->elf.init_got_refcount.glist = NULL;
4467   htab->elf.init_plt_refcount.refcount = 0;
4468   htab->elf.init_plt_refcount.glist = NULL;
4469   htab->elf.init_got_offset.offset = 0;
4470   htab->elf.init_got_offset.glist = NULL;
4471   htab->elf.init_plt_offset.offset = 0;
4472   htab->elf.init_plt_offset.glist = NULL;
4473
4474   return &htab->elf.root;
4475 }
4476
4477 /* Create sections for linker generated code.  */
4478
4479 static bfd_boolean
4480 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4481 {
4482   struct ppc_link_hash_table *htab;
4483   flagword flags;
4484
4485   htab = ppc_hash_table (info);
4486
4487   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4488            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4489   if (htab->params->save_restore_funcs)
4490     {
4491       /* Create .sfpr for code to save and restore fp regs.  */
4492       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4493                                                        flags);
4494       if (htab->sfpr == NULL
4495           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4496         return FALSE;
4497     }
4498
4499   if (bfd_link_relocatable (info))
4500     return TRUE;
4501
4502   /* Create .glink for lazy dynamic linking support.  */
4503   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4504                                                     flags);
4505   if (htab->glink == NULL
4506       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4507     return FALSE;
4508
4509   /* The part of .glink used by global entry stubs, separate so that
4510      it can be aligned appropriately without affecting htab->glink.  */
4511   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4512                                                            flags);
4513   if (htab->global_entry == NULL
4514       || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4515     return FALSE;
4516
4517   if (!info->no_ld_generated_unwind_info)
4518     {
4519       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4520                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4521       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4522                                                                  ".eh_frame",
4523                                                                  flags);
4524       if (htab->glink_eh_frame == NULL
4525           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4526         return FALSE;
4527     }
4528
4529   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4530   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4531   if (htab->elf.iplt == NULL
4532       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4533     return FALSE;
4534
4535   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4536            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4537   htab->elf.irelplt
4538     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4539   if (htab->elf.irelplt == NULL
4540       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4541     return FALSE;
4542
4543   /* Create branch lookup table for plt_branch stubs.  */
4544   flags = (SEC_ALLOC | SEC_LOAD
4545            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4546   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4547                                                    flags);
4548   if (htab->brlt == NULL
4549       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4550     return FALSE;
4551
4552   /* Local plt entries, put in .branch_lt but a separate section for
4553      convenience.  */
4554   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4555                                                        flags);
4556   if (htab->pltlocal == NULL
4557       || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4558     return FALSE;
4559
4560   if (!bfd_link_pic (info))
4561     return TRUE;
4562
4563   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4564            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4565   htab->relbrlt
4566     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4567   if (htab->relbrlt == NULL
4568       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4569     return FALSE;
4570
4571   htab->relpltlocal
4572     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4573   if (htab->relpltlocal == NULL
4574       || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4575     return FALSE;
4576
4577   return TRUE;
4578 }
4579
4580 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4581
4582 bfd_boolean
4583 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4584                          struct ppc64_elf_params *params)
4585 {
4586   struct ppc_link_hash_table *htab;
4587
4588   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4589
4590 /* Always hook our dynamic sections into the first bfd, which is the
4591    linker created stub bfd.  This ensures that the GOT header is at
4592    the start of the output TOC section.  */
4593   htab = ppc_hash_table (info);
4594   htab->elf.dynobj = params->stub_bfd;
4595   htab->params = params;
4596
4597   return create_linkage_sections (htab->elf.dynobj, info);
4598 }
4599
4600 /* Build a name for an entry in the stub hash table.  */
4601
4602 static char *
4603 ppc_stub_name (const asection *input_section,
4604                const asection *sym_sec,
4605                const struct ppc_link_hash_entry *h,
4606                const Elf_Internal_Rela *rel)
4607 {
4608   char *stub_name;
4609   ssize_t len;
4610
4611   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4612      offsets from a sym as a branch target?  In fact, we could
4613      probably assume the addend is always zero.  */
4614   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4615
4616   if (h)
4617     {
4618       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4619       stub_name = bfd_malloc (len);
4620       if (stub_name == NULL)
4621         return stub_name;
4622
4623       len = sprintf (stub_name, "%08x.%s+%x",
4624                      input_section->id & 0xffffffff,
4625                      h->elf.root.root.string,
4626                      (int) rel->r_addend & 0xffffffff);
4627     }
4628   else
4629     {
4630       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4631       stub_name = bfd_malloc (len);
4632       if (stub_name == NULL)
4633         return stub_name;
4634
4635       len = sprintf (stub_name, "%08x.%x:%x+%x",
4636                      input_section->id & 0xffffffff,
4637                      sym_sec->id & 0xffffffff,
4638                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4639                      (int) rel->r_addend & 0xffffffff);
4640     }
4641   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4642     stub_name[len - 2] = 0;
4643   return stub_name;
4644 }
4645
4646 /* Look up an entry in the stub hash.  Stub entries are cached because
4647    creating the stub name takes a bit of time.  */
4648
4649 static struct ppc_stub_hash_entry *
4650 ppc_get_stub_entry (const asection *input_section,
4651                     const asection *sym_sec,
4652                     struct ppc_link_hash_entry *h,
4653                     const Elf_Internal_Rela *rel,
4654                     struct ppc_link_hash_table *htab)
4655 {
4656   struct ppc_stub_hash_entry *stub_entry;
4657   struct map_stub *group;
4658
4659   /* If this input section is part of a group of sections sharing one
4660      stub section, then use the id of the first section in the group.
4661      Stub names need to include a section id, as there may well be
4662      more than one stub used to reach say, printf, and we need to
4663      distinguish between them.  */
4664   group = htab->sec_info[input_section->id].u.group;
4665   if (group == NULL)
4666     return NULL;
4667
4668   if (h != NULL && h->u.stub_cache != NULL
4669       && h->u.stub_cache->h == h
4670       && h->u.stub_cache->group == group)
4671     {
4672       stub_entry = h->u.stub_cache;
4673     }
4674   else
4675     {
4676       char *stub_name;
4677
4678       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4679       if (stub_name == NULL)
4680         return NULL;
4681
4682       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4683                                          stub_name, FALSE, FALSE);
4684       if (h != NULL)
4685         h->u.stub_cache = stub_entry;
4686
4687       free (stub_name);
4688     }
4689
4690   return stub_entry;
4691 }
4692
4693 /* Add a new stub entry to the stub hash.  Not all fields of the new
4694    stub entry are initialised.  */
4695
4696 static struct ppc_stub_hash_entry *
4697 ppc_add_stub (const char *stub_name,
4698               asection *section,
4699               struct bfd_link_info *info)
4700 {
4701   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4702   struct map_stub *group;
4703   asection *link_sec;
4704   asection *stub_sec;
4705   struct ppc_stub_hash_entry *stub_entry;
4706
4707   group = htab->sec_info[section->id].u.group;
4708   link_sec = group->link_sec;
4709   stub_sec = group->stub_sec;
4710   if (stub_sec == NULL)
4711     {
4712       size_t namelen;
4713       bfd_size_type len;
4714       char *s_name;
4715
4716       namelen = strlen (link_sec->name);
4717       len = namelen + sizeof (STUB_SUFFIX);
4718       s_name = bfd_alloc (htab->params->stub_bfd, len);
4719       if (s_name == NULL)
4720         return NULL;
4721
4722       memcpy (s_name, link_sec->name, namelen);
4723       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4724       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4725       if (stub_sec == NULL)
4726         return NULL;
4727       group->stub_sec = stub_sec;
4728     }
4729
4730   /* Enter this entry into the linker stub hash table.  */
4731   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4732                                      TRUE, FALSE);
4733   if (stub_entry == NULL)
4734     {
4735       /* xgettext:c-format */
4736       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4737                           section->owner, stub_name);
4738       return NULL;
4739     }
4740
4741   stub_entry->group = group;
4742   stub_entry->stub_offset = 0;
4743   return stub_entry;
4744 }
4745
4746 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4747    not already done.  */
4748
4749 static bfd_boolean
4750 create_got_section (bfd *abfd, struct bfd_link_info *info)
4751 {
4752   asection *got, *relgot;
4753   flagword flags;
4754   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4755
4756   if (!is_ppc64_elf (abfd))
4757     return FALSE;
4758   if (htab == NULL)
4759     return FALSE;
4760
4761   if (!htab->elf.sgot
4762       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4763     return FALSE;
4764
4765   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4766            | SEC_LINKER_CREATED);
4767
4768   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4769   if (!got
4770       || !bfd_set_section_alignment (abfd, got, 3))
4771     return FALSE;
4772
4773   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4774                                                flags | SEC_READONLY);
4775   if (!relgot
4776       || ! bfd_set_section_alignment (abfd, relgot, 3))
4777     return FALSE;
4778
4779   ppc64_elf_tdata (abfd)->got = got;
4780   ppc64_elf_tdata (abfd)->relgot = relgot;
4781   return TRUE;
4782 }
4783
4784 /* Follow indirect and warning symbol links.  */
4785
4786 static inline struct bfd_link_hash_entry *
4787 follow_link (struct bfd_link_hash_entry *h)
4788 {
4789   while (h->type == bfd_link_hash_indirect
4790          || h->type == bfd_link_hash_warning)
4791     h = h->u.i.link;
4792   return h;
4793 }
4794
4795 static inline struct elf_link_hash_entry *
4796 elf_follow_link (struct elf_link_hash_entry *h)
4797 {
4798   return (struct elf_link_hash_entry *) follow_link (&h->root);
4799 }
4800
4801 static inline struct ppc_link_hash_entry *
4802 ppc_follow_link (struct ppc_link_hash_entry *h)
4803 {
4804   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4805 }
4806
4807 /* Merge PLT info on FROM with that on TO.  */
4808
4809 static void
4810 move_plt_plist (struct ppc_link_hash_entry *from,
4811                 struct ppc_link_hash_entry *to)
4812 {
4813   if (from->elf.plt.plist != NULL)
4814     {
4815       if (to->elf.plt.plist != NULL)
4816         {
4817           struct plt_entry **entp;
4818           struct plt_entry *ent;
4819
4820           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4821             {
4822               struct plt_entry *dent;
4823
4824               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4825                 if (dent->addend == ent->addend)
4826                   {
4827                     dent->plt.refcount += ent->plt.refcount;
4828                     *entp = ent->next;
4829                     break;
4830                   }
4831               if (dent == NULL)
4832                 entp = &ent->next;
4833             }
4834           *entp = to->elf.plt.plist;
4835         }
4836
4837       to->elf.plt.plist = from->elf.plt.plist;
4838       from->elf.plt.plist = NULL;
4839     }
4840 }
4841
4842 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4843
4844 static void
4845 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4846                                 struct elf_link_hash_entry *dir,
4847                                 struct elf_link_hash_entry *ind)
4848 {
4849   struct ppc_link_hash_entry *edir, *eind;
4850
4851   edir = (struct ppc_link_hash_entry *) dir;
4852   eind = (struct ppc_link_hash_entry *) ind;
4853
4854   edir->is_func |= eind->is_func;
4855   edir->is_func_descriptor |= eind->is_func_descriptor;
4856   edir->tls_mask |= eind->tls_mask;
4857   if (eind->oh != NULL)
4858     edir->oh = ppc_follow_link (eind->oh);
4859
4860   if (edir->elf.versioned != versioned_hidden)
4861     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4862   edir->elf.ref_regular |= eind->elf.ref_regular;
4863   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4864   edir->elf.non_got_ref |= eind->elf.non_got_ref;
4865   edir->elf.needs_plt |= eind->elf.needs_plt;
4866   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4867
4868   /* If we were called to copy over info for a weak sym, don't copy
4869      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4870      in order to simplify readonly_dynrelocs and save a field in the
4871      symbol hash entry, but that means dyn_relocs can't be used in any
4872      tests about a specific symbol, or affect other symbol flags which
4873      are then tested.  */
4874   if (eind->elf.root.type != bfd_link_hash_indirect)
4875     return;
4876
4877   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4878   if (eind->dyn_relocs != NULL)
4879     {
4880       if (edir->dyn_relocs != NULL)
4881         {
4882           struct elf_dyn_relocs **pp;
4883           struct elf_dyn_relocs *p;
4884
4885           /* Add reloc counts against the indirect sym to the direct sym
4886              list.  Merge any entries against the same section.  */
4887           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4888             {
4889               struct elf_dyn_relocs *q;
4890
4891               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4892                 if (q->sec == p->sec)
4893                   {
4894                     q->pc_count += p->pc_count;
4895                     q->count += p->count;
4896                     *pp = p->next;
4897                     break;
4898                   }
4899               if (q == NULL)
4900                 pp = &p->next;
4901             }
4902           *pp = edir->dyn_relocs;
4903         }
4904
4905       edir->dyn_relocs = eind->dyn_relocs;
4906       eind->dyn_relocs = NULL;
4907     }
4908
4909   /* Copy over got entries that we may have already seen to the
4910      symbol which just became indirect.  */
4911   if (eind->elf.got.glist != NULL)
4912     {
4913       if (edir->elf.got.glist != NULL)
4914         {
4915           struct got_entry **entp;
4916           struct got_entry *ent;
4917
4918           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4919             {
4920               struct got_entry *dent;
4921
4922               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4923                 if (dent->addend == ent->addend
4924                     && dent->owner == ent->owner
4925                     && dent->tls_type == ent->tls_type)
4926                   {
4927                     dent->got.refcount += ent->got.refcount;
4928                     *entp = ent->next;
4929                     break;
4930                   }
4931               if (dent == NULL)
4932                 entp = &ent->next;
4933             }
4934           *entp = edir->elf.got.glist;
4935         }
4936
4937       edir->elf.got.glist = eind->elf.got.glist;
4938       eind->elf.got.glist = NULL;
4939     }
4940
4941   /* And plt entries.  */
4942   move_plt_plist (eind, edir);
4943
4944   if (eind->elf.dynindx != -1)
4945     {
4946       if (edir->elf.dynindx != -1)
4947         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4948                                 edir->elf.dynstr_index);
4949       edir->elf.dynindx = eind->elf.dynindx;
4950       edir->elf.dynstr_index = eind->elf.dynstr_index;
4951       eind->elf.dynindx = -1;
4952       eind->elf.dynstr_index = 0;
4953     }
4954 }
4955
4956 /* Find the function descriptor hash entry from the given function code
4957    hash entry FH.  Link the entries via their OH fields.  */
4958
4959 static struct ppc_link_hash_entry *
4960 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4961 {
4962   struct ppc_link_hash_entry *fdh = fh->oh;
4963
4964   if (fdh == NULL)
4965     {
4966       const char *fd_name = fh->elf.root.root.string + 1;
4967
4968       fdh = (struct ppc_link_hash_entry *)
4969         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4970       if (fdh == NULL)
4971         return fdh;
4972
4973       fdh->is_func_descriptor = 1;
4974       fdh->oh = fh;
4975       fh->is_func = 1;
4976       fh->oh = fdh;
4977     }
4978
4979   fdh = ppc_follow_link (fdh);
4980   fdh->is_func_descriptor = 1;
4981   fdh->oh = fh;
4982   return fdh;
4983 }
4984
4985 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4986
4987 static struct ppc_link_hash_entry *
4988 make_fdh (struct bfd_link_info *info,
4989           struct ppc_link_hash_entry *fh)
4990 {
4991   bfd *abfd = fh->elf.root.u.undef.abfd;
4992   struct bfd_link_hash_entry *bh = NULL;
4993   struct ppc_link_hash_entry *fdh;
4994   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4995                     ? BSF_WEAK
4996                     : BSF_GLOBAL);
4997
4998   if (!_bfd_generic_link_add_one_symbol (info, abfd,
4999                                          fh->elf.root.root.string + 1,
5000                                          flags, bfd_und_section_ptr, 0,
5001                                          NULL, FALSE, FALSE, &bh))
5002     return NULL;
5003
5004   fdh = (struct ppc_link_hash_entry *) bh;
5005   fdh->elf.non_elf = 0;
5006   fdh->fake = 1;
5007   fdh->is_func_descriptor = 1;
5008   fdh->oh = fh;
5009   fh->is_func = 1;
5010   fh->oh = fdh;
5011   return fdh;
5012 }
5013
5014 /* Fix function descriptor symbols defined in .opd sections to be
5015    function type.  */
5016
5017 static bfd_boolean
5018 ppc64_elf_add_symbol_hook (bfd *ibfd,
5019                            struct bfd_link_info *info,
5020                            Elf_Internal_Sym *isym,
5021                            const char **name,
5022                            flagword *flags ATTRIBUTE_UNUSED,
5023                            asection **sec,
5024                            bfd_vma *value)
5025 {
5026   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5027       && (ibfd->flags & DYNAMIC) == 0
5028       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
5029     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
5030
5031   if (*sec != NULL
5032       && strcmp ((*sec)->name, ".opd") == 0)
5033     {
5034       asection *code_sec;
5035
5036       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5037             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5038         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5039
5040       /* If the symbol is a function defined in .opd, and the function
5041          code is in a discarded group, let it appear to be undefined.  */
5042       if (!bfd_link_relocatable (info)
5043           && (*sec)->reloc_count != 0
5044           && opd_entry_value (*sec, *value, &code_sec, NULL,
5045                               FALSE) != (bfd_vma) -1
5046           && discarded_section (code_sec))
5047         {
5048           *sec = bfd_und_section_ptr;
5049           isym->st_shndx = SHN_UNDEF;
5050         }
5051     }
5052   else if (*sec != NULL
5053            && strcmp ((*sec)->name, ".toc") == 0
5054            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5055     {
5056       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5057       if (htab != NULL)
5058         htab->params->object_in_toc = 1;
5059     }
5060
5061   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5062     {
5063       if (abiversion (ibfd) == 0)
5064         set_abiversion (ibfd, 2);
5065       else if (abiversion (ibfd) == 1)
5066         {
5067           _bfd_error_handler (_("symbol '%s' has invalid st_other"
5068                                 " for ABI version 1"), *name);
5069           bfd_set_error (bfd_error_bad_value);
5070           return FALSE;
5071         }
5072     }
5073
5074   return TRUE;
5075 }
5076
5077 /* Merge non-visibility st_other attributes: local entry point.  */
5078
5079 static void
5080 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5081                                   const Elf_Internal_Sym *isym,
5082                                   bfd_boolean definition,
5083                                   bfd_boolean dynamic)
5084 {
5085   if (definition && (!dynamic || !h->def_regular))
5086     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5087                 | ELF_ST_VISIBILITY (h->other));
5088 }
5089
5090 /* Hook called on merging a symbol.  We use this to clear "fake" since
5091    we now have a real symbol.  */
5092
5093 static bfd_boolean
5094 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5095                         const Elf_Internal_Sym *isym,
5096                         asection **psec ATTRIBUTE_UNUSED,
5097                         bfd_boolean newdef ATTRIBUTE_UNUSED,
5098                         bfd_boolean olddef ATTRIBUTE_UNUSED,
5099                         bfd *oldbfd ATTRIBUTE_UNUSED,
5100                         const asection *oldsec ATTRIBUTE_UNUSED)
5101 {
5102   ((struct ppc_link_hash_entry *) h)->fake = 0;
5103   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5104     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5105   return TRUE;
5106 }
5107
5108 /* This function makes an old ABI object reference to ".bar" cause the
5109    inclusion of a new ABI object archive that defines "bar".
5110    NAME is a symbol defined in an archive.  Return a symbol in the hash
5111    table that might be satisfied by the archive symbols.  */
5112
5113 static struct elf_link_hash_entry *
5114 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5115                                  struct bfd_link_info *info,
5116                                  const char *name)
5117 {
5118   struct elf_link_hash_entry *h;
5119   char *dot_name;
5120   size_t len;
5121
5122   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5123   if (h != NULL
5124       /* Don't return this sym if it is a fake function descriptor
5125          created by add_symbol_adjust.  */
5126       && !((struct ppc_link_hash_entry *) h)->fake)
5127     return h;
5128
5129   if (name[0] == '.')
5130     return h;
5131
5132   len = strlen (name);
5133   dot_name = bfd_alloc (abfd, len + 2);
5134   if (dot_name == NULL)
5135     return (struct elf_link_hash_entry *) -1;
5136   dot_name[0] = '.';
5137   memcpy (dot_name + 1, name, len + 1);
5138   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5139   bfd_release (abfd, dot_name);
5140   return h;
5141 }
5142
5143 /* This function satisfies all old ABI object references to ".bar" if a
5144    new ABI object defines "bar".  Well, at least, undefined dot symbols
5145    are made weak.  This stops later archive searches from including an
5146    object if we already have a function descriptor definition.  It also
5147    prevents the linker complaining about undefined symbols.
5148    We also check and correct mismatched symbol visibility here.  The
5149    most restrictive visibility of the function descriptor and the
5150    function entry symbol is used.  */
5151
5152 static bfd_boolean
5153 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5154 {
5155   struct ppc_link_hash_table *htab;
5156   struct ppc_link_hash_entry *fdh;
5157
5158   if (eh->elf.root.type == bfd_link_hash_warning)
5159     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5160
5161   if (eh->elf.root.type == bfd_link_hash_indirect)
5162     return TRUE;
5163
5164   if (eh->elf.root.root.string[0] != '.')
5165     abort ();
5166
5167   htab = ppc_hash_table (info);
5168   if (htab == NULL)
5169     return FALSE;
5170
5171   fdh = lookup_fdh (eh, htab);
5172   if (fdh == NULL
5173       && !bfd_link_relocatable (info)
5174       && (eh->elf.root.type == bfd_link_hash_undefined
5175           || eh->elf.root.type == bfd_link_hash_undefweak)
5176       && eh->elf.ref_regular)
5177     {
5178       /* Make an undefined function descriptor sym, in order to
5179          pull in an --as-needed shared lib.  Archives are handled
5180          elsewhere.  */
5181       fdh = make_fdh (info, eh);
5182       if (fdh == NULL)
5183         return FALSE;
5184     }
5185
5186   if (fdh != NULL)
5187     {
5188       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5189       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5190
5191       /* Make both descriptor and entry symbol have the most
5192          constraining visibility of either symbol.  */
5193       if (entry_vis < descr_vis)
5194         fdh->elf.other += entry_vis - descr_vis;
5195       else if (entry_vis > descr_vis)
5196         eh->elf.other += descr_vis - entry_vis;
5197
5198       /* Propagate reference flags from entry symbol to function
5199          descriptor symbol.  */
5200       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5201       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5202       fdh->elf.ref_regular |= eh->elf.ref_regular;
5203       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5204
5205       if (!fdh->elf.forced_local
5206           && fdh->elf.dynindx == -1
5207           && fdh->elf.versioned != versioned_hidden
5208           && (bfd_link_dll (info)
5209               || fdh->elf.def_dynamic
5210               || fdh->elf.ref_dynamic)
5211           && (eh->elf.ref_regular
5212               || eh->elf.def_regular))
5213         {
5214           if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5215             return FALSE;
5216         }
5217     }
5218
5219   return TRUE;
5220 }
5221
5222 /* Set up opd section info and abiversion for IBFD, and process list
5223    of dot-symbols we made in link_hash_newfunc.  */
5224
5225 static bfd_boolean
5226 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5227 {
5228   struct ppc_link_hash_table *htab;
5229   struct ppc_link_hash_entry **p, *eh;
5230   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5231
5232   if (opd != NULL && opd->size != 0)
5233     {
5234       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5235       ppc64_elf_section_data (opd)->sec_type = sec_opd;
5236
5237       if (abiversion (ibfd) == 0)
5238         set_abiversion (ibfd, 1);
5239       else if (abiversion (ibfd) >= 2)
5240         {
5241           /* xgettext:c-format */
5242           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5243                               ibfd, abiversion (ibfd));
5244           bfd_set_error (bfd_error_bad_value);
5245           return FALSE;
5246         }
5247     }
5248
5249   if (is_ppc64_elf (info->output_bfd))
5250     {
5251       /* For input files without an explicit abiversion in e_flags
5252          we should have flagged any with symbol st_other bits set
5253          as ELFv1 and above flagged those with .opd as ELFv2.
5254          Set the output abiversion if not yet set, and for any input
5255          still ambiguous, take its abiversion from the output.
5256          Differences in ABI are reported later.  */
5257       if (abiversion (info->output_bfd) == 0)
5258         set_abiversion (info->output_bfd, abiversion (ibfd));
5259       else if (abiversion (ibfd) == 0)
5260         set_abiversion (ibfd, abiversion (info->output_bfd));
5261     }
5262
5263   htab = ppc_hash_table (info);
5264   if (htab == NULL)
5265     return TRUE;
5266
5267   if (opd != NULL && opd->size != 0
5268       && (ibfd->flags & DYNAMIC) == 0
5269       && (opd->flags & SEC_RELOC) != 0
5270       && opd->reloc_count != 0
5271       && !bfd_is_abs_section (opd->output_section)
5272       && info->gc_sections)
5273     {
5274       /* Garbage collection needs some extra help with .opd sections.
5275          We don't want to necessarily keep everything referenced by
5276          relocs in .opd, as that would keep all functions.  Instead,
5277          if we reference an .opd symbol (a function descriptor), we
5278          want to keep the function code symbol's section.  This is
5279          easy for global symbols, but for local syms we need to keep
5280          information about the associated function section.  */
5281       bfd_size_type amt;
5282       asection **opd_sym_map;
5283       Elf_Internal_Shdr *symtab_hdr;
5284       Elf_Internal_Rela *relocs, *rel_end, *rel;
5285
5286       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5287       opd_sym_map = bfd_zalloc (ibfd, amt);
5288       if (opd_sym_map == NULL)
5289         return FALSE;
5290       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5291       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5292                                           info->keep_memory);
5293       if (relocs == NULL)
5294         return FALSE;
5295       symtab_hdr = &elf_symtab_hdr (ibfd);
5296       rel_end = relocs + opd->reloc_count - 1;
5297       for (rel = relocs; rel < rel_end; rel++)
5298         {
5299           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5300           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5301
5302           if (r_type == R_PPC64_ADDR64
5303               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5304               && r_symndx < symtab_hdr->sh_info)
5305             {
5306               Elf_Internal_Sym *isym;
5307               asection *s;
5308
5309               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5310               if (isym == NULL)
5311                 {
5312                   if (elf_section_data (opd)->relocs != relocs)
5313                     free (relocs);
5314                   return FALSE;
5315                 }
5316
5317               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5318               if (s != NULL && s != opd)
5319                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5320             }
5321         }
5322       if (elf_section_data (opd)->relocs != relocs)
5323         free (relocs);
5324     }
5325
5326   p = &htab->dot_syms;
5327   while ((eh = *p) != NULL)
5328     {
5329       *p = NULL;
5330       if (&eh->elf == htab->elf.hgot)
5331         ;
5332       else if (htab->elf.hgot == NULL
5333                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5334         htab->elf.hgot = &eh->elf;
5335       else if (abiversion (ibfd) <= 1)
5336         {
5337           htab->need_func_desc_adj = 1;
5338           if (!add_symbol_adjust (eh, info))
5339             return FALSE;
5340         }
5341       p = &eh->u.next_dot_sym;
5342     }
5343   return TRUE;
5344 }
5345
5346 /* Undo hash table changes when an --as-needed input file is determined
5347    not to be needed.  */
5348
5349 static bfd_boolean
5350 ppc64_elf_notice_as_needed (bfd *ibfd,
5351                             struct bfd_link_info *info,
5352                             enum notice_asneeded_action act)
5353 {
5354   if (act == notice_not_needed)
5355     {
5356       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5357
5358       if (htab == NULL)
5359         return FALSE;
5360
5361       htab->dot_syms = NULL;
5362     }
5363   return _bfd_elf_notice_as_needed (ibfd, info, act);
5364 }
5365
5366 /* If --just-symbols against a final linked binary, then assume we need
5367    toc adjusting stubs when calling functions defined there.  */
5368
5369 static void
5370 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5371 {
5372   if ((sec->flags & SEC_CODE) != 0
5373       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5374       && is_ppc64_elf (sec->owner))
5375     {
5376       if (abiversion (sec->owner) >= 2
5377           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5378         sec->has_toc_reloc = 1;
5379     }
5380   _bfd_elf_link_just_syms (sec, info);
5381 }
5382
5383 static struct plt_entry **
5384 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5385                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5386 {
5387   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5388   struct plt_entry **local_plt;
5389   unsigned char *local_got_tls_masks;
5390
5391   if (local_got_ents == NULL)
5392     {
5393       bfd_size_type size = symtab_hdr->sh_info;
5394
5395       size *= (sizeof (*local_got_ents)
5396                + sizeof (*local_plt)
5397                + sizeof (*local_got_tls_masks));
5398       local_got_ents = bfd_zalloc (abfd, size);
5399       if (local_got_ents == NULL)
5400         return NULL;
5401       elf_local_got_ents (abfd) = local_got_ents;
5402     }
5403
5404   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5405     {
5406       struct got_entry *ent;
5407
5408       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5409         if (ent->addend == r_addend
5410             && ent->owner == abfd
5411             && ent->tls_type == tls_type)
5412           break;
5413       if (ent == NULL)
5414         {
5415           bfd_size_type amt = sizeof (*ent);
5416           ent = bfd_alloc (abfd, amt);
5417           if (ent == NULL)
5418             return FALSE;
5419           ent->next = local_got_ents[r_symndx];
5420           ent->addend = r_addend;
5421           ent->owner = abfd;
5422           ent->tls_type = tls_type;
5423           ent->is_indirect = FALSE;
5424           ent->got.refcount = 0;
5425           local_got_ents[r_symndx] = ent;
5426         }
5427       ent->got.refcount += 1;
5428     }
5429
5430   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5431   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5432   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5433
5434   return local_plt + r_symndx;
5435 }
5436
5437 static bfd_boolean
5438 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5439 {
5440   struct plt_entry *ent;
5441
5442   for (ent = *plist; ent != NULL; ent = ent->next)
5443     if (ent->addend == addend)
5444       break;
5445   if (ent == NULL)
5446     {
5447       bfd_size_type amt = sizeof (*ent);
5448       ent = bfd_alloc (abfd, amt);
5449       if (ent == NULL)
5450         return FALSE;
5451       ent->next = *plist;
5452       ent->addend = addend;
5453       ent->plt.refcount = 0;
5454       *plist = ent;
5455     }
5456   ent->plt.refcount += 1;
5457   return TRUE;
5458 }
5459
5460 static bfd_boolean
5461 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5462 {
5463   return (r_type == R_PPC64_REL24
5464           || r_type == R_PPC64_REL14
5465           || r_type == R_PPC64_REL14_BRTAKEN
5466           || r_type == R_PPC64_REL14_BRNTAKEN
5467           || r_type == R_PPC64_ADDR24
5468           || r_type == R_PPC64_ADDR14
5469           || r_type == R_PPC64_ADDR14_BRTAKEN
5470           || r_type == R_PPC64_ADDR14_BRNTAKEN
5471           || r_type == R_PPC64_PLTCALL);
5472 }
5473
5474 /* Relocs on inline plt call sequence insns prior to the call.  */
5475
5476 static bfd_boolean
5477 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5478 {
5479   return (r_type == R_PPC64_PLT16_HA
5480           || r_type == R_PPC64_PLT16_HI
5481           || r_type == R_PPC64_PLT16_LO
5482           || r_type == R_PPC64_PLT16_LO_DS
5483           || r_type == R_PPC64_PLTSEQ);
5484 }
5485
5486 /* Look through the relocs for a section during the first phase, and
5487    calculate needed space in the global offset table, procedure
5488    linkage table, and dynamic reloc sections.  */
5489
5490 static bfd_boolean
5491 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5492                         asection *sec, const Elf_Internal_Rela *relocs)
5493 {
5494   struct ppc_link_hash_table *htab;
5495   Elf_Internal_Shdr *symtab_hdr;
5496   struct elf_link_hash_entry **sym_hashes;
5497   const Elf_Internal_Rela *rel;
5498   const Elf_Internal_Rela *rel_end;
5499   asection *sreloc;
5500   struct elf_link_hash_entry *tga, *dottga;
5501   bfd_boolean is_opd;
5502
5503   if (bfd_link_relocatable (info))
5504     return TRUE;
5505
5506   /* Don't do anything special with non-loaded, non-alloced sections.
5507      In particular, any relocs in such sections should not affect GOT
5508      and PLT reference counting (ie. we don't allow them to create GOT
5509      or PLT entries), there's no possibility or desire to optimize TLS
5510      relocs, and there's not much point in propagating relocs to shared
5511      libs that the dynamic linker won't relocate.  */
5512   if ((sec->flags & SEC_ALLOC) == 0)
5513     return TRUE;
5514
5515   BFD_ASSERT (is_ppc64_elf (abfd));
5516
5517   htab = ppc_hash_table (info);
5518   if (htab == NULL)
5519     return FALSE;
5520
5521   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5522                               FALSE, FALSE, TRUE);
5523   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5524                                  FALSE, FALSE, TRUE);
5525   symtab_hdr = &elf_symtab_hdr (abfd);
5526   sym_hashes = elf_sym_hashes (abfd);
5527   sreloc = NULL;
5528   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5529   rel_end = relocs + sec->reloc_count;
5530   for (rel = relocs; rel < rel_end; rel++)
5531     {
5532       unsigned long r_symndx;
5533       struct elf_link_hash_entry *h;
5534       enum elf_ppc64_reloc_type r_type;
5535       int tls_type;
5536       struct _ppc64_elf_section_data *ppc64_sec;
5537       struct plt_entry **ifunc, **plt_list;
5538
5539       r_symndx = ELF64_R_SYM (rel->r_info);
5540       if (r_symndx < symtab_hdr->sh_info)
5541         h = NULL;
5542       else
5543         {
5544           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5545           h = elf_follow_link (h);
5546
5547           if (h == htab->elf.hgot)
5548             sec->has_toc_reloc = 1;
5549         }
5550
5551       tls_type = 0;
5552       ifunc = NULL;
5553       if (h != NULL)
5554         {
5555           if (h->type == STT_GNU_IFUNC)
5556             {
5557               h->needs_plt = 1;
5558               ifunc = &h->plt.plist;
5559             }
5560         }
5561       else
5562         {
5563           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5564                                                           abfd, r_symndx);
5565           if (isym == NULL)
5566             return FALSE;
5567
5568           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5569             {
5570               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5571                                              rel->r_addend,
5572                                              NON_GOT | PLT_IFUNC);
5573               if (ifunc == NULL)
5574                 return FALSE;
5575             }
5576         }
5577
5578       r_type = ELF64_R_TYPE (rel->r_info);
5579       switch (r_type)
5580         {
5581         case R_PPC64_TLSGD:
5582         case R_PPC64_TLSLD:
5583           /* These special tls relocs tie a call to __tls_get_addr with
5584              its parameter symbol.  */
5585           if (h != NULL)
5586             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5587           else
5588             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5589                                         rel->r_addend,
5590                                         NON_GOT | TLS_TLS | TLS_MARK))
5591               return FALSE;
5592           sec->has_tls_reloc = 1;
5593           break;
5594
5595         case R_PPC64_GOT_TLSLD16:
5596         case R_PPC64_GOT_TLSLD16_LO:
5597         case R_PPC64_GOT_TLSLD16_HI:
5598         case R_PPC64_GOT_TLSLD16_HA:
5599           tls_type = TLS_TLS | TLS_LD;
5600           goto dogottls;
5601
5602         case R_PPC64_GOT_TLSGD16:
5603         case R_PPC64_GOT_TLSGD16_LO:
5604         case R_PPC64_GOT_TLSGD16_HI:
5605         case R_PPC64_GOT_TLSGD16_HA:
5606           tls_type = TLS_TLS | TLS_GD;
5607           goto dogottls;
5608
5609         case R_PPC64_GOT_TPREL16_DS:
5610         case R_PPC64_GOT_TPREL16_LO_DS:
5611         case R_PPC64_GOT_TPREL16_HI:
5612         case R_PPC64_GOT_TPREL16_HA:
5613           if (bfd_link_dll (info))
5614             info->flags |= DF_STATIC_TLS;
5615           tls_type = TLS_TLS | TLS_TPREL;
5616           goto dogottls;
5617
5618         case R_PPC64_GOT_DTPREL16_DS:
5619         case R_PPC64_GOT_DTPREL16_LO_DS:
5620         case R_PPC64_GOT_DTPREL16_HI:
5621         case R_PPC64_GOT_DTPREL16_HA:
5622           tls_type = TLS_TLS | TLS_DTPREL;
5623         dogottls:
5624           sec->has_tls_reloc = 1;
5625           /* Fall through */
5626
5627         case R_PPC64_GOT16:
5628         case R_PPC64_GOT16_DS:
5629         case R_PPC64_GOT16_HA:
5630         case R_PPC64_GOT16_HI:
5631         case R_PPC64_GOT16_LO:
5632         case R_PPC64_GOT16_LO_DS:
5633           /* This symbol requires a global offset table entry.  */
5634           sec->has_toc_reloc = 1;
5635           if (r_type == R_PPC64_GOT_TLSLD16
5636               || r_type == R_PPC64_GOT_TLSGD16
5637               || r_type == R_PPC64_GOT_TPREL16_DS
5638               || r_type == R_PPC64_GOT_DTPREL16_DS
5639               || r_type == R_PPC64_GOT16
5640               || r_type == R_PPC64_GOT16_DS)
5641             {
5642               htab->do_multi_toc = 1;
5643               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5644             }
5645
5646           if (ppc64_elf_tdata (abfd)->got == NULL
5647               && !create_got_section (abfd, info))
5648             return FALSE;
5649
5650           if (h != NULL)
5651             {
5652               struct ppc_link_hash_entry *eh;
5653               struct got_entry *ent;
5654
5655               eh = (struct ppc_link_hash_entry *) h;
5656               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5657                 if (ent->addend == rel->r_addend
5658                     && ent->owner == abfd
5659                     && ent->tls_type == tls_type)
5660                   break;
5661               if (ent == NULL)
5662                 {
5663                   bfd_size_type amt = sizeof (*ent);
5664                   ent = bfd_alloc (abfd, amt);
5665                   if (ent == NULL)
5666                     return FALSE;
5667                   ent->next = eh->elf.got.glist;
5668                   ent->addend = rel->r_addend;
5669                   ent->owner = abfd;
5670                   ent->tls_type = tls_type;
5671                   ent->is_indirect = FALSE;
5672                   ent->got.refcount = 0;
5673                   eh->elf.got.glist = ent;
5674                 }
5675               ent->got.refcount += 1;
5676               eh->tls_mask |= tls_type;
5677             }
5678           else
5679             /* This is a global offset table entry for a local symbol.  */
5680             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5681                                         rel->r_addend, tls_type))
5682               return FALSE;
5683
5684           /* We may also need a plt entry if the symbol turns out to be
5685              an ifunc.  */
5686           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5687             {
5688               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5689                 return FALSE;
5690             }
5691           break;
5692
5693         case R_PPC64_PLT16_HA:
5694         case R_PPC64_PLT16_HI:
5695         case R_PPC64_PLT16_LO:
5696         case R_PPC64_PLT16_LO_DS:
5697         case R_PPC64_PLT32:
5698         case R_PPC64_PLT64:
5699           /* This symbol requires a procedure linkage table entry.  */
5700           plt_list = ifunc;
5701           if (h != NULL)
5702             {
5703               h->needs_plt = 1;
5704               if (h->root.root.string[0] == '.'
5705                   && h->root.root.string[1] != '\0')
5706                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5707               ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5708               plt_list = &h->plt.plist;
5709             }
5710           if (plt_list == NULL)
5711             plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5712                                               rel->r_addend,
5713                                               NON_GOT | PLT_KEEP);
5714           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5715             return FALSE;
5716           break;
5717
5718           /* The following relocations don't need to propagate the
5719              relocation if linking a shared object since they are
5720              section relative.  */
5721         case R_PPC64_SECTOFF:
5722         case R_PPC64_SECTOFF_LO:
5723         case R_PPC64_SECTOFF_HI:
5724         case R_PPC64_SECTOFF_HA:
5725         case R_PPC64_SECTOFF_DS:
5726         case R_PPC64_SECTOFF_LO_DS:
5727         case R_PPC64_DTPREL16:
5728         case R_PPC64_DTPREL16_LO:
5729         case R_PPC64_DTPREL16_HI:
5730         case R_PPC64_DTPREL16_HA:
5731         case R_PPC64_DTPREL16_DS:
5732         case R_PPC64_DTPREL16_LO_DS:
5733         case R_PPC64_DTPREL16_HIGH:
5734         case R_PPC64_DTPREL16_HIGHA:
5735         case R_PPC64_DTPREL16_HIGHER:
5736         case R_PPC64_DTPREL16_HIGHERA:
5737         case R_PPC64_DTPREL16_HIGHEST:
5738         case R_PPC64_DTPREL16_HIGHESTA:
5739           break;
5740
5741           /* Nor do these.  */
5742         case R_PPC64_REL16:
5743         case R_PPC64_REL16_LO:
5744         case R_PPC64_REL16_HI:
5745         case R_PPC64_REL16_HA:
5746         case R_PPC64_REL16DX_HA:
5747           break;
5748
5749           /* Not supported as a dynamic relocation.  */
5750         case R_PPC64_ADDR64_LOCAL:
5751           if (bfd_link_pic (info))
5752             {
5753               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5754                 ppc_howto_init ();
5755               /* xgettext:c-format */
5756               info->callbacks->einfo (_("%H: %s reloc unsupported "
5757                                         "in shared libraries and PIEs\n"),
5758                                       abfd, sec, rel->r_offset,
5759                                       ppc64_elf_howto_table[r_type]->name);
5760               bfd_set_error (bfd_error_bad_value);
5761               return FALSE;
5762             }
5763           break;
5764
5765         case R_PPC64_TOC16:
5766         case R_PPC64_TOC16_DS:
5767           htab->do_multi_toc = 1;
5768           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5769           /* Fall through.  */
5770         case R_PPC64_TOC16_LO:
5771         case R_PPC64_TOC16_HI:
5772         case R_PPC64_TOC16_HA:
5773         case R_PPC64_TOC16_LO_DS:
5774           sec->has_toc_reloc = 1;
5775           break;
5776
5777           /* Marker reloc.  */
5778         case R_PPC64_ENTRY:
5779           break;
5780
5781           /* This relocation describes the C++ object vtable hierarchy.
5782              Reconstruct it for later use during GC.  */
5783         case R_PPC64_GNU_VTINHERIT:
5784           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5785             return FALSE;
5786           break;
5787
5788           /* This relocation describes which C++ vtable entries are actually
5789              used.  Record for later use during GC.  */
5790         case R_PPC64_GNU_VTENTRY:
5791           BFD_ASSERT (h != NULL);
5792           if (h != NULL
5793               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5794             return FALSE;
5795           break;
5796
5797         case R_PPC64_REL14:
5798         case R_PPC64_REL14_BRTAKEN:
5799         case R_PPC64_REL14_BRNTAKEN:
5800           {
5801             asection *dest = NULL;
5802
5803             /* Heuristic: If jumping outside our section, chances are
5804                we are going to need a stub.  */
5805             if (h != NULL)
5806               {
5807                 /* If the sym is weak it may be overridden later, so
5808                    don't assume we know where a weak sym lives.  */
5809                 if (h->root.type == bfd_link_hash_defined)
5810                   dest = h->root.u.def.section;
5811               }
5812             else
5813               {
5814                 Elf_Internal_Sym *isym;
5815
5816                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5817                                               abfd, r_symndx);
5818                 if (isym == NULL)
5819                   return FALSE;
5820
5821                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5822               }
5823
5824             if (dest != sec)
5825               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5826           }
5827           goto rel24;
5828
5829         case R_PPC64_PLTCALL:
5830           ppc64_elf_section_data (sec)->has_pltcall = 1;
5831           /* Fall through.  */
5832
5833         case R_PPC64_REL24:
5834         rel24:
5835           plt_list = ifunc;
5836           if (h != NULL)
5837             {
5838               h->needs_plt = 1;
5839               if (h->root.root.string[0] == '.'
5840                   && h->root.root.string[1] != '\0')
5841                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5842
5843               if (h == tga || h == dottga)
5844                 {
5845                   sec->has_tls_reloc = 1;
5846                   if (rel != relocs
5847                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5848                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5849                     /* We have a new-style __tls_get_addr call with
5850                        a marker reloc.  */
5851                     ;
5852                   else
5853                     /* Mark this section as having an old-style call.  */
5854                     sec->has_tls_get_addr_call = 1;
5855                 }
5856               plt_list = &h->plt.plist;
5857             }
5858
5859           /* We may need a .plt entry if the function this reloc
5860              refers to is in a shared lib.  */
5861           if (plt_list
5862               && !update_plt_info (abfd, plt_list, rel->r_addend))
5863             return FALSE;
5864           break;
5865
5866         case R_PPC64_ADDR14:
5867         case R_PPC64_ADDR14_BRNTAKEN:
5868         case R_PPC64_ADDR14_BRTAKEN:
5869         case R_PPC64_ADDR24:
5870           goto dodyn;
5871
5872         case R_PPC64_TPREL64:
5873           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5874           if (bfd_link_dll (info))
5875             info->flags |= DF_STATIC_TLS;
5876           goto dotlstoc;
5877
5878         case R_PPC64_DTPMOD64:
5879           if (rel + 1 < rel_end
5880               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5881               && rel[1].r_offset == rel->r_offset + 8)
5882             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5883           else
5884             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5885           goto dotlstoc;
5886
5887         case R_PPC64_DTPREL64:
5888           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5889           if (rel != relocs
5890               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5891               && rel[-1].r_offset == rel->r_offset - 8)
5892             /* This is the second reloc of a dtpmod, dtprel pair.
5893                Don't mark with TLS_DTPREL.  */
5894             goto dodyn;
5895
5896         dotlstoc:
5897           sec->has_tls_reloc = 1;
5898           if (h != NULL)
5899             {
5900               struct ppc_link_hash_entry *eh;
5901               eh = (struct ppc_link_hash_entry *) h;
5902               eh->tls_mask |= tls_type;
5903             }
5904           else
5905             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5906                                         rel->r_addend, tls_type))
5907               return FALSE;
5908
5909           ppc64_sec = ppc64_elf_section_data (sec);
5910           if (ppc64_sec->sec_type != sec_toc)
5911             {
5912               bfd_size_type amt;
5913
5914               /* One extra to simplify get_tls_mask.  */
5915               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5916               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5917               if (ppc64_sec->u.toc.symndx == NULL)
5918                 return FALSE;
5919               amt = sec->size * sizeof (bfd_vma) / 8;
5920               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5921               if (ppc64_sec->u.toc.add == NULL)
5922                 return FALSE;
5923               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5924               ppc64_sec->sec_type = sec_toc;
5925             }
5926           BFD_ASSERT (rel->r_offset % 8 == 0);
5927           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5928           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5929
5930           /* Mark the second slot of a GD or LD entry.
5931              -1 to indicate GD and -2 to indicate LD.  */
5932           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5933             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5934           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5935             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5936           goto dodyn;
5937
5938         case R_PPC64_TPREL16:
5939         case R_PPC64_TPREL16_LO:
5940         case R_PPC64_TPREL16_HI:
5941         case R_PPC64_TPREL16_HA:
5942         case R_PPC64_TPREL16_DS:
5943         case R_PPC64_TPREL16_LO_DS:
5944         case R_PPC64_TPREL16_HIGH:
5945         case R_PPC64_TPREL16_HIGHA:
5946         case R_PPC64_TPREL16_HIGHER:
5947         case R_PPC64_TPREL16_HIGHERA:
5948         case R_PPC64_TPREL16_HIGHEST:
5949         case R_PPC64_TPREL16_HIGHESTA:
5950           if (bfd_link_dll (info))
5951             info->flags |= DF_STATIC_TLS;
5952           goto dodyn;
5953
5954         case R_PPC64_ADDR64:
5955           if (is_opd
5956               && rel + 1 < rel_end
5957               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5958             {
5959               if (h != NULL)
5960                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5961             }
5962           /* Fall through.  */
5963
5964         case R_PPC64_ADDR16:
5965         case R_PPC64_ADDR16_DS:
5966         case R_PPC64_ADDR16_HA:
5967         case R_PPC64_ADDR16_HI:
5968         case R_PPC64_ADDR16_HIGH:
5969         case R_PPC64_ADDR16_HIGHA:
5970         case R_PPC64_ADDR16_HIGHER:
5971         case R_PPC64_ADDR16_HIGHERA:
5972         case R_PPC64_ADDR16_HIGHEST:
5973         case R_PPC64_ADDR16_HIGHESTA:
5974         case R_PPC64_ADDR16_LO:
5975         case R_PPC64_ADDR16_LO_DS:
5976           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5977               && rel->r_addend == 0)
5978             {
5979               /* We may need a .plt entry if this reloc refers to a
5980                  function in a shared lib.  */
5981               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5982                 return FALSE;
5983               h->pointer_equality_needed = 1;
5984             }
5985           /* Fall through.  */
5986
5987         case R_PPC64_REL30:
5988         case R_PPC64_REL32:
5989         case R_PPC64_REL64:
5990         case R_PPC64_ADDR32:
5991         case R_PPC64_UADDR16:
5992         case R_PPC64_UADDR32:
5993         case R_PPC64_UADDR64:
5994         case R_PPC64_TOC:
5995           if (h != NULL && !bfd_link_pic (info))
5996             /* We may need a copy reloc.  */
5997             h->non_got_ref = 1;
5998
5999           /* Don't propagate .opd relocs.  */
6000           if (NO_OPD_RELOCS && is_opd)
6001             break;
6002
6003           /* If we are creating a shared library, and this is a reloc
6004              against a global symbol, or a non PC relative reloc
6005              against a local symbol, then we need to copy the reloc
6006              into the shared library.  However, if we are linking with
6007              -Bsymbolic, we do not need to copy a reloc against a
6008              global symbol which is defined in an object we are
6009              including in the link (i.e., DEF_REGULAR is set).  At
6010              this point we have not seen all the input files, so it is
6011              possible that DEF_REGULAR is not set now but will be set
6012              later (it is never cleared).  In case of a weak definition,
6013              DEF_REGULAR may be cleared later by a strong definition in
6014              a shared library.  We account for that possibility below by
6015              storing information in the dyn_relocs field of the hash
6016              table entry.  A similar situation occurs when creating
6017              shared libraries and symbol visibility changes render the
6018              symbol local.
6019
6020              If on the other hand, we are creating an executable, we
6021              may need to keep relocations for symbols satisfied by a
6022              dynamic library if we manage to avoid copy relocs for the
6023              symbol.  */
6024         dodyn:
6025           if ((bfd_link_pic (info)
6026                && (must_be_dyn_reloc (info, r_type)
6027                    || (h != NULL
6028                        && (!SYMBOLIC_BIND (info, h)
6029                            || h->root.type == bfd_link_hash_defweak
6030                            || !h->def_regular))))
6031               || (ELIMINATE_COPY_RELOCS
6032                   && !bfd_link_pic (info)
6033                   && h != NULL
6034                   && (h->root.type == bfd_link_hash_defweak
6035                       || !h->def_regular))
6036               || (!bfd_link_pic (info)
6037                   && ifunc != NULL))
6038             {
6039               /* We must copy these reloc types into the output file.
6040                  Create a reloc section in dynobj and make room for
6041                  this reloc.  */
6042               if (sreloc == NULL)
6043                 {
6044                   sreloc = _bfd_elf_make_dynamic_reloc_section
6045                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6046
6047                   if (sreloc == NULL)
6048                     return FALSE;
6049                 }
6050
6051               /* If this is a global symbol, we count the number of
6052                  relocations we need for this symbol.  */
6053               if (h != NULL)
6054                 {
6055                   struct elf_dyn_relocs *p;
6056                   struct elf_dyn_relocs **head;
6057
6058                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6059                   p = *head;
6060                   if (p == NULL || p->sec != sec)
6061                     {
6062                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6063                       if (p == NULL)
6064                         return FALSE;
6065                       p->next = *head;
6066                       *head = p;
6067                       p->sec = sec;
6068                       p->count = 0;
6069                       p->pc_count = 0;
6070                     }
6071                   p->count += 1;
6072                   if (!must_be_dyn_reloc (info, r_type))
6073                     p->pc_count += 1;
6074                 }
6075               else
6076                 {
6077                   /* Track dynamic relocs needed for local syms too.
6078                      We really need local syms available to do this
6079                      easily.  Oh well.  */
6080                   struct ppc_dyn_relocs *p;
6081                   struct ppc_dyn_relocs **head;
6082                   bfd_boolean is_ifunc;
6083                   asection *s;
6084                   void *vpp;
6085                   Elf_Internal_Sym *isym;
6086
6087                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6088                                                 abfd, r_symndx);
6089                   if (isym == NULL)
6090                     return FALSE;
6091
6092                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6093                   if (s == NULL)
6094                     s = sec;
6095
6096                   vpp = &elf_section_data (s)->local_dynrel;
6097                   head = (struct ppc_dyn_relocs **) vpp;
6098                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6099                   p = *head;
6100                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6101                     p = p->next;
6102                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6103                     {
6104                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6105                       if (p == NULL)
6106                         return FALSE;
6107                       p->next = *head;
6108                       *head = p;
6109                       p->sec = sec;
6110                       p->ifunc = is_ifunc;
6111                       p->count = 0;
6112                     }
6113                   p->count += 1;
6114                 }
6115             }
6116           break;
6117
6118         default:
6119           break;
6120         }
6121     }
6122
6123   return TRUE;
6124 }
6125
6126 /* Merge backend specific data from an object file to the output
6127    object file when linking.  */
6128
6129 static bfd_boolean
6130 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6131 {
6132   bfd *obfd = info->output_bfd;
6133   unsigned long iflags, oflags;
6134
6135   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6136     return TRUE;
6137
6138   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6139     return TRUE;
6140
6141   if (!_bfd_generic_verify_endian_match (ibfd, info))
6142     return FALSE;
6143
6144   iflags = elf_elfheader (ibfd)->e_flags;
6145   oflags = elf_elfheader (obfd)->e_flags;
6146
6147   if (iflags & ~EF_PPC64_ABI)
6148     {
6149       _bfd_error_handler
6150         /* xgettext:c-format */
6151         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6152       bfd_set_error (bfd_error_bad_value);
6153       return FALSE;
6154     }
6155   else if (iflags != oflags && iflags != 0)
6156     {
6157       _bfd_error_handler
6158         /* xgettext:c-format */
6159         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6160          ibfd, iflags, oflags);
6161       bfd_set_error (bfd_error_bad_value);
6162       return FALSE;
6163     }
6164
6165   _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6166
6167   /* Merge Tag_compatibility attributes and any common GNU ones.  */
6168   _bfd_elf_merge_object_attributes (ibfd, info);
6169
6170   return TRUE;
6171 }
6172
6173 static bfd_boolean
6174 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6175 {
6176   /* Print normal ELF private data.  */
6177   _bfd_elf_print_private_bfd_data (abfd, ptr);
6178
6179   if (elf_elfheader (abfd)->e_flags != 0)
6180     {
6181       FILE *file = ptr;
6182
6183       fprintf (file, _("private flags = 0x%lx:"),
6184                elf_elfheader (abfd)->e_flags);
6185
6186       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6187         fprintf (file, _(" [abiv%ld]"),
6188                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6189       fputc ('\n', file);
6190     }
6191
6192   return TRUE;
6193 }
6194
6195 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6196    of the code entry point, and its section, which must be in the same
6197    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6198
6199 static bfd_vma
6200 opd_entry_value (asection *opd_sec,
6201                  bfd_vma offset,
6202                  asection **code_sec,
6203                  bfd_vma *code_off,
6204                  bfd_boolean in_code_sec)
6205 {
6206   bfd *opd_bfd = opd_sec->owner;
6207   Elf_Internal_Rela *relocs;
6208   Elf_Internal_Rela *lo, *hi, *look;
6209   bfd_vma val;
6210
6211   /* No relocs implies we are linking a --just-symbols object, or looking
6212      at a final linked executable with addr2line or somesuch.  */
6213   if (opd_sec->reloc_count == 0)
6214     {
6215       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6216
6217       if (contents == NULL)
6218         {
6219           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6220             return (bfd_vma) -1;
6221           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6222         }
6223
6224       /* PR 17512: file: 64b9dfbb.  */
6225       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6226         return (bfd_vma) -1;
6227
6228       val = bfd_get_64 (opd_bfd, contents + offset);
6229       if (code_sec != NULL)
6230         {
6231           asection *sec, *likely = NULL;
6232
6233           if (in_code_sec)
6234             {
6235               sec = *code_sec;
6236               if (sec->vma <= val
6237                   && val < sec->vma + sec->size)
6238                 likely = sec;
6239               else
6240                 val = -1;
6241             }
6242           else
6243             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6244               if (sec->vma <= val
6245                   && (sec->flags & SEC_LOAD) != 0
6246                   && (sec->flags & SEC_ALLOC) != 0)
6247                 likely = sec;
6248           if (likely != NULL)
6249             {
6250               *code_sec = likely;
6251               if (code_off != NULL)
6252                 *code_off = val - likely->vma;
6253             }
6254         }
6255       return val;
6256     }
6257
6258   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6259
6260   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6261   if (relocs == NULL)
6262     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6263   /* PR 17512: file: df8e1fd6.  */
6264   if (relocs == NULL)
6265     return (bfd_vma) -1;
6266
6267   /* Go find the opd reloc at the sym address.  */
6268   lo = relocs;
6269   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6270   val = (bfd_vma) -1;
6271   while (lo < hi)
6272     {
6273       look = lo + (hi - lo) / 2;
6274       if (look->r_offset < offset)
6275         lo = look + 1;
6276       else if (look->r_offset > offset)
6277         hi = look;
6278       else
6279         {
6280           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6281
6282           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6283               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6284             {
6285               unsigned long symndx = ELF64_R_SYM (look->r_info);
6286               asection *sec = NULL;
6287
6288               if (symndx >= symtab_hdr->sh_info
6289                   && elf_sym_hashes (opd_bfd) != NULL)
6290                 {
6291                   struct elf_link_hash_entry **sym_hashes;
6292                   struct elf_link_hash_entry *rh;
6293
6294                   sym_hashes = elf_sym_hashes (opd_bfd);
6295                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6296                   if (rh != NULL)
6297                     {
6298                       rh = elf_follow_link (rh);
6299                       if (rh->root.type != bfd_link_hash_defined
6300                           && rh->root.type != bfd_link_hash_defweak)
6301                         break;
6302                       if (rh->root.u.def.section->owner == opd_bfd)
6303                         {
6304                           val = rh->root.u.def.value;
6305                           sec = rh->root.u.def.section;
6306                         }
6307                     }
6308                 }
6309
6310               if (sec == NULL)
6311                 {
6312                   Elf_Internal_Sym *sym;
6313
6314                   if (symndx < symtab_hdr->sh_info)
6315                     {
6316                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6317                       if (sym == NULL)
6318                         {
6319                           size_t symcnt = symtab_hdr->sh_info;
6320                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6321                                                       symcnt, 0,
6322                                                       NULL, NULL, NULL);
6323                           if (sym == NULL)
6324                             break;
6325                           symtab_hdr->contents = (bfd_byte *) sym;
6326                         }
6327                       sym += symndx;
6328                     }
6329                   else
6330                     {
6331                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6332                                                   1, symndx,
6333                                                   NULL, NULL, NULL);
6334                       if (sym == NULL)
6335                         break;
6336                     }
6337                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6338                   if (sec == NULL)
6339                     break;
6340                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6341                   val = sym->st_value;
6342                 }
6343
6344               val += look->r_addend;
6345               if (code_off != NULL)
6346                 *code_off = val;
6347               if (code_sec != NULL)
6348                 {
6349                   if (in_code_sec && *code_sec != sec)
6350                     return -1;
6351                   else
6352                     *code_sec = sec;
6353                 }
6354               if (sec->output_section != NULL)
6355                 val += sec->output_section->vma + sec->output_offset;
6356             }
6357           break;
6358         }
6359     }
6360
6361   return val;
6362 }
6363
6364 /* If the ELF symbol SYM might be a function in SEC, return the
6365    function size and set *CODE_OFF to the function's entry point,
6366    otherwise return zero.  */
6367
6368 static bfd_size_type
6369 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6370                               bfd_vma *code_off)
6371 {
6372   bfd_size_type size;
6373
6374   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6375                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6376     return 0;
6377
6378   size = 0;
6379   if (!(sym->flags & BSF_SYNTHETIC))
6380     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6381
6382   if (strcmp (sym->section->name, ".opd") == 0)
6383     {
6384       struct _opd_sec_data *opd = get_opd_info (sym->section);
6385       bfd_vma symval = sym->value;
6386
6387       if (opd != NULL
6388           && opd->adjust != NULL
6389           && elf_section_data (sym->section)->relocs != NULL)
6390         {
6391           /* opd_entry_value will use cached relocs that have been
6392              adjusted, but with raw symbols.  That means both local
6393              and global symbols need adjusting.  */
6394           long adjust = opd->adjust[OPD_NDX (symval)];
6395           if (adjust == -1)
6396             return 0;
6397           symval += adjust;
6398         }
6399
6400       if (opd_entry_value (sym->section, symval,
6401                            &sec, code_off, TRUE) == (bfd_vma) -1)
6402         return 0;
6403       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6404          symbol.  This size has nothing to do with the code size of the
6405          function, which is what we're supposed to return, but the
6406          code size isn't available without looking up the dot-sym.
6407          However, doing that would be a waste of time particularly
6408          since elf_find_function will look at the dot-sym anyway.
6409          Now, elf_find_function will keep the largest size of any
6410          function sym found at the code address of interest, so return
6411          1 here to avoid it incorrectly caching a larger function size
6412          for a small function.  This does mean we return the wrong
6413          size for a new-ABI function of size 24, but all that does is
6414          disable caching for such functions.  */
6415       if (size == 24)
6416         size = 1;
6417     }
6418   else
6419     {
6420       if (sym->section != sec)
6421         return 0;
6422       *code_off = sym->value;
6423     }
6424   if (size == 0)
6425     size = 1;
6426   return size;
6427 }
6428
6429 /* Return true if symbol is a strong function defined in an ELFv2
6430    object with st_other localentry bits of zero, ie. its local entry
6431    point coincides with its global entry point.  */
6432
6433 static bfd_boolean
6434 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6435 {
6436   return (h != NULL
6437           && h->type == STT_FUNC
6438           && h->root.type == bfd_link_hash_defined
6439           && (STO_PPC64_LOCAL_MASK & h->other) == 0
6440           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6441           && is_ppc64_elf (h->root.u.def.section->owner)
6442           && abiversion (h->root.u.def.section->owner) >= 2);
6443 }
6444
6445 /* Return true if symbol is defined in a regular object file.  */
6446
6447 static bfd_boolean
6448 is_static_defined (struct elf_link_hash_entry *h)
6449 {
6450   return ((h->root.type == bfd_link_hash_defined
6451            || h->root.type == bfd_link_hash_defweak)
6452           && h->root.u.def.section != NULL
6453           && h->root.u.def.section->output_section != NULL);
6454 }
6455
6456 /* If FDH is a function descriptor symbol, return the associated code
6457    entry symbol if it is defined.  Return NULL otherwise.  */
6458
6459 static struct ppc_link_hash_entry *
6460 defined_code_entry (struct ppc_link_hash_entry *fdh)
6461 {
6462   if (fdh->is_func_descriptor)
6463     {
6464       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6465       if (fh->elf.root.type == bfd_link_hash_defined
6466           || fh->elf.root.type == bfd_link_hash_defweak)
6467         return fh;
6468     }
6469   return NULL;
6470 }
6471
6472 /* If FH is a function code entry symbol, return the associated
6473    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6474
6475 static struct ppc_link_hash_entry *
6476 defined_func_desc (struct ppc_link_hash_entry *fh)
6477 {
6478   if (fh->oh != NULL
6479       && fh->oh->is_func_descriptor)
6480     {
6481       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6482       if (fdh->elf.root.type == bfd_link_hash_defined
6483           || fdh->elf.root.type == bfd_link_hash_defweak)
6484         return fdh;
6485     }
6486   return NULL;
6487 }
6488
6489 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6490
6491 /* Garbage collect sections, after first dealing with dot-symbols.  */
6492
6493 static bfd_boolean
6494 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6495 {
6496   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6497
6498   if (htab != NULL && htab->need_func_desc_adj)
6499     {
6500       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6501       htab->need_func_desc_adj = 0;
6502     }
6503   return bfd_elf_gc_sections (abfd, info);
6504 }
6505
6506 /* Mark all our entry sym sections, both opd and code section.  */
6507
6508 static void
6509 ppc64_elf_gc_keep (struct bfd_link_info *info)
6510 {
6511   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6512   struct bfd_sym_chain *sym;
6513
6514   if (htab == NULL)
6515     return;
6516
6517   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6518     {
6519       struct ppc_link_hash_entry *eh, *fh;
6520       asection *sec;
6521
6522       eh = (struct ppc_link_hash_entry *)
6523         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6524       if (eh == NULL)
6525         continue;
6526       if (eh->elf.root.type != bfd_link_hash_defined
6527           && eh->elf.root.type != bfd_link_hash_defweak)
6528         continue;
6529
6530       fh = defined_code_entry (eh);
6531       if (fh != NULL)
6532         {
6533           sec = fh->elf.root.u.def.section;
6534           sec->flags |= SEC_KEEP;
6535         }
6536       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6537                && opd_entry_value (eh->elf.root.u.def.section,
6538                                    eh->elf.root.u.def.value,
6539                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6540         sec->flags |= SEC_KEEP;
6541
6542       sec = eh->elf.root.u.def.section;
6543       sec->flags |= SEC_KEEP;
6544     }
6545 }
6546
6547 /* Mark sections containing dynamically referenced symbols.  When
6548    building shared libraries, we must assume that any visible symbol is
6549    referenced.  */
6550
6551 static bfd_boolean
6552 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6553 {
6554   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6555   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6556   struct ppc_link_hash_entry *fdh;
6557   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6558
6559   /* Dynamic linking info is on the func descriptor sym.  */
6560   fdh = defined_func_desc (eh);
6561   if (fdh != NULL)
6562     eh = fdh;
6563
6564   if ((eh->elf.root.type == bfd_link_hash_defined
6565        || eh->elf.root.type == bfd_link_hash_defweak)
6566       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6567           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6568               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6569               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6570               && (!bfd_link_executable (info)
6571                   || info->gc_keep_exported
6572                   || info->export_dynamic
6573                   || (eh->elf.dynamic
6574                       && d != NULL
6575                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6576               && (eh->elf.versioned >= versioned
6577                   || !bfd_hide_sym_by_version (info->version_info,
6578                                                eh->elf.root.root.string)))))
6579     {
6580       asection *code_sec;
6581       struct ppc_link_hash_entry *fh;
6582
6583       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6584
6585       /* Function descriptor syms cause the associated
6586          function code sym section to be marked.  */
6587       fh = defined_code_entry (eh);
6588       if (fh != NULL)
6589         {
6590           code_sec = fh->elf.root.u.def.section;
6591           code_sec->flags |= SEC_KEEP;
6592         }
6593       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6594                && opd_entry_value (eh->elf.root.u.def.section,
6595                                    eh->elf.root.u.def.value,
6596                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6597         code_sec->flags |= SEC_KEEP;
6598     }
6599
6600   return TRUE;
6601 }
6602
6603 /* Return the section that should be marked against GC for a given
6604    relocation.  */
6605
6606 static asection *
6607 ppc64_elf_gc_mark_hook (asection *sec,
6608                         struct bfd_link_info *info,
6609                         Elf_Internal_Rela *rel,
6610                         struct elf_link_hash_entry *h,
6611                         Elf_Internal_Sym *sym)
6612 {
6613   asection *rsec;
6614
6615   /* Syms return NULL if we're marking .opd, so we avoid marking all
6616      function sections, as all functions are referenced in .opd.  */
6617   rsec = NULL;
6618   if (get_opd_info (sec) != NULL)
6619     return rsec;
6620
6621   if (h != NULL)
6622     {
6623       enum elf_ppc64_reloc_type r_type;
6624       struct ppc_link_hash_entry *eh, *fh, *fdh;
6625
6626       r_type = ELF64_R_TYPE (rel->r_info);
6627       switch (r_type)
6628         {
6629         case R_PPC64_GNU_VTINHERIT:
6630         case R_PPC64_GNU_VTENTRY:
6631           break;
6632
6633         default:
6634           switch (h->root.type)
6635             {
6636             case bfd_link_hash_defined:
6637             case bfd_link_hash_defweak:
6638               eh = (struct ppc_link_hash_entry *) h;
6639               fdh = defined_func_desc (eh);
6640               if (fdh != NULL)
6641                 {
6642                   /* -mcall-aixdesc code references the dot-symbol on
6643                      a call reloc.  Mark the function descriptor too
6644                      against garbage collection.  */
6645                   fdh->elf.mark = 1;
6646                   if (fdh->elf.is_weakalias)
6647                     weakdef (&fdh->elf)->mark = 1;
6648                   eh = fdh;
6649                 }
6650
6651               /* Function descriptor syms cause the associated
6652                  function code sym section to be marked.  */
6653               fh = defined_code_entry (eh);
6654               if (fh != NULL)
6655                 {
6656                   /* They also mark their opd section.  */
6657                   eh->elf.root.u.def.section->gc_mark = 1;
6658
6659                   rsec = fh->elf.root.u.def.section;
6660                 }
6661               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6662                        && opd_entry_value (eh->elf.root.u.def.section,
6663                                            eh->elf.root.u.def.value,
6664                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6665                 eh->elf.root.u.def.section->gc_mark = 1;
6666               else
6667                 rsec = h->root.u.def.section;
6668               break;
6669
6670             case bfd_link_hash_common:
6671               rsec = h->root.u.c.p->section;
6672               break;
6673
6674             default:
6675               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6676             }
6677         }
6678     }
6679   else
6680     {
6681       struct _opd_sec_data *opd;
6682
6683       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6684       opd = get_opd_info (rsec);
6685       if (opd != NULL && opd->func_sec != NULL)
6686         {
6687           rsec->gc_mark = 1;
6688
6689           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6690         }
6691     }
6692
6693   return rsec;
6694 }
6695
6696 /* The maximum size of .sfpr.  */
6697 #define SFPR_MAX (218*4)
6698
6699 struct sfpr_def_parms
6700 {
6701   const char name[12];
6702   unsigned char lo, hi;
6703   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6704   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6705 };
6706
6707 /* Auto-generate _save*, _rest* functions in .sfpr.
6708    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6709    instead.  */
6710
6711 static bfd_boolean
6712 sfpr_define (struct bfd_link_info *info,
6713              const struct sfpr_def_parms *parm,
6714              asection *stub_sec)
6715 {
6716   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6717   unsigned int i;
6718   size_t len = strlen (parm->name);
6719   bfd_boolean writing = FALSE;
6720   char sym[16];
6721
6722   if (htab == NULL)
6723     return FALSE;
6724
6725   memcpy (sym, parm->name, len);
6726   sym[len + 2] = 0;
6727
6728   for (i = parm->lo; i <= parm->hi; i++)
6729     {
6730       struct ppc_link_hash_entry *h;
6731
6732       sym[len + 0] = i / 10 + '0';
6733       sym[len + 1] = i % 10 + '0';
6734       h = (struct ppc_link_hash_entry *)
6735         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6736       if (stub_sec != NULL)
6737         {
6738           if (h != NULL
6739               && h->elf.root.type == bfd_link_hash_defined
6740               && h->elf.root.u.def.section == htab->sfpr)
6741             {
6742               struct elf_link_hash_entry *s;
6743               char buf[32];
6744               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6745               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6746               if (s == NULL)
6747                 return FALSE;
6748               if (s->root.type == bfd_link_hash_new
6749                   || (s->root.type = bfd_link_hash_defined
6750                       && s->root.u.def.section == stub_sec))
6751                 {
6752                   s->root.type = bfd_link_hash_defined;
6753                   s->root.u.def.section = stub_sec;
6754                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6755                                          + h->elf.root.u.def.value);
6756                   s->ref_regular = 1;
6757                   s->def_regular = 1;
6758                   s->ref_regular_nonweak = 1;
6759                   s->forced_local = 1;
6760                   s->non_elf = 0;
6761                   s->root.linker_def = 1;
6762                 }
6763             }
6764           continue;
6765         }
6766       if (h != NULL)
6767         {
6768           h->save_res = 1;
6769           if (!h->elf.def_regular)
6770             {
6771               h->elf.root.type = bfd_link_hash_defined;
6772               h->elf.root.u.def.section = htab->sfpr;
6773               h->elf.root.u.def.value = htab->sfpr->size;
6774               h->elf.type = STT_FUNC;
6775               h->elf.def_regular = 1;
6776               h->elf.non_elf = 0;
6777               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6778               writing = TRUE;
6779               if (htab->sfpr->contents == NULL)
6780                 {
6781                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6782                   if (htab->sfpr->contents == NULL)
6783                     return FALSE;
6784                 }
6785             }
6786         }
6787       if (writing)
6788         {
6789           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6790           if (i != parm->hi)
6791             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6792           else
6793             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6794           htab->sfpr->size = p - htab->sfpr->contents;
6795         }
6796     }
6797
6798   return TRUE;
6799 }
6800
6801 static bfd_byte *
6802 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6803 {
6804   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6805   return p + 4;
6806 }
6807
6808 static bfd_byte *
6809 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6810 {
6811   p = savegpr0 (abfd, p, r);
6812   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6813   p = p + 4;
6814   bfd_put_32 (abfd, BLR, p);
6815   return p + 4;
6816 }
6817
6818 static bfd_byte *
6819 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6820 {
6821   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6822   return p + 4;
6823 }
6824
6825 static bfd_byte *
6826 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6827 {
6828   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6829   p = p + 4;
6830   p = restgpr0 (abfd, p, r);
6831   bfd_put_32 (abfd, MTLR_R0, p);
6832   p = p + 4;
6833   if (r == 29)
6834     {
6835       p = restgpr0 (abfd, p, 30);
6836       p = restgpr0 (abfd, p, 31);
6837     }
6838   bfd_put_32 (abfd, BLR, p);
6839   return p + 4;
6840 }
6841
6842 static bfd_byte *
6843 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6844 {
6845   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6846   return p + 4;
6847 }
6848
6849 static bfd_byte *
6850 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6851 {
6852   p = savegpr1 (abfd, p, r);
6853   bfd_put_32 (abfd, BLR, p);
6854   return p + 4;
6855 }
6856
6857 static bfd_byte *
6858 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6859 {
6860   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6861   return p + 4;
6862 }
6863
6864 static bfd_byte *
6865 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6866 {
6867   p = restgpr1 (abfd, p, r);
6868   bfd_put_32 (abfd, BLR, p);
6869   return p + 4;
6870 }
6871
6872 static bfd_byte *
6873 savefpr (bfd *abfd, bfd_byte *p, int r)
6874 {
6875   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6876   return p + 4;
6877 }
6878
6879 static bfd_byte *
6880 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6881 {
6882   p = savefpr (abfd, p, r);
6883   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6884   p = p + 4;
6885   bfd_put_32 (abfd, BLR, p);
6886   return p + 4;
6887 }
6888
6889 static bfd_byte *
6890 restfpr (bfd *abfd, bfd_byte *p, int r)
6891 {
6892   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6893   return p + 4;
6894 }
6895
6896 static bfd_byte *
6897 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6898 {
6899   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6900   p = p + 4;
6901   p = restfpr (abfd, p, r);
6902   bfd_put_32 (abfd, MTLR_R0, p);
6903   p = p + 4;
6904   if (r == 29)
6905     {
6906       p = restfpr (abfd, p, 30);
6907       p = restfpr (abfd, p, 31);
6908     }
6909   bfd_put_32 (abfd, BLR, p);
6910   return p + 4;
6911 }
6912
6913 static bfd_byte *
6914 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6915 {
6916   p = savefpr (abfd, p, r);
6917   bfd_put_32 (abfd, BLR, p);
6918   return p + 4;
6919 }
6920
6921 static bfd_byte *
6922 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6923 {
6924   p = restfpr (abfd, p, r);
6925   bfd_put_32 (abfd, BLR, p);
6926   return p + 4;
6927 }
6928
6929 static bfd_byte *
6930 savevr (bfd *abfd, bfd_byte *p, int r)
6931 {
6932   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6933   p = p + 4;
6934   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6935   return p + 4;
6936 }
6937
6938 static bfd_byte *
6939 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6940 {
6941   p = savevr (abfd, p, r);
6942   bfd_put_32 (abfd, BLR, p);
6943   return p + 4;
6944 }
6945
6946 static bfd_byte *
6947 restvr (bfd *abfd, bfd_byte *p, int r)
6948 {
6949   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6950   p = p + 4;
6951   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6952   return p + 4;
6953 }
6954
6955 static bfd_byte *
6956 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6957 {
6958   p = restvr (abfd, p, r);
6959   bfd_put_32 (abfd, BLR, p);
6960   return p + 4;
6961 }
6962
6963 /* Called via elf_link_hash_traverse to transfer dynamic linking
6964    information on function code symbol entries to their corresponding
6965    function descriptor symbol entries.  */
6966
6967 static bfd_boolean
6968 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6969 {
6970   struct bfd_link_info *info;
6971   struct ppc_link_hash_table *htab;
6972   struct ppc_link_hash_entry *fh;
6973   struct ppc_link_hash_entry *fdh;
6974   bfd_boolean force_local;
6975
6976   fh = (struct ppc_link_hash_entry *) h;
6977   if (fh->elf.root.type == bfd_link_hash_indirect)
6978     return TRUE;
6979
6980   if (!fh->is_func)
6981     return TRUE;
6982
6983   if (fh->elf.root.root.string[0] != '.'
6984       || fh->elf.root.root.string[1] == '\0')
6985     return TRUE;
6986
6987   info = inf;
6988   htab = ppc_hash_table (info);
6989   if (htab == NULL)
6990     return FALSE;
6991
6992   /* Find the corresponding function descriptor symbol.  */
6993   fdh = lookup_fdh (fh, htab);
6994
6995   /* Resolve undefined references to dot-symbols as the value
6996      in the function descriptor, if we have one in a regular object.
6997      This is to satisfy cases like ".quad .foo".  Calls to functions
6998      in dynamic objects are handled elsewhere.  */
6999   if ((fh->elf.root.type == bfd_link_hash_undefined
7000        || fh->elf.root.type == bfd_link_hash_undefweak)
7001       && (fdh->elf.root.type == bfd_link_hash_defined
7002           || fdh->elf.root.type == bfd_link_hash_defweak)
7003       && get_opd_info (fdh->elf.root.u.def.section) != NULL
7004       && opd_entry_value (fdh->elf.root.u.def.section,
7005                           fdh->elf.root.u.def.value,
7006                           &fh->elf.root.u.def.section,
7007                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7008     {
7009       fh->elf.root.type = fdh->elf.root.type;
7010       fh->elf.forced_local = 1;
7011       fh->elf.def_regular = fdh->elf.def_regular;
7012       fh->elf.def_dynamic = fdh->elf.def_dynamic;
7013     }
7014
7015   if (!fh->elf.dynamic)
7016     {
7017       struct plt_entry *ent;
7018
7019       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7020         if (ent->plt.refcount > 0)
7021           break;
7022       if (ent == NULL)
7023         return TRUE;
7024     }
7025
7026   /* Create a descriptor as undefined if necessary.  */
7027   if (fdh == NULL
7028       && !bfd_link_executable (info)
7029       && (fh->elf.root.type == bfd_link_hash_undefined
7030           || fh->elf.root.type == bfd_link_hash_undefweak))
7031     {
7032       fdh = make_fdh (info, fh);
7033       if (fdh == NULL)
7034         return FALSE;
7035     }
7036
7037   /* We can't support overriding of symbols on a fake descriptor.  */
7038   if (fdh != NULL
7039       && fdh->fake
7040       && (fh->elf.root.type == bfd_link_hash_defined
7041           || fh->elf.root.type == bfd_link_hash_defweak))
7042     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7043
7044   /* Transfer dynamic linking information to the function descriptor.  */
7045   if (fdh != NULL)
7046     {
7047       fdh->elf.ref_regular |= fh->elf.ref_regular;
7048       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7049       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7050       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7051       fdh->elf.dynamic |= fh->elf.dynamic;
7052       fdh->elf.needs_plt |= (fh->elf.needs_plt
7053                              || fh->elf.type == STT_FUNC
7054                              || fh->elf.type == STT_GNU_IFUNC);
7055       move_plt_plist (fh, fdh);
7056
7057       if (!fdh->elf.forced_local
7058           && fh->elf.dynindx != -1)
7059         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7060           return FALSE;
7061     }
7062
7063   /* Now that the info is on the function descriptor, clear the
7064      function code sym info.  Any function code syms for which we
7065      don't have a definition in a regular file, we force local.
7066      This prevents a shared library from exporting syms that have
7067      been imported from another library.  Function code syms that
7068      are really in the library we must leave global to prevent the
7069      linker dragging in a definition from a static library.  */
7070   force_local = (!fh->elf.def_regular
7071                  || fdh == NULL
7072                  || !fdh->elf.def_regular
7073                  || fdh->elf.forced_local);
7074   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7075
7076   return TRUE;
7077 }
7078
7079 static const struct sfpr_def_parms save_res_funcs[] =
7080   {
7081     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7082     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7083     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7084     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7085     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7086     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7087     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7088     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7089     { "._savef", 14, 31, savefpr, savefpr1_tail },
7090     { "._restf", 14, 31, restfpr, restfpr1_tail },
7091     { "_savevr_", 20, 31, savevr, savevr_tail },
7092     { "_restvr_", 20, 31, restvr, restvr_tail }
7093   };
7094
7095 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
7096    this hook to a) provide some gcc support functions, and b) transfer
7097    dynamic linking information gathered so far on function code symbol
7098    entries, to their corresponding function descriptor symbol entries.  */
7099
7100 static bfd_boolean
7101 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7102                             struct bfd_link_info *info)
7103 {
7104   struct ppc_link_hash_table *htab;
7105
7106   htab = ppc_hash_table (info);
7107   if (htab == NULL)
7108     return FALSE;
7109
7110   /* Provide any missing _save* and _rest* functions.  */
7111   if (htab->sfpr != NULL)
7112     {
7113       unsigned int i;
7114
7115       htab->sfpr->size = 0;
7116       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7117         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7118           return FALSE;
7119       if (htab->sfpr->size == 0)
7120         htab->sfpr->flags |= SEC_EXCLUDE;
7121     }
7122
7123   if (bfd_link_relocatable (info))
7124     return TRUE;
7125
7126   if (htab->elf.hgot != NULL)
7127     {
7128       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7129       /* Make .TOC. defined so as to prevent it being made dynamic.
7130          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7131       if (!htab->elf.hgot->def_regular
7132           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7133         {
7134           htab->elf.hgot->root.type = bfd_link_hash_defined;
7135           htab->elf.hgot->root.u.def.value = 0;
7136           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7137           htab->elf.hgot->def_regular = 1;
7138           htab->elf.hgot->root.linker_def = 1;
7139         }
7140       htab->elf.hgot->type = STT_OBJECT;
7141       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7142                                | STV_HIDDEN);
7143     }
7144
7145   if (htab->need_func_desc_adj)
7146     {
7147       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7148       htab->need_func_desc_adj = 0;
7149     }
7150
7151   return TRUE;
7152 }
7153
7154 /* Find dynamic relocs for H that apply to read-only sections.  */
7155
7156 static asection *
7157 readonly_dynrelocs (struct elf_link_hash_entry *h)
7158 {
7159   struct ppc_link_hash_entry *eh;
7160   struct elf_dyn_relocs *p;
7161
7162   eh = (struct ppc_link_hash_entry *) h;
7163   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7164     {
7165       asection *s = p->sec->output_section;
7166
7167       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7168         return p->sec;
7169     }
7170   return NULL;
7171 }
7172
7173 /* Return true if we have dynamic relocs against H or any of its weak
7174    aliases, that apply to read-only sections.  Cannot be used after
7175    size_dynamic_sections.  */
7176
7177 static bfd_boolean
7178 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7179 {
7180   struct ppc_link_hash_entry *eh;
7181
7182   eh = (struct ppc_link_hash_entry *) h;
7183   do
7184     {
7185       if (readonly_dynrelocs (&eh->elf))
7186         return TRUE;
7187       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7188     } while (eh != NULL && &eh->elf != h);
7189
7190   return FALSE;
7191 }
7192
7193 /* Return whether EH has pc-relative dynamic relocs.  */
7194
7195 static bfd_boolean
7196 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7197 {
7198   struct elf_dyn_relocs *p;
7199
7200   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7201     if (p->pc_count != 0)
7202       return TRUE;
7203   return FALSE;
7204 }
7205
7206 /* Return true if a global entry stub will be created for H.  Valid
7207    for ELFv2 before plt entries have been allocated.  */
7208
7209 static bfd_boolean
7210 global_entry_stub (struct elf_link_hash_entry *h)
7211 {
7212   struct plt_entry *pent;
7213
7214   if (!h->pointer_equality_needed
7215       || h->def_regular)
7216     return FALSE;
7217
7218   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7219     if (pent->plt.refcount > 0
7220         && pent->addend == 0)
7221       return TRUE;
7222
7223   return FALSE;
7224 }
7225
7226 /* Adjust a symbol defined by a dynamic object and referenced by a
7227    regular object.  The current definition is in some section of the
7228    dynamic object, but we're not including those sections.  We have to
7229    change the definition to something the rest of the link can
7230    understand.  */
7231
7232 static bfd_boolean
7233 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7234                                  struct elf_link_hash_entry *h)
7235 {
7236   struct ppc_link_hash_table *htab;
7237   asection *s, *srel;
7238
7239   htab = ppc_hash_table (info);
7240   if (htab == NULL)
7241     return FALSE;
7242
7243   /* Deal with function syms.  */
7244   if (h->type == STT_FUNC
7245       || h->type == STT_GNU_IFUNC
7246       || h->needs_plt)
7247     {
7248       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7249                            || SYMBOL_CALLS_LOCAL (info, h)
7250                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7251       /* Discard dyn_relocs when non-pic if we've decided that a
7252          function symbol is local and not an ifunc.  We keep dynamic
7253          relocs for ifuncs when local rather than always emitting a
7254          plt call stub for them and defining the symbol on the call
7255          stub.  We can't do that for ELFv1 anyway (a function symbol
7256          is defined on a descriptor, not code) and it can be faster at
7257          run-time due to not needing to bounce through a stub.  The
7258          dyn_relocs for ifuncs will be applied even in a static
7259          executable.  */
7260       if (!bfd_link_pic (info)
7261           && h->type != STT_GNU_IFUNC
7262           && local)
7263         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7264
7265       /* Clear procedure linkage table information for any symbol that
7266          won't need a .plt entry.  */
7267       struct plt_entry *ent;
7268       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7269         if (ent->plt.refcount > 0)
7270           break;
7271       if (ent == NULL
7272           || (h->type != STT_GNU_IFUNC
7273               && local
7274               && (htab->can_convert_all_inline_plt
7275                   || (((struct ppc_link_hash_entry *) h)->tls_mask
7276                       & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7277         {
7278           h->plt.plist = NULL;
7279           h->needs_plt = 0;
7280           h->pointer_equality_needed = 0;
7281         }
7282       else if (abiversion (info->output_bfd) >= 2)
7283         {
7284           /* Taking a function's address in a read/write section
7285              doesn't require us to define the function symbol in the
7286              executable on a global entry stub.  A dynamic reloc can
7287              be used instead.  The reason we prefer a few more dynamic
7288              relocs is that calling via a global entry stub costs a
7289              few more instructions, and pointer_equality_needed causes
7290              extra work in ld.so when resolving these symbols.  */
7291           if (global_entry_stub (h))
7292             {
7293               if (!readonly_dynrelocs (h))
7294                 {
7295                   h->pointer_equality_needed = 0;
7296                   /* If we haven't seen a branch reloc then we don't need
7297                      a plt entry.  */
7298                   if (!h->needs_plt)
7299                     h->plt.plist = NULL;
7300                 }
7301               else if (!bfd_link_pic (info))
7302                 /* We are going to be defining the function symbol on the
7303                    plt stub, so no dyn_relocs needed when non-pic.  */
7304                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7305             }
7306
7307           /* ELFv2 function symbols can't have copy relocs.  */
7308           return TRUE;
7309         }
7310       else if (!h->needs_plt
7311                && !readonly_dynrelocs (h))
7312         {
7313           /* If we haven't seen a branch reloc then we don't need a
7314              plt entry.  */
7315           h->plt.plist = NULL;
7316           h->pointer_equality_needed = 0;
7317           return TRUE;
7318         }
7319     }
7320   else
7321     h->plt.plist = NULL;
7322
7323   /* If this is a weak symbol, and there is a real definition, the
7324      processor independent code will have arranged for us to see the
7325      real definition first, and we can just use the same value.  */
7326   if (h->is_weakalias)
7327     {
7328       struct elf_link_hash_entry *def = weakdef (h);
7329       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7330       h->root.u.def.section = def->root.u.def.section;
7331       h->root.u.def.value = def->root.u.def.value;
7332       if (def->root.u.def.section == htab->elf.sdynbss
7333           || def->root.u.def.section == htab->elf.sdynrelro)
7334         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7335       return TRUE;
7336     }
7337
7338   /* If we are creating a shared library, we must presume that the
7339      only references to the symbol are via the global offset table.
7340      For such cases we need not do anything here; the relocations will
7341      be handled correctly by relocate_section.  */
7342   if (bfd_link_pic (info))
7343     return TRUE;
7344
7345   /* If there are no references to this symbol that do not use the
7346      GOT, we don't need to generate a copy reloc.  */
7347   if (!h->non_got_ref)
7348     return TRUE;
7349
7350   /* Don't generate a copy reloc for symbols defined in the executable.  */
7351   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7352
7353       /* If -z nocopyreloc was given, don't generate them either.  */
7354       || info->nocopyreloc
7355
7356       /* If we don't find any dynamic relocs in read-only sections, then
7357          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7358       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7359
7360       /* Protected variables do not work with .dynbss.  The copy in
7361          .dynbss won't be used by the shared library with the protected
7362          definition for the variable.  Text relocations are preferable
7363          to an incorrect program.  */
7364       || h->protected_def)
7365     return TRUE;
7366
7367   if (h->plt.plist != NULL)
7368     {
7369       /* We should never get here, but unfortunately there are versions
7370          of gcc out there that improperly (for this ABI) put initialized
7371          function pointers, vtable refs and suchlike in read-only
7372          sections.  Allow them to proceed, but warn that this might
7373          break at runtime.  */
7374       info->callbacks->einfo
7375         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7376            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7377          h->root.root.string);
7378     }
7379
7380   /* This is a reference to a symbol defined by a dynamic object which
7381      is not a function.  */
7382
7383   /* We must allocate the symbol in our .dynbss section, which will
7384      become part of the .bss section of the executable.  There will be
7385      an entry for this symbol in the .dynsym section.  The dynamic
7386      object will contain position independent code, so all references
7387      from the dynamic object to this symbol will go through the global
7388      offset table.  The dynamic linker will use the .dynsym entry to
7389      determine the address it must put in the global offset table, so
7390      both the dynamic object and the regular object will refer to the
7391      same memory location for the variable.  */
7392   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7393     {
7394       s = htab->elf.sdynrelro;
7395       srel = htab->elf.sreldynrelro;
7396     }
7397   else
7398     {
7399       s = htab->elf.sdynbss;
7400       srel = htab->elf.srelbss;
7401     }
7402   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7403     {
7404       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7405          linker to copy the initial value out of the dynamic object
7406          and into the runtime process image.  */
7407       srel->size += sizeof (Elf64_External_Rela);
7408       h->needs_copy = 1;
7409     }
7410
7411   /* We no longer want dyn_relocs.  */
7412   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7413   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7414 }
7415
7416 /* If given a function descriptor symbol, hide both the function code
7417    sym and the descriptor.  */
7418 static void
7419 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7420                        struct elf_link_hash_entry *h,
7421                        bfd_boolean force_local)
7422 {
7423   struct ppc_link_hash_entry *eh;
7424   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7425
7426   eh = (struct ppc_link_hash_entry *) h;
7427   if (eh->is_func_descriptor)
7428     {
7429       struct ppc_link_hash_entry *fh = eh->oh;
7430
7431       if (fh == NULL)
7432         {
7433           const char *p, *q;
7434           struct elf_link_hash_table *htab = elf_hash_table (info);
7435           char save;
7436
7437           /* We aren't supposed to use alloca in BFD because on
7438              systems which do not have alloca the version in libiberty
7439              calls xmalloc, which might cause the program to crash
7440              when it runs out of memory.  This function doesn't have a
7441              return status, so there's no way to gracefully return an
7442              error.  So cheat.  We know that string[-1] can be safely
7443              accessed;  It's either a string in an ELF string table,
7444              or allocated in an objalloc structure.  */
7445
7446           p = eh->elf.root.root.string - 1;
7447           save = *p;
7448           *(char *) p = '.';
7449           fh = (struct ppc_link_hash_entry *)
7450             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7451           *(char *) p = save;
7452
7453           /* Unfortunately, if it so happens that the string we were
7454              looking for was allocated immediately before this string,
7455              then we overwrote the string terminator.  That's the only
7456              reason the lookup should fail.  */
7457           if (fh == NULL)
7458             {
7459               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7460               while (q >= eh->elf.root.root.string && *q == *p)
7461                 --q, --p;
7462               if (q < eh->elf.root.root.string && *p == '.')
7463                 fh = (struct ppc_link_hash_entry *)
7464                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7465             }
7466           if (fh != NULL)
7467             {
7468               eh->oh = fh;
7469               fh->oh = eh;
7470             }
7471         }
7472       if (fh != NULL)
7473         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7474     }
7475 }
7476
7477 static bfd_boolean
7478 get_sym_h (struct elf_link_hash_entry **hp,
7479            Elf_Internal_Sym **symp,
7480            asection **symsecp,
7481            unsigned char **tls_maskp,
7482            Elf_Internal_Sym **locsymsp,
7483            unsigned long r_symndx,
7484            bfd *ibfd)
7485 {
7486   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7487
7488   if (r_symndx >= symtab_hdr->sh_info)
7489     {
7490       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7491       struct elf_link_hash_entry *h;
7492
7493       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7494       h = elf_follow_link (h);
7495
7496       if (hp != NULL)
7497         *hp = h;
7498
7499       if (symp != NULL)
7500         *symp = NULL;
7501
7502       if (symsecp != NULL)
7503         {
7504           asection *symsec = NULL;
7505           if (h->root.type == bfd_link_hash_defined
7506               || h->root.type == bfd_link_hash_defweak)
7507             symsec = h->root.u.def.section;
7508           *symsecp = symsec;
7509         }
7510
7511       if (tls_maskp != NULL)
7512         {
7513           struct ppc_link_hash_entry *eh;
7514
7515           eh = (struct ppc_link_hash_entry *) h;
7516           *tls_maskp = &eh->tls_mask;
7517         }
7518     }
7519   else
7520     {
7521       Elf_Internal_Sym *sym;
7522       Elf_Internal_Sym *locsyms = *locsymsp;
7523
7524       if (locsyms == NULL)
7525         {
7526           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7527           if (locsyms == NULL)
7528             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7529                                             symtab_hdr->sh_info,
7530                                             0, NULL, NULL, NULL);
7531           if (locsyms == NULL)
7532             return FALSE;
7533           *locsymsp = locsyms;
7534         }
7535       sym = locsyms + r_symndx;
7536
7537       if (hp != NULL)
7538         *hp = NULL;
7539
7540       if (symp != NULL)
7541         *symp = sym;
7542
7543       if (symsecp != NULL)
7544         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7545
7546       if (tls_maskp != NULL)
7547         {
7548           struct got_entry **lgot_ents;
7549           unsigned char *tls_mask;
7550
7551           tls_mask = NULL;
7552           lgot_ents = elf_local_got_ents (ibfd);
7553           if (lgot_ents != NULL)
7554             {
7555               struct plt_entry **local_plt = (struct plt_entry **)
7556                 (lgot_ents + symtab_hdr->sh_info);
7557               unsigned char *lgot_masks = (unsigned char *)
7558                 (local_plt + symtab_hdr->sh_info);
7559               tls_mask = &lgot_masks[r_symndx];
7560             }
7561           *tls_maskp = tls_mask;
7562         }
7563     }
7564   return TRUE;
7565 }
7566
7567 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7568    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7569    type suitable for optimization, and 1 otherwise.  */
7570
7571 static int
7572 get_tls_mask (unsigned char **tls_maskp,
7573               unsigned long *toc_symndx,
7574               bfd_vma *toc_addend,
7575               Elf_Internal_Sym **locsymsp,
7576               const Elf_Internal_Rela *rel,
7577               bfd *ibfd)
7578 {
7579   unsigned long r_symndx;
7580   int next_r;
7581   struct elf_link_hash_entry *h;
7582   Elf_Internal_Sym *sym;
7583   asection *sec;
7584   bfd_vma off;
7585
7586   r_symndx = ELF64_R_SYM (rel->r_info);
7587   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7588     return 0;
7589
7590   if ((*tls_maskp != NULL
7591        && (**tls_maskp & TLS_TLS) != 0
7592        && **tls_maskp != (TLS_TLS | TLS_MARK))
7593       || sec == NULL
7594       || ppc64_elf_section_data (sec) == NULL
7595       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7596     return 1;
7597
7598   /* Look inside a TOC section too.  */
7599   if (h != NULL)
7600     {
7601       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7602       off = h->root.u.def.value;
7603     }
7604   else
7605     off = sym->st_value;
7606   off += rel->r_addend;
7607   BFD_ASSERT (off % 8 == 0);
7608   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7609   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7610   if (toc_symndx != NULL)
7611     *toc_symndx = r_symndx;
7612   if (toc_addend != NULL)
7613     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7614   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7615     return 0;
7616   if ((h == NULL || is_static_defined (h))
7617       && (next_r == -1 || next_r == -2))
7618     return 1 - next_r;
7619   return 1;
7620 }
7621
7622 /* Find (or create) an entry in the tocsave hash table.  */
7623
7624 static struct tocsave_entry *
7625 tocsave_find (struct ppc_link_hash_table *htab,
7626               enum insert_option insert,
7627               Elf_Internal_Sym **local_syms,
7628               const Elf_Internal_Rela *irela,
7629               bfd *ibfd)
7630 {
7631   unsigned long r_indx;
7632   struct elf_link_hash_entry *h;
7633   Elf_Internal_Sym *sym;
7634   struct tocsave_entry ent, *p;
7635   hashval_t hash;
7636   struct tocsave_entry **slot;
7637
7638   r_indx = ELF64_R_SYM (irela->r_info);
7639   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7640     return NULL;
7641   if (ent.sec == NULL || ent.sec->output_section == NULL)
7642     {
7643       _bfd_error_handler
7644         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7645       return NULL;
7646     }
7647
7648   if (h != NULL)
7649     ent.offset = h->root.u.def.value;
7650   else
7651     ent.offset = sym->st_value;
7652   ent.offset += irela->r_addend;
7653
7654   hash = tocsave_htab_hash (&ent);
7655   slot = ((struct tocsave_entry **)
7656           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7657   if (slot == NULL)
7658     return NULL;
7659
7660   if (*slot == NULL)
7661     {
7662       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7663       if (p == NULL)
7664         return NULL;
7665       *p = ent;
7666       *slot = p;
7667     }
7668   return *slot;
7669 }
7670
7671 /* Adjust all global syms defined in opd sections.  In gcc generated
7672    code for the old ABI, these will already have been done.  */
7673
7674 static bfd_boolean
7675 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7676 {
7677   struct ppc_link_hash_entry *eh;
7678   asection *sym_sec;
7679   struct _opd_sec_data *opd;
7680
7681   if (h->root.type == bfd_link_hash_indirect)
7682     return TRUE;
7683
7684   if (h->root.type != bfd_link_hash_defined
7685       && h->root.type != bfd_link_hash_defweak)
7686     return TRUE;
7687
7688   eh = (struct ppc_link_hash_entry *) h;
7689   if (eh->adjust_done)
7690     return TRUE;
7691
7692   sym_sec = eh->elf.root.u.def.section;
7693   opd = get_opd_info (sym_sec);
7694   if (opd != NULL && opd->adjust != NULL)
7695     {
7696       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7697       if (adjust == -1)
7698         {
7699           /* This entry has been deleted.  */
7700           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7701           if (dsec == NULL)
7702             {
7703               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7704                 if (discarded_section (dsec))
7705                   {
7706                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7707                     break;
7708                   }
7709             }
7710           eh->elf.root.u.def.value = 0;
7711           eh->elf.root.u.def.section = dsec;
7712         }
7713       else
7714         eh->elf.root.u.def.value += adjust;
7715       eh->adjust_done = 1;
7716     }
7717   return TRUE;
7718 }
7719
7720 /* Handles decrementing dynamic reloc counts for the reloc specified by
7721    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7722    have already been determined.  */
7723
7724 static bfd_boolean
7725 dec_dynrel_count (bfd_vma r_info,
7726                   asection *sec,
7727                   struct bfd_link_info *info,
7728                   Elf_Internal_Sym **local_syms,
7729                   struct elf_link_hash_entry *h,
7730                   Elf_Internal_Sym *sym)
7731 {
7732   enum elf_ppc64_reloc_type r_type;
7733   asection *sym_sec = NULL;
7734
7735   /* Can this reloc be dynamic?  This switch, and later tests here
7736      should be kept in sync with the code in check_relocs.  */
7737   r_type = ELF64_R_TYPE (r_info);
7738   switch (r_type)
7739     {
7740     default:
7741       return TRUE;
7742
7743     case R_PPC64_TPREL16:
7744     case R_PPC64_TPREL16_LO:
7745     case R_PPC64_TPREL16_HI:
7746     case R_PPC64_TPREL16_HA:
7747     case R_PPC64_TPREL16_DS:
7748     case R_PPC64_TPREL16_LO_DS:
7749     case R_PPC64_TPREL16_HIGH:
7750     case R_PPC64_TPREL16_HIGHA:
7751     case R_PPC64_TPREL16_HIGHER:
7752     case R_PPC64_TPREL16_HIGHERA:
7753     case R_PPC64_TPREL16_HIGHEST:
7754     case R_PPC64_TPREL16_HIGHESTA:
7755     case R_PPC64_TPREL64:
7756     case R_PPC64_DTPMOD64:
7757     case R_PPC64_DTPREL64:
7758     case R_PPC64_ADDR64:
7759     case R_PPC64_REL30:
7760     case R_PPC64_REL32:
7761     case R_PPC64_REL64:
7762     case R_PPC64_ADDR14:
7763     case R_PPC64_ADDR14_BRNTAKEN:
7764     case R_PPC64_ADDR14_BRTAKEN:
7765     case R_PPC64_ADDR16:
7766     case R_PPC64_ADDR16_DS:
7767     case R_PPC64_ADDR16_HA:
7768     case R_PPC64_ADDR16_HI:
7769     case R_PPC64_ADDR16_HIGH:
7770     case R_PPC64_ADDR16_HIGHA:
7771     case R_PPC64_ADDR16_HIGHER:
7772     case R_PPC64_ADDR16_HIGHERA:
7773     case R_PPC64_ADDR16_HIGHEST:
7774     case R_PPC64_ADDR16_HIGHESTA:
7775     case R_PPC64_ADDR16_LO:
7776     case R_PPC64_ADDR16_LO_DS:
7777     case R_PPC64_ADDR24:
7778     case R_PPC64_ADDR32:
7779     case R_PPC64_UADDR16:
7780     case R_PPC64_UADDR32:
7781     case R_PPC64_UADDR64:
7782     case R_PPC64_TOC:
7783       break;
7784     }
7785
7786   if (local_syms != NULL)
7787     {
7788       unsigned long r_symndx;
7789       bfd *ibfd = sec->owner;
7790
7791       r_symndx = ELF64_R_SYM (r_info);
7792       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7793         return FALSE;
7794     }
7795
7796   if ((bfd_link_pic (info)
7797        && (must_be_dyn_reloc (info, r_type)
7798            || (h != NULL
7799                && (!SYMBOLIC_BIND (info, h)
7800                    || h->root.type == bfd_link_hash_defweak
7801                    || !h->def_regular))))
7802       || (ELIMINATE_COPY_RELOCS
7803           && !bfd_link_pic (info)
7804           && h != NULL
7805           && (h->root.type == bfd_link_hash_defweak
7806               || !h->def_regular)))
7807     ;
7808   else
7809     return TRUE;
7810
7811   if (h != NULL)
7812     {
7813       struct elf_dyn_relocs *p;
7814       struct elf_dyn_relocs **pp;
7815       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7816
7817       /* elf_gc_sweep may have already removed all dyn relocs associated
7818          with local syms for a given section.  Also, symbol flags are
7819          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7820          report a dynreloc miscount.  */
7821       if (*pp == NULL && info->gc_sections)
7822         return TRUE;
7823
7824       while ((p = *pp) != NULL)
7825         {
7826           if (p->sec == sec)
7827             {
7828               if (!must_be_dyn_reloc (info, r_type))
7829                 p->pc_count -= 1;
7830               p->count -= 1;
7831               if (p->count == 0)
7832                 *pp = p->next;
7833               return TRUE;
7834             }
7835           pp = &p->next;
7836         }
7837     }
7838   else
7839     {
7840       struct ppc_dyn_relocs *p;
7841       struct ppc_dyn_relocs **pp;
7842       void *vpp;
7843       bfd_boolean is_ifunc;
7844
7845       if (local_syms == NULL)
7846         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7847       if (sym_sec == NULL)
7848         sym_sec = sec;
7849
7850       vpp = &elf_section_data (sym_sec)->local_dynrel;
7851       pp = (struct ppc_dyn_relocs **) vpp;
7852
7853       if (*pp == NULL && info->gc_sections)
7854         return TRUE;
7855
7856       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7857       while ((p = *pp) != NULL)
7858         {
7859           if (p->sec == sec && p->ifunc == is_ifunc)
7860             {
7861               p->count -= 1;
7862               if (p->count == 0)
7863                 *pp = p->next;
7864               return TRUE;
7865             }
7866           pp = &p->next;
7867         }
7868     }
7869
7870   /* xgettext:c-format */
7871   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7872                       sec->owner, sec);
7873   bfd_set_error (bfd_error_bad_value);
7874   return FALSE;
7875 }
7876
7877 /* Remove unused Official Procedure Descriptor entries.  Currently we
7878    only remove those associated with functions in discarded link-once
7879    sections, or weakly defined functions that have been overridden.  It
7880    would be possible to remove many more entries for statically linked
7881    applications.  */
7882
7883 bfd_boolean
7884 ppc64_elf_edit_opd (struct bfd_link_info *info)
7885 {
7886   bfd *ibfd;
7887   bfd_boolean some_edited = FALSE;
7888   asection *need_pad = NULL;
7889   struct ppc_link_hash_table *htab;
7890
7891   htab = ppc_hash_table (info);
7892   if (htab == NULL)
7893     return FALSE;
7894
7895   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7896     {
7897       asection *sec;
7898       Elf_Internal_Rela *relstart, *rel, *relend;
7899       Elf_Internal_Shdr *symtab_hdr;
7900       Elf_Internal_Sym *local_syms;
7901       struct _opd_sec_data *opd;
7902       bfd_boolean need_edit, add_aux_fields, broken;
7903       bfd_size_type cnt_16b = 0;
7904
7905       if (!is_ppc64_elf (ibfd))
7906         continue;
7907
7908       sec = bfd_get_section_by_name (ibfd, ".opd");
7909       if (sec == NULL || sec->size == 0)
7910         continue;
7911
7912       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7913         continue;
7914
7915       if (sec->output_section == bfd_abs_section_ptr)
7916         continue;
7917
7918       /* Look through the section relocs.  */
7919       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7920         continue;
7921
7922       local_syms = NULL;
7923       symtab_hdr = &elf_symtab_hdr (ibfd);
7924
7925       /* Read the relocations.  */
7926       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7927                                             info->keep_memory);
7928       if (relstart == NULL)
7929         return FALSE;
7930
7931       /* First run through the relocs to check they are sane, and to
7932          determine whether we need to edit this opd section.  */
7933       need_edit = FALSE;
7934       broken = FALSE;
7935       need_pad = sec;
7936       relend = relstart + sec->reloc_count;
7937       for (rel = relstart; rel < relend; )
7938         {
7939           enum elf_ppc64_reloc_type r_type;
7940           unsigned long r_symndx;
7941           asection *sym_sec;
7942           struct elf_link_hash_entry *h;
7943           Elf_Internal_Sym *sym;
7944           bfd_vma offset;
7945
7946           /* .opd contains an array of 16 or 24 byte entries.  We're
7947              only interested in the reloc pointing to a function entry
7948              point.  */
7949           offset = rel->r_offset;
7950           if (rel + 1 == relend
7951               || rel[1].r_offset != offset + 8)
7952             {
7953               /* If someone messes with .opd alignment then after a
7954                  "ld -r" we might have padding in the middle of .opd.
7955                  Also, there's nothing to prevent someone putting
7956                  something silly in .opd with the assembler.  No .opd
7957                  optimization for them!  */
7958             broken_opd:
7959               _bfd_error_handler
7960                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7961               broken = TRUE;
7962               break;
7963             }
7964
7965           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7966               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7967             {
7968               _bfd_error_handler
7969                 /* xgettext:c-format */
7970                 (_("%pB: unexpected reloc type %u in .opd section"),
7971                  ibfd, r_type);
7972               broken = TRUE;
7973               break;
7974             }
7975
7976           r_symndx = ELF64_R_SYM (rel->r_info);
7977           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7978                           r_symndx, ibfd))
7979             goto error_ret;
7980
7981           if (sym_sec == NULL || sym_sec->owner == NULL)
7982             {
7983               const char *sym_name;
7984               if (h != NULL)
7985                 sym_name = h->root.root.string;
7986               else
7987                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7988                                              sym_sec);
7989
7990               _bfd_error_handler
7991                 /* xgettext:c-format */
7992                 (_("%pB: undefined sym `%s' in .opd section"),
7993                  ibfd, sym_name);
7994               broken = TRUE;
7995               break;
7996             }
7997
7998           /* opd entries are always for functions defined in the
7999              current input bfd.  If the symbol isn't defined in the
8000              input bfd, then we won't be using the function in this
8001              bfd;  It must be defined in a linkonce section in another
8002              bfd, or is weak.  It's also possible that we are
8003              discarding the function due to a linker script /DISCARD/,
8004              which we test for via the output_section.  */
8005           if (sym_sec->owner != ibfd
8006               || sym_sec->output_section == bfd_abs_section_ptr)
8007             need_edit = TRUE;
8008
8009           rel += 2;
8010           if (rel + 1 == relend
8011               || (rel + 2 < relend
8012                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8013             ++rel;
8014
8015           if (rel == relend)
8016             {
8017               if (sec->size == offset + 24)
8018                 {
8019                   need_pad = NULL;
8020                   break;
8021                 }
8022               if (sec->size == offset + 16)
8023                 {
8024                   cnt_16b++;
8025                   break;
8026                 }
8027               goto broken_opd;
8028             }
8029           else if (rel + 1 < relend
8030                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8031                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8032             {
8033               if (rel[0].r_offset == offset + 16)
8034                 cnt_16b++;
8035               else if (rel[0].r_offset != offset + 24)
8036                 goto broken_opd;
8037             }
8038           else
8039             goto broken_opd;
8040         }
8041
8042       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8043
8044       if (!broken && (need_edit || add_aux_fields))
8045         {
8046           Elf_Internal_Rela *write_rel;
8047           Elf_Internal_Shdr *rel_hdr;
8048           bfd_byte *rptr, *wptr;
8049           bfd_byte *new_contents;
8050           bfd_size_type amt;
8051
8052           new_contents = NULL;
8053           amt = OPD_NDX (sec->size) * sizeof (long);
8054           opd = &ppc64_elf_section_data (sec)->u.opd;
8055           opd->adjust = bfd_zalloc (sec->owner, amt);
8056           if (opd->adjust == NULL)
8057             return FALSE;
8058
8059           /* This seems a waste of time as input .opd sections are all
8060              zeros as generated by gcc, but I suppose there's no reason
8061              this will always be so.  We might start putting something in
8062              the third word of .opd entries.  */
8063           if ((sec->flags & SEC_IN_MEMORY) == 0)
8064             {
8065               bfd_byte *loc;
8066               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8067                 {
8068                   if (loc != NULL)
8069                     free (loc);
8070                 error_ret:
8071                   if (local_syms != NULL
8072                       && symtab_hdr->contents != (unsigned char *) local_syms)
8073                     free (local_syms);
8074                   if (elf_section_data (sec)->relocs != relstart)
8075                     free (relstart);
8076                   return FALSE;
8077                 }
8078               sec->contents = loc;
8079               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8080             }
8081
8082           elf_section_data (sec)->relocs = relstart;
8083
8084           new_contents = sec->contents;
8085           if (add_aux_fields)
8086             {
8087               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8088               if (new_contents == NULL)
8089                 return FALSE;
8090               need_pad = NULL;
8091             }
8092           wptr = new_contents;
8093           rptr = sec->contents;
8094           write_rel = relstart;
8095           for (rel = relstart; rel < relend; )
8096             {
8097               unsigned long r_symndx;
8098               asection *sym_sec;
8099               struct elf_link_hash_entry *h;
8100               struct ppc_link_hash_entry *fdh = NULL;
8101               Elf_Internal_Sym *sym;
8102               long opd_ent_size;
8103               Elf_Internal_Rela *next_rel;
8104               bfd_boolean skip;
8105
8106               r_symndx = ELF64_R_SYM (rel->r_info);
8107               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8108                               r_symndx, ibfd))
8109                 goto error_ret;
8110
8111               next_rel = rel + 2;
8112               if (next_rel + 1 == relend
8113                   || (next_rel + 2 < relend
8114                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8115                 ++next_rel;
8116
8117               /* See if the .opd entry is full 24 byte or
8118                  16 byte (with fd_aux entry overlapped with next
8119                  fd_func).  */
8120               opd_ent_size = 24;
8121               if (next_rel == relend)
8122                 {
8123                   if (sec->size == rel->r_offset + 16)
8124                     opd_ent_size = 16;
8125                 }
8126               else if (next_rel->r_offset == rel->r_offset + 16)
8127                 opd_ent_size = 16;
8128
8129               if (h != NULL
8130                   && h->root.root.string[0] == '.')
8131                 {
8132                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
8133                   if (fdh != NULL)
8134                     {
8135                       fdh = ppc_follow_link (fdh);
8136                       if (fdh->elf.root.type != bfd_link_hash_defined
8137                           && fdh->elf.root.type != bfd_link_hash_defweak)
8138                         fdh = NULL;
8139                     }
8140                 }
8141
8142               skip = (sym_sec->owner != ibfd
8143                       || sym_sec->output_section == bfd_abs_section_ptr);
8144               if (skip)
8145                 {
8146                   if (fdh != NULL && sym_sec->owner == ibfd)
8147                     {
8148                       /* Arrange for the function descriptor sym
8149                          to be dropped.  */
8150                       fdh->elf.root.u.def.value = 0;
8151                       fdh->elf.root.u.def.section = sym_sec;
8152                     }
8153                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8154
8155                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8156                     rel = next_rel;
8157                   else
8158                     while (1)
8159                       {
8160                         if (!dec_dynrel_count (rel->r_info, sec, info,
8161                                                NULL, h, sym))
8162                           goto error_ret;
8163
8164                         if (++rel == next_rel)
8165                           break;
8166
8167                         r_symndx = ELF64_R_SYM (rel->r_info);
8168                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8169                                         r_symndx, ibfd))
8170                           goto error_ret;
8171                       }
8172                 }
8173               else
8174                 {
8175                   /* We'll be keeping this opd entry.  */
8176                   long adjust;
8177
8178                   if (fdh != NULL)
8179                     {
8180                       /* Redefine the function descriptor symbol to
8181                          this location in the opd section.  It is
8182                          necessary to update the value here rather
8183                          than using an array of adjustments as we do
8184                          for local symbols, because various places
8185                          in the generic ELF code use the value
8186                          stored in u.def.value.  */
8187                       fdh->elf.root.u.def.value = wptr - new_contents;
8188                       fdh->adjust_done = 1;
8189                     }
8190
8191                   /* Local syms are a bit tricky.  We could
8192                      tweak them as they can be cached, but
8193                      we'd need to look through the local syms
8194                      for the function descriptor sym which we
8195                      don't have at the moment.  So keep an
8196                      array of adjustments.  */
8197                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8198                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8199
8200                   if (wptr != rptr)
8201                     memcpy (wptr, rptr, opd_ent_size);
8202                   wptr += opd_ent_size;
8203                   if (add_aux_fields && opd_ent_size == 16)
8204                     {
8205                       memset (wptr, '\0', 8);
8206                       wptr += 8;
8207                     }
8208
8209                   /* We need to adjust any reloc offsets to point to the
8210                      new opd entries.  */
8211                   for ( ; rel != next_rel; ++rel)
8212                     {
8213                       rel->r_offset += adjust;
8214                       if (write_rel != rel)
8215                         memcpy (write_rel, rel, sizeof (*rel));
8216                       ++write_rel;
8217                     }
8218                 }
8219
8220               rptr += opd_ent_size;
8221             }
8222
8223           sec->size = wptr - new_contents;
8224           sec->reloc_count = write_rel - relstart;
8225           if (add_aux_fields)
8226             {
8227               free (sec->contents);
8228               sec->contents = new_contents;
8229             }
8230
8231           /* Fudge the header size too, as this is used later in
8232              elf_bfd_final_link if we are emitting relocs.  */
8233           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8234           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8235           some_edited = TRUE;
8236         }
8237       else if (elf_section_data (sec)->relocs != relstart)
8238         free (relstart);
8239
8240       if (local_syms != NULL
8241           && symtab_hdr->contents != (unsigned char *) local_syms)
8242         {
8243           if (!info->keep_memory)
8244             free (local_syms);
8245           else
8246             symtab_hdr->contents = (unsigned char *) local_syms;
8247         }
8248     }
8249
8250   if (some_edited)
8251     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8252
8253   /* If we are doing a final link and the last .opd entry is just 16 byte
8254      long, add a 8 byte padding after it.  */
8255   if (need_pad != NULL && !bfd_link_relocatable (info))
8256     {
8257       bfd_byte *p;
8258
8259       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8260         {
8261           BFD_ASSERT (need_pad->size > 0);
8262
8263           p = bfd_malloc (need_pad->size + 8);
8264           if (p == NULL)
8265             return FALSE;
8266
8267           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8268                                           p, 0, need_pad->size))
8269             return FALSE;
8270
8271           need_pad->contents = p;
8272           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8273         }
8274       else
8275         {
8276           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8277           if (p == NULL)
8278             return FALSE;
8279
8280           need_pad->contents = p;
8281         }
8282
8283       memset (need_pad->contents + need_pad->size, 0, 8);
8284       need_pad->size += 8;
8285     }
8286
8287   return TRUE;
8288 }
8289
8290 /* Analyze inline PLT call relocations to see whether calls to locally
8291    defined functions can be converted to direct calls.  */
8292
8293 bfd_boolean
8294 ppc64_elf_inline_plt (struct bfd_link_info *info)
8295 {
8296   struct ppc_link_hash_table *htab;
8297   bfd *ibfd;
8298   asection *sec;
8299   bfd_vma low_vma, high_vma, limit;
8300
8301   htab = ppc_hash_table (info);
8302   if (htab == NULL)
8303     return FALSE;
8304
8305   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
8306      reduced somewhat to cater for possible stubs that might be added
8307      between the call and its destination.  */
8308   if (htab->params->group_size < 0)
8309     {
8310       limit = -htab->params->group_size;
8311       if (limit == 1)
8312         limit = 0x1e00000;
8313     }
8314   else
8315     {
8316       limit = htab->params->group_size;
8317       if (limit == 1)
8318         limit = 0x1c00000;
8319     }
8320
8321   low_vma = -1;
8322   high_vma = 0;
8323   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8324     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8325       {
8326         if (low_vma > sec->vma)
8327           low_vma = sec->vma;
8328         if (high_vma < sec->vma + sec->size)
8329           high_vma = sec->vma + sec->size;
8330       }
8331
8332   /* If a "bl" can reach anywhere in local code sections, then we can
8333      convert all inline PLT sequences to direct calls when the symbol
8334      is local.  */
8335   if (high_vma - low_vma < limit)
8336     {
8337       htab->can_convert_all_inline_plt = 1;
8338       return TRUE;
8339     }
8340
8341   /* Otherwise, go looking through relocs for cases where a direct
8342      call won't reach.  Mark the symbol on any such reloc to disable
8343      the optimization and keep the PLT entry as it seems likely that
8344      this will be better than creating trampolines.  Note that this
8345      will disable the optimization for all inline PLT calls to a
8346      particular symbol, not just those that won't reach.  The
8347      difficulty in doing a more precise optimization is that the
8348      linker needs to make a decision depending on whether a
8349      particular R_PPC64_PLTCALL insn can be turned into a direct
8350      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8351      the sequence, and there is nothing that ties those relocs
8352      together except their symbol.  */
8353
8354   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8355     {
8356       Elf_Internal_Shdr *symtab_hdr;
8357       Elf_Internal_Sym *local_syms;
8358
8359       if (!is_ppc64_elf (ibfd))
8360         continue;
8361
8362       local_syms = NULL;
8363       symtab_hdr = &elf_symtab_hdr (ibfd);
8364
8365       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8366         if (ppc64_elf_section_data (sec)->has_pltcall
8367             && !bfd_is_abs_section (sec->output_section))
8368           {
8369             Elf_Internal_Rela *relstart, *rel, *relend;
8370
8371             /* Read the relocations.  */
8372             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8373                                                   info->keep_memory);
8374             if (relstart == NULL)
8375               return FALSE;
8376
8377             relend = relstart + sec->reloc_count;
8378             for (rel = relstart; rel < relend; )
8379               {
8380                 enum elf_ppc64_reloc_type r_type;
8381                 unsigned long r_symndx;
8382                 asection *sym_sec;
8383                 struct elf_link_hash_entry *h;
8384                 Elf_Internal_Sym *sym;
8385                 unsigned char *tls_maskp;
8386
8387                 r_type = ELF64_R_TYPE (rel->r_info);
8388                 if (r_type != R_PPC64_PLTCALL)
8389                   continue;
8390
8391                 r_symndx = ELF64_R_SYM (rel->r_info);
8392                 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8393                                 r_symndx, ibfd))
8394                   {
8395                     if (elf_section_data (sec)->relocs != relstart)
8396                       free (relstart);
8397                     if (local_syms != NULL
8398                         && symtab_hdr->contents != (unsigned char *) local_syms)
8399                       free (local_syms);
8400                     return FALSE;
8401                   }
8402
8403                 if (sym_sec != NULL && sym_sec->output_section != NULL)
8404                   {
8405                     bfd_vma from, to;
8406                     if (h != NULL)
8407                       to = h->root.u.def.value;
8408                     else
8409                       to = sym->st_value;
8410                     to += (rel->r_addend
8411                            + sym_sec->output_offset
8412                            + sym_sec->output_section->vma);
8413                     from = (rel->r_offset
8414                             + sec->output_offset
8415                             + sec->output_section->vma);
8416                     if (to - from + limit < 2 * limit)
8417                       *tls_maskp &= ~PLT_KEEP;
8418                   }
8419               }
8420             if (elf_section_data (sec)->relocs != relstart)
8421               free (relstart);
8422           }
8423
8424       if (local_syms != NULL
8425           && symtab_hdr->contents != (unsigned char *) local_syms)
8426         {
8427           if (!info->keep_memory)
8428             free (local_syms);
8429           else
8430             symtab_hdr->contents = (unsigned char *) local_syms;
8431         }
8432     }
8433
8434   return TRUE;
8435 }
8436
8437 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8438
8439 asection *
8440 ppc64_elf_tls_setup (struct bfd_link_info *info)
8441 {
8442   struct ppc_link_hash_table *htab;
8443
8444   htab = ppc_hash_table (info);
8445   if (htab == NULL)
8446     return NULL;
8447
8448   if (abiversion (info->output_bfd) == 1)
8449     htab->opd_abi = 1;
8450
8451   if (htab->params->no_multi_toc)
8452     htab->do_multi_toc = 0;
8453   else if (!htab->do_multi_toc)
8454     htab->params->no_multi_toc = 1;
8455
8456   /* Default to --no-plt-localentry, as this option can cause problems
8457      with symbol interposition.  For example, glibc libpthread.so and
8458      libc.so duplicate many pthread symbols, with a fallback
8459      implementation in libc.so.  In some cases the fallback does more
8460      work than the pthread implementation.  __pthread_condattr_destroy
8461      is one such symbol: the libpthread.so implementation is
8462      localentry:0 while the libc.so implementation is localentry:8.
8463      An app that "cleverly" uses dlopen to only load necessary
8464      libraries at runtime may omit loading libpthread.so when not
8465      running multi-threaded, which then results in the libc.so
8466      fallback symbols being used and ld.so complaining.  Now there
8467      are workarounds in ld (see non_zero_localentry) to detect the
8468      pthread situation, but that may not be the only case where
8469      --plt-localentry can cause trouble.  */
8470   if (htab->params->plt_localentry0 < 0)
8471     htab->params->plt_localentry0 = 0;
8472   if (htab->params->plt_localentry0
8473       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8474                                FALSE, FALSE, FALSE) == NULL)
8475     _bfd_error_handler
8476       (_("warning: --plt-localentry is especially dangerous without "
8477          "ld.so support to detect ABI violations"));
8478
8479   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8480                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8481                                               FALSE, FALSE, TRUE));
8482   /* Move dynamic linking info to the function descriptor sym.  */
8483   if (htab->tls_get_addr != NULL)
8484     func_desc_adjust (&htab->tls_get_addr->elf, info);
8485   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8486                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8487                                                  FALSE, FALSE, TRUE));
8488   if (htab->params->tls_get_addr_opt)
8489     {
8490       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8491
8492       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8493                                   FALSE, FALSE, TRUE);
8494       if (opt != NULL)
8495         func_desc_adjust (opt, info);
8496       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8497                                      FALSE, FALSE, TRUE);
8498       if (opt_fd != NULL
8499           && (opt_fd->root.type == bfd_link_hash_defined
8500               || opt_fd->root.type == bfd_link_hash_defweak))
8501         {
8502           /* If glibc supports an optimized __tls_get_addr call stub,
8503              signalled by the presence of __tls_get_addr_opt, and we'll
8504              be calling __tls_get_addr via a plt call stub, then
8505              make __tls_get_addr point to __tls_get_addr_opt.  */
8506           tga_fd = &htab->tls_get_addr_fd->elf;
8507           if (htab->elf.dynamic_sections_created
8508               && tga_fd != NULL
8509               && (tga_fd->type == STT_FUNC
8510                   || tga_fd->needs_plt)
8511               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8512                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8513             {
8514               struct plt_entry *ent;
8515
8516               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8517                 if (ent->plt.refcount > 0)
8518                   break;
8519               if (ent != NULL)
8520                 {
8521                   tga_fd->root.type = bfd_link_hash_indirect;
8522                   tga_fd->root.u.i.link = &opt_fd->root;
8523                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8524                   opt_fd->mark = 1;
8525                   if (opt_fd->dynindx != -1)
8526                     {
8527                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8528                       opt_fd->dynindx = -1;
8529                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8530                                               opt_fd->dynstr_index);
8531                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8532                         return NULL;
8533                     }
8534                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8535                   tga = &htab->tls_get_addr->elf;
8536                   if (opt != NULL && tga != NULL)
8537                     {
8538                       tga->root.type = bfd_link_hash_indirect;
8539                       tga->root.u.i.link = &opt->root;
8540                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8541                       opt->mark = 1;
8542                       _bfd_elf_link_hash_hide_symbol (info, opt,
8543                                                       tga->forced_local);
8544                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8545                     }
8546                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8547                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8548                   if (htab->tls_get_addr != NULL)
8549                     {
8550                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8551                       htab->tls_get_addr->is_func = 1;
8552                     }
8553                 }
8554             }
8555         }
8556       else if (htab->params->tls_get_addr_opt < 0)
8557         htab->params->tls_get_addr_opt = 0;
8558     }
8559   return _bfd_elf_tls_setup (info->output_bfd, info);
8560 }
8561
8562 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8563    HASH1 or HASH2.  */
8564
8565 static bfd_boolean
8566 branch_reloc_hash_match (const bfd *ibfd,
8567                          const Elf_Internal_Rela *rel,
8568                          const struct ppc_link_hash_entry *hash1,
8569                          const struct ppc_link_hash_entry *hash2)
8570 {
8571   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8572   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8573   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8574
8575   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8576     {
8577       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8578       struct elf_link_hash_entry *h;
8579
8580       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8581       h = elf_follow_link (h);
8582       if (h == &hash1->elf || h == &hash2->elf)
8583         return TRUE;
8584     }
8585   return FALSE;
8586 }
8587
8588 /* Run through all the TLS relocs looking for optimization
8589    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8590    a preliminary section layout so that we know the TLS segment
8591    offsets.  We can't optimize earlier because some optimizations need
8592    to know the tp offset, and we need to optimize before allocating
8593    dynamic relocations.  */
8594
8595 bfd_boolean
8596 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8597 {
8598   bfd *ibfd;
8599   asection *sec;
8600   struct ppc_link_hash_table *htab;
8601   unsigned char *toc_ref;
8602   int pass;
8603
8604   if (!bfd_link_executable (info))
8605     return TRUE;
8606
8607   htab = ppc_hash_table (info);
8608   if (htab == NULL)
8609     return FALSE;
8610
8611   /* Make two passes over the relocs.  On the first pass, mark toc
8612      entries involved with tls relocs, and check that tls relocs
8613      involved in setting up a tls_get_addr call are indeed followed by
8614      such a call.  If they are not, we can't do any tls optimization.
8615      On the second pass twiddle tls_mask flags to notify
8616      relocate_section that optimization can be done, and adjust got
8617      and plt refcounts.  */
8618   toc_ref = NULL;
8619   for (pass = 0; pass < 2; ++pass)
8620     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8621       {
8622         Elf_Internal_Sym *locsyms = NULL;
8623         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8624
8625         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8626           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8627             {
8628               Elf_Internal_Rela *relstart, *rel, *relend;
8629               bfd_boolean found_tls_get_addr_arg = 0;
8630
8631               /* Read the relocations.  */
8632               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8633                                                     info->keep_memory);
8634               if (relstart == NULL)
8635                 {
8636                   free (toc_ref);
8637                   return FALSE;
8638                 }
8639
8640               relend = relstart + sec->reloc_count;
8641               for (rel = relstart; rel < relend; rel++)
8642                 {
8643                   enum elf_ppc64_reloc_type r_type;
8644                   unsigned long r_symndx;
8645                   struct elf_link_hash_entry *h;
8646                   Elf_Internal_Sym *sym;
8647                   asection *sym_sec;
8648                   unsigned char *tls_mask;
8649                   unsigned char tls_set, tls_clear, tls_type = 0;
8650                   bfd_vma value;
8651                   bfd_boolean ok_tprel, is_local;
8652                   long toc_ref_index = 0;
8653                   int expecting_tls_get_addr = 0;
8654                   bfd_boolean ret = FALSE;
8655
8656                   r_symndx = ELF64_R_SYM (rel->r_info);
8657                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8658                                   r_symndx, ibfd))
8659                     {
8660                     err_free_rel:
8661                       if (elf_section_data (sec)->relocs != relstart)
8662                         free (relstart);
8663                       if (toc_ref != NULL)
8664                         free (toc_ref);
8665                       if (locsyms != NULL
8666                           && (elf_symtab_hdr (ibfd).contents
8667                               != (unsigned char *) locsyms))
8668                         free (locsyms);
8669                       return ret;
8670                     }
8671
8672                   if (h != NULL)
8673                     {
8674                       if (h->root.type == bfd_link_hash_defined
8675                           || h->root.type == bfd_link_hash_defweak)
8676                         value = h->root.u.def.value;
8677                       else if (h->root.type == bfd_link_hash_undefweak)
8678                         value = 0;
8679                       else
8680                         {
8681                           found_tls_get_addr_arg = 0;
8682                           continue;
8683                         }
8684                     }
8685                   else
8686                     /* Symbols referenced by TLS relocs must be of type
8687                        STT_TLS.  So no need for .opd local sym adjust.  */
8688                     value = sym->st_value;
8689
8690                   ok_tprel = FALSE;
8691                   is_local = FALSE;
8692                   if (h == NULL
8693                       || !h->def_dynamic)
8694                     {
8695                       is_local = TRUE;
8696                       if (h != NULL
8697                           && h->root.type == bfd_link_hash_undefweak)
8698                         ok_tprel = TRUE;
8699                       else if (sym_sec != NULL
8700                                && sym_sec->output_section != NULL)
8701                         {
8702                           value += sym_sec->output_offset;
8703                           value += sym_sec->output_section->vma;
8704                           value -= htab->elf.tls_sec->vma;
8705                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8706                                       < (bfd_vma) 1 << 32);
8707                         }
8708                     }
8709
8710                   r_type = ELF64_R_TYPE (rel->r_info);
8711                   /* If this section has old-style __tls_get_addr calls
8712                      without marker relocs, then check that each
8713                      __tls_get_addr call reloc is preceded by a reloc
8714                      that conceivably belongs to the __tls_get_addr arg
8715                      setup insn.  If we don't find matching arg setup
8716                      relocs, don't do any tls optimization.  */
8717                   if (pass == 0
8718                       && sec->has_tls_get_addr_call
8719                       && h != NULL
8720                       && (h == &htab->tls_get_addr->elf
8721                           || h == &htab->tls_get_addr_fd->elf)
8722                       && !found_tls_get_addr_arg
8723                       && is_branch_reloc (r_type))
8724                     {
8725                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8726                                                 "TLS optimization disabled\n"),
8727                                               ibfd, sec, rel->r_offset);
8728                       ret = TRUE;
8729                       goto err_free_rel;
8730                     }
8731
8732                   found_tls_get_addr_arg = 0;
8733                   switch (r_type)
8734                     {
8735                     case R_PPC64_GOT_TLSLD16:
8736                     case R_PPC64_GOT_TLSLD16_LO:
8737                       expecting_tls_get_addr = 1;
8738                       found_tls_get_addr_arg = 1;
8739                       /* Fall through.  */
8740
8741                     case R_PPC64_GOT_TLSLD16_HI:
8742                     case R_PPC64_GOT_TLSLD16_HA:
8743                       /* These relocs should never be against a symbol
8744                          defined in a shared lib.  Leave them alone if
8745                          that turns out to be the case.  */
8746                       if (!is_local)
8747                         continue;
8748
8749                       /* LD -> LE */
8750                       tls_set = 0;
8751                       tls_clear = TLS_LD;
8752                       tls_type = TLS_TLS | TLS_LD;
8753                       break;
8754
8755                     case R_PPC64_GOT_TLSGD16:
8756                     case R_PPC64_GOT_TLSGD16_LO:
8757                       expecting_tls_get_addr = 1;
8758                       found_tls_get_addr_arg = 1;
8759                       /* Fall through. */
8760
8761                     case R_PPC64_GOT_TLSGD16_HI:
8762                     case R_PPC64_GOT_TLSGD16_HA:
8763                       if (ok_tprel)
8764                         /* GD -> LE */
8765                         tls_set = 0;
8766                       else
8767                         /* GD -> IE */
8768                         tls_set = TLS_TLS | TLS_TPRELGD;
8769                       tls_clear = TLS_GD;
8770                       tls_type = TLS_TLS | TLS_GD;
8771                       break;
8772
8773                     case R_PPC64_GOT_TPREL16_DS:
8774                     case R_PPC64_GOT_TPREL16_LO_DS:
8775                     case R_PPC64_GOT_TPREL16_HI:
8776                     case R_PPC64_GOT_TPREL16_HA:
8777                       if (ok_tprel)
8778                         {
8779                           /* IE -> LE */
8780                           tls_set = 0;
8781                           tls_clear = TLS_TPREL;
8782                           tls_type = TLS_TLS | TLS_TPREL;
8783                           break;
8784                         }
8785                       continue;
8786
8787                     case R_PPC64_TLSGD:
8788                     case R_PPC64_TLSLD:
8789                       if (rel + 1 < relend
8790                           && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8791                         {
8792                           if (pass != 0
8793                               && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8794                             {
8795                               r_symndx = ELF64_R_SYM (rel[1].r_info);
8796                               if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8797                                   r_symndx, ibfd))
8798                                 goto err_free_rel;
8799                               if (h != NULL)
8800                                 {
8801                                   struct plt_entry *ent = NULL;
8802
8803                                   for (ent = h->plt.plist;
8804                                        ent != NULL;
8805                                        ent = ent->next)
8806                                     if (ent->addend == rel[1].r_addend)
8807                                       break;
8808
8809                                   if (ent != NULL
8810                                       && ent->plt.refcount > 0)
8811                                     ent->plt.refcount -= 1;
8812                                 }
8813                             }
8814                           continue;
8815                         }
8816                       found_tls_get_addr_arg = 1;
8817                       /* Fall through.  */
8818
8819                     case R_PPC64_TLS:
8820                     case R_PPC64_TOC16:
8821                     case R_PPC64_TOC16_LO:
8822                       if (sym_sec == NULL || sym_sec != toc)
8823                         continue;
8824
8825                       /* Mark this toc entry as referenced by a TLS
8826                          code sequence.  We can do that now in the
8827                          case of R_PPC64_TLS, and after checking for
8828                          tls_get_addr for the TOC16 relocs.  */
8829                       if (toc_ref == NULL)
8830                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8831                       if (toc_ref == NULL)
8832                         goto err_free_rel;
8833
8834                       if (h != NULL)
8835                         value = h->root.u.def.value;
8836                       else
8837                         value = sym->st_value;
8838                       value += rel->r_addend;
8839                       if (value % 8 != 0)
8840                         continue;
8841                       BFD_ASSERT (value < toc->size
8842                                   && toc->output_offset % 8 == 0);
8843                       toc_ref_index = (value + toc->output_offset) / 8;
8844                       if (r_type == R_PPC64_TLS
8845                           || r_type == R_PPC64_TLSGD
8846                           || r_type == R_PPC64_TLSLD)
8847                         {
8848                           toc_ref[toc_ref_index] = 1;
8849                           continue;
8850                         }
8851
8852                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8853                         continue;
8854
8855                       tls_set = 0;
8856                       tls_clear = 0;
8857                       expecting_tls_get_addr = 2;
8858                       break;
8859
8860                     case R_PPC64_TPREL64:
8861                       if (pass == 0
8862                           || sec != toc
8863                           || toc_ref == NULL
8864                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8865                         continue;
8866                       if (ok_tprel)
8867                         {
8868                           /* IE -> LE */
8869                           tls_set = TLS_EXPLICIT;
8870                           tls_clear = TLS_TPREL;
8871                           break;
8872                         }
8873                       continue;
8874
8875                     case R_PPC64_DTPMOD64:
8876                       if (pass == 0
8877                           || sec != toc
8878                           || toc_ref == NULL
8879                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8880                         continue;
8881                       if (rel + 1 < relend
8882                           && (rel[1].r_info
8883                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8884                           && rel[1].r_offset == rel->r_offset + 8)
8885                         {
8886                           if (ok_tprel)
8887                             /* GD -> LE */
8888                             tls_set = TLS_EXPLICIT | TLS_GD;
8889                           else
8890                             /* GD -> IE */
8891                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8892                           tls_clear = TLS_GD;
8893                         }
8894                       else
8895                         {
8896                           if (!is_local)
8897                             continue;
8898
8899                           /* LD -> LE */
8900                           tls_set = TLS_EXPLICIT;
8901                           tls_clear = TLS_LD;
8902                         }
8903                       break;
8904
8905                     default:
8906                       continue;
8907                     }
8908
8909                   if (pass == 0)
8910                     {
8911                       if (!expecting_tls_get_addr
8912                           || !sec->has_tls_get_addr_call)
8913                         continue;
8914
8915                       if (rel + 1 < relend
8916                           && branch_reloc_hash_match (ibfd, rel + 1,
8917                                                       htab->tls_get_addr,
8918                                                       htab->tls_get_addr_fd))
8919                         {
8920                           if (expecting_tls_get_addr == 2)
8921                             {
8922                               /* Check for toc tls entries.  */
8923                               unsigned char *toc_tls;
8924                               int retval;
8925
8926                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8927                                                      &locsyms,
8928                                                      rel, ibfd);
8929                               if (retval == 0)
8930                                 goto err_free_rel;
8931                               if (toc_tls != NULL)
8932                                 {
8933                                   if ((*toc_tls & TLS_TLS) != 0
8934                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8935                                     found_tls_get_addr_arg = 1;
8936                                   if (retval > 1)
8937                                     toc_ref[toc_ref_index] = 1;
8938                                 }
8939                             }
8940                           continue;
8941                         }
8942
8943                       /* Uh oh, we didn't find the expected call.  We
8944                          could just mark this symbol to exclude it
8945                          from tls optimization but it's safer to skip
8946                          the entire optimization.  */
8947                       /* xgettext:c-format */
8948                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8949                                                 "TLS optimization disabled\n"),
8950                                               ibfd, sec, rel->r_offset);
8951                       ret = TRUE;
8952                       goto err_free_rel;
8953                     }
8954
8955                   /* If we don't have old-style __tls_get_addr calls
8956                      without TLSGD/TLSLD marker relocs, and we haven't
8957                      found a new-style __tls_get_addr call with a
8958                      marker for this symbol, then we either have a
8959                      broken object file or an -mlongcall style
8960                      indirect call to __tls_get_addr without a marker.
8961                      Disable optimization in this case.  */
8962                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8963                       && (tls_set & TLS_EXPLICIT) == 0
8964                       && !sec->has_tls_get_addr_call
8965                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
8966                           != (TLS_TLS | TLS_MARK)))
8967                     continue;
8968
8969                   if (expecting_tls_get_addr)
8970                     {
8971                       struct plt_entry *ent = NULL;
8972
8973                       if (htab->tls_get_addr != NULL)
8974                         for (ent = htab->tls_get_addr->elf.plt.plist;
8975                              ent != NULL;
8976                              ent = ent->next)
8977                           if (ent->addend == 0)
8978                             break;
8979
8980                       if (ent == NULL && htab->tls_get_addr_fd != NULL)
8981                         for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8982                              ent != NULL;
8983                              ent = ent->next)
8984                           if (ent->addend == 0)
8985                             break;
8986
8987                       if (ent != NULL
8988                           && ent->plt.refcount > 0)
8989                         ent->plt.refcount -= 1;
8990                     }
8991
8992                   if (tls_clear == 0)
8993                     continue;
8994
8995                   if ((tls_set & TLS_EXPLICIT) == 0)
8996                     {
8997                       struct got_entry *ent;
8998
8999                       /* Adjust got entry for this reloc.  */
9000                       if (h != NULL)
9001                         ent = h->got.glist;
9002                       else
9003                         ent = elf_local_got_ents (ibfd)[r_symndx];
9004
9005                       for (; ent != NULL; ent = ent->next)
9006                         if (ent->addend == rel->r_addend
9007                             && ent->owner == ibfd
9008                             && ent->tls_type == tls_type)
9009                           break;
9010                       if (ent == NULL)
9011                         abort ();
9012
9013                       if (tls_set == 0)
9014                         {
9015                           /* We managed to get rid of a got entry.  */
9016                           if (ent->got.refcount > 0)
9017                             ent->got.refcount -= 1;
9018                         }
9019                     }
9020                   else
9021                     {
9022                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
9023                          we'll lose one or two dyn relocs.  */
9024                       if (!dec_dynrel_count (rel->r_info, sec, info,
9025                                              NULL, h, sym))
9026                         return FALSE;
9027
9028                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
9029                         {
9030                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9031                                                  NULL, h, sym))
9032                             return FALSE;
9033                         }
9034                     }
9035
9036                   *tls_mask |= tls_set;
9037                   *tls_mask &= ~tls_clear;
9038                 }
9039
9040               if (elf_section_data (sec)->relocs != relstart)
9041                 free (relstart);
9042             }
9043
9044         if (locsyms != NULL
9045             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9046           {
9047             if (!info->keep_memory)
9048               free (locsyms);
9049             else
9050               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9051           }
9052       }
9053
9054   if (toc_ref != NULL)
9055     free (toc_ref);
9056   htab->do_tls_opt = 1;
9057   return TRUE;
9058 }
9059
9060 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9061    the values of any global symbols in a toc section that has been
9062    edited.  Globals in toc sections should be a rarity, so this function
9063    sets a flag if any are found in toc sections other than the one just
9064    edited, so that further hash table traversals can be avoided.  */
9065
9066 struct adjust_toc_info
9067 {
9068   asection *toc;
9069   unsigned long *skip;
9070   bfd_boolean global_toc_syms;
9071 };
9072
9073 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9074
9075 static bfd_boolean
9076 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9077 {
9078   struct ppc_link_hash_entry *eh;
9079   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9080   unsigned long i;
9081
9082   if (h->root.type != bfd_link_hash_defined
9083       && h->root.type != bfd_link_hash_defweak)
9084     return TRUE;
9085
9086   eh = (struct ppc_link_hash_entry *) h;
9087   if (eh->adjust_done)
9088     return TRUE;
9089
9090   if (eh->elf.root.u.def.section == toc_inf->toc)
9091     {
9092       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9093         i = toc_inf->toc->rawsize >> 3;
9094       else
9095         i = eh->elf.root.u.def.value >> 3;
9096
9097       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9098         {
9099           _bfd_error_handler
9100             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9101           do
9102             ++i;
9103           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9104           eh->elf.root.u.def.value = (bfd_vma) i << 3;
9105         }
9106
9107       eh->elf.root.u.def.value -= toc_inf->skip[i];
9108       eh->adjust_done = 1;
9109     }
9110   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9111     toc_inf->global_toc_syms = TRUE;
9112
9113   return TRUE;
9114 }
9115
9116 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9117    on a _LO variety toc/got reloc.  */
9118
9119 static bfd_boolean
9120 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9121 {
9122   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9123           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9124           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9125           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9126           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9127           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9128           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9129           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9130           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9131           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9132           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9133           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9134           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9135           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9136           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9137           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9138           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9139               /* Exclude lfqu by testing reloc.  If relocs are ever
9140                  defined for the reduced D field in psq_lu then those
9141                  will need testing too.  */
9142               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9143           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9144               && (insn & 1) == 0)
9145           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9146           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9147               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
9148               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9149           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9150               && (insn & 1) == 0));
9151 }
9152
9153 /* Examine all relocs referencing .toc sections in order to remove
9154    unused .toc entries.  */
9155
9156 bfd_boolean
9157 ppc64_elf_edit_toc (struct bfd_link_info *info)
9158 {
9159   bfd *ibfd;
9160   struct adjust_toc_info toc_inf;
9161   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9162
9163   htab->do_toc_opt = 1;
9164   toc_inf.global_toc_syms = TRUE;
9165   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9166     {
9167       asection *toc, *sec;
9168       Elf_Internal_Shdr *symtab_hdr;
9169       Elf_Internal_Sym *local_syms;
9170       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9171       unsigned long *skip, *drop;
9172       unsigned char *used;
9173       unsigned char *keep, last, some_unused;
9174
9175       if (!is_ppc64_elf (ibfd))
9176         continue;
9177
9178       toc = bfd_get_section_by_name (ibfd, ".toc");
9179       if (toc == NULL
9180           || toc->size == 0
9181           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9182           || discarded_section (toc))
9183         continue;
9184
9185       toc_relocs = NULL;
9186       local_syms = NULL;
9187       symtab_hdr = &elf_symtab_hdr (ibfd);
9188
9189       /* Look at sections dropped from the final link.  */
9190       skip = NULL;
9191       relstart = NULL;
9192       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9193         {
9194           if (sec->reloc_count == 0
9195               || !discarded_section (sec)
9196               || get_opd_info (sec)
9197               || (sec->flags & SEC_ALLOC) == 0
9198               || (sec->flags & SEC_DEBUGGING) != 0)
9199             continue;
9200
9201           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9202           if (relstart == NULL)
9203             goto error_ret;
9204
9205           /* Run through the relocs to see which toc entries might be
9206              unused.  */
9207           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9208             {
9209               enum elf_ppc64_reloc_type r_type;
9210               unsigned long r_symndx;
9211               asection *sym_sec;
9212               struct elf_link_hash_entry *h;
9213               Elf_Internal_Sym *sym;
9214               bfd_vma val;
9215
9216               r_type = ELF64_R_TYPE (rel->r_info);
9217               switch (r_type)
9218                 {
9219                 default:
9220                   continue;
9221
9222                 case R_PPC64_TOC16:
9223                 case R_PPC64_TOC16_LO:
9224                 case R_PPC64_TOC16_HI:
9225                 case R_PPC64_TOC16_HA:
9226                 case R_PPC64_TOC16_DS:
9227                 case R_PPC64_TOC16_LO_DS:
9228                   break;
9229                 }
9230
9231               r_symndx = ELF64_R_SYM (rel->r_info);
9232               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9233                               r_symndx, ibfd))
9234                 goto error_ret;
9235
9236               if (sym_sec != toc)
9237                 continue;
9238
9239               if (h != NULL)
9240                 val = h->root.u.def.value;
9241               else
9242                 val = sym->st_value;
9243               val += rel->r_addend;
9244
9245               if (val >= toc->size)
9246                 continue;
9247
9248               /* Anything in the toc ought to be aligned to 8 bytes.
9249                  If not, don't mark as unused.  */
9250               if (val & 7)
9251                 continue;
9252
9253               if (skip == NULL)
9254                 {
9255                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9256                   if (skip == NULL)
9257                     goto error_ret;
9258                 }
9259
9260               skip[val >> 3] = ref_from_discarded;
9261             }
9262
9263           if (elf_section_data (sec)->relocs != relstart)
9264             free (relstart);
9265         }
9266
9267       /* For largetoc loads of address constants, we can convert
9268          .  addis rx,2,addr@got@ha
9269          .  ld ry,addr@got@l(rx)
9270          to
9271          .  addis rx,2,addr@toc@ha
9272          .  addi ry,rx,addr@toc@l
9273          when addr is within 2G of the toc pointer.  This then means
9274          that the word storing "addr" in the toc is no longer needed.  */
9275
9276       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9277           && toc->output_section->rawsize < (bfd_vma) 1 << 31
9278           && toc->reloc_count != 0)
9279         {
9280           /* Read toc relocs.  */
9281           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9282                                                   info->keep_memory);
9283           if (toc_relocs == NULL)
9284             goto error_ret;
9285
9286           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9287             {
9288               enum elf_ppc64_reloc_type r_type;
9289               unsigned long r_symndx;
9290               asection *sym_sec;
9291               struct elf_link_hash_entry *h;
9292               Elf_Internal_Sym *sym;
9293               bfd_vma val, addr;
9294
9295               r_type = ELF64_R_TYPE (rel->r_info);
9296               if (r_type != R_PPC64_ADDR64)
9297                 continue;
9298
9299               r_symndx = ELF64_R_SYM (rel->r_info);
9300               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9301                               r_symndx, ibfd))
9302                 goto error_ret;
9303
9304               if (sym_sec == NULL
9305                   || sym_sec->output_section == NULL
9306                   || discarded_section (sym_sec))
9307                 continue;
9308
9309               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9310                 continue;
9311
9312               if (h != NULL)
9313                 {
9314                   if (h->type == STT_GNU_IFUNC)
9315                     continue;
9316                   val = h->root.u.def.value;
9317                 }
9318               else
9319                 {
9320                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9321                     continue;
9322                   val = sym->st_value;
9323                 }
9324               val += rel->r_addend;
9325               val += sym_sec->output_section->vma + sym_sec->output_offset;
9326
9327               /* We don't yet know the exact toc pointer value, but we
9328                  know it will be somewhere in the toc section.  Don't
9329                  optimize if the difference from any possible toc
9330                  pointer is outside [ff..f80008000, 7fff7fff].  */
9331               addr = toc->output_section->vma + TOC_BASE_OFF;
9332               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9333                 continue;
9334
9335               addr = toc->output_section->vma + toc->output_section->rawsize;
9336               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9337                 continue;
9338
9339               if (skip == NULL)
9340                 {
9341                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9342                   if (skip == NULL)
9343                     goto error_ret;
9344                 }
9345
9346               skip[rel->r_offset >> 3]
9347                 |= can_optimize | ((rel - toc_relocs) << 2);
9348             }
9349         }
9350
9351       if (skip == NULL)
9352         continue;
9353
9354       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9355       if (used == NULL)
9356         {
9357         error_ret:
9358           if (local_syms != NULL
9359               && symtab_hdr->contents != (unsigned char *) local_syms)
9360             free (local_syms);
9361           if (sec != NULL
9362               && relstart != NULL
9363               && elf_section_data (sec)->relocs != relstart)
9364             free (relstart);
9365           if (toc_relocs != NULL
9366               && elf_section_data (toc)->relocs != toc_relocs)
9367             free (toc_relocs);
9368           if (skip != NULL)
9369             free (skip);
9370           return FALSE;
9371         }
9372
9373       /* Now check all kept sections that might reference the toc.
9374          Check the toc itself last.  */
9375       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9376                   : ibfd->sections);
9377            sec != NULL;
9378            sec = (sec == toc ? NULL
9379                   : sec->next == NULL ? toc
9380                   : sec->next == toc && toc->next ? toc->next
9381                   : sec->next))
9382         {
9383           int repeat;
9384
9385           if (sec->reloc_count == 0
9386               || discarded_section (sec)
9387               || get_opd_info (sec)
9388               || (sec->flags & SEC_ALLOC) == 0
9389               || (sec->flags & SEC_DEBUGGING) != 0)
9390             continue;
9391
9392           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9393                                                 info->keep_memory);
9394           if (relstart == NULL)
9395             {
9396               free (used);
9397               goto error_ret;
9398             }
9399
9400           /* Mark toc entries referenced as used.  */
9401           do
9402             {
9403               repeat = 0;
9404               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9405                 {
9406                   enum elf_ppc64_reloc_type r_type;
9407                   unsigned long r_symndx;
9408                   asection *sym_sec;
9409                   struct elf_link_hash_entry *h;
9410                   Elf_Internal_Sym *sym;
9411                   bfd_vma val;
9412                   enum {no_check, check_lo, check_ha} insn_check;
9413
9414                   r_type = ELF64_R_TYPE (rel->r_info);
9415                   switch (r_type)
9416                     {
9417                     default:
9418                       insn_check = no_check;
9419                       break;
9420
9421                     case R_PPC64_GOT_TLSLD16_HA:
9422                     case R_PPC64_GOT_TLSGD16_HA:
9423                     case R_PPC64_GOT_TPREL16_HA:
9424                     case R_PPC64_GOT_DTPREL16_HA:
9425                     case R_PPC64_GOT16_HA:
9426                     case R_PPC64_TOC16_HA:
9427                       insn_check = check_ha;
9428                       break;
9429
9430                     case R_PPC64_GOT_TLSLD16_LO:
9431                     case R_PPC64_GOT_TLSGD16_LO:
9432                     case R_PPC64_GOT_TPREL16_LO_DS:
9433                     case R_PPC64_GOT_DTPREL16_LO_DS:
9434                     case R_PPC64_GOT16_LO:
9435                     case R_PPC64_GOT16_LO_DS:
9436                     case R_PPC64_TOC16_LO:
9437                     case R_PPC64_TOC16_LO_DS:
9438                       insn_check = check_lo;
9439                       break;
9440                     }
9441
9442                   if (insn_check != no_check)
9443                     {
9444                       bfd_vma off = rel->r_offset & ~3;
9445                       unsigned char buf[4];
9446                       unsigned int insn;
9447
9448                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9449                         {
9450                           free (used);
9451                           goto error_ret;
9452                         }
9453                       insn = bfd_get_32 (ibfd, buf);
9454                       if (insn_check == check_lo
9455                           ? !ok_lo_toc_insn (insn, r_type)
9456                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9457                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9458                         {
9459                           char str[12];
9460
9461                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9462                           sprintf (str, "%#08x", insn);
9463                           info->callbacks->einfo
9464                             /* xgettext:c-format */
9465                             (_("%H: toc optimization is not supported for"
9466                                " %s instruction\n"),
9467                              ibfd, sec, rel->r_offset & ~3, str);
9468                         }
9469                     }
9470
9471                   switch (r_type)
9472                     {
9473                     case R_PPC64_TOC16:
9474                     case R_PPC64_TOC16_LO:
9475                     case R_PPC64_TOC16_HI:
9476                     case R_PPC64_TOC16_HA:
9477                     case R_PPC64_TOC16_DS:
9478                     case R_PPC64_TOC16_LO_DS:
9479                       /* In case we're taking addresses of toc entries.  */
9480                     case R_PPC64_ADDR64:
9481                       break;
9482
9483                     default:
9484                       continue;
9485                     }
9486
9487                   r_symndx = ELF64_R_SYM (rel->r_info);
9488                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9489                                   r_symndx, ibfd))
9490                     {
9491                       free (used);
9492                       goto error_ret;
9493                     }
9494
9495                   if (sym_sec != toc)
9496                     continue;
9497
9498                   if (h != NULL)
9499                     val = h->root.u.def.value;
9500                   else
9501                     val = sym->st_value;
9502                   val += rel->r_addend;
9503
9504                   if (val >= toc->size)
9505                     continue;
9506
9507                   if ((skip[val >> 3] & can_optimize) != 0)
9508                     {
9509                       bfd_vma off;
9510                       unsigned char opc;
9511
9512                       switch (r_type)
9513                         {
9514                         case R_PPC64_TOC16_HA:
9515                           break;
9516
9517                         case R_PPC64_TOC16_LO_DS:
9518                           off = rel->r_offset;
9519                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9520                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9521                                                          off, 1))
9522                             {
9523                               free (used);
9524                               goto error_ret;
9525                             }
9526                           if ((opc & (0x3f << 2)) == (58u << 2))
9527                             break;
9528                           /* Fall through.  */
9529
9530                         default:
9531                           /* Wrong sort of reloc, or not a ld.  We may
9532                              as well clear ref_from_discarded too.  */
9533                           skip[val >> 3] = 0;
9534                         }
9535                     }
9536
9537                   if (sec != toc)
9538                     used[val >> 3] = 1;
9539                   /* For the toc section, we only mark as used if this
9540                      entry itself isn't unused.  */
9541                   else if ((used[rel->r_offset >> 3]
9542                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9543                            && !used[val >> 3])
9544                     {
9545                       /* Do all the relocs again, to catch reference
9546                          chains.  */
9547                       repeat = 1;
9548                       used[val >> 3] = 1;
9549                     }
9550                 }
9551             }
9552           while (repeat);
9553
9554           if (elf_section_data (sec)->relocs != relstart)
9555             free (relstart);
9556         }
9557
9558       /* Merge the used and skip arrays.  Assume that TOC
9559          doublewords not appearing as either used or unused belong
9560          to an entry more than one doubleword in size.  */
9561       for (drop = skip, keep = used, last = 0, some_unused = 0;
9562            drop < skip + (toc->size + 7) / 8;
9563            ++drop, ++keep)
9564         {
9565           if (*keep)
9566             {
9567               *drop &= ~ref_from_discarded;
9568               if ((*drop & can_optimize) != 0)
9569                 some_unused = 1;
9570               last = 0;
9571             }
9572           else if ((*drop & ref_from_discarded) != 0)
9573             {
9574               some_unused = 1;
9575               last = ref_from_discarded;
9576             }
9577           else
9578             *drop = last;
9579         }
9580
9581       free (used);
9582
9583       if (some_unused)
9584         {
9585           bfd_byte *contents, *src;
9586           unsigned long off;
9587           Elf_Internal_Sym *sym;
9588           bfd_boolean local_toc_syms = FALSE;
9589
9590           /* Shuffle the toc contents, and at the same time convert the
9591              skip array from booleans into offsets.  */
9592           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9593             goto error_ret;
9594
9595           elf_section_data (toc)->this_hdr.contents = contents;
9596
9597           for (src = contents, off = 0, drop = skip;
9598                src < contents + toc->size;
9599                src += 8, ++drop)
9600             {
9601               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9602                 off += 8;
9603               else if (off != 0)
9604                 {
9605                   *drop = off;
9606                   memcpy (src - off, src, 8);
9607                 }
9608             }
9609           *drop = off;
9610           toc->rawsize = toc->size;
9611           toc->size = src - contents - off;
9612
9613           /* Adjust addends for relocs against the toc section sym,
9614              and optimize any accesses we can.  */
9615           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9616             {
9617               if (sec->reloc_count == 0
9618                   || discarded_section (sec))
9619                 continue;
9620
9621               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9622                                                     info->keep_memory);
9623               if (relstart == NULL)
9624                 goto error_ret;
9625
9626               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9627                 {
9628                   enum elf_ppc64_reloc_type r_type;
9629                   unsigned long r_symndx;
9630                   asection *sym_sec;
9631                   struct elf_link_hash_entry *h;
9632                   bfd_vma val;
9633
9634                   r_type = ELF64_R_TYPE (rel->r_info);
9635                   switch (r_type)
9636                     {
9637                     default:
9638                       continue;
9639
9640                     case R_PPC64_TOC16:
9641                     case R_PPC64_TOC16_LO:
9642                     case R_PPC64_TOC16_HI:
9643                     case R_PPC64_TOC16_HA:
9644                     case R_PPC64_TOC16_DS:
9645                     case R_PPC64_TOC16_LO_DS:
9646                     case R_PPC64_ADDR64:
9647                       break;
9648                     }
9649
9650                   r_symndx = ELF64_R_SYM (rel->r_info);
9651                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9652                                   r_symndx, ibfd))
9653                     goto error_ret;
9654
9655                   if (sym_sec != toc)
9656                     continue;
9657
9658                   if (h != NULL)
9659                     val = h->root.u.def.value;
9660                   else
9661                     {
9662                       val = sym->st_value;
9663                       if (val != 0)
9664                         local_toc_syms = TRUE;
9665                     }
9666
9667                   val += rel->r_addend;
9668
9669                   if (val > toc->rawsize)
9670                     val = toc->rawsize;
9671                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9672                     continue;
9673                   else if ((skip[val >> 3] & can_optimize) != 0)
9674                     {
9675                       Elf_Internal_Rela *tocrel
9676                         = toc_relocs + (skip[val >> 3] >> 2);
9677                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9678
9679                       switch (r_type)
9680                         {
9681                         case R_PPC64_TOC16_HA:
9682                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9683                           break;
9684
9685                         case R_PPC64_TOC16_LO_DS:
9686                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9687                           break;
9688
9689                         default:
9690                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9691                             ppc_howto_init ();
9692                           info->callbacks->einfo
9693                             /* xgettext:c-format */
9694                             (_("%H: %s references "
9695                                "optimized away TOC entry\n"),
9696                              ibfd, sec, rel->r_offset,
9697                              ppc64_elf_howto_table[r_type]->name);
9698                           bfd_set_error (bfd_error_bad_value);
9699                           goto error_ret;
9700                         }
9701                       rel->r_addend = tocrel->r_addend;
9702                       elf_section_data (sec)->relocs = relstart;
9703                       continue;
9704                     }
9705
9706                   if (h != NULL || sym->st_value != 0)
9707                     continue;
9708
9709                   rel->r_addend -= skip[val >> 3];
9710                   elf_section_data (sec)->relocs = relstart;
9711                 }
9712
9713               if (elf_section_data (sec)->relocs != relstart)
9714                 free (relstart);
9715             }
9716
9717           /* We shouldn't have local or global symbols defined in the TOC,
9718              but handle them anyway.  */
9719           if (local_syms != NULL)
9720             for (sym = local_syms;
9721                  sym < local_syms + symtab_hdr->sh_info;
9722                  ++sym)
9723               if (sym->st_value != 0
9724                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9725                 {
9726                   unsigned long i;
9727
9728                   if (sym->st_value > toc->rawsize)
9729                     i = toc->rawsize >> 3;
9730                   else
9731                     i = sym->st_value >> 3;
9732
9733                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9734                     {
9735                       if (local_toc_syms)
9736                         _bfd_error_handler
9737                           (_("%s defined on removed toc entry"),
9738                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9739                       do
9740                         ++i;
9741                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9742                       sym->st_value = (bfd_vma) i << 3;
9743                     }
9744
9745                   sym->st_value -= skip[i];
9746                   symtab_hdr->contents = (unsigned char *) local_syms;
9747                 }
9748
9749           /* Adjust any global syms defined in this toc input section.  */
9750           if (toc_inf.global_toc_syms)
9751             {
9752               toc_inf.toc = toc;
9753               toc_inf.skip = skip;
9754               toc_inf.global_toc_syms = FALSE;
9755               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9756                                       &toc_inf);
9757             }
9758
9759           if (toc->reloc_count != 0)
9760             {
9761               Elf_Internal_Shdr *rel_hdr;
9762               Elf_Internal_Rela *wrel;
9763               bfd_size_type sz;
9764
9765               /* Remove unused toc relocs, and adjust those we keep.  */
9766               if (toc_relocs == NULL)
9767                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9768                                                         info->keep_memory);
9769               if (toc_relocs == NULL)
9770                 goto error_ret;
9771
9772               wrel = toc_relocs;
9773               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9774                 if ((skip[rel->r_offset >> 3]
9775                      & (ref_from_discarded | can_optimize)) == 0)
9776                   {
9777                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9778                     wrel->r_info = rel->r_info;
9779                     wrel->r_addend = rel->r_addend;
9780                     ++wrel;
9781                   }
9782                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9783                                             &local_syms, NULL, NULL))
9784                   goto error_ret;
9785
9786               elf_section_data (toc)->relocs = toc_relocs;
9787               toc->reloc_count = wrel - toc_relocs;
9788               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9789               sz = rel_hdr->sh_entsize;
9790               rel_hdr->sh_size = toc->reloc_count * sz;
9791             }
9792         }
9793       else if (toc_relocs != NULL
9794                && elf_section_data (toc)->relocs != toc_relocs)
9795         free (toc_relocs);
9796
9797       if (local_syms != NULL
9798           && symtab_hdr->contents != (unsigned char *) local_syms)
9799         {
9800           if (!info->keep_memory)
9801             free (local_syms);
9802           else
9803             symtab_hdr->contents = (unsigned char *) local_syms;
9804         }
9805       free (skip);
9806     }
9807
9808   return TRUE;
9809 }
9810
9811 /* Return true iff input section I references the TOC using
9812    instructions limited to +/-32k offsets.  */
9813
9814 bfd_boolean
9815 ppc64_elf_has_small_toc_reloc (asection *i)
9816 {
9817   return (is_ppc64_elf (i->owner)
9818           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9819 }
9820
9821 /* Allocate space for one GOT entry.  */
9822
9823 static void
9824 allocate_got (struct elf_link_hash_entry *h,
9825               struct bfd_link_info *info,
9826               struct got_entry *gent)
9827 {
9828   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9829   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9830   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9831                  ? 16 : 8);
9832   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9833                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9834   asection *got = ppc64_elf_tdata (gent->owner)->got;
9835
9836   gent->got.offset = got->size;
9837   got->size += entsize;
9838
9839   if (h->type == STT_GNU_IFUNC)
9840     {
9841       htab->elf.irelplt->size += rentsize;
9842       htab->got_reli_size += rentsize;
9843     }
9844   else if (((bfd_link_pic (info)
9845              && !((gent->tls_type & TLS_TPREL) != 0
9846                   && bfd_link_executable (info)
9847                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9848             || (htab->elf.dynamic_sections_created
9849                 && h->dynindx != -1
9850                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9851            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9852     {
9853       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9854       relgot->size += rentsize;
9855     }
9856 }
9857
9858 /* This function merges got entries in the same toc group.  */
9859
9860 static void
9861 merge_got_entries (struct got_entry **pent)
9862 {
9863   struct got_entry *ent, *ent2;
9864
9865   for (ent = *pent; ent != NULL; ent = ent->next)
9866     if (!ent->is_indirect)
9867       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9868         if (!ent2->is_indirect
9869             && ent2->addend == ent->addend
9870             && ent2->tls_type == ent->tls_type
9871             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9872           {
9873             ent2->is_indirect = TRUE;
9874             ent2->got.ent = ent;
9875           }
9876 }
9877
9878 /* If H is undefined, make it dynamic if that makes sense.  */
9879
9880 static bfd_boolean
9881 ensure_undef_dynamic (struct bfd_link_info *info,
9882                       struct elf_link_hash_entry *h)
9883 {
9884   struct elf_link_hash_table *htab = elf_hash_table (info);
9885
9886   if (htab->dynamic_sections_created
9887       && ((info->dynamic_undefined_weak != 0
9888            && h->root.type == bfd_link_hash_undefweak)
9889           || h->root.type == bfd_link_hash_undefined)
9890       && h->dynindx == -1
9891       && !h->forced_local
9892       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9893     return bfd_elf_link_record_dynamic_symbol (info, h);
9894   return TRUE;
9895 }
9896
9897 /* Allocate space in .plt, .got and associated reloc sections for
9898    dynamic relocs.  */
9899
9900 static bfd_boolean
9901 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9902 {
9903   struct bfd_link_info *info;
9904   struct ppc_link_hash_table *htab;
9905   asection *s;
9906   struct ppc_link_hash_entry *eh;
9907   struct got_entry **pgent, *gent;
9908
9909   if (h->root.type == bfd_link_hash_indirect)
9910     return TRUE;
9911
9912   info = (struct bfd_link_info *) inf;
9913   htab = ppc_hash_table (info);
9914   if (htab == NULL)
9915     return FALSE;
9916
9917   eh = (struct ppc_link_hash_entry *) h;
9918   /* Run through the TLS GD got entries first if we're changing them
9919      to TPREL.  */
9920   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9921     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9922       if (gent->got.refcount > 0
9923           && (gent->tls_type & TLS_GD) != 0)
9924         {
9925           /* This was a GD entry that has been converted to TPREL.  If
9926              there happens to be a TPREL entry we can use that one.  */
9927           struct got_entry *ent;
9928           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9929             if (ent->got.refcount > 0
9930                 && (ent->tls_type & TLS_TPREL) != 0
9931                 && ent->addend == gent->addend
9932                 && ent->owner == gent->owner)
9933               {
9934                 gent->got.refcount = 0;
9935                 break;
9936               }
9937
9938           /* If not, then we'll be using our own TPREL entry.  */
9939           if (gent->got.refcount != 0)
9940             gent->tls_type = TLS_TLS | TLS_TPREL;
9941         }
9942
9943   /* Remove any list entry that won't generate a word in the GOT before
9944      we call merge_got_entries.  Otherwise we risk merging to empty
9945      entries.  */
9946   pgent = &h->got.glist;
9947   while ((gent = *pgent) != NULL)
9948     if (gent->got.refcount > 0)
9949       {
9950         if ((gent->tls_type & TLS_LD) != 0
9951             && !h->def_dynamic)
9952           {
9953             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9954             *pgent = gent->next;
9955           }
9956         else
9957           pgent = &gent->next;
9958       }
9959     else
9960       *pgent = gent->next;
9961
9962   if (!htab->do_multi_toc)
9963     merge_got_entries (&h->got.glist);
9964
9965   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9966     if (!gent->is_indirect)
9967       {
9968         /* Make sure this symbol is output as a dynamic symbol.  */
9969         if (!ensure_undef_dynamic (info, h))
9970           return FALSE;
9971
9972         if (!is_ppc64_elf (gent->owner))
9973           abort ();
9974
9975         allocate_got (h, info, gent);
9976       }
9977
9978   /* If no dynamic sections we can't have dynamic relocs, except for
9979      IFUNCs which are handled even in static executables.  */
9980   if (!htab->elf.dynamic_sections_created
9981       && h->type != STT_GNU_IFUNC)
9982     eh->dyn_relocs = NULL;
9983
9984   /* Discard relocs on undefined symbols that must be local.  */
9985   else if (h->root.type == bfd_link_hash_undefined
9986            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9987     eh->dyn_relocs = NULL;
9988
9989   /* Also discard relocs on undefined weak syms with non-default
9990      visibility, or when dynamic_undefined_weak says so.  */
9991   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9992     eh->dyn_relocs = NULL;
9993
9994   if (eh->dyn_relocs != NULL)
9995     {
9996       struct elf_dyn_relocs *p, **pp;
9997
9998       /* In the shared -Bsymbolic case, discard space allocated for
9999          dynamic pc-relative relocs against symbols which turn out to
10000          be defined in regular objects.  For the normal shared case,
10001          discard space for relocs that have become local due to symbol
10002          visibility changes.  */
10003
10004       if (bfd_link_pic (info))
10005         {
10006           /* Relocs that use pc_count are those that appear on a call
10007              insn, or certain REL relocs (see must_be_dyn_reloc) that
10008              can be generated via assembly.  We want calls to
10009              protected symbols to resolve directly to the function
10010              rather than going via the plt.  If people want function
10011              pointer comparisons to work as expected then they should
10012              avoid writing weird assembly.  */
10013           if (SYMBOL_CALLS_LOCAL (info, h))
10014             {
10015               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10016                 {
10017                   p->count -= p->pc_count;
10018                   p->pc_count = 0;
10019                   if (p->count == 0)
10020                     *pp = p->next;
10021                   else
10022                     pp = &p->next;
10023                 }
10024             }
10025
10026           if (eh->dyn_relocs != NULL)
10027             {
10028               /* Make sure this symbol is output as a dynamic symbol.  */
10029               if (!ensure_undef_dynamic (info, h))
10030                 return FALSE;
10031             }
10032         }
10033       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10034         {
10035           /* For the non-pic case, discard space for relocs against
10036              symbols which turn out to need copy relocs or are not
10037              dynamic.  */
10038           if (h->dynamic_adjusted
10039               && !h->def_regular
10040               && !ELF_COMMON_DEF_P (h))
10041             {
10042               /* Make sure this symbol is output as a dynamic symbol.  */
10043               if (!ensure_undef_dynamic (info, h))
10044                 return FALSE;
10045
10046               if (h->dynindx == -1)
10047                 eh->dyn_relocs = NULL;
10048             }
10049           else
10050             eh->dyn_relocs = NULL;
10051         }
10052
10053       /* Finally, allocate space.  */
10054       for (p = eh->dyn_relocs; p != NULL; p = p->next)
10055         {
10056           asection *sreloc = elf_section_data (p->sec)->sreloc;
10057           if (eh->elf.type == STT_GNU_IFUNC)
10058             sreloc = htab->elf.irelplt;
10059           sreloc->size += p->count * sizeof (Elf64_External_Rela);
10060         }
10061     }
10062
10063   /* We might need a PLT entry when the symbol
10064      a) is dynamic, or
10065      b) is an ifunc, or
10066      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10067      d) has plt16 relocs and we are linking statically.  */
10068   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10069       || h->type == STT_GNU_IFUNC
10070       || (h->needs_plt && h->dynamic_adjusted)
10071       || (h->needs_plt
10072           && h->def_regular
10073           && !htab->elf.dynamic_sections_created
10074           && !htab->can_convert_all_inline_plt
10075           && (((struct ppc_link_hash_entry *) h)->tls_mask
10076               & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10077     {
10078       struct plt_entry *pent;
10079       bfd_boolean doneone = FALSE;
10080       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10081         if (pent->plt.refcount > 0)
10082           {
10083             if (!htab->elf.dynamic_sections_created
10084                 || h->dynindx == -1)
10085               {
10086                 if (h->type == STT_GNU_IFUNC)
10087                   {
10088                     s = htab->elf.iplt;
10089                     pent->plt.offset = s->size;
10090                     s->size += PLT_ENTRY_SIZE (htab);
10091                     s = htab->elf.irelplt;
10092                   }
10093                 else
10094                   {
10095                     s = htab->pltlocal;
10096                     pent->plt.offset = s->size;
10097                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10098                     s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10099                   }
10100               }
10101             else
10102               {
10103                 /* If this is the first .plt entry, make room for the special
10104                    first entry.  */
10105                 s = htab->elf.splt;
10106                 if (s->size == 0)
10107                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10108
10109                 pent->plt.offset = s->size;
10110
10111                 /* Make room for this entry.  */
10112                 s->size += PLT_ENTRY_SIZE (htab);
10113
10114                 /* Make room for the .glink code.  */
10115                 s = htab->glink;
10116                 if (s->size == 0)
10117                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
10118                 if (htab->opd_abi)
10119                   {
10120                     /* We need bigger stubs past index 32767.  */
10121                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10122                       s->size += 4;
10123                     s->size += 2*4;
10124                   }
10125                 else
10126                   s->size += 4;
10127
10128                 /* We also need to make an entry in the .rela.plt section.  */
10129                 s = htab->elf.srelplt;
10130               }
10131             if (s != NULL)
10132               s->size += sizeof (Elf64_External_Rela);
10133             doneone = TRUE;
10134           }
10135         else
10136           pent->plt.offset = (bfd_vma) -1;
10137       if (!doneone)
10138         {
10139           h->plt.plist = NULL;
10140           h->needs_plt = 0;
10141         }
10142     }
10143   else
10144     {
10145       h->plt.plist = NULL;
10146       h->needs_plt = 0;
10147     }
10148
10149   return TRUE;
10150 }
10151
10152 #define PPC_LO(v) ((v) & 0xffff)
10153 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10154 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10155
10156 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10157    to set up space for global entry stubs.  These are put in glink,
10158    after the branch table.  */
10159
10160 static bfd_boolean
10161 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10162 {
10163   struct bfd_link_info *info;
10164   struct ppc_link_hash_table *htab;
10165   struct plt_entry *pent;
10166   asection *s, *plt;
10167
10168   if (h->root.type == bfd_link_hash_indirect)
10169     return TRUE;
10170
10171   if (!h->pointer_equality_needed)
10172     return TRUE;
10173
10174   if (h->def_regular)
10175     return TRUE;
10176
10177   info = inf;
10178   htab = ppc_hash_table (info);
10179   if (htab == NULL)
10180     return FALSE;
10181
10182   s = htab->global_entry;
10183   plt = htab->elf.splt;
10184   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10185     if (pent->plt.offset != (bfd_vma) -1
10186         && pent->addend == 0)
10187       {
10188         /* For ELFv2, if this symbol is not defined in a regular file
10189            and we are not generating a shared library or pie, then we
10190            need to define the symbol in the executable on a call stub.
10191            This is to avoid text relocations.  */
10192         bfd_vma off, stub_align, stub_off, stub_size;
10193         unsigned int align_power;
10194
10195         stub_size = 16;
10196         stub_off = s->size;
10197         if (htab->params->plt_stub_align >= 0)
10198           align_power = htab->params->plt_stub_align;
10199         else
10200           align_power = -htab->params->plt_stub_align;
10201         /* Setting section alignment is delayed until we know it is
10202            non-empty.  Otherwise the .text output section will be
10203            aligned at least to plt_stub_align even when no global
10204            entry stubs are needed.  */
10205         if (s->alignment_power < align_power)
10206           s->alignment_power = align_power;
10207         stub_align = (bfd_vma) 1 << align_power;
10208         if (htab->params->plt_stub_align >= 0
10209             || ((((stub_off + stub_size - 1) & -stub_align)
10210                  - (stub_off & -stub_align))
10211                 > ((stub_size - 1) & -stub_align)))
10212           stub_off = (stub_off + stub_align - 1) & -stub_align;
10213         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10214         off -= stub_off + s->output_offset + s->output_section->vma;
10215         /* Note that for --plt-stub-align negative we have a possible
10216            dependency between stub offset and size.  Break that
10217            dependency by assuming the max stub size when calculating
10218            the stub offset.  */
10219         if (PPC_HA (off) == 0)
10220           stub_size -= 4;
10221         h->root.type = bfd_link_hash_defined;
10222         h->root.u.def.section = s;
10223         h->root.u.def.value = stub_off;
10224         s->size = stub_off + stub_size;
10225         break;
10226       }
10227   return TRUE;
10228 }
10229
10230 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10231    read-only sections.  */
10232
10233 static bfd_boolean
10234 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10235 {
10236   asection *sec;
10237
10238   if (h->root.type == bfd_link_hash_indirect)
10239     return TRUE;
10240
10241   sec = readonly_dynrelocs (h);
10242   if (sec != NULL)
10243     {
10244       struct bfd_link_info *info = (struct bfd_link_info *) inf;
10245
10246       info->flags |= DF_TEXTREL;
10247       info->callbacks->minfo
10248         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10249          sec->owner, h->root.root.string, sec);
10250
10251       /* Not an error, just cut short the traversal.  */
10252       return FALSE;
10253     }
10254   return TRUE;
10255 }
10256
10257 /* Set the sizes of the dynamic sections.  */
10258
10259 static bfd_boolean
10260 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10261                                  struct bfd_link_info *info)
10262 {
10263   struct ppc_link_hash_table *htab;
10264   bfd *dynobj;
10265   asection *s;
10266   bfd_boolean relocs;
10267   bfd *ibfd;
10268   struct got_entry *first_tlsld;
10269
10270   htab = ppc_hash_table (info);
10271   if (htab == NULL)
10272     return FALSE;
10273
10274   dynobj = htab->elf.dynobj;
10275   if (dynobj == NULL)
10276     abort ();
10277
10278   if (htab->elf.dynamic_sections_created)
10279     {
10280       /* Set the contents of the .interp section to the interpreter.  */
10281       if (bfd_link_executable (info) && !info->nointerp)
10282         {
10283           s = bfd_get_linker_section (dynobj, ".interp");
10284           if (s == NULL)
10285             abort ();
10286           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10287           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10288         }
10289     }
10290
10291   /* Set up .got offsets for local syms, and space for local dynamic
10292      relocs.  */
10293   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10294     {
10295       struct got_entry **lgot_ents;
10296       struct got_entry **end_lgot_ents;
10297       struct plt_entry **local_plt;
10298       struct plt_entry **end_local_plt;
10299       unsigned char *lgot_masks;
10300       bfd_size_type locsymcount;
10301       Elf_Internal_Shdr *symtab_hdr;
10302
10303       if (!is_ppc64_elf (ibfd))
10304         continue;
10305
10306       for (s = ibfd->sections; s != NULL; s = s->next)
10307         {
10308           struct ppc_dyn_relocs *p;
10309
10310           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10311             {
10312               if (!bfd_is_abs_section (p->sec)
10313                   && bfd_is_abs_section (p->sec->output_section))
10314                 {
10315                   /* Input section has been discarded, either because
10316                      it is a copy of a linkonce section or due to
10317                      linker script /DISCARD/, so we'll be discarding
10318                      the relocs too.  */
10319                 }
10320               else if (p->count != 0)
10321                 {
10322                   asection *srel = elf_section_data (p->sec)->sreloc;
10323                   if (p->ifunc)
10324                     srel = htab->elf.irelplt;
10325                   srel->size += p->count * sizeof (Elf64_External_Rela);
10326                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10327                     info->flags |= DF_TEXTREL;
10328                 }
10329             }
10330         }
10331
10332       lgot_ents = elf_local_got_ents (ibfd);
10333       if (!lgot_ents)
10334         continue;
10335
10336       symtab_hdr = &elf_symtab_hdr (ibfd);
10337       locsymcount = symtab_hdr->sh_info;
10338       end_lgot_ents = lgot_ents + locsymcount;
10339       local_plt = (struct plt_entry **) end_lgot_ents;
10340       end_local_plt = local_plt + locsymcount;
10341       lgot_masks = (unsigned char *) end_local_plt;
10342       s = ppc64_elf_tdata (ibfd)->got;
10343       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10344         {
10345           struct got_entry **pent, *ent;
10346
10347           pent = lgot_ents;
10348           while ((ent = *pent) != NULL)
10349             if (ent->got.refcount > 0)
10350               {
10351                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10352                   {
10353                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10354                     *pent = ent->next;
10355                   }
10356                 else
10357                   {
10358                     unsigned int ent_size = 8;
10359                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10360
10361                     ent->got.offset = s->size;
10362                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10363                       {
10364                         ent_size *= 2;
10365                         rel_size *= 2;
10366                       }
10367                     s->size += ent_size;
10368                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10369                       {
10370                         htab->elf.irelplt->size += rel_size;
10371                         htab->got_reli_size += rel_size;
10372                       }
10373                     else if (bfd_link_pic (info)
10374                              && !((ent->tls_type & TLS_TPREL) != 0
10375                                   && bfd_link_executable (info)))
10376                       {
10377                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10378                         srel->size += rel_size;
10379                       }
10380                     pent = &ent->next;
10381                   }
10382               }
10383             else
10384               *pent = ent->next;
10385         }
10386
10387       /* Allocate space for plt calls to local syms.  */
10388       lgot_masks = (unsigned char *) end_local_plt;
10389       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10390         {
10391           struct plt_entry *ent;
10392
10393           for (ent = *local_plt; ent != NULL; ent = ent->next)
10394             if (ent->plt.refcount > 0)
10395               {
10396                 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10397                   {
10398                     s = htab->elf.iplt;
10399                     ent->plt.offset = s->size;
10400                     s->size += PLT_ENTRY_SIZE (htab);
10401                     htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10402                   }
10403                 else if (htab->can_convert_all_inline_plt
10404                          || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10405                   ent->plt.offset = (bfd_vma) -1;
10406                 else
10407                   {
10408                     s = htab->pltlocal;
10409                     ent->plt.offset = s->size;
10410                     s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10411                     if (bfd_link_pic (info))
10412                       htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10413                   }
10414               }
10415             else
10416               ent->plt.offset = (bfd_vma) -1;
10417         }
10418     }
10419
10420   /* Allocate global sym .plt and .got entries, and space for global
10421      sym dynamic relocs.  */
10422   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10423
10424   if (!htab->opd_abi && !bfd_link_pic (info))
10425     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10426
10427   first_tlsld = NULL;
10428   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10429     {
10430       struct got_entry *ent;
10431
10432       if (!is_ppc64_elf (ibfd))
10433         continue;
10434
10435       ent = ppc64_tlsld_got (ibfd);
10436       if (ent->got.refcount > 0)
10437         {
10438           if (!htab->do_multi_toc && first_tlsld != NULL)
10439             {
10440               ent->is_indirect = TRUE;
10441               ent->got.ent = first_tlsld;
10442             }
10443           else
10444             {
10445               if (first_tlsld == NULL)
10446                 first_tlsld = ent;
10447               s = ppc64_elf_tdata (ibfd)->got;
10448               ent->got.offset = s->size;
10449               ent->owner = ibfd;
10450               s->size += 16;
10451               if (bfd_link_pic (info))
10452                 {
10453                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10454                   srel->size += sizeof (Elf64_External_Rela);
10455                 }
10456             }
10457         }
10458       else
10459         ent->got.offset = (bfd_vma) -1;
10460     }
10461
10462   /* We now have determined the sizes of the various dynamic sections.
10463      Allocate memory for them.  */
10464   relocs = FALSE;
10465   for (s = dynobj->sections; s != NULL; s = s->next)
10466     {
10467       if ((s->flags & SEC_LINKER_CREATED) == 0)
10468         continue;
10469
10470       if (s == htab->brlt || s == htab->relbrlt)
10471         /* These haven't been allocated yet;  don't strip.  */
10472         continue;
10473       else if (s == htab->elf.sgot
10474                || s == htab->elf.splt
10475                || s == htab->elf.iplt
10476                || s == htab->pltlocal
10477                || s == htab->glink
10478                || s == htab->global_entry
10479                || s == htab->elf.sdynbss
10480                || s == htab->elf.sdynrelro)
10481         {
10482           /* Strip this section if we don't need it; see the
10483              comment below.  */
10484         }
10485       else if (s == htab->glink_eh_frame)
10486         {
10487           if (!bfd_is_abs_section (s->output_section))
10488             /* Not sized yet.  */
10489             continue;
10490         }
10491       else if (CONST_STRNEQ (s->name, ".rela"))
10492         {
10493           if (s->size != 0)
10494             {
10495               if (s != htab->elf.srelplt)
10496                 relocs = TRUE;
10497
10498               /* We use the reloc_count field as a counter if we need
10499                  to copy relocs into the output file.  */
10500               s->reloc_count = 0;
10501             }
10502         }
10503       else
10504         {
10505           /* It's not one of our sections, so don't allocate space.  */
10506           continue;
10507         }
10508
10509       if (s->size == 0)
10510         {
10511           /* If we don't need this section, strip it from the
10512              output file.  This is mostly to handle .rela.bss and
10513              .rela.plt.  We must create both sections in
10514              create_dynamic_sections, because they must be created
10515              before the linker maps input sections to output
10516              sections.  The linker does that before
10517              adjust_dynamic_symbol is called, and it is that
10518              function which decides whether anything needs to go
10519              into these sections.  */
10520           s->flags |= SEC_EXCLUDE;
10521           continue;
10522         }
10523
10524       if (bfd_is_abs_section (s->output_section))
10525         _bfd_error_handler (_("warning: discarding dynamic section %s"),
10526                             s->name);
10527
10528       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10529         continue;
10530
10531       /* Allocate memory for the section contents.  We use bfd_zalloc
10532          here in case unused entries are not reclaimed before the
10533          section's contents are written out.  This should not happen,
10534          but this way if it does we get a R_PPC64_NONE reloc in .rela
10535          sections instead of garbage.
10536          We also rely on the section contents being zero when writing
10537          the GOT and .dynrelro.  */
10538       s->contents = bfd_zalloc (dynobj, s->size);
10539       if (s->contents == NULL)
10540         return FALSE;
10541     }
10542
10543   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10544     {
10545       if (!is_ppc64_elf (ibfd))
10546         continue;
10547
10548       s = ppc64_elf_tdata (ibfd)->got;
10549       if (s != NULL && s != htab->elf.sgot)
10550         {
10551           if (s->size == 0)
10552             s->flags |= SEC_EXCLUDE;
10553           else
10554             {
10555               s->contents = bfd_zalloc (ibfd, s->size);
10556               if (s->contents == NULL)
10557                 return FALSE;
10558             }
10559         }
10560       s = ppc64_elf_tdata (ibfd)->relgot;
10561       if (s != NULL)
10562         {
10563           if (s->size == 0)
10564             s->flags |= SEC_EXCLUDE;
10565           else
10566             {
10567               s->contents = bfd_zalloc (ibfd, s->size);
10568               if (s->contents == NULL)
10569                 return FALSE;
10570               relocs = TRUE;
10571               s->reloc_count = 0;
10572             }
10573         }
10574     }
10575
10576   if (htab->elf.dynamic_sections_created)
10577     {
10578       bfd_boolean tls_opt;
10579
10580       /* Add some entries to the .dynamic section.  We fill in the
10581          values later, in ppc64_elf_finish_dynamic_sections, but we
10582          must add the entries now so that we get the correct size for
10583          the .dynamic section.  The DT_DEBUG entry is filled in by the
10584          dynamic linker and used by the debugger.  */
10585 #define add_dynamic_entry(TAG, VAL) \
10586   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10587
10588       if (bfd_link_executable (info))
10589         {
10590           if (!add_dynamic_entry (DT_DEBUG, 0))
10591             return FALSE;
10592         }
10593
10594       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10595         {
10596           if (!add_dynamic_entry (DT_PLTGOT, 0)
10597               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10598               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10599               || !add_dynamic_entry (DT_JMPREL, 0)
10600               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10601             return FALSE;
10602         }
10603
10604       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10605         {
10606           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10607               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10608             return FALSE;
10609         }
10610
10611       tls_opt = (htab->params->tls_get_addr_opt
10612                  && htab->tls_get_addr_fd != NULL
10613                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10614       if (tls_opt || !htab->opd_abi)
10615         {
10616           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10617             return FALSE;
10618         }
10619
10620       if (relocs)
10621         {
10622           if (!add_dynamic_entry (DT_RELA, 0)
10623               || !add_dynamic_entry (DT_RELASZ, 0)
10624               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10625             return FALSE;
10626
10627           /* If any dynamic relocs apply to a read-only section,
10628              then we need a DT_TEXTREL entry.  */
10629           if ((info->flags & DF_TEXTREL) == 0)
10630             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10631
10632           if ((info->flags & DF_TEXTREL) != 0)
10633             {
10634               if (!add_dynamic_entry (DT_TEXTREL, 0))
10635                 return FALSE;
10636             }
10637         }
10638     }
10639 #undef add_dynamic_entry
10640
10641   return TRUE;
10642 }
10643
10644 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10645
10646 static bfd_boolean
10647 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10648 {
10649   if (h->plt.plist != NULL
10650       && !h->def_regular
10651       && !h->pointer_equality_needed)
10652     return FALSE;
10653
10654   return _bfd_elf_hash_symbol (h);
10655 }
10656
10657 /* Determine the type of stub needed, if any, for a call.  */
10658
10659 static inline enum ppc_stub_type
10660 ppc_type_of_stub (asection *input_sec,
10661                   const Elf_Internal_Rela *rel,
10662                   struct ppc_link_hash_entry **hash,
10663                   struct plt_entry **plt_ent,
10664                   bfd_vma destination,
10665                   unsigned long local_off)
10666 {
10667   struct ppc_link_hash_entry *h = *hash;
10668   bfd_vma location;
10669   bfd_vma branch_offset;
10670   bfd_vma max_branch_offset;
10671   enum elf_ppc64_reloc_type r_type;
10672
10673   if (h != NULL)
10674     {
10675       struct plt_entry *ent;
10676       struct ppc_link_hash_entry *fdh = h;
10677       if (h->oh != NULL
10678           && h->oh->is_func_descriptor)
10679         {
10680           fdh = ppc_follow_link (h->oh);
10681           *hash = fdh;
10682         }
10683
10684       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10685         if (ent->addend == rel->r_addend
10686             && ent->plt.offset != (bfd_vma) -1)
10687           {
10688             *plt_ent = ent;
10689             return ppc_stub_plt_call;
10690           }
10691
10692       /* Here, we know we don't have a plt entry.  If we don't have a
10693          either a defined function descriptor or a defined entry symbol
10694          in a regular object file, then it is pointless trying to make
10695          any other type of stub.  */
10696       if (!is_static_defined (&fdh->elf)
10697           && !is_static_defined (&h->elf))
10698         return ppc_stub_none;
10699     }
10700   else if (elf_local_got_ents (input_sec->owner) != NULL)
10701     {
10702       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10703       struct plt_entry **local_plt = (struct plt_entry **)
10704         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10705       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10706
10707       if (local_plt[r_symndx] != NULL)
10708         {
10709           struct plt_entry *ent;
10710
10711           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10712             if (ent->addend == rel->r_addend
10713                 && ent->plt.offset != (bfd_vma) -1)
10714               {
10715                 *plt_ent = ent;
10716                 return ppc_stub_plt_call;
10717               }
10718         }
10719     }
10720
10721   /* Determine where the call point is.  */
10722   location = (input_sec->output_offset
10723               + input_sec->output_section->vma
10724               + rel->r_offset);
10725
10726   branch_offset = destination - location;
10727   r_type = ELF64_R_TYPE (rel->r_info);
10728
10729   /* Determine if a long branch stub is needed.  */
10730   max_branch_offset = 1 << 25;
10731   if (r_type == R_PPC64_REL14
10732       || r_type == R_PPC64_REL14_BRTAKEN
10733       || r_type == R_PPC64_REL14_BRNTAKEN)
10734     max_branch_offset = 1 << 15;
10735
10736   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10737     /* We need a stub.  Figure out whether a long_branch or plt_branch
10738        is needed later.  */
10739     return ppc_stub_long_branch;
10740
10741   return ppc_stub_none;
10742 }
10743
10744 /* With power7 weakly ordered memory model, it is possible for ld.so
10745    to update a plt entry in one thread and have another thread see a
10746    stale zero toc entry.  To avoid this we need some sort of acquire
10747    barrier in the call stub.  One solution is to make the load of the
10748    toc word seem to appear to depend on the load of the function entry
10749    word.  Another solution is to test for r2 being zero, and branch to
10750    the appropriate glink entry if so.
10751
10752    .    fake dep barrier        compare
10753    .    ld 12,xxx(2)            ld 12,xxx(2)
10754    .    mtctr 12                mtctr 12
10755    .    xor 11,12,12            ld 2,xxx+8(2)
10756    .    add 2,2,11              cmpldi 2,0
10757    .    ld 2,xxx+8(2)           bnectr+
10758    .    bctr                    b <glink_entry>
10759
10760    The solution involving the compare turns out to be faster, so
10761    that's what we use unless the branch won't reach.  */
10762
10763 #define ALWAYS_USE_FAKE_DEP 0
10764 #define ALWAYS_EMIT_R2SAVE 0
10765
10766 static inline unsigned int
10767 plt_stub_size (struct ppc_link_hash_table *htab,
10768                struct ppc_stub_hash_entry *stub_entry,
10769                bfd_vma off)
10770 {
10771   unsigned size = 12;
10772
10773   if (ALWAYS_EMIT_R2SAVE
10774       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10775     size += 4;
10776   if (PPC_HA (off) != 0)
10777     size += 4;
10778   if (htab->opd_abi)
10779     {
10780       size += 4;
10781       if (htab->params->plt_static_chain)
10782         size += 4;
10783       if (htab->params->plt_thread_safe
10784           && htab->elf.dynamic_sections_created
10785           && stub_entry->h != NULL
10786           && stub_entry->h->elf.dynindx != -1)
10787         size += 8;
10788       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10789         size += 4;
10790     }
10791   if (stub_entry->h != NULL
10792       && (stub_entry->h == htab->tls_get_addr_fd
10793           || stub_entry->h == htab->tls_get_addr)
10794       && htab->params->tls_get_addr_opt)
10795     {
10796       size += 7 * 4;
10797       if (ALWAYS_EMIT_R2SAVE
10798           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10799         size += 6 * 4;
10800     }
10801   return size;
10802 }
10803
10804 /* Depending on the sign of plt_stub_align:
10805    If positive, return the padding to align to a 2**plt_stub_align
10806    boundary.
10807    If negative, if this stub would cross fewer 2**plt_stub_align
10808    boundaries if we align, then return the padding needed to do so.  */
10809
10810 static inline unsigned int
10811 plt_stub_pad (struct ppc_link_hash_table *htab,
10812               struct ppc_stub_hash_entry *stub_entry,
10813               bfd_vma plt_off)
10814 {
10815   int stub_align;
10816   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10817   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10818
10819   if (htab->params->plt_stub_align >= 0)
10820     {
10821       stub_align = 1 << htab->params->plt_stub_align;
10822       if ((stub_off & (stub_align - 1)) != 0)
10823         return stub_align - (stub_off & (stub_align - 1));
10824       return 0;
10825     }
10826
10827   stub_align = 1 << -htab->params->plt_stub_align;
10828   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10829       > ((stub_size - 1) & -stub_align))
10830     return stub_align - (stub_off & (stub_align - 1));
10831   return 0;
10832 }
10833
10834 /* Build a .plt call stub.  */
10835
10836 static inline bfd_byte *
10837 build_plt_stub (struct ppc_link_hash_table *htab,
10838                 struct ppc_stub_hash_entry *stub_entry,
10839                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10840 {
10841   bfd *obfd = htab->params->stub_bfd;
10842   bfd_boolean plt_load_toc = htab->opd_abi;
10843   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10844   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10845                                  && htab->elf.dynamic_sections_created
10846                                  && stub_entry->h != NULL
10847                                  && stub_entry->h->elf.dynindx != -1);
10848   bfd_boolean use_fake_dep = plt_thread_safe;
10849   bfd_vma cmp_branch_off = 0;
10850
10851   if (!ALWAYS_USE_FAKE_DEP
10852       && plt_load_toc
10853       && plt_thread_safe
10854       && !((stub_entry->h == htab->tls_get_addr_fd
10855             || stub_entry->h == htab->tls_get_addr)
10856            && htab->params->tls_get_addr_opt))
10857     {
10858       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10859       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10860                           / PLT_ENTRY_SIZE (htab));
10861       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10862       bfd_vma to, from;
10863
10864       if (pltindex > 32768)
10865         glinkoff += (pltindex - 32768) * 4;
10866       to = (glinkoff
10867             + htab->glink->output_offset
10868             + htab->glink->output_section->vma);
10869       from = (p - stub_entry->group->stub_sec->contents
10870               + 4 * (ALWAYS_EMIT_R2SAVE
10871                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10872               + 4 * (PPC_HA (offset) != 0)
10873               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10874                      != PPC_HA (offset))
10875               + 4 * (plt_static_chain != 0)
10876               + 20
10877               + stub_entry->group->stub_sec->output_offset
10878               + stub_entry->group->stub_sec->output_section->vma);
10879       cmp_branch_off = to - from;
10880       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10881     }
10882
10883   if (PPC_HA (offset) != 0)
10884     {
10885       if (r != NULL)
10886         {
10887           if (ALWAYS_EMIT_R2SAVE
10888               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10889             r[0].r_offset += 4;
10890           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10891           r[1].r_offset = r[0].r_offset + 4;
10892           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10893           r[1].r_addend = r[0].r_addend;
10894           if (plt_load_toc)
10895             {
10896               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10897                 {
10898                   r[2].r_offset = r[1].r_offset + 4;
10899                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10900                   r[2].r_addend = r[0].r_addend;
10901                 }
10902               else
10903                 {
10904                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10905                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10906                   r[2].r_addend = r[0].r_addend + 8;
10907                   if (plt_static_chain)
10908                     {
10909                       r[3].r_offset = r[2].r_offset + 4;
10910                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10911                       r[3].r_addend = r[0].r_addend + 16;
10912                     }
10913                 }
10914             }
10915         }
10916       if (ALWAYS_EMIT_R2SAVE
10917           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10918         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10919       if (plt_load_toc)
10920         {
10921           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10922           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10923         }
10924       else
10925         {
10926           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10927           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10928         }
10929       if (plt_load_toc
10930           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10931         {
10932           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10933           offset = 0;
10934         }
10935       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10936       if (plt_load_toc)
10937         {
10938           if (use_fake_dep)
10939             {
10940               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10941               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10942             }
10943           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10944           if (plt_static_chain)
10945             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10946         }
10947     }
10948   else
10949     {
10950       if (r != NULL)
10951         {
10952           if (ALWAYS_EMIT_R2SAVE
10953               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10954             r[0].r_offset += 4;
10955           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10956           if (plt_load_toc)
10957             {
10958               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10959                 {
10960                   r[1].r_offset = r[0].r_offset + 4;
10961                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10962                   r[1].r_addend = r[0].r_addend;
10963                 }
10964               else
10965                 {
10966                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10967                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10968                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10969                   if (plt_static_chain)
10970                     {
10971                       r[2].r_offset = r[1].r_offset + 4;
10972                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10973                       r[2].r_addend = r[0].r_addend + 8;
10974                     }
10975                 }
10976             }
10977         }
10978       if (ALWAYS_EMIT_R2SAVE
10979           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10980         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10981       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10982       if (plt_load_toc
10983           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10984         {
10985           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10986           offset = 0;
10987         }
10988       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10989       if (plt_load_toc)
10990         {
10991           if (use_fake_dep)
10992             {
10993               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10994               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10995             }
10996           if (plt_static_chain)
10997             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10998           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10999         }
11000     }
11001   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11002     {
11003       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
11004       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
11005       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11006     }
11007   else
11008     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
11009   return p;
11010 }
11011
11012 /* Build a special .plt call stub for __tls_get_addr.  */
11013
11014 #define LD_R11_0R3      0xe9630000
11015 #define LD_R12_0R3      0xe9830000
11016 #define MR_R0_R3        0x7c601b78
11017 #define CMPDI_R11_0     0x2c2b0000
11018 #define ADD_R3_R12_R13  0x7c6c6a14
11019 #define BEQLR           0x4d820020
11020 #define MR_R3_R0        0x7c030378
11021 #define STD_R11_0R1     0xf9610000
11022 #define BCTRL           0x4e800421
11023 #define LD_R11_0R1      0xe9610000
11024 #define MTLR_R11        0x7d6803a6
11025
11026 static inline bfd_byte *
11027 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11028                          struct ppc_stub_hash_entry *stub_entry,
11029                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11030 {
11031   bfd *obfd = htab->params->stub_bfd;
11032
11033   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
11034   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
11035   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
11036   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
11037   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
11038   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
11039   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
11040   if (r != NULL)
11041     r[0].r_offset += 7 * 4;
11042   if (!ALWAYS_EMIT_R2SAVE
11043       && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11044     return build_plt_stub (htab, stub_entry, p, offset, r);
11045
11046   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
11047   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11048
11049   if (r != NULL)
11050     r[0].r_offset += 2 * 4;
11051   p = build_plt_stub (htab, stub_entry, p, offset, r);
11052   bfd_put_32 (obfd, BCTRL, p - 4);
11053
11054   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
11055   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11056   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
11057   bfd_put_32 (obfd, BLR, p),                    p += 4;
11058
11059   return p;
11060 }
11061
11062 static Elf_Internal_Rela *
11063 get_relocs (asection *sec, int count)
11064 {
11065   Elf_Internal_Rela *relocs;
11066   struct bfd_elf_section_data *elfsec_data;
11067
11068   elfsec_data = elf_section_data (sec);
11069   relocs = elfsec_data->relocs;
11070   if (relocs == NULL)
11071     {
11072       bfd_size_type relsize;
11073       relsize = sec->reloc_count * sizeof (*relocs);
11074       relocs = bfd_alloc (sec->owner, relsize);
11075       if (relocs == NULL)
11076         return NULL;
11077       elfsec_data->relocs = relocs;
11078       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11079                                           sizeof (Elf_Internal_Shdr));
11080       if (elfsec_data->rela.hdr == NULL)
11081         return NULL;
11082       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11083                                         * sizeof (Elf64_External_Rela));
11084       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11085       sec->reloc_count = 0;
11086     }
11087   relocs += sec->reloc_count;
11088   sec->reloc_count += count;
11089   return relocs;
11090 }
11091
11092 static bfd_vma
11093 get_r2off (struct bfd_link_info *info,
11094            struct ppc_stub_hash_entry *stub_entry)
11095 {
11096   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11097   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11098
11099   if (r2off == 0)
11100     {
11101       /* Support linking -R objects.  Get the toc pointer from the
11102          opd entry.  */
11103       char buf[8];
11104       if (!htab->opd_abi)
11105         return r2off;
11106       asection *opd = stub_entry->h->elf.root.u.def.section;
11107       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11108
11109       if (strcmp (opd->name, ".opd") != 0
11110           || opd->reloc_count != 0)
11111         {
11112           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11113                                   stub_entry->h->elf.root.root.string);
11114           bfd_set_error (bfd_error_bad_value);
11115           return (bfd_vma) -1;
11116         }
11117       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11118         return (bfd_vma) -1;
11119       r2off = bfd_get_64 (opd->owner, buf);
11120       r2off -= elf_gp (info->output_bfd);
11121     }
11122   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11123   return r2off;
11124 }
11125
11126 static bfd_boolean
11127 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11128 {
11129   struct ppc_stub_hash_entry *stub_entry;
11130   struct ppc_branch_hash_entry *br_entry;
11131   struct bfd_link_info *info;
11132   struct ppc_link_hash_table *htab;
11133   bfd_byte *loc;
11134   bfd_byte *p;
11135   bfd_vma dest, off;
11136   Elf_Internal_Rela *r;
11137   asection *plt;
11138
11139   /* Massage our args to the form they really have.  */
11140   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11141   info = in_arg;
11142
11143   htab = ppc_hash_table (info);
11144   if (htab == NULL)
11145     return FALSE;
11146
11147   /* Make a note of the offset within the stubs for this entry.  */
11148   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11149   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11150
11151   htab->stub_count[stub_entry->stub_type - 1] += 1;
11152   switch (stub_entry->stub_type)
11153     {
11154     case ppc_stub_long_branch:
11155     case ppc_stub_long_branch_r2off:
11156       /* Branches are relative.  This is where we are going to.  */
11157       dest = (stub_entry->target_value
11158               + stub_entry->target_section->output_offset
11159               + stub_entry->target_section->output_section->vma);
11160       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11161       off = dest;
11162
11163       /* And this is where we are coming from.  */
11164       off -= (stub_entry->stub_offset
11165               + stub_entry->group->stub_sec->output_offset
11166               + stub_entry->group->stub_sec->output_section->vma);
11167
11168       p = loc;
11169       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11170         {
11171           bfd_vma r2off = get_r2off (info, stub_entry);
11172
11173           if (r2off == (bfd_vma) -1)
11174             {
11175               htab->stub_error = TRUE;
11176               return FALSE;
11177             }
11178           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11179           p += 4;
11180           if (PPC_HA (r2off) != 0)
11181             {
11182               bfd_put_32 (htab->params->stub_bfd,
11183                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11184               p += 4;
11185             }
11186           if (PPC_LO (r2off) != 0)
11187             {
11188               bfd_put_32 (htab->params->stub_bfd,
11189                           ADDI_R2_R2 | PPC_LO (r2off), p);
11190               p += 4;
11191             }
11192           off -= p - loc;
11193         }
11194       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11195       p += 4;
11196
11197       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11198         {
11199           _bfd_error_handler
11200             (_("long branch stub `%s' offset overflow"),
11201              stub_entry->root.string);
11202           htab->stub_error = TRUE;
11203           return FALSE;
11204         }
11205
11206       if (info->emitrelocations)
11207         {
11208           r = get_relocs (stub_entry->group->stub_sec, 1);
11209           if (r == NULL)
11210             return FALSE;
11211           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11212           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11213           r->r_addend = dest;
11214           if (stub_entry->h != NULL)
11215             {
11216               struct elf_link_hash_entry **hashes;
11217               unsigned long symndx;
11218               struct ppc_link_hash_entry *h;
11219
11220               hashes = elf_sym_hashes (htab->params->stub_bfd);
11221               if (hashes == NULL)
11222                 {
11223                   bfd_size_type hsize;
11224
11225                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11226                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11227                   if (hashes == NULL)
11228                     return FALSE;
11229                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
11230                   htab->stub_globals = 1;
11231                 }
11232               symndx = htab->stub_globals++;
11233               h = stub_entry->h;
11234               hashes[symndx] = &h->elf;
11235               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11236               if (h->oh != NULL && h->oh->is_func)
11237                 h = ppc_follow_link (h->oh);
11238               if (h->elf.root.u.def.section != stub_entry->target_section)
11239                 /* H is an opd symbol.  The addend must be zero.  */
11240                 r->r_addend = 0;
11241               else
11242                 {
11243                   off = (h->elf.root.u.def.value
11244                          + h->elf.root.u.def.section->output_offset
11245                          + h->elf.root.u.def.section->output_section->vma);
11246                   r->r_addend -= off;
11247                 }
11248             }
11249         }
11250       break;
11251
11252     case ppc_stub_plt_branch:
11253     case ppc_stub_plt_branch_r2off:
11254       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11255                                          stub_entry->root.string + 9,
11256                                          FALSE, FALSE);
11257       if (br_entry == NULL)
11258         {
11259           _bfd_error_handler (_("can't find branch stub `%s'"),
11260                               stub_entry->root.string);
11261           htab->stub_error = TRUE;
11262           return FALSE;
11263         }
11264
11265       dest = (stub_entry->target_value
11266               + stub_entry->target_section->output_offset
11267               + stub_entry->target_section->output_section->vma);
11268       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11269         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11270
11271       bfd_put_64 (htab->brlt->owner, dest,
11272                   htab->brlt->contents + br_entry->offset);
11273
11274       if (br_entry->iter == htab->stub_iteration)
11275         {
11276           br_entry->iter = 0;
11277
11278           if (htab->relbrlt != NULL)
11279             {
11280               /* Create a reloc for the branch lookup table entry.  */
11281               Elf_Internal_Rela rela;
11282               bfd_byte *rl;
11283
11284               rela.r_offset = (br_entry->offset
11285                                + htab->brlt->output_offset
11286                                + htab->brlt->output_section->vma);
11287               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11288               rela.r_addend = dest;
11289
11290               rl = htab->relbrlt->contents;
11291               rl += (htab->relbrlt->reloc_count++
11292                      * sizeof (Elf64_External_Rela));
11293               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11294             }
11295           else if (info->emitrelocations)
11296             {
11297               r = get_relocs (htab->brlt, 1);
11298               if (r == NULL)
11299                 return FALSE;
11300               /* brlt, being SEC_LINKER_CREATED does not go through the
11301                  normal reloc processing.  Symbols and offsets are not
11302                  translated from input file to output file form, so
11303                  set up the offset per the output file.  */
11304               r->r_offset = (br_entry->offset
11305                              + htab->brlt->output_offset
11306                              + htab->brlt->output_section->vma);
11307               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11308               r->r_addend = dest;
11309             }
11310         }
11311
11312       dest = (br_entry->offset
11313               + htab->brlt->output_offset
11314               + htab->brlt->output_section->vma);
11315
11316       off = (dest
11317              - elf_gp (info->output_bfd)
11318              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11319
11320       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11321         {
11322           info->callbacks->einfo
11323             (_("%P: linkage table error against `%pT'\n"),
11324              stub_entry->root.string);
11325           bfd_set_error (bfd_error_bad_value);
11326           htab->stub_error = TRUE;
11327           return FALSE;
11328         }
11329
11330       if (info->emitrelocations)
11331         {
11332           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11333           if (r == NULL)
11334             return FALSE;
11335           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11336           if (bfd_big_endian (info->output_bfd))
11337             r[0].r_offset += 2;
11338           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11339             r[0].r_offset += 4;
11340           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11341           r[0].r_addend = dest;
11342           if (PPC_HA (off) != 0)
11343             {
11344               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11345               r[1].r_offset = r[0].r_offset + 4;
11346               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11347               r[1].r_addend = r[0].r_addend;
11348             }
11349         }
11350
11351       p = loc;
11352       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11353         {
11354           if (PPC_HA (off) != 0)
11355             {
11356               bfd_put_32 (htab->params->stub_bfd,
11357                           ADDIS_R12_R2 | PPC_HA (off), p);
11358               p += 4;
11359               bfd_put_32 (htab->params->stub_bfd,
11360                           LD_R12_0R12 | PPC_LO (off), p);
11361             }
11362           else
11363             bfd_put_32 (htab->params->stub_bfd,
11364                         LD_R12_0R2 | PPC_LO (off), p);
11365         }
11366       else
11367         {
11368           bfd_vma r2off = get_r2off (info, stub_entry);
11369
11370           if (r2off == (bfd_vma) -1)
11371             {
11372               htab->stub_error = TRUE;
11373               return FALSE;
11374             }
11375
11376           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11377           p += 4;
11378           if (PPC_HA (off) != 0)
11379             {
11380               bfd_put_32 (htab->params->stub_bfd,
11381                           ADDIS_R12_R2 | PPC_HA (off), p);
11382               p += 4;
11383               bfd_put_32 (htab->params->stub_bfd,
11384                           LD_R12_0R12 | PPC_LO (off), p);
11385             }
11386           else
11387             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11388
11389           if (PPC_HA (r2off) != 0)
11390             {
11391               p += 4;
11392               bfd_put_32 (htab->params->stub_bfd,
11393                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11394             }
11395           if (PPC_LO (r2off) != 0)
11396             {
11397               p += 4;
11398               bfd_put_32 (htab->params->stub_bfd,
11399                           ADDI_R2_R2 | PPC_LO (r2off), p);
11400             }
11401         }
11402       p += 4;
11403       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11404       p += 4;
11405       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11406       p += 4;
11407       break;
11408
11409     case ppc_stub_plt_call:
11410     case ppc_stub_plt_call_r2save:
11411       if (stub_entry->h != NULL
11412           && stub_entry->h->is_func_descriptor
11413           && stub_entry->h->oh != NULL)
11414         {
11415           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11416
11417           /* If the old-ABI "dot-symbol" is undefined make it weak so
11418              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11419           if (fh->elf.root.type == bfd_link_hash_undefined
11420               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11421                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11422             fh->elf.root.type = bfd_link_hash_undefweak;
11423         }
11424
11425       /* Now build the stub.  */
11426       dest = stub_entry->plt_ent->plt.offset & ~1;
11427       if (dest >= (bfd_vma) -2)
11428         abort ();
11429
11430       plt = htab->elf.splt;
11431       if (!htab->elf.dynamic_sections_created
11432           || stub_entry->h == NULL
11433           || stub_entry->h->elf.dynindx == -1)
11434         {
11435           if (stub_entry->symtype == STT_GNU_IFUNC)
11436             plt = htab->elf.iplt;
11437           else
11438             plt = htab->pltlocal;
11439         }
11440
11441       dest += plt->output_offset + plt->output_section->vma;
11442
11443       off = (dest
11444              - elf_gp (info->output_bfd)
11445              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11446
11447       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11448         {
11449           info->callbacks->einfo
11450             /* xgettext:c-format */
11451             (_("%P: linkage table error against `%pT'\n"),
11452              stub_entry->h != NULL
11453              ? stub_entry->h->elf.root.root.string
11454              : "<local sym>");
11455           bfd_set_error (bfd_error_bad_value);
11456           htab->stub_error = TRUE;
11457           return FALSE;
11458         }
11459
11460       if (htab->params->plt_stub_align != 0)
11461         {
11462           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11463
11464           stub_entry->group->stub_sec->size += pad;
11465           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11466           loc += pad;
11467         }
11468
11469       r = NULL;
11470       if (info->emitrelocations)
11471         {
11472           r = get_relocs (stub_entry->group->stub_sec,
11473                           ((PPC_HA (off) != 0)
11474                            + (htab->opd_abi
11475                               ? 2 + (htab->params->plt_static_chain
11476                                      && PPC_HA (off + 16) == PPC_HA (off))
11477                               : 1)));
11478           if (r == NULL)
11479             return FALSE;
11480           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11481           if (bfd_big_endian (info->output_bfd))
11482             r[0].r_offset += 2;
11483           r[0].r_addend = dest;
11484         }
11485       if (stub_entry->h != NULL
11486           && (stub_entry->h == htab->tls_get_addr_fd
11487               || stub_entry->h == htab->tls_get_addr)
11488           && htab->params->tls_get_addr_opt)
11489         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11490       else
11491         p = build_plt_stub (htab, stub_entry, loc, off, r);
11492       break;
11493
11494     case ppc_stub_save_res:
11495       return TRUE;
11496
11497     default:
11498       BFD_FAIL ();
11499       return FALSE;
11500     }
11501
11502   stub_entry->group->stub_sec->size += p - loc;
11503
11504   if (htab->params->emit_stub_syms)
11505     {
11506       struct elf_link_hash_entry *h;
11507       size_t len1, len2;
11508       char *name;
11509       const char *const stub_str[] = { "long_branch",
11510                                        "long_branch_r2off",
11511                                        "plt_branch",
11512                                        "plt_branch_r2off",
11513                                        "plt_call",
11514                                        "plt_call" };
11515
11516       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11517       len2 = strlen (stub_entry->root.string);
11518       name = bfd_malloc (len1 + len2 + 2);
11519       if (name == NULL)
11520         return FALSE;
11521       memcpy (name, stub_entry->root.string, 9);
11522       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11523       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11524       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11525       if (h == NULL)
11526         return FALSE;
11527       if (h->root.type == bfd_link_hash_new)
11528         {
11529           h->root.type = bfd_link_hash_defined;
11530           h->root.u.def.section = stub_entry->group->stub_sec;
11531           h->root.u.def.value = stub_entry->stub_offset;
11532           h->ref_regular = 1;
11533           h->def_regular = 1;
11534           h->ref_regular_nonweak = 1;
11535           h->forced_local = 1;
11536           h->non_elf = 0;
11537           h->root.linker_def = 1;
11538         }
11539     }
11540
11541   return TRUE;
11542 }
11543
11544 /* As above, but don't actually build the stub.  Just bump offset so
11545    we know stub section sizes, and select plt_branch stubs where
11546    long_branch stubs won't do.  */
11547
11548 static bfd_boolean
11549 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11550 {
11551   struct ppc_stub_hash_entry *stub_entry;
11552   struct bfd_link_info *info;
11553   struct ppc_link_hash_table *htab;
11554   bfd_vma off;
11555   int size;
11556
11557   /* Massage our args to the form they really have.  */
11558   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11559   info = in_arg;
11560
11561   htab = ppc_hash_table (info);
11562   if (htab == NULL)
11563     return FALSE;
11564
11565   if (stub_entry->h != NULL
11566       && stub_entry->h->save_res
11567       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11568       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11569     {
11570       /* Don't make stubs to out-of-line register save/restore
11571          functions.  Instead, emit copies of the functions.  */
11572       stub_entry->group->needs_save_res = 1;
11573       stub_entry->stub_type = ppc_stub_save_res;
11574       return TRUE;
11575     }
11576
11577   if (stub_entry->stub_type == ppc_stub_plt_call
11578       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11579     {
11580       asection *plt;
11581       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11582       if (off >= (bfd_vma) -2)
11583         abort ();
11584       plt = htab->elf.splt;
11585       if (!htab->elf.dynamic_sections_created
11586           || stub_entry->h == NULL
11587           || stub_entry->h->elf.dynindx == -1)
11588         {
11589           if (stub_entry->symtype == STT_GNU_IFUNC)
11590             plt = htab->elf.iplt;
11591           else
11592             plt = htab->pltlocal;
11593         }
11594       off += (plt->output_offset
11595               + plt->output_section->vma
11596               - elf_gp (info->output_bfd)
11597               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11598
11599       size = plt_stub_size (htab, stub_entry, off);
11600       if (stub_entry->h != NULL
11601           && (stub_entry->h == htab->tls_get_addr_fd
11602               || stub_entry->h == htab->tls_get_addr)
11603           && htab->params->tls_get_addr_opt
11604           && (ALWAYS_EMIT_R2SAVE
11605               || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11606         stub_entry->group->tls_get_addr_opt_bctrl
11607           = stub_entry->group->stub_sec->size + size - 5 * 4;
11608
11609       if (htab->params->plt_stub_align)
11610         size += plt_stub_pad (htab, stub_entry, off);
11611       if (info->emitrelocations)
11612         {
11613           stub_entry->group->stub_sec->reloc_count
11614             += ((PPC_HA (off) != 0)
11615                 + (htab->opd_abi
11616                    ? 2 + (htab->params->plt_static_chain
11617                           && PPC_HA (off + 16) == PPC_HA (off))
11618                    : 1));
11619           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11620         }
11621     }
11622   else
11623     {
11624       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11625          variants.  */
11626       bfd_vma r2off = 0;
11627       bfd_vma local_off = 0;
11628
11629       off = (stub_entry->target_value
11630              + stub_entry->target_section->output_offset
11631              + stub_entry->target_section->output_section->vma);
11632       off -= (stub_entry->group->stub_sec->size
11633               + stub_entry->group->stub_sec->output_offset
11634               + stub_entry->group->stub_sec->output_section->vma);
11635
11636       /* Reset the stub type from the plt variant in case we now
11637          can reach with a shorter stub.  */
11638       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11639         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11640
11641       size = 4;
11642       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11643         {
11644           r2off = get_r2off (info, stub_entry);
11645           if (r2off == (bfd_vma) -1)
11646             {
11647               htab->stub_error = TRUE;
11648               return FALSE;
11649             }
11650           size = 8;
11651           if (PPC_HA (r2off) != 0)
11652             size += 4;
11653           if (PPC_LO (r2off) != 0)
11654             size += 4;
11655           off -= size - 4;
11656         }
11657
11658       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11659
11660       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11661          Do the same for -R objects without function descriptors.  */
11662       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11663           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11664               && r2off == 0
11665               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11666         {
11667           struct ppc_branch_hash_entry *br_entry;
11668
11669           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11670                                              stub_entry->root.string + 9,
11671                                              TRUE, FALSE);
11672           if (br_entry == NULL)
11673             {
11674               _bfd_error_handler (_("can't build branch stub `%s'"),
11675                                   stub_entry->root.string);
11676               htab->stub_error = TRUE;
11677               return FALSE;
11678             }
11679
11680           if (br_entry->iter != htab->stub_iteration)
11681             {
11682               br_entry->iter = htab->stub_iteration;
11683               br_entry->offset = htab->brlt->size;
11684               htab->brlt->size += 8;
11685
11686               if (htab->relbrlt != NULL)
11687                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11688               else if (info->emitrelocations)
11689                 {
11690                   htab->brlt->reloc_count += 1;
11691                   htab->brlt->flags |= SEC_RELOC;
11692                 }
11693             }
11694
11695           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11696           off = (br_entry->offset
11697                  + htab->brlt->output_offset
11698                  + htab->brlt->output_section->vma
11699                  - elf_gp (info->output_bfd)
11700                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11701
11702           if (info->emitrelocations)
11703             {
11704               stub_entry->group->stub_sec->reloc_count
11705                 += 1 + (PPC_HA (off) != 0);
11706               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11707             }
11708
11709           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11710             {
11711               size = 12;
11712               if (PPC_HA (off) != 0)
11713                 size = 16;
11714             }
11715           else
11716             {
11717               size = 16;
11718               if (PPC_HA (off) != 0)
11719                 size += 4;
11720
11721               if (PPC_HA (r2off) != 0)
11722                 size += 4;
11723               if (PPC_LO (r2off) != 0)
11724                 size += 4;
11725             }
11726         }
11727       else if (info->emitrelocations)
11728         {
11729           stub_entry->group->stub_sec->reloc_count += 1;
11730           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11731         }
11732     }
11733
11734   stub_entry->group->stub_sec->size += size;
11735   return TRUE;
11736 }
11737
11738 /* Set up various things so that we can make a list of input sections
11739    for each output section included in the link.  Returns -1 on error,
11740    0 when no stubs will be needed, and 1 on success.  */
11741
11742 int
11743 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11744 {
11745   unsigned int id;
11746   bfd_size_type amt;
11747   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11748
11749   if (htab == NULL)
11750     return -1;
11751
11752   htab->sec_info_arr_size = bfd_get_next_section_id ();
11753   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11754   htab->sec_info = bfd_zmalloc (amt);
11755   if (htab->sec_info == NULL)
11756     return -1;
11757
11758   /* Set toc_off for com, und, abs and ind sections.  */
11759   for (id = 0; id < 3; id++)
11760     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11761
11762   return 1;
11763 }
11764
11765 /* Set up for first pass at multitoc partitioning.  */
11766
11767 void
11768 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11769 {
11770   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11771
11772   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11773   htab->toc_bfd = NULL;
11774   htab->toc_first_sec = NULL;
11775 }
11776
11777 /* The linker repeatedly calls this function for each TOC input section
11778    and linker generated GOT section.  Group input bfds such that the toc
11779    within a group is less than 64k in size.  */
11780
11781 bfd_boolean
11782 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11783 {
11784   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11785   bfd_vma addr, off, limit;
11786
11787   if (htab == NULL)
11788     return FALSE;
11789
11790   if (!htab->second_toc_pass)
11791     {
11792       /* Keep track of the first .toc or .got section for this input bfd.  */
11793       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11794
11795       if (new_bfd)
11796         {
11797           htab->toc_bfd = isec->owner;
11798           htab->toc_first_sec = isec;
11799         }
11800
11801       addr = isec->output_offset + isec->output_section->vma;
11802       off = addr - htab->toc_curr;
11803       limit = 0x80008000;
11804       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11805         limit = 0x10000;
11806       if (off + isec->size > limit)
11807         {
11808           addr = (htab->toc_first_sec->output_offset
11809                   + htab->toc_first_sec->output_section->vma);
11810           htab->toc_curr = addr;
11811           htab->toc_curr &= -TOC_BASE_ALIGN;
11812         }
11813
11814       /* toc_curr is the base address of this toc group.  Set elf_gp
11815          for the input section to be the offset relative to the
11816          output toc base plus 0x8000.  Making the input elf_gp an
11817          offset allows us to move the toc as a whole without
11818          recalculating input elf_gp.  */
11819       off = htab->toc_curr - elf_gp (info->output_bfd);
11820       off += TOC_BASE_OFF;
11821
11822       /* Die if someone uses a linker script that doesn't keep input
11823          file .toc and .got together.  */
11824       if (new_bfd
11825           && elf_gp (isec->owner) != 0
11826           && elf_gp (isec->owner) != off)
11827         return FALSE;
11828
11829       elf_gp (isec->owner) = off;
11830       return TRUE;
11831     }
11832
11833   /* During the second pass toc_first_sec points to the start of
11834      a toc group, and toc_curr is used to track the old elf_gp.
11835      We use toc_bfd to ensure we only look at each bfd once.  */
11836   if (htab->toc_bfd == isec->owner)
11837     return TRUE;
11838   htab->toc_bfd = isec->owner;
11839
11840   if (htab->toc_first_sec == NULL
11841       || htab->toc_curr != elf_gp (isec->owner))
11842     {
11843       htab->toc_curr = elf_gp (isec->owner);
11844       htab->toc_first_sec = isec;
11845     }
11846   addr = (htab->toc_first_sec->output_offset
11847           + htab->toc_first_sec->output_section->vma);
11848   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11849   elf_gp (isec->owner) = off;
11850
11851   return TRUE;
11852 }
11853
11854 /* Called via elf_link_hash_traverse to merge GOT entries for global
11855    symbol H.  */
11856
11857 static bfd_boolean
11858 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11859 {
11860   if (h->root.type == bfd_link_hash_indirect)
11861     return TRUE;
11862
11863   merge_got_entries (&h->got.glist);
11864
11865   return TRUE;
11866 }
11867
11868 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11869    symbol H.  */
11870
11871 static bfd_boolean
11872 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11873 {
11874   struct got_entry *gent;
11875
11876   if (h->root.type == bfd_link_hash_indirect)
11877     return TRUE;
11878
11879   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11880     if (!gent->is_indirect)
11881       allocate_got (h, (struct bfd_link_info *) inf, gent);
11882   return TRUE;
11883 }
11884
11885 /* Called on the first multitoc pass after the last call to
11886    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11887    entries.  */
11888
11889 bfd_boolean
11890 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11891 {
11892   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11893   struct bfd *ibfd, *ibfd2;
11894   bfd_boolean done_something;
11895
11896   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11897
11898   if (!htab->do_multi_toc)
11899     return FALSE;
11900
11901   /* Merge global sym got entries within a toc group.  */
11902   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11903
11904   /* And tlsld_got.  */
11905   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11906     {
11907       struct got_entry *ent, *ent2;
11908
11909       if (!is_ppc64_elf (ibfd))
11910         continue;
11911
11912       ent = ppc64_tlsld_got (ibfd);
11913       if (!ent->is_indirect
11914           && ent->got.offset != (bfd_vma) -1)
11915         {
11916           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11917             {
11918               if (!is_ppc64_elf (ibfd2))
11919                 continue;
11920
11921               ent2 = ppc64_tlsld_got (ibfd2);
11922               if (!ent2->is_indirect
11923                   && ent2->got.offset != (bfd_vma) -1
11924                   && elf_gp (ibfd2) == elf_gp (ibfd))
11925                 {
11926                   ent2->is_indirect = TRUE;
11927                   ent2->got.ent = ent;
11928                 }
11929             }
11930         }
11931     }
11932
11933   /* Zap sizes of got sections.  */
11934   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11935   htab->elf.irelplt->size -= htab->got_reli_size;
11936   htab->got_reli_size = 0;
11937
11938   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11939     {
11940       asection *got, *relgot;
11941
11942       if (!is_ppc64_elf (ibfd))
11943         continue;
11944
11945       got = ppc64_elf_tdata (ibfd)->got;
11946       if (got != NULL)
11947         {
11948           got->rawsize = got->size;
11949           got->size = 0;
11950           relgot = ppc64_elf_tdata (ibfd)->relgot;
11951           relgot->rawsize = relgot->size;
11952           relgot->size = 0;
11953         }
11954     }
11955
11956   /* Now reallocate the got, local syms first.  We don't need to
11957      allocate section contents again since we never increase size.  */
11958   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11959     {
11960       struct got_entry **lgot_ents;
11961       struct got_entry **end_lgot_ents;
11962       struct plt_entry **local_plt;
11963       struct plt_entry **end_local_plt;
11964       unsigned char *lgot_masks;
11965       bfd_size_type locsymcount;
11966       Elf_Internal_Shdr *symtab_hdr;
11967       asection *s;
11968
11969       if (!is_ppc64_elf (ibfd))
11970         continue;
11971
11972       lgot_ents = elf_local_got_ents (ibfd);
11973       if (!lgot_ents)
11974         continue;
11975
11976       symtab_hdr = &elf_symtab_hdr (ibfd);
11977       locsymcount = symtab_hdr->sh_info;
11978       end_lgot_ents = lgot_ents + locsymcount;
11979       local_plt = (struct plt_entry **) end_lgot_ents;
11980       end_local_plt = local_plt + locsymcount;
11981       lgot_masks = (unsigned char *) end_local_plt;
11982       s = ppc64_elf_tdata (ibfd)->got;
11983       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11984         {
11985           struct got_entry *ent;
11986
11987           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11988             {
11989               unsigned int ent_size = 8;
11990               unsigned int rel_size = sizeof (Elf64_External_Rela);
11991
11992               ent->got.offset = s->size;
11993               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11994                 {
11995                   ent_size *= 2;
11996                   rel_size *= 2;
11997                 }
11998               s->size += ent_size;
11999               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12000                 {
12001                   htab->elf.irelplt->size += rel_size;
12002                   htab->got_reli_size += rel_size;
12003                 }
12004               else if (bfd_link_pic (info)
12005                        && !((ent->tls_type & TLS_TPREL) != 0
12006                             && bfd_link_executable (info)))
12007                 {
12008                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12009                   srel->size += rel_size;
12010                 }
12011             }
12012         }
12013     }
12014
12015   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12016
12017   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12018     {
12019       struct got_entry *ent;
12020
12021       if (!is_ppc64_elf (ibfd))
12022         continue;
12023
12024       ent = ppc64_tlsld_got (ibfd);
12025       if (!ent->is_indirect
12026           && ent->got.offset != (bfd_vma) -1)
12027         {
12028           asection *s = ppc64_elf_tdata (ibfd)->got;
12029           ent->got.offset = s->size;
12030           s->size += 16;
12031           if (bfd_link_pic (info))
12032             {
12033               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12034               srel->size += sizeof (Elf64_External_Rela);
12035             }
12036         }
12037     }
12038
12039   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12040   if (!done_something)
12041     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12042       {
12043         asection *got;
12044
12045         if (!is_ppc64_elf (ibfd))
12046           continue;
12047
12048         got = ppc64_elf_tdata (ibfd)->got;
12049         if (got != NULL)
12050           {
12051             done_something = got->rawsize != got->size;
12052             if (done_something)
12053               break;
12054           }
12055       }
12056
12057   if (done_something)
12058     (*htab->params->layout_sections_again) ();
12059
12060   /* Set up for second pass over toc sections to recalculate elf_gp
12061      on input sections.  */
12062   htab->toc_bfd = NULL;
12063   htab->toc_first_sec = NULL;
12064   htab->second_toc_pass = TRUE;
12065   return done_something;
12066 }
12067
12068 /* Called after second pass of multitoc partitioning.  */
12069
12070 void
12071 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12072 {
12073   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12074
12075   /* After the second pass, toc_curr tracks the TOC offset used
12076      for code sections below in ppc64_elf_next_input_section.  */
12077   htab->toc_curr = TOC_BASE_OFF;
12078 }
12079
12080 /* No toc references were found in ISEC.  If the code in ISEC makes no
12081    calls, then there's no need to use toc adjusting stubs when branching
12082    into ISEC.  Actually, indirect calls from ISEC are OK as they will
12083    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
12084    needed, and 2 if a cyclical call-graph was found but no other reason
12085    for a stub was detected.  If called from the top level, a return of
12086    2 means the same as a return of 0.  */
12087
12088 static int
12089 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12090 {
12091   int ret;
12092
12093   /* Mark this section as checked.  */
12094   isec->call_check_done = 1;
12095
12096   /* We know none of our code bearing sections will need toc stubs.  */
12097   if ((isec->flags & SEC_LINKER_CREATED) != 0)
12098     return 0;
12099
12100   if (isec->size == 0)
12101     return 0;
12102
12103   if (isec->output_section == NULL)
12104     return 0;
12105
12106   ret = 0;
12107   if (isec->reloc_count != 0)
12108     {
12109       Elf_Internal_Rela *relstart, *rel;
12110       Elf_Internal_Sym *local_syms;
12111       struct ppc_link_hash_table *htab;
12112
12113       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12114                                             info->keep_memory);
12115       if (relstart == NULL)
12116         return -1;
12117
12118       /* Look for branches to outside of this section.  */
12119       local_syms = NULL;
12120       htab = ppc_hash_table (info);
12121       if (htab == NULL)
12122         return -1;
12123
12124       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12125         {
12126           enum elf_ppc64_reloc_type r_type;
12127           unsigned long r_symndx;
12128           struct elf_link_hash_entry *h;
12129           struct ppc_link_hash_entry *eh;
12130           Elf_Internal_Sym *sym;
12131           asection *sym_sec;
12132           struct _opd_sec_data *opd;
12133           bfd_vma sym_value;
12134           bfd_vma dest;
12135
12136           r_type = ELF64_R_TYPE (rel->r_info);
12137           if (r_type != R_PPC64_REL24
12138               && r_type != R_PPC64_REL14
12139               && r_type != R_PPC64_REL14_BRTAKEN
12140               && r_type != R_PPC64_REL14_BRNTAKEN
12141               && r_type != R_PPC64_PLTCALL)
12142             continue;
12143
12144           r_symndx = ELF64_R_SYM (rel->r_info);
12145           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12146                           isec->owner))
12147             {
12148               ret = -1;
12149               break;
12150             }
12151
12152           /* Calls to dynamic lib functions go through a plt call stub
12153              that uses r2.  */
12154           eh = (struct ppc_link_hash_entry *) h;
12155           if (eh != NULL
12156               && (eh->elf.plt.plist != NULL
12157                   || (eh->oh != NULL
12158                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12159             {
12160               ret = 1;
12161               break;
12162             }
12163
12164           if (sym_sec == NULL)
12165             /* Ignore other undefined symbols.  */
12166             continue;
12167
12168           /* Assume branches to other sections not included in the
12169              link need stubs too, to cover -R and absolute syms.  */
12170           if (sym_sec->output_section == NULL)
12171             {
12172               ret = 1;
12173               break;
12174             }
12175
12176           if (h == NULL)
12177             sym_value = sym->st_value;
12178           else
12179             {
12180               if (h->root.type != bfd_link_hash_defined
12181                   && h->root.type != bfd_link_hash_defweak)
12182                 abort ();
12183               sym_value = h->root.u.def.value;
12184             }
12185           sym_value += rel->r_addend;
12186
12187           /* If this branch reloc uses an opd sym, find the code section.  */
12188           opd = get_opd_info (sym_sec);
12189           if (opd != NULL)
12190             {
12191               if (h == NULL && opd->adjust != NULL)
12192                 {
12193                   long adjust;
12194
12195                   adjust = opd->adjust[OPD_NDX (sym_value)];
12196                   if (adjust == -1)
12197                     /* Assume deleted functions won't ever be called.  */
12198                     continue;
12199                   sym_value += adjust;
12200                 }
12201
12202               dest = opd_entry_value (sym_sec, sym_value,
12203                                       &sym_sec, NULL, FALSE);
12204               if (dest == (bfd_vma) -1)
12205                 continue;
12206             }
12207           else
12208             dest = (sym_value
12209                     + sym_sec->output_offset
12210                     + sym_sec->output_section->vma);
12211
12212           /* Ignore branch to self.  */
12213           if (sym_sec == isec)
12214             continue;
12215
12216           /* If the called function uses the toc, we need a stub.  */
12217           if (sym_sec->has_toc_reloc
12218               || sym_sec->makes_toc_func_call)
12219             {
12220               ret = 1;
12221               break;
12222             }
12223
12224           /* Assume any branch that needs a long branch stub might in fact
12225              need a plt_branch stub.  A plt_branch stub uses r2.  */
12226           else if (dest - (isec->output_offset
12227                            + isec->output_section->vma
12228                            + rel->r_offset) + (1 << 25)
12229                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12230                                                              ? h->other
12231                                                              : sym->st_other))
12232             {
12233               ret = 1;
12234               break;
12235             }
12236
12237           /* If calling back to a section in the process of being
12238              tested, we can't say for sure that no toc adjusting stubs
12239              are needed, so don't return zero.  */
12240           else if (sym_sec->call_check_in_progress)
12241             ret = 2;
12242
12243           /* Branches to another section that itself doesn't have any TOC
12244              references are OK.  Recursively call ourselves to check.  */
12245           else if (!sym_sec->call_check_done)
12246             {
12247               int recur;
12248
12249               /* Mark current section as indeterminate, so that other
12250                  sections that call back to current won't be marked as
12251                  known.  */
12252               isec->call_check_in_progress = 1;
12253               recur = toc_adjusting_stub_needed (info, sym_sec);
12254               isec->call_check_in_progress = 0;
12255
12256               if (recur != 0)
12257                 {
12258                   ret = recur;
12259                   if (recur != 2)
12260                     break;
12261                 }
12262             }
12263         }
12264
12265       if (local_syms != NULL
12266           && (elf_symtab_hdr (isec->owner).contents
12267               != (unsigned char *) local_syms))
12268         free (local_syms);
12269       if (elf_section_data (isec)->relocs != relstart)
12270         free (relstart);
12271     }
12272
12273   if ((ret & 1) == 0
12274       && isec->map_head.s != NULL
12275       && (strcmp (isec->output_section->name, ".init") == 0
12276           || strcmp (isec->output_section->name, ".fini") == 0))
12277     {
12278       if (isec->map_head.s->has_toc_reloc
12279           || isec->map_head.s->makes_toc_func_call)
12280         ret = 1;
12281       else if (!isec->map_head.s->call_check_done)
12282         {
12283           int recur;
12284           isec->call_check_in_progress = 1;
12285           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12286           isec->call_check_in_progress = 0;
12287           if (recur != 0)
12288             ret = recur;
12289         }
12290     }
12291
12292   if (ret == 1)
12293     isec->makes_toc_func_call = 1;
12294
12295   return ret;
12296 }
12297
12298 /* The linker repeatedly calls this function for each input section,
12299    in the order that input sections are linked into output sections.
12300    Build lists of input sections to determine groupings between which
12301    we may insert linker stubs.  */
12302
12303 bfd_boolean
12304 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12305 {
12306   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12307
12308   if (htab == NULL)
12309     return FALSE;
12310
12311   if ((isec->output_section->flags & SEC_CODE) != 0
12312       && isec->output_section->id < htab->sec_info_arr_size)
12313     {
12314       /* This happens to make the list in reverse order,
12315          which is what we want.  */
12316       htab->sec_info[isec->id].u.list
12317         = htab->sec_info[isec->output_section->id].u.list;
12318       htab->sec_info[isec->output_section->id].u.list = isec;
12319     }
12320
12321   if (htab->multi_toc_needed)
12322     {
12323       /* Analyse sections that aren't already flagged as needing a
12324          valid toc pointer.  Exclude .fixup for the linux kernel.
12325          .fixup contains branches, but only back to the function that
12326          hit an exception.  */
12327       if (!(isec->has_toc_reloc
12328             || (isec->flags & SEC_CODE) == 0
12329             || strcmp (isec->name, ".fixup") == 0
12330             || isec->call_check_done))
12331         {
12332           if (toc_adjusting_stub_needed (info, isec) < 0)
12333             return FALSE;
12334         }
12335       /* Make all sections use the TOC assigned for this object file.
12336          This will be wrong for pasted sections;  We fix that in
12337          check_pasted_section().  */
12338       if (elf_gp (isec->owner) != 0)
12339         htab->toc_curr = elf_gp (isec->owner);
12340     }
12341
12342   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12343   return TRUE;
12344 }
12345
12346 /* Check that all .init and .fini sections use the same toc, if they
12347    have toc relocs.  */
12348
12349 static bfd_boolean
12350 check_pasted_section (struct bfd_link_info *info, const char *name)
12351 {
12352   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12353
12354   if (o != NULL)
12355     {
12356       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12357       bfd_vma toc_off = 0;
12358       asection *i;
12359
12360       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12361         if (i->has_toc_reloc)
12362           {
12363             if (toc_off == 0)
12364               toc_off = htab->sec_info[i->id].toc_off;
12365             else if (toc_off != htab->sec_info[i->id].toc_off)
12366               return FALSE;
12367           }
12368
12369       if (toc_off == 0)
12370         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12371           if (i->makes_toc_func_call)
12372             {
12373               toc_off = htab->sec_info[i->id].toc_off;
12374               break;
12375             }
12376
12377       /* Make sure the whole pasted function uses the same toc offset.  */
12378       if (toc_off != 0)
12379         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12380           htab->sec_info[i->id].toc_off = toc_off;
12381     }
12382   return TRUE;
12383 }
12384
12385 bfd_boolean
12386 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12387 {
12388   return (check_pasted_section (info, ".init")
12389           & check_pasted_section (info, ".fini"));
12390 }
12391
12392 /* See whether we can group stub sections together.  Grouping stub
12393    sections may result in fewer stubs.  More importantly, we need to
12394    put all .init* and .fini* stubs at the beginning of the .init or
12395    .fini output sections respectively, because glibc splits the
12396    _init and _fini functions into multiple parts.  Putting a stub in
12397    the middle of a function is not a good idea.  */
12398
12399 static bfd_boolean
12400 group_sections (struct bfd_link_info *info,
12401                 bfd_size_type stub_group_size,
12402                 bfd_boolean stubs_always_before_branch)
12403 {
12404   struct ppc_link_hash_table *htab;
12405   asection *osec;
12406   bfd_boolean suppress_size_errors;
12407
12408   htab = ppc_hash_table (info);
12409   if (htab == NULL)
12410     return FALSE;
12411
12412   suppress_size_errors = FALSE;
12413   if (stub_group_size == 1)
12414     {
12415       /* Default values.  */
12416       if (stubs_always_before_branch)
12417         stub_group_size = 0x1e00000;
12418       else
12419         stub_group_size = 0x1c00000;
12420       suppress_size_errors = TRUE;
12421     }
12422
12423   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12424     {
12425       asection *tail;
12426
12427       if (osec->id >= htab->sec_info_arr_size)
12428         continue;
12429
12430       tail = htab->sec_info[osec->id].u.list;
12431       while (tail != NULL)
12432         {
12433           asection *curr;
12434           asection *prev;
12435           bfd_size_type total;
12436           bfd_boolean big_sec;
12437           bfd_vma curr_toc;
12438           struct map_stub *group;
12439           bfd_size_type group_size;
12440
12441           curr = tail;
12442           total = tail->size;
12443           group_size = (ppc64_elf_section_data (tail) != NULL
12444                         && ppc64_elf_section_data (tail)->has_14bit_branch
12445                         ? stub_group_size >> 10 : stub_group_size);
12446
12447           big_sec = total > group_size;
12448           if (big_sec && !suppress_size_errors)
12449             /* xgettext:c-format */
12450             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12451                                 tail->owner, tail);
12452           curr_toc = htab->sec_info[tail->id].toc_off;
12453
12454           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12455                  && ((total += curr->output_offset - prev->output_offset)
12456                      < (ppc64_elf_section_data (prev) != NULL
12457                         && ppc64_elf_section_data (prev)->has_14bit_branch
12458                         ? (group_size = stub_group_size >> 10) : group_size))
12459                  && htab->sec_info[prev->id].toc_off == curr_toc)
12460             curr = prev;
12461
12462           /* OK, the size from the start of CURR to the end is less
12463              than group_size and thus can be handled by one stub
12464              section.  (or the tail section is itself larger than
12465              group_size, in which case we may be toast.)  We should
12466              really be keeping track of the total size of stubs added
12467              here, as stubs contribute to the final output section
12468              size.  That's a little tricky, and this way will only
12469              break if stubs added make the total size more than 2^25,
12470              ie. for the default stub_group_size, if stubs total more
12471              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12472           group = bfd_alloc (curr->owner, sizeof (*group));
12473           if (group == NULL)
12474             return FALSE;
12475           group->link_sec = curr;
12476           group->stub_sec = NULL;
12477           group->needs_save_res = 0;
12478           group->tls_get_addr_opt_bctrl = -1u;
12479           group->next = htab->group;
12480           htab->group = group;
12481           do
12482             {
12483               prev = htab->sec_info[tail->id].u.list;
12484               /* Set up this stub group.  */
12485               htab->sec_info[tail->id].u.group = group;
12486             }
12487           while (tail != curr && (tail = prev) != NULL);
12488
12489           /* But wait, there's more!  Input sections up to group_size
12490              bytes before the stub section can be handled by it too.
12491              Don't do this if we have a really large section after the
12492              stubs, as adding more stubs increases the chance that
12493              branches may not reach into the stub section.  */
12494           if (!stubs_always_before_branch && !big_sec)
12495             {
12496               total = 0;
12497               while (prev != NULL
12498                      && ((total += tail->output_offset - prev->output_offset)
12499                          < (ppc64_elf_section_data (prev) != NULL
12500                             && ppc64_elf_section_data (prev)->has_14bit_branch
12501                             ? (group_size = stub_group_size >> 10) : group_size))
12502                      && htab->sec_info[prev->id].toc_off == curr_toc)
12503                 {
12504                   tail = prev;
12505                   prev = htab->sec_info[tail->id].u.list;
12506                   htab->sec_info[tail->id].u.group = group;
12507                 }
12508             }
12509           tail = prev;
12510         }
12511     }
12512   return TRUE;
12513 }
12514
12515 static const unsigned char glink_eh_frame_cie[] =
12516 {
12517   0, 0, 0, 16,                          /* length.  */
12518   0, 0, 0, 0,                           /* id.  */
12519   1,                                    /* CIE version.  */
12520   'z', 'R', 0,                          /* Augmentation string.  */
12521   4,                                    /* Code alignment.  */
12522   0x78,                                 /* Data alignment.  */
12523   65,                                   /* RA reg.  */
12524   1,                                    /* Augmentation size.  */
12525   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12526   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12527 };
12528
12529 static size_t
12530 stub_eh_frame_size (struct map_stub *group, size_t align)
12531 {
12532   size_t this_size = 17;
12533   if (group->tls_get_addr_opt_bctrl != -1u)
12534     {
12535       unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12536       if (to_bctrl < 64)
12537         this_size += 1;
12538       else if (to_bctrl < 256)
12539         this_size += 2;
12540       else if (to_bctrl < 65536)
12541         this_size += 3;
12542       else
12543         this_size += 5;
12544       this_size += 6;
12545     }
12546   this_size = (this_size + align - 1) & -align;
12547   return this_size;
12548 }
12549
12550 /* Stripping output sections is normally done before dynamic section
12551    symbols have been allocated.  This function is called later, and
12552    handles cases like htab->brlt which is mapped to its own output
12553    section.  */
12554
12555 static void
12556 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12557 {
12558   if (isec->size == 0
12559       && isec->output_section->size == 0
12560       && !(isec->output_section->flags & SEC_KEEP)
12561       && !bfd_section_removed_from_list (info->output_bfd,
12562                                          isec->output_section)
12563       && elf_section_data (isec->output_section)->dynindx == 0)
12564     {
12565       isec->output_section->flags |= SEC_EXCLUDE;
12566       bfd_section_list_remove (info->output_bfd, isec->output_section);
12567       info->output_bfd->section_count--;
12568     }
12569 }
12570
12571 /* Determine and set the size of the stub section for a final link.
12572
12573    The basic idea here is to examine all the relocations looking for
12574    PC-relative calls to a target that is unreachable with a "bl"
12575    instruction.  */
12576
12577 bfd_boolean
12578 ppc64_elf_size_stubs (struct bfd_link_info *info)
12579 {
12580   bfd_size_type stub_group_size;
12581   bfd_boolean stubs_always_before_branch;
12582   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12583
12584   if (htab == NULL)
12585     return FALSE;
12586
12587   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12588     htab->params->plt_thread_safe = 1;
12589   if (!htab->opd_abi)
12590     htab->params->plt_thread_safe = 0;
12591   else if (htab->params->plt_thread_safe == -1)
12592     {
12593       static const char *const thread_starter[] =
12594         {
12595           "pthread_create",
12596           /* libstdc++ */
12597           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12598           /* librt */
12599           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12600           "mq_notify", "create_timer",
12601           /* libanl */
12602           "getaddrinfo_a",
12603           /* libgomp */
12604           "GOMP_parallel",
12605           "GOMP_parallel_start",
12606           "GOMP_parallel_loop_static",
12607           "GOMP_parallel_loop_static_start",
12608           "GOMP_parallel_loop_dynamic",
12609           "GOMP_parallel_loop_dynamic_start",
12610           "GOMP_parallel_loop_guided",
12611           "GOMP_parallel_loop_guided_start",
12612           "GOMP_parallel_loop_runtime",
12613           "GOMP_parallel_loop_runtime_start",
12614           "GOMP_parallel_sections",
12615           "GOMP_parallel_sections_start",
12616           /* libgo */
12617           "__go_go",
12618         };
12619       unsigned i;
12620
12621       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12622         {
12623           struct elf_link_hash_entry *h;
12624           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12625                                     FALSE, FALSE, TRUE);
12626           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12627           if (htab->params->plt_thread_safe)
12628             break;
12629         }
12630     }
12631   stubs_always_before_branch = htab->params->group_size < 0;
12632   if (htab->params->group_size < 0)
12633     stub_group_size = -htab->params->group_size;
12634   else
12635     stub_group_size = htab->params->group_size;
12636
12637   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12638     return FALSE;
12639
12640 #define STUB_SHRINK_ITER 20
12641   /* Loop until no stubs added.  After iteration 20 of this loop we may
12642      exit on a stub section shrinking.  This is to break out of a
12643      pathological case where adding stubs on one iteration decreases
12644      section gaps (perhaps due to alignment), which then requires
12645      fewer or smaller stubs on the next iteration.  */
12646
12647   while (1)
12648     {
12649       bfd *input_bfd;
12650       unsigned int bfd_indx;
12651       struct map_stub *group;
12652
12653       htab->stub_iteration += 1;
12654
12655       for (input_bfd = info->input_bfds, bfd_indx = 0;
12656            input_bfd != NULL;
12657            input_bfd = input_bfd->link.next, bfd_indx++)
12658         {
12659           Elf_Internal_Shdr *symtab_hdr;
12660           asection *section;
12661           Elf_Internal_Sym *local_syms = NULL;
12662
12663           if (!is_ppc64_elf (input_bfd))
12664             continue;
12665
12666           /* We'll need the symbol table in a second.  */
12667           symtab_hdr = &elf_symtab_hdr (input_bfd);
12668           if (symtab_hdr->sh_info == 0)
12669             continue;
12670
12671           /* Walk over each section attached to the input bfd.  */
12672           for (section = input_bfd->sections;
12673                section != NULL;
12674                section = section->next)
12675             {
12676               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12677
12678               /* If there aren't any relocs, then there's nothing more
12679                  to do.  */
12680               if ((section->flags & SEC_RELOC) == 0
12681                   || (section->flags & SEC_ALLOC) == 0
12682                   || (section->flags & SEC_LOAD) == 0
12683                   || (section->flags & SEC_CODE) == 0
12684                   || section->reloc_count == 0)
12685                 continue;
12686
12687               /* If this section is a link-once section that will be
12688                  discarded, then don't create any stubs.  */
12689               if (section->output_section == NULL
12690                   || section->output_section->owner != info->output_bfd)
12691                 continue;
12692
12693               /* Get the relocs.  */
12694               internal_relocs
12695                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12696                                              info->keep_memory);
12697               if (internal_relocs == NULL)
12698                 goto error_ret_free_local;
12699
12700               /* Now examine each relocation.  */
12701               irela = internal_relocs;
12702               irelaend = irela + section->reloc_count;
12703               for (; irela < irelaend; irela++)
12704                 {
12705                   enum elf_ppc64_reloc_type r_type;
12706                   unsigned int r_indx;
12707                   enum ppc_stub_type stub_type;
12708                   struct ppc_stub_hash_entry *stub_entry;
12709                   asection *sym_sec, *code_sec;
12710                   bfd_vma sym_value, code_value;
12711                   bfd_vma destination;
12712                   unsigned long local_off;
12713                   bfd_boolean ok_dest;
12714                   struct ppc_link_hash_entry *hash;
12715                   struct ppc_link_hash_entry *fdh;
12716                   struct elf_link_hash_entry *h;
12717                   Elf_Internal_Sym *sym;
12718                   char *stub_name;
12719                   const asection *id_sec;
12720                   struct _opd_sec_data *opd;
12721                   struct plt_entry *plt_ent;
12722
12723                   r_type = ELF64_R_TYPE (irela->r_info);
12724                   r_indx = ELF64_R_SYM (irela->r_info);
12725
12726                   if (r_type >= R_PPC64_max)
12727                     {
12728                       bfd_set_error (bfd_error_bad_value);
12729                       goto error_ret_free_internal;
12730                     }
12731
12732                   /* Only look for stubs on branch instructions.  */
12733                   if (r_type != R_PPC64_REL24
12734                       && r_type != R_PPC64_REL14
12735                       && r_type != R_PPC64_REL14_BRTAKEN
12736                       && r_type != R_PPC64_REL14_BRNTAKEN)
12737                     continue;
12738
12739                   /* Now determine the call target, its name, value,
12740                      section.  */
12741                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12742                                   r_indx, input_bfd))
12743                     goto error_ret_free_internal;
12744                   hash = (struct ppc_link_hash_entry *) h;
12745
12746                   ok_dest = FALSE;
12747                   fdh = NULL;
12748                   sym_value = 0;
12749                   if (hash == NULL)
12750                     {
12751                       sym_value = sym->st_value;
12752                       if (sym_sec != NULL
12753                           && sym_sec->output_section != NULL)
12754                         ok_dest = TRUE;
12755                     }
12756                   else if (hash->elf.root.type == bfd_link_hash_defined
12757                            || hash->elf.root.type == bfd_link_hash_defweak)
12758                     {
12759                       sym_value = hash->elf.root.u.def.value;
12760                       if (sym_sec->output_section != NULL)
12761                         ok_dest = TRUE;
12762                     }
12763                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12764                            || hash->elf.root.type == bfd_link_hash_undefined)
12765                     {
12766                       /* Recognise an old ABI func code entry sym, and
12767                          use the func descriptor sym instead if it is
12768                          defined.  */
12769                       if (hash->elf.root.root.string[0] == '.'
12770                           && hash->oh != NULL)
12771                         {
12772                           fdh = ppc_follow_link (hash->oh);
12773                           if (fdh->elf.root.type == bfd_link_hash_defined
12774                               || fdh->elf.root.type == bfd_link_hash_defweak)
12775                             {
12776                               sym_sec = fdh->elf.root.u.def.section;
12777                               sym_value = fdh->elf.root.u.def.value;
12778                               if (sym_sec->output_section != NULL)
12779                                 ok_dest = TRUE;
12780                             }
12781                           else
12782                             fdh = NULL;
12783                         }
12784                     }
12785                   else
12786                     {
12787                       bfd_set_error (bfd_error_bad_value);
12788                       goto error_ret_free_internal;
12789                     }
12790
12791                   destination = 0;
12792                   local_off = 0;
12793                   if (ok_dest)
12794                     {
12795                       sym_value += irela->r_addend;
12796                       destination = (sym_value
12797                                      + sym_sec->output_offset
12798                                      + sym_sec->output_section->vma);
12799                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12800                                                             ? hash->elf.other
12801                                                             : sym->st_other);
12802                     }
12803
12804                   code_sec = sym_sec;
12805                   code_value = sym_value;
12806                   opd = get_opd_info (sym_sec);
12807                   if (opd != NULL)
12808                     {
12809                       bfd_vma dest;
12810
12811                       if (hash == NULL && opd->adjust != NULL)
12812                         {
12813                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12814                           if (adjust == -1)
12815                             continue;
12816                           code_value += adjust;
12817                           sym_value += adjust;
12818                         }
12819                       dest = opd_entry_value (sym_sec, sym_value,
12820                                               &code_sec, &code_value, FALSE);
12821                       if (dest != (bfd_vma) -1)
12822                         {
12823                           destination = dest;
12824                           if (fdh != NULL)
12825                             {
12826                               /* Fixup old ABI sym to point at code
12827                                  entry.  */
12828                               hash->elf.root.type = bfd_link_hash_defweak;
12829                               hash->elf.root.u.def.section = code_sec;
12830                               hash->elf.root.u.def.value = code_value;
12831                             }
12832                         }
12833                     }
12834
12835                   /* Determine what (if any) linker stub is needed.  */
12836                   plt_ent = NULL;
12837                   stub_type = ppc_type_of_stub (section, irela, &hash,
12838                                                 &plt_ent, destination,
12839                                                 local_off);
12840
12841                   if (stub_type != ppc_stub_plt_call)
12842                     {
12843                       /* Check whether we need a TOC adjusting stub.
12844                          Since the linker pastes together pieces from
12845                          different object files when creating the
12846                          _init and _fini functions, it may be that a
12847                          call to what looks like a local sym is in
12848                          fact a call needing a TOC adjustment.  */
12849                       if (code_sec != NULL
12850                           && code_sec->output_section != NULL
12851                           && (htab->sec_info[code_sec->id].toc_off
12852                               != htab->sec_info[section->id].toc_off)
12853                           && (code_sec->has_toc_reloc
12854                               || code_sec->makes_toc_func_call))
12855                         stub_type = ppc_stub_long_branch_r2off;
12856                     }
12857
12858                   if (stub_type == ppc_stub_none)
12859                     continue;
12860
12861                   /* __tls_get_addr calls might be eliminated.  */
12862                   if (stub_type != ppc_stub_plt_call
12863                       && hash != NULL
12864                       && (hash == htab->tls_get_addr
12865                           || hash == htab->tls_get_addr_fd)
12866                       && section->has_tls_reloc
12867                       && irela != internal_relocs)
12868                     {
12869                       /* Get tls info.  */
12870                       unsigned char *tls_mask;
12871
12872                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12873                                          irela - 1, input_bfd))
12874                         goto error_ret_free_internal;
12875                       if ((*tls_mask & TLS_TLS) != 0)
12876                         continue;
12877                     }
12878
12879                   if (stub_type == ppc_stub_plt_call)
12880                     {
12881                       if (!htab->opd_abi
12882                           && htab->params->plt_localentry0 != 0
12883                           && is_elfv2_localentry0 (&hash->elf))
12884                         htab->has_plt_localentry0 = 1;
12885                       else if (irela + 1 < irelaend
12886                                && irela[1].r_offset == irela->r_offset + 4
12887                                && (ELF64_R_TYPE (irela[1].r_info)
12888                                    == R_PPC64_TOCSAVE))
12889                         {
12890                           if (!tocsave_find (htab, INSERT,
12891                                              &local_syms, irela + 1, input_bfd))
12892                             goto error_ret_free_internal;
12893                         }
12894                       else
12895                         stub_type = ppc_stub_plt_call_r2save;
12896                     }
12897
12898                   /* Support for grouping stub sections.  */
12899                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12900
12901                   /* Get the name of this stub.  */
12902                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12903                   if (!stub_name)
12904                     goto error_ret_free_internal;
12905
12906                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12907                                                      stub_name, FALSE, FALSE);
12908                   if (stub_entry != NULL)
12909                     {
12910                       /* The proper stub has already been created.  */
12911                       free (stub_name);
12912                       if (stub_type == ppc_stub_plt_call_r2save)
12913                         stub_entry->stub_type = stub_type;
12914                       continue;
12915                     }
12916
12917                   stub_entry = ppc_add_stub (stub_name, section, info);
12918                   if (stub_entry == NULL)
12919                     {
12920                       free (stub_name);
12921                     error_ret_free_internal:
12922                       if (elf_section_data (section)->relocs == NULL)
12923                         free (internal_relocs);
12924                     error_ret_free_local:
12925                       if (local_syms != NULL
12926                           && (symtab_hdr->contents
12927                               != (unsigned char *) local_syms))
12928                         free (local_syms);
12929                       return FALSE;
12930                     }
12931
12932                   stub_entry->stub_type = stub_type;
12933                   if (stub_type != ppc_stub_plt_call
12934                       && stub_type != ppc_stub_plt_call_r2save)
12935                     {
12936                       stub_entry->target_value = code_value;
12937                       stub_entry->target_section = code_sec;
12938                     }
12939                   else
12940                     {
12941                       stub_entry->target_value = sym_value;
12942                       stub_entry->target_section = sym_sec;
12943                     }
12944                   stub_entry->h = hash;
12945                   stub_entry->plt_ent = plt_ent;
12946                   stub_entry->symtype
12947                     = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12948                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12949
12950                   if (stub_entry->h != NULL)
12951                     htab->stub_globals += 1;
12952                 }
12953
12954               /* We're done with the internal relocs, free them.  */
12955               if (elf_section_data (section)->relocs != internal_relocs)
12956                 free (internal_relocs);
12957             }
12958
12959           if (local_syms != NULL
12960               && symtab_hdr->contents != (unsigned char *) local_syms)
12961             {
12962               if (!info->keep_memory)
12963                 free (local_syms);
12964               else
12965                 symtab_hdr->contents = (unsigned char *) local_syms;
12966             }
12967         }
12968
12969       /* We may have added some stubs.  Find out the new size of the
12970          stub sections.  */
12971       for (group = htab->group; group != NULL; group = group->next)
12972         if (group->stub_sec != NULL)
12973           {
12974             asection *stub_sec = group->stub_sec;
12975
12976             if (htab->stub_iteration <= STUB_SHRINK_ITER
12977                 || stub_sec->rawsize < stub_sec->size)
12978               /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12979               stub_sec->rawsize = stub_sec->size;
12980             stub_sec->size = 0;
12981             stub_sec->reloc_count = 0;
12982             stub_sec->flags &= ~SEC_RELOC;
12983           }
12984
12985       if (htab->stub_iteration <= STUB_SHRINK_ITER
12986           || htab->brlt->rawsize < htab->brlt->size)
12987         htab->brlt->rawsize = htab->brlt->size;
12988       htab->brlt->size = 0;
12989       htab->brlt->reloc_count = 0;
12990       htab->brlt->flags &= ~SEC_RELOC;
12991       if (htab->relbrlt != NULL)
12992         htab->relbrlt->size = 0;
12993
12994       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12995
12996       for (group = htab->group; group != NULL; group = group->next)
12997         if (group->needs_save_res)
12998           group->stub_sec->size += htab->sfpr->size;
12999
13000       if (info->emitrelocations
13001           && htab->glink != NULL && htab->glink->size != 0)
13002         {
13003           htab->glink->reloc_count = 1;
13004           htab->glink->flags |= SEC_RELOC;
13005         }
13006
13007       if (htab->glink_eh_frame != NULL
13008           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13009           && htab->glink_eh_frame->output_section->size > 8)
13010         {
13011           size_t size = 0, align = 4;
13012
13013           for (group = htab->group; group != NULL; group = group->next)
13014             if (group->stub_sec != NULL)
13015               size += stub_eh_frame_size (group, align);
13016           if (htab->glink != NULL && htab->glink->size != 0)
13017             size += (24 + align - 1) & -align;
13018           if (size != 0)
13019             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13020           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13021           size = (size + align - 1) & -align;
13022           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13023           htab->glink_eh_frame->size = size;
13024         }
13025
13026       if (htab->params->plt_stub_align != 0)
13027         for (group = htab->group; group != NULL; group = group->next)
13028           if (group->stub_sec != NULL)
13029             {
13030               int align = abs (htab->params->plt_stub_align);
13031               group->stub_sec->size
13032                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13033             }
13034
13035       for (group = htab->group; group != NULL; group = group->next)
13036         if (group->stub_sec != NULL
13037             && group->stub_sec->rawsize != group->stub_sec->size
13038             && (htab->stub_iteration <= STUB_SHRINK_ITER
13039                 || group->stub_sec->rawsize < group->stub_sec->size))
13040           break;
13041
13042       if (group == NULL
13043           && (htab->brlt->rawsize == htab->brlt->size
13044               || (htab->stub_iteration > STUB_SHRINK_ITER
13045                   && htab->brlt->rawsize > htab->brlt->size))
13046           && (htab->glink_eh_frame == NULL
13047               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13048         break;
13049
13050       /* Ask the linker to do its stuff.  */
13051       (*htab->params->layout_sections_again) ();
13052     }
13053
13054   if (htab->glink_eh_frame != NULL
13055       && htab->glink_eh_frame->size != 0)
13056     {
13057       bfd_vma val;
13058       bfd_byte *p, *last_fde;
13059       size_t last_fde_len, size, align, pad;
13060       struct map_stub *group;
13061
13062       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13063       if (p == NULL)
13064         return FALSE;
13065       htab->glink_eh_frame->contents = p;
13066       last_fde = p;
13067       align = 4;
13068
13069       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13070       /* CIE length (rewrite in case little-endian).  */
13071       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13072       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13073       p += last_fde_len + 4;
13074
13075       for (group = htab->group; group != NULL; group = group->next)
13076         if (group->stub_sec != NULL)
13077           {
13078             last_fde = p;
13079             last_fde_len = stub_eh_frame_size (group, align) - 4;
13080             /* FDE length.  */
13081             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13082             p += 4;
13083             /* CIE pointer.  */
13084             val = p - htab->glink_eh_frame->contents;
13085             bfd_put_32 (htab->elf.dynobj, val, p);
13086             p += 4;
13087             /* Offset to stub section, written later.  */
13088             p += 4;
13089             /* stub section size.  */
13090             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13091             p += 4;
13092             /* Augmentation.  */
13093             p += 1;
13094             if (group->tls_get_addr_opt_bctrl != -1u)
13095               {
13096                 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13097
13098                 /* This FDE needs more than just the default.
13099                    Describe __tls_get_addr_opt stub LR.  */
13100                 if (to_bctrl < 64)
13101                   *p++ = DW_CFA_advance_loc + to_bctrl;
13102                 else if (to_bctrl < 256)
13103                   {
13104                     *p++ = DW_CFA_advance_loc1;
13105                     *p++ = to_bctrl;
13106                   }
13107                 else if (to_bctrl < 65536)
13108                   {
13109                     *p++ = DW_CFA_advance_loc2;
13110                     bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13111                     p += 2;
13112                   }
13113                 else
13114                   {
13115                     *p++ = DW_CFA_advance_loc4;
13116                     bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13117                     p += 4;
13118                   }
13119                 *p++ = DW_CFA_offset_extended_sf;
13120                 *p++ = 65;
13121                 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13122                 *p++ = DW_CFA_advance_loc + 4;
13123                 *p++ = DW_CFA_restore_extended;
13124                 *p++ = 65;
13125               }
13126             /* Pad.  */
13127             p = last_fde + last_fde_len + 4;
13128           }
13129       if (htab->glink != NULL && htab->glink->size != 0)
13130         {
13131           last_fde = p;
13132           last_fde_len = ((24 + align - 1) & -align) - 4;
13133           /* FDE length.  */
13134           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13135           p += 4;
13136           /* CIE pointer.  */
13137           val = p - htab->glink_eh_frame->contents;
13138           bfd_put_32 (htab->elf.dynobj, val, p);
13139           p += 4;
13140           /* Offset to .glink, written later.  */
13141           p += 4;
13142           /* .glink size.  */
13143           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13144           p += 4;
13145           /* Augmentation.  */
13146           p += 1;
13147
13148           *p++ = DW_CFA_advance_loc + 1;
13149           *p++ = DW_CFA_register;
13150           *p++ = 65;
13151           *p++ = htab->opd_abi ? 12 : 0;
13152           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13153           *p++ = DW_CFA_restore_extended;
13154           *p++ = 65;
13155           p += ((24 + align - 1) & -align) - 24;
13156         }
13157       /* Subsume any padding into the last FDE if user .eh_frame
13158          sections are aligned more than glink_eh_frame.  Otherwise any
13159          zero padding will be seen as a terminator.  */
13160       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13161       size = p - htab->glink_eh_frame->contents;
13162       pad = ((size + align - 1) & -align) - size;
13163       htab->glink_eh_frame->size = size + pad;
13164       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13165     }
13166
13167   maybe_strip_output (info, htab->brlt);
13168   if (htab->glink_eh_frame != NULL)
13169     maybe_strip_output (info, htab->glink_eh_frame);
13170
13171   return TRUE;
13172 }
13173
13174 /* Called after we have determined section placement.  If sections
13175    move, we'll be called again.  Provide a value for TOCstart.  */
13176
13177 bfd_vma
13178 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13179 {
13180   asection *s;
13181   bfd_vma TOCstart, adjust;
13182
13183   if (info != NULL)
13184     {
13185       struct elf_link_hash_entry *h;
13186       struct elf_link_hash_table *htab = elf_hash_table (info);
13187
13188       if (is_elf_hash_table (htab)
13189           && htab->hgot != NULL)
13190         h = htab->hgot;
13191       else
13192         {
13193           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13194           if (is_elf_hash_table (htab))
13195             htab->hgot = h;
13196         }
13197       if (h != NULL
13198           && h->root.type == bfd_link_hash_defined
13199           && !h->root.linker_def
13200           && (!is_elf_hash_table (htab)
13201               || h->def_regular))
13202         {
13203           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13204                       + h->root.u.def.section->output_offset
13205                       + h->root.u.def.section->output_section->vma);
13206           _bfd_set_gp_value (obfd, TOCstart);
13207           return TOCstart;
13208         }
13209     }
13210
13211   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13212      order.  The TOC starts where the first of these sections starts.  */
13213   s = bfd_get_section_by_name (obfd, ".got");
13214   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13215     s = bfd_get_section_by_name (obfd, ".toc");
13216   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13217     s = bfd_get_section_by_name (obfd, ".tocbss");
13218   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13219     s = bfd_get_section_by_name (obfd, ".plt");
13220   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13221     {
13222       /* This may happen for
13223          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13224          .toc directive
13225          o  bad linker script
13226          o --gc-sections and empty TOC sections
13227
13228          FIXME: Warn user?  */
13229
13230       /* Look for a likely section.  We probably won't even be
13231          using TOCstart.  */
13232       for (s = obfd->sections; s != NULL; s = s->next)
13233         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13234                          | SEC_EXCLUDE))
13235             == (SEC_ALLOC | SEC_SMALL_DATA))
13236           break;
13237       if (s == NULL)
13238         for (s = obfd->sections; s != NULL; s = s->next)
13239           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13240               == (SEC_ALLOC | SEC_SMALL_DATA))
13241             break;
13242       if (s == NULL)
13243         for (s = obfd->sections; s != NULL; s = s->next)
13244           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13245               == SEC_ALLOC)
13246             break;
13247       if (s == NULL)
13248         for (s = obfd->sections; s != NULL; s = s->next)
13249           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13250             break;
13251     }
13252
13253   TOCstart = 0;
13254   if (s != NULL)
13255     TOCstart = s->output_section->vma + s->output_offset;
13256
13257   /* Force alignment.  */
13258   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13259   TOCstart -= adjust;
13260   _bfd_set_gp_value (obfd, TOCstart);
13261
13262   if (info != NULL && s != NULL)
13263     {
13264       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13265
13266       if (htab != NULL)
13267         {
13268           if (htab->elf.hgot != NULL)
13269             {
13270               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13271               htab->elf.hgot->root.u.def.section = s;
13272             }
13273         }
13274       else
13275         {
13276           struct bfd_link_hash_entry *bh = NULL;
13277           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13278                                             s, TOC_BASE_OFF - adjust,
13279                                             NULL, FALSE, FALSE, &bh);
13280         }
13281     }
13282   return TOCstart;
13283 }
13284
13285 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13286    write out any global entry stubs, and PLT relocations.  */
13287
13288 static bfd_boolean
13289 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13290 {
13291   struct bfd_link_info *info;
13292   struct ppc_link_hash_table *htab;
13293   struct plt_entry *ent;
13294   asection *s;
13295
13296   if (h->root.type == bfd_link_hash_indirect)
13297     return TRUE;
13298
13299   info = inf;
13300   htab = ppc_hash_table (info);
13301   if (htab == NULL)
13302     return FALSE;
13303
13304   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13305     if (ent->plt.offset != (bfd_vma) -1)
13306       {
13307         /* This symbol has an entry in the procedure linkage
13308            table.  Set it up.  */
13309         Elf_Internal_Rela rela;
13310         asection *plt, *relplt;
13311         bfd_byte *loc;
13312
13313         if (!htab->elf.dynamic_sections_created
13314             || h->dynindx == -1)
13315           {
13316             if (!(h->def_regular
13317                   && (h->root.type == bfd_link_hash_defined
13318                       || h->root.type == bfd_link_hash_defweak)))
13319               continue;
13320             if (h->type == STT_GNU_IFUNC)
13321               {
13322                 plt = htab->elf.iplt;
13323                 relplt = htab->elf.irelplt;
13324                 htab->local_ifunc_resolver = 1;
13325                 if (htab->opd_abi)
13326                   rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13327                 else
13328                   rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13329               }
13330             else
13331               {
13332                 plt = htab->pltlocal;
13333                 if (bfd_link_pic (info))
13334                   {
13335                     relplt = htab->relpltlocal;
13336                     if (htab->opd_abi)
13337                       rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13338                     else
13339                       rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13340                   }
13341                 else
13342                   relplt = NULL;
13343               }
13344             rela.r_addend = (h->root.u.def.value
13345                              + h->root.u.def.section->output_offset
13346                              + h->root.u.def.section->output_section->vma
13347                              + ent->addend);
13348
13349             if (relplt == NULL)
13350               {
13351                 loc = plt->contents + ent->plt.offset;
13352                 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13353                 if (htab->opd_abi)
13354                   {
13355                     bfd_vma toc = elf_gp (info->output_bfd);
13356                     toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13357                     bfd_put_64 (info->output_bfd, toc, loc + 8);
13358                   }
13359               }
13360             else
13361               {
13362                 rela.r_offset = (plt->output_section->vma
13363                                  + plt->output_offset
13364                                  + ent->plt.offset);
13365                 loc = relplt->contents + (relplt->reloc_count++
13366                                           * sizeof (Elf64_External_Rela));
13367                 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13368               }
13369           }
13370         else
13371           {
13372             rela.r_offset = (htab->elf.splt->output_section->vma
13373                              + htab->elf.splt->output_offset
13374                              + ent->plt.offset);
13375             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13376             rela.r_addend = ent->addend;
13377             loc = (htab->elf.srelplt->contents
13378                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13379                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13380             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13381               htab->maybe_local_ifunc_resolver = 1;
13382             bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13383           }
13384       }
13385
13386   if (!h->pointer_equality_needed)
13387     return TRUE;
13388
13389   if (h->def_regular)
13390     return TRUE;
13391
13392   s = htab->global_entry;
13393   if (s == NULL || s->size == 0)
13394     return TRUE;
13395
13396   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13397     if (ent->plt.offset != (bfd_vma) -1
13398         && ent->addend == 0)
13399       {
13400         bfd_byte *p;
13401         asection *plt;
13402         bfd_vma off;
13403
13404         p = s->contents + h->root.u.def.value;
13405         plt = htab->elf.splt;
13406         if (!htab->elf.dynamic_sections_created
13407             || h->dynindx == -1)
13408           {
13409             if (h->type == STT_GNU_IFUNC)
13410               plt = htab->elf.iplt;
13411             else
13412               plt = htab->pltlocal;
13413           }
13414         off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13415         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13416
13417         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13418           {
13419             info->callbacks->einfo
13420               (_("%P: linkage table error against `%pT'\n"),
13421                h->root.root.string);
13422             bfd_set_error (bfd_error_bad_value);
13423             htab->stub_error = TRUE;
13424           }
13425
13426         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13427         if (htab->params->emit_stub_syms)
13428           {
13429             size_t len = strlen (h->root.root.string);
13430             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13431
13432             if (name == NULL)
13433               return FALSE;
13434
13435             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13436             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13437             if (h == NULL)
13438               return FALSE;
13439             if (h->root.type == bfd_link_hash_new)
13440               {
13441                 h->root.type = bfd_link_hash_defined;
13442                 h->root.u.def.section = s;
13443                 h->root.u.def.value = p - s->contents;
13444                 h->ref_regular = 1;
13445                 h->def_regular = 1;
13446                 h->ref_regular_nonweak = 1;
13447                 h->forced_local = 1;
13448                 h->non_elf = 0;
13449                 h->root.linker_def = 1;
13450               }
13451           }
13452
13453         if (PPC_HA (off) != 0)
13454           {
13455             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13456             p += 4;
13457           }
13458         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13459         p += 4;
13460         bfd_put_32 (s->owner, MTCTR_R12, p);
13461         p += 4;
13462         bfd_put_32 (s->owner, BCTR, p);
13463         break;
13464       }
13465   return TRUE;
13466 }
13467
13468 /* Write PLT relocs for locals.  */
13469
13470 static bfd_boolean
13471 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13472 {
13473   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13474   bfd *ibfd;
13475
13476   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13477     {
13478       struct got_entry **lgot_ents, **end_lgot_ents;
13479       struct plt_entry **local_plt, **lplt, **end_local_plt;
13480       Elf_Internal_Shdr *symtab_hdr;
13481       bfd_size_type locsymcount;
13482       Elf_Internal_Sym *local_syms = NULL;
13483       struct plt_entry *ent;
13484
13485       if (!is_ppc64_elf (ibfd))
13486         continue;
13487
13488       lgot_ents = elf_local_got_ents (ibfd);
13489       if (!lgot_ents)
13490         continue;
13491
13492       symtab_hdr = &elf_symtab_hdr (ibfd);
13493       locsymcount = symtab_hdr->sh_info;
13494       end_lgot_ents = lgot_ents + locsymcount;
13495       local_plt = (struct plt_entry **) end_lgot_ents;
13496       end_local_plt = local_plt + locsymcount;
13497       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13498         for (ent = *lplt; ent != NULL; ent = ent->next)
13499           if (ent->plt.offset != (bfd_vma) -1)
13500             {
13501               Elf_Internal_Sym *sym;
13502               asection *sym_sec;
13503               asection *plt, *relplt;
13504               bfd_byte *loc;
13505               bfd_vma val;
13506
13507               if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13508                               lplt - local_plt, ibfd))
13509                 {
13510                   if (local_syms != NULL
13511                       && symtab_hdr->contents != (unsigned char *) local_syms)
13512                     free (local_syms);
13513                   return FALSE;
13514                 }
13515
13516               val = sym->st_value + ent->addend;
13517               val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13518               if (sym_sec != NULL && sym_sec->output_section != NULL)
13519                 val += sym_sec->output_offset + sym_sec->output_section->vma;
13520
13521               if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13522                 {
13523                   htab->local_ifunc_resolver = 1;
13524                   plt = htab->elf.iplt;
13525                   relplt = htab->elf.irelplt;
13526                 }
13527               else
13528                 {
13529                   plt = htab->pltlocal;
13530                   relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13531                 }
13532
13533               if (relplt == NULL)
13534                 {
13535                   loc = plt->contents + ent->plt.offset;
13536                   bfd_put_64 (info->output_bfd, val, loc);
13537                   if (htab->opd_abi)
13538                     {
13539                       bfd_vma toc = elf_gp (ibfd);
13540                       bfd_put_64 (info->output_bfd, toc, loc + 8);
13541                     }
13542                 }
13543               else
13544                 {
13545                   Elf_Internal_Rela rela;
13546                   rela.r_offset = (ent->plt.offset
13547                                    + plt->output_offset
13548                                    + plt->output_section->vma);
13549                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13550                     {
13551                       if (htab->opd_abi)
13552                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13553                       else
13554                         rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13555                     }
13556                   else
13557                     {
13558                       if (htab->opd_abi)
13559                         rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13560                       else
13561                         rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13562                     }
13563                   rela.r_addend = val;
13564                   loc = relplt->contents + (relplt->reloc_count++
13565                                             * sizeof (Elf64_External_Rela));
13566                   bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13567                 }
13568             }
13569
13570       if (local_syms != NULL
13571           && symtab_hdr->contents != (unsigned char *) local_syms)
13572         {
13573           if (!info->keep_memory)
13574             free (local_syms);
13575           else
13576             symtab_hdr->contents = (unsigned char *) local_syms;
13577         }
13578     }
13579   return TRUE;
13580 }
13581
13582 /* Build all the stubs associated with the current output file.
13583    The stubs are kept in a hash table attached to the main linker
13584    hash table.  This function is called via gldelf64ppc_finish.  */
13585
13586 bfd_boolean
13587 ppc64_elf_build_stubs (struct bfd_link_info *info,
13588                        char **stats)
13589 {
13590   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13591   struct map_stub *group;
13592   asection *stub_sec;
13593   bfd_byte *p;
13594   int stub_sec_count = 0;
13595
13596   if (htab == NULL)
13597     return FALSE;
13598
13599   /* Allocate memory to hold the linker stubs.  */
13600   for (group = htab->group; group != NULL; group = group->next)
13601     if ((stub_sec = group->stub_sec) != NULL
13602         && stub_sec->size != 0)
13603       {
13604         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13605         if (stub_sec->contents == NULL)
13606           return FALSE;
13607         stub_sec->size = 0;
13608       }
13609
13610   if (htab->glink != NULL && htab->glink->size != 0)
13611     {
13612       unsigned int indx;
13613       bfd_vma plt0;
13614
13615       /* Build the .glink plt call stub.  */
13616       if (htab->params->emit_stub_syms)
13617         {
13618           struct elf_link_hash_entry *h;
13619           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13620                                     TRUE, FALSE, FALSE);
13621           if (h == NULL)
13622             return FALSE;
13623           if (h->root.type == bfd_link_hash_new)
13624             {
13625               h->root.type = bfd_link_hash_defined;
13626               h->root.u.def.section = htab->glink;
13627               h->root.u.def.value = 8;
13628               h->ref_regular = 1;
13629               h->def_regular = 1;
13630               h->ref_regular_nonweak = 1;
13631               h->forced_local = 1;
13632               h->non_elf = 0;
13633               h->root.linker_def = 1;
13634             }
13635         }
13636       plt0 = (htab->elf.splt->output_section->vma
13637               + htab->elf.splt->output_offset
13638               - 16);
13639       if (info->emitrelocations)
13640         {
13641           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13642           if (r == NULL)
13643             return FALSE;
13644           r->r_offset = (htab->glink->output_offset
13645                          + htab->glink->output_section->vma);
13646           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13647           r->r_addend = plt0;
13648         }
13649       p = htab->glink->contents;
13650       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13651       bfd_put_64 (htab->glink->owner, plt0, p);
13652       p += 8;
13653       if (htab->opd_abi)
13654         {
13655           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13656           p += 4;
13657           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13658           p += 4;
13659           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13660           p += 4;
13661           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13662           p += 4;
13663           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13664           p += 4;
13665           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13666           p += 4;
13667           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13668           p += 4;
13669           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13670           p += 4;
13671           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13672           p += 4;
13673           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13674           p += 4;
13675         }
13676       else
13677         {
13678           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13679           p += 4;
13680           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13681           p += 4;
13682           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13683           p += 4;
13684           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13685           p += 4;
13686           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13687           p += 4;
13688           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13689           p += 4;
13690           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13691           p += 4;
13692           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13693           p += 4;
13694           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13695           p += 4;
13696           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13697           p += 4;
13698           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13699           p += 4;
13700           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13701           p += 4;
13702           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13703           p += 4;
13704         }
13705       bfd_put_32 (htab->glink->owner, BCTR, p);
13706       p += 4;
13707       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13708
13709       /* Build the .glink lazy link call stubs.  */
13710       indx = 0;
13711       while (p < htab->glink->contents + htab->glink->size)
13712         {
13713           if (htab->opd_abi)
13714             {
13715               if (indx < 0x8000)
13716                 {
13717                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13718                   p += 4;
13719                 }
13720               else
13721                 {
13722                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13723                   p += 4;
13724                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13725                               p);
13726                   p += 4;
13727                 }
13728             }
13729           bfd_put_32 (htab->glink->owner,
13730                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13731           indx++;
13732           p += 4;
13733         }
13734     }
13735
13736   /* Build .glink global entry stubs, and PLT relocs for globals.  */
13737   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13738
13739   if (!write_plt_relocs_for_local_syms (info))
13740     return FALSE;
13741
13742   if (htab->brlt != NULL && htab->brlt->size != 0)
13743     {
13744       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13745                                          htab->brlt->size);
13746       if (htab->brlt->contents == NULL)
13747         return FALSE;
13748     }
13749   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13750     {
13751       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13752                                             htab->relbrlt->size);
13753       if (htab->relbrlt->contents == NULL)
13754         return FALSE;
13755     }
13756
13757   /* Build the stubs as directed by the stub hash table.  */
13758   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13759
13760   for (group = htab->group; group != NULL; group = group->next)
13761     if (group->needs_save_res)
13762       group->stub_sec->size += htab->sfpr->size;
13763
13764   if (htab->relbrlt != NULL)
13765     htab->relbrlt->reloc_count = 0;
13766
13767   if (htab->params->plt_stub_align != 0)
13768     for (group = htab->group; group != NULL; group = group->next)
13769       if ((stub_sec = group->stub_sec) != NULL)
13770         {
13771           int align = abs (htab->params->plt_stub_align);
13772           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13773         }
13774
13775   for (group = htab->group; group != NULL; group = group->next)
13776     if (group->needs_save_res)
13777       {
13778         stub_sec = group->stub_sec;
13779         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13780                 htab->sfpr->contents, htab->sfpr->size);
13781         if (htab->params->emit_stub_syms)
13782           {
13783             unsigned int i;
13784
13785             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13786               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13787                 return FALSE;
13788           }
13789       }
13790
13791   for (group = htab->group; group != NULL; group = group->next)
13792     if ((stub_sec = group->stub_sec) != NULL)
13793       {
13794         stub_sec_count += 1;
13795         if (stub_sec->rawsize != stub_sec->size
13796             && (htab->stub_iteration <= STUB_SHRINK_ITER
13797                 || stub_sec->rawsize < stub_sec->size))
13798           break;
13799       }
13800
13801   if (group != NULL)
13802     {
13803       htab->stub_error = TRUE;
13804       _bfd_error_handler (_("stubs don't match calculated size"));
13805     }
13806
13807   if (htab->stub_error)
13808     return FALSE;
13809
13810   if (stats != NULL)
13811     {
13812       size_t len;
13813       *stats = bfd_malloc (500);
13814       if (*stats == NULL)
13815         return FALSE;
13816
13817       len = sprintf (*stats,
13818                      ngettext ("linker stubs in %u group\n",
13819                                "linker stubs in %u groups\n",
13820                                stub_sec_count),
13821                      stub_sec_count);
13822       sprintf (*stats + len, _("  branch       %lu\n"
13823                                "  toc adjust   %lu\n"
13824                                "  long branch  %lu\n"
13825                                "  long toc adj %lu\n"
13826                                "  plt call     %lu\n"
13827                                "  plt call toc %lu\n"
13828                                "  global entry %lu"),
13829                htab->stub_count[ppc_stub_long_branch - 1],
13830                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13831                htab->stub_count[ppc_stub_plt_branch - 1],
13832                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13833                htab->stub_count[ppc_stub_plt_call - 1],
13834                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13835                htab->stub_count[ppc_stub_global_entry - 1]);
13836     }
13837   return TRUE;
13838 }
13839
13840 /* What to do when ld finds relocations against symbols defined in
13841    discarded sections.  */
13842
13843 static unsigned int
13844 ppc64_elf_action_discarded (asection *sec)
13845 {
13846   if (strcmp (".opd", sec->name) == 0)
13847     return 0;
13848
13849   if (strcmp (".toc", sec->name) == 0)
13850     return 0;
13851
13852   if (strcmp (".toc1", sec->name) == 0)
13853     return 0;
13854
13855   return _bfd_elf_default_action_discarded (sec);
13856 }
13857
13858 /* The RELOCATE_SECTION function is called by the ELF backend linker
13859    to handle the relocations for a section.
13860
13861    The relocs are always passed as Rela structures; if the section
13862    actually uses Rel structures, the r_addend field will always be
13863    zero.
13864
13865    This function is responsible for adjust the section contents as
13866    necessary, and (if using Rela relocs and generating a
13867    relocatable output file) adjusting the reloc addend as
13868    necessary.
13869
13870    This function does not have to worry about setting the reloc
13871    address or the reloc symbol index.
13872
13873    LOCAL_SYMS is a pointer to the swapped in local symbols.
13874
13875    LOCAL_SECTIONS is an array giving the section in the input file
13876    corresponding to the st_shndx field of each local symbol.
13877
13878    The global hash table entry for the global symbols can be found
13879    via elf_sym_hashes (input_bfd).
13880
13881    When generating relocatable output, this function must handle
13882    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13883    going to be the section symbol corresponding to the output
13884    section, which means that the addend must be adjusted
13885    accordingly.  */
13886
13887 static bfd_boolean
13888 ppc64_elf_relocate_section (bfd *output_bfd,
13889                             struct bfd_link_info *info,
13890                             bfd *input_bfd,
13891                             asection *input_section,
13892                             bfd_byte *contents,
13893                             Elf_Internal_Rela *relocs,
13894                             Elf_Internal_Sym *local_syms,
13895                             asection **local_sections)
13896 {
13897   struct ppc_link_hash_table *htab;
13898   Elf_Internal_Shdr *symtab_hdr;
13899   struct elf_link_hash_entry **sym_hashes;
13900   Elf_Internal_Rela *rel;
13901   Elf_Internal_Rela *wrel;
13902   Elf_Internal_Rela *relend;
13903   Elf_Internal_Rela outrel;
13904   bfd_byte *loc;
13905   struct got_entry **local_got_ents;
13906   bfd_vma TOCstart;
13907   bfd_boolean ret = TRUE;
13908   bfd_boolean is_opd;
13909   /* Assume 'at' branch hints.  */
13910   bfd_boolean is_isa_v2 = TRUE;
13911   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13912
13913   /* Initialize howto table if needed.  */
13914   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13915     ppc_howto_init ();
13916
13917   htab = ppc_hash_table (info);
13918   if (htab == NULL)
13919     return FALSE;
13920
13921   /* Don't relocate stub sections.  */
13922   if (input_section->owner == htab->params->stub_bfd)
13923     return TRUE;
13924
13925   BFD_ASSERT (is_ppc64_elf (input_bfd));
13926
13927   local_got_ents = elf_local_got_ents (input_bfd);
13928   TOCstart = elf_gp (output_bfd);
13929   symtab_hdr = &elf_symtab_hdr (input_bfd);
13930   sym_hashes = elf_sym_hashes (input_bfd);
13931   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13932
13933   rel = wrel = relocs;
13934   relend = relocs + input_section->reloc_count;
13935   for (; rel < relend; wrel++, rel++)
13936     {
13937       enum elf_ppc64_reloc_type r_type;
13938       bfd_vma addend;
13939       bfd_reloc_status_type r;
13940       Elf_Internal_Sym *sym;
13941       asection *sec;
13942       struct elf_link_hash_entry *h_elf;
13943       struct ppc_link_hash_entry *h;
13944       struct ppc_link_hash_entry *fdh;
13945       const char *sym_name;
13946       unsigned long r_symndx, toc_symndx;
13947       bfd_vma toc_addend;
13948       unsigned char tls_mask, tls_gd, tls_type;
13949       unsigned char sym_type;
13950       bfd_vma relocation;
13951       bfd_boolean unresolved_reloc, save_unresolved_reloc;
13952       bfd_boolean warned;
13953       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13954       unsigned int insn;
13955       unsigned int mask;
13956       struct ppc_stub_hash_entry *stub_entry;
13957       bfd_vma max_br_offset;
13958       bfd_vma from;
13959       Elf_Internal_Rela orig_rel;
13960       reloc_howto_type *howto;
13961       struct reloc_howto_struct alt_howto;
13962
13963     again:
13964       orig_rel = *rel;
13965
13966       r_type = ELF64_R_TYPE (rel->r_info);
13967       r_symndx = ELF64_R_SYM (rel->r_info);
13968
13969       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13970          symbol of the previous ADDR64 reloc.  The symbol gives us the
13971          proper TOC base to use.  */
13972       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13973           && wrel != relocs
13974           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13975           && is_opd)
13976         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13977
13978       sym = NULL;
13979       sec = NULL;
13980       h_elf = NULL;
13981       sym_name = NULL;
13982       unresolved_reloc = FALSE;
13983       warned = FALSE;
13984
13985       if (r_symndx < symtab_hdr->sh_info)
13986         {
13987           /* It's a local symbol.  */
13988           struct _opd_sec_data *opd;
13989
13990           sym = local_syms + r_symndx;
13991           sec = local_sections[r_symndx];
13992           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13993           sym_type = ELF64_ST_TYPE (sym->st_info);
13994           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13995           opd = get_opd_info (sec);
13996           if (opd != NULL && opd->adjust != NULL)
13997             {
13998               long adjust = opd->adjust[OPD_NDX (sym->st_value
13999                                                  + rel->r_addend)];
14000               if (adjust == -1)
14001                 relocation = 0;
14002               else
14003                 {
14004                   /* If this is a relocation against the opd section sym
14005                      and we have edited .opd, adjust the reloc addend so
14006                      that ld -r and ld --emit-relocs output is correct.
14007                      If it is a reloc against some other .opd symbol,
14008                      then the symbol value will be adjusted later.  */
14009                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14010                     rel->r_addend += adjust;
14011                   else
14012                     relocation += adjust;
14013                 }
14014             }
14015         }
14016       else
14017         {
14018           bfd_boolean ignored;
14019
14020           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14021                                    r_symndx, symtab_hdr, sym_hashes,
14022                                    h_elf, sec, relocation,
14023                                    unresolved_reloc, warned, ignored);
14024           sym_name = h_elf->root.root.string;
14025           sym_type = h_elf->type;
14026           if (sec != NULL
14027               && sec->owner == output_bfd
14028               && strcmp (sec->name, ".opd") == 0)
14029             {
14030               /* This is a symbol defined in a linker script.  All
14031                  such are defined in output sections, even those
14032                  defined by simple assignment from a symbol defined in
14033                  an input section.  Transfer the symbol to an
14034                  appropriate input .opd section, so that a branch to
14035                  this symbol will be mapped to the location specified
14036                  by the opd entry.  */
14037               struct bfd_link_order *lo;
14038               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14039                 if (lo->type == bfd_indirect_link_order)
14040                   {
14041                     asection *isec = lo->u.indirect.section;
14042                     if (h_elf->root.u.def.value >= isec->output_offset
14043                         && h_elf->root.u.def.value < (isec->output_offset
14044                                                       + isec->size))
14045                       {
14046                         h_elf->root.u.def.value -= isec->output_offset;
14047                         h_elf->root.u.def.section = isec;
14048                         sec = isec;
14049                         break;
14050                       }
14051                   }
14052             }
14053         }
14054       h = (struct ppc_link_hash_entry *) h_elf;
14055
14056       if (sec != NULL && discarded_section (sec))
14057         {
14058           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14059                                input_bfd, input_section,
14060                                contents + rel->r_offset);
14061           wrel->r_offset = rel->r_offset;
14062           wrel->r_info = 0;
14063           wrel->r_addend = 0;
14064
14065           /* For ld -r, remove relocations in debug sections against
14066              symbols defined in discarded sections.  Not done for
14067              non-debug to preserve relocs in .eh_frame which the
14068              eh_frame editing code expects to be present.  */
14069           if (bfd_link_relocatable (info)
14070               && (input_section->flags & SEC_DEBUGGING))
14071             wrel--;
14072
14073           continue;
14074         }
14075
14076       if (bfd_link_relocatable (info))
14077         goto copy_reloc;
14078
14079       if (h != NULL && &h->elf == htab->elf.hgot)
14080         {
14081           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14082           sec = bfd_abs_section_ptr;
14083           unresolved_reloc = FALSE;
14084         }
14085
14086       /* TLS optimizations.  Replace instruction sequences and relocs
14087          based on information we collected in tls_optimize.  We edit
14088          RELOCS so that --emit-relocs will output something sensible
14089          for the final instruction stream.  */
14090       tls_mask = 0;
14091       tls_gd = 0;
14092       toc_symndx = 0;
14093       if (h != NULL)
14094         tls_mask = h->tls_mask;
14095       else if (local_got_ents != NULL)
14096         {
14097           struct plt_entry **local_plt = (struct plt_entry **)
14098             (local_got_ents + symtab_hdr->sh_info);
14099           unsigned char *lgot_masks = (unsigned char *)
14100             (local_plt + symtab_hdr->sh_info);
14101           tls_mask = lgot_masks[r_symndx];
14102         }
14103       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14104           && (r_type == R_PPC64_TLS
14105               || r_type == R_PPC64_TLSGD
14106               || r_type == R_PPC64_TLSLD))
14107         {
14108           /* Check for toc tls entries.  */
14109           unsigned char *toc_tls;
14110
14111           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14112                              &local_syms, rel, input_bfd))
14113             return FALSE;
14114
14115           if (toc_tls)
14116             tls_mask = *toc_tls;
14117         }
14118
14119       /* Check that tls relocs are used with tls syms, and non-tls
14120          relocs are used with non-tls syms.  */
14121       if (r_symndx != STN_UNDEF
14122           && r_type != R_PPC64_NONE
14123           && (h == NULL
14124               || h->elf.root.type == bfd_link_hash_defined
14125               || h->elf.root.type == bfd_link_hash_defweak)
14126           && (IS_PPC64_TLS_RELOC (r_type)
14127               != (sym_type == STT_TLS
14128                   || (sym_type == STT_SECTION
14129                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14130         {
14131           if ((tls_mask & TLS_TLS) != 0
14132               && (r_type == R_PPC64_TLS
14133                   || r_type == R_PPC64_TLSGD
14134                   || r_type == R_PPC64_TLSLD))
14135             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
14136             ;
14137           else
14138             info->callbacks->einfo
14139               (!IS_PPC64_TLS_RELOC (r_type)
14140                /* xgettext:c-format */
14141                ? _("%H: %s used with TLS symbol `%pT'\n")
14142                /* xgettext:c-format */
14143                : _("%H: %s used with non-TLS symbol `%pT'\n"),
14144                input_bfd, input_section, rel->r_offset,
14145                ppc64_elf_howto_table[r_type]->name,
14146                sym_name);
14147         }
14148
14149       /* Ensure reloc mapping code below stays sane.  */
14150       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14151           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14152           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
14153           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14154           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14155           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14156           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
14157           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14158           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14159           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14160         abort ();
14161
14162       switch (r_type)
14163         {
14164         default:
14165           break;
14166
14167         case R_PPC64_LO_DS_OPT:
14168           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14169           if ((insn & (0x3f << 26)) != 58u << 26)
14170             abort ();
14171           insn += (14u << 26) - (58u << 26);
14172           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14173           r_type = R_PPC64_TOC16_LO;
14174           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14175           break;
14176
14177         case R_PPC64_TOC16:
14178         case R_PPC64_TOC16_LO:
14179         case R_PPC64_TOC16_DS:
14180         case R_PPC64_TOC16_LO_DS:
14181           {
14182             /* Check for toc tls entries.  */
14183             unsigned char *toc_tls;
14184             int retval;
14185
14186             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14187                                    &local_syms, rel, input_bfd);
14188             if (retval == 0)
14189               return FALSE;
14190
14191             if (toc_tls)
14192               {
14193                 tls_mask = *toc_tls;
14194                 if (r_type == R_PPC64_TOC16_DS
14195                     || r_type == R_PPC64_TOC16_LO_DS)
14196                   {
14197                     if ((tls_mask & TLS_TLS) != 0
14198                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14199                       goto toctprel;
14200                   }
14201                 else
14202                   {
14203                     /* If we found a GD reloc pair, then we might be
14204                        doing a GD->IE transition.  */
14205                     if (retval == 2)
14206                       {
14207                         tls_gd = TLS_TPRELGD;
14208                         if ((tls_mask & TLS_TLS) != 0
14209                             && (tls_mask & TLS_GD) == 0)
14210                           goto tls_ldgd_opt;
14211                       }
14212                     else if (retval == 3)
14213                       {
14214                         if ((tls_mask & TLS_TLS) != 0
14215                             && (tls_mask & TLS_LD) == 0)
14216                           goto tls_ldgd_opt;
14217                       }
14218                   }
14219               }
14220           }
14221           break;
14222
14223         case R_PPC64_GOT_TPREL16_HI:
14224         case R_PPC64_GOT_TPREL16_HA:
14225           if ((tls_mask & TLS_TLS) != 0
14226               && (tls_mask & TLS_TPREL) == 0)
14227             {
14228               rel->r_offset -= d_offset;
14229               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14230               r_type = R_PPC64_NONE;
14231               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14232             }
14233           break;
14234
14235         case R_PPC64_GOT_TPREL16_DS:
14236         case R_PPC64_GOT_TPREL16_LO_DS:
14237           if ((tls_mask & TLS_TLS) != 0
14238               && (tls_mask & TLS_TPREL) == 0)
14239             {
14240             toctprel:
14241               insn = bfd_get_32 (input_bfd,
14242                                  contents + rel->r_offset - d_offset);
14243               insn &= 31 << 21;
14244               insn |= 0x3c0d0000;       /* addis 0,13,0 */
14245               bfd_put_32 (input_bfd, insn,
14246                           contents + rel->r_offset - d_offset);
14247               r_type = R_PPC64_TPREL16_HA;
14248               if (toc_symndx != 0)
14249                 {
14250                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14251                   rel->r_addend = toc_addend;
14252                   /* We changed the symbol.  Start over in order to
14253                      get h, sym, sec etc. right.  */
14254                   goto again;
14255                 }
14256               else
14257                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14258             }
14259           break;
14260
14261         case R_PPC64_TLS:
14262           if ((tls_mask & TLS_TLS) != 0
14263               && (tls_mask & TLS_TPREL) == 0)
14264             {
14265               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14266               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14267               if (insn == 0)
14268                 abort ();
14269               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14270               /* Was PPC64_TLS which sits on insn boundary, now
14271                  PPC64_TPREL16_LO which is at low-order half-word.  */
14272               rel->r_offset += d_offset;
14273               r_type = R_PPC64_TPREL16_LO;
14274               if (toc_symndx != 0)
14275                 {
14276                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14277                   rel->r_addend = toc_addend;
14278                   /* We changed the symbol.  Start over in order to
14279                      get h, sym, sec etc. right.  */
14280                   goto again;
14281                 }
14282               else
14283                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14284             }
14285           break;
14286
14287         case R_PPC64_GOT_TLSGD16_HI:
14288         case R_PPC64_GOT_TLSGD16_HA:
14289           tls_gd = TLS_TPRELGD;
14290           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14291             goto tls_gdld_hi;
14292           break;
14293
14294         case R_PPC64_GOT_TLSLD16_HI:
14295         case R_PPC64_GOT_TLSLD16_HA:
14296           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14297             {
14298             tls_gdld_hi:
14299               if ((tls_mask & tls_gd) != 0)
14300                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14301                           + R_PPC64_GOT_TPREL16_DS);
14302               else
14303                 {
14304                   rel->r_offset -= d_offset;
14305                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14306                   r_type = R_PPC64_NONE;
14307                 }
14308               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14309             }
14310           break;
14311
14312         case R_PPC64_GOT_TLSGD16:
14313         case R_PPC64_GOT_TLSGD16_LO:
14314           tls_gd = TLS_TPRELGD;
14315           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14316             goto tls_ldgd_opt;
14317           break;
14318
14319         case R_PPC64_GOT_TLSLD16:
14320         case R_PPC64_GOT_TLSLD16_LO:
14321           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14322             {
14323               unsigned int insn1, insn2;
14324               bfd_vma offset;
14325
14326             tls_ldgd_opt:
14327               offset = (bfd_vma) -1;
14328               /* If not using the newer R_PPC64_TLSGD/LD to mark
14329                  __tls_get_addr calls, we must trust that the call
14330                  stays with its arg setup insns, ie. that the next
14331                  reloc is the __tls_get_addr call associated with
14332                  the current reloc.  Edit both insns.  */
14333               if (input_section->has_tls_get_addr_call
14334                   && rel + 1 < relend
14335                   && branch_reloc_hash_match (input_bfd, rel + 1,
14336                                               htab->tls_get_addr,
14337                                               htab->tls_get_addr_fd))
14338                 offset = rel[1].r_offset;
14339               /* We read the low GOT_TLS (or TOC16) insn because we
14340                  need to keep the destination reg.  It may be
14341                  something other than the usual r3, and moved to r3
14342                  before the call by intervening code.  */
14343               insn1 = bfd_get_32 (input_bfd,
14344                                   contents + rel->r_offset - d_offset);
14345               if ((tls_mask & tls_gd) != 0)
14346                 {
14347                   /* IE */
14348                   insn1 &= (0x1f << 21) | (0x1f << 16);
14349                   insn1 |= 58 << 26;    /* ld */
14350                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14351                   if (offset != (bfd_vma) -1)
14352                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14353                   if ((tls_mask & TLS_EXPLICIT) == 0)
14354                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14355                               + R_PPC64_GOT_TPREL16_DS);
14356                   else
14357                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14358                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14359                 }
14360               else
14361                 {
14362                   /* LE */
14363                   insn1 &= 0x1f << 21;
14364                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
14365                   insn2 = 0x38630000;   /* addi 3,3,0 */
14366                   if (tls_gd == 0)
14367                     {
14368                       /* Was an LD reloc.  */
14369                       if (toc_symndx)
14370                         sec = local_sections[toc_symndx];
14371                       for (r_symndx = 0;
14372                            r_symndx < symtab_hdr->sh_info;
14373                            r_symndx++)
14374                         if (local_sections[r_symndx] == sec)
14375                           break;
14376                       if (r_symndx >= symtab_hdr->sh_info)
14377                         r_symndx = STN_UNDEF;
14378                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14379                       if (r_symndx != STN_UNDEF)
14380                         rel->r_addend -= (local_syms[r_symndx].st_value
14381                                           + sec->output_offset
14382                                           + sec->output_section->vma);
14383                     }
14384                   else if (toc_symndx != 0)
14385                     {
14386                       r_symndx = toc_symndx;
14387                       rel->r_addend = toc_addend;
14388                     }
14389                   r_type = R_PPC64_TPREL16_HA;
14390                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14391                   if (offset != (bfd_vma) -1)
14392                     {
14393                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14394                                                     R_PPC64_TPREL16_LO);
14395                       rel[1].r_offset = offset + d_offset;
14396                       rel[1].r_addend = rel->r_addend;
14397                     }
14398                 }
14399               bfd_put_32 (input_bfd, insn1,
14400                           contents + rel->r_offset - d_offset);
14401               if (offset != (bfd_vma) -1)
14402                 bfd_put_32 (input_bfd, insn2, contents + offset);
14403               if ((tls_mask & tls_gd) == 0
14404                   && (tls_gd == 0 || toc_symndx != 0))
14405                 {
14406                   /* We changed the symbol.  Start over in order
14407                      to get h, sym, sec etc. right.  */
14408                   goto again;
14409                 }
14410             }
14411           break;
14412
14413         case R_PPC64_TLSGD:
14414           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14415               && rel + 1 < relend)
14416             {
14417               unsigned int insn2;
14418               bfd_vma offset = rel->r_offset;
14419
14420               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14421                 {
14422                   bfd_put_32 (output_bfd, NOP, contents + offset);
14423                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14424                   break;
14425                 }
14426
14427               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14428                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14429
14430               if ((tls_mask & TLS_TPRELGD) != 0)
14431                 {
14432                   /* IE */
14433                   r_type = R_PPC64_NONE;
14434                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14435                 }
14436               else
14437                 {
14438                   /* LE */
14439                   if (toc_symndx != 0)
14440                     {
14441                       r_symndx = toc_symndx;
14442                       rel->r_addend = toc_addend;
14443                     }
14444                   r_type = R_PPC64_TPREL16_LO;
14445                   rel->r_offset = offset + d_offset;
14446                   insn2 = 0x38630000;   /* addi 3,3,0 */
14447                 }
14448               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14449               /* Zap the reloc on the _tls_get_addr call too.  */
14450               BFD_ASSERT (offset == rel[1].r_offset);
14451               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14452               bfd_put_32 (input_bfd, insn2, contents + offset);
14453               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14454                 goto again;
14455             }
14456           break;
14457
14458         case R_PPC64_TLSLD:
14459           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14460               && rel + 1 < relend)
14461             {
14462               unsigned int insn2;
14463               bfd_vma offset = rel->r_offset;
14464
14465               if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14466                 {
14467                   bfd_put_32 (output_bfd, NOP, contents + offset);
14468                   rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14469                   break;
14470                 }
14471
14472               if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14473                 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14474
14475               if (toc_symndx)
14476                 sec = local_sections[toc_symndx];
14477               for (r_symndx = 0;
14478                    r_symndx < symtab_hdr->sh_info;
14479                    r_symndx++)
14480                 if (local_sections[r_symndx] == sec)
14481                   break;
14482               if (r_symndx >= symtab_hdr->sh_info)
14483                 r_symndx = STN_UNDEF;
14484               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14485               if (r_symndx != STN_UNDEF)
14486                 rel->r_addend -= (local_syms[r_symndx].st_value
14487                                   + sec->output_offset
14488                                   + sec->output_section->vma);
14489
14490               r_type = R_PPC64_TPREL16_LO;
14491               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14492               rel->r_offset = offset + d_offset;
14493               /* Zap the reloc on the _tls_get_addr call too.  */
14494               BFD_ASSERT (offset == rel[1].r_offset);
14495               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14496               insn2 = 0x38630000;       /* addi 3,3,0 */
14497               bfd_put_32 (input_bfd, insn2, contents + offset);
14498               goto again;
14499             }
14500           break;
14501
14502         case R_PPC64_DTPMOD64:
14503           if (rel + 1 < relend
14504               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14505               && rel[1].r_offset == rel->r_offset + 8)
14506             {
14507               if ((tls_mask & TLS_GD) == 0)
14508                 {
14509                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14510                   if ((tls_mask & TLS_TPRELGD) != 0)
14511                     r_type = R_PPC64_TPREL64;
14512                   else
14513                     {
14514                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14515                       r_type = R_PPC64_NONE;
14516                     }
14517                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14518                 }
14519             }
14520           else
14521             {
14522               if ((tls_mask & TLS_LD) == 0)
14523                 {
14524                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14525                   r_type = R_PPC64_NONE;
14526                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14527                 }
14528             }
14529           break;
14530
14531         case R_PPC64_TPREL64:
14532           if ((tls_mask & TLS_TPREL) == 0)
14533             {
14534               r_type = R_PPC64_NONE;
14535               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14536             }
14537           break;
14538
14539         case R_PPC64_ENTRY:
14540           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14541           if (!bfd_link_pic (info)
14542               && !info->traditional_format
14543               && relocation + 0x80008000 <= 0xffffffff)
14544             {
14545               unsigned int insn1, insn2;
14546
14547               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14548               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14549               if ((insn1 & ~0xfffc) == LD_R2_0R12
14550                   && insn2 == ADD_R2_R2_R12)
14551                 {
14552                   bfd_put_32 (input_bfd,
14553                               LIS_R2 + PPC_HA (relocation),
14554                               contents + rel->r_offset);
14555                   bfd_put_32 (input_bfd,
14556                               ADDI_R2_R2 + PPC_LO (relocation),
14557                               contents + rel->r_offset + 4);
14558                 }
14559             }
14560           else
14561             {
14562               relocation -= (rel->r_offset
14563                              + input_section->output_offset
14564                              + input_section->output_section->vma);
14565               if (relocation + 0x80008000 <= 0xffffffff)
14566                 {
14567                   unsigned int insn1, insn2;
14568
14569                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14570                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14571                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14572                       && insn2 == ADD_R2_R2_R12)
14573                     {
14574                       bfd_put_32 (input_bfd,
14575                                   ADDIS_R2_R12 + PPC_HA (relocation),
14576                                   contents + rel->r_offset);
14577                       bfd_put_32 (input_bfd,
14578                                   ADDI_R2_R2 + PPC_LO (relocation),
14579                                   contents + rel->r_offset + 4);
14580                     }
14581                 }
14582             }
14583           break;
14584
14585         case R_PPC64_REL16_HA:
14586           /* If we are generating a non-PIC executable, edit
14587              .  0:      addis 2,12,.TOC.-0b@ha
14588              .          addi 2,2,.TOC.-0b@l
14589              used by ELFv2 global entry points to set up r2, to
14590              .          lis 2,.TOC.@ha
14591              .          addi 2,2,.TOC.@l
14592              if .TOC. is in range.  */
14593           if (!bfd_link_pic (info)
14594               && !info->traditional_format
14595               && !htab->opd_abi
14596               && rel->r_addend == d_offset
14597               && h != NULL && &h->elf == htab->elf.hgot
14598               && rel + 1 < relend
14599               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14600               && rel[1].r_offset == rel->r_offset + 4
14601               && rel[1].r_addend == rel->r_addend + 4
14602               && relocation + 0x80008000 <= 0xffffffff)
14603             {
14604               unsigned int insn1, insn2;
14605               bfd_vma offset = rel->r_offset - d_offset;
14606               insn1 = bfd_get_32 (input_bfd, contents + offset);
14607               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14608               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14609                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14610                 {
14611                   r_type = R_PPC64_ADDR16_HA;
14612                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14613                   rel->r_addend -= d_offset;
14614                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14615                   rel[1].r_addend -= d_offset + 4;
14616                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14617                 }
14618             }
14619           break;
14620         }
14621
14622       /* Handle other relocations that tweak non-addend part of insn.  */
14623       insn = 0;
14624       max_br_offset = 1 << 25;
14625       addend = rel->r_addend;
14626       reloc_dest = DEST_NORMAL;
14627       switch (r_type)
14628         {
14629         default:
14630           break;
14631
14632         case R_PPC64_TOCSAVE:
14633           if (relocation + addend == (rel->r_offset
14634                                       + input_section->output_offset
14635                                       + input_section->output_section->vma)
14636               && tocsave_find (htab, NO_INSERT,
14637                                &local_syms, rel, input_bfd))
14638             {
14639               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14640               if (insn == NOP
14641                   || insn == CROR_151515 || insn == CROR_313131)
14642                 bfd_put_32 (input_bfd,
14643                             STD_R2_0R1 + STK_TOC (htab),
14644                             contents + rel->r_offset);
14645             }
14646           break;
14647
14648           /* Branch taken prediction relocations.  */
14649         case R_PPC64_ADDR14_BRTAKEN:
14650         case R_PPC64_REL14_BRTAKEN:
14651           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14652           /* Fall through.  */
14653
14654           /* Branch not taken prediction relocations.  */
14655         case R_PPC64_ADDR14_BRNTAKEN:
14656         case R_PPC64_REL14_BRNTAKEN:
14657           insn |= bfd_get_32 (input_bfd,
14658                               contents + rel->r_offset) & ~(0x01 << 21);
14659           /* Fall through.  */
14660
14661         case R_PPC64_REL14:
14662           max_br_offset = 1 << 15;
14663           /* Fall through.  */
14664
14665         case R_PPC64_REL24:
14666         case R_PPC64_PLTCALL:
14667           /* Calls to functions with a different TOC, such as calls to
14668              shared objects, need to alter the TOC pointer.  This is
14669              done using a linkage stub.  A REL24 branching to these
14670              linkage stubs needs to be followed by a nop, as the nop
14671              will be replaced with an instruction to restore the TOC
14672              base pointer.  */
14673           fdh = h;
14674           if (h != NULL
14675               && h->oh != NULL
14676               && h->oh->is_func_descriptor)
14677             fdh = ppc_follow_link (h->oh);
14678           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14679                                            htab);
14680           if (r_type == R_PPC64_PLTCALL
14681               && stub_entry != NULL
14682               && (stub_entry->stub_type == ppc_stub_plt_call
14683                   || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14684             stub_entry = NULL;
14685
14686           if (stub_entry != NULL
14687               && (stub_entry->stub_type == ppc_stub_plt_call
14688                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14689                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14690                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14691             {
14692               bfd_boolean can_plt_call = FALSE;
14693
14694               if (stub_entry->stub_type == ppc_stub_plt_call
14695                   && !htab->opd_abi
14696                   && htab->params->plt_localentry0 != 0
14697                   && is_elfv2_localentry0 (&h->elf))
14698                 {
14699                   /* The function doesn't use or change r2.  */
14700                   can_plt_call = TRUE;
14701                 }
14702
14703               /* All of these stubs may modify r2, so there must be a
14704                  branch and link followed by a nop.  The nop is
14705                  replaced by an insn to restore r2.  */
14706               else if (rel->r_offset + 8 <= input_section->size)
14707                 {
14708                   unsigned long br;
14709
14710                   br = bfd_get_32 (input_bfd,
14711                                    contents + rel->r_offset);
14712                   if ((br & 1) != 0)
14713                     {
14714                       unsigned long nop;
14715
14716                       nop = bfd_get_32 (input_bfd,
14717                                         contents + rel->r_offset + 4);
14718                       if (nop == LD_R2_0R1 + STK_TOC (htab))
14719                         can_plt_call = TRUE;
14720                       else if (nop == NOP
14721                                || nop == CROR_151515
14722                                || nop == CROR_313131)
14723                         {
14724                           if (h != NULL
14725                               && (h == htab->tls_get_addr_fd
14726                                   || h == htab->tls_get_addr)
14727                               && htab->params->tls_get_addr_opt)
14728                             {
14729                               /* Special stub used, leave nop alone.  */
14730                             }
14731                           else
14732                             bfd_put_32 (input_bfd,
14733                                         LD_R2_0R1 + STK_TOC (htab),
14734                                         contents + rel->r_offset + 4);
14735                           can_plt_call = TRUE;
14736                         }
14737                     }
14738                 }
14739
14740               if (!can_plt_call && h != NULL)
14741                 {
14742                   const char *name = h->elf.root.root.string;
14743
14744                   if (*name == '.')
14745                     ++name;
14746
14747                   if (strncmp (name, "__libc_start_main", 17) == 0
14748                       && (name[17] == 0 || name[17] == '@'))
14749                     {
14750                       /* Allow crt1 branch to go via a toc adjusting
14751                          stub.  Other calls that never return could do
14752                          the same, if we could detect such.  */
14753                       can_plt_call = TRUE;
14754                     }
14755                 }
14756
14757               if (!can_plt_call)
14758                 {
14759                   /* g++ as of 20130507 emits self-calls without a
14760                      following nop.  This is arguably wrong since we
14761                      have conflicting information.  On the one hand a
14762                      global symbol and on the other a local call
14763                      sequence, but don't error for this special case.
14764                      It isn't possible to cheaply verify we have
14765                      exactly such a call.  Allow all calls to the same
14766                      section.  */
14767                   asection *code_sec = sec;
14768
14769                   if (get_opd_info (sec) != NULL)
14770                     {
14771                       bfd_vma off = (relocation + addend
14772                                      - sec->output_section->vma
14773                                      - sec->output_offset);
14774
14775                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14776                     }
14777                   if (code_sec == input_section)
14778                     can_plt_call = TRUE;
14779                 }
14780
14781               if (!can_plt_call)
14782                 {
14783                   if (stub_entry->stub_type == ppc_stub_plt_call
14784                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14785                     info->callbacks->einfo
14786                       /* xgettext:c-format */
14787                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14788                          "recompile with -fPIC\n"),
14789                        input_bfd, input_section, rel->r_offset, sym_name);
14790                   else
14791                     info->callbacks->einfo
14792                       /* xgettext:c-format */
14793                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14794                          "(-mcmodel=small toc adjust stub)\n"),
14795                        input_bfd, input_section, rel->r_offset, sym_name);
14796
14797                   bfd_set_error (bfd_error_bad_value);
14798                   ret = FALSE;
14799                 }
14800
14801               if (can_plt_call
14802                   && (stub_entry->stub_type == ppc_stub_plt_call
14803                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14804                 unresolved_reloc = FALSE;
14805             }
14806
14807           if ((stub_entry == NULL
14808                || stub_entry->stub_type == ppc_stub_long_branch
14809                || stub_entry->stub_type == ppc_stub_plt_branch)
14810               && get_opd_info (sec) != NULL)
14811             {
14812               /* The branch destination is the value of the opd entry. */
14813               bfd_vma off = (relocation + addend
14814                              - sec->output_section->vma
14815                              - sec->output_offset);
14816               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14817               if (dest != (bfd_vma) -1)
14818                 {
14819                   relocation = dest;
14820                   addend = 0;
14821                   reloc_dest = DEST_OPD;
14822                 }
14823             }
14824
14825           /* If the branch is out of reach we ought to have a long
14826              branch stub.  */
14827           from = (rel->r_offset
14828                   + input_section->output_offset
14829                   + input_section->output_section->vma);
14830
14831           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14832                                                   ? fdh->elf.other
14833                                                   : sym->st_other);
14834
14835           if (stub_entry != NULL
14836               && (stub_entry->stub_type == ppc_stub_long_branch
14837                   || stub_entry->stub_type == ppc_stub_plt_branch)
14838               && (r_type == R_PPC64_ADDR14_BRTAKEN
14839                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14840                   || (relocation + addend - from + max_br_offset
14841                       < 2 * max_br_offset)))
14842             /* Don't use the stub if this branch is in range.  */
14843             stub_entry = NULL;
14844
14845           if (stub_entry != NULL)
14846             {
14847               /* Munge up the value and addend so that we call the stub
14848                  rather than the procedure directly.  */
14849               asection *stub_sec = stub_entry->group->stub_sec;
14850
14851               if (stub_entry->stub_type == ppc_stub_save_res)
14852                 relocation += (stub_sec->output_offset
14853                                + stub_sec->output_section->vma
14854                                + stub_sec->size - htab->sfpr->size
14855                                - htab->sfpr->output_offset
14856                                - htab->sfpr->output_section->vma);
14857               else
14858                 relocation = (stub_entry->stub_offset
14859                               + stub_sec->output_offset
14860                               + stub_sec->output_section->vma);
14861               addend = 0;
14862               reloc_dest = DEST_STUB;
14863
14864               if ((stub_entry->stub_type == ppc_stub_plt_call
14865                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14866                   && (ALWAYS_EMIT_R2SAVE
14867                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14868                   && rel + 1 < relend
14869                   && rel[1].r_offset == rel->r_offset + 4
14870                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14871                 relocation += 4;
14872             }
14873
14874           if (insn != 0)
14875             {
14876               if (is_isa_v2)
14877                 {
14878                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14879                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14880                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14881                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14882                     insn |= 0x02 << 21;
14883                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14884                     insn |= 0x08 << 21;
14885                   else
14886                     break;
14887                 }
14888               else
14889                 {
14890                   /* Invert 'y' bit if not the default.  */
14891                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14892                     insn ^= 0x01 << 21;
14893                 }
14894
14895               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14896             }
14897
14898           /* NOP out calls to undefined weak functions.
14899              We can thus call a weak function without first
14900              checking whether the function is defined.  */
14901           else if (h != NULL
14902                    && h->elf.root.type == bfd_link_hash_undefweak
14903                    && h->elf.dynindx == -1
14904                    && r_type == R_PPC64_REL24
14905                    && relocation == 0
14906                    && addend == 0)
14907             {
14908               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14909               goto copy_reloc;
14910             }
14911           break;
14912         }
14913
14914       /* Set `addend'.  */
14915       tls_type = 0;
14916       save_unresolved_reloc = unresolved_reloc;
14917       switch (r_type)
14918         {
14919         default:
14920           /* xgettext:c-format */
14921           _bfd_error_handler (_("%pB: %s unsupported"),
14922                               input_bfd, ppc64_elf_howto_table[r_type]->name);
14923
14924           bfd_set_error (bfd_error_bad_value);
14925           ret = FALSE;
14926           goto copy_reloc;
14927
14928         case R_PPC64_NONE:
14929         case R_PPC64_TLS:
14930         case R_PPC64_TLSGD:
14931         case R_PPC64_TLSLD:
14932         case R_PPC64_TOCSAVE:
14933         case R_PPC64_GNU_VTINHERIT:
14934         case R_PPC64_GNU_VTENTRY:
14935         case R_PPC64_ENTRY:
14936           goto copy_reloc;
14937
14938           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14939              address in the GOT as relocation value instead of the
14940              symbol's value itself.  Also, create a GOT entry for the
14941              symbol and put the symbol value there.  */
14942         case R_PPC64_GOT_TLSGD16:
14943         case R_PPC64_GOT_TLSGD16_LO:
14944         case R_PPC64_GOT_TLSGD16_HI:
14945         case R_PPC64_GOT_TLSGD16_HA:
14946           tls_type = TLS_TLS | TLS_GD;
14947           goto dogot;
14948
14949         case R_PPC64_GOT_TLSLD16:
14950         case R_PPC64_GOT_TLSLD16_LO:
14951         case R_PPC64_GOT_TLSLD16_HI:
14952         case R_PPC64_GOT_TLSLD16_HA:
14953           tls_type = TLS_TLS | TLS_LD;
14954           goto dogot;
14955
14956         case R_PPC64_GOT_TPREL16_DS:
14957         case R_PPC64_GOT_TPREL16_LO_DS:
14958         case R_PPC64_GOT_TPREL16_HI:
14959         case R_PPC64_GOT_TPREL16_HA:
14960           tls_type = TLS_TLS | TLS_TPREL;
14961           goto dogot;
14962
14963         case R_PPC64_GOT_DTPREL16_DS:
14964         case R_PPC64_GOT_DTPREL16_LO_DS:
14965         case R_PPC64_GOT_DTPREL16_HI:
14966         case R_PPC64_GOT_DTPREL16_HA:
14967           tls_type = TLS_TLS | TLS_DTPREL;
14968           goto dogot;
14969
14970         case R_PPC64_GOT16:
14971         case R_PPC64_GOT16_LO:
14972         case R_PPC64_GOT16_HI:
14973         case R_PPC64_GOT16_HA:
14974         case R_PPC64_GOT16_DS:
14975         case R_PPC64_GOT16_LO_DS:
14976         dogot:
14977           {
14978             /* Relocation is to the entry for this symbol in the global
14979                offset table.  */
14980             asection *got;
14981             bfd_vma *offp;
14982             bfd_vma off;
14983             unsigned long indx = 0;
14984             struct got_entry *ent;
14985
14986             if (tls_type == (TLS_TLS | TLS_LD)
14987                 && (h == NULL
14988                     || !h->elf.def_dynamic))
14989               ent = ppc64_tlsld_got (input_bfd);
14990             else
14991               {
14992                 if (h != NULL)
14993                   {
14994                     if (!htab->elf.dynamic_sections_created
14995                         || h->elf.dynindx == -1
14996                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14997                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14998                       /* This is actually a static link, or it is a
14999                          -Bsymbolic link and the symbol is defined
15000                          locally, or the symbol was forced to be local
15001                          because of a version file.  */
15002                       ;
15003                     else
15004                       {
15005                         indx = h->elf.dynindx;
15006                         unresolved_reloc = FALSE;
15007                       }
15008                     ent = h->elf.got.glist;
15009                   }
15010                 else
15011                   {
15012                     if (local_got_ents == NULL)
15013                       abort ();
15014                     ent = local_got_ents[r_symndx];
15015                   }
15016
15017                 for (; ent != NULL; ent = ent->next)
15018                   if (ent->addend == orig_rel.r_addend
15019                       && ent->owner == input_bfd
15020                       && ent->tls_type == tls_type)
15021                     break;
15022               }
15023
15024             if (ent == NULL)
15025               abort ();
15026             if (ent->is_indirect)
15027               ent = ent->got.ent;
15028             offp = &ent->got.offset;
15029             got = ppc64_elf_tdata (ent->owner)->got;
15030             if (got == NULL)
15031               abort ();
15032
15033             /* The offset must always be a multiple of 8.  We use the
15034                least significant bit to record whether we have already
15035                processed this entry.  */
15036             off = *offp;
15037             if ((off & 1) != 0)
15038               off &= ~1;
15039             else
15040               {
15041                 /* Generate relocs for the dynamic linker, except in
15042                    the case of TLSLD where we'll use one entry per
15043                    module.  */
15044                 asection *relgot;
15045                 bfd_boolean ifunc;
15046
15047                 *offp = off | 1;
15048                 relgot = NULL;
15049                 ifunc = (h != NULL
15050                          ? h->elf.type == STT_GNU_IFUNC
15051                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15052                 if (ifunc)
15053                   {
15054                     relgot = htab->elf.irelplt;
15055                     if (indx == 0)
15056                       htab->local_ifunc_resolver = 1;
15057                     else if (is_static_defined (&h->elf))
15058                       htab->maybe_local_ifunc_resolver = 1;
15059                   }
15060                 else if (indx != 0
15061                          || (bfd_link_pic (info)
15062                              && (h == NULL
15063                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15064                                  || (tls_type == (TLS_TLS | TLS_LD)
15065                                      && !h->elf.def_dynamic))
15066                              && !(tls_type == (TLS_TLS | TLS_TPREL)
15067                                   && bfd_link_executable (info)
15068                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15069                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
15070                 if (relgot != NULL)
15071                   {
15072                     outrel.r_offset = (got->output_section->vma
15073                                        + got->output_offset
15074                                        + off);
15075                     outrel.r_addend = addend;
15076                     if (tls_type & (TLS_LD | TLS_GD))
15077                       {
15078                         outrel.r_addend = 0;
15079                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15080                         if (tls_type == (TLS_TLS | TLS_GD))
15081                           {
15082                             loc = relgot->contents;
15083                             loc += (relgot->reloc_count++
15084                                     * sizeof (Elf64_External_Rela));
15085                             bfd_elf64_swap_reloca_out (output_bfd,
15086                                                        &outrel, loc);
15087                             outrel.r_offset += 8;
15088                             outrel.r_addend = addend;
15089                             outrel.r_info
15090                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15091                           }
15092                       }
15093                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
15094                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15095                     else if (tls_type == (TLS_TLS | TLS_TPREL))
15096                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15097                     else if (indx != 0)
15098                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15099                     else
15100                       {
15101                         if (ifunc)
15102                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15103                         else
15104                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15105
15106                         /* Write the .got section contents for the sake
15107                            of prelink.  */
15108                         loc = got->contents + off;
15109                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15110                                     loc);
15111                       }
15112
15113                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15114                       {
15115                         outrel.r_addend += relocation;
15116                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15117                           {
15118                             if (htab->elf.tls_sec == NULL)
15119                               outrel.r_addend = 0;
15120                             else
15121                               outrel.r_addend -= htab->elf.tls_sec->vma;
15122                           }
15123                       }
15124                     loc = relgot->contents;
15125                     loc += (relgot->reloc_count++
15126                             * sizeof (Elf64_External_Rela));
15127                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15128                   }
15129
15130                 /* Init the .got section contents here if we're not
15131                    emitting a reloc.  */
15132                 else
15133                   {
15134                     relocation += addend;
15135                     if (tls_type != 0)
15136                       {
15137                         if (htab->elf.tls_sec == NULL)
15138                           relocation = 0;
15139                         else
15140                           {
15141                             if (tls_type & TLS_LD)
15142                               relocation = 0;
15143                             else
15144                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15145                             if (tls_type & TLS_TPREL)
15146                               relocation += DTP_OFFSET - TP_OFFSET;
15147                           }
15148
15149                         if (tls_type & (TLS_GD | TLS_LD))
15150                           {
15151                             bfd_put_64 (output_bfd, relocation,
15152                                         got->contents + off + 8);
15153                             relocation = 1;
15154                           }
15155                       }
15156                     bfd_put_64 (output_bfd, relocation,
15157                                 got->contents + off);
15158                   }
15159               }
15160
15161             if (off >= (bfd_vma) -2)
15162               abort ();
15163
15164             relocation = got->output_section->vma + got->output_offset + off;
15165             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15166           }
15167           break;
15168
15169         case R_PPC64_PLT16_HA:
15170         case R_PPC64_PLT16_HI:
15171         case R_PPC64_PLT16_LO:
15172         case R_PPC64_PLT16_LO_DS:
15173         case R_PPC64_PLT32:
15174         case R_PPC64_PLT64:
15175         case R_PPC64_PLTSEQ:
15176         case R_PPC64_PLTCALL:
15177           /* Relocation is to the entry for this symbol in the
15178              procedure linkage table.  */
15179           unresolved_reloc = TRUE;
15180           {
15181             struct plt_entry **plt_list = NULL;
15182             if (h != NULL)
15183               plt_list = &h->elf.plt.plist;
15184             else if (local_got_ents != NULL)
15185               {
15186                 struct plt_entry **local_plt = (struct plt_entry **)
15187                   (local_got_ents + symtab_hdr->sh_info);
15188                 plt_list = local_plt + r_symndx;
15189               }
15190             if (plt_list)
15191               {
15192                 struct plt_entry *ent;
15193
15194                 for (ent = *plt_list; ent != NULL; ent = ent->next)
15195                   if (ent->plt.offset != (bfd_vma) -1
15196                       && ent->addend == orig_rel.r_addend)
15197                     {
15198                       asection *plt;
15199                       bfd_vma got;
15200
15201                       plt = htab->elf.splt;
15202                       if (!htab->elf.dynamic_sections_created
15203                           || h == NULL
15204                           || h->elf.dynindx == -1)
15205                         {
15206                           if (h != NULL
15207                               ? h->elf.type == STT_GNU_IFUNC
15208                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15209                             plt = htab->elf.iplt;
15210                           else
15211                             plt = htab->pltlocal;
15212                         }
15213                       relocation = (plt->output_section->vma
15214                                     + plt->output_offset
15215                                     + ent->plt.offset);
15216                       if (r_type == R_PPC64_PLT16_HA
15217                           || r_type ==R_PPC64_PLT16_HI
15218                           || r_type ==R_PPC64_PLT16_LO
15219                           || r_type ==R_PPC64_PLT16_LO_DS)
15220                         {
15221                           got = (elf_gp (output_bfd)
15222                                  + htab->sec_info[input_section->id].toc_off);
15223                           relocation -= got;
15224                         }
15225                       addend = 0;
15226                       unresolved_reloc = FALSE;
15227                       break;
15228                     }
15229               }
15230           }
15231           break;
15232
15233         case R_PPC64_TOC:
15234           /* Relocation value is TOC base.  */
15235           relocation = TOCstart;
15236           if (r_symndx == STN_UNDEF)
15237             relocation += htab->sec_info[input_section->id].toc_off;
15238           else if (unresolved_reloc)
15239             ;
15240           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15241             relocation += htab->sec_info[sec->id].toc_off;
15242           else
15243             unresolved_reloc = TRUE;
15244           goto dodyn;
15245
15246           /* TOC16 relocs.  We want the offset relative to the TOC base,
15247              which is the address of the start of the TOC plus 0x8000.
15248              The TOC consists of sections .got, .toc, .tocbss, and .plt,
15249              in this order.  */
15250         case R_PPC64_TOC16:
15251         case R_PPC64_TOC16_LO:
15252         case R_PPC64_TOC16_HI:
15253         case R_PPC64_TOC16_DS:
15254         case R_PPC64_TOC16_LO_DS:
15255         case R_PPC64_TOC16_HA:
15256           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15257           break;
15258
15259           /* Relocate against the beginning of the section.  */
15260         case R_PPC64_SECTOFF:
15261         case R_PPC64_SECTOFF_LO:
15262         case R_PPC64_SECTOFF_HI:
15263         case R_PPC64_SECTOFF_DS:
15264         case R_PPC64_SECTOFF_LO_DS:
15265         case R_PPC64_SECTOFF_HA:
15266           if (sec != NULL)
15267             addend -= sec->output_section->vma;
15268           break;
15269
15270         case R_PPC64_REL16:
15271         case R_PPC64_REL16_LO:
15272         case R_PPC64_REL16_HI:
15273         case R_PPC64_REL16_HA:
15274         case R_PPC64_REL16DX_HA:
15275           break;
15276
15277         case R_PPC64_REL14:
15278         case R_PPC64_REL14_BRNTAKEN:
15279         case R_PPC64_REL14_BRTAKEN:
15280         case R_PPC64_REL24:
15281           break;
15282
15283         case R_PPC64_TPREL16:
15284         case R_PPC64_TPREL16_LO:
15285         case R_PPC64_TPREL16_HI:
15286         case R_PPC64_TPREL16_HA:
15287         case R_PPC64_TPREL16_DS:
15288         case R_PPC64_TPREL16_LO_DS:
15289         case R_PPC64_TPREL16_HIGH:
15290         case R_PPC64_TPREL16_HIGHA:
15291         case R_PPC64_TPREL16_HIGHER:
15292         case R_PPC64_TPREL16_HIGHERA:
15293         case R_PPC64_TPREL16_HIGHEST:
15294         case R_PPC64_TPREL16_HIGHESTA:
15295           if (h != NULL
15296               && h->elf.root.type == bfd_link_hash_undefweak
15297               && h->elf.dynindx == -1)
15298             {
15299               /* Make this relocation against an undefined weak symbol
15300                  resolve to zero.  This is really just a tweak, since
15301                  code using weak externs ought to check that they are
15302                  defined before using them.  */
15303               bfd_byte *p = contents + rel->r_offset - d_offset;
15304
15305               insn = bfd_get_32 (input_bfd, p);
15306               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15307               if (insn != 0)
15308                 bfd_put_32 (input_bfd, insn, p);
15309               break;
15310             }
15311           if (htab->elf.tls_sec != NULL)
15312             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15313           /* The TPREL16 relocs shouldn't really be used in shared
15314              libs or with non-local symbols as that will result in
15315              DT_TEXTREL being set, but support them anyway.  */
15316           goto dodyn;
15317
15318         case R_PPC64_DTPREL16:
15319         case R_PPC64_DTPREL16_LO:
15320         case R_PPC64_DTPREL16_HI:
15321         case R_PPC64_DTPREL16_HA:
15322         case R_PPC64_DTPREL16_DS:
15323         case R_PPC64_DTPREL16_LO_DS:
15324         case R_PPC64_DTPREL16_HIGH:
15325         case R_PPC64_DTPREL16_HIGHA:
15326         case R_PPC64_DTPREL16_HIGHER:
15327         case R_PPC64_DTPREL16_HIGHERA:
15328         case R_PPC64_DTPREL16_HIGHEST:
15329         case R_PPC64_DTPREL16_HIGHESTA:
15330           if (htab->elf.tls_sec != NULL)
15331             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15332           break;
15333
15334         case R_PPC64_ADDR64_LOCAL:
15335           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15336                                               ? h->elf.other
15337                                               : sym->st_other);
15338           break;
15339
15340         case R_PPC64_DTPMOD64:
15341           relocation = 1;
15342           addend = 0;
15343           goto dodyn;
15344
15345         case R_PPC64_TPREL64:
15346           if (htab->elf.tls_sec != NULL)
15347             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15348           goto dodyn;
15349
15350         case R_PPC64_DTPREL64:
15351           if (htab->elf.tls_sec != NULL)
15352             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15353           /* Fall through.  */
15354
15355           /* Relocations that may need to be propagated if this is a
15356              dynamic object.  */
15357         case R_PPC64_REL30:
15358         case R_PPC64_REL32:
15359         case R_PPC64_REL64:
15360         case R_PPC64_ADDR14:
15361         case R_PPC64_ADDR14_BRNTAKEN:
15362         case R_PPC64_ADDR14_BRTAKEN:
15363         case R_PPC64_ADDR16:
15364         case R_PPC64_ADDR16_DS:
15365         case R_PPC64_ADDR16_HA:
15366         case R_PPC64_ADDR16_HI:
15367         case R_PPC64_ADDR16_HIGH:
15368         case R_PPC64_ADDR16_HIGHA:
15369         case R_PPC64_ADDR16_HIGHER:
15370         case R_PPC64_ADDR16_HIGHERA:
15371         case R_PPC64_ADDR16_HIGHEST:
15372         case R_PPC64_ADDR16_HIGHESTA:
15373         case R_PPC64_ADDR16_LO:
15374         case R_PPC64_ADDR16_LO_DS:
15375         case R_PPC64_ADDR24:
15376         case R_PPC64_ADDR32:
15377         case R_PPC64_ADDR64:
15378         case R_PPC64_UADDR16:
15379         case R_PPC64_UADDR32:
15380         case R_PPC64_UADDR64:
15381         dodyn:
15382           if ((input_section->flags & SEC_ALLOC) == 0)
15383             break;
15384
15385           if (NO_OPD_RELOCS && is_opd)
15386             break;
15387
15388           if (bfd_link_pic (info)
15389               ? ((h == NULL
15390                   || h->dyn_relocs != NULL)
15391                  && ((h != NULL && pc_dynrelocs (h))
15392                      || must_be_dyn_reloc (info, r_type)))
15393               : (h != NULL
15394                  ? h->dyn_relocs != NULL
15395                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15396             {
15397               bfd_boolean skip, relocate;
15398               asection *sreloc;
15399               bfd_vma out_off;
15400               long indx = 0;
15401
15402               /* When generating a dynamic object, these relocations
15403                  are copied into the output file to be resolved at run
15404                  time.  */
15405
15406               skip = FALSE;
15407               relocate = FALSE;
15408
15409               out_off = _bfd_elf_section_offset (output_bfd, info,
15410                                                  input_section, rel->r_offset);
15411               if (out_off == (bfd_vma) -1)
15412                 skip = TRUE;
15413               else if (out_off == (bfd_vma) -2)
15414                 skip = TRUE, relocate = TRUE;
15415               out_off += (input_section->output_section->vma
15416                           + input_section->output_offset);
15417               outrel.r_offset = out_off;
15418               outrel.r_addend = rel->r_addend;
15419
15420               /* Optimize unaligned reloc use.  */
15421               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15422                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15423                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15424               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15425                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15426                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15427               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15428                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15429                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15430
15431               if (skip)
15432                 memset (&outrel, 0, sizeof outrel);
15433               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15434                        && !is_opd
15435                        && r_type != R_PPC64_TOC)
15436                 {
15437                   indx = h->elf.dynindx;
15438                   BFD_ASSERT (indx != -1);
15439                   outrel.r_info = ELF64_R_INFO (indx, r_type);
15440                 }
15441               else
15442                 {
15443                   /* This symbol is local, or marked to become local,
15444                      or this is an opd section reloc which must point
15445                      at a local function.  */
15446                   outrel.r_addend += relocation;
15447                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15448                     {
15449                       if (is_opd && h != NULL)
15450                         {
15451                           /* Lie about opd entries.  This case occurs
15452                              when building shared libraries and we
15453                              reference a function in another shared
15454                              lib.  The same thing happens for a weak
15455                              definition in an application that's
15456                              overridden by a strong definition in a
15457                              shared lib.  (I believe this is a generic
15458                              bug in binutils handling of weak syms.)
15459                              In these cases we won't use the opd
15460                              entry in this lib.  */
15461                           unresolved_reloc = FALSE;
15462                         }
15463                       if (!is_opd
15464                           && r_type == R_PPC64_ADDR64
15465                           && (h != NULL
15466                               ? h->elf.type == STT_GNU_IFUNC
15467                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15468                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15469                       else
15470                         {
15471                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15472
15473                           /* We need to relocate .opd contents for ld.so.
15474                              Prelink also wants simple and consistent rules
15475                              for relocs.  This make all RELATIVE relocs have
15476                              *r_offset equal to r_addend.  */
15477                           relocate = TRUE;
15478                         }
15479                     }
15480                   else
15481                     {
15482                       if (h != NULL
15483                           ? h->elf.type == STT_GNU_IFUNC
15484                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15485                         {
15486                           info->callbacks->einfo
15487                             /* xgettext:c-format */
15488                             (_("%H: %s for indirect "
15489                                "function `%pT' unsupported\n"),
15490                              input_bfd, input_section, rel->r_offset,
15491                              ppc64_elf_howto_table[r_type]->name,
15492                              sym_name);
15493                           ret = FALSE;
15494                         }
15495                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15496                         ;
15497                       else if (sec == NULL || sec->owner == NULL)
15498                         {
15499                           bfd_set_error (bfd_error_bad_value);
15500                           return FALSE;
15501                         }
15502                       else
15503                         {
15504                           asection *osec;
15505
15506                           osec = sec->output_section;
15507                           indx = elf_section_data (osec)->dynindx;
15508
15509                           if (indx == 0)
15510                             {
15511                               if ((osec->flags & SEC_READONLY) == 0
15512                                   && htab->elf.data_index_section != NULL)
15513                                 osec = htab->elf.data_index_section;
15514                               else
15515                                 osec = htab->elf.text_index_section;
15516                               indx = elf_section_data (osec)->dynindx;
15517                             }
15518                           BFD_ASSERT (indx != 0);
15519
15520                           /* We are turning this relocation into one
15521                              against a section symbol, so subtract out
15522                              the output section's address but not the
15523                              offset of the input section in the output
15524                              section.  */
15525                           outrel.r_addend -= osec->vma;
15526                         }
15527
15528                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15529                     }
15530                 }
15531
15532               sreloc = elf_section_data (input_section)->sreloc;
15533               if (h != NULL
15534                   ? h->elf.type == STT_GNU_IFUNC
15535                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15536                 {
15537                   sreloc = htab->elf.irelplt;
15538                   if (indx == 0)
15539                     htab->local_ifunc_resolver = 1;
15540                   else if (is_static_defined (&h->elf))
15541                     htab->maybe_local_ifunc_resolver = 1;
15542                 }
15543               if (sreloc == NULL)
15544                 abort ();
15545
15546               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15547                   >= sreloc->size)
15548                 abort ();
15549               loc = sreloc->contents;
15550               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15551               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15552
15553               /* If this reloc is against an external symbol, it will
15554                  be computed at runtime, so there's no need to do
15555                  anything now.  However, for the sake of prelink ensure
15556                  that the section contents are a known value.  */
15557               if (! relocate)
15558                 {
15559                   unresolved_reloc = FALSE;
15560                   /* The value chosen here is quite arbitrary as ld.so
15561                      ignores section contents except for the special
15562                      case of .opd where the contents might be accessed
15563                      before relocation.  Choose zero, as that won't
15564                      cause reloc overflow.  */
15565                   relocation = 0;
15566                   addend = 0;
15567                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15568                      to improve backward compatibility with older
15569                      versions of ld.  */
15570                   if (r_type == R_PPC64_ADDR64)
15571                     addend = outrel.r_addend;
15572                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15573                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15574                     addend = outrel.r_offset;
15575                 }
15576             }
15577           break;
15578
15579         case R_PPC64_COPY:
15580         case R_PPC64_GLOB_DAT:
15581         case R_PPC64_JMP_SLOT:
15582         case R_PPC64_JMP_IREL:
15583         case R_PPC64_RELATIVE:
15584           /* We shouldn't ever see these dynamic relocs in relocatable
15585              files.  */
15586           /* Fall through.  */
15587
15588         case R_PPC64_PLTGOT16:
15589         case R_PPC64_PLTGOT16_DS:
15590         case R_PPC64_PLTGOT16_HA:
15591         case R_PPC64_PLTGOT16_HI:
15592         case R_PPC64_PLTGOT16_LO:
15593         case R_PPC64_PLTGOT16_LO_DS:
15594         case R_PPC64_PLTREL32:
15595         case R_PPC64_PLTREL64:
15596           /* These ones haven't been implemented yet.  */
15597
15598           info->callbacks->einfo
15599             /* xgettext:c-format */
15600             (_("%P: %pB: %s is not supported for `%pT'\n"),
15601              input_bfd,
15602              ppc64_elf_howto_table[r_type]->name, sym_name);
15603
15604           bfd_set_error (bfd_error_invalid_operation);
15605           ret = FALSE;
15606           goto copy_reloc;
15607         }
15608
15609       /* Multi-instruction sequences that access the TOC can be
15610          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15611          to             nop;           addi rb,r2,x;  */
15612       switch (r_type)
15613         {
15614         default:
15615           break;
15616
15617         case R_PPC64_GOT_TLSLD16_HI:
15618         case R_PPC64_GOT_TLSGD16_HI:
15619         case R_PPC64_GOT_TPREL16_HI:
15620         case R_PPC64_GOT_DTPREL16_HI:
15621         case R_PPC64_GOT16_HI:
15622         case R_PPC64_TOC16_HI:
15623           /* These relocs would only be useful if building up an
15624              offset to later add to r2, perhaps in an indexed
15625              addressing mode instruction.  Don't try to optimize.
15626              Unfortunately, the possibility of someone building up an
15627              offset like this or even with the HA relocs, means that
15628              we need to check the high insn when optimizing the low
15629              insn.  */
15630           break;
15631
15632         case R_PPC64_PLTCALL:
15633           if (unresolved_reloc)
15634             {
15635               /* No plt entry.  Make this into a direct call.  */
15636               bfd_byte *p = contents + rel->r_offset;
15637               insn = bfd_get_32 (input_bfd, p);
15638               insn &= 1;
15639               bfd_put_32 (input_bfd, B_DOT | insn, p);
15640               bfd_put_32 (input_bfd, NOP, p + 4);
15641               unresolved_reloc = save_unresolved_reloc;
15642               r_type = R_PPC64_REL24;
15643             }
15644           break;
15645
15646         case R_PPC64_PLTSEQ:
15647           if (unresolved_reloc)
15648             {
15649               unresolved_reloc = FALSE;
15650               goto nop_it;
15651             }
15652           break;
15653
15654         case R_PPC64_PLT16_HA:
15655           if (unresolved_reloc)
15656             {
15657               unresolved_reloc = FALSE;
15658               goto nop_it;
15659             }
15660           /* Fall through.  */
15661         case R_PPC64_GOT_TLSLD16_HA:
15662         case R_PPC64_GOT_TLSGD16_HA:
15663         case R_PPC64_GOT_TPREL16_HA:
15664         case R_PPC64_GOT_DTPREL16_HA:
15665         case R_PPC64_GOT16_HA:
15666         case R_PPC64_TOC16_HA:
15667           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15668               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15669             {
15670               bfd_byte *p;
15671             nop_it:
15672               p = contents + (rel->r_offset & ~3);
15673               bfd_put_32 (input_bfd, NOP, p);
15674               goto copy_reloc;
15675             }
15676           break;
15677
15678         case R_PPC64_PLT16_LO:
15679         case R_PPC64_PLT16_LO_DS:
15680           if (unresolved_reloc)
15681             {
15682               unresolved_reloc = FALSE;
15683               goto nop_it;
15684             }
15685           /* Fall through.  */
15686         case R_PPC64_GOT_TLSLD16_LO:
15687         case R_PPC64_GOT_TLSGD16_LO:
15688         case R_PPC64_GOT_TPREL16_LO_DS:
15689         case R_PPC64_GOT_DTPREL16_LO_DS:
15690         case R_PPC64_GOT16_LO:
15691         case R_PPC64_GOT16_LO_DS:
15692         case R_PPC64_TOC16_LO:
15693         case R_PPC64_TOC16_LO_DS:
15694           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15695               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15696             {
15697               bfd_byte *p = contents + (rel->r_offset & ~3);
15698               insn = bfd_get_32 (input_bfd, p);
15699               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15700                 {
15701                   /* Transform addic to addi when we change reg.  */
15702                   insn &= ~((0x3f << 26) | (0x1f << 16));
15703                   insn |= (14u << 26) | (2 << 16);
15704                 }
15705               else
15706                 {
15707                   insn &= ~(0x1f << 16);
15708                   insn |= 2 << 16;
15709                 }
15710               bfd_put_32 (input_bfd, insn, p);
15711             }
15712           break;
15713
15714         case R_PPC64_TPREL16_HA:
15715           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15716             {
15717               bfd_byte *p = contents + (rel->r_offset & ~3);
15718               insn = bfd_get_32 (input_bfd, p);
15719               if ((insn & ((0x3f << 26) | 0x1f << 16))
15720                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15721                 /* xgettext:c-format */
15722                 info->callbacks->minfo
15723                   (_("%H: warning: %s unexpected insn %#x.\n"),
15724                    input_bfd, input_section, rel->r_offset,
15725                    ppc64_elf_howto_table[r_type]->name, insn);
15726               else
15727                 {
15728                   bfd_put_32 (input_bfd, NOP, p);
15729                   goto copy_reloc;
15730                 }
15731             }
15732           break;
15733
15734         case R_PPC64_TPREL16_LO:
15735         case R_PPC64_TPREL16_LO_DS:
15736           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15737             {
15738               bfd_byte *p = contents + (rel->r_offset & ~3);
15739               insn = bfd_get_32 (input_bfd, p);
15740               insn &= ~(0x1f << 16);
15741               insn |= 13 << 16;
15742               bfd_put_32 (input_bfd, insn, p);
15743             }
15744           break;
15745         }
15746
15747       /* Do any further special processing.  */
15748       switch (r_type)
15749         {
15750         default:
15751           break;
15752
15753         case R_PPC64_REL16_HA:
15754         case R_PPC64_REL16DX_HA:
15755         case R_PPC64_ADDR16_HA:
15756         case R_PPC64_ADDR16_HIGHA:
15757         case R_PPC64_ADDR16_HIGHERA:
15758         case R_PPC64_ADDR16_HIGHESTA:
15759         case R_PPC64_TOC16_HA:
15760         case R_PPC64_SECTOFF_HA:
15761         case R_PPC64_TPREL16_HA:
15762         case R_PPC64_TPREL16_HIGHA:
15763         case R_PPC64_TPREL16_HIGHERA:
15764         case R_PPC64_TPREL16_HIGHESTA:
15765         case R_PPC64_DTPREL16_HA:
15766         case R_PPC64_DTPREL16_HIGHA:
15767         case R_PPC64_DTPREL16_HIGHERA:
15768         case R_PPC64_DTPREL16_HIGHESTA:
15769           /* It's just possible that this symbol is a weak symbol
15770              that's not actually defined anywhere. In that case,
15771              'sec' would be NULL, and we should leave the symbol
15772              alone (it will be set to zero elsewhere in the link).  */
15773           if (sec == NULL)
15774             break;
15775           /* Fall through.  */
15776
15777         case R_PPC64_GOT16_HA:
15778         case R_PPC64_PLTGOT16_HA:
15779         case R_PPC64_PLT16_HA:
15780         case R_PPC64_GOT_TLSGD16_HA:
15781         case R_PPC64_GOT_TLSLD16_HA:
15782         case R_PPC64_GOT_TPREL16_HA:
15783         case R_PPC64_GOT_DTPREL16_HA:
15784           /* Add 0x10000 if sign bit in 0:15 is set.
15785              Bits 0:15 are not used.  */
15786           addend += 0x8000;
15787           break;
15788
15789         case R_PPC64_ADDR16_DS:
15790         case R_PPC64_ADDR16_LO_DS:
15791         case R_PPC64_GOT16_DS:
15792         case R_PPC64_GOT16_LO_DS:
15793         case R_PPC64_PLT16_LO_DS:
15794         case R_PPC64_SECTOFF_DS:
15795         case R_PPC64_SECTOFF_LO_DS:
15796         case R_PPC64_TOC16_DS:
15797         case R_PPC64_TOC16_LO_DS:
15798         case R_PPC64_PLTGOT16_DS:
15799         case R_PPC64_PLTGOT16_LO_DS:
15800         case R_PPC64_GOT_TPREL16_DS:
15801         case R_PPC64_GOT_TPREL16_LO_DS:
15802         case R_PPC64_GOT_DTPREL16_DS:
15803         case R_PPC64_GOT_DTPREL16_LO_DS:
15804         case R_PPC64_TPREL16_DS:
15805         case R_PPC64_TPREL16_LO_DS:
15806         case R_PPC64_DTPREL16_DS:
15807         case R_PPC64_DTPREL16_LO_DS:
15808           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15809           mask = 3;
15810           /* If this reloc is against an lq, lxv, or stxv insn, then
15811              the value must be a multiple of 16.  This is somewhat of
15812              a hack, but the "correct" way to do this by defining _DQ
15813              forms of all the _DS relocs bloats all reloc switches in
15814              this file.  It doesn't make much sense to use these
15815              relocs in data, so testing the insn should be safe.  */
15816           if ((insn & (0x3f << 26)) == (56u << 26)
15817               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15818             mask = 15;
15819           relocation += addend;
15820           addend = insn & (mask ^ 3);
15821           if ((relocation & mask) != 0)
15822             {
15823               relocation ^= relocation & mask;
15824               info->callbacks->einfo
15825                 /* xgettext:c-format */
15826                 (_("%H: error: %s not a multiple of %u\n"),
15827                  input_bfd, input_section, rel->r_offset,
15828                  ppc64_elf_howto_table[r_type]->name,
15829                  mask + 1);
15830               bfd_set_error (bfd_error_bad_value);
15831               ret = FALSE;
15832               goto copy_reloc;
15833             }
15834           break;
15835         }
15836
15837       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15838          because such sections are not SEC_ALLOC and thus ld.so will
15839          not process them.  */
15840       howto = ppc64_elf_howto_table[(int) r_type];
15841       if (unresolved_reloc
15842           && !((input_section->flags & SEC_DEBUGGING) != 0
15843                && h->elf.def_dynamic)
15844           && _bfd_elf_section_offset (output_bfd, info, input_section,
15845                                       rel->r_offset) != (bfd_vma) -1)
15846         {
15847           info->callbacks->einfo
15848             /* xgettext:c-format */
15849             (_("%H: unresolvable %s against `%pT'\n"),
15850              input_bfd, input_section, rel->r_offset,
15851              howto->name,
15852              h->elf.root.root.string);
15853           ret = FALSE;
15854         }
15855
15856       /* 16-bit fields in insns mostly have signed values, but a
15857          few insns have 16-bit unsigned values.  Really, we should
15858          have different reloc types.  */
15859       if (howto->complain_on_overflow != complain_overflow_dont
15860           && howto->dst_mask == 0xffff
15861           && (input_section->flags & SEC_CODE) != 0)
15862         {
15863           enum complain_overflow complain = complain_overflow_signed;
15864
15865           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15866           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15867             complain = complain_overflow_bitfield;
15868           else if (howto->rightshift == 0
15869                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15870                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15871                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15872                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15873                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15874                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15875             complain = complain_overflow_unsigned;
15876           if (howto->complain_on_overflow != complain)
15877             {
15878               alt_howto = *howto;
15879               alt_howto.complain_on_overflow = complain;
15880               howto = &alt_howto;
15881             }
15882         }
15883
15884       if (r_type == R_PPC64_REL16DX_HA)
15885         {
15886           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15887           if (rel->r_offset + 4 > input_section->size)
15888             r = bfd_reloc_outofrange;
15889           else
15890             {
15891               relocation += addend;
15892               relocation -= (rel->r_offset
15893                              + input_section->output_offset
15894                              + input_section->output_section->vma);
15895               relocation = (bfd_signed_vma) relocation >> 16;
15896               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15897               insn &= ~0x1fffc1;
15898               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15899               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15900               r = bfd_reloc_ok;
15901               if (relocation + 0x8000 > 0xffff)
15902                 r = bfd_reloc_overflow;
15903             }
15904         }
15905       else
15906         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15907                                       rel->r_offset, relocation, addend);
15908
15909       if (r != bfd_reloc_ok)
15910         {
15911           char *more_info = NULL;
15912           const char *reloc_name = howto->name;
15913
15914           if (reloc_dest != DEST_NORMAL)
15915             {
15916               more_info = bfd_malloc (strlen (reloc_name) + 8);
15917               if (more_info != NULL)
15918                 {
15919                   strcpy (more_info, reloc_name);
15920                   strcat (more_info, (reloc_dest == DEST_OPD
15921                                       ? " (OPD)" : " (stub)"));
15922                   reloc_name = more_info;
15923                 }
15924             }
15925
15926           if (r == bfd_reloc_overflow)
15927             {
15928               /* On code like "if (foo) foo();" don't report overflow
15929                  on a branch to zero when foo is undefined.  */
15930               if (!warned
15931                   && (reloc_dest == DEST_STUB
15932                       || !(h != NULL
15933                            && (h->elf.root.type == bfd_link_hash_undefweak
15934                                || h->elf.root.type == bfd_link_hash_undefined)
15935                            && is_branch_reloc (r_type))))
15936                 info->callbacks->reloc_overflow (info, &h->elf.root,
15937                                                  sym_name, reloc_name,
15938                                                  orig_rel.r_addend,
15939                                                  input_bfd, input_section,
15940                                                  rel->r_offset);
15941             }
15942           else
15943             {
15944               info->callbacks->einfo
15945                 /* xgettext:c-format */
15946                 (_("%H: %s against `%pT': error %d\n"),
15947                  input_bfd, input_section, rel->r_offset,
15948                  reloc_name, sym_name, (int) r);
15949               ret = FALSE;
15950             }
15951           if (more_info != NULL)
15952             free (more_info);
15953         }
15954     copy_reloc:
15955       if (wrel != rel)
15956         *wrel = *rel;
15957     }
15958
15959   if (wrel != rel)
15960     {
15961       Elf_Internal_Shdr *rel_hdr;
15962       size_t deleted = rel - wrel;
15963
15964       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15965       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15966       if (rel_hdr->sh_size == 0)
15967         {
15968           /* It is too late to remove an empty reloc section.  Leave
15969              one NONE reloc.
15970              ??? What is wrong with an empty section???  */
15971           rel_hdr->sh_size = rel_hdr->sh_entsize;
15972           deleted -= 1;
15973         }
15974       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15975       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15976       input_section->reloc_count -= deleted;
15977     }
15978
15979   /* If we're emitting relocations, then shortly after this function
15980      returns, reloc offsets and addends for this section will be
15981      adjusted.  Worse, reloc symbol indices will be for the output
15982      file rather than the input.  Save a copy of the relocs for
15983      opd_entry_value.  */
15984   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15985     {
15986       bfd_size_type amt;
15987       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15988       rel = bfd_alloc (input_bfd, amt);
15989       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15990       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15991       if (rel == NULL)
15992         return FALSE;
15993       memcpy (rel, relocs, amt);
15994     }
15995   return ret;
15996 }
15997
15998 /* Adjust the value of any local symbols in opd sections.  */
15999
16000 static int
16001 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16002                               const char *name ATTRIBUTE_UNUSED,
16003                               Elf_Internal_Sym *elfsym,
16004                               asection *input_sec,
16005                               struct elf_link_hash_entry *h)
16006 {
16007   struct _opd_sec_data *opd;
16008   long adjust;
16009   bfd_vma value;
16010
16011   if (h != NULL)
16012     return 1;
16013
16014   opd = get_opd_info (input_sec);
16015   if (opd == NULL || opd->adjust == NULL)
16016     return 1;
16017
16018   value = elfsym->st_value - input_sec->output_offset;
16019   if (!bfd_link_relocatable (info))
16020     value -= input_sec->output_section->vma;
16021
16022   adjust = opd->adjust[OPD_NDX (value)];
16023   if (adjust == -1)
16024     return 2;
16025
16026   elfsym->st_value += adjust;
16027   return 1;
16028 }
16029
16030 /* Finish up dynamic symbol handling.  We set the contents of various
16031    dynamic sections here.  */
16032
16033 static bfd_boolean
16034 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16035                                  struct bfd_link_info *info,
16036                                  struct elf_link_hash_entry *h,
16037                                  Elf_Internal_Sym *sym)
16038 {
16039   struct ppc_link_hash_table *htab;
16040   struct plt_entry *ent;
16041
16042   htab = ppc_hash_table (info);
16043   if (htab == NULL)
16044     return FALSE;
16045
16046   if (!htab->opd_abi && !h->def_regular)
16047     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16048       if (ent->plt.offset != (bfd_vma) -1)
16049         {
16050           /* Mark the symbol as undefined, rather than as
16051              defined in glink.  Leave the value if there were
16052              any relocations where pointer equality matters
16053              (this is a clue for the dynamic linker, to make
16054              function pointer comparisons work between an
16055              application and shared library), otherwise set it
16056              to zero.  */
16057           sym->st_shndx = SHN_UNDEF;
16058           if (!h->pointer_equality_needed)
16059             sym->st_value = 0;
16060           else if (!h->ref_regular_nonweak)
16061             {
16062               /* This breaks function pointer comparisons, but
16063                  that is better than breaking tests for a NULL
16064                  function pointer.  */
16065               sym->st_value = 0;
16066             }
16067           break;
16068         }
16069
16070   if (h->needs_copy)
16071     {
16072       /* This symbol needs a copy reloc.  Set it up.  */
16073       Elf_Internal_Rela rela;
16074       asection *srel;
16075       bfd_byte *loc;
16076
16077       if (h->dynindx == -1
16078           || (h->root.type != bfd_link_hash_defined
16079               && h->root.type != bfd_link_hash_defweak)
16080           || htab->elf.srelbss == NULL
16081           || htab->elf.sreldynrelro == NULL)
16082         abort ();
16083
16084       rela.r_offset = (h->root.u.def.value
16085                        + h->root.u.def.section->output_section->vma
16086                        + h->root.u.def.section->output_offset);
16087       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16088       rela.r_addend = 0;
16089       if (h->root.u.def.section == htab->elf.sdynrelro)
16090         srel = htab->elf.sreldynrelro;
16091       else
16092         srel = htab->elf.srelbss;
16093       loc = srel->contents;
16094       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16095       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16096     }
16097
16098   return TRUE;
16099 }
16100
16101 /* Used to decide how to sort relocs in an optimal manner for the
16102    dynamic linker, before writing them out.  */
16103
16104 static enum elf_reloc_type_class
16105 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16106                             const asection *rel_sec,
16107                             const Elf_Internal_Rela *rela)
16108 {
16109   enum elf_ppc64_reloc_type r_type;
16110   struct ppc_link_hash_table *htab = ppc_hash_table (info);
16111
16112   if (rel_sec == htab->elf.irelplt)
16113     return reloc_class_ifunc;
16114
16115   r_type = ELF64_R_TYPE (rela->r_info);
16116   switch (r_type)
16117     {
16118     case R_PPC64_RELATIVE:
16119       return reloc_class_relative;
16120     case R_PPC64_JMP_SLOT:
16121       return reloc_class_plt;
16122     case R_PPC64_COPY:
16123       return reloc_class_copy;
16124     default:
16125       return reloc_class_normal;
16126     }
16127 }
16128
16129 /* Finish up the dynamic sections.  */
16130
16131 static bfd_boolean
16132 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16133                                    struct bfd_link_info *info)
16134 {
16135   struct ppc_link_hash_table *htab;
16136   bfd *dynobj;
16137   asection *sdyn;
16138
16139   htab = ppc_hash_table (info);
16140   if (htab == NULL)
16141     return FALSE;
16142
16143   dynobj = htab->elf.dynobj;
16144   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16145
16146   if (htab->elf.dynamic_sections_created)
16147     {
16148       Elf64_External_Dyn *dyncon, *dynconend;
16149
16150       if (sdyn == NULL || htab->elf.sgot == NULL)
16151         abort ();
16152
16153       dyncon = (Elf64_External_Dyn *) sdyn->contents;
16154       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16155       for (; dyncon < dynconend; dyncon++)
16156         {
16157           Elf_Internal_Dyn dyn;
16158           asection *s;
16159
16160           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16161
16162           switch (dyn.d_tag)
16163             {
16164             default:
16165               continue;
16166
16167             case DT_PPC64_GLINK:
16168               s = htab->glink;
16169               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16170               /* We stupidly defined DT_PPC64_GLINK to be the start
16171                  of glink rather than the first entry point, which is
16172                  what ld.so needs, and now have a bigger stub to
16173                  support automatic multiple TOCs.  */
16174               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16175               break;
16176
16177             case DT_PPC64_OPD:
16178               s = bfd_get_section_by_name (output_bfd, ".opd");
16179               if (s == NULL)
16180                 continue;
16181               dyn.d_un.d_ptr = s->vma;
16182               break;
16183
16184             case DT_PPC64_OPT:
16185               if (htab->do_multi_toc && htab->multi_toc_needed)
16186                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16187               if (htab->has_plt_localentry0)
16188                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16189               break;
16190
16191             case DT_PPC64_OPDSZ:
16192               s = bfd_get_section_by_name (output_bfd, ".opd");
16193               if (s == NULL)
16194                 continue;
16195               dyn.d_un.d_val = s->size;
16196               break;
16197
16198             case DT_PLTGOT:
16199               s = htab->elf.splt;
16200               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16201               break;
16202
16203             case DT_JMPREL:
16204               s = htab->elf.srelplt;
16205               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16206               break;
16207
16208             case DT_PLTRELSZ:
16209               dyn.d_un.d_val = htab->elf.srelplt->size;
16210               break;
16211
16212             case DT_TEXTREL:
16213               if (htab->local_ifunc_resolver)
16214                 info->callbacks->einfo
16215                   (_("%X%P: text relocations and GNU indirect "
16216                      "functions will result in a segfault at runtime\n"));
16217               else if (htab->maybe_local_ifunc_resolver)
16218                 info->callbacks->einfo
16219                   (_("%P: warning: text relocations and GNU indirect "
16220                      "functions may result in a segfault at runtime\n"));
16221               continue;
16222             }
16223
16224           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16225         }
16226     }
16227
16228   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16229       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16230     {
16231       /* Fill in the first entry in the global offset table.
16232          We use it to hold the link-time TOCbase.  */
16233       bfd_put_64 (output_bfd,
16234                   elf_gp (output_bfd) + TOC_BASE_OFF,
16235                   htab->elf.sgot->contents);
16236
16237       /* Set .got entry size.  */
16238       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16239     }
16240
16241   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16242       && htab->elf.splt->output_section != bfd_abs_section_ptr)
16243     {
16244       /* Set .plt entry size.  */
16245       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16246         = PLT_ENTRY_SIZE (htab);
16247     }
16248
16249   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16250      brlt ourselves if emitrelocations.  */
16251   if (htab->brlt != NULL
16252       && htab->brlt->reloc_count != 0
16253       && !_bfd_elf_link_output_relocs (output_bfd,
16254                                        htab->brlt,
16255                                        elf_section_data (htab->brlt)->rela.hdr,
16256                                        elf_section_data (htab->brlt)->relocs,
16257                                        NULL))
16258     return FALSE;
16259
16260   if (htab->glink != NULL
16261       && htab->glink->reloc_count != 0
16262       && !_bfd_elf_link_output_relocs (output_bfd,
16263                                        htab->glink,
16264                                        elf_section_data (htab->glink)->rela.hdr,
16265                                        elf_section_data (htab->glink)->relocs,
16266                                        NULL))
16267     return FALSE;
16268
16269   if (htab->glink_eh_frame != NULL
16270       && htab->glink_eh_frame->size != 0)
16271     {
16272       bfd_vma val;
16273       bfd_byte *p;
16274       struct map_stub *group;
16275       size_t align = 4;
16276
16277       p = htab->glink_eh_frame->contents;
16278       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16279
16280       for (group = htab->group; group != NULL; group = group->next)
16281         if (group->stub_sec != NULL)
16282           {
16283             /* Offset to stub section.  */
16284             val = (group->stub_sec->output_section->vma
16285                    + group->stub_sec->output_offset);
16286             val -= (htab->glink_eh_frame->output_section->vma
16287                     + htab->glink_eh_frame->output_offset
16288                     + (p + 8 - htab->glink_eh_frame->contents));
16289             if (val + 0x80000000 > 0xffffffff)
16290               {
16291                 _bfd_error_handler
16292                   (_("%s offset too large for .eh_frame sdata4 encoding"),
16293                    group->stub_sec->name);
16294                 return FALSE;
16295               }
16296             bfd_put_32 (dynobj, val, p + 8);
16297             p += stub_eh_frame_size (group, align);
16298           }
16299       if (htab->glink != NULL && htab->glink->size != 0)
16300         {
16301           /* Offset to .glink.  */
16302           val = (htab->glink->output_section->vma
16303                  + htab->glink->output_offset
16304                  + 8);
16305           val -= (htab->glink_eh_frame->output_section->vma
16306                   + htab->glink_eh_frame->output_offset
16307                   + (p + 8 - htab->glink_eh_frame->contents));
16308           if (val + 0x80000000 > 0xffffffff)
16309             {
16310               _bfd_error_handler
16311                 (_("%s offset too large for .eh_frame sdata4 encoding"),
16312                  htab->glink->name);
16313               return FALSE;
16314             }
16315           bfd_put_32 (dynobj, val, p + 8);
16316           p += (24 + align - 1) & -align;
16317         }
16318
16319       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16320           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16321                                                htab->glink_eh_frame,
16322                                                htab->glink_eh_frame->contents))
16323         return FALSE;
16324     }
16325
16326   /* We need to handle writing out multiple GOT sections ourselves,
16327      since we didn't add them to DYNOBJ.  We know dynobj is the first
16328      bfd.  */
16329   while ((dynobj = dynobj->link.next) != NULL)
16330     {
16331       asection *s;
16332
16333       if (!is_ppc64_elf (dynobj))
16334         continue;
16335
16336       s = ppc64_elf_tdata (dynobj)->got;
16337       if (s != NULL
16338           && s->size != 0
16339           && s->output_section != bfd_abs_section_ptr
16340           && !bfd_set_section_contents (output_bfd, s->output_section,
16341                                         s->contents, s->output_offset,
16342                                         s->size))
16343         return FALSE;
16344       s = ppc64_elf_tdata (dynobj)->relgot;
16345       if (s != NULL
16346           && s->size != 0
16347           && s->output_section != bfd_abs_section_ptr
16348           && !bfd_set_section_contents (output_bfd, s->output_section,
16349                                         s->contents, s->output_offset,
16350                                         s->size))
16351         return FALSE;
16352     }
16353
16354   return TRUE;
16355 }
16356
16357 #include "elf64-target.h"
16358
16359 /* FreeBSD support */
16360
16361 #undef  TARGET_LITTLE_SYM
16362 #undef  TARGET_LITTLE_NAME
16363
16364 #undef  TARGET_BIG_SYM
16365 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
16366 #undef  TARGET_BIG_NAME
16367 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16368
16369 #undef  ELF_OSABI
16370 #define ELF_OSABI       ELFOSABI_FREEBSD
16371
16372 #undef  elf64_bed
16373 #define elf64_bed       elf64_powerpc_fbsd_bed
16374
16375 #include "elf64-target.h"