PowerPC64, don't relocate nops
[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
133 /* The initial size of the plt reserved for the dynamic linker.  */
134 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
135
136 /* Offsets to some stack save slots.  */
137 #define STK_LR 16
138 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
139 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
140    CR save slot.  Used only by optimised __tls_get_addr call stub,
141    relying on __tls_get_addr_opt not saving CR..  */
142 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143
144 /* TOC base pointers offset from start of TOC.  */
145 #define TOC_BASE_OFF    0x8000
146 /* TOC base alignment.  */
147 #define TOC_BASE_ALIGN  256
148
149 /* Offset of tp and dtp pointers from start of TLS block.  */
150 #define TP_OFFSET       0x7000
151 #define DTP_OFFSET      0x8000
152
153 /* .plt call stub instructions.  The normal stub is like this, but
154    sometimes the .plt entry crosses a 64k boundary and we need to
155    insert an addi to adjust r11.  */
156 #define STD_R2_0R1      0xf8410000      /* std   %r2,0+40(%r1)       */
157 #define ADDIS_R11_R2    0x3d620000      /* addis %r11,%r2,xxx@ha     */
158 #define LD_R12_0R11     0xe98b0000      /* ld    %r12,xxx+0@l(%r11)  */
159 #define MTCTR_R12       0x7d8903a6      /* mtctr %r12                */
160 #define LD_R2_0R11      0xe84b0000      /* ld    %r2,xxx+8@l(%r11)   */
161 #define LD_R11_0R11     0xe96b0000      /* ld    %r11,xxx+16@l(%r11) */
162 #define BCTR            0x4e800420      /* bctr                      */
163
164 #define CRSETEQ         0x4c421242      /* crset 4*%cr0+%eq      */
165 #define BEQCTRM         0x4dc20420      /* beqctr-               */
166 #define BEQCTRLM        0x4dc20421      /* beqctrl-              */
167
168 #define ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
169 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
170 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
171
172 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
173 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
174 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
175 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
176 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
177 #define BNECTR          0x4ca20420      /* bnectr+               */
178 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
179
180 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
181 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
182 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
183
184 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
185 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
186 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
187
188 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
189 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
190 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
191 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
192 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
193
194 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
195 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
196   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4)               \
197    + (!htab->params->speculate_indirect_jumps ? 2 * 4 : 0))
198                                         /* 0:                           */
199                                         /*  .quad plt0-1f               */
200                                         /* __glink:                     */
201 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
202 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
203                                         /* 1:                           */
204 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
205                                         /*  ld %2,(0b-1b)(%11)          */
206 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
207 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
208                                         /*  ld %12,0(%11)               */
209                                         /*  ld %2,8(%11)                */
210                                         /*  mtctr %12                   */
211                                         /*  ld %11,16(%11)              */
212                                         /*  bctr                        */
213 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
214 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
215 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
216 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
217 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
218
219 /* Pad with this.  */
220 #define NOP             0x60000000
221
222 /* Some other nops.  */
223 #define CROR_151515     0x4def7b82
224 #define CROR_313131     0x4ffffb82
225
226 /* .glink entries for the first 32k functions are two instructions.  */
227 #define LI_R0_0         0x38000000      /* li    %r0,0          */
228 #define B_DOT           0x48000000      /* b     .              */
229
230 /* After that, we need two instructions to load the index, followed by
231    a branch.  */
232 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
233 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
234
235 /* Instructions used by the save and restore reg functions.  */
236 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
237 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
238 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
239 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
240 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
241 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
242 #define LI_R12_0        0x39800000      /* li    %r12,0         */
243 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
244 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
245 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
246 #define BLR             0x4e800020      /* blr                  */
247
248 /* Since .opd is an array of descriptors and each entry will end up
249    with identical R_PPC64_RELATIVE relocs, there is really no need to
250    propagate .opd relocs;  The dynamic linker should be taught to
251    relocate .opd without reloc entries.  */
252 #ifndef NO_OPD_RELOCS
253 #define NO_OPD_RELOCS 0
254 #endif
255
256 #ifndef ARRAY_SIZE
257 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
258 #endif
259
260 static inline int
261 abiversion (bfd *abfd)
262 {
263   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
264 }
265
266 static inline void
267 set_abiversion (bfd *abfd, int ver)
268 {
269   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
270   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
271 }
272 \f
273 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
274
275 /* Relocation HOWTO's.  */
276 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
277
278 static reloc_howto_type ppc64_elf_howto_raw[] = {
279   /* This reloc does nothing.  */
280   HOWTO (R_PPC64_NONE,          /* type */
281          0,                     /* rightshift */
282          3,                     /* size (0 = byte, 1 = short, 2 = long) */
283          0,                     /* bitsize */
284          FALSE,                 /* pc_relative */
285          0,                     /* bitpos */
286          complain_overflow_dont, /* complain_on_overflow */
287          bfd_elf_generic_reloc, /* special_function */
288          "R_PPC64_NONE",        /* name */
289          FALSE,                 /* partial_inplace */
290          0,                     /* src_mask */
291          0,                     /* dst_mask */
292          FALSE),                /* pcrel_offset */
293
294   /* A standard 32 bit relocation.  */
295   HOWTO (R_PPC64_ADDR32,        /* type */
296          0,                     /* rightshift */
297          2,                     /* size (0 = byte, 1 = short, 2 = long) */
298          32,                    /* bitsize */
299          FALSE,                 /* pc_relative */
300          0,                     /* bitpos */
301          complain_overflow_bitfield, /* complain_on_overflow */
302          bfd_elf_generic_reloc, /* special_function */
303          "R_PPC64_ADDR32",      /* name */
304          FALSE,                 /* partial_inplace */
305          0,                     /* src_mask */
306          0xffffffff,            /* dst_mask */
307          FALSE),                /* pcrel_offset */
308
309   /* An absolute 26 bit branch; the lower two bits must be zero.
310      FIXME: we don't check that, we just clear them.  */
311   HOWTO (R_PPC64_ADDR24,        /* type */
312          0,                     /* rightshift */
313          2,                     /* size (0 = byte, 1 = short, 2 = long) */
314          26,                    /* bitsize */
315          FALSE,                 /* pc_relative */
316          0,                     /* bitpos */
317          complain_overflow_bitfield, /* complain_on_overflow */
318          bfd_elf_generic_reloc, /* special_function */
319          "R_PPC64_ADDR24",      /* name */
320          FALSE,                 /* partial_inplace */
321          0,                     /* src_mask */
322          0x03fffffc,            /* dst_mask */
323          FALSE),                /* pcrel_offset */
324
325   /* A standard 16 bit relocation.  */
326   HOWTO (R_PPC64_ADDR16,        /* type */
327          0,                     /* rightshift */
328          1,                     /* size (0 = byte, 1 = short, 2 = long) */
329          16,                    /* bitsize */
330          FALSE,                 /* pc_relative */
331          0,                     /* bitpos */
332          complain_overflow_bitfield, /* complain_on_overflow */
333          bfd_elf_generic_reloc, /* special_function */
334          "R_PPC64_ADDR16",      /* name */
335          FALSE,                 /* partial_inplace */
336          0,                     /* src_mask */
337          0xffff,                /* dst_mask */
338          FALSE),                /* pcrel_offset */
339
340   /* A 16 bit relocation without overflow.  */
341   HOWTO (R_PPC64_ADDR16_LO,     /* type */
342          0,                     /* rightshift */
343          1,                     /* size (0 = byte, 1 = short, 2 = long) */
344          16,                    /* bitsize */
345          FALSE,                 /* pc_relative */
346          0,                     /* bitpos */
347          complain_overflow_dont,/* complain_on_overflow */
348          bfd_elf_generic_reloc, /* special_function */
349          "R_PPC64_ADDR16_LO",   /* name */
350          FALSE,                 /* partial_inplace */
351          0,                     /* src_mask */
352          0xffff,                /* dst_mask */
353          FALSE),                /* pcrel_offset */
354
355   /* Bits 16-31 of an address.  */
356   HOWTO (R_PPC64_ADDR16_HI,     /* type */
357          16,                    /* rightshift */
358          1,                     /* size (0 = byte, 1 = short, 2 = long) */
359          16,                    /* bitsize */
360          FALSE,                 /* pc_relative */
361          0,                     /* bitpos */
362          complain_overflow_signed, /* complain_on_overflow */
363          bfd_elf_generic_reloc, /* special_function */
364          "R_PPC64_ADDR16_HI",   /* name */
365          FALSE,                 /* partial_inplace */
366          0,                     /* src_mask */
367          0xffff,                /* dst_mask */
368          FALSE),                /* pcrel_offset */
369
370   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
371      bits, treated as a signed number, is negative.  */
372   HOWTO (R_PPC64_ADDR16_HA,     /* type */
373          16,                    /* rightshift */
374          1,                     /* size (0 = byte, 1 = short, 2 = long) */
375          16,                    /* bitsize */
376          FALSE,                 /* pc_relative */
377          0,                     /* bitpos */
378          complain_overflow_signed, /* complain_on_overflow */
379          ppc64_elf_ha_reloc,    /* special_function */
380          "R_PPC64_ADDR16_HA",   /* name */
381          FALSE,                 /* partial_inplace */
382          0,                     /* src_mask */
383          0xffff,                /* dst_mask */
384          FALSE),                /* pcrel_offset */
385
386   /* An absolute 16 bit branch; the lower two bits must be zero.
387      FIXME: we don't check that, we just clear them.  */
388   HOWTO (R_PPC64_ADDR14,        /* type */
389          0,                     /* rightshift */
390          2,                     /* size (0 = byte, 1 = short, 2 = long) */
391          16,                    /* bitsize */
392          FALSE,                 /* pc_relative */
393          0,                     /* bitpos */
394          complain_overflow_signed, /* complain_on_overflow */
395          ppc64_elf_branch_reloc, /* special_function */
396          "R_PPC64_ADDR14",      /* name */
397          FALSE,                 /* partial_inplace */
398          0,                     /* src_mask */
399          0x0000fffc,            /* dst_mask */
400          FALSE),                /* pcrel_offset */
401
402   /* An absolute 16 bit branch, for which bit 10 should be set to
403      indicate that the branch is expected to be taken.  The lower two
404      bits must be zero.  */
405   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
406          0,                     /* rightshift */
407          2,                     /* size (0 = byte, 1 = short, 2 = long) */
408          16,                    /* bitsize */
409          FALSE,                 /* pc_relative */
410          0,                     /* bitpos */
411          complain_overflow_signed, /* complain_on_overflow */
412          ppc64_elf_brtaken_reloc, /* special_function */
413          "R_PPC64_ADDR14_BRTAKEN",/* name */
414          FALSE,                 /* partial_inplace */
415          0,                     /* src_mask */
416          0x0000fffc,            /* dst_mask */
417          FALSE),                /* pcrel_offset */
418
419   /* An absolute 16 bit branch, for which bit 10 should be set to
420      indicate that the branch is not expected to be taken.  The lower
421      two bits must be zero.  */
422   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
423          0,                     /* rightshift */
424          2,                     /* size (0 = byte, 1 = short, 2 = long) */
425          16,                    /* bitsize */
426          FALSE,                 /* pc_relative */
427          0,                     /* bitpos */
428          complain_overflow_signed, /* complain_on_overflow */
429          ppc64_elf_brtaken_reloc, /* special_function */
430          "R_PPC64_ADDR14_BRNTAKEN",/* name */
431          FALSE,                 /* partial_inplace */
432          0,                     /* src_mask */
433          0x0000fffc,            /* dst_mask */
434          FALSE),                /* pcrel_offset */
435
436   /* A relative 26 bit branch; the lower two bits must be zero.  */
437   HOWTO (R_PPC64_REL24,         /* type */
438          0,                     /* rightshift */
439          2,                     /* size (0 = byte, 1 = short, 2 = long) */
440          26,                    /* bitsize */
441          TRUE,                  /* pc_relative */
442          0,                     /* bitpos */
443          complain_overflow_signed, /* complain_on_overflow */
444          ppc64_elf_branch_reloc, /* special_function */
445          "R_PPC64_REL24",       /* name */
446          FALSE,                 /* partial_inplace */
447          0,                     /* src_mask */
448          0x03fffffc,            /* dst_mask */
449          TRUE),                 /* pcrel_offset */
450
451   /* A relative 16 bit branch; the lower two bits must be zero.  */
452   HOWTO (R_PPC64_REL14,         /* type */
453          0,                     /* rightshift */
454          2,                     /* size (0 = byte, 1 = short, 2 = long) */
455          16,                    /* bitsize */
456          TRUE,                  /* pc_relative */
457          0,                     /* bitpos */
458          complain_overflow_signed, /* complain_on_overflow */
459          ppc64_elf_branch_reloc, /* special_function */
460          "R_PPC64_REL14",       /* name */
461          FALSE,                 /* partial_inplace */
462          0,                     /* src_mask */
463          0x0000fffc,            /* dst_mask */
464          TRUE),                 /* pcrel_offset */
465
466   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
467      the branch is expected to be taken.  The lower two bits must be
468      zero.  */
469   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
470          0,                     /* rightshift */
471          2,                     /* size (0 = byte, 1 = short, 2 = long) */
472          16,                    /* bitsize */
473          TRUE,                  /* pc_relative */
474          0,                     /* bitpos */
475          complain_overflow_signed, /* complain_on_overflow */
476          ppc64_elf_brtaken_reloc, /* special_function */
477          "R_PPC64_REL14_BRTAKEN", /* name */
478          FALSE,                 /* partial_inplace */
479          0,                     /* src_mask */
480          0x0000fffc,            /* dst_mask */
481          TRUE),                 /* pcrel_offset */
482
483   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
484      the branch is not expected to be taken.  The lower two bits must
485      be zero.  */
486   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
487          0,                     /* rightshift */
488          2,                     /* size (0 = byte, 1 = short, 2 = long) */
489          16,                    /* bitsize */
490          TRUE,                  /* pc_relative */
491          0,                     /* bitpos */
492          complain_overflow_signed, /* complain_on_overflow */
493          ppc64_elf_brtaken_reloc, /* special_function */
494          "R_PPC64_REL14_BRNTAKEN",/* name */
495          FALSE,                 /* partial_inplace */
496          0,                     /* src_mask */
497          0x0000fffc,            /* dst_mask */
498          TRUE),                 /* pcrel_offset */
499
500   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
501      symbol.  */
502   HOWTO (R_PPC64_GOT16,         /* type */
503          0,                     /* rightshift */
504          1,                     /* size (0 = byte, 1 = short, 2 = long) */
505          16,                    /* bitsize */
506          FALSE,                 /* pc_relative */
507          0,                     /* bitpos */
508          complain_overflow_signed, /* complain_on_overflow */
509          ppc64_elf_unhandled_reloc, /* special_function */
510          "R_PPC64_GOT16",       /* name */
511          FALSE,                 /* partial_inplace */
512          0,                     /* src_mask */
513          0xffff,                /* dst_mask */
514          FALSE),                /* pcrel_offset */
515
516   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
517      the symbol.  */
518   HOWTO (R_PPC64_GOT16_LO,      /* type */
519          0,                     /* rightshift */
520          1,                     /* size (0 = byte, 1 = short, 2 = long) */
521          16,                    /* bitsize */
522          FALSE,                 /* pc_relative */
523          0,                     /* bitpos */
524          complain_overflow_dont, /* complain_on_overflow */
525          ppc64_elf_unhandled_reloc, /* special_function */
526          "R_PPC64_GOT16_LO",    /* name */
527          FALSE,                 /* partial_inplace */
528          0,                     /* src_mask */
529          0xffff,                /* dst_mask */
530          FALSE),                /* pcrel_offset */
531
532   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
533      the symbol.  */
534   HOWTO (R_PPC64_GOT16_HI,      /* type */
535          16,                    /* rightshift */
536          1,                     /* size (0 = byte, 1 = short, 2 = long) */
537          16,                    /* bitsize */
538          FALSE,                 /* pc_relative */
539          0,                     /* bitpos */
540          complain_overflow_signed,/* complain_on_overflow */
541          ppc64_elf_unhandled_reloc, /* special_function */
542          "R_PPC64_GOT16_HI",    /* name */
543          FALSE,                 /* partial_inplace */
544          0,                     /* src_mask */
545          0xffff,                /* dst_mask */
546          FALSE),                /* pcrel_offset */
547
548   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
549      the symbol.  */
550   HOWTO (R_PPC64_GOT16_HA,      /* type */
551          16,                    /* rightshift */
552          1,                     /* size (0 = byte, 1 = short, 2 = long) */
553          16,                    /* bitsize */
554          FALSE,                 /* pc_relative */
555          0,                     /* bitpos */
556          complain_overflow_signed,/* complain_on_overflow */
557          ppc64_elf_unhandled_reloc, /* special_function */
558          "R_PPC64_GOT16_HA",    /* name */
559          FALSE,                 /* partial_inplace */
560          0,                     /* src_mask */
561          0xffff,                /* dst_mask */
562          FALSE),                /* pcrel_offset */
563
564   /* This is used only by the dynamic linker.  The symbol should exist
565      both in the object being run and in some shared library.  The
566      dynamic linker copies the data addressed by the symbol from the
567      shared library into the object, because the object being
568      run has to have the data at some particular address.  */
569   HOWTO (R_PPC64_COPY,          /* type */
570          0,                     /* rightshift */
571          0,                     /* this one is variable size */
572          0,                     /* bitsize */
573          FALSE,                 /* pc_relative */
574          0,                     /* bitpos */
575          complain_overflow_dont, /* complain_on_overflow */
576          ppc64_elf_unhandled_reloc, /* special_function */
577          "R_PPC64_COPY",        /* name */
578          FALSE,                 /* partial_inplace */
579          0,                     /* src_mask */
580          0,                     /* dst_mask */
581          FALSE),                /* pcrel_offset */
582
583   /* Like R_PPC64_ADDR64, but used when setting global offset table
584      entries.  */
585   HOWTO (R_PPC64_GLOB_DAT,      /* type */
586          0,                     /* rightshift */
587          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
588          64,                    /* bitsize */
589          FALSE,                 /* pc_relative */
590          0,                     /* bitpos */
591          complain_overflow_dont, /* complain_on_overflow */
592          ppc64_elf_unhandled_reloc,  /* special_function */
593          "R_PPC64_GLOB_DAT",    /* name */
594          FALSE,                 /* partial_inplace */
595          0,                     /* src_mask */
596          ONES (64),             /* dst_mask */
597          FALSE),                /* pcrel_offset */
598
599   /* Created by the link editor.  Marks a procedure linkage table
600      entry for a symbol.  */
601   HOWTO (R_PPC64_JMP_SLOT,      /* type */
602          0,                     /* rightshift */
603          0,                     /* size (0 = byte, 1 = short, 2 = long) */
604          0,                     /* bitsize */
605          FALSE,                 /* pc_relative */
606          0,                     /* bitpos */
607          complain_overflow_dont, /* complain_on_overflow */
608          ppc64_elf_unhandled_reloc, /* special_function */
609          "R_PPC64_JMP_SLOT",    /* name */
610          FALSE,                 /* partial_inplace */
611          0,                     /* src_mask */
612          0,                     /* dst_mask */
613          FALSE),                /* pcrel_offset */
614
615   /* Used only by the dynamic linker.  When the object is run, this
616      doubleword64 is set to the load address of the object, plus the
617      addend.  */
618   HOWTO (R_PPC64_RELATIVE,      /* type */
619          0,                     /* rightshift */
620          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
621          64,                    /* bitsize */
622          FALSE,                 /* pc_relative */
623          0,                     /* bitpos */
624          complain_overflow_dont, /* complain_on_overflow */
625          bfd_elf_generic_reloc, /* special_function */
626          "R_PPC64_RELATIVE",    /* name */
627          FALSE,                 /* partial_inplace */
628          0,                     /* src_mask */
629          ONES (64),             /* dst_mask */
630          FALSE),                /* pcrel_offset */
631
632   /* Like R_PPC64_ADDR32, but may be unaligned.  */
633   HOWTO (R_PPC64_UADDR32,       /* type */
634          0,                     /* rightshift */
635          2,                     /* size (0 = byte, 1 = short, 2 = long) */
636          32,                    /* bitsize */
637          FALSE,                 /* pc_relative */
638          0,                     /* bitpos */
639          complain_overflow_bitfield, /* complain_on_overflow */
640          bfd_elf_generic_reloc, /* special_function */
641          "R_PPC64_UADDR32",     /* name */
642          FALSE,                 /* partial_inplace */
643          0,                     /* src_mask */
644          0xffffffff,            /* dst_mask */
645          FALSE),                /* pcrel_offset */
646
647   /* Like R_PPC64_ADDR16, but may be unaligned.  */
648   HOWTO (R_PPC64_UADDR16,       /* type */
649          0,                     /* rightshift */
650          1,                     /* size (0 = byte, 1 = short, 2 = long) */
651          16,                    /* bitsize */
652          FALSE,                 /* pc_relative */
653          0,                     /* bitpos */
654          complain_overflow_bitfield, /* complain_on_overflow */
655          bfd_elf_generic_reloc, /* special_function */
656          "R_PPC64_UADDR16",     /* name */
657          FALSE,                 /* partial_inplace */
658          0,                     /* src_mask */
659          0xffff,                /* dst_mask */
660          FALSE),                /* pcrel_offset */
661
662   /* 32-bit PC relative.  */
663   HOWTO (R_PPC64_REL32,         /* type */
664          0,                     /* rightshift */
665          2,                     /* size (0 = byte, 1 = short, 2 = long) */
666          32,                    /* bitsize */
667          TRUE,                  /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_signed, /* complain_on_overflow */
670          bfd_elf_generic_reloc, /* special_function */
671          "R_PPC64_REL32",       /* name */
672          FALSE,                 /* partial_inplace */
673          0,                     /* src_mask */
674          0xffffffff,            /* dst_mask */
675          TRUE),                 /* pcrel_offset */
676
677   /* 32-bit relocation to the symbol's procedure linkage table.  */
678   HOWTO (R_PPC64_PLT32,         /* type */
679          0,                     /* rightshift */
680          2,                     /* size (0 = byte, 1 = short, 2 = long) */
681          32,                    /* bitsize */
682          FALSE,                 /* pc_relative */
683          0,                     /* bitpos */
684          complain_overflow_bitfield, /* complain_on_overflow */
685          ppc64_elf_unhandled_reloc, /* special_function */
686          "R_PPC64_PLT32",       /* name */
687          FALSE,                 /* partial_inplace */
688          0,                     /* src_mask */
689          0xffffffff,            /* dst_mask */
690          FALSE),                /* pcrel_offset */
691
692   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
693      FIXME: R_PPC64_PLTREL32 not supported.  */
694   HOWTO (R_PPC64_PLTREL32,      /* type */
695          0,                     /* rightshift */
696          2,                     /* size (0 = byte, 1 = short, 2 = long) */
697          32,                    /* bitsize */
698          TRUE,                  /* pc_relative */
699          0,                     /* bitpos */
700          complain_overflow_signed, /* complain_on_overflow */
701          ppc64_elf_unhandled_reloc, /* special_function */
702          "R_PPC64_PLTREL32",    /* name */
703          FALSE,                 /* partial_inplace */
704          0,                     /* src_mask */
705          0xffffffff,            /* dst_mask */
706          TRUE),                 /* pcrel_offset */
707
708   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
709      the symbol.  */
710   HOWTO (R_PPC64_PLT16_LO,      /* type */
711          0,                     /* rightshift */
712          1,                     /* size (0 = byte, 1 = short, 2 = long) */
713          16,                    /* bitsize */
714          FALSE,                 /* pc_relative */
715          0,                     /* bitpos */
716          complain_overflow_dont, /* complain_on_overflow */
717          ppc64_elf_unhandled_reloc, /* special_function */
718          "R_PPC64_PLT16_LO",    /* name */
719          FALSE,                 /* partial_inplace */
720          0,                     /* src_mask */
721          0xffff,                /* dst_mask */
722          FALSE),                /* pcrel_offset */
723
724   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
725      the symbol.  */
726   HOWTO (R_PPC64_PLT16_HI,      /* type */
727          16,                    /* rightshift */
728          1,                     /* size (0 = byte, 1 = short, 2 = long) */
729          16,                    /* bitsize */
730          FALSE,                 /* pc_relative */
731          0,                     /* bitpos */
732          complain_overflow_signed, /* complain_on_overflow */
733          ppc64_elf_unhandled_reloc, /* special_function */
734          "R_PPC64_PLT16_HI",    /* name */
735          FALSE,                 /* partial_inplace */
736          0,                     /* src_mask */
737          0xffff,                /* dst_mask */
738          FALSE),                /* pcrel_offset */
739
740   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
741      the symbol.  */
742   HOWTO (R_PPC64_PLT16_HA,      /* type */
743          16,                    /* rightshift */
744          1,                     /* size (0 = byte, 1 = short, 2 = long) */
745          16,                    /* bitsize */
746          FALSE,                 /* pc_relative */
747          0,                     /* bitpos */
748          complain_overflow_signed, /* complain_on_overflow */
749          ppc64_elf_unhandled_reloc, /* special_function */
750          "R_PPC64_PLT16_HA",    /* name */
751          FALSE,                 /* partial_inplace */
752          0,                     /* src_mask */
753          0xffff,                /* dst_mask */
754          FALSE),                /* pcrel_offset */
755
756   /* 16-bit section relative relocation.  */
757   HOWTO (R_PPC64_SECTOFF,       /* type */
758          0,                     /* rightshift */
759          1,                     /* size (0 = byte, 1 = short, 2 = long) */
760          16,                    /* bitsize */
761          FALSE,                 /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_signed, /* complain_on_overflow */
764          ppc64_elf_sectoff_reloc, /* special_function */
765          "R_PPC64_SECTOFF",     /* name */
766          FALSE,                 /* partial_inplace */
767          0,                     /* src_mask */
768          0xffff,                /* dst_mask */
769          FALSE),                /* pcrel_offset */
770
771   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
772   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
773          0,                     /* rightshift */
774          1,                     /* size (0 = byte, 1 = short, 2 = long) */
775          16,                    /* bitsize */
776          FALSE,                 /* pc_relative */
777          0,                     /* bitpos */
778          complain_overflow_dont, /* complain_on_overflow */
779          ppc64_elf_sectoff_reloc, /* special_function */
780          "R_PPC64_SECTOFF_LO",  /* name */
781          FALSE,                 /* partial_inplace */
782          0,                     /* src_mask */
783          0xffff,                /* dst_mask */
784          FALSE),                /* pcrel_offset */
785
786   /* 16-bit upper half section relative relocation.  */
787   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
788          16,                    /* rightshift */
789          1,                     /* size (0 = byte, 1 = short, 2 = long) */
790          16,                    /* bitsize */
791          FALSE,                 /* pc_relative */
792          0,                     /* bitpos */
793          complain_overflow_signed, /* complain_on_overflow */
794          ppc64_elf_sectoff_reloc, /* special_function */
795          "R_PPC64_SECTOFF_HI",  /* name */
796          FALSE,                 /* partial_inplace */
797          0,                     /* src_mask */
798          0xffff,                /* dst_mask */
799          FALSE),                /* pcrel_offset */
800
801   /* 16-bit upper half adjusted section relative relocation.  */
802   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
803          16,                    /* rightshift */
804          1,                     /* size (0 = byte, 1 = short, 2 = long) */
805          16,                    /* bitsize */
806          FALSE,                 /* pc_relative */
807          0,                     /* bitpos */
808          complain_overflow_signed, /* complain_on_overflow */
809          ppc64_elf_sectoff_ha_reloc, /* special_function */
810          "R_PPC64_SECTOFF_HA",  /* name */
811          FALSE,                 /* partial_inplace */
812          0,                     /* src_mask */
813          0xffff,                /* dst_mask */
814          FALSE),                /* pcrel_offset */
815
816   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
817   HOWTO (R_PPC64_REL30,         /* type */
818          2,                     /* rightshift */
819          2,                     /* size (0 = byte, 1 = short, 2 = long) */
820          30,                    /* bitsize */
821          TRUE,                  /* pc_relative */
822          0,                     /* bitpos */
823          complain_overflow_dont, /* complain_on_overflow */
824          bfd_elf_generic_reloc, /* special_function */
825          "R_PPC64_REL30",       /* name */
826          FALSE,                 /* partial_inplace */
827          0,                     /* src_mask */
828          0xfffffffc,            /* dst_mask */
829          TRUE),                 /* pcrel_offset */
830
831   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
832
833   /* A standard 64-bit relocation.  */
834   HOWTO (R_PPC64_ADDR64,        /* type */
835          0,                     /* rightshift */
836          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
837          64,                    /* bitsize */
838          FALSE,                 /* pc_relative */
839          0,                     /* bitpos */
840          complain_overflow_dont, /* complain_on_overflow */
841          bfd_elf_generic_reloc, /* special_function */
842          "R_PPC64_ADDR64",      /* name */
843          FALSE,                 /* partial_inplace */
844          0,                     /* src_mask */
845          ONES (64),             /* dst_mask */
846          FALSE),                /* pcrel_offset */
847
848   /* The bits 32-47 of an address.  */
849   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
850          32,                    /* rightshift */
851          1,                     /* size (0 = byte, 1 = short, 2 = long) */
852          16,                    /* bitsize */
853          FALSE,                 /* pc_relative */
854          0,                     /* bitpos */
855          complain_overflow_dont, /* complain_on_overflow */
856          bfd_elf_generic_reloc, /* special_function */
857          "R_PPC64_ADDR16_HIGHER", /* name */
858          FALSE,                 /* partial_inplace */
859          0,                     /* src_mask */
860          0xffff,                /* dst_mask */
861          FALSE),                /* pcrel_offset */
862
863   /* The bits 32-47 of an address, plus 1 if the contents of the low
864      16 bits, treated as a signed number, is negative.  */
865   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
866          32,                    /* rightshift */
867          1,                     /* size (0 = byte, 1 = short, 2 = long) */
868          16,                    /* bitsize */
869          FALSE,                 /* pc_relative */
870          0,                     /* bitpos */
871          complain_overflow_dont, /* complain_on_overflow */
872          ppc64_elf_ha_reloc,    /* special_function */
873          "R_PPC64_ADDR16_HIGHERA", /* name */
874          FALSE,                 /* partial_inplace */
875          0,                     /* src_mask */
876          0xffff,                /* dst_mask */
877          FALSE),                /* pcrel_offset */
878
879   /* The bits 48-63 of an address.  */
880   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
881          48,                    /* rightshift */
882          1,                     /* size (0 = byte, 1 = short, 2 = long) */
883          16,                    /* bitsize */
884          FALSE,                 /* pc_relative */
885          0,                     /* bitpos */
886          complain_overflow_dont, /* complain_on_overflow */
887          bfd_elf_generic_reloc, /* special_function */
888          "R_PPC64_ADDR16_HIGHEST", /* name */
889          FALSE,                 /* partial_inplace */
890          0,                     /* src_mask */
891          0xffff,                /* dst_mask */
892          FALSE),                /* pcrel_offset */
893
894   /* The bits 48-63 of an address, plus 1 if the contents of the low
895      16 bits, treated as a signed number, is negative.  */
896   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
897          48,                    /* rightshift */
898          1,                     /* size (0 = byte, 1 = short, 2 = long) */
899          16,                    /* bitsize */
900          FALSE,                 /* pc_relative */
901          0,                     /* bitpos */
902          complain_overflow_dont, /* complain_on_overflow */
903          ppc64_elf_ha_reloc,    /* special_function */
904          "R_PPC64_ADDR16_HIGHESTA", /* name */
905          FALSE,                 /* partial_inplace */
906          0,                     /* src_mask */
907          0xffff,                /* dst_mask */
908          FALSE),                /* pcrel_offset */
909
910   /* Like ADDR64, but may be unaligned.  */
911   HOWTO (R_PPC64_UADDR64,       /* type */
912          0,                     /* rightshift */
913          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914          64,                    /* bitsize */
915          FALSE,                 /* pc_relative */
916          0,                     /* bitpos */
917          complain_overflow_dont, /* complain_on_overflow */
918          bfd_elf_generic_reloc, /* special_function */
919          "R_PPC64_UADDR64",     /* name */
920          FALSE,                 /* partial_inplace */
921          0,                     /* src_mask */
922          ONES (64),             /* dst_mask */
923          FALSE),                /* pcrel_offset */
924
925   /* 64-bit relative relocation.  */
926   HOWTO (R_PPC64_REL64,         /* type */
927          0,                     /* rightshift */
928          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
929          64,                    /* bitsize */
930          TRUE,                  /* pc_relative */
931          0,                     /* bitpos */
932          complain_overflow_dont, /* complain_on_overflow */
933          bfd_elf_generic_reloc, /* special_function */
934          "R_PPC64_REL64",       /* name */
935          FALSE,                 /* partial_inplace */
936          0,                     /* src_mask */
937          ONES (64),             /* dst_mask */
938          TRUE),                 /* pcrel_offset */
939
940   /* 64-bit relocation to the symbol's procedure linkage table.  */
941   HOWTO (R_PPC64_PLT64,         /* type */
942          0,                     /* rightshift */
943          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
944          64,                    /* bitsize */
945          FALSE,                 /* pc_relative */
946          0,                     /* bitpos */
947          complain_overflow_dont, /* complain_on_overflow */
948          ppc64_elf_unhandled_reloc, /* special_function */
949          "R_PPC64_PLT64",       /* name */
950          FALSE,                 /* partial_inplace */
951          0,                     /* src_mask */
952          ONES (64),             /* dst_mask */
953          FALSE),                /* pcrel_offset */
954
955   /* 64-bit PC relative relocation to the symbol's procedure linkage
956      table.  */
957   /* FIXME: R_PPC64_PLTREL64 not supported.  */
958   HOWTO (R_PPC64_PLTREL64,      /* type */
959          0,                     /* rightshift */
960          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
961          64,                    /* bitsize */
962          TRUE,                  /* pc_relative */
963          0,                     /* bitpos */
964          complain_overflow_dont, /* complain_on_overflow */
965          ppc64_elf_unhandled_reloc, /* special_function */
966          "R_PPC64_PLTREL64",    /* name */
967          FALSE,                 /* partial_inplace */
968          0,                     /* src_mask */
969          ONES (64),             /* dst_mask */
970          TRUE),                 /* pcrel_offset */
971
972   /* 16 bit TOC-relative relocation.  */
973
974   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
975   HOWTO (R_PPC64_TOC16,         /* type */
976          0,                     /* rightshift */
977          1,                     /* size (0 = byte, 1 = short, 2 = long) */
978          16,                    /* bitsize */
979          FALSE,                 /* pc_relative */
980          0,                     /* bitpos */
981          complain_overflow_signed, /* complain_on_overflow */
982          ppc64_elf_toc_reloc,   /* special_function */
983          "R_PPC64_TOC16",       /* name */
984          FALSE,                 /* partial_inplace */
985          0,                     /* src_mask */
986          0xffff,                /* dst_mask */
987          FALSE),                /* pcrel_offset */
988
989   /* 16 bit TOC-relative relocation without overflow.  */
990
991   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
992   HOWTO (R_PPC64_TOC16_LO,      /* type */
993          0,                     /* rightshift */
994          1,                     /* size (0 = byte, 1 = short, 2 = long) */
995          16,                    /* bitsize */
996          FALSE,                 /* pc_relative */
997          0,                     /* bitpos */
998          complain_overflow_dont, /* complain_on_overflow */
999          ppc64_elf_toc_reloc,   /* special_function */
1000          "R_PPC64_TOC16_LO",    /* name */
1001          FALSE,                 /* partial_inplace */
1002          0,                     /* src_mask */
1003          0xffff,                /* dst_mask */
1004          FALSE),                /* pcrel_offset */
1005
1006   /* 16 bit TOC-relative relocation, high 16 bits.  */
1007
1008   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
1009   HOWTO (R_PPC64_TOC16_HI,      /* type */
1010          16,                    /* rightshift */
1011          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1012          16,                    /* bitsize */
1013          FALSE,                 /* pc_relative */
1014          0,                     /* bitpos */
1015          complain_overflow_signed, /* complain_on_overflow */
1016          ppc64_elf_toc_reloc,   /* special_function */
1017          "R_PPC64_TOC16_HI",    /* name */
1018          FALSE,                 /* partial_inplace */
1019          0,                     /* src_mask */
1020          0xffff,                /* dst_mask */
1021          FALSE),                /* pcrel_offset */
1022
1023   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1024      contents of the low 16 bits, treated as a signed number, is
1025      negative.  */
1026
1027   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1028   HOWTO (R_PPC64_TOC16_HA,      /* type */
1029          16,                    /* rightshift */
1030          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1031          16,                    /* bitsize */
1032          FALSE,                 /* pc_relative */
1033          0,                     /* bitpos */
1034          complain_overflow_signed, /* complain_on_overflow */
1035          ppc64_elf_toc_ha_reloc, /* special_function */
1036          "R_PPC64_TOC16_HA",    /* name */
1037          FALSE,                 /* partial_inplace */
1038          0,                     /* src_mask */
1039          0xffff,                /* dst_mask */
1040          FALSE),                /* pcrel_offset */
1041
1042   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1043
1044   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1045   HOWTO (R_PPC64_TOC,           /* type */
1046          0,                     /* rightshift */
1047          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1048          64,                    /* bitsize */
1049          FALSE,                 /* pc_relative */
1050          0,                     /* bitpos */
1051          complain_overflow_dont, /* complain_on_overflow */
1052          ppc64_elf_toc64_reloc, /* special_function */
1053          "R_PPC64_TOC",         /* name */
1054          FALSE,                 /* partial_inplace */
1055          0,                     /* src_mask */
1056          ONES (64),             /* dst_mask */
1057          FALSE),                /* pcrel_offset */
1058
1059   /* Like R_PPC64_GOT16, but also informs the link editor that the
1060      value to relocate may (!) refer to a PLT entry which the link
1061      editor (a) may replace with the symbol value.  If the link editor
1062      is unable to fully resolve the symbol, it may (b) create a PLT
1063      entry and store the address to the new PLT entry in the GOT.
1064      This permits lazy resolution of function symbols at run time.
1065      The link editor may also skip all of this and just (c) emit a
1066      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1067   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1068     HOWTO (R_PPC64_PLTGOT16,    /* type */
1069          0,                     /* rightshift */
1070          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1071          16,                    /* bitsize */
1072          FALSE,                 /* pc_relative */
1073          0,                     /* bitpos */
1074          complain_overflow_signed, /* complain_on_overflow */
1075          ppc64_elf_unhandled_reloc, /* special_function */
1076          "R_PPC64_PLTGOT16",    /* name */
1077          FALSE,                 /* partial_inplace */
1078          0,                     /* src_mask */
1079          0xffff,                /* dst_mask */
1080          FALSE),                /* pcrel_offset */
1081
1082   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1083   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1084   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1085          0,                     /* rightshift */
1086          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1087          16,                    /* bitsize */
1088          FALSE,                 /* pc_relative */
1089          0,                     /* bitpos */
1090          complain_overflow_dont, /* complain_on_overflow */
1091          ppc64_elf_unhandled_reloc, /* special_function */
1092          "R_PPC64_PLTGOT16_LO", /* name */
1093          FALSE,                 /* partial_inplace */
1094          0,                     /* src_mask */
1095          0xffff,                /* dst_mask */
1096          FALSE),                /* pcrel_offset */
1097
1098   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1099   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1100   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1101          16,                    /* rightshift */
1102          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1103          16,                    /* bitsize */
1104          FALSE,                 /* pc_relative */
1105          0,                     /* bitpos */
1106          complain_overflow_signed, /* complain_on_overflow */
1107          ppc64_elf_unhandled_reloc, /* special_function */
1108          "R_PPC64_PLTGOT16_HI", /* name */
1109          FALSE,                 /* partial_inplace */
1110          0,                     /* src_mask */
1111          0xffff,                /* dst_mask */
1112          FALSE),                /* pcrel_offset */
1113
1114   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1115      1 if the contents of the low 16 bits, treated as a signed number,
1116      is negative.  */
1117   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1118   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1119          16,                    /* rightshift */
1120          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1121          16,                    /* bitsize */
1122          FALSE,                 /* pc_relative */
1123          0,                     /* bitpos */
1124          complain_overflow_signed, /* complain_on_overflow */
1125          ppc64_elf_unhandled_reloc, /* special_function */
1126          "R_PPC64_PLTGOT16_HA", /* name */
1127          FALSE,                 /* partial_inplace */
1128          0,                     /* src_mask */
1129          0xffff,                /* dst_mask */
1130          FALSE),                /* pcrel_offset */
1131
1132   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1133   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1134          0,                     /* rightshift */
1135          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1136          16,                    /* bitsize */
1137          FALSE,                 /* pc_relative */
1138          0,                     /* bitpos */
1139          complain_overflow_signed, /* complain_on_overflow */
1140          bfd_elf_generic_reloc, /* special_function */
1141          "R_PPC64_ADDR16_DS",   /* name */
1142          FALSE,                 /* partial_inplace */
1143          0,                     /* src_mask */
1144          0xfffc,                /* dst_mask */
1145          FALSE),                /* pcrel_offset */
1146
1147   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1148   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1149          0,                     /* rightshift */
1150          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1151          16,                    /* bitsize */
1152          FALSE,                 /* pc_relative */
1153          0,                     /* bitpos */
1154          complain_overflow_dont,/* complain_on_overflow */
1155          bfd_elf_generic_reloc, /* special_function */
1156          "R_PPC64_ADDR16_LO_DS",/* name */
1157          FALSE,                 /* partial_inplace */
1158          0,                     /* src_mask */
1159          0xfffc,                /* dst_mask */
1160          FALSE),                /* pcrel_offset */
1161
1162   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1163   HOWTO (R_PPC64_GOT16_DS,      /* type */
1164          0,                     /* rightshift */
1165          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1166          16,                    /* bitsize */
1167          FALSE,                 /* pc_relative */
1168          0,                     /* bitpos */
1169          complain_overflow_signed, /* complain_on_overflow */
1170          ppc64_elf_unhandled_reloc, /* special_function */
1171          "R_PPC64_GOT16_DS",    /* name */
1172          FALSE,                 /* partial_inplace */
1173          0,                     /* src_mask */
1174          0xfffc,                /* dst_mask */
1175          FALSE),                /* pcrel_offset */
1176
1177   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1178   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1179          0,                     /* rightshift */
1180          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1181          16,                    /* bitsize */
1182          FALSE,                 /* pc_relative */
1183          0,                     /* bitpos */
1184          complain_overflow_dont, /* complain_on_overflow */
1185          ppc64_elf_unhandled_reloc, /* special_function */
1186          "R_PPC64_GOT16_LO_DS", /* name */
1187          FALSE,                 /* partial_inplace */
1188          0,                     /* src_mask */
1189          0xfffc,                /* dst_mask */
1190          FALSE),                /* pcrel_offset */
1191
1192   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1193   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1194          0,                     /* rightshift */
1195          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1196          16,                    /* bitsize */
1197          FALSE,                 /* pc_relative */
1198          0,                     /* bitpos */
1199          complain_overflow_dont, /* complain_on_overflow */
1200          ppc64_elf_unhandled_reloc, /* special_function */
1201          "R_PPC64_PLT16_LO_DS", /* name */
1202          FALSE,                 /* partial_inplace */
1203          0,                     /* src_mask */
1204          0xfffc,                /* dst_mask */
1205          FALSE),                /* pcrel_offset */
1206
1207   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1208   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1209          0,                     /* rightshift */
1210          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1211          16,                    /* bitsize */
1212          FALSE,                 /* pc_relative */
1213          0,                     /* bitpos */
1214          complain_overflow_signed, /* complain_on_overflow */
1215          ppc64_elf_sectoff_reloc, /* special_function */
1216          "R_PPC64_SECTOFF_DS",  /* name */
1217          FALSE,                 /* partial_inplace */
1218          0,                     /* src_mask */
1219          0xfffc,                /* dst_mask */
1220          FALSE),                /* pcrel_offset */
1221
1222   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1223   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1224          0,                     /* rightshift */
1225          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1226          16,                    /* bitsize */
1227          FALSE,                 /* pc_relative */
1228          0,                     /* bitpos */
1229          complain_overflow_dont, /* complain_on_overflow */
1230          ppc64_elf_sectoff_reloc, /* special_function */
1231          "R_PPC64_SECTOFF_LO_DS",/* name */
1232          FALSE,                 /* partial_inplace */
1233          0,                     /* src_mask */
1234          0xfffc,                /* dst_mask */
1235          FALSE),                /* pcrel_offset */
1236
1237   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1238   HOWTO (R_PPC64_TOC16_DS,      /* type */
1239          0,                     /* rightshift */
1240          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1241          16,                    /* bitsize */
1242          FALSE,                 /* pc_relative */
1243          0,                     /* bitpos */
1244          complain_overflow_signed, /* complain_on_overflow */
1245          ppc64_elf_toc_reloc,   /* special_function */
1246          "R_PPC64_TOC16_DS",    /* name */
1247          FALSE,                 /* partial_inplace */
1248          0,                     /* src_mask */
1249          0xfffc,                /* dst_mask */
1250          FALSE),                /* pcrel_offset */
1251
1252   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1253   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1254          0,                     /* rightshift */
1255          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1256          16,                    /* bitsize */
1257          FALSE,                 /* pc_relative */
1258          0,                     /* bitpos */
1259          complain_overflow_dont, /* complain_on_overflow */
1260          ppc64_elf_toc_reloc,   /* special_function */
1261          "R_PPC64_TOC16_LO_DS", /* name */
1262          FALSE,                 /* partial_inplace */
1263          0,                     /* src_mask */
1264          0xfffc,                /* dst_mask */
1265          FALSE),                /* pcrel_offset */
1266
1267   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1268   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1269   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1270          0,                     /* rightshift */
1271          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1272          16,                    /* bitsize */
1273          FALSE,                 /* pc_relative */
1274          0,                     /* bitpos */
1275          complain_overflow_signed, /* complain_on_overflow */
1276          ppc64_elf_unhandled_reloc, /* special_function */
1277          "R_PPC64_PLTGOT16_DS", /* name */
1278          FALSE,                 /* partial_inplace */
1279          0,                     /* src_mask */
1280          0xfffc,                /* dst_mask */
1281          FALSE),                /* pcrel_offset */
1282
1283   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1284   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1285   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1286          0,                     /* rightshift */
1287          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1288          16,                    /* bitsize */
1289          FALSE,                 /* pc_relative */
1290          0,                     /* bitpos */
1291          complain_overflow_dont, /* complain_on_overflow */
1292          ppc64_elf_unhandled_reloc, /* special_function */
1293          "R_PPC64_PLTGOT16_LO_DS",/* name */
1294          FALSE,                 /* partial_inplace */
1295          0,                     /* src_mask */
1296          0xfffc,                /* dst_mask */
1297          FALSE),                /* pcrel_offset */
1298
1299   /* Marker relocs for TLS.  */
1300   HOWTO (R_PPC64_TLS,
1301          0,                     /* rightshift */
1302          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1303          32,                    /* bitsize */
1304          FALSE,                 /* pc_relative */
1305          0,                     /* bitpos */
1306          complain_overflow_dont, /* complain_on_overflow */
1307          bfd_elf_generic_reloc, /* special_function */
1308          "R_PPC64_TLS",         /* name */
1309          FALSE,                 /* partial_inplace */
1310          0,                     /* src_mask */
1311          0,                     /* dst_mask */
1312          FALSE),                /* pcrel_offset */
1313
1314   HOWTO (R_PPC64_TLSGD,
1315          0,                     /* rightshift */
1316          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1317          32,                    /* bitsize */
1318          FALSE,                 /* pc_relative */
1319          0,                     /* bitpos */
1320          complain_overflow_dont, /* complain_on_overflow */
1321          bfd_elf_generic_reloc, /* special_function */
1322          "R_PPC64_TLSGD",       /* name */
1323          FALSE,                 /* partial_inplace */
1324          0,                     /* src_mask */
1325          0,                     /* dst_mask */
1326          FALSE),                /* pcrel_offset */
1327
1328   HOWTO (R_PPC64_TLSLD,
1329          0,                     /* rightshift */
1330          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1331          32,                    /* bitsize */
1332          FALSE,                 /* pc_relative */
1333          0,                     /* bitpos */
1334          complain_overflow_dont, /* complain_on_overflow */
1335          bfd_elf_generic_reloc, /* special_function */
1336          "R_PPC64_TLSLD",       /* name */
1337          FALSE,                 /* partial_inplace */
1338          0,                     /* src_mask */
1339          0,                     /* dst_mask */
1340          FALSE),                /* pcrel_offset */
1341
1342   HOWTO (R_PPC64_TOCSAVE,
1343          0,                     /* rightshift */
1344          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1345          32,                    /* bitsize */
1346          FALSE,                 /* pc_relative */
1347          0,                     /* bitpos */
1348          complain_overflow_dont, /* complain_on_overflow */
1349          bfd_elf_generic_reloc, /* special_function */
1350          "R_PPC64_TOCSAVE",     /* name */
1351          FALSE,                 /* partial_inplace */
1352          0,                     /* src_mask */
1353          0,                     /* dst_mask */
1354          FALSE),                /* pcrel_offset */
1355
1356   /* Computes the load module index of the load module that contains the
1357      definition of its TLS sym.  */
1358   HOWTO (R_PPC64_DTPMOD64,
1359          0,                     /* rightshift */
1360          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1361          64,                    /* bitsize */
1362          FALSE,                 /* pc_relative */
1363          0,                     /* bitpos */
1364          complain_overflow_dont, /* complain_on_overflow */
1365          ppc64_elf_unhandled_reloc, /* special_function */
1366          "R_PPC64_DTPMOD64",    /* name */
1367          FALSE,                 /* partial_inplace */
1368          0,                     /* src_mask */
1369          ONES (64),             /* dst_mask */
1370          FALSE),                /* pcrel_offset */
1371
1372   /* Computes a dtv-relative displacement, the difference between the value
1373      of sym+add and the base address of the thread-local storage block that
1374      contains the definition of sym, minus 0x8000.  */
1375   HOWTO (R_PPC64_DTPREL64,
1376          0,                     /* rightshift */
1377          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1378          64,                    /* bitsize */
1379          FALSE,                 /* pc_relative */
1380          0,                     /* bitpos */
1381          complain_overflow_dont, /* complain_on_overflow */
1382          ppc64_elf_unhandled_reloc, /* special_function */
1383          "R_PPC64_DTPREL64",    /* name */
1384          FALSE,                 /* partial_inplace */
1385          0,                     /* src_mask */
1386          ONES (64),             /* dst_mask */
1387          FALSE),                /* pcrel_offset */
1388
1389   /* A 16 bit dtprel reloc.  */
1390   HOWTO (R_PPC64_DTPREL16,
1391          0,                     /* rightshift */
1392          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1393          16,                    /* bitsize */
1394          FALSE,                 /* pc_relative */
1395          0,                     /* bitpos */
1396          complain_overflow_signed, /* complain_on_overflow */
1397          ppc64_elf_unhandled_reloc, /* special_function */
1398          "R_PPC64_DTPREL16",    /* name */
1399          FALSE,                 /* partial_inplace */
1400          0,                     /* src_mask */
1401          0xffff,                /* dst_mask */
1402          FALSE),                /* pcrel_offset */
1403
1404   /* Like DTPREL16, but no overflow.  */
1405   HOWTO (R_PPC64_DTPREL16_LO,
1406          0,                     /* rightshift */
1407          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1408          16,                    /* bitsize */
1409          FALSE,                 /* pc_relative */
1410          0,                     /* bitpos */
1411          complain_overflow_dont, /* complain_on_overflow */
1412          ppc64_elf_unhandled_reloc, /* special_function */
1413          "R_PPC64_DTPREL16_LO", /* name */
1414          FALSE,                 /* partial_inplace */
1415          0,                     /* src_mask */
1416          0xffff,                /* dst_mask */
1417          FALSE),                /* pcrel_offset */
1418
1419   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1420   HOWTO (R_PPC64_DTPREL16_HI,
1421          16,                    /* rightshift */
1422          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1423          16,                    /* bitsize */
1424          FALSE,                 /* pc_relative */
1425          0,                     /* bitpos */
1426          complain_overflow_signed, /* complain_on_overflow */
1427          ppc64_elf_unhandled_reloc, /* special_function */
1428          "R_PPC64_DTPREL16_HI", /* name */
1429          FALSE,                 /* partial_inplace */
1430          0,                     /* src_mask */
1431          0xffff,                /* dst_mask */
1432          FALSE),                /* pcrel_offset */
1433
1434   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1435   HOWTO (R_PPC64_DTPREL16_HA,
1436          16,                    /* rightshift */
1437          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1438          16,                    /* bitsize */
1439          FALSE,                 /* pc_relative */
1440          0,                     /* bitpos */
1441          complain_overflow_signed, /* complain_on_overflow */
1442          ppc64_elf_unhandled_reloc, /* special_function */
1443          "R_PPC64_DTPREL16_HA", /* name */
1444          FALSE,                 /* partial_inplace */
1445          0,                     /* src_mask */
1446          0xffff,                /* dst_mask */
1447          FALSE),                /* pcrel_offset */
1448
1449   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1450   HOWTO (R_PPC64_DTPREL16_HIGHER,
1451          32,                    /* rightshift */
1452          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1453          16,                    /* bitsize */
1454          FALSE,                 /* pc_relative */
1455          0,                     /* bitpos */
1456          complain_overflow_dont, /* complain_on_overflow */
1457          ppc64_elf_unhandled_reloc, /* special_function */
1458          "R_PPC64_DTPREL16_HIGHER", /* name */
1459          FALSE,                 /* partial_inplace */
1460          0,                     /* src_mask */
1461          0xffff,                /* dst_mask */
1462          FALSE),                /* pcrel_offset */
1463
1464   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1465   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1466          32,                    /* rightshift */
1467          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1468          16,                    /* bitsize */
1469          FALSE,                 /* pc_relative */
1470          0,                     /* bitpos */
1471          complain_overflow_dont, /* complain_on_overflow */
1472          ppc64_elf_unhandled_reloc, /* special_function */
1473          "R_PPC64_DTPREL16_HIGHERA", /* name */
1474          FALSE,                 /* partial_inplace */
1475          0,                     /* src_mask */
1476          0xffff,                /* dst_mask */
1477          FALSE),                /* pcrel_offset */
1478
1479   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1480   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1481          48,                    /* rightshift */
1482          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1483          16,                    /* bitsize */
1484          FALSE,                 /* pc_relative */
1485          0,                     /* bitpos */
1486          complain_overflow_dont, /* complain_on_overflow */
1487          ppc64_elf_unhandled_reloc, /* special_function */
1488          "R_PPC64_DTPREL16_HIGHEST", /* name */
1489          FALSE,                 /* partial_inplace */
1490          0,                     /* src_mask */
1491          0xffff,                /* dst_mask */
1492          FALSE),                /* pcrel_offset */
1493
1494   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1495   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1496          48,                    /* rightshift */
1497          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1498          16,                    /* bitsize */
1499          FALSE,                 /* pc_relative */
1500          0,                     /* bitpos */
1501          complain_overflow_dont, /* complain_on_overflow */
1502          ppc64_elf_unhandled_reloc, /* special_function */
1503          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1504          FALSE,                 /* partial_inplace */
1505          0,                     /* src_mask */
1506          0xffff,                /* dst_mask */
1507          FALSE),                /* pcrel_offset */
1508
1509   /* Like DTPREL16, but for insns with a DS field.  */
1510   HOWTO (R_PPC64_DTPREL16_DS,
1511          0,                     /* rightshift */
1512          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1513          16,                    /* bitsize */
1514          FALSE,                 /* pc_relative */
1515          0,                     /* bitpos */
1516          complain_overflow_signed, /* complain_on_overflow */
1517          ppc64_elf_unhandled_reloc, /* special_function */
1518          "R_PPC64_DTPREL16_DS", /* name */
1519          FALSE,                 /* partial_inplace */
1520          0,                     /* src_mask */
1521          0xfffc,                /* dst_mask */
1522          FALSE),                /* pcrel_offset */
1523
1524   /* Like DTPREL16_DS, but no overflow.  */
1525   HOWTO (R_PPC64_DTPREL16_LO_DS,
1526          0,                     /* rightshift */
1527          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1528          16,                    /* bitsize */
1529          FALSE,                 /* pc_relative */
1530          0,                     /* bitpos */
1531          complain_overflow_dont, /* complain_on_overflow */
1532          ppc64_elf_unhandled_reloc, /* special_function */
1533          "R_PPC64_DTPREL16_LO_DS", /* name */
1534          FALSE,                 /* partial_inplace */
1535          0,                     /* src_mask */
1536          0xfffc,                /* dst_mask */
1537          FALSE),                /* pcrel_offset */
1538
1539   /* Computes a tp-relative displacement, the difference between the value of
1540      sym+add and the value of the thread pointer (r13).  */
1541   HOWTO (R_PPC64_TPREL64,
1542          0,                     /* rightshift */
1543          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1544          64,                    /* bitsize */
1545          FALSE,                 /* pc_relative */
1546          0,                     /* bitpos */
1547          complain_overflow_dont, /* complain_on_overflow */
1548          ppc64_elf_unhandled_reloc, /* special_function */
1549          "R_PPC64_TPREL64",     /* name */
1550          FALSE,                 /* partial_inplace */
1551          0,                     /* src_mask */
1552          ONES (64),             /* dst_mask */
1553          FALSE),                /* pcrel_offset */
1554
1555   /* A 16 bit tprel reloc.  */
1556   HOWTO (R_PPC64_TPREL16,
1557          0,                     /* rightshift */
1558          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1559          16,                    /* bitsize */
1560          FALSE,                 /* pc_relative */
1561          0,                     /* bitpos */
1562          complain_overflow_signed, /* complain_on_overflow */
1563          ppc64_elf_unhandled_reloc, /* special_function */
1564          "R_PPC64_TPREL16",     /* name */
1565          FALSE,                 /* partial_inplace */
1566          0,                     /* src_mask */
1567          0xffff,                /* dst_mask */
1568          FALSE),                /* pcrel_offset */
1569
1570   /* Like TPREL16, but no overflow.  */
1571   HOWTO (R_PPC64_TPREL16_LO,
1572          0,                     /* rightshift */
1573          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1574          16,                    /* bitsize */
1575          FALSE,                 /* pc_relative */
1576          0,                     /* bitpos */
1577          complain_overflow_dont, /* complain_on_overflow */
1578          ppc64_elf_unhandled_reloc, /* special_function */
1579          "R_PPC64_TPREL16_LO",  /* name */
1580          FALSE,                 /* partial_inplace */
1581          0,                     /* src_mask */
1582          0xffff,                /* dst_mask */
1583          FALSE),                /* pcrel_offset */
1584
1585   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1586   HOWTO (R_PPC64_TPREL16_HI,
1587          16,                    /* rightshift */
1588          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1589          16,                    /* bitsize */
1590          FALSE,                 /* pc_relative */
1591          0,                     /* bitpos */
1592          complain_overflow_signed, /* complain_on_overflow */
1593          ppc64_elf_unhandled_reloc, /* special_function */
1594          "R_PPC64_TPREL16_HI",  /* name */
1595          FALSE,                 /* partial_inplace */
1596          0,                     /* src_mask */
1597          0xffff,                /* dst_mask */
1598          FALSE),                /* pcrel_offset */
1599
1600   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1601   HOWTO (R_PPC64_TPREL16_HA,
1602          16,                    /* rightshift */
1603          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1604          16,                    /* bitsize */
1605          FALSE,                 /* pc_relative */
1606          0,                     /* bitpos */
1607          complain_overflow_signed, /* complain_on_overflow */
1608          ppc64_elf_unhandled_reloc, /* special_function */
1609          "R_PPC64_TPREL16_HA",  /* name */
1610          FALSE,                 /* partial_inplace */
1611          0,                     /* src_mask */
1612          0xffff,                /* dst_mask */
1613          FALSE),                /* pcrel_offset */
1614
1615   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1616   HOWTO (R_PPC64_TPREL16_HIGHER,
1617          32,                    /* rightshift */
1618          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1619          16,                    /* bitsize */
1620          FALSE,                 /* pc_relative */
1621          0,                     /* bitpos */
1622          complain_overflow_dont, /* complain_on_overflow */
1623          ppc64_elf_unhandled_reloc, /* special_function */
1624          "R_PPC64_TPREL16_HIGHER",      /* name */
1625          FALSE,                 /* partial_inplace */
1626          0,                     /* src_mask */
1627          0xffff,                /* dst_mask */
1628          FALSE),                /* pcrel_offset */
1629
1630   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1631   HOWTO (R_PPC64_TPREL16_HIGHERA,
1632          32,                    /* rightshift */
1633          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1634          16,                    /* bitsize */
1635          FALSE,                 /* pc_relative */
1636          0,                     /* bitpos */
1637          complain_overflow_dont, /* complain_on_overflow */
1638          ppc64_elf_unhandled_reloc, /* special_function */
1639          "R_PPC64_TPREL16_HIGHERA", /* name */
1640          FALSE,                 /* partial_inplace */
1641          0,                     /* src_mask */
1642          0xffff,                /* dst_mask */
1643          FALSE),                /* pcrel_offset */
1644
1645   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1646   HOWTO (R_PPC64_TPREL16_HIGHEST,
1647          48,                    /* rightshift */
1648          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1649          16,                    /* bitsize */
1650          FALSE,                 /* pc_relative */
1651          0,                     /* bitpos */
1652          complain_overflow_dont, /* complain_on_overflow */
1653          ppc64_elf_unhandled_reloc, /* special_function */
1654          "R_PPC64_TPREL16_HIGHEST", /* name */
1655          FALSE,                 /* partial_inplace */
1656          0,                     /* src_mask */
1657          0xffff,                /* dst_mask */
1658          FALSE),                /* pcrel_offset */
1659
1660   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1661   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1662          48,                    /* rightshift */
1663          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1664          16,                    /* bitsize */
1665          FALSE,                 /* pc_relative */
1666          0,                     /* bitpos */
1667          complain_overflow_dont, /* complain_on_overflow */
1668          ppc64_elf_unhandled_reloc, /* special_function */
1669          "R_PPC64_TPREL16_HIGHESTA", /* name */
1670          FALSE,                 /* partial_inplace */
1671          0,                     /* src_mask */
1672          0xffff,                /* dst_mask */
1673          FALSE),                /* pcrel_offset */
1674
1675   /* Like TPREL16, but for insns with a DS field.  */
1676   HOWTO (R_PPC64_TPREL16_DS,
1677          0,                     /* rightshift */
1678          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1679          16,                    /* bitsize */
1680          FALSE,                 /* pc_relative */
1681          0,                     /* bitpos */
1682          complain_overflow_signed, /* complain_on_overflow */
1683          ppc64_elf_unhandled_reloc, /* special_function */
1684          "R_PPC64_TPREL16_DS",  /* name */
1685          FALSE,                 /* partial_inplace */
1686          0,                     /* src_mask */
1687          0xfffc,                /* dst_mask */
1688          FALSE),                /* pcrel_offset */
1689
1690   /* Like TPREL16_DS, but no overflow.  */
1691   HOWTO (R_PPC64_TPREL16_LO_DS,
1692          0,                     /* rightshift */
1693          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1694          16,                    /* bitsize */
1695          FALSE,                 /* pc_relative */
1696          0,                     /* bitpos */
1697          complain_overflow_dont, /* complain_on_overflow */
1698          ppc64_elf_unhandled_reloc, /* special_function */
1699          "R_PPC64_TPREL16_LO_DS", /* name */
1700          FALSE,                 /* partial_inplace */
1701          0,                     /* src_mask */
1702          0xfffc,                /* dst_mask */
1703          FALSE),                /* pcrel_offset */
1704
1705   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1706      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1707      to the first entry relative to the TOC base (r2).  */
1708   HOWTO (R_PPC64_GOT_TLSGD16,
1709          0,                     /* rightshift */
1710          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1711          16,                    /* bitsize */
1712          FALSE,                 /* pc_relative */
1713          0,                     /* bitpos */
1714          complain_overflow_signed, /* complain_on_overflow */
1715          ppc64_elf_unhandled_reloc, /* special_function */
1716          "R_PPC64_GOT_TLSGD16", /* name */
1717          FALSE,                 /* partial_inplace */
1718          0,                     /* src_mask */
1719          0xffff,                /* dst_mask */
1720          FALSE),                /* pcrel_offset */
1721
1722   /* Like GOT_TLSGD16, but no overflow.  */
1723   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1724          0,                     /* rightshift */
1725          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1726          16,                    /* bitsize */
1727          FALSE,                 /* pc_relative */
1728          0,                     /* bitpos */
1729          complain_overflow_dont, /* complain_on_overflow */
1730          ppc64_elf_unhandled_reloc, /* special_function */
1731          "R_PPC64_GOT_TLSGD16_LO", /* name */
1732          FALSE,                 /* partial_inplace */
1733          0,                     /* src_mask */
1734          0xffff,                /* dst_mask */
1735          FALSE),                /* pcrel_offset */
1736
1737   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1738   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1739          16,                    /* rightshift */
1740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1741          16,                    /* bitsize */
1742          FALSE,                 /* pc_relative */
1743          0,                     /* bitpos */
1744          complain_overflow_signed, /* complain_on_overflow */
1745          ppc64_elf_unhandled_reloc, /* special_function */
1746          "R_PPC64_GOT_TLSGD16_HI", /* name */
1747          FALSE,                 /* partial_inplace */
1748          0,                     /* src_mask */
1749          0xffff,                /* dst_mask */
1750          FALSE),                /* pcrel_offset */
1751
1752   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1753   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1754          16,                    /* rightshift */
1755          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1756          16,                    /* bitsize */
1757          FALSE,                 /* pc_relative */
1758          0,                     /* bitpos */
1759          complain_overflow_signed, /* complain_on_overflow */
1760          ppc64_elf_unhandled_reloc, /* special_function */
1761          "R_PPC64_GOT_TLSGD16_HA", /* name */
1762          FALSE,                 /* partial_inplace */
1763          0,                     /* src_mask */
1764          0xffff,                /* dst_mask */
1765          FALSE),                /* pcrel_offset */
1766
1767   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1768      with values (sym+add)@dtpmod and zero, and computes the offset to the
1769      first entry relative to the TOC base (r2).  */
1770   HOWTO (R_PPC64_GOT_TLSLD16,
1771          0,                     /* rightshift */
1772          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1773          16,                    /* bitsize */
1774          FALSE,                 /* pc_relative */
1775          0,                     /* bitpos */
1776          complain_overflow_signed, /* complain_on_overflow */
1777          ppc64_elf_unhandled_reloc, /* special_function */
1778          "R_PPC64_GOT_TLSLD16", /* name */
1779          FALSE,                 /* partial_inplace */
1780          0,                     /* src_mask */
1781          0xffff,                /* dst_mask */
1782          FALSE),                /* pcrel_offset */
1783
1784   /* Like GOT_TLSLD16, but no overflow.  */
1785   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1786          0,                     /* rightshift */
1787          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1788          16,                    /* bitsize */
1789          FALSE,                 /* pc_relative */
1790          0,                     /* bitpos */
1791          complain_overflow_dont, /* complain_on_overflow */
1792          ppc64_elf_unhandled_reloc, /* special_function */
1793          "R_PPC64_GOT_TLSLD16_LO", /* name */
1794          FALSE,                 /* partial_inplace */
1795          0,                     /* src_mask */
1796          0xffff,                /* dst_mask */
1797          FALSE),                /* pcrel_offset */
1798
1799   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1800   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1801          16,                    /* rightshift */
1802          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1803          16,                    /* bitsize */
1804          FALSE,                 /* pc_relative */
1805          0,                     /* bitpos */
1806          complain_overflow_signed, /* complain_on_overflow */
1807          ppc64_elf_unhandled_reloc, /* special_function */
1808          "R_PPC64_GOT_TLSLD16_HI", /* name */
1809          FALSE,                 /* partial_inplace */
1810          0,                     /* src_mask */
1811          0xffff,                /* dst_mask */
1812          FALSE),                /* pcrel_offset */
1813
1814   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1815   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1816          16,                    /* rightshift */
1817          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1818          16,                    /* bitsize */
1819          FALSE,                 /* pc_relative */
1820          0,                     /* bitpos */
1821          complain_overflow_signed, /* complain_on_overflow */
1822          ppc64_elf_unhandled_reloc, /* special_function */
1823          "R_PPC64_GOT_TLSLD16_HA", /* name */
1824          FALSE,                 /* partial_inplace */
1825          0,                     /* src_mask */
1826          0xffff,                /* dst_mask */
1827          FALSE),                /* pcrel_offset */
1828
1829   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1830      the offset to the entry relative to the TOC base (r2).  */
1831   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1832          0,                     /* rightshift */
1833          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1834          16,                    /* bitsize */
1835          FALSE,                 /* pc_relative */
1836          0,                     /* bitpos */
1837          complain_overflow_signed, /* complain_on_overflow */
1838          ppc64_elf_unhandled_reloc, /* special_function */
1839          "R_PPC64_GOT_DTPREL16_DS", /* name */
1840          FALSE,                 /* partial_inplace */
1841          0,                     /* src_mask */
1842          0xfffc,                /* dst_mask */
1843          FALSE),                /* pcrel_offset */
1844
1845   /* Like GOT_DTPREL16_DS, but no overflow.  */
1846   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1847          0,                     /* rightshift */
1848          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1849          16,                    /* bitsize */
1850          FALSE,                 /* pc_relative */
1851          0,                     /* bitpos */
1852          complain_overflow_dont, /* complain_on_overflow */
1853          ppc64_elf_unhandled_reloc, /* special_function */
1854          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1855          FALSE,                 /* partial_inplace */
1856          0,                     /* src_mask */
1857          0xfffc,                /* dst_mask */
1858          FALSE),                /* pcrel_offset */
1859
1860   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1861   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1862          16,                    /* rightshift */
1863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1864          16,                    /* bitsize */
1865          FALSE,                 /* pc_relative */
1866          0,                     /* bitpos */
1867          complain_overflow_signed, /* complain_on_overflow */
1868          ppc64_elf_unhandled_reloc, /* special_function */
1869          "R_PPC64_GOT_DTPREL16_HI", /* name */
1870          FALSE,                 /* partial_inplace */
1871          0,                     /* src_mask */
1872          0xffff,                /* dst_mask */
1873          FALSE),                /* pcrel_offset */
1874
1875   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1876   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1877          16,                    /* rightshift */
1878          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1879          16,                    /* bitsize */
1880          FALSE,                 /* pc_relative */
1881          0,                     /* bitpos */
1882          complain_overflow_signed, /* complain_on_overflow */
1883          ppc64_elf_unhandled_reloc, /* special_function */
1884          "R_PPC64_GOT_DTPREL16_HA", /* name */
1885          FALSE,                 /* partial_inplace */
1886          0,                     /* src_mask */
1887          0xffff,                /* dst_mask */
1888          FALSE),                /* pcrel_offset */
1889
1890   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1891      offset to the entry relative to the TOC base (r2).  */
1892   HOWTO (R_PPC64_GOT_TPREL16_DS,
1893          0,                     /* rightshift */
1894          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1895          16,                    /* bitsize */
1896          FALSE,                 /* pc_relative */
1897          0,                     /* bitpos */
1898          complain_overflow_signed, /* complain_on_overflow */
1899          ppc64_elf_unhandled_reloc, /* special_function */
1900          "R_PPC64_GOT_TPREL16_DS", /* name */
1901          FALSE,                 /* partial_inplace */
1902          0,                     /* src_mask */
1903          0xfffc,                /* dst_mask */
1904          FALSE),                /* pcrel_offset */
1905
1906   /* Like GOT_TPREL16_DS, but no overflow.  */
1907   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1908          0,                     /* rightshift */
1909          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1910          16,                    /* bitsize */
1911          FALSE,                 /* pc_relative */
1912          0,                     /* bitpos */
1913          complain_overflow_dont, /* complain_on_overflow */
1914          ppc64_elf_unhandled_reloc, /* special_function */
1915          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1916          FALSE,                 /* partial_inplace */
1917          0,                     /* src_mask */
1918          0xfffc,                /* dst_mask */
1919          FALSE),                /* pcrel_offset */
1920
1921   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1922   HOWTO (R_PPC64_GOT_TPREL16_HI,
1923          16,                    /* rightshift */
1924          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1925          16,                    /* bitsize */
1926          FALSE,                 /* pc_relative */
1927          0,                     /* bitpos */
1928          complain_overflow_signed, /* complain_on_overflow */
1929          ppc64_elf_unhandled_reloc, /* special_function */
1930          "R_PPC64_GOT_TPREL16_HI", /* name */
1931          FALSE,                 /* partial_inplace */
1932          0,                     /* src_mask */
1933          0xffff,                /* dst_mask */
1934          FALSE),                /* pcrel_offset */
1935
1936   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1937   HOWTO (R_PPC64_GOT_TPREL16_HA,
1938          16,                    /* rightshift */
1939          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1940          16,                    /* bitsize */
1941          FALSE,                 /* pc_relative */
1942          0,                     /* bitpos */
1943          complain_overflow_signed, /* complain_on_overflow */
1944          ppc64_elf_unhandled_reloc, /* special_function */
1945          "R_PPC64_GOT_TPREL16_HA", /* name */
1946          FALSE,                 /* partial_inplace */
1947          0,                     /* src_mask */
1948          0xffff,                /* dst_mask */
1949          FALSE),                /* pcrel_offset */
1950
1951   HOWTO (R_PPC64_JMP_IREL,      /* type */
1952          0,                     /* rightshift */
1953          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1954          0,                     /* bitsize */
1955          FALSE,                 /* pc_relative */
1956          0,                     /* bitpos */
1957          complain_overflow_dont, /* complain_on_overflow */
1958          ppc64_elf_unhandled_reloc, /* special_function */
1959          "R_PPC64_JMP_IREL",    /* name */
1960          FALSE,                 /* partial_inplace */
1961          0,                     /* src_mask */
1962          0,                     /* dst_mask */
1963          FALSE),                /* pcrel_offset */
1964
1965   HOWTO (R_PPC64_IRELATIVE,     /* type */
1966          0,                     /* rightshift */
1967          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1968          64,                    /* bitsize */
1969          FALSE,                 /* pc_relative */
1970          0,                     /* bitpos */
1971          complain_overflow_dont, /* complain_on_overflow */
1972          bfd_elf_generic_reloc, /* special_function */
1973          "R_PPC64_IRELATIVE",   /* name */
1974          FALSE,                 /* partial_inplace */
1975          0,                     /* src_mask */
1976          ONES (64),             /* dst_mask */
1977          FALSE),                /* pcrel_offset */
1978
1979   /* A 16 bit relative relocation.  */
1980   HOWTO (R_PPC64_REL16,         /* type */
1981          0,                     /* rightshift */
1982          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1983          16,                    /* bitsize */
1984          TRUE,                  /* pc_relative */
1985          0,                     /* bitpos */
1986          complain_overflow_signed, /* complain_on_overflow */
1987          bfd_elf_generic_reloc, /* special_function */
1988          "R_PPC64_REL16",       /* name */
1989          FALSE,                 /* partial_inplace */
1990          0,                     /* src_mask */
1991          0xffff,                /* dst_mask */
1992          TRUE),                 /* pcrel_offset */
1993
1994   /* A 16 bit relative relocation without overflow.  */
1995   HOWTO (R_PPC64_REL16_LO,      /* type */
1996          0,                     /* rightshift */
1997          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1998          16,                    /* bitsize */
1999          TRUE,                  /* pc_relative */
2000          0,                     /* bitpos */
2001          complain_overflow_dont,/* complain_on_overflow */
2002          bfd_elf_generic_reloc, /* special_function */
2003          "R_PPC64_REL16_LO",    /* name */
2004          FALSE,                 /* partial_inplace */
2005          0,                     /* src_mask */
2006          0xffff,                /* dst_mask */
2007          TRUE),                 /* pcrel_offset */
2008
2009   /* The high order 16 bits of a relative address.  */
2010   HOWTO (R_PPC64_REL16_HI,      /* type */
2011          16,                    /* rightshift */
2012          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2013          16,                    /* bitsize */
2014          TRUE,                  /* pc_relative */
2015          0,                     /* bitpos */
2016          complain_overflow_signed, /* complain_on_overflow */
2017          bfd_elf_generic_reloc, /* special_function */
2018          "R_PPC64_REL16_HI",    /* name */
2019          FALSE,                 /* partial_inplace */
2020          0,                     /* src_mask */
2021          0xffff,                /* dst_mask */
2022          TRUE),                 /* pcrel_offset */
2023
2024   /* The high order 16 bits of a relative address, plus 1 if the contents of
2025      the low 16 bits, treated as a signed number, is negative.  */
2026   HOWTO (R_PPC64_REL16_HA,      /* type */
2027          16,                    /* rightshift */
2028          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2029          16,                    /* bitsize */
2030          TRUE,                  /* pc_relative */
2031          0,                     /* bitpos */
2032          complain_overflow_signed, /* complain_on_overflow */
2033          ppc64_elf_ha_reloc,    /* special_function */
2034          "R_PPC64_REL16_HA",    /* name */
2035          FALSE,                 /* partial_inplace */
2036          0,                     /* src_mask */
2037          0xffff,                /* dst_mask */
2038          TRUE),                 /* pcrel_offset */
2039
2040   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2041   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2042          16,                    /* rightshift */
2043          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2044          16,                    /* bitsize */
2045          TRUE,                  /* pc_relative */
2046          0,                     /* bitpos */
2047          complain_overflow_signed, /* complain_on_overflow */
2048          ppc64_elf_ha_reloc,    /* special_function */
2049          "R_PPC64_REL16DX_HA",  /* name */
2050          FALSE,                 /* partial_inplace */
2051          0,                     /* src_mask */
2052          0x1fffc1,              /* dst_mask */
2053          TRUE),                 /* pcrel_offset */
2054
2055   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
2056   HOWTO (R_PPC64_16DX_HA,       /* type */
2057          16,                    /* rightshift */
2058          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2059          16,                    /* bitsize */
2060          FALSE,                 /* pc_relative */
2061          0,                     /* bitpos */
2062          complain_overflow_signed, /* complain_on_overflow */
2063          ppc64_elf_ha_reloc,    /* special_function */
2064          "R_PPC64_16DX_HA",     /* name */
2065          FALSE,                 /* partial_inplace */
2066          0,                     /* src_mask */
2067          0x1fffc1,              /* dst_mask */
2068          FALSE),                /* pcrel_offset */
2069
2070   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2071   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2072          16,                    /* rightshift */
2073          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2074          16,                    /* bitsize */
2075          FALSE,                 /* pc_relative */
2076          0,                     /* bitpos */
2077          complain_overflow_dont, /* complain_on_overflow */
2078          bfd_elf_generic_reloc, /* special_function */
2079          "R_PPC64_ADDR16_HIGH", /* name */
2080          FALSE,                 /* partial_inplace */
2081          0,                     /* src_mask */
2082          0xffff,                /* dst_mask */
2083          FALSE),                /* pcrel_offset */
2084
2085   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2086   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2087          16,                    /* rightshift */
2088          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2089          16,                    /* bitsize */
2090          FALSE,                 /* pc_relative */
2091          0,                     /* bitpos */
2092          complain_overflow_dont, /* complain_on_overflow */
2093          ppc64_elf_ha_reloc,    /* special_function */
2094          "R_PPC64_ADDR16_HIGHA",        /* name */
2095          FALSE,                 /* partial_inplace */
2096          0,                     /* src_mask */
2097          0xffff,                /* dst_mask */
2098          FALSE),                /* pcrel_offset */
2099
2100   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2101   HOWTO (R_PPC64_DTPREL16_HIGH,
2102          16,                    /* rightshift */
2103          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2104          16,                    /* bitsize */
2105          FALSE,                 /* pc_relative */
2106          0,                     /* bitpos */
2107          complain_overflow_dont, /* complain_on_overflow */
2108          ppc64_elf_unhandled_reloc, /* special_function */
2109          "R_PPC64_DTPREL16_HIGH", /* name */
2110          FALSE,                 /* partial_inplace */
2111          0,                     /* src_mask */
2112          0xffff,                /* dst_mask */
2113          FALSE),                /* pcrel_offset */
2114
2115   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2116   HOWTO (R_PPC64_DTPREL16_HIGHA,
2117          16,                    /* rightshift */
2118          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2119          16,                    /* bitsize */
2120          FALSE,                 /* pc_relative */
2121          0,                     /* bitpos */
2122          complain_overflow_dont, /* complain_on_overflow */
2123          ppc64_elf_unhandled_reloc, /* special_function */
2124          "R_PPC64_DTPREL16_HIGHA", /* name */
2125          FALSE,                 /* partial_inplace */
2126          0,                     /* src_mask */
2127          0xffff,                /* dst_mask */
2128          FALSE),                /* pcrel_offset */
2129
2130   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2131   HOWTO (R_PPC64_TPREL16_HIGH,
2132          16,                    /* rightshift */
2133          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2134          16,                    /* bitsize */
2135          FALSE,                 /* pc_relative */
2136          0,                     /* bitpos */
2137          complain_overflow_dont, /* complain_on_overflow */
2138          ppc64_elf_unhandled_reloc, /* special_function */
2139          "R_PPC64_TPREL16_HIGH",        /* name */
2140          FALSE,                 /* partial_inplace */
2141          0,                     /* src_mask */
2142          0xffff,                /* dst_mask */
2143          FALSE),                /* pcrel_offset */
2144
2145   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2146   HOWTO (R_PPC64_TPREL16_HIGHA,
2147          16,                    /* rightshift */
2148          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2149          16,                    /* bitsize */
2150          FALSE,                 /* pc_relative */
2151          0,                     /* bitpos */
2152          complain_overflow_dont, /* complain_on_overflow */
2153          ppc64_elf_unhandled_reloc, /* special_function */
2154          "R_PPC64_TPREL16_HIGHA",       /* name */
2155          FALSE,                 /* partial_inplace */
2156          0,                     /* src_mask */
2157          0xffff,                /* dst_mask */
2158          FALSE),                /* pcrel_offset */
2159
2160   /* Marker reloc on ELFv2 large-model function entry.  */
2161   HOWTO (R_PPC64_ENTRY,
2162          0,                     /* rightshift */
2163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2164          32,                    /* bitsize */
2165          FALSE,                 /* pc_relative */
2166          0,                     /* bitpos */
2167          complain_overflow_dont, /* complain_on_overflow */
2168          bfd_elf_generic_reloc, /* special_function */
2169          "R_PPC64_ENTRY",       /* name */
2170          FALSE,                 /* partial_inplace */
2171          0,                     /* src_mask */
2172          0,                     /* dst_mask */
2173          FALSE),                /* pcrel_offset */
2174
2175   /* Like ADDR64, but use local entry point of function.  */
2176   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2177          0,                     /* rightshift */
2178          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2179          64,                    /* bitsize */
2180          FALSE,                 /* pc_relative */
2181          0,                     /* bitpos */
2182          complain_overflow_dont, /* complain_on_overflow */
2183          bfd_elf_generic_reloc, /* special_function */
2184          "R_PPC64_ADDR64_LOCAL", /* name */
2185          FALSE,                 /* partial_inplace */
2186          0,                     /* src_mask */
2187          ONES (64),             /* dst_mask */
2188          FALSE),                /* pcrel_offset */
2189
2190   /* GNU extension to record C++ vtable hierarchy.  */
2191   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2192          0,                     /* rightshift */
2193          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2194          0,                     /* bitsize */
2195          FALSE,                 /* pc_relative */
2196          0,                     /* bitpos */
2197          complain_overflow_dont, /* complain_on_overflow */
2198          NULL,                  /* special_function */
2199          "R_PPC64_GNU_VTINHERIT", /* name */
2200          FALSE,                 /* partial_inplace */
2201          0,                     /* src_mask */
2202          0,                     /* dst_mask */
2203          FALSE),                /* pcrel_offset */
2204
2205   /* GNU extension to record C++ vtable member usage.  */
2206   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2207          0,                     /* rightshift */
2208          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2209          0,                     /* bitsize */
2210          FALSE,                 /* pc_relative */
2211          0,                     /* bitpos */
2212          complain_overflow_dont, /* complain_on_overflow */
2213          NULL,                  /* special_function */
2214          "R_PPC64_GNU_VTENTRY", /* name */
2215          FALSE,                 /* partial_inplace */
2216          0,                     /* src_mask */
2217          0,                     /* dst_mask */
2218          FALSE),                /* pcrel_offset */
2219 };
2220
2221 \f
2222 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2223    be done.  */
2224
2225 static void
2226 ppc_howto_init (void)
2227 {
2228   unsigned int i, type;
2229
2230   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2231     {
2232       type = ppc64_elf_howto_raw[i].type;
2233       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2234       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2235     }
2236 }
2237
2238 static reloc_howto_type *
2239 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2240                              bfd_reloc_code_real_type code)
2241 {
2242   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2243
2244   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2245     /* Initialize howto table if needed.  */
2246     ppc_howto_init ();
2247
2248   switch (code)
2249     {
2250     default:
2251       return NULL;
2252
2253     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2254       break;
2255     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2256       break;
2257     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2258       break;
2259     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2260       break;
2261     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2262       break;
2263     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2264       break;
2265     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2266       break;
2267     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2268       break;
2269     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2270       break;
2271     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2272       break;
2273     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2274       break;
2275     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2276       break;
2277     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2278       break;
2279     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2280       break;
2281     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2282       break;
2283     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2284       break;
2285     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2286       break;
2287     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2288       break;
2289     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2290       break;
2291     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2292       break;
2293     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2294       break;
2295     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2296       break;
2297     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2298       break;
2299     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2300       break;
2301     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2302       break;
2303     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2304       break;
2305     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2306       break;
2307     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2308       break;
2309     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2310       break;
2311     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2312       break;
2313     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2314       break;
2315     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2316       break;
2317     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2318       break;
2319     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2320       break;
2321     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2322       break;
2323     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2324       break;
2325     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2326       break;
2327     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2328       break;
2329     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2330       break;
2331     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2332       break;
2333     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2334       break;
2335     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2336       break;
2337     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2338       break;
2339     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2340       break;
2341     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2342       break;
2343     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2344       break;
2345     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2346       break;
2347     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2348       break;
2349     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2350       break;
2351     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2352       break;
2353     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2354       break;
2355     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2356       break;
2357     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2358       break;
2359     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2360       break;
2361     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2362       break;
2363     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2364       break;
2365     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2366       break;
2367     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2368       break;
2369     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2370       break;
2371     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2372       break;
2373     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2374       break;
2375     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2376       break;
2377     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2378       break;
2379     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2380       break;
2381     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2382       break;
2383     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2384       break;
2385     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2386       break;
2387     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2388       break;
2389     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2390       break;
2391     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2392       break;
2393     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2394       break;
2395     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2396       break;
2397     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2398       break;
2399     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2400       break;
2401     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2402       break;
2403     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2404       break;
2405     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2406       break;
2407     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2408       break;
2409     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2410       break;
2411     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2412       break;
2413     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2414       break;
2415     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2416       break;
2417     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2418       break;
2419     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2420       break;
2421     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2422       break;
2423     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2424       break;
2425     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2426       break;
2427     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2428       break;
2429     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2430       break;
2431     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2432       break;
2433     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2434       break;
2435     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2436       break;
2437     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2438       break;
2439     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2440       break;
2441     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2442       break;
2443     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2444       break;
2445     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2446       break;
2447     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2448       break;
2449     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2450       break;
2451     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2452       break;
2453     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2454       break;
2455     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2456       break;
2457     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2458       break;
2459     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2460       break;
2461     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2462       break;
2463     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2464       break;
2465     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2466       break;
2467     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2468       break;
2469     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2470       break;
2471     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2472       break;
2473     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2474       break;
2475     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
2476       break;
2477     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2478       break;
2479     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2480       break;
2481     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2482       break;
2483     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2484       break;
2485     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2486       break;
2487     }
2488
2489   return ppc64_elf_howto_table[r];
2490 };
2491
2492 static reloc_howto_type *
2493 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2494                              const char *r_name)
2495 {
2496   unsigned int i;
2497
2498   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2499     if (ppc64_elf_howto_raw[i].name != NULL
2500         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2501       return &ppc64_elf_howto_raw[i];
2502
2503   return NULL;
2504 }
2505
2506 /* Set the howto pointer for a PowerPC ELF reloc.  */
2507
2508 static void
2509 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2510                          Elf_Internal_Rela *dst)
2511 {
2512   unsigned int type;
2513
2514   /* Initialize howto table if needed.  */
2515   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2516     ppc_howto_init ();
2517
2518   type = ELF64_R_TYPE (dst->r_info);
2519   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2520     {
2521       /* xgettext:c-format */
2522       _bfd_error_handler (_("%B: invalid relocation type %d"),
2523                           abfd, (int) type);
2524       type = R_PPC64_NONE;
2525     }
2526   cache_ptr->howto = ppc64_elf_howto_table[type];
2527 }
2528
2529 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2530
2531 static bfd_reloc_status_type
2532 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2533                     void *data, asection *input_section,
2534                     bfd *output_bfd, char **error_message)
2535 {
2536   enum elf_ppc64_reloc_type r_type;
2537   long insn;
2538   bfd_size_type octets;
2539   bfd_vma value;
2540
2541   /* If this is a relocatable link (output_bfd test tells us), just
2542      call the generic function.  Any adjustment will be done at final
2543      link time.  */
2544   if (output_bfd != NULL)
2545     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2546                                   input_section, output_bfd, error_message);
2547
2548   /* Adjust the addend for sign extension of the low 16 bits.
2549      We won't actually be using the low 16 bits, so trashing them
2550      doesn't matter.  */
2551   reloc_entry->addend += 0x8000;
2552   r_type = reloc_entry->howto->type;
2553   if (r_type != R_PPC64_REL16DX_HA)
2554     return bfd_reloc_continue;
2555
2556   value = 0;
2557   if (!bfd_is_com_section (symbol->section))
2558     value = symbol->value;
2559   value += (reloc_entry->addend
2560             + symbol->section->output_offset
2561             + symbol->section->output_section->vma);
2562   value -= (reloc_entry->address
2563             + input_section->output_offset
2564             + input_section->output_section->vma);
2565   value = (bfd_signed_vma) value >> 16;
2566
2567   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2568   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2569   insn &= ~0x1fffc1;
2570   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2571   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2572   if (value + 0x8000 > 0xffff)
2573     return bfd_reloc_overflow;
2574   return bfd_reloc_ok;
2575 }
2576
2577 static bfd_reloc_status_type
2578 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2579                         void *data, asection *input_section,
2580                         bfd *output_bfd, char **error_message)
2581 {
2582   if (output_bfd != NULL)
2583     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2584                                   input_section, output_bfd, error_message);
2585
2586   if (strcmp (symbol->section->name, ".opd") == 0
2587       && (symbol->section->owner->flags & DYNAMIC) == 0)
2588     {
2589       bfd_vma dest = opd_entry_value (symbol->section,
2590                                       symbol->value + reloc_entry->addend,
2591                                       NULL, NULL, FALSE);
2592       if (dest != (bfd_vma) -1)
2593         reloc_entry->addend = dest - (symbol->value
2594                                       + symbol->section->output_section->vma
2595                                       + symbol->section->output_offset);
2596     }
2597   else
2598     {
2599       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2600
2601       if (symbol->section->owner != abfd
2602           && symbol->section->owner != NULL
2603           && abiversion (symbol->section->owner) >= 2)
2604         {
2605           unsigned int i;
2606
2607           for (i = 0; i < symbol->section->owner->symcount; ++i)
2608             {
2609               asymbol *symdef = symbol->section->owner->outsymbols[i];
2610
2611               if (strcmp (symdef->name, symbol->name) == 0)
2612                 {
2613                   elfsym = (elf_symbol_type *) symdef;
2614                   break;
2615                 }
2616             }
2617         }
2618       reloc_entry->addend
2619         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2620     }
2621   return bfd_reloc_continue;
2622 }
2623
2624 static bfd_reloc_status_type
2625 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2626                          void *data, asection *input_section,
2627                          bfd *output_bfd, char **error_message)
2628 {
2629   long insn;
2630   enum elf_ppc64_reloc_type r_type;
2631   bfd_size_type octets;
2632   /* Assume 'at' branch hints.  */
2633   bfd_boolean is_isa_v2 = TRUE;
2634
2635   /* If this is a relocatable link (output_bfd test tells us), just
2636      call the generic function.  Any adjustment will be done at final
2637      link time.  */
2638   if (output_bfd != NULL)
2639     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2640                                   input_section, output_bfd, error_message);
2641
2642   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2643   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2644   insn &= ~(0x01 << 21);
2645   r_type = reloc_entry->howto->type;
2646   if (r_type == R_PPC64_ADDR14_BRTAKEN
2647       || r_type == R_PPC64_REL14_BRTAKEN)
2648     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2649
2650   if (is_isa_v2)
2651     {
2652       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2653          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2654          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2655       if ((insn & (0x14 << 21)) == (0x04 << 21))
2656         insn |= 0x02 << 21;
2657       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2658         insn |= 0x08 << 21;
2659       else
2660         goto out;
2661     }
2662   else
2663     {
2664       bfd_vma target = 0;
2665       bfd_vma from;
2666
2667       if (!bfd_is_com_section (symbol->section))
2668         target = symbol->value;
2669       target += symbol->section->output_section->vma;
2670       target += symbol->section->output_offset;
2671       target += reloc_entry->addend;
2672
2673       from = (reloc_entry->address
2674               + input_section->output_offset
2675               + input_section->output_section->vma);
2676
2677       /* Invert 'y' bit if not the default.  */
2678       if ((bfd_signed_vma) (target - from) < 0)
2679         insn ^= 0x01 << 21;
2680     }
2681   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2682  out:
2683   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2684                                  input_section, output_bfd, error_message);
2685 }
2686
2687 static bfd_reloc_status_type
2688 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2689                          void *data, asection *input_section,
2690                          bfd *output_bfd, char **error_message)
2691 {
2692   /* If this is a relocatable link (output_bfd test tells us), just
2693      call the generic function.  Any adjustment will be done at final
2694      link time.  */
2695   if (output_bfd != NULL)
2696     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2697                                   input_section, output_bfd, error_message);
2698
2699   /* Subtract the symbol section base address.  */
2700   reloc_entry->addend -= symbol->section->output_section->vma;
2701   return bfd_reloc_continue;
2702 }
2703
2704 static bfd_reloc_status_type
2705 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2706                             void *data, asection *input_section,
2707                             bfd *output_bfd, char **error_message)
2708 {
2709   /* If this is a relocatable link (output_bfd test tells us), just
2710      call the generic function.  Any adjustment will be done at final
2711      link time.  */
2712   if (output_bfd != NULL)
2713     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2714                                   input_section, output_bfd, error_message);
2715
2716   /* Subtract the symbol section base address.  */
2717   reloc_entry->addend -= symbol->section->output_section->vma;
2718
2719   /* Adjust the addend for sign extension of the low 16 bits.  */
2720   reloc_entry->addend += 0x8000;
2721   return bfd_reloc_continue;
2722 }
2723
2724 static bfd_reloc_status_type
2725 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2726                      void *data, asection *input_section,
2727                      bfd *output_bfd, char **error_message)
2728 {
2729   bfd_vma TOCstart;
2730
2731   /* If this is a relocatable link (output_bfd test tells us), just
2732      call the generic function.  Any adjustment will be done at final
2733      link time.  */
2734   if (output_bfd != NULL)
2735     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2736                                   input_section, output_bfd, error_message);
2737
2738   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2739   if (TOCstart == 0)
2740     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2741
2742   /* Subtract the TOC base address.  */
2743   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2744   return bfd_reloc_continue;
2745 }
2746
2747 static bfd_reloc_status_type
2748 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2749                         void *data, asection *input_section,
2750                         bfd *output_bfd, char **error_message)
2751 {
2752   bfd_vma TOCstart;
2753
2754   /* If this is a relocatable link (output_bfd test tells us), just
2755      call the generic function.  Any adjustment will be done at final
2756      link time.  */
2757   if (output_bfd != NULL)
2758     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2759                                   input_section, output_bfd, error_message);
2760
2761   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2762   if (TOCstart == 0)
2763     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2764
2765   /* Subtract the TOC base address.  */
2766   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2767
2768   /* Adjust the addend for sign extension of the low 16 bits.  */
2769   reloc_entry->addend += 0x8000;
2770   return bfd_reloc_continue;
2771 }
2772
2773 static bfd_reloc_status_type
2774 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2775                        void *data, asection *input_section,
2776                        bfd *output_bfd, char **error_message)
2777 {
2778   bfd_vma TOCstart;
2779   bfd_size_type octets;
2780
2781   /* If this is a relocatable link (output_bfd test tells us), just
2782      call the generic function.  Any adjustment will be done at final
2783      link time.  */
2784   if (output_bfd != NULL)
2785     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2786                                   input_section, output_bfd, error_message);
2787
2788   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2789   if (TOCstart == 0)
2790     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2791
2792   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2793   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2794   return bfd_reloc_ok;
2795 }
2796
2797 static bfd_reloc_status_type
2798 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2799                            void *data, asection *input_section,
2800                            bfd *output_bfd, char **error_message)
2801 {
2802   /* If this is a relocatable link (output_bfd test tells us), just
2803      call the generic function.  Any adjustment will be done at final
2804      link time.  */
2805   if (output_bfd != NULL)
2806     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2807                                   input_section, output_bfd, error_message);
2808
2809   if (error_message != NULL)
2810     {
2811       static char buf[60];
2812       sprintf (buf, "generic linker can't handle %s",
2813                reloc_entry->howto->name);
2814       *error_message = buf;
2815     }
2816   return bfd_reloc_dangerous;
2817 }
2818
2819 /* Track GOT entries needed for a given symbol.  We might need more
2820    than one got entry per symbol.  */
2821 struct got_entry
2822 {
2823   struct got_entry *next;
2824
2825   /* The symbol addend that we'll be placing in the GOT.  */
2826   bfd_vma addend;
2827
2828   /* Unlike other ELF targets, we use separate GOT entries for the same
2829      symbol referenced from different input files.  This is to support
2830      automatic multiple TOC/GOT sections, where the TOC base can vary
2831      from one input file to another.  After partitioning into TOC groups
2832      we merge entries within the group.
2833
2834      Point to the BFD owning this GOT entry.  */
2835   bfd *owner;
2836
2837   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2838      TLS_TPREL or TLS_DTPREL for tls entries.  */
2839   unsigned char tls_type;
2840
2841   /* Non-zero if got.ent points to real entry.  */
2842   unsigned char is_indirect;
2843
2844   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2845   union
2846     {
2847       bfd_signed_vma refcount;
2848       bfd_vma offset;
2849       struct got_entry *ent;
2850     } got;
2851 };
2852
2853 /* The same for PLT.  */
2854 struct plt_entry
2855 {
2856   struct plt_entry *next;
2857
2858   bfd_vma addend;
2859
2860   union
2861     {
2862       bfd_signed_vma refcount;
2863       bfd_vma offset;
2864     } plt;
2865 };
2866
2867 struct ppc64_elf_obj_tdata
2868 {
2869   struct elf_obj_tdata elf;
2870
2871   /* Shortcuts to dynamic linker sections.  */
2872   asection *got;
2873   asection *relgot;
2874
2875   /* Used during garbage collection.  We attach global symbols defined
2876      on removed .opd entries to this section so that the sym is removed.  */
2877   asection *deleted_section;
2878
2879   /* TLS local dynamic got entry handling.  Support for multiple GOT
2880      sections means we potentially need one of these for each input bfd.  */
2881   struct got_entry tlsld_got;
2882
2883   union {
2884     /* A copy of relocs before they are modified for --emit-relocs.  */
2885     Elf_Internal_Rela *relocs;
2886
2887     /* Section contents.  */
2888     bfd_byte *contents;
2889   } opd;
2890
2891   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2892      the reloc to be in the range -32768 to 32767.  */
2893   unsigned int has_small_toc_reloc : 1;
2894
2895   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2896      instruction not one we handle.  */
2897   unsigned int unexpected_toc_insn : 1;
2898 };
2899
2900 #define ppc64_elf_tdata(bfd) \
2901   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2902
2903 #define ppc64_tlsld_got(bfd) \
2904   (&ppc64_elf_tdata (bfd)->tlsld_got)
2905
2906 #define is_ppc64_elf(bfd) \
2907   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2908    && elf_object_id (bfd) == PPC64_ELF_DATA)
2909
2910 /* Override the generic function because we store some extras.  */
2911
2912 static bfd_boolean
2913 ppc64_elf_mkobject (bfd *abfd)
2914 {
2915   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2916                                   PPC64_ELF_DATA);
2917 }
2918
2919 /* Fix bad default arch selected for a 64 bit input bfd when the
2920    default is 32 bit.  Also select arch based on apuinfo.  */
2921
2922 static bfd_boolean
2923 ppc64_elf_object_p (bfd *abfd)
2924 {
2925   if (!abfd->arch_info->the_default)
2926     return TRUE;
2927
2928   if (abfd->arch_info->bits_per_word == 32)
2929     {
2930       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2931
2932       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2933         {
2934           /* Relies on arch after 32 bit default being 64 bit default.  */
2935           abfd->arch_info = abfd->arch_info->next;
2936           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2937         }
2938     }
2939   return _bfd_elf_ppc_set_arch (abfd);
2940 }
2941
2942 /* Support for core dump NOTE sections.  */
2943
2944 static bfd_boolean
2945 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2946 {
2947   size_t offset, size;
2948
2949   if (note->descsz != 504)
2950     return FALSE;
2951
2952   /* pr_cursig */
2953   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2954
2955   /* pr_pid */
2956   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2957
2958   /* pr_reg */
2959   offset = 112;
2960   size = 384;
2961
2962   /* Make a ".reg/999" section.  */
2963   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2964                                           size, note->descpos + offset);
2965 }
2966
2967 static bfd_boolean
2968 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2969 {
2970   if (note->descsz != 136)
2971     return FALSE;
2972
2973   elf_tdata (abfd)->core->pid
2974     = bfd_get_32 (abfd, note->descdata + 24);
2975   elf_tdata (abfd)->core->program
2976     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2977   elf_tdata (abfd)->core->command
2978     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2979
2980   return TRUE;
2981 }
2982
2983 static char *
2984 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2985                            ...)
2986 {
2987   switch (note_type)
2988     {
2989     default:
2990       return NULL;
2991
2992     case NT_PRPSINFO:
2993       {
2994         char data[136];
2995         va_list ap;
2996
2997         va_start (ap, note_type);
2998         memset (data, 0, sizeof (data));
2999         strncpy (data + 40, va_arg (ap, const char *), 16);
3000         strncpy (data + 56, va_arg (ap, const char *), 80);
3001         va_end (ap);
3002         return elfcore_write_note (abfd, buf, bufsiz,
3003                                    "CORE", note_type, data, sizeof (data));
3004       }
3005
3006     case NT_PRSTATUS:
3007       {
3008         char data[504];
3009         va_list ap;
3010         long pid;
3011         int cursig;
3012         const void *greg;
3013
3014         va_start (ap, note_type);
3015         memset (data, 0, 112);
3016         pid = va_arg (ap, long);
3017         bfd_put_32 (abfd, pid, data + 32);
3018         cursig = va_arg (ap, int);
3019         bfd_put_16 (abfd, cursig, data + 12);
3020         greg = va_arg (ap, const void *);
3021         memcpy (data + 112, greg, 384);
3022         memset (data + 496, 0, 8);
3023         va_end (ap);
3024         return elfcore_write_note (abfd, buf, bufsiz,
3025                                    "CORE", note_type, data, sizeof (data));
3026       }
3027     }
3028 }
3029
3030 /* Add extra PPC sections.  */
3031
3032 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3033 {
3034   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3035   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3036   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3037   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3038   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3039   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3040   { NULL,                     0,  0, 0,            0 }
3041 };
3042
3043 enum _ppc64_sec_type {
3044   sec_normal = 0,
3045   sec_opd = 1,
3046   sec_toc = 2
3047 };
3048
3049 struct _ppc64_elf_section_data
3050 {
3051   struct bfd_elf_section_data elf;
3052
3053   union
3054   {
3055     /* An array with one entry for each opd function descriptor,
3056        and some spares since opd entries may be either 16 or 24 bytes.  */
3057 #define OPD_NDX(OFF) ((OFF) >> 4)
3058     struct _opd_sec_data
3059     {
3060       /* Points to the function code section for local opd entries.  */
3061       asection **func_sec;
3062
3063       /* After editing .opd, adjust references to opd local syms.  */
3064       long *adjust;
3065     } opd;
3066
3067     /* An array for toc sections, indexed by offset/8.  */
3068     struct _toc_sec_data
3069     {
3070       /* Specifies the relocation symbol index used at a given toc offset.  */
3071       unsigned *symndx;
3072
3073       /* And the relocation addend.  */
3074       bfd_vma *add;
3075     } toc;
3076   } u;
3077
3078   enum _ppc64_sec_type sec_type:2;
3079
3080   /* Flag set when small branches are detected.  Used to
3081      select suitable defaults for the stub group size.  */
3082   unsigned int has_14bit_branch:1;
3083 };
3084
3085 #define ppc64_elf_section_data(sec) \
3086   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3087
3088 static bfd_boolean
3089 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3090 {
3091   if (!sec->used_by_bfd)
3092     {
3093       struct _ppc64_elf_section_data *sdata;
3094       bfd_size_type amt = sizeof (*sdata);
3095
3096       sdata = bfd_zalloc (abfd, amt);
3097       if (sdata == NULL)
3098         return FALSE;
3099       sec->used_by_bfd = sdata;
3100     }
3101
3102   return _bfd_elf_new_section_hook (abfd, sec);
3103 }
3104
3105 static struct _opd_sec_data *
3106 get_opd_info (asection * sec)
3107 {
3108   if (sec != NULL
3109       && ppc64_elf_section_data (sec) != NULL
3110       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3111     return &ppc64_elf_section_data (sec)->u.opd;
3112   return NULL;
3113 }
3114 \f
3115 /* Parameters for the qsort hook.  */
3116 static bfd_boolean synthetic_relocatable;
3117 static asection *synthetic_opd;
3118
3119 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3120
3121 static int
3122 compare_symbols (const void *ap, const void *bp)
3123 {
3124   const asymbol *a = * (const asymbol **) ap;
3125   const asymbol *b = * (const asymbol **) bp;
3126
3127   /* Section symbols first.  */
3128   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3129     return -1;
3130   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3131     return 1;
3132
3133   /* then .opd symbols.  */
3134   if (synthetic_opd != NULL)
3135     {
3136       if (strcmp (a->section->name, ".opd") == 0
3137           && strcmp (b->section->name, ".opd") != 0)
3138         return -1;
3139       if (strcmp (a->section->name, ".opd") != 0
3140           && strcmp (b->section->name, ".opd") == 0)
3141         return 1;
3142     }
3143
3144   /* then other code symbols.  */
3145   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3146       == (SEC_CODE | SEC_ALLOC)
3147       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3148          != (SEC_CODE | SEC_ALLOC))
3149     return -1;
3150
3151   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3152       != (SEC_CODE | SEC_ALLOC)
3153       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3154          == (SEC_CODE | SEC_ALLOC))
3155     return 1;
3156
3157   if (synthetic_relocatable)
3158     {
3159       if (a->section->id < b->section->id)
3160         return -1;
3161
3162       if (a->section->id > b->section->id)
3163         return 1;
3164     }
3165
3166   if (a->value + a->section->vma < b->value + b->section->vma)
3167     return -1;
3168
3169   if (a->value + a->section->vma > b->value + b->section->vma)
3170     return 1;
3171
3172   /* For syms with the same value, prefer strong dynamic global function
3173      syms over other syms.  */
3174   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3175     return -1;
3176
3177   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3178     return 1;
3179
3180   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3181     return -1;
3182
3183   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3184     return 1;
3185
3186   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3187     return -1;
3188
3189   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3190     return 1;
3191
3192   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3193     return -1;
3194
3195   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3196     return 1;
3197
3198   return a > b;
3199 }
3200
3201 /* Search SYMS for a symbol of the given VALUE.  */
3202
3203 static asymbol *
3204 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3205 {
3206   long mid;
3207
3208   if (id == (unsigned) -1)
3209     {
3210       while (lo < hi)
3211         {
3212           mid = (lo + hi) >> 1;
3213           if (syms[mid]->value + syms[mid]->section->vma < value)
3214             lo = mid + 1;
3215           else if (syms[mid]->value + syms[mid]->section->vma > value)
3216             hi = mid;
3217           else
3218             return syms[mid];
3219         }
3220     }
3221   else
3222     {
3223       while (lo < hi)
3224         {
3225           mid = (lo + hi) >> 1;
3226           if (syms[mid]->section->id < id)
3227             lo = mid + 1;
3228           else if (syms[mid]->section->id > id)
3229             hi = mid;
3230           else if (syms[mid]->value < value)
3231             lo = mid + 1;
3232           else if (syms[mid]->value > value)
3233             hi = mid;
3234           else
3235             return syms[mid];
3236         }
3237     }
3238   return NULL;
3239 }
3240
3241 static bfd_boolean
3242 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3243 {
3244   bfd_vma vma = *(bfd_vma *) ptr;
3245   return ((section->flags & SEC_ALLOC) != 0
3246           && section->vma <= vma
3247           && vma < section->vma + section->size);
3248 }
3249
3250 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3251    entry syms.  Also generate @plt symbols for the glink branch table.
3252    Returns count of synthetic symbols in RET or -1 on error.  */
3253
3254 static long
3255 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3256                                 long static_count, asymbol **static_syms,
3257                                 long dyn_count, asymbol **dyn_syms,
3258                                 asymbol **ret)
3259 {
3260   asymbol *s;
3261   long i;
3262   long count;
3263   char *names;
3264   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3265   asection *opd = NULL;
3266   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3267   asymbol **syms;
3268   int abi = abiversion (abfd);
3269
3270   *ret = NULL;
3271
3272   if (abi < 2)
3273     {
3274       opd = bfd_get_section_by_name (abfd, ".opd");
3275       if (opd == NULL && abi == 1)
3276         return 0;
3277     }
3278
3279   syms = NULL;
3280   codesecsym = 0;
3281   codesecsymend = 0;
3282   secsymend = 0;
3283   opdsymend = 0;
3284   symcount = 0;
3285   if (opd != NULL)
3286     {
3287       symcount = static_count;
3288       if (!relocatable)
3289         symcount += dyn_count;
3290       if (symcount == 0)
3291         return 0;
3292
3293       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3294       if (syms == NULL)
3295         return -1;
3296
3297       if (!relocatable && static_count != 0 && dyn_count != 0)
3298         {
3299           /* Use both symbol tables.  */
3300           memcpy (syms, static_syms, static_count * sizeof (*syms));
3301           memcpy (syms + static_count, dyn_syms,
3302                   (dyn_count + 1) * sizeof (*syms));
3303         }
3304       else if (!relocatable && static_count == 0)
3305         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3306       else
3307         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3308
3309       synthetic_relocatable = relocatable;
3310       synthetic_opd = opd;
3311       qsort (syms, symcount, sizeof (*syms), compare_symbols);
3312
3313       if (!relocatable && symcount > 1)
3314         {
3315           long j;
3316           /* Trim duplicate syms, since we may have merged the normal and
3317              dynamic symbols.  Actually, we only care about syms that have
3318              different values, so trim any with the same value.  */
3319           for (i = 1, j = 1; i < symcount; ++i)
3320             if (syms[i - 1]->value + syms[i - 1]->section->vma
3321                 != syms[i]->value + syms[i]->section->vma)
3322               syms[j++] = syms[i];
3323           symcount = j;
3324         }
3325
3326       i = 0;
3327       /* Note that here and in compare_symbols we can't compare opd and
3328          sym->section directly.  With separate debug info files, the
3329          symbols will be extracted from the debug file while abfd passed
3330          to this function is the real binary.  */
3331       if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3332         ++i;
3333       codesecsym = i;
3334
3335       for (; i < symcount; ++i)
3336         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3337                                          | SEC_THREAD_LOCAL))
3338              != (SEC_CODE | SEC_ALLOC))
3339             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3340           break;
3341       codesecsymend = i;
3342
3343       for (; i < symcount; ++i)
3344         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3345           break;
3346       secsymend = i;
3347
3348       for (; i < symcount; ++i)
3349         if (strcmp (syms[i]->section->name, ".opd") != 0)
3350           break;
3351       opdsymend = i;
3352
3353       for (; i < symcount; ++i)
3354         if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3355             != (SEC_CODE | SEC_ALLOC))
3356           break;
3357       symcount = i;
3358     }
3359   count = 0;
3360
3361   if (relocatable)
3362     {
3363       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3364       arelent *r;
3365       size_t size;
3366       long relcount;
3367
3368       if (opdsymend == secsymend)
3369         goto done;
3370
3371       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3372       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3373       if (relcount == 0)
3374         goto done;
3375
3376       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3377         {
3378           count = -1;
3379           goto done;
3380         }
3381
3382       size = 0;
3383       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3384         {
3385           asymbol *sym;
3386
3387           while (r < opd->relocation + relcount
3388                  && r->address < syms[i]->value + opd->vma)
3389             ++r;
3390
3391           if (r == opd->relocation + relcount)
3392             break;
3393
3394           if (r->address != syms[i]->value + opd->vma)
3395             continue;
3396
3397           if (r->howto->type != R_PPC64_ADDR64)
3398             continue;
3399
3400           sym = *r->sym_ptr_ptr;
3401           if (!sym_exists_at (syms, opdsymend, symcount,
3402                               sym->section->id, sym->value + r->addend))
3403             {
3404               ++count;
3405               size += sizeof (asymbol);
3406               size += strlen (syms[i]->name) + 2;
3407             }
3408         }
3409
3410       if (size == 0)
3411         goto done;
3412       s = *ret = bfd_malloc (size);
3413       if (s == NULL)
3414         {
3415           count = -1;
3416           goto done;
3417         }
3418
3419       names = (char *) (s + count);
3420
3421       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3422         {
3423           asymbol *sym;
3424
3425           while (r < opd->relocation + relcount
3426                  && r->address < syms[i]->value + opd->vma)
3427             ++r;
3428
3429           if (r == opd->relocation + relcount)
3430             break;
3431
3432           if (r->address != syms[i]->value + opd->vma)
3433             continue;
3434
3435           if (r->howto->type != R_PPC64_ADDR64)
3436             continue;
3437
3438           sym = *r->sym_ptr_ptr;
3439           if (!sym_exists_at (syms, opdsymend, symcount,
3440                               sym->section->id, sym->value + r->addend))
3441             {
3442               size_t len;
3443
3444               *s = *syms[i];
3445               s->flags |= BSF_SYNTHETIC;
3446               s->section = sym->section;
3447               s->value = sym->value + r->addend;
3448               s->name = names;
3449               *names++ = '.';
3450               len = strlen (syms[i]->name);
3451               memcpy (names, syms[i]->name, len + 1);
3452               names += len + 1;
3453               /* Have udata.p point back to the original symbol this
3454                  synthetic symbol was derived from.  */
3455               s->udata.p = syms[i];
3456               s++;
3457             }
3458         }
3459     }
3460   else
3461     {
3462       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3463       bfd_byte *contents = NULL;
3464       size_t size;
3465       long plt_count = 0;
3466       bfd_vma glink_vma = 0, resolv_vma = 0;
3467       asection *dynamic, *glink = NULL, *relplt = NULL;
3468       arelent *p;
3469
3470       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3471         {
3472         free_contents_and_exit_err:
3473           count = -1;
3474         free_contents_and_exit:
3475           if (contents)
3476             free (contents);
3477           goto done;
3478         }
3479
3480       size = 0;
3481       for (i = secsymend; i < opdsymend; ++i)
3482         {
3483           bfd_vma ent;
3484
3485           /* Ignore bogus symbols.  */
3486           if (syms[i]->value > opd->size - 8)
3487             continue;
3488
3489           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3490           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3491             {
3492               ++count;
3493               size += sizeof (asymbol);
3494               size += strlen (syms[i]->name) + 2;
3495             }
3496         }
3497
3498       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3499       if (dyn_count != 0
3500           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3501         {
3502           bfd_byte *dynbuf, *extdyn, *extdynend;
3503           size_t extdynsize;
3504           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3505
3506           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3507             goto free_contents_and_exit_err;
3508
3509           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3510           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3511
3512           extdyn = dynbuf;
3513           extdynend = extdyn + dynamic->size;
3514           for (; extdyn < extdynend; extdyn += extdynsize)
3515             {
3516               Elf_Internal_Dyn dyn;
3517               (*swap_dyn_in) (abfd, extdyn, &dyn);
3518
3519               if (dyn.d_tag == DT_NULL)
3520                 break;
3521
3522               if (dyn.d_tag == DT_PPC64_GLINK)
3523                 {
3524                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3525                      See comment in ppc64_elf_finish_dynamic_sections. */
3526                   glink_vma = dyn.d_un.d_val + 8 * 4;
3527                   /* The .glink section usually does not survive the final
3528                      link; search for the section (usually .text) where the
3529                      glink stubs now reside.  */
3530                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3531                                                 &glink_vma);
3532                   break;
3533                 }
3534             }
3535
3536           free (dynbuf);
3537         }
3538
3539       if (glink != NULL)
3540         {
3541           /* Determine __glink trampoline by reading the relative branch
3542              from the first glink stub.  */
3543           bfd_byte buf[4];
3544           unsigned int off = 0;
3545
3546           while (bfd_get_section_contents (abfd, glink, buf,
3547                                            glink_vma + off - glink->vma, 4))
3548             {
3549               unsigned int insn = bfd_get_32 (abfd, buf);
3550               insn ^= B_DOT;
3551               if ((insn & ~0x3fffffc) == 0)
3552                 {
3553                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3554                   break;
3555                 }
3556               off += 4;
3557               if (off > 4)
3558                 break;
3559             }
3560
3561           if (resolv_vma)
3562             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3563
3564           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3565           if (relplt != NULL)
3566             {
3567               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3568               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3569                 goto free_contents_and_exit_err;
3570
3571               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3572               size += plt_count * sizeof (asymbol);
3573
3574               p = relplt->relocation;
3575               for (i = 0; i < plt_count; i++, p++)
3576                 {
3577                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3578                   if (p->addend != 0)
3579                     size += sizeof ("+0x") - 1 + 16;
3580                 }
3581             }
3582         }
3583
3584       if (size == 0)
3585         goto free_contents_and_exit;
3586       s = *ret = bfd_malloc (size);
3587       if (s == NULL)
3588         goto free_contents_and_exit_err;
3589
3590       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3591
3592       for (i = secsymend; i < opdsymend; ++i)
3593         {
3594           bfd_vma ent;
3595
3596           if (syms[i]->value > opd->size - 8)
3597             continue;
3598
3599           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3600           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3601             {
3602               long lo, hi;
3603               size_t len;
3604               asection *sec = abfd->sections;
3605
3606               *s = *syms[i];
3607               lo = codesecsym;
3608               hi = codesecsymend;
3609               while (lo < hi)
3610                 {
3611                   long mid = (lo + hi) >> 1;
3612                   if (syms[mid]->section->vma < ent)
3613                     lo = mid + 1;
3614                   else if (syms[mid]->section->vma > ent)
3615                     hi = mid;
3616                   else
3617                     {
3618                       sec = syms[mid]->section;
3619                       break;
3620                     }
3621                 }
3622
3623               if (lo >= hi && lo > codesecsym)
3624                 sec = syms[lo - 1]->section;
3625
3626               for (; sec != NULL; sec = sec->next)
3627                 {
3628                   if (sec->vma > ent)
3629                     break;
3630                   /* SEC_LOAD may not be set if SEC is from a separate debug
3631                      info file.  */
3632                   if ((sec->flags & SEC_ALLOC) == 0)
3633                     break;
3634                   if ((sec->flags & SEC_CODE) != 0)
3635                     s->section = sec;
3636                 }
3637               s->flags |= BSF_SYNTHETIC;
3638               s->value = ent - s->section->vma;
3639               s->name = names;
3640               *names++ = '.';
3641               len = strlen (syms[i]->name);
3642               memcpy (names, syms[i]->name, len + 1);
3643               names += len + 1;
3644               /* Have udata.p point back to the original symbol this
3645                  synthetic symbol was derived from.  */
3646               s->udata.p = syms[i];
3647               s++;
3648             }
3649         }
3650       free (contents);
3651
3652       if (glink != NULL && relplt != NULL)
3653         {
3654           if (resolv_vma)
3655             {
3656               /* Add a symbol for the main glink trampoline.  */
3657               memset (s, 0, sizeof *s);
3658               s->the_bfd = abfd;
3659               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3660               s->section = glink;
3661               s->value = resolv_vma - glink->vma;
3662               s->name = names;
3663               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3664               names += sizeof ("__glink_PLTresolve");
3665               s++;
3666               count++;
3667             }
3668
3669           /* FIXME: It would be very much nicer to put sym@plt on the
3670              stub rather than on the glink branch table entry.  The
3671              objdump disassembler would then use a sensible symbol
3672              name on plt calls.  The difficulty in doing so is
3673              a) finding the stubs, and,
3674              b) matching stubs against plt entries, and,
3675              c) there can be multiple stubs for a given plt entry.
3676
3677              Solving (a) could be done by code scanning, but older
3678              ppc64 binaries used different stubs to current code.
3679              (b) is the tricky one since you need to known the toc
3680              pointer for at least one function that uses a pic stub to
3681              be able to calculate the plt address referenced.
3682              (c) means gdb would need to set multiple breakpoints (or
3683              find the glink branch itself) when setting breakpoints
3684              for pending shared library loads.  */
3685           p = relplt->relocation;
3686           for (i = 0; i < plt_count; i++, p++)
3687             {
3688               size_t len;
3689
3690               *s = **p->sym_ptr_ptr;
3691               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3692                  we are defining a symbol, ensure one of them is set.  */
3693               if ((s->flags & BSF_LOCAL) == 0)
3694                 s->flags |= BSF_GLOBAL;
3695               s->flags |= BSF_SYNTHETIC;
3696               s->section = glink;
3697               s->value = glink_vma - glink->vma;
3698               s->name = names;
3699               s->udata.p = NULL;
3700               len = strlen ((*p->sym_ptr_ptr)->name);
3701               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3702               names += len;
3703               if (p->addend != 0)
3704                 {
3705                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3706                   names += sizeof ("+0x") - 1;
3707                   bfd_sprintf_vma (abfd, names, p->addend);
3708                   names += strlen (names);
3709                 }
3710               memcpy (names, "@plt", sizeof ("@plt"));
3711               names += sizeof ("@plt");
3712               s++;
3713               if (abi < 2)
3714                 {
3715                   glink_vma += 8;
3716                   if (i >= 0x8000)
3717                     glink_vma += 4;
3718                 }
3719               else
3720                 glink_vma += 4;
3721             }
3722           count += plt_count;
3723         }
3724     }
3725
3726  done:
3727   free (syms);
3728   return count;
3729 }
3730 \f
3731 /* The following functions are specific to the ELF linker, while
3732    functions above are used generally.  Those named ppc64_elf_* are
3733    called by the main ELF linker code.  They appear in this file more
3734    or less in the order in which they are called.  eg.
3735    ppc64_elf_check_relocs is called early in the link process,
3736    ppc64_elf_finish_dynamic_sections is one of the last functions
3737    called.
3738
3739    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3740    functions have both a function code symbol and a function descriptor
3741    symbol.  A call to foo in a relocatable object file looks like:
3742
3743    .            .text
3744    .    x:
3745    .            bl      .foo
3746    .            nop
3747
3748    The function definition in another object file might be:
3749
3750    .            .section .opd
3751    .    foo:    .quad   .foo
3752    .            .quad   .TOC.@tocbase
3753    .            .quad   0
3754    .
3755    .            .text
3756    .    .foo:   blr
3757
3758    When the linker resolves the call during a static link, the branch
3759    unsurprisingly just goes to .foo and the .opd information is unused.
3760    If the function definition is in a shared library, things are a little
3761    different:  The call goes via a plt call stub, the opd information gets
3762    copied to the plt, and the linker patches the nop.
3763
3764    .    x:
3765    .            bl      .foo_stub
3766    .            ld      2,40(1)
3767    .
3768    .
3769    .    .foo_stub:
3770    .            std     2,40(1)                 # in practice, the call stub
3771    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3772    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3773    .            ld      12,0(11)
3774    .            ld      2,8(11)
3775    .            mtctr   12
3776    .            ld      11,16(11)
3777    .            bctr
3778    .
3779    .            .section .plt
3780    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3781
3782    The "reloc ()" notation is supposed to indicate that the linker emits
3783    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3784    copying.
3785
3786    What are the difficulties here?  Well, firstly, the relocations
3787    examined by the linker in check_relocs are against the function code
3788    sym .foo, while the dynamic relocation in the plt is emitted against
3789    the function descriptor symbol, foo.  Somewhere along the line, we need
3790    to carefully copy dynamic link information from one symbol to the other.
3791    Secondly, the generic part of the elf linker will make .foo a dynamic
3792    symbol as is normal for most other backends.  We need foo dynamic
3793    instead, at least for an application final link.  However, when
3794    creating a shared library containing foo, we need to have both symbols
3795    dynamic so that references to .foo are satisfied during the early
3796    stages of linking.  Otherwise the linker might decide to pull in a
3797    definition from some other object, eg. a static library.
3798
3799    Update: As of August 2004, we support a new convention.  Function
3800    calls may use the function descriptor symbol, ie. "bl foo".  This
3801    behaves exactly as "bl .foo".  */
3802
3803 /* Of those relocs that might be copied as dynamic relocs, this
3804    function selects those that must be copied when linking a shared
3805    library or PIE, even when the symbol is local.  */
3806
3807 static int
3808 must_be_dyn_reloc (struct bfd_link_info *info,
3809                    enum elf_ppc64_reloc_type r_type)
3810 {
3811   switch (r_type)
3812     {
3813     default:
3814       /* Only relative relocs can be resolved when the object load
3815          address isn't fixed.  DTPREL64 is excluded because the
3816          dynamic linker needs to differentiate global dynamic from
3817          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
3818       return 1;
3819
3820     case R_PPC64_REL32:
3821     case R_PPC64_REL64:
3822     case R_PPC64_REL30:
3823       return 0;
3824
3825     case R_PPC64_TPREL16:
3826     case R_PPC64_TPREL16_LO:
3827     case R_PPC64_TPREL16_HI:
3828     case R_PPC64_TPREL16_HA:
3829     case R_PPC64_TPREL16_DS:
3830     case R_PPC64_TPREL16_LO_DS:
3831     case R_PPC64_TPREL16_HIGH:
3832     case R_PPC64_TPREL16_HIGHA:
3833     case R_PPC64_TPREL16_HIGHER:
3834     case R_PPC64_TPREL16_HIGHERA:
3835     case R_PPC64_TPREL16_HIGHEST:
3836     case R_PPC64_TPREL16_HIGHESTA:
3837     case R_PPC64_TPREL64:
3838       /* These relocations are relative but in a shared library the
3839          linker doesn't know the thread pointer base.  */
3840       return bfd_link_dll (info);
3841     }
3842 }
3843
3844 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3845    copying dynamic variables from a shared lib into an app's dynbss
3846    section, and instead use a dynamic relocation to point into the
3847    shared lib.  With code that gcc generates, it's vital that this be
3848    enabled;  In the PowerPC64 ABI, the address of a function is actually
3849    the address of a function descriptor, which resides in the .opd
3850    section.  gcc uses the descriptor directly rather than going via the
3851    GOT as some other ABI's do, which means that initialized function
3852    pointers must reference the descriptor.  Thus, a function pointer
3853    initialized to the address of a function in a shared library will
3854    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3855    redefines the function descriptor symbol to point to the copy.  This
3856    presents a problem as a plt entry for that function is also
3857    initialized from the function descriptor symbol and the copy reloc
3858    may not be initialized first.  */
3859 #define ELIMINATE_COPY_RELOCS 1
3860
3861 /* Section name for stubs is the associated section name plus this
3862    string.  */
3863 #define STUB_SUFFIX ".stub"
3864
3865 /* Linker stubs.
3866    ppc_stub_long_branch:
3867    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3868    destination, but a 24 bit branch in a stub section will reach.
3869    .    b       dest
3870
3871    ppc_stub_plt_branch:
3872    Similar to the above, but a 24 bit branch in the stub section won't
3873    reach its destination.
3874    .    addis   %r11,%r2,xxx@toc@ha
3875    .    ld      %r12,xxx@toc@l(%r11)
3876    .    mtctr   %r12
3877    .    bctr
3878
3879    ppc_stub_plt_call:
3880    Used to call a function in a shared library.  If it so happens that
3881    the plt entry referenced crosses a 64k boundary, then an extra
3882    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3883    .    std     %r2,40(%r1)
3884    .    addis   %r11,%r2,xxx@toc@ha
3885    .    ld      %r12,xxx+0@toc@l(%r11)
3886    .    mtctr   %r12
3887    .    ld      %r2,xxx+8@toc@l(%r11)
3888    .    ld      %r11,xxx+16@toc@l(%r11)
3889    .    bctr
3890
3891    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3892    code to adjust the value and save r2 to support multiple toc sections.
3893    A ppc_stub_long_branch with an r2 offset looks like:
3894    .    std     %r2,40(%r1)
3895    .    addis   %r2,%r2,off@ha
3896    .    addi    %r2,%r2,off@l
3897    .    b       dest
3898
3899    A ppc_stub_plt_branch with an r2 offset looks like:
3900    .    std     %r2,40(%r1)
3901    .    addis   %r11,%r2,xxx@toc@ha
3902    .    ld      %r12,xxx@toc@l(%r11)
3903    .    addis   %r2,%r2,off@ha
3904    .    addi    %r2,%r2,off@l
3905    .    mtctr   %r12
3906    .    bctr
3907
3908    In cases where the "addis" instruction would add zero, the "addis" is
3909    omitted and following instructions modified slightly in some cases.
3910 */
3911
3912 enum ppc_stub_type {
3913   ppc_stub_none,
3914   ppc_stub_long_branch,
3915   ppc_stub_long_branch_r2off,
3916   ppc_stub_plt_branch,
3917   ppc_stub_plt_branch_r2off,
3918   ppc_stub_plt_call,
3919   ppc_stub_plt_call_r2save,
3920   ppc_stub_global_entry,
3921   ppc_stub_save_res
3922 };
3923
3924 /* Information on stub grouping.  */
3925 struct map_stub
3926 {
3927   /* The stub section.  */
3928   asection *stub_sec;
3929   /* This is the section to which stubs in the group will be attached.  */
3930   asection *link_sec;
3931   /* Next group.  */
3932   struct map_stub *next;
3933   /* Whether to emit a copy of register save/restore functions in this
3934      group.  */
3935   int needs_save_res;
3936   /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3937      or -1u if no such stub with bctrl exists.  */
3938   unsigned int tls_get_addr_opt_bctrl;
3939 };
3940
3941 struct ppc_stub_hash_entry {
3942
3943   /* Base hash table entry structure.  */
3944   struct bfd_hash_entry root;
3945
3946   enum ppc_stub_type stub_type;
3947
3948   /* Group information.  */
3949   struct map_stub *group;
3950
3951   /* Offset within stub_sec of the beginning of this stub.  */
3952   bfd_vma stub_offset;
3953
3954   /* Given the symbol's value and its section we can determine its final
3955      value when building the stubs (so the stub knows where to jump.  */
3956   bfd_vma target_value;
3957   asection *target_section;
3958
3959   /* The symbol table entry, if any, that this was derived from.  */
3960   struct ppc_link_hash_entry *h;
3961   struct plt_entry *plt_ent;
3962
3963   /* Symbol st_other.  */
3964   unsigned char other;
3965 };
3966
3967 struct ppc_branch_hash_entry {
3968
3969   /* Base hash table entry structure.  */
3970   struct bfd_hash_entry root;
3971
3972   /* Offset within branch lookup table.  */
3973   unsigned int offset;
3974
3975   /* Generation marker.  */
3976   unsigned int iter;
3977 };
3978
3979 /* Used to track dynamic relocations for local symbols.  */
3980 struct ppc_dyn_relocs
3981 {
3982   struct ppc_dyn_relocs *next;
3983
3984   /* The input section of the reloc.  */
3985   asection *sec;
3986
3987   /* Total number of relocs copied for the input section.  */
3988   unsigned int count : 31;
3989
3990   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3991   unsigned int ifunc : 1;
3992 };
3993
3994 struct ppc_link_hash_entry
3995 {
3996   struct elf_link_hash_entry elf;
3997
3998   union {
3999     /* A pointer to the most recently used stub hash entry against this
4000        symbol.  */
4001     struct ppc_stub_hash_entry *stub_cache;
4002
4003     /* A pointer to the next symbol starting with a '.'  */
4004     struct ppc_link_hash_entry *next_dot_sym;
4005   } u;
4006
4007   /* Track dynamic relocs copied for this symbol.  */
4008   struct elf_dyn_relocs *dyn_relocs;
4009
4010   /* Link between function code and descriptor symbols.  */
4011   struct ppc_link_hash_entry *oh;
4012
4013   /* Flag function code and descriptor symbols.  */
4014   unsigned int is_func:1;
4015   unsigned int is_func_descriptor:1;
4016   unsigned int fake:1;
4017
4018   /* Whether global opd/toc sym has been adjusted or not.
4019      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4020      should be set for all globals defined in any opd/toc section.  */
4021   unsigned int adjust_done:1;
4022
4023   /* Set if this is an out-of-line register save/restore function,
4024      with non-standard calling convention.  */
4025   unsigned int save_res:1;
4026
4027   /* Set if a duplicate symbol with non-zero localentry is detected,
4028      even when the duplicate symbol does not provide a definition.  */
4029   unsigned int non_zero_localentry:1;
4030
4031   /* Contexts in which symbol is used in the GOT (or TOC).
4032      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4033      corresponding relocs are encountered during check_relocs.
4034      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4035      indicate the corresponding GOT entry type is not needed.
4036      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4037      a TPREL one.  We use a separate flag rather than setting TPREL
4038      just for convenience in distinguishing the two cases.  */
4039 #define TLS_GD           1      /* GD reloc. */
4040 #define TLS_LD           2      /* LD reloc. */
4041 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
4042 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
4043 #define TLS_TLS         16      /* Any TLS reloc.  */
4044 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
4045 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
4046 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
4047   unsigned char tls_mask;
4048 };
4049
4050 /* ppc64 ELF linker hash table.  */
4051
4052 struct ppc_link_hash_table
4053 {
4054   struct elf_link_hash_table elf;
4055
4056   /* The stub hash table.  */
4057   struct bfd_hash_table stub_hash_table;
4058
4059   /* Another hash table for plt_branch stubs.  */
4060   struct bfd_hash_table branch_hash_table;
4061
4062   /* Hash table for function prologue tocsave.  */
4063   htab_t tocsave_htab;
4064
4065   /* Various options and other info passed from the linker.  */
4066   struct ppc64_elf_params *params;
4067
4068   /* The size of sec_info below.  */
4069   unsigned int sec_info_arr_size;
4070
4071   /* Per-section array of extra section info.  Done this way rather
4072      than as part of ppc64_elf_section_data so we have the info for
4073      non-ppc64 sections.  */
4074   struct
4075   {
4076     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4077     bfd_vma toc_off;
4078
4079     union
4080     {
4081       /* The section group that this section belongs to.  */
4082       struct map_stub *group;
4083       /* A temp section list pointer.  */
4084       asection *list;
4085     } u;
4086   } *sec_info;
4087
4088   /* Linked list of groups.  */
4089   struct map_stub *group;
4090
4091   /* Temp used when calculating TOC pointers.  */
4092   bfd_vma toc_curr;
4093   bfd *toc_bfd;
4094   asection *toc_first_sec;
4095
4096   /* Used when adding symbols.  */
4097   struct ppc_link_hash_entry *dot_syms;
4098
4099   /* Shortcuts to get to dynamic linker sections.  */
4100   asection *glink;
4101   asection *global_entry;
4102   asection *sfpr;
4103   asection *brlt;
4104   asection *relbrlt;
4105   asection *glink_eh_frame;
4106
4107   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4108   struct ppc_link_hash_entry *tls_get_addr;
4109   struct ppc_link_hash_entry *tls_get_addr_fd;
4110
4111   /* The size of reliplt used by got entry relocs.  */
4112   bfd_size_type got_reli_size;
4113
4114   /* Statistics.  */
4115   unsigned long stub_count[ppc_stub_global_entry];
4116
4117   /* Number of stubs against global syms.  */
4118   unsigned long stub_globals;
4119
4120   /* Set if we're linking code with function descriptors.  */
4121   unsigned int opd_abi:1;
4122
4123   /* Support for multiple toc sections.  */
4124   unsigned int do_multi_toc:1;
4125   unsigned int multi_toc_needed:1;
4126   unsigned int second_toc_pass:1;
4127   unsigned int do_toc_opt:1;
4128
4129   /* Set if tls optimization is enabled.  */
4130   unsigned int do_tls_opt:1;
4131
4132   /* Set on error.  */
4133   unsigned int stub_error:1;
4134
4135   /* Whether func_desc_adjust needs to be run over symbols.  */
4136   unsigned int need_func_desc_adj:1;
4137
4138   /* Whether there exist local gnu indirect function resolvers,
4139      referenced by dynamic relocations.  */
4140   unsigned int local_ifunc_resolver:1;
4141   unsigned int maybe_local_ifunc_resolver:1;
4142
4143   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
4144   unsigned int has_plt_localentry0:1;
4145
4146   /* Incremented every time we size stubs.  */
4147   unsigned int stub_iteration;
4148
4149   /* Small local sym cache.  */
4150   struct sym_cache sym_cache;
4151 };
4152
4153 /* Rename some of the generic section flags to better document how they
4154    are used here.  */
4155
4156 /* Nonzero if this section has TLS related relocations.  */
4157 #define has_tls_reloc sec_flg0
4158
4159 /* Nonzero if this section has a call to __tls_get_addr.  */
4160 #define has_tls_get_addr_call sec_flg1
4161
4162 /* Nonzero if this section has any toc or got relocs.  */
4163 #define has_toc_reloc sec_flg2
4164
4165 /* Nonzero if this section has a call to another section that uses
4166    the toc or got.  */
4167 #define makes_toc_func_call sec_flg3
4168
4169 /* Recursion protection when determining above flag.  */
4170 #define call_check_in_progress sec_flg4
4171 #define call_check_done sec_flg5
4172
4173 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4174
4175 #define ppc_hash_table(p) \
4176   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4177   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4178
4179 #define ppc_stub_hash_lookup(table, string, create, copy) \
4180   ((struct ppc_stub_hash_entry *) \
4181    bfd_hash_lookup ((table), (string), (create), (copy)))
4182
4183 #define ppc_branch_hash_lookup(table, string, create, copy) \
4184   ((struct ppc_branch_hash_entry *) \
4185    bfd_hash_lookup ((table), (string), (create), (copy)))
4186
4187 /* Create an entry in the stub hash table.  */
4188
4189 static struct bfd_hash_entry *
4190 stub_hash_newfunc (struct bfd_hash_entry *entry,
4191                    struct bfd_hash_table *table,
4192                    const char *string)
4193 {
4194   /* Allocate the structure if it has not already been allocated by a
4195      subclass.  */
4196   if (entry == NULL)
4197     {
4198       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4199       if (entry == NULL)
4200         return entry;
4201     }
4202
4203   /* Call the allocation method of the superclass.  */
4204   entry = bfd_hash_newfunc (entry, table, string);
4205   if (entry != NULL)
4206     {
4207       struct ppc_stub_hash_entry *eh;
4208
4209       /* Initialize the local fields.  */
4210       eh = (struct ppc_stub_hash_entry *) entry;
4211       eh->stub_type = ppc_stub_none;
4212       eh->group = NULL;
4213       eh->stub_offset = 0;
4214       eh->target_value = 0;
4215       eh->target_section = NULL;
4216       eh->h = NULL;
4217       eh->plt_ent = NULL;
4218       eh->other = 0;
4219     }
4220
4221   return entry;
4222 }
4223
4224 /* Create an entry in the branch hash table.  */
4225
4226 static struct bfd_hash_entry *
4227 branch_hash_newfunc (struct bfd_hash_entry *entry,
4228                      struct bfd_hash_table *table,
4229                      const char *string)
4230 {
4231   /* Allocate the structure if it has not already been allocated by a
4232      subclass.  */
4233   if (entry == NULL)
4234     {
4235       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4236       if (entry == NULL)
4237         return entry;
4238     }
4239
4240   /* Call the allocation method of the superclass.  */
4241   entry = bfd_hash_newfunc (entry, table, string);
4242   if (entry != NULL)
4243     {
4244       struct ppc_branch_hash_entry *eh;
4245
4246       /* Initialize the local fields.  */
4247       eh = (struct ppc_branch_hash_entry *) entry;
4248       eh->offset = 0;
4249       eh->iter = 0;
4250     }
4251
4252   return entry;
4253 }
4254
4255 /* Create an entry in a ppc64 ELF linker hash table.  */
4256
4257 static struct bfd_hash_entry *
4258 link_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_link_hash_entry));
4267       if (entry == NULL)
4268         return entry;
4269     }
4270
4271   /* Call the allocation method of the superclass.  */
4272   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4273   if (entry != NULL)
4274     {
4275       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4276
4277       memset (&eh->u.stub_cache, 0,
4278               (sizeof (struct ppc_link_hash_entry)
4279                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4280
4281       /* When making function calls, old ABI code references function entry
4282          points (dot symbols), while new ABI code references the function
4283          descriptor symbol.  We need to make any combination of reference and
4284          definition work together, without breaking archive linking.
4285
4286          For a defined function "foo" and an undefined call to "bar":
4287          An old object defines "foo" and ".foo", references ".bar" (possibly
4288          "bar" too).
4289          A new object defines "foo" and references "bar".
4290
4291          A new object thus has no problem with its undefined symbols being
4292          satisfied by definitions in an old object.  On the other hand, the
4293          old object won't have ".bar" satisfied by a new object.
4294
4295          Keep a list of newly added dot-symbols.  */
4296
4297       if (string[0] == '.')
4298         {
4299           struct ppc_link_hash_table *htab;
4300
4301           htab = (struct ppc_link_hash_table *) table;
4302           eh->u.next_dot_sym = htab->dot_syms;
4303           htab->dot_syms = eh;
4304         }
4305     }
4306
4307   return entry;
4308 }
4309
4310 struct tocsave_entry {
4311   asection *sec;
4312   bfd_vma offset;
4313 };
4314
4315 static hashval_t
4316 tocsave_htab_hash (const void *p)
4317 {
4318   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4319   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4320 }
4321
4322 static int
4323 tocsave_htab_eq (const void *p1, const void *p2)
4324 {
4325   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4326   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4327   return e1->sec == e2->sec && e1->offset == e2->offset;
4328 }
4329
4330 /* Destroy a ppc64 ELF linker hash table.  */
4331
4332 static void
4333 ppc64_elf_link_hash_table_free (bfd *obfd)
4334 {
4335   struct ppc_link_hash_table *htab;
4336
4337   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4338   if (htab->tocsave_htab)
4339     htab_delete (htab->tocsave_htab);
4340   bfd_hash_table_free (&htab->branch_hash_table);
4341   bfd_hash_table_free (&htab->stub_hash_table);
4342   _bfd_elf_link_hash_table_free (obfd);
4343 }
4344
4345 /* Create a ppc64 ELF linker hash table.  */
4346
4347 static struct bfd_link_hash_table *
4348 ppc64_elf_link_hash_table_create (bfd *abfd)
4349 {
4350   struct ppc_link_hash_table *htab;
4351   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4352
4353   htab = bfd_zmalloc (amt);
4354   if (htab == NULL)
4355     return NULL;
4356
4357   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4358                                       sizeof (struct ppc_link_hash_entry),
4359                                       PPC64_ELF_DATA))
4360     {
4361       free (htab);
4362       return NULL;
4363     }
4364
4365   /* Init the stub hash table too.  */
4366   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4367                             sizeof (struct ppc_stub_hash_entry)))
4368     {
4369       _bfd_elf_link_hash_table_free (abfd);
4370       return NULL;
4371     }
4372
4373   /* And the branch hash table.  */
4374   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4375                             sizeof (struct ppc_branch_hash_entry)))
4376     {
4377       bfd_hash_table_free (&htab->stub_hash_table);
4378       _bfd_elf_link_hash_table_free (abfd);
4379       return NULL;
4380     }
4381
4382   htab->tocsave_htab = htab_try_create (1024,
4383                                         tocsave_htab_hash,
4384                                         tocsave_htab_eq,
4385                                         NULL);
4386   if (htab->tocsave_htab == NULL)
4387     {
4388       ppc64_elf_link_hash_table_free (abfd);
4389       return NULL;
4390     }
4391   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4392
4393   /* Initializing two fields of the union is just cosmetic.  We really
4394      only care about glist, but when compiled on a 32-bit host the
4395      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4396      debugger inspection of these fields look nicer.  */
4397   htab->elf.init_got_refcount.refcount = 0;
4398   htab->elf.init_got_refcount.glist = NULL;
4399   htab->elf.init_plt_refcount.refcount = 0;
4400   htab->elf.init_plt_refcount.glist = NULL;
4401   htab->elf.init_got_offset.offset = 0;
4402   htab->elf.init_got_offset.glist = NULL;
4403   htab->elf.init_plt_offset.offset = 0;
4404   htab->elf.init_plt_offset.glist = NULL;
4405
4406   return &htab->elf.root;
4407 }
4408
4409 /* Create sections for linker generated code.  */
4410
4411 static bfd_boolean
4412 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4413 {
4414   struct ppc_link_hash_table *htab;
4415   flagword flags;
4416
4417   htab = ppc_hash_table (info);
4418
4419   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4420            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4421   if (htab->params->save_restore_funcs)
4422     {
4423       /* Create .sfpr for code to save and restore fp regs.  */
4424       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4425                                                        flags);
4426       if (htab->sfpr == NULL
4427           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4428         return FALSE;
4429     }
4430
4431   if (bfd_link_relocatable (info))
4432     return TRUE;
4433
4434   /* Create .glink for lazy dynamic linking support.  */
4435   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4436                                                     flags);
4437   if (htab->glink == NULL
4438       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4439     return FALSE;
4440
4441   /* The part of .glink used by global entry stubs, separate so that
4442      it can be aligned appropriately without affecting htab->glink.  */
4443   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4444                                                            flags);
4445   if (htab->global_entry == NULL
4446       || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4447     return FALSE;
4448
4449   if (!info->no_ld_generated_unwind_info)
4450     {
4451       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4452                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4453       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4454                                                                  ".eh_frame",
4455                                                                  flags);
4456       if (htab->glink_eh_frame == NULL
4457           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4458         return FALSE;
4459     }
4460
4461   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4462   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4463   if (htab->elf.iplt == NULL
4464       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4465     return FALSE;
4466
4467   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4468            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4469   htab->elf.irelplt
4470     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4471   if (htab->elf.irelplt == NULL
4472       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4473     return FALSE;
4474
4475   /* Create branch lookup table for plt_branch stubs.  */
4476   flags = (SEC_ALLOC | SEC_LOAD
4477            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4478   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4479                                                    flags);
4480   if (htab->brlt == NULL
4481       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4482     return FALSE;
4483
4484   if (!bfd_link_pic (info))
4485     return TRUE;
4486
4487   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4488            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4489   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4490                                                       ".rela.branch_lt",
4491                                                       flags);
4492   if (htab->relbrlt == NULL
4493       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4494     return FALSE;
4495
4496   return TRUE;
4497 }
4498
4499 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4500
4501 bfd_boolean
4502 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4503                          struct ppc64_elf_params *params)
4504 {
4505   struct ppc_link_hash_table *htab;
4506
4507   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4508
4509 /* Always hook our dynamic sections into the first bfd, which is the
4510    linker created stub bfd.  This ensures that the GOT header is at
4511    the start of the output TOC section.  */
4512   htab = ppc_hash_table (info);
4513   htab->elf.dynobj = params->stub_bfd;
4514   htab->params = params;
4515
4516   return create_linkage_sections (htab->elf.dynobj, info);
4517 }
4518
4519 /* Build a name for an entry in the stub hash table.  */
4520
4521 static char *
4522 ppc_stub_name (const asection *input_section,
4523                const asection *sym_sec,
4524                const struct ppc_link_hash_entry *h,
4525                const Elf_Internal_Rela *rel)
4526 {
4527   char *stub_name;
4528   ssize_t len;
4529
4530   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4531      offsets from a sym as a branch target?  In fact, we could
4532      probably assume the addend is always zero.  */
4533   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4534
4535   if (h)
4536     {
4537       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4538       stub_name = bfd_malloc (len);
4539       if (stub_name == NULL)
4540         return stub_name;
4541
4542       len = sprintf (stub_name, "%08x.%s+%x",
4543                      input_section->id & 0xffffffff,
4544                      h->elf.root.root.string,
4545                      (int) rel->r_addend & 0xffffffff);
4546     }
4547   else
4548     {
4549       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4550       stub_name = bfd_malloc (len);
4551       if (stub_name == NULL)
4552         return stub_name;
4553
4554       len = sprintf (stub_name, "%08x.%x:%x+%x",
4555                      input_section->id & 0xffffffff,
4556                      sym_sec->id & 0xffffffff,
4557                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4558                      (int) rel->r_addend & 0xffffffff);
4559     }
4560   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4561     stub_name[len - 2] = 0;
4562   return stub_name;
4563 }
4564
4565 /* Look up an entry in the stub hash.  Stub entries are cached because
4566    creating the stub name takes a bit of time.  */
4567
4568 static struct ppc_stub_hash_entry *
4569 ppc_get_stub_entry (const asection *input_section,
4570                     const asection *sym_sec,
4571                     struct ppc_link_hash_entry *h,
4572                     const Elf_Internal_Rela *rel,
4573                     struct ppc_link_hash_table *htab)
4574 {
4575   struct ppc_stub_hash_entry *stub_entry;
4576   struct map_stub *group;
4577
4578   /* If this input section is part of a group of sections sharing one
4579      stub section, then use the id of the first section in the group.
4580      Stub names need to include a section id, as there may well be
4581      more than one stub used to reach say, printf, and we need to
4582      distinguish between them.  */
4583   group = htab->sec_info[input_section->id].u.group;
4584   if (group == NULL)
4585     return NULL;
4586
4587   if (h != NULL && h->u.stub_cache != NULL
4588       && h->u.stub_cache->h == h
4589       && h->u.stub_cache->group == group)
4590     {
4591       stub_entry = h->u.stub_cache;
4592     }
4593   else
4594     {
4595       char *stub_name;
4596
4597       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4598       if (stub_name == NULL)
4599         return NULL;
4600
4601       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4602                                          stub_name, FALSE, FALSE);
4603       if (h != NULL)
4604         h->u.stub_cache = stub_entry;
4605
4606       free (stub_name);
4607     }
4608
4609   return stub_entry;
4610 }
4611
4612 /* Add a new stub entry to the stub hash.  Not all fields of the new
4613    stub entry are initialised.  */
4614
4615 static struct ppc_stub_hash_entry *
4616 ppc_add_stub (const char *stub_name,
4617               asection *section,
4618               struct bfd_link_info *info)
4619 {
4620   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4621   struct map_stub *group;
4622   asection *link_sec;
4623   asection *stub_sec;
4624   struct ppc_stub_hash_entry *stub_entry;
4625
4626   group = htab->sec_info[section->id].u.group;
4627   link_sec = group->link_sec;
4628   stub_sec = group->stub_sec;
4629   if (stub_sec == NULL)
4630     {
4631       size_t namelen;
4632       bfd_size_type len;
4633       char *s_name;
4634
4635       namelen = strlen (link_sec->name);
4636       len = namelen + sizeof (STUB_SUFFIX);
4637       s_name = bfd_alloc (htab->params->stub_bfd, len);
4638       if (s_name == NULL)
4639         return NULL;
4640
4641       memcpy (s_name, link_sec->name, namelen);
4642       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4643       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4644       if (stub_sec == NULL)
4645         return NULL;
4646       group->stub_sec = stub_sec;
4647     }
4648
4649   /* Enter this entry into the linker stub hash table.  */
4650   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4651                                      TRUE, FALSE);
4652   if (stub_entry == NULL)
4653     {
4654       /* xgettext:c-format */
4655       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4656                               section->owner, stub_name);
4657       return NULL;
4658     }
4659
4660   stub_entry->group = group;
4661   stub_entry->stub_offset = 0;
4662   return stub_entry;
4663 }
4664
4665 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4666    not already done.  */
4667
4668 static bfd_boolean
4669 create_got_section (bfd *abfd, struct bfd_link_info *info)
4670 {
4671   asection *got, *relgot;
4672   flagword flags;
4673   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4674
4675   if (!is_ppc64_elf (abfd))
4676     return FALSE;
4677   if (htab == NULL)
4678     return FALSE;
4679
4680   if (!htab->elf.sgot
4681       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4682     return FALSE;
4683
4684   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4685            | SEC_LINKER_CREATED);
4686
4687   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4688   if (!got
4689       || !bfd_set_section_alignment (abfd, got, 3))
4690     return FALSE;
4691
4692   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4693                                                flags | SEC_READONLY);
4694   if (!relgot
4695       || ! bfd_set_section_alignment (abfd, relgot, 3))
4696     return FALSE;
4697
4698   ppc64_elf_tdata (abfd)->got = got;
4699   ppc64_elf_tdata (abfd)->relgot = relgot;
4700   return TRUE;
4701 }
4702
4703 /* Follow indirect and warning symbol links.  */
4704
4705 static inline struct bfd_link_hash_entry *
4706 follow_link (struct bfd_link_hash_entry *h)
4707 {
4708   while (h->type == bfd_link_hash_indirect
4709          || h->type == bfd_link_hash_warning)
4710     h = h->u.i.link;
4711   return h;
4712 }
4713
4714 static inline struct elf_link_hash_entry *
4715 elf_follow_link (struct elf_link_hash_entry *h)
4716 {
4717   return (struct elf_link_hash_entry *) follow_link (&h->root);
4718 }
4719
4720 static inline struct ppc_link_hash_entry *
4721 ppc_follow_link (struct ppc_link_hash_entry *h)
4722 {
4723   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4724 }
4725
4726 /* Merge PLT info on FROM with that on TO.  */
4727
4728 static void
4729 move_plt_plist (struct ppc_link_hash_entry *from,
4730                 struct ppc_link_hash_entry *to)
4731 {
4732   if (from->elf.plt.plist != NULL)
4733     {
4734       if (to->elf.plt.plist != NULL)
4735         {
4736           struct plt_entry **entp;
4737           struct plt_entry *ent;
4738
4739           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4740             {
4741               struct plt_entry *dent;
4742
4743               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4744                 if (dent->addend == ent->addend)
4745                   {
4746                     dent->plt.refcount += ent->plt.refcount;
4747                     *entp = ent->next;
4748                     break;
4749                   }
4750               if (dent == NULL)
4751                 entp = &ent->next;
4752             }
4753           *entp = to->elf.plt.plist;
4754         }
4755
4756       to->elf.plt.plist = from->elf.plt.plist;
4757       from->elf.plt.plist = NULL;
4758     }
4759 }
4760
4761 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4762
4763 static void
4764 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4765                                 struct elf_link_hash_entry *dir,
4766                                 struct elf_link_hash_entry *ind)
4767 {
4768   struct ppc_link_hash_entry *edir, *eind;
4769
4770   edir = (struct ppc_link_hash_entry *) dir;
4771   eind = (struct ppc_link_hash_entry *) ind;
4772
4773   edir->is_func |= eind->is_func;
4774   edir->is_func_descriptor |= eind->is_func_descriptor;
4775   edir->tls_mask |= eind->tls_mask;
4776   if (eind->oh != NULL)
4777     edir->oh = ppc_follow_link (eind->oh);
4778
4779   if (edir->elf.versioned != versioned_hidden)
4780     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4781   edir->elf.ref_regular |= eind->elf.ref_regular;
4782   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4783   edir->elf.non_got_ref |= eind->elf.non_got_ref;
4784   edir->elf.needs_plt |= eind->elf.needs_plt;
4785   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4786
4787   /* If we were called to copy over info for a weak sym, don't copy
4788      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4789      in order to simplify readonly_dynrelocs and save a field in the
4790      symbol hash entry, but that means dyn_relocs can't be used in any
4791      tests about a specific symbol, or affect other symbol flags which
4792      are then tested.  */
4793   if (eind->elf.root.type != bfd_link_hash_indirect)
4794     return;
4795
4796   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4797   if (eind->dyn_relocs != NULL)
4798     {
4799       if (edir->dyn_relocs != NULL)
4800         {
4801           struct elf_dyn_relocs **pp;
4802           struct elf_dyn_relocs *p;
4803
4804           /* Add reloc counts against the indirect sym to the direct sym
4805              list.  Merge any entries against the same section.  */
4806           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4807             {
4808               struct elf_dyn_relocs *q;
4809
4810               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4811                 if (q->sec == p->sec)
4812                   {
4813                     q->pc_count += p->pc_count;
4814                     q->count += p->count;
4815                     *pp = p->next;
4816                     break;
4817                   }
4818               if (q == NULL)
4819                 pp = &p->next;
4820             }
4821           *pp = edir->dyn_relocs;
4822         }
4823
4824       edir->dyn_relocs = eind->dyn_relocs;
4825       eind->dyn_relocs = NULL;
4826     }
4827
4828   /* Copy over got entries that we may have already seen to the
4829      symbol which just became indirect.  */
4830   if (eind->elf.got.glist != NULL)
4831     {
4832       if (edir->elf.got.glist != NULL)
4833         {
4834           struct got_entry **entp;
4835           struct got_entry *ent;
4836
4837           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4838             {
4839               struct got_entry *dent;
4840
4841               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4842                 if (dent->addend == ent->addend
4843                     && dent->owner == ent->owner
4844                     && dent->tls_type == ent->tls_type)
4845                   {
4846                     dent->got.refcount += ent->got.refcount;
4847                     *entp = ent->next;
4848                     break;
4849                   }
4850               if (dent == NULL)
4851                 entp = &ent->next;
4852             }
4853           *entp = edir->elf.got.glist;
4854         }
4855
4856       edir->elf.got.glist = eind->elf.got.glist;
4857       eind->elf.got.glist = NULL;
4858     }
4859
4860   /* And plt entries.  */
4861   move_plt_plist (eind, edir);
4862
4863   if (eind->elf.dynindx != -1)
4864     {
4865       if (edir->elf.dynindx != -1)
4866         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4867                                 edir->elf.dynstr_index);
4868       edir->elf.dynindx = eind->elf.dynindx;
4869       edir->elf.dynstr_index = eind->elf.dynstr_index;
4870       eind->elf.dynindx = -1;
4871       eind->elf.dynstr_index = 0;
4872     }
4873 }
4874
4875 /* Find the function descriptor hash entry from the given function code
4876    hash entry FH.  Link the entries via their OH fields.  */
4877
4878 static struct ppc_link_hash_entry *
4879 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4880 {
4881   struct ppc_link_hash_entry *fdh = fh->oh;
4882
4883   if (fdh == NULL)
4884     {
4885       const char *fd_name = fh->elf.root.root.string + 1;
4886
4887       fdh = (struct ppc_link_hash_entry *)
4888         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4889       if (fdh == NULL)
4890         return fdh;
4891
4892       fdh->is_func_descriptor = 1;
4893       fdh->oh = fh;
4894       fh->is_func = 1;
4895       fh->oh = fdh;
4896     }
4897
4898   fdh = ppc_follow_link (fdh);
4899   fdh->is_func_descriptor = 1;
4900   fdh->oh = fh;
4901   return fdh;
4902 }
4903
4904 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4905
4906 static struct ppc_link_hash_entry *
4907 make_fdh (struct bfd_link_info *info,
4908           struct ppc_link_hash_entry *fh)
4909 {
4910   bfd *abfd = fh->elf.root.u.undef.abfd;
4911   struct bfd_link_hash_entry *bh = NULL;
4912   struct ppc_link_hash_entry *fdh;
4913   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4914                     ? BSF_WEAK
4915                     : BSF_GLOBAL);
4916
4917   if (!_bfd_generic_link_add_one_symbol (info, abfd,
4918                                          fh->elf.root.root.string + 1,
4919                                          flags, bfd_und_section_ptr, 0,
4920                                          NULL, FALSE, FALSE, &bh))
4921     return NULL;
4922
4923   fdh = (struct ppc_link_hash_entry *) bh;
4924   fdh->elf.non_elf = 0;
4925   fdh->fake = 1;
4926   fdh->is_func_descriptor = 1;
4927   fdh->oh = fh;
4928   fh->is_func = 1;
4929   fh->oh = fdh;
4930   return fdh;
4931 }
4932
4933 /* Fix function descriptor symbols defined in .opd sections to be
4934    function type.  */
4935
4936 static bfd_boolean
4937 ppc64_elf_add_symbol_hook (bfd *ibfd,
4938                            struct bfd_link_info *info,
4939                            Elf_Internal_Sym *isym,
4940                            const char **name,
4941                            flagword *flags ATTRIBUTE_UNUSED,
4942                            asection **sec,
4943                            bfd_vma *value)
4944 {
4945   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4946       && (ibfd->flags & DYNAMIC) == 0
4947       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4948     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4949
4950   if (*sec != NULL
4951       && strcmp ((*sec)->name, ".opd") == 0)
4952     {
4953       asection *code_sec;
4954
4955       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4956             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4957         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4958
4959       /* If the symbol is a function defined in .opd, and the function
4960          code is in a discarded group, let it appear to be undefined.  */
4961       if (!bfd_link_relocatable (info)
4962           && (*sec)->reloc_count != 0
4963           && opd_entry_value (*sec, *value, &code_sec, NULL,
4964                               FALSE) != (bfd_vma) -1
4965           && discarded_section (code_sec))
4966         {
4967           *sec = bfd_und_section_ptr;
4968           isym->st_shndx = SHN_UNDEF;
4969         }
4970     }
4971   else if (*sec != NULL
4972            && strcmp ((*sec)->name, ".toc") == 0
4973            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4974     {
4975       struct ppc_link_hash_table *htab = ppc_hash_table (info);
4976       if (htab != NULL)
4977         htab->params->object_in_toc = 1;
4978     }
4979
4980   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4981     {
4982       if (abiversion (ibfd) == 0)
4983         set_abiversion (ibfd, 2);
4984       else if (abiversion (ibfd) == 1)
4985         {
4986           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4987                                     " for ABI version 1\n"), name);
4988           bfd_set_error (bfd_error_bad_value);
4989           return FALSE;
4990         }
4991     }
4992
4993   return TRUE;
4994 }
4995
4996 /* Merge non-visibility st_other attributes: local entry point.  */
4997
4998 static void
4999 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5000                                   const Elf_Internal_Sym *isym,
5001                                   bfd_boolean definition,
5002                                   bfd_boolean dynamic)
5003 {
5004   if (definition && (!dynamic || !h->def_regular))
5005     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5006                 | ELF_ST_VISIBILITY (h->other));
5007 }
5008
5009 /* Hook called on merging a symbol.  We use this to clear "fake" since
5010    we now have a real symbol.  */
5011
5012 static bfd_boolean
5013 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5014                         const Elf_Internal_Sym *isym,
5015                         asection **psec ATTRIBUTE_UNUSED,
5016                         bfd_boolean newdef ATTRIBUTE_UNUSED,
5017                         bfd_boolean olddef ATTRIBUTE_UNUSED,
5018                         bfd *oldbfd ATTRIBUTE_UNUSED,
5019                         const asection *oldsec ATTRIBUTE_UNUSED)
5020 {
5021   ((struct ppc_link_hash_entry *) h)->fake = 0;
5022   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5023     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5024   return TRUE;
5025 }
5026
5027 /* This function makes an old ABI object reference to ".bar" cause the
5028    inclusion of a new ABI object archive that defines "bar".
5029    NAME is a symbol defined in an archive.  Return a symbol in the hash
5030    table that might be satisfied by the archive symbols.  */
5031
5032 static struct elf_link_hash_entry *
5033 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5034                                  struct bfd_link_info *info,
5035                                  const char *name)
5036 {
5037   struct elf_link_hash_entry *h;
5038   char *dot_name;
5039   size_t len;
5040
5041   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5042   if (h != NULL
5043       /* Don't return this sym if it is a fake function descriptor
5044          created by add_symbol_adjust.  */
5045       && !((struct ppc_link_hash_entry *) h)->fake)
5046     return h;
5047
5048   if (name[0] == '.')
5049     return h;
5050
5051   len = strlen (name);
5052   dot_name = bfd_alloc (abfd, len + 2);
5053   if (dot_name == NULL)
5054     return (struct elf_link_hash_entry *) -1;
5055   dot_name[0] = '.';
5056   memcpy (dot_name + 1, name, len + 1);
5057   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5058   bfd_release (abfd, dot_name);
5059   return h;
5060 }
5061
5062 /* This function satisfies all old ABI object references to ".bar" if a
5063    new ABI object defines "bar".  Well, at least, undefined dot symbols
5064    are made weak.  This stops later archive searches from including an
5065    object if we already have a function descriptor definition.  It also
5066    prevents the linker complaining about undefined symbols.
5067    We also check and correct mismatched symbol visibility here.  The
5068    most restrictive visibility of the function descriptor and the
5069    function entry symbol is used.  */
5070
5071 static bfd_boolean
5072 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5073 {
5074   struct ppc_link_hash_table *htab;
5075   struct ppc_link_hash_entry *fdh;
5076
5077   if (eh->elf.root.type == bfd_link_hash_warning)
5078     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5079
5080   if (eh->elf.root.type == bfd_link_hash_indirect)
5081     return TRUE;
5082
5083   if (eh->elf.root.root.string[0] != '.')
5084     abort ();
5085
5086   htab = ppc_hash_table (info);
5087   if (htab == NULL)
5088     return FALSE;
5089
5090   fdh = lookup_fdh (eh, htab);
5091   if (fdh == NULL
5092       && !bfd_link_relocatable (info)
5093       && (eh->elf.root.type == bfd_link_hash_undefined
5094           || eh->elf.root.type == bfd_link_hash_undefweak)
5095       && eh->elf.ref_regular)
5096     {
5097       /* Make an undefined function descriptor sym, in order to
5098          pull in an --as-needed shared lib.  Archives are handled
5099          elsewhere.  */
5100       fdh = make_fdh (info, eh);
5101       if (fdh == NULL)
5102         return FALSE;
5103     }
5104
5105   if (fdh != NULL)
5106     {
5107       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5108       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5109
5110       /* Make both descriptor and entry symbol have the most
5111          constraining visibility of either symbol.  */
5112       if (entry_vis < descr_vis)
5113         fdh->elf.other += entry_vis - descr_vis;
5114       else if (entry_vis > descr_vis)
5115         eh->elf.other += descr_vis - entry_vis;
5116
5117       /* Propagate reference flags from entry symbol to function
5118          descriptor symbol.  */
5119       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5120       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5121       fdh->elf.ref_regular |= eh->elf.ref_regular;
5122       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5123
5124       if (!fdh->elf.forced_local
5125           && fdh->elf.dynindx == -1
5126           && fdh->elf.versioned != versioned_hidden
5127           && (bfd_link_dll (info)
5128               || fdh->elf.def_dynamic
5129               || fdh->elf.ref_dynamic)
5130           && (eh->elf.ref_regular
5131               || eh->elf.def_regular))
5132         {
5133           if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5134             return FALSE;
5135         }
5136     }
5137
5138   return TRUE;
5139 }
5140
5141 /* Set up opd section info and abiversion for IBFD, and process list
5142    of dot-symbols we made in link_hash_newfunc.  */
5143
5144 static bfd_boolean
5145 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5146 {
5147   struct ppc_link_hash_table *htab;
5148   struct ppc_link_hash_entry **p, *eh;
5149   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5150
5151   if (opd != NULL && opd->size != 0)
5152     {
5153       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5154       ppc64_elf_section_data (opd)->sec_type = sec_opd;
5155
5156       if (abiversion (ibfd) == 0)
5157         set_abiversion (ibfd, 1);
5158       else if (abiversion (ibfd) >= 2)
5159         {
5160           /* xgettext:c-format */
5161           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5162                                     " version %d\n"),
5163                                   ibfd, abiversion (ibfd));
5164           bfd_set_error (bfd_error_bad_value);
5165           return FALSE;
5166         }
5167     }
5168
5169   if (is_ppc64_elf (info->output_bfd))
5170     {
5171       /* For input files without an explicit abiversion in e_flags
5172          we should have flagged any with symbol st_other bits set
5173          as ELFv1 and above flagged those with .opd as ELFv2.
5174          Set the output abiversion if not yet set, and for any input
5175          still ambiguous, take its abiversion from the output.
5176          Differences in ABI are reported later.  */
5177       if (abiversion (info->output_bfd) == 0)
5178         set_abiversion (info->output_bfd, abiversion (ibfd));
5179       else if (abiversion (ibfd) == 0)
5180         set_abiversion (ibfd, abiversion (info->output_bfd));
5181     }
5182
5183   htab = ppc_hash_table (info);
5184   if (htab == NULL)
5185     return TRUE;
5186
5187   if (opd != NULL && opd->size != 0
5188       && (ibfd->flags & DYNAMIC) == 0
5189       && (opd->flags & SEC_RELOC) != 0
5190       && opd->reloc_count != 0
5191       && !bfd_is_abs_section (opd->output_section)
5192       && info->gc_sections)
5193     {
5194       /* Garbage collection needs some extra help with .opd sections.
5195          We don't want to necessarily keep everything referenced by
5196          relocs in .opd, as that would keep all functions.  Instead,
5197          if we reference an .opd symbol (a function descriptor), we
5198          want to keep the function code symbol's section.  This is
5199          easy for global symbols, but for local syms we need to keep
5200          information about the associated function section.  */
5201       bfd_size_type amt;
5202       asection **opd_sym_map;
5203       Elf_Internal_Shdr *symtab_hdr;
5204       Elf_Internal_Rela *relocs, *rel_end, *rel;
5205
5206       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5207       opd_sym_map = bfd_zalloc (ibfd, amt);
5208       if (opd_sym_map == NULL)
5209         return FALSE;
5210       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5211       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5212                                           info->keep_memory);
5213       if (relocs == NULL)
5214         return FALSE;
5215       symtab_hdr = &elf_symtab_hdr (ibfd);
5216       rel_end = relocs + opd->reloc_count - 1;
5217       for (rel = relocs; rel < rel_end; rel++)
5218         {
5219           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5220           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5221
5222           if (r_type == R_PPC64_ADDR64
5223               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5224               && r_symndx < symtab_hdr->sh_info)
5225             {
5226               Elf_Internal_Sym *isym;
5227               asection *s;
5228
5229               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5230               if (isym == NULL)
5231                 {
5232                   if (elf_section_data (opd)->relocs != relocs)
5233                     free (relocs);
5234                   return FALSE;
5235                 }
5236
5237               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5238               if (s != NULL && s != opd)
5239                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5240             }
5241         }
5242       if (elf_section_data (opd)->relocs != relocs)
5243         free (relocs);
5244     }
5245
5246   p = &htab->dot_syms;
5247   while ((eh = *p) != NULL)
5248     {
5249       *p = NULL;
5250       if (&eh->elf == htab->elf.hgot)
5251         ;
5252       else if (htab->elf.hgot == NULL
5253                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5254         htab->elf.hgot = &eh->elf;
5255       else if (abiversion (ibfd) <= 1)
5256         {
5257           htab->need_func_desc_adj = 1;
5258           if (!add_symbol_adjust (eh, info))
5259             return FALSE;
5260         }
5261       p = &eh->u.next_dot_sym;
5262     }
5263   return TRUE;
5264 }
5265
5266 /* Undo hash table changes when an --as-needed input file is determined
5267    not to be needed.  */
5268
5269 static bfd_boolean
5270 ppc64_elf_notice_as_needed (bfd *ibfd,
5271                             struct bfd_link_info *info,
5272                             enum notice_asneeded_action act)
5273 {
5274   if (act == notice_not_needed)
5275     {
5276       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5277
5278       if (htab == NULL)
5279         return FALSE;
5280
5281       htab->dot_syms = NULL;
5282     }
5283   return _bfd_elf_notice_as_needed (ibfd, info, act);
5284 }
5285
5286 /* If --just-symbols against a final linked binary, then assume we need
5287    toc adjusting stubs when calling functions defined there.  */
5288
5289 static void
5290 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5291 {
5292   if ((sec->flags & SEC_CODE) != 0
5293       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5294       && is_ppc64_elf (sec->owner))
5295     {
5296       if (abiversion (sec->owner) >= 2
5297           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5298         sec->has_toc_reloc = 1;
5299     }
5300   _bfd_elf_link_just_syms (sec, info);
5301 }
5302
5303 static struct plt_entry **
5304 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5305                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5306 {
5307   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5308   struct plt_entry **local_plt;
5309   unsigned char *local_got_tls_masks;
5310
5311   if (local_got_ents == NULL)
5312     {
5313       bfd_size_type size = symtab_hdr->sh_info;
5314
5315       size *= (sizeof (*local_got_ents)
5316                + sizeof (*local_plt)
5317                + sizeof (*local_got_tls_masks));
5318       local_got_ents = bfd_zalloc (abfd, size);
5319       if (local_got_ents == NULL)
5320         return NULL;
5321       elf_local_got_ents (abfd) = local_got_ents;
5322     }
5323
5324   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5325     {
5326       struct got_entry *ent;
5327
5328       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5329         if (ent->addend == r_addend
5330             && ent->owner == abfd
5331             && ent->tls_type == tls_type)
5332           break;
5333       if (ent == NULL)
5334         {
5335           bfd_size_type amt = sizeof (*ent);
5336           ent = bfd_alloc (abfd, amt);
5337           if (ent == NULL)
5338             return FALSE;
5339           ent->next = local_got_ents[r_symndx];
5340           ent->addend = r_addend;
5341           ent->owner = abfd;
5342           ent->tls_type = tls_type;
5343           ent->is_indirect = FALSE;
5344           ent->got.refcount = 0;
5345           local_got_ents[r_symndx] = ent;
5346         }
5347       ent->got.refcount += 1;
5348     }
5349
5350   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5351   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5352   local_got_tls_masks[r_symndx] |= tls_type;
5353
5354   return local_plt + r_symndx;
5355 }
5356
5357 static bfd_boolean
5358 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5359 {
5360   struct plt_entry *ent;
5361
5362   for (ent = *plist; ent != NULL; ent = ent->next)
5363     if (ent->addend == addend)
5364       break;
5365   if (ent == NULL)
5366     {
5367       bfd_size_type amt = sizeof (*ent);
5368       ent = bfd_alloc (abfd, amt);
5369       if (ent == NULL)
5370         return FALSE;
5371       ent->next = *plist;
5372       ent->addend = addend;
5373       ent->plt.refcount = 0;
5374       *plist = ent;
5375     }
5376   ent->plt.refcount += 1;
5377   return TRUE;
5378 }
5379
5380 static bfd_boolean
5381 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5382 {
5383   return (r_type == R_PPC64_REL24
5384           || r_type == R_PPC64_REL14
5385           || r_type == R_PPC64_REL14_BRTAKEN
5386           || r_type == R_PPC64_REL14_BRNTAKEN
5387           || r_type == R_PPC64_ADDR24
5388           || r_type == R_PPC64_ADDR14
5389           || r_type == R_PPC64_ADDR14_BRTAKEN
5390           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5391 }
5392
5393 /* Look through the relocs for a section during the first phase, and
5394    calculate needed space in the global offset table, procedure
5395    linkage table, and dynamic reloc sections.  */
5396
5397 static bfd_boolean
5398 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5399                         asection *sec, const Elf_Internal_Rela *relocs)
5400 {
5401   struct ppc_link_hash_table *htab;
5402   Elf_Internal_Shdr *symtab_hdr;
5403   struct elf_link_hash_entry **sym_hashes;
5404   const Elf_Internal_Rela *rel;
5405   const Elf_Internal_Rela *rel_end;
5406   asection *sreloc;
5407   struct elf_link_hash_entry *tga, *dottga;
5408   bfd_boolean is_opd;
5409
5410   if (bfd_link_relocatable (info))
5411     return TRUE;
5412
5413   /* Don't do anything special with non-loaded, non-alloced sections.
5414      In particular, any relocs in such sections should not affect GOT
5415      and PLT reference counting (ie. we don't allow them to create GOT
5416      or PLT entries), there's no possibility or desire to optimize TLS
5417      relocs, and there's not much point in propagating relocs to shared
5418      libs that the dynamic linker won't relocate.  */
5419   if ((sec->flags & SEC_ALLOC) == 0)
5420     return TRUE;
5421
5422   BFD_ASSERT (is_ppc64_elf (abfd));
5423
5424   htab = ppc_hash_table (info);
5425   if (htab == NULL)
5426     return FALSE;
5427
5428   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5429                               FALSE, FALSE, TRUE);
5430   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5431                                  FALSE, FALSE, TRUE);
5432   symtab_hdr = &elf_symtab_hdr (abfd);
5433   sym_hashes = elf_sym_hashes (abfd);
5434   sreloc = NULL;
5435   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5436   rel_end = relocs + sec->reloc_count;
5437   for (rel = relocs; rel < rel_end; rel++)
5438     {
5439       unsigned long r_symndx;
5440       struct elf_link_hash_entry *h;
5441       enum elf_ppc64_reloc_type r_type;
5442       int tls_type;
5443       struct _ppc64_elf_section_data *ppc64_sec;
5444       struct plt_entry **ifunc, **plt_list;
5445
5446       r_symndx = ELF64_R_SYM (rel->r_info);
5447       if (r_symndx < symtab_hdr->sh_info)
5448         h = NULL;
5449       else
5450         {
5451           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5452           h = elf_follow_link (h);
5453
5454           if (h == htab->elf.hgot)
5455             sec->has_toc_reloc = 1;
5456         }
5457
5458       tls_type = 0;
5459       ifunc = NULL;
5460       if (h != NULL)
5461         {
5462           if (h->type == STT_GNU_IFUNC)
5463             {
5464               h->needs_plt = 1;
5465               ifunc = &h->plt.plist;
5466             }
5467         }
5468       else
5469         {
5470           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5471                                                           abfd, r_symndx);
5472           if (isym == NULL)
5473             return FALSE;
5474
5475           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5476             {
5477               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5478                                              rel->r_addend, PLT_IFUNC);
5479               if (ifunc == NULL)
5480                 return FALSE;
5481             }
5482         }
5483
5484       r_type = ELF64_R_TYPE (rel->r_info);
5485       switch (r_type)
5486         {
5487         case R_PPC64_TLSGD:
5488         case R_PPC64_TLSLD:
5489           /* These special tls relocs tie a call to __tls_get_addr with
5490              its parameter symbol.  */
5491           break;
5492
5493         case R_PPC64_GOT_TLSLD16:
5494         case R_PPC64_GOT_TLSLD16_LO:
5495         case R_PPC64_GOT_TLSLD16_HI:
5496         case R_PPC64_GOT_TLSLD16_HA:
5497           tls_type = TLS_TLS | TLS_LD;
5498           goto dogottls;
5499
5500         case R_PPC64_GOT_TLSGD16:
5501         case R_PPC64_GOT_TLSGD16_LO:
5502         case R_PPC64_GOT_TLSGD16_HI:
5503         case R_PPC64_GOT_TLSGD16_HA:
5504           tls_type = TLS_TLS | TLS_GD;
5505           goto dogottls;
5506
5507         case R_PPC64_GOT_TPREL16_DS:
5508         case R_PPC64_GOT_TPREL16_LO_DS:
5509         case R_PPC64_GOT_TPREL16_HI:
5510         case R_PPC64_GOT_TPREL16_HA:
5511           if (bfd_link_dll (info))
5512             info->flags |= DF_STATIC_TLS;
5513           tls_type = TLS_TLS | TLS_TPREL;
5514           goto dogottls;
5515
5516         case R_PPC64_GOT_DTPREL16_DS:
5517         case R_PPC64_GOT_DTPREL16_LO_DS:
5518         case R_PPC64_GOT_DTPREL16_HI:
5519         case R_PPC64_GOT_DTPREL16_HA:
5520           tls_type = TLS_TLS | TLS_DTPREL;
5521         dogottls:
5522           sec->has_tls_reloc = 1;
5523           /* Fall through */
5524
5525         case R_PPC64_GOT16:
5526         case R_PPC64_GOT16_DS:
5527         case R_PPC64_GOT16_HA:
5528         case R_PPC64_GOT16_HI:
5529         case R_PPC64_GOT16_LO:
5530         case R_PPC64_GOT16_LO_DS:
5531           /* This symbol requires a global offset table entry.  */
5532           sec->has_toc_reloc = 1;
5533           if (r_type == R_PPC64_GOT_TLSLD16
5534               || r_type == R_PPC64_GOT_TLSGD16
5535               || r_type == R_PPC64_GOT_TPREL16_DS
5536               || r_type == R_PPC64_GOT_DTPREL16_DS
5537               || r_type == R_PPC64_GOT16
5538               || r_type == R_PPC64_GOT16_DS)
5539             {
5540               htab->do_multi_toc = 1;
5541               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5542             }
5543
5544           if (ppc64_elf_tdata (abfd)->got == NULL
5545               && !create_got_section (abfd, info))
5546             return FALSE;
5547
5548           if (h != NULL)
5549             {
5550               struct ppc_link_hash_entry *eh;
5551               struct got_entry *ent;
5552
5553               eh = (struct ppc_link_hash_entry *) h;
5554               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5555                 if (ent->addend == rel->r_addend
5556                     && ent->owner == abfd
5557                     && ent->tls_type == tls_type)
5558                   break;
5559               if (ent == NULL)
5560                 {
5561                   bfd_size_type amt = sizeof (*ent);
5562                   ent = bfd_alloc (abfd, amt);
5563                   if (ent == NULL)
5564                     return FALSE;
5565                   ent->next = eh->elf.got.glist;
5566                   ent->addend = rel->r_addend;
5567                   ent->owner = abfd;
5568                   ent->tls_type = tls_type;
5569                   ent->is_indirect = FALSE;
5570                   ent->got.refcount = 0;
5571                   eh->elf.got.glist = ent;
5572                 }
5573               ent->got.refcount += 1;
5574               eh->tls_mask |= tls_type;
5575             }
5576           else
5577             /* This is a global offset table entry for a local symbol.  */
5578             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5579                                         rel->r_addend, tls_type))
5580               return FALSE;
5581
5582           /* We may also need a plt entry if the symbol turns out to be
5583              an ifunc.  */
5584           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5585             {
5586               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5587                 return FALSE;
5588             }
5589           break;
5590
5591         case R_PPC64_PLT16_HA:
5592         case R_PPC64_PLT16_HI:
5593         case R_PPC64_PLT16_LO:
5594         case R_PPC64_PLT32:
5595         case R_PPC64_PLT64:
5596           /* This symbol requires a procedure linkage table entry.  */
5597           plt_list = ifunc;
5598           if (h != NULL)
5599             {
5600               h->needs_plt = 1;
5601               if (h->root.root.string[0] == '.'
5602                   && h->root.root.string[1] != '\0')
5603                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5604               plt_list = &h->plt.plist;
5605             }
5606           if (plt_list == NULL)
5607             {
5608               /* It does not make sense to have a procedure linkage
5609                  table entry for a non-ifunc local symbol.  */
5610               info->callbacks->einfo
5611                 /* xgettext:c-format */
5612                 (_("%H: %s reloc against local symbol\n"),
5613                  abfd, sec, rel->r_offset,
5614                  ppc64_elf_howto_table[r_type]->name);
5615               bfd_set_error (bfd_error_bad_value);
5616               return FALSE;
5617             }
5618           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5619             return FALSE;
5620           break;
5621
5622           /* The following relocations don't need to propagate the
5623              relocation if linking a shared object since they are
5624              section relative.  */
5625         case R_PPC64_SECTOFF:
5626         case R_PPC64_SECTOFF_LO:
5627         case R_PPC64_SECTOFF_HI:
5628         case R_PPC64_SECTOFF_HA:
5629         case R_PPC64_SECTOFF_DS:
5630         case R_PPC64_SECTOFF_LO_DS:
5631         case R_PPC64_DTPREL16:
5632         case R_PPC64_DTPREL16_LO:
5633         case R_PPC64_DTPREL16_HI:
5634         case R_PPC64_DTPREL16_HA:
5635         case R_PPC64_DTPREL16_DS:
5636         case R_PPC64_DTPREL16_LO_DS:
5637         case R_PPC64_DTPREL16_HIGH:
5638         case R_PPC64_DTPREL16_HIGHA:
5639         case R_PPC64_DTPREL16_HIGHER:
5640         case R_PPC64_DTPREL16_HIGHERA:
5641         case R_PPC64_DTPREL16_HIGHEST:
5642         case R_PPC64_DTPREL16_HIGHESTA:
5643           break;
5644
5645           /* Nor do these.  */
5646         case R_PPC64_REL16:
5647         case R_PPC64_REL16_LO:
5648         case R_PPC64_REL16_HI:
5649         case R_PPC64_REL16_HA:
5650         case R_PPC64_REL16DX_HA:
5651           break;
5652
5653           /* Not supported as a dynamic relocation.  */
5654         case R_PPC64_ADDR64_LOCAL:
5655           if (bfd_link_pic (info))
5656             {
5657               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5658                 ppc_howto_init ();
5659               /* xgettext:c-format */
5660               info->callbacks->einfo (_("%H: %s reloc unsupported "
5661                                         "in shared libraries and PIEs.\n"),
5662                                       abfd, sec, rel->r_offset,
5663                                       ppc64_elf_howto_table[r_type]->name);
5664               bfd_set_error (bfd_error_bad_value);
5665               return FALSE;
5666             }
5667           break;
5668
5669         case R_PPC64_TOC16:
5670         case R_PPC64_TOC16_DS:
5671           htab->do_multi_toc = 1;
5672           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5673           /* Fall through.  */
5674         case R_PPC64_TOC16_LO:
5675         case R_PPC64_TOC16_HI:
5676         case R_PPC64_TOC16_HA:
5677         case R_PPC64_TOC16_LO_DS:
5678           sec->has_toc_reloc = 1;
5679           break;
5680
5681           /* Marker reloc.  */
5682         case R_PPC64_ENTRY:
5683           break;
5684
5685           /* This relocation describes the C++ object vtable hierarchy.
5686              Reconstruct it for later use during GC.  */
5687         case R_PPC64_GNU_VTINHERIT:
5688           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5689             return FALSE;
5690           break;
5691
5692           /* This relocation describes which C++ vtable entries are actually
5693              used.  Record for later use during GC.  */
5694         case R_PPC64_GNU_VTENTRY:
5695           BFD_ASSERT (h != NULL);
5696           if (h != NULL
5697               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5698             return FALSE;
5699           break;
5700
5701         case R_PPC64_REL14:
5702         case R_PPC64_REL14_BRTAKEN:
5703         case R_PPC64_REL14_BRNTAKEN:
5704           {
5705             asection *dest = NULL;
5706
5707             /* Heuristic: If jumping outside our section, chances are
5708                we are going to need a stub.  */
5709             if (h != NULL)
5710               {
5711                 /* If the sym is weak it may be overridden later, so
5712                    don't assume we know where a weak sym lives.  */
5713                 if (h->root.type == bfd_link_hash_defined)
5714                   dest = h->root.u.def.section;
5715               }
5716             else
5717               {
5718                 Elf_Internal_Sym *isym;
5719
5720                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5721                                               abfd, r_symndx);
5722                 if (isym == NULL)
5723                   return FALSE;
5724
5725                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5726               }
5727
5728             if (dest != sec)
5729               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5730           }
5731           /* Fall through.  */
5732
5733         case R_PPC64_REL24:
5734           plt_list = ifunc;
5735           if (h != NULL)
5736             {
5737               h->needs_plt = 1;
5738               if (h->root.root.string[0] == '.'
5739                   && h->root.root.string[1] != '\0')
5740                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5741
5742               if (h == tga || h == dottga)
5743                 {
5744                   sec->has_tls_reloc = 1;
5745                   if (rel != relocs
5746                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5747                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5748                     /* We have a new-style __tls_get_addr call with
5749                        a marker reloc.  */
5750                     ;
5751                   else
5752                     /* Mark this section as having an old-style call.  */
5753                     sec->has_tls_get_addr_call = 1;
5754                 }
5755               plt_list = &h->plt.plist;
5756             }
5757
5758           /* We may need a .plt entry if the function this reloc
5759              refers to is in a shared lib.  */
5760           if (plt_list
5761               && !update_plt_info (abfd, plt_list, rel->r_addend))
5762             return FALSE;
5763           break;
5764
5765         case R_PPC64_ADDR14:
5766         case R_PPC64_ADDR14_BRNTAKEN:
5767         case R_PPC64_ADDR14_BRTAKEN:
5768         case R_PPC64_ADDR24:
5769           goto dodyn;
5770
5771         case R_PPC64_TPREL64:
5772           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5773           if (bfd_link_dll (info))
5774             info->flags |= DF_STATIC_TLS;
5775           goto dotlstoc;
5776
5777         case R_PPC64_DTPMOD64:
5778           if (rel + 1 < rel_end
5779               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5780               && rel[1].r_offset == rel->r_offset + 8)
5781             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5782           else
5783             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5784           goto dotlstoc;
5785
5786         case R_PPC64_DTPREL64:
5787           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5788           if (rel != relocs
5789               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5790               && rel[-1].r_offset == rel->r_offset - 8)
5791             /* This is the second reloc of a dtpmod, dtprel pair.
5792                Don't mark with TLS_DTPREL.  */
5793             goto dodyn;
5794
5795         dotlstoc:
5796           sec->has_tls_reloc = 1;
5797           if (h != NULL)
5798             {
5799               struct ppc_link_hash_entry *eh;
5800               eh = (struct ppc_link_hash_entry *) h;
5801               eh->tls_mask |= tls_type;
5802             }
5803           else
5804             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5805                                         rel->r_addend, tls_type))
5806               return FALSE;
5807
5808           ppc64_sec = ppc64_elf_section_data (sec);
5809           if (ppc64_sec->sec_type != sec_toc)
5810             {
5811               bfd_size_type amt;
5812
5813               /* One extra to simplify get_tls_mask.  */
5814               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5815               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5816               if (ppc64_sec->u.toc.symndx == NULL)
5817                 return FALSE;
5818               amt = sec->size * sizeof (bfd_vma) / 8;
5819               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5820               if (ppc64_sec->u.toc.add == NULL)
5821                 return FALSE;
5822               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5823               ppc64_sec->sec_type = sec_toc;
5824             }
5825           BFD_ASSERT (rel->r_offset % 8 == 0);
5826           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5827           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5828
5829           /* Mark the second slot of a GD or LD entry.
5830              -1 to indicate GD and -2 to indicate LD.  */
5831           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5832             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5833           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5834             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5835           goto dodyn;
5836
5837         case R_PPC64_TPREL16:
5838         case R_PPC64_TPREL16_LO:
5839         case R_PPC64_TPREL16_HI:
5840         case R_PPC64_TPREL16_HA:
5841         case R_PPC64_TPREL16_DS:
5842         case R_PPC64_TPREL16_LO_DS:
5843         case R_PPC64_TPREL16_HIGH:
5844         case R_PPC64_TPREL16_HIGHA:
5845         case R_PPC64_TPREL16_HIGHER:
5846         case R_PPC64_TPREL16_HIGHERA:
5847         case R_PPC64_TPREL16_HIGHEST:
5848         case R_PPC64_TPREL16_HIGHESTA:
5849           if (bfd_link_dll (info))
5850             info->flags |= DF_STATIC_TLS;
5851           goto dodyn;
5852
5853         case R_PPC64_ADDR64:
5854           if (is_opd
5855               && rel + 1 < rel_end
5856               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5857             {
5858               if (h != NULL)
5859                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5860             }
5861           /* Fall through.  */
5862
5863         case R_PPC64_ADDR16:
5864         case R_PPC64_ADDR16_DS:
5865         case R_PPC64_ADDR16_HA:
5866         case R_PPC64_ADDR16_HI:
5867         case R_PPC64_ADDR16_HIGH:
5868         case R_PPC64_ADDR16_HIGHA:
5869         case R_PPC64_ADDR16_HIGHER:
5870         case R_PPC64_ADDR16_HIGHERA:
5871         case R_PPC64_ADDR16_HIGHEST:
5872         case R_PPC64_ADDR16_HIGHESTA:
5873         case R_PPC64_ADDR16_LO:
5874         case R_PPC64_ADDR16_LO_DS:
5875           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5876               && rel->r_addend == 0)
5877             {
5878               /* We may need a .plt entry if this reloc refers to a
5879                  function in a shared lib.  */
5880               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5881                 return FALSE;
5882               h->pointer_equality_needed = 1;
5883             }
5884           /* Fall through.  */
5885
5886         case R_PPC64_REL30:
5887         case R_PPC64_REL32:
5888         case R_PPC64_REL64:
5889         case R_PPC64_ADDR32:
5890         case R_PPC64_UADDR16:
5891         case R_PPC64_UADDR32:
5892         case R_PPC64_UADDR64:
5893         case R_PPC64_TOC:
5894           if (h != NULL && !bfd_link_pic (info))
5895             /* We may need a copy reloc.  */
5896             h->non_got_ref = 1;
5897
5898           /* Don't propagate .opd relocs.  */
5899           if (NO_OPD_RELOCS && is_opd)
5900             break;
5901
5902           /* If we are creating a shared library, and this is a reloc
5903              against a global symbol, or a non PC relative reloc
5904              against a local symbol, then we need to copy the reloc
5905              into the shared library.  However, if we are linking with
5906              -Bsymbolic, we do not need to copy a reloc against a
5907              global symbol which is defined in an object we are
5908              including in the link (i.e., DEF_REGULAR is set).  At
5909              this point we have not seen all the input files, so it is
5910              possible that DEF_REGULAR is not set now but will be set
5911              later (it is never cleared).  In case of a weak definition,
5912              DEF_REGULAR may be cleared later by a strong definition in
5913              a shared library.  We account for that possibility below by
5914              storing information in the dyn_relocs field of the hash
5915              table entry.  A similar situation occurs when creating
5916              shared libraries and symbol visibility changes render the
5917              symbol local.
5918
5919              If on the other hand, we are creating an executable, we
5920              may need to keep relocations for symbols satisfied by a
5921              dynamic library if we manage to avoid copy relocs for the
5922              symbol.  */
5923         dodyn:
5924           if ((bfd_link_pic (info)
5925                && (must_be_dyn_reloc (info, r_type)
5926                    || (h != NULL
5927                        && (!SYMBOLIC_BIND (info, h)
5928                            || h->root.type == bfd_link_hash_defweak
5929                            || !h->def_regular))))
5930               || (ELIMINATE_COPY_RELOCS
5931                   && !bfd_link_pic (info)
5932                   && h != NULL
5933                   && (h->root.type == bfd_link_hash_defweak
5934                       || !h->def_regular))
5935               || (!bfd_link_pic (info)
5936                   && ifunc != NULL))
5937             {
5938               /* We must copy these reloc types into the output file.
5939                  Create a reloc section in dynobj and make room for
5940                  this reloc.  */
5941               if (sreloc == NULL)
5942                 {
5943                   sreloc = _bfd_elf_make_dynamic_reloc_section
5944                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5945
5946                   if (sreloc == NULL)
5947                     return FALSE;
5948                 }
5949
5950               /* If this is a global symbol, we count the number of
5951                  relocations we need for this symbol.  */
5952               if (h != NULL)
5953                 {
5954                   struct elf_dyn_relocs *p;
5955                   struct elf_dyn_relocs **head;
5956
5957                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5958                   p = *head;
5959                   if (p == NULL || p->sec != sec)
5960                     {
5961                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5962                       if (p == NULL)
5963                         return FALSE;
5964                       p->next = *head;
5965                       *head = p;
5966                       p->sec = sec;
5967                       p->count = 0;
5968                       p->pc_count = 0;
5969                     }
5970                   p->count += 1;
5971                   if (!must_be_dyn_reloc (info, r_type))
5972                     p->pc_count += 1;
5973                 }
5974               else
5975                 {
5976                   /* Track dynamic relocs needed for local syms too.
5977                      We really need local syms available to do this
5978                      easily.  Oh well.  */
5979                   struct ppc_dyn_relocs *p;
5980                   struct ppc_dyn_relocs **head;
5981                   bfd_boolean is_ifunc;
5982                   asection *s;
5983                   void *vpp;
5984                   Elf_Internal_Sym *isym;
5985
5986                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5987                                                 abfd, r_symndx);
5988                   if (isym == NULL)
5989                     return FALSE;
5990
5991                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5992                   if (s == NULL)
5993                     s = sec;
5994
5995                   vpp = &elf_section_data (s)->local_dynrel;
5996                   head = (struct ppc_dyn_relocs **) vpp;
5997                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5998                   p = *head;
5999                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6000                     p = p->next;
6001                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6002                     {
6003                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6004                       if (p == NULL)
6005                         return FALSE;
6006                       p->next = *head;
6007                       *head = p;
6008                       p->sec = sec;
6009                       p->ifunc = is_ifunc;
6010                       p->count = 0;
6011                     }
6012                   p->count += 1;
6013                 }
6014             }
6015           break;
6016
6017         default:
6018           break;
6019         }
6020     }
6021
6022   return TRUE;
6023 }
6024
6025 /* Merge backend specific data from an object file to the output
6026    object file when linking.  */
6027
6028 static bfd_boolean
6029 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6030 {
6031   bfd *obfd = info->output_bfd;
6032   unsigned long iflags, oflags;
6033
6034   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6035     return TRUE;
6036
6037   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6038     return TRUE;
6039
6040   if (!_bfd_generic_verify_endian_match (ibfd, info))
6041     return FALSE;
6042
6043   iflags = elf_elfheader (ibfd)->e_flags;
6044   oflags = elf_elfheader (obfd)->e_flags;
6045
6046   if (iflags & ~EF_PPC64_ABI)
6047     {
6048       _bfd_error_handler
6049         /* xgettext:c-format */
6050         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6051       bfd_set_error (bfd_error_bad_value);
6052       return FALSE;
6053     }
6054   else if (iflags != oflags && iflags != 0)
6055     {
6056       _bfd_error_handler
6057         /* xgettext:c-format */
6058         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6059          ibfd, iflags, oflags);
6060       bfd_set_error (bfd_error_bad_value);
6061       return FALSE;
6062     }
6063
6064   _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6065
6066   /* Merge Tag_compatibility attributes and any common GNU ones.  */
6067   _bfd_elf_merge_object_attributes (ibfd, info);
6068
6069   return TRUE;
6070 }
6071
6072 static bfd_boolean
6073 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6074 {
6075   /* Print normal ELF private data.  */
6076   _bfd_elf_print_private_bfd_data (abfd, ptr);
6077
6078   if (elf_elfheader (abfd)->e_flags != 0)
6079     {
6080       FILE *file = ptr;
6081
6082       fprintf (file, _("private flags = 0x%lx:"),
6083                elf_elfheader (abfd)->e_flags);
6084
6085       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6086         fprintf (file, _(" [abiv%ld]"),
6087                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6088       fputc ('\n', file);
6089     }
6090
6091   return TRUE;
6092 }
6093
6094 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6095    of the code entry point, and its section, which must be in the same
6096    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6097
6098 static bfd_vma
6099 opd_entry_value (asection *opd_sec,
6100                  bfd_vma offset,
6101                  asection **code_sec,
6102                  bfd_vma *code_off,
6103                  bfd_boolean in_code_sec)
6104 {
6105   bfd *opd_bfd = opd_sec->owner;
6106   Elf_Internal_Rela *relocs;
6107   Elf_Internal_Rela *lo, *hi, *look;
6108   bfd_vma val;
6109
6110   /* No relocs implies we are linking a --just-symbols object, or looking
6111      at a final linked executable with addr2line or somesuch.  */
6112   if (opd_sec->reloc_count == 0)
6113     {
6114       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6115
6116       if (contents == NULL)
6117         {
6118           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6119             return (bfd_vma) -1;
6120           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6121         }
6122
6123       /* PR 17512: file: 64b9dfbb.  */
6124       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6125         return (bfd_vma) -1;
6126
6127       val = bfd_get_64 (opd_bfd, contents + offset);
6128       if (code_sec != NULL)
6129         {
6130           asection *sec, *likely = NULL;
6131
6132           if (in_code_sec)
6133             {
6134               sec = *code_sec;
6135               if (sec->vma <= val
6136                   && val < sec->vma + sec->size)
6137                 likely = sec;
6138               else
6139                 val = -1;
6140             }
6141           else
6142             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6143               if (sec->vma <= val
6144                   && (sec->flags & SEC_LOAD) != 0
6145                   && (sec->flags & SEC_ALLOC) != 0)
6146                 likely = sec;
6147           if (likely != NULL)
6148             {
6149               *code_sec = likely;
6150               if (code_off != NULL)
6151                 *code_off = val - likely->vma;
6152             }
6153         }
6154       return val;
6155     }
6156
6157   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6158
6159   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6160   if (relocs == NULL)
6161     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6162   /* PR 17512: file: df8e1fd6.  */
6163   if (relocs == NULL)
6164     return (bfd_vma) -1;
6165
6166   /* Go find the opd reloc at the sym address.  */
6167   lo = relocs;
6168   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6169   val = (bfd_vma) -1;
6170   while (lo < hi)
6171     {
6172       look = lo + (hi - lo) / 2;
6173       if (look->r_offset < offset)
6174         lo = look + 1;
6175       else if (look->r_offset > offset)
6176         hi = look;
6177       else
6178         {
6179           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6180
6181           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6182               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6183             {
6184               unsigned long symndx = ELF64_R_SYM (look->r_info);
6185               asection *sec = NULL;
6186
6187               if (symndx >= symtab_hdr->sh_info
6188                   && elf_sym_hashes (opd_bfd) != NULL)
6189                 {
6190                   struct elf_link_hash_entry **sym_hashes;
6191                   struct elf_link_hash_entry *rh;
6192
6193                   sym_hashes = elf_sym_hashes (opd_bfd);
6194                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6195                   if (rh != NULL)
6196                     {
6197                       rh = elf_follow_link (rh);
6198                       if (rh->root.type != bfd_link_hash_defined
6199                           && rh->root.type != bfd_link_hash_defweak)
6200                         break;
6201                       if (rh->root.u.def.section->owner == opd_bfd)
6202                         {
6203                           val = rh->root.u.def.value;
6204                           sec = rh->root.u.def.section;
6205                         }
6206                     }
6207                 }
6208
6209               if (sec == NULL)
6210                 {
6211                   Elf_Internal_Sym *sym;
6212
6213                   if (symndx < symtab_hdr->sh_info)
6214                     {
6215                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6216                       if (sym == NULL)
6217                         {
6218                           size_t symcnt = symtab_hdr->sh_info;
6219                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6220                                                       symcnt, 0,
6221                                                       NULL, NULL, NULL);
6222                           if (sym == NULL)
6223                             break;
6224                           symtab_hdr->contents = (bfd_byte *) sym;
6225                         }
6226                       sym += symndx;
6227                     }
6228                   else
6229                     {
6230                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6231                                                   1, symndx,
6232                                                   NULL, NULL, NULL);
6233                       if (sym == NULL)
6234                         break;
6235                     }
6236                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6237                   if (sec == NULL)
6238                     break;
6239                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6240                   val = sym->st_value;
6241                 }
6242
6243               val += look->r_addend;
6244               if (code_off != NULL)
6245                 *code_off = val;
6246               if (code_sec != NULL)
6247                 {
6248                   if (in_code_sec && *code_sec != sec)
6249                     return -1;
6250                   else
6251                     *code_sec = sec;
6252                 }
6253               if (sec->output_section != NULL)
6254                 val += sec->output_section->vma + sec->output_offset;
6255             }
6256           break;
6257         }
6258     }
6259
6260   return val;
6261 }
6262
6263 /* If the ELF symbol SYM might be a function in SEC, return the
6264    function size and set *CODE_OFF to the function's entry point,
6265    otherwise return zero.  */
6266
6267 static bfd_size_type
6268 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6269                               bfd_vma *code_off)
6270 {
6271   bfd_size_type size;
6272
6273   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6274                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6275     return 0;
6276
6277   size = 0;
6278   if (!(sym->flags & BSF_SYNTHETIC))
6279     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6280
6281   if (strcmp (sym->section->name, ".opd") == 0)
6282     {
6283       struct _opd_sec_data *opd = get_opd_info (sym->section);
6284       bfd_vma symval = sym->value;
6285
6286       if (opd != NULL
6287           && opd->adjust != NULL
6288           && elf_section_data (sym->section)->relocs != NULL)
6289         {
6290           /* opd_entry_value will use cached relocs that have been
6291              adjusted, but with raw symbols.  That means both local
6292              and global symbols need adjusting.  */
6293           long adjust = opd->adjust[OPD_NDX (symval)];
6294           if (adjust == -1)
6295             return 0;
6296           symval += adjust;
6297         }
6298
6299       if (opd_entry_value (sym->section, symval,
6300                            &sec, code_off, TRUE) == (bfd_vma) -1)
6301         return 0;
6302       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6303          symbol.  This size has nothing to do with the code size of the
6304          function, which is what we're supposed to return, but the
6305          code size isn't available without looking up the dot-sym.
6306          However, doing that would be a waste of time particularly
6307          since elf_find_function will look at the dot-sym anyway.
6308          Now, elf_find_function will keep the largest size of any
6309          function sym found at the code address of interest, so return
6310          1 here to avoid it incorrectly caching a larger function size
6311          for a small function.  This does mean we return the wrong
6312          size for a new-ABI function of size 24, but all that does is
6313          disable caching for such functions.  */
6314       if (size == 24)
6315         size = 1;
6316     }
6317   else
6318     {
6319       if (sym->section != sec)
6320         return 0;
6321       *code_off = sym->value;
6322     }
6323   if (size == 0)
6324     size = 1;
6325   return size;
6326 }
6327
6328 /* Return true if symbol is a strong function defined in an ELFv2
6329    object with st_other localentry bits of zero, ie. its local entry
6330    point coincides with its global entry point.  */
6331
6332 static bfd_boolean
6333 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6334 {
6335   return (h != NULL
6336           && h->type == STT_FUNC
6337           && h->root.type == bfd_link_hash_defined
6338           && (STO_PPC64_LOCAL_MASK & h->other) == 0
6339           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6340           && is_ppc64_elf (h->root.u.def.section->owner)
6341           && abiversion (h->root.u.def.section->owner) >= 2);
6342 }
6343
6344 /* Return true if symbol is defined in a regular object file.  */
6345
6346 static bfd_boolean
6347 is_static_defined (struct elf_link_hash_entry *h)
6348 {
6349   return ((h->root.type == bfd_link_hash_defined
6350            || h->root.type == bfd_link_hash_defweak)
6351           && h->root.u.def.section != NULL
6352           && h->root.u.def.section->output_section != NULL);
6353 }
6354
6355 /* If FDH is a function descriptor symbol, return the associated code
6356    entry symbol if it is defined.  Return NULL otherwise.  */
6357
6358 static struct ppc_link_hash_entry *
6359 defined_code_entry (struct ppc_link_hash_entry *fdh)
6360 {
6361   if (fdh->is_func_descriptor)
6362     {
6363       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6364       if (fh->elf.root.type == bfd_link_hash_defined
6365           || fh->elf.root.type == bfd_link_hash_defweak)
6366         return fh;
6367     }
6368   return NULL;
6369 }
6370
6371 /* If FH is a function code entry symbol, return the associated
6372    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6373
6374 static struct ppc_link_hash_entry *
6375 defined_func_desc (struct ppc_link_hash_entry *fh)
6376 {
6377   if (fh->oh != NULL
6378       && fh->oh->is_func_descriptor)
6379     {
6380       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6381       if (fdh->elf.root.type == bfd_link_hash_defined
6382           || fdh->elf.root.type == bfd_link_hash_defweak)
6383         return fdh;
6384     }
6385   return NULL;
6386 }
6387
6388 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6389
6390 /* Garbage collect sections, after first dealing with dot-symbols.  */
6391
6392 static bfd_boolean
6393 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6394 {
6395   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6396
6397   if (htab != NULL && htab->need_func_desc_adj)
6398     {
6399       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6400       htab->need_func_desc_adj = 0;
6401     }
6402   return bfd_elf_gc_sections (abfd, info);
6403 }
6404
6405 /* Mark all our entry sym sections, both opd and code section.  */
6406
6407 static void
6408 ppc64_elf_gc_keep (struct bfd_link_info *info)
6409 {
6410   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6411   struct bfd_sym_chain *sym;
6412
6413   if (htab == NULL)
6414     return;
6415
6416   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6417     {
6418       struct ppc_link_hash_entry *eh, *fh;
6419       asection *sec;
6420
6421       eh = (struct ppc_link_hash_entry *)
6422         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6423       if (eh == NULL)
6424         continue;
6425       if (eh->elf.root.type != bfd_link_hash_defined
6426           && eh->elf.root.type != bfd_link_hash_defweak)
6427         continue;
6428
6429       fh = defined_code_entry (eh);
6430       if (fh != NULL)
6431         {
6432           sec = fh->elf.root.u.def.section;
6433           sec->flags |= SEC_KEEP;
6434         }
6435       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6436                && opd_entry_value (eh->elf.root.u.def.section,
6437                                    eh->elf.root.u.def.value,
6438                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6439         sec->flags |= SEC_KEEP;
6440
6441       sec = eh->elf.root.u.def.section;
6442       sec->flags |= SEC_KEEP;
6443     }
6444 }
6445
6446 /* Mark sections containing dynamically referenced symbols.  When
6447    building shared libraries, we must assume that any visible symbol is
6448    referenced.  */
6449
6450 static bfd_boolean
6451 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6452 {
6453   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6454   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6455   struct ppc_link_hash_entry *fdh;
6456   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6457
6458   /* Dynamic linking info is on the func descriptor sym.  */
6459   fdh = defined_func_desc (eh);
6460   if (fdh != NULL)
6461     eh = fdh;
6462
6463   if ((eh->elf.root.type == bfd_link_hash_defined
6464        || eh->elf.root.type == bfd_link_hash_defweak)
6465       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6466           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6467               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6468               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6469               && (!bfd_link_executable (info)
6470                   || info->gc_keep_exported
6471                   || info->export_dynamic
6472                   || (eh->elf.dynamic
6473                       && d != NULL
6474                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6475               && (eh->elf.versioned >= versioned
6476                   || !bfd_hide_sym_by_version (info->version_info,
6477                                                eh->elf.root.root.string)))))
6478     {
6479       asection *code_sec;
6480       struct ppc_link_hash_entry *fh;
6481
6482       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6483
6484       /* Function descriptor syms cause the associated
6485          function code sym section to be marked.  */
6486       fh = defined_code_entry (eh);
6487       if (fh != NULL)
6488         {
6489           code_sec = fh->elf.root.u.def.section;
6490           code_sec->flags |= SEC_KEEP;
6491         }
6492       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6493                && opd_entry_value (eh->elf.root.u.def.section,
6494                                    eh->elf.root.u.def.value,
6495                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6496         code_sec->flags |= SEC_KEEP;
6497     }
6498
6499   return TRUE;
6500 }
6501
6502 /* Return the section that should be marked against GC for a given
6503    relocation.  */
6504
6505 static asection *
6506 ppc64_elf_gc_mark_hook (asection *sec,
6507                         struct bfd_link_info *info,
6508                         Elf_Internal_Rela *rel,
6509                         struct elf_link_hash_entry *h,
6510                         Elf_Internal_Sym *sym)
6511 {
6512   asection *rsec;
6513
6514   /* Syms return NULL if we're marking .opd, so we avoid marking all
6515      function sections, as all functions are referenced in .opd.  */
6516   rsec = NULL;
6517   if (get_opd_info (sec) != NULL)
6518     return rsec;
6519
6520   if (h != NULL)
6521     {
6522       enum elf_ppc64_reloc_type r_type;
6523       struct ppc_link_hash_entry *eh, *fh, *fdh;
6524
6525       r_type = ELF64_R_TYPE (rel->r_info);
6526       switch (r_type)
6527         {
6528         case R_PPC64_GNU_VTINHERIT:
6529         case R_PPC64_GNU_VTENTRY:
6530           break;
6531
6532         default:
6533           switch (h->root.type)
6534             {
6535             case bfd_link_hash_defined:
6536             case bfd_link_hash_defweak:
6537               eh = (struct ppc_link_hash_entry *) h;
6538               fdh = defined_func_desc (eh);
6539               if (fdh != NULL)
6540                 {
6541                   /* -mcall-aixdesc code references the dot-symbol on
6542                      a call reloc.  Mark the function descriptor too
6543                      against garbage collection.  */
6544                   fdh->elf.mark = 1;
6545                   if (fdh->elf.is_weakalias)
6546                     weakdef (&fdh->elf)->mark = 1;
6547                   eh = fdh;
6548                 }
6549
6550               /* Function descriptor syms cause the associated
6551                  function code sym section to be marked.  */
6552               fh = defined_code_entry (eh);
6553               if (fh != NULL)
6554                 {
6555                   /* They also mark their opd section.  */
6556                   eh->elf.root.u.def.section->gc_mark = 1;
6557
6558                   rsec = fh->elf.root.u.def.section;
6559                 }
6560               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6561                        && opd_entry_value (eh->elf.root.u.def.section,
6562                                            eh->elf.root.u.def.value,
6563                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6564                 eh->elf.root.u.def.section->gc_mark = 1;
6565               else
6566                 rsec = h->root.u.def.section;
6567               break;
6568
6569             case bfd_link_hash_common:
6570               rsec = h->root.u.c.p->section;
6571               break;
6572
6573             default:
6574               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6575             }
6576         }
6577     }
6578   else
6579     {
6580       struct _opd_sec_data *opd;
6581
6582       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6583       opd = get_opd_info (rsec);
6584       if (opd != NULL && opd->func_sec != NULL)
6585         {
6586           rsec->gc_mark = 1;
6587
6588           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6589         }
6590     }
6591
6592   return rsec;
6593 }
6594
6595 /* The maximum size of .sfpr.  */
6596 #define SFPR_MAX (218*4)
6597
6598 struct sfpr_def_parms
6599 {
6600   const char name[12];
6601   unsigned char lo, hi;
6602   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6603   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6604 };
6605
6606 /* Auto-generate _save*, _rest* functions in .sfpr.
6607    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6608    instead.  */
6609
6610 static bfd_boolean
6611 sfpr_define (struct bfd_link_info *info,
6612              const struct sfpr_def_parms *parm,
6613              asection *stub_sec)
6614 {
6615   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6616   unsigned int i;
6617   size_t len = strlen (parm->name);
6618   bfd_boolean writing = FALSE;
6619   char sym[16];
6620
6621   if (htab == NULL)
6622     return FALSE;
6623
6624   memcpy (sym, parm->name, len);
6625   sym[len + 2] = 0;
6626
6627   for (i = parm->lo; i <= parm->hi; i++)
6628     {
6629       struct ppc_link_hash_entry *h;
6630
6631       sym[len + 0] = i / 10 + '0';
6632       sym[len + 1] = i % 10 + '0';
6633       h = (struct ppc_link_hash_entry *)
6634         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6635       if (stub_sec != NULL)
6636         {
6637           if (h != NULL
6638               && h->elf.root.type == bfd_link_hash_defined
6639               && h->elf.root.u.def.section == htab->sfpr)
6640             {
6641               struct elf_link_hash_entry *s;
6642               char buf[32];
6643               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6644               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6645               if (s == NULL)
6646                 return FALSE;
6647               if (s->root.type == bfd_link_hash_new
6648                   || (s->root.type = bfd_link_hash_defined
6649                       && s->root.u.def.section == stub_sec))
6650                 {
6651                   s->root.type = bfd_link_hash_defined;
6652                   s->root.u.def.section = stub_sec;
6653                   s->root.u.def.value = (stub_sec->size
6654                                          + h->elf.root.u.def.value);
6655                   s->ref_regular = 1;
6656                   s->def_regular = 1;
6657                   s->ref_regular_nonweak = 1;
6658                   s->forced_local = 1;
6659                   s->non_elf = 0;
6660                   s->root.linker_def = 1;
6661                 }
6662             }
6663           continue;
6664         }
6665       if (h != NULL)
6666         {
6667           h->save_res = 1;
6668           if (!h->elf.def_regular)
6669             {
6670               h->elf.root.type = bfd_link_hash_defined;
6671               h->elf.root.u.def.section = htab->sfpr;
6672               h->elf.root.u.def.value = htab->sfpr->size;
6673               h->elf.type = STT_FUNC;
6674               h->elf.def_regular = 1;
6675               h->elf.non_elf = 0;
6676               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6677               writing = TRUE;
6678               if (htab->sfpr->contents == NULL)
6679                 {
6680                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6681                   if (htab->sfpr->contents == NULL)
6682                     return FALSE;
6683                 }
6684             }
6685         }
6686       if (writing)
6687         {
6688           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6689           if (i != parm->hi)
6690             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6691           else
6692             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6693           htab->sfpr->size = p - htab->sfpr->contents;
6694         }
6695     }
6696
6697   return TRUE;
6698 }
6699
6700 static bfd_byte *
6701 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6702 {
6703   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6704   return p + 4;
6705 }
6706
6707 static bfd_byte *
6708 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6709 {
6710   p = savegpr0 (abfd, p, r);
6711   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6712   p = p + 4;
6713   bfd_put_32 (abfd, BLR, p);
6714   return p + 4;
6715 }
6716
6717 static bfd_byte *
6718 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6719 {
6720   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6721   return p + 4;
6722 }
6723
6724 static bfd_byte *
6725 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6726 {
6727   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6728   p = p + 4;
6729   p = restgpr0 (abfd, p, r);
6730   bfd_put_32 (abfd, MTLR_R0, p);
6731   p = p + 4;
6732   if (r == 29)
6733     {
6734       p = restgpr0 (abfd, p, 30);
6735       p = restgpr0 (abfd, p, 31);
6736     }
6737   bfd_put_32 (abfd, BLR, p);
6738   return p + 4;
6739 }
6740
6741 static bfd_byte *
6742 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6743 {
6744   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6745   return p + 4;
6746 }
6747
6748 static bfd_byte *
6749 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6750 {
6751   p = savegpr1 (abfd, p, r);
6752   bfd_put_32 (abfd, BLR, p);
6753   return p + 4;
6754 }
6755
6756 static bfd_byte *
6757 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6758 {
6759   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6760   return p + 4;
6761 }
6762
6763 static bfd_byte *
6764 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6765 {
6766   p = restgpr1 (abfd, p, r);
6767   bfd_put_32 (abfd, BLR, p);
6768   return p + 4;
6769 }
6770
6771 static bfd_byte *
6772 savefpr (bfd *abfd, bfd_byte *p, int r)
6773 {
6774   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6775   return p + 4;
6776 }
6777
6778 static bfd_byte *
6779 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6780 {
6781   p = savefpr (abfd, p, r);
6782   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6783   p = p + 4;
6784   bfd_put_32 (abfd, BLR, p);
6785   return p + 4;
6786 }
6787
6788 static bfd_byte *
6789 restfpr (bfd *abfd, bfd_byte *p, int r)
6790 {
6791   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6792   return p + 4;
6793 }
6794
6795 static bfd_byte *
6796 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6797 {
6798   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6799   p = p + 4;
6800   p = restfpr (abfd, p, r);
6801   bfd_put_32 (abfd, MTLR_R0, p);
6802   p = p + 4;
6803   if (r == 29)
6804     {
6805       p = restfpr (abfd, p, 30);
6806       p = restfpr (abfd, p, 31);
6807     }
6808   bfd_put_32 (abfd, BLR, p);
6809   return p + 4;
6810 }
6811
6812 static bfd_byte *
6813 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6814 {
6815   p = savefpr (abfd, p, r);
6816   bfd_put_32 (abfd, BLR, p);
6817   return p + 4;
6818 }
6819
6820 static bfd_byte *
6821 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6822 {
6823   p = restfpr (abfd, p, r);
6824   bfd_put_32 (abfd, BLR, p);
6825   return p + 4;
6826 }
6827
6828 static bfd_byte *
6829 savevr (bfd *abfd, bfd_byte *p, int r)
6830 {
6831   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6832   p = p + 4;
6833   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6834   return p + 4;
6835 }
6836
6837 static bfd_byte *
6838 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6839 {
6840   p = savevr (abfd, p, r);
6841   bfd_put_32 (abfd, BLR, p);
6842   return p + 4;
6843 }
6844
6845 static bfd_byte *
6846 restvr (bfd *abfd, bfd_byte *p, int r)
6847 {
6848   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6849   p = p + 4;
6850   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6851   return p + 4;
6852 }
6853
6854 static bfd_byte *
6855 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6856 {
6857   p = restvr (abfd, p, r);
6858   bfd_put_32 (abfd, BLR, p);
6859   return p + 4;
6860 }
6861
6862 /* Called via elf_link_hash_traverse to transfer dynamic linking
6863    information on function code symbol entries to their corresponding
6864    function descriptor symbol entries.  */
6865
6866 static bfd_boolean
6867 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6868 {
6869   struct bfd_link_info *info;
6870   struct ppc_link_hash_table *htab;
6871   struct ppc_link_hash_entry *fh;
6872   struct ppc_link_hash_entry *fdh;
6873   bfd_boolean force_local;
6874
6875   fh = (struct ppc_link_hash_entry *) h;
6876   if (fh->elf.root.type == bfd_link_hash_indirect)
6877     return TRUE;
6878
6879   if (!fh->is_func)
6880     return TRUE;
6881
6882   if (fh->elf.root.root.string[0] != '.'
6883       || fh->elf.root.root.string[1] == '\0')
6884     return TRUE;
6885
6886   info = inf;
6887   htab = ppc_hash_table (info);
6888   if (htab == NULL)
6889     return FALSE;
6890
6891   /* Find the corresponding function descriptor symbol.  */
6892   fdh = lookup_fdh (fh, htab);
6893
6894   /* Resolve undefined references to dot-symbols as the value
6895      in the function descriptor, if we have one in a regular object.
6896      This is to satisfy cases like ".quad .foo".  Calls to functions
6897      in dynamic objects are handled elsewhere.  */
6898   if ((fh->elf.root.type == bfd_link_hash_undefined
6899        || fh->elf.root.type == bfd_link_hash_undefweak)
6900       && (fdh->elf.root.type == bfd_link_hash_defined
6901           || fdh->elf.root.type == bfd_link_hash_defweak)
6902       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6903       && opd_entry_value (fdh->elf.root.u.def.section,
6904                           fdh->elf.root.u.def.value,
6905                           &fh->elf.root.u.def.section,
6906                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6907     {
6908       fh->elf.root.type = fdh->elf.root.type;
6909       fh->elf.forced_local = 1;
6910       fh->elf.def_regular = fdh->elf.def_regular;
6911       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6912     }
6913
6914   if (!fh->elf.dynamic)
6915     {
6916       struct plt_entry *ent;
6917
6918       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6919         if (ent->plt.refcount > 0)
6920           break;
6921       if (ent == NULL)
6922         return TRUE;
6923     }
6924
6925   /* Create a descriptor as undefined if necessary.  */
6926   if (fdh == NULL
6927       && !bfd_link_executable (info)
6928       && (fh->elf.root.type == bfd_link_hash_undefined
6929           || fh->elf.root.type == bfd_link_hash_undefweak))
6930     {
6931       fdh = make_fdh (info, fh);
6932       if (fdh == NULL)
6933         return FALSE;
6934     }
6935
6936   /* We can't support overriding of symbols on a fake descriptor.  */
6937   if (fdh != NULL
6938       && fdh->fake
6939       && (fh->elf.root.type == bfd_link_hash_defined
6940           || fh->elf.root.type == bfd_link_hash_defweak))
6941     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6942
6943   /* Transfer dynamic linking information to the function descriptor.  */
6944   if (fdh != NULL)
6945     {
6946       fdh->elf.ref_regular |= fh->elf.ref_regular;
6947       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6948       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6949       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6950       fdh->elf.dynamic |= fh->elf.dynamic;
6951       fdh->elf.needs_plt |= (fh->elf.needs_plt
6952                              || fh->elf.type == STT_FUNC
6953                              || fh->elf.type == STT_GNU_IFUNC);
6954       move_plt_plist (fh, fdh);
6955
6956       if (!fdh->elf.forced_local
6957           && fh->elf.dynindx != -1)
6958         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6959           return FALSE;
6960     }
6961
6962   /* Now that the info is on the function descriptor, clear the
6963      function code sym info.  Any function code syms for which we
6964      don't have a definition in a regular file, we force local.
6965      This prevents a shared library from exporting syms that have
6966      been imported from another library.  Function code syms that
6967      are really in the library we must leave global to prevent the
6968      linker dragging in a definition from a static library.  */
6969   force_local = (!fh->elf.def_regular
6970                  || fdh == NULL
6971                  || !fdh->elf.def_regular
6972                  || fdh->elf.forced_local);
6973   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6974
6975   return TRUE;
6976 }
6977
6978 static const struct sfpr_def_parms save_res_funcs[] =
6979   {
6980     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6981     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6982     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6983     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6984     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6985     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6986     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6987     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6988     { "._savef", 14, 31, savefpr, savefpr1_tail },
6989     { "._restf", 14, 31, restfpr, restfpr1_tail },
6990     { "_savevr_", 20, 31, savevr, savevr_tail },
6991     { "_restvr_", 20, 31, restvr, restvr_tail }
6992   };
6993
6994 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
6995    this hook to a) provide some gcc support functions, and b) transfer
6996    dynamic linking information gathered so far on function code symbol
6997    entries, to their corresponding function descriptor symbol entries.  */
6998
6999 static bfd_boolean
7000 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7001                             struct bfd_link_info *info)
7002 {
7003   struct ppc_link_hash_table *htab;
7004
7005   htab = ppc_hash_table (info);
7006   if (htab == NULL)
7007     return FALSE;
7008
7009   /* Provide any missing _save* and _rest* functions.  */
7010   if (htab->sfpr != NULL)
7011     {
7012       unsigned int i;
7013
7014       htab->sfpr->size = 0;
7015       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7016         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7017           return FALSE;
7018       if (htab->sfpr->size == 0)
7019         htab->sfpr->flags |= SEC_EXCLUDE;
7020     }
7021
7022   if (bfd_link_relocatable (info))
7023     return TRUE;
7024
7025   if (htab->elf.hgot != NULL)
7026     {
7027       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7028       /* Make .TOC. defined so as to prevent it being made dynamic.
7029          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7030       if (!htab->elf.hgot->def_regular
7031           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7032         {
7033           htab->elf.hgot->root.type = bfd_link_hash_defined;
7034           htab->elf.hgot->root.u.def.value = 0;
7035           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7036           htab->elf.hgot->def_regular = 1;
7037           htab->elf.hgot->root.linker_def = 1;
7038         }
7039       htab->elf.hgot->type = STT_OBJECT;
7040       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7041                                | STV_HIDDEN);
7042     }
7043
7044   if (htab->need_func_desc_adj)
7045     {
7046       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7047       htab->need_func_desc_adj = 0;
7048     }
7049
7050   return TRUE;
7051 }
7052
7053 /* Find dynamic relocs for H that apply to read-only sections.  */
7054
7055 static asection *
7056 readonly_dynrelocs (struct elf_link_hash_entry *h)
7057 {
7058   struct ppc_link_hash_entry *eh;
7059   struct elf_dyn_relocs *p;
7060
7061   eh = (struct ppc_link_hash_entry *) h;
7062   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7063     {
7064       asection *s = p->sec->output_section;
7065
7066       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7067         return p->sec;
7068     }
7069   return NULL;
7070 }
7071
7072 /* Return true if we have dynamic relocs against H or any of its weak
7073    aliases, that apply to read-only sections.  Cannot be used after
7074    size_dynamic_sections.  */
7075
7076 static bfd_boolean
7077 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7078 {
7079   struct ppc_link_hash_entry *eh;
7080
7081   eh = (struct ppc_link_hash_entry *) h;
7082   do
7083     {
7084       if (readonly_dynrelocs (&eh->elf))
7085         return TRUE;
7086       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7087     } while (eh != NULL && &eh->elf != h);
7088
7089   return FALSE;
7090 }
7091
7092 /* Return whether EH has pc-relative dynamic relocs.  */
7093
7094 static bfd_boolean
7095 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7096 {
7097   struct elf_dyn_relocs *p;
7098
7099   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7100     if (p->pc_count != 0)
7101       return TRUE;
7102   return FALSE;
7103 }
7104
7105 /* Return true if a global entry stub will be created for H.  Valid
7106    for ELFv2 before plt entries have been allocated.  */
7107
7108 static bfd_boolean
7109 global_entry_stub (struct elf_link_hash_entry *h)
7110 {
7111   struct plt_entry *pent;
7112
7113   if (!h->pointer_equality_needed
7114       || h->def_regular)
7115     return FALSE;
7116
7117   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7118     if (pent->plt.refcount > 0
7119         && pent->addend == 0)
7120       return TRUE;
7121
7122   return FALSE;
7123 }
7124
7125 /* Adjust a symbol defined by a dynamic object and referenced by a
7126    regular object.  The current definition is in some section of the
7127    dynamic object, but we're not including those sections.  We have to
7128    change the definition to something the rest of the link can
7129    understand.  */
7130
7131 static bfd_boolean
7132 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7133                                  struct elf_link_hash_entry *h)
7134 {
7135   struct ppc_link_hash_table *htab;
7136   asection *s, *srel;
7137
7138   htab = ppc_hash_table (info);
7139   if (htab == NULL)
7140     return FALSE;
7141
7142   /* Deal with function syms.  */
7143   if (h->type == STT_FUNC
7144       || h->type == STT_GNU_IFUNC
7145       || h->needs_plt)
7146     {
7147       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7148                            || SYMBOL_CALLS_LOCAL (info, h)
7149                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7150       /* Discard dyn_relocs when non-pic if we've decided that a
7151          function symbol is local and not an ifunc.  We keep dynamic
7152          relocs for ifuncs when local rather than always emitting a
7153          plt call stub for them and defining the symbol on the call
7154          stub.  We can't do that for ELFv1 anyway (a function symbol
7155          is defined on a descriptor, not code) and it can be faster at
7156          run-time due to not needing to bounce through a stub.  The
7157          dyn_relocs for ifuncs will be applied even in a static
7158          executable.  */
7159       if (!bfd_link_pic (info)
7160           && h->type != STT_GNU_IFUNC
7161           && local)
7162         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7163
7164       /* Clear procedure linkage table information for any symbol that
7165          won't need a .plt entry.  */
7166       struct plt_entry *ent;
7167       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7168         if (ent->plt.refcount > 0)
7169           break;
7170       if (ent == NULL
7171           || (h->type != STT_GNU_IFUNC && local))
7172         {
7173           h->plt.plist = NULL;
7174           h->needs_plt = 0;
7175           h->pointer_equality_needed = 0;
7176         }
7177       else if (abiversion (info->output_bfd) >= 2)
7178         {
7179           /* Taking a function's address in a read/write section
7180              doesn't require us to define the function symbol in the
7181              executable on a global entry stub.  A dynamic reloc can
7182              be used instead.  The reason we prefer a few more dynamic
7183              relocs is that calling via a global entry stub costs a
7184              few more instructions, and pointer_equality_needed causes
7185              extra work in ld.so when resolving these symbols.  */
7186           if (global_entry_stub (h))
7187             {
7188               if (!readonly_dynrelocs (h))
7189                 {
7190                   h->pointer_equality_needed = 0;
7191                   /* If we haven't seen a branch reloc then we don't need
7192                      a plt entry.  */
7193                   if (!h->needs_plt)
7194                     h->plt.plist = NULL;
7195                 }
7196               else if (!bfd_link_pic (info))
7197                 /* We are going to be defining the function symbol on the
7198                    plt stub, so no dyn_relocs needed when non-pic.  */
7199                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7200             }
7201
7202           /* ELFv2 function symbols can't have copy relocs.  */
7203           return TRUE;
7204         }
7205       else if (!h->needs_plt
7206                && !readonly_dynrelocs (h))
7207         {
7208           /* If we haven't seen a branch reloc then we don't need a
7209              plt entry.  */
7210           h->plt.plist = NULL;
7211           h->pointer_equality_needed = 0;
7212           return TRUE;
7213         }
7214     }
7215   else
7216     h->plt.plist = NULL;
7217
7218   /* If this is a weak symbol, and there is a real definition, the
7219      processor independent code will have arranged for us to see the
7220      real definition first, and we can just use the same value.  */
7221   if (h->is_weakalias)
7222     {
7223       struct elf_link_hash_entry *def = weakdef (h);
7224       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7225       h->root.u.def.section = def->root.u.def.section;
7226       h->root.u.def.value = def->root.u.def.value;
7227       if (def->root.u.def.section == htab->elf.sdynbss
7228           || def->root.u.def.section == htab->elf.sdynrelro)
7229         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7230       return TRUE;
7231     }
7232
7233   /* If we are creating a shared library, we must presume that the
7234      only references to the symbol are via the global offset table.
7235      For such cases we need not do anything here; the relocations will
7236      be handled correctly by relocate_section.  */
7237   if (bfd_link_pic (info))
7238     return TRUE;
7239
7240   /* If there are no references to this symbol that do not use the
7241      GOT, we don't need to generate a copy reloc.  */
7242   if (!h->non_got_ref)
7243     return TRUE;
7244
7245   /* Don't generate a copy reloc for symbols defined in the executable.  */
7246   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7247
7248       /* If -z nocopyreloc was given, don't generate them either.  */
7249       || info->nocopyreloc
7250
7251       /* If we don't find any dynamic relocs in read-only sections, then
7252          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7253       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7254
7255       /* Protected variables do not work with .dynbss.  The copy in
7256          .dynbss won't be used by the shared library with the protected
7257          definition for the variable.  Text relocations are preferable
7258          to an incorrect program.  */
7259       || h->protected_def)
7260     return TRUE;
7261
7262   if (h->plt.plist != NULL)
7263     {
7264       /* We should never get here, but unfortunately there are versions
7265          of gcc out there that improperly (for this ABI) put initialized
7266          function pointers, vtable refs and suchlike in read-only
7267          sections.  Allow them to proceed, but warn that this might
7268          break at runtime.  */
7269       info->callbacks->einfo
7270         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7271            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7272          h->root.root.string);
7273     }
7274
7275   /* This is a reference to a symbol defined by a dynamic object which
7276      is not a function.  */
7277
7278   /* We must allocate the symbol in our .dynbss section, which will
7279      become part of the .bss section of the executable.  There will be
7280      an entry for this symbol in the .dynsym section.  The dynamic
7281      object will contain position independent code, so all references
7282      from the dynamic object to this symbol will go through the global
7283      offset table.  The dynamic linker will use the .dynsym entry to
7284      determine the address it must put in the global offset table, so
7285      both the dynamic object and the regular object will refer to the
7286      same memory location for the variable.  */
7287   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7288     {
7289       s = htab->elf.sdynrelro;
7290       srel = htab->elf.sreldynrelro;
7291     }
7292   else
7293     {
7294       s = htab->elf.sdynbss;
7295       srel = htab->elf.srelbss;
7296     }
7297   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7298     {
7299       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7300          linker to copy the initial value out of the dynamic object
7301          and into the runtime process image.  */
7302       srel->size += sizeof (Elf64_External_Rela);
7303       h->needs_copy = 1;
7304     }
7305
7306   /* We no longer want dyn_relocs.  */
7307   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7308   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7309 }
7310
7311 /* If given a function descriptor symbol, hide both the function code
7312    sym and the descriptor.  */
7313 static void
7314 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7315                        struct elf_link_hash_entry *h,
7316                        bfd_boolean force_local)
7317 {
7318   struct ppc_link_hash_entry *eh;
7319   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7320
7321   eh = (struct ppc_link_hash_entry *) h;
7322   if (eh->is_func_descriptor)
7323     {
7324       struct ppc_link_hash_entry *fh = eh->oh;
7325
7326       if (fh == NULL)
7327         {
7328           const char *p, *q;
7329           struct elf_link_hash_table *htab = elf_hash_table (info);
7330           char save;
7331
7332           /* We aren't supposed to use alloca in BFD because on
7333              systems which do not have alloca the version in libiberty
7334              calls xmalloc, which might cause the program to crash
7335              when it runs out of memory.  This function doesn't have a
7336              return status, so there's no way to gracefully return an
7337              error.  So cheat.  We know that string[-1] can be safely
7338              accessed;  It's either a string in an ELF string table,
7339              or allocated in an objalloc structure.  */
7340
7341           p = eh->elf.root.root.string - 1;
7342           save = *p;
7343           *(char *) p = '.';
7344           fh = (struct ppc_link_hash_entry *)
7345             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7346           *(char *) p = save;
7347
7348           /* Unfortunately, if it so happens that the string we were
7349              looking for was allocated immediately before this string,
7350              then we overwrote the string terminator.  That's the only
7351              reason the lookup should fail.  */
7352           if (fh == NULL)
7353             {
7354               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7355               while (q >= eh->elf.root.root.string && *q == *p)
7356                 --q, --p;
7357               if (q < eh->elf.root.root.string && *p == '.')
7358                 fh = (struct ppc_link_hash_entry *)
7359                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7360             }
7361           if (fh != NULL)
7362             {
7363               eh->oh = fh;
7364               fh->oh = eh;
7365             }
7366         }
7367       if (fh != NULL)
7368         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7369     }
7370 }
7371
7372 static bfd_boolean
7373 get_sym_h (struct elf_link_hash_entry **hp,
7374            Elf_Internal_Sym **symp,
7375            asection **symsecp,
7376            unsigned char **tls_maskp,
7377            Elf_Internal_Sym **locsymsp,
7378            unsigned long r_symndx,
7379            bfd *ibfd)
7380 {
7381   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7382
7383   if (r_symndx >= symtab_hdr->sh_info)
7384     {
7385       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7386       struct elf_link_hash_entry *h;
7387
7388       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7389       h = elf_follow_link (h);
7390
7391       if (hp != NULL)
7392         *hp = h;
7393
7394       if (symp != NULL)
7395         *symp = NULL;
7396
7397       if (symsecp != NULL)
7398         {
7399           asection *symsec = NULL;
7400           if (h->root.type == bfd_link_hash_defined
7401               || h->root.type == bfd_link_hash_defweak)
7402             symsec = h->root.u.def.section;
7403           *symsecp = symsec;
7404         }
7405
7406       if (tls_maskp != NULL)
7407         {
7408           struct ppc_link_hash_entry *eh;
7409
7410           eh = (struct ppc_link_hash_entry *) h;
7411           *tls_maskp = &eh->tls_mask;
7412         }
7413     }
7414   else
7415     {
7416       Elf_Internal_Sym *sym;
7417       Elf_Internal_Sym *locsyms = *locsymsp;
7418
7419       if (locsyms == NULL)
7420         {
7421           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7422           if (locsyms == NULL)
7423             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7424                                             symtab_hdr->sh_info,
7425                                             0, NULL, NULL, NULL);
7426           if (locsyms == NULL)
7427             return FALSE;
7428           *locsymsp = locsyms;
7429         }
7430       sym = locsyms + r_symndx;
7431
7432       if (hp != NULL)
7433         *hp = NULL;
7434
7435       if (symp != NULL)
7436         *symp = sym;
7437
7438       if (symsecp != NULL)
7439         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7440
7441       if (tls_maskp != NULL)
7442         {
7443           struct got_entry **lgot_ents;
7444           unsigned char *tls_mask;
7445
7446           tls_mask = NULL;
7447           lgot_ents = elf_local_got_ents (ibfd);
7448           if (lgot_ents != NULL)
7449             {
7450               struct plt_entry **local_plt = (struct plt_entry **)
7451                 (lgot_ents + symtab_hdr->sh_info);
7452               unsigned char *lgot_masks = (unsigned char *)
7453                 (local_plt + symtab_hdr->sh_info);
7454               tls_mask = &lgot_masks[r_symndx];
7455             }
7456           *tls_maskp = tls_mask;
7457         }
7458     }
7459   return TRUE;
7460 }
7461
7462 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7463    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7464    type suitable for optimization, and 1 otherwise.  */
7465
7466 static int
7467 get_tls_mask (unsigned char **tls_maskp,
7468               unsigned long *toc_symndx,
7469               bfd_vma *toc_addend,
7470               Elf_Internal_Sym **locsymsp,
7471               const Elf_Internal_Rela *rel,
7472               bfd *ibfd)
7473 {
7474   unsigned long r_symndx;
7475   int next_r;
7476   struct elf_link_hash_entry *h;
7477   Elf_Internal_Sym *sym;
7478   asection *sec;
7479   bfd_vma off;
7480
7481   r_symndx = ELF64_R_SYM (rel->r_info);
7482   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7483     return 0;
7484
7485   if ((*tls_maskp != NULL && **tls_maskp != 0)
7486       || sec == NULL
7487       || ppc64_elf_section_data (sec) == NULL
7488       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7489     return 1;
7490
7491   /* Look inside a TOC section too.  */
7492   if (h != NULL)
7493     {
7494       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7495       off = h->root.u.def.value;
7496     }
7497   else
7498     off = sym->st_value;
7499   off += rel->r_addend;
7500   BFD_ASSERT (off % 8 == 0);
7501   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7502   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7503   if (toc_symndx != NULL)
7504     *toc_symndx = r_symndx;
7505   if (toc_addend != NULL)
7506     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7507   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7508     return 0;
7509   if ((h == NULL || is_static_defined (h))
7510       && (next_r == -1 || next_r == -2))
7511     return 1 - next_r;
7512   return 1;
7513 }
7514
7515 /* Find (or create) an entry in the tocsave hash table.  */
7516
7517 static struct tocsave_entry *
7518 tocsave_find (struct ppc_link_hash_table *htab,
7519               enum insert_option insert,
7520               Elf_Internal_Sym **local_syms,
7521               const Elf_Internal_Rela *irela,
7522               bfd *ibfd)
7523 {
7524   unsigned long r_indx;
7525   struct elf_link_hash_entry *h;
7526   Elf_Internal_Sym *sym;
7527   struct tocsave_entry ent, *p;
7528   hashval_t hash;
7529   struct tocsave_entry **slot;
7530
7531   r_indx = ELF64_R_SYM (irela->r_info);
7532   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7533     return NULL;
7534   if (ent.sec == NULL || ent.sec->output_section == NULL)
7535     {
7536       _bfd_error_handler
7537         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7538       return NULL;
7539     }
7540
7541   if (h != NULL)
7542     ent.offset = h->root.u.def.value;
7543   else
7544     ent.offset = sym->st_value;
7545   ent.offset += irela->r_addend;
7546
7547   hash = tocsave_htab_hash (&ent);
7548   slot = ((struct tocsave_entry **)
7549           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7550   if (slot == NULL)
7551     return NULL;
7552
7553   if (*slot == NULL)
7554     {
7555       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7556       if (p == NULL)
7557         return NULL;
7558       *p = ent;
7559       *slot = p;
7560     }
7561   return *slot;
7562 }
7563
7564 /* Adjust all global syms defined in opd sections.  In gcc generated
7565    code for the old ABI, these will already have been done.  */
7566
7567 static bfd_boolean
7568 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7569 {
7570   struct ppc_link_hash_entry *eh;
7571   asection *sym_sec;
7572   struct _opd_sec_data *opd;
7573
7574   if (h->root.type == bfd_link_hash_indirect)
7575     return TRUE;
7576
7577   if (h->root.type != bfd_link_hash_defined
7578       && h->root.type != bfd_link_hash_defweak)
7579     return TRUE;
7580
7581   eh = (struct ppc_link_hash_entry *) h;
7582   if (eh->adjust_done)
7583     return TRUE;
7584
7585   sym_sec = eh->elf.root.u.def.section;
7586   opd = get_opd_info (sym_sec);
7587   if (opd != NULL && opd->adjust != NULL)
7588     {
7589       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7590       if (adjust == -1)
7591         {
7592           /* This entry has been deleted.  */
7593           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7594           if (dsec == NULL)
7595             {
7596               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7597                 if (discarded_section (dsec))
7598                   {
7599                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7600                     break;
7601                   }
7602             }
7603           eh->elf.root.u.def.value = 0;
7604           eh->elf.root.u.def.section = dsec;
7605         }
7606       else
7607         eh->elf.root.u.def.value += adjust;
7608       eh->adjust_done = 1;
7609     }
7610   return TRUE;
7611 }
7612
7613 /* Handles decrementing dynamic reloc counts for the reloc specified by
7614    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7615    have already been determined.  */
7616
7617 static bfd_boolean
7618 dec_dynrel_count (bfd_vma r_info,
7619                   asection *sec,
7620                   struct bfd_link_info *info,
7621                   Elf_Internal_Sym **local_syms,
7622                   struct elf_link_hash_entry *h,
7623                   Elf_Internal_Sym *sym)
7624 {
7625   enum elf_ppc64_reloc_type r_type;
7626   asection *sym_sec = NULL;
7627
7628   /* Can this reloc be dynamic?  This switch, and later tests here
7629      should be kept in sync with the code in check_relocs.  */
7630   r_type = ELF64_R_TYPE (r_info);
7631   switch (r_type)
7632     {
7633     default:
7634       return TRUE;
7635
7636     case R_PPC64_TPREL16:
7637     case R_PPC64_TPREL16_LO:
7638     case R_PPC64_TPREL16_HI:
7639     case R_PPC64_TPREL16_HA:
7640     case R_PPC64_TPREL16_DS:
7641     case R_PPC64_TPREL16_LO_DS:
7642     case R_PPC64_TPREL16_HIGH:
7643     case R_PPC64_TPREL16_HIGHA:
7644     case R_PPC64_TPREL16_HIGHER:
7645     case R_PPC64_TPREL16_HIGHERA:
7646     case R_PPC64_TPREL16_HIGHEST:
7647     case R_PPC64_TPREL16_HIGHESTA:
7648     case R_PPC64_TPREL64:
7649     case R_PPC64_DTPMOD64:
7650     case R_PPC64_DTPREL64:
7651     case R_PPC64_ADDR64:
7652     case R_PPC64_REL30:
7653     case R_PPC64_REL32:
7654     case R_PPC64_REL64:
7655     case R_PPC64_ADDR14:
7656     case R_PPC64_ADDR14_BRNTAKEN:
7657     case R_PPC64_ADDR14_BRTAKEN:
7658     case R_PPC64_ADDR16:
7659     case R_PPC64_ADDR16_DS:
7660     case R_PPC64_ADDR16_HA:
7661     case R_PPC64_ADDR16_HI:
7662     case R_PPC64_ADDR16_HIGH:
7663     case R_PPC64_ADDR16_HIGHA:
7664     case R_PPC64_ADDR16_HIGHER:
7665     case R_PPC64_ADDR16_HIGHERA:
7666     case R_PPC64_ADDR16_HIGHEST:
7667     case R_PPC64_ADDR16_HIGHESTA:
7668     case R_PPC64_ADDR16_LO:
7669     case R_PPC64_ADDR16_LO_DS:
7670     case R_PPC64_ADDR24:
7671     case R_PPC64_ADDR32:
7672     case R_PPC64_UADDR16:
7673     case R_PPC64_UADDR32:
7674     case R_PPC64_UADDR64:
7675     case R_PPC64_TOC:
7676       break;
7677     }
7678
7679   if (local_syms != NULL)
7680     {
7681       unsigned long r_symndx;
7682       bfd *ibfd = sec->owner;
7683
7684       r_symndx = ELF64_R_SYM (r_info);
7685       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7686         return FALSE;
7687     }
7688
7689   if ((bfd_link_pic (info)
7690        && (must_be_dyn_reloc (info, r_type)
7691            || (h != NULL
7692                && (!SYMBOLIC_BIND (info, h)
7693                    || h->root.type == bfd_link_hash_defweak
7694                    || !h->def_regular))))
7695       || (ELIMINATE_COPY_RELOCS
7696           && !bfd_link_pic (info)
7697           && h != NULL
7698           && (h->root.type == bfd_link_hash_defweak
7699               || !h->def_regular)))
7700     ;
7701   else
7702     return TRUE;
7703
7704   if (h != NULL)
7705     {
7706       struct elf_dyn_relocs *p;
7707       struct elf_dyn_relocs **pp;
7708       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7709
7710       /* elf_gc_sweep may have already removed all dyn relocs associated
7711          with local syms for a given section.  Also, symbol flags are
7712          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7713          report a dynreloc miscount.  */
7714       if (*pp == NULL && info->gc_sections)
7715         return TRUE;
7716
7717       while ((p = *pp) != NULL)
7718         {
7719           if (p->sec == sec)
7720             {
7721               if (!must_be_dyn_reloc (info, r_type))
7722                 p->pc_count -= 1;
7723               p->count -= 1;
7724               if (p->count == 0)
7725                 *pp = p->next;
7726               return TRUE;
7727             }
7728           pp = &p->next;
7729         }
7730     }
7731   else
7732     {
7733       struct ppc_dyn_relocs *p;
7734       struct ppc_dyn_relocs **pp;
7735       void *vpp;
7736       bfd_boolean is_ifunc;
7737
7738       if (local_syms == NULL)
7739         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7740       if (sym_sec == NULL)
7741         sym_sec = sec;
7742
7743       vpp = &elf_section_data (sym_sec)->local_dynrel;
7744       pp = (struct ppc_dyn_relocs **) vpp;
7745
7746       if (*pp == NULL && info->gc_sections)
7747         return TRUE;
7748
7749       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7750       while ((p = *pp) != NULL)
7751         {
7752           if (p->sec == sec && p->ifunc == is_ifunc)
7753             {
7754               p->count -= 1;
7755               if (p->count == 0)
7756                 *pp = p->next;
7757               return TRUE;
7758             }
7759           pp = &p->next;
7760         }
7761     }
7762
7763   /* xgettext:c-format */
7764   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7765                           sec->owner, sec);
7766   bfd_set_error (bfd_error_bad_value);
7767   return FALSE;
7768 }
7769
7770 /* Remove unused Official Procedure Descriptor entries.  Currently we
7771    only remove those associated with functions in discarded link-once
7772    sections, or weakly defined functions that have been overridden.  It
7773    would be possible to remove many more entries for statically linked
7774    applications.  */
7775
7776 bfd_boolean
7777 ppc64_elf_edit_opd (struct bfd_link_info *info)
7778 {
7779   bfd *ibfd;
7780   bfd_boolean some_edited = FALSE;
7781   asection *need_pad = NULL;
7782   struct ppc_link_hash_table *htab;
7783
7784   htab = ppc_hash_table (info);
7785   if (htab == NULL)
7786     return FALSE;
7787
7788   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7789     {
7790       asection *sec;
7791       Elf_Internal_Rela *relstart, *rel, *relend;
7792       Elf_Internal_Shdr *symtab_hdr;
7793       Elf_Internal_Sym *local_syms;
7794       struct _opd_sec_data *opd;
7795       bfd_boolean need_edit, add_aux_fields, broken;
7796       bfd_size_type cnt_16b = 0;
7797
7798       if (!is_ppc64_elf (ibfd))
7799         continue;
7800
7801       sec = bfd_get_section_by_name (ibfd, ".opd");
7802       if (sec == NULL || sec->size == 0)
7803         continue;
7804
7805       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7806         continue;
7807
7808       if (sec->output_section == bfd_abs_section_ptr)
7809         continue;
7810
7811       /* Look through the section relocs.  */
7812       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7813         continue;
7814
7815       local_syms = NULL;
7816       symtab_hdr = &elf_symtab_hdr (ibfd);
7817
7818       /* Read the relocations.  */
7819       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7820                                             info->keep_memory);
7821       if (relstart == NULL)
7822         return FALSE;
7823
7824       /* First run through the relocs to check they are sane, and to
7825          determine whether we need to edit this opd section.  */
7826       need_edit = FALSE;
7827       broken = FALSE;
7828       need_pad = sec;
7829       relend = relstart + sec->reloc_count;
7830       for (rel = relstart; rel < relend; )
7831         {
7832           enum elf_ppc64_reloc_type r_type;
7833           unsigned long r_symndx;
7834           asection *sym_sec;
7835           struct elf_link_hash_entry *h;
7836           Elf_Internal_Sym *sym;
7837           bfd_vma offset;
7838
7839           /* .opd contains an array of 16 or 24 byte entries.  We're
7840              only interested in the reloc pointing to a function entry
7841              point.  */
7842           offset = rel->r_offset;
7843           if (rel + 1 == relend
7844               || rel[1].r_offset != offset + 8)
7845             {
7846               /* If someone messes with .opd alignment then after a
7847                  "ld -r" we might have padding in the middle of .opd.
7848                  Also, there's nothing to prevent someone putting
7849                  something silly in .opd with the assembler.  No .opd
7850                  optimization for them!  */
7851             broken_opd:
7852               _bfd_error_handler
7853                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7854               broken = TRUE;
7855               break;
7856             }
7857
7858           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7859               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7860             {
7861               _bfd_error_handler
7862                 /* xgettext:c-format */
7863                 (_("%B: unexpected reloc type %u in .opd section"),
7864                  ibfd, r_type);
7865               broken = TRUE;
7866               break;
7867             }
7868
7869           r_symndx = ELF64_R_SYM (rel->r_info);
7870           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7871                           r_symndx, ibfd))
7872             goto error_ret;
7873
7874           if (sym_sec == NULL || sym_sec->owner == NULL)
7875             {
7876               const char *sym_name;
7877               if (h != NULL)
7878                 sym_name = h->root.root.string;
7879               else
7880                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7881                                              sym_sec);
7882
7883               _bfd_error_handler
7884                 /* xgettext:c-format */
7885                 (_("%B: undefined sym `%s' in .opd section"),
7886                  ibfd, sym_name);
7887               broken = TRUE;
7888               break;
7889             }
7890
7891           /* opd entries are always for functions defined in the
7892              current input bfd.  If the symbol isn't defined in the
7893              input bfd, then we won't be using the function in this
7894              bfd;  It must be defined in a linkonce section in another
7895              bfd, or is weak.  It's also possible that we are
7896              discarding the function due to a linker script /DISCARD/,
7897              which we test for via the output_section.  */
7898           if (sym_sec->owner != ibfd
7899               || sym_sec->output_section == bfd_abs_section_ptr)
7900             need_edit = TRUE;
7901
7902           rel += 2;
7903           if (rel + 1 == relend
7904               || (rel + 2 < relend
7905                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7906             ++rel;
7907
7908           if (rel == relend)
7909             {
7910               if (sec->size == offset + 24)
7911                 {
7912                   need_pad = NULL;
7913                   break;
7914                 }
7915               if (sec->size == offset + 16)
7916                 {
7917                   cnt_16b++;
7918                   break;
7919                 }
7920               goto broken_opd;
7921             }
7922           else if (rel + 1 < relend
7923                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7924                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7925             {
7926               if (rel[0].r_offset == offset + 16)
7927                 cnt_16b++;
7928               else if (rel[0].r_offset != offset + 24)
7929                 goto broken_opd;
7930             }
7931           else
7932             goto broken_opd;
7933         }
7934
7935       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7936
7937       if (!broken && (need_edit || add_aux_fields))
7938         {
7939           Elf_Internal_Rela *write_rel;
7940           Elf_Internal_Shdr *rel_hdr;
7941           bfd_byte *rptr, *wptr;
7942           bfd_byte *new_contents;
7943           bfd_size_type amt;
7944
7945           new_contents = NULL;
7946           amt = OPD_NDX (sec->size) * sizeof (long);
7947           opd = &ppc64_elf_section_data (sec)->u.opd;
7948           opd->adjust = bfd_zalloc (sec->owner, amt);
7949           if (opd->adjust == NULL)
7950             return FALSE;
7951
7952           /* This seems a waste of time as input .opd sections are all
7953              zeros as generated by gcc, but I suppose there's no reason
7954              this will always be so.  We might start putting something in
7955              the third word of .opd entries.  */
7956           if ((sec->flags & SEC_IN_MEMORY) == 0)
7957             {
7958               bfd_byte *loc;
7959               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7960                 {
7961                   if (loc != NULL)
7962                     free (loc);
7963                 error_ret:
7964                   if (local_syms != NULL
7965                       && symtab_hdr->contents != (unsigned char *) local_syms)
7966                     free (local_syms);
7967                   if (elf_section_data (sec)->relocs != relstart)
7968                     free (relstart);
7969                   return FALSE;
7970                 }
7971               sec->contents = loc;
7972               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7973             }
7974
7975           elf_section_data (sec)->relocs = relstart;
7976
7977           new_contents = sec->contents;
7978           if (add_aux_fields)
7979             {
7980               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7981               if (new_contents == NULL)
7982                 return FALSE;
7983               need_pad = NULL;
7984             }
7985           wptr = new_contents;
7986           rptr = sec->contents;
7987           write_rel = relstart;
7988           for (rel = relstart; rel < relend; )
7989             {
7990               unsigned long r_symndx;
7991               asection *sym_sec;
7992               struct elf_link_hash_entry *h;
7993               struct ppc_link_hash_entry *fdh = NULL;
7994               Elf_Internal_Sym *sym;
7995               long opd_ent_size;
7996               Elf_Internal_Rela *next_rel;
7997               bfd_boolean skip;
7998
7999               r_symndx = ELF64_R_SYM (rel->r_info);
8000               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8001                               r_symndx, ibfd))
8002                 goto error_ret;
8003
8004               next_rel = rel + 2;
8005               if (next_rel + 1 == relend
8006                   || (next_rel + 2 < relend
8007                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8008                 ++next_rel;
8009
8010               /* See if the .opd entry is full 24 byte or
8011                  16 byte (with fd_aux entry overlapped with next
8012                  fd_func).  */
8013               opd_ent_size = 24;
8014               if (next_rel == relend)
8015                 {
8016                   if (sec->size == rel->r_offset + 16)
8017                     opd_ent_size = 16;
8018                 }
8019               else if (next_rel->r_offset == rel->r_offset + 16)
8020                 opd_ent_size = 16;
8021
8022               if (h != NULL
8023                   && h->root.root.string[0] == '.')
8024                 {
8025                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
8026                   if (fdh != NULL)
8027                     {
8028                       fdh = ppc_follow_link (fdh);
8029                       if (fdh->elf.root.type != bfd_link_hash_defined
8030                           && fdh->elf.root.type != bfd_link_hash_defweak)
8031                         fdh = NULL;
8032                     }
8033                 }
8034
8035               skip = (sym_sec->owner != ibfd
8036                       || sym_sec->output_section == bfd_abs_section_ptr);
8037               if (skip)
8038                 {
8039                   if (fdh != NULL && sym_sec->owner == ibfd)
8040                     {
8041                       /* Arrange for the function descriptor sym
8042                          to be dropped.  */
8043                       fdh->elf.root.u.def.value = 0;
8044                       fdh->elf.root.u.def.section = sym_sec;
8045                     }
8046                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8047
8048                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8049                     rel = next_rel;
8050                   else
8051                     while (1)
8052                       {
8053                         if (!dec_dynrel_count (rel->r_info, sec, info,
8054                                                NULL, h, sym))
8055                           goto error_ret;
8056
8057                         if (++rel == next_rel)
8058                           break;
8059
8060                         r_symndx = ELF64_R_SYM (rel->r_info);
8061                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8062                                         r_symndx, ibfd))
8063                           goto error_ret;
8064                       }
8065                 }
8066               else
8067                 {
8068                   /* We'll be keeping this opd entry.  */
8069                   long adjust;
8070
8071                   if (fdh != NULL)
8072                     {
8073                       /* Redefine the function descriptor symbol to
8074                          this location in the opd section.  It is
8075                          necessary to update the value here rather
8076                          than using an array of adjustments as we do
8077                          for local symbols, because various places
8078                          in the generic ELF code use the value
8079                          stored in u.def.value.  */
8080                       fdh->elf.root.u.def.value = wptr - new_contents;
8081                       fdh->adjust_done = 1;
8082                     }
8083
8084                   /* Local syms are a bit tricky.  We could
8085                      tweak them as they can be cached, but
8086                      we'd need to look through the local syms
8087                      for the function descriptor sym which we
8088                      don't have at the moment.  So keep an
8089                      array of adjustments.  */
8090                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8091                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8092
8093                   if (wptr != rptr)
8094                     memcpy (wptr, rptr, opd_ent_size);
8095                   wptr += opd_ent_size;
8096                   if (add_aux_fields && opd_ent_size == 16)
8097                     {
8098                       memset (wptr, '\0', 8);
8099                       wptr += 8;
8100                     }
8101
8102                   /* We need to adjust any reloc offsets to point to the
8103                      new opd entries.  */
8104                   for ( ; rel != next_rel; ++rel)
8105                     {
8106                       rel->r_offset += adjust;
8107                       if (write_rel != rel)
8108                         memcpy (write_rel, rel, sizeof (*rel));
8109                       ++write_rel;
8110                     }
8111                 }
8112
8113               rptr += opd_ent_size;
8114             }
8115
8116           sec->size = wptr - new_contents;
8117           sec->reloc_count = write_rel - relstart;
8118           if (add_aux_fields)
8119             {
8120               free (sec->contents);
8121               sec->contents = new_contents;
8122             }
8123
8124           /* Fudge the header size too, as this is used later in
8125              elf_bfd_final_link if we are emitting relocs.  */
8126           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8127           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8128           some_edited = TRUE;
8129         }
8130       else if (elf_section_data (sec)->relocs != relstart)
8131         free (relstart);
8132
8133       if (local_syms != NULL
8134           && symtab_hdr->contents != (unsigned char *) local_syms)
8135         {
8136           if (!info->keep_memory)
8137             free (local_syms);
8138           else
8139             symtab_hdr->contents = (unsigned char *) local_syms;
8140         }
8141     }
8142
8143   if (some_edited)
8144     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8145
8146   /* If we are doing a final link and the last .opd entry is just 16 byte
8147      long, add a 8 byte padding after it.  */
8148   if (need_pad != NULL && !bfd_link_relocatable (info))
8149     {
8150       bfd_byte *p;
8151
8152       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8153         {
8154           BFD_ASSERT (need_pad->size > 0);
8155
8156           p = bfd_malloc (need_pad->size + 8);
8157           if (p == NULL)
8158             return FALSE;
8159
8160           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8161                                           p, 0, need_pad->size))
8162             return FALSE;
8163
8164           need_pad->contents = p;
8165           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8166         }
8167       else
8168         {
8169           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8170           if (p == NULL)
8171             return FALSE;
8172
8173           need_pad->contents = p;
8174         }
8175
8176       memset (need_pad->contents + need_pad->size, 0, 8);
8177       need_pad->size += 8;
8178     }
8179
8180   return TRUE;
8181 }
8182
8183 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8184
8185 asection *
8186 ppc64_elf_tls_setup (struct bfd_link_info *info)
8187 {
8188   struct ppc_link_hash_table *htab;
8189
8190   htab = ppc_hash_table (info);
8191   if (htab == NULL)
8192     return NULL;
8193
8194   if (abiversion (info->output_bfd) == 1)
8195     htab->opd_abi = 1;
8196
8197   if (htab->params->no_multi_toc)
8198     htab->do_multi_toc = 0;
8199   else if (!htab->do_multi_toc)
8200     htab->params->no_multi_toc = 1;
8201
8202   /* Default to --no-plt-localentry, as this option can cause problems
8203      with symbol interposition.  For example, glibc libpthread.so and
8204      libc.so duplicate many pthread symbols, with a fallback
8205      implementation in libc.so.  In some cases the fallback does more
8206      work than the pthread implementation.  __pthread_condattr_destroy
8207      is one such symbol: the libpthread.so implementation is
8208      localentry:0 while the libc.so implementation is localentry:8.
8209      An app that "cleverly" uses dlopen to only load necessary
8210      libraries at runtime may omit loading libpthread.so when not
8211      running multi-threaded, which then results in the libc.so
8212      fallback symbols being used and ld.so complaining.  Now there
8213      are workarounds in ld (see non_zero_localentry) to detect the
8214      pthread situation, but that may not be the only case where
8215      --plt-localentry can cause trouble.  */
8216   if (htab->params->plt_localentry0 < 0)
8217     htab->params->plt_localentry0 = 0;
8218   if (htab->params->plt_localentry0
8219       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8220                                FALSE, FALSE, FALSE) == NULL)
8221     info->callbacks->einfo
8222       (_("%P: warning: --plt-localentry is especially dangerous without "
8223          "ld.so support to detect ABI violations.\n"));
8224
8225   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8226                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8227                                               FALSE, FALSE, TRUE));
8228   /* Move dynamic linking info to the function descriptor sym.  */
8229   if (htab->tls_get_addr != NULL)
8230     func_desc_adjust (&htab->tls_get_addr->elf, info);
8231   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8232                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8233                                                  FALSE, FALSE, TRUE));
8234   if (htab->params->tls_get_addr_opt)
8235     {
8236       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8237
8238       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8239                                   FALSE, FALSE, TRUE);
8240       if (opt != NULL)
8241         func_desc_adjust (opt, info);
8242       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8243                                      FALSE, FALSE, TRUE);
8244       if (opt_fd != NULL
8245           && (opt_fd->root.type == bfd_link_hash_defined
8246               || opt_fd->root.type == bfd_link_hash_defweak))
8247         {
8248           /* If glibc supports an optimized __tls_get_addr call stub,
8249              signalled by the presence of __tls_get_addr_opt, and we'll
8250              be calling __tls_get_addr via a plt call stub, then
8251              make __tls_get_addr point to __tls_get_addr_opt.  */
8252           tga_fd = &htab->tls_get_addr_fd->elf;
8253           if (htab->elf.dynamic_sections_created
8254               && tga_fd != NULL
8255               && (tga_fd->type == STT_FUNC
8256                   || tga_fd->needs_plt)
8257               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8258                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8259             {
8260               struct plt_entry *ent;
8261
8262               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8263                 if (ent->plt.refcount > 0)
8264                   break;
8265               if (ent != NULL)
8266                 {
8267                   tga_fd->root.type = bfd_link_hash_indirect;
8268                   tga_fd->root.u.i.link = &opt_fd->root;
8269                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8270                   opt_fd->mark = 1;
8271                   if (opt_fd->dynindx != -1)
8272                     {
8273                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8274                       opt_fd->dynindx = -1;
8275                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8276                                               opt_fd->dynstr_index);
8277                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8278                         return NULL;
8279                     }
8280                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8281                   tga = &htab->tls_get_addr->elf;
8282                   if (opt != NULL && tga != NULL)
8283                     {
8284                       tga->root.type = bfd_link_hash_indirect;
8285                       tga->root.u.i.link = &opt->root;
8286                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8287                       opt->mark = 1;
8288                       _bfd_elf_link_hash_hide_symbol (info, opt,
8289                                                       tga->forced_local);
8290                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8291                     }
8292                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8293                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8294                   if (htab->tls_get_addr != NULL)
8295                     {
8296                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8297                       htab->tls_get_addr->is_func = 1;
8298                     }
8299                 }
8300             }
8301         }
8302       else if (htab->params->tls_get_addr_opt < 0)
8303         htab->params->tls_get_addr_opt = 0;
8304     }
8305   return _bfd_elf_tls_setup (info->output_bfd, info);
8306 }
8307
8308 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8309    HASH1 or HASH2.  */
8310
8311 static bfd_boolean
8312 branch_reloc_hash_match (const bfd *ibfd,
8313                          const Elf_Internal_Rela *rel,
8314                          const struct ppc_link_hash_entry *hash1,
8315                          const struct ppc_link_hash_entry *hash2)
8316 {
8317   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8318   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8319   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8320
8321   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8322     {
8323       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8324       struct elf_link_hash_entry *h;
8325
8326       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8327       h = elf_follow_link (h);
8328       if (h == &hash1->elf || h == &hash2->elf)
8329         return TRUE;
8330     }
8331   return FALSE;
8332 }
8333
8334 /* Run through all the TLS relocs looking for optimization
8335    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8336    a preliminary section layout so that we know the TLS segment
8337    offsets.  We can't optimize earlier because some optimizations need
8338    to know the tp offset, and we need to optimize before allocating
8339    dynamic relocations.  */
8340
8341 bfd_boolean
8342 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8343 {
8344   bfd *ibfd;
8345   asection *sec;
8346   struct ppc_link_hash_table *htab;
8347   unsigned char *toc_ref;
8348   int pass;
8349
8350   if (!bfd_link_executable (info))
8351     return TRUE;
8352
8353   htab = ppc_hash_table (info);
8354   if (htab == NULL)
8355     return FALSE;
8356
8357   /* Make two passes over the relocs.  On the first pass, mark toc
8358      entries involved with tls relocs, and check that tls relocs
8359      involved in setting up a tls_get_addr call are indeed followed by
8360      such a call.  If they are not, we can't do any tls optimization.
8361      On the second pass twiddle tls_mask flags to notify
8362      relocate_section that optimization can be done, and adjust got
8363      and plt refcounts.  */
8364   toc_ref = NULL;
8365   for (pass = 0; pass < 2; ++pass)
8366     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8367       {
8368         Elf_Internal_Sym *locsyms = NULL;
8369         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8370
8371         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8372           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8373             {
8374               Elf_Internal_Rela *relstart, *rel, *relend;
8375               bfd_boolean found_tls_get_addr_arg = 0;
8376
8377               /* Read the relocations.  */
8378               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8379                                                     info->keep_memory);
8380               if (relstart == NULL)
8381                 {
8382                   free (toc_ref);
8383                   return FALSE;
8384                 }
8385
8386               relend = relstart + sec->reloc_count;
8387               for (rel = relstart; rel < relend; rel++)
8388                 {
8389                   enum elf_ppc64_reloc_type r_type;
8390                   unsigned long r_symndx;
8391                   struct elf_link_hash_entry *h;
8392                   Elf_Internal_Sym *sym;
8393                   asection *sym_sec;
8394                   unsigned char *tls_mask;
8395                   unsigned char tls_set, tls_clear, tls_type = 0;
8396                   bfd_vma value;
8397                   bfd_boolean ok_tprel, is_local;
8398                   long toc_ref_index = 0;
8399                   int expecting_tls_get_addr = 0;
8400                   bfd_boolean ret = FALSE;
8401
8402                   r_symndx = ELF64_R_SYM (rel->r_info);
8403                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8404                                   r_symndx, ibfd))
8405                     {
8406                     err_free_rel:
8407                       if (elf_section_data (sec)->relocs != relstart)
8408                         free (relstart);
8409                       if (toc_ref != NULL)
8410                         free (toc_ref);
8411                       if (locsyms != NULL
8412                           && (elf_symtab_hdr (ibfd).contents
8413                               != (unsigned char *) locsyms))
8414                         free (locsyms);
8415                       return ret;
8416                     }
8417
8418                   if (h != NULL)
8419                     {
8420                       if (h->root.type == bfd_link_hash_defined
8421                           || h->root.type == bfd_link_hash_defweak)
8422                         value = h->root.u.def.value;
8423                       else if (h->root.type == bfd_link_hash_undefweak)
8424                         value = 0;
8425                       else
8426                         {
8427                           found_tls_get_addr_arg = 0;
8428                           continue;
8429                         }
8430                     }
8431                   else
8432                     /* Symbols referenced by TLS relocs must be of type
8433                        STT_TLS.  So no need for .opd local sym adjust.  */
8434                     value = sym->st_value;
8435
8436                   ok_tprel = FALSE;
8437                   is_local = FALSE;
8438                   if (h == NULL
8439                       || !h->def_dynamic)
8440                     {
8441                       is_local = TRUE;
8442                       if (h != NULL
8443                           && h->root.type == bfd_link_hash_undefweak)
8444                         ok_tprel = TRUE;
8445                       else if (sym_sec != NULL
8446                                && sym_sec->output_section != NULL)
8447                         {
8448                           value += sym_sec->output_offset;
8449                           value += sym_sec->output_section->vma;
8450                           value -= htab->elf.tls_sec->vma;
8451                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8452                                       < (bfd_vma) 1 << 32);
8453                         }
8454                     }
8455
8456                   r_type = ELF64_R_TYPE (rel->r_info);
8457                   /* If this section has old-style __tls_get_addr calls
8458                      without marker relocs, then check that each
8459                      __tls_get_addr call reloc is preceded by a reloc
8460                      that conceivably belongs to the __tls_get_addr arg
8461                      setup insn.  If we don't find matching arg setup
8462                      relocs, don't do any tls optimization.  */
8463                   if (pass == 0
8464                       && sec->has_tls_get_addr_call
8465                       && h != NULL
8466                       && (h == &htab->tls_get_addr->elf
8467                           || h == &htab->tls_get_addr_fd->elf)
8468                       && !found_tls_get_addr_arg
8469                       && is_branch_reloc (r_type))
8470                     {
8471                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8472                                                 "TLS optimization disabled\n"),
8473                                               ibfd, sec, rel->r_offset);
8474                       ret = TRUE;
8475                       goto err_free_rel;
8476                     }
8477
8478                   found_tls_get_addr_arg = 0;
8479                   switch (r_type)
8480                     {
8481                     case R_PPC64_GOT_TLSLD16:
8482                     case R_PPC64_GOT_TLSLD16_LO:
8483                       expecting_tls_get_addr = 1;
8484                       found_tls_get_addr_arg = 1;
8485                       /* Fall through.  */
8486
8487                     case R_PPC64_GOT_TLSLD16_HI:
8488                     case R_PPC64_GOT_TLSLD16_HA:
8489                       /* These relocs should never be against a symbol
8490                          defined in a shared lib.  Leave them alone if
8491                          that turns out to be the case.  */
8492                       if (!is_local)
8493                         continue;
8494
8495                       /* LD -> LE */
8496                       tls_set = 0;
8497                       tls_clear = TLS_LD;
8498                       tls_type = TLS_TLS | TLS_LD;
8499                       break;
8500
8501                     case R_PPC64_GOT_TLSGD16:
8502                     case R_PPC64_GOT_TLSGD16_LO:
8503                       expecting_tls_get_addr = 1;
8504                       found_tls_get_addr_arg = 1;
8505                       /* Fall through. */
8506
8507                     case R_PPC64_GOT_TLSGD16_HI:
8508                     case R_PPC64_GOT_TLSGD16_HA:
8509                       if (ok_tprel)
8510                         /* GD -> LE */
8511                         tls_set = 0;
8512                       else
8513                         /* GD -> IE */
8514                         tls_set = TLS_TLS | TLS_TPRELGD;
8515                       tls_clear = TLS_GD;
8516                       tls_type = TLS_TLS | TLS_GD;
8517                       break;
8518
8519                     case R_PPC64_GOT_TPREL16_DS:
8520                     case R_PPC64_GOT_TPREL16_LO_DS:
8521                     case R_PPC64_GOT_TPREL16_HI:
8522                     case R_PPC64_GOT_TPREL16_HA:
8523                       if (ok_tprel)
8524                         {
8525                           /* IE -> LE */
8526                           tls_set = 0;
8527                           tls_clear = TLS_TPREL;
8528                           tls_type = TLS_TLS | TLS_TPREL;
8529                           break;
8530                         }
8531                       continue;
8532
8533                     case R_PPC64_TLSGD:
8534                     case R_PPC64_TLSLD:
8535                       found_tls_get_addr_arg = 1;
8536                       /* Fall through.  */
8537
8538                     case R_PPC64_TLS:
8539                     case R_PPC64_TOC16:
8540                     case R_PPC64_TOC16_LO:
8541                       if (sym_sec == NULL || sym_sec != toc)
8542                         continue;
8543
8544                       /* Mark this toc entry as referenced by a TLS
8545                          code sequence.  We can do that now in the
8546                          case of R_PPC64_TLS, and after checking for
8547                          tls_get_addr for the TOC16 relocs.  */
8548                       if (toc_ref == NULL)
8549                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8550                       if (toc_ref == NULL)
8551                         goto err_free_rel;
8552
8553                       if (h != NULL)
8554                         value = h->root.u.def.value;
8555                       else
8556                         value = sym->st_value;
8557                       value += rel->r_addend;
8558                       if (value % 8 != 0)
8559                         continue;
8560                       BFD_ASSERT (value < toc->size
8561                                   && toc->output_offset % 8 == 0);
8562                       toc_ref_index = (value + toc->output_offset) / 8;
8563                       if (r_type == R_PPC64_TLS
8564                           || r_type == R_PPC64_TLSGD
8565                           || r_type == R_PPC64_TLSLD)
8566                         {
8567                           toc_ref[toc_ref_index] = 1;
8568                           continue;
8569                         }
8570
8571                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8572                         continue;
8573
8574                       tls_set = 0;
8575                       tls_clear = 0;
8576                       expecting_tls_get_addr = 2;
8577                       break;
8578
8579                     case R_PPC64_TPREL64:
8580                       if (pass == 0
8581                           || sec != toc
8582                           || toc_ref == NULL
8583                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8584                         continue;
8585                       if (ok_tprel)
8586                         {
8587                           /* IE -> LE */
8588                           tls_set = TLS_EXPLICIT;
8589                           tls_clear = TLS_TPREL;
8590                           break;
8591                         }
8592                       continue;
8593
8594                     case R_PPC64_DTPMOD64:
8595                       if (pass == 0
8596                           || sec != toc
8597                           || toc_ref == NULL
8598                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8599                         continue;
8600                       if (rel + 1 < relend
8601                           && (rel[1].r_info
8602                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8603                           && rel[1].r_offset == rel->r_offset + 8)
8604                         {
8605                           if (ok_tprel)
8606                             /* GD -> LE */
8607                             tls_set = TLS_EXPLICIT | TLS_GD;
8608                           else
8609                             /* GD -> IE */
8610                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8611                           tls_clear = TLS_GD;
8612                         }
8613                       else
8614                         {
8615                           if (!is_local)
8616                             continue;
8617
8618                           /* LD -> LE */
8619                           tls_set = TLS_EXPLICIT;
8620                           tls_clear = TLS_LD;
8621                         }
8622                       break;
8623
8624                     default:
8625                       continue;
8626                     }
8627
8628                   if (pass == 0)
8629                     {
8630                       if (!expecting_tls_get_addr
8631                           || !sec->has_tls_get_addr_call)
8632                         continue;
8633
8634                       if (rel + 1 < relend
8635                           && branch_reloc_hash_match (ibfd, rel + 1,
8636                                                       htab->tls_get_addr,
8637                                                       htab->tls_get_addr_fd))
8638                         {
8639                           if (expecting_tls_get_addr == 2)
8640                             {
8641                               /* Check for toc tls entries.  */
8642                               unsigned char *toc_tls;
8643                               int retval;
8644
8645                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8646                                                      &locsyms,
8647                                                      rel, ibfd);
8648                               if (retval == 0)
8649                                 goto err_free_rel;
8650                               if (toc_tls != NULL)
8651                                 {
8652                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8653                                     found_tls_get_addr_arg = 1;
8654                                   if (retval > 1)
8655                                     toc_ref[toc_ref_index] = 1;
8656                                 }
8657                             }
8658                           continue;
8659                         }
8660
8661                       if (expecting_tls_get_addr != 1)
8662                         continue;
8663
8664                       /* Uh oh, we didn't find the expected call.  We
8665                          could just mark this symbol to exclude it
8666                          from tls optimization but it's safer to skip
8667                          the entire optimization.  */
8668                       /* xgettext:c-format */
8669                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8670                                                 "TLS optimization disabled\n"),
8671                                               ibfd, sec, rel->r_offset);
8672                       ret = TRUE;
8673                       goto err_free_rel;
8674                     }
8675
8676                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8677                     {
8678                       struct plt_entry *ent;
8679                       for (ent = htab->tls_get_addr->elf.plt.plist;
8680                            ent != NULL;
8681                            ent = ent->next)
8682                         if (ent->addend == 0)
8683                           {
8684                             if (ent->plt.refcount > 0)
8685                               {
8686                                 ent->plt.refcount -= 1;
8687                                 expecting_tls_get_addr = 0;
8688                               }
8689                             break;
8690                           }
8691                     }
8692
8693                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8694                     {
8695                       struct plt_entry *ent;
8696                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8697                            ent != NULL;
8698                            ent = ent->next)
8699                         if (ent->addend == 0)
8700                           {
8701                             if (ent->plt.refcount > 0)
8702                               ent->plt.refcount -= 1;
8703                             break;
8704                           }
8705                     }
8706
8707                   if (tls_clear == 0)
8708                     continue;
8709
8710                   if ((tls_set & TLS_EXPLICIT) == 0)
8711                     {
8712                       struct got_entry *ent;
8713
8714                       /* Adjust got entry for this reloc.  */
8715                       if (h != NULL)
8716                         ent = h->got.glist;
8717                       else
8718                         ent = elf_local_got_ents (ibfd)[r_symndx];
8719
8720                       for (; ent != NULL; ent = ent->next)
8721                         if (ent->addend == rel->r_addend
8722                             && ent->owner == ibfd
8723                             && ent->tls_type == tls_type)
8724                           break;
8725                       if (ent == NULL)
8726                         abort ();
8727
8728                       if (tls_set == 0)
8729                         {
8730                           /* We managed to get rid of a got entry.  */
8731                           if (ent->got.refcount > 0)
8732                             ent->got.refcount -= 1;
8733                         }
8734                     }
8735                   else
8736                     {
8737                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8738                          we'll lose one or two dyn relocs.  */
8739                       if (!dec_dynrel_count (rel->r_info, sec, info,
8740                                              NULL, h, sym))
8741                         return FALSE;
8742
8743                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8744                         {
8745                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8746                                                  NULL, h, sym))
8747                             return FALSE;
8748                         }
8749                     }
8750
8751                   *tls_mask |= tls_set;
8752                   *tls_mask &= ~tls_clear;
8753                 }
8754
8755               if (elf_section_data (sec)->relocs != relstart)
8756                 free (relstart);
8757             }
8758
8759         if (locsyms != NULL
8760             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8761           {
8762             if (!info->keep_memory)
8763               free (locsyms);
8764             else
8765               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8766           }
8767       }
8768
8769   if (toc_ref != NULL)
8770     free (toc_ref);
8771   htab->do_tls_opt = 1;
8772   return TRUE;
8773 }
8774
8775 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8776    the values of any global symbols in a toc section that has been
8777    edited.  Globals in toc sections should be a rarity, so this function
8778    sets a flag if any are found in toc sections other than the one just
8779    edited, so that further hash table traversals can be avoided.  */
8780
8781 struct adjust_toc_info
8782 {
8783   asection *toc;
8784   unsigned long *skip;
8785   bfd_boolean global_toc_syms;
8786 };
8787
8788 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8789
8790 static bfd_boolean
8791 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8792 {
8793   struct ppc_link_hash_entry *eh;
8794   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8795   unsigned long i;
8796
8797   if (h->root.type != bfd_link_hash_defined
8798       && h->root.type != bfd_link_hash_defweak)
8799     return TRUE;
8800
8801   eh = (struct ppc_link_hash_entry *) h;
8802   if (eh->adjust_done)
8803     return TRUE;
8804
8805   if (eh->elf.root.u.def.section == toc_inf->toc)
8806     {
8807       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8808         i = toc_inf->toc->rawsize >> 3;
8809       else
8810         i = eh->elf.root.u.def.value >> 3;
8811
8812       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8813         {
8814           _bfd_error_handler
8815             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8816           do
8817             ++i;
8818           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8819           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8820         }
8821
8822       eh->elf.root.u.def.value -= toc_inf->skip[i];
8823       eh->adjust_done = 1;
8824     }
8825   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8826     toc_inf->global_toc_syms = TRUE;
8827
8828   return TRUE;
8829 }
8830
8831 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8832    on a _LO variety toc/got reloc.  */
8833
8834 static bfd_boolean
8835 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8836 {
8837   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8838           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8839           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8840           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8841           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8842           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8843           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8844           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8845           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8846           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8847           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8848           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8849           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8850           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8851           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8852           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8853           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8854               /* Exclude lfqu by testing reloc.  If relocs are ever
8855                  defined for the reduced D field in psq_lu then those
8856                  will need testing too.  */
8857               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8858           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8859               && (insn & 1) == 0)
8860           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8861           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8862               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8863               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8864           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8865               && (insn & 1) == 0));
8866 }
8867
8868 /* Examine all relocs referencing .toc sections in order to remove
8869    unused .toc entries.  */
8870
8871 bfd_boolean
8872 ppc64_elf_edit_toc (struct bfd_link_info *info)
8873 {
8874   bfd *ibfd;
8875   struct adjust_toc_info toc_inf;
8876   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8877
8878   htab->do_toc_opt = 1;
8879   toc_inf.global_toc_syms = TRUE;
8880   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8881     {
8882       asection *toc, *sec;
8883       Elf_Internal_Shdr *symtab_hdr;
8884       Elf_Internal_Sym *local_syms;
8885       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8886       unsigned long *skip, *drop;
8887       unsigned char *used;
8888       unsigned char *keep, last, some_unused;
8889
8890       if (!is_ppc64_elf (ibfd))
8891         continue;
8892
8893       toc = bfd_get_section_by_name (ibfd, ".toc");
8894       if (toc == NULL
8895           || toc->size == 0
8896           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8897           || discarded_section (toc))
8898         continue;
8899
8900       toc_relocs = NULL;
8901       local_syms = NULL;
8902       symtab_hdr = &elf_symtab_hdr (ibfd);
8903
8904       /* Look at sections dropped from the final link.  */
8905       skip = NULL;
8906       relstart = NULL;
8907       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8908         {
8909           if (sec->reloc_count == 0
8910               || !discarded_section (sec)
8911               || get_opd_info (sec)
8912               || (sec->flags & SEC_ALLOC) == 0
8913               || (sec->flags & SEC_DEBUGGING) != 0)
8914             continue;
8915
8916           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8917           if (relstart == NULL)
8918             goto error_ret;
8919
8920           /* Run through the relocs to see which toc entries might be
8921              unused.  */
8922           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8923             {
8924               enum elf_ppc64_reloc_type r_type;
8925               unsigned long r_symndx;
8926               asection *sym_sec;
8927               struct elf_link_hash_entry *h;
8928               Elf_Internal_Sym *sym;
8929               bfd_vma val;
8930
8931               r_type = ELF64_R_TYPE (rel->r_info);
8932               switch (r_type)
8933                 {
8934                 default:
8935                   continue;
8936
8937                 case R_PPC64_TOC16:
8938                 case R_PPC64_TOC16_LO:
8939                 case R_PPC64_TOC16_HI:
8940                 case R_PPC64_TOC16_HA:
8941                 case R_PPC64_TOC16_DS:
8942                 case R_PPC64_TOC16_LO_DS:
8943                   break;
8944                 }
8945
8946               r_symndx = ELF64_R_SYM (rel->r_info);
8947               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8948                               r_symndx, ibfd))
8949                 goto error_ret;
8950
8951               if (sym_sec != toc)
8952                 continue;
8953
8954               if (h != NULL)
8955                 val = h->root.u.def.value;
8956               else
8957                 val = sym->st_value;
8958               val += rel->r_addend;
8959
8960               if (val >= toc->size)
8961                 continue;
8962
8963               /* Anything in the toc ought to be aligned to 8 bytes.
8964                  If not, don't mark as unused.  */
8965               if (val & 7)
8966                 continue;
8967
8968               if (skip == NULL)
8969                 {
8970                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8971                   if (skip == NULL)
8972                     goto error_ret;
8973                 }
8974
8975               skip[val >> 3] = ref_from_discarded;
8976             }
8977
8978           if (elf_section_data (sec)->relocs != relstart)
8979             free (relstart);
8980         }
8981
8982       /* For largetoc loads of address constants, we can convert
8983          .  addis rx,2,addr@got@ha
8984          .  ld ry,addr@got@l(rx)
8985          to
8986          .  addis rx,2,addr@toc@ha
8987          .  addi ry,rx,addr@toc@l
8988          when addr is within 2G of the toc pointer.  This then means
8989          that the word storing "addr" in the toc is no longer needed.  */
8990
8991       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8992           && toc->output_section->rawsize < (bfd_vma) 1 << 31
8993           && toc->reloc_count != 0)
8994         {
8995           /* Read toc relocs.  */
8996           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8997                                                   info->keep_memory);
8998           if (toc_relocs == NULL)
8999             goto error_ret;
9000
9001           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9002             {
9003               enum elf_ppc64_reloc_type r_type;
9004               unsigned long r_symndx;
9005               asection *sym_sec;
9006               struct elf_link_hash_entry *h;
9007               Elf_Internal_Sym *sym;
9008               bfd_vma val, addr;
9009
9010               r_type = ELF64_R_TYPE (rel->r_info);
9011               if (r_type != R_PPC64_ADDR64)
9012                 continue;
9013
9014               r_symndx = ELF64_R_SYM (rel->r_info);
9015               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9016                               r_symndx, ibfd))
9017                 goto error_ret;
9018
9019               if (sym_sec == NULL
9020                   || sym_sec->output_section == NULL
9021                   || discarded_section (sym_sec))
9022                 continue;
9023
9024               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9025                 continue;
9026
9027               if (h != NULL)
9028                 {
9029                   if (h->type == STT_GNU_IFUNC)
9030                     continue;
9031                   val = h->root.u.def.value;
9032                 }
9033               else
9034                 {
9035                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9036                     continue;
9037                   val = sym->st_value;
9038                 }
9039               val += rel->r_addend;
9040               val += sym_sec->output_section->vma + sym_sec->output_offset;
9041
9042               /* We don't yet know the exact toc pointer value, but we
9043                  know it will be somewhere in the toc section.  Don't
9044                  optimize if the difference from any possible toc
9045                  pointer is outside [ff..f80008000, 7fff7fff].  */
9046               addr = toc->output_section->vma + TOC_BASE_OFF;
9047               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9048                 continue;
9049
9050               addr = toc->output_section->vma + toc->output_section->rawsize;
9051               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9052                 continue;
9053
9054               if (skip == NULL)
9055                 {
9056                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9057                   if (skip == NULL)
9058                     goto error_ret;
9059                 }
9060
9061               skip[rel->r_offset >> 3]
9062                 |= can_optimize | ((rel - toc_relocs) << 2);
9063             }
9064         }
9065
9066       if (skip == NULL)
9067         continue;
9068
9069       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9070       if (used == NULL)
9071         {
9072         error_ret:
9073           if (local_syms != NULL
9074               && symtab_hdr->contents != (unsigned char *) local_syms)
9075             free (local_syms);
9076           if (sec != NULL
9077               && relstart != NULL
9078               && elf_section_data (sec)->relocs != relstart)
9079             free (relstart);
9080           if (toc_relocs != NULL
9081               && elf_section_data (toc)->relocs != toc_relocs)
9082             free (toc_relocs);
9083           if (skip != NULL)
9084             free (skip);
9085           return FALSE;
9086         }
9087
9088       /* Now check all kept sections that might reference the toc.
9089          Check the toc itself last.  */
9090       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9091                   : ibfd->sections);
9092            sec != NULL;
9093            sec = (sec == toc ? NULL
9094                   : sec->next == NULL ? toc
9095                   : sec->next == toc && toc->next ? toc->next
9096                   : sec->next))
9097         {
9098           int repeat;
9099
9100           if (sec->reloc_count == 0
9101               || discarded_section (sec)
9102               || get_opd_info (sec)
9103               || (sec->flags & SEC_ALLOC) == 0
9104               || (sec->flags & SEC_DEBUGGING) != 0)
9105             continue;
9106
9107           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9108                                                 info->keep_memory);
9109           if (relstart == NULL)
9110             {
9111               free (used);
9112               goto error_ret;
9113             }
9114
9115           /* Mark toc entries referenced as used.  */
9116           do
9117             {
9118               repeat = 0;
9119               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9120                 {
9121                   enum elf_ppc64_reloc_type r_type;
9122                   unsigned long r_symndx;
9123                   asection *sym_sec;
9124                   struct elf_link_hash_entry *h;
9125                   Elf_Internal_Sym *sym;
9126                   bfd_vma val;
9127                   enum {no_check, check_lo, check_ha} insn_check;
9128
9129                   r_type = ELF64_R_TYPE (rel->r_info);
9130                   switch (r_type)
9131                     {
9132                     default:
9133                       insn_check = no_check;
9134                       break;
9135
9136                     case R_PPC64_GOT_TLSLD16_HA:
9137                     case R_PPC64_GOT_TLSGD16_HA:
9138                     case R_PPC64_GOT_TPREL16_HA:
9139                     case R_PPC64_GOT_DTPREL16_HA:
9140                     case R_PPC64_GOT16_HA:
9141                     case R_PPC64_TOC16_HA:
9142                       insn_check = check_ha;
9143                       break;
9144
9145                     case R_PPC64_GOT_TLSLD16_LO:
9146                     case R_PPC64_GOT_TLSGD16_LO:
9147                     case R_PPC64_GOT_TPREL16_LO_DS:
9148                     case R_PPC64_GOT_DTPREL16_LO_DS:
9149                     case R_PPC64_GOT16_LO:
9150                     case R_PPC64_GOT16_LO_DS:
9151                     case R_PPC64_TOC16_LO:
9152                     case R_PPC64_TOC16_LO_DS:
9153                       insn_check = check_lo;
9154                       break;
9155                     }
9156
9157                   if (insn_check != no_check)
9158                     {
9159                       bfd_vma off = rel->r_offset & ~3;
9160                       unsigned char buf[4];
9161                       unsigned int insn;
9162
9163                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9164                         {
9165                           free (used);
9166                           goto error_ret;
9167                         }
9168                       insn = bfd_get_32 (ibfd, buf);
9169                       if (insn_check == check_lo
9170                           ? !ok_lo_toc_insn (insn, r_type)
9171                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9172                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9173                         {
9174                           char str[12];
9175
9176                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9177                           sprintf (str, "%#08x", insn);
9178                           info->callbacks->einfo
9179                             /* xgettext:c-format */
9180                             (_("%H: toc optimization is not supported for"
9181                                " %s instruction.\n"),
9182                              ibfd, sec, rel->r_offset & ~3, str);
9183                         }
9184                     }
9185
9186                   switch (r_type)
9187                     {
9188                     case R_PPC64_TOC16:
9189                     case R_PPC64_TOC16_LO:
9190                     case R_PPC64_TOC16_HI:
9191                     case R_PPC64_TOC16_HA:
9192                     case R_PPC64_TOC16_DS:
9193                     case R_PPC64_TOC16_LO_DS:
9194                       /* In case we're taking addresses of toc entries.  */
9195                     case R_PPC64_ADDR64:
9196                       break;
9197
9198                     default:
9199                       continue;
9200                     }
9201
9202                   r_symndx = ELF64_R_SYM (rel->r_info);
9203                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9204                                   r_symndx, ibfd))
9205                     {
9206                       free (used);
9207                       goto error_ret;
9208                     }
9209
9210                   if (sym_sec != toc)
9211                     continue;
9212
9213                   if (h != NULL)
9214                     val = h->root.u.def.value;
9215                   else
9216                     val = sym->st_value;
9217                   val += rel->r_addend;
9218
9219                   if (val >= toc->size)
9220                     continue;
9221
9222                   if ((skip[val >> 3] & can_optimize) != 0)
9223                     {
9224                       bfd_vma off;
9225                       unsigned char opc;
9226
9227                       switch (r_type)
9228                         {
9229                         case R_PPC64_TOC16_HA:
9230                           break;
9231
9232                         case R_PPC64_TOC16_LO_DS:
9233                           off = rel->r_offset;
9234                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9235                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9236                                                          off, 1))
9237                             {
9238                               free (used);
9239                               goto error_ret;
9240                             }
9241                           if ((opc & (0x3f << 2)) == (58u << 2))
9242                             break;
9243                           /* Fall through.  */
9244
9245                         default:
9246                           /* Wrong sort of reloc, or not a ld.  We may
9247                              as well clear ref_from_discarded too.  */
9248                           skip[val >> 3] = 0;
9249                         }
9250                     }
9251
9252                   if (sec != toc)
9253                     used[val >> 3] = 1;
9254                   /* For the toc section, we only mark as used if this
9255                      entry itself isn't unused.  */
9256                   else if ((used[rel->r_offset >> 3]
9257                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9258                            && !used[val >> 3])
9259                     {
9260                       /* Do all the relocs again, to catch reference
9261                          chains.  */
9262                       repeat = 1;
9263                       used[val >> 3] = 1;
9264                     }
9265                 }
9266             }
9267           while (repeat);
9268
9269           if (elf_section_data (sec)->relocs != relstart)
9270             free (relstart);
9271         }
9272
9273       /* Merge the used and skip arrays.  Assume that TOC
9274          doublewords not appearing as either used or unused belong
9275          to an entry more than one doubleword in size.  */
9276       for (drop = skip, keep = used, last = 0, some_unused = 0;
9277            drop < skip + (toc->size + 7) / 8;
9278            ++drop, ++keep)
9279         {
9280           if (*keep)
9281             {
9282               *drop &= ~ref_from_discarded;
9283               if ((*drop & can_optimize) != 0)
9284                 some_unused = 1;
9285               last = 0;
9286             }
9287           else if ((*drop & ref_from_discarded) != 0)
9288             {
9289               some_unused = 1;
9290               last = ref_from_discarded;
9291             }
9292           else
9293             *drop = last;
9294         }
9295
9296       free (used);
9297
9298       if (some_unused)
9299         {
9300           bfd_byte *contents, *src;
9301           unsigned long off;
9302           Elf_Internal_Sym *sym;
9303           bfd_boolean local_toc_syms = FALSE;
9304
9305           /* Shuffle the toc contents, and at the same time convert the
9306              skip array from booleans into offsets.  */
9307           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9308             goto error_ret;
9309
9310           elf_section_data (toc)->this_hdr.contents = contents;
9311
9312           for (src = contents, off = 0, drop = skip;
9313                src < contents + toc->size;
9314                src += 8, ++drop)
9315             {
9316               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9317                 off += 8;
9318               else if (off != 0)
9319                 {
9320                   *drop = off;
9321                   memcpy (src - off, src, 8);
9322                 }
9323             }
9324           *drop = off;
9325           toc->rawsize = toc->size;
9326           toc->size = src - contents - off;
9327
9328           /* Adjust addends for relocs against the toc section sym,
9329              and optimize any accesses we can.  */
9330           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9331             {
9332               if (sec->reloc_count == 0
9333                   || discarded_section (sec))
9334                 continue;
9335
9336               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9337                                                     info->keep_memory);
9338               if (relstart == NULL)
9339                 goto error_ret;
9340
9341               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9342                 {
9343                   enum elf_ppc64_reloc_type r_type;
9344                   unsigned long r_symndx;
9345                   asection *sym_sec;
9346                   struct elf_link_hash_entry *h;
9347                   bfd_vma val;
9348
9349                   r_type = ELF64_R_TYPE (rel->r_info);
9350                   switch (r_type)
9351                     {
9352                     default:
9353                       continue;
9354
9355                     case R_PPC64_TOC16:
9356                     case R_PPC64_TOC16_LO:
9357                     case R_PPC64_TOC16_HI:
9358                     case R_PPC64_TOC16_HA:
9359                     case R_PPC64_TOC16_DS:
9360                     case R_PPC64_TOC16_LO_DS:
9361                     case R_PPC64_ADDR64:
9362                       break;
9363                     }
9364
9365                   r_symndx = ELF64_R_SYM (rel->r_info);
9366                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9367                                   r_symndx, ibfd))
9368                     goto error_ret;
9369
9370                   if (sym_sec != toc)
9371                     continue;
9372
9373                   if (h != NULL)
9374                     val = h->root.u.def.value;
9375                   else
9376                     {
9377                       val = sym->st_value;
9378                       if (val != 0)
9379                         local_toc_syms = TRUE;
9380                     }
9381
9382                   val += rel->r_addend;
9383
9384                   if (val > toc->rawsize)
9385                     val = toc->rawsize;
9386                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9387                     continue;
9388                   else if ((skip[val >> 3] & can_optimize) != 0)
9389                     {
9390                       Elf_Internal_Rela *tocrel
9391                         = toc_relocs + (skip[val >> 3] >> 2);
9392                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9393
9394                       switch (r_type)
9395                         {
9396                         case R_PPC64_TOC16_HA:
9397                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9398                           break;
9399
9400                         case R_PPC64_TOC16_LO_DS:
9401                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9402                           break;
9403
9404                         default:
9405                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9406                             ppc_howto_init ();
9407                           info->callbacks->einfo
9408                             /* xgettext:c-format */
9409                             (_("%H: %s references "
9410                                "optimized away TOC entry\n"),
9411                              ibfd, sec, rel->r_offset,
9412                              ppc64_elf_howto_table[r_type]->name);
9413                           bfd_set_error (bfd_error_bad_value);
9414                           goto error_ret;
9415                         }
9416                       rel->r_addend = tocrel->r_addend;
9417                       elf_section_data (sec)->relocs = relstart;
9418                       continue;
9419                     }
9420
9421                   if (h != NULL || sym->st_value != 0)
9422                     continue;
9423
9424                   rel->r_addend -= skip[val >> 3];
9425                   elf_section_data (sec)->relocs = relstart;
9426                 }
9427
9428               if (elf_section_data (sec)->relocs != relstart)
9429                 free (relstart);
9430             }
9431
9432           /* We shouldn't have local or global symbols defined in the TOC,
9433              but handle them anyway.  */
9434           if (local_syms != NULL)
9435             for (sym = local_syms;
9436                  sym < local_syms + symtab_hdr->sh_info;
9437                  ++sym)
9438               if (sym->st_value != 0
9439                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9440                 {
9441                   unsigned long i;
9442
9443                   if (sym->st_value > toc->rawsize)
9444                     i = toc->rawsize >> 3;
9445                   else
9446                     i = sym->st_value >> 3;
9447
9448                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9449                     {
9450                       if (local_toc_syms)
9451                         _bfd_error_handler
9452                           (_("%s defined on removed toc entry"),
9453                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9454                       do
9455                         ++i;
9456                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9457                       sym->st_value = (bfd_vma) i << 3;
9458                     }
9459
9460                   sym->st_value -= skip[i];
9461                   symtab_hdr->contents = (unsigned char *) local_syms;
9462                 }
9463
9464           /* Adjust any global syms defined in this toc input section.  */
9465           if (toc_inf.global_toc_syms)
9466             {
9467               toc_inf.toc = toc;
9468               toc_inf.skip = skip;
9469               toc_inf.global_toc_syms = FALSE;
9470               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9471                                       &toc_inf);
9472             }
9473
9474           if (toc->reloc_count != 0)
9475             {
9476               Elf_Internal_Shdr *rel_hdr;
9477               Elf_Internal_Rela *wrel;
9478               bfd_size_type sz;
9479
9480               /* Remove unused toc relocs, and adjust those we keep.  */
9481               if (toc_relocs == NULL)
9482                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9483                                                         info->keep_memory);
9484               if (toc_relocs == NULL)
9485                 goto error_ret;
9486
9487               wrel = toc_relocs;
9488               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9489                 if ((skip[rel->r_offset >> 3]
9490                      & (ref_from_discarded | can_optimize)) == 0)
9491                   {
9492                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9493                     wrel->r_info = rel->r_info;
9494                     wrel->r_addend = rel->r_addend;
9495                     ++wrel;
9496                   }
9497                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9498                                             &local_syms, NULL, NULL))
9499                   goto error_ret;
9500
9501               elf_section_data (toc)->relocs = toc_relocs;
9502               toc->reloc_count = wrel - toc_relocs;
9503               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9504               sz = rel_hdr->sh_entsize;
9505               rel_hdr->sh_size = toc->reloc_count * sz;
9506             }
9507         }
9508       else if (toc_relocs != NULL
9509                && elf_section_data (toc)->relocs != toc_relocs)
9510         free (toc_relocs);
9511
9512       if (local_syms != NULL
9513           && symtab_hdr->contents != (unsigned char *) local_syms)
9514         {
9515           if (!info->keep_memory)
9516             free (local_syms);
9517           else
9518             symtab_hdr->contents = (unsigned char *) local_syms;
9519         }
9520       free (skip);
9521     }
9522
9523   return TRUE;
9524 }
9525
9526 /* Return true iff input section I references the TOC using
9527    instructions limited to +/-32k offsets.  */
9528
9529 bfd_boolean
9530 ppc64_elf_has_small_toc_reloc (asection *i)
9531 {
9532   return (is_ppc64_elf (i->owner)
9533           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9534 }
9535
9536 /* Allocate space for one GOT entry.  */
9537
9538 static void
9539 allocate_got (struct elf_link_hash_entry *h,
9540               struct bfd_link_info *info,
9541               struct got_entry *gent)
9542 {
9543   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9544   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9545   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9546                  ? 16 : 8);
9547   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9548                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9549   asection *got = ppc64_elf_tdata (gent->owner)->got;
9550
9551   gent->got.offset = got->size;
9552   got->size += entsize;
9553
9554   if (h->type == STT_GNU_IFUNC)
9555     {
9556       htab->elf.irelplt->size += rentsize;
9557       htab->got_reli_size += rentsize;
9558     }
9559   else if (((bfd_link_pic (info)
9560              && !((gent->tls_type & TLS_TPREL) != 0
9561                   && bfd_link_executable (info)
9562                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9563             || (htab->elf.dynamic_sections_created
9564                 && h->dynindx != -1
9565                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9566            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9567     {
9568       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9569       relgot->size += rentsize;
9570     }
9571 }
9572
9573 /* This function merges got entries in the same toc group.  */
9574
9575 static void
9576 merge_got_entries (struct got_entry **pent)
9577 {
9578   struct got_entry *ent, *ent2;
9579
9580   for (ent = *pent; ent != NULL; ent = ent->next)
9581     if (!ent->is_indirect)
9582       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9583         if (!ent2->is_indirect
9584             && ent2->addend == ent->addend
9585             && ent2->tls_type == ent->tls_type
9586             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9587           {
9588             ent2->is_indirect = TRUE;
9589             ent2->got.ent = ent;
9590           }
9591 }
9592
9593 /* If H is undefined, make it dynamic if that makes sense.  */
9594
9595 static bfd_boolean
9596 ensure_undef_dynamic (struct bfd_link_info *info,
9597                       struct elf_link_hash_entry *h)
9598 {
9599   struct elf_link_hash_table *htab = elf_hash_table (info);
9600
9601   if (htab->dynamic_sections_created
9602       && ((info->dynamic_undefined_weak != 0
9603            && h->root.type == bfd_link_hash_undefweak)
9604           || h->root.type == bfd_link_hash_undefined)
9605       && h->dynindx == -1
9606       && !h->forced_local
9607       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9608     return bfd_elf_link_record_dynamic_symbol (info, h);
9609   return TRUE;
9610 }
9611
9612 /* Allocate space in .plt, .got and associated reloc sections for
9613    dynamic relocs.  */
9614
9615 static bfd_boolean
9616 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9617 {
9618   struct bfd_link_info *info;
9619   struct ppc_link_hash_table *htab;
9620   asection *s;
9621   struct ppc_link_hash_entry *eh;
9622   struct got_entry **pgent, *gent;
9623
9624   if (h->root.type == bfd_link_hash_indirect)
9625     return TRUE;
9626
9627   info = (struct bfd_link_info *) inf;
9628   htab = ppc_hash_table (info);
9629   if (htab == NULL)
9630     return FALSE;
9631
9632   eh = (struct ppc_link_hash_entry *) h;
9633   /* Run through the TLS GD got entries first if we're changing them
9634      to TPREL.  */
9635   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9636     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9637       if (gent->got.refcount > 0
9638           && (gent->tls_type & TLS_GD) != 0)
9639         {
9640           /* This was a GD entry that has been converted to TPREL.  If
9641              there happens to be a TPREL entry we can use that one.  */
9642           struct got_entry *ent;
9643           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9644             if (ent->got.refcount > 0
9645                 && (ent->tls_type & TLS_TPREL) != 0
9646                 && ent->addend == gent->addend
9647                 && ent->owner == gent->owner)
9648               {
9649                 gent->got.refcount = 0;
9650                 break;
9651               }
9652
9653           /* If not, then we'll be using our own TPREL entry.  */
9654           if (gent->got.refcount != 0)
9655             gent->tls_type = TLS_TLS | TLS_TPREL;
9656         }
9657
9658   /* Remove any list entry that won't generate a word in the GOT before
9659      we call merge_got_entries.  Otherwise we risk merging to empty
9660      entries.  */
9661   pgent = &h->got.glist;
9662   while ((gent = *pgent) != NULL)
9663     if (gent->got.refcount > 0)
9664       {
9665         if ((gent->tls_type & TLS_LD) != 0
9666             && !h->def_dynamic)
9667           {
9668             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9669             *pgent = gent->next;
9670           }
9671         else
9672           pgent = &gent->next;
9673       }
9674     else
9675       *pgent = gent->next;
9676
9677   if (!htab->do_multi_toc)
9678     merge_got_entries (&h->got.glist);
9679
9680   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9681     if (!gent->is_indirect)
9682       {
9683         /* Make sure this symbol is output as a dynamic symbol.  */
9684         if (!ensure_undef_dynamic (info, h))
9685           return FALSE;
9686
9687         if (!is_ppc64_elf (gent->owner))
9688           abort ();
9689
9690         allocate_got (h, info, gent);
9691       }
9692
9693   /* If no dynamic sections we can't have dynamic relocs, except for
9694      IFUNCs which are handled even in static executables.  */
9695   if (!htab->elf.dynamic_sections_created
9696       && h->type != STT_GNU_IFUNC)
9697     eh->dyn_relocs = NULL;
9698
9699   /* Discard relocs on undefined symbols that must be local.  */
9700   else if (h->root.type == bfd_link_hash_undefined
9701            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9702     eh->dyn_relocs = NULL;
9703
9704   /* Also discard relocs on undefined weak syms with non-default
9705      visibility, or when dynamic_undefined_weak says so.  */
9706   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9707     eh->dyn_relocs = NULL;
9708
9709   if (eh->dyn_relocs != NULL)
9710     {
9711       struct elf_dyn_relocs *p, **pp;
9712
9713       /* In the shared -Bsymbolic case, discard space allocated for
9714          dynamic pc-relative relocs against symbols which turn out to
9715          be defined in regular objects.  For the normal shared case,
9716          discard space for relocs that have become local due to symbol
9717          visibility changes.  */
9718
9719       if (bfd_link_pic (info))
9720         {
9721           /* Relocs that use pc_count are those that appear on a call
9722              insn, or certain REL relocs (see must_be_dyn_reloc) that
9723              can be generated via assembly.  We want calls to
9724              protected symbols to resolve directly to the function
9725              rather than going via the plt.  If people want function
9726              pointer comparisons to work as expected then they should
9727              avoid writing weird assembly.  */
9728           if (SYMBOL_CALLS_LOCAL (info, h))
9729             {
9730               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9731                 {
9732                   p->count -= p->pc_count;
9733                   p->pc_count = 0;
9734                   if (p->count == 0)
9735                     *pp = p->next;
9736                   else
9737                     pp = &p->next;
9738                 }
9739             }
9740
9741           if (eh->dyn_relocs != NULL)
9742             {
9743               /* Make sure this symbol is output as a dynamic symbol.  */
9744               if (!ensure_undef_dynamic (info, h))
9745                 return FALSE;
9746             }
9747         }
9748       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9749         {
9750           /* For the non-pic case, discard space for relocs against
9751              symbols which turn out to need copy relocs or are not
9752              dynamic.  */
9753           if (h->dynamic_adjusted
9754               && !h->def_regular
9755               && !ELF_COMMON_DEF_P (h))
9756             {
9757               /* Make sure this symbol is output as a dynamic symbol.  */
9758               if (!ensure_undef_dynamic (info, h))
9759                 return FALSE;
9760
9761               if (h->dynindx == -1)
9762                 eh->dyn_relocs = NULL;
9763             }
9764           else
9765             eh->dyn_relocs = NULL;
9766         }
9767
9768       /* Finally, allocate space.  */
9769       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9770         {
9771           asection *sreloc = elf_section_data (p->sec)->sreloc;
9772           if (eh->elf.type == STT_GNU_IFUNC)
9773             sreloc = htab->elf.irelplt;
9774           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9775         }
9776     }
9777
9778   if ((htab->elf.dynamic_sections_created
9779        && h->dynindx != -1)
9780       || h->type == STT_GNU_IFUNC)
9781     {
9782       struct plt_entry *pent;
9783       bfd_boolean doneone = FALSE;
9784       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9785         if (pent->plt.refcount > 0)
9786           {
9787             if (!htab->elf.dynamic_sections_created
9788                 || h->dynindx == -1)
9789               {
9790                 s = htab->elf.iplt;
9791                 pent->plt.offset = s->size;
9792                 s->size += PLT_ENTRY_SIZE (htab);
9793                 s = htab->elf.irelplt;
9794               }
9795             else
9796               {
9797                 /* If this is the first .plt entry, make room for the special
9798                    first entry.  */
9799                 s = htab->elf.splt;
9800                 if (s->size == 0)
9801                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9802
9803                 pent->plt.offset = s->size;
9804
9805                 /* Make room for this entry.  */
9806                 s->size += PLT_ENTRY_SIZE (htab);
9807
9808                 /* Make room for the .glink code.  */
9809                 s = htab->glink;
9810                 if (s->size == 0)
9811                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9812                 if (htab->opd_abi)
9813                   {
9814                     /* We need bigger stubs past index 32767.  */
9815                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9816                       s->size += 4;
9817                     s->size += 2*4;
9818                   }
9819                 else
9820                   s->size += 4;
9821
9822                 /* We also need to make an entry in the .rela.plt section.  */
9823                 s = htab->elf.srelplt;
9824               }
9825             s->size += sizeof (Elf64_External_Rela);
9826             doneone = TRUE;
9827           }
9828         else
9829           pent->plt.offset = (bfd_vma) -1;
9830       if (!doneone)
9831         {
9832           h->plt.plist = NULL;
9833           h->needs_plt = 0;
9834         }
9835     }
9836   else
9837     {
9838       h->plt.plist = NULL;
9839       h->needs_plt = 0;
9840     }
9841
9842   return TRUE;
9843 }
9844
9845 #define PPC_LO(v) ((v) & 0xffff)
9846 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9847 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9848
9849 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9850    to set up space for global entry stubs.  These are put in glink,
9851    after the branch table.  */
9852
9853 static bfd_boolean
9854 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9855 {
9856   struct bfd_link_info *info;
9857   struct ppc_link_hash_table *htab;
9858   struct plt_entry *pent;
9859   asection *s, *plt;
9860
9861   if (h->root.type == bfd_link_hash_indirect)
9862     return TRUE;
9863
9864   if (!h->pointer_equality_needed)
9865     return TRUE;
9866
9867   if (h->def_regular)
9868     return TRUE;
9869
9870   info = inf;
9871   htab = ppc_hash_table (info);
9872   if (htab == NULL)
9873     return FALSE;
9874
9875   s = htab->global_entry;
9876   plt = htab->elf.splt;
9877   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9878     if (pent->plt.offset != (bfd_vma) -1
9879         && pent->addend == 0)
9880       {
9881         /* For ELFv2, if this symbol is not defined in a regular file
9882            and we are not generating a shared library or pie, then we
9883            need to define the symbol in the executable on a call stub.
9884            This is to avoid text relocations.  */
9885         bfd_vma off, stub_align, stub_off, stub_size;
9886         unsigned int align_power;
9887
9888         stub_size = 16;
9889         if (!htab->params->speculate_indirect_jumps)
9890           stub_size += 8;
9891         stub_off = s->size;
9892         if (htab->params->plt_stub_align >= 0)
9893           align_power = htab->params->plt_stub_align;
9894         else
9895           align_power = -htab->params->plt_stub_align;
9896         /* Setting section alignment is delayed until we know it is
9897            non-empty.  Otherwise the .text output section will be
9898            aligned at least to plt_stub_align even when no global
9899            entry stubs are needed.  */
9900         if (s->alignment_power < align_power)
9901           s->alignment_power = align_power;
9902         stub_align = (bfd_vma) 1 << align_power;
9903         if (htab->params->plt_stub_align >= 0
9904             || ((((stub_off + stub_size - 1) & -stub_align)
9905                  - (stub_off & -stub_align))
9906                 > ((stub_size - 1) & -stub_align)))
9907           stub_off = (stub_off + stub_align - 1) & -stub_align;
9908         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9909         off -= stub_off + s->output_offset + s->output_section->vma;
9910         /* Note that for --plt-stub-align negative we have a possible
9911            dependency between stub offset and size.  Break that
9912            dependency by assuming the max stub size when calculating
9913            the stub offset.  */
9914         if (PPC_HA (off) == 0)
9915           stub_size -= 4;
9916         h->root.type = bfd_link_hash_defined;
9917         h->root.u.def.section = s;
9918         h->root.u.def.value = stub_off;
9919         s->size = stub_off + stub_size;
9920         break;
9921       }
9922   return TRUE;
9923 }
9924
9925 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9926    read-only sections.  */
9927
9928 static bfd_boolean
9929 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9930 {
9931   asection *sec;
9932
9933   if (h->root.type == bfd_link_hash_indirect)
9934     return TRUE;
9935
9936   sec = readonly_dynrelocs (h);
9937   if (sec != NULL)
9938     {
9939       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9940
9941       info->flags |= DF_TEXTREL;
9942       info->callbacks->minfo
9943         (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
9944          sec->owner, h->root.root.string, sec);
9945
9946       /* Not an error, just cut short the traversal.  */
9947       return FALSE;
9948     }
9949   return TRUE;
9950 }
9951
9952 /* Set the sizes of the dynamic sections.  */
9953
9954 static bfd_boolean
9955 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9956                                  struct bfd_link_info *info)
9957 {
9958   struct ppc_link_hash_table *htab;
9959   bfd *dynobj;
9960   asection *s;
9961   bfd_boolean relocs;
9962   bfd *ibfd;
9963   struct got_entry *first_tlsld;
9964
9965   htab = ppc_hash_table (info);
9966   if (htab == NULL)
9967     return FALSE;
9968
9969   dynobj = htab->elf.dynobj;
9970   if (dynobj == NULL)
9971     abort ();
9972
9973   if (htab->elf.dynamic_sections_created)
9974     {
9975       /* Set the contents of the .interp section to the interpreter.  */
9976       if (bfd_link_executable (info) && !info->nointerp)
9977         {
9978           s = bfd_get_linker_section (dynobj, ".interp");
9979           if (s == NULL)
9980             abort ();
9981           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9982           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9983         }
9984     }
9985
9986   /* Set up .got offsets for local syms, and space for local dynamic
9987      relocs.  */
9988   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9989     {
9990       struct got_entry **lgot_ents;
9991       struct got_entry **end_lgot_ents;
9992       struct plt_entry **local_plt;
9993       struct plt_entry **end_local_plt;
9994       unsigned char *lgot_masks;
9995       bfd_size_type locsymcount;
9996       Elf_Internal_Shdr *symtab_hdr;
9997
9998       if (!is_ppc64_elf (ibfd))
9999         continue;
10000
10001       for (s = ibfd->sections; s != NULL; s = s->next)
10002         {
10003           struct ppc_dyn_relocs *p;
10004
10005           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10006             {
10007               if (!bfd_is_abs_section (p->sec)
10008                   && bfd_is_abs_section (p->sec->output_section))
10009                 {
10010                   /* Input section has been discarded, either because
10011                      it is a copy of a linkonce section or due to
10012                      linker script /DISCARD/, so we'll be discarding
10013                      the relocs too.  */
10014                 }
10015               else if (p->count != 0)
10016                 {
10017                   asection *srel = elf_section_data (p->sec)->sreloc;
10018                   if (p->ifunc)
10019                     srel = htab->elf.irelplt;
10020                   srel->size += p->count * sizeof (Elf64_External_Rela);
10021                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10022                     info->flags |= DF_TEXTREL;
10023                 }
10024             }
10025         }
10026
10027       lgot_ents = elf_local_got_ents (ibfd);
10028       if (!lgot_ents)
10029         continue;
10030
10031       symtab_hdr = &elf_symtab_hdr (ibfd);
10032       locsymcount = symtab_hdr->sh_info;
10033       end_lgot_ents = lgot_ents + locsymcount;
10034       local_plt = (struct plt_entry **) end_lgot_ents;
10035       end_local_plt = local_plt + locsymcount;
10036       lgot_masks = (unsigned char *) end_local_plt;
10037       s = ppc64_elf_tdata (ibfd)->got;
10038       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10039         {
10040           struct got_entry **pent, *ent;
10041
10042           pent = lgot_ents;
10043           while ((ent = *pent) != NULL)
10044             if (ent->got.refcount > 0)
10045               {
10046                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10047                   {
10048                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10049                     *pent = ent->next;
10050                   }
10051                 else
10052                   {
10053                     unsigned int ent_size = 8;
10054                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10055
10056                     ent->got.offset = s->size;
10057                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10058                       {
10059                         ent_size *= 2;
10060                         rel_size *= 2;
10061                       }
10062                     s->size += ent_size;
10063                     if ((*lgot_masks & PLT_IFUNC) != 0)
10064                       {
10065                         htab->elf.irelplt->size += rel_size;
10066                         htab->got_reli_size += rel_size;
10067                       }
10068                     else if (bfd_link_pic (info)
10069                              && !((ent->tls_type & TLS_TPREL) != 0
10070                                   && bfd_link_executable (info)))
10071                       {
10072                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10073                         srel->size += rel_size;
10074                       }
10075                     pent = &ent->next;
10076                   }
10077               }
10078             else
10079               *pent = ent->next;
10080         }
10081
10082       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
10083       for (; local_plt < end_local_plt; ++local_plt)
10084         {
10085           struct plt_entry *ent;
10086
10087           for (ent = *local_plt; ent != NULL; ent = ent->next)
10088             if (ent->plt.refcount > 0)
10089               {
10090                 s = htab->elf.iplt;
10091                 ent->plt.offset = s->size;
10092                 s->size += PLT_ENTRY_SIZE (htab);
10093
10094                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10095               }
10096             else
10097               ent->plt.offset = (bfd_vma) -1;
10098         }
10099     }
10100
10101   /* Allocate global sym .plt and .got entries, and space for global
10102      sym dynamic relocs.  */
10103   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10104
10105   if (!htab->opd_abi && !bfd_link_pic (info))
10106     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10107
10108   first_tlsld = NULL;
10109   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10110     {
10111       struct got_entry *ent;
10112
10113       if (!is_ppc64_elf (ibfd))
10114         continue;
10115
10116       ent = ppc64_tlsld_got (ibfd);
10117       if (ent->got.refcount > 0)
10118         {
10119           if (!htab->do_multi_toc && first_tlsld != NULL)
10120             {
10121               ent->is_indirect = TRUE;
10122               ent->got.ent = first_tlsld;
10123             }
10124           else
10125             {
10126               if (first_tlsld == NULL)
10127                 first_tlsld = ent;
10128               s = ppc64_elf_tdata (ibfd)->got;
10129               ent->got.offset = s->size;
10130               ent->owner = ibfd;
10131               s->size += 16;
10132               if (bfd_link_pic (info))
10133                 {
10134                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10135                   srel->size += sizeof (Elf64_External_Rela);
10136                 }
10137             }
10138         }
10139       else
10140         ent->got.offset = (bfd_vma) -1;
10141     }
10142
10143   /* We now have determined the sizes of the various dynamic sections.
10144      Allocate memory for them.  */
10145   relocs = FALSE;
10146   for (s = dynobj->sections; s != NULL; s = s->next)
10147     {
10148       if ((s->flags & SEC_LINKER_CREATED) == 0)
10149         continue;
10150
10151       if (s == htab->brlt || s == htab->relbrlt)
10152         /* These haven't been allocated yet;  don't strip.  */
10153         continue;
10154       else if (s == htab->elf.sgot
10155                || s == htab->elf.splt
10156                || s == htab->elf.iplt
10157                || s == htab->glink
10158                || s == htab->global_entry
10159                || s == htab->elf.sdynbss
10160                || s == htab->elf.sdynrelro)
10161         {
10162           /* Strip this section if we don't need it; see the
10163              comment below.  */
10164         }
10165       else if (s == htab->glink_eh_frame)
10166         {
10167           if (!bfd_is_abs_section (s->output_section))
10168             /* Not sized yet.  */
10169             continue;
10170         }
10171       else if (CONST_STRNEQ (s->name, ".rela"))
10172         {
10173           if (s->size != 0)
10174             {
10175               if (s != htab->elf.srelplt)
10176                 relocs = TRUE;
10177
10178               /* We use the reloc_count field as a counter if we need
10179                  to copy relocs into the output file.  */
10180               s->reloc_count = 0;
10181             }
10182         }
10183       else
10184         {
10185           /* It's not one of our sections, so don't allocate space.  */
10186           continue;
10187         }
10188
10189       if (s->size == 0)
10190         {
10191           /* If we don't need this section, strip it from the
10192              output file.  This is mostly to handle .rela.bss and
10193              .rela.plt.  We must create both sections in
10194              create_dynamic_sections, because they must be created
10195              before the linker maps input sections to output
10196              sections.  The linker does that before
10197              adjust_dynamic_symbol is called, and it is that
10198              function which decides whether anything needs to go
10199              into these sections.  */
10200           s->flags |= SEC_EXCLUDE;
10201           continue;
10202         }
10203
10204       if (bfd_is_abs_section (s->output_section))
10205         _bfd_error_handler (_("warning: discarding dynamic section %s"),
10206                             s->name);
10207
10208       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10209         continue;
10210
10211       /* Allocate memory for the section contents.  We use bfd_zalloc
10212          here in case unused entries are not reclaimed before the
10213          section's contents are written out.  This should not happen,
10214          but this way if it does we get a R_PPC64_NONE reloc in .rela
10215          sections instead of garbage.
10216          We also rely on the section contents being zero when writing
10217          the GOT and .dynrelro.  */
10218       s->contents = bfd_zalloc (dynobj, s->size);
10219       if (s->contents == NULL)
10220         return FALSE;
10221     }
10222
10223   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10224     {
10225       if (!is_ppc64_elf (ibfd))
10226         continue;
10227
10228       s = ppc64_elf_tdata (ibfd)->got;
10229       if (s != NULL && s != htab->elf.sgot)
10230         {
10231           if (s->size == 0)
10232             s->flags |= SEC_EXCLUDE;
10233           else
10234             {
10235               s->contents = bfd_zalloc (ibfd, s->size);
10236               if (s->contents == NULL)
10237                 return FALSE;
10238             }
10239         }
10240       s = ppc64_elf_tdata (ibfd)->relgot;
10241       if (s != NULL)
10242         {
10243           if (s->size == 0)
10244             s->flags |= SEC_EXCLUDE;
10245           else
10246             {
10247               s->contents = bfd_zalloc (ibfd, s->size);
10248               if (s->contents == NULL)
10249                 return FALSE;
10250               relocs = TRUE;
10251               s->reloc_count = 0;
10252             }
10253         }
10254     }
10255
10256   if (htab->elf.dynamic_sections_created)
10257     {
10258       bfd_boolean tls_opt;
10259
10260       /* Add some entries to the .dynamic section.  We fill in the
10261          values later, in ppc64_elf_finish_dynamic_sections, but we
10262          must add the entries now so that we get the correct size for
10263          the .dynamic section.  The DT_DEBUG entry is filled in by the
10264          dynamic linker and used by the debugger.  */
10265 #define add_dynamic_entry(TAG, VAL) \
10266   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10267
10268       if (bfd_link_executable (info))
10269         {
10270           if (!add_dynamic_entry (DT_DEBUG, 0))
10271             return FALSE;
10272         }
10273
10274       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10275         {
10276           if (!add_dynamic_entry (DT_PLTGOT, 0)
10277               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10278               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10279               || !add_dynamic_entry (DT_JMPREL, 0)
10280               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10281             return FALSE;
10282         }
10283
10284       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10285         {
10286           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10287               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10288             return FALSE;
10289         }
10290
10291       tls_opt = (htab->params->tls_get_addr_opt
10292                  && htab->tls_get_addr_fd != NULL
10293                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10294       if (tls_opt || !htab->opd_abi)
10295         {
10296           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10297             return FALSE;
10298         }
10299
10300       if (relocs)
10301         {
10302           if (!add_dynamic_entry (DT_RELA, 0)
10303               || !add_dynamic_entry (DT_RELASZ, 0)
10304               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10305             return FALSE;
10306
10307           /* If any dynamic relocs apply to a read-only section,
10308              then we need a DT_TEXTREL entry.  */
10309           if ((info->flags & DF_TEXTREL) == 0)
10310             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10311
10312           if ((info->flags & DF_TEXTREL) != 0)
10313             {
10314               if (!add_dynamic_entry (DT_TEXTREL, 0))
10315                 return FALSE;
10316             }
10317         }
10318     }
10319 #undef add_dynamic_entry
10320
10321   return TRUE;
10322 }
10323
10324 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10325
10326 static bfd_boolean
10327 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10328 {
10329   if (h->plt.plist != NULL
10330       && !h->def_regular
10331       && !h->pointer_equality_needed)
10332     return FALSE;
10333
10334   return _bfd_elf_hash_symbol (h);
10335 }
10336
10337 /* Determine the type of stub needed, if any, for a call.  */
10338
10339 static inline enum ppc_stub_type
10340 ppc_type_of_stub (asection *input_sec,
10341                   const Elf_Internal_Rela *rel,
10342                   struct ppc_link_hash_entry **hash,
10343                   struct plt_entry **plt_ent,
10344                   bfd_vma destination,
10345                   unsigned long local_off)
10346 {
10347   struct ppc_link_hash_entry *h = *hash;
10348   bfd_vma location;
10349   bfd_vma branch_offset;
10350   bfd_vma max_branch_offset;
10351   enum elf_ppc64_reloc_type r_type;
10352
10353   if (h != NULL)
10354     {
10355       struct plt_entry *ent;
10356       struct ppc_link_hash_entry *fdh = h;
10357       if (h->oh != NULL
10358           && h->oh->is_func_descriptor)
10359         {
10360           fdh = ppc_follow_link (h->oh);
10361           *hash = fdh;
10362         }
10363
10364       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10365         if (ent->addend == rel->r_addend
10366             && ent->plt.offset != (bfd_vma) -1)
10367           {
10368             *plt_ent = ent;
10369             return ppc_stub_plt_call;
10370           }
10371
10372       /* Here, we know we don't have a plt entry.  If we don't have a
10373          either a defined function descriptor or a defined entry symbol
10374          in a regular object file, then it is pointless trying to make
10375          any other type of stub.  */
10376       if (!is_static_defined (&fdh->elf)
10377           && !is_static_defined (&h->elf))
10378         return ppc_stub_none;
10379     }
10380   else if (elf_local_got_ents (input_sec->owner) != NULL)
10381     {
10382       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10383       struct plt_entry **local_plt = (struct plt_entry **)
10384         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10385       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10386
10387       if (local_plt[r_symndx] != NULL)
10388         {
10389           struct plt_entry *ent;
10390
10391           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10392             if (ent->addend == rel->r_addend
10393                 && ent->plt.offset != (bfd_vma) -1)
10394               {
10395                 *plt_ent = ent;
10396                 return ppc_stub_plt_call;
10397               }
10398         }
10399     }
10400
10401   /* Determine where the call point is.  */
10402   location = (input_sec->output_offset
10403               + input_sec->output_section->vma
10404               + rel->r_offset);
10405
10406   branch_offset = destination - location;
10407   r_type = ELF64_R_TYPE (rel->r_info);
10408
10409   /* Determine if a long branch stub is needed.  */
10410   max_branch_offset = 1 << 25;
10411   if (r_type != R_PPC64_REL24)
10412     max_branch_offset = 1 << 15;
10413
10414   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10415     /* We need a stub.  Figure out whether a long_branch or plt_branch
10416        is needed later.  */
10417     return ppc_stub_long_branch;
10418
10419   return ppc_stub_none;
10420 }
10421
10422 /* With power7 weakly ordered memory model, it is possible for ld.so
10423    to update a plt entry in one thread and have another thread see a
10424    stale zero toc entry.  To avoid this we need some sort of acquire
10425    barrier in the call stub.  One solution is to make the load of the
10426    toc word seem to appear to depend on the load of the function entry
10427    word.  Another solution is to test for r2 being zero, and branch to
10428    the appropriate glink entry if so.
10429
10430    .    fake dep barrier        compare
10431    .    ld 12,xxx(2)            ld 12,xxx(2)
10432    .    mtctr 12                mtctr 12
10433    .    xor 11,12,12            ld 2,xxx+8(2)
10434    .    add 2,2,11              cmpldi 2,0
10435    .    ld 2,xxx+8(2)           bnectr+
10436    .    bctr                    b <glink_entry>
10437
10438    The solution involving the compare turns out to be faster, so
10439    that's what we use unless the branch won't reach.  */
10440
10441 #define ALWAYS_USE_FAKE_DEP 0
10442 #define ALWAYS_EMIT_R2SAVE 0
10443
10444 static inline unsigned int
10445 plt_stub_size (struct ppc_link_hash_table *htab,
10446                struct ppc_stub_hash_entry *stub_entry,
10447                bfd_vma off)
10448 {
10449   unsigned size = 12;
10450
10451   if (ALWAYS_EMIT_R2SAVE
10452       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10453     size += 4;
10454   if (PPC_HA (off) != 0)
10455     size += 4;
10456   if (!htab->params->speculate_indirect_jumps)
10457     size += 8;
10458   if (htab->opd_abi)
10459     {
10460       size += 4;
10461       if (htab->params->plt_static_chain)
10462         size += 4;
10463       if (htab->params->plt_thread_safe
10464           && htab->elf.dynamic_sections_created
10465           && stub_entry->h != NULL
10466           && stub_entry->h->elf.dynindx != -1)
10467         size += 8;
10468       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10469         size += 4;
10470     }
10471   if (stub_entry->h != NULL
10472       && (stub_entry->h == htab->tls_get_addr_fd
10473           || stub_entry->h == htab->tls_get_addr)
10474       && htab->params->tls_get_addr_opt)
10475     {
10476       size += 7 * 4;
10477       if (ALWAYS_EMIT_R2SAVE
10478           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10479         {
10480           size += 6 * 4;
10481           if (!htab->params->speculate_indirect_jumps)
10482             size -= 4;
10483         }
10484     }
10485   return size;
10486 }
10487
10488 /* Depending on the sign of plt_stub_align:
10489    If positive, return the padding to align to a 2**plt_stub_align
10490    boundary.
10491    If negative, if this stub would cross fewer 2**plt_stub_align
10492    boundaries if we align, then return the padding needed to do so.  */
10493
10494 static inline unsigned int
10495 plt_stub_pad (struct ppc_link_hash_table *htab,
10496               struct ppc_stub_hash_entry *stub_entry,
10497               bfd_vma plt_off)
10498 {
10499   int stub_align;
10500   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10501   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10502
10503   if (htab->params->plt_stub_align >= 0)
10504     {
10505       stub_align = 1 << htab->params->plt_stub_align;
10506       if ((stub_off & (stub_align - 1)) != 0)
10507         return stub_align - (stub_off & (stub_align - 1));
10508       return 0;
10509     }
10510
10511   stub_align = 1 << -htab->params->plt_stub_align;
10512   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10513       > ((stub_size - 1) & -stub_align))
10514     return stub_align - (stub_off & (stub_align - 1));
10515   return 0;
10516 }
10517
10518 static inline bfd_byte *
10519 output_bctr (struct ppc_link_hash_table *htab, bfd *obfd, bfd_byte *p)
10520 {
10521   if (!htab->params->speculate_indirect_jumps)
10522     {
10523       bfd_put_32 (obfd, CRSETEQ, p);
10524       p += 4;
10525       bfd_put_32 (obfd, BEQCTRM, p);
10526       p += 4;
10527       bfd_put_32 (obfd, B_DOT, p);
10528       p += 4;
10529     }
10530   else
10531     {
10532       bfd_put_32 (obfd, BCTR, p);
10533       p += 4;
10534     }
10535   return p;
10536 }
10537
10538 /* Build a .plt call stub.  */
10539
10540 static inline bfd_byte *
10541 build_plt_stub (struct ppc_link_hash_table *htab,
10542                 struct ppc_stub_hash_entry *stub_entry,
10543                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10544 {
10545   bfd *obfd = htab->params->stub_bfd;
10546   bfd_boolean plt_load_toc = htab->opd_abi;
10547   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10548   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10549                                  && htab->elf.dynamic_sections_created
10550                                  && stub_entry->h != NULL
10551                                  && stub_entry->h->elf.dynindx != -1);
10552   bfd_boolean use_fake_dep = plt_thread_safe;
10553   bfd_vma cmp_branch_off = 0;
10554
10555   if (!ALWAYS_USE_FAKE_DEP
10556       && plt_load_toc
10557       && plt_thread_safe
10558       && htab->params->speculate_indirect_jumps
10559       && !((stub_entry->h == htab->tls_get_addr_fd
10560             || stub_entry->h == htab->tls_get_addr)
10561            && htab->params->tls_get_addr_opt))
10562     {
10563       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10564       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10565                           / PLT_ENTRY_SIZE (htab));
10566       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10567       bfd_vma to, from;
10568
10569       if (pltindex > 32768)
10570         glinkoff += (pltindex - 32768) * 4;
10571       to = (glinkoff
10572             + htab->glink->output_offset
10573             + htab->glink->output_section->vma);
10574       from = (p - stub_entry->group->stub_sec->contents
10575               + 4 * (ALWAYS_EMIT_R2SAVE
10576                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10577               + 4 * (PPC_HA (offset) != 0)
10578               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10579                      != PPC_HA (offset))
10580               + 4 * (plt_static_chain != 0)
10581               + 20
10582               + stub_entry->group->stub_sec->output_offset
10583               + stub_entry->group->stub_sec->output_section->vma);
10584       cmp_branch_off = to - from;
10585       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10586     }
10587
10588   if (PPC_HA (offset) != 0)
10589     {
10590       if (r != NULL)
10591         {
10592           if (ALWAYS_EMIT_R2SAVE
10593               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10594             r[0].r_offset += 4;
10595           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10596           r[1].r_offset = r[0].r_offset + 4;
10597           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10598           r[1].r_addend = r[0].r_addend;
10599           if (plt_load_toc)
10600             {
10601               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10602                 {
10603                   r[2].r_offset = r[1].r_offset + 4;
10604                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10605                   r[2].r_addend = r[0].r_addend;
10606                 }
10607               else
10608                 {
10609                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10610                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10611                   r[2].r_addend = r[0].r_addend + 8;
10612                   if (plt_static_chain)
10613                     {
10614                       r[3].r_offset = r[2].r_offset + 4;
10615                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10616                       r[3].r_addend = r[0].r_addend + 16;
10617                     }
10618                 }
10619             }
10620         }
10621       if (ALWAYS_EMIT_R2SAVE
10622           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10623         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10624       if (plt_load_toc)
10625         {
10626           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10627           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10628         }
10629       else
10630         {
10631           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10632           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10633         }
10634       if (plt_load_toc
10635           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10636         {
10637           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10638           offset = 0;
10639         }
10640       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10641       if (plt_load_toc)
10642         {
10643           if (use_fake_dep)
10644             {
10645               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10646               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10647             }
10648           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10649           if (plt_static_chain)
10650             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10651         }
10652     }
10653   else
10654     {
10655       if (r != NULL)
10656         {
10657           if (ALWAYS_EMIT_R2SAVE
10658               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10659             r[0].r_offset += 4;
10660           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10661           if (plt_load_toc)
10662             {
10663               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10664                 {
10665                   r[1].r_offset = r[0].r_offset + 4;
10666                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10667                   r[1].r_addend = r[0].r_addend;
10668                 }
10669               else
10670                 {
10671                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10672                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10673                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10674                   if (plt_static_chain)
10675                     {
10676                       r[2].r_offset = r[1].r_offset + 4;
10677                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10678                       r[2].r_addend = r[0].r_addend + 8;
10679                     }
10680                 }
10681             }
10682         }
10683       if (ALWAYS_EMIT_R2SAVE
10684           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10685         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10686       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10687       if (plt_load_toc
10688           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10689         {
10690           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10691           offset = 0;
10692         }
10693       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10694       if (plt_load_toc)
10695         {
10696           if (use_fake_dep)
10697             {
10698               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10699               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10700             }
10701           if (plt_static_chain)
10702             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10703           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10704         }
10705     }
10706   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10707     {
10708       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10709       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10710       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10711     }
10712   else
10713     p = output_bctr (htab, obfd, p);
10714   return p;
10715 }
10716
10717 /* Build a special .plt call stub for __tls_get_addr.  */
10718
10719 #define LD_R11_0R3      0xe9630000
10720 #define LD_R12_0R3      0xe9830000
10721 #define MR_R0_R3        0x7c601b78
10722 #define CMPDI_R11_0     0x2c2b0000
10723 #define ADD_R3_R12_R13  0x7c6c6a14
10724 #define BEQLR           0x4d820020
10725 #define MR_R3_R0        0x7c030378
10726 #define STD_R11_0R1     0xf9610000
10727 #define BCTRL           0x4e800421
10728 #define LD_R11_0R1      0xe9610000
10729 #define MTLR_R11        0x7d6803a6
10730
10731 static inline bfd_byte *
10732 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10733                          struct ppc_stub_hash_entry *stub_entry,
10734                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10735 {
10736   bfd *obfd = htab->params->stub_bfd;
10737
10738   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10739   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10740   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10741   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10742   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10743   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10744   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10745   if (r != NULL)
10746     r[0].r_offset += 7 * 4;
10747   if (!ALWAYS_EMIT_R2SAVE
10748       && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10749     return build_plt_stub (htab, stub_entry, p, offset, r);
10750
10751   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10752   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10753
10754   if (r != NULL)
10755     r[0].r_offset += 2 * 4;
10756   p = build_plt_stub (htab, stub_entry, p, offset, r);
10757   if (!htab->params->speculate_indirect_jumps)
10758     {
10759       p -= 4;
10760       bfd_put_32 (obfd, BEQCTRLM, p - 4);
10761     }
10762   else
10763     bfd_put_32 (obfd, BCTRL, p - 4);
10764
10765   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10766   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10767   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10768   bfd_put_32 (obfd, BLR, p),                    p += 4;
10769
10770   return p;
10771 }
10772
10773 static Elf_Internal_Rela *
10774 get_relocs (asection *sec, int count)
10775 {
10776   Elf_Internal_Rela *relocs;
10777   struct bfd_elf_section_data *elfsec_data;
10778
10779   elfsec_data = elf_section_data (sec);
10780   relocs = elfsec_data->relocs;
10781   if (relocs == NULL)
10782     {
10783       bfd_size_type relsize;
10784       relsize = sec->reloc_count * sizeof (*relocs);
10785       relocs = bfd_alloc (sec->owner, relsize);
10786       if (relocs == NULL)
10787         return NULL;
10788       elfsec_data->relocs = relocs;
10789       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10790                                           sizeof (Elf_Internal_Shdr));
10791       if (elfsec_data->rela.hdr == NULL)
10792         return NULL;
10793       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10794                                         * sizeof (Elf64_External_Rela));
10795       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10796       sec->reloc_count = 0;
10797     }
10798   relocs += sec->reloc_count;
10799   sec->reloc_count += count;
10800   return relocs;
10801 }
10802
10803 static bfd_vma
10804 get_r2off (struct bfd_link_info *info,
10805            struct ppc_stub_hash_entry *stub_entry)
10806 {
10807   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10808   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10809
10810   if (r2off == 0)
10811     {
10812       /* Support linking -R objects.  Get the toc pointer from the
10813          opd entry.  */
10814       char buf[8];
10815       if (!htab->opd_abi)
10816         return r2off;
10817       asection *opd = stub_entry->h->elf.root.u.def.section;
10818       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10819
10820       if (strcmp (opd->name, ".opd") != 0
10821           || opd->reloc_count != 0)
10822         {
10823           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10824                                   stub_entry->h->elf.root.root.string);
10825           bfd_set_error (bfd_error_bad_value);
10826           return (bfd_vma) -1;
10827         }
10828       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10829         return (bfd_vma) -1;
10830       r2off = bfd_get_64 (opd->owner, buf);
10831       r2off -= elf_gp (info->output_bfd);
10832     }
10833   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10834   return r2off;
10835 }
10836
10837 static bfd_boolean
10838 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10839 {
10840   struct ppc_stub_hash_entry *stub_entry;
10841   struct ppc_branch_hash_entry *br_entry;
10842   struct bfd_link_info *info;
10843   struct ppc_link_hash_table *htab;
10844   bfd_byte *loc;
10845   bfd_byte *p;
10846   bfd_vma dest, off;
10847   Elf_Internal_Rela *r;
10848   asection *plt;
10849
10850   /* Massage our args to the form they really have.  */
10851   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10852   info = in_arg;
10853
10854   htab = ppc_hash_table (info);
10855   if (htab == NULL)
10856     return FALSE;
10857
10858   /* Make a note of the offset within the stubs for this entry.  */
10859   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10860   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10861
10862   htab->stub_count[stub_entry->stub_type - 1] += 1;
10863   switch (stub_entry->stub_type)
10864     {
10865     case ppc_stub_long_branch:
10866     case ppc_stub_long_branch_r2off:
10867       /* Branches are relative.  This is where we are going to.  */
10868       dest = (stub_entry->target_value
10869               + stub_entry->target_section->output_offset
10870               + stub_entry->target_section->output_section->vma);
10871       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10872       off = dest;
10873
10874       /* And this is where we are coming from.  */
10875       off -= (stub_entry->stub_offset
10876               + stub_entry->group->stub_sec->output_offset
10877               + stub_entry->group->stub_sec->output_section->vma);
10878
10879       p = loc;
10880       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10881         {
10882           bfd_vma r2off = get_r2off (info, stub_entry);
10883
10884           if (r2off == (bfd_vma) -1)
10885             {
10886               htab->stub_error = TRUE;
10887               return FALSE;
10888             }
10889           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10890           p += 4;
10891           if (PPC_HA (r2off) != 0)
10892             {
10893               bfd_put_32 (htab->params->stub_bfd,
10894                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10895               p += 4;
10896             }
10897           if (PPC_LO (r2off) != 0)
10898             {
10899               bfd_put_32 (htab->params->stub_bfd,
10900                           ADDI_R2_R2 | PPC_LO (r2off), p);
10901               p += 4;
10902             }
10903           off -= p - loc;
10904         }
10905       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10906       p += 4;
10907
10908       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10909         {
10910           info->callbacks->einfo
10911             (_("%P: long branch stub `%s' offset overflow\n"),
10912              stub_entry->root.string);
10913           htab->stub_error = TRUE;
10914           return FALSE;
10915         }
10916
10917       if (info->emitrelocations)
10918         {
10919           r = get_relocs (stub_entry->group->stub_sec, 1);
10920           if (r == NULL)
10921             return FALSE;
10922           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10923           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10924           r->r_addend = dest;
10925           if (stub_entry->h != NULL)
10926             {
10927               struct elf_link_hash_entry **hashes;
10928               unsigned long symndx;
10929               struct ppc_link_hash_entry *h;
10930
10931               hashes = elf_sym_hashes (htab->params->stub_bfd);
10932               if (hashes == NULL)
10933                 {
10934                   bfd_size_type hsize;
10935
10936                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10937                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10938                   if (hashes == NULL)
10939                     return FALSE;
10940                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10941                   htab->stub_globals = 1;
10942                 }
10943               symndx = htab->stub_globals++;
10944               h = stub_entry->h;
10945               hashes[symndx] = &h->elf;
10946               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10947               if (h->oh != NULL && h->oh->is_func)
10948                 h = ppc_follow_link (h->oh);
10949               if (h->elf.root.u.def.section != stub_entry->target_section)
10950                 /* H is an opd symbol.  The addend must be zero.  */
10951                 r->r_addend = 0;
10952               else
10953                 {
10954                   off = (h->elf.root.u.def.value
10955                          + h->elf.root.u.def.section->output_offset
10956                          + h->elf.root.u.def.section->output_section->vma);
10957                   r->r_addend -= off;
10958                 }
10959             }
10960         }
10961       break;
10962
10963     case ppc_stub_plt_branch:
10964     case ppc_stub_plt_branch_r2off:
10965       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10966                                          stub_entry->root.string + 9,
10967                                          FALSE, FALSE);
10968       if (br_entry == NULL)
10969         {
10970           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10971                                   stub_entry->root.string);
10972           htab->stub_error = TRUE;
10973           return FALSE;
10974         }
10975
10976       dest = (stub_entry->target_value
10977               + stub_entry->target_section->output_offset
10978               + stub_entry->target_section->output_section->vma);
10979       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10980         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10981
10982       bfd_put_64 (htab->brlt->owner, dest,
10983                   htab->brlt->contents + br_entry->offset);
10984
10985       if (br_entry->iter == htab->stub_iteration)
10986         {
10987           br_entry->iter = 0;
10988
10989           if (htab->relbrlt != NULL)
10990             {
10991               /* Create a reloc for the branch lookup table entry.  */
10992               Elf_Internal_Rela rela;
10993               bfd_byte *rl;
10994
10995               rela.r_offset = (br_entry->offset
10996                                + htab->brlt->output_offset
10997                                + htab->brlt->output_section->vma);
10998               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10999               rela.r_addend = dest;
11000
11001               rl = htab->relbrlt->contents;
11002               rl += (htab->relbrlt->reloc_count++
11003                      * sizeof (Elf64_External_Rela));
11004               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11005             }
11006           else if (info->emitrelocations)
11007             {
11008               r = get_relocs (htab->brlt, 1);
11009               if (r == NULL)
11010                 return FALSE;
11011               /* brlt, being SEC_LINKER_CREATED does not go through the
11012                  normal reloc processing.  Symbols and offsets are not
11013                  translated from input file to output file form, so
11014                  set up the offset per the output file.  */
11015               r->r_offset = (br_entry->offset
11016                              + htab->brlt->output_offset
11017                              + htab->brlt->output_section->vma);
11018               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11019               r->r_addend = dest;
11020             }
11021         }
11022
11023       dest = (br_entry->offset
11024               + htab->brlt->output_offset
11025               + htab->brlt->output_section->vma);
11026
11027       off = (dest
11028              - elf_gp (info->output_bfd)
11029              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11030
11031       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11032         {
11033           info->callbacks->einfo
11034             (_("%P: linkage table error against `%T'\n"),
11035              stub_entry->root.string);
11036           bfd_set_error (bfd_error_bad_value);
11037           htab->stub_error = TRUE;
11038           return FALSE;
11039         }
11040
11041       if (info->emitrelocations)
11042         {
11043           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11044           if (r == NULL)
11045             return FALSE;
11046           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11047           if (bfd_big_endian (info->output_bfd))
11048             r[0].r_offset += 2;
11049           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11050             r[0].r_offset += 4;
11051           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11052           r[0].r_addend = dest;
11053           if (PPC_HA (off) != 0)
11054             {
11055               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11056               r[1].r_offset = r[0].r_offset + 4;
11057               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11058               r[1].r_addend = r[0].r_addend;
11059             }
11060         }
11061
11062       p = loc;
11063       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11064         {
11065           if (PPC_HA (off) != 0)
11066             {
11067               bfd_put_32 (htab->params->stub_bfd,
11068                           ADDIS_R12_R2 | PPC_HA (off), p);
11069               p += 4;
11070               bfd_put_32 (htab->params->stub_bfd,
11071                           LD_R12_0R12 | PPC_LO (off), p);
11072             }
11073           else
11074             bfd_put_32 (htab->params->stub_bfd,
11075                         LD_R12_0R2 | PPC_LO (off), p);
11076         }
11077       else
11078         {
11079           bfd_vma r2off = get_r2off (info, stub_entry);
11080
11081           if (r2off == (bfd_vma) -1)
11082             {
11083               htab->stub_error = TRUE;
11084               return FALSE;
11085             }
11086
11087           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11088           p += 4;
11089           if (PPC_HA (off) != 0)
11090             {
11091               bfd_put_32 (htab->params->stub_bfd,
11092                           ADDIS_R12_R2 | PPC_HA (off), p);
11093               p += 4;
11094               bfd_put_32 (htab->params->stub_bfd,
11095                           LD_R12_0R12 | PPC_LO (off), p);
11096             }
11097           else
11098             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11099
11100           if (PPC_HA (r2off) != 0)
11101             {
11102               p += 4;
11103               bfd_put_32 (htab->params->stub_bfd,
11104                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11105             }
11106           if (PPC_LO (r2off) != 0)
11107             {
11108               p += 4;
11109               bfd_put_32 (htab->params->stub_bfd,
11110                           ADDI_R2_R2 | PPC_LO (r2off), p);
11111             }
11112         }
11113       p += 4;
11114       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11115       p += 4;
11116       p = output_bctr (htab, htab->params->stub_bfd, p);
11117       break;
11118
11119     case ppc_stub_plt_call:
11120     case ppc_stub_plt_call_r2save:
11121       if (stub_entry->h != NULL
11122           && stub_entry->h->is_func_descriptor
11123           && stub_entry->h->oh != NULL)
11124         {
11125           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11126
11127           /* If the old-ABI "dot-symbol" is undefined make it weak so
11128              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11129           if (fh->elf.root.type == bfd_link_hash_undefined
11130               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11131                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11132             fh->elf.root.type = bfd_link_hash_undefweak;
11133         }
11134
11135       /* Now build the stub.  */
11136       dest = stub_entry->plt_ent->plt.offset & ~1;
11137       if (dest >= (bfd_vma) -2)
11138         abort ();
11139
11140       plt = htab->elf.splt;
11141       if (!htab->elf.dynamic_sections_created
11142           || stub_entry->h == NULL
11143           || stub_entry->h->elf.dynindx == -1)
11144         plt = htab->elf.iplt;
11145
11146       dest += plt->output_offset + plt->output_section->vma;
11147
11148       if (stub_entry->h == NULL
11149           && (stub_entry->plt_ent->plt.offset & 1) == 0)
11150         {
11151           Elf_Internal_Rela rela;
11152           bfd_byte *rl;
11153
11154           rela.r_offset = dest;
11155           if (htab->opd_abi)
11156             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11157           else
11158             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11159           rela.r_addend = (stub_entry->target_value
11160                            + stub_entry->target_section->output_offset
11161                            + stub_entry->target_section->output_section->vma);
11162
11163           rl = (htab->elf.irelplt->contents
11164                 + (htab->elf.irelplt->reloc_count++
11165                    * sizeof (Elf64_External_Rela)));
11166           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11167           stub_entry->plt_ent->plt.offset |= 1;
11168           htab->local_ifunc_resolver = 1;
11169         }
11170
11171       off = (dest
11172              - elf_gp (info->output_bfd)
11173              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11174
11175       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11176         {
11177           info->callbacks->einfo
11178             /* xgettext:c-format */
11179             (_("%P: linkage table error against `%T'\n"),
11180              stub_entry->h != NULL
11181              ? stub_entry->h->elf.root.root.string
11182              : "<local sym>");
11183           bfd_set_error (bfd_error_bad_value);
11184           htab->stub_error = TRUE;
11185           return FALSE;
11186         }
11187
11188       if (htab->params->plt_stub_align != 0)
11189         {
11190           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11191
11192           stub_entry->group->stub_sec->size += pad;
11193           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11194           loc += pad;
11195         }
11196
11197       r = NULL;
11198       if (info->emitrelocations)
11199         {
11200           r = get_relocs (stub_entry->group->stub_sec,
11201                           ((PPC_HA (off) != 0)
11202                            + (htab->opd_abi
11203                               ? 2 + (htab->params->plt_static_chain
11204                                      && PPC_HA (off + 16) == PPC_HA (off))
11205                               : 1)));
11206           if (r == NULL)
11207             return FALSE;
11208           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11209           if (bfd_big_endian (info->output_bfd))
11210             r[0].r_offset += 2;
11211           r[0].r_addend = dest;
11212         }
11213       if (stub_entry->h != NULL
11214           && (stub_entry->h == htab->tls_get_addr_fd
11215               || stub_entry->h == htab->tls_get_addr)
11216           && htab->params->tls_get_addr_opt)
11217         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11218       else
11219         p = build_plt_stub (htab, stub_entry, loc, off, r);
11220       break;
11221
11222     case ppc_stub_save_res:
11223       return TRUE;
11224
11225     default:
11226       BFD_FAIL ();
11227       return FALSE;
11228     }
11229
11230   stub_entry->group->stub_sec->size += p - loc;
11231
11232   if (htab->params->emit_stub_syms)
11233     {
11234       struct elf_link_hash_entry *h;
11235       size_t len1, len2;
11236       char *name;
11237       const char *const stub_str[] = { "long_branch",
11238                                        "long_branch_r2off",
11239                                        "plt_branch",
11240                                        "plt_branch_r2off",
11241                                        "plt_call",
11242                                        "plt_call" };
11243
11244       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11245       len2 = strlen (stub_entry->root.string);
11246       name = bfd_malloc (len1 + len2 + 2);
11247       if (name == NULL)
11248         return FALSE;
11249       memcpy (name, stub_entry->root.string, 9);
11250       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11251       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11252       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11253       if (h == NULL)
11254         return FALSE;
11255       if (h->root.type == bfd_link_hash_new)
11256         {
11257           h->root.type = bfd_link_hash_defined;
11258           h->root.u.def.section = stub_entry->group->stub_sec;
11259           h->root.u.def.value = stub_entry->stub_offset;
11260           h->ref_regular = 1;
11261           h->def_regular = 1;
11262           h->ref_regular_nonweak = 1;
11263           h->forced_local = 1;
11264           h->non_elf = 0;
11265           h->root.linker_def = 1;
11266         }
11267     }
11268
11269   return TRUE;
11270 }
11271
11272 /* As above, but don't actually build the stub.  Just bump offset so
11273    we know stub section sizes, and select plt_branch stubs where
11274    long_branch stubs won't do.  */
11275
11276 static bfd_boolean
11277 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11278 {
11279   struct ppc_stub_hash_entry *stub_entry;
11280   struct bfd_link_info *info;
11281   struct ppc_link_hash_table *htab;
11282   bfd_vma off;
11283   int size;
11284
11285   /* Massage our args to the form they really have.  */
11286   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11287   info = in_arg;
11288
11289   htab = ppc_hash_table (info);
11290   if (htab == NULL)
11291     return FALSE;
11292
11293   if (stub_entry->h != NULL
11294       && stub_entry->h->save_res
11295       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11296       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11297     {
11298       /* Don't make stubs to out-of-line register save/restore
11299          functions.  Instead, emit copies of the functions.  */
11300       stub_entry->group->needs_save_res = 1;
11301       stub_entry->stub_type = ppc_stub_save_res;
11302       return TRUE;
11303     }
11304
11305   if (stub_entry->stub_type == ppc_stub_plt_call
11306       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11307     {
11308       asection *plt;
11309       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11310       if (off >= (bfd_vma) -2)
11311         abort ();
11312       plt = htab->elf.splt;
11313       if (!htab->elf.dynamic_sections_created
11314           || stub_entry->h == NULL
11315           || stub_entry->h->elf.dynindx == -1)
11316         plt = htab->elf.iplt;
11317       off += (plt->output_offset
11318               + plt->output_section->vma
11319               - elf_gp (info->output_bfd)
11320               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11321
11322       size = plt_stub_size (htab, stub_entry, off);
11323       if (stub_entry->h != NULL
11324           && (stub_entry->h == htab->tls_get_addr_fd
11325               || stub_entry->h == htab->tls_get_addr)
11326           && htab->params->tls_get_addr_opt
11327           && (ALWAYS_EMIT_R2SAVE
11328               || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11329         stub_entry->group->tls_get_addr_opt_bctrl
11330           = stub_entry->group->stub_sec->size + size - 5 * 4;
11331
11332       if (htab->params->plt_stub_align)
11333         size += plt_stub_pad (htab, stub_entry, off);
11334       if (info->emitrelocations)
11335         {
11336           stub_entry->group->stub_sec->reloc_count
11337             += ((PPC_HA (off) != 0)
11338                 + (htab->opd_abi
11339                    ? 2 + (htab->params->plt_static_chain
11340                           && PPC_HA (off + 16) == PPC_HA (off))
11341                    : 1));
11342           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11343         }
11344     }
11345   else
11346     {
11347       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11348          variants.  */
11349       bfd_vma r2off = 0;
11350       bfd_vma local_off = 0;
11351
11352       off = (stub_entry->target_value
11353              + stub_entry->target_section->output_offset
11354              + stub_entry->target_section->output_section->vma);
11355       off -= (stub_entry->group->stub_sec->size
11356               + stub_entry->group->stub_sec->output_offset
11357               + stub_entry->group->stub_sec->output_section->vma);
11358
11359       /* Reset the stub type from the plt variant in case we now
11360          can reach with a shorter stub.  */
11361       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11362         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11363
11364       size = 4;
11365       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11366         {
11367           r2off = get_r2off (info, stub_entry);
11368           if (r2off == (bfd_vma) -1)
11369             {
11370               htab->stub_error = TRUE;
11371               return FALSE;
11372             }
11373           size = 8;
11374           if (PPC_HA (r2off) != 0)
11375             size += 4;
11376           if (PPC_LO (r2off) != 0)
11377             size += 4;
11378           off -= size - 4;
11379         }
11380
11381       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11382
11383       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11384          Do the same for -R objects without function descriptors.  */
11385       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11386           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11387               && r2off == 0
11388               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11389         {
11390           struct ppc_branch_hash_entry *br_entry;
11391
11392           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11393                                              stub_entry->root.string + 9,
11394                                              TRUE, FALSE);
11395           if (br_entry == NULL)
11396             {
11397               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11398                                       stub_entry->root.string);
11399               htab->stub_error = TRUE;
11400               return FALSE;
11401             }
11402
11403           if (br_entry->iter != htab->stub_iteration)
11404             {
11405               br_entry->iter = htab->stub_iteration;
11406               br_entry->offset = htab->brlt->size;
11407               htab->brlt->size += 8;
11408
11409               if (htab->relbrlt != NULL)
11410                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11411               else if (info->emitrelocations)
11412                 {
11413                   htab->brlt->reloc_count += 1;
11414                   htab->brlt->flags |= SEC_RELOC;
11415                 }
11416             }
11417
11418           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11419           off = (br_entry->offset
11420                  + htab->brlt->output_offset
11421                  + htab->brlt->output_section->vma
11422                  - elf_gp (info->output_bfd)
11423                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11424
11425           if (info->emitrelocations)
11426             {
11427               stub_entry->group->stub_sec->reloc_count
11428                 += 1 + (PPC_HA (off) != 0);
11429               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11430             }
11431
11432           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11433             {
11434               size = 12;
11435               if (PPC_HA (off) != 0)
11436                 size = 16;
11437             }
11438           else
11439             {
11440               size = 16;
11441               if (PPC_HA (off) != 0)
11442                 size += 4;
11443
11444               if (PPC_HA (r2off) != 0)
11445                 size += 4;
11446               if (PPC_LO (r2off) != 0)
11447                 size += 4;
11448             }
11449           if (!htab->params->speculate_indirect_jumps)
11450             size += 8;
11451         }
11452       else if (info->emitrelocations)
11453         {
11454           stub_entry->group->stub_sec->reloc_count += 1;
11455           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11456         }
11457     }
11458
11459   stub_entry->group->stub_sec->size += size;
11460   return TRUE;
11461 }
11462
11463 /* Set up various things so that we can make a list of input sections
11464    for each output section included in the link.  Returns -1 on error,
11465    0 when no stubs will be needed, and 1 on success.  */
11466
11467 int
11468 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11469 {
11470   unsigned int id;
11471   bfd_size_type amt;
11472   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11473
11474   if (htab == NULL)
11475     return -1;
11476
11477   htab->sec_info_arr_size = bfd_get_next_section_id ();
11478   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11479   htab->sec_info = bfd_zmalloc (amt);
11480   if (htab->sec_info == NULL)
11481     return -1;
11482
11483   /* Set toc_off for com, und, abs and ind sections.  */
11484   for (id = 0; id < 3; id++)
11485     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11486
11487   return 1;
11488 }
11489
11490 /* Set up for first pass at multitoc partitioning.  */
11491
11492 void
11493 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11494 {
11495   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11496
11497   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11498   htab->toc_bfd = NULL;
11499   htab->toc_first_sec = NULL;
11500 }
11501
11502 /* The linker repeatedly calls this function for each TOC input section
11503    and linker generated GOT section.  Group input bfds such that the toc
11504    within a group is less than 64k in size.  */
11505
11506 bfd_boolean
11507 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11508 {
11509   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11510   bfd_vma addr, off, limit;
11511
11512   if (htab == NULL)
11513     return FALSE;
11514
11515   if (!htab->second_toc_pass)
11516     {
11517       /* Keep track of the first .toc or .got section for this input bfd.  */
11518       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11519
11520       if (new_bfd)
11521         {
11522           htab->toc_bfd = isec->owner;
11523           htab->toc_first_sec = isec;
11524         }
11525
11526       addr = isec->output_offset + isec->output_section->vma;
11527       off = addr - htab->toc_curr;
11528       limit = 0x80008000;
11529       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11530         limit = 0x10000;
11531       if (off + isec->size > limit)
11532         {
11533           addr = (htab->toc_first_sec->output_offset
11534                   + htab->toc_first_sec->output_section->vma);
11535           htab->toc_curr = addr;
11536           htab->toc_curr &= -TOC_BASE_ALIGN;
11537         }
11538
11539       /* toc_curr is the base address of this toc group.  Set elf_gp
11540          for the input section to be the offset relative to the
11541          output toc base plus 0x8000.  Making the input elf_gp an
11542          offset allows us to move the toc as a whole without
11543          recalculating input elf_gp.  */
11544       off = htab->toc_curr - elf_gp (info->output_bfd);
11545       off += TOC_BASE_OFF;
11546
11547       /* Die if someone uses a linker script that doesn't keep input
11548          file .toc and .got together.  */
11549       if (new_bfd
11550           && elf_gp (isec->owner) != 0
11551           && elf_gp (isec->owner) != off)
11552         return FALSE;
11553
11554       elf_gp (isec->owner) = off;
11555       return TRUE;
11556     }
11557
11558   /* During the second pass toc_first_sec points to the start of
11559      a toc group, and toc_curr is used to track the old elf_gp.
11560      We use toc_bfd to ensure we only look at each bfd once.  */
11561   if (htab->toc_bfd == isec->owner)
11562     return TRUE;
11563   htab->toc_bfd = isec->owner;
11564
11565   if (htab->toc_first_sec == NULL
11566       || htab->toc_curr != elf_gp (isec->owner))
11567     {
11568       htab->toc_curr = elf_gp (isec->owner);
11569       htab->toc_first_sec = isec;
11570     }
11571   addr = (htab->toc_first_sec->output_offset
11572           + htab->toc_first_sec->output_section->vma);
11573   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11574   elf_gp (isec->owner) = off;
11575
11576   return TRUE;
11577 }
11578
11579 /* Called via elf_link_hash_traverse to merge GOT entries for global
11580    symbol H.  */
11581
11582 static bfd_boolean
11583 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11584 {
11585   if (h->root.type == bfd_link_hash_indirect)
11586     return TRUE;
11587
11588   merge_got_entries (&h->got.glist);
11589
11590   return TRUE;
11591 }
11592
11593 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11594    symbol H.  */
11595
11596 static bfd_boolean
11597 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11598 {
11599   struct got_entry *gent;
11600
11601   if (h->root.type == bfd_link_hash_indirect)
11602     return TRUE;
11603
11604   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11605     if (!gent->is_indirect)
11606       allocate_got (h, (struct bfd_link_info *) inf, gent);
11607   return TRUE;
11608 }
11609
11610 /* Called on the first multitoc pass after the last call to
11611    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11612    entries.  */
11613
11614 bfd_boolean
11615 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11616 {
11617   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11618   struct bfd *ibfd, *ibfd2;
11619   bfd_boolean done_something;
11620
11621   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11622
11623   if (!htab->do_multi_toc)
11624     return FALSE;
11625
11626   /* Merge global sym got entries within a toc group.  */
11627   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11628
11629   /* And tlsld_got.  */
11630   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11631     {
11632       struct got_entry *ent, *ent2;
11633
11634       if (!is_ppc64_elf (ibfd))
11635         continue;
11636
11637       ent = ppc64_tlsld_got (ibfd);
11638       if (!ent->is_indirect
11639           && ent->got.offset != (bfd_vma) -1)
11640         {
11641           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11642             {
11643               if (!is_ppc64_elf (ibfd2))
11644                 continue;
11645
11646               ent2 = ppc64_tlsld_got (ibfd2);
11647               if (!ent2->is_indirect
11648                   && ent2->got.offset != (bfd_vma) -1
11649                   && elf_gp (ibfd2) == elf_gp (ibfd))
11650                 {
11651                   ent2->is_indirect = TRUE;
11652                   ent2->got.ent = ent;
11653                 }
11654             }
11655         }
11656     }
11657
11658   /* Zap sizes of got sections.  */
11659   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11660   htab->elf.irelplt->size -= htab->got_reli_size;
11661   htab->got_reli_size = 0;
11662
11663   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11664     {
11665       asection *got, *relgot;
11666
11667       if (!is_ppc64_elf (ibfd))
11668         continue;
11669
11670       got = ppc64_elf_tdata (ibfd)->got;
11671       if (got != NULL)
11672         {
11673           got->rawsize = got->size;
11674           got->size = 0;
11675           relgot = ppc64_elf_tdata (ibfd)->relgot;
11676           relgot->rawsize = relgot->size;
11677           relgot->size = 0;
11678         }
11679     }
11680
11681   /* Now reallocate the got, local syms first.  We don't need to
11682      allocate section contents again since we never increase size.  */
11683   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11684     {
11685       struct got_entry **lgot_ents;
11686       struct got_entry **end_lgot_ents;
11687       struct plt_entry **local_plt;
11688       struct plt_entry **end_local_plt;
11689       unsigned char *lgot_masks;
11690       bfd_size_type locsymcount;
11691       Elf_Internal_Shdr *symtab_hdr;
11692       asection *s;
11693
11694       if (!is_ppc64_elf (ibfd))
11695         continue;
11696
11697       lgot_ents = elf_local_got_ents (ibfd);
11698       if (!lgot_ents)
11699         continue;
11700
11701       symtab_hdr = &elf_symtab_hdr (ibfd);
11702       locsymcount = symtab_hdr->sh_info;
11703       end_lgot_ents = lgot_ents + locsymcount;
11704       local_plt = (struct plt_entry **) end_lgot_ents;
11705       end_local_plt = local_plt + locsymcount;
11706       lgot_masks = (unsigned char *) end_local_plt;
11707       s = ppc64_elf_tdata (ibfd)->got;
11708       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11709         {
11710           struct got_entry *ent;
11711
11712           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11713             {
11714               unsigned int ent_size = 8;
11715               unsigned int rel_size = sizeof (Elf64_External_Rela);
11716
11717               ent->got.offset = s->size;
11718               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11719                 {
11720                   ent_size *= 2;
11721                   rel_size *= 2;
11722                 }
11723               s->size += ent_size;
11724               if ((*lgot_masks & PLT_IFUNC) != 0)
11725                 {
11726                   htab->elf.irelplt->size += rel_size;
11727                   htab->got_reli_size += rel_size;
11728                 }
11729               else if (bfd_link_pic (info))
11730                 {
11731                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11732                   srel->size += rel_size;
11733                 }
11734             }
11735         }
11736     }
11737
11738   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11739
11740   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11741     {
11742       struct got_entry *ent;
11743
11744       if (!is_ppc64_elf (ibfd))
11745         continue;
11746
11747       ent = ppc64_tlsld_got (ibfd);
11748       if (!ent->is_indirect
11749           && ent->got.offset != (bfd_vma) -1)
11750         {
11751           asection *s = ppc64_elf_tdata (ibfd)->got;
11752           ent->got.offset = s->size;
11753           s->size += 16;
11754           if (bfd_link_pic (info))
11755             {
11756               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11757               srel->size += sizeof (Elf64_External_Rela);
11758             }
11759         }
11760     }
11761
11762   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11763   if (!done_something)
11764     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11765       {
11766         asection *got;
11767
11768         if (!is_ppc64_elf (ibfd))
11769           continue;
11770
11771         got = ppc64_elf_tdata (ibfd)->got;
11772         if (got != NULL)
11773           {
11774             done_something = got->rawsize != got->size;
11775             if (done_something)
11776               break;
11777           }
11778       }
11779
11780   if (done_something)
11781     (*htab->params->layout_sections_again) ();
11782
11783   /* Set up for second pass over toc sections to recalculate elf_gp
11784      on input sections.  */
11785   htab->toc_bfd = NULL;
11786   htab->toc_first_sec = NULL;
11787   htab->second_toc_pass = TRUE;
11788   return done_something;
11789 }
11790
11791 /* Called after second pass of multitoc partitioning.  */
11792
11793 void
11794 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11795 {
11796   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11797
11798   /* After the second pass, toc_curr tracks the TOC offset used
11799      for code sections below in ppc64_elf_next_input_section.  */
11800   htab->toc_curr = TOC_BASE_OFF;
11801 }
11802
11803 /* No toc references were found in ISEC.  If the code in ISEC makes no
11804    calls, then there's no need to use toc adjusting stubs when branching
11805    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11806    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11807    needed, and 2 if a cyclical call-graph was found but no other reason
11808    for a stub was detected.  If called from the top level, a return of
11809    2 means the same as a return of 0.  */
11810
11811 static int
11812 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11813 {
11814   int ret;
11815
11816   /* Mark this section as checked.  */
11817   isec->call_check_done = 1;
11818
11819   /* We know none of our code bearing sections will need toc stubs.  */
11820   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11821     return 0;
11822
11823   if (isec->size == 0)
11824     return 0;
11825
11826   if (isec->output_section == NULL)
11827     return 0;
11828
11829   ret = 0;
11830   if (isec->reloc_count != 0)
11831     {
11832       Elf_Internal_Rela *relstart, *rel;
11833       Elf_Internal_Sym *local_syms;
11834       struct ppc_link_hash_table *htab;
11835
11836       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11837                                             info->keep_memory);
11838       if (relstart == NULL)
11839         return -1;
11840
11841       /* Look for branches to outside of this section.  */
11842       local_syms = NULL;
11843       htab = ppc_hash_table (info);
11844       if (htab == NULL)
11845         return -1;
11846
11847       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11848         {
11849           enum elf_ppc64_reloc_type r_type;
11850           unsigned long r_symndx;
11851           struct elf_link_hash_entry *h;
11852           struct ppc_link_hash_entry *eh;
11853           Elf_Internal_Sym *sym;
11854           asection *sym_sec;
11855           struct _opd_sec_data *opd;
11856           bfd_vma sym_value;
11857           bfd_vma dest;
11858
11859           r_type = ELF64_R_TYPE (rel->r_info);
11860           if (r_type != R_PPC64_REL24
11861               && r_type != R_PPC64_REL14
11862               && r_type != R_PPC64_REL14_BRTAKEN
11863               && r_type != R_PPC64_REL14_BRNTAKEN)
11864             continue;
11865
11866           r_symndx = ELF64_R_SYM (rel->r_info);
11867           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11868                           isec->owner))
11869             {
11870               ret = -1;
11871               break;
11872             }
11873
11874           /* Calls to dynamic lib functions go through a plt call stub
11875              that uses r2.  */
11876           eh = (struct ppc_link_hash_entry *) h;
11877           if (eh != NULL
11878               && (eh->elf.plt.plist != NULL
11879                   || (eh->oh != NULL
11880                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11881             {
11882               ret = 1;
11883               break;
11884             }
11885
11886           if (sym_sec == NULL)
11887             /* Ignore other undefined symbols.  */
11888             continue;
11889
11890           /* Assume branches to other sections not included in the
11891              link need stubs too, to cover -R and absolute syms.  */
11892           if (sym_sec->output_section == NULL)
11893             {
11894               ret = 1;
11895               break;
11896             }
11897
11898           if (h == NULL)
11899             sym_value = sym->st_value;
11900           else
11901             {
11902               if (h->root.type != bfd_link_hash_defined
11903                   && h->root.type != bfd_link_hash_defweak)
11904                 abort ();
11905               sym_value = h->root.u.def.value;
11906             }
11907           sym_value += rel->r_addend;
11908
11909           /* If this branch reloc uses an opd sym, find the code section.  */
11910           opd = get_opd_info (sym_sec);
11911           if (opd != NULL)
11912             {
11913               if (h == NULL && opd->adjust != NULL)
11914                 {
11915                   long adjust;
11916
11917                   adjust = opd->adjust[OPD_NDX (sym_value)];
11918                   if (adjust == -1)
11919                     /* Assume deleted functions won't ever be called.  */
11920                     continue;
11921                   sym_value += adjust;
11922                 }
11923
11924               dest = opd_entry_value (sym_sec, sym_value,
11925                                       &sym_sec, NULL, FALSE);
11926               if (dest == (bfd_vma) -1)
11927                 continue;
11928             }
11929           else
11930             dest = (sym_value
11931                     + sym_sec->output_offset
11932                     + sym_sec->output_section->vma);
11933
11934           /* Ignore branch to self.  */
11935           if (sym_sec == isec)
11936             continue;
11937
11938           /* If the called function uses the toc, we need a stub.  */
11939           if (sym_sec->has_toc_reloc
11940               || sym_sec->makes_toc_func_call)
11941             {
11942               ret = 1;
11943               break;
11944             }
11945
11946           /* Assume any branch that needs a long branch stub might in fact
11947              need a plt_branch stub.  A plt_branch stub uses r2.  */
11948           else if (dest - (isec->output_offset
11949                            + isec->output_section->vma
11950                            + rel->r_offset) + (1 << 25)
11951                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11952                                                              ? h->other
11953                                                              : sym->st_other))
11954             {
11955               ret = 1;
11956               break;
11957             }
11958
11959           /* If calling back to a section in the process of being
11960              tested, we can't say for sure that no toc adjusting stubs
11961              are needed, so don't return zero.  */
11962           else if (sym_sec->call_check_in_progress)
11963             ret = 2;
11964
11965           /* Branches to another section that itself doesn't have any TOC
11966              references are OK.  Recursively call ourselves to check.  */
11967           else if (!sym_sec->call_check_done)
11968             {
11969               int recur;
11970
11971               /* Mark current section as indeterminate, so that other
11972                  sections that call back to current won't be marked as
11973                  known.  */
11974               isec->call_check_in_progress = 1;
11975               recur = toc_adjusting_stub_needed (info, sym_sec);
11976               isec->call_check_in_progress = 0;
11977
11978               if (recur != 0)
11979                 {
11980                   ret = recur;
11981                   if (recur != 2)
11982                     break;
11983                 }
11984             }
11985         }
11986
11987       if (local_syms != NULL
11988           && (elf_symtab_hdr (isec->owner).contents
11989               != (unsigned char *) local_syms))
11990         free (local_syms);
11991       if (elf_section_data (isec)->relocs != relstart)
11992         free (relstart);
11993     }
11994
11995   if ((ret & 1) == 0
11996       && isec->map_head.s != NULL
11997       && (strcmp (isec->output_section->name, ".init") == 0
11998           || strcmp (isec->output_section->name, ".fini") == 0))
11999     {
12000       if (isec->map_head.s->has_toc_reloc
12001           || isec->map_head.s->makes_toc_func_call)
12002         ret = 1;
12003       else if (!isec->map_head.s->call_check_done)
12004         {
12005           int recur;
12006           isec->call_check_in_progress = 1;
12007           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12008           isec->call_check_in_progress = 0;
12009           if (recur != 0)
12010             ret = recur;
12011         }
12012     }
12013
12014   if (ret == 1)
12015     isec->makes_toc_func_call = 1;
12016
12017   return ret;
12018 }
12019
12020 /* The linker repeatedly calls this function for each input section,
12021    in the order that input sections are linked into output sections.
12022    Build lists of input sections to determine groupings between which
12023    we may insert linker stubs.  */
12024
12025 bfd_boolean
12026 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12027 {
12028   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12029
12030   if (htab == NULL)
12031     return FALSE;
12032
12033   if ((isec->output_section->flags & SEC_CODE) != 0
12034       && isec->output_section->id < htab->sec_info_arr_size)
12035     {
12036       /* This happens to make the list in reverse order,
12037          which is what we want.  */
12038       htab->sec_info[isec->id].u.list
12039         = htab->sec_info[isec->output_section->id].u.list;
12040       htab->sec_info[isec->output_section->id].u.list = isec;
12041     }
12042
12043   if (htab->multi_toc_needed)
12044     {
12045       /* Analyse sections that aren't already flagged as needing a
12046          valid toc pointer.  Exclude .fixup for the linux kernel.
12047          .fixup contains branches, but only back to the function that
12048          hit an exception.  */
12049       if (!(isec->has_toc_reloc
12050             || (isec->flags & SEC_CODE) == 0
12051             || strcmp (isec->name, ".fixup") == 0
12052             || isec->call_check_done))
12053         {
12054           if (toc_adjusting_stub_needed (info, isec) < 0)
12055             return FALSE;
12056         }
12057       /* Make all sections use the TOC assigned for this object file.
12058          This will be wrong for pasted sections;  We fix that in
12059          check_pasted_section().  */
12060       if (elf_gp (isec->owner) != 0)
12061         htab->toc_curr = elf_gp (isec->owner);
12062     }
12063
12064   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12065   return TRUE;
12066 }
12067
12068 /* Check that all .init and .fini sections use the same toc, if they
12069    have toc relocs.  */
12070
12071 static bfd_boolean
12072 check_pasted_section (struct bfd_link_info *info, const char *name)
12073 {
12074   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12075
12076   if (o != NULL)
12077     {
12078       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12079       bfd_vma toc_off = 0;
12080       asection *i;
12081
12082       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12083         if (i->has_toc_reloc)
12084           {
12085             if (toc_off == 0)
12086               toc_off = htab->sec_info[i->id].toc_off;
12087             else if (toc_off != htab->sec_info[i->id].toc_off)
12088               return FALSE;
12089           }
12090
12091       if (toc_off == 0)
12092         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12093           if (i->makes_toc_func_call)
12094             {
12095               toc_off = htab->sec_info[i->id].toc_off;
12096               break;
12097             }
12098
12099       /* Make sure the whole pasted function uses the same toc offset.  */
12100       if (toc_off != 0)
12101         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12102           htab->sec_info[i->id].toc_off = toc_off;
12103     }
12104   return TRUE;
12105 }
12106
12107 bfd_boolean
12108 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12109 {
12110   return (check_pasted_section (info, ".init")
12111           & check_pasted_section (info, ".fini"));
12112 }
12113
12114 /* See whether we can group stub sections together.  Grouping stub
12115    sections may result in fewer stubs.  More importantly, we need to
12116    put all .init* and .fini* stubs at the beginning of the .init or
12117    .fini output sections respectively, because glibc splits the
12118    _init and _fini functions into multiple parts.  Putting a stub in
12119    the middle of a function is not a good idea.  */
12120
12121 static bfd_boolean
12122 group_sections (struct bfd_link_info *info,
12123                 bfd_size_type stub_group_size,
12124                 bfd_boolean stubs_always_before_branch)
12125 {
12126   struct ppc_link_hash_table *htab;
12127   asection *osec;
12128   bfd_boolean suppress_size_errors;
12129
12130   htab = ppc_hash_table (info);
12131   if (htab == NULL)
12132     return FALSE;
12133
12134   suppress_size_errors = FALSE;
12135   if (stub_group_size == 1)
12136     {
12137       /* Default values.  */
12138       if (stubs_always_before_branch)
12139         stub_group_size = 0x1e00000;
12140       else
12141         stub_group_size = 0x1c00000;
12142       suppress_size_errors = TRUE;
12143     }
12144
12145   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12146     {
12147       asection *tail;
12148
12149       if (osec->id >= htab->sec_info_arr_size)
12150         continue;
12151
12152       tail = htab->sec_info[osec->id].u.list;
12153       while (tail != NULL)
12154         {
12155           asection *curr;
12156           asection *prev;
12157           bfd_size_type total;
12158           bfd_boolean big_sec;
12159           bfd_vma curr_toc;
12160           struct map_stub *group;
12161           bfd_size_type group_size;
12162
12163           curr = tail;
12164           total = tail->size;
12165           group_size = (ppc64_elf_section_data (tail) != NULL
12166                         && ppc64_elf_section_data (tail)->has_14bit_branch
12167                         ? stub_group_size >> 10 : stub_group_size);
12168
12169           big_sec = total > group_size;
12170           if (big_sec && !suppress_size_errors)
12171             /* xgettext:c-format */
12172             _bfd_error_handler (_("%B section %A exceeds stub group size"),
12173                                 tail->owner, tail);
12174           curr_toc = htab->sec_info[tail->id].toc_off;
12175
12176           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12177                  && ((total += curr->output_offset - prev->output_offset)
12178                      < (ppc64_elf_section_data (prev) != NULL
12179                         && ppc64_elf_section_data (prev)->has_14bit_branch
12180                         ? (group_size = stub_group_size >> 10) : group_size))
12181                  && htab->sec_info[prev->id].toc_off == curr_toc)
12182             curr = prev;
12183
12184           /* OK, the size from the start of CURR to the end is less
12185              than group_size and thus can be handled by one stub
12186              section.  (or the tail section is itself larger than
12187              group_size, in which case we may be toast.)  We should
12188              really be keeping track of the total size of stubs added
12189              here, as stubs contribute to the final output section
12190              size.  That's a little tricky, and this way will only
12191              break if stubs added make the total size more than 2^25,
12192              ie. for the default stub_group_size, if stubs total more
12193              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12194           group = bfd_alloc (curr->owner, sizeof (*group));
12195           if (group == NULL)
12196             return FALSE;
12197           group->link_sec = curr;
12198           group->stub_sec = NULL;
12199           group->needs_save_res = 0;
12200           group->tls_get_addr_opt_bctrl = -1u;
12201           group->next = htab->group;
12202           htab->group = group;
12203           do
12204             {
12205               prev = htab->sec_info[tail->id].u.list;
12206               /* Set up this stub group.  */
12207               htab->sec_info[tail->id].u.group = group;
12208             }
12209           while (tail != curr && (tail = prev) != NULL);
12210
12211           /* But wait, there's more!  Input sections up to group_size
12212              bytes before the stub section can be handled by it too.
12213              Don't do this if we have a really large section after the
12214              stubs, as adding more stubs increases the chance that
12215              branches may not reach into the stub section.  */
12216           if (!stubs_always_before_branch && !big_sec)
12217             {
12218               total = 0;
12219               while (prev != NULL
12220                      && ((total += tail->output_offset - prev->output_offset)
12221                          < (ppc64_elf_section_data (prev) != NULL
12222                             && ppc64_elf_section_data (prev)->has_14bit_branch
12223                             ? (group_size = stub_group_size >> 10) : group_size))
12224                      && htab->sec_info[prev->id].toc_off == curr_toc)
12225                 {
12226                   tail = prev;
12227                   prev = htab->sec_info[tail->id].u.list;
12228                   htab->sec_info[tail->id].u.group = group;
12229                 }
12230             }
12231           tail = prev;
12232         }
12233     }
12234   return TRUE;
12235 }
12236
12237 static const unsigned char glink_eh_frame_cie[] =
12238 {
12239   0, 0, 0, 16,                          /* length.  */
12240   0, 0, 0, 0,                           /* id.  */
12241   1,                                    /* CIE version.  */
12242   'z', 'R', 0,                          /* Augmentation string.  */
12243   4,                                    /* Code alignment.  */
12244   0x78,                                 /* Data alignment.  */
12245   65,                                   /* RA reg.  */
12246   1,                                    /* Augmentation size.  */
12247   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12248   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12249 };
12250
12251 static size_t
12252 stub_eh_frame_size (struct map_stub *group, size_t align)
12253 {
12254   size_t this_size = 17;
12255   if (group->tls_get_addr_opt_bctrl != -1u)
12256     {
12257       unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12258       if (to_bctrl < 64)
12259         this_size += 1;
12260       else if (to_bctrl < 256)
12261         this_size += 2;
12262       else if (to_bctrl < 65536)
12263         this_size += 3;
12264       else
12265         this_size += 5;
12266       this_size += 6;
12267     }
12268   this_size = (this_size + align - 1) & -align;
12269   return this_size;
12270 }
12271
12272 /* Stripping output sections is normally done before dynamic section
12273    symbols have been allocated.  This function is called later, and
12274    handles cases like htab->brlt which is mapped to its own output
12275    section.  */
12276
12277 static void
12278 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12279 {
12280   if (isec->size == 0
12281       && isec->output_section->size == 0
12282       && !(isec->output_section->flags & SEC_KEEP)
12283       && !bfd_section_removed_from_list (info->output_bfd,
12284                                          isec->output_section)
12285       && elf_section_data (isec->output_section)->dynindx == 0)
12286     {
12287       isec->output_section->flags |= SEC_EXCLUDE;
12288       bfd_section_list_remove (info->output_bfd, isec->output_section);
12289       info->output_bfd->section_count--;
12290     }
12291 }
12292
12293 /* Determine and set the size of the stub section for a final link.
12294
12295    The basic idea here is to examine all the relocations looking for
12296    PC-relative calls to a target that is unreachable with a "bl"
12297    instruction.  */
12298
12299 bfd_boolean
12300 ppc64_elf_size_stubs (struct bfd_link_info *info)
12301 {
12302   bfd_size_type stub_group_size;
12303   bfd_boolean stubs_always_before_branch;
12304   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12305
12306   if (htab == NULL)
12307     return FALSE;
12308
12309   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12310     htab->params->plt_thread_safe = 1;
12311   if (!htab->opd_abi)
12312     htab->params->plt_thread_safe = 0;
12313   else if (htab->params->plt_thread_safe == -1)
12314     {
12315       static const char *const thread_starter[] =
12316         {
12317           "pthread_create",
12318           /* libstdc++ */
12319           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12320           /* librt */
12321           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12322           "mq_notify", "create_timer",
12323           /* libanl */
12324           "getaddrinfo_a",
12325           /* libgomp */
12326           "GOMP_parallel",
12327           "GOMP_parallel_start",
12328           "GOMP_parallel_loop_static",
12329           "GOMP_parallel_loop_static_start",
12330           "GOMP_parallel_loop_dynamic",
12331           "GOMP_parallel_loop_dynamic_start",
12332           "GOMP_parallel_loop_guided",
12333           "GOMP_parallel_loop_guided_start",
12334           "GOMP_parallel_loop_runtime",
12335           "GOMP_parallel_loop_runtime_start",
12336           "GOMP_parallel_sections",
12337           "GOMP_parallel_sections_start",
12338           /* libgo */
12339           "__go_go",
12340         };
12341       unsigned i;
12342
12343       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12344         {
12345           struct elf_link_hash_entry *h;
12346           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12347                                     FALSE, FALSE, TRUE);
12348           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12349           if (htab->params->plt_thread_safe)
12350             break;
12351         }
12352     }
12353   stubs_always_before_branch = htab->params->group_size < 0;
12354   if (htab->params->group_size < 0)
12355     stub_group_size = -htab->params->group_size;
12356   else
12357     stub_group_size = htab->params->group_size;
12358
12359   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12360     return FALSE;
12361
12362 #define STUB_SHRINK_ITER 20
12363   /* Loop until no stubs added.  After iteration 20 of this loop we may
12364      exit on a stub section shrinking.  This is to break out of a
12365      pathological case where adding stubs on one iteration decreases
12366      section gaps (perhaps due to alignment), which then requires
12367      fewer or smaller stubs on the next iteration.  */
12368
12369   while (1)
12370     {
12371       bfd *input_bfd;
12372       unsigned int bfd_indx;
12373       struct map_stub *group;
12374
12375       htab->stub_iteration += 1;
12376
12377       for (input_bfd = info->input_bfds, bfd_indx = 0;
12378            input_bfd != NULL;
12379            input_bfd = input_bfd->link.next, bfd_indx++)
12380         {
12381           Elf_Internal_Shdr *symtab_hdr;
12382           asection *section;
12383           Elf_Internal_Sym *local_syms = NULL;
12384
12385           if (!is_ppc64_elf (input_bfd))
12386             continue;
12387
12388           /* We'll need the symbol table in a second.  */
12389           symtab_hdr = &elf_symtab_hdr (input_bfd);
12390           if (symtab_hdr->sh_info == 0)
12391             continue;
12392
12393           /* Walk over each section attached to the input bfd.  */
12394           for (section = input_bfd->sections;
12395                section != NULL;
12396                section = section->next)
12397             {
12398               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12399
12400               /* If there aren't any relocs, then there's nothing more
12401                  to do.  */
12402               if ((section->flags & SEC_RELOC) == 0
12403                   || (section->flags & SEC_ALLOC) == 0
12404                   || (section->flags & SEC_LOAD) == 0
12405                   || (section->flags & SEC_CODE) == 0
12406                   || section->reloc_count == 0)
12407                 continue;
12408
12409               /* If this section is a link-once section that will be
12410                  discarded, then don't create any stubs.  */
12411               if (section->output_section == NULL
12412                   || section->output_section->owner != info->output_bfd)
12413                 continue;
12414
12415               /* Get the relocs.  */
12416               internal_relocs
12417                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12418                                              info->keep_memory);
12419               if (internal_relocs == NULL)
12420                 goto error_ret_free_local;
12421
12422               /* Now examine each relocation.  */
12423               irela = internal_relocs;
12424               irelaend = irela + section->reloc_count;
12425               for (; irela < irelaend; irela++)
12426                 {
12427                   enum elf_ppc64_reloc_type r_type;
12428                   unsigned int r_indx;
12429                   enum ppc_stub_type stub_type;
12430                   struct ppc_stub_hash_entry *stub_entry;
12431                   asection *sym_sec, *code_sec;
12432                   bfd_vma sym_value, code_value;
12433                   bfd_vma destination;
12434                   unsigned long local_off;
12435                   bfd_boolean ok_dest;
12436                   struct ppc_link_hash_entry *hash;
12437                   struct ppc_link_hash_entry *fdh;
12438                   struct elf_link_hash_entry *h;
12439                   Elf_Internal_Sym *sym;
12440                   char *stub_name;
12441                   const asection *id_sec;
12442                   struct _opd_sec_data *opd;
12443                   struct plt_entry *plt_ent;
12444
12445                   r_type = ELF64_R_TYPE (irela->r_info);
12446                   r_indx = ELF64_R_SYM (irela->r_info);
12447
12448                   if (r_type >= R_PPC64_max)
12449                     {
12450                       bfd_set_error (bfd_error_bad_value);
12451                       goto error_ret_free_internal;
12452                     }
12453
12454                   /* Only look for stubs on branch instructions.  */
12455                   if (r_type != R_PPC64_REL24
12456                       && r_type != R_PPC64_REL14
12457                       && r_type != R_PPC64_REL14_BRTAKEN
12458                       && r_type != R_PPC64_REL14_BRNTAKEN)
12459                     continue;
12460
12461                   /* Now determine the call target, its name, value,
12462                      section.  */
12463                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12464                                   r_indx, input_bfd))
12465                     goto error_ret_free_internal;
12466                   hash = (struct ppc_link_hash_entry *) h;
12467
12468                   ok_dest = FALSE;
12469                   fdh = NULL;
12470                   sym_value = 0;
12471                   if (hash == NULL)
12472                     {
12473                       sym_value = sym->st_value;
12474                       if (sym_sec != NULL
12475                           && sym_sec->output_section != NULL)
12476                         ok_dest = TRUE;
12477                     }
12478                   else if (hash->elf.root.type == bfd_link_hash_defined
12479                            || hash->elf.root.type == bfd_link_hash_defweak)
12480                     {
12481                       sym_value = hash->elf.root.u.def.value;
12482                       if (sym_sec->output_section != NULL)
12483                         ok_dest = TRUE;
12484                     }
12485                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12486                            || hash->elf.root.type == bfd_link_hash_undefined)
12487                     {
12488                       /* Recognise an old ABI func code entry sym, and
12489                          use the func descriptor sym instead if it is
12490                          defined.  */
12491                       if (hash->elf.root.root.string[0] == '.'
12492                           && hash->oh != NULL)
12493                         {
12494                           fdh = ppc_follow_link (hash->oh);
12495                           if (fdh->elf.root.type == bfd_link_hash_defined
12496                               || fdh->elf.root.type == bfd_link_hash_defweak)
12497                             {
12498                               sym_sec = fdh->elf.root.u.def.section;
12499                               sym_value = fdh->elf.root.u.def.value;
12500                               if (sym_sec->output_section != NULL)
12501                                 ok_dest = TRUE;
12502                             }
12503                           else
12504                             fdh = NULL;
12505                         }
12506                     }
12507                   else
12508                     {
12509                       bfd_set_error (bfd_error_bad_value);
12510                       goto error_ret_free_internal;
12511                     }
12512
12513                   destination = 0;
12514                   local_off = 0;
12515                   if (ok_dest)
12516                     {
12517                       sym_value += irela->r_addend;
12518                       destination = (sym_value
12519                                      + sym_sec->output_offset
12520                                      + sym_sec->output_section->vma);
12521                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12522                                                             ? hash->elf.other
12523                                                             : sym->st_other);
12524                     }
12525
12526                   code_sec = sym_sec;
12527                   code_value = sym_value;
12528                   opd = get_opd_info (sym_sec);
12529                   if (opd != NULL)
12530                     {
12531                       bfd_vma dest;
12532
12533                       if (hash == NULL && opd->adjust != NULL)
12534                         {
12535                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12536                           if (adjust == -1)
12537                             continue;
12538                           code_value += adjust;
12539                           sym_value += adjust;
12540                         }
12541                       dest = opd_entry_value (sym_sec, sym_value,
12542                                               &code_sec, &code_value, FALSE);
12543                       if (dest != (bfd_vma) -1)
12544                         {
12545                           destination = dest;
12546                           if (fdh != NULL)
12547                             {
12548                               /* Fixup old ABI sym to point at code
12549                                  entry.  */
12550                               hash->elf.root.type = bfd_link_hash_defweak;
12551                               hash->elf.root.u.def.section = code_sec;
12552                               hash->elf.root.u.def.value = code_value;
12553                             }
12554                         }
12555                     }
12556
12557                   /* Determine what (if any) linker stub is needed.  */
12558                   plt_ent = NULL;
12559                   stub_type = ppc_type_of_stub (section, irela, &hash,
12560                                                 &plt_ent, destination,
12561                                                 local_off);
12562
12563                   if (stub_type != ppc_stub_plt_call)
12564                     {
12565                       /* Check whether we need a TOC adjusting stub.
12566                          Since the linker pastes together pieces from
12567                          different object files when creating the
12568                          _init and _fini functions, it may be that a
12569                          call to what looks like a local sym is in
12570                          fact a call needing a TOC adjustment.  */
12571                       if (code_sec != NULL
12572                           && code_sec->output_section != NULL
12573                           && (htab->sec_info[code_sec->id].toc_off
12574                               != htab->sec_info[section->id].toc_off)
12575                           && (code_sec->has_toc_reloc
12576                               || code_sec->makes_toc_func_call))
12577                         stub_type = ppc_stub_long_branch_r2off;
12578                     }
12579
12580                   if (stub_type == ppc_stub_none)
12581                     continue;
12582
12583                   /* __tls_get_addr calls might be eliminated.  */
12584                   if (stub_type != ppc_stub_plt_call
12585                       && hash != NULL
12586                       && (hash == htab->tls_get_addr
12587                           || hash == htab->tls_get_addr_fd)
12588                       && section->has_tls_reloc
12589                       && irela != internal_relocs)
12590                     {
12591                       /* Get tls info.  */
12592                       unsigned char *tls_mask;
12593
12594                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12595                                          irela - 1, input_bfd))
12596                         goto error_ret_free_internal;
12597                       if (*tls_mask != 0)
12598                         continue;
12599                     }
12600
12601                   if (stub_type == ppc_stub_plt_call)
12602                     {
12603                       if (!htab->opd_abi
12604                           && htab->params->plt_localentry0 != 0
12605                           && is_elfv2_localentry0 (&hash->elf))
12606                         htab->has_plt_localentry0 = 1;
12607                       else if (irela + 1 < irelaend
12608                                && irela[1].r_offset == irela->r_offset + 4
12609                                && (ELF64_R_TYPE (irela[1].r_info)
12610                                    == R_PPC64_TOCSAVE))
12611                         {
12612                           if (!tocsave_find (htab, INSERT,
12613                                              &local_syms, irela + 1, input_bfd))
12614                             goto error_ret_free_internal;
12615                         }
12616                       else
12617                         stub_type = ppc_stub_plt_call_r2save;
12618                     }
12619
12620                   /* Support for grouping stub sections.  */
12621                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12622
12623                   /* Get the name of this stub.  */
12624                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12625                   if (!stub_name)
12626                     goto error_ret_free_internal;
12627
12628                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12629                                                      stub_name, FALSE, FALSE);
12630                   if (stub_entry != NULL)
12631                     {
12632                       /* The proper stub has already been created.  */
12633                       free (stub_name);
12634                       if (stub_type == ppc_stub_plt_call_r2save)
12635                         stub_entry->stub_type = stub_type;
12636                       continue;
12637                     }
12638
12639                   stub_entry = ppc_add_stub (stub_name, section, info);
12640                   if (stub_entry == NULL)
12641                     {
12642                       free (stub_name);
12643                     error_ret_free_internal:
12644                       if (elf_section_data (section)->relocs == NULL)
12645                         free (internal_relocs);
12646                     error_ret_free_local:
12647                       if (local_syms != NULL
12648                           && (symtab_hdr->contents
12649                               != (unsigned char *) local_syms))
12650                         free (local_syms);
12651                       return FALSE;
12652                     }
12653
12654                   stub_entry->stub_type = stub_type;
12655                   if (stub_type != ppc_stub_plt_call
12656                       && stub_type != ppc_stub_plt_call_r2save)
12657                     {
12658                       stub_entry->target_value = code_value;
12659                       stub_entry->target_section = code_sec;
12660                     }
12661                   else
12662                     {
12663                       stub_entry->target_value = sym_value;
12664                       stub_entry->target_section = sym_sec;
12665                     }
12666                   stub_entry->h = hash;
12667                   stub_entry->plt_ent = plt_ent;
12668                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12669
12670                   if (stub_entry->h != NULL)
12671                     htab->stub_globals += 1;
12672                 }
12673
12674               /* We're done with the internal relocs, free them.  */
12675               if (elf_section_data (section)->relocs != internal_relocs)
12676                 free (internal_relocs);
12677             }
12678
12679           if (local_syms != NULL
12680               && symtab_hdr->contents != (unsigned char *) local_syms)
12681             {
12682               if (!info->keep_memory)
12683                 free (local_syms);
12684               else
12685                 symtab_hdr->contents = (unsigned char *) local_syms;
12686             }
12687         }
12688
12689       /* We may have added some stubs.  Find out the new size of the
12690          stub sections.  */
12691       for (group = htab->group; group != NULL; group = group->next)
12692         if (group->stub_sec != NULL)
12693           {
12694             asection *stub_sec = group->stub_sec;
12695
12696             if (htab->stub_iteration <= STUB_SHRINK_ITER
12697                 || stub_sec->rawsize < stub_sec->size)
12698               /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12699               stub_sec->rawsize = stub_sec->size;
12700             stub_sec->size = 0;
12701             stub_sec->reloc_count = 0;
12702             stub_sec->flags &= ~SEC_RELOC;
12703           }
12704
12705       if (htab->stub_iteration <= STUB_SHRINK_ITER
12706           || htab->brlt->rawsize < htab->brlt->size)
12707         htab->brlt->rawsize = htab->brlt->size;
12708       htab->brlt->size = 0;
12709       htab->brlt->reloc_count = 0;
12710       htab->brlt->flags &= ~SEC_RELOC;
12711       if (htab->relbrlt != NULL)
12712         htab->relbrlt->size = 0;
12713
12714       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12715
12716       for (group = htab->group; group != NULL; group = group->next)
12717         if (group->needs_save_res)
12718           group->stub_sec->size += htab->sfpr->size;
12719
12720       if (info->emitrelocations
12721           && htab->glink != NULL && htab->glink->size != 0)
12722         {
12723           htab->glink->reloc_count = 1;
12724           htab->glink->flags |= SEC_RELOC;
12725         }
12726
12727       if (htab->glink_eh_frame != NULL
12728           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12729           && htab->glink_eh_frame->output_section->size > 8)
12730         {
12731           size_t size = 0, align = 4;
12732
12733           for (group = htab->group; group != NULL; group = group->next)
12734             if (group->stub_sec != NULL)
12735               size += stub_eh_frame_size (group, align);
12736           if (htab->glink != NULL && htab->glink->size != 0)
12737             size += (24 + align - 1) & -align;
12738           if (size != 0)
12739             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12740           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12741           size = (size + align - 1) & -align;
12742           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12743           htab->glink_eh_frame->size = size;
12744         }
12745
12746       if (htab->params->plt_stub_align != 0)
12747         for (group = htab->group; group != NULL; group = group->next)
12748           if (group->stub_sec != NULL)
12749             {
12750               int align = abs (htab->params->plt_stub_align);
12751               group->stub_sec->size
12752                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12753             }
12754
12755       for (group = htab->group; group != NULL; group = group->next)
12756         if (group->stub_sec != NULL
12757             && group->stub_sec->rawsize != group->stub_sec->size
12758             && (htab->stub_iteration <= STUB_SHRINK_ITER
12759                 || group->stub_sec->rawsize < group->stub_sec->size))
12760           break;
12761
12762       if (group == NULL
12763           && (htab->brlt->rawsize == htab->brlt->size
12764               || (htab->stub_iteration > STUB_SHRINK_ITER
12765                   && htab->brlt->rawsize > htab->brlt->size))
12766           && (htab->glink_eh_frame == NULL
12767               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12768         break;
12769
12770       /* Ask the linker to do its stuff.  */
12771       (*htab->params->layout_sections_again) ();
12772     }
12773
12774   if (htab->glink_eh_frame != NULL
12775       && htab->glink_eh_frame->size != 0)
12776     {
12777       bfd_vma val;
12778       bfd_byte *p, *last_fde;
12779       size_t last_fde_len, size, align, pad;
12780       struct map_stub *group;
12781
12782       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12783       if (p == NULL)
12784         return FALSE;
12785       htab->glink_eh_frame->contents = p;
12786       last_fde = p;
12787       align = 4;
12788
12789       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12790       /* CIE length (rewrite in case little-endian).  */
12791       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12792       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12793       p += last_fde_len + 4;
12794
12795       for (group = htab->group; group != NULL; group = group->next)
12796         if (group->stub_sec != NULL)
12797           {
12798             last_fde = p;
12799             last_fde_len = stub_eh_frame_size (group, align) - 4;
12800             /* FDE length.  */
12801             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12802             p += 4;
12803             /* CIE pointer.  */
12804             val = p - htab->glink_eh_frame->contents;
12805             bfd_put_32 (htab->elf.dynobj, val, p);
12806             p += 4;
12807             /* Offset to stub section, written later.  */
12808             p += 4;
12809             /* stub section size.  */
12810             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12811             p += 4;
12812             /* Augmentation.  */
12813             p += 1;
12814             if (group->tls_get_addr_opt_bctrl != -1u)
12815               {
12816                 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12817
12818                 /* This FDE needs more than just the default.
12819                    Describe __tls_get_addr_opt stub LR.  */
12820                 if (to_bctrl < 64)
12821                   *p++ = DW_CFA_advance_loc + to_bctrl;
12822                 else if (to_bctrl < 256)
12823                   {
12824                     *p++ = DW_CFA_advance_loc1;
12825                     *p++ = to_bctrl;
12826                   }
12827                 else if (to_bctrl < 65536)
12828                   {
12829                     *p++ = DW_CFA_advance_loc2;
12830                     bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12831                     p += 2;
12832                   }
12833                 else
12834                   {
12835                     *p++ = DW_CFA_advance_loc4;
12836                     bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12837                     p += 4;
12838                   }
12839                 *p++ = DW_CFA_offset_extended_sf;
12840                 *p++ = 65;
12841                 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12842                 *p++ = DW_CFA_advance_loc + 4;
12843                 *p++ = DW_CFA_restore_extended;
12844                 *p++ = 65;
12845               }
12846             /* Pad.  */
12847             p = last_fde + last_fde_len + 4;
12848           }
12849       if (htab->glink != NULL && htab->glink->size != 0)
12850         {
12851           last_fde = p;
12852           last_fde_len = ((24 + align - 1) & -align) - 4;
12853           /* FDE length.  */
12854           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12855           p += 4;
12856           /* CIE pointer.  */
12857           val = p - htab->glink_eh_frame->contents;
12858           bfd_put_32 (htab->elf.dynobj, val, p);
12859           p += 4;
12860           /* Offset to .glink, written later.  */
12861           p += 4;
12862           /* .glink size.  */
12863           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12864           p += 4;
12865           /* Augmentation.  */
12866           p += 1;
12867
12868           *p++ = DW_CFA_advance_loc + 1;
12869           *p++ = DW_CFA_register;
12870           *p++ = 65;
12871           *p++ = htab->opd_abi ? 12 : 0;
12872           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12873           *p++ = DW_CFA_restore_extended;
12874           *p++ = 65;
12875           p += ((24 + align - 1) & -align) - 24;
12876         }
12877       /* Subsume any padding into the last FDE if user .eh_frame
12878          sections are aligned more than glink_eh_frame.  Otherwise any
12879          zero padding will be seen as a terminator.  */
12880       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12881       size = p - htab->glink_eh_frame->contents;
12882       pad = ((size + align - 1) & -align) - size;
12883       htab->glink_eh_frame->size = size + pad;
12884       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12885     }
12886
12887   maybe_strip_output (info, htab->brlt);
12888   if (htab->glink_eh_frame != NULL)
12889     maybe_strip_output (info, htab->glink_eh_frame);
12890
12891   return TRUE;
12892 }
12893
12894 /* Called after we have determined section placement.  If sections
12895    move, we'll be called again.  Provide a value for TOCstart.  */
12896
12897 bfd_vma
12898 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12899 {
12900   asection *s;
12901   bfd_vma TOCstart, adjust;
12902
12903   if (info != NULL)
12904     {
12905       struct elf_link_hash_entry *h;
12906       struct elf_link_hash_table *htab = elf_hash_table (info);
12907
12908       if (is_elf_hash_table (htab)
12909           && htab->hgot != NULL)
12910         h = htab->hgot;
12911       else
12912         {
12913           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12914           if (is_elf_hash_table (htab))
12915             htab->hgot = h;
12916         }
12917       if (h != NULL
12918           && h->root.type == bfd_link_hash_defined
12919           && !h->root.linker_def
12920           && (!is_elf_hash_table (htab)
12921               || h->def_regular))
12922         {
12923           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12924                       + h->root.u.def.section->output_offset
12925                       + h->root.u.def.section->output_section->vma);
12926           _bfd_set_gp_value (obfd, TOCstart);
12927           return TOCstart;
12928         }
12929     }
12930
12931   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12932      order.  The TOC starts where the first of these sections starts.  */
12933   s = bfd_get_section_by_name (obfd, ".got");
12934   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12935     s = bfd_get_section_by_name (obfd, ".toc");
12936   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12937     s = bfd_get_section_by_name (obfd, ".tocbss");
12938   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12939     s = bfd_get_section_by_name (obfd, ".plt");
12940   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12941     {
12942       /* This may happen for
12943          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12944          .toc directive
12945          o  bad linker script
12946          o --gc-sections and empty TOC sections
12947
12948          FIXME: Warn user?  */
12949
12950       /* Look for a likely section.  We probably won't even be
12951          using TOCstart.  */
12952       for (s = obfd->sections; s != NULL; s = s->next)
12953         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12954                          | SEC_EXCLUDE))
12955             == (SEC_ALLOC | SEC_SMALL_DATA))
12956           break;
12957       if (s == NULL)
12958         for (s = obfd->sections; s != NULL; s = s->next)
12959           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12960               == (SEC_ALLOC | SEC_SMALL_DATA))
12961             break;
12962       if (s == NULL)
12963         for (s = obfd->sections; s != NULL; s = s->next)
12964           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12965               == SEC_ALLOC)
12966             break;
12967       if (s == NULL)
12968         for (s = obfd->sections; s != NULL; s = s->next)
12969           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12970             break;
12971     }
12972
12973   TOCstart = 0;
12974   if (s != NULL)
12975     TOCstart = s->output_section->vma + s->output_offset;
12976
12977   /* Force alignment.  */
12978   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12979   TOCstart -= adjust;
12980   _bfd_set_gp_value (obfd, TOCstart);
12981
12982   if (info != NULL && s != NULL)
12983     {
12984       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12985
12986       if (htab != NULL)
12987         {
12988           if (htab->elf.hgot != NULL)
12989             {
12990               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12991               htab->elf.hgot->root.u.def.section = s;
12992             }
12993         }
12994       else
12995         {
12996           struct bfd_link_hash_entry *bh = NULL;
12997           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12998                                             s, TOC_BASE_OFF - adjust,
12999                                             NULL, FALSE, FALSE, &bh);
13000         }
13001     }
13002   return TOCstart;
13003 }
13004
13005 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13006    write out any global entry stubs.  */
13007
13008 static bfd_boolean
13009 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
13010 {
13011   struct bfd_link_info *info;
13012   struct ppc_link_hash_table *htab;
13013   struct plt_entry *pent;
13014   asection *s;
13015
13016   if (h->root.type == bfd_link_hash_indirect)
13017     return TRUE;
13018
13019   if (!h->pointer_equality_needed)
13020     return TRUE;
13021
13022   if (h->def_regular)
13023     return TRUE;
13024
13025   info = inf;
13026   htab = ppc_hash_table (info);
13027   if (htab == NULL)
13028     return FALSE;
13029
13030   s = htab->global_entry;
13031   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13032     if (pent->plt.offset != (bfd_vma) -1
13033         && pent->addend == 0)
13034       {
13035         bfd_byte *p;
13036         asection *plt;
13037         bfd_vma off;
13038
13039         p = s->contents + h->root.u.def.value;
13040         plt = htab->elf.splt;
13041         if (!htab->elf.dynamic_sections_created
13042             || h->dynindx == -1)
13043           plt = htab->elf.iplt;
13044         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13045         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13046
13047         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13048           {
13049             info->callbacks->einfo
13050               (_("%P: linkage table error against `%T'\n"),
13051                h->root.root.string);
13052             bfd_set_error (bfd_error_bad_value);
13053             htab->stub_error = TRUE;
13054           }
13055
13056         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13057         if (htab->params->emit_stub_syms)
13058           {
13059             size_t len = strlen (h->root.root.string);
13060             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13061
13062             if (name == NULL)
13063               return FALSE;
13064
13065             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13066             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13067             if (h == NULL)
13068               return FALSE;
13069             if (h->root.type == bfd_link_hash_new)
13070               {
13071                 h->root.type = bfd_link_hash_defined;
13072                 h->root.u.def.section = s;
13073                 h->root.u.def.value = p - s->contents;
13074                 h->ref_regular = 1;
13075                 h->def_regular = 1;
13076                 h->ref_regular_nonweak = 1;
13077                 h->forced_local = 1;
13078                 h->non_elf = 0;
13079                 h->root.linker_def = 1;
13080               }
13081           }
13082
13083         if (PPC_HA (off) != 0)
13084           {
13085             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13086             p += 4;
13087           }
13088         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13089         p += 4;
13090         bfd_put_32 (s->owner, MTCTR_R12, p);
13091         p += 4;
13092         output_bctr (htab, s->owner, p);
13093         break;
13094       }
13095   return TRUE;
13096 }
13097
13098 /* Build all the stubs associated with the current output file.
13099    The stubs are kept in a hash table attached to the main linker
13100    hash table.  This function is called via gldelf64ppc_finish.  */
13101
13102 bfd_boolean
13103 ppc64_elf_build_stubs (struct bfd_link_info *info,
13104                        char **stats)
13105 {
13106   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13107   struct map_stub *group;
13108   asection *stub_sec;
13109   bfd_byte *p;
13110   int stub_sec_count = 0;
13111
13112   if (htab == NULL)
13113     return FALSE;
13114
13115   /* Allocate memory to hold the linker stubs.  */
13116   for (group = htab->group; group != NULL; group = group->next)
13117     if ((stub_sec = group->stub_sec) != NULL
13118         && stub_sec->size != 0)
13119       {
13120         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13121         if (stub_sec->contents == NULL)
13122           return FALSE;
13123         stub_sec->size = 0;
13124       }
13125
13126   if (htab->glink != NULL && htab->glink->size != 0)
13127     {
13128       unsigned int indx;
13129       bfd_vma plt0;
13130
13131       /* Build the .glink plt call stub.  */
13132       if (htab->params->emit_stub_syms)
13133         {
13134           struct elf_link_hash_entry *h;
13135           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13136                                     TRUE, FALSE, FALSE);
13137           if (h == NULL)
13138             return FALSE;
13139           if (h->root.type == bfd_link_hash_new)
13140             {
13141               h->root.type = bfd_link_hash_defined;
13142               h->root.u.def.section = htab->glink;
13143               h->root.u.def.value = 8;
13144               h->ref_regular = 1;
13145               h->def_regular = 1;
13146               h->ref_regular_nonweak = 1;
13147               h->forced_local = 1;
13148               h->non_elf = 0;
13149               h->root.linker_def = 1;
13150             }
13151         }
13152       plt0 = (htab->elf.splt->output_section->vma
13153               + htab->elf.splt->output_offset
13154               - 16);
13155       if (info->emitrelocations)
13156         {
13157           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13158           if (r == NULL)
13159             return FALSE;
13160           r->r_offset = (htab->glink->output_offset
13161                          + htab->glink->output_section->vma);
13162           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13163           r->r_addend = plt0;
13164         }
13165       p = htab->glink->contents;
13166       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13167       bfd_put_64 (htab->glink->owner, plt0, p);
13168       p += 8;
13169       if (htab->opd_abi)
13170         {
13171           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13172           p += 4;
13173           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13174           p += 4;
13175           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13176           p += 4;
13177           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13178           p += 4;
13179           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13180           p += 4;
13181           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13182           p += 4;
13183           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13184           p += 4;
13185           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13186           p += 4;
13187           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13188           p += 4;
13189           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13190           p += 4;
13191         }
13192       else
13193         {
13194           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13195           p += 4;
13196           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13197           p += 4;
13198           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13199           p += 4;
13200           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13201           p += 4;
13202           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13203           p += 4;
13204           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13205           p += 4;
13206           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13207           p += 4;
13208           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13209           p += 4;
13210           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13211           p += 4;
13212           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13213           p += 4;
13214           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13215           p += 4;
13216           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13217           p += 4;
13218           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13219           p += 4;
13220         }
13221       p = output_bctr (htab, htab->glink->owner, p);
13222       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13223
13224       /* Build the .glink lazy link call stubs.  */
13225       indx = 0;
13226       while (p < htab->glink->contents + htab->glink->size)
13227         {
13228           if (htab->opd_abi)
13229             {
13230               if (indx < 0x8000)
13231                 {
13232                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13233                   p += 4;
13234                 }
13235               else
13236                 {
13237                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13238                   p += 4;
13239                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13240                               p);
13241                   p += 4;
13242                 }
13243             }
13244           bfd_put_32 (htab->glink->owner,
13245                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13246           indx++;
13247           p += 4;
13248         }
13249     }
13250
13251   /* Build .glink global entry stubs.  */
13252   if (htab->global_entry != NULL && htab->global_entry->size != 0)
13253     elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13254
13255   if (htab->brlt != NULL && htab->brlt->size != 0)
13256     {
13257       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13258                                          htab->brlt->size);
13259       if (htab->brlt->contents == NULL)
13260         return FALSE;
13261     }
13262   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13263     {
13264       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13265                                             htab->relbrlt->size);
13266       if (htab->relbrlt->contents == NULL)
13267         return FALSE;
13268     }
13269
13270   /* Build the stubs as directed by the stub hash table.  */
13271   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13272
13273   for (group = htab->group; group != NULL; group = group->next)
13274     if (group->needs_save_res)
13275       {
13276         stub_sec = group->stub_sec;
13277         memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13278                 htab->sfpr->size);
13279         if (htab->params->emit_stub_syms)
13280           {
13281             unsigned int i;
13282
13283             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13284               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13285                 return FALSE;
13286           }
13287         stub_sec->size += htab->sfpr->size;
13288       }
13289
13290   if (htab->relbrlt != NULL)
13291     htab->relbrlt->reloc_count = 0;
13292
13293   if (htab->params->plt_stub_align != 0)
13294     for (group = htab->group; group != NULL; group = group->next)
13295       if ((stub_sec = group->stub_sec) != NULL)
13296         {
13297           int align = abs (htab->params->plt_stub_align);
13298           stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13299         }
13300
13301   for (group = htab->group; group != NULL; group = group->next)
13302     if ((stub_sec = group->stub_sec) != NULL)
13303       {
13304         stub_sec_count += 1;
13305         if (stub_sec->rawsize != stub_sec->size
13306             && (htab->stub_iteration <= STUB_SHRINK_ITER
13307                 || stub_sec->rawsize < stub_sec->size))
13308           break;
13309       }
13310
13311   if (group != NULL)
13312     {
13313       htab->stub_error = TRUE;
13314       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13315     }
13316
13317   if (htab->stub_error)
13318     return FALSE;
13319
13320   if (stats != NULL)
13321     {
13322       size_t len;
13323       *stats = bfd_malloc (500);
13324       if (*stats == NULL)
13325         return FALSE;
13326
13327       len = sprintf (*stats,
13328                      ngettext ("linker stubs in %u group\n",
13329                                "linker stubs in %u groups\n",
13330                                stub_sec_count),
13331                      stub_sec_count);
13332       sprintf (*stats + len, _("  branch       %lu\n"
13333                                "  toc adjust   %lu\n"
13334                                "  long branch  %lu\n"
13335                                "  long toc adj %lu\n"
13336                                "  plt call     %lu\n"
13337                                "  plt call toc %lu\n"
13338                                "  global entry %lu"),
13339                htab->stub_count[ppc_stub_long_branch - 1],
13340                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13341                htab->stub_count[ppc_stub_plt_branch - 1],
13342                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13343                htab->stub_count[ppc_stub_plt_call - 1],
13344                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13345                htab->stub_count[ppc_stub_global_entry - 1]);
13346     }
13347   return TRUE;
13348 }
13349
13350 /* What to do when ld finds relocations against symbols defined in
13351    discarded sections.  */
13352
13353 static unsigned int
13354 ppc64_elf_action_discarded (asection *sec)
13355 {
13356   if (strcmp (".opd", sec->name) == 0)
13357     return 0;
13358
13359   if (strcmp (".toc", sec->name) == 0)
13360     return 0;
13361
13362   if (strcmp (".toc1", sec->name) == 0)
13363     return 0;
13364
13365   return _bfd_elf_default_action_discarded (sec);
13366 }
13367
13368 /* The RELOCATE_SECTION function is called by the ELF backend linker
13369    to handle the relocations for a section.
13370
13371    The relocs are always passed as Rela structures; if the section
13372    actually uses Rel structures, the r_addend field will always be
13373    zero.
13374
13375    This function is responsible for adjust the section contents as
13376    necessary, and (if using Rela relocs and generating a
13377    relocatable output file) adjusting the reloc addend as
13378    necessary.
13379
13380    This function does not have to worry about setting the reloc
13381    address or the reloc symbol index.
13382
13383    LOCAL_SYMS is a pointer to the swapped in local symbols.
13384
13385    LOCAL_SECTIONS is an array giving the section in the input file
13386    corresponding to the st_shndx field of each local symbol.
13387
13388    The global hash table entry for the global symbols can be found
13389    via elf_sym_hashes (input_bfd).
13390
13391    When generating relocatable output, this function must handle
13392    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13393    going to be the section symbol corresponding to the output
13394    section, which means that the addend must be adjusted
13395    accordingly.  */
13396
13397 static bfd_boolean
13398 ppc64_elf_relocate_section (bfd *output_bfd,
13399                             struct bfd_link_info *info,
13400                             bfd *input_bfd,
13401                             asection *input_section,
13402                             bfd_byte *contents,
13403                             Elf_Internal_Rela *relocs,
13404                             Elf_Internal_Sym *local_syms,
13405                             asection **local_sections)
13406 {
13407   struct ppc_link_hash_table *htab;
13408   Elf_Internal_Shdr *symtab_hdr;
13409   struct elf_link_hash_entry **sym_hashes;
13410   Elf_Internal_Rela *rel;
13411   Elf_Internal_Rela *wrel;
13412   Elf_Internal_Rela *relend;
13413   Elf_Internal_Rela outrel;
13414   bfd_byte *loc;
13415   struct got_entry **local_got_ents;
13416   bfd_vma TOCstart;
13417   bfd_boolean ret = TRUE;
13418   bfd_boolean is_opd;
13419   /* Assume 'at' branch hints.  */
13420   bfd_boolean is_isa_v2 = TRUE;
13421   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13422
13423   /* Initialize howto table if needed.  */
13424   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13425     ppc_howto_init ();
13426
13427   htab = ppc_hash_table (info);
13428   if (htab == NULL)
13429     return FALSE;
13430
13431   /* Don't relocate stub sections.  */
13432   if (input_section->owner == htab->params->stub_bfd)
13433     return TRUE;
13434
13435   BFD_ASSERT (is_ppc64_elf (input_bfd));
13436
13437   local_got_ents = elf_local_got_ents (input_bfd);
13438   TOCstart = elf_gp (output_bfd);
13439   symtab_hdr = &elf_symtab_hdr (input_bfd);
13440   sym_hashes = elf_sym_hashes (input_bfd);
13441   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13442
13443   rel = wrel = relocs;
13444   relend = relocs + input_section->reloc_count;
13445   for (; rel < relend; wrel++, rel++)
13446     {
13447       enum elf_ppc64_reloc_type r_type;
13448       bfd_vma addend;
13449       bfd_reloc_status_type r;
13450       Elf_Internal_Sym *sym;
13451       asection *sec;
13452       struct elf_link_hash_entry *h_elf;
13453       struct ppc_link_hash_entry *h;
13454       struct ppc_link_hash_entry *fdh;
13455       const char *sym_name;
13456       unsigned long r_symndx, toc_symndx;
13457       bfd_vma toc_addend;
13458       unsigned char tls_mask, tls_gd, tls_type;
13459       unsigned char sym_type;
13460       bfd_vma relocation;
13461       bfd_boolean unresolved_reloc;
13462       bfd_boolean warned;
13463       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13464       unsigned int insn;
13465       unsigned int mask;
13466       struct ppc_stub_hash_entry *stub_entry;
13467       bfd_vma max_br_offset;
13468       bfd_vma from;
13469       Elf_Internal_Rela orig_rel;
13470       reloc_howto_type *howto;
13471       struct reloc_howto_struct alt_howto;
13472
13473     again:
13474       orig_rel = *rel;
13475
13476       r_type = ELF64_R_TYPE (rel->r_info);
13477       r_symndx = ELF64_R_SYM (rel->r_info);
13478
13479       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13480          symbol of the previous ADDR64 reloc.  The symbol gives us the
13481          proper TOC base to use.  */
13482       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13483           && wrel != relocs
13484           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13485           && is_opd)
13486         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13487
13488       sym = NULL;
13489       sec = NULL;
13490       h_elf = NULL;
13491       sym_name = NULL;
13492       unresolved_reloc = FALSE;
13493       warned = FALSE;
13494
13495       if (r_symndx < symtab_hdr->sh_info)
13496         {
13497           /* It's a local symbol.  */
13498           struct _opd_sec_data *opd;
13499
13500           sym = local_syms + r_symndx;
13501           sec = local_sections[r_symndx];
13502           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13503           sym_type = ELF64_ST_TYPE (sym->st_info);
13504           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13505           opd = get_opd_info (sec);
13506           if (opd != NULL && opd->adjust != NULL)
13507             {
13508               long adjust = opd->adjust[OPD_NDX (sym->st_value
13509                                                  + rel->r_addend)];
13510               if (adjust == -1)
13511                 relocation = 0;
13512               else
13513                 {
13514                   /* If this is a relocation against the opd section sym
13515                      and we have edited .opd, adjust the reloc addend so
13516                      that ld -r and ld --emit-relocs output is correct.
13517                      If it is a reloc against some other .opd symbol,
13518                      then the symbol value will be adjusted later.  */
13519                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13520                     rel->r_addend += adjust;
13521                   else
13522                     relocation += adjust;
13523                 }
13524             }
13525         }
13526       else
13527         {
13528           bfd_boolean ignored;
13529
13530           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13531                                    r_symndx, symtab_hdr, sym_hashes,
13532                                    h_elf, sec, relocation,
13533                                    unresolved_reloc, warned, ignored);
13534           sym_name = h_elf->root.root.string;
13535           sym_type = h_elf->type;
13536           if (sec != NULL
13537               && sec->owner == output_bfd
13538               && strcmp (sec->name, ".opd") == 0)
13539             {
13540               /* This is a symbol defined in a linker script.  All
13541                  such are defined in output sections, even those
13542                  defined by simple assignment from a symbol defined in
13543                  an input section.  Transfer the symbol to an
13544                  appropriate input .opd section, so that a branch to
13545                  this symbol will be mapped to the location specified
13546                  by the opd entry.  */
13547               struct bfd_link_order *lo;
13548               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13549                 if (lo->type == bfd_indirect_link_order)
13550                   {
13551                     asection *isec = lo->u.indirect.section;
13552                     if (h_elf->root.u.def.value >= isec->output_offset
13553                         && h_elf->root.u.def.value < (isec->output_offset
13554                                                       + isec->size))
13555                       {
13556                         h_elf->root.u.def.value -= isec->output_offset;
13557                         h_elf->root.u.def.section = isec;
13558                         sec = isec;
13559                         break;
13560                       }
13561                   }
13562             }
13563         }
13564       h = (struct ppc_link_hash_entry *) h_elf;
13565
13566       if (sec != NULL && discarded_section (sec))
13567         {
13568           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13569                                input_bfd, input_section,
13570                                contents + rel->r_offset);
13571           wrel->r_offset = rel->r_offset;
13572           wrel->r_info = 0;
13573           wrel->r_addend = 0;
13574
13575           /* For ld -r, remove relocations in debug sections against
13576              symbols defined in discarded sections.  Not done for
13577              non-debug to preserve relocs in .eh_frame which the
13578              eh_frame editing code expects to be present.  */
13579           if (bfd_link_relocatable (info)
13580               && (input_section->flags & SEC_DEBUGGING))
13581             wrel--;
13582
13583           continue;
13584         }
13585
13586       if (bfd_link_relocatable (info))
13587         goto copy_reloc;
13588
13589       if (h != NULL && &h->elf == htab->elf.hgot)
13590         {
13591           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13592           sec = bfd_abs_section_ptr;
13593           unresolved_reloc = FALSE;
13594         }
13595
13596       /* TLS optimizations.  Replace instruction sequences and relocs
13597          based on information we collected in tls_optimize.  We edit
13598          RELOCS so that --emit-relocs will output something sensible
13599          for the final instruction stream.  */
13600       tls_mask = 0;
13601       tls_gd = 0;
13602       toc_symndx = 0;
13603       if (h != NULL)
13604         tls_mask = h->tls_mask;
13605       else if (local_got_ents != NULL)
13606         {
13607           struct plt_entry **local_plt = (struct plt_entry **)
13608             (local_got_ents + symtab_hdr->sh_info);
13609           unsigned char *lgot_masks = (unsigned char *)
13610             (local_plt + symtab_hdr->sh_info);
13611           tls_mask = lgot_masks[r_symndx];
13612         }
13613       if (tls_mask == 0
13614           && (r_type == R_PPC64_TLS
13615               || r_type == R_PPC64_TLSGD
13616               || r_type == R_PPC64_TLSLD))
13617         {
13618           /* Check for toc tls entries.  */
13619           unsigned char *toc_tls;
13620
13621           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13622                              &local_syms, rel, input_bfd))
13623             return FALSE;
13624
13625           if (toc_tls)
13626             tls_mask = *toc_tls;
13627         }
13628
13629       /* Check that tls relocs are used with tls syms, and non-tls
13630          relocs are used with non-tls syms.  */
13631       if (r_symndx != STN_UNDEF
13632           && r_type != R_PPC64_NONE
13633           && (h == NULL
13634               || h->elf.root.type == bfd_link_hash_defined
13635               || h->elf.root.type == bfd_link_hash_defweak)
13636           && (IS_PPC64_TLS_RELOC (r_type)
13637               != (sym_type == STT_TLS
13638                   || (sym_type == STT_SECTION
13639                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13640         {
13641           if (tls_mask != 0
13642               && (r_type == R_PPC64_TLS
13643                   || r_type == R_PPC64_TLSGD
13644                   || r_type == R_PPC64_TLSLD))
13645             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13646             ;
13647           else
13648             info->callbacks->einfo
13649               (!IS_PPC64_TLS_RELOC (r_type)
13650                /* xgettext:c-format */
13651                ? _("%H: %s used with TLS symbol `%T'\n")
13652                /* xgettext:c-format */
13653                : _("%H: %s used with non-TLS symbol `%T'\n"),
13654                input_bfd, input_section, rel->r_offset,
13655                ppc64_elf_howto_table[r_type]->name,
13656                sym_name);
13657         }
13658
13659       /* Ensure reloc mapping code below stays sane.  */
13660       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13661           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13662           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13663           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13664           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13665           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13666           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13667           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13668           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13669           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13670         abort ();
13671
13672       switch (r_type)
13673         {
13674         default:
13675           break;
13676
13677         case R_PPC64_LO_DS_OPT:
13678           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13679           if ((insn & (0x3f << 26)) != 58u << 26)
13680             abort ();
13681           insn += (14u << 26) - (58u << 26);
13682           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13683           r_type = R_PPC64_TOC16_LO;
13684           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13685           break;
13686
13687         case R_PPC64_TOC16:
13688         case R_PPC64_TOC16_LO:
13689         case R_PPC64_TOC16_DS:
13690         case R_PPC64_TOC16_LO_DS:
13691           {
13692             /* Check for toc tls entries.  */
13693             unsigned char *toc_tls;
13694             int retval;
13695
13696             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13697                                    &local_syms, rel, input_bfd);
13698             if (retval == 0)
13699               return FALSE;
13700
13701             if (toc_tls)
13702               {
13703                 tls_mask = *toc_tls;
13704                 if (r_type == R_PPC64_TOC16_DS
13705                     || r_type == R_PPC64_TOC16_LO_DS)
13706                   {
13707                     if (tls_mask != 0
13708                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13709                       goto toctprel;
13710                   }
13711                 else
13712                   {
13713                     /* If we found a GD reloc pair, then we might be
13714                        doing a GD->IE transition.  */
13715                     if (retval == 2)
13716                       {
13717                         tls_gd = TLS_TPRELGD;
13718                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13719                           goto tls_ldgd_opt;
13720                       }
13721                     else if (retval == 3)
13722                       {
13723                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13724                           goto tls_ldgd_opt;
13725                       }
13726                   }
13727               }
13728           }
13729           break;
13730
13731         case R_PPC64_GOT_TPREL16_HI:
13732         case R_PPC64_GOT_TPREL16_HA:
13733           if (tls_mask != 0
13734               && (tls_mask & TLS_TPREL) == 0)
13735             {
13736               rel->r_offset -= d_offset;
13737               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13738               r_type = R_PPC64_NONE;
13739               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13740             }
13741           break;
13742
13743         case R_PPC64_GOT_TPREL16_DS:
13744         case R_PPC64_GOT_TPREL16_LO_DS:
13745           if (tls_mask != 0
13746               && (tls_mask & TLS_TPREL) == 0)
13747             {
13748             toctprel:
13749               insn = bfd_get_32 (input_bfd,
13750                                  contents + rel->r_offset - d_offset);
13751               insn &= 31 << 21;
13752               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13753               bfd_put_32 (input_bfd, insn,
13754                           contents + rel->r_offset - d_offset);
13755               r_type = R_PPC64_TPREL16_HA;
13756               if (toc_symndx != 0)
13757                 {
13758                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13759                   rel->r_addend = toc_addend;
13760                   /* We changed the symbol.  Start over in order to
13761                      get h, sym, sec etc. right.  */
13762                   goto again;
13763                 }
13764               else
13765                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13766             }
13767           break;
13768
13769         case R_PPC64_TLS:
13770           if (tls_mask != 0
13771               && (tls_mask & TLS_TPREL) == 0)
13772             {
13773               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13774               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13775               if (insn == 0)
13776                 abort ();
13777               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13778               /* Was PPC64_TLS which sits on insn boundary, now
13779                  PPC64_TPREL16_LO which is at low-order half-word.  */
13780               rel->r_offset += d_offset;
13781               r_type = R_PPC64_TPREL16_LO;
13782               if (toc_symndx != 0)
13783                 {
13784                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13785                   rel->r_addend = toc_addend;
13786                   /* We changed the symbol.  Start over in order to
13787                      get h, sym, sec etc. right.  */
13788                   goto again;
13789                 }
13790               else
13791                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13792             }
13793           break;
13794
13795         case R_PPC64_GOT_TLSGD16_HI:
13796         case R_PPC64_GOT_TLSGD16_HA:
13797           tls_gd = TLS_TPRELGD;
13798           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13799             goto tls_gdld_hi;
13800           break;
13801
13802         case R_PPC64_GOT_TLSLD16_HI:
13803         case R_PPC64_GOT_TLSLD16_HA:
13804           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13805             {
13806             tls_gdld_hi:
13807               if ((tls_mask & tls_gd) != 0)
13808                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13809                           + R_PPC64_GOT_TPREL16_DS);
13810               else
13811                 {
13812                   rel->r_offset -= d_offset;
13813                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13814                   r_type = R_PPC64_NONE;
13815                 }
13816               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13817             }
13818           break;
13819
13820         case R_PPC64_GOT_TLSGD16:
13821         case R_PPC64_GOT_TLSGD16_LO:
13822           tls_gd = TLS_TPRELGD;
13823           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13824             goto tls_ldgd_opt;
13825           break;
13826
13827         case R_PPC64_GOT_TLSLD16:
13828         case R_PPC64_GOT_TLSLD16_LO:
13829           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13830             {
13831               unsigned int insn1, insn2;
13832               bfd_vma offset;
13833
13834             tls_ldgd_opt:
13835               offset = (bfd_vma) -1;
13836               /* If not using the newer R_PPC64_TLSGD/LD to mark
13837                  __tls_get_addr calls, we must trust that the call
13838                  stays with its arg setup insns, ie. that the next
13839                  reloc is the __tls_get_addr call associated with
13840                  the current reloc.  Edit both insns.  */
13841               if (input_section->has_tls_get_addr_call
13842                   && rel + 1 < relend
13843                   && branch_reloc_hash_match (input_bfd, rel + 1,
13844                                               htab->tls_get_addr,
13845                                               htab->tls_get_addr_fd))
13846                 offset = rel[1].r_offset;
13847               /* We read the low GOT_TLS (or TOC16) insn because we
13848                  need to keep the destination reg.  It may be
13849                  something other than the usual r3, and moved to r3
13850                  before the call by intervening code.  */
13851               insn1 = bfd_get_32 (input_bfd,
13852                                   contents + rel->r_offset - d_offset);
13853               if ((tls_mask & tls_gd) != 0)
13854                 {
13855                   /* IE */
13856                   insn1 &= (0x1f << 21) | (0x1f << 16);
13857                   insn1 |= 58 << 26;    /* ld */
13858                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13859                   if (offset != (bfd_vma) -1)
13860                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13861                   if ((tls_mask & TLS_EXPLICIT) == 0)
13862                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13863                               + R_PPC64_GOT_TPREL16_DS);
13864                   else
13865                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13866                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13867                 }
13868               else
13869                 {
13870                   /* LE */
13871                   insn1 &= 0x1f << 21;
13872                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13873                   insn2 = 0x38630000;   /* addi 3,3,0 */
13874                   if (tls_gd == 0)
13875                     {
13876                       /* Was an LD reloc.  */
13877                       if (toc_symndx)
13878                         sec = local_sections[toc_symndx];
13879                       for (r_symndx = 0;
13880                            r_symndx < symtab_hdr->sh_info;
13881                            r_symndx++)
13882                         if (local_sections[r_symndx] == sec)
13883                           break;
13884                       if (r_symndx >= symtab_hdr->sh_info)
13885                         r_symndx = STN_UNDEF;
13886                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13887                       if (r_symndx != STN_UNDEF)
13888                         rel->r_addend -= (local_syms[r_symndx].st_value
13889                                           + sec->output_offset
13890                                           + sec->output_section->vma);
13891                     }
13892                   else if (toc_symndx != 0)
13893                     {
13894                       r_symndx = toc_symndx;
13895                       rel->r_addend = toc_addend;
13896                     }
13897                   r_type = R_PPC64_TPREL16_HA;
13898                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13899                   if (offset != (bfd_vma) -1)
13900                     {
13901                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13902                                                     R_PPC64_TPREL16_LO);
13903                       rel[1].r_offset = offset + d_offset;
13904                       rel[1].r_addend = rel->r_addend;
13905                     }
13906                 }
13907               bfd_put_32 (input_bfd, insn1,
13908                           contents + rel->r_offset - d_offset);
13909               if (offset != (bfd_vma) -1)
13910                 bfd_put_32 (input_bfd, insn2, contents + offset);
13911               if ((tls_mask & tls_gd) == 0
13912                   && (tls_gd == 0 || toc_symndx != 0))
13913                 {
13914                   /* We changed the symbol.  Start over in order
13915                      to get h, sym, sec etc. right.  */
13916                   goto again;
13917                 }
13918             }
13919           break;
13920
13921         case R_PPC64_TLSGD:
13922           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13923             {
13924               unsigned int insn2;
13925               bfd_vma offset = rel->r_offset;
13926
13927               if ((tls_mask & TLS_TPRELGD) != 0)
13928                 {
13929                   /* IE */
13930                   r_type = R_PPC64_NONE;
13931                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13932                 }
13933               else
13934                 {
13935                   /* LE */
13936                   if (toc_symndx != 0)
13937                     {
13938                       r_symndx = toc_symndx;
13939                       rel->r_addend = toc_addend;
13940                     }
13941                   r_type = R_PPC64_TPREL16_LO;
13942                   rel->r_offset = offset + d_offset;
13943                   insn2 = 0x38630000;   /* addi 3,3,0 */
13944                 }
13945               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13946               /* Zap the reloc on the _tls_get_addr call too.  */
13947               BFD_ASSERT (offset == rel[1].r_offset);
13948               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13949               bfd_put_32 (input_bfd, insn2, contents + offset);
13950               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13951                 goto again;
13952             }
13953           break;
13954
13955         case R_PPC64_TLSLD:
13956           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13957             {
13958               unsigned int insn2;
13959               bfd_vma offset = rel->r_offset;
13960
13961               if (toc_symndx)
13962                 sec = local_sections[toc_symndx];
13963               for (r_symndx = 0;
13964                    r_symndx < symtab_hdr->sh_info;
13965                    r_symndx++)
13966                 if (local_sections[r_symndx] == sec)
13967                   break;
13968               if (r_symndx >= symtab_hdr->sh_info)
13969                 r_symndx = STN_UNDEF;
13970               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13971               if (r_symndx != STN_UNDEF)
13972                 rel->r_addend -= (local_syms[r_symndx].st_value
13973                                   + sec->output_offset
13974                                   + sec->output_section->vma);
13975
13976               r_type = R_PPC64_TPREL16_LO;
13977               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13978               rel->r_offset = offset + d_offset;
13979               /* Zap the reloc on the _tls_get_addr call too.  */
13980               BFD_ASSERT (offset == rel[1].r_offset);
13981               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13982               insn2 = 0x38630000;       /* addi 3,3,0 */
13983               bfd_put_32 (input_bfd, insn2, contents + offset);
13984               goto again;
13985             }
13986           break;
13987
13988         case R_PPC64_DTPMOD64:
13989           if (rel + 1 < relend
13990               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13991               && rel[1].r_offset == rel->r_offset + 8)
13992             {
13993               if ((tls_mask & TLS_GD) == 0)
13994                 {
13995                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13996                   if ((tls_mask & TLS_TPRELGD) != 0)
13997                     r_type = R_PPC64_TPREL64;
13998                   else
13999                     {
14000                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14001                       r_type = R_PPC64_NONE;
14002                     }
14003                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14004                 }
14005             }
14006           else
14007             {
14008               if ((tls_mask & TLS_LD) == 0)
14009                 {
14010                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14011                   r_type = R_PPC64_NONE;
14012                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14013                 }
14014             }
14015           break;
14016
14017         case R_PPC64_TPREL64:
14018           if ((tls_mask & TLS_TPREL) == 0)
14019             {
14020               r_type = R_PPC64_NONE;
14021               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14022             }
14023           break;
14024
14025         case R_PPC64_ENTRY:
14026           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14027           if (!bfd_link_pic (info)
14028               && !info->traditional_format
14029               && relocation + 0x80008000 <= 0xffffffff)
14030             {
14031               unsigned int insn1, insn2;
14032
14033               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14034               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14035               if ((insn1 & ~0xfffc) == LD_R2_0R12
14036                   && insn2 == ADD_R2_R2_R12)
14037                 {
14038                   bfd_put_32 (input_bfd,
14039                               LIS_R2 + PPC_HA (relocation),
14040                               contents + rel->r_offset);
14041                   bfd_put_32 (input_bfd,
14042                               ADDI_R2_R2 + PPC_LO (relocation),
14043                               contents + rel->r_offset + 4);
14044                 }
14045             }
14046           else
14047             {
14048               relocation -= (rel->r_offset
14049                              + input_section->output_offset
14050                              + input_section->output_section->vma);
14051               if (relocation + 0x80008000 <= 0xffffffff)
14052                 {
14053                   unsigned int insn1, insn2;
14054
14055                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14056                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14057                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14058                       && insn2 == ADD_R2_R2_R12)
14059                     {
14060                       bfd_put_32 (input_bfd,
14061                                   ADDIS_R2_R12 + PPC_HA (relocation),
14062                                   contents + rel->r_offset);
14063                       bfd_put_32 (input_bfd,
14064                                   ADDI_R2_R2 + PPC_LO (relocation),
14065                                   contents + rel->r_offset + 4);
14066                     }
14067                 }
14068             }
14069           break;
14070
14071         case R_PPC64_REL16_HA:
14072           /* If we are generating a non-PIC executable, edit
14073              .  0:      addis 2,12,.TOC.-0b@ha
14074              .          addi 2,2,.TOC.-0b@l
14075              used by ELFv2 global entry points to set up r2, to
14076              .          lis 2,.TOC.@ha
14077              .          addi 2,2,.TOC.@l
14078              if .TOC. is in range.  */
14079           if (!bfd_link_pic (info)
14080               && !info->traditional_format
14081               && !htab->opd_abi
14082               && rel->r_addend == d_offset
14083               && h != NULL && &h->elf == htab->elf.hgot
14084               && rel + 1 < relend
14085               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14086               && rel[1].r_offset == rel->r_offset + 4
14087               && rel[1].r_addend == rel->r_addend + 4
14088               && relocation + 0x80008000 <= 0xffffffff)
14089             {
14090               unsigned int insn1, insn2;
14091               bfd_vma offset = rel->r_offset - d_offset;
14092               insn1 = bfd_get_32 (input_bfd, contents + offset);
14093               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14094               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14095                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14096                 {
14097                   r_type = R_PPC64_ADDR16_HA;
14098                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14099                   rel->r_addend -= d_offset;
14100                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14101                   rel[1].r_addend -= d_offset + 4;
14102                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14103                 }
14104             }
14105           break;
14106         }
14107
14108       /* Handle other relocations that tweak non-addend part of insn.  */
14109       insn = 0;
14110       max_br_offset = 1 << 25;
14111       addend = rel->r_addend;
14112       reloc_dest = DEST_NORMAL;
14113       switch (r_type)
14114         {
14115         default:
14116           break;
14117
14118         case R_PPC64_TOCSAVE:
14119           if (relocation + addend == (rel->r_offset
14120                                       + input_section->output_offset
14121                                       + input_section->output_section->vma)
14122               && tocsave_find (htab, NO_INSERT,
14123                                &local_syms, rel, input_bfd))
14124             {
14125               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14126               if (insn == NOP
14127                   || insn == CROR_151515 || insn == CROR_313131)
14128                 bfd_put_32 (input_bfd,
14129                             STD_R2_0R1 + STK_TOC (htab),
14130                             contents + rel->r_offset);
14131             }
14132           break;
14133
14134           /* Branch taken prediction relocations.  */
14135         case R_PPC64_ADDR14_BRTAKEN:
14136         case R_PPC64_REL14_BRTAKEN:
14137           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14138           /* Fall through.  */
14139
14140           /* Branch not taken prediction relocations.  */
14141         case R_PPC64_ADDR14_BRNTAKEN:
14142         case R_PPC64_REL14_BRNTAKEN:
14143           insn |= bfd_get_32 (input_bfd,
14144                               contents + rel->r_offset) & ~(0x01 << 21);
14145           /* Fall through.  */
14146
14147         case R_PPC64_REL14:
14148           max_br_offset = 1 << 15;
14149           /* Fall through.  */
14150
14151         case R_PPC64_REL24:
14152           /* Calls to functions with a different TOC, such as calls to
14153              shared objects, need to alter the TOC pointer.  This is
14154              done using a linkage stub.  A REL24 branching to these
14155              linkage stubs needs to be followed by a nop, as the nop
14156              will be replaced with an instruction to restore the TOC
14157              base pointer.  */
14158           fdh = h;
14159           if (h != NULL
14160               && h->oh != NULL
14161               && h->oh->is_func_descriptor)
14162             fdh = ppc_follow_link (h->oh);
14163           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14164                                            htab);
14165           if (stub_entry != NULL
14166               && (stub_entry->stub_type == ppc_stub_plt_call
14167                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14168                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14169                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14170             {
14171               bfd_boolean can_plt_call = FALSE;
14172
14173               if (stub_entry->stub_type == ppc_stub_plt_call
14174                   && !htab->opd_abi
14175                   && htab->params->plt_localentry0 != 0
14176                   && is_elfv2_localentry0 (&h->elf))
14177                 {
14178                   /* The function doesn't use or change r2.  */
14179                   can_plt_call = TRUE;
14180                 }
14181
14182               /* All of these stubs may modify r2, so there must be a
14183                  branch and link followed by a nop.  The nop is
14184                  replaced by an insn to restore r2.  */
14185               else if (rel->r_offset + 8 <= input_section->size)
14186                 {
14187                   unsigned long br;
14188
14189                   br = bfd_get_32 (input_bfd,
14190                                    contents + rel->r_offset);
14191                   if ((br & 1) != 0)
14192                     {
14193                       unsigned long nop;
14194
14195                       nop = bfd_get_32 (input_bfd,
14196                                         contents + rel->r_offset + 4);
14197                       if (nop == NOP
14198                           || nop == CROR_151515 || nop == CROR_313131)
14199                         {
14200                           if (h != NULL
14201                               && (h == htab->tls_get_addr_fd
14202                                   || h == htab->tls_get_addr)
14203                               && htab->params->tls_get_addr_opt)
14204                             {
14205                               /* Special stub used, leave nop alone.  */
14206                             }
14207                           else
14208                             bfd_put_32 (input_bfd,
14209                                         LD_R2_0R1 + STK_TOC (htab),
14210                                         contents + rel->r_offset + 4);
14211                           can_plt_call = TRUE;
14212                         }
14213                     }
14214                 }
14215
14216               if (!can_plt_call && h != NULL)
14217                 {
14218                   const char *name = h->elf.root.root.string;
14219
14220                   if (*name == '.')
14221                     ++name;
14222
14223                   if (strncmp (name, "__libc_start_main", 17) == 0
14224                       && (name[17] == 0 || name[17] == '@'))
14225                     {
14226                       /* Allow crt1 branch to go via a toc adjusting
14227                          stub.  Other calls that never return could do
14228                          the same, if we could detect such.  */
14229                       can_plt_call = TRUE;
14230                     }
14231                 }
14232
14233               if (!can_plt_call)
14234                 {
14235                   /* g++ as of 20130507 emits self-calls without a
14236                      following nop.  This is arguably wrong since we
14237                      have conflicting information.  On the one hand a
14238                      global symbol and on the other a local call
14239                      sequence, but don't error for this special case.
14240                      It isn't possible to cheaply verify we have
14241                      exactly such a call.  Allow all calls to the same
14242                      section.  */
14243                   asection *code_sec = sec;
14244
14245                   if (get_opd_info (sec) != NULL)
14246                     {
14247                       bfd_vma off = (relocation + addend
14248                                      - sec->output_section->vma
14249                                      - sec->output_offset);
14250
14251                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14252                     }
14253                   if (code_sec == input_section)
14254                     can_plt_call = TRUE;
14255                 }
14256
14257               if (!can_plt_call)
14258                 {
14259                   if (stub_entry->stub_type == ppc_stub_plt_call
14260                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14261                     info->callbacks->einfo
14262                       /* xgettext:c-format */
14263                       (_("%H: call to `%T' lacks nop, can't restore toc; "
14264                          "recompile with -fPIC\n"),
14265                        input_bfd, input_section, rel->r_offset, sym_name);
14266                   else
14267                     info->callbacks->einfo
14268                       /* xgettext:c-format */
14269                       (_("%H: call to `%T' lacks nop, can't restore toc; "
14270                          "(-mcmodel=small toc adjust stub)\n"),
14271                        input_bfd, input_section, rel->r_offset, sym_name);
14272
14273                   bfd_set_error (bfd_error_bad_value);
14274                   ret = FALSE;
14275                 }
14276
14277               if (can_plt_call
14278                   && (stub_entry->stub_type == ppc_stub_plt_call
14279                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14280                 unresolved_reloc = FALSE;
14281             }
14282
14283           if ((stub_entry == NULL
14284                || stub_entry->stub_type == ppc_stub_long_branch
14285                || stub_entry->stub_type == ppc_stub_plt_branch)
14286               && get_opd_info (sec) != NULL)
14287             {
14288               /* The branch destination is the value of the opd entry. */
14289               bfd_vma off = (relocation + addend
14290                              - sec->output_section->vma
14291                              - sec->output_offset);
14292               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14293               if (dest != (bfd_vma) -1)
14294                 {
14295                   relocation = dest;
14296                   addend = 0;
14297                   reloc_dest = DEST_OPD;
14298                 }
14299             }
14300
14301           /* If the branch is out of reach we ought to have a long
14302              branch stub.  */
14303           from = (rel->r_offset
14304                   + input_section->output_offset
14305                   + input_section->output_section->vma);
14306
14307           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14308                                                   ? fdh->elf.other
14309                                                   : sym->st_other);
14310
14311           if (stub_entry != NULL
14312               && (stub_entry->stub_type == ppc_stub_long_branch
14313                   || stub_entry->stub_type == ppc_stub_plt_branch)
14314               && (r_type == R_PPC64_ADDR14_BRTAKEN
14315                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14316                   || (relocation + addend - from + max_br_offset
14317                       < 2 * max_br_offset)))
14318             /* Don't use the stub if this branch is in range.  */
14319             stub_entry = NULL;
14320
14321           if (stub_entry != NULL)
14322             {
14323               /* Munge up the value and addend so that we call the stub
14324                  rather than the procedure directly.  */
14325               asection *stub_sec = stub_entry->group->stub_sec;
14326
14327               if (stub_entry->stub_type == ppc_stub_save_res)
14328                 relocation += (stub_sec->output_offset
14329                                + stub_sec->output_section->vma
14330                                + stub_sec->size - htab->sfpr->size
14331                                - htab->sfpr->output_offset
14332                                - htab->sfpr->output_section->vma);
14333               else
14334                 relocation = (stub_entry->stub_offset
14335                               + stub_sec->output_offset
14336                               + stub_sec->output_section->vma);
14337               addend = 0;
14338               reloc_dest = DEST_STUB;
14339
14340               if ((stub_entry->stub_type == ppc_stub_plt_call
14341                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14342                   && (ALWAYS_EMIT_R2SAVE
14343                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14344                   && rel + 1 < relend
14345                   && rel[1].r_offset == rel->r_offset + 4
14346                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14347                 relocation += 4;
14348             }
14349
14350           if (insn != 0)
14351             {
14352               if (is_isa_v2)
14353                 {
14354                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14355                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14356                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14357                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14358                     insn |= 0x02 << 21;
14359                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14360                     insn |= 0x08 << 21;
14361                   else
14362                     break;
14363                 }
14364               else
14365                 {
14366                   /* Invert 'y' bit if not the default.  */
14367                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14368                     insn ^= 0x01 << 21;
14369                 }
14370
14371               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14372             }
14373
14374           /* NOP out calls to undefined weak functions.
14375              We can thus call a weak function without first
14376              checking whether the function is defined.  */
14377           else if (h != NULL
14378                    && h->elf.root.type == bfd_link_hash_undefweak
14379                    && h->elf.dynindx == -1
14380                    && r_type == R_PPC64_REL24
14381                    && relocation == 0
14382                    && addend == 0)
14383             {
14384               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14385               goto copy_reloc;
14386             }
14387           break;
14388         }
14389
14390       /* Set `addend'.  */
14391       tls_type = 0;
14392       switch (r_type)
14393         {
14394         default:
14395           info->callbacks->einfo
14396             /* xgettext:c-format */
14397             (_("%P: %B: unknown relocation type %d for `%T'\n"),
14398              input_bfd, (int) r_type, sym_name);
14399
14400           bfd_set_error (bfd_error_bad_value);
14401           ret = FALSE;
14402           goto copy_reloc;
14403
14404         case R_PPC64_NONE:
14405         case R_PPC64_TLS:
14406         case R_PPC64_TLSGD:
14407         case R_PPC64_TLSLD:
14408         case R_PPC64_TOCSAVE:
14409         case R_PPC64_GNU_VTINHERIT:
14410         case R_PPC64_GNU_VTENTRY:
14411         case R_PPC64_ENTRY:
14412           goto copy_reloc;
14413
14414           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14415              address in the GOT as relocation value instead of the
14416              symbol's value itself.  Also, create a GOT entry for the
14417              symbol and put the symbol value there.  */
14418         case R_PPC64_GOT_TLSGD16:
14419         case R_PPC64_GOT_TLSGD16_LO:
14420         case R_PPC64_GOT_TLSGD16_HI:
14421         case R_PPC64_GOT_TLSGD16_HA:
14422           tls_type = TLS_TLS | TLS_GD;
14423           goto dogot;
14424
14425         case R_PPC64_GOT_TLSLD16:
14426         case R_PPC64_GOT_TLSLD16_LO:
14427         case R_PPC64_GOT_TLSLD16_HI:
14428         case R_PPC64_GOT_TLSLD16_HA:
14429           tls_type = TLS_TLS | TLS_LD;
14430           goto dogot;
14431
14432         case R_PPC64_GOT_TPREL16_DS:
14433         case R_PPC64_GOT_TPREL16_LO_DS:
14434         case R_PPC64_GOT_TPREL16_HI:
14435         case R_PPC64_GOT_TPREL16_HA:
14436           tls_type = TLS_TLS | TLS_TPREL;
14437           goto dogot;
14438
14439         case R_PPC64_GOT_DTPREL16_DS:
14440         case R_PPC64_GOT_DTPREL16_LO_DS:
14441         case R_PPC64_GOT_DTPREL16_HI:
14442         case R_PPC64_GOT_DTPREL16_HA:
14443           tls_type = TLS_TLS | TLS_DTPREL;
14444           goto dogot;
14445
14446         case R_PPC64_GOT16:
14447         case R_PPC64_GOT16_LO:
14448         case R_PPC64_GOT16_HI:
14449         case R_PPC64_GOT16_HA:
14450         case R_PPC64_GOT16_DS:
14451         case R_PPC64_GOT16_LO_DS:
14452         dogot:
14453           {
14454             /* Relocation is to the entry for this symbol in the global
14455                offset table.  */
14456             asection *got;
14457             bfd_vma *offp;
14458             bfd_vma off;
14459             unsigned long indx = 0;
14460             struct got_entry *ent;
14461
14462             if (tls_type == (TLS_TLS | TLS_LD)
14463                 && (h == NULL
14464                     || !h->elf.def_dynamic))
14465               ent = ppc64_tlsld_got (input_bfd);
14466             else
14467               {
14468                 if (h != NULL)
14469                   {
14470                     if (!htab->elf.dynamic_sections_created
14471                         || h->elf.dynindx == -1
14472                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14473                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14474                       /* This is actually a static link, or it is a
14475                          -Bsymbolic link and the symbol is defined
14476                          locally, or the symbol was forced to be local
14477                          because of a version file.  */
14478                       ;
14479                     else
14480                       {
14481                         indx = h->elf.dynindx;
14482                         unresolved_reloc = FALSE;
14483                       }
14484                     ent = h->elf.got.glist;
14485                   }
14486                 else
14487                   {
14488                     if (local_got_ents == NULL)
14489                       abort ();
14490                     ent = local_got_ents[r_symndx];
14491                   }
14492
14493                 for (; ent != NULL; ent = ent->next)
14494                   if (ent->addend == orig_rel.r_addend
14495                       && ent->owner == input_bfd
14496                       && ent->tls_type == tls_type)
14497                     break;
14498               }
14499
14500             if (ent == NULL)
14501               abort ();
14502             if (ent->is_indirect)
14503               ent = ent->got.ent;
14504             offp = &ent->got.offset;
14505             got = ppc64_elf_tdata (ent->owner)->got;
14506             if (got == NULL)
14507               abort ();
14508
14509             /* The offset must always be a multiple of 8.  We use the
14510                least significant bit to record whether we have already
14511                processed this entry.  */
14512             off = *offp;
14513             if ((off & 1) != 0)
14514               off &= ~1;
14515             else
14516               {
14517                 /* Generate relocs for the dynamic linker, except in
14518                    the case of TLSLD where we'll use one entry per
14519                    module.  */
14520                 asection *relgot;
14521                 bfd_boolean ifunc;
14522
14523                 *offp = off | 1;
14524                 relgot = NULL;
14525                 ifunc = (h != NULL
14526                          ? h->elf.type == STT_GNU_IFUNC
14527                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14528                 if (ifunc)
14529                   {
14530                     relgot = htab->elf.irelplt;
14531                     if (indx == 0)
14532                       htab->local_ifunc_resolver = 1;
14533                     else if (is_static_defined (&h->elf))
14534                       htab->maybe_local_ifunc_resolver = 1;
14535                   }
14536                 else if (indx != 0
14537                          || (bfd_link_pic (info)
14538                              && (h == NULL
14539                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14540                                  || (tls_type == (TLS_TLS | TLS_LD)
14541                                      && !h->elf.def_dynamic))
14542                              && !(tls_type == (TLS_TLS | TLS_TPREL)
14543                                   && bfd_link_executable (info)
14544                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14545                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14546                 if (relgot != NULL)
14547                   {
14548                     outrel.r_offset = (got->output_section->vma
14549                                        + got->output_offset
14550                                        + off);
14551                     outrel.r_addend = addend;
14552                     if (tls_type & (TLS_LD | TLS_GD))
14553                       {
14554                         outrel.r_addend = 0;
14555                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14556                         if (tls_type == (TLS_TLS | TLS_GD))
14557                           {
14558                             loc = relgot->contents;
14559                             loc += (relgot->reloc_count++
14560                                     * sizeof (Elf64_External_Rela));
14561                             bfd_elf64_swap_reloca_out (output_bfd,
14562                                                        &outrel, loc);
14563                             outrel.r_offset += 8;
14564                             outrel.r_addend = addend;
14565                             outrel.r_info
14566                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14567                           }
14568                       }
14569                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14570                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14571                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14572                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14573                     else if (indx != 0)
14574                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14575                     else
14576                       {
14577                         if (ifunc)
14578                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14579                         else
14580                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14581
14582                         /* Write the .got section contents for the sake
14583                            of prelink.  */
14584                         loc = got->contents + off;
14585                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14586                                     loc);
14587                       }
14588
14589                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14590                       {
14591                         outrel.r_addend += relocation;
14592                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14593                           {
14594                             if (htab->elf.tls_sec == NULL)
14595                               outrel.r_addend = 0;
14596                             else
14597                               outrel.r_addend -= htab->elf.tls_sec->vma;
14598                           }
14599                       }
14600                     loc = relgot->contents;
14601                     loc += (relgot->reloc_count++
14602                             * sizeof (Elf64_External_Rela));
14603                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14604                   }
14605
14606                 /* Init the .got section contents here if we're not
14607                    emitting a reloc.  */
14608                 else
14609                   {
14610                     relocation += addend;
14611                     if (tls_type != 0)
14612                       {
14613                         if (htab->elf.tls_sec == NULL)
14614                           relocation = 0;
14615                         else
14616                           {
14617                             if (tls_type & TLS_LD)
14618                               relocation = 0;
14619                             else
14620                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14621                             if (tls_type & TLS_TPREL)
14622                               relocation += DTP_OFFSET - TP_OFFSET;
14623                           }
14624
14625                         if (tls_type & (TLS_GD | TLS_LD))
14626                           {
14627                             bfd_put_64 (output_bfd, relocation,
14628                                         got->contents + off + 8);
14629                             relocation = 1;
14630                           }
14631                       }
14632                     bfd_put_64 (output_bfd, relocation,
14633                                 got->contents + off);
14634                   }
14635               }
14636
14637             if (off >= (bfd_vma) -2)
14638               abort ();
14639
14640             relocation = got->output_section->vma + got->output_offset + off;
14641             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14642           }
14643           break;
14644
14645         case R_PPC64_PLT16_HA:
14646         case R_PPC64_PLT16_HI:
14647         case R_PPC64_PLT16_LO:
14648         case R_PPC64_PLT32:
14649         case R_PPC64_PLT64:
14650           /* Relocation is to the entry for this symbol in the
14651              procedure linkage table.  */
14652           {
14653             struct plt_entry **plt_list = NULL;
14654             if (h != NULL)
14655               plt_list = &h->elf.plt.plist;
14656             else if (local_got_ents != NULL)
14657               {
14658                 struct plt_entry **local_plt = (struct plt_entry **)
14659                   (local_got_ents + symtab_hdr->sh_info);
14660                 unsigned char *local_got_tls_masks = (unsigned char *)
14661                   (local_plt + symtab_hdr->sh_info);
14662                 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14663                   plt_list = local_plt + r_symndx;
14664               }
14665             if (plt_list)
14666               {
14667                 struct plt_entry *ent;
14668
14669                 for (ent = *plt_list; ent != NULL; ent = ent->next)
14670                   if (ent->plt.offset != (bfd_vma) -1
14671                       && ent->addend == orig_rel.r_addend)
14672                     {
14673                       asection *plt;
14674
14675                       plt = htab->elf.splt;
14676                       if (!htab->elf.dynamic_sections_created
14677                           || h == NULL
14678                           || h->elf.dynindx == -1)
14679                         plt = htab->elf.iplt;
14680                       relocation = (plt->output_section->vma
14681                                     + plt->output_offset
14682                                     + ent->plt.offset);
14683                       addend = 0;
14684                       unresolved_reloc = FALSE;
14685                       break;
14686                     }
14687               }
14688           }
14689           break;
14690
14691         case R_PPC64_TOC:
14692           /* Relocation value is TOC base.  */
14693           relocation = TOCstart;
14694           if (r_symndx == STN_UNDEF)
14695             relocation += htab->sec_info[input_section->id].toc_off;
14696           else if (unresolved_reloc)
14697             ;
14698           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14699             relocation += htab->sec_info[sec->id].toc_off;
14700           else
14701             unresolved_reloc = TRUE;
14702           goto dodyn;
14703
14704           /* TOC16 relocs.  We want the offset relative to the TOC base,
14705              which is the address of the start of the TOC plus 0x8000.
14706              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14707              in this order.  */
14708         case R_PPC64_TOC16:
14709         case R_PPC64_TOC16_LO:
14710         case R_PPC64_TOC16_HI:
14711         case R_PPC64_TOC16_DS:
14712         case R_PPC64_TOC16_LO_DS:
14713         case R_PPC64_TOC16_HA:
14714           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14715           break;
14716
14717           /* Relocate against the beginning of the section.  */
14718         case R_PPC64_SECTOFF:
14719         case R_PPC64_SECTOFF_LO:
14720         case R_PPC64_SECTOFF_HI:
14721         case R_PPC64_SECTOFF_DS:
14722         case R_PPC64_SECTOFF_LO_DS:
14723         case R_PPC64_SECTOFF_HA:
14724           if (sec != NULL)
14725             addend -= sec->output_section->vma;
14726           break;
14727
14728         case R_PPC64_REL16:
14729         case R_PPC64_REL16_LO:
14730         case R_PPC64_REL16_HI:
14731         case R_PPC64_REL16_HA:
14732         case R_PPC64_REL16DX_HA:
14733           break;
14734
14735         case R_PPC64_REL14:
14736         case R_PPC64_REL14_BRNTAKEN:
14737         case R_PPC64_REL14_BRTAKEN:
14738         case R_PPC64_REL24:
14739           break;
14740
14741         case R_PPC64_TPREL16:
14742         case R_PPC64_TPREL16_LO:
14743         case R_PPC64_TPREL16_HI:
14744         case R_PPC64_TPREL16_HA:
14745         case R_PPC64_TPREL16_DS:
14746         case R_PPC64_TPREL16_LO_DS:
14747         case R_PPC64_TPREL16_HIGH:
14748         case R_PPC64_TPREL16_HIGHA:
14749         case R_PPC64_TPREL16_HIGHER:
14750         case R_PPC64_TPREL16_HIGHERA:
14751         case R_PPC64_TPREL16_HIGHEST:
14752         case R_PPC64_TPREL16_HIGHESTA:
14753           if (h != NULL
14754               && h->elf.root.type == bfd_link_hash_undefweak
14755               && h->elf.dynindx == -1)
14756             {
14757               /* Make this relocation against an undefined weak symbol
14758                  resolve to zero.  This is really just a tweak, since
14759                  code using weak externs ought to check that they are
14760                  defined before using them.  */
14761               bfd_byte *p = contents + rel->r_offset - d_offset;
14762
14763               insn = bfd_get_32 (input_bfd, p);
14764               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14765               if (insn != 0)
14766                 bfd_put_32 (input_bfd, insn, p);
14767               break;
14768             }
14769           if (htab->elf.tls_sec != NULL)
14770             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14771           /* The TPREL16 relocs shouldn't really be used in shared
14772              libs or with non-local symbols as that will result in
14773              DT_TEXTREL being set, but support them anyway.  */
14774           goto dodyn;
14775
14776         case R_PPC64_DTPREL16:
14777         case R_PPC64_DTPREL16_LO:
14778         case R_PPC64_DTPREL16_HI:
14779         case R_PPC64_DTPREL16_HA:
14780         case R_PPC64_DTPREL16_DS:
14781         case R_PPC64_DTPREL16_LO_DS:
14782         case R_PPC64_DTPREL16_HIGH:
14783         case R_PPC64_DTPREL16_HIGHA:
14784         case R_PPC64_DTPREL16_HIGHER:
14785         case R_PPC64_DTPREL16_HIGHERA:
14786         case R_PPC64_DTPREL16_HIGHEST:
14787         case R_PPC64_DTPREL16_HIGHESTA:
14788           if (htab->elf.tls_sec != NULL)
14789             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14790           break;
14791
14792         case R_PPC64_ADDR64_LOCAL:
14793           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14794                                               ? h->elf.other
14795                                               : sym->st_other);
14796           break;
14797
14798         case R_PPC64_DTPMOD64:
14799           relocation = 1;
14800           addend = 0;
14801           goto dodyn;
14802
14803         case R_PPC64_TPREL64:
14804           if (htab->elf.tls_sec != NULL)
14805             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14806           goto dodyn;
14807
14808         case R_PPC64_DTPREL64:
14809           if (htab->elf.tls_sec != NULL)
14810             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14811           /* Fall through.  */
14812
14813           /* Relocations that may need to be propagated if this is a
14814              dynamic object.  */
14815         case R_PPC64_REL30:
14816         case R_PPC64_REL32:
14817         case R_PPC64_REL64:
14818         case R_PPC64_ADDR14:
14819         case R_PPC64_ADDR14_BRNTAKEN:
14820         case R_PPC64_ADDR14_BRTAKEN:
14821         case R_PPC64_ADDR16:
14822         case R_PPC64_ADDR16_DS:
14823         case R_PPC64_ADDR16_HA:
14824         case R_PPC64_ADDR16_HI:
14825         case R_PPC64_ADDR16_HIGH:
14826         case R_PPC64_ADDR16_HIGHA:
14827         case R_PPC64_ADDR16_HIGHER:
14828         case R_PPC64_ADDR16_HIGHERA:
14829         case R_PPC64_ADDR16_HIGHEST:
14830         case R_PPC64_ADDR16_HIGHESTA:
14831         case R_PPC64_ADDR16_LO:
14832         case R_PPC64_ADDR16_LO_DS:
14833         case R_PPC64_ADDR24:
14834         case R_PPC64_ADDR32:
14835         case R_PPC64_ADDR64:
14836         case R_PPC64_UADDR16:
14837         case R_PPC64_UADDR32:
14838         case R_PPC64_UADDR64:
14839         dodyn:
14840           if ((input_section->flags & SEC_ALLOC) == 0)
14841             break;
14842
14843           if (NO_OPD_RELOCS && is_opd)
14844             break;
14845
14846           if (bfd_link_pic (info)
14847               ? ((h == NULL
14848                   || h->dyn_relocs != NULL)
14849                  && ((h != NULL && pc_dynrelocs (h))
14850                      || must_be_dyn_reloc (info, r_type)))
14851               : (h != NULL
14852                  ? h->dyn_relocs != NULL
14853                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14854             {
14855               bfd_boolean skip, relocate;
14856               asection *sreloc;
14857               bfd_vma out_off;
14858               long indx = 0;
14859
14860               /* When generating a dynamic object, these relocations
14861                  are copied into the output file to be resolved at run
14862                  time.  */
14863
14864               skip = FALSE;
14865               relocate = FALSE;
14866
14867               out_off = _bfd_elf_section_offset (output_bfd, info,
14868                                                  input_section, rel->r_offset);
14869               if (out_off == (bfd_vma) -1)
14870                 skip = TRUE;
14871               else if (out_off == (bfd_vma) -2)
14872                 skip = TRUE, relocate = TRUE;
14873               out_off += (input_section->output_section->vma
14874                           + input_section->output_offset);
14875               outrel.r_offset = out_off;
14876               outrel.r_addend = rel->r_addend;
14877
14878               /* Optimize unaligned reloc use.  */
14879               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14880                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14881                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14882               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14883                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14884                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14885               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14886                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14887                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14888
14889               if (skip)
14890                 memset (&outrel, 0, sizeof outrel);
14891               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14892                        && !is_opd
14893                        && r_type != R_PPC64_TOC)
14894                 {
14895                   indx = h->elf.dynindx;
14896                   BFD_ASSERT (indx != -1);
14897                   outrel.r_info = ELF64_R_INFO (indx, r_type);
14898                 }
14899               else
14900                 {
14901                   /* This symbol is local, or marked to become local,
14902                      or this is an opd section reloc which must point
14903                      at a local function.  */
14904                   outrel.r_addend += relocation;
14905                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14906                     {
14907                       if (is_opd && h != NULL)
14908                         {
14909                           /* Lie about opd entries.  This case occurs
14910                              when building shared libraries and we
14911                              reference a function in another shared
14912                              lib.  The same thing happens for a weak
14913                              definition in an application that's
14914                              overridden by a strong definition in a
14915                              shared lib.  (I believe this is a generic
14916                              bug in binutils handling of weak syms.)
14917                              In these cases we won't use the opd
14918                              entry in this lib.  */
14919                           unresolved_reloc = FALSE;
14920                         }
14921                       if (!is_opd
14922                           && r_type == R_PPC64_ADDR64
14923                           && (h != NULL
14924                               ? h->elf.type == STT_GNU_IFUNC
14925                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14926                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14927                       else
14928                         {
14929                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14930
14931                           /* We need to relocate .opd contents for ld.so.
14932                              Prelink also wants simple and consistent rules
14933                              for relocs.  This make all RELATIVE relocs have
14934                              *r_offset equal to r_addend.  */
14935                           relocate = TRUE;
14936                         }
14937                     }
14938                   else
14939                     {
14940                       if (h != NULL
14941                           ? h->elf.type == STT_GNU_IFUNC
14942                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14943                         {
14944                           info->callbacks->einfo
14945                             /* xgettext:c-format */
14946                             (_("%H: %s for indirect "
14947                                "function `%T' unsupported\n"),
14948                              input_bfd, input_section, rel->r_offset,
14949                              ppc64_elf_howto_table[r_type]->name,
14950                              sym_name);
14951                           ret = FALSE;
14952                         }
14953                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14954                         ;
14955                       else if (sec == NULL || sec->owner == NULL)
14956                         {
14957                           bfd_set_error (bfd_error_bad_value);
14958                           return FALSE;
14959                         }
14960                       else
14961                         {
14962                           asection *osec;
14963
14964                           osec = sec->output_section;
14965                           indx = elf_section_data (osec)->dynindx;
14966
14967                           if (indx == 0)
14968                             {
14969                               if ((osec->flags & SEC_READONLY) == 0
14970                                   && htab->elf.data_index_section != NULL)
14971                                 osec = htab->elf.data_index_section;
14972                               else
14973                                 osec = htab->elf.text_index_section;
14974                               indx = elf_section_data (osec)->dynindx;
14975                             }
14976                           BFD_ASSERT (indx != 0);
14977
14978                           /* We are turning this relocation into one
14979                              against a section symbol, so subtract out
14980                              the output section's address but not the
14981                              offset of the input section in the output
14982                              section.  */
14983                           outrel.r_addend -= osec->vma;
14984                         }
14985
14986                       outrel.r_info = ELF64_R_INFO (indx, r_type);
14987                     }
14988                 }
14989
14990               sreloc = elf_section_data (input_section)->sreloc;
14991               if (h != NULL
14992                   ? h->elf.type == STT_GNU_IFUNC
14993                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14994                 {
14995                   sreloc = htab->elf.irelplt;
14996                   if (indx == 0)
14997                     htab->local_ifunc_resolver = 1;
14998                   else if (is_static_defined (&h->elf))
14999                     htab->maybe_local_ifunc_resolver = 1;
15000                 }
15001               if (sreloc == NULL)
15002                 abort ();
15003
15004               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15005                   >= sreloc->size)
15006                 abort ();
15007               loc = sreloc->contents;
15008               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15009               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15010
15011               /* If this reloc is against an external symbol, it will
15012                  be computed at runtime, so there's no need to do
15013                  anything now.  However, for the sake of prelink ensure
15014                  that the section contents are a known value.  */
15015               if (! relocate)
15016                 {
15017                   unresolved_reloc = FALSE;
15018                   /* The value chosen here is quite arbitrary as ld.so
15019                      ignores section contents except for the special
15020                      case of .opd where the contents might be accessed
15021                      before relocation.  Choose zero, as that won't
15022                      cause reloc overflow.  */
15023                   relocation = 0;
15024                   addend = 0;
15025                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15026                      to improve backward compatibility with older
15027                      versions of ld.  */
15028                   if (r_type == R_PPC64_ADDR64)
15029                     addend = outrel.r_addend;
15030                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15031                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15032                     addend = outrel.r_offset;
15033                 }
15034             }
15035           break;
15036
15037         case R_PPC64_COPY:
15038         case R_PPC64_GLOB_DAT:
15039         case R_PPC64_JMP_SLOT:
15040         case R_PPC64_JMP_IREL:
15041         case R_PPC64_RELATIVE:
15042           /* We shouldn't ever see these dynamic relocs in relocatable
15043              files.  */
15044           /* Fall through.  */
15045
15046         case R_PPC64_PLTGOT16:
15047         case R_PPC64_PLTGOT16_DS:
15048         case R_PPC64_PLTGOT16_HA:
15049         case R_PPC64_PLTGOT16_HI:
15050         case R_PPC64_PLTGOT16_LO:
15051         case R_PPC64_PLTGOT16_LO_DS:
15052         case R_PPC64_PLTREL32:
15053         case R_PPC64_PLTREL64:
15054           /* These ones haven't been implemented yet.  */
15055
15056           info->callbacks->einfo
15057             /* xgettext:c-format */
15058             (_("%P: %B: %s is not supported for `%T'\n"),
15059              input_bfd,
15060              ppc64_elf_howto_table[r_type]->name, sym_name);
15061
15062           bfd_set_error (bfd_error_invalid_operation);
15063           ret = FALSE;
15064           goto copy_reloc;
15065         }
15066
15067       /* Multi-instruction sequences that access the TOC can be
15068          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15069          to             nop;           addi rb,r2,x;  */
15070       switch (r_type)
15071         {
15072         default:
15073           break;
15074
15075         case R_PPC64_GOT_TLSLD16_HI:
15076         case R_PPC64_GOT_TLSGD16_HI:
15077         case R_PPC64_GOT_TPREL16_HI:
15078         case R_PPC64_GOT_DTPREL16_HI:
15079         case R_PPC64_GOT16_HI:
15080         case R_PPC64_TOC16_HI:
15081           /* These relocs would only be useful if building up an
15082              offset to later add to r2, perhaps in an indexed
15083              addressing mode instruction.  Don't try to optimize.
15084              Unfortunately, the possibility of someone building up an
15085              offset like this or even with the HA relocs, means that
15086              we need to check the high insn when optimizing the low
15087              insn.  */
15088           break;
15089
15090         case R_PPC64_GOT_TLSLD16_HA:
15091         case R_PPC64_GOT_TLSGD16_HA:
15092         case R_PPC64_GOT_TPREL16_HA:
15093         case R_PPC64_GOT_DTPREL16_HA:
15094         case R_PPC64_GOT16_HA:
15095         case R_PPC64_TOC16_HA:
15096           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15097               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15098             {
15099               bfd_byte *p = contents + (rel->r_offset & ~3);
15100               bfd_put_32 (input_bfd, NOP, p);
15101               goto copy_reloc;
15102             }
15103           break;
15104
15105         case R_PPC64_GOT_TLSLD16_LO:
15106         case R_PPC64_GOT_TLSGD16_LO:
15107         case R_PPC64_GOT_TPREL16_LO_DS:
15108         case R_PPC64_GOT_DTPREL16_LO_DS:
15109         case R_PPC64_GOT16_LO:
15110         case R_PPC64_GOT16_LO_DS:
15111         case R_PPC64_TOC16_LO:
15112         case R_PPC64_TOC16_LO_DS:
15113           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15114               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15115             {
15116               bfd_byte *p = contents + (rel->r_offset & ~3);
15117               insn = bfd_get_32 (input_bfd, p);
15118               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15119                 {
15120                   /* Transform addic to addi when we change reg.  */
15121                   insn &= ~((0x3f << 26) | (0x1f << 16));
15122                   insn |= (14u << 26) | (2 << 16);
15123                 }
15124               else
15125                 {
15126                   insn &= ~(0x1f << 16);
15127                   insn |= 2 << 16;
15128                 }
15129               bfd_put_32 (input_bfd, insn, p);
15130             }
15131           break;
15132
15133         case R_PPC64_TPREL16_HA:
15134           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15135             {
15136               bfd_byte *p = contents + (rel->r_offset & ~3);
15137               insn = bfd_get_32 (input_bfd, p);
15138               if ((insn & ((0x3f << 26) | 0x1f << 16))
15139                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15140                 /* xgettext:c-format */
15141                 info->callbacks->minfo
15142                   (_("%H: warning: %s unexpected insn %#x.\n"),
15143                    input_bfd, input_section, rel->r_offset,
15144                    ppc64_elf_howto_table[r_type]->name, insn);
15145               else
15146                 {
15147                   bfd_put_32 (input_bfd, NOP, p);
15148                   goto copy_reloc;
15149                 }
15150             }
15151           break;
15152
15153         case R_PPC64_TPREL16_LO:
15154         case R_PPC64_TPREL16_LO_DS:
15155           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15156             {
15157               bfd_byte *p = contents + (rel->r_offset & ~3);
15158               insn = bfd_get_32 (input_bfd, p);
15159               insn &= ~(0x1f << 16);
15160               insn |= 13 << 16;
15161               bfd_put_32 (input_bfd, insn, p);
15162             }
15163           break;
15164         }
15165
15166       /* Do any further special processing.  */
15167       switch (r_type)
15168         {
15169         default:
15170           break;
15171
15172         case R_PPC64_REL16_HA:
15173         case R_PPC64_REL16DX_HA:
15174         case R_PPC64_ADDR16_HA:
15175         case R_PPC64_ADDR16_HIGHA:
15176         case R_PPC64_ADDR16_HIGHERA:
15177         case R_PPC64_ADDR16_HIGHESTA:
15178         case R_PPC64_TOC16_HA:
15179         case R_PPC64_SECTOFF_HA:
15180         case R_PPC64_TPREL16_HA:
15181         case R_PPC64_TPREL16_HIGHA:
15182         case R_PPC64_TPREL16_HIGHERA:
15183         case R_PPC64_TPREL16_HIGHESTA:
15184         case R_PPC64_DTPREL16_HA:
15185         case R_PPC64_DTPREL16_HIGHA:
15186         case R_PPC64_DTPREL16_HIGHERA:
15187         case R_PPC64_DTPREL16_HIGHESTA:
15188           /* It's just possible that this symbol is a weak symbol
15189              that's not actually defined anywhere. In that case,
15190              'sec' would be NULL, and we should leave the symbol
15191              alone (it will be set to zero elsewhere in the link).  */
15192           if (sec == NULL)
15193             break;
15194           /* Fall through.  */
15195
15196         case R_PPC64_GOT16_HA:
15197         case R_PPC64_PLTGOT16_HA:
15198         case R_PPC64_PLT16_HA:
15199         case R_PPC64_GOT_TLSGD16_HA:
15200         case R_PPC64_GOT_TLSLD16_HA:
15201         case R_PPC64_GOT_TPREL16_HA:
15202         case R_PPC64_GOT_DTPREL16_HA:
15203           /* Add 0x10000 if sign bit in 0:15 is set.
15204              Bits 0:15 are not used.  */
15205           addend += 0x8000;
15206           break;
15207
15208         case R_PPC64_ADDR16_DS:
15209         case R_PPC64_ADDR16_LO_DS:
15210         case R_PPC64_GOT16_DS:
15211         case R_PPC64_GOT16_LO_DS:
15212         case R_PPC64_PLT16_LO_DS:
15213         case R_PPC64_SECTOFF_DS:
15214         case R_PPC64_SECTOFF_LO_DS:
15215         case R_PPC64_TOC16_DS:
15216         case R_PPC64_TOC16_LO_DS:
15217         case R_PPC64_PLTGOT16_DS:
15218         case R_PPC64_PLTGOT16_LO_DS:
15219         case R_PPC64_GOT_TPREL16_DS:
15220         case R_PPC64_GOT_TPREL16_LO_DS:
15221         case R_PPC64_GOT_DTPREL16_DS:
15222         case R_PPC64_GOT_DTPREL16_LO_DS:
15223         case R_PPC64_TPREL16_DS:
15224         case R_PPC64_TPREL16_LO_DS:
15225         case R_PPC64_DTPREL16_DS:
15226         case R_PPC64_DTPREL16_LO_DS:
15227           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15228           mask = 3;
15229           /* If this reloc is against an lq, lxv, or stxv insn, then
15230              the value must be a multiple of 16.  This is somewhat of
15231              a hack, but the "correct" way to do this by defining _DQ
15232              forms of all the _DS relocs bloats all reloc switches in
15233              this file.  It doesn't make much sense to use these
15234              relocs in data, so testing the insn should be safe.  */
15235           if ((insn & (0x3f << 26)) == (56u << 26)
15236               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15237             mask = 15;
15238           relocation += addend;
15239           addend = insn & (mask ^ 3);
15240           if ((relocation & mask) != 0)
15241             {
15242               relocation ^= relocation & mask;
15243               info->callbacks->einfo
15244                 /* xgettext:c-format */
15245                 (_("%H: error: %s not a multiple of %u\n"),
15246                  input_bfd, input_section, rel->r_offset,
15247                  ppc64_elf_howto_table[r_type]->name,
15248                  mask + 1);
15249               bfd_set_error (bfd_error_bad_value);
15250               ret = FALSE;
15251               goto copy_reloc;
15252             }
15253           break;
15254         }
15255
15256       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15257          because such sections are not SEC_ALLOC and thus ld.so will
15258          not process them.  */
15259       howto = ppc64_elf_howto_table[(int) r_type];
15260       if (unresolved_reloc
15261           && !((input_section->flags & SEC_DEBUGGING) != 0
15262                && h->elf.def_dynamic)
15263           && _bfd_elf_section_offset (output_bfd, info, input_section,
15264                                       rel->r_offset) != (bfd_vma) -1)
15265         {
15266           info->callbacks->einfo
15267             /* xgettext:c-format */
15268             (_("%H: unresolvable %s against `%T'\n"),
15269              input_bfd, input_section, rel->r_offset,
15270              howto->name,
15271              h->elf.root.root.string);
15272           ret = FALSE;
15273         }
15274
15275       /* 16-bit fields in insns mostly have signed values, but a
15276          few insns have 16-bit unsigned values.  Really, we should
15277          have different reloc types.  */
15278       if (howto->complain_on_overflow != complain_overflow_dont
15279           && howto->dst_mask == 0xffff
15280           && (input_section->flags & SEC_CODE) != 0)
15281         {
15282           enum complain_overflow complain = complain_overflow_signed;
15283
15284           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15285           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15286             complain = complain_overflow_bitfield;
15287           else if (howto->rightshift == 0
15288                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15289                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15290                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15291                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15292                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15293                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15294             complain = complain_overflow_unsigned;
15295           if (howto->complain_on_overflow != complain)
15296             {
15297               alt_howto = *howto;
15298               alt_howto.complain_on_overflow = complain;
15299               howto = &alt_howto;
15300             }
15301         }
15302
15303       if (r_type == R_PPC64_REL16DX_HA)
15304         {
15305           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15306           if (rel->r_offset + 4 > input_section->size)
15307             r = bfd_reloc_outofrange;
15308           else
15309             {
15310               relocation += addend;
15311               relocation -= (rel->r_offset
15312                              + input_section->output_offset
15313                              + input_section->output_section->vma);
15314               relocation = (bfd_signed_vma) relocation >> 16;
15315               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15316               insn &= ~0x1fffc1;
15317               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15318               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15319               r = bfd_reloc_ok;
15320               if (relocation + 0x8000 > 0xffff)
15321                 r = bfd_reloc_overflow;
15322             }
15323         }
15324       else
15325         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15326                                       rel->r_offset, relocation, addend);
15327
15328       if (r != bfd_reloc_ok)
15329         {
15330           char *more_info = NULL;
15331           const char *reloc_name = howto->name;
15332
15333           if (reloc_dest != DEST_NORMAL)
15334             {
15335               more_info = bfd_malloc (strlen (reloc_name) + 8);
15336               if (more_info != NULL)
15337                 {
15338                   strcpy (more_info, reloc_name);
15339                   strcat (more_info, (reloc_dest == DEST_OPD
15340                                       ? " (OPD)" : " (stub)"));
15341                   reloc_name = more_info;
15342                 }
15343             }
15344
15345           if (r == bfd_reloc_overflow)
15346             {
15347               /* On code like "if (foo) foo();" don't report overflow
15348                  on a branch to zero when foo is undefined.  */
15349               if (!warned
15350                   && (reloc_dest == DEST_STUB
15351                       || !(h != NULL
15352                            && (h->elf.root.type == bfd_link_hash_undefweak
15353                                || h->elf.root.type == bfd_link_hash_undefined)
15354                            && is_branch_reloc (r_type))))
15355                 info->callbacks->reloc_overflow (info, &h->elf.root,
15356                                                  sym_name, reloc_name,
15357                                                  orig_rel.r_addend,
15358                                                  input_bfd, input_section,
15359                                                  rel->r_offset);
15360             }
15361           else
15362             {
15363               info->callbacks->einfo
15364                 /* xgettext:c-format */
15365                 (_("%H: %s against `%T': error %d\n"),
15366                  input_bfd, input_section, rel->r_offset,
15367                  reloc_name, sym_name, (int) r);
15368               ret = FALSE;
15369             }
15370           if (more_info != NULL)
15371             free (more_info);
15372         }
15373     copy_reloc:
15374       if (wrel != rel)
15375         *wrel = *rel;
15376     }
15377
15378   if (wrel != rel)
15379     {
15380       Elf_Internal_Shdr *rel_hdr;
15381       size_t deleted = rel - wrel;
15382
15383       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15384       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15385       if (rel_hdr->sh_size == 0)
15386         {
15387           /* It is too late to remove an empty reloc section.  Leave
15388              one NONE reloc.
15389              ??? What is wrong with an empty section???  */
15390           rel_hdr->sh_size = rel_hdr->sh_entsize;
15391           deleted -= 1;
15392         }
15393       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15394       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15395       input_section->reloc_count -= deleted;
15396     }
15397
15398   /* If we're emitting relocations, then shortly after this function
15399      returns, reloc offsets and addends for this section will be
15400      adjusted.  Worse, reloc symbol indices will be for the output
15401      file rather than the input.  Save a copy of the relocs for
15402      opd_entry_value.  */
15403   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15404     {
15405       bfd_size_type amt;
15406       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15407       rel = bfd_alloc (input_bfd, amt);
15408       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15409       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15410       if (rel == NULL)
15411         return FALSE;
15412       memcpy (rel, relocs, amt);
15413     }
15414   return ret;
15415 }
15416
15417 /* Adjust the value of any local symbols in opd sections.  */
15418
15419 static int
15420 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15421                               const char *name ATTRIBUTE_UNUSED,
15422                               Elf_Internal_Sym *elfsym,
15423                               asection *input_sec,
15424                               struct elf_link_hash_entry *h)
15425 {
15426   struct _opd_sec_data *opd;
15427   long adjust;
15428   bfd_vma value;
15429
15430   if (h != NULL)
15431     return 1;
15432
15433   opd = get_opd_info (input_sec);
15434   if (opd == NULL || opd->adjust == NULL)
15435     return 1;
15436
15437   value = elfsym->st_value - input_sec->output_offset;
15438   if (!bfd_link_relocatable (info))
15439     value -= input_sec->output_section->vma;
15440
15441   adjust = opd->adjust[OPD_NDX (value)];
15442   if (adjust == -1)
15443     return 2;
15444
15445   elfsym->st_value += adjust;
15446   return 1;
15447 }
15448
15449 /* Finish up dynamic symbol handling.  We set the contents of various
15450    dynamic sections here.  */
15451
15452 static bfd_boolean
15453 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15454                                  struct bfd_link_info *info,
15455                                  struct elf_link_hash_entry *h,
15456                                  Elf_Internal_Sym *sym)
15457 {
15458   struct ppc_link_hash_table *htab;
15459   struct plt_entry *ent;
15460   Elf_Internal_Rela rela;
15461   bfd_byte *loc;
15462
15463   htab = ppc_hash_table (info);
15464   if (htab == NULL)
15465     return FALSE;
15466
15467   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15468     if (ent->plt.offset != (bfd_vma) -1)
15469       {
15470         /* This symbol has an entry in the procedure linkage
15471            table.  Set it up.  */
15472         if (!htab->elf.dynamic_sections_created
15473             || h->dynindx == -1)
15474           {
15475             BFD_ASSERT (h->type == STT_GNU_IFUNC
15476                         && h->def_regular
15477                         && (h->root.type == bfd_link_hash_defined
15478                             || h->root.type == bfd_link_hash_defweak));
15479             rela.r_offset = (htab->elf.iplt->output_section->vma
15480                              + htab->elf.iplt->output_offset
15481                              + ent->plt.offset);
15482             if (htab->opd_abi)
15483               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15484             else
15485               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15486             rela.r_addend = (h->root.u.def.value
15487                              + h->root.u.def.section->output_offset
15488                              + h->root.u.def.section->output_section->vma
15489                              + ent->addend);
15490             loc = (htab->elf.irelplt->contents
15491                    + (htab->elf.irelplt->reloc_count++
15492                       * sizeof (Elf64_External_Rela)));
15493             htab->local_ifunc_resolver = 1;
15494           }
15495         else
15496           {
15497             rela.r_offset = (htab->elf.splt->output_section->vma
15498                              + htab->elf.splt->output_offset
15499                              + ent->plt.offset);
15500             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15501             rela.r_addend = ent->addend;
15502             loc = (htab->elf.srelplt->contents
15503                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15504                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15505             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15506               htab->maybe_local_ifunc_resolver = 1;
15507           }
15508         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15509
15510         if (!htab->opd_abi)
15511           {
15512             if (!h->def_regular)
15513               {
15514                 /* Mark the symbol as undefined, rather than as
15515                    defined in glink.  Leave the value if there were
15516                    any relocations where pointer equality matters
15517                    (this is a clue for the dynamic linker, to make
15518                    function pointer comparisons work between an
15519                    application and shared library), otherwise set it
15520                    to zero.  */
15521                 sym->st_shndx = SHN_UNDEF;
15522                 if (!h->pointer_equality_needed)
15523                   sym->st_value = 0;
15524                 else if (!h->ref_regular_nonweak)
15525                   {
15526                     /* This breaks function pointer comparisons, but
15527                        that is better than breaking tests for a NULL
15528                        function pointer.  */
15529                     sym->st_value = 0;
15530                   }
15531               }
15532           }
15533       }
15534
15535   if (h->needs_copy)
15536     {
15537       /* This symbol needs a copy reloc.  Set it up.  */
15538       asection *srel;
15539
15540       if (h->dynindx == -1
15541           || (h->root.type != bfd_link_hash_defined
15542               && h->root.type != bfd_link_hash_defweak)
15543           || htab->elf.srelbss == NULL
15544           || htab->elf.sreldynrelro == NULL)
15545         abort ();
15546
15547       rela.r_offset = (h->root.u.def.value
15548                        + h->root.u.def.section->output_section->vma
15549                        + h->root.u.def.section->output_offset);
15550       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15551       rela.r_addend = 0;
15552       if (h->root.u.def.section == htab->elf.sdynrelro)
15553         srel = htab->elf.sreldynrelro;
15554       else
15555         srel = htab->elf.srelbss;
15556       loc = srel->contents;
15557       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15558       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15559     }
15560
15561   return TRUE;
15562 }
15563
15564 /* Used to decide how to sort relocs in an optimal manner for the
15565    dynamic linker, before writing them out.  */
15566
15567 static enum elf_reloc_type_class
15568 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15569                             const asection *rel_sec,
15570                             const Elf_Internal_Rela *rela)
15571 {
15572   enum elf_ppc64_reloc_type r_type;
15573   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15574
15575   if (rel_sec == htab->elf.irelplt)
15576     return reloc_class_ifunc;
15577
15578   r_type = ELF64_R_TYPE (rela->r_info);
15579   switch (r_type)
15580     {
15581     case R_PPC64_RELATIVE:
15582       return reloc_class_relative;
15583     case R_PPC64_JMP_SLOT:
15584       return reloc_class_plt;
15585     case R_PPC64_COPY:
15586       return reloc_class_copy;
15587     default:
15588       return reloc_class_normal;
15589     }
15590 }
15591
15592 /* Finish up the dynamic sections.  */
15593
15594 static bfd_boolean
15595 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15596                                    struct bfd_link_info *info)
15597 {
15598   struct ppc_link_hash_table *htab;
15599   bfd *dynobj;
15600   asection *sdyn;
15601
15602   htab = ppc_hash_table (info);
15603   if (htab == NULL)
15604     return FALSE;
15605
15606   dynobj = htab->elf.dynobj;
15607   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15608
15609   if (htab->elf.dynamic_sections_created)
15610     {
15611       Elf64_External_Dyn *dyncon, *dynconend;
15612
15613       if (sdyn == NULL || htab->elf.sgot == NULL)
15614         abort ();
15615
15616       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15617       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15618       for (; dyncon < dynconend; dyncon++)
15619         {
15620           Elf_Internal_Dyn dyn;
15621           asection *s;
15622
15623           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15624
15625           switch (dyn.d_tag)
15626             {
15627             default:
15628               continue;
15629
15630             case DT_PPC64_GLINK:
15631               s = htab->glink;
15632               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15633               /* We stupidly defined DT_PPC64_GLINK to be the start
15634                  of glink rather than the first entry point, which is
15635                  what ld.so needs, and now have a bigger stub to
15636                  support automatic multiple TOCs.  */
15637               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15638               break;
15639
15640             case DT_PPC64_OPD:
15641               s = bfd_get_section_by_name (output_bfd, ".opd");
15642               if (s == NULL)
15643                 continue;
15644               dyn.d_un.d_ptr = s->vma;
15645               break;
15646
15647             case DT_PPC64_OPT:
15648               if (htab->do_multi_toc && htab->multi_toc_needed)
15649                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15650               if (htab->has_plt_localentry0)
15651                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15652               break;
15653
15654             case DT_PPC64_OPDSZ:
15655               s = bfd_get_section_by_name (output_bfd, ".opd");
15656               if (s == NULL)
15657                 continue;
15658               dyn.d_un.d_val = s->size;
15659               break;
15660
15661             case DT_PLTGOT:
15662               s = htab->elf.splt;
15663               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15664               break;
15665
15666             case DT_JMPREL:
15667               s = htab->elf.srelplt;
15668               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15669               break;
15670
15671             case DT_PLTRELSZ:
15672               dyn.d_un.d_val = htab->elf.srelplt->size;
15673               break;
15674
15675             case DT_TEXTREL:
15676               if (htab->local_ifunc_resolver)
15677                 info->callbacks->einfo
15678                   (_("%X%P: text relocations and GNU indirect "
15679                      "functions will result in a segfault at runtime\n"));
15680               else if (htab->maybe_local_ifunc_resolver)
15681                 info->callbacks->einfo
15682                   (_("%P: warning: text relocations and GNU indirect "
15683                      "functions may result in a segfault at runtime\n"));
15684               continue;
15685             }
15686
15687           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15688         }
15689     }
15690
15691   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15692       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15693     {
15694       /* Fill in the first entry in the global offset table.
15695          We use it to hold the link-time TOCbase.  */
15696       bfd_put_64 (output_bfd,
15697                   elf_gp (output_bfd) + TOC_BASE_OFF,
15698                   htab->elf.sgot->contents);
15699
15700       /* Set .got entry size.  */
15701       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15702     }
15703
15704   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15705       && htab->elf.splt->output_section != bfd_abs_section_ptr)
15706     {
15707       /* Set .plt entry size.  */
15708       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15709         = PLT_ENTRY_SIZE (htab);
15710     }
15711
15712   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15713      brlt ourselves if emitrelocations.  */
15714   if (htab->brlt != NULL
15715       && htab->brlt->reloc_count != 0
15716       && !_bfd_elf_link_output_relocs (output_bfd,
15717                                        htab->brlt,
15718                                        elf_section_data (htab->brlt)->rela.hdr,
15719                                        elf_section_data (htab->brlt)->relocs,
15720                                        NULL))
15721     return FALSE;
15722
15723   if (htab->glink != NULL
15724       && htab->glink->reloc_count != 0
15725       && !_bfd_elf_link_output_relocs (output_bfd,
15726                                        htab->glink,
15727                                        elf_section_data (htab->glink)->rela.hdr,
15728                                        elf_section_data (htab->glink)->relocs,
15729                                        NULL))
15730     return FALSE;
15731
15732   if (htab->glink_eh_frame != NULL
15733       && htab->glink_eh_frame->size != 0)
15734     {
15735       bfd_vma val;
15736       bfd_byte *p;
15737       struct map_stub *group;
15738       size_t align = 4;
15739
15740       p = htab->glink_eh_frame->contents;
15741       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15742
15743       for (group = htab->group; group != NULL; group = group->next)
15744         if (group->stub_sec != NULL)
15745           {
15746             /* Offset to stub section.  */
15747             val = (group->stub_sec->output_section->vma
15748                    + group->stub_sec->output_offset);
15749             val -= (htab->glink_eh_frame->output_section->vma
15750                     + htab->glink_eh_frame->output_offset
15751                     + (p + 8 - htab->glink_eh_frame->contents));
15752             if (val + 0x80000000 > 0xffffffff)
15753               {
15754                 info->callbacks->einfo
15755                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15756                    group->stub_sec->name);
15757                 return FALSE;
15758               }
15759             bfd_put_32 (dynobj, val, p + 8);
15760             p += stub_eh_frame_size (group, align);
15761           }
15762       if (htab->glink != NULL && htab->glink->size != 0)
15763         {
15764           /* Offset to .glink.  */
15765           val = (htab->glink->output_section->vma
15766                  + htab->glink->output_offset
15767                  + 8);
15768           val -= (htab->glink_eh_frame->output_section->vma
15769                   + htab->glink_eh_frame->output_offset
15770                   + (p + 8 - htab->glink_eh_frame->contents));
15771           if (val + 0x80000000 > 0xffffffff)
15772             {
15773               info->callbacks->einfo
15774                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15775                  htab->glink->name);
15776               return FALSE;
15777             }
15778           bfd_put_32 (dynobj, val, p + 8);
15779           p += (24 + align - 1) & -align;
15780         }
15781
15782       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15783           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15784                                                htab->glink_eh_frame,
15785                                                htab->glink_eh_frame->contents))
15786         return FALSE;
15787     }
15788
15789   /* We need to handle writing out multiple GOT sections ourselves,
15790      since we didn't add them to DYNOBJ.  We know dynobj is the first
15791      bfd.  */
15792   while ((dynobj = dynobj->link.next) != NULL)
15793     {
15794       asection *s;
15795
15796       if (!is_ppc64_elf (dynobj))
15797         continue;
15798
15799       s = ppc64_elf_tdata (dynobj)->got;
15800       if (s != NULL
15801           && s->size != 0
15802           && s->output_section != bfd_abs_section_ptr
15803           && !bfd_set_section_contents (output_bfd, s->output_section,
15804                                         s->contents, s->output_offset,
15805                                         s->size))
15806         return FALSE;
15807       s = ppc64_elf_tdata (dynobj)->relgot;
15808       if (s != NULL
15809           && s->size != 0
15810           && s->output_section != bfd_abs_section_ptr
15811           && !bfd_set_section_contents (output_bfd, s->output_section,
15812                                         s->contents, s->output_offset,
15813                                         s->size))
15814         return FALSE;
15815     }
15816
15817   return TRUE;
15818 }
15819
15820 #include "elf64-target.h"
15821
15822 /* FreeBSD support */
15823
15824 #undef  TARGET_LITTLE_SYM
15825 #undef  TARGET_LITTLE_NAME
15826
15827 #undef  TARGET_BIG_SYM
15828 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15829 #undef  TARGET_BIG_NAME
15830 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15831
15832 #undef  ELF_OSABI
15833 #define ELF_OSABI       ELFOSABI_FREEBSD
15834
15835 #undef  elf64_bed
15836 #define elf64_bed       elf64_powerpc_fbsd_bed
15837
15838 #include "elf64-target.h"