PowerPC indirect calls to __tls_get_addr
[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 ADDI_R11_R11    0x396b0000      /* addi %r11,%r11,off@l  */
165 #define ADDIS_R2_R2     0x3c420000      /* addis %r2,%r2,off@ha  */
166 #define ADDI_R2_R2      0x38420000      /* addi  %r2,%r2,off@l   */
167
168 #define XOR_R2_R12_R12  0x7d826278      /* xor   %r2,%r12,%r12   */
169 #define ADD_R11_R11_R2  0x7d6b1214      /* add   %r11,%r11,%r2   */
170 #define XOR_R11_R12_R12 0x7d8b6278      /* xor   %r11,%r12,%r12  */
171 #define ADD_R2_R2_R11   0x7c425a14      /* add   %r2,%r2,%r11    */
172 #define CMPLDI_R2_0     0x28220000      /* cmpldi %r2,0          */
173 #define BNECTR          0x4ca20420      /* bnectr+               */
174 #define BNECTR_P4       0x4ce20420      /* bnectr+               */
175
176 #define LD_R12_0R2      0xe9820000      /* ld    %r12,xxx+0(%r2) */
177 #define LD_R11_0R2      0xe9620000      /* ld    %r11,xxx+0(%r2) */
178 #define LD_R2_0R2       0xe8420000      /* ld    %r2,xxx+0(%r2)  */
179
180 #define LD_R2_0R1       0xe8410000      /* ld    %r2,0(%r1)      */
181 #define LD_R2_0R12      0xe84c0000      /* ld    %r2,0(%r12)     */
182 #define ADD_R2_R2_R12   0x7c426214      /* add   %r2,%r2,%r12    */
183
184 #define LIS_R2          0x3c400000      /* lis %r2,xxx@ha         */
185 #define ADDIS_R2_R12    0x3c4c0000      /* addis %r2,%r12,xxx@ha  */
186 #define ADDIS_R12_R2    0x3d820000      /* addis %r12,%r2,xxx@ha  */
187 #define ADDIS_R12_R12   0x3d8c0000      /* addis %r12,%r12,xxx@ha */
188 #define LD_R12_0R12     0xe98c0000      /* ld    %r12,xxx@l(%r12) */
189
190 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
191 #define GLINK_PLTRESOLVE_SIZE(htab)                     \
192   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
193                                         /* 0:                           */
194                                         /*  .quad plt0-1f               */
195                                         /* __glink:                     */
196 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
197 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
198                                         /* 1:                           */
199 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
200                                         /*  ld %2,(0b-1b)(%11)          */
201 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
202 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
203                                         /*  ld %12,0(%11)               */
204                                         /*  ld %2,8(%11)                */
205                                         /*  mtctr %12                   */
206                                         /*  ld %11,16(%11)              */
207                                         /*  bctr                        */
208 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
209 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
210 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
211 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
212 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
213
214 /* Pad with this.  */
215 #define NOP             0x60000000
216
217 /* Some other nops.  */
218 #define CROR_151515     0x4def7b82
219 #define CROR_313131     0x4ffffb82
220
221 /* .glink entries for the first 32k functions are two instructions.  */
222 #define LI_R0_0         0x38000000      /* li    %r0,0          */
223 #define B_DOT           0x48000000      /* b     .              */
224
225 /* After that, we need two instructions to load the index, followed by
226    a branch.  */
227 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
228 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
229
230 /* Instructions used by the save and restore reg functions.  */
231 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
232 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
233 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
234 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
235 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
236 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
237 #define LI_R12_0        0x39800000      /* li    %r12,0         */
238 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
239 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
240 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
241 #define BLR             0x4e800020      /* blr                  */
242
243 /* Since .opd is an array of descriptors and each entry will end up
244    with identical R_PPC64_RELATIVE relocs, there is really no need to
245    propagate .opd relocs;  The dynamic linker should be taught to
246    relocate .opd without reloc entries.  */
247 #ifndef NO_OPD_RELOCS
248 #define NO_OPD_RELOCS 0
249 #endif
250
251 #ifndef ARRAY_SIZE
252 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
253 #endif
254
255 static inline int
256 abiversion (bfd *abfd)
257 {
258   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
259 }
260
261 static inline void
262 set_abiversion (bfd *abfd, int ver)
263 {
264   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
265   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
266 }
267 \f
268 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
269
270 /* Relocation HOWTO's.  */
271 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
272
273 static reloc_howto_type ppc64_elf_howto_raw[] =
274 {
275   /* This reloc does nothing.  */
276   HOWTO (R_PPC64_NONE,          /* type */
277          0,                     /* rightshift */
278          3,                     /* size (0 = byte, 1 = short, 2 = long) */
279          0,                     /* bitsize */
280          FALSE,                 /* pc_relative */
281          0,                     /* bitpos */
282          complain_overflow_dont, /* complain_on_overflow */
283          bfd_elf_generic_reloc, /* special_function */
284          "R_PPC64_NONE",        /* name */
285          FALSE,                 /* partial_inplace */
286          0,                     /* src_mask */
287          0,                     /* dst_mask */
288          FALSE),                /* pcrel_offset */
289
290   /* A standard 32 bit relocation.  */
291   HOWTO (R_PPC64_ADDR32,        /* type */
292          0,                     /* rightshift */
293          2,                     /* size (0 = byte, 1 = short, 2 = long) */
294          32,                    /* bitsize */
295          FALSE,                 /* pc_relative */
296          0,                     /* bitpos */
297          complain_overflow_bitfield, /* complain_on_overflow */
298          bfd_elf_generic_reloc, /* special_function */
299          "R_PPC64_ADDR32",      /* name */
300          FALSE,                 /* partial_inplace */
301          0,                     /* src_mask */
302          0xffffffff,            /* dst_mask */
303          FALSE),                /* pcrel_offset */
304
305   /* An absolute 26 bit branch; the lower two bits must be zero.
306      FIXME: we don't check that, we just clear them.  */
307   HOWTO (R_PPC64_ADDR24,        /* type */
308          0,                     /* rightshift */
309          2,                     /* size (0 = byte, 1 = short, 2 = long) */
310          26,                    /* bitsize */
311          FALSE,                 /* pc_relative */
312          0,                     /* bitpos */
313          complain_overflow_bitfield, /* complain_on_overflow */
314          bfd_elf_generic_reloc, /* special_function */
315          "R_PPC64_ADDR24",      /* name */
316          FALSE,                 /* partial_inplace */
317          0,                     /* src_mask */
318          0x03fffffc,            /* dst_mask */
319          FALSE),                /* pcrel_offset */
320
321   /* A standard 16 bit relocation.  */
322   HOWTO (R_PPC64_ADDR16,        /* type */
323          0,                     /* rightshift */
324          1,                     /* size (0 = byte, 1 = short, 2 = long) */
325          16,                    /* bitsize */
326          FALSE,                 /* pc_relative */
327          0,                     /* bitpos */
328          complain_overflow_bitfield, /* complain_on_overflow */
329          bfd_elf_generic_reloc, /* special_function */
330          "R_PPC64_ADDR16",      /* name */
331          FALSE,                 /* partial_inplace */
332          0,                     /* src_mask */
333          0xffff,                /* dst_mask */
334          FALSE),                /* pcrel_offset */
335
336   /* A 16 bit relocation without overflow.  */
337   HOWTO (R_PPC64_ADDR16_LO,     /* type */
338          0,                     /* rightshift */
339          1,                     /* size (0 = byte, 1 = short, 2 = long) */
340          16,                    /* bitsize */
341          FALSE,                 /* pc_relative */
342          0,                     /* bitpos */
343          complain_overflow_dont,/* complain_on_overflow */
344          bfd_elf_generic_reloc, /* special_function */
345          "R_PPC64_ADDR16_LO",   /* name */
346          FALSE,                 /* partial_inplace */
347          0,                     /* src_mask */
348          0xffff,                /* dst_mask */
349          FALSE),                /* pcrel_offset */
350
351   /* Bits 16-31 of an address.  */
352   HOWTO (R_PPC64_ADDR16_HI,     /* type */
353          16,                    /* rightshift */
354          1,                     /* size (0 = byte, 1 = short, 2 = long) */
355          16,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_signed, /* complain_on_overflow */
359          bfd_elf_generic_reloc, /* special_function */
360          "R_PPC64_ADDR16_HI",   /* name */
361          FALSE,                 /* partial_inplace */
362          0,                     /* src_mask */
363          0xffff,                /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
367      bits, treated as a signed number, is negative.  */
368   HOWTO (R_PPC64_ADDR16_HA,     /* type */
369          16,                    /* rightshift */
370          1,                     /* size (0 = byte, 1 = short, 2 = long) */
371          16,                    /* bitsize */
372          FALSE,                 /* pc_relative */
373          0,                     /* bitpos */
374          complain_overflow_signed, /* complain_on_overflow */
375          ppc64_elf_ha_reloc,    /* special_function */
376          "R_PPC64_ADDR16_HA",   /* name */
377          FALSE,                 /* partial_inplace */
378          0,                     /* src_mask */
379          0xffff,                /* dst_mask */
380          FALSE),                /* pcrel_offset */
381
382   /* An absolute 16 bit branch; the lower two bits must be zero.
383      FIXME: we don't check that, we just clear them.  */
384   HOWTO (R_PPC64_ADDR14,        /* type */
385          0,                     /* rightshift */
386          2,                     /* size (0 = byte, 1 = short, 2 = long) */
387          16,                    /* bitsize */
388          FALSE,                 /* pc_relative */
389          0,                     /* bitpos */
390          complain_overflow_signed, /* complain_on_overflow */
391          ppc64_elf_branch_reloc, /* special_function */
392          "R_PPC64_ADDR14",      /* name */
393          FALSE,                 /* partial_inplace */
394          0,                     /* src_mask */
395          0x0000fffc,            /* dst_mask */
396          FALSE),                /* pcrel_offset */
397
398   /* An absolute 16 bit branch, for which bit 10 should be set to
399      indicate that the branch is expected to be taken.  The lower two
400      bits must be zero.  */
401   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
402          0,                     /* rightshift */
403          2,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          FALSE,                 /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_signed, /* complain_on_overflow */
408          ppc64_elf_brtaken_reloc, /* special_function */
409          "R_PPC64_ADDR14_BRTAKEN",/* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0x0000fffc,            /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   /* An absolute 16 bit branch, for which bit 10 should be set to
416      indicate that the branch is not expected to be taken.  The lower
417      two bits must be zero.  */
418   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
419          0,                     /* rightshift */
420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
421          16,                    /* bitsize */
422          FALSE,                 /* pc_relative */
423          0,                     /* bitpos */
424          complain_overflow_signed, /* complain_on_overflow */
425          ppc64_elf_brtaken_reloc, /* special_function */
426          "R_PPC64_ADDR14_BRNTAKEN",/* name */
427          FALSE,                 /* partial_inplace */
428          0,                     /* src_mask */
429          0x0000fffc,            /* dst_mask */
430          FALSE),                /* pcrel_offset */
431
432   /* A relative 26 bit branch; the lower two bits must be zero.  */
433   HOWTO (R_PPC64_REL24,         /* type */
434          0,                     /* rightshift */
435          2,                     /* size (0 = byte, 1 = short, 2 = long) */
436          26,                    /* bitsize */
437          TRUE,                  /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_signed, /* complain_on_overflow */
440          ppc64_elf_branch_reloc, /* special_function */
441          "R_PPC64_REL24",       /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0x03fffffc,            /* dst_mask */
445          TRUE),                 /* pcrel_offset */
446
447   /* A relative 16 bit branch; the lower two bits must be zero.  */
448   HOWTO (R_PPC64_REL14,         /* type */
449          0,                     /* rightshift */
450          2,                     /* size (0 = byte, 1 = short, 2 = long) */
451          16,                    /* bitsize */
452          TRUE,                  /* pc_relative */
453          0,                     /* bitpos */
454          complain_overflow_signed, /* complain_on_overflow */
455          ppc64_elf_branch_reloc, /* special_function */
456          "R_PPC64_REL14",       /* name */
457          FALSE,                 /* partial_inplace */
458          0,                     /* src_mask */
459          0x0000fffc,            /* dst_mask */
460          TRUE),                 /* pcrel_offset */
461
462   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
463      the branch is expected to be taken.  The lower two bits must be
464      zero.  */
465   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
466          0,                     /* rightshift */
467          2,                     /* size (0 = byte, 1 = short, 2 = long) */
468          16,                    /* bitsize */
469          TRUE,                  /* pc_relative */
470          0,                     /* bitpos */
471          complain_overflow_signed, /* complain_on_overflow */
472          ppc64_elf_brtaken_reloc, /* special_function */
473          "R_PPC64_REL14_BRTAKEN", /* name */
474          FALSE,                 /* partial_inplace */
475          0,                     /* src_mask */
476          0x0000fffc,            /* dst_mask */
477          TRUE),                 /* pcrel_offset */
478
479   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
480      the branch is not expected to be taken.  The lower two bits must
481      be zero.  */
482   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
483          0,                     /* rightshift */
484          2,                     /* size (0 = byte, 1 = short, 2 = long) */
485          16,                    /* bitsize */
486          TRUE,                  /* pc_relative */
487          0,                     /* bitpos */
488          complain_overflow_signed, /* complain_on_overflow */
489          ppc64_elf_brtaken_reloc, /* special_function */
490          "R_PPC64_REL14_BRNTAKEN",/* name */
491          FALSE,                 /* partial_inplace */
492          0,                     /* src_mask */
493          0x0000fffc,            /* dst_mask */
494          TRUE),                 /* pcrel_offset */
495
496   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
497      symbol.  */
498   HOWTO (R_PPC64_GOT16,         /* type */
499          0,                     /* rightshift */
500          1,                     /* size (0 = byte, 1 = short, 2 = long) */
501          16,                    /* bitsize */
502          FALSE,                 /* pc_relative */
503          0,                     /* bitpos */
504          complain_overflow_signed, /* complain_on_overflow */
505          ppc64_elf_unhandled_reloc, /* special_function */
506          "R_PPC64_GOT16",       /* name */
507          FALSE,                 /* partial_inplace */
508          0,                     /* src_mask */
509          0xffff,                /* dst_mask */
510          FALSE),                /* pcrel_offset */
511
512   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
513      the symbol.  */
514   HOWTO (R_PPC64_GOT16_LO,      /* type */
515          0,                     /* rightshift */
516          1,                     /* size (0 = byte, 1 = short, 2 = long) */
517          16,                    /* bitsize */
518          FALSE,                 /* pc_relative */
519          0,                     /* bitpos */
520          complain_overflow_dont, /* complain_on_overflow */
521          ppc64_elf_unhandled_reloc, /* special_function */
522          "R_PPC64_GOT16_LO",    /* name */
523          FALSE,                 /* partial_inplace */
524          0,                     /* src_mask */
525          0xffff,                /* dst_mask */
526          FALSE),                /* pcrel_offset */
527
528   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
529      the symbol.  */
530   HOWTO (R_PPC64_GOT16_HI,      /* type */
531          16,                    /* rightshift */
532          1,                     /* size (0 = byte, 1 = short, 2 = long) */
533          16,                    /* bitsize */
534          FALSE,                 /* pc_relative */
535          0,                     /* bitpos */
536          complain_overflow_signed,/* complain_on_overflow */
537          ppc64_elf_unhandled_reloc, /* special_function */
538          "R_PPC64_GOT16_HI",    /* name */
539          FALSE,                 /* partial_inplace */
540          0,                     /* src_mask */
541          0xffff,                /* dst_mask */
542          FALSE),                /* pcrel_offset */
543
544   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
545      the symbol.  */
546   HOWTO (R_PPC64_GOT16_HA,      /* type */
547          16,                    /* rightshift */
548          1,                     /* size (0 = byte, 1 = short, 2 = long) */
549          16,                    /* bitsize */
550          FALSE,                 /* pc_relative */
551          0,                     /* bitpos */
552          complain_overflow_signed,/* complain_on_overflow */
553          ppc64_elf_unhandled_reloc, /* special_function */
554          "R_PPC64_GOT16_HA",    /* name */
555          FALSE,                 /* partial_inplace */
556          0,                     /* src_mask */
557          0xffff,                /* dst_mask */
558          FALSE),                /* pcrel_offset */
559
560   /* This is used only by the dynamic linker.  The symbol should exist
561      both in the object being run and in some shared library.  The
562      dynamic linker copies the data addressed by the symbol from the
563      shared library into the object, because the object being
564      run has to have the data at some particular address.  */
565   HOWTO (R_PPC64_COPY,          /* type */
566          0,                     /* rightshift */
567          0,                     /* this one is variable size */
568          0,                     /* bitsize */
569          FALSE,                 /* pc_relative */
570          0,                     /* bitpos */
571          complain_overflow_dont, /* complain_on_overflow */
572          ppc64_elf_unhandled_reloc, /* special_function */
573          "R_PPC64_COPY",        /* name */
574          FALSE,                 /* partial_inplace */
575          0,                     /* src_mask */
576          0,                     /* dst_mask */
577          FALSE),                /* pcrel_offset */
578
579   /* Like R_PPC64_ADDR64, but used when setting global offset table
580      entries.  */
581   HOWTO (R_PPC64_GLOB_DAT,      /* type */
582          0,                     /* rightshift */
583          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
584          64,                    /* bitsize */
585          FALSE,                 /* pc_relative */
586          0,                     /* bitpos */
587          complain_overflow_dont, /* complain_on_overflow */
588          ppc64_elf_unhandled_reloc,  /* special_function */
589          "R_PPC64_GLOB_DAT",    /* name */
590          FALSE,                 /* partial_inplace */
591          0,                     /* src_mask */
592          ONES (64),             /* dst_mask */
593          FALSE),                /* pcrel_offset */
594
595   /* Created by the link editor.  Marks a procedure linkage table
596      entry for a symbol.  */
597   HOWTO (R_PPC64_JMP_SLOT,      /* type */
598          0,                     /* rightshift */
599          0,                     /* size (0 = byte, 1 = short, 2 = long) */
600          0,                     /* bitsize */
601          FALSE,                 /* pc_relative */
602          0,                     /* bitpos */
603          complain_overflow_dont, /* complain_on_overflow */
604          ppc64_elf_unhandled_reloc, /* special_function */
605          "R_PPC64_JMP_SLOT",    /* name */
606          FALSE,                 /* partial_inplace */
607          0,                     /* src_mask */
608          0,                     /* dst_mask */
609          FALSE),                /* pcrel_offset */
610
611   /* Used only by the dynamic linker.  When the object is run, this
612      doubleword64 is set to the load address of the object, plus the
613      addend.  */
614   HOWTO (R_PPC64_RELATIVE,      /* type */
615          0,                     /* rightshift */
616          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
617          64,                    /* bitsize */
618          FALSE,                 /* pc_relative */
619          0,                     /* bitpos */
620          complain_overflow_dont, /* complain_on_overflow */
621          bfd_elf_generic_reloc, /* special_function */
622          "R_PPC64_RELATIVE",    /* name */
623          FALSE,                 /* partial_inplace */
624          0,                     /* src_mask */
625          ONES (64),             /* dst_mask */
626          FALSE),                /* pcrel_offset */
627
628   /* Like R_PPC64_ADDR32, but may be unaligned.  */
629   HOWTO (R_PPC64_UADDR32,       /* type */
630          0,                     /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          32,                    /* bitsize */
633          FALSE,                 /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_bitfield, /* complain_on_overflow */
636          bfd_elf_generic_reloc, /* special_function */
637          "R_PPC64_UADDR32",     /* name */
638          FALSE,                 /* partial_inplace */
639          0,                     /* src_mask */
640          0xffffffff,            /* dst_mask */
641          FALSE),                /* pcrel_offset */
642
643   /* Like R_PPC64_ADDR16, but may be unaligned.  */
644   HOWTO (R_PPC64_UADDR16,       /* type */
645          0,                     /* rightshift */
646          1,                     /* size (0 = byte, 1 = short, 2 = long) */
647          16,                    /* bitsize */
648          FALSE,                 /* pc_relative */
649          0,                     /* bitpos */
650          complain_overflow_bitfield, /* complain_on_overflow */
651          bfd_elf_generic_reloc, /* special_function */
652          "R_PPC64_UADDR16",     /* name */
653          FALSE,                 /* partial_inplace */
654          0,                     /* src_mask */
655          0xffff,                /* dst_mask */
656          FALSE),                /* pcrel_offset */
657
658   /* 32-bit PC relative.  */
659   HOWTO (R_PPC64_REL32,         /* type */
660          0,                     /* rightshift */
661          2,                     /* size (0 = byte, 1 = short, 2 = long) */
662          32,                    /* bitsize */
663          TRUE,                  /* pc_relative */
664          0,                     /* bitpos */
665          complain_overflow_signed, /* complain_on_overflow */
666          bfd_elf_generic_reloc, /* special_function */
667          "R_PPC64_REL32",       /* name */
668          FALSE,                 /* partial_inplace */
669          0,                     /* src_mask */
670          0xffffffff,            /* dst_mask */
671          TRUE),                 /* pcrel_offset */
672
673   /* 32-bit relocation to the symbol's procedure linkage table.  */
674   HOWTO (R_PPC64_PLT32,         /* type */
675          0,                     /* rightshift */
676          2,                     /* size (0 = byte, 1 = short, 2 = long) */
677          32,                    /* bitsize */
678          FALSE,                 /* pc_relative */
679          0,                     /* bitpos */
680          complain_overflow_bitfield, /* complain_on_overflow */
681          ppc64_elf_unhandled_reloc, /* special_function */
682          "R_PPC64_PLT32",       /* name */
683          FALSE,                 /* partial_inplace */
684          0,                     /* src_mask */
685          0xffffffff,            /* dst_mask */
686          FALSE),                /* pcrel_offset */
687
688   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
689      FIXME: R_PPC64_PLTREL32 not supported.  */
690   HOWTO (R_PPC64_PLTREL32,      /* type */
691          0,                     /* rightshift */
692          2,                     /* size (0 = byte, 1 = short, 2 = long) */
693          32,                    /* bitsize */
694          TRUE,                  /* pc_relative */
695          0,                     /* bitpos */
696          complain_overflow_signed, /* complain_on_overflow */
697          ppc64_elf_unhandled_reloc, /* special_function */
698          "R_PPC64_PLTREL32",    /* name */
699          FALSE,                 /* partial_inplace */
700          0,                     /* src_mask */
701          0xffffffff,            /* dst_mask */
702          TRUE),                 /* pcrel_offset */
703
704   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
705      the symbol.  */
706   HOWTO (R_PPC64_PLT16_LO,      /* type */
707          0,                     /* rightshift */
708          1,                     /* size (0 = byte, 1 = short, 2 = long) */
709          16,                    /* bitsize */
710          FALSE,                 /* pc_relative */
711          0,                     /* bitpos */
712          complain_overflow_dont, /* complain_on_overflow */
713          ppc64_elf_unhandled_reloc, /* special_function */
714          "R_PPC64_PLT16_LO",    /* name */
715          FALSE,                 /* partial_inplace */
716          0,                     /* src_mask */
717          0xffff,                /* dst_mask */
718          FALSE),                /* pcrel_offset */
719
720   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
721      the symbol.  */
722   HOWTO (R_PPC64_PLT16_HI,      /* type */
723          16,                    /* rightshift */
724          1,                     /* size (0 = byte, 1 = short, 2 = long) */
725          16,                    /* bitsize */
726          FALSE,                 /* pc_relative */
727          0,                     /* bitpos */
728          complain_overflow_signed, /* complain_on_overflow */
729          ppc64_elf_unhandled_reloc, /* special_function */
730          "R_PPC64_PLT16_HI",    /* name */
731          FALSE,                 /* partial_inplace */
732          0,                     /* src_mask */
733          0xffff,                /* dst_mask */
734          FALSE),                /* pcrel_offset */
735
736   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
737      the symbol.  */
738   HOWTO (R_PPC64_PLT16_HA,      /* type */
739          16,                    /* rightshift */
740          1,                     /* size (0 = byte, 1 = short, 2 = long) */
741          16,                    /* bitsize */
742          FALSE,                 /* pc_relative */
743          0,                     /* bitpos */
744          complain_overflow_signed, /* complain_on_overflow */
745          ppc64_elf_unhandled_reloc, /* special_function */
746          "R_PPC64_PLT16_HA",    /* name */
747          FALSE,                 /* partial_inplace */
748          0,                     /* src_mask */
749          0xffff,                /* dst_mask */
750          FALSE),                /* pcrel_offset */
751
752   /* 16-bit section relative relocation.  */
753   HOWTO (R_PPC64_SECTOFF,       /* type */
754          0,                     /* rightshift */
755          1,                     /* size (0 = byte, 1 = short, 2 = long) */
756          16,                    /* bitsize */
757          FALSE,                 /* pc_relative */
758          0,                     /* bitpos */
759          complain_overflow_signed, /* complain_on_overflow */
760          ppc64_elf_sectoff_reloc, /* special_function */
761          "R_PPC64_SECTOFF",     /* name */
762          FALSE,                 /* partial_inplace */
763          0,                     /* src_mask */
764          0xffff,                /* dst_mask */
765          FALSE),                /* pcrel_offset */
766
767   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
768   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
769          0,                     /* rightshift */
770          1,                     /* size (0 = byte, 1 = short, 2 = long) */
771          16,                    /* bitsize */
772          FALSE,                 /* pc_relative */
773          0,                     /* bitpos */
774          complain_overflow_dont, /* complain_on_overflow */
775          ppc64_elf_sectoff_reloc, /* special_function */
776          "R_PPC64_SECTOFF_LO",  /* name */
777          FALSE,                 /* partial_inplace */
778          0,                     /* src_mask */
779          0xffff,                /* dst_mask */
780          FALSE),                /* pcrel_offset */
781
782   /* 16-bit upper half section relative relocation.  */
783   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
784          16,                    /* rightshift */
785          1,                     /* size (0 = byte, 1 = short, 2 = long) */
786          16,                    /* bitsize */
787          FALSE,                 /* pc_relative */
788          0,                     /* bitpos */
789          complain_overflow_signed, /* complain_on_overflow */
790          ppc64_elf_sectoff_reloc, /* special_function */
791          "R_PPC64_SECTOFF_HI",  /* name */
792          FALSE,                 /* partial_inplace */
793          0,                     /* src_mask */
794          0xffff,                /* dst_mask */
795          FALSE),                /* pcrel_offset */
796
797   /* 16-bit upper half adjusted section relative relocation.  */
798   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
799          16,                    /* rightshift */
800          1,                     /* size (0 = byte, 1 = short, 2 = long) */
801          16,                    /* bitsize */
802          FALSE,                 /* pc_relative */
803          0,                     /* bitpos */
804          complain_overflow_signed, /* complain_on_overflow */
805          ppc64_elf_sectoff_ha_reloc, /* special_function */
806          "R_PPC64_SECTOFF_HA",  /* name */
807          FALSE,                 /* partial_inplace */
808          0,                     /* src_mask */
809          0xffff,                /* dst_mask */
810          FALSE),                /* pcrel_offset */
811
812   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
813   HOWTO (R_PPC64_REL30,         /* type */
814          2,                     /* rightshift */
815          2,                     /* size (0 = byte, 1 = short, 2 = long) */
816          30,                    /* bitsize */
817          TRUE,                  /* pc_relative */
818          0,                     /* bitpos */
819          complain_overflow_dont, /* complain_on_overflow */
820          bfd_elf_generic_reloc, /* special_function */
821          "R_PPC64_REL30",       /* name */
822          FALSE,                 /* partial_inplace */
823          0,                     /* src_mask */
824          0xfffffffc,            /* dst_mask */
825          TRUE),                 /* pcrel_offset */
826
827   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
828
829   /* A standard 64-bit relocation.  */
830   HOWTO (R_PPC64_ADDR64,        /* type */
831          0,                     /* rightshift */
832          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
833          64,                    /* bitsize */
834          FALSE,                 /* pc_relative */
835          0,                     /* bitpos */
836          complain_overflow_dont, /* complain_on_overflow */
837          bfd_elf_generic_reloc, /* special_function */
838          "R_PPC64_ADDR64",      /* name */
839          FALSE,                 /* partial_inplace */
840          0,                     /* src_mask */
841          ONES (64),             /* dst_mask */
842          FALSE),                /* pcrel_offset */
843
844   /* The bits 32-47 of an address.  */
845   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
846          32,                    /* rightshift */
847          1,                     /* size (0 = byte, 1 = short, 2 = long) */
848          16,                    /* bitsize */
849          FALSE,                 /* pc_relative */
850          0,                     /* bitpos */
851          complain_overflow_dont, /* complain_on_overflow */
852          bfd_elf_generic_reloc, /* special_function */
853          "R_PPC64_ADDR16_HIGHER", /* name */
854          FALSE,                 /* partial_inplace */
855          0,                     /* src_mask */
856          0xffff,                /* dst_mask */
857          FALSE),                /* pcrel_offset */
858
859   /* The bits 32-47 of an address, plus 1 if the contents of the low
860      16 bits, treated as a signed number, is negative.  */
861   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
862          32,                    /* rightshift */
863          1,                     /* size (0 = byte, 1 = short, 2 = long) */
864          16,                    /* bitsize */
865          FALSE,                 /* pc_relative */
866          0,                     /* bitpos */
867          complain_overflow_dont, /* complain_on_overflow */
868          ppc64_elf_ha_reloc,    /* special_function */
869          "R_PPC64_ADDR16_HIGHERA", /* name */
870          FALSE,                 /* partial_inplace */
871          0,                     /* src_mask */
872          0xffff,                /* dst_mask */
873          FALSE),                /* pcrel_offset */
874
875   /* The bits 48-63 of an address.  */
876   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
877          48,                    /* rightshift */
878          1,                     /* size (0 = byte, 1 = short, 2 = long) */
879          16,                    /* bitsize */
880          FALSE,                 /* pc_relative */
881          0,                     /* bitpos */
882          complain_overflow_dont, /* complain_on_overflow */
883          bfd_elf_generic_reloc, /* special_function */
884          "R_PPC64_ADDR16_HIGHEST", /* name */
885          FALSE,                 /* partial_inplace */
886          0,                     /* src_mask */
887          0xffff,                /* dst_mask */
888          FALSE),                /* pcrel_offset */
889
890   /* The bits 48-63 of an address, plus 1 if the contents of the low
891      16 bits, treated as a signed number, is negative.  */
892   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
893          48,                    /* rightshift */
894          1,                     /* size (0 = byte, 1 = short, 2 = long) */
895          16,                    /* bitsize */
896          FALSE,                 /* pc_relative */
897          0,                     /* bitpos */
898          complain_overflow_dont, /* complain_on_overflow */
899          ppc64_elf_ha_reloc,    /* special_function */
900          "R_PPC64_ADDR16_HIGHESTA", /* name */
901          FALSE,                 /* partial_inplace */
902          0,                     /* src_mask */
903          0xffff,                /* dst_mask */
904          FALSE),                /* pcrel_offset */
905
906   /* Like ADDR64, but may be unaligned.  */
907   HOWTO (R_PPC64_UADDR64,       /* type */
908          0,                     /* rightshift */
909          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
910          64,                    /* bitsize */
911          FALSE,                 /* pc_relative */
912          0,                     /* bitpos */
913          complain_overflow_dont, /* complain_on_overflow */
914          bfd_elf_generic_reloc, /* special_function */
915          "R_PPC64_UADDR64",     /* name */
916          FALSE,                 /* partial_inplace */
917          0,                     /* src_mask */
918          ONES (64),             /* dst_mask */
919          FALSE),                /* pcrel_offset */
920
921   /* 64-bit relative relocation.  */
922   HOWTO (R_PPC64_REL64,         /* type */
923          0,                     /* rightshift */
924          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
925          64,                    /* bitsize */
926          TRUE,                  /* pc_relative */
927          0,                     /* bitpos */
928          complain_overflow_dont, /* complain_on_overflow */
929          bfd_elf_generic_reloc, /* special_function */
930          "R_PPC64_REL64",       /* name */
931          FALSE,                 /* partial_inplace */
932          0,                     /* src_mask */
933          ONES (64),             /* dst_mask */
934          TRUE),                 /* pcrel_offset */
935
936   /* 64-bit relocation to the symbol's procedure linkage table.  */
937   HOWTO (R_PPC64_PLT64,         /* type */
938          0,                     /* rightshift */
939          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
940          64,                    /* bitsize */
941          FALSE,                 /* pc_relative */
942          0,                     /* bitpos */
943          complain_overflow_dont, /* complain_on_overflow */
944          ppc64_elf_unhandled_reloc, /* special_function */
945          "R_PPC64_PLT64",       /* name */
946          FALSE,                 /* partial_inplace */
947          0,                     /* src_mask */
948          ONES (64),             /* dst_mask */
949          FALSE),                /* pcrel_offset */
950
951   /* 64-bit PC relative relocation to the symbol's procedure linkage
952      table.  */
953   /* FIXME: R_PPC64_PLTREL64 not supported.  */
954   HOWTO (R_PPC64_PLTREL64,      /* type */
955          0,                     /* rightshift */
956          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
957          64,                    /* bitsize */
958          TRUE,                  /* pc_relative */
959          0,                     /* bitpos */
960          complain_overflow_dont, /* complain_on_overflow */
961          ppc64_elf_unhandled_reloc, /* special_function */
962          "R_PPC64_PLTREL64",    /* name */
963          FALSE,                 /* partial_inplace */
964          0,                     /* src_mask */
965          ONES (64),             /* dst_mask */
966          TRUE),                 /* pcrel_offset */
967
968   /* 16 bit TOC-relative relocation.  */
969
970   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
971   HOWTO (R_PPC64_TOC16,         /* type */
972          0,                     /* rightshift */
973          1,                     /* size (0 = byte, 1 = short, 2 = long) */
974          16,                    /* bitsize */
975          FALSE,                 /* pc_relative */
976          0,                     /* bitpos */
977          complain_overflow_signed, /* complain_on_overflow */
978          ppc64_elf_toc_reloc,   /* special_function */
979          "R_PPC64_TOC16",       /* name */
980          FALSE,                 /* partial_inplace */
981          0,                     /* src_mask */
982          0xffff,                /* dst_mask */
983          FALSE),                /* pcrel_offset */
984
985   /* 16 bit TOC-relative relocation without overflow.  */
986
987   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
988   HOWTO (R_PPC64_TOC16_LO,      /* type */
989          0,                     /* rightshift */
990          1,                     /* size (0 = byte, 1 = short, 2 = long) */
991          16,                    /* bitsize */
992          FALSE,                 /* pc_relative */
993          0,                     /* bitpos */
994          complain_overflow_dont, /* complain_on_overflow */
995          ppc64_elf_toc_reloc,   /* special_function */
996          "R_PPC64_TOC16_LO",    /* name */
997          FALSE,                 /* partial_inplace */
998          0,                     /* src_mask */
999          0xffff,                /* dst_mask */
1000          FALSE),                /* pcrel_offset */
1001
1002   /* 16 bit TOC-relative relocation, high 16 bits.  */
1003
1004   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
1005   HOWTO (R_PPC64_TOC16_HI,      /* type */
1006          16,                    /* rightshift */
1007          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1008          16,                    /* bitsize */
1009          FALSE,                 /* pc_relative */
1010          0,                     /* bitpos */
1011          complain_overflow_signed, /* complain_on_overflow */
1012          ppc64_elf_toc_reloc,   /* special_function */
1013          "R_PPC64_TOC16_HI",    /* name */
1014          FALSE,                 /* partial_inplace */
1015          0,                     /* src_mask */
1016          0xffff,                /* dst_mask */
1017          FALSE),                /* pcrel_offset */
1018
1019   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1020      contents of the low 16 bits, treated as a signed number, is
1021      negative.  */
1022
1023   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1024   HOWTO (R_PPC64_TOC16_HA,      /* type */
1025          16,                    /* rightshift */
1026          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1027          16,                    /* bitsize */
1028          FALSE,                 /* pc_relative */
1029          0,                     /* bitpos */
1030          complain_overflow_signed, /* complain_on_overflow */
1031          ppc64_elf_toc_ha_reloc, /* special_function */
1032          "R_PPC64_TOC16_HA",    /* name */
1033          FALSE,                 /* partial_inplace */
1034          0,                     /* src_mask */
1035          0xffff,                /* dst_mask */
1036          FALSE),                /* pcrel_offset */
1037
1038   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1039
1040   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1041   HOWTO (R_PPC64_TOC,           /* type */
1042          0,                     /* rightshift */
1043          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1044          64,                    /* bitsize */
1045          FALSE,                 /* pc_relative */
1046          0,                     /* bitpos */
1047          complain_overflow_dont, /* complain_on_overflow */
1048          ppc64_elf_toc64_reloc, /* special_function */
1049          "R_PPC64_TOC",         /* name */
1050          FALSE,                 /* partial_inplace */
1051          0,                     /* src_mask */
1052          ONES (64),             /* dst_mask */
1053          FALSE),                /* pcrel_offset */
1054
1055   /* Like R_PPC64_GOT16, but also informs the link editor that the
1056      value to relocate may (!) refer to a PLT entry which the link
1057      editor (a) may replace with the symbol value.  If the link editor
1058      is unable to fully resolve the symbol, it may (b) create a PLT
1059      entry and store the address to the new PLT entry in the GOT.
1060      This permits lazy resolution of function symbols at run time.
1061      The link editor may also skip all of this and just (c) emit a
1062      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1063   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1064     HOWTO (R_PPC64_PLTGOT16,    /* type */
1065          0,                     /* rightshift */
1066          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1067          16,                    /* bitsize */
1068          FALSE,                 /* pc_relative */
1069          0,                     /* bitpos */
1070          complain_overflow_signed, /* complain_on_overflow */
1071          ppc64_elf_unhandled_reloc, /* special_function */
1072          "R_PPC64_PLTGOT16",    /* name */
1073          FALSE,                 /* partial_inplace */
1074          0,                     /* src_mask */
1075          0xffff,                /* dst_mask */
1076          FALSE),                /* pcrel_offset */
1077
1078   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1079   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1080   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1081          0,                     /* rightshift */
1082          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1083          16,                    /* bitsize */
1084          FALSE,                 /* pc_relative */
1085          0,                     /* bitpos */
1086          complain_overflow_dont, /* complain_on_overflow */
1087          ppc64_elf_unhandled_reloc, /* special_function */
1088          "R_PPC64_PLTGOT16_LO", /* name */
1089          FALSE,                 /* partial_inplace */
1090          0,                     /* src_mask */
1091          0xffff,                /* dst_mask */
1092          FALSE),                /* pcrel_offset */
1093
1094   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1095   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1096   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1097          16,                    /* rightshift */
1098          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1099          16,                    /* bitsize */
1100          FALSE,                 /* pc_relative */
1101          0,                     /* bitpos */
1102          complain_overflow_signed, /* complain_on_overflow */
1103          ppc64_elf_unhandled_reloc, /* special_function */
1104          "R_PPC64_PLTGOT16_HI", /* name */
1105          FALSE,                 /* partial_inplace */
1106          0,                     /* src_mask */
1107          0xffff,                /* dst_mask */
1108          FALSE),                /* pcrel_offset */
1109
1110   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1111      1 if the contents of the low 16 bits, treated as a signed number,
1112      is negative.  */
1113   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1114   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1115          16,                    /* rightshift */
1116          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1117          16,                    /* bitsize */
1118          FALSE,                 /* pc_relative */
1119          0,                     /* bitpos */
1120          complain_overflow_signed, /* complain_on_overflow */
1121          ppc64_elf_unhandled_reloc, /* special_function */
1122          "R_PPC64_PLTGOT16_HA", /* name */
1123          FALSE,                 /* partial_inplace */
1124          0,                     /* src_mask */
1125          0xffff,                /* dst_mask */
1126          FALSE),                /* pcrel_offset */
1127
1128   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1129   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1130          0,                     /* rightshift */
1131          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1132          16,                    /* bitsize */
1133          FALSE,                 /* pc_relative */
1134          0,                     /* bitpos */
1135          complain_overflow_signed, /* complain_on_overflow */
1136          bfd_elf_generic_reloc, /* special_function */
1137          "R_PPC64_ADDR16_DS",   /* name */
1138          FALSE,                 /* partial_inplace */
1139          0,                     /* src_mask */
1140          0xfffc,                /* dst_mask */
1141          FALSE),                /* pcrel_offset */
1142
1143   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1144   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1145          0,                     /* rightshift */
1146          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1147          16,                    /* bitsize */
1148          FALSE,                 /* pc_relative */
1149          0,                     /* bitpos */
1150          complain_overflow_dont,/* complain_on_overflow */
1151          bfd_elf_generic_reloc, /* special_function */
1152          "R_PPC64_ADDR16_LO_DS",/* name */
1153          FALSE,                 /* partial_inplace */
1154          0,                     /* src_mask */
1155          0xfffc,                /* dst_mask */
1156          FALSE),                /* pcrel_offset */
1157
1158   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1159   HOWTO (R_PPC64_GOT16_DS,      /* type */
1160          0,                     /* rightshift */
1161          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1162          16,                    /* bitsize */
1163          FALSE,                 /* pc_relative */
1164          0,                     /* bitpos */
1165          complain_overflow_signed, /* complain_on_overflow */
1166          ppc64_elf_unhandled_reloc, /* special_function */
1167          "R_PPC64_GOT16_DS",    /* name */
1168          FALSE,                 /* partial_inplace */
1169          0,                     /* src_mask */
1170          0xfffc,                /* dst_mask */
1171          FALSE),                /* pcrel_offset */
1172
1173   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1174   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1175          0,                     /* rightshift */
1176          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1177          16,                    /* bitsize */
1178          FALSE,                 /* pc_relative */
1179          0,                     /* bitpos */
1180          complain_overflow_dont, /* complain_on_overflow */
1181          ppc64_elf_unhandled_reloc, /* special_function */
1182          "R_PPC64_GOT16_LO_DS", /* name */
1183          FALSE,                 /* partial_inplace */
1184          0,                     /* src_mask */
1185          0xfffc,                /* dst_mask */
1186          FALSE),                /* pcrel_offset */
1187
1188   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1189   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1190          0,                     /* rightshift */
1191          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1192          16,                    /* bitsize */
1193          FALSE,                 /* pc_relative */
1194          0,                     /* bitpos */
1195          complain_overflow_dont, /* complain_on_overflow */
1196          ppc64_elf_unhandled_reloc, /* special_function */
1197          "R_PPC64_PLT16_LO_DS", /* name */
1198          FALSE,                 /* partial_inplace */
1199          0,                     /* src_mask */
1200          0xfffc,                /* dst_mask */
1201          FALSE),                /* pcrel_offset */
1202
1203   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1204   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1205          0,                     /* rightshift */
1206          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1207          16,                    /* bitsize */
1208          FALSE,                 /* pc_relative */
1209          0,                     /* bitpos */
1210          complain_overflow_signed, /* complain_on_overflow */
1211          ppc64_elf_sectoff_reloc, /* special_function */
1212          "R_PPC64_SECTOFF_DS",  /* name */
1213          FALSE,                 /* partial_inplace */
1214          0,                     /* src_mask */
1215          0xfffc,                /* dst_mask */
1216          FALSE),                /* pcrel_offset */
1217
1218   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1219   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1220          0,                     /* rightshift */
1221          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1222          16,                    /* bitsize */
1223          FALSE,                 /* pc_relative */
1224          0,                     /* bitpos */
1225          complain_overflow_dont, /* complain_on_overflow */
1226          ppc64_elf_sectoff_reloc, /* special_function */
1227          "R_PPC64_SECTOFF_LO_DS",/* name */
1228          FALSE,                 /* partial_inplace */
1229          0,                     /* src_mask */
1230          0xfffc,                /* dst_mask */
1231          FALSE),                /* pcrel_offset */
1232
1233   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1234   HOWTO (R_PPC64_TOC16_DS,      /* type */
1235          0,                     /* rightshift */
1236          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1237          16,                    /* bitsize */
1238          FALSE,                 /* pc_relative */
1239          0,                     /* bitpos */
1240          complain_overflow_signed, /* complain_on_overflow */
1241          ppc64_elf_toc_reloc,   /* special_function */
1242          "R_PPC64_TOC16_DS",    /* name */
1243          FALSE,                 /* partial_inplace */
1244          0,                     /* src_mask */
1245          0xfffc,                /* dst_mask */
1246          FALSE),                /* pcrel_offset */
1247
1248   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1249   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1250          0,                     /* rightshift */
1251          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1252          16,                    /* bitsize */
1253          FALSE,                 /* pc_relative */
1254          0,                     /* bitpos */
1255          complain_overflow_dont, /* complain_on_overflow */
1256          ppc64_elf_toc_reloc,   /* special_function */
1257          "R_PPC64_TOC16_LO_DS", /* name */
1258          FALSE,                 /* partial_inplace */
1259          0,                     /* src_mask */
1260          0xfffc,                /* dst_mask */
1261          FALSE),                /* pcrel_offset */
1262
1263   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1264   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1265   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1266          0,                     /* rightshift */
1267          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1268          16,                    /* bitsize */
1269          FALSE,                 /* pc_relative */
1270          0,                     /* bitpos */
1271          complain_overflow_signed, /* complain_on_overflow */
1272          ppc64_elf_unhandled_reloc, /* special_function */
1273          "R_PPC64_PLTGOT16_DS", /* name */
1274          FALSE,                 /* partial_inplace */
1275          0,                     /* src_mask */
1276          0xfffc,                /* dst_mask */
1277          FALSE),                /* pcrel_offset */
1278
1279   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1280   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1281   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1282          0,                     /* rightshift */
1283          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1284          16,                    /* bitsize */
1285          FALSE,                 /* pc_relative */
1286          0,                     /* bitpos */
1287          complain_overflow_dont, /* complain_on_overflow */
1288          ppc64_elf_unhandled_reloc, /* special_function */
1289          "R_PPC64_PLTGOT16_LO_DS",/* name */
1290          FALSE,                 /* partial_inplace */
1291          0,                     /* src_mask */
1292          0xfffc,                /* dst_mask */
1293          FALSE),                /* pcrel_offset */
1294
1295   /* Marker relocs for TLS.  */
1296   HOWTO (R_PPC64_TLS,
1297          0,                     /* rightshift */
1298          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1299          32,                    /* bitsize */
1300          FALSE,                 /* pc_relative */
1301          0,                     /* bitpos */
1302          complain_overflow_dont, /* complain_on_overflow */
1303          bfd_elf_generic_reloc, /* special_function */
1304          "R_PPC64_TLS",         /* name */
1305          FALSE,                 /* partial_inplace */
1306          0,                     /* src_mask */
1307          0,                     /* dst_mask */
1308          FALSE),                /* pcrel_offset */
1309
1310   HOWTO (R_PPC64_TLSGD,
1311          0,                     /* rightshift */
1312          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1313          32,                    /* bitsize */
1314          FALSE,                 /* pc_relative */
1315          0,                     /* bitpos */
1316          complain_overflow_dont, /* complain_on_overflow */
1317          bfd_elf_generic_reloc, /* special_function */
1318          "R_PPC64_TLSGD",       /* name */
1319          FALSE,                 /* partial_inplace */
1320          0,                     /* src_mask */
1321          0,                     /* dst_mask */
1322          FALSE),                /* pcrel_offset */
1323
1324   HOWTO (R_PPC64_TLSLD,
1325          0,                     /* rightshift */
1326          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1327          32,                    /* bitsize */
1328          FALSE,                 /* pc_relative */
1329          0,                     /* bitpos */
1330          complain_overflow_dont, /* complain_on_overflow */
1331          bfd_elf_generic_reloc, /* special_function */
1332          "R_PPC64_TLSLD",       /* name */
1333          FALSE,                 /* partial_inplace */
1334          0,                     /* src_mask */
1335          0,                     /* dst_mask */
1336          FALSE),                /* pcrel_offset */
1337
1338   HOWTO (R_PPC64_TOCSAVE,
1339          0,                     /* rightshift */
1340          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1341          32,                    /* bitsize */
1342          FALSE,                 /* pc_relative */
1343          0,                     /* bitpos */
1344          complain_overflow_dont, /* complain_on_overflow */
1345          bfd_elf_generic_reloc, /* special_function */
1346          "R_PPC64_TOCSAVE",     /* name */
1347          FALSE,                 /* partial_inplace */
1348          0,                     /* src_mask */
1349          0,                     /* dst_mask */
1350          FALSE),                /* pcrel_offset */
1351
1352   /* Computes the load module index of the load module that contains the
1353      definition of its TLS sym.  */
1354   HOWTO (R_PPC64_DTPMOD64,
1355          0,                     /* rightshift */
1356          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1357          64,                    /* bitsize */
1358          FALSE,                 /* pc_relative */
1359          0,                     /* bitpos */
1360          complain_overflow_dont, /* complain_on_overflow */
1361          ppc64_elf_unhandled_reloc, /* special_function */
1362          "R_PPC64_DTPMOD64",    /* name */
1363          FALSE,                 /* partial_inplace */
1364          0,                     /* src_mask */
1365          ONES (64),             /* dst_mask */
1366          FALSE),                /* pcrel_offset */
1367
1368   /* Computes a dtv-relative displacement, the difference between the value
1369      of sym+add and the base address of the thread-local storage block that
1370      contains the definition of sym, minus 0x8000.  */
1371   HOWTO (R_PPC64_DTPREL64,
1372          0,                     /* rightshift */
1373          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1374          64,                    /* bitsize */
1375          FALSE,                 /* pc_relative */
1376          0,                     /* bitpos */
1377          complain_overflow_dont, /* complain_on_overflow */
1378          ppc64_elf_unhandled_reloc, /* special_function */
1379          "R_PPC64_DTPREL64",    /* name */
1380          FALSE,                 /* partial_inplace */
1381          0,                     /* src_mask */
1382          ONES (64),             /* dst_mask */
1383          FALSE),                /* pcrel_offset */
1384
1385   /* A 16 bit dtprel reloc.  */
1386   HOWTO (R_PPC64_DTPREL16,
1387          0,                     /* rightshift */
1388          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1389          16,                    /* bitsize */
1390          FALSE,                 /* pc_relative */
1391          0,                     /* bitpos */
1392          complain_overflow_signed, /* complain_on_overflow */
1393          ppc64_elf_unhandled_reloc, /* special_function */
1394          "R_PPC64_DTPREL16",    /* name */
1395          FALSE,                 /* partial_inplace */
1396          0,                     /* src_mask */
1397          0xffff,                /* dst_mask */
1398          FALSE),                /* pcrel_offset */
1399
1400   /* Like DTPREL16, but no overflow.  */
1401   HOWTO (R_PPC64_DTPREL16_LO,
1402          0,                     /* rightshift */
1403          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1404          16,                    /* bitsize */
1405          FALSE,                 /* pc_relative */
1406          0,                     /* bitpos */
1407          complain_overflow_dont, /* complain_on_overflow */
1408          ppc64_elf_unhandled_reloc, /* special_function */
1409          "R_PPC64_DTPREL16_LO", /* name */
1410          FALSE,                 /* partial_inplace */
1411          0,                     /* src_mask */
1412          0xffff,                /* dst_mask */
1413          FALSE),                /* pcrel_offset */
1414
1415   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1416   HOWTO (R_PPC64_DTPREL16_HI,
1417          16,                    /* rightshift */
1418          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1419          16,                    /* bitsize */
1420          FALSE,                 /* pc_relative */
1421          0,                     /* bitpos */
1422          complain_overflow_signed, /* complain_on_overflow */
1423          ppc64_elf_unhandled_reloc, /* special_function */
1424          "R_PPC64_DTPREL16_HI", /* name */
1425          FALSE,                 /* partial_inplace */
1426          0,                     /* src_mask */
1427          0xffff,                /* dst_mask */
1428          FALSE),                /* pcrel_offset */
1429
1430   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1431   HOWTO (R_PPC64_DTPREL16_HA,
1432          16,                    /* rightshift */
1433          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1434          16,                    /* bitsize */
1435          FALSE,                 /* pc_relative */
1436          0,                     /* bitpos */
1437          complain_overflow_signed, /* complain_on_overflow */
1438          ppc64_elf_unhandled_reloc, /* special_function */
1439          "R_PPC64_DTPREL16_HA", /* name */
1440          FALSE,                 /* partial_inplace */
1441          0,                     /* src_mask */
1442          0xffff,                /* dst_mask */
1443          FALSE),                /* pcrel_offset */
1444
1445   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1446   HOWTO (R_PPC64_DTPREL16_HIGHER,
1447          32,                    /* rightshift */
1448          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1449          16,                    /* bitsize */
1450          FALSE,                 /* pc_relative */
1451          0,                     /* bitpos */
1452          complain_overflow_dont, /* complain_on_overflow */
1453          ppc64_elf_unhandled_reloc, /* special_function */
1454          "R_PPC64_DTPREL16_HIGHER", /* name */
1455          FALSE,                 /* partial_inplace */
1456          0,                     /* src_mask */
1457          0xffff,                /* dst_mask */
1458          FALSE),                /* pcrel_offset */
1459
1460   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1461   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1462          32,                    /* rightshift */
1463          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1464          16,                    /* bitsize */
1465          FALSE,                 /* pc_relative */
1466          0,                     /* bitpos */
1467          complain_overflow_dont, /* complain_on_overflow */
1468          ppc64_elf_unhandled_reloc, /* special_function */
1469          "R_PPC64_DTPREL16_HIGHERA", /* name */
1470          FALSE,                 /* partial_inplace */
1471          0,                     /* src_mask */
1472          0xffff,                /* dst_mask */
1473          FALSE),                /* pcrel_offset */
1474
1475   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1476   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1477          48,                    /* rightshift */
1478          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1479          16,                    /* bitsize */
1480          FALSE,                 /* pc_relative */
1481          0,                     /* bitpos */
1482          complain_overflow_dont, /* complain_on_overflow */
1483          ppc64_elf_unhandled_reloc, /* special_function */
1484          "R_PPC64_DTPREL16_HIGHEST", /* name */
1485          FALSE,                 /* partial_inplace */
1486          0,                     /* src_mask */
1487          0xffff,                /* dst_mask */
1488          FALSE),                /* pcrel_offset */
1489
1490   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1491   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1492          48,                    /* rightshift */
1493          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1494          16,                    /* bitsize */
1495          FALSE,                 /* pc_relative */
1496          0,                     /* bitpos */
1497          complain_overflow_dont, /* complain_on_overflow */
1498          ppc64_elf_unhandled_reloc, /* special_function */
1499          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1500          FALSE,                 /* partial_inplace */
1501          0,                     /* src_mask */
1502          0xffff,                /* dst_mask */
1503          FALSE),                /* pcrel_offset */
1504
1505   /* Like DTPREL16, but for insns with a DS field.  */
1506   HOWTO (R_PPC64_DTPREL16_DS,
1507          0,                     /* rightshift */
1508          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          16,                    /* bitsize */
1510          FALSE,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          complain_overflow_signed, /* complain_on_overflow */
1513          ppc64_elf_unhandled_reloc, /* special_function */
1514          "R_PPC64_DTPREL16_DS", /* name */
1515          FALSE,                 /* partial_inplace */
1516          0,                     /* src_mask */
1517          0xfffc,                /* dst_mask */
1518          FALSE),                /* pcrel_offset */
1519
1520   /* Like DTPREL16_DS, but no overflow.  */
1521   HOWTO (R_PPC64_DTPREL16_LO_DS,
1522          0,                     /* rightshift */
1523          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          16,                    /* bitsize */
1525          FALSE,                 /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_dont, /* complain_on_overflow */
1528          ppc64_elf_unhandled_reloc, /* special_function */
1529          "R_PPC64_DTPREL16_LO_DS", /* name */
1530          FALSE,                 /* partial_inplace */
1531          0,                     /* src_mask */
1532          0xfffc,                /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   /* Computes a tp-relative displacement, the difference between the value of
1536      sym+add and the value of the thread pointer (r13).  */
1537   HOWTO (R_PPC64_TPREL64,
1538          0,                     /* rightshift */
1539          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1540          64,                    /* bitsize */
1541          FALSE,                 /* pc_relative */
1542          0,                     /* bitpos */
1543          complain_overflow_dont, /* complain_on_overflow */
1544          ppc64_elf_unhandled_reloc, /* special_function */
1545          "R_PPC64_TPREL64",     /* name */
1546          FALSE,                 /* partial_inplace */
1547          0,                     /* src_mask */
1548          ONES (64),             /* dst_mask */
1549          FALSE),                /* pcrel_offset */
1550
1551   /* A 16 bit tprel reloc.  */
1552   HOWTO (R_PPC64_TPREL16,
1553          0,                     /* rightshift */
1554          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1555          16,                    /* bitsize */
1556          FALSE,                 /* pc_relative */
1557          0,                     /* bitpos */
1558          complain_overflow_signed, /* complain_on_overflow */
1559          ppc64_elf_unhandled_reloc, /* special_function */
1560          "R_PPC64_TPREL16",     /* name */
1561          FALSE,                 /* partial_inplace */
1562          0,                     /* src_mask */
1563          0xffff,                /* dst_mask */
1564          FALSE),                /* pcrel_offset */
1565
1566   /* Like TPREL16, but no overflow.  */
1567   HOWTO (R_PPC64_TPREL16_LO,
1568          0,                     /* rightshift */
1569          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1570          16,                    /* bitsize */
1571          FALSE,                 /* pc_relative */
1572          0,                     /* bitpos */
1573          complain_overflow_dont, /* complain_on_overflow */
1574          ppc64_elf_unhandled_reloc, /* special_function */
1575          "R_PPC64_TPREL16_LO",  /* name */
1576          FALSE,                 /* partial_inplace */
1577          0,                     /* src_mask */
1578          0xffff,                /* dst_mask */
1579          FALSE),                /* pcrel_offset */
1580
1581   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1582   HOWTO (R_PPC64_TPREL16_HI,
1583          16,                    /* rightshift */
1584          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1585          16,                    /* bitsize */
1586          FALSE,                 /* pc_relative */
1587          0,                     /* bitpos */
1588          complain_overflow_signed, /* complain_on_overflow */
1589          ppc64_elf_unhandled_reloc, /* special_function */
1590          "R_PPC64_TPREL16_HI",  /* name */
1591          FALSE,                 /* partial_inplace */
1592          0,                     /* src_mask */
1593          0xffff,                /* dst_mask */
1594          FALSE),                /* pcrel_offset */
1595
1596   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1597   HOWTO (R_PPC64_TPREL16_HA,
1598          16,                    /* rightshift */
1599          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1600          16,                    /* bitsize */
1601          FALSE,                 /* pc_relative */
1602          0,                     /* bitpos */
1603          complain_overflow_signed, /* complain_on_overflow */
1604          ppc64_elf_unhandled_reloc, /* special_function */
1605          "R_PPC64_TPREL16_HA",  /* name */
1606          FALSE,                 /* partial_inplace */
1607          0,                     /* src_mask */
1608          0xffff,                /* dst_mask */
1609          FALSE),                /* pcrel_offset */
1610
1611   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1612   HOWTO (R_PPC64_TPREL16_HIGHER,
1613          32,                    /* rightshift */
1614          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1615          16,                    /* bitsize */
1616          FALSE,                 /* pc_relative */
1617          0,                     /* bitpos */
1618          complain_overflow_dont, /* complain_on_overflow */
1619          ppc64_elf_unhandled_reloc, /* special_function */
1620          "R_PPC64_TPREL16_HIGHER",      /* name */
1621          FALSE,                 /* partial_inplace */
1622          0,                     /* src_mask */
1623          0xffff,                /* dst_mask */
1624          FALSE),                /* pcrel_offset */
1625
1626   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1627   HOWTO (R_PPC64_TPREL16_HIGHERA,
1628          32,                    /* rightshift */
1629          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1630          16,                    /* bitsize */
1631          FALSE,                 /* pc_relative */
1632          0,                     /* bitpos */
1633          complain_overflow_dont, /* complain_on_overflow */
1634          ppc64_elf_unhandled_reloc, /* special_function */
1635          "R_PPC64_TPREL16_HIGHERA", /* name */
1636          FALSE,                 /* partial_inplace */
1637          0,                     /* src_mask */
1638          0xffff,                /* dst_mask */
1639          FALSE),                /* pcrel_offset */
1640
1641   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1642   HOWTO (R_PPC64_TPREL16_HIGHEST,
1643          48,                    /* rightshift */
1644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1645          16,                    /* bitsize */
1646          FALSE,                 /* pc_relative */
1647          0,                     /* bitpos */
1648          complain_overflow_dont, /* complain_on_overflow */
1649          ppc64_elf_unhandled_reloc, /* special_function */
1650          "R_PPC64_TPREL16_HIGHEST", /* name */
1651          FALSE,                 /* partial_inplace */
1652          0,                     /* src_mask */
1653          0xffff,                /* dst_mask */
1654          FALSE),                /* pcrel_offset */
1655
1656   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1657   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1658          48,                    /* rightshift */
1659          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1660          16,                    /* bitsize */
1661          FALSE,                 /* pc_relative */
1662          0,                     /* bitpos */
1663          complain_overflow_dont, /* complain_on_overflow */
1664          ppc64_elf_unhandled_reloc, /* special_function */
1665          "R_PPC64_TPREL16_HIGHESTA", /* name */
1666          FALSE,                 /* partial_inplace */
1667          0,                     /* src_mask */
1668          0xffff,                /* dst_mask */
1669          FALSE),                /* pcrel_offset */
1670
1671   /* Like TPREL16, but for insns with a DS field.  */
1672   HOWTO (R_PPC64_TPREL16_DS,
1673          0,                     /* rightshift */
1674          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1675          16,                    /* bitsize */
1676          FALSE,                 /* pc_relative */
1677          0,                     /* bitpos */
1678          complain_overflow_signed, /* complain_on_overflow */
1679          ppc64_elf_unhandled_reloc, /* special_function */
1680          "R_PPC64_TPREL16_DS",  /* name */
1681          FALSE,                 /* partial_inplace */
1682          0,                     /* src_mask */
1683          0xfffc,                /* dst_mask */
1684          FALSE),                /* pcrel_offset */
1685
1686   /* Like TPREL16_DS, but no overflow.  */
1687   HOWTO (R_PPC64_TPREL16_LO_DS,
1688          0,                     /* rightshift */
1689          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1690          16,                    /* bitsize */
1691          FALSE,                 /* pc_relative */
1692          0,                     /* bitpos */
1693          complain_overflow_dont, /* complain_on_overflow */
1694          ppc64_elf_unhandled_reloc, /* special_function */
1695          "R_PPC64_TPREL16_LO_DS", /* name */
1696          FALSE,                 /* partial_inplace */
1697          0,                     /* src_mask */
1698          0xfffc,                /* dst_mask */
1699          FALSE),                /* pcrel_offset */
1700
1701   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1702      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1703      to the first entry relative to the TOC base (r2).  */
1704   HOWTO (R_PPC64_GOT_TLSGD16,
1705          0,                     /* rightshift */
1706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1707          16,                    /* bitsize */
1708          FALSE,                 /* pc_relative */
1709          0,                     /* bitpos */
1710          complain_overflow_signed, /* complain_on_overflow */
1711          ppc64_elf_unhandled_reloc, /* special_function */
1712          "R_PPC64_GOT_TLSGD16", /* name */
1713          FALSE,                 /* partial_inplace */
1714          0,                     /* src_mask */
1715          0xffff,                /* dst_mask */
1716          FALSE),                /* pcrel_offset */
1717
1718   /* Like GOT_TLSGD16, but no overflow.  */
1719   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1720          0,                     /* rightshift */
1721          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1722          16,                    /* bitsize */
1723          FALSE,                 /* pc_relative */
1724          0,                     /* bitpos */
1725          complain_overflow_dont, /* complain_on_overflow */
1726          ppc64_elf_unhandled_reloc, /* special_function */
1727          "R_PPC64_GOT_TLSGD16_LO", /* name */
1728          FALSE,                 /* partial_inplace */
1729          0,                     /* src_mask */
1730          0xffff,                /* dst_mask */
1731          FALSE),                /* pcrel_offset */
1732
1733   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1734   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1735          16,                    /* rightshift */
1736          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1737          16,                    /* bitsize */
1738          FALSE,                 /* pc_relative */
1739          0,                     /* bitpos */
1740          complain_overflow_signed, /* complain_on_overflow */
1741          ppc64_elf_unhandled_reloc, /* special_function */
1742          "R_PPC64_GOT_TLSGD16_HI", /* name */
1743          FALSE,                 /* partial_inplace */
1744          0,                     /* src_mask */
1745          0xffff,                /* dst_mask */
1746          FALSE),                /* pcrel_offset */
1747
1748   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1749   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1750          16,                    /* rightshift */
1751          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1752          16,                    /* bitsize */
1753          FALSE,                 /* pc_relative */
1754          0,                     /* bitpos */
1755          complain_overflow_signed, /* complain_on_overflow */
1756          ppc64_elf_unhandled_reloc, /* special_function */
1757          "R_PPC64_GOT_TLSGD16_HA", /* name */
1758          FALSE,                 /* partial_inplace */
1759          0,                     /* src_mask */
1760          0xffff,                /* dst_mask */
1761          FALSE),                /* pcrel_offset */
1762
1763   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1764      with values (sym+add)@dtpmod and zero, and computes the offset to the
1765      first entry relative to the TOC base (r2).  */
1766   HOWTO (R_PPC64_GOT_TLSLD16,
1767          0,                     /* rightshift */
1768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1769          16,                    /* bitsize */
1770          FALSE,                 /* pc_relative */
1771          0,                     /* bitpos */
1772          complain_overflow_signed, /* complain_on_overflow */
1773          ppc64_elf_unhandled_reloc, /* special_function */
1774          "R_PPC64_GOT_TLSLD16", /* name */
1775          FALSE,                 /* partial_inplace */
1776          0,                     /* src_mask */
1777          0xffff,                /* dst_mask */
1778          FALSE),                /* pcrel_offset */
1779
1780   /* Like GOT_TLSLD16, but no overflow.  */
1781   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1782          0,                     /* rightshift */
1783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1784          16,                    /* bitsize */
1785          FALSE,                 /* pc_relative */
1786          0,                     /* bitpos */
1787          complain_overflow_dont, /* complain_on_overflow */
1788          ppc64_elf_unhandled_reloc, /* special_function */
1789          "R_PPC64_GOT_TLSLD16_LO", /* name */
1790          FALSE,                 /* partial_inplace */
1791          0,                     /* src_mask */
1792          0xffff,                /* dst_mask */
1793          FALSE),                /* pcrel_offset */
1794
1795   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1796   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1797          16,                    /* rightshift */
1798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1799          16,                    /* bitsize */
1800          FALSE,                 /* pc_relative */
1801          0,                     /* bitpos */
1802          complain_overflow_signed, /* complain_on_overflow */
1803          ppc64_elf_unhandled_reloc, /* special_function */
1804          "R_PPC64_GOT_TLSLD16_HI", /* name */
1805          FALSE,                 /* partial_inplace */
1806          0,                     /* src_mask */
1807          0xffff,                /* dst_mask */
1808          FALSE),                /* pcrel_offset */
1809
1810   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1811   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1812          16,                    /* rightshift */
1813          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1814          16,                    /* bitsize */
1815          FALSE,                 /* pc_relative */
1816          0,                     /* bitpos */
1817          complain_overflow_signed, /* complain_on_overflow */
1818          ppc64_elf_unhandled_reloc, /* special_function */
1819          "R_PPC64_GOT_TLSLD16_HA", /* name */
1820          FALSE,                 /* partial_inplace */
1821          0,                     /* src_mask */
1822          0xffff,                /* dst_mask */
1823          FALSE),                /* pcrel_offset */
1824
1825   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1826      the offset to the entry relative to the TOC base (r2).  */
1827   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1828          0,                     /* rightshift */
1829          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1830          16,                    /* bitsize */
1831          FALSE,                 /* pc_relative */
1832          0,                     /* bitpos */
1833          complain_overflow_signed, /* complain_on_overflow */
1834          ppc64_elf_unhandled_reloc, /* special_function */
1835          "R_PPC64_GOT_DTPREL16_DS", /* name */
1836          FALSE,                 /* partial_inplace */
1837          0,                     /* src_mask */
1838          0xfffc,                /* dst_mask */
1839          FALSE),                /* pcrel_offset */
1840
1841   /* Like GOT_DTPREL16_DS, but no overflow.  */
1842   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1843          0,                     /* rightshift */
1844          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1845          16,                    /* bitsize */
1846          FALSE,                 /* pc_relative */
1847          0,                     /* bitpos */
1848          complain_overflow_dont, /* complain_on_overflow */
1849          ppc64_elf_unhandled_reloc, /* special_function */
1850          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1851          FALSE,                 /* partial_inplace */
1852          0,                     /* src_mask */
1853          0xfffc,                /* dst_mask */
1854          FALSE),                /* pcrel_offset */
1855
1856   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1857   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1858          16,                    /* rightshift */
1859          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1860          16,                    /* bitsize */
1861          FALSE,                 /* pc_relative */
1862          0,                     /* bitpos */
1863          complain_overflow_signed, /* complain_on_overflow */
1864          ppc64_elf_unhandled_reloc, /* special_function */
1865          "R_PPC64_GOT_DTPREL16_HI", /* name */
1866          FALSE,                 /* partial_inplace */
1867          0,                     /* src_mask */
1868          0xffff,                /* dst_mask */
1869          FALSE),                /* pcrel_offset */
1870
1871   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1872   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1873          16,                    /* rightshift */
1874          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1875          16,                    /* bitsize */
1876          FALSE,                 /* pc_relative */
1877          0,                     /* bitpos */
1878          complain_overflow_signed, /* complain_on_overflow */
1879          ppc64_elf_unhandled_reloc, /* special_function */
1880          "R_PPC64_GOT_DTPREL16_HA", /* name */
1881          FALSE,                 /* partial_inplace */
1882          0,                     /* src_mask */
1883          0xffff,                /* dst_mask */
1884          FALSE),                /* pcrel_offset */
1885
1886   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1887      offset to the entry relative to the TOC base (r2).  */
1888   HOWTO (R_PPC64_GOT_TPREL16_DS,
1889          0,                     /* rightshift */
1890          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1891          16,                    /* bitsize */
1892          FALSE,                 /* pc_relative */
1893          0,                     /* bitpos */
1894          complain_overflow_signed, /* complain_on_overflow */
1895          ppc64_elf_unhandled_reloc, /* special_function */
1896          "R_PPC64_GOT_TPREL16_DS", /* name */
1897          FALSE,                 /* partial_inplace */
1898          0,                     /* src_mask */
1899          0xfffc,                /* dst_mask */
1900          FALSE),                /* pcrel_offset */
1901
1902   /* Like GOT_TPREL16_DS, but no overflow.  */
1903   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1904          0,                     /* rightshift */
1905          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1906          16,                    /* bitsize */
1907          FALSE,                 /* pc_relative */
1908          0,                     /* bitpos */
1909          complain_overflow_dont, /* complain_on_overflow */
1910          ppc64_elf_unhandled_reloc, /* special_function */
1911          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1912          FALSE,                 /* partial_inplace */
1913          0,                     /* src_mask */
1914          0xfffc,                /* dst_mask */
1915          FALSE),                /* pcrel_offset */
1916
1917   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1918   HOWTO (R_PPC64_GOT_TPREL16_HI,
1919          16,                    /* rightshift */
1920          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1921          16,                    /* bitsize */
1922          FALSE,                 /* pc_relative */
1923          0,                     /* bitpos */
1924          complain_overflow_signed, /* complain_on_overflow */
1925          ppc64_elf_unhandled_reloc, /* special_function */
1926          "R_PPC64_GOT_TPREL16_HI", /* name */
1927          FALSE,                 /* partial_inplace */
1928          0,                     /* src_mask */
1929          0xffff,                /* dst_mask */
1930          FALSE),                /* pcrel_offset */
1931
1932   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1933   HOWTO (R_PPC64_GOT_TPREL16_HA,
1934          16,                    /* rightshift */
1935          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1936          16,                    /* bitsize */
1937          FALSE,                 /* pc_relative */
1938          0,                     /* bitpos */
1939          complain_overflow_signed, /* complain_on_overflow */
1940          ppc64_elf_unhandled_reloc, /* special_function */
1941          "R_PPC64_GOT_TPREL16_HA", /* name */
1942          FALSE,                 /* partial_inplace */
1943          0,                     /* src_mask */
1944          0xffff,                /* dst_mask */
1945          FALSE),                /* pcrel_offset */
1946
1947   HOWTO (R_PPC64_JMP_IREL,      /* type */
1948          0,                     /* rightshift */
1949          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1950          0,                     /* bitsize */
1951          FALSE,                 /* pc_relative */
1952          0,                     /* bitpos */
1953          complain_overflow_dont, /* complain_on_overflow */
1954          ppc64_elf_unhandled_reloc, /* special_function */
1955          "R_PPC64_JMP_IREL",    /* name */
1956          FALSE,                 /* partial_inplace */
1957          0,                     /* src_mask */
1958          0,                     /* dst_mask */
1959          FALSE),                /* pcrel_offset */
1960
1961   HOWTO (R_PPC64_IRELATIVE,     /* type */
1962          0,                     /* rightshift */
1963          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1964          64,                    /* bitsize */
1965          FALSE,                 /* pc_relative */
1966          0,                     /* bitpos */
1967          complain_overflow_dont, /* complain_on_overflow */
1968          bfd_elf_generic_reloc, /* special_function */
1969          "R_PPC64_IRELATIVE",   /* name */
1970          FALSE,                 /* partial_inplace */
1971          0,                     /* src_mask */
1972          ONES (64),             /* dst_mask */
1973          FALSE),                /* pcrel_offset */
1974
1975   /* A 16 bit relative relocation.  */
1976   HOWTO (R_PPC64_REL16,         /* type */
1977          0,                     /* rightshift */
1978          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1979          16,                    /* bitsize */
1980          TRUE,                  /* pc_relative */
1981          0,                     /* bitpos */
1982          complain_overflow_signed, /* complain_on_overflow */
1983          bfd_elf_generic_reloc, /* special_function */
1984          "R_PPC64_REL16",       /* name */
1985          FALSE,                 /* partial_inplace */
1986          0,                     /* src_mask */
1987          0xffff,                /* dst_mask */
1988          TRUE),                 /* pcrel_offset */
1989
1990   /* A 16 bit relative relocation without overflow.  */
1991   HOWTO (R_PPC64_REL16_LO,      /* type */
1992          0,                     /* rightshift */
1993          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1994          16,                    /* bitsize */
1995          TRUE,                  /* pc_relative */
1996          0,                     /* bitpos */
1997          complain_overflow_dont,/* complain_on_overflow */
1998          bfd_elf_generic_reloc, /* special_function */
1999          "R_PPC64_REL16_LO",    /* name */
2000          FALSE,                 /* partial_inplace */
2001          0,                     /* src_mask */
2002          0xffff,                /* dst_mask */
2003          TRUE),                 /* pcrel_offset */
2004
2005   /* The high order 16 bits of a relative address.  */
2006   HOWTO (R_PPC64_REL16_HI,      /* type */
2007          16,                    /* rightshift */
2008          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2009          16,                    /* bitsize */
2010          TRUE,                  /* pc_relative */
2011          0,                     /* bitpos */
2012          complain_overflow_signed, /* complain_on_overflow */
2013          bfd_elf_generic_reloc, /* special_function */
2014          "R_PPC64_REL16_HI",    /* name */
2015          FALSE,                 /* partial_inplace */
2016          0,                     /* src_mask */
2017          0xffff,                /* dst_mask */
2018          TRUE),                 /* pcrel_offset */
2019
2020   /* The high order 16 bits of a relative address, plus 1 if the contents of
2021      the low 16 bits, treated as a signed number, is negative.  */
2022   HOWTO (R_PPC64_REL16_HA,      /* type */
2023          16,                    /* rightshift */
2024          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2025          16,                    /* bitsize */
2026          TRUE,                  /* pc_relative */
2027          0,                     /* bitpos */
2028          complain_overflow_signed, /* complain_on_overflow */
2029          ppc64_elf_ha_reloc,    /* special_function */
2030          "R_PPC64_REL16_HA",    /* name */
2031          FALSE,                 /* partial_inplace */
2032          0,                     /* src_mask */
2033          0xffff,                /* dst_mask */
2034          TRUE),                 /* pcrel_offset */
2035
2036   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2037   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2038          16,                    /* rightshift */
2039          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2040          16,                    /* bitsize */
2041          TRUE,                  /* pc_relative */
2042          0,                     /* bitpos */
2043          complain_overflow_signed, /* complain_on_overflow */
2044          ppc64_elf_ha_reloc,    /* special_function */
2045          "R_PPC64_REL16DX_HA",  /* name */
2046          FALSE,                 /* partial_inplace */
2047          0,                     /* src_mask */
2048          0x1fffc1,              /* dst_mask */
2049          TRUE),                 /* pcrel_offset */
2050
2051   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
2052   HOWTO (R_PPC64_16DX_HA,       /* type */
2053          16,                    /* rightshift */
2054          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2055          16,                    /* bitsize */
2056          FALSE,                 /* pc_relative */
2057          0,                     /* bitpos */
2058          complain_overflow_signed, /* complain_on_overflow */
2059          ppc64_elf_ha_reloc,    /* special_function */
2060          "R_PPC64_16DX_HA",     /* name */
2061          FALSE,                 /* partial_inplace */
2062          0,                     /* src_mask */
2063          0x1fffc1,              /* dst_mask */
2064          FALSE),                /* pcrel_offset */
2065
2066   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2067   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2068          16,                    /* rightshift */
2069          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2070          16,                    /* bitsize */
2071          FALSE,                 /* pc_relative */
2072          0,                     /* bitpos */
2073          complain_overflow_dont, /* complain_on_overflow */
2074          bfd_elf_generic_reloc, /* special_function */
2075          "R_PPC64_ADDR16_HIGH", /* name */
2076          FALSE,                 /* partial_inplace */
2077          0,                     /* src_mask */
2078          0xffff,                /* dst_mask */
2079          FALSE),                /* pcrel_offset */
2080
2081   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2082   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2083          16,                    /* rightshift */
2084          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2085          16,                    /* bitsize */
2086          FALSE,                 /* pc_relative */
2087          0,                     /* bitpos */
2088          complain_overflow_dont, /* complain_on_overflow */
2089          ppc64_elf_ha_reloc,    /* special_function */
2090          "R_PPC64_ADDR16_HIGHA",        /* name */
2091          FALSE,                 /* partial_inplace */
2092          0,                     /* src_mask */
2093          0xffff,                /* dst_mask */
2094          FALSE),                /* pcrel_offset */
2095
2096   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2097   HOWTO (R_PPC64_DTPREL16_HIGH,
2098          16,                    /* rightshift */
2099          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2100          16,                    /* bitsize */
2101          FALSE,                 /* pc_relative */
2102          0,                     /* bitpos */
2103          complain_overflow_dont, /* complain_on_overflow */
2104          ppc64_elf_unhandled_reloc, /* special_function */
2105          "R_PPC64_DTPREL16_HIGH", /* name */
2106          FALSE,                 /* partial_inplace */
2107          0,                     /* src_mask */
2108          0xffff,                /* dst_mask */
2109          FALSE),                /* pcrel_offset */
2110
2111   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2112   HOWTO (R_PPC64_DTPREL16_HIGHA,
2113          16,                    /* rightshift */
2114          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2115          16,                    /* bitsize */
2116          FALSE,                 /* pc_relative */
2117          0,                     /* bitpos */
2118          complain_overflow_dont, /* complain_on_overflow */
2119          ppc64_elf_unhandled_reloc, /* special_function */
2120          "R_PPC64_DTPREL16_HIGHA", /* name */
2121          FALSE,                 /* partial_inplace */
2122          0,                     /* src_mask */
2123          0xffff,                /* dst_mask */
2124          FALSE),                /* pcrel_offset */
2125
2126   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2127   HOWTO (R_PPC64_TPREL16_HIGH,
2128          16,                    /* rightshift */
2129          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2130          16,                    /* bitsize */
2131          FALSE,                 /* pc_relative */
2132          0,                     /* bitpos */
2133          complain_overflow_dont, /* complain_on_overflow */
2134          ppc64_elf_unhandled_reloc, /* special_function */
2135          "R_PPC64_TPREL16_HIGH",        /* name */
2136          FALSE,                 /* partial_inplace */
2137          0,                     /* src_mask */
2138          0xffff,                /* dst_mask */
2139          FALSE),                /* pcrel_offset */
2140
2141   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2142   HOWTO (R_PPC64_TPREL16_HIGHA,
2143          16,                    /* rightshift */
2144          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2145          16,                    /* bitsize */
2146          FALSE,                 /* pc_relative */
2147          0,                     /* bitpos */
2148          complain_overflow_dont, /* complain_on_overflow */
2149          ppc64_elf_unhandled_reloc, /* special_function */
2150          "R_PPC64_TPREL16_HIGHA",       /* name */
2151          FALSE,                 /* partial_inplace */
2152          0,                     /* src_mask */
2153          0xffff,                /* dst_mask */
2154          FALSE),                /* pcrel_offset */
2155
2156   /* Marker reloc on ELFv2 large-model function entry.  */
2157   HOWTO (R_PPC64_ENTRY,
2158          0,                     /* rightshift */
2159          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2160          32,                    /* bitsize */
2161          FALSE,                 /* pc_relative */
2162          0,                     /* bitpos */
2163          complain_overflow_dont, /* complain_on_overflow */
2164          bfd_elf_generic_reloc, /* special_function */
2165          "R_PPC64_ENTRY",       /* name */
2166          FALSE,                 /* partial_inplace */
2167          0,                     /* src_mask */
2168          0,                     /* dst_mask */
2169          FALSE),                /* pcrel_offset */
2170
2171   /* Like ADDR64, but use local entry point of function.  */
2172   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2173          0,                     /* rightshift */
2174          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2175          64,                    /* bitsize */
2176          FALSE,                 /* pc_relative */
2177          0,                     /* bitpos */
2178          complain_overflow_dont, /* complain_on_overflow */
2179          bfd_elf_generic_reloc, /* special_function */
2180          "R_PPC64_ADDR64_LOCAL", /* name */
2181          FALSE,                 /* partial_inplace */
2182          0,                     /* src_mask */
2183          ONES (64),             /* dst_mask */
2184          FALSE),                /* pcrel_offset */
2185
2186   /* GNU extension to record C++ vtable hierarchy.  */
2187   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2188          0,                     /* rightshift */
2189          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2190          0,                     /* bitsize */
2191          FALSE,                 /* pc_relative */
2192          0,                     /* bitpos */
2193          complain_overflow_dont, /* complain_on_overflow */
2194          NULL,                  /* special_function */
2195          "R_PPC64_GNU_VTINHERIT", /* name */
2196          FALSE,                 /* partial_inplace */
2197          0,                     /* src_mask */
2198          0,                     /* dst_mask */
2199          FALSE),                /* pcrel_offset */
2200
2201   /* GNU extension to record C++ vtable member usage.  */
2202   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2203          0,                     /* rightshift */
2204          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2205          0,                     /* bitsize */
2206          FALSE,                 /* pc_relative */
2207          0,                     /* bitpos */
2208          complain_overflow_dont, /* complain_on_overflow */
2209          NULL,                  /* special_function */
2210          "R_PPC64_GNU_VTENTRY", /* name */
2211          FALSE,                 /* partial_inplace */
2212          0,                     /* src_mask */
2213          0,                     /* dst_mask */
2214          FALSE),                /* pcrel_offset */
2215 };
2216
2217 \f
2218 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2219    be done.  */
2220
2221 static void
2222 ppc_howto_init (void)
2223 {
2224   unsigned int i, type;
2225
2226   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2227     {
2228       type = ppc64_elf_howto_raw[i].type;
2229       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2230       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2231     }
2232 }
2233
2234 static reloc_howto_type *
2235 ppc64_elf_reloc_type_lookup (bfd *abfd,
2236                              bfd_reloc_code_real_type code)
2237 {
2238   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2239
2240   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2241     /* Initialize howto table if needed.  */
2242     ppc_howto_init ();
2243
2244   switch (code)
2245     {
2246     default:
2247       /* xgettext:c-format */
2248       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2249       bfd_set_error (bfd_error_bad_value);
2250       return NULL;
2251
2252     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2253       break;
2254     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2255       break;
2256     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2257       break;
2258     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2259       break;
2260     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2261       break;
2262     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2263       break;
2264     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2265       break;
2266     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2267       break;
2268     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2269       break;
2270     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2271       break;
2272     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2273       break;
2274     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2275       break;
2276     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2277       break;
2278     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2279       break;
2280     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2281       break;
2282     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2283       break;
2284     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2285       break;
2286     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2287       break;
2288     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2289       break;
2290     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2291       break;
2292     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2293       break;
2294     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2295       break;
2296     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2297       break;
2298     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2299       break;
2300     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2301       break;
2302     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2303       break;
2304     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2305       break;
2306     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2307       break;
2308     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2309       break;
2310     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2311       break;
2312     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2313       break;
2314     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2315       break;
2316     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2317       break;
2318     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2319       break;
2320     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2321       break;
2322     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2323       break;
2324     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2325       break;
2326     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2327       break;
2328     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2329       break;
2330     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2331       break;
2332     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2333       break;
2334     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2335       break;
2336     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2337       break;
2338     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2339       break;
2340     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2341       break;
2342     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2343       break;
2344     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2345       break;
2346     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2347       break;
2348     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2349       break;
2350     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2351       break;
2352     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2353       break;
2354     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2355       break;
2356     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2357       break;
2358     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2359       break;
2360     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2361       break;
2362     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2363       break;
2364     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2365       break;
2366     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2367       break;
2368     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2369       break;
2370     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2371       break;
2372     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2373       break;
2374     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2375       break;
2376     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2377       break;
2378     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2379       break;
2380     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2381       break;
2382     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2383       break;
2384     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2385       break;
2386     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2387       break;
2388     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2389       break;
2390     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2391       break;
2392     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2393       break;
2394     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2395       break;
2396     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2397       break;
2398     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2399       break;
2400     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2401       break;
2402     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2403       break;
2404     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2405       break;
2406     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2407       break;
2408     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2409       break;
2410     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2411       break;
2412     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2413       break;
2414     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2415       break;
2416     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2417       break;
2418     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2419       break;
2420     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2421       break;
2422     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2423       break;
2424     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2425       break;
2426     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2427       break;
2428     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2429       break;
2430     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2431       break;
2432     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2433       break;
2434     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2435       break;
2436     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2437       break;
2438     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2439       break;
2440     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2441       break;
2442     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2443       break;
2444     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2445       break;
2446     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2447       break;
2448     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2449       break;
2450     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2451       break;
2452     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2453       break;
2454     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2455       break;
2456     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2457       break;
2458     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2459       break;
2460     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2461       break;
2462     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2463       break;
2464     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2465       break;
2466     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2467       break;
2468     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2469       break;
2470     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2471       break;
2472     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2473       break;
2474     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
2475       break;
2476     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2477       break;
2478     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2479       break;
2480     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2481       break;
2482     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2483       break;
2484     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2485       break;
2486     }
2487
2488   return ppc64_elf_howto_table[r];
2489 };
2490
2491 static reloc_howto_type *
2492 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2493                              const char *r_name)
2494 {
2495   unsigned int i;
2496
2497   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2498     if (ppc64_elf_howto_raw[i].name != NULL
2499         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2500       return &ppc64_elf_howto_raw[i];
2501
2502   
2503   return NULL;
2504 }
2505
2506 /* Set the howto pointer for a PowerPC ELF reloc.  */
2507
2508 static bfd_boolean
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 (_("%pB: unsupported relocation type %#x"),
2523                           abfd, type);
2524       bfd_set_error (bfd_error_bad_value);
2525       return FALSE;
2526     }
2527   cache_ptr->howto = ppc64_elf_howto_table[type];
2528   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2529     {
2530       /* xgettext:c-format */
2531       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2532                           abfd, type);
2533       bfd_set_error (bfd_error_bad_value);
2534       return FALSE;
2535     }
2536   
2537   return TRUE;
2538 }
2539
2540 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2541
2542 static bfd_reloc_status_type
2543 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2544                     void *data, asection *input_section,
2545                     bfd *output_bfd, char **error_message)
2546 {
2547   enum elf_ppc64_reloc_type r_type;
2548   long insn;
2549   bfd_size_type octets;
2550   bfd_vma value;
2551
2552   /* If this is a relocatable link (output_bfd test tells us), just
2553      call the generic function.  Any adjustment will be done at final
2554      link time.  */
2555   if (output_bfd != NULL)
2556     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2557                                   input_section, output_bfd, error_message);
2558
2559   /* Adjust the addend for sign extension of the low 16 bits.
2560      We won't actually be using the low 16 bits, so trashing them
2561      doesn't matter.  */
2562   reloc_entry->addend += 0x8000;
2563   r_type = reloc_entry->howto->type;
2564   if (r_type != R_PPC64_REL16DX_HA)
2565     return bfd_reloc_continue;
2566
2567   value = 0;
2568   if (!bfd_is_com_section (symbol->section))
2569     value = symbol->value;
2570   value += (reloc_entry->addend
2571             + symbol->section->output_offset
2572             + symbol->section->output_section->vma);
2573   value -= (reloc_entry->address
2574             + input_section->output_offset
2575             + input_section->output_section->vma);
2576   value = (bfd_signed_vma) value >> 16;
2577
2578   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2579   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2580   insn &= ~0x1fffc1;
2581   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2582   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2583   if (value + 0x8000 > 0xffff)
2584     return bfd_reloc_overflow;
2585   return bfd_reloc_ok;
2586 }
2587
2588 static bfd_reloc_status_type
2589 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2590                         void *data, asection *input_section,
2591                         bfd *output_bfd, char **error_message)
2592 {
2593   if (output_bfd != NULL)
2594     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2595                                   input_section, output_bfd, error_message);
2596
2597   if (strcmp (symbol->section->name, ".opd") == 0
2598       && (symbol->section->owner->flags & DYNAMIC) == 0)
2599     {
2600       bfd_vma dest = opd_entry_value (symbol->section,
2601                                       symbol->value + reloc_entry->addend,
2602                                       NULL, NULL, FALSE);
2603       if (dest != (bfd_vma) -1)
2604         reloc_entry->addend = dest - (symbol->value
2605                                       + symbol->section->output_section->vma
2606                                       + symbol->section->output_offset);
2607     }
2608   else
2609     {
2610       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2611
2612       if (symbol->section->owner != abfd
2613           && symbol->section->owner != NULL
2614           && abiversion (symbol->section->owner) >= 2)
2615         {
2616           unsigned int i;
2617
2618           for (i = 0; i < symbol->section->owner->symcount; ++i)
2619             {
2620               asymbol *symdef = symbol->section->owner->outsymbols[i];
2621
2622               if (strcmp (symdef->name, symbol->name) == 0)
2623                 {
2624                   elfsym = (elf_symbol_type *) symdef;
2625                   break;
2626                 }
2627             }
2628         }
2629       reloc_entry->addend
2630         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2631     }
2632   return bfd_reloc_continue;
2633 }
2634
2635 static bfd_reloc_status_type
2636 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2637                          void *data, asection *input_section,
2638                          bfd *output_bfd, char **error_message)
2639 {
2640   long insn;
2641   enum elf_ppc64_reloc_type r_type;
2642   bfd_size_type octets;
2643   /* Assume 'at' branch hints.  */
2644   bfd_boolean is_isa_v2 = TRUE;
2645
2646   /* If this is a relocatable link (output_bfd test tells us), just
2647      call the generic function.  Any adjustment will be done at final
2648      link time.  */
2649   if (output_bfd != NULL)
2650     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2651                                   input_section, output_bfd, error_message);
2652
2653   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2654   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2655   insn &= ~(0x01 << 21);
2656   r_type = reloc_entry->howto->type;
2657   if (r_type == R_PPC64_ADDR14_BRTAKEN
2658       || r_type == R_PPC64_REL14_BRTAKEN)
2659     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2660
2661   if (is_isa_v2)
2662     {
2663       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2664          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2665          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2666       if ((insn & (0x14 << 21)) == (0x04 << 21))
2667         insn |= 0x02 << 21;
2668       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2669         insn |= 0x08 << 21;
2670       else
2671         goto out;
2672     }
2673   else
2674     {
2675       bfd_vma target = 0;
2676       bfd_vma from;
2677
2678       if (!bfd_is_com_section (symbol->section))
2679         target = symbol->value;
2680       target += symbol->section->output_section->vma;
2681       target += symbol->section->output_offset;
2682       target += reloc_entry->addend;
2683
2684       from = (reloc_entry->address
2685               + input_section->output_offset
2686               + input_section->output_section->vma);
2687
2688       /* Invert 'y' bit if not the default.  */
2689       if ((bfd_signed_vma) (target - from) < 0)
2690         insn ^= 0x01 << 21;
2691     }
2692   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2693  out:
2694   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2695                                  input_section, output_bfd, error_message);
2696 }
2697
2698 static bfd_reloc_status_type
2699 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2700                          void *data, asection *input_section,
2701                          bfd *output_bfd, char **error_message)
2702 {
2703   /* If this is a relocatable link (output_bfd test tells us), just
2704      call the generic function.  Any adjustment will be done at final
2705      link time.  */
2706   if (output_bfd != NULL)
2707     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2708                                   input_section, output_bfd, error_message);
2709
2710   /* Subtract the symbol section base address.  */
2711   reloc_entry->addend -= symbol->section->output_section->vma;
2712   return bfd_reloc_continue;
2713 }
2714
2715 static bfd_reloc_status_type
2716 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2717                             void *data, asection *input_section,
2718                             bfd *output_bfd, char **error_message)
2719 {
2720   /* If this is a relocatable link (output_bfd test tells us), just
2721      call the generic function.  Any adjustment will be done at final
2722      link time.  */
2723   if (output_bfd != NULL)
2724     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2725                                   input_section, output_bfd, error_message);
2726
2727   /* Subtract the symbol section base address.  */
2728   reloc_entry->addend -= symbol->section->output_section->vma;
2729
2730   /* Adjust the addend for sign extension of the low 16 bits.  */
2731   reloc_entry->addend += 0x8000;
2732   return bfd_reloc_continue;
2733 }
2734
2735 static bfd_reloc_status_type
2736 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2737                      void *data, asection *input_section,
2738                      bfd *output_bfd, char **error_message)
2739 {
2740   bfd_vma TOCstart;
2741
2742   /* If this is a relocatable link (output_bfd test tells us), just
2743      call the generic function.  Any adjustment will be done at final
2744      link time.  */
2745   if (output_bfd != NULL)
2746     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2747                                   input_section, output_bfd, error_message);
2748
2749   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2750   if (TOCstart == 0)
2751     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2752
2753   /* Subtract the TOC base address.  */
2754   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2755   return bfd_reloc_continue;
2756 }
2757
2758 static bfd_reloc_status_type
2759 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2760                         void *data, asection *input_section,
2761                         bfd *output_bfd, char **error_message)
2762 {
2763   bfd_vma TOCstart;
2764
2765   /* If this is a relocatable link (output_bfd test tells us), just
2766      call the generic function.  Any adjustment will be done at final
2767      link time.  */
2768   if (output_bfd != NULL)
2769     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2770                                   input_section, output_bfd, error_message);
2771
2772   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2773   if (TOCstart == 0)
2774     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2775
2776   /* Subtract the TOC base address.  */
2777   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2778
2779   /* Adjust the addend for sign extension of the low 16 bits.  */
2780   reloc_entry->addend += 0x8000;
2781   return bfd_reloc_continue;
2782 }
2783
2784 static bfd_reloc_status_type
2785 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2786                        void *data, asection *input_section,
2787                        bfd *output_bfd, char **error_message)
2788 {
2789   bfd_vma TOCstart;
2790   bfd_size_type octets;
2791
2792   /* If this is a relocatable link (output_bfd test tells us), just
2793      call the generic function.  Any adjustment will be done at final
2794      link time.  */
2795   if (output_bfd != NULL)
2796     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2797                                   input_section, output_bfd, error_message);
2798
2799   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2800   if (TOCstart == 0)
2801     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2802
2803   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2804   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2805   return bfd_reloc_ok;
2806 }
2807
2808 static bfd_reloc_status_type
2809 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2810                            void *data, asection *input_section,
2811                            bfd *output_bfd, char **error_message)
2812 {
2813   /* If this is a relocatable link (output_bfd test tells us), just
2814      call the generic function.  Any adjustment will be done at final
2815      link time.  */
2816   if (output_bfd != NULL)
2817     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2818                                   input_section, output_bfd, error_message);
2819
2820   if (error_message != NULL)
2821     {
2822       static char buf[60];
2823       sprintf (buf, "generic linker can't handle %s",
2824                reloc_entry->howto->name);
2825       *error_message = buf;
2826     }
2827   return bfd_reloc_dangerous;
2828 }
2829
2830 /* Track GOT entries needed for a given symbol.  We might need more
2831    than one got entry per symbol.  */
2832 struct got_entry
2833 {
2834   struct got_entry *next;
2835
2836   /* The symbol addend that we'll be placing in the GOT.  */
2837   bfd_vma addend;
2838
2839   /* Unlike other ELF targets, we use separate GOT entries for the same
2840      symbol referenced from different input files.  This is to support
2841      automatic multiple TOC/GOT sections, where the TOC base can vary
2842      from one input file to another.  After partitioning into TOC groups
2843      we merge entries within the group.
2844
2845      Point to the BFD owning this GOT entry.  */
2846   bfd *owner;
2847
2848   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2849      TLS_TPREL or TLS_DTPREL for tls entries.  */
2850   unsigned char tls_type;
2851
2852   /* Non-zero if got.ent points to real entry.  */
2853   unsigned char is_indirect;
2854
2855   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2856   union
2857     {
2858       bfd_signed_vma refcount;
2859       bfd_vma offset;
2860       struct got_entry *ent;
2861     } got;
2862 };
2863
2864 /* The same for PLT.  */
2865 struct plt_entry
2866 {
2867   struct plt_entry *next;
2868
2869   bfd_vma addend;
2870
2871   union
2872     {
2873       bfd_signed_vma refcount;
2874       bfd_vma offset;
2875     } plt;
2876 };
2877
2878 struct ppc64_elf_obj_tdata
2879 {
2880   struct elf_obj_tdata elf;
2881
2882   /* Shortcuts to dynamic linker sections.  */
2883   asection *got;
2884   asection *relgot;
2885
2886   /* Used during garbage collection.  We attach global symbols defined
2887      on removed .opd entries to this section so that the sym is removed.  */
2888   asection *deleted_section;
2889
2890   /* TLS local dynamic got entry handling.  Support for multiple GOT
2891      sections means we potentially need one of these for each input bfd.  */
2892   struct got_entry tlsld_got;
2893
2894   union {
2895     /* A copy of relocs before they are modified for --emit-relocs.  */
2896     Elf_Internal_Rela *relocs;
2897
2898     /* Section contents.  */
2899     bfd_byte *contents;
2900   } opd;
2901
2902   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2903      the reloc to be in the range -32768 to 32767.  */
2904   unsigned int has_small_toc_reloc : 1;
2905
2906   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2907      instruction not one we handle.  */
2908   unsigned int unexpected_toc_insn : 1;
2909 };
2910
2911 #define ppc64_elf_tdata(bfd) \
2912   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2913
2914 #define ppc64_tlsld_got(bfd) \
2915   (&ppc64_elf_tdata (bfd)->tlsld_got)
2916
2917 #define is_ppc64_elf(bfd) \
2918   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2919    && elf_object_id (bfd) == PPC64_ELF_DATA)
2920
2921 /* Override the generic function because we store some extras.  */
2922
2923 static bfd_boolean
2924 ppc64_elf_mkobject (bfd *abfd)
2925 {
2926   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2927                                   PPC64_ELF_DATA);
2928 }
2929
2930 /* Fix bad default arch selected for a 64 bit input bfd when the
2931    default is 32 bit.  Also select arch based on apuinfo.  */
2932
2933 static bfd_boolean
2934 ppc64_elf_object_p (bfd *abfd)
2935 {
2936   if (!abfd->arch_info->the_default)
2937     return TRUE;
2938
2939   if (abfd->arch_info->bits_per_word == 32)
2940     {
2941       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2942
2943       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2944         {
2945           /* Relies on arch after 32 bit default being 64 bit default.  */
2946           abfd->arch_info = abfd->arch_info->next;
2947           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2948         }
2949     }
2950   return _bfd_elf_ppc_set_arch (abfd);
2951 }
2952
2953 /* Support for core dump NOTE sections.  */
2954
2955 static bfd_boolean
2956 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2957 {
2958   size_t offset, size;
2959
2960   if (note->descsz != 504)
2961     return FALSE;
2962
2963   /* pr_cursig */
2964   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2965
2966   /* pr_pid */
2967   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2968
2969   /* pr_reg */
2970   offset = 112;
2971   size = 384;
2972
2973   /* Make a ".reg/999" section.  */
2974   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2975                                           size, note->descpos + offset);
2976 }
2977
2978 static bfd_boolean
2979 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2980 {
2981   if (note->descsz != 136)
2982     return FALSE;
2983
2984   elf_tdata (abfd)->core->pid
2985     = bfd_get_32 (abfd, note->descdata + 24);
2986   elf_tdata (abfd)->core->program
2987     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2988   elf_tdata (abfd)->core->command
2989     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2990
2991   return TRUE;
2992 }
2993
2994 static char *
2995 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2996                            ...)
2997 {
2998   switch (note_type)
2999     {
3000     default:
3001       return NULL;
3002
3003     case NT_PRPSINFO:
3004       {
3005         char data[136];
3006         va_list ap;
3007
3008         va_start (ap, note_type);
3009         memset (data, 0, sizeof (data));
3010         strncpy (data + 40, va_arg (ap, const char *), 16);
3011         strncpy (data + 56, va_arg (ap, const char *), 80);
3012         va_end (ap);
3013         return elfcore_write_note (abfd, buf, bufsiz,
3014                                    "CORE", note_type, data, sizeof (data));
3015       }
3016
3017     case NT_PRSTATUS:
3018       {
3019         char data[504];
3020         va_list ap;
3021         long pid;
3022         int cursig;
3023         const void *greg;
3024
3025         va_start (ap, note_type);
3026         memset (data, 0, 112);
3027         pid = va_arg (ap, long);
3028         bfd_put_32 (abfd, pid, data + 32);
3029         cursig = va_arg (ap, int);
3030         bfd_put_16 (abfd, cursig, data + 12);
3031         greg = va_arg (ap, const void *);
3032         memcpy (data + 112, greg, 384);
3033         memset (data + 496, 0, 8);
3034         va_end (ap);
3035         return elfcore_write_note (abfd, buf, bufsiz,
3036                                    "CORE", note_type, data, sizeof (data));
3037       }
3038     }
3039 }
3040
3041 /* Add extra PPC sections.  */
3042
3043 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3044 {
3045   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3046   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3047   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3048   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3049   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3050   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3051   { NULL,                     0,  0, 0,            0 }
3052 };
3053
3054 enum _ppc64_sec_type {
3055   sec_normal = 0,
3056   sec_opd = 1,
3057   sec_toc = 2
3058 };
3059
3060 struct _ppc64_elf_section_data
3061 {
3062   struct bfd_elf_section_data elf;
3063
3064   union
3065   {
3066     /* An array with one entry for each opd function descriptor,
3067        and some spares since opd entries may be either 16 or 24 bytes.  */
3068 #define OPD_NDX(OFF) ((OFF) >> 4)
3069     struct _opd_sec_data
3070     {
3071       /* Points to the function code section for local opd entries.  */
3072       asection **func_sec;
3073
3074       /* After editing .opd, adjust references to opd local syms.  */
3075       long *adjust;
3076     } opd;
3077
3078     /* An array for toc sections, indexed by offset/8.  */
3079     struct _toc_sec_data
3080     {
3081       /* Specifies the relocation symbol index used at a given toc offset.  */
3082       unsigned *symndx;
3083
3084       /* And the relocation addend.  */
3085       bfd_vma *add;
3086     } toc;
3087   } u;
3088
3089   enum _ppc64_sec_type sec_type:2;
3090
3091   /* Flag set when small branches are detected.  Used to
3092      select suitable defaults for the stub group size.  */
3093   unsigned int has_14bit_branch:1;
3094 };
3095
3096 #define ppc64_elf_section_data(sec) \
3097   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3098
3099 static bfd_boolean
3100 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3101 {
3102   if (!sec->used_by_bfd)
3103     {
3104       struct _ppc64_elf_section_data *sdata;
3105       bfd_size_type amt = sizeof (*sdata);
3106
3107       sdata = bfd_zalloc (abfd, amt);
3108       if (sdata == NULL)
3109         return FALSE;
3110       sec->used_by_bfd = sdata;
3111     }
3112
3113   return _bfd_elf_new_section_hook (abfd, sec);
3114 }
3115
3116 static struct _opd_sec_data *
3117 get_opd_info (asection * sec)
3118 {
3119   if (sec != NULL
3120       && ppc64_elf_section_data (sec) != NULL
3121       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3122     return &ppc64_elf_section_data (sec)->u.opd;
3123   return NULL;
3124 }
3125 \f
3126 /* Parameters for the qsort hook.  */
3127 static bfd_boolean synthetic_relocatable;
3128 static asection *synthetic_opd;
3129
3130 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3131
3132 static int
3133 compare_symbols (const void *ap, const void *bp)
3134 {
3135   const asymbol *a = * (const asymbol **) ap;
3136   const asymbol *b = * (const asymbol **) bp;
3137
3138   /* Section symbols first.  */
3139   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3140     return -1;
3141   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3142     return 1;
3143
3144   /* then .opd symbols.  */
3145   if (synthetic_opd != NULL)
3146     {
3147       if (strcmp (a->section->name, ".opd") == 0
3148           && strcmp (b->section->name, ".opd") != 0)
3149         return -1;
3150       if (strcmp (a->section->name, ".opd") != 0
3151           && strcmp (b->section->name, ".opd") == 0)
3152         return 1;
3153     }
3154
3155   /* then other code symbols.  */
3156   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3157       == (SEC_CODE | SEC_ALLOC)
3158       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3159          != (SEC_CODE | SEC_ALLOC))
3160     return -1;
3161
3162   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3163       != (SEC_CODE | SEC_ALLOC)
3164       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3165          == (SEC_CODE | SEC_ALLOC))
3166     return 1;
3167
3168   if (synthetic_relocatable)
3169     {
3170       if (a->section->id < b->section->id)
3171         return -1;
3172
3173       if (a->section->id > b->section->id)
3174         return 1;
3175     }
3176
3177   if (a->value + a->section->vma < b->value + b->section->vma)
3178     return -1;
3179
3180   if (a->value + a->section->vma > b->value + b->section->vma)
3181     return 1;
3182
3183   /* For syms with the same value, prefer strong dynamic global function
3184      syms over other syms.  */
3185   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3186     return -1;
3187
3188   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3189     return 1;
3190
3191   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3192     return -1;
3193
3194   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3195     return 1;
3196
3197   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3198     return -1;
3199
3200   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3201     return 1;
3202
3203   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3204     return -1;
3205
3206   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3207     return 1;
3208
3209   return a > b;
3210 }
3211
3212 /* Search SYMS for a symbol of the given VALUE.  */
3213
3214 static asymbol *
3215 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3216 {
3217   long mid;
3218
3219   if (id == (unsigned) -1)
3220     {
3221       while (lo < hi)
3222         {
3223           mid = (lo + hi) >> 1;
3224           if (syms[mid]->value + syms[mid]->section->vma < value)
3225             lo = mid + 1;
3226           else if (syms[mid]->value + syms[mid]->section->vma > value)
3227             hi = mid;
3228           else
3229             return syms[mid];
3230         }
3231     }
3232   else
3233     {
3234       while (lo < hi)
3235         {
3236           mid = (lo + hi) >> 1;
3237           if (syms[mid]->section->id < id)
3238             lo = mid + 1;
3239           else if (syms[mid]->section->id > id)
3240             hi = mid;
3241           else if (syms[mid]->value < value)
3242             lo = mid + 1;
3243           else if (syms[mid]->value > value)
3244             hi = mid;
3245           else
3246             return syms[mid];
3247         }
3248     }
3249   return NULL;
3250 }
3251
3252 static bfd_boolean
3253 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3254 {
3255   bfd_vma vma = *(bfd_vma *) ptr;
3256   return ((section->flags & SEC_ALLOC) != 0
3257           && section->vma <= vma
3258           && vma < section->vma + section->size);
3259 }
3260
3261 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3262    entry syms.  Also generate @plt symbols for the glink branch table.
3263    Returns count of synthetic symbols in RET or -1 on error.  */
3264
3265 static long
3266 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3267                                 long static_count, asymbol **static_syms,
3268                                 long dyn_count, asymbol **dyn_syms,
3269                                 asymbol **ret)
3270 {
3271   asymbol *s;
3272   size_t i, j, count;
3273   char *names;
3274   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3275   asection *opd = NULL;
3276   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3277   asymbol **syms;
3278   int abi = abiversion (abfd);
3279
3280   *ret = NULL;
3281
3282   if (abi < 2)
3283     {
3284       opd = bfd_get_section_by_name (abfd, ".opd");
3285       if (opd == NULL && abi == 1)
3286         return 0;
3287     }
3288
3289   syms = NULL;
3290   codesecsym = 0;
3291   codesecsymend = 0;
3292   secsymend = 0;
3293   opdsymend = 0;
3294   symcount = 0;
3295   if (opd != NULL)
3296     {
3297       symcount = static_count;
3298       if (!relocatable)
3299         symcount += dyn_count;
3300       if (symcount == 0)
3301         return 0;
3302
3303       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3304       if (syms == NULL)
3305         return -1;
3306
3307       if (!relocatable && static_count != 0 && dyn_count != 0)
3308         {
3309           /* Use both symbol tables.  */
3310           memcpy (syms, static_syms, static_count * sizeof (*syms));
3311           memcpy (syms + static_count, dyn_syms,
3312                   (dyn_count + 1) * sizeof (*syms));
3313         }
3314       else if (!relocatable && static_count == 0)
3315         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3316       else
3317         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3318
3319       /* Trim uninteresting symbols.  Interesting symbols are section,
3320          function, and notype symbols.  */
3321       for (i = 0, j = 0; i < symcount; ++i)
3322         if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3323                                | BSF_RELC | BSF_SRELC)) == 0)
3324           syms[j++] = syms[i];
3325       symcount = j;
3326
3327       synthetic_relocatable = relocatable;
3328       synthetic_opd = opd;
3329       qsort (syms, symcount, sizeof (*syms), compare_symbols);
3330
3331       if (!relocatable && symcount > 1)
3332         {
3333           /* Trim duplicate syms, since we may have merged the normal and
3334              dynamic symbols.  Actually, we only care about syms that have
3335              different values, so trim any with the same value.  */
3336           for (i = 1, j = 1; i < symcount; ++i)
3337             if (syms[i - 1]->value + syms[i - 1]->section->vma
3338                 != syms[i]->value + syms[i]->section->vma)
3339               syms[j++] = syms[i];
3340           symcount = j;
3341         }
3342
3343       i = 0;
3344       /* Note that here and in compare_symbols we can't compare opd and
3345          sym->section directly.  With separate debug info files, the
3346          symbols will be extracted from the debug file while abfd passed
3347          to this function is the real binary.  */
3348       if (strcmp (syms[i]->section->name, ".opd") == 0)
3349         ++i;
3350       codesecsym = i;
3351
3352       for (; i < symcount; ++i)
3353         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3354                                          | SEC_THREAD_LOCAL))
3355              != (SEC_CODE | SEC_ALLOC))
3356             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3357           break;
3358       codesecsymend = i;
3359
3360       for (; i < symcount; ++i)
3361         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3362           break;
3363       secsymend = i;
3364
3365       for (; i < symcount; ++i)
3366         if (strcmp (syms[i]->section->name, ".opd") != 0)
3367           break;
3368       opdsymend = i;
3369
3370       for (; i < symcount; ++i)
3371         if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3372             != (SEC_CODE | SEC_ALLOC))
3373           break;
3374       symcount = i;
3375     }
3376   count = 0;
3377
3378   if (relocatable)
3379     {
3380       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3381       arelent *r;
3382       size_t size;
3383       size_t relcount;
3384
3385       if (opdsymend == secsymend)
3386         goto done;
3387
3388       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3389       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3390       if (relcount == 0)
3391         goto done;
3392
3393       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3394         {
3395           count = -1;
3396           goto done;
3397         }
3398
3399       size = 0;
3400       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3401         {
3402           asymbol *sym;
3403
3404           while (r < opd->relocation + relcount
3405                  && r->address < syms[i]->value + opd->vma)
3406             ++r;
3407
3408           if (r == opd->relocation + relcount)
3409             break;
3410
3411           if (r->address != syms[i]->value + opd->vma)
3412             continue;
3413
3414           if (r->howto->type != R_PPC64_ADDR64)
3415             continue;
3416
3417           sym = *r->sym_ptr_ptr;
3418           if (!sym_exists_at (syms, opdsymend, symcount,
3419                               sym->section->id, sym->value + r->addend))
3420             {
3421               ++count;
3422               size += sizeof (asymbol);
3423               size += strlen (syms[i]->name) + 2;
3424             }
3425         }
3426
3427       if (size == 0)
3428         goto done;
3429       s = *ret = bfd_malloc (size);
3430       if (s == NULL)
3431         {
3432           count = -1;
3433           goto done;
3434         }
3435
3436       names = (char *) (s + count);
3437
3438       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3439         {
3440           asymbol *sym;
3441
3442           while (r < opd->relocation + relcount
3443                  && r->address < syms[i]->value + opd->vma)
3444             ++r;
3445
3446           if (r == opd->relocation + relcount)
3447             break;
3448
3449           if (r->address != syms[i]->value + opd->vma)
3450             continue;
3451
3452           if (r->howto->type != R_PPC64_ADDR64)
3453             continue;
3454
3455           sym = *r->sym_ptr_ptr;
3456           if (!sym_exists_at (syms, opdsymend, symcount,
3457                               sym->section->id, sym->value + r->addend))
3458             {
3459               size_t len;
3460
3461               *s = *syms[i];
3462               s->flags |= BSF_SYNTHETIC;
3463               s->section = sym->section;
3464               s->value = sym->value + r->addend;
3465               s->name = names;
3466               *names++ = '.';
3467               len = strlen (syms[i]->name);
3468               memcpy (names, syms[i]->name, len + 1);
3469               names += len + 1;
3470               /* Have udata.p point back to the original symbol this
3471                  synthetic symbol was derived from.  */
3472               s->udata.p = syms[i];
3473               s++;
3474             }
3475         }
3476     }
3477   else
3478     {
3479       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3480       bfd_byte *contents = NULL;
3481       size_t size;
3482       size_t plt_count = 0;
3483       bfd_vma glink_vma = 0, resolv_vma = 0;
3484       asection *dynamic, *glink = NULL, *relplt = NULL;
3485       arelent *p;
3486
3487       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3488         {
3489         free_contents_and_exit_err:
3490           count = -1;
3491         free_contents_and_exit:
3492           if (contents)
3493             free (contents);
3494           goto done;
3495         }
3496
3497       size = 0;
3498       for (i = secsymend; i < opdsymend; ++i)
3499         {
3500           bfd_vma ent;
3501
3502           /* Ignore bogus symbols.  */
3503           if (syms[i]->value > opd->size - 8)
3504             continue;
3505
3506           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3507           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3508             {
3509               ++count;
3510               size += sizeof (asymbol);
3511               size += strlen (syms[i]->name) + 2;
3512             }
3513         }
3514
3515       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3516       if (dyn_count != 0
3517           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3518         {
3519           bfd_byte *dynbuf, *extdyn, *extdynend;
3520           size_t extdynsize;
3521           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3522
3523           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3524             goto free_contents_and_exit_err;
3525
3526           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3527           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3528
3529           extdyn = dynbuf;
3530           extdynend = extdyn + dynamic->size;
3531           for (; extdyn < extdynend; extdyn += extdynsize)
3532             {
3533               Elf_Internal_Dyn dyn;
3534               (*swap_dyn_in) (abfd, extdyn, &dyn);
3535
3536               if (dyn.d_tag == DT_NULL)
3537                 break;
3538
3539               if (dyn.d_tag == DT_PPC64_GLINK)
3540                 {
3541                   /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3542                      See comment in ppc64_elf_finish_dynamic_sections. */
3543                   glink_vma = dyn.d_un.d_val + 8 * 4;
3544                   /* The .glink section usually does not survive the final
3545                      link; search for the section (usually .text) where the
3546                      glink stubs now reside.  */
3547                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3548                                                 &glink_vma);
3549                   break;
3550                 }
3551             }
3552
3553           free (dynbuf);
3554         }
3555
3556       if (glink != NULL)
3557         {
3558           /* Determine __glink trampoline by reading the relative branch
3559              from the first glink stub.  */
3560           bfd_byte buf[4];
3561           unsigned int off = 0;
3562
3563           while (bfd_get_section_contents (abfd, glink, buf,
3564                                            glink_vma + off - glink->vma, 4))
3565             {
3566               unsigned int insn = bfd_get_32 (abfd, buf);
3567               insn ^= B_DOT;
3568               if ((insn & ~0x3fffffc) == 0)
3569                 {
3570                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3571                   break;
3572                 }
3573               off += 4;
3574               if (off > 4)
3575                 break;
3576             }
3577
3578           if (resolv_vma)
3579             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3580
3581           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3582           if (relplt != NULL)
3583             {
3584               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3585               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3586                 goto free_contents_and_exit_err;
3587
3588               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3589               size += plt_count * sizeof (asymbol);
3590
3591               p = relplt->relocation;
3592               for (i = 0; i < plt_count; i++, p++)
3593                 {
3594                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3595                   if (p->addend != 0)
3596                     size += sizeof ("+0x") - 1 + 16;
3597                 }
3598             }
3599         }
3600
3601       if (size == 0)
3602         goto free_contents_and_exit;
3603       s = *ret = bfd_malloc (size);
3604       if (s == NULL)
3605         goto free_contents_and_exit_err;
3606
3607       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3608
3609       for (i = secsymend; i < opdsymend; ++i)
3610         {
3611           bfd_vma ent;
3612
3613           if (syms[i]->value > opd->size - 8)
3614             continue;
3615
3616           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3617           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3618             {
3619               size_t lo, hi;
3620               size_t len;
3621               asection *sec = abfd->sections;
3622
3623               *s = *syms[i];
3624               lo = codesecsym;
3625               hi = codesecsymend;
3626               while (lo < hi)
3627                 {
3628                   size_t mid = (lo + hi) >> 1;
3629                   if (syms[mid]->section->vma < ent)
3630                     lo = mid + 1;
3631                   else if (syms[mid]->section->vma > ent)
3632                     hi = mid;
3633                   else
3634                     {
3635                       sec = syms[mid]->section;
3636                       break;
3637                     }
3638                 }
3639
3640               if (lo >= hi && lo > codesecsym)
3641                 sec = syms[lo - 1]->section;
3642
3643               for (; sec != NULL; sec = sec->next)
3644                 {
3645                   if (sec->vma > ent)
3646                     break;
3647                   /* SEC_LOAD may not be set if SEC is from a separate debug
3648                      info file.  */
3649                   if ((sec->flags & SEC_ALLOC) == 0)
3650                     break;
3651                   if ((sec->flags & SEC_CODE) != 0)
3652                     s->section = sec;
3653                 }
3654               s->flags |= BSF_SYNTHETIC;
3655               s->value = ent - s->section->vma;
3656               s->name = names;
3657               *names++ = '.';
3658               len = strlen (syms[i]->name);
3659               memcpy (names, syms[i]->name, len + 1);
3660               names += len + 1;
3661               /* Have udata.p point back to the original symbol this
3662                  synthetic symbol was derived from.  */
3663               s->udata.p = syms[i];
3664               s++;
3665             }
3666         }
3667       free (contents);
3668
3669       if (glink != NULL && relplt != NULL)
3670         {
3671           if (resolv_vma)
3672             {
3673               /* Add a symbol for the main glink trampoline.  */
3674               memset (s, 0, sizeof *s);
3675               s->the_bfd = abfd;
3676               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3677               s->section = glink;
3678               s->value = resolv_vma - glink->vma;
3679               s->name = names;
3680               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3681               names += sizeof ("__glink_PLTresolve");
3682               s++;
3683               count++;
3684             }
3685
3686           /* FIXME: It would be very much nicer to put sym@plt on the
3687              stub rather than on the glink branch table entry.  The
3688              objdump disassembler would then use a sensible symbol
3689              name on plt calls.  The difficulty in doing so is
3690              a) finding the stubs, and,
3691              b) matching stubs against plt entries, and,
3692              c) there can be multiple stubs for a given plt entry.
3693
3694              Solving (a) could be done by code scanning, but older
3695              ppc64 binaries used different stubs to current code.
3696              (b) is the tricky one since you need to known the toc
3697              pointer for at least one function that uses a pic stub to
3698              be able to calculate the plt address referenced.
3699              (c) means gdb would need to set multiple breakpoints (or
3700              find the glink branch itself) when setting breakpoints
3701              for pending shared library loads.  */
3702           p = relplt->relocation;
3703           for (i = 0; i < plt_count; i++, p++)
3704             {
3705               size_t len;
3706
3707               *s = **p->sym_ptr_ptr;
3708               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3709                  we are defining a symbol, ensure one of them is set.  */
3710               if ((s->flags & BSF_LOCAL) == 0)
3711                 s->flags |= BSF_GLOBAL;
3712               s->flags |= BSF_SYNTHETIC;
3713               s->section = glink;
3714               s->value = glink_vma - glink->vma;
3715               s->name = names;
3716               s->udata.p = NULL;
3717               len = strlen ((*p->sym_ptr_ptr)->name);
3718               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3719               names += len;
3720               if (p->addend != 0)
3721                 {
3722                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3723                   names += sizeof ("+0x") - 1;
3724                   bfd_sprintf_vma (abfd, names, p->addend);
3725                   names += strlen (names);
3726                 }
3727               memcpy (names, "@plt", sizeof ("@plt"));
3728               names += sizeof ("@plt");
3729               s++;
3730               if (abi < 2)
3731                 {
3732                   glink_vma += 8;
3733                   if (i >= 0x8000)
3734                     glink_vma += 4;
3735                 }
3736               else
3737                 glink_vma += 4;
3738             }
3739           count += plt_count;
3740         }
3741     }
3742
3743  done:
3744   free (syms);
3745   return count;
3746 }
3747 \f
3748 /* The following functions are specific to the ELF linker, while
3749    functions above are used generally.  Those named ppc64_elf_* are
3750    called by the main ELF linker code.  They appear in this file more
3751    or less in the order in which they are called.  eg.
3752    ppc64_elf_check_relocs is called early in the link process,
3753    ppc64_elf_finish_dynamic_sections is one of the last functions
3754    called.
3755
3756    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3757    functions have both a function code symbol and a function descriptor
3758    symbol.  A call to foo in a relocatable object file looks like:
3759
3760    .            .text
3761    .    x:
3762    .            bl      .foo
3763    .            nop
3764
3765    The function definition in another object file might be:
3766
3767    .            .section .opd
3768    .    foo:    .quad   .foo
3769    .            .quad   .TOC.@tocbase
3770    .            .quad   0
3771    .
3772    .            .text
3773    .    .foo:   blr
3774
3775    When the linker resolves the call during a static link, the branch
3776    unsurprisingly just goes to .foo and the .opd information is unused.
3777    If the function definition is in a shared library, things are a little
3778    different:  The call goes via a plt call stub, the opd information gets
3779    copied to the plt, and the linker patches the nop.
3780
3781    .    x:
3782    .            bl      .foo_stub
3783    .            ld      2,40(1)
3784    .
3785    .
3786    .    .foo_stub:
3787    .            std     2,40(1)                 # in practice, the call stub
3788    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3789    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3790    .            ld      12,0(11)
3791    .            ld      2,8(11)
3792    .            mtctr   12
3793    .            ld      11,16(11)
3794    .            bctr
3795    .
3796    .            .section .plt
3797    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3798
3799    The "reloc ()" notation is supposed to indicate that the linker emits
3800    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3801    copying.
3802
3803    What are the difficulties here?  Well, firstly, the relocations
3804    examined by the linker in check_relocs are against the function code
3805    sym .foo, while the dynamic relocation in the plt is emitted against
3806    the function descriptor symbol, foo.  Somewhere along the line, we need
3807    to carefully copy dynamic link information from one symbol to the other.
3808    Secondly, the generic part of the elf linker will make .foo a dynamic
3809    symbol as is normal for most other backends.  We need foo dynamic
3810    instead, at least for an application final link.  However, when
3811    creating a shared library containing foo, we need to have both symbols
3812    dynamic so that references to .foo are satisfied during the early
3813    stages of linking.  Otherwise the linker might decide to pull in a
3814    definition from some other object, eg. a static library.
3815
3816    Update: As of August 2004, we support a new convention.  Function
3817    calls may use the function descriptor symbol, ie. "bl foo".  This
3818    behaves exactly as "bl .foo".  */
3819
3820 /* Of those relocs that might be copied as dynamic relocs, this
3821    function selects those that must be copied when linking a shared
3822    library or PIE, even when the symbol is local.  */
3823
3824 static int
3825 must_be_dyn_reloc (struct bfd_link_info *info,
3826                    enum elf_ppc64_reloc_type r_type)
3827 {
3828   switch (r_type)
3829     {
3830     default:
3831       /* Only relative relocs can be resolved when the object load
3832          address isn't fixed.  DTPREL64 is excluded because the
3833          dynamic linker needs to differentiate global dynamic from
3834          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
3835       return 1;
3836
3837     case R_PPC64_REL32:
3838     case R_PPC64_REL64:
3839     case R_PPC64_REL30:
3840       return 0;
3841
3842     case R_PPC64_TPREL16:
3843     case R_PPC64_TPREL16_LO:
3844     case R_PPC64_TPREL16_HI:
3845     case R_PPC64_TPREL16_HA:
3846     case R_PPC64_TPREL16_DS:
3847     case R_PPC64_TPREL16_LO_DS:
3848     case R_PPC64_TPREL16_HIGH:
3849     case R_PPC64_TPREL16_HIGHA:
3850     case R_PPC64_TPREL16_HIGHER:
3851     case R_PPC64_TPREL16_HIGHERA:
3852     case R_PPC64_TPREL16_HIGHEST:
3853     case R_PPC64_TPREL16_HIGHESTA:
3854     case R_PPC64_TPREL64:
3855       /* These relocations are relative but in a shared library the
3856          linker doesn't know the thread pointer base.  */
3857       return bfd_link_dll (info);
3858     }
3859 }
3860
3861 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3862    copying dynamic variables from a shared lib into an app's dynbss
3863    section, and instead use a dynamic relocation to point into the
3864    shared lib.  With code that gcc generates, it's vital that this be
3865    enabled;  In the PowerPC64 ABI, the address of a function is actually
3866    the address of a function descriptor, which resides in the .opd
3867    section.  gcc uses the descriptor directly rather than going via the
3868    GOT as some other ABI's do, which means that initialized function
3869    pointers must reference the descriptor.  Thus, a function pointer
3870    initialized to the address of a function in a shared library will
3871    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3872    redefines the function descriptor symbol to point to the copy.  This
3873    presents a problem as a plt entry for that function is also
3874    initialized from the function descriptor symbol and the copy reloc
3875    may not be initialized first.  */
3876 #define ELIMINATE_COPY_RELOCS 1
3877
3878 /* Section name for stubs is the associated section name plus this
3879    string.  */
3880 #define STUB_SUFFIX ".stub"
3881
3882 /* Linker stubs.
3883    ppc_stub_long_branch:
3884    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3885    destination, but a 24 bit branch in a stub section will reach.
3886    .    b       dest
3887
3888    ppc_stub_plt_branch:
3889    Similar to the above, but a 24 bit branch in the stub section won't
3890    reach its destination.
3891    .    addis   %r11,%r2,xxx@toc@ha
3892    .    ld      %r12,xxx@toc@l(%r11)
3893    .    mtctr   %r12
3894    .    bctr
3895
3896    ppc_stub_plt_call:
3897    Used to call a function in a shared library.  If it so happens that
3898    the plt entry referenced crosses a 64k boundary, then an extra
3899    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3900    .    std     %r2,40(%r1)
3901    .    addis   %r11,%r2,xxx@toc@ha
3902    .    ld      %r12,xxx+0@toc@l(%r11)
3903    .    mtctr   %r12
3904    .    ld      %r2,xxx+8@toc@l(%r11)
3905    .    ld      %r11,xxx+16@toc@l(%r11)
3906    .    bctr
3907
3908    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3909    code to adjust the value and save r2 to support multiple toc sections.
3910    A ppc_stub_long_branch with an r2 offset looks like:
3911    .    std     %r2,40(%r1)
3912    .    addis   %r2,%r2,off@ha
3913    .    addi    %r2,%r2,off@l
3914    .    b       dest
3915
3916    A ppc_stub_plt_branch with an r2 offset looks like:
3917    .    std     %r2,40(%r1)
3918    .    addis   %r11,%r2,xxx@toc@ha
3919    .    ld      %r12,xxx@toc@l(%r11)
3920    .    addis   %r2,%r2,off@ha
3921    .    addi    %r2,%r2,off@l
3922    .    mtctr   %r12
3923    .    bctr
3924
3925    In cases where the "addis" instruction would add zero, the "addis" is
3926    omitted and following instructions modified slightly in some cases.
3927 */
3928
3929 enum ppc_stub_type {
3930   ppc_stub_none,
3931   ppc_stub_long_branch,
3932   ppc_stub_long_branch_r2off,
3933   ppc_stub_plt_branch,
3934   ppc_stub_plt_branch_r2off,
3935   ppc_stub_plt_call,
3936   ppc_stub_plt_call_r2save,
3937   ppc_stub_global_entry,
3938   ppc_stub_save_res
3939 };
3940
3941 /* Information on stub grouping.  */
3942 struct map_stub
3943 {
3944   /* The stub section.  */
3945   asection *stub_sec;
3946   /* This is the section to which stubs in the group will be attached.  */
3947   asection *link_sec;
3948   /* Next group.  */
3949   struct map_stub *next;
3950   /* Whether to emit a copy of register save/restore functions in this
3951      group.  */
3952   int needs_save_res;
3953   /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3954      or -1u if no such stub with bctrl exists.  */
3955   unsigned int tls_get_addr_opt_bctrl;
3956 };
3957
3958 struct ppc_stub_hash_entry {
3959
3960   /* Base hash table entry structure.  */
3961   struct bfd_hash_entry root;
3962
3963   enum ppc_stub_type stub_type;
3964
3965   /* Group information.  */
3966   struct map_stub *group;
3967
3968   /* Offset within stub_sec of the beginning of this stub.  */
3969   bfd_vma stub_offset;
3970
3971   /* Given the symbol's value and its section we can determine its final
3972      value when building the stubs (so the stub knows where to jump.  */
3973   bfd_vma target_value;
3974   asection *target_section;
3975
3976   /* The symbol table entry, if any, that this was derived from.  */
3977   struct ppc_link_hash_entry *h;
3978   struct plt_entry *plt_ent;
3979
3980   /* Symbol st_other.  */
3981   unsigned char other;
3982 };
3983
3984 struct ppc_branch_hash_entry {
3985
3986   /* Base hash table entry structure.  */
3987   struct bfd_hash_entry root;
3988
3989   /* Offset within branch lookup table.  */
3990   unsigned int offset;
3991
3992   /* Generation marker.  */
3993   unsigned int iter;
3994 };
3995
3996 /* Used to track dynamic relocations for local symbols.  */
3997 struct ppc_dyn_relocs
3998 {
3999   struct ppc_dyn_relocs *next;
4000
4001   /* The input section of the reloc.  */
4002   asection *sec;
4003
4004   /* Total number of relocs copied for the input section.  */
4005   unsigned int count : 31;
4006
4007   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
4008   unsigned int ifunc : 1;
4009 };
4010
4011 struct ppc_link_hash_entry
4012 {
4013   struct elf_link_hash_entry elf;
4014
4015   union {
4016     /* A pointer to the most recently used stub hash entry against this
4017        symbol.  */
4018     struct ppc_stub_hash_entry *stub_cache;
4019
4020     /* A pointer to the next symbol starting with a '.'  */
4021     struct ppc_link_hash_entry *next_dot_sym;
4022   } u;
4023
4024   /* Track dynamic relocs copied for this symbol.  */
4025   struct elf_dyn_relocs *dyn_relocs;
4026
4027   /* Link between function code and descriptor symbols.  */
4028   struct ppc_link_hash_entry *oh;
4029
4030   /* Flag function code and descriptor symbols.  */
4031   unsigned int is_func:1;
4032   unsigned int is_func_descriptor:1;
4033   unsigned int fake:1;
4034
4035   /* Whether global opd/toc sym has been adjusted or not.
4036      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4037      should be set for all globals defined in any opd/toc section.  */
4038   unsigned int adjust_done:1;
4039
4040   /* Set if this is an out-of-line register save/restore function,
4041      with non-standard calling convention.  */
4042   unsigned int save_res:1;
4043
4044   /* Set if a duplicate symbol with non-zero localentry is detected,
4045      even when the duplicate symbol does not provide a definition.  */
4046   unsigned int non_zero_localentry:1;
4047
4048   /* Contexts in which symbol is used in the GOT (or TOC).
4049      Bits are or'd into the mask as the corresponding relocs are
4050      encountered during check_relocs, with TLS_TLS being set when any
4051      of the other TLS bits are set.  tls_optimize clears bits when
4052      optimizing to indicate the corresponding GOT entry type is not
4053      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
4054      set TLS_TPRELGD when a GD reloc turns into a TPREL one.  We use a
4055      separate flag rather than setting TPREL just for convenience in
4056      distinguishing the two cases.
4057      These flags are also kept for local symbols.  */
4058 #define TLS_TLS          1      /* Any TLS reloc.  */
4059 #define TLS_GD           2      /* GD reloc. */
4060 #define TLS_LD           4      /* LD reloc. */
4061 #define TLS_TPREL        8      /* TPREL reloc, => IE. */
4062 #define TLS_DTPREL      16      /* DTPREL reloc, => LD. */
4063 #define TLS_MARK        32      /* __tls_get_addr call marked. */
4064 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
4065 #define TLS_EXPLICIT   128      /* Marks TOC section TLS relocs. */
4066   unsigned char tls_mask;
4067
4068   /* The above field is also used to mark function symbols.  In which
4069      case TLS_TLS will be 0.  */
4070 #define PLT_IFUNC        2      /* STT_GNU_IFUNC.  */
4071 #define NON_GOT        256      /* local symbol plt, not stored.  */
4072 };
4073
4074 /* ppc64 ELF linker hash table.  */
4075
4076 struct ppc_link_hash_table
4077 {
4078   struct elf_link_hash_table elf;
4079
4080   /* The stub hash table.  */
4081   struct bfd_hash_table stub_hash_table;
4082
4083   /* Another hash table for plt_branch stubs.  */
4084   struct bfd_hash_table branch_hash_table;
4085
4086   /* Hash table for function prologue tocsave.  */
4087   htab_t tocsave_htab;
4088
4089   /* Various options and other info passed from the linker.  */
4090   struct ppc64_elf_params *params;
4091
4092   /* The size of sec_info below.  */
4093   unsigned int sec_info_arr_size;
4094
4095   /* Per-section array of extra section info.  Done this way rather
4096      than as part of ppc64_elf_section_data so we have the info for
4097      non-ppc64 sections.  */
4098   struct
4099   {
4100     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4101     bfd_vma toc_off;
4102
4103     union
4104     {
4105       /* The section group that this section belongs to.  */
4106       struct map_stub *group;
4107       /* A temp section list pointer.  */
4108       asection *list;
4109     } u;
4110   } *sec_info;
4111
4112   /* Linked list of groups.  */
4113   struct map_stub *group;
4114
4115   /* Temp used when calculating TOC pointers.  */
4116   bfd_vma toc_curr;
4117   bfd *toc_bfd;
4118   asection *toc_first_sec;
4119
4120   /* Used when adding symbols.  */
4121   struct ppc_link_hash_entry *dot_syms;
4122
4123   /* Shortcuts to get to dynamic linker sections.  */
4124   asection *glink;
4125   asection *global_entry;
4126   asection *sfpr;
4127   asection *brlt;
4128   asection *relbrlt;
4129   asection *glink_eh_frame;
4130
4131   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4132   struct ppc_link_hash_entry *tls_get_addr;
4133   struct ppc_link_hash_entry *tls_get_addr_fd;
4134
4135   /* The size of reliplt used by got entry relocs.  */
4136   bfd_size_type got_reli_size;
4137
4138   /* Statistics.  */
4139   unsigned long stub_count[ppc_stub_global_entry];
4140
4141   /* Number of stubs against global syms.  */
4142   unsigned long stub_globals;
4143
4144   /* Set if we're linking code with function descriptors.  */
4145   unsigned int opd_abi:1;
4146
4147   /* Support for multiple toc sections.  */
4148   unsigned int do_multi_toc:1;
4149   unsigned int multi_toc_needed:1;
4150   unsigned int second_toc_pass:1;
4151   unsigned int do_toc_opt:1;
4152
4153   /* Set if tls optimization is enabled.  */
4154   unsigned int do_tls_opt:1;
4155
4156   /* Set on error.  */
4157   unsigned int stub_error:1;
4158
4159   /* Whether func_desc_adjust needs to be run over symbols.  */
4160   unsigned int need_func_desc_adj:1;
4161
4162   /* Whether there exist local gnu indirect function resolvers,
4163      referenced by dynamic relocations.  */
4164   unsigned int local_ifunc_resolver:1;
4165   unsigned int maybe_local_ifunc_resolver:1;
4166
4167   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
4168   unsigned int has_plt_localentry0:1;
4169
4170   /* Incremented every time we size stubs.  */
4171   unsigned int stub_iteration;
4172
4173   /* Small local sym cache.  */
4174   struct sym_cache sym_cache;
4175 };
4176
4177 /* Rename some of the generic section flags to better document how they
4178    are used here.  */
4179
4180 /* Nonzero if this section has TLS related relocations.  */
4181 #define has_tls_reloc sec_flg0
4182
4183 /* Nonzero if this section has a call to __tls_get_addr.  */
4184 #define has_tls_get_addr_call sec_flg1
4185
4186 /* Nonzero if this section has any toc or got relocs.  */
4187 #define has_toc_reloc sec_flg2
4188
4189 /* Nonzero if this section has a call to another section that uses
4190    the toc or got.  */
4191 #define makes_toc_func_call sec_flg3
4192
4193 /* Recursion protection when determining above flag.  */
4194 #define call_check_in_progress sec_flg4
4195 #define call_check_done sec_flg5
4196
4197 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4198
4199 #define ppc_hash_table(p) \
4200   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4201   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4202
4203 #define ppc_stub_hash_lookup(table, string, create, copy) \
4204   ((struct ppc_stub_hash_entry *) \
4205    bfd_hash_lookup ((table), (string), (create), (copy)))
4206
4207 #define ppc_branch_hash_lookup(table, string, create, copy) \
4208   ((struct ppc_branch_hash_entry *) \
4209    bfd_hash_lookup ((table), (string), (create), (copy)))
4210
4211 /* Create an entry in the stub hash table.  */
4212
4213 static struct bfd_hash_entry *
4214 stub_hash_newfunc (struct bfd_hash_entry *entry,
4215                    struct bfd_hash_table *table,
4216                    const char *string)
4217 {
4218   /* Allocate the structure if it has not already been allocated by a
4219      subclass.  */
4220   if (entry == NULL)
4221     {
4222       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4223       if (entry == NULL)
4224         return entry;
4225     }
4226
4227   /* Call the allocation method of the superclass.  */
4228   entry = bfd_hash_newfunc (entry, table, string);
4229   if (entry != NULL)
4230     {
4231       struct ppc_stub_hash_entry *eh;
4232
4233       /* Initialize the local fields.  */
4234       eh = (struct ppc_stub_hash_entry *) entry;
4235       eh->stub_type = ppc_stub_none;
4236       eh->group = NULL;
4237       eh->stub_offset = 0;
4238       eh->target_value = 0;
4239       eh->target_section = NULL;
4240       eh->h = NULL;
4241       eh->plt_ent = NULL;
4242       eh->other = 0;
4243     }
4244
4245   return entry;
4246 }
4247
4248 /* Create an entry in the branch hash table.  */
4249
4250 static struct bfd_hash_entry *
4251 branch_hash_newfunc (struct bfd_hash_entry *entry,
4252                      struct bfd_hash_table *table,
4253                      const char *string)
4254 {
4255   /* Allocate the structure if it has not already been allocated by a
4256      subclass.  */
4257   if (entry == NULL)
4258     {
4259       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4260       if (entry == NULL)
4261         return entry;
4262     }
4263
4264   /* Call the allocation method of the superclass.  */
4265   entry = bfd_hash_newfunc (entry, table, string);
4266   if (entry != NULL)
4267     {
4268       struct ppc_branch_hash_entry *eh;
4269
4270       /* Initialize the local fields.  */
4271       eh = (struct ppc_branch_hash_entry *) entry;
4272       eh->offset = 0;
4273       eh->iter = 0;
4274     }
4275
4276   return entry;
4277 }
4278
4279 /* Create an entry in a ppc64 ELF linker hash table.  */
4280
4281 static struct bfd_hash_entry *
4282 link_hash_newfunc (struct bfd_hash_entry *entry,
4283                    struct bfd_hash_table *table,
4284                    const char *string)
4285 {
4286   /* Allocate the structure if it has not already been allocated by a
4287      subclass.  */
4288   if (entry == NULL)
4289     {
4290       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4291       if (entry == NULL)
4292         return entry;
4293     }
4294
4295   /* Call the allocation method of the superclass.  */
4296   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4297   if (entry != NULL)
4298     {
4299       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4300
4301       memset (&eh->u.stub_cache, 0,
4302               (sizeof (struct ppc_link_hash_entry)
4303                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4304
4305       /* When making function calls, old ABI code references function entry
4306          points (dot symbols), while new ABI code references the function
4307          descriptor symbol.  We need to make any combination of reference and
4308          definition work together, without breaking archive linking.
4309
4310          For a defined function "foo" and an undefined call to "bar":
4311          An old object defines "foo" and ".foo", references ".bar" (possibly
4312          "bar" too).
4313          A new object defines "foo" and references "bar".
4314
4315          A new object thus has no problem with its undefined symbols being
4316          satisfied by definitions in an old object.  On the other hand, the
4317          old object won't have ".bar" satisfied by a new object.
4318
4319          Keep a list of newly added dot-symbols.  */
4320
4321       if (string[0] == '.')
4322         {
4323           struct ppc_link_hash_table *htab;
4324
4325           htab = (struct ppc_link_hash_table *) table;
4326           eh->u.next_dot_sym = htab->dot_syms;
4327           htab->dot_syms = eh;
4328         }
4329     }
4330
4331   return entry;
4332 }
4333
4334 struct tocsave_entry {
4335   asection *sec;
4336   bfd_vma offset;
4337 };
4338
4339 static hashval_t
4340 tocsave_htab_hash (const void *p)
4341 {
4342   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4343   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4344 }
4345
4346 static int
4347 tocsave_htab_eq (const void *p1, const void *p2)
4348 {
4349   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4350   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4351   return e1->sec == e2->sec && e1->offset == e2->offset;
4352 }
4353
4354 /* Destroy a ppc64 ELF linker hash table.  */
4355
4356 static void
4357 ppc64_elf_link_hash_table_free (bfd *obfd)
4358 {
4359   struct ppc_link_hash_table *htab;
4360
4361   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4362   if (htab->tocsave_htab)
4363     htab_delete (htab->tocsave_htab);
4364   bfd_hash_table_free (&htab->branch_hash_table);
4365   bfd_hash_table_free (&htab->stub_hash_table);
4366   _bfd_elf_link_hash_table_free (obfd);
4367 }
4368
4369 /* Create a ppc64 ELF linker hash table.  */
4370
4371 static struct bfd_link_hash_table *
4372 ppc64_elf_link_hash_table_create (bfd *abfd)
4373 {
4374   struct ppc_link_hash_table *htab;
4375   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4376
4377   htab = bfd_zmalloc (amt);
4378   if (htab == NULL)
4379     return NULL;
4380
4381   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4382                                       sizeof (struct ppc_link_hash_entry),
4383                                       PPC64_ELF_DATA))
4384     {
4385       free (htab);
4386       return NULL;
4387     }
4388
4389   /* Init the stub hash table too.  */
4390   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4391                             sizeof (struct ppc_stub_hash_entry)))
4392     {
4393       _bfd_elf_link_hash_table_free (abfd);
4394       return NULL;
4395     }
4396
4397   /* And the branch hash table.  */
4398   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4399                             sizeof (struct ppc_branch_hash_entry)))
4400     {
4401       bfd_hash_table_free (&htab->stub_hash_table);
4402       _bfd_elf_link_hash_table_free (abfd);
4403       return NULL;
4404     }
4405
4406   htab->tocsave_htab = htab_try_create (1024,
4407                                         tocsave_htab_hash,
4408                                         tocsave_htab_eq,
4409                                         NULL);
4410   if (htab->tocsave_htab == NULL)
4411     {
4412       ppc64_elf_link_hash_table_free (abfd);
4413       return NULL;
4414     }
4415   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4416
4417   /* Initializing two fields of the union is just cosmetic.  We really
4418      only care about glist, but when compiled on a 32-bit host the
4419      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4420      debugger inspection of these fields look nicer.  */
4421   htab->elf.init_got_refcount.refcount = 0;
4422   htab->elf.init_got_refcount.glist = NULL;
4423   htab->elf.init_plt_refcount.refcount = 0;
4424   htab->elf.init_plt_refcount.glist = NULL;
4425   htab->elf.init_got_offset.offset = 0;
4426   htab->elf.init_got_offset.glist = NULL;
4427   htab->elf.init_plt_offset.offset = 0;
4428   htab->elf.init_plt_offset.glist = NULL;
4429
4430   return &htab->elf.root;
4431 }
4432
4433 /* Create sections for linker generated code.  */
4434
4435 static bfd_boolean
4436 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4437 {
4438   struct ppc_link_hash_table *htab;
4439   flagword flags;
4440
4441   htab = ppc_hash_table (info);
4442
4443   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4444            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4445   if (htab->params->save_restore_funcs)
4446     {
4447       /* Create .sfpr for code to save and restore fp regs.  */
4448       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4449                                                        flags);
4450       if (htab->sfpr == NULL
4451           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4452         return FALSE;
4453     }
4454
4455   if (bfd_link_relocatable (info))
4456     return TRUE;
4457
4458   /* Create .glink for lazy dynamic linking support.  */
4459   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4460                                                     flags);
4461   if (htab->glink == NULL
4462       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4463     return FALSE;
4464
4465   /* The part of .glink used by global entry stubs, separate so that
4466      it can be aligned appropriately without affecting htab->glink.  */
4467   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4468                                                            flags);
4469   if (htab->global_entry == NULL
4470       || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4471     return FALSE;
4472
4473   if (!info->no_ld_generated_unwind_info)
4474     {
4475       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4476                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4477       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4478                                                                  ".eh_frame",
4479                                                                  flags);
4480       if (htab->glink_eh_frame == NULL
4481           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4482         return FALSE;
4483     }
4484
4485   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4486   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4487   if (htab->elf.iplt == NULL
4488       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4489     return FALSE;
4490
4491   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4492            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4493   htab->elf.irelplt
4494     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4495   if (htab->elf.irelplt == NULL
4496       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4497     return FALSE;
4498
4499   /* Create branch lookup table for plt_branch stubs.  */
4500   flags = (SEC_ALLOC | SEC_LOAD
4501            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4502   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4503                                                    flags);
4504   if (htab->brlt == NULL
4505       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4506     return FALSE;
4507
4508   if (!bfd_link_pic (info))
4509     return TRUE;
4510
4511   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4512            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4513   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4514                                                       ".rela.branch_lt",
4515                                                       flags);
4516   if (htab->relbrlt == NULL
4517       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4518     return FALSE;
4519
4520   return TRUE;
4521 }
4522
4523 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4524
4525 bfd_boolean
4526 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4527                          struct ppc64_elf_params *params)
4528 {
4529   struct ppc_link_hash_table *htab;
4530
4531   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4532
4533 /* Always hook our dynamic sections into the first bfd, which is the
4534    linker created stub bfd.  This ensures that the GOT header is at
4535    the start of the output TOC section.  */
4536   htab = ppc_hash_table (info);
4537   htab->elf.dynobj = params->stub_bfd;
4538   htab->params = params;
4539
4540   return create_linkage_sections (htab->elf.dynobj, info);
4541 }
4542
4543 /* Build a name for an entry in the stub hash table.  */
4544
4545 static char *
4546 ppc_stub_name (const asection *input_section,
4547                const asection *sym_sec,
4548                const struct ppc_link_hash_entry *h,
4549                const Elf_Internal_Rela *rel)
4550 {
4551   char *stub_name;
4552   ssize_t len;
4553
4554   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4555      offsets from a sym as a branch target?  In fact, we could
4556      probably assume the addend is always zero.  */
4557   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4558
4559   if (h)
4560     {
4561       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4562       stub_name = bfd_malloc (len);
4563       if (stub_name == NULL)
4564         return stub_name;
4565
4566       len = sprintf (stub_name, "%08x.%s+%x",
4567                      input_section->id & 0xffffffff,
4568                      h->elf.root.root.string,
4569                      (int) rel->r_addend & 0xffffffff);
4570     }
4571   else
4572     {
4573       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4574       stub_name = bfd_malloc (len);
4575       if (stub_name == NULL)
4576         return stub_name;
4577
4578       len = sprintf (stub_name, "%08x.%x:%x+%x",
4579                      input_section->id & 0xffffffff,
4580                      sym_sec->id & 0xffffffff,
4581                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4582                      (int) rel->r_addend & 0xffffffff);
4583     }
4584   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4585     stub_name[len - 2] = 0;
4586   return stub_name;
4587 }
4588
4589 /* Look up an entry in the stub hash.  Stub entries are cached because
4590    creating the stub name takes a bit of time.  */
4591
4592 static struct ppc_stub_hash_entry *
4593 ppc_get_stub_entry (const asection *input_section,
4594                     const asection *sym_sec,
4595                     struct ppc_link_hash_entry *h,
4596                     const Elf_Internal_Rela *rel,
4597                     struct ppc_link_hash_table *htab)
4598 {
4599   struct ppc_stub_hash_entry *stub_entry;
4600   struct map_stub *group;
4601
4602   /* If this input section is part of a group of sections sharing one
4603      stub section, then use the id of the first section in the group.
4604      Stub names need to include a section id, as there may well be
4605      more than one stub used to reach say, printf, and we need to
4606      distinguish between them.  */
4607   group = htab->sec_info[input_section->id].u.group;
4608   if (group == NULL)
4609     return NULL;
4610
4611   if (h != NULL && h->u.stub_cache != NULL
4612       && h->u.stub_cache->h == h
4613       && h->u.stub_cache->group == group)
4614     {
4615       stub_entry = h->u.stub_cache;
4616     }
4617   else
4618     {
4619       char *stub_name;
4620
4621       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4622       if (stub_name == NULL)
4623         return NULL;
4624
4625       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4626                                          stub_name, FALSE, FALSE);
4627       if (h != NULL)
4628         h->u.stub_cache = stub_entry;
4629
4630       free (stub_name);
4631     }
4632
4633   return stub_entry;
4634 }
4635
4636 /* Add a new stub entry to the stub hash.  Not all fields of the new
4637    stub entry are initialised.  */
4638
4639 static struct ppc_stub_hash_entry *
4640 ppc_add_stub (const char *stub_name,
4641               asection *section,
4642               struct bfd_link_info *info)
4643 {
4644   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4645   struct map_stub *group;
4646   asection *link_sec;
4647   asection *stub_sec;
4648   struct ppc_stub_hash_entry *stub_entry;
4649
4650   group = htab->sec_info[section->id].u.group;
4651   link_sec = group->link_sec;
4652   stub_sec = group->stub_sec;
4653   if (stub_sec == NULL)
4654     {
4655       size_t namelen;
4656       bfd_size_type len;
4657       char *s_name;
4658
4659       namelen = strlen (link_sec->name);
4660       len = namelen + sizeof (STUB_SUFFIX);
4661       s_name = bfd_alloc (htab->params->stub_bfd, len);
4662       if (s_name == NULL)
4663         return NULL;
4664
4665       memcpy (s_name, link_sec->name, namelen);
4666       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4667       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4668       if (stub_sec == NULL)
4669         return NULL;
4670       group->stub_sec = stub_sec;
4671     }
4672
4673   /* Enter this entry into the linker stub hash table.  */
4674   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4675                                      TRUE, FALSE);
4676   if (stub_entry == NULL)
4677     {
4678       /* xgettext:c-format */
4679       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4680                           section->owner, stub_name);
4681       return NULL;
4682     }
4683
4684   stub_entry->group = group;
4685   stub_entry->stub_offset = 0;
4686   return stub_entry;
4687 }
4688
4689 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4690    not already done.  */
4691
4692 static bfd_boolean
4693 create_got_section (bfd *abfd, struct bfd_link_info *info)
4694 {
4695   asection *got, *relgot;
4696   flagword flags;
4697   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4698
4699   if (!is_ppc64_elf (abfd))
4700     return FALSE;
4701   if (htab == NULL)
4702     return FALSE;
4703
4704   if (!htab->elf.sgot
4705       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4706     return FALSE;
4707
4708   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4709            | SEC_LINKER_CREATED);
4710
4711   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4712   if (!got
4713       || !bfd_set_section_alignment (abfd, got, 3))
4714     return FALSE;
4715
4716   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4717                                                flags | SEC_READONLY);
4718   if (!relgot
4719       || ! bfd_set_section_alignment (abfd, relgot, 3))
4720     return FALSE;
4721
4722   ppc64_elf_tdata (abfd)->got = got;
4723   ppc64_elf_tdata (abfd)->relgot = relgot;
4724   return TRUE;
4725 }
4726
4727 /* Follow indirect and warning symbol links.  */
4728
4729 static inline struct bfd_link_hash_entry *
4730 follow_link (struct bfd_link_hash_entry *h)
4731 {
4732   while (h->type == bfd_link_hash_indirect
4733          || h->type == bfd_link_hash_warning)
4734     h = h->u.i.link;
4735   return h;
4736 }
4737
4738 static inline struct elf_link_hash_entry *
4739 elf_follow_link (struct elf_link_hash_entry *h)
4740 {
4741   return (struct elf_link_hash_entry *) follow_link (&h->root);
4742 }
4743
4744 static inline struct ppc_link_hash_entry *
4745 ppc_follow_link (struct ppc_link_hash_entry *h)
4746 {
4747   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4748 }
4749
4750 /* Merge PLT info on FROM with that on TO.  */
4751
4752 static void
4753 move_plt_plist (struct ppc_link_hash_entry *from,
4754                 struct ppc_link_hash_entry *to)
4755 {
4756   if (from->elf.plt.plist != NULL)
4757     {
4758       if (to->elf.plt.plist != NULL)
4759         {
4760           struct plt_entry **entp;
4761           struct plt_entry *ent;
4762
4763           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4764             {
4765               struct plt_entry *dent;
4766
4767               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4768                 if (dent->addend == ent->addend)
4769                   {
4770                     dent->plt.refcount += ent->plt.refcount;
4771                     *entp = ent->next;
4772                     break;
4773                   }
4774               if (dent == NULL)
4775                 entp = &ent->next;
4776             }
4777           *entp = to->elf.plt.plist;
4778         }
4779
4780       to->elf.plt.plist = from->elf.plt.plist;
4781       from->elf.plt.plist = NULL;
4782     }
4783 }
4784
4785 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4786
4787 static void
4788 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4789                                 struct elf_link_hash_entry *dir,
4790                                 struct elf_link_hash_entry *ind)
4791 {
4792   struct ppc_link_hash_entry *edir, *eind;
4793
4794   edir = (struct ppc_link_hash_entry *) dir;
4795   eind = (struct ppc_link_hash_entry *) ind;
4796
4797   edir->is_func |= eind->is_func;
4798   edir->is_func_descriptor |= eind->is_func_descriptor;
4799   edir->tls_mask |= eind->tls_mask;
4800   if (eind->oh != NULL)
4801     edir->oh = ppc_follow_link (eind->oh);
4802
4803   if (edir->elf.versioned != versioned_hidden)
4804     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4805   edir->elf.ref_regular |= eind->elf.ref_regular;
4806   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4807   edir->elf.non_got_ref |= eind->elf.non_got_ref;
4808   edir->elf.needs_plt |= eind->elf.needs_plt;
4809   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4810
4811   /* If we were called to copy over info for a weak sym, don't copy
4812      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4813      in order to simplify readonly_dynrelocs and save a field in the
4814      symbol hash entry, but that means dyn_relocs can't be used in any
4815      tests about a specific symbol, or affect other symbol flags which
4816      are then tested.  */
4817   if (eind->elf.root.type != bfd_link_hash_indirect)
4818     return;
4819
4820   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4821   if (eind->dyn_relocs != NULL)
4822     {
4823       if (edir->dyn_relocs != NULL)
4824         {
4825           struct elf_dyn_relocs **pp;
4826           struct elf_dyn_relocs *p;
4827
4828           /* Add reloc counts against the indirect sym to the direct sym
4829              list.  Merge any entries against the same section.  */
4830           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4831             {
4832               struct elf_dyn_relocs *q;
4833
4834               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4835                 if (q->sec == p->sec)
4836                   {
4837                     q->pc_count += p->pc_count;
4838                     q->count += p->count;
4839                     *pp = p->next;
4840                     break;
4841                   }
4842               if (q == NULL)
4843                 pp = &p->next;
4844             }
4845           *pp = edir->dyn_relocs;
4846         }
4847
4848       edir->dyn_relocs = eind->dyn_relocs;
4849       eind->dyn_relocs = NULL;
4850     }
4851
4852   /* Copy over got entries that we may have already seen to the
4853      symbol which just became indirect.  */
4854   if (eind->elf.got.glist != NULL)
4855     {
4856       if (edir->elf.got.glist != NULL)
4857         {
4858           struct got_entry **entp;
4859           struct got_entry *ent;
4860
4861           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4862             {
4863               struct got_entry *dent;
4864
4865               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4866                 if (dent->addend == ent->addend
4867                     && dent->owner == ent->owner
4868                     && dent->tls_type == ent->tls_type)
4869                   {
4870                     dent->got.refcount += ent->got.refcount;
4871                     *entp = ent->next;
4872                     break;
4873                   }
4874               if (dent == NULL)
4875                 entp = &ent->next;
4876             }
4877           *entp = edir->elf.got.glist;
4878         }
4879
4880       edir->elf.got.glist = eind->elf.got.glist;
4881       eind->elf.got.glist = NULL;
4882     }
4883
4884   /* And plt entries.  */
4885   move_plt_plist (eind, edir);
4886
4887   if (eind->elf.dynindx != -1)
4888     {
4889       if (edir->elf.dynindx != -1)
4890         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4891                                 edir->elf.dynstr_index);
4892       edir->elf.dynindx = eind->elf.dynindx;
4893       edir->elf.dynstr_index = eind->elf.dynstr_index;
4894       eind->elf.dynindx = -1;
4895       eind->elf.dynstr_index = 0;
4896     }
4897 }
4898
4899 /* Find the function descriptor hash entry from the given function code
4900    hash entry FH.  Link the entries via their OH fields.  */
4901
4902 static struct ppc_link_hash_entry *
4903 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4904 {
4905   struct ppc_link_hash_entry *fdh = fh->oh;
4906
4907   if (fdh == NULL)
4908     {
4909       const char *fd_name = fh->elf.root.root.string + 1;
4910
4911       fdh = (struct ppc_link_hash_entry *)
4912         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4913       if (fdh == NULL)
4914         return fdh;
4915
4916       fdh->is_func_descriptor = 1;
4917       fdh->oh = fh;
4918       fh->is_func = 1;
4919       fh->oh = fdh;
4920     }
4921
4922   fdh = ppc_follow_link (fdh);
4923   fdh->is_func_descriptor = 1;
4924   fdh->oh = fh;
4925   return fdh;
4926 }
4927
4928 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4929
4930 static struct ppc_link_hash_entry *
4931 make_fdh (struct bfd_link_info *info,
4932           struct ppc_link_hash_entry *fh)
4933 {
4934   bfd *abfd = fh->elf.root.u.undef.abfd;
4935   struct bfd_link_hash_entry *bh = NULL;
4936   struct ppc_link_hash_entry *fdh;
4937   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4938                     ? BSF_WEAK
4939                     : BSF_GLOBAL);
4940
4941   if (!_bfd_generic_link_add_one_symbol (info, abfd,
4942                                          fh->elf.root.root.string + 1,
4943                                          flags, bfd_und_section_ptr, 0,
4944                                          NULL, FALSE, FALSE, &bh))
4945     return NULL;
4946
4947   fdh = (struct ppc_link_hash_entry *) bh;
4948   fdh->elf.non_elf = 0;
4949   fdh->fake = 1;
4950   fdh->is_func_descriptor = 1;
4951   fdh->oh = fh;
4952   fh->is_func = 1;
4953   fh->oh = fdh;
4954   return fdh;
4955 }
4956
4957 /* Fix function descriptor symbols defined in .opd sections to be
4958    function type.  */
4959
4960 static bfd_boolean
4961 ppc64_elf_add_symbol_hook (bfd *ibfd,
4962                            struct bfd_link_info *info,
4963                            Elf_Internal_Sym *isym,
4964                            const char **name,
4965                            flagword *flags ATTRIBUTE_UNUSED,
4966                            asection **sec,
4967                            bfd_vma *value)
4968 {
4969   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4970       && (ibfd->flags & DYNAMIC) == 0
4971       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4972     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4973
4974   if (*sec != NULL
4975       && strcmp ((*sec)->name, ".opd") == 0)
4976     {
4977       asection *code_sec;
4978
4979       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4980             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4981         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4982
4983       /* If the symbol is a function defined in .opd, and the function
4984          code is in a discarded group, let it appear to be undefined.  */
4985       if (!bfd_link_relocatable (info)
4986           && (*sec)->reloc_count != 0
4987           && opd_entry_value (*sec, *value, &code_sec, NULL,
4988                               FALSE) != (bfd_vma) -1
4989           && discarded_section (code_sec))
4990         {
4991           *sec = bfd_und_section_ptr;
4992           isym->st_shndx = SHN_UNDEF;
4993         }
4994     }
4995   else if (*sec != NULL
4996            && strcmp ((*sec)->name, ".toc") == 0
4997            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4998     {
4999       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5000       if (htab != NULL)
5001         htab->params->object_in_toc = 1;
5002     }
5003
5004   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5005     {
5006       if (abiversion (ibfd) == 0)
5007         set_abiversion (ibfd, 2);
5008       else if (abiversion (ibfd) == 1)
5009         {
5010           _bfd_error_handler (_("symbol '%s' has invalid st_other"
5011                                 " for ABI version 1"), *name);
5012           bfd_set_error (bfd_error_bad_value);
5013           return FALSE;
5014         }
5015     }
5016
5017   return TRUE;
5018 }
5019
5020 /* Merge non-visibility st_other attributes: local entry point.  */
5021
5022 static void
5023 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5024                                   const Elf_Internal_Sym *isym,
5025                                   bfd_boolean definition,
5026                                   bfd_boolean dynamic)
5027 {
5028   if (definition && (!dynamic || !h->def_regular))
5029     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5030                 | ELF_ST_VISIBILITY (h->other));
5031 }
5032
5033 /* Hook called on merging a symbol.  We use this to clear "fake" since
5034    we now have a real symbol.  */
5035
5036 static bfd_boolean
5037 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5038                         const Elf_Internal_Sym *isym,
5039                         asection **psec ATTRIBUTE_UNUSED,
5040                         bfd_boolean newdef ATTRIBUTE_UNUSED,
5041                         bfd_boolean olddef ATTRIBUTE_UNUSED,
5042                         bfd *oldbfd ATTRIBUTE_UNUSED,
5043                         const asection *oldsec ATTRIBUTE_UNUSED)
5044 {
5045   ((struct ppc_link_hash_entry *) h)->fake = 0;
5046   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5047     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5048   return TRUE;
5049 }
5050
5051 /* This function makes an old ABI object reference to ".bar" cause the
5052    inclusion of a new ABI object archive that defines "bar".
5053    NAME is a symbol defined in an archive.  Return a symbol in the hash
5054    table that might be satisfied by the archive symbols.  */
5055
5056 static struct elf_link_hash_entry *
5057 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5058                                  struct bfd_link_info *info,
5059                                  const char *name)
5060 {
5061   struct elf_link_hash_entry *h;
5062   char *dot_name;
5063   size_t len;
5064
5065   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5066   if (h != NULL
5067       /* Don't return this sym if it is a fake function descriptor
5068          created by add_symbol_adjust.  */
5069       && !((struct ppc_link_hash_entry *) h)->fake)
5070     return h;
5071
5072   if (name[0] == '.')
5073     return h;
5074
5075   len = strlen (name);
5076   dot_name = bfd_alloc (abfd, len + 2);
5077   if (dot_name == NULL)
5078     return (struct elf_link_hash_entry *) -1;
5079   dot_name[0] = '.';
5080   memcpy (dot_name + 1, name, len + 1);
5081   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5082   bfd_release (abfd, dot_name);
5083   return h;
5084 }
5085
5086 /* This function satisfies all old ABI object references to ".bar" if a
5087    new ABI object defines "bar".  Well, at least, undefined dot symbols
5088    are made weak.  This stops later archive searches from including an
5089    object if we already have a function descriptor definition.  It also
5090    prevents the linker complaining about undefined symbols.
5091    We also check and correct mismatched symbol visibility here.  The
5092    most restrictive visibility of the function descriptor and the
5093    function entry symbol is used.  */
5094
5095 static bfd_boolean
5096 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5097 {
5098   struct ppc_link_hash_table *htab;
5099   struct ppc_link_hash_entry *fdh;
5100
5101   if (eh->elf.root.type == bfd_link_hash_warning)
5102     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5103
5104   if (eh->elf.root.type == bfd_link_hash_indirect)
5105     return TRUE;
5106
5107   if (eh->elf.root.root.string[0] != '.')
5108     abort ();
5109
5110   htab = ppc_hash_table (info);
5111   if (htab == NULL)
5112     return FALSE;
5113
5114   fdh = lookup_fdh (eh, htab);
5115   if (fdh == NULL
5116       && !bfd_link_relocatable (info)
5117       && (eh->elf.root.type == bfd_link_hash_undefined
5118           || eh->elf.root.type == bfd_link_hash_undefweak)
5119       && eh->elf.ref_regular)
5120     {
5121       /* Make an undefined function descriptor sym, in order to
5122          pull in an --as-needed shared lib.  Archives are handled
5123          elsewhere.  */
5124       fdh = make_fdh (info, eh);
5125       if (fdh == NULL)
5126         return FALSE;
5127     }
5128
5129   if (fdh != NULL)
5130     {
5131       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5132       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5133
5134       /* Make both descriptor and entry symbol have the most
5135          constraining visibility of either symbol.  */
5136       if (entry_vis < descr_vis)
5137         fdh->elf.other += entry_vis - descr_vis;
5138       else if (entry_vis > descr_vis)
5139         eh->elf.other += descr_vis - entry_vis;
5140
5141       /* Propagate reference flags from entry symbol to function
5142          descriptor symbol.  */
5143       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5144       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5145       fdh->elf.ref_regular |= eh->elf.ref_regular;
5146       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5147
5148       if (!fdh->elf.forced_local
5149           && fdh->elf.dynindx == -1
5150           && fdh->elf.versioned != versioned_hidden
5151           && (bfd_link_dll (info)
5152               || fdh->elf.def_dynamic
5153               || fdh->elf.ref_dynamic)
5154           && (eh->elf.ref_regular
5155               || eh->elf.def_regular))
5156         {
5157           if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5158             return FALSE;
5159         }
5160     }
5161
5162   return TRUE;
5163 }
5164
5165 /* Set up opd section info and abiversion for IBFD, and process list
5166    of dot-symbols we made in link_hash_newfunc.  */
5167
5168 static bfd_boolean
5169 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5170 {
5171   struct ppc_link_hash_table *htab;
5172   struct ppc_link_hash_entry **p, *eh;
5173   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5174
5175   if (opd != NULL && opd->size != 0)
5176     {
5177       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5178       ppc64_elf_section_data (opd)->sec_type = sec_opd;
5179
5180       if (abiversion (ibfd) == 0)
5181         set_abiversion (ibfd, 1);
5182       else if (abiversion (ibfd) >= 2)
5183         {
5184           /* xgettext:c-format */
5185           _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5186                               ibfd, abiversion (ibfd));
5187           bfd_set_error (bfd_error_bad_value);
5188           return FALSE;
5189         }
5190     }
5191
5192   if (is_ppc64_elf (info->output_bfd))
5193     {
5194       /* For input files without an explicit abiversion in e_flags
5195          we should have flagged any with symbol st_other bits set
5196          as ELFv1 and above flagged those with .opd as ELFv2.
5197          Set the output abiversion if not yet set, and for any input
5198          still ambiguous, take its abiversion from the output.
5199          Differences in ABI are reported later.  */
5200       if (abiversion (info->output_bfd) == 0)
5201         set_abiversion (info->output_bfd, abiversion (ibfd));
5202       else if (abiversion (ibfd) == 0)
5203         set_abiversion (ibfd, abiversion (info->output_bfd));
5204     }
5205
5206   htab = ppc_hash_table (info);
5207   if (htab == NULL)
5208     return TRUE;
5209
5210   if (opd != NULL && opd->size != 0
5211       && (ibfd->flags & DYNAMIC) == 0
5212       && (opd->flags & SEC_RELOC) != 0
5213       && opd->reloc_count != 0
5214       && !bfd_is_abs_section (opd->output_section)
5215       && info->gc_sections)
5216     {
5217       /* Garbage collection needs some extra help with .opd sections.
5218          We don't want to necessarily keep everything referenced by
5219          relocs in .opd, as that would keep all functions.  Instead,
5220          if we reference an .opd symbol (a function descriptor), we
5221          want to keep the function code symbol's section.  This is
5222          easy for global symbols, but for local syms we need to keep
5223          information about the associated function section.  */
5224       bfd_size_type amt;
5225       asection **opd_sym_map;
5226       Elf_Internal_Shdr *symtab_hdr;
5227       Elf_Internal_Rela *relocs, *rel_end, *rel;
5228
5229       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5230       opd_sym_map = bfd_zalloc (ibfd, amt);
5231       if (opd_sym_map == NULL)
5232         return FALSE;
5233       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5234       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5235                                           info->keep_memory);
5236       if (relocs == NULL)
5237         return FALSE;
5238       symtab_hdr = &elf_symtab_hdr (ibfd);
5239       rel_end = relocs + opd->reloc_count - 1;
5240       for (rel = relocs; rel < rel_end; rel++)
5241         {
5242           enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5243           unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5244
5245           if (r_type == R_PPC64_ADDR64
5246               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5247               && r_symndx < symtab_hdr->sh_info)
5248             {
5249               Elf_Internal_Sym *isym;
5250               asection *s;
5251
5252               isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5253               if (isym == NULL)
5254                 {
5255                   if (elf_section_data (opd)->relocs != relocs)
5256                     free (relocs);
5257                   return FALSE;
5258                 }
5259
5260               s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5261               if (s != NULL && s != opd)
5262                 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5263             }
5264         }
5265       if (elf_section_data (opd)->relocs != relocs)
5266         free (relocs);
5267     }
5268
5269   p = &htab->dot_syms;
5270   while ((eh = *p) != NULL)
5271     {
5272       *p = NULL;
5273       if (&eh->elf == htab->elf.hgot)
5274         ;
5275       else if (htab->elf.hgot == NULL
5276                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5277         htab->elf.hgot = &eh->elf;
5278       else if (abiversion (ibfd) <= 1)
5279         {
5280           htab->need_func_desc_adj = 1;
5281           if (!add_symbol_adjust (eh, info))
5282             return FALSE;
5283         }
5284       p = &eh->u.next_dot_sym;
5285     }
5286   return TRUE;
5287 }
5288
5289 /* Undo hash table changes when an --as-needed input file is determined
5290    not to be needed.  */
5291
5292 static bfd_boolean
5293 ppc64_elf_notice_as_needed (bfd *ibfd,
5294                             struct bfd_link_info *info,
5295                             enum notice_asneeded_action act)
5296 {
5297   if (act == notice_not_needed)
5298     {
5299       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5300
5301       if (htab == NULL)
5302         return FALSE;
5303
5304       htab->dot_syms = NULL;
5305     }
5306   return _bfd_elf_notice_as_needed (ibfd, info, act);
5307 }
5308
5309 /* If --just-symbols against a final linked binary, then assume we need
5310    toc adjusting stubs when calling functions defined there.  */
5311
5312 static void
5313 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5314 {
5315   if ((sec->flags & SEC_CODE) != 0
5316       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5317       && is_ppc64_elf (sec->owner))
5318     {
5319       if (abiversion (sec->owner) >= 2
5320           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5321         sec->has_toc_reloc = 1;
5322     }
5323   _bfd_elf_link_just_syms (sec, info);
5324 }
5325
5326 static struct plt_entry **
5327 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5328                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5329 {
5330   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5331   struct plt_entry **local_plt;
5332   unsigned char *local_got_tls_masks;
5333
5334   if (local_got_ents == NULL)
5335     {
5336       bfd_size_type size = symtab_hdr->sh_info;
5337
5338       size *= (sizeof (*local_got_ents)
5339                + sizeof (*local_plt)
5340                + sizeof (*local_got_tls_masks));
5341       local_got_ents = bfd_zalloc (abfd, size);
5342       if (local_got_ents == NULL)
5343         return NULL;
5344       elf_local_got_ents (abfd) = local_got_ents;
5345     }
5346
5347   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5348     {
5349       struct got_entry *ent;
5350
5351       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5352         if (ent->addend == r_addend
5353             && ent->owner == abfd
5354             && ent->tls_type == tls_type)
5355           break;
5356       if (ent == NULL)
5357         {
5358           bfd_size_type amt = sizeof (*ent);
5359           ent = bfd_alloc (abfd, amt);
5360           if (ent == NULL)
5361             return FALSE;
5362           ent->next = local_got_ents[r_symndx];
5363           ent->addend = r_addend;
5364           ent->owner = abfd;
5365           ent->tls_type = tls_type;
5366           ent->is_indirect = FALSE;
5367           ent->got.refcount = 0;
5368           local_got_ents[r_symndx] = ent;
5369         }
5370       ent->got.refcount += 1;
5371     }
5372
5373   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5374   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5375   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5376
5377   return local_plt + r_symndx;
5378 }
5379
5380 static bfd_boolean
5381 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5382 {
5383   struct plt_entry *ent;
5384
5385   for (ent = *plist; ent != NULL; ent = ent->next)
5386     if (ent->addend == addend)
5387       break;
5388   if (ent == NULL)
5389     {
5390       bfd_size_type amt = sizeof (*ent);
5391       ent = bfd_alloc (abfd, amt);
5392       if (ent == NULL)
5393         return FALSE;
5394       ent->next = *plist;
5395       ent->addend = addend;
5396       ent->plt.refcount = 0;
5397       *plist = ent;
5398     }
5399   ent->plt.refcount += 1;
5400   return TRUE;
5401 }
5402
5403 static bfd_boolean
5404 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5405 {
5406   return (r_type == R_PPC64_REL24
5407           || r_type == R_PPC64_REL14
5408           || r_type == R_PPC64_REL14_BRTAKEN
5409           || r_type == R_PPC64_REL14_BRNTAKEN
5410           || r_type == R_PPC64_ADDR24
5411           || r_type == R_PPC64_ADDR14
5412           || r_type == R_PPC64_ADDR14_BRTAKEN
5413           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5414 }
5415
5416 /* Look through the relocs for a section during the first phase, and
5417    calculate needed space in the global offset table, procedure
5418    linkage table, and dynamic reloc sections.  */
5419
5420 static bfd_boolean
5421 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5422                         asection *sec, const Elf_Internal_Rela *relocs)
5423 {
5424   struct ppc_link_hash_table *htab;
5425   Elf_Internal_Shdr *symtab_hdr;
5426   struct elf_link_hash_entry **sym_hashes;
5427   const Elf_Internal_Rela *rel;
5428   const Elf_Internal_Rela *rel_end;
5429   asection *sreloc;
5430   struct elf_link_hash_entry *tga, *dottga;
5431   bfd_boolean is_opd;
5432
5433   if (bfd_link_relocatable (info))
5434     return TRUE;
5435
5436   /* Don't do anything special with non-loaded, non-alloced sections.
5437      In particular, any relocs in such sections should not affect GOT
5438      and PLT reference counting (ie. we don't allow them to create GOT
5439      or PLT entries), there's no possibility or desire to optimize TLS
5440      relocs, and there's not much point in propagating relocs to shared
5441      libs that the dynamic linker won't relocate.  */
5442   if ((sec->flags & SEC_ALLOC) == 0)
5443     return TRUE;
5444
5445   BFD_ASSERT (is_ppc64_elf (abfd));
5446
5447   htab = ppc_hash_table (info);
5448   if (htab == NULL)
5449     return FALSE;
5450
5451   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5452                               FALSE, FALSE, TRUE);
5453   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5454                                  FALSE, FALSE, TRUE);
5455   symtab_hdr = &elf_symtab_hdr (abfd);
5456   sym_hashes = elf_sym_hashes (abfd);
5457   sreloc = NULL;
5458   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5459   rel_end = relocs + sec->reloc_count;
5460   for (rel = relocs; rel < rel_end; rel++)
5461     {
5462       unsigned long r_symndx;
5463       struct elf_link_hash_entry *h;
5464       enum elf_ppc64_reloc_type r_type;
5465       int tls_type;
5466       struct _ppc64_elf_section_data *ppc64_sec;
5467       struct plt_entry **ifunc, **plt_list;
5468
5469       r_symndx = ELF64_R_SYM (rel->r_info);
5470       if (r_symndx < symtab_hdr->sh_info)
5471         h = NULL;
5472       else
5473         {
5474           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5475           h = elf_follow_link (h);
5476
5477           if (h == htab->elf.hgot)
5478             sec->has_toc_reloc = 1;
5479         }
5480
5481       tls_type = 0;
5482       ifunc = NULL;
5483       if (h != NULL)
5484         {
5485           if (h->type == STT_GNU_IFUNC)
5486             {
5487               h->needs_plt = 1;
5488               ifunc = &h->plt.plist;
5489             }
5490         }
5491       else
5492         {
5493           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5494                                                           abfd, r_symndx);
5495           if (isym == NULL)
5496             return FALSE;
5497
5498           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5499             {
5500               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5501                                              rel->r_addend,
5502                                              NON_GOT | PLT_IFUNC);
5503               if (ifunc == NULL)
5504                 return FALSE;
5505             }
5506         }
5507
5508       r_type = ELF64_R_TYPE (rel->r_info);
5509       switch (r_type)
5510         {
5511         case R_PPC64_TLSGD:
5512         case R_PPC64_TLSLD:
5513           /* These special tls relocs tie a call to __tls_get_addr with
5514              its parameter symbol.  */
5515           if (h != NULL)
5516             ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5517           else
5518             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5519                                         rel->r_addend,
5520                                         NON_GOT | TLS_TLS | TLS_MARK))
5521               return FALSE;
5522           sec->has_tls_reloc = 1;
5523           break;
5524
5525         case R_PPC64_GOT_TLSLD16:
5526         case R_PPC64_GOT_TLSLD16_LO:
5527         case R_PPC64_GOT_TLSLD16_HI:
5528         case R_PPC64_GOT_TLSLD16_HA:
5529           tls_type = TLS_TLS | TLS_LD;
5530           goto dogottls;
5531
5532         case R_PPC64_GOT_TLSGD16:
5533         case R_PPC64_GOT_TLSGD16_LO:
5534         case R_PPC64_GOT_TLSGD16_HI:
5535         case R_PPC64_GOT_TLSGD16_HA:
5536           tls_type = TLS_TLS | TLS_GD;
5537           goto dogottls;
5538
5539         case R_PPC64_GOT_TPREL16_DS:
5540         case R_PPC64_GOT_TPREL16_LO_DS:
5541         case R_PPC64_GOT_TPREL16_HI:
5542         case R_PPC64_GOT_TPREL16_HA:
5543           if (bfd_link_dll (info))
5544             info->flags |= DF_STATIC_TLS;
5545           tls_type = TLS_TLS | TLS_TPREL;
5546           goto dogottls;
5547
5548         case R_PPC64_GOT_DTPREL16_DS:
5549         case R_PPC64_GOT_DTPREL16_LO_DS:
5550         case R_PPC64_GOT_DTPREL16_HI:
5551         case R_PPC64_GOT_DTPREL16_HA:
5552           tls_type = TLS_TLS | TLS_DTPREL;
5553         dogottls:
5554           sec->has_tls_reloc = 1;
5555           /* Fall through */
5556
5557         case R_PPC64_GOT16:
5558         case R_PPC64_GOT16_DS:
5559         case R_PPC64_GOT16_HA:
5560         case R_PPC64_GOT16_HI:
5561         case R_PPC64_GOT16_LO:
5562         case R_PPC64_GOT16_LO_DS:
5563           /* This symbol requires a global offset table entry.  */
5564           sec->has_toc_reloc = 1;
5565           if (r_type == R_PPC64_GOT_TLSLD16
5566               || r_type == R_PPC64_GOT_TLSGD16
5567               || r_type == R_PPC64_GOT_TPREL16_DS
5568               || r_type == R_PPC64_GOT_DTPREL16_DS
5569               || r_type == R_PPC64_GOT16
5570               || r_type == R_PPC64_GOT16_DS)
5571             {
5572               htab->do_multi_toc = 1;
5573               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5574             }
5575
5576           if (ppc64_elf_tdata (abfd)->got == NULL
5577               && !create_got_section (abfd, info))
5578             return FALSE;
5579
5580           if (h != NULL)
5581             {
5582               struct ppc_link_hash_entry *eh;
5583               struct got_entry *ent;
5584
5585               eh = (struct ppc_link_hash_entry *) h;
5586               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5587                 if (ent->addend == rel->r_addend
5588                     && ent->owner == abfd
5589                     && ent->tls_type == tls_type)
5590                   break;
5591               if (ent == NULL)
5592                 {
5593                   bfd_size_type amt = sizeof (*ent);
5594                   ent = bfd_alloc (abfd, amt);
5595                   if (ent == NULL)
5596                     return FALSE;
5597                   ent->next = eh->elf.got.glist;
5598                   ent->addend = rel->r_addend;
5599                   ent->owner = abfd;
5600                   ent->tls_type = tls_type;
5601                   ent->is_indirect = FALSE;
5602                   ent->got.refcount = 0;
5603                   eh->elf.got.glist = ent;
5604                 }
5605               ent->got.refcount += 1;
5606               eh->tls_mask |= tls_type;
5607             }
5608           else
5609             /* This is a global offset table entry for a local symbol.  */
5610             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5611                                         rel->r_addend, tls_type))
5612               return FALSE;
5613
5614           /* We may also need a plt entry if the symbol turns out to be
5615              an ifunc.  */
5616           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5617             {
5618               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5619                 return FALSE;
5620             }
5621           break;
5622
5623         case R_PPC64_PLT16_HA:
5624         case R_PPC64_PLT16_HI:
5625         case R_PPC64_PLT16_LO:
5626         case R_PPC64_PLT32:
5627         case R_PPC64_PLT64:
5628           /* This symbol requires a procedure linkage table entry.  */
5629           plt_list = ifunc;
5630           if (h != NULL)
5631             {
5632               h->needs_plt = 1;
5633               if (h->root.root.string[0] == '.'
5634                   && h->root.root.string[1] != '\0')
5635                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5636               plt_list = &h->plt.plist;
5637             }
5638           if (plt_list == NULL)
5639             {
5640               /* It does not make sense to have a procedure linkage
5641                  table entry for a non-ifunc local symbol.  */
5642               info->callbacks->einfo
5643                 /* xgettext:c-format */
5644                 (_("%H: %s reloc against local symbol\n"),
5645                  abfd, sec, rel->r_offset,
5646                  ppc64_elf_howto_table[r_type]->name);
5647               bfd_set_error (bfd_error_bad_value);
5648               return FALSE;
5649             }
5650           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5651             return FALSE;
5652           break;
5653
5654           /* The following relocations don't need to propagate the
5655              relocation if linking a shared object since they are
5656              section relative.  */
5657         case R_PPC64_SECTOFF:
5658         case R_PPC64_SECTOFF_LO:
5659         case R_PPC64_SECTOFF_HI:
5660         case R_PPC64_SECTOFF_HA:
5661         case R_PPC64_SECTOFF_DS:
5662         case R_PPC64_SECTOFF_LO_DS:
5663         case R_PPC64_DTPREL16:
5664         case R_PPC64_DTPREL16_LO:
5665         case R_PPC64_DTPREL16_HI:
5666         case R_PPC64_DTPREL16_HA:
5667         case R_PPC64_DTPREL16_DS:
5668         case R_PPC64_DTPREL16_LO_DS:
5669         case R_PPC64_DTPREL16_HIGH:
5670         case R_PPC64_DTPREL16_HIGHA:
5671         case R_PPC64_DTPREL16_HIGHER:
5672         case R_PPC64_DTPREL16_HIGHERA:
5673         case R_PPC64_DTPREL16_HIGHEST:
5674         case R_PPC64_DTPREL16_HIGHESTA:
5675           break;
5676
5677           /* Nor do these.  */
5678         case R_PPC64_REL16:
5679         case R_PPC64_REL16_LO:
5680         case R_PPC64_REL16_HI:
5681         case R_PPC64_REL16_HA:
5682         case R_PPC64_REL16DX_HA:
5683           break;
5684
5685           /* Not supported as a dynamic relocation.  */
5686         case R_PPC64_ADDR64_LOCAL:
5687           if (bfd_link_pic (info))
5688             {
5689               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5690                 ppc_howto_init ();
5691               /* xgettext:c-format */
5692               info->callbacks->einfo (_("%H: %s reloc unsupported "
5693                                         "in shared libraries and PIEs\n"),
5694                                       abfd, sec, rel->r_offset,
5695                                       ppc64_elf_howto_table[r_type]->name);
5696               bfd_set_error (bfd_error_bad_value);
5697               return FALSE;
5698             }
5699           break;
5700
5701         case R_PPC64_TOC16:
5702         case R_PPC64_TOC16_DS:
5703           htab->do_multi_toc = 1;
5704           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5705           /* Fall through.  */
5706         case R_PPC64_TOC16_LO:
5707         case R_PPC64_TOC16_HI:
5708         case R_PPC64_TOC16_HA:
5709         case R_PPC64_TOC16_LO_DS:
5710           sec->has_toc_reloc = 1;
5711           break;
5712
5713           /* Marker reloc.  */
5714         case R_PPC64_ENTRY:
5715           break;
5716
5717           /* This relocation describes the C++ object vtable hierarchy.
5718              Reconstruct it for later use during GC.  */
5719         case R_PPC64_GNU_VTINHERIT:
5720           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5721             return FALSE;
5722           break;
5723
5724           /* This relocation describes which C++ vtable entries are actually
5725              used.  Record for later use during GC.  */
5726         case R_PPC64_GNU_VTENTRY:
5727           BFD_ASSERT (h != NULL);
5728           if (h != NULL
5729               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5730             return FALSE;
5731           break;
5732
5733         case R_PPC64_REL14:
5734         case R_PPC64_REL14_BRTAKEN:
5735         case R_PPC64_REL14_BRNTAKEN:
5736           {
5737             asection *dest = NULL;
5738
5739             /* Heuristic: If jumping outside our section, chances are
5740                we are going to need a stub.  */
5741             if (h != NULL)
5742               {
5743                 /* If the sym is weak it may be overridden later, so
5744                    don't assume we know where a weak sym lives.  */
5745                 if (h->root.type == bfd_link_hash_defined)
5746                   dest = h->root.u.def.section;
5747               }
5748             else
5749               {
5750                 Elf_Internal_Sym *isym;
5751
5752                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5753                                               abfd, r_symndx);
5754                 if (isym == NULL)
5755                   return FALSE;
5756
5757                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5758               }
5759
5760             if (dest != sec)
5761               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5762           }
5763           /* Fall through.  */
5764
5765         case R_PPC64_REL24:
5766           plt_list = ifunc;
5767           if (h != NULL)
5768             {
5769               h->needs_plt = 1;
5770               if (h->root.root.string[0] == '.'
5771                   && h->root.root.string[1] != '\0')
5772                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5773
5774               if (h == tga || h == dottga)
5775                 {
5776                   sec->has_tls_reloc = 1;
5777                   if (rel != relocs
5778                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5779                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5780                     /* We have a new-style __tls_get_addr call with
5781                        a marker reloc.  */
5782                     ;
5783                   else
5784                     /* Mark this section as having an old-style call.  */
5785                     sec->has_tls_get_addr_call = 1;
5786                 }
5787               plt_list = &h->plt.plist;
5788             }
5789
5790           /* We may need a .plt entry if the function this reloc
5791              refers to is in a shared lib.  */
5792           if (plt_list
5793               && !update_plt_info (abfd, plt_list, rel->r_addend))
5794             return FALSE;
5795           break;
5796
5797         case R_PPC64_ADDR14:
5798         case R_PPC64_ADDR14_BRNTAKEN:
5799         case R_PPC64_ADDR14_BRTAKEN:
5800         case R_PPC64_ADDR24:
5801           goto dodyn;
5802
5803         case R_PPC64_TPREL64:
5804           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5805           if (bfd_link_dll (info))
5806             info->flags |= DF_STATIC_TLS;
5807           goto dotlstoc;
5808
5809         case R_PPC64_DTPMOD64:
5810           if (rel + 1 < rel_end
5811               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5812               && rel[1].r_offset == rel->r_offset + 8)
5813             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5814           else
5815             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5816           goto dotlstoc;
5817
5818         case R_PPC64_DTPREL64:
5819           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5820           if (rel != relocs
5821               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5822               && rel[-1].r_offset == rel->r_offset - 8)
5823             /* This is the second reloc of a dtpmod, dtprel pair.
5824                Don't mark with TLS_DTPREL.  */
5825             goto dodyn;
5826
5827         dotlstoc:
5828           sec->has_tls_reloc = 1;
5829           if (h != NULL)
5830             {
5831               struct ppc_link_hash_entry *eh;
5832               eh = (struct ppc_link_hash_entry *) h;
5833               eh->tls_mask |= tls_type;
5834             }
5835           else
5836             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5837                                         rel->r_addend, tls_type))
5838               return FALSE;
5839
5840           ppc64_sec = ppc64_elf_section_data (sec);
5841           if (ppc64_sec->sec_type != sec_toc)
5842             {
5843               bfd_size_type amt;
5844
5845               /* One extra to simplify get_tls_mask.  */
5846               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5847               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5848               if (ppc64_sec->u.toc.symndx == NULL)
5849                 return FALSE;
5850               amt = sec->size * sizeof (bfd_vma) / 8;
5851               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5852               if (ppc64_sec->u.toc.add == NULL)
5853                 return FALSE;
5854               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5855               ppc64_sec->sec_type = sec_toc;
5856             }
5857           BFD_ASSERT (rel->r_offset % 8 == 0);
5858           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5859           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5860
5861           /* Mark the second slot of a GD or LD entry.
5862              -1 to indicate GD and -2 to indicate LD.  */
5863           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5864             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5865           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5866             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5867           goto dodyn;
5868
5869         case R_PPC64_TPREL16:
5870         case R_PPC64_TPREL16_LO:
5871         case R_PPC64_TPREL16_HI:
5872         case R_PPC64_TPREL16_HA:
5873         case R_PPC64_TPREL16_DS:
5874         case R_PPC64_TPREL16_LO_DS:
5875         case R_PPC64_TPREL16_HIGH:
5876         case R_PPC64_TPREL16_HIGHA:
5877         case R_PPC64_TPREL16_HIGHER:
5878         case R_PPC64_TPREL16_HIGHERA:
5879         case R_PPC64_TPREL16_HIGHEST:
5880         case R_PPC64_TPREL16_HIGHESTA:
5881           if (bfd_link_dll (info))
5882             info->flags |= DF_STATIC_TLS;
5883           goto dodyn;
5884
5885         case R_PPC64_ADDR64:
5886           if (is_opd
5887               && rel + 1 < rel_end
5888               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5889             {
5890               if (h != NULL)
5891                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5892             }
5893           /* Fall through.  */
5894
5895         case R_PPC64_ADDR16:
5896         case R_PPC64_ADDR16_DS:
5897         case R_PPC64_ADDR16_HA:
5898         case R_PPC64_ADDR16_HI:
5899         case R_PPC64_ADDR16_HIGH:
5900         case R_PPC64_ADDR16_HIGHA:
5901         case R_PPC64_ADDR16_HIGHER:
5902         case R_PPC64_ADDR16_HIGHERA:
5903         case R_PPC64_ADDR16_HIGHEST:
5904         case R_PPC64_ADDR16_HIGHESTA:
5905         case R_PPC64_ADDR16_LO:
5906         case R_PPC64_ADDR16_LO_DS:
5907           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5908               && rel->r_addend == 0)
5909             {
5910               /* We may need a .plt entry if this reloc refers to a
5911                  function in a shared lib.  */
5912               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5913                 return FALSE;
5914               h->pointer_equality_needed = 1;
5915             }
5916           /* Fall through.  */
5917
5918         case R_PPC64_REL30:
5919         case R_PPC64_REL32:
5920         case R_PPC64_REL64:
5921         case R_PPC64_ADDR32:
5922         case R_PPC64_UADDR16:
5923         case R_PPC64_UADDR32:
5924         case R_PPC64_UADDR64:
5925         case R_PPC64_TOC:
5926           if (h != NULL && !bfd_link_pic (info))
5927             /* We may need a copy reloc.  */
5928             h->non_got_ref = 1;
5929
5930           /* Don't propagate .opd relocs.  */
5931           if (NO_OPD_RELOCS && is_opd)
5932             break;
5933
5934           /* If we are creating a shared library, and this is a reloc
5935              against a global symbol, or a non PC relative reloc
5936              against a local symbol, then we need to copy the reloc
5937              into the shared library.  However, if we are linking with
5938              -Bsymbolic, we do not need to copy a reloc against a
5939              global symbol which is defined in an object we are
5940              including in the link (i.e., DEF_REGULAR is set).  At
5941              this point we have not seen all the input files, so it is
5942              possible that DEF_REGULAR is not set now but will be set
5943              later (it is never cleared).  In case of a weak definition,
5944              DEF_REGULAR may be cleared later by a strong definition in
5945              a shared library.  We account for that possibility below by
5946              storing information in the dyn_relocs field of the hash
5947              table entry.  A similar situation occurs when creating
5948              shared libraries and symbol visibility changes render the
5949              symbol local.
5950
5951              If on the other hand, we are creating an executable, we
5952              may need to keep relocations for symbols satisfied by a
5953              dynamic library if we manage to avoid copy relocs for the
5954              symbol.  */
5955         dodyn:
5956           if ((bfd_link_pic (info)
5957                && (must_be_dyn_reloc (info, r_type)
5958                    || (h != NULL
5959                        && (!SYMBOLIC_BIND (info, h)
5960                            || h->root.type == bfd_link_hash_defweak
5961                            || !h->def_regular))))
5962               || (ELIMINATE_COPY_RELOCS
5963                   && !bfd_link_pic (info)
5964                   && h != NULL
5965                   && (h->root.type == bfd_link_hash_defweak
5966                       || !h->def_regular))
5967               || (!bfd_link_pic (info)
5968                   && ifunc != NULL))
5969             {
5970               /* We must copy these reloc types into the output file.
5971                  Create a reloc section in dynobj and make room for
5972                  this reloc.  */
5973               if (sreloc == NULL)
5974                 {
5975                   sreloc = _bfd_elf_make_dynamic_reloc_section
5976                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5977
5978                   if (sreloc == NULL)
5979                     return FALSE;
5980                 }
5981
5982               /* If this is a global symbol, we count the number of
5983                  relocations we need for this symbol.  */
5984               if (h != NULL)
5985                 {
5986                   struct elf_dyn_relocs *p;
5987                   struct elf_dyn_relocs **head;
5988
5989                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5990                   p = *head;
5991                   if (p == NULL || p->sec != sec)
5992                     {
5993                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5994                       if (p == NULL)
5995                         return FALSE;
5996                       p->next = *head;
5997                       *head = p;
5998                       p->sec = sec;
5999                       p->count = 0;
6000                       p->pc_count = 0;
6001                     }
6002                   p->count += 1;
6003                   if (!must_be_dyn_reloc (info, r_type))
6004                     p->pc_count += 1;
6005                 }
6006               else
6007                 {
6008                   /* Track dynamic relocs needed for local syms too.
6009                      We really need local syms available to do this
6010                      easily.  Oh well.  */
6011                   struct ppc_dyn_relocs *p;
6012                   struct ppc_dyn_relocs **head;
6013                   bfd_boolean is_ifunc;
6014                   asection *s;
6015                   void *vpp;
6016                   Elf_Internal_Sym *isym;
6017
6018                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6019                                                 abfd, r_symndx);
6020                   if (isym == NULL)
6021                     return FALSE;
6022
6023                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6024                   if (s == NULL)
6025                     s = sec;
6026
6027                   vpp = &elf_section_data (s)->local_dynrel;
6028                   head = (struct ppc_dyn_relocs **) vpp;
6029                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6030                   p = *head;
6031                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6032                     p = p->next;
6033                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6034                     {
6035                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6036                       if (p == NULL)
6037                         return FALSE;
6038                       p->next = *head;
6039                       *head = p;
6040                       p->sec = sec;
6041                       p->ifunc = is_ifunc;
6042                       p->count = 0;
6043                     }
6044                   p->count += 1;
6045                 }
6046             }
6047           break;
6048
6049         default:
6050           break;
6051         }
6052     }
6053
6054   return TRUE;
6055 }
6056
6057 /* Merge backend specific data from an object file to the output
6058    object file when linking.  */
6059
6060 static bfd_boolean
6061 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6062 {
6063   bfd *obfd = info->output_bfd;
6064   unsigned long iflags, oflags;
6065
6066   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6067     return TRUE;
6068
6069   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6070     return TRUE;
6071
6072   if (!_bfd_generic_verify_endian_match (ibfd, info))
6073     return FALSE;
6074
6075   iflags = elf_elfheader (ibfd)->e_flags;
6076   oflags = elf_elfheader (obfd)->e_flags;
6077
6078   if (iflags & ~EF_PPC64_ABI)
6079     {
6080       _bfd_error_handler
6081         /* xgettext:c-format */
6082         (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6083       bfd_set_error (bfd_error_bad_value);
6084       return FALSE;
6085     }
6086   else if (iflags != oflags && iflags != 0)
6087     {
6088       _bfd_error_handler
6089         /* xgettext:c-format */
6090         (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6091          ibfd, iflags, oflags);
6092       bfd_set_error (bfd_error_bad_value);
6093       return FALSE;
6094     }
6095
6096   _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6097
6098   /* Merge Tag_compatibility attributes and any common GNU ones.  */
6099   _bfd_elf_merge_object_attributes (ibfd, info);
6100
6101   return TRUE;
6102 }
6103
6104 static bfd_boolean
6105 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6106 {
6107   /* Print normal ELF private data.  */
6108   _bfd_elf_print_private_bfd_data (abfd, ptr);
6109
6110   if (elf_elfheader (abfd)->e_flags != 0)
6111     {
6112       FILE *file = ptr;
6113
6114       fprintf (file, _("private flags = 0x%lx:"),
6115                elf_elfheader (abfd)->e_flags);
6116
6117       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6118         fprintf (file, _(" [abiv%ld]"),
6119                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6120       fputc ('\n', file);
6121     }
6122
6123   return TRUE;
6124 }
6125
6126 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6127    of the code entry point, and its section, which must be in the same
6128    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6129
6130 static bfd_vma
6131 opd_entry_value (asection *opd_sec,
6132                  bfd_vma offset,
6133                  asection **code_sec,
6134                  bfd_vma *code_off,
6135                  bfd_boolean in_code_sec)
6136 {
6137   bfd *opd_bfd = opd_sec->owner;
6138   Elf_Internal_Rela *relocs;
6139   Elf_Internal_Rela *lo, *hi, *look;
6140   bfd_vma val;
6141
6142   /* No relocs implies we are linking a --just-symbols object, or looking
6143      at a final linked executable with addr2line or somesuch.  */
6144   if (opd_sec->reloc_count == 0)
6145     {
6146       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6147
6148       if (contents == NULL)
6149         {
6150           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6151             return (bfd_vma) -1;
6152           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6153         }
6154
6155       /* PR 17512: file: 64b9dfbb.  */
6156       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6157         return (bfd_vma) -1;
6158
6159       val = bfd_get_64 (opd_bfd, contents + offset);
6160       if (code_sec != NULL)
6161         {
6162           asection *sec, *likely = NULL;
6163
6164           if (in_code_sec)
6165             {
6166               sec = *code_sec;
6167               if (sec->vma <= val
6168                   && val < sec->vma + sec->size)
6169                 likely = sec;
6170               else
6171                 val = -1;
6172             }
6173           else
6174             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6175               if (sec->vma <= val
6176                   && (sec->flags & SEC_LOAD) != 0
6177                   && (sec->flags & SEC_ALLOC) != 0)
6178                 likely = sec;
6179           if (likely != NULL)
6180             {
6181               *code_sec = likely;
6182               if (code_off != NULL)
6183                 *code_off = val - likely->vma;
6184             }
6185         }
6186       return val;
6187     }
6188
6189   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6190
6191   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6192   if (relocs == NULL)
6193     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6194   /* PR 17512: file: df8e1fd6.  */
6195   if (relocs == NULL)
6196     return (bfd_vma) -1;
6197
6198   /* Go find the opd reloc at the sym address.  */
6199   lo = relocs;
6200   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6201   val = (bfd_vma) -1;
6202   while (lo < hi)
6203     {
6204       look = lo + (hi - lo) / 2;
6205       if (look->r_offset < offset)
6206         lo = look + 1;
6207       else if (look->r_offset > offset)
6208         hi = look;
6209       else
6210         {
6211           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6212
6213           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6214               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6215             {
6216               unsigned long symndx = ELF64_R_SYM (look->r_info);
6217               asection *sec = NULL;
6218
6219               if (symndx >= symtab_hdr->sh_info
6220                   && elf_sym_hashes (opd_bfd) != NULL)
6221                 {
6222                   struct elf_link_hash_entry **sym_hashes;
6223                   struct elf_link_hash_entry *rh;
6224
6225                   sym_hashes = elf_sym_hashes (opd_bfd);
6226                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6227                   if (rh != NULL)
6228                     {
6229                       rh = elf_follow_link (rh);
6230                       if (rh->root.type != bfd_link_hash_defined
6231                           && rh->root.type != bfd_link_hash_defweak)
6232                         break;
6233                       if (rh->root.u.def.section->owner == opd_bfd)
6234                         {
6235                           val = rh->root.u.def.value;
6236                           sec = rh->root.u.def.section;
6237                         }
6238                     }
6239                 }
6240
6241               if (sec == NULL)
6242                 {
6243                   Elf_Internal_Sym *sym;
6244
6245                   if (symndx < symtab_hdr->sh_info)
6246                     {
6247                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6248                       if (sym == NULL)
6249                         {
6250                           size_t symcnt = symtab_hdr->sh_info;
6251                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6252                                                       symcnt, 0,
6253                                                       NULL, NULL, NULL);
6254                           if (sym == NULL)
6255                             break;
6256                           symtab_hdr->contents = (bfd_byte *) sym;
6257                         }
6258                       sym += symndx;
6259                     }
6260                   else
6261                     {
6262                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6263                                                   1, symndx,
6264                                                   NULL, NULL, NULL);
6265                       if (sym == NULL)
6266                         break;
6267                     }
6268                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6269                   if (sec == NULL)
6270                     break;
6271                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6272                   val = sym->st_value;
6273                 }
6274
6275               val += look->r_addend;
6276               if (code_off != NULL)
6277                 *code_off = val;
6278               if (code_sec != NULL)
6279                 {
6280                   if (in_code_sec && *code_sec != sec)
6281                     return -1;
6282                   else
6283                     *code_sec = sec;
6284                 }
6285               if (sec->output_section != NULL)
6286                 val += sec->output_section->vma + sec->output_offset;
6287             }
6288           break;
6289         }
6290     }
6291
6292   return val;
6293 }
6294
6295 /* If the ELF symbol SYM might be a function in SEC, return the
6296    function size and set *CODE_OFF to the function's entry point,
6297    otherwise return zero.  */
6298
6299 static bfd_size_type
6300 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6301                               bfd_vma *code_off)
6302 {
6303   bfd_size_type size;
6304
6305   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6306                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6307     return 0;
6308
6309   size = 0;
6310   if (!(sym->flags & BSF_SYNTHETIC))
6311     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6312
6313   if (strcmp (sym->section->name, ".opd") == 0)
6314     {
6315       struct _opd_sec_data *opd = get_opd_info (sym->section);
6316       bfd_vma symval = sym->value;
6317
6318       if (opd != NULL
6319           && opd->adjust != NULL
6320           && elf_section_data (sym->section)->relocs != NULL)
6321         {
6322           /* opd_entry_value will use cached relocs that have been
6323              adjusted, but with raw symbols.  That means both local
6324              and global symbols need adjusting.  */
6325           long adjust = opd->adjust[OPD_NDX (symval)];
6326           if (adjust == -1)
6327             return 0;
6328           symval += adjust;
6329         }
6330
6331       if (opd_entry_value (sym->section, symval,
6332                            &sec, code_off, TRUE) == (bfd_vma) -1)
6333         return 0;
6334       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6335          symbol.  This size has nothing to do with the code size of the
6336          function, which is what we're supposed to return, but the
6337          code size isn't available without looking up the dot-sym.
6338          However, doing that would be a waste of time particularly
6339          since elf_find_function will look at the dot-sym anyway.
6340          Now, elf_find_function will keep the largest size of any
6341          function sym found at the code address of interest, so return
6342          1 here to avoid it incorrectly caching a larger function size
6343          for a small function.  This does mean we return the wrong
6344          size for a new-ABI function of size 24, but all that does is
6345          disable caching for such functions.  */
6346       if (size == 24)
6347         size = 1;
6348     }
6349   else
6350     {
6351       if (sym->section != sec)
6352         return 0;
6353       *code_off = sym->value;
6354     }
6355   if (size == 0)
6356     size = 1;
6357   return size;
6358 }
6359
6360 /* Return true if symbol is a strong function defined in an ELFv2
6361    object with st_other localentry bits of zero, ie. its local entry
6362    point coincides with its global entry point.  */
6363
6364 static bfd_boolean
6365 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6366 {
6367   return (h != NULL
6368           && h->type == STT_FUNC
6369           && h->root.type == bfd_link_hash_defined
6370           && (STO_PPC64_LOCAL_MASK & h->other) == 0
6371           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6372           && is_ppc64_elf (h->root.u.def.section->owner)
6373           && abiversion (h->root.u.def.section->owner) >= 2);
6374 }
6375
6376 /* Return true if symbol is defined in a regular object file.  */
6377
6378 static bfd_boolean
6379 is_static_defined (struct elf_link_hash_entry *h)
6380 {
6381   return ((h->root.type == bfd_link_hash_defined
6382            || h->root.type == bfd_link_hash_defweak)
6383           && h->root.u.def.section != NULL
6384           && h->root.u.def.section->output_section != NULL);
6385 }
6386
6387 /* If FDH is a function descriptor symbol, return the associated code
6388    entry symbol if it is defined.  Return NULL otherwise.  */
6389
6390 static struct ppc_link_hash_entry *
6391 defined_code_entry (struct ppc_link_hash_entry *fdh)
6392 {
6393   if (fdh->is_func_descriptor)
6394     {
6395       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6396       if (fh->elf.root.type == bfd_link_hash_defined
6397           || fh->elf.root.type == bfd_link_hash_defweak)
6398         return fh;
6399     }
6400   return NULL;
6401 }
6402
6403 /* If FH is a function code entry symbol, return the associated
6404    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6405
6406 static struct ppc_link_hash_entry *
6407 defined_func_desc (struct ppc_link_hash_entry *fh)
6408 {
6409   if (fh->oh != NULL
6410       && fh->oh->is_func_descriptor)
6411     {
6412       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6413       if (fdh->elf.root.type == bfd_link_hash_defined
6414           || fdh->elf.root.type == bfd_link_hash_defweak)
6415         return fdh;
6416     }
6417   return NULL;
6418 }
6419
6420 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6421
6422 /* Garbage collect sections, after first dealing with dot-symbols.  */
6423
6424 static bfd_boolean
6425 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6426 {
6427   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6428
6429   if (htab != NULL && htab->need_func_desc_adj)
6430     {
6431       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6432       htab->need_func_desc_adj = 0;
6433     }
6434   return bfd_elf_gc_sections (abfd, info);
6435 }
6436
6437 /* Mark all our entry sym sections, both opd and code section.  */
6438
6439 static void
6440 ppc64_elf_gc_keep (struct bfd_link_info *info)
6441 {
6442   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6443   struct bfd_sym_chain *sym;
6444
6445   if (htab == NULL)
6446     return;
6447
6448   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6449     {
6450       struct ppc_link_hash_entry *eh, *fh;
6451       asection *sec;
6452
6453       eh = (struct ppc_link_hash_entry *)
6454         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6455       if (eh == NULL)
6456         continue;
6457       if (eh->elf.root.type != bfd_link_hash_defined
6458           && eh->elf.root.type != bfd_link_hash_defweak)
6459         continue;
6460
6461       fh = defined_code_entry (eh);
6462       if (fh != NULL)
6463         {
6464           sec = fh->elf.root.u.def.section;
6465           sec->flags |= SEC_KEEP;
6466         }
6467       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6468                && opd_entry_value (eh->elf.root.u.def.section,
6469                                    eh->elf.root.u.def.value,
6470                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6471         sec->flags |= SEC_KEEP;
6472
6473       sec = eh->elf.root.u.def.section;
6474       sec->flags |= SEC_KEEP;
6475     }
6476 }
6477
6478 /* Mark sections containing dynamically referenced symbols.  When
6479    building shared libraries, we must assume that any visible symbol is
6480    referenced.  */
6481
6482 static bfd_boolean
6483 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6484 {
6485   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6486   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6487   struct ppc_link_hash_entry *fdh;
6488   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6489
6490   /* Dynamic linking info is on the func descriptor sym.  */
6491   fdh = defined_func_desc (eh);
6492   if (fdh != NULL)
6493     eh = fdh;
6494
6495   if ((eh->elf.root.type == bfd_link_hash_defined
6496        || eh->elf.root.type == bfd_link_hash_defweak)
6497       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6498           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6499               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6500               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6501               && (!bfd_link_executable (info)
6502                   || info->gc_keep_exported
6503                   || info->export_dynamic
6504                   || (eh->elf.dynamic
6505                       && d != NULL
6506                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6507               && (eh->elf.versioned >= versioned
6508                   || !bfd_hide_sym_by_version (info->version_info,
6509                                                eh->elf.root.root.string)))))
6510     {
6511       asection *code_sec;
6512       struct ppc_link_hash_entry *fh;
6513
6514       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6515
6516       /* Function descriptor syms cause the associated
6517          function code sym section to be marked.  */
6518       fh = defined_code_entry (eh);
6519       if (fh != NULL)
6520         {
6521           code_sec = fh->elf.root.u.def.section;
6522           code_sec->flags |= SEC_KEEP;
6523         }
6524       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6525                && opd_entry_value (eh->elf.root.u.def.section,
6526                                    eh->elf.root.u.def.value,
6527                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6528         code_sec->flags |= SEC_KEEP;
6529     }
6530
6531   return TRUE;
6532 }
6533
6534 /* Return the section that should be marked against GC for a given
6535    relocation.  */
6536
6537 static asection *
6538 ppc64_elf_gc_mark_hook (asection *sec,
6539                         struct bfd_link_info *info,
6540                         Elf_Internal_Rela *rel,
6541                         struct elf_link_hash_entry *h,
6542                         Elf_Internal_Sym *sym)
6543 {
6544   asection *rsec;
6545
6546   /* Syms return NULL if we're marking .opd, so we avoid marking all
6547      function sections, as all functions are referenced in .opd.  */
6548   rsec = NULL;
6549   if (get_opd_info (sec) != NULL)
6550     return rsec;
6551
6552   if (h != NULL)
6553     {
6554       enum elf_ppc64_reloc_type r_type;
6555       struct ppc_link_hash_entry *eh, *fh, *fdh;
6556
6557       r_type = ELF64_R_TYPE (rel->r_info);
6558       switch (r_type)
6559         {
6560         case R_PPC64_GNU_VTINHERIT:
6561         case R_PPC64_GNU_VTENTRY:
6562           break;
6563
6564         default:
6565           switch (h->root.type)
6566             {
6567             case bfd_link_hash_defined:
6568             case bfd_link_hash_defweak:
6569               eh = (struct ppc_link_hash_entry *) h;
6570               fdh = defined_func_desc (eh);
6571               if (fdh != NULL)
6572                 {
6573                   /* -mcall-aixdesc code references the dot-symbol on
6574                      a call reloc.  Mark the function descriptor too
6575                      against garbage collection.  */
6576                   fdh->elf.mark = 1;
6577                   if (fdh->elf.is_weakalias)
6578                     weakdef (&fdh->elf)->mark = 1;
6579                   eh = fdh;
6580                 }
6581
6582               /* Function descriptor syms cause the associated
6583                  function code sym section to be marked.  */
6584               fh = defined_code_entry (eh);
6585               if (fh != NULL)
6586                 {
6587                   /* They also mark their opd section.  */
6588                   eh->elf.root.u.def.section->gc_mark = 1;
6589
6590                   rsec = fh->elf.root.u.def.section;
6591                 }
6592               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6593                        && opd_entry_value (eh->elf.root.u.def.section,
6594                                            eh->elf.root.u.def.value,
6595                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6596                 eh->elf.root.u.def.section->gc_mark = 1;
6597               else
6598                 rsec = h->root.u.def.section;
6599               break;
6600
6601             case bfd_link_hash_common:
6602               rsec = h->root.u.c.p->section;
6603               break;
6604
6605             default:
6606               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6607             }
6608         }
6609     }
6610   else
6611     {
6612       struct _opd_sec_data *opd;
6613
6614       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6615       opd = get_opd_info (rsec);
6616       if (opd != NULL && opd->func_sec != NULL)
6617         {
6618           rsec->gc_mark = 1;
6619
6620           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6621         }
6622     }
6623
6624   return rsec;
6625 }
6626
6627 /* The maximum size of .sfpr.  */
6628 #define SFPR_MAX (218*4)
6629
6630 struct sfpr_def_parms
6631 {
6632   const char name[12];
6633   unsigned char lo, hi;
6634   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6635   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6636 };
6637
6638 /* Auto-generate _save*, _rest* functions in .sfpr.
6639    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6640    instead.  */
6641
6642 static bfd_boolean
6643 sfpr_define (struct bfd_link_info *info,
6644              const struct sfpr_def_parms *parm,
6645              asection *stub_sec)
6646 {
6647   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6648   unsigned int i;
6649   size_t len = strlen (parm->name);
6650   bfd_boolean writing = FALSE;
6651   char sym[16];
6652
6653   if (htab == NULL)
6654     return FALSE;
6655
6656   memcpy (sym, parm->name, len);
6657   sym[len + 2] = 0;
6658
6659   for (i = parm->lo; i <= parm->hi; i++)
6660     {
6661       struct ppc_link_hash_entry *h;
6662
6663       sym[len + 0] = i / 10 + '0';
6664       sym[len + 1] = i % 10 + '0';
6665       h = (struct ppc_link_hash_entry *)
6666         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6667       if (stub_sec != NULL)
6668         {
6669           if (h != NULL
6670               && h->elf.root.type == bfd_link_hash_defined
6671               && h->elf.root.u.def.section == htab->sfpr)
6672             {
6673               struct elf_link_hash_entry *s;
6674               char buf[32];
6675               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6676               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6677               if (s == NULL)
6678                 return FALSE;
6679               if (s->root.type == bfd_link_hash_new
6680                   || (s->root.type = bfd_link_hash_defined
6681                       && s->root.u.def.section == stub_sec))
6682                 {
6683                   s->root.type = bfd_link_hash_defined;
6684                   s->root.u.def.section = stub_sec;
6685                   s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6686                                          + h->elf.root.u.def.value);
6687                   s->ref_regular = 1;
6688                   s->def_regular = 1;
6689                   s->ref_regular_nonweak = 1;
6690                   s->forced_local = 1;
6691                   s->non_elf = 0;
6692                   s->root.linker_def = 1;
6693                 }
6694             }
6695           continue;
6696         }
6697       if (h != NULL)
6698         {
6699           h->save_res = 1;
6700           if (!h->elf.def_regular)
6701             {
6702               h->elf.root.type = bfd_link_hash_defined;
6703               h->elf.root.u.def.section = htab->sfpr;
6704               h->elf.root.u.def.value = htab->sfpr->size;
6705               h->elf.type = STT_FUNC;
6706               h->elf.def_regular = 1;
6707               h->elf.non_elf = 0;
6708               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6709               writing = TRUE;
6710               if (htab->sfpr->contents == NULL)
6711                 {
6712                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6713                   if (htab->sfpr->contents == NULL)
6714                     return FALSE;
6715                 }
6716             }
6717         }
6718       if (writing)
6719         {
6720           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6721           if (i != parm->hi)
6722             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6723           else
6724             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6725           htab->sfpr->size = p - htab->sfpr->contents;
6726         }
6727     }
6728
6729   return TRUE;
6730 }
6731
6732 static bfd_byte *
6733 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6734 {
6735   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6736   return p + 4;
6737 }
6738
6739 static bfd_byte *
6740 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6741 {
6742   p = savegpr0 (abfd, p, r);
6743   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6744   p = p + 4;
6745   bfd_put_32 (abfd, BLR, p);
6746   return p + 4;
6747 }
6748
6749 static bfd_byte *
6750 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6751 {
6752   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6753   return p + 4;
6754 }
6755
6756 static bfd_byte *
6757 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6758 {
6759   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6760   p = p + 4;
6761   p = restgpr0 (abfd, p, r);
6762   bfd_put_32 (abfd, MTLR_R0, p);
6763   p = p + 4;
6764   if (r == 29)
6765     {
6766       p = restgpr0 (abfd, p, 30);
6767       p = restgpr0 (abfd, p, 31);
6768     }
6769   bfd_put_32 (abfd, BLR, p);
6770   return p + 4;
6771 }
6772
6773 static bfd_byte *
6774 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6775 {
6776   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6777   return p + 4;
6778 }
6779
6780 static bfd_byte *
6781 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6782 {
6783   p = savegpr1 (abfd, p, r);
6784   bfd_put_32 (abfd, BLR, p);
6785   return p + 4;
6786 }
6787
6788 static bfd_byte *
6789 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6790 {
6791   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6792   return p + 4;
6793 }
6794
6795 static bfd_byte *
6796 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6797 {
6798   p = restgpr1 (abfd, p, r);
6799   bfd_put_32 (abfd, BLR, p);
6800   return p + 4;
6801 }
6802
6803 static bfd_byte *
6804 savefpr (bfd *abfd, bfd_byte *p, int r)
6805 {
6806   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6807   return p + 4;
6808 }
6809
6810 static bfd_byte *
6811 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6812 {
6813   p = savefpr (abfd, p, r);
6814   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6815   p = p + 4;
6816   bfd_put_32 (abfd, BLR, p);
6817   return p + 4;
6818 }
6819
6820 static bfd_byte *
6821 restfpr (bfd *abfd, bfd_byte *p, int r)
6822 {
6823   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6824   return p + 4;
6825 }
6826
6827 static bfd_byte *
6828 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6829 {
6830   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6831   p = p + 4;
6832   p = restfpr (abfd, p, r);
6833   bfd_put_32 (abfd, MTLR_R0, p);
6834   p = p + 4;
6835   if (r == 29)
6836     {
6837       p = restfpr (abfd, p, 30);
6838       p = restfpr (abfd, p, 31);
6839     }
6840   bfd_put_32 (abfd, BLR, p);
6841   return p + 4;
6842 }
6843
6844 static bfd_byte *
6845 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6846 {
6847   p = savefpr (abfd, p, r);
6848   bfd_put_32 (abfd, BLR, p);
6849   return p + 4;
6850 }
6851
6852 static bfd_byte *
6853 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6854 {
6855   p = restfpr (abfd, p, r);
6856   bfd_put_32 (abfd, BLR, p);
6857   return p + 4;
6858 }
6859
6860 static bfd_byte *
6861 savevr (bfd *abfd, bfd_byte *p, int r)
6862 {
6863   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6864   p = p + 4;
6865   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6866   return p + 4;
6867 }
6868
6869 static bfd_byte *
6870 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6871 {
6872   p = savevr (abfd, p, r);
6873   bfd_put_32 (abfd, BLR, p);
6874   return p + 4;
6875 }
6876
6877 static bfd_byte *
6878 restvr (bfd *abfd, bfd_byte *p, int r)
6879 {
6880   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6881   p = p + 4;
6882   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6883   return p + 4;
6884 }
6885
6886 static bfd_byte *
6887 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6888 {
6889   p = restvr (abfd, p, r);
6890   bfd_put_32 (abfd, BLR, p);
6891   return p + 4;
6892 }
6893
6894 /* Called via elf_link_hash_traverse to transfer dynamic linking
6895    information on function code symbol entries to their corresponding
6896    function descriptor symbol entries.  */
6897
6898 static bfd_boolean
6899 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6900 {
6901   struct bfd_link_info *info;
6902   struct ppc_link_hash_table *htab;
6903   struct ppc_link_hash_entry *fh;
6904   struct ppc_link_hash_entry *fdh;
6905   bfd_boolean force_local;
6906
6907   fh = (struct ppc_link_hash_entry *) h;
6908   if (fh->elf.root.type == bfd_link_hash_indirect)
6909     return TRUE;
6910
6911   if (!fh->is_func)
6912     return TRUE;
6913
6914   if (fh->elf.root.root.string[0] != '.'
6915       || fh->elf.root.root.string[1] == '\0')
6916     return TRUE;
6917
6918   info = inf;
6919   htab = ppc_hash_table (info);
6920   if (htab == NULL)
6921     return FALSE;
6922
6923   /* Find the corresponding function descriptor symbol.  */
6924   fdh = lookup_fdh (fh, htab);
6925
6926   /* Resolve undefined references to dot-symbols as the value
6927      in the function descriptor, if we have one in a regular object.
6928      This is to satisfy cases like ".quad .foo".  Calls to functions
6929      in dynamic objects are handled elsewhere.  */
6930   if ((fh->elf.root.type == bfd_link_hash_undefined
6931        || fh->elf.root.type == bfd_link_hash_undefweak)
6932       && (fdh->elf.root.type == bfd_link_hash_defined
6933           || fdh->elf.root.type == bfd_link_hash_defweak)
6934       && get_opd_info (fdh->elf.root.u.def.section) != NULL
6935       && opd_entry_value (fdh->elf.root.u.def.section,
6936                           fdh->elf.root.u.def.value,
6937                           &fh->elf.root.u.def.section,
6938                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6939     {
6940       fh->elf.root.type = fdh->elf.root.type;
6941       fh->elf.forced_local = 1;
6942       fh->elf.def_regular = fdh->elf.def_regular;
6943       fh->elf.def_dynamic = fdh->elf.def_dynamic;
6944     }
6945
6946   if (!fh->elf.dynamic)
6947     {
6948       struct plt_entry *ent;
6949
6950       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6951         if (ent->plt.refcount > 0)
6952           break;
6953       if (ent == NULL)
6954         return TRUE;
6955     }
6956
6957   /* Create a descriptor as undefined if necessary.  */
6958   if (fdh == NULL
6959       && !bfd_link_executable (info)
6960       && (fh->elf.root.type == bfd_link_hash_undefined
6961           || fh->elf.root.type == bfd_link_hash_undefweak))
6962     {
6963       fdh = make_fdh (info, fh);
6964       if (fdh == NULL)
6965         return FALSE;
6966     }
6967
6968   /* We can't support overriding of symbols on a fake descriptor.  */
6969   if (fdh != NULL
6970       && fdh->fake
6971       && (fh->elf.root.type == bfd_link_hash_defined
6972           || fh->elf.root.type == bfd_link_hash_defweak))
6973     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6974
6975   /* Transfer dynamic linking information to the function descriptor.  */
6976   if (fdh != NULL)
6977     {
6978       fdh->elf.ref_regular |= fh->elf.ref_regular;
6979       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6980       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6981       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6982       fdh->elf.dynamic |= fh->elf.dynamic;
6983       fdh->elf.needs_plt |= (fh->elf.needs_plt
6984                              || fh->elf.type == STT_FUNC
6985                              || fh->elf.type == STT_GNU_IFUNC);
6986       move_plt_plist (fh, fdh);
6987
6988       if (!fdh->elf.forced_local
6989           && fh->elf.dynindx != -1)
6990         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6991           return FALSE;
6992     }
6993
6994   /* Now that the info is on the function descriptor, clear the
6995      function code sym info.  Any function code syms for which we
6996      don't have a definition in a regular file, we force local.
6997      This prevents a shared library from exporting syms that have
6998      been imported from another library.  Function code syms that
6999      are really in the library we must leave global to prevent the
7000      linker dragging in a definition from a static library.  */
7001   force_local = (!fh->elf.def_regular
7002                  || fdh == NULL
7003                  || !fdh->elf.def_regular
7004                  || fdh->elf.forced_local);
7005   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7006
7007   return TRUE;
7008 }
7009
7010 static const struct sfpr_def_parms save_res_funcs[] =
7011   {
7012     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7013     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7014     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7015     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7016     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7017     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7018     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7019     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7020     { "._savef", 14, 31, savefpr, savefpr1_tail },
7021     { "._restf", 14, 31, restfpr, restfpr1_tail },
7022     { "_savevr_", 20, 31, savevr, savevr_tail },
7023     { "_restvr_", 20, 31, restvr, restvr_tail }
7024   };
7025
7026 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
7027    this hook to a) provide some gcc support functions, and b) transfer
7028    dynamic linking information gathered so far on function code symbol
7029    entries, to their corresponding function descriptor symbol entries.  */
7030
7031 static bfd_boolean
7032 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7033                             struct bfd_link_info *info)
7034 {
7035   struct ppc_link_hash_table *htab;
7036
7037   htab = ppc_hash_table (info);
7038   if (htab == NULL)
7039     return FALSE;
7040
7041   /* Provide any missing _save* and _rest* functions.  */
7042   if (htab->sfpr != NULL)
7043     {
7044       unsigned int i;
7045
7046       htab->sfpr->size = 0;
7047       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7048         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7049           return FALSE;
7050       if (htab->sfpr->size == 0)
7051         htab->sfpr->flags |= SEC_EXCLUDE;
7052     }
7053
7054   if (bfd_link_relocatable (info))
7055     return TRUE;
7056
7057   if (htab->elf.hgot != NULL)
7058     {
7059       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7060       /* Make .TOC. defined so as to prevent it being made dynamic.
7061          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7062       if (!htab->elf.hgot->def_regular
7063           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7064         {
7065           htab->elf.hgot->root.type = bfd_link_hash_defined;
7066           htab->elf.hgot->root.u.def.value = 0;
7067           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7068           htab->elf.hgot->def_regular = 1;
7069           htab->elf.hgot->root.linker_def = 1;
7070         }
7071       htab->elf.hgot->type = STT_OBJECT;
7072       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7073                                | STV_HIDDEN);
7074     }
7075
7076   if (htab->need_func_desc_adj)
7077     {
7078       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7079       htab->need_func_desc_adj = 0;
7080     }
7081
7082   return TRUE;
7083 }
7084
7085 /* Find dynamic relocs for H that apply to read-only sections.  */
7086
7087 static asection *
7088 readonly_dynrelocs (struct elf_link_hash_entry *h)
7089 {
7090   struct ppc_link_hash_entry *eh;
7091   struct elf_dyn_relocs *p;
7092
7093   eh = (struct ppc_link_hash_entry *) h;
7094   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7095     {
7096       asection *s = p->sec->output_section;
7097
7098       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7099         return p->sec;
7100     }
7101   return NULL;
7102 }
7103
7104 /* Return true if we have dynamic relocs against H or any of its weak
7105    aliases, that apply to read-only sections.  Cannot be used after
7106    size_dynamic_sections.  */
7107
7108 static bfd_boolean
7109 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7110 {
7111   struct ppc_link_hash_entry *eh;
7112
7113   eh = (struct ppc_link_hash_entry *) h;
7114   do
7115     {
7116       if (readonly_dynrelocs (&eh->elf))
7117         return TRUE;
7118       eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7119     } while (eh != NULL && &eh->elf != h);
7120
7121   return FALSE;
7122 }
7123
7124 /* Return whether EH has pc-relative dynamic relocs.  */
7125
7126 static bfd_boolean
7127 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7128 {
7129   struct elf_dyn_relocs *p;
7130
7131   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7132     if (p->pc_count != 0)
7133       return TRUE;
7134   return FALSE;
7135 }
7136
7137 /* Return true if a global entry stub will be created for H.  Valid
7138    for ELFv2 before plt entries have been allocated.  */
7139
7140 static bfd_boolean
7141 global_entry_stub (struct elf_link_hash_entry *h)
7142 {
7143   struct plt_entry *pent;
7144
7145   if (!h->pointer_equality_needed
7146       || h->def_regular)
7147     return FALSE;
7148
7149   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7150     if (pent->plt.refcount > 0
7151         && pent->addend == 0)
7152       return TRUE;
7153
7154   return FALSE;
7155 }
7156
7157 /* Adjust a symbol defined by a dynamic object and referenced by a
7158    regular object.  The current definition is in some section of the
7159    dynamic object, but we're not including those sections.  We have to
7160    change the definition to something the rest of the link can
7161    understand.  */
7162
7163 static bfd_boolean
7164 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7165                                  struct elf_link_hash_entry *h)
7166 {
7167   struct ppc_link_hash_table *htab;
7168   asection *s, *srel;
7169
7170   htab = ppc_hash_table (info);
7171   if (htab == NULL)
7172     return FALSE;
7173
7174   /* Deal with function syms.  */
7175   if (h->type == STT_FUNC
7176       || h->type == STT_GNU_IFUNC
7177       || h->needs_plt)
7178     {
7179       bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7180                            || SYMBOL_CALLS_LOCAL (info, h)
7181                            || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7182       /* Discard dyn_relocs when non-pic if we've decided that a
7183          function symbol is local and not an ifunc.  We keep dynamic
7184          relocs for ifuncs when local rather than always emitting a
7185          plt call stub for them and defining the symbol on the call
7186          stub.  We can't do that for ELFv1 anyway (a function symbol
7187          is defined on a descriptor, not code) and it can be faster at
7188          run-time due to not needing to bounce through a stub.  The
7189          dyn_relocs for ifuncs will be applied even in a static
7190          executable.  */
7191       if (!bfd_link_pic (info)
7192           && h->type != STT_GNU_IFUNC
7193           && local)
7194         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7195
7196       /* Clear procedure linkage table information for any symbol that
7197          won't need a .plt entry.  */
7198       struct plt_entry *ent;
7199       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7200         if (ent->plt.refcount > 0)
7201           break;
7202       if (ent == NULL
7203           || (h->type != STT_GNU_IFUNC && local))
7204         {
7205           h->plt.plist = NULL;
7206           h->needs_plt = 0;
7207           h->pointer_equality_needed = 0;
7208         }
7209       else if (abiversion (info->output_bfd) >= 2)
7210         {
7211           /* Taking a function's address in a read/write section
7212              doesn't require us to define the function symbol in the
7213              executable on a global entry stub.  A dynamic reloc can
7214              be used instead.  The reason we prefer a few more dynamic
7215              relocs is that calling via a global entry stub costs a
7216              few more instructions, and pointer_equality_needed causes
7217              extra work in ld.so when resolving these symbols.  */
7218           if (global_entry_stub (h))
7219             {
7220               if (!readonly_dynrelocs (h))
7221                 {
7222                   h->pointer_equality_needed = 0;
7223                   /* If we haven't seen a branch reloc then we don't need
7224                      a plt entry.  */
7225                   if (!h->needs_plt)
7226                     h->plt.plist = NULL;
7227                 }
7228               else if (!bfd_link_pic (info))
7229                 /* We are going to be defining the function symbol on the
7230                    plt stub, so no dyn_relocs needed when non-pic.  */
7231                 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7232             }
7233
7234           /* ELFv2 function symbols can't have copy relocs.  */
7235           return TRUE;
7236         }
7237       else if (!h->needs_plt
7238                && !readonly_dynrelocs (h))
7239         {
7240           /* If we haven't seen a branch reloc then we don't need a
7241              plt entry.  */
7242           h->plt.plist = NULL;
7243           h->pointer_equality_needed = 0;
7244           return TRUE;
7245         }
7246     }
7247   else
7248     h->plt.plist = NULL;
7249
7250   /* If this is a weak symbol, and there is a real definition, the
7251      processor independent code will have arranged for us to see the
7252      real definition first, and we can just use the same value.  */
7253   if (h->is_weakalias)
7254     {
7255       struct elf_link_hash_entry *def = weakdef (h);
7256       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7257       h->root.u.def.section = def->root.u.def.section;
7258       h->root.u.def.value = def->root.u.def.value;
7259       if (def->root.u.def.section == htab->elf.sdynbss
7260           || def->root.u.def.section == htab->elf.sdynrelro)
7261         ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7262       return TRUE;
7263     }
7264
7265   /* If we are creating a shared library, we must presume that the
7266      only references to the symbol are via the global offset table.
7267      For such cases we need not do anything here; the relocations will
7268      be handled correctly by relocate_section.  */
7269   if (bfd_link_pic (info))
7270     return TRUE;
7271
7272   /* If there are no references to this symbol that do not use the
7273      GOT, we don't need to generate a copy reloc.  */
7274   if (!h->non_got_ref)
7275     return TRUE;
7276
7277   /* Don't generate a copy reloc for symbols defined in the executable.  */
7278   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7279
7280       /* If -z nocopyreloc was given, don't generate them either.  */
7281       || info->nocopyreloc
7282
7283       /* If we don't find any dynamic relocs in read-only sections, then
7284          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7285       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7286
7287       /* Protected variables do not work with .dynbss.  The copy in
7288          .dynbss won't be used by the shared library with the protected
7289          definition for the variable.  Text relocations are preferable
7290          to an incorrect program.  */
7291       || h->protected_def)
7292     return TRUE;
7293
7294   if (h->plt.plist != NULL)
7295     {
7296       /* We should never get here, but unfortunately there are versions
7297          of gcc out there that improperly (for this ABI) put initialized
7298          function pointers, vtable refs and suchlike in read-only
7299          sections.  Allow them to proceed, but warn that this might
7300          break at runtime.  */
7301       info->callbacks->einfo
7302         (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7303            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7304          h->root.root.string);
7305     }
7306
7307   /* This is a reference to a symbol defined by a dynamic object which
7308      is not a function.  */
7309
7310   /* We must allocate the symbol in our .dynbss section, which will
7311      become part of the .bss section of the executable.  There will be
7312      an entry for this symbol in the .dynsym section.  The dynamic
7313      object will contain position independent code, so all references
7314      from the dynamic object to this symbol will go through the global
7315      offset table.  The dynamic linker will use the .dynsym entry to
7316      determine the address it must put in the global offset table, so
7317      both the dynamic object and the regular object will refer to the
7318      same memory location for the variable.  */
7319   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7320     {
7321       s = htab->elf.sdynrelro;
7322       srel = htab->elf.sreldynrelro;
7323     }
7324   else
7325     {
7326       s = htab->elf.sdynbss;
7327       srel = htab->elf.srelbss;
7328     }
7329   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7330     {
7331       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7332          linker to copy the initial value out of the dynamic object
7333          and into the runtime process image.  */
7334       srel->size += sizeof (Elf64_External_Rela);
7335       h->needs_copy = 1;
7336     }
7337
7338   /* We no longer want dyn_relocs.  */
7339   ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7340   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7341 }
7342
7343 /* If given a function descriptor symbol, hide both the function code
7344    sym and the descriptor.  */
7345 static void
7346 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7347                        struct elf_link_hash_entry *h,
7348                        bfd_boolean force_local)
7349 {
7350   struct ppc_link_hash_entry *eh;
7351   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7352
7353   eh = (struct ppc_link_hash_entry *) h;
7354   if (eh->is_func_descriptor)
7355     {
7356       struct ppc_link_hash_entry *fh = eh->oh;
7357
7358       if (fh == NULL)
7359         {
7360           const char *p, *q;
7361           struct elf_link_hash_table *htab = elf_hash_table (info);
7362           char save;
7363
7364           /* We aren't supposed to use alloca in BFD because on
7365              systems which do not have alloca the version in libiberty
7366              calls xmalloc, which might cause the program to crash
7367              when it runs out of memory.  This function doesn't have a
7368              return status, so there's no way to gracefully return an
7369              error.  So cheat.  We know that string[-1] can be safely
7370              accessed;  It's either a string in an ELF string table,
7371              or allocated in an objalloc structure.  */
7372
7373           p = eh->elf.root.root.string - 1;
7374           save = *p;
7375           *(char *) p = '.';
7376           fh = (struct ppc_link_hash_entry *)
7377             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7378           *(char *) p = save;
7379
7380           /* Unfortunately, if it so happens that the string we were
7381              looking for was allocated immediately before this string,
7382              then we overwrote the string terminator.  That's the only
7383              reason the lookup should fail.  */
7384           if (fh == NULL)
7385             {
7386               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7387               while (q >= eh->elf.root.root.string && *q == *p)
7388                 --q, --p;
7389               if (q < eh->elf.root.root.string && *p == '.')
7390                 fh = (struct ppc_link_hash_entry *)
7391                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7392             }
7393           if (fh != NULL)
7394             {
7395               eh->oh = fh;
7396               fh->oh = eh;
7397             }
7398         }
7399       if (fh != NULL)
7400         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7401     }
7402 }
7403
7404 static bfd_boolean
7405 get_sym_h (struct elf_link_hash_entry **hp,
7406            Elf_Internal_Sym **symp,
7407            asection **symsecp,
7408            unsigned char **tls_maskp,
7409            Elf_Internal_Sym **locsymsp,
7410            unsigned long r_symndx,
7411            bfd *ibfd)
7412 {
7413   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7414
7415   if (r_symndx >= symtab_hdr->sh_info)
7416     {
7417       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7418       struct elf_link_hash_entry *h;
7419
7420       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7421       h = elf_follow_link (h);
7422
7423       if (hp != NULL)
7424         *hp = h;
7425
7426       if (symp != NULL)
7427         *symp = NULL;
7428
7429       if (symsecp != NULL)
7430         {
7431           asection *symsec = NULL;
7432           if (h->root.type == bfd_link_hash_defined
7433               || h->root.type == bfd_link_hash_defweak)
7434             symsec = h->root.u.def.section;
7435           *symsecp = symsec;
7436         }
7437
7438       if (tls_maskp != NULL)
7439         {
7440           struct ppc_link_hash_entry *eh;
7441
7442           eh = (struct ppc_link_hash_entry *) h;
7443           *tls_maskp = &eh->tls_mask;
7444         }
7445     }
7446   else
7447     {
7448       Elf_Internal_Sym *sym;
7449       Elf_Internal_Sym *locsyms = *locsymsp;
7450
7451       if (locsyms == NULL)
7452         {
7453           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7454           if (locsyms == NULL)
7455             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7456                                             symtab_hdr->sh_info,
7457                                             0, NULL, NULL, NULL);
7458           if (locsyms == NULL)
7459             return FALSE;
7460           *locsymsp = locsyms;
7461         }
7462       sym = locsyms + r_symndx;
7463
7464       if (hp != NULL)
7465         *hp = NULL;
7466
7467       if (symp != NULL)
7468         *symp = sym;
7469
7470       if (symsecp != NULL)
7471         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7472
7473       if (tls_maskp != NULL)
7474         {
7475           struct got_entry **lgot_ents;
7476           unsigned char *tls_mask;
7477
7478           tls_mask = NULL;
7479           lgot_ents = elf_local_got_ents (ibfd);
7480           if (lgot_ents != NULL)
7481             {
7482               struct plt_entry **local_plt = (struct plt_entry **)
7483                 (lgot_ents + symtab_hdr->sh_info);
7484               unsigned char *lgot_masks = (unsigned char *)
7485                 (local_plt + symtab_hdr->sh_info);
7486               tls_mask = &lgot_masks[r_symndx];
7487             }
7488           *tls_maskp = tls_mask;
7489         }
7490     }
7491   return TRUE;
7492 }
7493
7494 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7495    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7496    type suitable for optimization, and 1 otherwise.  */
7497
7498 static int
7499 get_tls_mask (unsigned char **tls_maskp,
7500               unsigned long *toc_symndx,
7501               bfd_vma *toc_addend,
7502               Elf_Internal_Sym **locsymsp,
7503               const Elf_Internal_Rela *rel,
7504               bfd *ibfd)
7505 {
7506   unsigned long r_symndx;
7507   int next_r;
7508   struct elf_link_hash_entry *h;
7509   Elf_Internal_Sym *sym;
7510   asection *sec;
7511   bfd_vma off;
7512
7513   r_symndx = ELF64_R_SYM (rel->r_info);
7514   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7515     return 0;
7516
7517   if ((*tls_maskp != NULL
7518        && (**tls_maskp & TLS_TLS) != 0
7519        && **tls_maskp != (TLS_TLS | TLS_MARK))
7520       || sec == NULL
7521       || ppc64_elf_section_data (sec) == NULL
7522       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7523     return 1;
7524
7525   /* Look inside a TOC section too.  */
7526   if (h != NULL)
7527     {
7528       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7529       off = h->root.u.def.value;
7530     }
7531   else
7532     off = sym->st_value;
7533   off += rel->r_addend;
7534   BFD_ASSERT (off % 8 == 0);
7535   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7536   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7537   if (toc_symndx != NULL)
7538     *toc_symndx = r_symndx;
7539   if (toc_addend != NULL)
7540     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7541   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7542     return 0;
7543   if ((h == NULL || is_static_defined (h))
7544       && (next_r == -1 || next_r == -2))
7545     return 1 - next_r;
7546   return 1;
7547 }
7548
7549 /* Find (or create) an entry in the tocsave hash table.  */
7550
7551 static struct tocsave_entry *
7552 tocsave_find (struct ppc_link_hash_table *htab,
7553               enum insert_option insert,
7554               Elf_Internal_Sym **local_syms,
7555               const Elf_Internal_Rela *irela,
7556               bfd *ibfd)
7557 {
7558   unsigned long r_indx;
7559   struct elf_link_hash_entry *h;
7560   Elf_Internal_Sym *sym;
7561   struct tocsave_entry ent, *p;
7562   hashval_t hash;
7563   struct tocsave_entry **slot;
7564
7565   r_indx = ELF64_R_SYM (irela->r_info);
7566   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7567     return NULL;
7568   if (ent.sec == NULL || ent.sec->output_section == NULL)
7569     {
7570       _bfd_error_handler
7571         (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7572       return NULL;
7573     }
7574
7575   if (h != NULL)
7576     ent.offset = h->root.u.def.value;
7577   else
7578     ent.offset = sym->st_value;
7579   ent.offset += irela->r_addend;
7580
7581   hash = tocsave_htab_hash (&ent);
7582   slot = ((struct tocsave_entry **)
7583           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7584   if (slot == NULL)
7585     return NULL;
7586
7587   if (*slot == NULL)
7588     {
7589       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7590       if (p == NULL)
7591         return NULL;
7592       *p = ent;
7593       *slot = p;
7594     }
7595   return *slot;
7596 }
7597
7598 /* Adjust all global syms defined in opd sections.  In gcc generated
7599    code for the old ABI, these will already have been done.  */
7600
7601 static bfd_boolean
7602 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7603 {
7604   struct ppc_link_hash_entry *eh;
7605   asection *sym_sec;
7606   struct _opd_sec_data *opd;
7607
7608   if (h->root.type == bfd_link_hash_indirect)
7609     return TRUE;
7610
7611   if (h->root.type != bfd_link_hash_defined
7612       && h->root.type != bfd_link_hash_defweak)
7613     return TRUE;
7614
7615   eh = (struct ppc_link_hash_entry *) h;
7616   if (eh->adjust_done)
7617     return TRUE;
7618
7619   sym_sec = eh->elf.root.u.def.section;
7620   opd = get_opd_info (sym_sec);
7621   if (opd != NULL && opd->adjust != NULL)
7622     {
7623       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7624       if (adjust == -1)
7625         {
7626           /* This entry has been deleted.  */
7627           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7628           if (dsec == NULL)
7629             {
7630               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7631                 if (discarded_section (dsec))
7632                   {
7633                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7634                     break;
7635                   }
7636             }
7637           eh->elf.root.u.def.value = 0;
7638           eh->elf.root.u.def.section = dsec;
7639         }
7640       else
7641         eh->elf.root.u.def.value += adjust;
7642       eh->adjust_done = 1;
7643     }
7644   return TRUE;
7645 }
7646
7647 /* Handles decrementing dynamic reloc counts for the reloc specified by
7648    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7649    have already been determined.  */
7650
7651 static bfd_boolean
7652 dec_dynrel_count (bfd_vma r_info,
7653                   asection *sec,
7654                   struct bfd_link_info *info,
7655                   Elf_Internal_Sym **local_syms,
7656                   struct elf_link_hash_entry *h,
7657                   Elf_Internal_Sym *sym)
7658 {
7659   enum elf_ppc64_reloc_type r_type;
7660   asection *sym_sec = NULL;
7661
7662   /* Can this reloc be dynamic?  This switch, and later tests here
7663      should be kept in sync with the code in check_relocs.  */
7664   r_type = ELF64_R_TYPE (r_info);
7665   switch (r_type)
7666     {
7667     default:
7668       return TRUE;
7669
7670     case R_PPC64_TPREL16:
7671     case R_PPC64_TPREL16_LO:
7672     case R_PPC64_TPREL16_HI:
7673     case R_PPC64_TPREL16_HA:
7674     case R_PPC64_TPREL16_DS:
7675     case R_PPC64_TPREL16_LO_DS:
7676     case R_PPC64_TPREL16_HIGH:
7677     case R_PPC64_TPREL16_HIGHA:
7678     case R_PPC64_TPREL16_HIGHER:
7679     case R_PPC64_TPREL16_HIGHERA:
7680     case R_PPC64_TPREL16_HIGHEST:
7681     case R_PPC64_TPREL16_HIGHESTA:
7682     case R_PPC64_TPREL64:
7683     case R_PPC64_DTPMOD64:
7684     case R_PPC64_DTPREL64:
7685     case R_PPC64_ADDR64:
7686     case R_PPC64_REL30:
7687     case R_PPC64_REL32:
7688     case R_PPC64_REL64:
7689     case R_PPC64_ADDR14:
7690     case R_PPC64_ADDR14_BRNTAKEN:
7691     case R_PPC64_ADDR14_BRTAKEN:
7692     case R_PPC64_ADDR16:
7693     case R_PPC64_ADDR16_DS:
7694     case R_PPC64_ADDR16_HA:
7695     case R_PPC64_ADDR16_HI:
7696     case R_PPC64_ADDR16_HIGH:
7697     case R_PPC64_ADDR16_HIGHA:
7698     case R_PPC64_ADDR16_HIGHER:
7699     case R_PPC64_ADDR16_HIGHERA:
7700     case R_PPC64_ADDR16_HIGHEST:
7701     case R_PPC64_ADDR16_HIGHESTA:
7702     case R_PPC64_ADDR16_LO:
7703     case R_PPC64_ADDR16_LO_DS:
7704     case R_PPC64_ADDR24:
7705     case R_PPC64_ADDR32:
7706     case R_PPC64_UADDR16:
7707     case R_PPC64_UADDR32:
7708     case R_PPC64_UADDR64:
7709     case R_PPC64_TOC:
7710       break;
7711     }
7712
7713   if (local_syms != NULL)
7714     {
7715       unsigned long r_symndx;
7716       bfd *ibfd = sec->owner;
7717
7718       r_symndx = ELF64_R_SYM (r_info);
7719       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7720         return FALSE;
7721     }
7722
7723   if ((bfd_link_pic (info)
7724        && (must_be_dyn_reloc (info, r_type)
7725            || (h != NULL
7726                && (!SYMBOLIC_BIND (info, h)
7727                    || h->root.type == bfd_link_hash_defweak
7728                    || !h->def_regular))))
7729       || (ELIMINATE_COPY_RELOCS
7730           && !bfd_link_pic (info)
7731           && h != NULL
7732           && (h->root.type == bfd_link_hash_defweak
7733               || !h->def_regular)))
7734     ;
7735   else
7736     return TRUE;
7737
7738   if (h != NULL)
7739     {
7740       struct elf_dyn_relocs *p;
7741       struct elf_dyn_relocs **pp;
7742       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7743
7744       /* elf_gc_sweep may have already removed all dyn relocs associated
7745          with local syms for a given section.  Also, symbol flags are
7746          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7747          report a dynreloc miscount.  */
7748       if (*pp == NULL && info->gc_sections)
7749         return TRUE;
7750
7751       while ((p = *pp) != NULL)
7752         {
7753           if (p->sec == sec)
7754             {
7755               if (!must_be_dyn_reloc (info, r_type))
7756                 p->pc_count -= 1;
7757               p->count -= 1;
7758               if (p->count == 0)
7759                 *pp = p->next;
7760               return TRUE;
7761             }
7762           pp = &p->next;
7763         }
7764     }
7765   else
7766     {
7767       struct ppc_dyn_relocs *p;
7768       struct ppc_dyn_relocs **pp;
7769       void *vpp;
7770       bfd_boolean is_ifunc;
7771
7772       if (local_syms == NULL)
7773         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7774       if (sym_sec == NULL)
7775         sym_sec = sec;
7776
7777       vpp = &elf_section_data (sym_sec)->local_dynrel;
7778       pp = (struct ppc_dyn_relocs **) vpp;
7779
7780       if (*pp == NULL && info->gc_sections)
7781         return TRUE;
7782
7783       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7784       while ((p = *pp) != NULL)
7785         {
7786           if (p->sec == sec && p->ifunc == is_ifunc)
7787             {
7788               p->count -= 1;
7789               if (p->count == 0)
7790                 *pp = p->next;
7791               return TRUE;
7792             }
7793           pp = &p->next;
7794         }
7795     }
7796
7797   /* xgettext:c-format */
7798   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7799                       sec->owner, sec);
7800   bfd_set_error (bfd_error_bad_value);
7801   return FALSE;
7802 }
7803
7804 /* Remove unused Official Procedure Descriptor entries.  Currently we
7805    only remove those associated with functions in discarded link-once
7806    sections, or weakly defined functions that have been overridden.  It
7807    would be possible to remove many more entries for statically linked
7808    applications.  */
7809
7810 bfd_boolean
7811 ppc64_elf_edit_opd (struct bfd_link_info *info)
7812 {
7813   bfd *ibfd;
7814   bfd_boolean some_edited = FALSE;
7815   asection *need_pad = NULL;
7816   struct ppc_link_hash_table *htab;
7817
7818   htab = ppc_hash_table (info);
7819   if (htab == NULL)
7820     return FALSE;
7821
7822   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7823     {
7824       asection *sec;
7825       Elf_Internal_Rela *relstart, *rel, *relend;
7826       Elf_Internal_Shdr *symtab_hdr;
7827       Elf_Internal_Sym *local_syms;
7828       struct _opd_sec_data *opd;
7829       bfd_boolean need_edit, add_aux_fields, broken;
7830       bfd_size_type cnt_16b = 0;
7831
7832       if (!is_ppc64_elf (ibfd))
7833         continue;
7834
7835       sec = bfd_get_section_by_name (ibfd, ".opd");
7836       if (sec == NULL || sec->size == 0)
7837         continue;
7838
7839       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7840         continue;
7841
7842       if (sec->output_section == bfd_abs_section_ptr)
7843         continue;
7844
7845       /* Look through the section relocs.  */
7846       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7847         continue;
7848
7849       local_syms = NULL;
7850       symtab_hdr = &elf_symtab_hdr (ibfd);
7851
7852       /* Read the relocations.  */
7853       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7854                                             info->keep_memory);
7855       if (relstart == NULL)
7856         return FALSE;
7857
7858       /* First run through the relocs to check they are sane, and to
7859          determine whether we need to edit this opd section.  */
7860       need_edit = FALSE;
7861       broken = FALSE;
7862       need_pad = sec;
7863       relend = relstart + sec->reloc_count;
7864       for (rel = relstart; rel < relend; )
7865         {
7866           enum elf_ppc64_reloc_type r_type;
7867           unsigned long r_symndx;
7868           asection *sym_sec;
7869           struct elf_link_hash_entry *h;
7870           Elf_Internal_Sym *sym;
7871           bfd_vma offset;
7872
7873           /* .opd contains an array of 16 or 24 byte entries.  We're
7874              only interested in the reloc pointing to a function entry
7875              point.  */
7876           offset = rel->r_offset;
7877           if (rel + 1 == relend
7878               || rel[1].r_offset != offset + 8)
7879             {
7880               /* If someone messes with .opd alignment then after a
7881                  "ld -r" we might have padding in the middle of .opd.
7882                  Also, there's nothing to prevent someone putting
7883                  something silly in .opd with the assembler.  No .opd
7884                  optimization for them!  */
7885             broken_opd:
7886               _bfd_error_handler
7887                 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7888               broken = TRUE;
7889               break;
7890             }
7891
7892           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7893               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7894             {
7895               _bfd_error_handler
7896                 /* xgettext:c-format */
7897                 (_("%pB: unexpected reloc type %u in .opd section"),
7898                  ibfd, r_type);
7899               broken = TRUE;
7900               break;
7901             }
7902
7903           r_symndx = ELF64_R_SYM (rel->r_info);
7904           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7905                           r_symndx, ibfd))
7906             goto error_ret;
7907
7908           if (sym_sec == NULL || sym_sec->owner == NULL)
7909             {
7910               const char *sym_name;
7911               if (h != NULL)
7912                 sym_name = h->root.root.string;
7913               else
7914                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7915                                              sym_sec);
7916
7917               _bfd_error_handler
7918                 /* xgettext:c-format */
7919                 (_("%pB: undefined sym `%s' in .opd section"),
7920                  ibfd, sym_name);
7921               broken = TRUE;
7922               break;
7923             }
7924
7925           /* opd entries are always for functions defined in the
7926              current input bfd.  If the symbol isn't defined in the
7927              input bfd, then we won't be using the function in this
7928              bfd;  It must be defined in a linkonce section in another
7929              bfd, or is weak.  It's also possible that we are
7930              discarding the function due to a linker script /DISCARD/,
7931              which we test for via the output_section.  */
7932           if (sym_sec->owner != ibfd
7933               || sym_sec->output_section == bfd_abs_section_ptr)
7934             need_edit = TRUE;
7935
7936           rel += 2;
7937           if (rel + 1 == relend
7938               || (rel + 2 < relend
7939                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7940             ++rel;
7941
7942           if (rel == relend)
7943             {
7944               if (sec->size == offset + 24)
7945                 {
7946                   need_pad = NULL;
7947                   break;
7948                 }
7949               if (sec->size == offset + 16)
7950                 {
7951                   cnt_16b++;
7952                   break;
7953                 }
7954               goto broken_opd;
7955             }
7956           else if (rel + 1 < relend
7957                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7958                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7959             {
7960               if (rel[0].r_offset == offset + 16)
7961                 cnt_16b++;
7962               else if (rel[0].r_offset != offset + 24)
7963                 goto broken_opd;
7964             }
7965           else
7966             goto broken_opd;
7967         }
7968
7969       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7970
7971       if (!broken && (need_edit || add_aux_fields))
7972         {
7973           Elf_Internal_Rela *write_rel;
7974           Elf_Internal_Shdr *rel_hdr;
7975           bfd_byte *rptr, *wptr;
7976           bfd_byte *new_contents;
7977           bfd_size_type amt;
7978
7979           new_contents = NULL;
7980           amt = OPD_NDX (sec->size) * sizeof (long);
7981           opd = &ppc64_elf_section_data (sec)->u.opd;
7982           opd->adjust = bfd_zalloc (sec->owner, amt);
7983           if (opd->adjust == NULL)
7984             return FALSE;
7985
7986           /* This seems a waste of time as input .opd sections are all
7987              zeros as generated by gcc, but I suppose there's no reason
7988              this will always be so.  We might start putting something in
7989              the third word of .opd entries.  */
7990           if ((sec->flags & SEC_IN_MEMORY) == 0)
7991             {
7992               bfd_byte *loc;
7993               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7994                 {
7995                   if (loc != NULL)
7996                     free (loc);
7997                 error_ret:
7998                   if (local_syms != NULL
7999                       && symtab_hdr->contents != (unsigned char *) local_syms)
8000                     free (local_syms);
8001                   if (elf_section_data (sec)->relocs != relstart)
8002                     free (relstart);
8003                   return FALSE;
8004                 }
8005               sec->contents = loc;
8006               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8007             }
8008
8009           elf_section_data (sec)->relocs = relstart;
8010
8011           new_contents = sec->contents;
8012           if (add_aux_fields)
8013             {
8014               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8015               if (new_contents == NULL)
8016                 return FALSE;
8017               need_pad = NULL;
8018             }
8019           wptr = new_contents;
8020           rptr = sec->contents;
8021           write_rel = relstart;
8022           for (rel = relstart; rel < relend; )
8023             {
8024               unsigned long r_symndx;
8025               asection *sym_sec;
8026               struct elf_link_hash_entry *h;
8027               struct ppc_link_hash_entry *fdh = NULL;
8028               Elf_Internal_Sym *sym;
8029               long opd_ent_size;
8030               Elf_Internal_Rela *next_rel;
8031               bfd_boolean skip;
8032
8033               r_symndx = ELF64_R_SYM (rel->r_info);
8034               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8035                               r_symndx, ibfd))
8036                 goto error_ret;
8037
8038               next_rel = rel + 2;
8039               if (next_rel + 1 == relend
8040                   || (next_rel + 2 < relend
8041                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8042                 ++next_rel;
8043
8044               /* See if the .opd entry is full 24 byte or
8045                  16 byte (with fd_aux entry overlapped with next
8046                  fd_func).  */
8047               opd_ent_size = 24;
8048               if (next_rel == relend)
8049                 {
8050                   if (sec->size == rel->r_offset + 16)
8051                     opd_ent_size = 16;
8052                 }
8053               else if (next_rel->r_offset == rel->r_offset + 16)
8054                 opd_ent_size = 16;
8055
8056               if (h != NULL
8057                   && h->root.root.string[0] == '.')
8058                 {
8059                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
8060                   if (fdh != NULL)
8061                     {
8062                       fdh = ppc_follow_link (fdh);
8063                       if (fdh->elf.root.type != bfd_link_hash_defined
8064                           && fdh->elf.root.type != bfd_link_hash_defweak)
8065                         fdh = NULL;
8066                     }
8067                 }
8068
8069               skip = (sym_sec->owner != ibfd
8070                       || sym_sec->output_section == bfd_abs_section_ptr);
8071               if (skip)
8072                 {
8073                   if (fdh != NULL && sym_sec->owner == ibfd)
8074                     {
8075                       /* Arrange for the function descriptor sym
8076                          to be dropped.  */
8077                       fdh->elf.root.u.def.value = 0;
8078                       fdh->elf.root.u.def.section = sym_sec;
8079                     }
8080                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8081
8082                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8083                     rel = next_rel;
8084                   else
8085                     while (1)
8086                       {
8087                         if (!dec_dynrel_count (rel->r_info, sec, info,
8088                                                NULL, h, sym))
8089                           goto error_ret;
8090
8091                         if (++rel == next_rel)
8092                           break;
8093
8094                         r_symndx = ELF64_R_SYM (rel->r_info);
8095                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8096                                         r_symndx, ibfd))
8097                           goto error_ret;
8098                       }
8099                 }
8100               else
8101                 {
8102                   /* We'll be keeping this opd entry.  */
8103                   long adjust;
8104
8105                   if (fdh != NULL)
8106                     {
8107                       /* Redefine the function descriptor symbol to
8108                          this location in the opd section.  It is
8109                          necessary to update the value here rather
8110                          than using an array of adjustments as we do
8111                          for local symbols, because various places
8112                          in the generic ELF code use the value
8113                          stored in u.def.value.  */
8114                       fdh->elf.root.u.def.value = wptr - new_contents;
8115                       fdh->adjust_done = 1;
8116                     }
8117
8118                   /* Local syms are a bit tricky.  We could
8119                      tweak them as they can be cached, but
8120                      we'd need to look through the local syms
8121                      for the function descriptor sym which we
8122                      don't have at the moment.  So keep an
8123                      array of adjustments.  */
8124                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8125                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8126
8127                   if (wptr != rptr)
8128                     memcpy (wptr, rptr, opd_ent_size);
8129                   wptr += opd_ent_size;
8130                   if (add_aux_fields && opd_ent_size == 16)
8131                     {
8132                       memset (wptr, '\0', 8);
8133                       wptr += 8;
8134                     }
8135
8136                   /* We need to adjust any reloc offsets to point to the
8137                      new opd entries.  */
8138                   for ( ; rel != next_rel; ++rel)
8139                     {
8140                       rel->r_offset += adjust;
8141                       if (write_rel != rel)
8142                         memcpy (write_rel, rel, sizeof (*rel));
8143                       ++write_rel;
8144                     }
8145                 }
8146
8147               rptr += opd_ent_size;
8148             }
8149
8150           sec->size = wptr - new_contents;
8151           sec->reloc_count = write_rel - relstart;
8152           if (add_aux_fields)
8153             {
8154               free (sec->contents);
8155               sec->contents = new_contents;
8156             }
8157
8158           /* Fudge the header size too, as this is used later in
8159              elf_bfd_final_link if we are emitting relocs.  */
8160           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8161           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8162           some_edited = TRUE;
8163         }
8164       else if (elf_section_data (sec)->relocs != relstart)
8165         free (relstart);
8166
8167       if (local_syms != NULL
8168           && symtab_hdr->contents != (unsigned char *) local_syms)
8169         {
8170           if (!info->keep_memory)
8171             free (local_syms);
8172           else
8173             symtab_hdr->contents = (unsigned char *) local_syms;
8174         }
8175     }
8176
8177   if (some_edited)
8178     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8179
8180   /* If we are doing a final link and the last .opd entry is just 16 byte
8181      long, add a 8 byte padding after it.  */
8182   if (need_pad != NULL && !bfd_link_relocatable (info))
8183     {
8184       bfd_byte *p;
8185
8186       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8187         {
8188           BFD_ASSERT (need_pad->size > 0);
8189
8190           p = bfd_malloc (need_pad->size + 8);
8191           if (p == NULL)
8192             return FALSE;
8193
8194           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8195                                           p, 0, need_pad->size))
8196             return FALSE;
8197
8198           need_pad->contents = p;
8199           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8200         }
8201       else
8202         {
8203           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8204           if (p == NULL)
8205             return FALSE;
8206
8207           need_pad->contents = p;
8208         }
8209
8210       memset (need_pad->contents + need_pad->size, 0, 8);
8211       need_pad->size += 8;
8212     }
8213
8214   return TRUE;
8215 }
8216
8217 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8218
8219 asection *
8220 ppc64_elf_tls_setup (struct bfd_link_info *info)
8221 {
8222   struct ppc_link_hash_table *htab;
8223
8224   htab = ppc_hash_table (info);
8225   if (htab == NULL)
8226     return NULL;
8227
8228   if (abiversion (info->output_bfd) == 1)
8229     htab->opd_abi = 1;
8230
8231   if (htab->params->no_multi_toc)
8232     htab->do_multi_toc = 0;
8233   else if (!htab->do_multi_toc)
8234     htab->params->no_multi_toc = 1;
8235
8236   /* Default to --no-plt-localentry, as this option can cause problems
8237      with symbol interposition.  For example, glibc libpthread.so and
8238      libc.so duplicate many pthread symbols, with a fallback
8239      implementation in libc.so.  In some cases the fallback does more
8240      work than the pthread implementation.  __pthread_condattr_destroy
8241      is one such symbol: the libpthread.so implementation is
8242      localentry:0 while the libc.so implementation is localentry:8.
8243      An app that "cleverly" uses dlopen to only load necessary
8244      libraries at runtime may omit loading libpthread.so when not
8245      running multi-threaded, which then results in the libc.so
8246      fallback symbols being used and ld.so complaining.  Now there
8247      are workarounds in ld (see non_zero_localentry) to detect the
8248      pthread situation, but that may not be the only case where
8249      --plt-localentry can cause trouble.  */
8250   if (htab->params->plt_localentry0 < 0)
8251     htab->params->plt_localentry0 = 0;
8252   if (htab->params->plt_localentry0
8253       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8254                                FALSE, FALSE, FALSE) == NULL)
8255     _bfd_error_handler
8256       (_("warning: --plt-localentry is especially dangerous without "
8257          "ld.so support to detect ABI violations"));
8258
8259   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8260                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8261                                               FALSE, FALSE, TRUE));
8262   /* Move dynamic linking info to the function descriptor sym.  */
8263   if (htab->tls_get_addr != NULL)
8264     func_desc_adjust (&htab->tls_get_addr->elf, info);
8265   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8266                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8267                                                  FALSE, FALSE, TRUE));
8268   if (htab->params->tls_get_addr_opt)
8269     {
8270       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8271
8272       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8273                                   FALSE, FALSE, TRUE);
8274       if (opt != NULL)
8275         func_desc_adjust (opt, info);
8276       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8277                                      FALSE, FALSE, TRUE);
8278       if (opt_fd != NULL
8279           && (opt_fd->root.type == bfd_link_hash_defined
8280               || opt_fd->root.type == bfd_link_hash_defweak))
8281         {
8282           /* If glibc supports an optimized __tls_get_addr call stub,
8283              signalled by the presence of __tls_get_addr_opt, and we'll
8284              be calling __tls_get_addr via a plt call stub, then
8285              make __tls_get_addr point to __tls_get_addr_opt.  */
8286           tga_fd = &htab->tls_get_addr_fd->elf;
8287           if (htab->elf.dynamic_sections_created
8288               && tga_fd != NULL
8289               && (tga_fd->type == STT_FUNC
8290                   || tga_fd->needs_plt)
8291               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8292                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8293             {
8294               struct plt_entry *ent;
8295
8296               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8297                 if (ent->plt.refcount > 0)
8298                   break;
8299               if (ent != NULL)
8300                 {
8301                   tga_fd->root.type = bfd_link_hash_indirect;
8302                   tga_fd->root.u.i.link = &opt_fd->root;
8303                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8304                   opt_fd->mark = 1;
8305                   if (opt_fd->dynindx != -1)
8306                     {
8307                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8308                       opt_fd->dynindx = -1;
8309                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8310                                               opt_fd->dynstr_index);
8311                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8312                         return NULL;
8313                     }
8314                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8315                   tga = &htab->tls_get_addr->elf;
8316                   if (opt != NULL && tga != NULL)
8317                     {
8318                       tga->root.type = bfd_link_hash_indirect;
8319                       tga->root.u.i.link = &opt->root;
8320                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8321                       opt->mark = 1;
8322                       _bfd_elf_link_hash_hide_symbol (info, opt,
8323                                                       tga->forced_local);
8324                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8325                     }
8326                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8327                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8328                   if (htab->tls_get_addr != NULL)
8329                     {
8330                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8331                       htab->tls_get_addr->is_func = 1;
8332                     }
8333                 }
8334             }
8335         }
8336       else if (htab->params->tls_get_addr_opt < 0)
8337         htab->params->tls_get_addr_opt = 0;
8338     }
8339   return _bfd_elf_tls_setup (info->output_bfd, info);
8340 }
8341
8342 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8343    HASH1 or HASH2.  */
8344
8345 static bfd_boolean
8346 branch_reloc_hash_match (const bfd *ibfd,
8347                          const Elf_Internal_Rela *rel,
8348                          const struct ppc_link_hash_entry *hash1,
8349                          const struct ppc_link_hash_entry *hash2)
8350 {
8351   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8352   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8353   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8354
8355   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8356     {
8357       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8358       struct elf_link_hash_entry *h;
8359
8360       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8361       h = elf_follow_link (h);
8362       if (h == &hash1->elf || h == &hash2->elf)
8363         return TRUE;
8364     }
8365   return FALSE;
8366 }
8367
8368 /* Run through all the TLS relocs looking for optimization
8369    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8370    a preliminary section layout so that we know the TLS segment
8371    offsets.  We can't optimize earlier because some optimizations need
8372    to know the tp offset, and we need to optimize before allocating
8373    dynamic relocations.  */
8374
8375 bfd_boolean
8376 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8377 {
8378   bfd *ibfd;
8379   asection *sec;
8380   struct ppc_link_hash_table *htab;
8381   unsigned char *toc_ref;
8382   int pass;
8383
8384   if (!bfd_link_executable (info))
8385     return TRUE;
8386
8387   htab = ppc_hash_table (info);
8388   if (htab == NULL)
8389     return FALSE;
8390
8391   /* Make two passes over the relocs.  On the first pass, mark toc
8392      entries involved with tls relocs, and check that tls relocs
8393      involved in setting up a tls_get_addr call are indeed followed by
8394      such a call.  If they are not, we can't do any tls optimization.
8395      On the second pass twiddle tls_mask flags to notify
8396      relocate_section that optimization can be done, and adjust got
8397      and plt refcounts.  */
8398   toc_ref = NULL;
8399   for (pass = 0; pass < 2; ++pass)
8400     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8401       {
8402         Elf_Internal_Sym *locsyms = NULL;
8403         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8404
8405         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8406           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8407             {
8408               Elf_Internal_Rela *relstart, *rel, *relend;
8409               bfd_boolean found_tls_get_addr_arg = 0;
8410
8411               /* Read the relocations.  */
8412               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8413                                                     info->keep_memory);
8414               if (relstart == NULL)
8415                 {
8416                   free (toc_ref);
8417                   return FALSE;
8418                 }
8419
8420               relend = relstart + sec->reloc_count;
8421               for (rel = relstart; rel < relend; rel++)
8422                 {
8423                   enum elf_ppc64_reloc_type r_type;
8424                   unsigned long r_symndx;
8425                   struct elf_link_hash_entry *h;
8426                   Elf_Internal_Sym *sym;
8427                   asection *sym_sec;
8428                   unsigned char *tls_mask;
8429                   unsigned char tls_set, tls_clear, tls_type = 0;
8430                   bfd_vma value;
8431                   bfd_boolean ok_tprel, is_local;
8432                   long toc_ref_index = 0;
8433                   int expecting_tls_get_addr = 0;
8434                   bfd_boolean ret = FALSE;
8435
8436                   r_symndx = ELF64_R_SYM (rel->r_info);
8437                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8438                                   r_symndx, ibfd))
8439                     {
8440                     err_free_rel:
8441                       if (elf_section_data (sec)->relocs != relstart)
8442                         free (relstart);
8443                       if (toc_ref != NULL)
8444                         free (toc_ref);
8445                       if (locsyms != NULL
8446                           && (elf_symtab_hdr (ibfd).contents
8447                               != (unsigned char *) locsyms))
8448                         free (locsyms);
8449                       return ret;
8450                     }
8451
8452                   if (h != NULL)
8453                     {
8454                       if (h->root.type == bfd_link_hash_defined
8455                           || h->root.type == bfd_link_hash_defweak)
8456                         value = h->root.u.def.value;
8457                       else if (h->root.type == bfd_link_hash_undefweak)
8458                         value = 0;
8459                       else
8460                         {
8461                           found_tls_get_addr_arg = 0;
8462                           continue;
8463                         }
8464                     }
8465                   else
8466                     /* Symbols referenced by TLS relocs must be of type
8467                        STT_TLS.  So no need for .opd local sym adjust.  */
8468                     value = sym->st_value;
8469
8470                   ok_tprel = FALSE;
8471                   is_local = FALSE;
8472                   if (h == NULL
8473                       || !h->def_dynamic)
8474                     {
8475                       is_local = TRUE;
8476                       if (h != NULL
8477                           && h->root.type == bfd_link_hash_undefweak)
8478                         ok_tprel = TRUE;
8479                       else if (sym_sec != NULL
8480                                && sym_sec->output_section != NULL)
8481                         {
8482                           value += sym_sec->output_offset;
8483                           value += sym_sec->output_section->vma;
8484                           value -= htab->elf.tls_sec->vma;
8485                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8486                                       < (bfd_vma) 1 << 32);
8487                         }
8488                     }
8489
8490                   r_type = ELF64_R_TYPE (rel->r_info);
8491                   /* If this section has old-style __tls_get_addr calls
8492                      without marker relocs, then check that each
8493                      __tls_get_addr call reloc is preceded by a reloc
8494                      that conceivably belongs to the __tls_get_addr arg
8495                      setup insn.  If we don't find matching arg setup
8496                      relocs, don't do any tls optimization.  */
8497                   if (pass == 0
8498                       && sec->has_tls_get_addr_call
8499                       && h != NULL
8500                       && (h == &htab->tls_get_addr->elf
8501                           || h == &htab->tls_get_addr_fd->elf)
8502                       && !found_tls_get_addr_arg
8503                       && is_branch_reloc (r_type))
8504                     {
8505                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8506                                                 "TLS optimization disabled\n"),
8507                                               ibfd, sec, rel->r_offset);
8508                       ret = TRUE;
8509                       goto err_free_rel;
8510                     }
8511
8512                   found_tls_get_addr_arg = 0;
8513                   switch (r_type)
8514                     {
8515                     case R_PPC64_GOT_TLSLD16:
8516                     case R_PPC64_GOT_TLSLD16_LO:
8517                       expecting_tls_get_addr = 1;
8518                       found_tls_get_addr_arg = 1;
8519                       /* Fall through.  */
8520
8521                     case R_PPC64_GOT_TLSLD16_HI:
8522                     case R_PPC64_GOT_TLSLD16_HA:
8523                       /* These relocs should never be against a symbol
8524                          defined in a shared lib.  Leave them alone if
8525                          that turns out to be the case.  */
8526                       if (!is_local)
8527                         continue;
8528
8529                       /* LD -> LE */
8530                       tls_set = 0;
8531                       tls_clear = TLS_LD;
8532                       tls_type = TLS_TLS | TLS_LD;
8533                       break;
8534
8535                     case R_PPC64_GOT_TLSGD16:
8536                     case R_PPC64_GOT_TLSGD16_LO:
8537                       expecting_tls_get_addr = 1;
8538                       found_tls_get_addr_arg = 1;
8539                       /* Fall through. */
8540
8541                     case R_PPC64_GOT_TLSGD16_HI:
8542                     case R_PPC64_GOT_TLSGD16_HA:
8543                       if (ok_tprel)
8544                         /* GD -> LE */
8545                         tls_set = 0;
8546                       else
8547                         /* GD -> IE */
8548                         tls_set = TLS_TLS | TLS_TPRELGD;
8549                       tls_clear = TLS_GD;
8550                       tls_type = TLS_TLS | TLS_GD;
8551                       break;
8552
8553                     case R_PPC64_GOT_TPREL16_DS:
8554                     case R_PPC64_GOT_TPREL16_LO_DS:
8555                     case R_PPC64_GOT_TPREL16_HI:
8556                     case R_PPC64_GOT_TPREL16_HA:
8557                       if (ok_tprel)
8558                         {
8559                           /* IE -> LE */
8560                           tls_set = 0;
8561                           tls_clear = TLS_TPREL;
8562                           tls_type = TLS_TLS | TLS_TPREL;
8563                           break;
8564                         }
8565                       continue;
8566
8567                     case R_PPC64_TLSGD:
8568                     case R_PPC64_TLSLD:
8569                       found_tls_get_addr_arg = 1;
8570                       /* Fall through.  */
8571
8572                     case R_PPC64_TLS:
8573                     case R_PPC64_TOC16:
8574                     case R_PPC64_TOC16_LO:
8575                       if (sym_sec == NULL || sym_sec != toc)
8576                         continue;
8577
8578                       /* Mark this toc entry as referenced by a TLS
8579                          code sequence.  We can do that now in the
8580                          case of R_PPC64_TLS, and after checking for
8581                          tls_get_addr for the TOC16 relocs.  */
8582                       if (toc_ref == NULL)
8583                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8584                       if (toc_ref == NULL)
8585                         goto err_free_rel;
8586
8587                       if (h != NULL)
8588                         value = h->root.u.def.value;
8589                       else
8590                         value = sym->st_value;
8591                       value += rel->r_addend;
8592                       if (value % 8 != 0)
8593                         continue;
8594                       BFD_ASSERT (value < toc->size
8595                                   && toc->output_offset % 8 == 0);
8596                       toc_ref_index = (value + toc->output_offset) / 8;
8597                       if (r_type == R_PPC64_TLS
8598                           || r_type == R_PPC64_TLSGD
8599                           || r_type == R_PPC64_TLSLD)
8600                         {
8601                           toc_ref[toc_ref_index] = 1;
8602                           continue;
8603                         }
8604
8605                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8606                         continue;
8607
8608                       tls_set = 0;
8609                       tls_clear = 0;
8610                       expecting_tls_get_addr = 2;
8611                       break;
8612
8613                     case R_PPC64_TPREL64:
8614                       if (pass == 0
8615                           || sec != toc
8616                           || toc_ref == NULL
8617                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8618                         continue;
8619                       if (ok_tprel)
8620                         {
8621                           /* IE -> LE */
8622                           tls_set = TLS_EXPLICIT;
8623                           tls_clear = TLS_TPREL;
8624                           break;
8625                         }
8626                       continue;
8627
8628                     case R_PPC64_DTPMOD64:
8629                       if (pass == 0
8630                           || sec != toc
8631                           || toc_ref == NULL
8632                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8633                         continue;
8634                       if (rel + 1 < relend
8635                           && (rel[1].r_info
8636                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8637                           && rel[1].r_offset == rel->r_offset + 8)
8638                         {
8639                           if (ok_tprel)
8640                             /* GD -> LE */
8641                             tls_set = TLS_EXPLICIT | TLS_GD;
8642                           else
8643                             /* GD -> IE */
8644                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8645                           tls_clear = TLS_GD;
8646                         }
8647                       else
8648                         {
8649                           if (!is_local)
8650                             continue;
8651
8652                           /* LD -> LE */
8653                           tls_set = TLS_EXPLICIT;
8654                           tls_clear = TLS_LD;
8655                         }
8656                       break;
8657
8658                     default:
8659                       continue;
8660                     }
8661
8662                   if (pass == 0)
8663                     {
8664                       if (!expecting_tls_get_addr
8665                           || !sec->has_tls_get_addr_call)
8666                         continue;
8667
8668                       if (rel + 1 < relend
8669                           && branch_reloc_hash_match (ibfd, rel + 1,
8670                                                       htab->tls_get_addr,
8671                                                       htab->tls_get_addr_fd))
8672                         {
8673                           if (expecting_tls_get_addr == 2)
8674                             {
8675                               /* Check for toc tls entries.  */
8676                               unsigned char *toc_tls;
8677                               int retval;
8678
8679                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8680                                                      &locsyms,
8681                                                      rel, ibfd);
8682                               if (retval == 0)
8683                                 goto err_free_rel;
8684                               if (toc_tls != NULL)
8685                                 {
8686                                   if ((*toc_tls & TLS_TLS) != 0
8687                                       && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8688                                     found_tls_get_addr_arg = 1;
8689                                   if (retval > 1)
8690                                     toc_ref[toc_ref_index] = 1;
8691                                 }
8692                             }
8693                           continue;
8694                         }
8695
8696                       /* Uh oh, we didn't find the expected call.  We
8697                          could just mark this symbol to exclude it
8698                          from tls optimization but it's safer to skip
8699                          the entire optimization.  */
8700                       /* xgettext:c-format */
8701                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8702                                                 "TLS optimization disabled\n"),
8703                                               ibfd, sec, rel->r_offset);
8704                       ret = TRUE;
8705                       goto err_free_rel;
8706                     }
8707
8708                   /* If we don't have old-style __tls_get_addr calls
8709                      without TLSGD/TLSLD marker relocs, and we haven't
8710                      found a new-style __tls_get_addr call with a
8711                      marker for this symbol, then we either have a
8712                      broken object file or an -mlongcall style
8713                      indirect call to __tls_get_addr without a marker.
8714                      Disable optimization in this case.  */
8715                   if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8716                       && (tls_set & TLS_EXPLICIT) == 0
8717                       && !sec->has_tls_get_addr_call
8718                       && ((*tls_mask & (TLS_TLS | TLS_MARK))
8719                           != (TLS_TLS | TLS_MARK)))
8720                     continue;
8721
8722                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8723                     {
8724                       struct plt_entry *ent;
8725                       for (ent = htab->tls_get_addr->elf.plt.plist;
8726                            ent != NULL;
8727                            ent = ent->next)
8728                         if (ent->addend == 0)
8729                           {
8730                             if (ent->plt.refcount > 0)
8731                               {
8732                                 ent->plt.refcount -= 1;
8733                                 expecting_tls_get_addr = 0;
8734                               }
8735                             break;
8736                           }
8737                     }
8738
8739                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8740                     {
8741                       struct plt_entry *ent;
8742                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8743                            ent != NULL;
8744                            ent = ent->next)
8745                         if (ent->addend == 0)
8746                           {
8747                             if (ent->plt.refcount > 0)
8748                               ent->plt.refcount -= 1;
8749                             break;
8750                           }
8751                     }
8752
8753                   if (tls_clear == 0)
8754                     continue;
8755
8756                   if ((tls_set & TLS_EXPLICIT) == 0)
8757                     {
8758                       struct got_entry *ent;
8759
8760                       /* Adjust got entry for this reloc.  */
8761                       if (h != NULL)
8762                         ent = h->got.glist;
8763                       else
8764                         ent = elf_local_got_ents (ibfd)[r_symndx];
8765
8766                       for (; ent != NULL; ent = ent->next)
8767                         if (ent->addend == rel->r_addend
8768                             && ent->owner == ibfd
8769                             && ent->tls_type == tls_type)
8770                           break;
8771                       if (ent == NULL)
8772                         abort ();
8773
8774                       if (tls_set == 0)
8775                         {
8776                           /* We managed to get rid of a got entry.  */
8777                           if (ent->got.refcount > 0)
8778                             ent->got.refcount -= 1;
8779                         }
8780                     }
8781                   else
8782                     {
8783                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8784                          we'll lose one or two dyn relocs.  */
8785                       if (!dec_dynrel_count (rel->r_info, sec, info,
8786                                              NULL, h, sym))
8787                         return FALSE;
8788
8789                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8790                         {
8791                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8792                                                  NULL, h, sym))
8793                             return FALSE;
8794                         }
8795                     }
8796
8797                   *tls_mask |= tls_set;
8798                   *tls_mask &= ~tls_clear;
8799                 }
8800
8801               if (elf_section_data (sec)->relocs != relstart)
8802                 free (relstart);
8803             }
8804
8805         if (locsyms != NULL
8806             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8807           {
8808             if (!info->keep_memory)
8809               free (locsyms);
8810             else
8811               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8812           }
8813       }
8814
8815   if (toc_ref != NULL)
8816     free (toc_ref);
8817   htab->do_tls_opt = 1;
8818   return TRUE;
8819 }
8820
8821 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8822    the values of any global symbols in a toc section that has been
8823    edited.  Globals in toc sections should be a rarity, so this function
8824    sets a flag if any are found in toc sections other than the one just
8825    edited, so that further hash table traversals can be avoided.  */
8826
8827 struct adjust_toc_info
8828 {
8829   asection *toc;
8830   unsigned long *skip;
8831   bfd_boolean global_toc_syms;
8832 };
8833
8834 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8835
8836 static bfd_boolean
8837 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8838 {
8839   struct ppc_link_hash_entry *eh;
8840   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8841   unsigned long i;
8842
8843   if (h->root.type != bfd_link_hash_defined
8844       && h->root.type != bfd_link_hash_defweak)
8845     return TRUE;
8846
8847   eh = (struct ppc_link_hash_entry *) h;
8848   if (eh->adjust_done)
8849     return TRUE;
8850
8851   if (eh->elf.root.u.def.section == toc_inf->toc)
8852     {
8853       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8854         i = toc_inf->toc->rawsize >> 3;
8855       else
8856         i = eh->elf.root.u.def.value >> 3;
8857
8858       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8859         {
8860           _bfd_error_handler
8861             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8862           do
8863             ++i;
8864           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8865           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8866         }
8867
8868       eh->elf.root.u.def.value -= toc_inf->skip[i];
8869       eh->adjust_done = 1;
8870     }
8871   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8872     toc_inf->global_toc_syms = TRUE;
8873
8874   return TRUE;
8875 }
8876
8877 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8878    on a _LO variety toc/got reloc.  */
8879
8880 static bfd_boolean
8881 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8882 {
8883   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8884           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8885           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8886           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8887           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8888           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8889           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8890           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8891           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8892           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8893           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8894           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8895           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8896           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8897           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8898           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8899           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8900               /* Exclude lfqu by testing reloc.  If relocs are ever
8901                  defined for the reduced D field in psq_lu then those
8902                  will need testing too.  */
8903               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8904           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8905               && (insn & 1) == 0)
8906           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8907           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8908               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8909               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8910           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8911               && (insn & 1) == 0));
8912 }
8913
8914 /* Examine all relocs referencing .toc sections in order to remove
8915    unused .toc entries.  */
8916
8917 bfd_boolean
8918 ppc64_elf_edit_toc (struct bfd_link_info *info)
8919 {
8920   bfd *ibfd;
8921   struct adjust_toc_info toc_inf;
8922   struct ppc_link_hash_table *htab = ppc_hash_table (info);
8923
8924   htab->do_toc_opt = 1;
8925   toc_inf.global_toc_syms = TRUE;
8926   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8927     {
8928       asection *toc, *sec;
8929       Elf_Internal_Shdr *symtab_hdr;
8930       Elf_Internal_Sym *local_syms;
8931       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8932       unsigned long *skip, *drop;
8933       unsigned char *used;
8934       unsigned char *keep, last, some_unused;
8935
8936       if (!is_ppc64_elf (ibfd))
8937         continue;
8938
8939       toc = bfd_get_section_by_name (ibfd, ".toc");
8940       if (toc == NULL
8941           || toc->size == 0
8942           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8943           || discarded_section (toc))
8944         continue;
8945
8946       toc_relocs = NULL;
8947       local_syms = NULL;
8948       symtab_hdr = &elf_symtab_hdr (ibfd);
8949
8950       /* Look at sections dropped from the final link.  */
8951       skip = NULL;
8952       relstart = NULL;
8953       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8954         {
8955           if (sec->reloc_count == 0
8956               || !discarded_section (sec)
8957               || get_opd_info (sec)
8958               || (sec->flags & SEC_ALLOC) == 0
8959               || (sec->flags & SEC_DEBUGGING) != 0)
8960             continue;
8961
8962           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8963           if (relstart == NULL)
8964             goto error_ret;
8965
8966           /* Run through the relocs to see which toc entries might be
8967              unused.  */
8968           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8969             {
8970               enum elf_ppc64_reloc_type r_type;
8971               unsigned long r_symndx;
8972               asection *sym_sec;
8973               struct elf_link_hash_entry *h;
8974               Elf_Internal_Sym *sym;
8975               bfd_vma val;
8976
8977               r_type = ELF64_R_TYPE (rel->r_info);
8978               switch (r_type)
8979                 {
8980                 default:
8981                   continue;
8982
8983                 case R_PPC64_TOC16:
8984                 case R_PPC64_TOC16_LO:
8985                 case R_PPC64_TOC16_HI:
8986                 case R_PPC64_TOC16_HA:
8987                 case R_PPC64_TOC16_DS:
8988                 case R_PPC64_TOC16_LO_DS:
8989                   break;
8990                 }
8991
8992               r_symndx = ELF64_R_SYM (rel->r_info);
8993               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8994                               r_symndx, ibfd))
8995                 goto error_ret;
8996
8997               if (sym_sec != toc)
8998                 continue;
8999
9000               if (h != NULL)
9001                 val = h->root.u.def.value;
9002               else
9003                 val = sym->st_value;
9004               val += rel->r_addend;
9005
9006               if (val >= toc->size)
9007                 continue;
9008
9009               /* Anything in the toc ought to be aligned to 8 bytes.
9010                  If not, don't mark as unused.  */
9011               if (val & 7)
9012                 continue;
9013
9014               if (skip == NULL)
9015                 {
9016                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9017                   if (skip == NULL)
9018                     goto error_ret;
9019                 }
9020
9021               skip[val >> 3] = ref_from_discarded;
9022             }
9023
9024           if (elf_section_data (sec)->relocs != relstart)
9025             free (relstart);
9026         }
9027
9028       /* For largetoc loads of address constants, we can convert
9029          .  addis rx,2,addr@got@ha
9030          .  ld ry,addr@got@l(rx)
9031          to
9032          .  addis rx,2,addr@toc@ha
9033          .  addi ry,rx,addr@toc@l
9034          when addr is within 2G of the toc pointer.  This then means
9035          that the word storing "addr" in the toc is no longer needed.  */
9036
9037       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9038           && toc->output_section->rawsize < (bfd_vma) 1 << 31
9039           && toc->reloc_count != 0)
9040         {
9041           /* Read toc relocs.  */
9042           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9043                                                   info->keep_memory);
9044           if (toc_relocs == NULL)
9045             goto error_ret;
9046
9047           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9048             {
9049               enum elf_ppc64_reloc_type r_type;
9050               unsigned long r_symndx;
9051               asection *sym_sec;
9052               struct elf_link_hash_entry *h;
9053               Elf_Internal_Sym *sym;
9054               bfd_vma val, addr;
9055
9056               r_type = ELF64_R_TYPE (rel->r_info);
9057               if (r_type != R_PPC64_ADDR64)
9058                 continue;
9059
9060               r_symndx = ELF64_R_SYM (rel->r_info);
9061               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9062                               r_symndx, ibfd))
9063                 goto error_ret;
9064
9065               if (sym_sec == NULL
9066                   || sym_sec->output_section == NULL
9067                   || discarded_section (sym_sec))
9068                 continue;
9069
9070               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9071                 continue;
9072
9073               if (h != NULL)
9074                 {
9075                   if (h->type == STT_GNU_IFUNC)
9076                     continue;
9077                   val = h->root.u.def.value;
9078                 }
9079               else
9080                 {
9081                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9082                     continue;
9083                   val = sym->st_value;
9084                 }
9085               val += rel->r_addend;
9086               val += sym_sec->output_section->vma + sym_sec->output_offset;
9087
9088               /* We don't yet know the exact toc pointer value, but we
9089                  know it will be somewhere in the toc section.  Don't
9090                  optimize if the difference from any possible toc
9091                  pointer is outside [ff..f80008000, 7fff7fff].  */
9092               addr = toc->output_section->vma + TOC_BASE_OFF;
9093               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9094                 continue;
9095
9096               addr = toc->output_section->vma + toc->output_section->rawsize;
9097               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9098                 continue;
9099
9100               if (skip == NULL)
9101                 {
9102                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9103                   if (skip == NULL)
9104                     goto error_ret;
9105                 }
9106
9107               skip[rel->r_offset >> 3]
9108                 |= can_optimize | ((rel - toc_relocs) << 2);
9109             }
9110         }
9111
9112       if (skip == NULL)
9113         continue;
9114
9115       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9116       if (used == NULL)
9117         {
9118         error_ret:
9119           if (local_syms != NULL
9120               && symtab_hdr->contents != (unsigned char *) local_syms)
9121             free (local_syms);
9122           if (sec != NULL
9123               && relstart != NULL
9124               && elf_section_data (sec)->relocs != relstart)
9125             free (relstart);
9126           if (toc_relocs != NULL
9127               && elf_section_data (toc)->relocs != toc_relocs)
9128             free (toc_relocs);
9129           if (skip != NULL)
9130             free (skip);
9131           return FALSE;
9132         }
9133
9134       /* Now check all kept sections that might reference the toc.
9135          Check the toc itself last.  */
9136       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9137                   : ibfd->sections);
9138            sec != NULL;
9139            sec = (sec == toc ? NULL
9140                   : sec->next == NULL ? toc
9141                   : sec->next == toc && toc->next ? toc->next
9142                   : sec->next))
9143         {
9144           int repeat;
9145
9146           if (sec->reloc_count == 0
9147               || discarded_section (sec)
9148               || get_opd_info (sec)
9149               || (sec->flags & SEC_ALLOC) == 0
9150               || (sec->flags & SEC_DEBUGGING) != 0)
9151             continue;
9152
9153           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9154                                                 info->keep_memory);
9155           if (relstart == NULL)
9156             {
9157               free (used);
9158               goto error_ret;
9159             }
9160
9161           /* Mark toc entries referenced as used.  */
9162           do
9163             {
9164               repeat = 0;
9165               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9166                 {
9167                   enum elf_ppc64_reloc_type r_type;
9168                   unsigned long r_symndx;
9169                   asection *sym_sec;
9170                   struct elf_link_hash_entry *h;
9171                   Elf_Internal_Sym *sym;
9172                   bfd_vma val;
9173                   enum {no_check, check_lo, check_ha} insn_check;
9174
9175                   r_type = ELF64_R_TYPE (rel->r_info);
9176                   switch (r_type)
9177                     {
9178                     default:
9179                       insn_check = no_check;
9180                       break;
9181
9182                     case R_PPC64_GOT_TLSLD16_HA:
9183                     case R_PPC64_GOT_TLSGD16_HA:
9184                     case R_PPC64_GOT_TPREL16_HA:
9185                     case R_PPC64_GOT_DTPREL16_HA:
9186                     case R_PPC64_GOT16_HA:
9187                     case R_PPC64_TOC16_HA:
9188                       insn_check = check_ha;
9189                       break;
9190
9191                     case R_PPC64_GOT_TLSLD16_LO:
9192                     case R_PPC64_GOT_TLSGD16_LO:
9193                     case R_PPC64_GOT_TPREL16_LO_DS:
9194                     case R_PPC64_GOT_DTPREL16_LO_DS:
9195                     case R_PPC64_GOT16_LO:
9196                     case R_PPC64_GOT16_LO_DS:
9197                     case R_PPC64_TOC16_LO:
9198                     case R_PPC64_TOC16_LO_DS:
9199                       insn_check = check_lo;
9200                       break;
9201                     }
9202
9203                   if (insn_check != no_check)
9204                     {
9205                       bfd_vma off = rel->r_offset & ~3;
9206                       unsigned char buf[4];
9207                       unsigned int insn;
9208
9209                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9210                         {
9211                           free (used);
9212                           goto error_ret;
9213                         }
9214                       insn = bfd_get_32 (ibfd, buf);
9215                       if (insn_check == check_lo
9216                           ? !ok_lo_toc_insn (insn, r_type)
9217                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9218                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9219                         {
9220                           char str[12];
9221
9222                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9223                           sprintf (str, "%#08x", insn);
9224                           info->callbacks->einfo
9225                             /* xgettext:c-format */
9226                             (_("%H: toc optimization is not supported for"
9227                                " %s instruction\n"),
9228                              ibfd, sec, rel->r_offset & ~3, str);
9229                         }
9230                     }
9231
9232                   switch (r_type)
9233                     {
9234                     case R_PPC64_TOC16:
9235                     case R_PPC64_TOC16_LO:
9236                     case R_PPC64_TOC16_HI:
9237                     case R_PPC64_TOC16_HA:
9238                     case R_PPC64_TOC16_DS:
9239                     case R_PPC64_TOC16_LO_DS:
9240                       /* In case we're taking addresses of toc entries.  */
9241                     case R_PPC64_ADDR64:
9242                       break;
9243
9244                     default:
9245                       continue;
9246                     }
9247
9248                   r_symndx = ELF64_R_SYM (rel->r_info);
9249                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9250                                   r_symndx, ibfd))
9251                     {
9252                       free (used);
9253                       goto error_ret;
9254                     }
9255
9256                   if (sym_sec != toc)
9257                     continue;
9258
9259                   if (h != NULL)
9260                     val = h->root.u.def.value;
9261                   else
9262                     val = sym->st_value;
9263                   val += rel->r_addend;
9264
9265                   if (val >= toc->size)
9266                     continue;
9267
9268                   if ((skip[val >> 3] & can_optimize) != 0)
9269                     {
9270                       bfd_vma off;
9271                       unsigned char opc;
9272
9273                       switch (r_type)
9274                         {
9275                         case R_PPC64_TOC16_HA:
9276                           break;
9277
9278                         case R_PPC64_TOC16_LO_DS:
9279                           off = rel->r_offset;
9280                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9281                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9282                                                          off, 1))
9283                             {
9284                               free (used);
9285                               goto error_ret;
9286                             }
9287                           if ((opc & (0x3f << 2)) == (58u << 2))
9288                             break;
9289                           /* Fall through.  */
9290
9291                         default:
9292                           /* Wrong sort of reloc, or not a ld.  We may
9293                              as well clear ref_from_discarded too.  */
9294                           skip[val >> 3] = 0;
9295                         }
9296                     }
9297
9298                   if (sec != toc)
9299                     used[val >> 3] = 1;
9300                   /* For the toc section, we only mark as used if this
9301                      entry itself isn't unused.  */
9302                   else if ((used[rel->r_offset >> 3]
9303                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9304                            && !used[val >> 3])
9305                     {
9306                       /* Do all the relocs again, to catch reference
9307                          chains.  */
9308                       repeat = 1;
9309                       used[val >> 3] = 1;
9310                     }
9311                 }
9312             }
9313           while (repeat);
9314
9315           if (elf_section_data (sec)->relocs != relstart)
9316             free (relstart);
9317         }
9318
9319       /* Merge the used and skip arrays.  Assume that TOC
9320          doublewords not appearing as either used or unused belong
9321          to an entry more than one doubleword in size.  */
9322       for (drop = skip, keep = used, last = 0, some_unused = 0;
9323            drop < skip + (toc->size + 7) / 8;
9324            ++drop, ++keep)
9325         {
9326           if (*keep)
9327             {
9328               *drop &= ~ref_from_discarded;
9329               if ((*drop & can_optimize) != 0)
9330                 some_unused = 1;
9331               last = 0;
9332             }
9333           else if ((*drop & ref_from_discarded) != 0)
9334             {
9335               some_unused = 1;
9336               last = ref_from_discarded;
9337             }
9338           else
9339             *drop = last;
9340         }
9341
9342       free (used);
9343
9344       if (some_unused)
9345         {
9346           bfd_byte *contents, *src;
9347           unsigned long off;
9348           Elf_Internal_Sym *sym;
9349           bfd_boolean local_toc_syms = FALSE;
9350
9351           /* Shuffle the toc contents, and at the same time convert the
9352              skip array from booleans into offsets.  */
9353           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9354             goto error_ret;
9355
9356           elf_section_data (toc)->this_hdr.contents = contents;
9357
9358           for (src = contents, off = 0, drop = skip;
9359                src < contents + toc->size;
9360                src += 8, ++drop)
9361             {
9362               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9363                 off += 8;
9364               else if (off != 0)
9365                 {
9366                   *drop = off;
9367                   memcpy (src - off, src, 8);
9368                 }
9369             }
9370           *drop = off;
9371           toc->rawsize = toc->size;
9372           toc->size = src - contents - off;
9373
9374           /* Adjust addends for relocs against the toc section sym,
9375              and optimize any accesses we can.  */
9376           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9377             {
9378               if (sec->reloc_count == 0
9379                   || discarded_section (sec))
9380                 continue;
9381
9382               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9383                                                     info->keep_memory);
9384               if (relstart == NULL)
9385                 goto error_ret;
9386
9387               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9388                 {
9389                   enum elf_ppc64_reloc_type r_type;
9390                   unsigned long r_symndx;
9391                   asection *sym_sec;
9392                   struct elf_link_hash_entry *h;
9393                   bfd_vma val;
9394
9395                   r_type = ELF64_R_TYPE (rel->r_info);
9396                   switch (r_type)
9397                     {
9398                     default:
9399                       continue;
9400
9401                     case R_PPC64_TOC16:
9402                     case R_PPC64_TOC16_LO:
9403                     case R_PPC64_TOC16_HI:
9404                     case R_PPC64_TOC16_HA:
9405                     case R_PPC64_TOC16_DS:
9406                     case R_PPC64_TOC16_LO_DS:
9407                     case R_PPC64_ADDR64:
9408                       break;
9409                     }
9410
9411                   r_symndx = ELF64_R_SYM (rel->r_info);
9412                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9413                                   r_symndx, ibfd))
9414                     goto error_ret;
9415
9416                   if (sym_sec != toc)
9417                     continue;
9418
9419                   if (h != NULL)
9420                     val = h->root.u.def.value;
9421                   else
9422                     {
9423                       val = sym->st_value;
9424                       if (val != 0)
9425                         local_toc_syms = TRUE;
9426                     }
9427
9428                   val += rel->r_addend;
9429
9430                   if (val > toc->rawsize)
9431                     val = toc->rawsize;
9432                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9433                     continue;
9434                   else if ((skip[val >> 3] & can_optimize) != 0)
9435                     {
9436                       Elf_Internal_Rela *tocrel
9437                         = toc_relocs + (skip[val >> 3] >> 2);
9438                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9439
9440                       switch (r_type)
9441                         {
9442                         case R_PPC64_TOC16_HA:
9443                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9444                           break;
9445
9446                         case R_PPC64_TOC16_LO_DS:
9447                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9448                           break;
9449
9450                         default:
9451                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9452                             ppc_howto_init ();
9453                           info->callbacks->einfo
9454                             /* xgettext:c-format */
9455                             (_("%H: %s references "
9456                                "optimized away TOC entry\n"),
9457                              ibfd, sec, rel->r_offset,
9458                              ppc64_elf_howto_table[r_type]->name);
9459                           bfd_set_error (bfd_error_bad_value);
9460                           goto error_ret;
9461                         }
9462                       rel->r_addend = tocrel->r_addend;
9463                       elf_section_data (sec)->relocs = relstart;
9464                       continue;
9465                     }
9466
9467                   if (h != NULL || sym->st_value != 0)
9468                     continue;
9469
9470                   rel->r_addend -= skip[val >> 3];
9471                   elf_section_data (sec)->relocs = relstart;
9472                 }
9473
9474               if (elf_section_data (sec)->relocs != relstart)
9475                 free (relstart);
9476             }
9477
9478           /* We shouldn't have local or global symbols defined in the TOC,
9479              but handle them anyway.  */
9480           if (local_syms != NULL)
9481             for (sym = local_syms;
9482                  sym < local_syms + symtab_hdr->sh_info;
9483                  ++sym)
9484               if (sym->st_value != 0
9485                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9486                 {
9487                   unsigned long i;
9488
9489                   if (sym->st_value > toc->rawsize)
9490                     i = toc->rawsize >> 3;
9491                   else
9492                     i = sym->st_value >> 3;
9493
9494                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9495                     {
9496                       if (local_toc_syms)
9497                         _bfd_error_handler
9498                           (_("%s defined on removed toc entry"),
9499                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9500                       do
9501                         ++i;
9502                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9503                       sym->st_value = (bfd_vma) i << 3;
9504                     }
9505
9506                   sym->st_value -= skip[i];
9507                   symtab_hdr->contents = (unsigned char *) local_syms;
9508                 }
9509
9510           /* Adjust any global syms defined in this toc input section.  */
9511           if (toc_inf.global_toc_syms)
9512             {
9513               toc_inf.toc = toc;
9514               toc_inf.skip = skip;
9515               toc_inf.global_toc_syms = FALSE;
9516               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9517                                       &toc_inf);
9518             }
9519
9520           if (toc->reloc_count != 0)
9521             {
9522               Elf_Internal_Shdr *rel_hdr;
9523               Elf_Internal_Rela *wrel;
9524               bfd_size_type sz;
9525
9526               /* Remove unused toc relocs, and adjust those we keep.  */
9527               if (toc_relocs == NULL)
9528                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9529                                                         info->keep_memory);
9530               if (toc_relocs == NULL)
9531                 goto error_ret;
9532
9533               wrel = toc_relocs;
9534               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9535                 if ((skip[rel->r_offset >> 3]
9536                      & (ref_from_discarded | can_optimize)) == 0)
9537                   {
9538                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9539                     wrel->r_info = rel->r_info;
9540                     wrel->r_addend = rel->r_addend;
9541                     ++wrel;
9542                   }
9543                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9544                                             &local_syms, NULL, NULL))
9545                   goto error_ret;
9546
9547               elf_section_data (toc)->relocs = toc_relocs;
9548               toc->reloc_count = wrel - toc_relocs;
9549               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9550               sz = rel_hdr->sh_entsize;
9551               rel_hdr->sh_size = toc->reloc_count * sz;
9552             }
9553         }
9554       else if (toc_relocs != NULL
9555                && elf_section_data (toc)->relocs != toc_relocs)
9556         free (toc_relocs);
9557
9558       if (local_syms != NULL
9559           && symtab_hdr->contents != (unsigned char *) local_syms)
9560         {
9561           if (!info->keep_memory)
9562             free (local_syms);
9563           else
9564             symtab_hdr->contents = (unsigned char *) local_syms;
9565         }
9566       free (skip);
9567     }
9568
9569   return TRUE;
9570 }
9571
9572 /* Return true iff input section I references the TOC using
9573    instructions limited to +/-32k offsets.  */
9574
9575 bfd_boolean
9576 ppc64_elf_has_small_toc_reloc (asection *i)
9577 {
9578   return (is_ppc64_elf (i->owner)
9579           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9580 }
9581
9582 /* Allocate space for one GOT entry.  */
9583
9584 static void
9585 allocate_got (struct elf_link_hash_entry *h,
9586               struct bfd_link_info *info,
9587               struct got_entry *gent)
9588 {
9589   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9590   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9591   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9592                  ? 16 : 8);
9593   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9594                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9595   asection *got = ppc64_elf_tdata (gent->owner)->got;
9596
9597   gent->got.offset = got->size;
9598   got->size += entsize;
9599
9600   if (h->type == STT_GNU_IFUNC)
9601     {
9602       htab->elf.irelplt->size += rentsize;
9603       htab->got_reli_size += rentsize;
9604     }
9605   else if (((bfd_link_pic (info)
9606              && !((gent->tls_type & TLS_TPREL) != 0
9607                   && bfd_link_executable (info)
9608                   && SYMBOL_REFERENCES_LOCAL (info, h)))
9609             || (htab->elf.dynamic_sections_created
9610                 && h->dynindx != -1
9611                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9612            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9613     {
9614       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9615       relgot->size += rentsize;
9616     }
9617 }
9618
9619 /* This function merges got entries in the same toc group.  */
9620
9621 static void
9622 merge_got_entries (struct got_entry **pent)
9623 {
9624   struct got_entry *ent, *ent2;
9625
9626   for (ent = *pent; ent != NULL; ent = ent->next)
9627     if (!ent->is_indirect)
9628       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9629         if (!ent2->is_indirect
9630             && ent2->addend == ent->addend
9631             && ent2->tls_type == ent->tls_type
9632             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9633           {
9634             ent2->is_indirect = TRUE;
9635             ent2->got.ent = ent;
9636           }
9637 }
9638
9639 /* If H is undefined, make it dynamic if that makes sense.  */
9640
9641 static bfd_boolean
9642 ensure_undef_dynamic (struct bfd_link_info *info,
9643                       struct elf_link_hash_entry *h)
9644 {
9645   struct elf_link_hash_table *htab = elf_hash_table (info);
9646
9647   if (htab->dynamic_sections_created
9648       && ((info->dynamic_undefined_weak != 0
9649            && h->root.type == bfd_link_hash_undefweak)
9650           || h->root.type == bfd_link_hash_undefined)
9651       && h->dynindx == -1
9652       && !h->forced_local
9653       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9654     return bfd_elf_link_record_dynamic_symbol (info, h);
9655   return TRUE;
9656 }
9657
9658 /* Allocate space in .plt, .got and associated reloc sections for
9659    dynamic relocs.  */
9660
9661 static bfd_boolean
9662 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9663 {
9664   struct bfd_link_info *info;
9665   struct ppc_link_hash_table *htab;
9666   asection *s;
9667   struct ppc_link_hash_entry *eh;
9668   struct got_entry **pgent, *gent;
9669
9670   if (h->root.type == bfd_link_hash_indirect)
9671     return TRUE;
9672
9673   info = (struct bfd_link_info *) inf;
9674   htab = ppc_hash_table (info);
9675   if (htab == NULL)
9676     return FALSE;
9677
9678   eh = (struct ppc_link_hash_entry *) h;
9679   /* Run through the TLS GD got entries first if we're changing them
9680      to TPREL.  */
9681   if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9682     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9683       if (gent->got.refcount > 0
9684           && (gent->tls_type & TLS_GD) != 0)
9685         {
9686           /* This was a GD entry that has been converted to TPREL.  If
9687              there happens to be a TPREL entry we can use that one.  */
9688           struct got_entry *ent;
9689           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9690             if (ent->got.refcount > 0
9691                 && (ent->tls_type & TLS_TPREL) != 0
9692                 && ent->addend == gent->addend
9693                 && ent->owner == gent->owner)
9694               {
9695                 gent->got.refcount = 0;
9696                 break;
9697               }
9698
9699           /* If not, then we'll be using our own TPREL entry.  */
9700           if (gent->got.refcount != 0)
9701             gent->tls_type = TLS_TLS | TLS_TPREL;
9702         }
9703
9704   /* Remove any list entry that won't generate a word in the GOT before
9705      we call merge_got_entries.  Otherwise we risk merging to empty
9706      entries.  */
9707   pgent = &h->got.glist;
9708   while ((gent = *pgent) != NULL)
9709     if (gent->got.refcount > 0)
9710       {
9711         if ((gent->tls_type & TLS_LD) != 0
9712             && !h->def_dynamic)
9713           {
9714             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9715             *pgent = gent->next;
9716           }
9717         else
9718           pgent = &gent->next;
9719       }
9720     else
9721       *pgent = gent->next;
9722
9723   if (!htab->do_multi_toc)
9724     merge_got_entries (&h->got.glist);
9725
9726   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9727     if (!gent->is_indirect)
9728       {
9729         /* Make sure this symbol is output as a dynamic symbol.  */
9730         if (!ensure_undef_dynamic (info, h))
9731           return FALSE;
9732
9733         if (!is_ppc64_elf (gent->owner))
9734           abort ();
9735
9736         allocate_got (h, info, gent);
9737       }
9738
9739   /* If no dynamic sections we can't have dynamic relocs, except for
9740      IFUNCs which are handled even in static executables.  */
9741   if (!htab->elf.dynamic_sections_created
9742       && h->type != STT_GNU_IFUNC)
9743     eh->dyn_relocs = NULL;
9744
9745   /* Discard relocs on undefined symbols that must be local.  */
9746   else if (h->root.type == bfd_link_hash_undefined
9747            && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9748     eh->dyn_relocs = NULL;
9749
9750   /* Also discard relocs on undefined weak syms with non-default
9751      visibility, or when dynamic_undefined_weak says so.  */
9752   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9753     eh->dyn_relocs = NULL;
9754
9755   if (eh->dyn_relocs != NULL)
9756     {
9757       struct elf_dyn_relocs *p, **pp;
9758
9759       /* In the shared -Bsymbolic case, discard space allocated for
9760          dynamic pc-relative relocs against symbols which turn out to
9761          be defined in regular objects.  For the normal shared case,
9762          discard space for relocs that have become local due to symbol
9763          visibility changes.  */
9764
9765       if (bfd_link_pic (info))
9766         {
9767           /* Relocs that use pc_count are those that appear on a call
9768              insn, or certain REL relocs (see must_be_dyn_reloc) that
9769              can be generated via assembly.  We want calls to
9770              protected symbols to resolve directly to the function
9771              rather than going via the plt.  If people want function
9772              pointer comparisons to work as expected then they should
9773              avoid writing weird assembly.  */
9774           if (SYMBOL_CALLS_LOCAL (info, h))
9775             {
9776               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9777                 {
9778                   p->count -= p->pc_count;
9779                   p->pc_count = 0;
9780                   if (p->count == 0)
9781                     *pp = p->next;
9782                   else
9783                     pp = &p->next;
9784                 }
9785             }
9786
9787           if (eh->dyn_relocs != NULL)
9788             {
9789               /* Make sure this symbol is output as a dynamic symbol.  */
9790               if (!ensure_undef_dynamic (info, h))
9791                 return FALSE;
9792             }
9793         }
9794       else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9795         {
9796           /* For the non-pic case, discard space for relocs against
9797              symbols which turn out to need copy relocs or are not
9798              dynamic.  */
9799           if (h->dynamic_adjusted
9800               && !h->def_regular
9801               && !ELF_COMMON_DEF_P (h))
9802             {
9803               /* Make sure this symbol is output as a dynamic symbol.  */
9804               if (!ensure_undef_dynamic (info, h))
9805                 return FALSE;
9806
9807               if (h->dynindx == -1)
9808                 eh->dyn_relocs = NULL;
9809             }
9810           else
9811             eh->dyn_relocs = NULL;
9812         }
9813
9814       /* Finally, allocate space.  */
9815       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9816         {
9817           asection *sreloc = elf_section_data (p->sec)->sreloc;
9818           if (eh->elf.type == STT_GNU_IFUNC)
9819             sreloc = htab->elf.irelplt;
9820           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9821         }
9822     }
9823
9824   if ((htab->elf.dynamic_sections_created
9825        && h->dynindx != -1)
9826       || h->type == STT_GNU_IFUNC)
9827     {
9828       struct plt_entry *pent;
9829       bfd_boolean doneone = FALSE;
9830       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9831         if (pent->plt.refcount > 0)
9832           {
9833             if (!htab->elf.dynamic_sections_created
9834                 || h->dynindx == -1)
9835               {
9836                 s = htab->elf.iplt;
9837                 pent->plt.offset = s->size;
9838                 s->size += PLT_ENTRY_SIZE (htab);
9839                 s = htab->elf.irelplt;
9840               }
9841             else
9842               {
9843                 /* If this is the first .plt entry, make room for the special
9844                    first entry.  */
9845                 s = htab->elf.splt;
9846                 if (s->size == 0)
9847                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9848
9849                 pent->plt.offset = s->size;
9850
9851                 /* Make room for this entry.  */
9852                 s->size += PLT_ENTRY_SIZE (htab);
9853
9854                 /* Make room for the .glink code.  */
9855                 s = htab->glink;
9856                 if (s->size == 0)
9857                   s->size += GLINK_PLTRESOLVE_SIZE (htab);
9858                 if (htab->opd_abi)
9859                   {
9860                     /* We need bigger stubs past index 32767.  */
9861                     if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9862                       s->size += 4;
9863                     s->size += 2*4;
9864                   }
9865                 else
9866                   s->size += 4;
9867
9868                 /* We also need to make an entry in the .rela.plt section.  */
9869                 s = htab->elf.srelplt;
9870               }
9871             s->size += sizeof (Elf64_External_Rela);
9872             doneone = TRUE;
9873           }
9874         else
9875           pent->plt.offset = (bfd_vma) -1;
9876       if (!doneone)
9877         {
9878           h->plt.plist = NULL;
9879           h->needs_plt = 0;
9880         }
9881     }
9882   else
9883     {
9884       h->plt.plist = NULL;
9885       h->needs_plt = 0;
9886     }
9887
9888   return TRUE;
9889 }
9890
9891 #define PPC_LO(v) ((v) & 0xffff)
9892 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9893 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9894
9895 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9896    to set up space for global entry stubs.  These are put in glink,
9897    after the branch table.  */
9898
9899 static bfd_boolean
9900 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9901 {
9902   struct bfd_link_info *info;
9903   struct ppc_link_hash_table *htab;
9904   struct plt_entry *pent;
9905   asection *s, *plt;
9906
9907   if (h->root.type == bfd_link_hash_indirect)
9908     return TRUE;
9909
9910   if (!h->pointer_equality_needed)
9911     return TRUE;
9912
9913   if (h->def_regular)
9914     return TRUE;
9915
9916   info = inf;
9917   htab = ppc_hash_table (info);
9918   if (htab == NULL)
9919     return FALSE;
9920
9921   s = htab->global_entry;
9922   plt = htab->elf.splt;
9923   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9924     if (pent->plt.offset != (bfd_vma) -1
9925         && pent->addend == 0)
9926       {
9927         /* For ELFv2, if this symbol is not defined in a regular file
9928            and we are not generating a shared library or pie, then we
9929            need to define the symbol in the executable on a call stub.
9930            This is to avoid text relocations.  */
9931         bfd_vma off, stub_align, stub_off, stub_size;
9932         unsigned int align_power;
9933
9934         stub_size = 16;
9935         stub_off = s->size;
9936         if (htab->params->plt_stub_align >= 0)
9937           align_power = htab->params->plt_stub_align;
9938         else
9939           align_power = -htab->params->plt_stub_align;
9940         /* Setting section alignment is delayed until we know it is
9941            non-empty.  Otherwise the .text output section will be
9942            aligned at least to plt_stub_align even when no global
9943            entry stubs are needed.  */
9944         if (s->alignment_power < align_power)
9945           s->alignment_power = align_power;
9946         stub_align = (bfd_vma) 1 << align_power;
9947         if (htab->params->plt_stub_align >= 0
9948             || ((((stub_off + stub_size - 1) & -stub_align)
9949                  - (stub_off & -stub_align))
9950                 > ((stub_size - 1) & -stub_align)))
9951           stub_off = (stub_off + stub_align - 1) & -stub_align;
9952         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9953         off -= stub_off + s->output_offset + s->output_section->vma;
9954         /* Note that for --plt-stub-align negative we have a possible
9955            dependency between stub offset and size.  Break that
9956            dependency by assuming the max stub size when calculating
9957            the stub offset.  */
9958         if (PPC_HA (off) == 0)
9959           stub_size -= 4;
9960         h->root.type = bfd_link_hash_defined;
9961         h->root.u.def.section = s;
9962         h->root.u.def.value = stub_off;
9963         s->size = stub_off + stub_size;
9964         break;
9965       }
9966   return TRUE;
9967 }
9968
9969 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9970    read-only sections.  */
9971
9972 static bfd_boolean
9973 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9974 {
9975   asection *sec;
9976
9977   if (h->root.type == bfd_link_hash_indirect)
9978     return TRUE;
9979
9980   sec = readonly_dynrelocs (h);
9981   if (sec != NULL)
9982     {
9983       struct bfd_link_info *info = (struct bfd_link_info *) inf;
9984
9985       info->flags |= DF_TEXTREL;
9986       info->callbacks->minfo
9987         (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
9988          sec->owner, h->root.root.string, sec);
9989
9990       /* Not an error, just cut short the traversal.  */
9991       return FALSE;
9992     }
9993   return TRUE;
9994 }
9995
9996 /* Set the sizes of the dynamic sections.  */
9997
9998 static bfd_boolean
9999 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10000                                  struct bfd_link_info *info)
10001 {
10002   struct ppc_link_hash_table *htab;
10003   bfd *dynobj;
10004   asection *s;
10005   bfd_boolean relocs;
10006   bfd *ibfd;
10007   struct got_entry *first_tlsld;
10008
10009   htab = ppc_hash_table (info);
10010   if (htab == NULL)
10011     return FALSE;
10012
10013   dynobj = htab->elf.dynobj;
10014   if (dynobj == NULL)
10015     abort ();
10016
10017   if (htab->elf.dynamic_sections_created)
10018     {
10019       /* Set the contents of the .interp section to the interpreter.  */
10020       if (bfd_link_executable (info) && !info->nointerp)
10021         {
10022           s = bfd_get_linker_section (dynobj, ".interp");
10023           if (s == NULL)
10024             abort ();
10025           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10026           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10027         }
10028     }
10029
10030   /* Set up .got offsets for local syms, and space for local dynamic
10031      relocs.  */
10032   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10033     {
10034       struct got_entry **lgot_ents;
10035       struct got_entry **end_lgot_ents;
10036       struct plt_entry **local_plt;
10037       struct plt_entry **end_local_plt;
10038       unsigned char *lgot_masks;
10039       bfd_size_type locsymcount;
10040       Elf_Internal_Shdr *symtab_hdr;
10041
10042       if (!is_ppc64_elf (ibfd))
10043         continue;
10044
10045       for (s = ibfd->sections; s != NULL; s = s->next)
10046         {
10047           struct ppc_dyn_relocs *p;
10048
10049           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10050             {
10051               if (!bfd_is_abs_section (p->sec)
10052                   && bfd_is_abs_section (p->sec->output_section))
10053                 {
10054                   /* Input section has been discarded, either because
10055                      it is a copy of a linkonce section or due to
10056                      linker script /DISCARD/, so we'll be discarding
10057                      the relocs too.  */
10058                 }
10059               else if (p->count != 0)
10060                 {
10061                   asection *srel = elf_section_data (p->sec)->sreloc;
10062                   if (p->ifunc)
10063                     srel = htab->elf.irelplt;
10064                   srel->size += p->count * sizeof (Elf64_External_Rela);
10065                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10066                     info->flags |= DF_TEXTREL;
10067                 }
10068             }
10069         }
10070
10071       lgot_ents = elf_local_got_ents (ibfd);
10072       if (!lgot_ents)
10073         continue;
10074
10075       symtab_hdr = &elf_symtab_hdr (ibfd);
10076       locsymcount = symtab_hdr->sh_info;
10077       end_lgot_ents = lgot_ents + locsymcount;
10078       local_plt = (struct plt_entry **) end_lgot_ents;
10079       end_local_plt = local_plt + locsymcount;
10080       lgot_masks = (unsigned char *) end_local_plt;
10081       s = ppc64_elf_tdata (ibfd)->got;
10082       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10083         {
10084           struct got_entry **pent, *ent;
10085
10086           pent = lgot_ents;
10087           while ((ent = *pent) != NULL)
10088             if (ent->got.refcount > 0)
10089               {
10090                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10091                   {
10092                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10093                     *pent = ent->next;
10094                   }
10095                 else
10096                   {
10097                     unsigned int ent_size = 8;
10098                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10099
10100                     ent->got.offset = s->size;
10101                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10102                       {
10103                         ent_size *= 2;
10104                         rel_size *= 2;
10105                       }
10106                     s->size += ent_size;
10107                     if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10108                       {
10109                         htab->elf.irelplt->size += rel_size;
10110                         htab->got_reli_size += rel_size;
10111                       }
10112                     else if (bfd_link_pic (info)
10113                              && !((ent->tls_type & TLS_TPREL) != 0
10114                                   && bfd_link_executable (info)))
10115                       {
10116                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10117                         srel->size += rel_size;
10118                       }
10119                     pent = &ent->next;
10120                   }
10121               }
10122             else
10123               *pent = ent->next;
10124         }
10125
10126       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
10127       for (; local_plt < end_local_plt; ++local_plt)
10128         {
10129           struct plt_entry *ent;
10130
10131           for (ent = *local_plt; ent != NULL; ent = ent->next)
10132             if (ent->plt.refcount > 0)
10133               {
10134                 s = htab->elf.iplt;
10135                 ent->plt.offset = s->size;
10136                 s->size += PLT_ENTRY_SIZE (htab);
10137
10138                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10139               }
10140             else
10141               ent->plt.offset = (bfd_vma) -1;
10142         }
10143     }
10144
10145   /* Allocate global sym .plt and .got entries, and space for global
10146      sym dynamic relocs.  */
10147   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10148
10149   if (!htab->opd_abi && !bfd_link_pic (info))
10150     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10151
10152   first_tlsld = NULL;
10153   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10154     {
10155       struct got_entry *ent;
10156
10157       if (!is_ppc64_elf (ibfd))
10158         continue;
10159
10160       ent = ppc64_tlsld_got (ibfd);
10161       if (ent->got.refcount > 0)
10162         {
10163           if (!htab->do_multi_toc && first_tlsld != NULL)
10164             {
10165               ent->is_indirect = TRUE;
10166               ent->got.ent = first_tlsld;
10167             }
10168           else
10169             {
10170               if (first_tlsld == NULL)
10171                 first_tlsld = ent;
10172               s = ppc64_elf_tdata (ibfd)->got;
10173               ent->got.offset = s->size;
10174               ent->owner = ibfd;
10175               s->size += 16;
10176               if (bfd_link_pic (info))
10177                 {
10178                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10179                   srel->size += sizeof (Elf64_External_Rela);
10180                 }
10181             }
10182         }
10183       else
10184         ent->got.offset = (bfd_vma) -1;
10185     }
10186
10187   /* We now have determined the sizes of the various dynamic sections.
10188      Allocate memory for them.  */
10189   relocs = FALSE;
10190   for (s = dynobj->sections; s != NULL; s = s->next)
10191     {
10192       if ((s->flags & SEC_LINKER_CREATED) == 0)
10193         continue;
10194
10195       if (s == htab->brlt || s == htab->relbrlt)
10196         /* These haven't been allocated yet;  don't strip.  */
10197         continue;
10198       else if (s == htab->elf.sgot
10199                || s == htab->elf.splt
10200                || s == htab->elf.iplt
10201                || s == htab->glink
10202                || s == htab->global_entry
10203                || s == htab->elf.sdynbss
10204                || s == htab->elf.sdynrelro)
10205         {
10206           /* Strip this section if we don't need it; see the
10207              comment below.  */
10208         }
10209       else if (s == htab->glink_eh_frame)
10210         {
10211           if (!bfd_is_abs_section (s->output_section))
10212             /* Not sized yet.  */
10213             continue;
10214         }
10215       else if (CONST_STRNEQ (s->name, ".rela"))
10216         {
10217           if (s->size != 0)
10218             {
10219               if (s != htab->elf.srelplt)
10220                 relocs = TRUE;
10221
10222               /* We use the reloc_count field as a counter if we need
10223                  to copy relocs into the output file.  */
10224               s->reloc_count = 0;
10225             }
10226         }
10227       else
10228         {
10229           /* It's not one of our sections, so don't allocate space.  */
10230           continue;
10231         }
10232
10233       if (s->size == 0)
10234         {
10235           /* If we don't need this section, strip it from the
10236              output file.  This is mostly to handle .rela.bss and
10237              .rela.plt.  We must create both sections in
10238              create_dynamic_sections, because they must be created
10239              before the linker maps input sections to output
10240              sections.  The linker does that before
10241              adjust_dynamic_symbol is called, and it is that
10242              function which decides whether anything needs to go
10243              into these sections.  */
10244           s->flags |= SEC_EXCLUDE;
10245           continue;
10246         }
10247
10248       if (bfd_is_abs_section (s->output_section))
10249         _bfd_error_handler (_("warning: discarding dynamic section %s"),
10250                             s->name);
10251
10252       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10253         continue;
10254
10255       /* Allocate memory for the section contents.  We use bfd_zalloc
10256          here in case unused entries are not reclaimed before the
10257          section's contents are written out.  This should not happen,
10258          but this way if it does we get a R_PPC64_NONE reloc in .rela
10259          sections instead of garbage.
10260          We also rely on the section contents being zero when writing
10261          the GOT and .dynrelro.  */
10262       s->contents = bfd_zalloc (dynobj, s->size);
10263       if (s->contents == NULL)
10264         return FALSE;
10265     }
10266
10267   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10268     {
10269       if (!is_ppc64_elf (ibfd))
10270         continue;
10271
10272       s = ppc64_elf_tdata (ibfd)->got;
10273       if (s != NULL && s != htab->elf.sgot)
10274         {
10275           if (s->size == 0)
10276             s->flags |= SEC_EXCLUDE;
10277           else
10278             {
10279               s->contents = bfd_zalloc (ibfd, s->size);
10280               if (s->contents == NULL)
10281                 return FALSE;
10282             }
10283         }
10284       s = ppc64_elf_tdata (ibfd)->relgot;
10285       if (s != NULL)
10286         {
10287           if (s->size == 0)
10288             s->flags |= SEC_EXCLUDE;
10289           else
10290             {
10291               s->contents = bfd_zalloc (ibfd, s->size);
10292               if (s->contents == NULL)
10293                 return FALSE;
10294               relocs = TRUE;
10295               s->reloc_count = 0;
10296             }
10297         }
10298     }
10299
10300   if (htab->elf.dynamic_sections_created)
10301     {
10302       bfd_boolean tls_opt;
10303
10304       /* Add some entries to the .dynamic section.  We fill in the
10305          values later, in ppc64_elf_finish_dynamic_sections, but we
10306          must add the entries now so that we get the correct size for
10307          the .dynamic section.  The DT_DEBUG entry is filled in by the
10308          dynamic linker and used by the debugger.  */
10309 #define add_dynamic_entry(TAG, VAL) \
10310   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10311
10312       if (bfd_link_executable (info))
10313         {
10314           if (!add_dynamic_entry (DT_DEBUG, 0))
10315             return FALSE;
10316         }
10317
10318       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10319         {
10320           if (!add_dynamic_entry (DT_PLTGOT, 0)
10321               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10322               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10323               || !add_dynamic_entry (DT_JMPREL, 0)
10324               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10325             return FALSE;
10326         }
10327
10328       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10329         {
10330           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10331               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10332             return FALSE;
10333         }
10334
10335       tls_opt = (htab->params->tls_get_addr_opt
10336                  && htab->tls_get_addr_fd != NULL
10337                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10338       if (tls_opt || !htab->opd_abi)
10339         {
10340           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10341             return FALSE;
10342         }
10343
10344       if (relocs)
10345         {
10346           if (!add_dynamic_entry (DT_RELA, 0)
10347               || !add_dynamic_entry (DT_RELASZ, 0)
10348               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10349             return FALSE;
10350
10351           /* If any dynamic relocs apply to a read-only section,
10352              then we need a DT_TEXTREL entry.  */
10353           if ((info->flags & DF_TEXTREL) == 0)
10354             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10355
10356           if ((info->flags & DF_TEXTREL) != 0)
10357             {
10358               if (!add_dynamic_entry (DT_TEXTREL, 0))
10359                 return FALSE;
10360             }
10361         }
10362     }
10363 #undef add_dynamic_entry
10364
10365   return TRUE;
10366 }
10367
10368 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10369
10370 static bfd_boolean
10371 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10372 {
10373   if (h->plt.plist != NULL
10374       && !h->def_regular
10375       && !h->pointer_equality_needed)
10376     return FALSE;
10377
10378   return _bfd_elf_hash_symbol (h);
10379 }
10380
10381 /* Determine the type of stub needed, if any, for a call.  */
10382
10383 static inline enum ppc_stub_type
10384 ppc_type_of_stub (asection *input_sec,
10385                   const Elf_Internal_Rela *rel,
10386                   struct ppc_link_hash_entry **hash,
10387                   struct plt_entry **plt_ent,
10388                   bfd_vma destination,
10389                   unsigned long local_off)
10390 {
10391   struct ppc_link_hash_entry *h = *hash;
10392   bfd_vma location;
10393   bfd_vma branch_offset;
10394   bfd_vma max_branch_offset;
10395   enum elf_ppc64_reloc_type r_type;
10396
10397   if (h != NULL)
10398     {
10399       struct plt_entry *ent;
10400       struct ppc_link_hash_entry *fdh = h;
10401       if (h->oh != NULL
10402           && h->oh->is_func_descriptor)
10403         {
10404           fdh = ppc_follow_link (h->oh);
10405           *hash = fdh;
10406         }
10407
10408       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10409         if (ent->addend == rel->r_addend
10410             && ent->plt.offset != (bfd_vma) -1)
10411           {
10412             *plt_ent = ent;
10413             return ppc_stub_plt_call;
10414           }
10415
10416       /* Here, we know we don't have a plt entry.  If we don't have a
10417          either a defined function descriptor or a defined entry symbol
10418          in a regular object file, then it is pointless trying to make
10419          any other type of stub.  */
10420       if (!is_static_defined (&fdh->elf)
10421           && !is_static_defined (&h->elf))
10422         return ppc_stub_none;
10423     }
10424   else if (elf_local_got_ents (input_sec->owner) != NULL)
10425     {
10426       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10427       struct plt_entry **local_plt = (struct plt_entry **)
10428         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10429       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10430
10431       if (local_plt[r_symndx] != NULL)
10432         {
10433           struct plt_entry *ent;
10434
10435           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10436             if (ent->addend == rel->r_addend
10437                 && ent->plt.offset != (bfd_vma) -1)
10438               {
10439                 *plt_ent = ent;
10440                 return ppc_stub_plt_call;
10441               }
10442         }
10443     }
10444
10445   /* Determine where the call point is.  */
10446   location = (input_sec->output_offset
10447               + input_sec->output_section->vma
10448               + rel->r_offset);
10449
10450   branch_offset = destination - location;
10451   r_type = ELF64_R_TYPE (rel->r_info);
10452
10453   /* Determine if a long branch stub is needed.  */
10454   max_branch_offset = 1 << 25;
10455   if (r_type != R_PPC64_REL24)
10456     max_branch_offset = 1 << 15;
10457
10458   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10459     /* We need a stub.  Figure out whether a long_branch or plt_branch
10460        is needed later.  */
10461     return ppc_stub_long_branch;
10462
10463   return ppc_stub_none;
10464 }
10465
10466 /* With power7 weakly ordered memory model, it is possible for ld.so
10467    to update a plt entry in one thread and have another thread see a
10468    stale zero toc entry.  To avoid this we need some sort of acquire
10469    barrier in the call stub.  One solution is to make the load of the
10470    toc word seem to appear to depend on the load of the function entry
10471    word.  Another solution is to test for r2 being zero, and branch to
10472    the appropriate glink entry if so.
10473
10474    .    fake dep barrier        compare
10475    .    ld 12,xxx(2)            ld 12,xxx(2)
10476    .    mtctr 12                mtctr 12
10477    .    xor 11,12,12            ld 2,xxx+8(2)
10478    .    add 2,2,11              cmpldi 2,0
10479    .    ld 2,xxx+8(2)           bnectr+
10480    .    bctr                    b <glink_entry>
10481
10482    The solution involving the compare turns out to be faster, so
10483    that's what we use unless the branch won't reach.  */
10484
10485 #define ALWAYS_USE_FAKE_DEP 0
10486 #define ALWAYS_EMIT_R2SAVE 0
10487
10488 static inline unsigned int
10489 plt_stub_size (struct ppc_link_hash_table *htab,
10490                struct ppc_stub_hash_entry *stub_entry,
10491                bfd_vma off)
10492 {
10493   unsigned size = 12;
10494
10495   if (ALWAYS_EMIT_R2SAVE
10496       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10497     size += 4;
10498   if (PPC_HA (off) != 0)
10499     size += 4;
10500   if (htab->opd_abi)
10501     {
10502       size += 4;
10503       if (htab->params->plt_static_chain)
10504         size += 4;
10505       if (htab->params->plt_thread_safe
10506           && htab->elf.dynamic_sections_created
10507           && stub_entry->h != NULL
10508           && stub_entry->h->elf.dynindx != -1)
10509         size += 8;
10510       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10511         size += 4;
10512     }
10513   if (stub_entry->h != NULL
10514       && (stub_entry->h == htab->tls_get_addr_fd
10515           || stub_entry->h == htab->tls_get_addr)
10516       && htab->params->tls_get_addr_opt)
10517     {
10518       size += 7 * 4;
10519       if (ALWAYS_EMIT_R2SAVE
10520           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10521         size += 6 * 4;
10522     }
10523   return size;
10524 }
10525
10526 /* Depending on the sign of plt_stub_align:
10527    If positive, return the padding to align to a 2**plt_stub_align
10528    boundary.
10529    If negative, if this stub would cross fewer 2**plt_stub_align
10530    boundaries if we align, then return the padding needed to do so.  */
10531
10532 static inline unsigned int
10533 plt_stub_pad (struct ppc_link_hash_table *htab,
10534               struct ppc_stub_hash_entry *stub_entry,
10535               bfd_vma plt_off)
10536 {
10537   int stub_align;
10538   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10539   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10540
10541   if (htab->params->plt_stub_align >= 0)
10542     {
10543       stub_align = 1 << htab->params->plt_stub_align;
10544       if ((stub_off & (stub_align - 1)) != 0)
10545         return stub_align - (stub_off & (stub_align - 1));
10546       return 0;
10547     }
10548
10549   stub_align = 1 << -htab->params->plt_stub_align;
10550   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10551       > ((stub_size - 1) & -stub_align))
10552     return stub_align - (stub_off & (stub_align - 1));
10553   return 0;
10554 }
10555
10556 /* Build a .plt call stub.  */
10557
10558 static inline bfd_byte *
10559 build_plt_stub (struct ppc_link_hash_table *htab,
10560                 struct ppc_stub_hash_entry *stub_entry,
10561                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10562 {
10563   bfd *obfd = htab->params->stub_bfd;
10564   bfd_boolean plt_load_toc = htab->opd_abi;
10565   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10566   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10567                                  && htab->elf.dynamic_sections_created
10568                                  && stub_entry->h != NULL
10569                                  && stub_entry->h->elf.dynindx != -1);
10570   bfd_boolean use_fake_dep = plt_thread_safe;
10571   bfd_vma cmp_branch_off = 0;
10572
10573   if (!ALWAYS_USE_FAKE_DEP
10574       && plt_load_toc
10575       && plt_thread_safe
10576       && !((stub_entry->h == htab->tls_get_addr_fd
10577             || stub_entry->h == htab->tls_get_addr)
10578            && htab->params->tls_get_addr_opt))
10579     {
10580       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10581       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10582                           / PLT_ENTRY_SIZE (htab));
10583       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10584       bfd_vma to, from;
10585
10586       if (pltindex > 32768)
10587         glinkoff += (pltindex - 32768) * 4;
10588       to = (glinkoff
10589             + htab->glink->output_offset
10590             + htab->glink->output_section->vma);
10591       from = (p - stub_entry->group->stub_sec->contents
10592               + 4 * (ALWAYS_EMIT_R2SAVE
10593                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10594               + 4 * (PPC_HA (offset) != 0)
10595               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10596                      != PPC_HA (offset))
10597               + 4 * (plt_static_chain != 0)
10598               + 20
10599               + stub_entry->group->stub_sec->output_offset
10600               + stub_entry->group->stub_sec->output_section->vma);
10601       cmp_branch_off = to - from;
10602       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10603     }
10604
10605   if (PPC_HA (offset) != 0)
10606     {
10607       if (r != NULL)
10608         {
10609           if (ALWAYS_EMIT_R2SAVE
10610               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10611             r[0].r_offset += 4;
10612           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10613           r[1].r_offset = r[0].r_offset + 4;
10614           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10615           r[1].r_addend = r[0].r_addend;
10616           if (plt_load_toc)
10617             {
10618               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10619                 {
10620                   r[2].r_offset = r[1].r_offset + 4;
10621                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10622                   r[2].r_addend = r[0].r_addend;
10623                 }
10624               else
10625                 {
10626                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10627                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10628                   r[2].r_addend = r[0].r_addend + 8;
10629                   if (plt_static_chain)
10630                     {
10631                       r[3].r_offset = r[2].r_offset + 4;
10632                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10633                       r[3].r_addend = r[0].r_addend + 16;
10634                     }
10635                 }
10636             }
10637         }
10638       if (ALWAYS_EMIT_R2SAVE
10639           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10640         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10641       if (plt_load_toc)
10642         {
10643           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10644           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10645         }
10646       else
10647         {
10648           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10649           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10650         }
10651       if (plt_load_toc
10652           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10653         {
10654           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10655           offset = 0;
10656         }
10657       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10658       if (plt_load_toc)
10659         {
10660           if (use_fake_dep)
10661             {
10662               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10663               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10664             }
10665           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10666           if (plt_static_chain)
10667             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10668         }
10669     }
10670   else
10671     {
10672       if (r != NULL)
10673         {
10674           if (ALWAYS_EMIT_R2SAVE
10675               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10676             r[0].r_offset += 4;
10677           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10678           if (plt_load_toc)
10679             {
10680               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10681                 {
10682                   r[1].r_offset = r[0].r_offset + 4;
10683                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10684                   r[1].r_addend = r[0].r_addend;
10685                 }
10686               else
10687                 {
10688                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10689                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10690                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10691                   if (plt_static_chain)
10692                     {
10693                       r[2].r_offset = r[1].r_offset + 4;
10694                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10695                       r[2].r_addend = r[0].r_addend + 8;
10696                     }
10697                 }
10698             }
10699         }
10700       if (ALWAYS_EMIT_R2SAVE
10701           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10702         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10703       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10704       if (plt_load_toc
10705           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10706         {
10707           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10708           offset = 0;
10709         }
10710       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10711       if (plt_load_toc)
10712         {
10713           if (use_fake_dep)
10714             {
10715               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10716               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10717             }
10718           if (plt_static_chain)
10719             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10720           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10721         }
10722     }
10723   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10724     {
10725       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10726       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10727       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10728     }
10729   else
10730     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10731   return p;
10732 }
10733
10734 /* Build a special .plt call stub for __tls_get_addr.  */
10735
10736 #define LD_R11_0R3      0xe9630000
10737 #define LD_R12_0R3      0xe9830000
10738 #define MR_R0_R3        0x7c601b78
10739 #define CMPDI_R11_0     0x2c2b0000
10740 #define ADD_R3_R12_R13  0x7c6c6a14
10741 #define BEQLR           0x4d820020
10742 #define MR_R3_R0        0x7c030378
10743 #define STD_R11_0R1     0xf9610000
10744 #define BCTRL           0x4e800421
10745 #define LD_R11_0R1      0xe9610000
10746 #define MTLR_R11        0x7d6803a6
10747
10748 static inline bfd_byte *
10749 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10750                          struct ppc_stub_hash_entry *stub_entry,
10751                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10752 {
10753   bfd *obfd = htab->params->stub_bfd;
10754
10755   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10756   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10757   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10758   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10759   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10760   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10761   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10762   if (r != NULL)
10763     r[0].r_offset += 7 * 4;
10764   if (!ALWAYS_EMIT_R2SAVE
10765       && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10766     return build_plt_stub (htab, stub_entry, p, offset, r);
10767
10768   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10769   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10770
10771   if (r != NULL)
10772     r[0].r_offset += 2 * 4;
10773   p = build_plt_stub (htab, stub_entry, p, offset, r);
10774   bfd_put_32 (obfd, BCTRL, p - 4);
10775
10776   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10777   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10778   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10779   bfd_put_32 (obfd, BLR, p),                    p += 4;
10780
10781   return p;
10782 }
10783
10784 static Elf_Internal_Rela *
10785 get_relocs (asection *sec, int count)
10786 {
10787   Elf_Internal_Rela *relocs;
10788   struct bfd_elf_section_data *elfsec_data;
10789
10790   elfsec_data = elf_section_data (sec);
10791   relocs = elfsec_data->relocs;
10792   if (relocs == NULL)
10793     {
10794       bfd_size_type relsize;
10795       relsize = sec->reloc_count * sizeof (*relocs);
10796       relocs = bfd_alloc (sec->owner, relsize);
10797       if (relocs == NULL)
10798         return NULL;
10799       elfsec_data->relocs = relocs;
10800       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10801                                           sizeof (Elf_Internal_Shdr));
10802       if (elfsec_data->rela.hdr == NULL)
10803         return NULL;
10804       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10805                                         * sizeof (Elf64_External_Rela));
10806       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10807       sec->reloc_count = 0;
10808     }
10809   relocs += sec->reloc_count;
10810   sec->reloc_count += count;
10811   return relocs;
10812 }
10813
10814 static bfd_vma
10815 get_r2off (struct bfd_link_info *info,
10816            struct ppc_stub_hash_entry *stub_entry)
10817 {
10818   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10819   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10820
10821   if (r2off == 0)
10822     {
10823       /* Support linking -R objects.  Get the toc pointer from the
10824          opd entry.  */
10825       char buf[8];
10826       if (!htab->opd_abi)
10827         return r2off;
10828       asection *opd = stub_entry->h->elf.root.u.def.section;
10829       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10830
10831       if (strcmp (opd->name, ".opd") != 0
10832           || opd->reloc_count != 0)
10833         {
10834           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
10835                                   stub_entry->h->elf.root.root.string);
10836           bfd_set_error (bfd_error_bad_value);
10837           return (bfd_vma) -1;
10838         }
10839       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10840         return (bfd_vma) -1;
10841       r2off = bfd_get_64 (opd->owner, buf);
10842       r2off -= elf_gp (info->output_bfd);
10843     }
10844   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10845   return r2off;
10846 }
10847
10848 static bfd_boolean
10849 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10850 {
10851   struct ppc_stub_hash_entry *stub_entry;
10852   struct ppc_branch_hash_entry *br_entry;
10853   struct bfd_link_info *info;
10854   struct ppc_link_hash_table *htab;
10855   bfd_byte *loc;
10856   bfd_byte *p;
10857   bfd_vma dest, off;
10858   Elf_Internal_Rela *r;
10859   asection *plt;
10860
10861   /* Massage our args to the form they really have.  */
10862   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10863   info = in_arg;
10864
10865   htab = ppc_hash_table (info);
10866   if (htab == NULL)
10867     return FALSE;
10868
10869   /* Make a note of the offset within the stubs for this entry.  */
10870   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10871   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10872
10873   htab->stub_count[stub_entry->stub_type - 1] += 1;
10874   switch (stub_entry->stub_type)
10875     {
10876     case ppc_stub_long_branch:
10877     case ppc_stub_long_branch_r2off:
10878       /* Branches are relative.  This is where we are going to.  */
10879       dest = (stub_entry->target_value
10880               + stub_entry->target_section->output_offset
10881               + stub_entry->target_section->output_section->vma);
10882       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10883       off = dest;
10884
10885       /* And this is where we are coming from.  */
10886       off -= (stub_entry->stub_offset
10887               + stub_entry->group->stub_sec->output_offset
10888               + stub_entry->group->stub_sec->output_section->vma);
10889
10890       p = loc;
10891       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10892         {
10893           bfd_vma r2off = get_r2off (info, stub_entry);
10894
10895           if (r2off == (bfd_vma) -1)
10896             {
10897               htab->stub_error = TRUE;
10898               return FALSE;
10899             }
10900           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10901           p += 4;
10902           if (PPC_HA (r2off) != 0)
10903             {
10904               bfd_put_32 (htab->params->stub_bfd,
10905                           ADDIS_R2_R2 | PPC_HA (r2off), p);
10906               p += 4;
10907             }
10908           if (PPC_LO (r2off) != 0)
10909             {
10910               bfd_put_32 (htab->params->stub_bfd,
10911                           ADDI_R2_R2 | PPC_LO (r2off), p);
10912               p += 4;
10913             }
10914           off -= p - loc;
10915         }
10916       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10917       p += 4;
10918
10919       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10920         {
10921           _bfd_error_handler
10922             (_("long branch stub `%s' offset overflow"),
10923              stub_entry->root.string);
10924           htab->stub_error = TRUE;
10925           return FALSE;
10926         }
10927
10928       if (info->emitrelocations)
10929         {
10930           r = get_relocs (stub_entry->group->stub_sec, 1);
10931           if (r == NULL)
10932             return FALSE;
10933           r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10934           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10935           r->r_addend = dest;
10936           if (stub_entry->h != NULL)
10937             {
10938               struct elf_link_hash_entry **hashes;
10939               unsigned long symndx;
10940               struct ppc_link_hash_entry *h;
10941
10942               hashes = elf_sym_hashes (htab->params->stub_bfd);
10943               if (hashes == NULL)
10944                 {
10945                   bfd_size_type hsize;
10946
10947                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10948                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10949                   if (hashes == NULL)
10950                     return FALSE;
10951                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
10952                   htab->stub_globals = 1;
10953                 }
10954               symndx = htab->stub_globals++;
10955               h = stub_entry->h;
10956               hashes[symndx] = &h->elf;
10957               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10958               if (h->oh != NULL && h->oh->is_func)
10959                 h = ppc_follow_link (h->oh);
10960               if (h->elf.root.u.def.section != stub_entry->target_section)
10961                 /* H is an opd symbol.  The addend must be zero.  */
10962                 r->r_addend = 0;
10963               else
10964                 {
10965                   off = (h->elf.root.u.def.value
10966                          + h->elf.root.u.def.section->output_offset
10967                          + h->elf.root.u.def.section->output_section->vma);
10968                   r->r_addend -= off;
10969                 }
10970             }
10971         }
10972       break;
10973
10974     case ppc_stub_plt_branch:
10975     case ppc_stub_plt_branch_r2off:
10976       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10977                                          stub_entry->root.string + 9,
10978                                          FALSE, FALSE);
10979       if (br_entry == NULL)
10980         {
10981           _bfd_error_handler (_("can't find branch stub `%s'"),
10982                               stub_entry->root.string);
10983           htab->stub_error = TRUE;
10984           return FALSE;
10985         }
10986
10987       dest = (stub_entry->target_value
10988               + stub_entry->target_section->output_offset
10989               + stub_entry->target_section->output_section->vma);
10990       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10991         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10992
10993       bfd_put_64 (htab->brlt->owner, dest,
10994                   htab->brlt->contents + br_entry->offset);
10995
10996       if (br_entry->iter == htab->stub_iteration)
10997         {
10998           br_entry->iter = 0;
10999
11000           if (htab->relbrlt != NULL)
11001             {
11002               /* Create a reloc for the branch lookup table entry.  */
11003               Elf_Internal_Rela rela;
11004               bfd_byte *rl;
11005
11006               rela.r_offset = (br_entry->offset
11007                                + htab->brlt->output_offset
11008                                + htab->brlt->output_section->vma);
11009               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11010               rela.r_addend = dest;
11011
11012               rl = htab->relbrlt->contents;
11013               rl += (htab->relbrlt->reloc_count++
11014                      * sizeof (Elf64_External_Rela));
11015               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11016             }
11017           else if (info->emitrelocations)
11018             {
11019               r = get_relocs (htab->brlt, 1);
11020               if (r == NULL)
11021                 return FALSE;
11022               /* brlt, being SEC_LINKER_CREATED does not go through the
11023                  normal reloc processing.  Symbols and offsets are not
11024                  translated from input file to output file form, so
11025                  set up the offset per the output file.  */
11026               r->r_offset = (br_entry->offset
11027                              + htab->brlt->output_offset
11028                              + htab->brlt->output_section->vma);
11029               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11030               r->r_addend = dest;
11031             }
11032         }
11033
11034       dest = (br_entry->offset
11035               + htab->brlt->output_offset
11036               + htab->brlt->output_section->vma);
11037
11038       off = (dest
11039              - elf_gp (info->output_bfd)
11040              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11041
11042       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11043         {
11044           info->callbacks->einfo
11045             (_("%P: linkage table error against `%pT'\n"),
11046              stub_entry->root.string);
11047           bfd_set_error (bfd_error_bad_value);
11048           htab->stub_error = TRUE;
11049           return FALSE;
11050         }
11051
11052       if (info->emitrelocations)
11053         {
11054           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11055           if (r == NULL)
11056             return FALSE;
11057           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11058           if (bfd_big_endian (info->output_bfd))
11059             r[0].r_offset += 2;
11060           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11061             r[0].r_offset += 4;
11062           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11063           r[0].r_addend = dest;
11064           if (PPC_HA (off) != 0)
11065             {
11066               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11067               r[1].r_offset = r[0].r_offset + 4;
11068               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11069               r[1].r_addend = r[0].r_addend;
11070             }
11071         }
11072
11073       p = loc;
11074       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11075         {
11076           if (PPC_HA (off) != 0)
11077             {
11078               bfd_put_32 (htab->params->stub_bfd,
11079                           ADDIS_R12_R2 | PPC_HA (off), p);
11080               p += 4;
11081               bfd_put_32 (htab->params->stub_bfd,
11082                           LD_R12_0R12 | PPC_LO (off), p);
11083             }
11084           else
11085             bfd_put_32 (htab->params->stub_bfd,
11086                         LD_R12_0R2 | PPC_LO (off), p);
11087         }
11088       else
11089         {
11090           bfd_vma r2off = get_r2off (info, stub_entry);
11091
11092           if (r2off == (bfd_vma) -1)
11093             {
11094               htab->stub_error = TRUE;
11095               return FALSE;
11096             }
11097
11098           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11099           p += 4;
11100           if (PPC_HA (off) != 0)
11101             {
11102               bfd_put_32 (htab->params->stub_bfd,
11103                           ADDIS_R12_R2 | PPC_HA (off), p);
11104               p += 4;
11105               bfd_put_32 (htab->params->stub_bfd,
11106                           LD_R12_0R12 | PPC_LO (off), p);
11107             }
11108           else
11109             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11110
11111           if (PPC_HA (r2off) != 0)
11112             {
11113               p += 4;
11114               bfd_put_32 (htab->params->stub_bfd,
11115                           ADDIS_R2_R2 | PPC_HA (r2off), p);
11116             }
11117           if (PPC_LO (r2off) != 0)
11118             {
11119               p += 4;
11120               bfd_put_32 (htab->params->stub_bfd,
11121                           ADDI_R2_R2 | PPC_LO (r2off), p);
11122             }
11123         }
11124       p += 4;
11125       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11126       p += 4;
11127       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11128       p += 4;
11129       break;
11130
11131     case ppc_stub_plt_call:
11132     case ppc_stub_plt_call_r2save:
11133       if (stub_entry->h != NULL
11134           && stub_entry->h->is_func_descriptor
11135           && stub_entry->h->oh != NULL)
11136         {
11137           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11138
11139           /* If the old-ABI "dot-symbol" is undefined make it weak so
11140              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11141           if (fh->elf.root.type == bfd_link_hash_undefined
11142               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11143                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11144             fh->elf.root.type = bfd_link_hash_undefweak;
11145         }
11146
11147       /* Now build the stub.  */
11148       dest = stub_entry->plt_ent->plt.offset & ~1;
11149       if (dest >= (bfd_vma) -2)
11150         abort ();
11151
11152       plt = htab->elf.splt;
11153       if (!htab->elf.dynamic_sections_created
11154           || stub_entry->h == NULL
11155           || stub_entry->h->elf.dynindx == -1)
11156         plt = htab->elf.iplt;
11157
11158       dest += plt->output_offset + plt->output_section->vma;
11159
11160       if (stub_entry->h == NULL
11161           && (stub_entry->plt_ent->plt.offset & 1) == 0)
11162         {
11163           Elf_Internal_Rela rela;
11164           bfd_byte *rl;
11165
11166           rela.r_offset = dest;
11167           if (htab->opd_abi)
11168             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11169           else
11170             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11171           rela.r_addend = (stub_entry->target_value
11172                            + stub_entry->target_section->output_offset
11173                            + stub_entry->target_section->output_section->vma);
11174
11175           rl = (htab->elf.irelplt->contents
11176                 + (htab->elf.irelplt->reloc_count++
11177                    * sizeof (Elf64_External_Rela)));
11178           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11179           stub_entry->plt_ent->plt.offset |= 1;
11180           htab->local_ifunc_resolver = 1;
11181         }
11182
11183       off = (dest
11184              - elf_gp (info->output_bfd)
11185              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11186
11187       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11188         {
11189           info->callbacks->einfo
11190             /* xgettext:c-format */
11191             (_("%P: linkage table error against `%pT'\n"),
11192              stub_entry->h != NULL
11193              ? stub_entry->h->elf.root.root.string
11194              : "<local sym>");
11195           bfd_set_error (bfd_error_bad_value);
11196           htab->stub_error = TRUE;
11197           return FALSE;
11198         }
11199
11200       if (htab->params->plt_stub_align != 0)
11201         {
11202           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11203
11204           stub_entry->group->stub_sec->size += pad;
11205           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11206           loc += pad;
11207         }
11208
11209       r = NULL;
11210       if (info->emitrelocations)
11211         {
11212           r = get_relocs (stub_entry->group->stub_sec,
11213                           ((PPC_HA (off) != 0)
11214                            + (htab->opd_abi
11215                               ? 2 + (htab->params->plt_static_chain
11216                                      && PPC_HA (off + 16) == PPC_HA (off))
11217                               : 1)));
11218           if (r == NULL)
11219             return FALSE;
11220           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11221           if (bfd_big_endian (info->output_bfd))
11222             r[0].r_offset += 2;
11223           r[0].r_addend = dest;
11224         }
11225       if (stub_entry->h != NULL
11226           && (stub_entry->h == htab->tls_get_addr_fd
11227               || stub_entry->h == htab->tls_get_addr)
11228           && htab->params->tls_get_addr_opt)
11229         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11230       else
11231         p = build_plt_stub (htab, stub_entry, loc, off, r);
11232       break;
11233
11234     case ppc_stub_save_res:
11235       return TRUE;
11236
11237     default:
11238       BFD_FAIL ();
11239       return FALSE;
11240     }
11241
11242   stub_entry->group->stub_sec->size += p - loc;
11243
11244   if (htab->params->emit_stub_syms)
11245     {
11246       struct elf_link_hash_entry *h;
11247       size_t len1, len2;
11248       char *name;
11249       const char *const stub_str[] = { "long_branch",
11250                                        "long_branch_r2off",
11251                                        "plt_branch",
11252                                        "plt_branch_r2off",
11253                                        "plt_call",
11254                                        "plt_call" };
11255
11256       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11257       len2 = strlen (stub_entry->root.string);
11258       name = bfd_malloc (len1 + len2 + 2);
11259       if (name == NULL)
11260         return FALSE;
11261       memcpy (name, stub_entry->root.string, 9);
11262       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11263       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11264       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11265       if (h == NULL)
11266         return FALSE;
11267       if (h->root.type == bfd_link_hash_new)
11268         {
11269           h->root.type = bfd_link_hash_defined;
11270           h->root.u.def.section = stub_entry->group->stub_sec;
11271           h->root.u.def.value = stub_entry->stub_offset;
11272           h->ref_regular = 1;
11273           h->def_regular = 1;
11274           h->ref_regular_nonweak = 1;
11275           h->forced_local = 1;
11276           h->non_elf = 0;
11277           h->root.linker_def = 1;
11278         }
11279     }
11280
11281   return TRUE;
11282 }
11283
11284 /* As above, but don't actually build the stub.  Just bump offset so
11285    we know stub section sizes, and select plt_branch stubs where
11286    long_branch stubs won't do.  */
11287
11288 static bfd_boolean
11289 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11290 {
11291   struct ppc_stub_hash_entry *stub_entry;
11292   struct bfd_link_info *info;
11293   struct ppc_link_hash_table *htab;
11294   bfd_vma off;
11295   int size;
11296
11297   /* Massage our args to the form they really have.  */
11298   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11299   info = in_arg;
11300
11301   htab = ppc_hash_table (info);
11302   if (htab == NULL)
11303     return FALSE;
11304
11305   if (stub_entry->h != NULL
11306       && stub_entry->h->save_res
11307       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11308       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11309     {
11310       /* Don't make stubs to out-of-line register save/restore
11311          functions.  Instead, emit copies of the functions.  */
11312       stub_entry->group->needs_save_res = 1;
11313       stub_entry->stub_type = ppc_stub_save_res;
11314       return TRUE;
11315     }
11316
11317   if (stub_entry->stub_type == ppc_stub_plt_call
11318       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11319     {
11320       asection *plt;
11321       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11322       if (off >= (bfd_vma) -2)
11323         abort ();
11324       plt = htab->elf.splt;
11325       if (!htab->elf.dynamic_sections_created
11326           || stub_entry->h == NULL
11327           || stub_entry->h->elf.dynindx == -1)
11328         plt = htab->elf.iplt;
11329       off += (plt->output_offset
11330               + plt->output_section->vma
11331               - elf_gp (info->output_bfd)
11332               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11333
11334       size = plt_stub_size (htab, stub_entry, off);
11335       if (stub_entry->h != NULL
11336           && (stub_entry->h == htab->tls_get_addr_fd
11337               || stub_entry->h == htab->tls_get_addr)
11338           && htab->params->tls_get_addr_opt
11339           && (ALWAYS_EMIT_R2SAVE
11340               || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11341         stub_entry->group->tls_get_addr_opt_bctrl
11342           = stub_entry->group->stub_sec->size + size - 5 * 4;
11343
11344       if (htab->params->plt_stub_align)
11345         size += plt_stub_pad (htab, stub_entry, off);
11346       if (info->emitrelocations)
11347         {
11348           stub_entry->group->stub_sec->reloc_count
11349             += ((PPC_HA (off) != 0)
11350                 + (htab->opd_abi
11351                    ? 2 + (htab->params->plt_static_chain
11352                           && PPC_HA (off + 16) == PPC_HA (off))
11353                    : 1));
11354           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11355         }
11356     }
11357   else
11358     {
11359       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11360          variants.  */
11361       bfd_vma r2off = 0;
11362       bfd_vma local_off = 0;
11363
11364       off = (stub_entry->target_value
11365              + stub_entry->target_section->output_offset
11366              + stub_entry->target_section->output_section->vma);
11367       off -= (stub_entry->group->stub_sec->size
11368               + stub_entry->group->stub_sec->output_offset
11369               + stub_entry->group->stub_sec->output_section->vma);
11370
11371       /* Reset the stub type from the plt variant in case we now
11372          can reach with a shorter stub.  */
11373       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11374         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11375
11376       size = 4;
11377       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11378         {
11379           r2off = get_r2off (info, stub_entry);
11380           if (r2off == (bfd_vma) -1)
11381             {
11382               htab->stub_error = TRUE;
11383               return FALSE;
11384             }
11385           size = 8;
11386           if (PPC_HA (r2off) != 0)
11387             size += 4;
11388           if (PPC_LO (r2off) != 0)
11389             size += 4;
11390           off -= size - 4;
11391         }
11392
11393       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11394
11395       /* If the branch offset is too big, use a ppc_stub_plt_branch.
11396          Do the same for -R objects without function descriptors.  */
11397       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11398           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11399               && r2off == 0
11400               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11401         {
11402           struct ppc_branch_hash_entry *br_entry;
11403
11404           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11405                                              stub_entry->root.string + 9,
11406                                              TRUE, FALSE);
11407           if (br_entry == NULL)
11408             {
11409               _bfd_error_handler (_("can't build branch stub `%s'"),
11410                                   stub_entry->root.string);
11411               htab->stub_error = TRUE;
11412               return FALSE;
11413             }
11414
11415           if (br_entry->iter != htab->stub_iteration)
11416             {
11417               br_entry->iter = htab->stub_iteration;
11418               br_entry->offset = htab->brlt->size;
11419               htab->brlt->size += 8;
11420
11421               if (htab->relbrlt != NULL)
11422                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11423               else if (info->emitrelocations)
11424                 {
11425                   htab->brlt->reloc_count += 1;
11426                   htab->brlt->flags |= SEC_RELOC;
11427                 }
11428             }
11429
11430           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11431           off = (br_entry->offset
11432                  + htab->brlt->output_offset
11433                  + htab->brlt->output_section->vma
11434                  - elf_gp (info->output_bfd)
11435                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11436
11437           if (info->emitrelocations)
11438             {
11439               stub_entry->group->stub_sec->reloc_count
11440                 += 1 + (PPC_HA (off) != 0);
11441               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11442             }
11443
11444           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11445             {
11446               size = 12;
11447               if (PPC_HA (off) != 0)
11448                 size = 16;
11449             }
11450           else
11451             {
11452               size = 16;
11453               if (PPC_HA (off) != 0)
11454                 size += 4;
11455
11456               if (PPC_HA (r2off) != 0)
11457                 size += 4;
11458               if (PPC_LO (r2off) != 0)
11459                 size += 4;
11460             }
11461         }
11462       else if (info->emitrelocations)
11463         {
11464           stub_entry->group->stub_sec->reloc_count += 1;
11465           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11466         }
11467     }
11468
11469   stub_entry->group->stub_sec->size += size;
11470   return TRUE;
11471 }
11472
11473 /* Set up various things so that we can make a list of input sections
11474    for each output section included in the link.  Returns -1 on error,
11475    0 when no stubs will be needed, and 1 on success.  */
11476
11477 int
11478 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11479 {
11480   unsigned int id;
11481   bfd_size_type amt;
11482   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11483
11484   if (htab == NULL)
11485     return -1;
11486
11487   htab->sec_info_arr_size = bfd_get_next_section_id ();
11488   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11489   htab->sec_info = bfd_zmalloc (amt);
11490   if (htab->sec_info == NULL)
11491     return -1;
11492
11493   /* Set toc_off for com, und, abs and ind sections.  */
11494   for (id = 0; id < 3; id++)
11495     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11496
11497   return 1;
11498 }
11499
11500 /* Set up for first pass at multitoc partitioning.  */
11501
11502 void
11503 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11504 {
11505   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11506
11507   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11508   htab->toc_bfd = NULL;
11509   htab->toc_first_sec = NULL;
11510 }
11511
11512 /* The linker repeatedly calls this function for each TOC input section
11513    and linker generated GOT section.  Group input bfds such that the toc
11514    within a group is less than 64k in size.  */
11515
11516 bfd_boolean
11517 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11518 {
11519   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11520   bfd_vma addr, off, limit;
11521
11522   if (htab == NULL)
11523     return FALSE;
11524
11525   if (!htab->second_toc_pass)
11526     {
11527       /* Keep track of the first .toc or .got section for this input bfd.  */
11528       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11529
11530       if (new_bfd)
11531         {
11532           htab->toc_bfd = isec->owner;
11533           htab->toc_first_sec = isec;
11534         }
11535
11536       addr = isec->output_offset + isec->output_section->vma;
11537       off = addr - htab->toc_curr;
11538       limit = 0x80008000;
11539       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11540         limit = 0x10000;
11541       if (off + isec->size > limit)
11542         {
11543           addr = (htab->toc_first_sec->output_offset
11544                   + htab->toc_first_sec->output_section->vma);
11545           htab->toc_curr = addr;
11546           htab->toc_curr &= -TOC_BASE_ALIGN;
11547         }
11548
11549       /* toc_curr is the base address of this toc group.  Set elf_gp
11550          for the input section to be the offset relative to the
11551          output toc base plus 0x8000.  Making the input elf_gp an
11552          offset allows us to move the toc as a whole without
11553          recalculating input elf_gp.  */
11554       off = htab->toc_curr - elf_gp (info->output_bfd);
11555       off += TOC_BASE_OFF;
11556
11557       /* Die if someone uses a linker script that doesn't keep input
11558          file .toc and .got together.  */
11559       if (new_bfd
11560           && elf_gp (isec->owner) != 0
11561           && elf_gp (isec->owner) != off)
11562         return FALSE;
11563
11564       elf_gp (isec->owner) = off;
11565       return TRUE;
11566     }
11567
11568   /* During the second pass toc_first_sec points to the start of
11569      a toc group, and toc_curr is used to track the old elf_gp.
11570      We use toc_bfd to ensure we only look at each bfd once.  */
11571   if (htab->toc_bfd == isec->owner)
11572     return TRUE;
11573   htab->toc_bfd = isec->owner;
11574
11575   if (htab->toc_first_sec == NULL
11576       || htab->toc_curr != elf_gp (isec->owner))
11577     {
11578       htab->toc_curr = elf_gp (isec->owner);
11579       htab->toc_first_sec = isec;
11580     }
11581   addr = (htab->toc_first_sec->output_offset
11582           + htab->toc_first_sec->output_section->vma);
11583   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11584   elf_gp (isec->owner) = off;
11585
11586   return TRUE;
11587 }
11588
11589 /* Called via elf_link_hash_traverse to merge GOT entries for global
11590    symbol H.  */
11591
11592 static bfd_boolean
11593 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11594 {
11595   if (h->root.type == bfd_link_hash_indirect)
11596     return TRUE;
11597
11598   merge_got_entries (&h->got.glist);
11599
11600   return TRUE;
11601 }
11602
11603 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11604    symbol H.  */
11605
11606 static bfd_boolean
11607 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11608 {
11609   struct got_entry *gent;
11610
11611   if (h->root.type == bfd_link_hash_indirect)
11612     return TRUE;
11613
11614   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11615     if (!gent->is_indirect)
11616       allocate_got (h, (struct bfd_link_info *) inf, gent);
11617   return TRUE;
11618 }
11619
11620 /* Called on the first multitoc pass after the last call to
11621    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11622    entries.  */
11623
11624 bfd_boolean
11625 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11626 {
11627   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11628   struct bfd *ibfd, *ibfd2;
11629   bfd_boolean done_something;
11630
11631   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11632
11633   if (!htab->do_multi_toc)
11634     return FALSE;
11635
11636   /* Merge global sym got entries within a toc group.  */
11637   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11638
11639   /* And tlsld_got.  */
11640   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11641     {
11642       struct got_entry *ent, *ent2;
11643
11644       if (!is_ppc64_elf (ibfd))
11645         continue;
11646
11647       ent = ppc64_tlsld_got (ibfd);
11648       if (!ent->is_indirect
11649           && ent->got.offset != (bfd_vma) -1)
11650         {
11651           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11652             {
11653               if (!is_ppc64_elf (ibfd2))
11654                 continue;
11655
11656               ent2 = ppc64_tlsld_got (ibfd2);
11657               if (!ent2->is_indirect
11658                   && ent2->got.offset != (bfd_vma) -1
11659                   && elf_gp (ibfd2) == elf_gp (ibfd))
11660                 {
11661                   ent2->is_indirect = TRUE;
11662                   ent2->got.ent = ent;
11663                 }
11664             }
11665         }
11666     }
11667
11668   /* Zap sizes of got sections.  */
11669   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11670   htab->elf.irelplt->size -= htab->got_reli_size;
11671   htab->got_reli_size = 0;
11672
11673   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11674     {
11675       asection *got, *relgot;
11676
11677       if (!is_ppc64_elf (ibfd))
11678         continue;
11679
11680       got = ppc64_elf_tdata (ibfd)->got;
11681       if (got != NULL)
11682         {
11683           got->rawsize = got->size;
11684           got->size = 0;
11685           relgot = ppc64_elf_tdata (ibfd)->relgot;
11686           relgot->rawsize = relgot->size;
11687           relgot->size = 0;
11688         }
11689     }
11690
11691   /* Now reallocate the got, local syms first.  We don't need to
11692      allocate section contents again since we never increase size.  */
11693   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11694     {
11695       struct got_entry **lgot_ents;
11696       struct got_entry **end_lgot_ents;
11697       struct plt_entry **local_plt;
11698       struct plt_entry **end_local_plt;
11699       unsigned char *lgot_masks;
11700       bfd_size_type locsymcount;
11701       Elf_Internal_Shdr *symtab_hdr;
11702       asection *s;
11703
11704       if (!is_ppc64_elf (ibfd))
11705         continue;
11706
11707       lgot_ents = elf_local_got_ents (ibfd);
11708       if (!lgot_ents)
11709         continue;
11710
11711       symtab_hdr = &elf_symtab_hdr (ibfd);
11712       locsymcount = symtab_hdr->sh_info;
11713       end_lgot_ents = lgot_ents + locsymcount;
11714       local_plt = (struct plt_entry **) end_lgot_ents;
11715       end_local_plt = local_plt + locsymcount;
11716       lgot_masks = (unsigned char *) end_local_plt;
11717       s = ppc64_elf_tdata (ibfd)->got;
11718       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11719         {
11720           struct got_entry *ent;
11721
11722           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11723             {
11724               unsigned int ent_size = 8;
11725               unsigned int rel_size = sizeof (Elf64_External_Rela);
11726
11727               ent->got.offset = s->size;
11728               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11729                 {
11730                   ent_size *= 2;
11731                   rel_size *= 2;
11732                 }
11733               s->size += ent_size;
11734               if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
11735                 {
11736                   htab->elf.irelplt->size += rel_size;
11737                   htab->got_reli_size += rel_size;
11738                 }
11739               else if (bfd_link_pic (info)
11740                        && !((ent->tls_type & TLS_TPREL) != 0
11741                             && bfd_link_executable (info)))
11742                 {
11743                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11744                   srel->size += rel_size;
11745                 }
11746             }
11747         }
11748     }
11749
11750   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11751
11752   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11753     {
11754       struct got_entry *ent;
11755
11756       if (!is_ppc64_elf (ibfd))
11757         continue;
11758
11759       ent = ppc64_tlsld_got (ibfd);
11760       if (!ent->is_indirect
11761           && ent->got.offset != (bfd_vma) -1)
11762         {
11763           asection *s = ppc64_elf_tdata (ibfd)->got;
11764           ent->got.offset = s->size;
11765           s->size += 16;
11766           if (bfd_link_pic (info))
11767             {
11768               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11769               srel->size += sizeof (Elf64_External_Rela);
11770             }
11771         }
11772     }
11773
11774   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11775   if (!done_something)
11776     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11777       {
11778         asection *got;
11779
11780         if (!is_ppc64_elf (ibfd))
11781           continue;
11782
11783         got = ppc64_elf_tdata (ibfd)->got;
11784         if (got != NULL)
11785           {
11786             done_something = got->rawsize != got->size;
11787             if (done_something)
11788               break;
11789           }
11790       }
11791
11792   if (done_something)
11793     (*htab->params->layout_sections_again) ();
11794
11795   /* Set up for second pass over toc sections to recalculate elf_gp
11796      on input sections.  */
11797   htab->toc_bfd = NULL;
11798   htab->toc_first_sec = NULL;
11799   htab->second_toc_pass = TRUE;
11800   return done_something;
11801 }
11802
11803 /* Called after second pass of multitoc partitioning.  */
11804
11805 void
11806 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11807 {
11808   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11809
11810   /* After the second pass, toc_curr tracks the TOC offset used
11811      for code sections below in ppc64_elf_next_input_section.  */
11812   htab->toc_curr = TOC_BASE_OFF;
11813 }
11814
11815 /* No toc references were found in ISEC.  If the code in ISEC makes no
11816    calls, then there's no need to use toc adjusting stubs when branching
11817    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11818    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11819    needed, and 2 if a cyclical call-graph was found but no other reason
11820    for a stub was detected.  If called from the top level, a return of
11821    2 means the same as a return of 0.  */
11822
11823 static int
11824 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11825 {
11826   int ret;
11827
11828   /* Mark this section as checked.  */
11829   isec->call_check_done = 1;
11830
11831   /* We know none of our code bearing sections will need toc stubs.  */
11832   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11833     return 0;
11834
11835   if (isec->size == 0)
11836     return 0;
11837
11838   if (isec->output_section == NULL)
11839     return 0;
11840
11841   ret = 0;
11842   if (isec->reloc_count != 0)
11843     {
11844       Elf_Internal_Rela *relstart, *rel;
11845       Elf_Internal_Sym *local_syms;
11846       struct ppc_link_hash_table *htab;
11847
11848       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11849                                             info->keep_memory);
11850       if (relstart == NULL)
11851         return -1;
11852
11853       /* Look for branches to outside of this section.  */
11854       local_syms = NULL;
11855       htab = ppc_hash_table (info);
11856       if (htab == NULL)
11857         return -1;
11858
11859       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11860         {
11861           enum elf_ppc64_reloc_type r_type;
11862           unsigned long r_symndx;
11863           struct elf_link_hash_entry *h;
11864           struct ppc_link_hash_entry *eh;
11865           Elf_Internal_Sym *sym;
11866           asection *sym_sec;
11867           struct _opd_sec_data *opd;
11868           bfd_vma sym_value;
11869           bfd_vma dest;
11870
11871           r_type = ELF64_R_TYPE (rel->r_info);
11872           if (r_type != R_PPC64_REL24
11873               && r_type != R_PPC64_REL14
11874               && r_type != R_PPC64_REL14_BRTAKEN
11875               && r_type != R_PPC64_REL14_BRNTAKEN)
11876             continue;
11877
11878           r_symndx = ELF64_R_SYM (rel->r_info);
11879           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11880                           isec->owner))
11881             {
11882               ret = -1;
11883               break;
11884             }
11885
11886           /* Calls to dynamic lib functions go through a plt call stub
11887              that uses r2.  */
11888           eh = (struct ppc_link_hash_entry *) h;
11889           if (eh != NULL
11890               && (eh->elf.plt.plist != NULL
11891                   || (eh->oh != NULL
11892                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11893             {
11894               ret = 1;
11895               break;
11896             }
11897
11898           if (sym_sec == NULL)
11899             /* Ignore other undefined symbols.  */
11900             continue;
11901
11902           /* Assume branches to other sections not included in the
11903              link need stubs too, to cover -R and absolute syms.  */
11904           if (sym_sec->output_section == NULL)
11905             {
11906               ret = 1;
11907               break;
11908             }
11909
11910           if (h == NULL)
11911             sym_value = sym->st_value;
11912           else
11913             {
11914               if (h->root.type != bfd_link_hash_defined
11915                   && h->root.type != bfd_link_hash_defweak)
11916                 abort ();
11917               sym_value = h->root.u.def.value;
11918             }
11919           sym_value += rel->r_addend;
11920
11921           /* If this branch reloc uses an opd sym, find the code section.  */
11922           opd = get_opd_info (sym_sec);
11923           if (opd != NULL)
11924             {
11925               if (h == NULL && opd->adjust != NULL)
11926                 {
11927                   long adjust;
11928
11929                   adjust = opd->adjust[OPD_NDX (sym_value)];
11930                   if (adjust == -1)
11931                     /* Assume deleted functions won't ever be called.  */
11932                     continue;
11933                   sym_value += adjust;
11934                 }
11935
11936               dest = opd_entry_value (sym_sec, sym_value,
11937                                       &sym_sec, NULL, FALSE);
11938               if (dest == (bfd_vma) -1)
11939                 continue;
11940             }
11941           else
11942             dest = (sym_value
11943                     + sym_sec->output_offset
11944                     + sym_sec->output_section->vma);
11945
11946           /* Ignore branch to self.  */
11947           if (sym_sec == isec)
11948             continue;
11949
11950           /* If the called function uses the toc, we need a stub.  */
11951           if (sym_sec->has_toc_reloc
11952               || sym_sec->makes_toc_func_call)
11953             {
11954               ret = 1;
11955               break;
11956             }
11957
11958           /* Assume any branch that needs a long branch stub might in fact
11959              need a plt_branch stub.  A plt_branch stub uses r2.  */
11960           else if (dest - (isec->output_offset
11961                            + isec->output_section->vma
11962                            + rel->r_offset) + (1 << 25)
11963                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11964                                                              ? h->other
11965                                                              : sym->st_other))
11966             {
11967               ret = 1;
11968               break;
11969             }
11970
11971           /* If calling back to a section in the process of being
11972              tested, we can't say for sure that no toc adjusting stubs
11973              are needed, so don't return zero.  */
11974           else if (sym_sec->call_check_in_progress)
11975             ret = 2;
11976
11977           /* Branches to another section that itself doesn't have any TOC
11978              references are OK.  Recursively call ourselves to check.  */
11979           else if (!sym_sec->call_check_done)
11980             {
11981               int recur;
11982
11983               /* Mark current section as indeterminate, so that other
11984                  sections that call back to current won't be marked as
11985                  known.  */
11986               isec->call_check_in_progress = 1;
11987               recur = toc_adjusting_stub_needed (info, sym_sec);
11988               isec->call_check_in_progress = 0;
11989
11990               if (recur != 0)
11991                 {
11992                   ret = recur;
11993                   if (recur != 2)
11994                     break;
11995                 }
11996             }
11997         }
11998
11999       if (local_syms != NULL
12000           && (elf_symtab_hdr (isec->owner).contents
12001               != (unsigned char *) local_syms))
12002         free (local_syms);
12003       if (elf_section_data (isec)->relocs != relstart)
12004         free (relstart);
12005     }
12006
12007   if ((ret & 1) == 0
12008       && isec->map_head.s != NULL
12009       && (strcmp (isec->output_section->name, ".init") == 0
12010           || strcmp (isec->output_section->name, ".fini") == 0))
12011     {
12012       if (isec->map_head.s->has_toc_reloc
12013           || isec->map_head.s->makes_toc_func_call)
12014         ret = 1;
12015       else if (!isec->map_head.s->call_check_done)
12016         {
12017           int recur;
12018           isec->call_check_in_progress = 1;
12019           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12020           isec->call_check_in_progress = 0;
12021           if (recur != 0)
12022             ret = recur;
12023         }
12024     }
12025
12026   if (ret == 1)
12027     isec->makes_toc_func_call = 1;
12028
12029   return ret;
12030 }
12031
12032 /* The linker repeatedly calls this function for each input section,
12033    in the order that input sections are linked into output sections.
12034    Build lists of input sections to determine groupings between which
12035    we may insert linker stubs.  */
12036
12037 bfd_boolean
12038 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12039 {
12040   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12041
12042   if (htab == NULL)
12043     return FALSE;
12044
12045   if ((isec->output_section->flags & SEC_CODE) != 0
12046       && isec->output_section->id < htab->sec_info_arr_size)
12047     {
12048       /* This happens to make the list in reverse order,
12049          which is what we want.  */
12050       htab->sec_info[isec->id].u.list
12051         = htab->sec_info[isec->output_section->id].u.list;
12052       htab->sec_info[isec->output_section->id].u.list = isec;
12053     }
12054
12055   if (htab->multi_toc_needed)
12056     {
12057       /* Analyse sections that aren't already flagged as needing a
12058          valid toc pointer.  Exclude .fixup for the linux kernel.
12059          .fixup contains branches, but only back to the function that
12060          hit an exception.  */
12061       if (!(isec->has_toc_reloc
12062             || (isec->flags & SEC_CODE) == 0
12063             || strcmp (isec->name, ".fixup") == 0
12064             || isec->call_check_done))
12065         {
12066           if (toc_adjusting_stub_needed (info, isec) < 0)
12067             return FALSE;
12068         }
12069       /* Make all sections use the TOC assigned for this object file.
12070          This will be wrong for pasted sections;  We fix that in
12071          check_pasted_section().  */
12072       if (elf_gp (isec->owner) != 0)
12073         htab->toc_curr = elf_gp (isec->owner);
12074     }
12075
12076   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12077   return TRUE;
12078 }
12079
12080 /* Check that all .init and .fini sections use the same toc, if they
12081    have toc relocs.  */
12082
12083 static bfd_boolean
12084 check_pasted_section (struct bfd_link_info *info, const char *name)
12085 {
12086   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12087
12088   if (o != NULL)
12089     {
12090       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12091       bfd_vma toc_off = 0;
12092       asection *i;
12093
12094       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12095         if (i->has_toc_reloc)
12096           {
12097             if (toc_off == 0)
12098               toc_off = htab->sec_info[i->id].toc_off;
12099             else if (toc_off != htab->sec_info[i->id].toc_off)
12100               return FALSE;
12101           }
12102
12103       if (toc_off == 0)
12104         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12105           if (i->makes_toc_func_call)
12106             {
12107               toc_off = htab->sec_info[i->id].toc_off;
12108               break;
12109             }
12110
12111       /* Make sure the whole pasted function uses the same toc offset.  */
12112       if (toc_off != 0)
12113         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12114           htab->sec_info[i->id].toc_off = toc_off;
12115     }
12116   return TRUE;
12117 }
12118
12119 bfd_boolean
12120 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12121 {
12122   return (check_pasted_section (info, ".init")
12123           & check_pasted_section (info, ".fini"));
12124 }
12125
12126 /* See whether we can group stub sections together.  Grouping stub
12127    sections may result in fewer stubs.  More importantly, we need to
12128    put all .init* and .fini* stubs at the beginning of the .init or
12129    .fini output sections respectively, because glibc splits the
12130    _init and _fini functions into multiple parts.  Putting a stub in
12131    the middle of a function is not a good idea.  */
12132
12133 static bfd_boolean
12134 group_sections (struct bfd_link_info *info,
12135                 bfd_size_type stub_group_size,
12136                 bfd_boolean stubs_always_before_branch)
12137 {
12138   struct ppc_link_hash_table *htab;
12139   asection *osec;
12140   bfd_boolean suppress_size_errors;
12141
12142   htab = ppc_hash_table (info);
12143   if (htab == NULL)
12144     return FALSE;
12145
12146   suppress_size_errors = FALSE;
12147   if (stub_group_size == 1)
12148     {
12149       /* Default values.  */
12150       if (stubs_always_before_branch)
12151         stub_group_size = 0x1e00000;
12152       else
12153         stub_group_size = 0x1c00000;
12154       suppress_size_errors = TRUE;
12155     }
12156
12157   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12158     {
12159       asection *tail;
12160
12161       if (osec->id >= htab->sec_info_arr_size)
12162         continue;
12163
12164       tail = htab->sec_info[osec->id].u.list;
12165       while (tail != NULL)
12166         {
12167           asection *curr;
12168           asection *prev;
12169           bfd_size_type total;
12170           bfd_boolean big_sec;
12171           bfd_vma curr_toc;
12172           struct map_stub *group;
12173           bfd_size_type group_size;
12174
12175           curr = tail;
12176           total = tail->size;
12177           group_size = (ppc64_elf_section_data (tail) != NULL
12178                         && ppc64_elf_section_data (tail)->has_14bit_branch
12179                         ? stub_group_size >> 10 : stub_group_size);
12180
12181           big_sec = total > group_size;
12182           if (big_sec && !suppress_size_errors)
12183             /* xgettext:c-format */
12184             _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12185                                 tail->owner, tail);
12186           curr_toc = htab->sec_info[tail->id].toc_off;
12187
12188           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12189                  && ((total += curr->output_offset - prev->output_offset)
12190                      < (ppc64_elf_section_data (prev) != NULL
12191                         && ppc64_elf_section_data (prev)->has_14bit_branch
12192                         ? (group_size = stub_group_size >> 10) : group_size))
12193                  && htab->sec_info[prev->id].toc_off == curr_toc)
12194             curr = prev;
12195
12196           /* OK, the size from the start of CURR to the end is less
12197              than group_size and thus can be handled by one stub
12198              section.  (or the tail section is itself larger than
12199              group_size, in which case we may be toast.)  We should
12200              really be keeping track of the total size of stubs added
12201              here, as stubs contribute to the final output section
12202              size.  That's a little tricky, and this way will only
12203              break if stubs added make the total size more than 2^25,
12204              ie. for the default stub_group_size, if stubs total more
12205              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12206           group = bfd_alloc (curr->owner, sizeof (*group));
12207           if (group == NULL)
12208             return FALSE;
12209           group->link_sec = curr;
12210           group->stub_sec = NULL;
12211           group->needs_save_res = 0;
12212           group->tls_get_addr_opt_bctrl = -1u;
12213           group->next = htab->group;
12214           htab->group = group;
12215           do
12216             {
12217               prev = htab->sec_info[tail->id].u.list;
12218               /* Set up this stub group.  */
12219               htab->sec_info[tail->id].u.group = group;
12220             }
12221           while (tail != curr && (tail = prev) != NULL);
12222
12223           /* But wait, there's more!  Input sections up to group_size
12224              bytes before the stub section can be handled by it too.
12225              Don't do this if we have a really large section after the
12226              stubs, as adding more stubs increases the chance that
12227              branches may not reach into the stub section.  */
12228           if (!stubs_always_before_branch && !big_sec)
12229             {
12230               total = 0;
12231               while (prev != NULL
12232                      && ((total += tail->output_offset - prev->output_offset)
12233                          < (ppc64_elf_section_data (prev) != NULL
12234                             && ppc64_elf_section_data (prev)->has_14bit_branch
12235                             ? (group_size = stub_group_size >> 10) : group_size))
12236                      && htab->sec_info[prev->id].toc_off == curr_toc)
12237                 {
12238                   tail = prev;
12239                   prev = htab->sec_info[tail->id].u.list;
12240                   htab->sec_info[tail->id].u.group = group;
12241                 }
12242             }
12243           tail = prev;
12244         }
12245     }
12246   return TRUE;
12247 }
12248
12249 static const unsigned char glink_eh_frame_cie[] =
12250 {
12251   0, 0, 0, 16,                          /* length.  */
12252   0, 0, 0, 0,                           /* id.  */
12253   1,                                    /* CIE version.  */
12254   'z', 'R', 0,                          /* Augmentation string.  */
12255   4,                                    /* Code alignment.  */
12256   0x78,                                 /* Data alignment.  */
12257   65,                                   /* RA reg.  */
12258   1,                                    /* Augmentation size.  */
12259   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12260   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12261 };
12262
12263 static size_t
12264 stub_eh_frame_size (struct map_stub *group, size_t align)
12265 {
12266   size_t this_size = 17;
12267   if (group->tls_get_addr_opt_bctrl != -1u)
12268     {
12269       unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12270       if (to_bctrl < 64)
12271         this_size += 1;
12272       else if (to_bctrl < 256)
12273         this_size += 2;
12274       else if (to_bctrl < 65536)
12275         this_size += 3;
12276       else
12277         this_size += 5;
12278       this_size += 6;
12279     }
12280   this_size = (this_size + align - 1) & -align;
12281   return this_size;
12282 }
12283
12284 /* Stripping output sections is normally done before dynamic section
12285    symbols have been allocated.  This function is called later, and
12286    handles cases like htab->brlt which is mapped to its own output
12287    section.  */
12288
12289 static void
12290 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12291 {
12292   if (isec->size == 0
12293       && isec->output_section->size == 0
12294       && !(isec->output_section->flags & SEC_KEEP)
12295       && !bfd_section_removed_from_list (info->output_bfd,
12296                                          isec->output_section)
12297       && elf_section_data (isec->output_section)->dynindx == 0)
12298     {
12299       isec->output_section->flags |= SEC_EXCLUDE;
12300       bfd_section_list_remove (info->output_bfd, isec->output_section);
12301       info->output_bfd->section_count--;
12302     }
12303 }
12304
12305 /* Determine and set the size of the stub section for a final link.
12306
12307    The basic idea here is to examine all the relocations looking for
12308    PC-relative calls to a target that is unreachable with a "bl"
12309    instruction.  */
12310
12311 bfd_boolean
12312 ppc64_elf_size_stubs (struct bfd_link_info *info)
12313 {
12314   bfd_size_type stub_group_size;
12315   bfd_boolean stubs_always_before_branch;
12316   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12317
12318   if (htab == NULL)
12319     return FALSE;
12320
12321   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12322     htab->params->plt_thread_safe = 1;
12323   if (!htab->opd_abi)
12324     htab->params->plt_thread_safe = 0;
12325   else if (htab->params->plt_thread_safe == -1)
12326     {
12327       static const char *const thread_starter[] =
12328         {
12329           "pthread_create",
12330           /* libstdc++ */
12331           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12332           /* librt */
12333           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12334           "mq_notify", "create_timer",
12335           /* libanl */
12336           "getaddrinfo_a",
12337           /* libgomp */
12338           "GOMP_parallel",
12339           "GOMP_parallel_start",
12340           "GOMP_parallel_loop_static",
12341           "GOMP_parallel_loop_static_start",
12342           "GOMP_parallel_loop_dynamic",
12343           "GOMP_parallel_loop_dynamic_start",
12344           "GOMP_parallel_loop_guided",
12345           "GOMP_parallel_loop_guided_start",
12346           "GOMP_parallel_loop_runtime",
12347           "GOMP_parallel_loop_runtime_start",
12348           "GOMP_parallel_sections",
12349           "GOMP_parallel_sections_start",
12350           /* libgo */
12351           "__go_go",
12352         };
12353       unsigned i;
12354
12355       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12356         {
12357           struct elf_link_hash_entry *h;
12358           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12359                                     FALSE, FALSE, TRUE);
12360           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12361           if (htab->params->plt_thread_safe)
12362             break;
12363         }
12364     }
12365   stubs_always_before_branch = htab->params->group_size < 0;
12366   if (htab->params->group_size < 0)
12367     stub_group_size = -htab->params->group_size;
12368   else
12369     stub_group_size = htab->params->group_size;
12370
12371   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12372     return FALSE;
12373
12374 #define STUB_SHRINK_ITER 20
12375   /* Loop until no stubs added.  After iteration 20 of this loop we may
12376      exit on a stub section shrinking.  This is to break out of a
12377      pathological case where adding stubs on one iteration decreases
12378      section gaps (perhaps due to alignment), which then requires
12379      fewer or smaller stubs on the next iteration.  */
12380
12381   while (1)
12382     {
12383       bfd *input_bfd;
12384       unsigned int bfd_indx;
12385       struct map_stub *group;
12386
12387       htab->stub_iteration += 1;
12388
12389       for (input_bfd = info->input_bfds, bfd_indx = 0;
12390            input_bfd != NULL;
12391            input_bfd = input_bfd->link.next, bfd_indx++)
12392         {
12393           Elf_Internal_Shdr *symtab_hdr;
12394           asection *section;
12395           Elf_Internal_Sym *local_syms = NULL;
12396
12397           if (!is_ppc64_elf (input_bfd))
12398             continue;
12399
12400           /* We'll need the symbol table in a second.  */
12401           symtab_hdr = &elf_symtab_hdr (input_bfd);
12402           if (symtab_hdr->sh_info == 0)
12403             continue;
12404
12405           /* Walk over each section attached to the input bfd.  */
12406           for (section = input_bfd->sections;
12407                section != NULL;
12408                section = section->next)
12409             {
12410               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12411
12412               /* If there aren't any relocs, then there's nothing more
12413                  to do.  */
12414               if ((section->flags & SEC_RELOC) == 0
12415                   || (section->flags & SEC_ALLOC) == 0
12416                   || (section->flags & SEC_LOAD) == 0
12417                   || (section->flags & SEC_CODE) == 0
12418                   || section->reloc_count == 0)
12419                 continue;
12420
12421               /* If this section is a link-once section that will be
12422                  discarded, then don't create any stubs.  */
12423               if (section->output_section == NULL
12424                   || section->output_section->owner != info->output_bfd)
12425                 continue;
12426
12427               /* Get the relocs.  */
12428               internal_relocs
12429                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12430                                              info->keep_memory);
12431               if (internal_relocs == NULL)
12432                 goto error_ret_free_local;
12433
12434               /* Now examine each relocation.  */
12435               irela = internal_relocs;
12436               irelaend = irela + section->reloc_count;
12437               for (; irela < irelaend; irela++)
12438                 {
12439                   enum elf_ppc64_reloc_type r_type;
12440                   unsigned int r_indx;
12441                   enum ppc_stub_type stub_type;
12442                   struct ppc_stub_hash_entry *stub_entry;
12443                   asection *sym_sec, *code_sec;
12444                   bfd_vma sym_value, code_value;
12445                   bfd_vma destination;
12446                   unsigned long local_off;
12447                   bfd_boolean ok_dest;
12448                   struct ppc_link_hash_entry *hash;
12449                   struct ppc_link_hash_entry *fdh;
12450                   struct elf_link_hash_entry *h;
12451                   Elf_Internal_Sym *sym;
12452                   char *stub_name;
12453                   const asection *id_sec;
12454                   struct _opd_sec_data *opd;
12455                   struct plt_entry *plt_ent;
12456
12457                   r_type = ELF64_R_TYPE (irela->r_info);
12458                   r_indx = ELF64_R_SYM (irela->r_info);
12459
12460                   if (r_type >= R_PPC64_max)
12461                     {
12462                       bfd_set_error (bfd_error_bad_value);
12463                       goto error_ret_free_internal;
12464                     }
12465
12466                   /* Only look for stubs on branch instructions.  */
12467                   if (r_type != R_PPC64_REL24
12468                       && r_type != R_PPC64_REL14
12469                       && r_type != R_PPC64_REL14_BRTAKEN
12470                       && r_type != R_PPC64_REL14_BRNTAKEN)
12471                     continue;
12472
12473                   /* Now determine the call target, its name, value,
12474                      section.  */
12475                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12476                                   r_indx, input_bfd))
12477                     goto error_ret_free_internal;
12478                   hash = (struct ppc_link_hash_entry *) h;
12479
12480                   ok_dest = FALSE;
12481                   fdh = NULL;
12482                   sym_value = 0;
12483                   if (hash == NULL)
12484                     {
12485                       sym_value = sym->st_value;
12486                       if (sym_sec != NULL
12487                           && sym_sec->output_section != NULL)
12488                         ok_dest = TRUE;
12489                     }
12490                   else if (hash->elf.root.type == bfd_link_hash_defined
12491                            || hash->elf.root.type == bfd_link_hash_defweak)
12492                     {
12493                       sym_value = hash->elf.root.u.def.value;
12494                       if (sym_sec->output_section != NULL)
12495                         ok_dest = TRUE;
12496                     }
12497                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12498                            || hash->elf.root.type == bfd_link_hash_undefined)
12499                     {
12500                       /* Recognise an old ABI func code entry sym, and
12501                          use the func descriptor sym instead if it is
12502                          defined.  */
12503                       if (hash->elf.root.root.string[0] == '.'
12504                           && hash->oh != NULL)
12505                         {
12506                           fdh = ppc_follow_link (hash->oh);
12507                           if (fdh->elf.root.type == bfd_link_hash_defined
12508                               || fdh->elf.root.type == bfd_link_hash_defweak)
12509                             {
12510                               sym_sec = fdh->elf.root.u.def.section;
12511                               sym_value = fdh->elf.root.u.def.value;
12512                               if (sym_sec->output_section != NULL)
12513                                 ok_dest = TRUE;
12514                             }
12515                           else
12516                             fdh = NULL;
12517                         }
12518                     }
12519                   else
12520                     {
12521                       bfd_set_error (bfd_error_bad_value);
12522                       goto error_ret_free_internal;
12523                     }
12524
12525                   destination = 0;
12526                   local_off = 0;
12527                   if (ok_dest)
12528                     {
12529                       sym_value += irela->r_addend;
12530                       destination = (sym_value
12531                                      + sym_sec->output_offset
12532                                      + sym_sec->output_section->vma);
12533                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12534                                                             ? hash->elf.other
12535                                                             : sym->st_other);
12536                     }
12537
12538                   code_sec = sym_sec;
12539                   code_value = sym_value;
12540                   opd = get_opd_info (sym_sec);
12541                   if (opd != NULL)
12542                     {
12543                       bfd_vma dest;
12544
12545                       if (hash == NULL && opd->adjust != NULL)
12546                         {
12547                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12548                           if (adjust == -1)
12549                             continue;
12550                           code_value += adjust;
12551                           sym_value += adjust;
12552                         }
12553                       dest = opd_entry_value (sym_sec, sym_value,
12554                                               &code_sec, &code_value, FALSE);
12555                       if (dest != (bfd_vma) -1)
12556                         {
12557                           destination = dest;
12558                           if (fdh != NULL)
12559                             {
12560                               /* Fixup old ABI sym to point at code
12561                                  entry.  */
12562                               hash->elf.root.type = bfd_link_hash_defweak;
12563                               hash->elf.root.u.def.section = code_sec;
12564                               hash->elf.root.u.def.value = code_value;
12565                             }
12566                         }
12567                     }
12568
12569                   /* Determine what (if any) linker stub is needed.  */
12570                   plt_ent = NULL;
12571                   stub_type = ppc_type_of_stub (section, irela, &hash,
12572                                                 &plt_ent, destination,
12573                                                 local_off);
12574
12575                   if (stub_type != ppc_stub_plt_call)
12576                     {
12577                       /* Check whether we need a TOC adjusting stub.
12578                          Since the linker pastes together pieces from
12579                          different object files when creating the
12580                          _init and _fini functions, it may be that a
12581                          call to what looks like a local sym is in
12582                          fact a call needing a TOC adjustment.  */
12583                       if (code_sec != NULL
12584                           && code_sec->output_section != NULL
12585                           && (htab->sec_info[code_sec->id].toc_off
12586                               != htab->sec_info[section->id].toc_off)
12587                           && (code_sec->has_toc_reloc
12588                               || code_sec->makes_toc_func_call))
12589                         stub_type = ppc_stub_long_branch_r2off;
12590                     }
12591
12592                   if (stub_type == ppc_stub_none)
12593                     continue;
12594
12595                   /* __tls_get_addr calls might be eliminated.  */
12596                   if (stub_type != ppc_stub_plt_call
12597                       && hash != NULL
12598                       && (hash == htab->tls_get_addr
12599                           || hash == htab->tls_get_addr_fd)
12600                       && section->has_tls_reloc
12601                       && irela != internal_relocs)
12602                     {
12603                       /* Get tls info.  */
12604                       unsigned char *tls_mask;
12605
12606                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12607                                          irela - 1, input_bfd))
12608                         goto error_ret_free_internal;
12609                       if ((*tls_mask & TLS_TLS) != 0)
12610                         continue;
12611                     }
12612
12613                   if (stub_type == ppc_stub_plt_call)
12614                     {
12615                       if (!htab->opd_abi
12616                           && htab->params->plt_localentry0 != 0
12617                           && is_elfv2_localentry0 (&hash->elf))
12618                         htab->has_plt_localentry0 = 1;
12619                       else if (irela + 1 < irelaend
12620                                && irela[1].r_offset == irela->r_offset + 4
12621                                && (ELF64_R_TYPE (irela[1].r_info)
12622                                    == R_PPC64_TOCSAVE))
12623                         {
12624                           if (!tocsave_find (htab, INSERT,
12625                                              &local_syms, irela + 1, input_bfd))
12626                             goto error_ret_free_internal;
12627                         }
12628                       else
12629                         stub_type = ppc_stub_plt_call_r2save;
12630                     }
12631
12632                   /* Support for grouping stub sections.  */
12633                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12634
12635                   /* Get the name of this stub.  */
12636                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12637                   if (!stub_name)
12638                     goto error_ret_free_internal;
12639
12640                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12641                                                      stub_name, FALSE, FALSE);
12642                   if (stub_entry != NULL)
12643                     {
12644                       /* The proper stub has already been created.  */
12645                       free (stub_name);
12646                       if (stub_type == ppc_stub_plt_call_r2save)
12647                         stub_entry->stub_type = stub_type;
12648                       continue;
12649                     }
12650
12651                   stub_entry = ppc_add_stub (stub_name, section, info);
12652                   if (stub_entry == NULL)
12653                     {
12654                       free (stub_name);
12655                     error_ret_free_internal:
12656                       if (elf_section_data (section)->relocs == NULL)
12657                         free (internal_relocs);
12658                     error_ret_free_local:
12659                       if (local_syms != NULL
12660                           && (symtab_hdr->contents
12661                               != (unsigned char *) local_syms))
12662                         free (local_syms);
12663                       return FALSE;
12664                     }
12665
12666                   stub_entry->stub_type = stub_type;
12667                   if (stub_type != ppc_stub_plt_call
12668                       && stub_type != ppc_stub_plt_call_r2save)
12669                     {
12670                       stub_entry->target_value = code_value;
12671                       stub_entry->target_section = code_sec;
12672                     }
12673                   else
12674                     {
12675                       stub_entry->target_value = sym_value;
12676                       stub_entry->target_section = sym_sec;
12677                     }
12678                   stub_entry->h = hash;
12679                   stub_entry->plt_ent = plt_ent;
12680                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12681
12682                   if (stub_entry->h != NULL)
12683                     htab->stub_globals += 1;
12684                 }
12685
12686               /* We're done with the internal relocs, free them.  */
12687               if (elf_section_data (section)->relocs != internal_relocs)
12688                 free (internal_relocs);
12689             }
12690
12691           if (local_syms != NULL
12692               && symtab_hdr->contents != (unsigned char *) local_syms)
12693             {
12694               if (!info->keep_memory)
12695                 free (local_syms);
12696               else
12697                 symtab_hdr->contents = (unsigned char *) local_syms;
12698             }
12699         }
12700
12701       /* We may have added some stubs.  Find out the new size of the
12702          stub sections.  */
12703       for (group = htab->group; group != NULL; group = group->next)
12704         if (group->stub_sec != NULL)
12705           {
12706             asection *stub_sec = group->stub_sec;
12707
12708             if (htab->stub_iteration <= STUB_SHRINK_ITER
12709                 || stub_sec->rawsize < stub_sec->size)
12710               /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12711               stub_sec->rawsize = stub_sec->size;
12712             stub_sec->size = 0;
12713             stub_sec->reloc_count = 0;
12714             stub_sec->flags &= ~SEC_RELOC;
12715           }
12716
12717       if (htab->stub_iteration <= STUB_SHRINK_ITER
12718           || htab->brlt->rawsize < htab->brlt->size)
12719         htab->brlt->rawsize = htab->brlt->size;
12720       htab->brlt->size = 0;
12721       htab->brlt->reloc_count = 0;
12722       htab->brlt->flags &= ~SEC_RELOC;
12723       if (htab->relbrlt != NULL)
12724         htab->relbrlt->size = 0;
12725
12726       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12727
12728       for (group = htab->group; group != NULL; group = group->next)
12729         if (group->needs_save_res)
12730           group->stub_sec->size += htab->sfpr->size;
12731
12732       if (info->emitrelocations
12733           && htab->glink != NULL && htab->glink->size != 0)
12734         {
12735           htab->glink->reloc_count = 1;
12736           htab->glink->flags |= SEC_RELOC;
12737         }
12738
12739       if (htab->glink_eh_frame != NULL
12740           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12741           && htab->glink_eh_frame->output_section->size > 8)
12742         {
12743           size_t size = 0, align = 4;
12744
12745           for (group = htab->group; group != NULL; group = group->next)
12746             if (group->stub_sec != NULL)
12747               size += stub_eh_frame_size (group, align);
12748           if (htab->glink != NULL && htab->glink->size != 0)
12749             size += (24 + align - 1) & -align;
12750           if (size != 0)
12751             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12752           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12753           size = (size + align - 1) & -align;
12754           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12755           htab->glink_eh_frame->size = size;
12756         }
12757
12758       if (htab->params->plt_stub_align != 0)
12759         for (group = htab->group; group != NULL; group = group->next)
12760           if (group->stub_sec != NULL)
12761             {
12762               int align = abs (htab->params->plt_stub_align);
12763               group->stub_sec->size
12764                 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12765             }
12766
12767       for (group = htab->group; group != NULL; group = group->next)
12768         if (group->stub_sec != NULL
12769             && group->stub_sec->rawsize != group->stub_sec->size
12770             && (htab->stub_iteration <= STUB_SHRINK_ITER
12771                 || group->stub_sec->rawsize < group->stub_sec->size))
12772           break;
12773
12774       if (group == NULL
12775           && (htab->brlt->rawsize == htab->brlt->size
12776               || (htab->stub_iteration > STUB_SHRINK_ITER
12777                   && htab->brlt->rawsize > htab->brlt->size))
12778           && (htab->glink_eh_frame == NULL
12779               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12780         break;
12781
12782       /* Ask the linker to do its stuff.  */
12783       (*htab->params->layout_sections_again) ();
12784     }
12785
12786   if (htab->glink_eh_frame != NULL
12787       && htab->glink_eh_frame->size != 0)
12788     {
12789       bfd_vma val;
12790       bfd_byte *p, *last_fde;
12791       size_t last_fde_len, size, align, pad;
12792       struct map_stub *group;
12793
12794       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12795       if (p == NULL)
12796         return FALSE;
12797       htab->glink_eh_frame->contents = p;
12798       last_fde = p;
12799       align = 4;
12800
12801       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12802       /* CIE length (rewrite in case little-endian).  */
12803       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12804       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12805       p += last_fde_len + 4;
12806
12807       for (group = htab->group; group != NULL; group = group->next)
12808         if (group->stub_sec != NULL)
12809           {
12810             last_fde = p;
12811             last_fde_len = stub_eh_frame_size (group, align) - 4;
12812             /* FDE length.  */
12813             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12814             p += 4;
12815             /* CIE pointer.  */
12816             val = p - htab->glink_eh_frame->contents;
12817             bfd_put_32 (htab->elf.dynobj, val, p);
12818             p += 4;
12819             /* Offset to stub section, written later.  */
12820             p += 4;
12821             /* stub section size.  */
12822             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12823             p += 4;
12824             /* Augmentation.  */
12825             p += 1;
12826             if (group->tls_get_addr_opt_bctrl != -1u)
12827               {
12828                 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12829
12830                 /* This FDE needs more than just the default.
12831                    Describe __tls_get_addr_opt stub LR.  */
12832                 if (to_bctrl < 64)
12833                   *p++ = DW_CFA_advance_loc + to_bctrl;
12834                 else if (to_bctrl < 256)
12835                   {
12836                     *p++ = DW_CFA_advance_loc1;
12837                     *p++ = to_bctrl;
12838                   }
12839                 else if (to_bctrl < 65536)
12840                   {
12841                     *p++ = DW_CFA_advance_loc2;
12842                     bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12843                     p += 2;
12844                   }
12845                 else
12846                   {
12847                     *p++ = DW_CFA_advance_loc4;
12848                     bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12849                     p += 4;
12850                   }
12851                 *p++ = DW_CFA_offset_extended_sf;
12852                 *p++ = 65;
12853                 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12854                 *p++ = DW_CFA_advance_loc + 4;
12855                 *p++ = DW_CFA_restore_extended;
12856                 *p++ = 65;
12857               }
12858             /* Pad.  */
12859             p = last_fde + last_fde_len + 4;
12860           }
12861       if (htab->glink != NULL && htab->glink->size != 0)
12862         {
12863           last_fde = p;
12864           last_fde_len = ((24 + align - 1) & -align) - 4;
12865           /* FDE length.  */
12866           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12867           p += 4;
12868           /* CIE pointer.  */
12869           val = p - htab->glink_eh_frame->contents;
12870           bfd_put_32 (htab->elf.dynobj, val, p);
12871           p += 4;
12872           /* Offset to .glink, written later.  */
12873           p += 4;
12874           /* .glink size.  */
12875           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12876           p += 4;
12877           /* Augmentation.  */
12878           p += 1;
12879
12880           *p++ = DW_CFA_advance_loc + 1;
12881           *p++ = DW_CFA_register;
12882           *p++ = 65;
12883           *p++ = htab->opd_abi ? 12 : 0;
12884           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12885           *p++ = DW_CFA_restore_extended;
12886           *p++ = 65;
12887           p += ((24 + align - 1) & -align) - 24;
12888         }
12889       /* Subsume any padding into the last FDE if user .eh_frame
12890          sections are aligned more than glink_eh_frame.  Otherwise any
12891          zero padding will be seen as a terminator.  */
12892       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12893       size = p - htab->glink_eh_frame->contents;
12894       pad = ((size + align - 1) & -align) - size;
12895       htab->glink_eh_frame->size = size + pad;
12896       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12897     }
12898
12899   maybe_strip_output (info, htab->brlt);
12900   if (htab->glink_eh_frame != NULL)
12901     maybe_strip_output (info, htab->glink_eh_frame);
12902
12903   return TRUE;
12904 }
12905
12906 /* Called after we have determined section placement.  If sections
12907    move, we'll be called again.  Provide a value for TOCstart.  */
12908
12909 bfd_vma
12910 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12911 {
12912   asection *s;
12913   bfd_vma TOCstart, adjust;
12914
12915   if (info != NULL)
12916     {
12917       struct elf_link_hash_entry *h;
12918       struct elf_link_hash_table *htab = elf_hash_table (info);
12919
12920       if (is_elf_hash_table (htab)
12921           && htab->hgot != NULL)
12922         h = htab->hgot;
12923       else
12924         {
12925           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12926           if (is_elf_hash_table (htab))
12927             htab->hgot = h;
12928         }
12929       if (h != NULL
12930           && h->root.type == bfd_link_hash_defined
12931           && !h->root.linker_def
12932           && (!is_elf_hash_table (htab)
12933               || h->def_regular))
12934         {
12935           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12936                       + h->root.u.def.section->output_offset
12937                       + h->root.u.def.section->output_section->vma);
12938           _bfd_set_gp_value (obfd, TOCstart);
12939           return TOCstart;
12940         }
12941     }
12942
12943   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12944      order.  The TOC starts where the first of these sections starts.  */
12945   s = bfd_get_section_by_name (obfd, ".got");
12946   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12947     s = bfd_get_section_by_name (obfd, ".toc");
12948   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12949     s = bfd_get_section_by_name (obfd, ".tocbss");
12950   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12951     s = bfd_get_section_by_name (obfd, ".plt");
12952   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12953     {
12954       /* This may happen for
12955          o  references to TOC base (SYM@toc / TOC[tc0]) without a
12956          .toc directive
12957          o  bad linker script
12958          o --gc-sections and empty TOC sections
12959
12960          FIXME: Warn user?  */
12961
12962       /* Look for a likely section.  We probably won't even be
12963          using TOCstart.  */
12964       for (s = obfd->sections; s != NULL; s = s->next)
12965         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12966                          | SEC_EXCLUDE))
12967             == (SEC_ALLOC | SEC_SMALL_DATA))
12968           break;
12969       if (s == NULL)
12970         for (s = obfd->sections; s != NULL; s = s->next)
12971           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12972               == (SEC_ALLOC | SEC_SMALL_DATA))
12973             break;
12974       if (s == NULL)
12975         for (s = obfd->sections; s != NULL; s = s->next)
12976           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12977               == SEC_ALLOC)
12978             break;
12979       if (s == NULL)
12980         for (s = obfd->sections; s != NULL; s = s->next)
12981           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12982             break;
12983     }
12984
12985   TOCstart = 0;
12986   if (s != NULL)
12987     TOCstart = s->output_section->vma + s->output_offset;
12988
12989   /* Force alignment.  */
12990   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12991   TOCstart -= adjust;
12992   _bfd_set_gp_value (obfd, TOCstart);
12993
12994   if (info != NULL && s != NULL)
12995     {
12996       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12997
12998       if (htab != NULL)
12999         {
13000           if (htab->elf.hgot != NULL)
13001             {
13002               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13003               htab->elf.hgot->root.u.def.section = s;
13004             }
13005         }
13006       else
13007         {
13008           struct bfd_link_hash_entry *bh = NULL;
13009           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13010                                             s, TOC_BASE_OFF - adjust,
13011                                             NULL, FALSE, FALSE, &bh);
13012         }
13013     }
13014   return TOCstart;
13015 }
13016
13017 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13018    write out any global entry stubs.  */
13019
13020 static bfd_boolean
13021 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
13022 {
13023   struct bfd_link_info *info;
13024   struct ppc_link_hash_table *htab;
13025   struct plt_entry *pent;
13026   asection *s;
13027
13028   if (h->root.type == bfd_link_hash_indirect)
13029     return TRUE;
13030
13031   if (!h->pointer_equality_needed)
13032     return TRUE;
13033
13034   if (h->def_regular)
13035     return TRUE;
13036
13037   info = inf;
13038   htab = ppc_hash_table (info);
13039   if (htab == NULL)
13040     return FALSE;
13041
13042   s = htab->global_entry;
13043   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13044     if (pent->plt.offset != (bfd_vma) -1
13045         && pent->addend == 0)
13046       {
13047         bfd_byte *p;
13048         asection *plt;
13049         bfd_vma off;
13050
13051         p = s->contents + h->root.u.def.value;
13052         plt = htab->elf.splt;
13053         if (!htab->elf.dynamic_sections_created
13054             || h->dynindx == -1)
13055           plt = htab->elf.iplt;
13056         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13057         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13058
13059         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13060           {
13061             info->callbacks->einfo
13062               (_("%P: linkage table error against `%pT'\n"),
13063                h->root.root.string);
13064             bfd_set_error (bfd_error_bad_value);
13065             htab->stub_error = TRUE;
13066           }
13067
13068         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13069         if (htab->params->emit_stub_syms)
13070           {
13071             size_t len = strlen (h->root.root.string);
13072             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13073
13074             if (name == NULL)
13075               return FALSE;
13076
13077             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13078             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13079             if (h == NULL)
13080               return FALSE;
13081             if (h->root.type == bfd_link_hash_new)
13082               {
13083                 h->root.type = bfd_link_hash_defined;
13084                 h->root.u.def.section = s;
13085                 h->root.u.def.value = p - s->contents;
13086                 h->ref_regular = 1;
13087                 h->def_regular = 1;
13088                 h->ref_regular_nonweak = 1;
13089                 h->forced_local = 1;
13090                 h->non_elf = 0;
13091                 h->root.linker_def = 1;
13092               }
13093           }
13094
13095         if (PPC_HA (off) != 0)
13096           {
13097             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13098             p += 4;
13099           }
13100         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13101         p += 4;
13102         bfd_put_32 (s->owner, MTCTR_R12, p);
13103         p += 4;
13104         bfd_put_32 (s->owner, BCTR, p);
13105         break;
13106       }
13107   return TRUE;
13108 }
13109
13110 /* Build all the stubs associated with the current output file.
13111    The stubs are kept in a hash table attached to the main linker
13112    hash table.  This function is called via gldelf64ppc_finish.  */
13113
13114 bfd_boolean
13115 ppc64_elf_build_stubs (struct bfd_link_info *info,
13116                        char **stats)
13117 {
13118   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13119   struct map_stub *group;
13120   asection *stub_sec;
13121   bfd_byte *p;
13122   int stub_sec_count = 0;
13123
13124   if (htab == NULL)
13125     return FALSE;
13126
13127   /* Allocate memory to hold the linker stubs.  */
13128   for (group = htab->group; group != NULL; group = group->next)
13129     if ((stub_sec = group->stub_sec) != NULL
13130         && stub_sec->size != 0)
13131       {
13132         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13133         if (stub_sec->contents == NULL)
13134           return FALSE;
13135         stub_sec->size = 0;
13136       }
13137
13138   if (htab->glink != NULL && htab->glink->size != 0)
13139     {
13140       unsigned int indx;
13141       bfd_vma plt0;
13142
13143       /* Build the .glink plt call stub.  */
13144       if (htab->params->emit_stub_syms)
13145         {
13146           struct elf_link_hash_entry *h;
13147           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13148                                     TRUE, FALSE, FALSE);
13149           if (h == NULL)
13150             return FALSE;
13151           if (h->root.type == bfd_link_hash_new)
13152             {
13153               h->root.type = bfd_link_hash_defined;
13154               h->root.u.def.section = htab->glink;
13155               h->root.u.def.value = 8;
13156               h->ref_regular = 1;
13157               h->def_regular = 1;
13158               h->ref_regular_nonweak = 1;
13159               h->forced_local = 1;
13160               h->non_elf = 0;
13161               h->root.linker_def = 1;
13162             }
13163         }
13164       plt0 = (htab->elf.splt->output_section->vma
13165               + htab->elf.splt->output_offset
13166               - 16);
13167       if (info->emitrelocations)
13168         {
13169           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13170           if (r == NULL)
13171             return FALSE;
13172           r->r_offset = (htab->glink->output_offset
13173                          + htab->glink->output_section->vma);
13174           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13175           r->r_addend = plt0;
13176         }
13177       p = htab->glink->contents;
13178       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13179       bfd_put_64 (htab->glink->owner, plt0, p);
13180       p += 8;
13181       if (htab->opd_abi)
13182         {
13183           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13184           p += 4;
13185           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13186           p += 4;
13187           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13188           p += 4;
13189           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13190           p += 4;
13191           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13192           p += 4;
13193           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13194           p += 4;
13195           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13196           p += 4;
13197           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13198           p += 4;
13199           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13200           p += 4;
13201           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13202           p += 4;
13203         }
13204       else
13205         {
13206           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13207           p += 4;
13208           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13209           p += 4;
13210           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13211           p += 4;
13212           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13213           p += 4;
13214           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13215           p += 4;
13216           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13217           p += 4;
13218           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13219           p += 4;
13220           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13221           p += 4;
13222           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13223           p += 4;
13224           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13225           p += 4;
13226           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13227           p += 4;
13228           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13229           p += 4;
13230           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13231           p += 4;
13232         }
13233       bfd_put_32 (htab->glink->owner, BCTR, p);
13234       p += 4;
13235       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13236
13237       /* Build the .glink lazy link call stubs.  */
13238       indx = 0;
13239       while (p < htab->glink->contents + htab->glink->size)
13240         {
13241           if (htab->opd_abi)
13242             {
13243               if (indx < 0x8000)
13244                 {
13245                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13246                   p += 4;
13247                 }
13248               else
13249                 {
13250                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13251                   p += 4;
13252                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13253                               p);
13254                   p += 4;
13255                 }
13256             }
13257           bfd_put_32 (htab->glink->owner,
13258                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13259           indx++;
13260           p += 4;
13261         }
13262     }
13263
13264   /* Build .glink global entry stubs.  */
13265   if (htab->global_entry != NULL && htab->global_entry->size != 0)
13266     elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13267
13268   if (htab->brlt != NULL && htab->brlt->size != 0)
13269     {
13270       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13271                                          htab->brlt->size);
13272       if (htab->brlt->contents == NULL)
13273         return FALSE;
13274     }
13275   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13276     {
13277       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13278                                             htab->relbrlt->size);
13279       if (htab->relbrlt->contents == NULL)
13280         return FALSE;
13281     }
13282
13283   /* Build the stubs as directed by the stub hash table.  */
13284   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13285
13286   for (group = htab->group; group != NULL; group = group->next)
13287     if (group->needs_save_res)
13288       group->stub_sec->size += htab->sfpr->size;
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 (group->needs_save_res)
13303       {
13304         stub_sec = group->stub_sec;
13305         memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13306                 htab->sfpr->contents, htab->sfpr->size);
13307         if (htab->params->emit_stub_syms)
13308           {
13309             unsigned int i;
13310
13311             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13312               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13313                 return FALSE;
13314           }
13315       }
13316
13317   for (group = htab->group; group != NULL; group = group->next)
13318     if ((stub_sec = group->stub_sec) != NULL)
13319       {
13320         stub_sec_count += 1;
13321         if (stub_sec->rawsize != stub_sec->size
13322             && (htab->stub_iteration <= STUB_SHRINK_ITER
13323                 || stub_sec->rawsize < stub_sec->size))
13324           break;
13325       }
13326
13327   if (group != NULL)
13328     {
13329       htab->stub_error = TRUE;
13330       _bfd_error_handler (_("stubs don't match calculated size"));
13331     }
13332
13333   if (htab->stub_error)
13334     return FALSE;
13335
13336   if (stats != NULL)
13337     {
13338       size_t len;
13339       *stats = bfd_malloc (500);
13340       if (*stats == NULL)
13341         return FALSE;
13342
13343       len = sprintf (*stats,
13344                      ngettext ("linker stubs in %u group\n",
13345                                "linker stubs in %u groups\n",
13346                                stub_sec_count),
13347                      stub_sec_count);
13348       sprintf (*stats + len, _("  branch       %lu\n"
13349                                "  toc adjust   %lu\n"
13350                                "  long branch  %lu\n"
13351                                "  long toc adj %lu\n"
13352                                "  plt call     %lu\n"
13353                                "  plt call toc %lu\n"
13354                                "  global entry %lu"),
13355                htab->stub_count[ppc_stub_long_branch - 1],
13356                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13357                htab->stub_count[ppc_stub_plt_branch - 1],
13358                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13359                htab->stub_count[ppc_stub_plt_call - 1],
13360                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13361                htab->stub_count[ppc_stub_global_entry - 1]);
13362     }
13363   return TRUE;
13364 }
13365
13366 /* What to do when ld finds relocations against symbols defined in
13367    discarded sections.  */
13368
13369 static unsigned int
13370 ppc64_elf_action_discarded (asection *sec)
13371 {
13372   if (strcmp (".opd", sec->name) == 0)
13373     return 0;
13374
13375   if (strcmp (".toc", sec->name) == 0)
13376     return 0;
13377
13378   if (strcmp (".toc1", sec->name) == 0)
13379     return 0;
13380
13381   return _bfd_elf_default_action_discarded (sec);
13382 }
13383
13384 /* The RELOCATE_SECTION function is called by the ELF backend linker
13385    to handle the relocations for a section.
13386
13387    The relocs are always passed as Rela structures; if the section
13388    actually uses Rel structures, the r_addend field will always be
13389    zero.
13390
13391    This function is responsible for adjust the section contents as
13392    necessary, and (if using Rela relocs and generating a
13393    relocatable output file) adjusting the reloc addend as
13394    necessary.
13395
13396    This function does not have to worry about setting the reloc
13397    address or the reloc symbol index.
13398
13399    LOCAL_SYMS is a pointer to the swapped in local symbols.
13400
13401    LOCAL_SECTIONS is an array giving the section in the input file
13402    corresponding to the st_shndx field of each local symbol.
13403
13404    The global hash table entry for the global symbols can be found
13405    via elf_sym_hashes (input_bfd).
13406
13407    When generating relocatable output, this function must handle
13408    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13409    going to be the section symbol corresponding to the output
13410    section, which means that the addend must be adjusted
13411    accordingly.  */
13412
13413 static bfd_boolean
13414 ppc64_elf_relocate_section (bfd *output_bfd,
13415                             struct bfd_link_info *info,
13416                             bfd *input_bfd,
13417                             asection *input_section,
13418                             bfd_byte *contents,
13419                             Elf_Internal_Rela *relocs,
13420                             Elf_Internal_Sym *local_syms,
13421                             asection **local_sections)
13422 {
13423   struct ppc_link_hash_table *htab;
13424   Elf_Internal_Shdr *symtab_hdr;
13425   struct elf_link_hash_entry **sym_hashes;
13426   Elf_Internal_Rela *rel;
13427   Elf_Internal_Rela *wrel;
13428   Elf_Internal_Rela *relend;
13429   Elf_Internal_Rela outrel;
13430   bfd_byte *loc;
13431   struct got_entry **local_got_ents;
13432   bfd_vma TOCstart;
13433   bfd_boolean ret = TRUE;
13434   bfd_boolean is_opd;
13435   /* Assume 'at' branch hints.  */
13436   bfd_boolean is_isa_v2 = TRUE;
13437   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13438
13439   /* Initialize howto table if needed.  */
13440   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13441     ppc_howto_init ();
13442
13443   htab = ppc_hash_table (info);
13444   if (htab == NULL)
13445     return FALSE;
13446
13447   /* Don't relocate stub sections.  */
13448   if (input_section->owner == htab->params->stub_bfd)
13449     return TRUE;
13450
13451   BFD_ASSERT (is_ppc64_elf (input_bfd));
13452
13453   local_got_ents = elf_local_got_ents (input_bfd);
13454   TOCstart = elf_gp (output_bfd);
13455   symtab_hdr = &elf_symtab_hdr (input_bfd);
13456   sym_hashes = elf_sym_hashes (input_bfd);
13457   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13458
13459   rel = wrel = relocs;
13460   relend = relocs + input_section->reloc_count;
13461   for (; rel < relend; wrel++, rel++)
13462     {
13463       enum elf_ppc64_reloc_type r_type;
13464       bfd_vma addend;
13465       bfd_reloc_status_type r;
13466       Elf_Internal_Sym *sym;
13467       asection *sec;
13468       struct elf_link_hash_entry *h_elf;
13469       struct ppc_link_hash_entry *h;
13470       struct ppc_link_hash_entry *fdh;
13471       const char *sym_name;
13472       unsigned long r_symndx, toc_symndx;
13473       bfd_vma toc_addend;
13474       unsigned char tls_mask, tls_gd, tls_type;
13475       unsigned char sym_type;
13476       bfd_vma relocation;
13477       bfd_boolean unresolved_reloc;
13478       bfd_boolean warned;
13479       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13480       unsigned int insn;
13481       unsigned int mask;
13482       struct ppc_stub_hash_entry *stub_entry;
13483       bfd_vma max_br_offset;
13484       bfd_vma from;
13485       Elf_Internal_Rela orig_rel;
13486       reloc_howto_type *howto;
13487       struct reloc_howto_struct alt_howto;
13488
13489     again:
13490       orig_rel = *rel;
13491
13492       r_type = ELF64_R_TYPE (rel->r_info);
13493       r_symndx = ELF64_R_SYM (rel->r_info);
13494
13495       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13496          symbol of the previous ADDR64 reloc.  The symbol gives us the
13497          proper TOC base to use.  */
13498       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13499           && wrel != relocs
13500           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13501           && is_opd)
13502         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13503
13504       sym = NULL;
13505       sec = NULL;
13506       h_elf = NULL;
13507       sym_name = NULL;
13508       unresolved_reloc = FALSE;
13509       warned = FALSE;
13510
13511       if (r_symndx < symtab_hdr->sh_info)
13512         {
13513           /* It's a local symbol.  */
13514           struct _opd_sec_data *opd;
13515
13516           sym = local_syms + r_symndx;
13517           sec = local_sections[r_symndx];
13518           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13519           sym_type = ELF64_ST_TYPE (sym->st_info);
13520           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13521           opd = get_opd_info (sec);
13522           if (opd != NULL && opd->adjust != NULL)
13523             {
13524               long adjust = opd->adjust[OPD_NDX (sym->st_value
13525                                                  + rel->r_addend)];
13526               if (adjust == -1)
13527                 relocation = 0;
13528               else
13529                 {
13530                   /* If this is a relocation against the opd section sym
13531                      and we have edited .opd, adjust the reloc addend so
13532                      that ld -r and ld --emit-relocs output is correct.
13533                      If it is a reloc against some other .opd symbol,
13534                      then the symbol value will be adjusted later.  */
13535                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13536                     rel->r_addend += adjust;
13537                   else
13538                     relocation += adjust;
13539                 }
13540             }
13541         }
13542       else
13543         {
13544           bfd_boolean ignored;
13545
13546           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13547                                    r_symndx, symtab_hdr, sym_hashes,
13548                                    h_elf, sec, relocation,
13549                                    unresolved_reloc, warned, ignored);
13550           sym_name = h_elf->root.root.string;
13551           sym_type = h_elf->type;
13552           if (sec != NULL
13553               && sec->owner == output_bfd
13554               && strcmp (sec->name, ".opd") == 0)
13555             {
13556               /* This is a symbol defined in a linker script.  All
13557                  such are defined in output sections, even those
13558                  defined by simple assignment from a symbol defined in
13559                  an input section.  Transfer the symbol to an
13560                  appropriate input .opd section, so that a branch to
13561                  this symbol will be mapped to the location specified
13562                  by the opd entry.  */
13563               struct bfd_link_order *lo;
13564               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13565                 if (lo->type == bfd_indirect_link_order)
13566                   {
13567                     asection *isec = lo->u.indirect.section;
13568                     if (h_elf->root.u.def.value >= isec->output_offset
13569                         && h_elf->root.u.def.value < (isec->output_offset
13570                                                       + isec->size))
13571                       {
13572                         h_elf->root.u.def.value -= isec->output_offset;
13573                         h_elf->root.u.def.section = isec;
13574                         sec = isec;
13575                         break;
13576                       }
13577                   }
13578             }
13579         }
13580       h = (struct ppc_link_hash_entry *) h_elf;
13581
13582       if (sec != NULL && discarded_section (sec))
13583         {
13584           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13585                                input_bfd, input_section,
13586                                contents + rel->r_offset);
13587           wrel->r_offset = rel->r_offset;
13588           wrel->r_info = 0;
13589           wrel->r_addend = 0;
13590
13591           /* For ld -r, remove relocations in debug sections against
13592              symbols defined in discarded sections.  Not done for
13593              non-debug to preserve relocs in .eh_frame which the
13594              eh_frame editing code expects to be present.  */
13595           if (bfd_link_relocatable (info)
13596               && (input_section->flags & SEC_DEBUGGING))
13597             wrel--;
13598
13599           continue;
13600         }
13601
13602       if (bfd_link_relocatable (info))
13603         goto copy_reloc;
13604
13605       if (h != NULL && &h->elf == htab->elf.hgot)
13606         {
13607           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13608           sec = bfd_abs_section_ptr;
13609           unresolved_reloc = FALSE;
13610         }
13611
13612       /* TLS optimizations.  Replace instruction sequences and relocs
13613          based on information we collected in tls_optimize.  We edit
13614          RELOCS so that --emit-relocs will output something sensible
13615          for the final instruction stream.  */
13616       tls_mask = 0;
13617       tls_gd = 0;
13618       toc_symndx = 0;
13619       if (h != NULL)
13620         tls_mask = h->tls_mask;
13621       else if (local_got_ents != NULL)
13622         {
13623           struct plt_entry **local_plt = (struct plt_entry **)
13624             (local_got_ents + symtab_hdr->sh_info);
13625           unsigned char *lgot_masks = (unsigned char *)
13626             (local_plt + symtab_hdr->sh_info);
13627           tls_mask = lgot_masks[r_symndx];
13628         }
13629       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
13630           && (r_type == R_PPC64_TLS
13631               || r_type == R_PPC64_TLSGD
13632               || r_type == R_PPC64_TLSLD))
13633         {
13634           /* Check for toc tls entries.  */
13635           unsigned char *toc_tls;
13636
13637           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13638                              &local_syms, rel, input_bfd))
13639             return FALSE;
13640
13641           if (toc_tls)
13642             tls_mask = *toc_tls;
13643         }
13644
13645       /* Check that tls relocs are used with tls syms, and non-tls
13646          relocs are used with non-tls syms.  */
13647       if (r_symndx != STN_UNDEF
13648           && r_type != R_PPC64_NONE
13649           && (h == NULL
13650               || h->elf.root.type == bfd_link_hash_defined
13651               || h->elf.root.type == bfd_link_hash_defweak)
13652           && (IS_PPC64_TLS_RELOC (r_type)
13653               != (sym_type == STT_TLS
13654                   || (sym_type == STT_SECTION
13655                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13656         {
13657           if ((tls_mask & TLS_TLS) != 0
13658               && (r_type == R_PPC64_TLS
13659                   || r_type == R_PPC64_TLSGD
13660                   || r_type == R_PPC64_TLSLD))
13661             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13662             ;
13663           else
13664             info->callbacks->einfo
13665               (!IS_PPC64_TLS_RELOC (r_type)
13666                /* xgettext:c-format */
13667                ? _("%H: %s used with TLS symbol `%pT'\n")
13668                /* xgettext:c-format */
13669                : _("%H: %s used with non-TLS symbol `%pT'\n"),
13670                input_bfd, input_section, rel->r_offset,
13671                ppc64_elf_howto_table[r_type]->name,
13672                sym_name);
13673         }
13674
13675       /* Ensure reloc mapping code below stays sane.  */
13676       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13677           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13678           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13679           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13680           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13681           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13682           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13683           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13684           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13685           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13686         abort ();
13687
13688       switch (r_type)
13689         {
13690         default:
13691           break;
13692
13693         case R_PPC64_LO_DS_OPT:
13694           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13695           if ((insn & (0x3f << 26)) != 58u << 26)
13696             abort ();
13697           insn += (14u << 26) - (58u << 26);
13698           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13699           r_type = R_PPC64_TOC16_LO;
13700           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13701           break;
13702
13703         case R_PPC64_TOC16:
13704         case R_PPC64_TOC16_LO:
13705         case R_PPC64_TOC16_DS:
13706         case R_PPC64_TOC16_LO_DS:
13707           {
13708             /* Check for toc tls entries.  */
13709             unsigned char *toc_tls;
13710             int retval;
13711
13712             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13713                                    &local_syms, rel, input_bfd);
13714             if (retval == 0)
13715               return FALSE;
13716
13717             if (toc_tls)
13718               {
13719                 tls_mask = *toc_tls;
13720                 if (r_type == R_PPC64_TOC16_DS
13721                     || r_type == R_PPC64_TOC16_LO_DS)
13722                   {
13723                     if ((tls_mask & TLS_TLS) != 0
13724                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13725                       goto toctprel;
13726                   }
13727                 else
13728                   {
13729                     /* If we found a GD reloc pair, then we might be
13730                        doing a GD->IE transition.  */
13731                     if (retval == 2)
13732                       {
13733                         tls_gd = TLS_TPRELGD;
13734                         if ((tls_mask & TLS_TLS) != 0
13735                             && (tls_mask & TLS_GD) == 0)
13736                           goto tls_ldgd_opt;
13737                       }
13738                     else if (retval == 3)
13739                       {
13740                         if ((tls_mask & TLS_TLS) != 0
13741                             && (tls_mask & TLS_LD) == 0)
13742                           goto tls_ldgd_opt;
13743                       }
13744                   }
13745               }
13746           }
13747           break;
13748
13749         case R_PPC64_GOT_TPREL16_HI:
13750         case R_PPC64_GOT_TPREL16_HA:
13751           if ((tls_mask & TLS_TLS) != 0
13752               && (tls_mask & TLS_TPREL) == 0)
13753             {
13754               rel->r_offset -= d_offset;
13755               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13756               r_type = R_PPC64_NONE;
13757               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13758             }
13759           break;
13760
13761         case R_PPC64_GOT_TPREL16_DS:
13762         case R_PPC64_GOT_TPREL16_LO_DS:
13763           if ((tls_mask & TLS_TLS) != 0
13764               && (tls_mask & TLS_TPREL) == 0)
13765             {
13766             toctprel:
13767               insn = bfd_get_32 (input_bfd,
13768                                  contents + rel->r_offset - d_offset);
13769               insn &= 31 << 21;
13770               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13771               bfd_put_32 (input_bfd, insn,
13772                           contents + rel->r_offset - d_offset);
13773               r_type = R_PPC64_TPREL16_HA;
13774               if (toc_symndx != 0)
13775                 {
13776                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13777                   rel->r_addend = toc_addend;
13778                   /* We changed the symbol.  Start over in order to
13779                      get h, sym, sec etc. right.  */
13780                   goto again;
13781                 }
13782               else
13783                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13784             }
13785           break;
13786
13787         case R_PPC64_TLS:
13788           if ((tls_mask & TLS_TLS) != 0
13789               && (tls_mask & TLS_TPREL) == 0)
13790             {
13791               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13792               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13793               if (insn == 0)
13794                 abort ();
13795               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13796               /* Was PPC64_TLS which sits on insn boundary, now
13797                  PPC64_TPREL16_LO which is at low-order half-word.  */
13798               rel->r_offset += d_offset;
13799               r_type = R_PPC64_TPREL16_LO;
13800               if (toc_symndx != 0)
13801                 {
13802                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13803                   rel->r_addend = toc_addend;
13804                   /* We changed the symbol.  Start over in order to
13805                      get h, sym, sec etc. right.  */
13806                   goto again;
13807                 }
13808               else
13809                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13810             }
13811           break;
13812
13813         case R_PPC64_GOT_TLSGD16_HI:
13814         case R_PPC64_GOT_TLSGD16_HA:
13815           tls_gd = TLS_TPRELGD;
13816           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13817             goto tls_gdld_hi;
13818           break;
13819
13820         case R_PPC64_GOT_TLSLD16_HI:
13821         case R_PPC64_GOT_TLSLD16_HA:
13822           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13823             {
13824             tls_gdld_hi:
13825               if ((tls_mask & tls_gd) != 0)
13826                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13827                           + R_PPC64_GOT_TPREL16_DS);
13828               else
13829                 {
13830                   rel->r_offset -= d_offset;
13831                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13832                   r_type = R_PPC64_NONE;
13833                 }
13834               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13835             }
13836           break;
13837
13838         case R_PPC64_GOT_TLSGD16:
13839         case R_PPC64_GOT_TLSGD16_LO:
13840           tls_gd = TLS_TPRELGD;
13841           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
13842             goto tls_ldgd_opt;
13843           break;
13844
13845         case R_PPC64_GOT_TLSLD16:
13846         case R_PPC64_GOT_TLSLD16_LO:
13847           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
13848             {
13849               unsigned int insn1, insn2;
13850               bfd_vma offset;
13851
13852             tls_ldgd_opt:
13853               offset = (bfd_vma) -1;
13854               /* If not using the newer R_PPC64_TLSGD/LD to mark
13855                  __tls_get_addr calls, we must trust that the call
13856                  stays with its arg setup insns, ie. that the next
13857                  reloc is the __tls_get_addr call associated with
13858                  the current reloc.  Edit both insns.  */
13859               if (input_section->has_tls_get_addr_call
13860                   && rel + 1 < relend
13861                   && branch_reloc_hash_match (input_bfd, rel + 1,
13862                                               htab->tls_get_addr,
13863                                               htab->tls_get_addr_fd))
13864                 offset = rel[1].r_offset;
13865               /* We read the low GOT_TLS (or TOC16) insn because we
13866                  need to keep the destination reg.  It may be
13867                  something other than the usual r3, and moved to r3
13868                  before the call by intervening code.  */
13869               insn1 = bfd_get_32 (input_bfd,
13870                                   contents + rel->r_offset - d_offset);
13871               if ((tls_mask & tls_gd) != 0)
13872                 {
13873                   /* IE */
13874                   insn1 &= (0x1f << 21) | (0x1f << 16);
13875                   insn1 |= 58 << 26;    /* ld */
13876                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13877                   if (offset != (bfd_vma) -1)
13878                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13879                   if ((tls_mask & TLS_EXPLICIT) == 0)
13880                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13881                               + R_PPC64_GOT_TPREL16_DS);
13882                   else
13883                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13884                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13885                 }
13886               else
13887                 {
13888                   /* LE */
13889                   insn1 &= 0x1f << 21;
13890                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13891                   insn2 = 0x38630000;   /* addi 3,3,0 */
13892                   if (tls_gd == 0)
13893                     {
13894                       /* Was an LD reloc.  */
13895                       if (toc_symndx)
13896                         sec = local_sections[toc_symndx];
13897                       for (r_symndx = 0;
13898                            r_symndx < symtab_hdr->sh_info;
13899                            r_symndx++)
13900                         if (local_sections[r_symndx] == sec)
13901                           break;
13902                       if (r_symndx >= symtab_hdr->sh_info)
13903                         r_symndx = STN_UNDEF;
13904                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13905                       if (r_symndx != STN_UNDEF)
13906                         rel->r_addend -= (local_syms[r_symndx].st_value
13907                                           + sec->output_offset
13908                                           + sec->output_section->vma);
13909                     }
13910                   else if (toc_symndx != 0)
13911                     {
13912                       r_symndx = toc_symndx;
13913                       rel->r_addend = toc_addend;
13914                     }
13915                   r_type = R_PPC64_TPREL16_HA;
13916                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13917                   if (offset != (bfd_vma) -1)
13918                     {
13919                       rel[1].r_info = ELF64_R_INFO (r_symndx,
13920                                                     R_PPC64_TPREL16_LO);
13921                       rel[1].r_offset = offset + d_offset;
13922                       rel[1].r_addend = rel->r_addend;
13923                     }
13924                 }
13925               bfd_put_32 (input_bfd, insn1,
13926                           contents + rel->r_offset - d_offset);
13927               if (offset != (bfd_vma) -1)
13928                 bfd_put_32 (input_bfd, insn2, contents + offset);
13929               if ((tls_mask & tls_gd) == 0
13930                   && (tls_gd == 0 || toc_symndx != 0))
13931                 {
13932                   /* We changed the symbol.  Start over in order
13933                      to get h, sym, sec etc. right.  */
13934                   goto again;
13935                 }
13936             }
13937           break;
13938
13939         case R_PPC64_TLSGD:
13940           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
13941               && rel + 1 < relend)
13942             {
13943               unsigned int insn2;
13944               bfd_vma offset = rel->r_offset;
13945
13946               if ((tls_mask & TLS_TPRELGD) != 0)
13947                 {
13948                   /* IE */
13949                   r_type = R_PPC64_NONE;
13950                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13951                 }
13952               else
13953                 {
13954                   /* LE */
13955                   if (toc_symndx != 0)
13956                     {
13957                       r_symndx = toc_symndx;
13958                       rel->r_addend = toc_addend;
13959                     }
13960                   r_type = R_PPC64_TPREL16_LO;
13961                   rel->r_offset = offset + d_offset;
13962                   insn2 = 0x38630000;   /* addi 3,3,0 */
13963                 }
13964               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13965               /* Zap the reloc on the _tls_get_addr call too.  */
13966               BFD_ASSERT (offset == rel[1].r_offset);
13967               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13968               bfd_put_32 (input_bfd, insn2, contents + offset);
13969               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13970                 goto again;
13971             }
13972           break;
13973
13974         case R_PPC64_TLSLD:
13975           if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
13976               && rel + 1 < relend)
13977             {
13978               unsigned int insn2;
13979               bfd_vma offset = rel->r_offset;
13980
13981               if (toc_symndx)
13982                 sec = local_sections[toc_symndx];
13983               for (r_symndx = 0;
13984                    r_symndx < symtab_hdr->sh_info;
13985                    r_symndx++)
13986                 if (local_sections[r_symndx] == sec)
13987                   break;
13988               if (r_symndx >= symtab_hdr->sh_info)
13989                 r_symndx = STN_UNDEF;
13990               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13991               if (r_symndx != STN_UNDEF)
13992                 rel->r_addend -= (local_syms[r_symndx].st_value
13993                                   + sec->output_offset
13994                                   + sec->output_section->vma);
13995
13996               r_type = R_PPC64_TPREL16_LO;
13997               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13998               rel->r_offset = offset + d_offset;
13999               /* Zap the reloc on the _tls_get_addr call too.  */
14000               BFD_ASSERT (offset == rel[1].r_offset);
14001               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14002               insn2 = 0x38630000;       /* addi 3,3,0 */
14003               bfd_put_32 (input_bfd, insn2, contents + offset);
14004               goto again;
14005             }
14006           break;
14007
14008         case R_PPC64_DTPMOD64:
14009           if (rel + 1 < relend
14010               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14011               && rel[1].r_offset == rel->r_offset + 8)
14012             {
14013               if ((tls_mask & TLS_GD) == 0)
14014                 {
14015                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14016                   if ((tls_mask & TLS_TPRELGD) != 0)
14017                     r_type = R_PPC64_TPREL64;
14018                   else
14019                     {
14020                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14021                       r_type = R_PPC64_NONE;
14022                     }
14023                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14024                 }
14025             }
14026           else
14027             {
14028               if ((tls_mask & TLS_LD) == 0)
14029                 {
14030                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14031                   r_type = R_PPC64_NONE;
14032                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14033                 }
14034             }
14035           break;
14036
14037         case R_PPC64_TPREL64:
14038           if ((tls_mask & TLS_TPREL) == 0)
14039             {
14040               r_type = R_PPC64_NONE;
14041               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14042             }
14043           break;
14044
14045         case R_PPC64_ENTRY:
14046           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14047           if (!bfd_link_pic (info)
14048               && !info->traditional_format
14049               && relocation + 0x80008000 <= 0xffffffff)
14050             {
14051               unsigned int insn1, insn2;
14052
14053               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14054               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14055               if ((insn1 & ~0xfffc) == LD_R2_0R12
14056                   && insn2 == ADD_R2_R2_R12)
14057                 {
14058                   bfd_put_32 (input_bfd,
14059                               LIS_R2 + PPC_HA (relocation),
14060                               contents + rel->r_offset);
14061                   bfd_put_32 (input_bfd,
14062                               ADDI_R2_R2 + PPC_LO (relocation),
14063                               contents + rel->r_offset + 4);
14064                 }
14065             }
14066           else
14067             {
14068               relocation -= (rel->r_offset
14069                              + input_section->output_offset
14070                              + input_section->output_section->vma);
14071               if (relocation + 0x80008000 <= 0xffffffff)
14072                 {
14073                   unsigned int insn1, insn2;
14074
14075                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14076                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14077                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14078                       && insn2 == ADD_R2_R2_R12)
14079                     {
14080                       bfd_put_32 (input_bfd,
14081                                   ADDIS_R2_R12 + PPC_HA (relocation),
14082                                   contents + rel->r_offset);
14083                       bfd_put_32 (input_bfd,
14084                                   ADDI_R2_R2 + PPC_LO (relocation),
14085                                   contents + rel->r_offset + 4);
14086                     }
14087                 }
14088             }
14089           break;
14090
14091         case R_PPC64_REL16_HA:
14092           /* If we are generating a non-PIC executable, edit
14093              .  0:      addis 2,12,.TOC.-0b@ha
14094              .          addi 2,2,.TOC.-0b@l
14095              used by ELFv2 global entry points to set up r2, to
14096              .          lis 2,.TOC.@ha
14097              .          addi 2,2,.TOC.@l
14098              if .TOC. is in range.  */
14099           if (!bfd_link_pic (info)
14100               && !info->traditional_format
14101               && !htab->opd_abi
14102               && rel->r_addend == d_offset
14103               && h != NULL && &h->elf == htab->elf.hgot
14104               && rel + 1 < relend
14105               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14106               && rel[1].r_offset == rel->r_offset + 4
14107               && rel[1].r_addend == rel->r_addend + 4
14108               && relocation + 0x80008000 <= 0xffffffff)
14109             {
14110               unsigned int insn1, insn2;
14111               bfd_vma offset = rel->r_offset - d_offset;
14112               insn1 = bfd_get_32 (input_bfd, contents + offset);
14113               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14114               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14115                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14116                 {
14117                   r_type = R_PPC64_ADDR16_HA;
14118                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14119                   rel->r_addend -= d_offset;
14120                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14121                   rel[1].r_addend -= d_offset + 4;
14122                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14123                 }
14124             }
14125           break;
14126         }
14127
14128       /* Handle other relocations that tweak non-addend part of insn.  */
14129       insn = 0;
14130       max_br_offset = 1 << 25;
14131       addend = rel->r_addend;
14132       reloc_dest = DEST_NORMAL;
14133       switch (r_type)
14134         {
14135         default:
14136           break;
14137
14138         case R_PPC64_TOCSAVE:
14139           if (relocation + addend == (rel->r_offset
14140                                       + input_section->output_offset
14141                                       + input_section->output_section->vma)
14142               && tocsave_find (htab, NO_INSERT,
14143                                &local_syms, rel, input_bfd))
14144             {
14145               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14146               if (insn == NOP
14147                   || insn == CROR_151515 || insn == CROR_313131)
14148                 bfd_put_32 (input_bfd,
14149                             STD_R2_0R1 + STK_TOC (htab),
14150                             contents + rel->r_offset);
14151             }
14152           break;
14153
14154           /* Branch taken prediction relocations.  */
14155         case R_PPC64_ADDR14_BRTAKEN:
14156         case R_PPC64_REL14_BRTAKEN:
14157           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14158           /* Fall through.  */
14159
14160           /* Branch not taken prediction relocations.  */
14161         case R_PPC64_ADDR14_BRNTAKEN:
14162         case R_PPC64_REL14_BRNTAKEN:
14163           insn |= bfd_get_32 (input_bfd,
14164                               contents + rel->r_offset) & ~(0x01 << 21);
14165           /* Fall through.  */
14166
14167         case R_PPC64_REL14:
14168           max_br_offset = 1 << 15;
14169           /* Fall through.  */
14170
14171         case R_PPC64_REL24:
14172           /* Calls to functions with a different TOC, such as calls to
14173              shared objects, need to alter the TOC pointer.  This is
14174              done using a linkage stub.  A REL24 branching to these
14175              linkage stubs needs to be followed by a nop, as the nop
14176              will be replaced with an instruction to restore the TOC
14177              base pointer.  */
14178           fdh = h;
14179           if (h != NULL
14180               && h->oh != NULL
14181               && h->oh->is_func_descriptor)
14182             fdh = ppc_follow_link (h->oh);
14183           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14184                                            htab);
14185           if (stub_entry != NULL
14186               && (stub_entry->stub_type == ppc_stub_plt_call
14187                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14188                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14189                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14190             {
14191               bfd_boolean can_plt_call = FALSE;
14192
14193               if (stub_entry->stub_type == ppc_stub_plt_call
14194                   && !htab->opd_abi
14195                   && htab->params->plt_localentry0 != 0
14196                   && is_elfv2_localentry0 (&h->elf))
14197                 {
14198                   /* The function doesn't use or change r2.  */
14199                   can_plt_call = TRUE;
14200                 }
14201
14202               /* All of these stubs may modify r2, so there must be a
14203                  branch and link followed by a nop.  The nop is
14204                  replaced by an insn to restore r2.  */
14205               else if (rel->r_offset + 8 <= input_section->size)
14206                 {
14207                   unsigned long br;
14208
14209                   br = bfd_get_32 (input_bfd,
14210                                    contents + rel->r_offset);
14211                   if ((br & 1) != 0)
14212                     {
14213                       unsigned long nop;
14214
14215                       nop = bfd_get_32 (input_bfd,
14216                                         contents + rel->r_offset + 4);
14217                       if (nop == NOP
14218                           || nop == CROR_151515 || nop == CROR_313131)
14219                         {
14220                           if (h != NULL
14221                               && (h == htab->tls_get_addr_fd
14222                                   || h == htab->tls_get_addr)
14223                               && htab->params->tls_get_addr_opt)
14224                             {
14225                               /* Special stub used, leave nop alone.  */
14226                             }
14227                           else
14228                             bfd_put_32 (input_bfd,
14229                                         LD_R2_0R1 + STK_TOC (htab),
14230                                         contents + rel->r_offset + 4);
14231                           can_plt_call = TRUE;
14232                         }
14233                     }
14234                 }
14235
14236               if (!can_plt_call && h != NULL)
14237                 {
14238                   const char *name = h->elf.root.root.string;
14239
14240                   if (*name == '.')
14241                     ++name;
14242
14243                   if (strncmp (name, "__libc_start_main", 17) == 0
14244                       && (name[17] == 0 || name[17] == '@'))
14245                     {
14246                       /* Allow crt1 branch to go via a toc adjusting
14247                          stub.  Other calls that never return could do
14248                          the same, if we could detect such.  */
14249                       can_plt_call = TRUE;
14250                     }
14251                 }
14252
14253               if (!can_plt_call)
14254                 {
14255                   /* g++ as of 20130507 emits self-calls without a
14256                      following nop.  This is arguably wrong since we
14257                      have conflicting information.  On the one hand a
14258                      global symbol and on the other a local call
14259                      sequence, but don't error for this special case.
14260                      It isn't possible to cheaply verify we have
14261                      exactly such a call.  Allow all calls to the same
14262                      section.  */
14263                   asection *code_sec = sec;
14264
14265                   if (get_opd_info (sec) != NULL)
14266                     {
14267                       bfd_vma off = (relocation + addend
14268                                      - sec->output_section->vma
14269                                      - sec->output_offset);
14270
14271                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14272                     }
14273                   if (code_sec == input_section)
14274                     can_plt_call = TRUE;
14275                 }
14276
14277               if (!can_plt_call)
14278                 {
14279                   if (stub_entry->stub_type == ppc_stub_plt_call
14280                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14281                     info->callbacks->einfo
14282                       /* xgettext:c-format */
14283                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14284                          "recompile with -fPIC\n"),
14285                        input_bfd, input_section, rel->r_offset, sym_name);
14286                   else
14287                     info->callbacks->einfo
14288                       /* xgettext:c-format */
14289                       (_("%H: call to `%pT' lacks nop, can't restore toc; "
14290                          "(-mcmodel=small toc adjust stub)\n"),
14291                        input_bfd, input_section, rel->r_offset, sym_name);
14292
14293                   bfd_set_error (bfd_error_bad_value);
14294                   ret = FALSE;
14295                 }
14296
14297               if (can_plt_call
14298                   && (stub_entry->stub_type == ppc_stub_plt_call
14299                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14300                 unresolved_reloc = FALSE;
14301             }
14302
14303           if ((stub_entry == NULL
14304                || stub_entry->stub_type == ppc_stub_long_branch
14305                || stub_entry->stub_type == ppc_stub_plt_branch)
14306               && get_opd_info (sec) != NULL)
14307             {
14308               /* The branch destination is the value of the opd entry. */
14309               bfd_vma off = (relocation + addend
14310                              - sec->output_section->vma
14311                              - sec->output_offset);
14312               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14313               if (dest != (bfd_vma) -1)
14314                 {
14315                   relocation = dest;
14316                   addend = 0;
14317                   reloc_dest = DEST_OPD;
14318                 }
14319             }
14320
14321           /* If the branch is out of reach we ought to have a long
14322              branch stub.  */
14323           from = (rel->r_offset
14324                   + input_section->output_offset
14325                   + input_section->output_section->vma);
14326
14327           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14328                                                   ? fdh->elf.other
14329                                                   : sym->st_other);
14330
14331           if (stub_entry != NULL
14332               && (stub_entry->stub_type == ppc_stub_long_branch
14333                   || stub_entry->stub_type == ppc_stub_plt_branch)
14334               && (r_type == R_PPC64_ADDR14_BRTAKEN
14335                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14336                   || (relocation + addend - from + max_br_offset
14337                       < 2 * max_br_offset)))
14338             /* Don't use the stub if this branch is in range.  */
14339             stub_entry = NULL;
14340
14341           if (stub_entry != NULL)
14342             {
14343               /* Munge up the value and addend so that we call the stub
14344                  rather than the procedure directly.  */
14345               asection *stub_sec = stub_entry->group->stub_sec;
14346
14347               if (stub_entry->stub_type == ppc_stub_save_res)
14348                 relocation += (stub_sec->output_offset
14349                                + stub_sec->output_section->vma
14350                                + stub_sec->size - htab->sfpr->size
14351                                - htab->sfpr->output_offset
14352                                - htab->sfpr->output_section->vma);
14353               else
14354                 relocation = (stub_entry->stub_offset
14355                               + stub_sec->output_offset
14356                               + stub_sec->output_section->vma);
14357               addend = 0;
14358               reloc_dest = DEST_STUB;
14359
14360               if ((stub_entry->stub_type == ppc_stub_plt_call
14361                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14362                   && (ALWAYS_EMIT_R2SAVE
14363                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14364                   && rel + 1 < relend
14365                   && rel[1].r_offset == rel->r_offset + 4
14366                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14367                 relocation += 4;
14368             }
14369
14370           if (insn != 0)
14371             {
14372               if (is_isa_v2)
14373                 {
14374                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14375                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14376                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14377                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14378                     insn |= 0x02 << 21;
14379                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14380                     insn |= 0x08 << 21;
14381                   else
14382                     break;
14383                 }
14384               else
14385                 {
14386                   /* Invert 'y' bit if not the default.  */
14387                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14388                     insn ^= 0x01 << 21;
14389                 }
14390
14391               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14392             }
14393
14394           /* NOP out calls to undefined weak functions.
14395              We can thus call a weak function without first
14396              checking whether the function is defined.  */
14397           else if (h != NULL
14398                    && h->elf.root.type == bfd_link_hash_undefweak
14399                    && h->elf.dynindx == -1
14400                    && r_type == R_PPC64_REL24
14401                    && relocation == 0
14402                    && addend == 0)
14403             {
14404               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14405               goto copy_reloc;
14406             }
14407           break;
14408         }
14409
14410       /* Set `addend'.  */
14411       tls_type = 0;
14412       switch (r_type)
14413         {
14414         default:
14415           /* xgettext:c-format */
14416           _bfd_error_handler (_("%pB: %s unsupported"),
14417                               input_bfd, ppc64_elf_howto_table[r_type]->name);
14418
14419           bfd_set_error (bfd_error_bad_value);
14420           ret = FALSE;
14421           goto copy_reloc;
14422
14423         case R_PPC64_NONE:
14424         case R_PPC64_TLS:
14425         case R_PPC64_TLSGD:
14426         case R_PPC64_TLSLD:
14427         case R_PPC64_TOCSAVE:
14428         case R_PPC64_GNU_VTINHERIT:
14429         case R_PPC64_GNU_VTENTRY:
14430         case R_PPC64_ENTRY:
14431           goto copy_reloc;
14432
14433           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14434              address in the GOT as relocation value instead of the
14435              symbol's value itself.  Also, create a GOT entry for the
14436              symbol and put the symbol value there.  */
14437         case R_PPC64_GOT_TLSGD16:
14438         case R_PPC64_GOT_TLSGD16_LO:
14439         case R_PPC64_GOT_TLSGD16_HI:
14440         case R_PPC64_GOT_TLSGD16_HA:
14441           tls_type = TLS_TLS | TLS_GD;
14442           goto dogot;
14443
14444         case R_PPC64_GOT_TLSLD16:
14445         case R_PPC64_GOT_TLSLD16_LO:
14446         case R_PPC64_GOT_TLSLD16_HI:
14447         case R_PPC64_GOT_TLSLD16_HA:
14448           tls_type = TLS_TLS | TLS_LD;
14449           goto dogot;
14450
14451         case R_PPC64_GOT_TPREL16_DS:
14452         case R_PPC64_GOT_TPREL16_LO_DS:
14453         case R_PPC64_GOT_TPREL16_HI:
14454         case R_PPC64_GOT_TPREL16_HA:
14455           tls_type = TLS_TLS | TLS_TPREL;
14456           goto dogot;
14457
14458         case R_PPC64_GOT_DTPREL16_DS:
14459         case R_PPC64_GOT_DTPREL16_LO_DS:
14460         case R_PPC64_GOT_DTPREL16_HI:
14461         case R_PPC64_GOT_DTPREL16_HA:
14462           tls_type = TLS_TLS | TLS_DTPREL;
14463           goto dogot;
14464
14465         case R_PPC64_GOT16:
14466         case R_PPC64_GOT16_LO:
14467         case R_PPC64_GOT16_HI:
14468         case R_PPC64_GOT16_HA:
14469         case R_PPC64_GOT16_DS:
14470         case R_PPC64_GOT16_LO_DS:
14471         dogot:
14472           {
14473             /* Relocation is to the entry for this symbol in the global
14474                offset table.  */
14475             asection *got;
14476             bfd_vma *offp;
14477             bfd_vma off;
14478             unsigned long indx = 0;
14479             struct got_entry *ent;
14480
14481             if (tls_type == (TLS_TLS | TLS_LD)
14482                 && (h == NULL
14483                     || !h->elf.def_dynamic))
14484               ent = ppc64_tlsld_got (input_bfd);
14485             else
14486               {
14487                 if (h != NULL)
14488                   {
14489                     if (!htab->elf.dynamic_sections_created
14490                         || h->elf.dynindx == -1
14491                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14492                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14493                       /* This is actually a static link, or it is a
14494                          -Bsymbolic link and the symbol is defined
14495                          locally, or the symbol was forced to be local
14496                          because of a version file.  */
14497                       ;
14498                     else
14499                       {
14500                         indx = h->elf.dynindx;
14501                         unresolved_reloc = FALSE;
14502                       }
14503                     ent = h->elf.got.glist;
14504                   }
14505                 else
14506                   {
14507                     if (local_got_ents == NULL)
14508                       abort ();
14509                     ent = local_got_ents[r_symndx];
14510                   }
14511
14512                 for (; ent != NULL; ent = ent->next)
14513                   if (ent->addend == orig_rel.r_addend
14514                       && ent->owner == input_bfd
14515                       && ent->tls_type == tls_type)
14516                     break;
14517               }
14518
14519             if (ent == NULL)
14520               abort ();
14521             if (ent->is_indirect)
14522               ent = ent->got.ent;
14523             offp = &ent->got.offset;
14524             got = ppc64_elf_tdata (ent->owner)->got;
14525             if (got == NULL)
14526               abort ();
14527
14528             /* The offset must always be a multiple of 8.  We use the
14529                least significant bit to record whether we have already
14530                processed this entry.  */
14531             off = *offp;
14532             if ((off & 1) != 0)
14533               off &= ~1;
14534             else
14535               {
14536                 /* Generate relocs for the dynamic linker, except in
14537                    the case of TLSLD where we'll use one entry per
14538                    module.  */
14539                 asection *relgot;
14540                 bfd_boolean ifunc;
14541
14542                 *offp = off | 1;
14543                 relgot = NULL;
14544                 ifunc = (h != NULL
14545                          ? h->elf.type == STT_GNU_IFUNC
14546                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14547                 if (ifunc)
14548                   {
14549                     relgot = htab->elf.irelplt;
14550                     if (indx == 0)
14551                       htab->local_ifunc_resolver = 1;
14552                     else if (is_static_defined (&h->elf))
14553                       htab->maybe_local_ifunc_resolver = 1;
14554                   }
14555                 else if (indx != 0
14556                          || (bfd_link_pic (info)
14557                              && (h == NULL
14558                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14559                                  || (tls_type == (TLS_TLS | TLS_LD)
14560                                      && !h->elf.def_dynamic))
14561                              && !(tls_type == (TLS_TLS | TLS_TPREL)
14562                                   && bfd_link_executable (info)
14563                                   && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14564                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14565                 if (relgot != NULL)
14566                   {
14567                     outrel.r_offset = (got->output_section->vma
14568                                        + got->output_offset
14569                                        + off);
14570                     outrel.r_addend = addend;
14571                     if (tls_type & (TLS_LD | TLS_GD))
14572                       {
14573                         outrel.r_addend = 0;
14574                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14575                         if (tls_type == (TLS_TLS | TLS_GD))
14576                           {
14577                             loc = relgot->contents;
14578                             loc += (relgot->reloc_count++
14579                                     * sizeof (Elf64_External_Rela));
14580                             bfd_elf64_swap_reloca_out (output_bfd,
14581                                                        &outrel, loc);
14582                             outrel.r_offset += 8;
14583                             outrel.r_addend = addend;
14584                             outrel.r_info
14585                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14586                           }
14587                       }
14588                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14589                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14590                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14591                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14592                     else if (indx != 0)
14593                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14594                     else
14595                       {
14596                         if (ifunc)
14597                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14598                         else
14599                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14600
14601                         /* Write the .got section contents for the sake
14602                            of prelink.  */
14603                         loc = got->contents + off;
14604                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14605                                     loc);
14606                       }
14607
14608                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14609                       {
14610                         outrel.r_addend += relocation;
14611                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14612                           {
14613                             if (htab->elf.tls_sec == NULL)
14614                               outrel.r_addend = 0;
14615                             else
14616                               outrel.r_addend -= htab->elf.tls_sec->vma;
14617                           }
14618                       }
14619                     loc = relgot->contents;
14620                     loc += (relgot->reloc_count++
14621                             * sizeof (Elf64_External_Rela));
14622                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14623                   }
14624
14625                 /* Init the .got section contents here if we're not
14626                    emitting a reloc.  */
14627                 else
14628                   {
14629                     relocation += addend;
14630                     if (tls_type != 0)
14631                       {
14632                         if (htab->elf.tls_sec == NULL)
14633                           relocation = 0;
14634                         else
14635                           {
14636                             if (tls_type & TLS_LD)
14637                               relocation = 0;
14638                             else
14639                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14640                             if (tls_type & TLS_TPREL)
14641                               relocation += DTP_OFFSET - TP_OFFSET;
14642                           }
14643
14644                         if (tls_type & (TLS_GD | TLS_LD))
14645                           {
14646                             bfd_put_64 (output_bfd, relocation,
14647                                         got->contents + off + 8);
14648                             relocation = 1;
14649                           }
14650                       }
14651                     bfd_put_64 (output_bfd, relocation,
14652                                 got->contents + off);
14653                   }
14654               }
14655
14656             if (off >= (bfd_vma) -2)
14657               abort ();
14658
14659             relocation = got->output_section->vma + got->output_offset + off;
14660             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14661           }
14662           break;
14663
14664         case R_PPC64_PLT16_HA:
14665         case R_PPC64_PLT16_HI:
14666         case R_PPC64_PLT16_LO:
14667         case R_PPC64_PLT32:
14668         case R_PPC64_PLT64:
14669           /* Relocation is to the entry for this symbol in the
14670              procedure linkage table.  */
14671           {
14672             struct plt_entry **plt_list = NULL;
14673             if (h != NULL)
14674               plt_list = &h->elf.plt.plist;
14675             else if (local_got_ents != NULL)
14676               {
14677                 struct plt_entry **local_plt = (struct plt_entry **)
14678                   (local_got_ents + symtab_hdr->sh_info);
14679                 unsigned char *local_got_tls_masks = (unsigned char *)
14680                   (local_plt + symtab_hdr->sh_info);
14681                 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14682                   plt_list = local_plt + r_symndx;
14683               }
14684             if (plt_list)
14685               {
14686                 struct plt_entry *ent;
14687
14688                 for (ent = *plt_list; ent != NULL; ent = ent->next)
14689                   if (ent->plt.offset != (bfd_vma) -1
14690                       && ent->addend == orig_rel.r_addend)
14691                     {
14692                       asection *plt;
14693
14694                       plt = htab->elf.splt;
14695                       if (!htab->elf.dynamic_sections_created
14696                           || h == NULL
14697                           || h->elf.dynindx == -1)
14698                         plt = htab->elf.iplt;
14699                       relocation = (plt->output_section->vma
14700                                     + plt->output_offset
14701                                     + ent->plt.offset);
14702                       addend = 0;
14703                       unresolved_reloc = FALSE;
14704                       break;
14705                     }
14706               }
14707           }
14708           break;
14709
14710         case R_PPC64_TOC:
14711           /* Relocation value is TOC base.  */
14712           relocation = TOCstart;
14713           if (r_symndx == STN_UNDEF)
14714             relocation += htab->sec_info[input_section->id].toc_off;
14715           else if (unresolved_reloc)
14716             ;
14717           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14718             relocation += htab->sec_info[sec->id].toc_off;
14719           else
14720             unresolved_reloc = TRUE;
14721           goto dodyn;
14722
14723           /* TOC16 relocs.  We want the offset relative to the TOC base,
14724              which is the address of the start of the TOC plus 0x8000.
14725              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14726              in this order.  */
14727         case R_PPC64_TOC16:
14728         case R_PPC64_TOC16_LO:
14729         case R_PPC64_TOC16_HI:
14730         case R_PPC64_TOC16_DS:
14731         case R_PPC64_TOC16_LO_DS:
14732         case R_PPC64_TOC16_HA:
14733           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14734           break;
14735
14736           /* Relocate against the beginning of the section.  */
14737         case R_PPC64_SECTOFF:
14738         case R_PPC64_SECTOFF_LO:
14739         case R_PPC64_SECTOFF_HI:
14740         case R_PPC64_SECTOFF_DS:
14741         case R_PPC64_SECTOFF_LO_DS:
14742         case R_PPC64_SECTOFF_HA:
14743           if (sec != NULL)
14744             addend -= sec->output_section->vma;
14745           break;
14746
14747         case R_PPC64_REL16:
14748         case R_PPC64_REL16_LO:
14749         case R_PPC64_REL16_HI:
14750         case R_PPC64_REL16_HA:
14751         case R_PPC64_REL16DX_HA:
14752           break;
14753
14754         case R_PPC64_REL14:
14755         case R_PPC64_REL14_BRNTAKEN:
14756         case R_PPC64_REL14_BRTAKEN:
14757         case R_PPC64_REL24:
14758           break;
14759
14760         case R_PPC64_TPREL16:
14761         case R_PPC64_TPREL16_LO:
14762         case R_PPC64_TPREL16_HI:
14763         case R_PPC64_TPREL16_HA:
14764         case R_PPC64_TPREL16_DS:
14765         case R_PPC64_TPREL16_LO_DS:
14766         case R_PPC64_TPREL16_HIGH:
14767         case R_PPC64_TPREL16_HIGHA:
14768         case R_PPC64_TPREL16_HIGHER:
14769         case R_PPC64_TPREL16_HIGHERA:
14770         case R_PPC64_TPREL16_HIGHEST:
14771         case R_PPC64_TPREL16_HIGHESTA:
14772           if (h != NULL
14773               && h->elf.root.type == bfd_link_hash_undefweak
14774               && h->elf.dynindx == -1)
14775             {
14776               /* Make this relocation against an undefined weak symbol
14777                  resolve to zero.  This is really just a tweak, since
14778                  code using weak externs ought to check that they are
14779                  defined before using them.  */
14780               bfd_byte *p = contents + rel->r_offset - d_offset;
14781
14782               insn = bfd_get_32 (input_bfd, p);
14783               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14784               if (insn != 0)
14785                 bfd_put_32 (input_bfd, insn, p);
14786               break;
14787             }
14788           if (htab->elf.tls_sec != NULL)
14789             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14790           /* The TPREL16 relocs shouldn't really be used in shared
14791              libs or with non-local symbols as that will result in
14792              DT_TEXTREL being set, but support them anyway.  */
14793           goto dodyn;
14794
14795         case R_PPC64_DTPREL16:
14796         case R_PPC64_DTPREL16_LO:
14797         case R_PPC64_DTPREL16_HI:
14798         case R_PPC64_DTPREL16_HA:
14799         case R_PPC64_DTPREL16_DS:
14800         case R_PPC64_DTPREL16_LO_DS:
14801         case R_PPC64_DTPREL16_HIGH:
14802         case R_PPC64_DTPREL16_HIGHA:
14803         case R_PPC64_DTPREL16_HIGHER:
14804         case R_PPC64_DTPREL16_HIGHERA:
14805         case R_PPC64_DTPREL16_HIGHEST:
14806         case R_PPC64_DTPREL16_HIGHESTA:
14807           if (htab->elf.tls_sec != NULL)
14808             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14809           break;
14810
14811         case R_PPC64_ADDR64_LOCAL:
14812           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14813                                               ? h->elf.other
14814                                               : sym->st_other);
14815           break;
14816
14817         case R_PPC64_DTPMOD64:
14818           relocation = 1;
14819           addend = 0;
14820           goto dodyn;
14821
14822         case R_PPC64_TPREL64:
14823           if (htab->elf.tls_sec != NULL)
14824             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14825           goto dodyn;
14826
14827         case R_PPC64_DTPREL64:
14828           if (htab->elf.tls_sec != NULL)
14829             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14830           /* Fall through.  */
14831
14832           /* Relocations that may need to be propagated if this is a
14833              dynamic object.  */
14834         case R_PPC64_REL30:
14835         case R_PPC64_REL32:
14836         case R_PPC64_REL64:
14837         case R_PPC64_ADDR14:
14838         case R_PPC64_ADDR14_BRNTAKEN:
14839         case R_PPC64_ADDR14_BRTAKEN:
14840         case R_PPC64_ADDR16:
14841         case R_PPC64_ADDR16_DS:
14842         case R_PPC64_ADDR16_HA:
14843         case R_PPC64_ADDR16_HI:
14844         case R_PPC64_ADDR16_HIGH:
14845         case R_PPC64_ADDR16_HIGHA:
14846         case R_PPC64_ADDR16_HIGHER:
14847         case R_PPC64_ADDR16_HIGHERA:
14848         case R_PPC64_ADDR16_HIGHEST:
14849         case R_PPC64_ADDR16_HIGHESTA:
14850         case R_PPC64_ADDR16_LO:
14851         case R_PPC64_ADDR16_LO_DS:
14852         case R_PPC64_ADDR24:
14853         case R_PPC64_ADDR32:
14854         case R_PPC64_ADDR64:
14855         case R_PPC64_UADDR16:
14856         case R_PPC64_UADDR32:
14857         case R_PPC64_UADDR64:
14858         dodyn:
14859           if ((input_section->flags & SEC_ALLOC) == 0)
14860             break;
14861
14862           if (NO_OPD_RELOCS && is_opd)
14863             break;
14864
14865           if (bfd_link_pic (info)
14866               ? ((h == NULL
14867                   || h->dyn_relocs != NULL)
14868                  && ((h != NULL && pc_dynrelocs (h))
14869                      || must_be_dyn_reloc (info, r_type)))
14870               : (h != NULL
14871                  ? h->dyn_relocs != NULL
14872                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14873             {
14874               bfd_boolean skip, relocate;
14875               asection *sreloc;
14876               bfd_vma out_off;
14877               long indx = 0;
14878
14879               /* When generating a dynamic object, these relocations
14880                  are copied into the output file to be resolved at run
14881                  time.  */
14882
14883               skip = FALSE;
14884               relocate = FALSE;
14885
14886               out_off = _bfd_elf_section_offset (output_bfd, info,
14887                                                  input_section, rel->r_offset);
14888               if (out_off == (bfd_vma) -1)
14889                 skip = TRUE;
14890               else if (out_off == (bfd_vma) -2)
14891                 skip = TRUE, relocate = TRUE;
14892               out_off += (input_section->output_section->vma
14893                           + input_section->output_offset);
14894               outrel.r_offset = out_off;
14895               outrel.r_addend = rel->r_addend;
14896
14897               /* Optimize unaligned reloc use.  */
14898               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14899                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14900                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14901               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14902                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14903                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14904               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14905                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14906                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14907
14908               if (skip)
14909                 memset (&outrel, 0, sizeof outrel);
14910               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14911                        && !is_opd
14912                        && r_type != R_PPC64_TOC)
14913                 {
14914                   indx = h->elf.dynindx;
14915                   BFD_ASSERT (indx != -1);
14916                   outrel.r_info = ELF64_R_INFO (indx, r_type);
14917                 }
14918               else
14919                 {
14920                   /* This symbol is local, or marked to become local,
14921                      or this is an opd section reloc which must point
14922                      at a local function.  */
14923                   outrel.r_addend += relocation;
14924                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14925                     {
14926                       if (is_opd && h != NULL)
14927                         {
14928                           /* Lie about opd entries.  This case occurs
14929                              when building shared libraries and we
14930                              reference a function in another shared
14931                              lib.  The same thing happens for a weak
14932                              definition in an application that's
14933                              overridden by a strong definition in a
14934                              shared lib.  (I believe this is a generic
14935                              bug in binutils handling of weak syms.)
14936                              In these cases we won't use the opd
14937                              entry in this lib.  */
14938                           unresolved_reloc = FALSE;
14939                         }
14940                       if (!is_opd
14941                           && r_type == R_PPC64_ADDR64
14942                           && (h != NULL
14943                               ? h->elf.type == STT_GNU_IFUNC
14944                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14945                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14946                       else
14947                         {
14948                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14949
14950                           /* We need to relocate .opd contents for ld.so.
14951                              Prelink also wants simple and consistent rules
14952                              for relocs.  This make all RELATIVE relocs have
14953                              *r_offset equal to r_addend.  */
14954                           relocate = TRUE;
14955                         }
14956                     }
14957                   else
14958                     {
14959                       if (h != NULL
14960                           ? h->elf.type == STT_GNU_IFUNC
14961                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14962                         {
14963                           info->callbacks->einfo
14964                             /* xgettext:c-format */
14965                             (_("%H: %s for indirect "
14966                                "function `%pT' unsupported\n"),
14967                              input_bfd, input_section, rel->r_offset,
14968                              ppc64_elf_howto_table[r_type]->name,
14969                              sym_name);
14970                           ret = FALSE;
14971                         }
14972                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14973                         ;
14974                       else if (sec == NULL || sec->owner == NULL)
14975                         {
14976                           bfd_set_error (bfd_error_bad_value);
14977                           return FALSE;
14978                         }
14979                       else
14980                         {
14981                           asection *osec;
14982
14983                           osec = sec->output_section;
14984                           indx = elf_section_data (osec)->dynindx;
14985
14986                           if (indx == 0)
14987                             {
14988                               if ((osec->flags & SEC_READONLY) == 0
14989                                   && htab->elf.data_index_section != NULL)
14990                                 osec = htab->elf.data_index_section;
14991                               else
14992                                 osec = htab->elf.text_index_section;
14993                               indx = elf_section_data (osec)->dynindx;
14994                             }
14995                           BFD_ASSERT (indx != 0);
14996
14997                           /* We are turning this relocation into one
14998                              against a section symbol, so subtract out
14999                              the output section's address but not the
15000                              offset of the input section in the output
15001                              section.  */
15002                           outrel.r_addend -= osec->vma;
15003                         }
15004
15005                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15006                     }
15007                 }
15008
15009               sreloc = elf_section_data (input_section)->sreloc;
15010               if (h != NULL
15011                   ? h->elf.type == STT_GNU_IFUNC
15012                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15013                 {
15014                   sreloc = htab->elf.irelplt;
15015                   if (indx == 0)
15016                     htab->local_ifunc_resolver = 1;
15017                   else if (is_static_defined (&h->elf))
15018                     htab->maybe_local_ifunc_resolver = 1;
15019                 }
15020               if (sreloc == NULL)
15021                 abort ();
15022
15023               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15024                   >= sreloc->size)
15025                 abort ();
15026               loc = sreloc->contents;
15027               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15028               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15029
15030               /* If this reloc is against an external symbol, it will
15031                  be computed at runtime, so there's no need to do
15032                  anything now.  However, for the sake of prelink ensure
15033                  that the section contents are a known value.  */
15034               if (! relocate)
15035                 {
15036                   unresolved_reloc = FALSE;
15037                   /* The value chosen here is quite arbitrary as ld.so
15038                      ignores section contents except for the special
15039                      case of .opd where the contents might be accessed
15040                      before relocation.  Choose zero, as that won't
15041                      cause reloc overflow.  */
15042                   relocation = 0;
15043                   addend = 0;
15044                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15045                      to improve backward compatibility with older
15046                      versions of ld.  */
15047                   if (r_type == R_PPC64_ADDR64)
15048                     addend = outrel.r_addend;
15049                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15050                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15051                     addend = outrel.r_offset;
15052                 }
15053             }
15054           break;
15055
15056         case R_PPC64_COPY:
15057         case R_PPC64_GLOB_DAT:
15058         case R_PPC64_JMP_SLOT:
15059         case R_PPC64_JMP_IREL:
15060         case R_PPC64_RELATIVE:
15061           /* We shouldn't ever see these dynamic relocs in relocatable
15062              files.  */
15063           /* Fall through.  */
15064
15065         case R_PPC64_PLTGOT16:
15066         case R_PPC64_PLTGOT16_DS:
15067         case R_PPC64_PLTGOT16_HA:
15068         case R_PPC64_PLTGOT16_HI:
15069         case R_PPC64_PLTGOT16_LO:
15070         case R_PPC64_PLTGOT16_LO_DS:
15071         case R_PPC64_PLTREL32:
15072         case R_PPC64_PLTREL64:
15073           /* These ones haven't been implemented yet.  */
15074
15075           info->callbacks->einfo
15076             /* xgettext:c-format */
15077             (_("%P: %pB: %s is not supported for `%pT'\n"),
15078              input_bfd,
15079              ppc64_elf_howto_table[r_type]->name, sym_name);
15080
15081           bfd_set_error (bfd_error_invalid_operation);
15082           ret = FALSE;
15083           goto copy_reloc;
15084         }
15085
15086       /* Multi-instruction sequences that access the TOC can be
15087          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15088          to             nop;           addi rb,r2,x;  */
15089       switch (r_type)
15090         {
15091         default:
15092           break;
15093
15094         case R_PPC64_GOT_TLSLD16_HI:
15095         case R_PPC64_GOT_TLSGD16_HI:
15096         case R_PPC64_GOT_TPREL16_HI:
15097         case R_PPC64_GOT_DTPREL16_HI:
15098         case R_PPC64_GOT16_HI:
15099         case R_PPC64_TOC16_HI:
15100           /* These relocs would only be useful if building up an
15101              offset to later add to r2, perhaps in an indexed
15102              addressing mode instruction.  Don't try to optimize.
15103              Unfortunately, the possibility of someone building up an
15104              offset like this or even with the HA relocs, means that
15105              we need to check the high insn when optimizing the low
15106              insn.  */
15107           break;
15108
15109         case R_PPC64_GOT_TLSLD16_HA:
15110         case R_PPC64_GOT_TLSGD16_HA:
15111         case R_PPC64_GOT_TPREL16_HA:
15112         case R_PPC64_GOT_DTPREL16_HA:
15113         case R_PPC64_GOT16_HA:
15114         case R_PPC64_TOC16_HA:
15115           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15116               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15117             {
15118               bfd_byte *p = contents + (rel->r_offset & ~3);
15119               bfd_put_32 (input_bfd, NOP, p);
15120               goto copy_reloc;
15121             }
15122           break;
15123
15124         case R_PPC64_GOT_TLSLD16_LO:
15125         case R_PPC64_GOT_TLSGD16_LO:
15126         case R_PPC64_GOT_TPREL16_LO_DS:
15127         case R_PPC64_GOT_DTPREL16_LO_DS:
15128         case R_PPC64_GOT16_LO:
15129         case R_PPC64_GOT16_LO_DS:
15130         case R_PPC64_TOC16_LO:
15131         case R_PPC64_TOC16_LO_DS:
15132           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15133               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15134             {
15135               bfd_byte *p = contents + (rel->r_offset & ~3);
15136               insn = bfd_get_32 (input_bfd, p);
15137               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15138                 {
15139                   /* Transform addic to addi when we change reg.  */
15140                   insn &= ~((0x3f << 26) | (0x1f << 16));
15141                   insn |= (14u << 26) | (2 << 16);
15142                 }
15143               else
15144                 {
15145                   insn &= ~(0x1f << 16);
15146                   insn |= 2 << 16;
15147                 }
15148               bfd_put_32 (input_bfd, insn, p);
15149             }
15150           break;
15151
15152         case R_PPC64_TPREL16_HA:
15153           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15154             {
15155               bfd_byte *p = contents + (rel->r_offset & ~3);
15156               insn = bfd_get_32 (input_bfd, p);
15157               if ((insn & ((0x3f << 26) | 0x1f << 16))
15158                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15159                 /* xgettext:c-format */
15160                 info->callbacks->minfo
15161                   (_("%H: warning: %s unexpected insn %#x.\n"),
15162                    input_bfd, input_section, rel->r_offset,
15163                    ppc64_elf_howto_table[r_type]->name, insn);
15164               else
15165                 {
15166                   bfd_put_32 (input_bfd, NOP, p);
15167                   goto copy_reloc;
15168                 }
15169             }
15170           break;
15171
15172         case R_PPC64_TPREL16_LO:
15173         case R_PPC64_TPREL16_LO_DS:
15174           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15175             {
15176               bfd_byte *p = contents + (rel->r_offset & ~3);
15177               insn = bfd_get_32 (input_bfd, p);
15178               insn &= ~(0x1f << 16);
15179               insn |= 13 << 16;
15180               bfd_put_32 (input_bfd, insn, p);
15181             }
15182           break;
15183         }
15184
15185       /* Do any further special processing.  */
15186       switch (r_type)
15187         {
15188         default:
15189           break;
15190
15191         case R_PPC64_REL16_HA:
15192         case R_PPC64_REL16DX_HA:
15193         case R_PPC64_ADDR16_HA:
15194         case R_PPC64_ADDR16_HIGHA:
15195         case R_PPC64_ADDR16_HIGHERA:
15196         case R_PPC64_ADDR16_HIGHESTA:
15197         case R_PPC64_TOC16_HA:
15198         case R_PPC64_SECTOFF_HA:
15199         case R_PPC64_TPREL16_HA:
15200         case R_PPC64_TPREL16_HIGHA:
15201         case R_PPC64_TPREL16_HIGHERA:
15202         case R_PPC64_TPREL16_HIGHESTA:
15203         case R_PPC64_DTPREL16_HA:
15204         case R_PPC64_DTPREL16_HIGHA:
15205         case R_PPC64_DTPREL16_HIGHERA:
15206         case R_PPC64_DTPREL16_HIGHESTA:
15207           /* It's just possible that this symbol is a weak symbol
15208              that's not actually defined anywhere. In that case,
15209              'sec' would be NULL, and we should leave the symbol
15210              alone (it will be set to zero elsewhere in the link).  */
15211           if (sec == NULL)
15212             break;
15213           /* Fall through.  */
15214
15215         case R_PPC64_GOT16_HA:
15216         case R_PPC64_PLTGOT16_HA:
15217         case R_PPC64_PLT16_HA:
15218         case R_PPC64_GOT_TLSGD16_HA:
15219         case R_PPC64_GOT_TLSLD16_HA:
15220         case R_PPC64_GOT_TPREL16_HA:
15221         case R_PPC64_GOT_DTPREL16_HA:
15222           /* Add 0x10000 if sign bit in 0:15 is set.
15223              Bits 0:15 are not used.  */
15224           addend += 0x8000;
15225           break;
15226
15227         case R_PPC64_ADDR16_DS:
15228         case R_PPC64_ADDR16_LO_DS:
15229         case R_PPC64_GOT16_DS:
15230         case R_PPC64_GOT16_LO_DS:
15231         case R_PPC64_PLT16_LO_DS:
15232         case R_PPC64_SECTOFF_DS:
15233         case R_PPC64_SECTOFF_LO_DS:
15234         case R_PPC64_TOC16_DS:
15235         case R_PPC64_TOC16_LO_DS:
15236         case R_PPC64_PLTGOT16_DS:
15237         case R_PPC64_PLTGOT16_LO_DS:
15238         case R_PPC64_GOT_TPREL16_DS:
15239         case R_PPC64_GOT_TPREL16_LO_DS:
15240         case R_PPC64_GOT_DTPREL16_DS:
15241         case R_PPC64_GOT_DTPREL16_LO_DS:
15242         case R_PPC64_TPREL16_DS:
15243         case R_PPC64_TPREL16_LO_DS:
15244         case R_PPC64_DTPREL16_DS:
15245         case R_PPC64_DTPREL16_LO_DS:
15246           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15247           mask = 3;
15248           /* If this reloc is against an lq, lxv, or stxv insn, then
15249              the value must be a multiple of 16.  This is somewhat of
15250              a hack, but the "correct" way to do this by defining _DQ
15251              forms of all the _DS relocs bloats all reloc switches in
15252              this file.  It doesn't make much sense to use these
15253              relocs in data, so testing the insn should be safe.  */
15254           if ((insn & (0x3f << 26)) == (56u << 26)
15255               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15256             mask = 15;
15257           relocation += addend;
15258           addend = insn & (mask ^ 3);
15259           if ((relocation & mask) != 0)
15260             {
15261               relocation ^= relocation & mask;
15262               info->callbacks->einfo
15263                 /* xgettext:c-format */
15264                 (_("%H: error: %s not a multiple of %u\n"),
15265                  input_bfd, input_section, rel->r_offset,
15266                  ppc64_elf_howto_table[r_type]->name,
15267                  mask + 1);
15268               bfd_set_error (bfd_error_bad_value);
15269               ret = FALSE;
15270               goto copy_reloc;
15271             }
15272           break;
15273         }
15274
15275       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15276          because such sections are not SEC_ALLOC and thus ld.so will
15277          not process them.  */
15278       howto = ppc64_elf_howto_table[(int) r_type];
15279       if (unresolved_reloc
15280           && !((input_section->flags & SEC_DEBUGGING) != 0
15281                && h->elf.def_dynamic)
15282           && _bfd_elf_section_offset (output_bfd, info, input_section,
15283                                       rel->r_offset) != (bfd_vma) -1)
15284         {
15285           info->callbacks->einfo
15286             /* xgettext:c-format */
15287             (_("%H: unresolvable %s against `%pT'\n"),
15288              input_bfd, input_section, rel->r_offset,
15289              howto->name,
15290              h->elf.root.root.string);
15291           ret = FALSE;
15292         }
15293
15294       /* 16-bit fields in insns mostly have signed values, but a
15295          few insns have 16-bit unsigned values.  Really, we should
15296          have different reloc types.  */
15297       if (howto->complain_on_overflow != complain_overflow_dont
15298           && howto->dst_mask == 0xffff
15299           && (input_section->flags & SEC_CODE) != 0)
15300         {
15301           enum complain_overflow complain = complain_overflow_signed;
15302
15303           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15304           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15305             complain = complain_overflow_bitfield;
15306           else if (howto->rightshift == 0
15307                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15308                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15309                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15310                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15311                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15312                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15313             complain = complain_overflow_unsigned;
15314           if (howto->complain_on_overflow != complain)
15315             {
15316               alt_howto = *howto;
15317               alt_howto.complain_on_overflow = complain;
15318               howto = &alt_howto;
15319             }
15320         }
15321
15322       if (r_type == R_PPC64_REL16DX_HA)
15323         {
15324           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15325           if (rel->r_offset + 4 > input_section->size)
15326             r = bfd_reloc_outofrange;
15327           else
15328             {
15329               relocation += addend;
15330               relocation -= (rel->r_offset
15331                              + input_section->output_offset
15332                              + input_section->output_section->vma);
15333               relocation = (bfd_signed_vma) relocation >> 16;
15334               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15335               insn &= ~0x1fffc1;
15336               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15337               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15338               r = bfd_reloc_ok;
15339               if (relocation + 0x8000 > 0xffff)
15340                 r = bfd_reloc_overflow;
15341             }
15342         }
15343       else
15344         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15345                                       rel->r_offset, relocation, addend);
15346
15347       if (r != bfd_reloc_ok)
15348         {
15349           char *more_info = NULL;
15350           const char *reloc_name = howto->name;
15351
15352           if (reloc_dest != DEST_NORMAL)
15353             {
15354               more_info = bfd_malloc (strlen (reloc_name) + 8);
15355               if (more_info != NULL)
15356                 {
15357                   strcpy (more_info, reloc_name);
15358                   strcat (more_info, (reloc_dest == DEST_OPD
15359                                       ? " (OPD)" : " (stub)"));
15360                   reloc_name = more_info;
15361                 }
15362             }
15363
15364           if (r == bfd_reloc_overflow)
15365             {
15366               /* On code like "if (foo) foo();" don't report overflow
15367                  on a branch to zero when foo is undefined.  */
15368               if (!warned
15369                   && (reloc_dest == DEST_STUB
15370                       || !(h != NULL
15371                            && (h->elf.root.type == bfd_link_hash_undefweak
15372                                || h->elf.root.type == bfd_link_hash_undefined)
15373                            && is_branch_reloc (r_type))))
15374                 info->callbacks->reloc_overflow (info, &h->elf.root,
15375                                                  sym_name, reloc_name,
15376                                                  orig_rel.r_addend,
15377                                                  input_bfd, input_section,
15378                                                  rel->r_offset);
15379             }
15380           else
15381             {
15382               info->callbacks->einfo
15383                 /* xgettext:c-format */
15384                 (_("%H: %s against `%pT': error %d\n"),
15385                  input_bfd, input_section, rel->r_offset,
15386                  reloc_name, sym_name, (int) r);
15387               ret = FALSE;
15388             }
15389           if (more_info != NULL)
15390             free (more_info);
15391         }
15392     copy_reloc:
15393       if (wrel != rel)
15394         *wrel = *rel;
15395     }
15396
15397   if (wrel != rel)
15398     {
15399       Elf_Internal_Shdr *rel_hdr;
15400       size_t deleted = rel - wrel;
15401
15402       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15403       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15404       if (rel_hdr->sh_size == 0)
15405         {
15406           /* It is too late to remove an empty reloc section.  Leave
15407              one NONE reloc.
15408              ??? What is wrong with an empty section???  */
15409           rel_hdr->sh_size = rel_hdr->sh_entsize;
15410           deleted -= 1;
15411         }
15412       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15413       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15414       input_section->reloc_count -= deleted;
15415     }
15416
15417   /* If we're emitting relocations, then shortly after this function
15418      returns, reloc offsets and addends for this section will be
15419      adjusted.  Worse, reloc symbol indices will be for the output
15420      file rather than the input.  Save a copy of the relocs for
15421      opd_entry_value.  */
15422   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15423     {
15424       bfd_size_type amt;
15425       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15426       rel = bfd_alloc (input_bfd, amt);
15427       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15428       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15429       if (rel == NULL)
15430         return FALSE;
15431       memcpy (rel, relocs, amt);
15432     }
15433   return ret;
15434 }
15435
15436 /* Adjust the value of any local symbols in opd sections.  */
15437
15438 static int
15439 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15440                               const char *name ATTRIBUTE_UNUSED,
15441                               Elf_Internal_Sym *elfsym,
15442                               asection *input_sec,
15443                               struct elf_link_hash_entry *h)
15444 {
15445   struct _opd_sec_data *opd;
15446   long adjust;
15447   bfd_vma value;
15448
15449   if (h != NULL)
15450     return 1;
15451
15452   opd = get_opd_info (input_sec);
15453   if (opd == NULL || opd->adjust == NULL)
15454     return 1;
15455
15456   value = elfsym->st_value - input_sec->output_offset;
15457   if (!bfd_link_relocatable (info))
15458     value -= input_sec->output_section->vma;
15459
15460   adjust = opd->adjust[OPD_NDX (value)];
15461   if (adjust == -1)
15462     return 2;
15463
15464   elfsym->st_value += adjust;
15465   return 1;
15466 }
15467
15468 /* Finish up dynamic symbol handling.  We set the contents of various
15469    dynamic sections here.  */
15470
15471 static bfd_boolean
15472 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15473                                  struct bfd_link_info *info,
15474                                  struct elf_link_hash_entry *h,
15475                                  Elf_Internal_Sym *sym)
15476 {
15477   struct ppc_link_hash_table *htab;
15478   struct plt_entry *ent;
15479   Elf_Internal_Rela rela;
15480   bfd_byte *loc;
15481
15482   htab = ppc_hash_table (info);
15483   if (htab == NULL)
15484     return FALSE;
15485
15486   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15487     if (ent->plt.offset != (bfd_vma) -1)
15488       {
15489         /* This symbol has an entry in the procedure linkage
15490            table.  Set it up.  */
15491         if (!htab->elf.dynamic_sections_created
15492             || h->dynindx == -1)
15493           {
15494             BFD_ASSERT (h->type == STT_GNU_IFUNC
15495                         && h->def_regular
15496                         && (h->root.type == bfd_link_hash_defined
15497                             || h->root.type == bfd_link_hash_defweak));
15498             rela.r_offset = (htab->elf.iplt->output_section->vma
15499                              + htab->elf.iplt->output_offset
15500                              + ent->plt.offset);
15501             if (htab->opd_abi)
15502               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15503             else
15504               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15505             rela.r_addend = (h->root.u.def.value
15506                              + h->root.u.def.section->output_offset
15507                              + h->root.u.def.section->output_section->vma
15508                              + ent->addend);
15509             loc = (htab->elf.irelplt->contents
15510                    + (htab->elf.irelplt->reloc_count++
15511                       * sizeof (Elf64_External_Rela)));
15512             htab->local_ifunc_resolver = 1;
15513           }
15514         else
15515           {
15516             rela.r_offset = (htab->elf.splt->output_section->vma
15517                              + htab->elf.splt->output_offset
15518                              + ent->plt.offset);
15519             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15520             rela.r_addend = ent->addend;
15521             loc = (htab->elf.srelplt->contents
15522                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15523                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15524             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15525               htab->maybe_local_ifunc_resolver = 1;
15526           }
15527         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15528
15529         if (!htab->opd_abi)
15530           {
15531             if (!h->def_regular)
15532               {
15533                 /* Mark the symbol as undefined, rather than as
15534                    defined in glink.  Leave the value if there were
15535                    any relocations where pointer equality matters
15536                    (this is a clue for the dynamic linker, to make
15537                    function pointer comparisons work between an
15538                    application and shared library), otherwise set it
15539                    to zero.  */
15540                 sym->st_shndx = SHN_UNDEF;
15541                 if (!h->pointer_equality_needed)
15542                   sym->st_value = 0;
15543                 else if (!h->ref_regular_nonweak)
15544                   {
15545                     /* This breaks function pointer comparisons, but
15546                        that is better than breaking tests for a NULL
15547                        function pointer.  */
15548                     sym->st_value = 0;
15549                   }
15550               }
15551           }
15552       }
15553
15554   if (h->needs_copy)
15555     {
15556       /* This symbol needs a copy reloc.  Set it up.  */
15557       asection *srel;
15558
15559       if (h->dynindx == -1
15560           || (h->root.type != bfd_link_hash_defined
15561               && h->root.type != bfd_link_hash_defweak)
15562           || htab->elf.srelbss == NULL
15563           || htab->elf.sreldynrelro == NULL)
15564         abort ();
15565
15566       rela.r_offset = (h->root.u.def.value
15567                        + h->root.u.def.section->output_section->vma
15568                        + h->root.u.def.section->output_offset);
15569       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15570       rela.r_addend = 0;
15571       if (h->root.u.def.section == htab->elf.sdynrelro)
15572         srel = htab->elf.sreldynrelro;
15573       else
15574         srel = htab->elf.srelbss;
15575       loc = srel->contents;
15576       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15577       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15578     }
15579
15580   return TRUE;
15581 }
15582
15583 /* Used to decide how to sort relocs in an optimal manner for the
15584    dynamic linker, before writing them out.  */
15585
15586 static enum elf_reloc_type_class
15587 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15588                             const asection *rel_sec,
15589                             const Elf_Internal_Rela *rela)
15590 {
15591   enum elf_ppc64_reloc_type r_type;
15592   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15593
15594   if (rel_sec == htab->elf.irelplt)
15595     return reloc_class_ifunc;
15596
15597   r_type = ELF64_R_TYPE (rela->r_info);
15598   switch (r_type)
15599     {
15600     case R_PPC64_RELATIVE:
15601       return reloc_class_relative;
15602     case R_PPC64_JMP_SLOT:
15603       return reloc_class_plt;
15604     case R_PPC64_COPY:
15605       return reloc_class_copy;
15606     default:
15607       return reloc_class_normal;
15608     }
15609 }
15610
15611 /* Finish up the dynamic sections.  */
15612
15613 static bfd_boolean
15614 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15615                                    struct bfd_link_info *info)
15616 {
15617   struct ppc_link_hash_table *htab;
15618   bfd *dynobj;
15619   asection *sdyn;
15620
15621   htab = ppc_hash_table (info);
15622   if (htab == NULL)
15623     return FALSE;
15624
15625   dynobj = htab->elf.dynobj;
15626   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15627
15628   if (htab->elf.dynamic_sections_created)
15629     {
15630       Elf64_External_Dyn *dyncon, *dynconend;
15631
15632       if (sdyn == NULL || htab->elf.sgot == NULL)
15633         abort ();
15634
15635       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15636       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15637       for (; dyncon < dynconend; dyncon++)
15638         {
15639           Elf_Internal_Dyn dyn;
15640           asection *s;
15641
15642           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15643
15644           switch (dyn.d_tag)
15645             {
15646             default:
15647               continue;
15648
15649             case DT_PPC64_GLINK:
15650               s = htab->glink;
15651               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15652               /* We stupidly defined DT_PPC64_GLINK to be the start
15653                  of glink rather than the first entry point, which is
15654                  what ld.so needs, and now have a bigger stub to
15655                  support automatic multiple TOCs.  */
15656               dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15657               break;
15658
15659             case DT_PPC64_OPD:
15660               s = bfd_get_section_by_name (output_bfd, ".opd");
15661               if (s == NULL)
15662                 continue;
15663               dyn.d_un.d_ptr = s->vma;
15664               break;
15665
15666             case DT_PPC64_OPT:
15667               if (htab->do_multi_toc && htab->multi_toc_needed)
15668                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15669               if (htab->has_plt_localentry0)
15670                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15671               break;
15672
15673             case DT_PPC64_OPDSZ:
15674               s = bfd_get_section_by_name (output_bfd, ".opd");
15675               if (s == NULL)
15676                 continue;
15677               dyn.d_un.d_val = s->size;
15678               break;
15679
15680             case DT_PLTGOT:
15681               s = htab->elf.splt;
15682               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15683               break;
15684
15685             case DT_JMPREL:
15686               s = htab->elf.srelplt;
15687               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15688               break;
15689
15690             case DT_PLTRELSZ:
15691               dyn.d_un.d_val = htab->elf.srelplt->size;
15692               break;
15693
15694             case DT_TEXTREL:
15695               if (htab->local_ifunc_resolver)
15696                 info->callbacks->einfo
15697                   (_("%X%P: text relocations and GNU indirect "
15698                      "functions will result in a segfault at runtime\n"));
15699               else if (htab->maybe_local_ifunc_resolver)
15700                 info->callbacks->einfo
15701                   (_("%P: warning: text relocations and GNU indirect "
15702                      "functions may result in a segfault at runtime\n"));
15703               continue;
15704             }
15705
15706           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15707         }
15708     }
15709
15710   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15711       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15712     {
15713       /* Fill in the first entry in the global offset table.
15714          We use it to hold the link-time TOCbase.  */
15715       bfd_put_64 (output_bfd,
15716                   elf_gp (output_bfd) + TOC_BASE_OFF,
15717                   htab->elf.sgot->contents);
15718
15719       /* Set .got entry size.  */
15720       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15721     }
15722
15723   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15724       && htab->elf.splt->output_section != bfd_abs_section_ptr)
15725     {
15726       /* Set .plt entry size.  */
15727       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15728         = PLT_ENTRY_SIZE (htab);
15729     }
15730
15731   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15732      brlt ourselves if emitrelocations.  */
15733   if (htab->brlt != NULL
15734       && htab->brlt->reloc_count != 0
15735       && !_bfd_elf_link_output_relocs (output_bfd,
15736                                        htab->brlt,
15737                                        elf_section_data (htab->brlt)->rela.hdr,
15738                                        elf_section_data (htab->brlt)->relocs,
15739                                        NULL))
15740     return FALSE;
15741
15742   if (htab->glink != NULL
15743       && htab->glink->reloc_count != 0
15744       && !_bfd_elf_link_output_relocs (output_bfd,
15745                                        htab->glink,
15746                                        elf_section_data (htab->glink)->rela.hdr,
15747                                        elf_section_data (htab->glink)->relocs,
15748                                        NULL))
15749     return FALSE;
15750
15751   if (htab->glink_eh_frame != NULL
15752       && htab->glink_eh_frame->size != 0)
15753     {
15754       bfd_vma val;
15755       bfd_byte *p;
15756       struct map_stub *group;
15757       size_t align = 4;
15758
15759       p = htab->glink_eh_frame->contents;
15760       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15761
15762       for (group = htab->group; group != NULL; group = group->next)
15763         if (group->stub_sec != NULL)
15764           {
15765             /* Offset to stub section.  */
15766             val = (group->stub_sec->output_section->vma
15767                    + group->stub_sec->output_offset);
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                 _bfd_error_handler
15774                   (_("%s offset too large for .eh_frame sdata4 encoding"),
15775                    group->stub_sec->name);
15776                 return FALSE;
15777               }
15778             bfd_put_32 (dynobj, val, p + 8);
15779             p += stub_eh_frame_size (group, align);
15780           }
15781       if (htab->glink != NULL && htab->glink->size != 0)
15782         {
15783           /* Offset to .glink.  */
15784           val = (htab->glink->output_section->vma
15785                  + htab->glink->output_offset
15786                  + 8);
15787           val -= (htab->glink_eh_frame->output_section->vma
15788                   + htab->glink_eh_frame->output_offset
15789                   + (p + 8 - htab->glink_eh_frame->contents));
15790           if (val + 0x80000000 > 0xffffffff)
15791             {
15792               _bfd_error_handler
15793                 (_("%s offset too large for .eh_frame sdata4 encoding"),
15794                  htab->glink->name);
15795               return FALSE;
15796             }
15797           bfd_put_32 (dynobj, val, p + 8);
15798           p += (24 + align - 1) & -align;
15799         }
15800
15801       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15802           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15803                                                htab->glink_eh_frame,
15804                                                htab->glink_eh_frame->contents))
15805         return FALSE;
15806     }
15807
15808   /* We need to handle writing out multiple GOT sections ourselves,
15809      since we didn't add them to DYNOBJ.  We know dynobj is the first
15810      bfd.  */
15811   while ((dynobj = dynobj->link.next) != NULL)
15812     {
15813       asection *s;
15814
15815       if (!is_ppc64_elf (dynobj))
15816         continue;
15817
15818       s = ppc64_elf_tdata (dynobj)->got;
15819       if (s != NULL
15820           && s->size != 0
15821           && s->output_section != bfd_abs_section_ptr
15822           && !bfd_set_section_contents (output_bfd, s->output_section,
15823                                         s->contents, s->output_offset,
15824                                         s->size))
15825         return FALSE;
15826       s = ppc64_elf_tdata (dynobj)->relgot;
15827       if (s != NULL
15828           && s->size != 0
15829           && s->output_section != bfd_abs_section_ptr
15830           && !bfd_set_section_contents (output_bfd, s->output_section,
15831                                         s->contents, s->output_offset,
15832                                         s->size))
15833         return FALSE;
15834     }
15835
15836   return TRUE;
15837 }
15838
15839 #include "elf64-target.h"
15840
15841 /* FreeBSD support */
15842
15843 #undef  TARGET_LITTLE_SYM
15844 #undef  TARGET_LITTLE_NAME
15845
15846 #undef  TARGET_BIG_SYM
15847 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15848 #undef  TARGET_BIG_NAME
15849 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15850
15851 #undef  ELF_OSABI
15852 #define ELF_OSABI       ELFOSABI_FREEBSD
15853
15854 #undef  elf64_bed
15855 #define elf64_bed       elf64_powerpc_fbsd_bed
15856
15857 #include "elf64-target.h"