PowerPC64 ELFv2 symbols not needed in get_synthetic_symtab
[external/binutils.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2    Copyright (C) 1999-2017 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_gc_keep                   ppc64_elf_gc_keep
105 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
106 #define elf_backend_gc_mark_hook              ppc64_elf_gc_mark_hook
107 #define elf_backend_gc_sweep_hook             ppc64_elf_gc_sweep_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 call stub instructions.  We enter with the index in R0.  */
191 #define GLINK_CALL_STUB_SIZE (16*4)
192                                         /* 0:                           */
193                                         /*  .quad plt0-1f               */
194                                         /* __glink:                     */
195 #define MFLR_R12        0x7d8802a6      /*  mflr %12                    */
196 #define BCL_20_31       0x429f0005      /*  bcl 20,31,1f                */
197                                         /* 1:                           */
198 #define MFLR_R11        0x7d6802a6      /*  mflr %11                    */
199                                         /*  ld %2,(0b-1b)(%11)          */
200 #define MTLR_R12        0x7d8803a6      /*  mtlr %12                    */
201 #define ADD_R11_R2_R11  0x7d625a14      /*  add %11,%2,%11              */
202                                         /*  ld %12,0(%11)               */
203                                         /*  ld %2,8(%11)                */
204                                         /*  mtctr %12                   */
205                                         /*  ld %11,16(%11)              */
206                                         /*  bctr                        */
207 #define MFLR_R0         0x7c0802a6      /*  mflr %r0                    */
208 #define MTLR_R0         0x7c0803a6      /*  mtlr %r0                    */
209 #define SUB_R12_R12_R11 0x7d8b6050      /*  subf %r12,%r11,%r12         */
210 #define ADDI_R0_R12     0x380c0000      /*  addi %r0,%r12,0             */
211 #define SRDI_R0_R0_2    0x7800f082      /*  rldicl %r0,%r0,62,2         */
212
213 /* Pad with this.  */
214 #define NOP             0x60000000
215
216 /* Some other nops.  */
217 #define CROR_151515     0x4def7b82
218 #define CROR_313131     0x4ffffb82
219
220 /* .glink entries for the first 32k functions are two instructions.  */
221 #define LI_R0_0         0x38000000      /* li    %r0,0          */
222 #define B_DOT           0x48000000      /* b     .              */
223
224 /* After that, we need two instructions to load the index, followed by
225    a branch.  */
226 #define LIS_R0_0        0x3c000000      /* lis   %r0,0          */
227 #define ORI_R0_R0_0     0x60000000      /* ori   %r0,%r0,0      */
228
229 /* Instructions used by the save and restore reg functions.  */
230 #define STD_R0_0R1      0xf8010000      /* std   %r0,0(%r1)     */
231 #define STD_R0_0R12     0xf80c0000      /* std   %r0,0(%r12)    */
232 #define LD_R0_0R1       0xe8010000      /* ld    %r0,0(%r1)     */
233 #define LD_R0_0R12      0xe80c0000      /* ld    %r0,0(%r12)    */
234 #define STFD_FR0_0R1    0xd8010000      /* stfd  %fr0,0(%r1)    */
235 #define LFD_FR0_0R1     0xc8010000      /* lfd   %fr0,0(%r1)    */
236 #define LI_R12_0        0x39800000      /* li    %r12,0         */
237 #define STVX_VR0_R12_R0 0x7c0c01ce      /* stvx  %v0,%r12,%r0   */
238 #define LVX_VR0_R12_R0  0x7c0c00ce      /* lvx   %v0,%r12,%r0   */
239 #define MTLR_R0         0x7c0803a6      /* mtlr  %r0            */
240 #define BLR             0x4e800020      /* blr                  */
241
242 /* Since .opd is an array of descriptors and each entry will end up
243    with identical R_PPC64_RELATIVE relocs, there is really no need to
244    propagate .opd relocs;  The dynamic linker should be taught to
245    relocate .opd without reloc entries.  */
246 #ifndef NO_OPD_RELOCS
247 #define NO_OPD_RELOCS 0
248 #endif
249
250 #ifndef ARRAY_SIZE
251 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
252 #endif
253
254 static inline int
255 abiversion (bfd *abfd)
256 {
257   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
258 }
259
260 static inline void
261 set_abiversion (bfd *abfd, int ver)
262 {
263   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
264   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
265 }
266 \f
267 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
268
269 /* Relocation HOWTO's.  */
270 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
271
272 static reloc_howto_type ppc64_elf_howto_raw[] = {
273   /* This reloc does nothing.  */
274   HOWTO (R_PPC64_NONE,          /* type */
275          0,                     /* rightshift */
276          3,                     /* size (0 = byte, 1 = short, 2 = long) */
277          0,                     /* bitsize */
278          FALSE,                 /* pc_relative */
279          0,                     /* bitpos */
280          complain_overflow_dont, /* complain_on_overflow */
281          bfd_elf_generic_reloc, /* special_function */
282          "R_PPC64_NONE",        /* name */
283          FALSE,                 /* partial_inplace */
284          0,                     /* src_mask */
285          0,                     /* dst_mask */
286          FALSE),                /* pcrel_offset */
287
288   /* A standard 32 bit relocation.  */
289   HOWTO (R_PPC64_ADDR32,        /* type */
290          0,                     /* rightshift */
291          2,                     /* size (0 = byte, 1 = short, 2 = long) */
292          32,                    /* bitsize */
293          FALSE,                 /* pc_relative */
294          0,                     /* bitpos */
295          complain_overflow_bitfield, /* complain_on_overflow */
296          bfd_elf_generic_reloc, /* special_function */
297          "R_PPC64_ADDR32",      /* name */
298          FALSE,                 /* partial_inplace */
299          0,                     /* src_mask */
300          0xffffffff,            /* dst_mask */
301          FALSE),                /* pcrel_offset */
302
303   /* An absolute 26 bit branch; the lower two bits must be zero.
304      FIXME: we don't check that, we just clear them.  */
305   HOWTO (R_PPC64_ADDR24,        /* type */
306          0,                     /* rightshift */
307          2,                     /* size (0 = byte, 1 = short, 2 = long) */
308          26,                    /* bitsize */
309          FALSE,                 /* pc_relative */
310          0,                     /* bitpos */
311          complain_overflow_bitfield, /* complain_on_overflow */
312          bfd_elf_generic_reloc, /* special_function */
313          "R_PPC64_ADDR24",      /* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0x03fffffc,            /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* A standard 16 bit relocation.  */
320   HOWTO (R_PPC64_ADDR16,        /* type */
321          0,                     /* rightshift */
322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
323          16,                    /* bitsize */
324          FALSE,                 /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_bitfield, /* complain_on_overflow */
327          bfd_elf_generic_reloc, /* special_function */
328          "R_PPC64_ADDR16",      /* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0xffff,                /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* A 16 bit relocation without overflow.  */
335   HOWTO (R_PPC64_ADDR16_LO,     /* type */
336          0,                     /* rightshift */
337          1,                     /* size (0 = byte, 1 = short, 2 = long) */
338          16,                    /* bitsize */
339          FALSE,                 /* pc_relative */
340          0,                     /* bitpos */
341          complain_overflow_dont,/* complain_on_overflow */
342          bfd_elf_generic_reloc, /* special_function */
343          "R_PPC64_ADDR16_LO",   /* name */
344          FALSE,                 /* partial_inplace */
345          0,                     /* src_mask */
346          0xffff,                /* dst_mask */
347          FALSE),                /* pcrel_offset */
348
349   /* Bits 16-31 of an address.  */
350   HOWTO (R_PPC64_ADDR16_HI,     /* type */
351          16,                    /* rightshift */
352          1,                     /* size (0 = byte, 1 = short, 2 = long) */
353          16,                    /* bitsize */
354          FALSE,                 /* pc_relative */
355          0,                     /* bitpos */
356          complain_overflow_signed, /* complain_on_overflow */
357          bfd_elf_generic_reloc, /* special_function */
358          "R_PPC64_ADDR16_HI",   /* name */
359          FALSE,                 /* partial_inplace */
360          0,                     /* src_mask */
361          0xffff,                /* dst_mask */
362          FALSE),                /* pcrel_offset */
363
364   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
365      bits, treated as a signed number, is negative.  */
366   HOWTO (R_PPC64_ADDR16_HA,     /* type */
367          16,                    /* rightshift */
368          1,                     /* size (0 = byte, 1 = short, 2 = long) */
369          16,                    /* bitsize */
370          FALSE,                 /* pc_relative */
371          0,                     /* bitpos */
372          complain_overflow_signed, /* complain_on_overflow */
373          ppc64_elf_ha_reloc,    /* special_function */
374          "R_PPC64_ADDR16_HA",   /* name */
375          FALSE,                 /* partial_inplace */
376          0,                     /* src_mask */
377          0xffff,                /* dst_mask */
378          FALSE),                /* pcrel_offset */
379
380   /* An absolute 16 bit branch; the lower two bits must be zero.
381      FIXME: we don't check that, we just clear them.  */
382   HOWTO (R_PPC64_ADDR14,        /* type */
383          0,                     /* rightshift */
384          2,                     /* size (0 = byte, 1 = short, 2 = long) */
385          16,                    /* bitsize */
386          FALSE,                 /* pc_relative */
387          0,                     /* bitpos */
388          complain_overflow_signed, /* complain_on_overflow */
389          ppc64_elf_branch_reloc, /* special_function */
390          "R_PPC64_ADDR14",      /* name */
391          FALSE,                 /* partial_inplace */
392          0,                     /* src_mask */
393          0x0000fffc,            /* dst_mask */
394          FALSE),                /* pcrel_offset */
395
396   /* An absolute 16 bit branch, for which bit 10 should be set to
397      indicate that the branch is expected to be taken.  The lower two
398      bits must be zero.  */
399   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
400          0,                     /* rightshift */
401          2,                     /* size (0 = byte, 1 = short, 2 = long) */
402          16,                    /* bitsize */
403          FALSE,                 /* pc_relative */
404          0,                     /* bitpos */
405          complain_overflow_signed, /* complain_on_overflow */
406          ppc64_elf_brtaken_reloc, /* special_function */
407          "R_PPC64_ADDR14_BRTAKEN",/* name */
408          FALSE,                 /* partial_inplace */
409          0,                     /* src_mask */
410          0x0000fffc,            /* dst_mask */
411          FALSE),                /* pcrel_offset */
412
413   /* An absolute 16 bit branch, for which bit 10 should be set to
414      indicate that the branch is not expected to be taken.  The lower
415      two bits must be zero.  */
416   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
417          0,                     /* rightshift */
418          2,                     /* size (0 = byte, 1 = short, 2 = long) */
419          16,                    /* bitsize */
420          FALSE,                 /* pc_relative */
421          0,                     /* bitpos */
422          complain_overflow_signed, /* complain_on_overflow */
423          ppc64_elf_brtaken_reloc, /* special_function */
424          "R_PPC64_ADDR14_BRNTAKEN",/* name */
425          FALSE,                 /* partial_inplace */
426          0,                     /* src_mask */
427          0x0000fffc,            /* dst_mask */
428          FALSE),                /* pcrel_offset */
429
430   /* A relative 26 bit branch; the lower two bits must be zero.  */
431   HOWTO (R_PPC64_REL24,         /* type */
432          0,                     /* rightshift */
433          2,                     /* size (0 = byte, 1 = short, 2 = long) */
434          26,                    /* bitsize */
435          TRUE,                  /* pc_relative */
436          0,                     /* bitpos */
437          complain_overflow_signed, /* complain_on_overflow */
438          ppc64_elf_branch_reloc, /* special_function */
439          "R_PPC64_REL24",       /* name */
440          FALSE,                 /* partial_inplace */
441          0,                     /* src_mask */
442          0x03fffffc,            /* dst_mask */
443          TRUE),                 /* pcrel_offset */
444
445   /* A relative 16 bit branch; the lower two bits must be zero.  */
446   HOWTO (R_PPC64_REL14,         /* type */
447          0,                     /* rightshift */
448          2,                     /* size (0 = byte, 1 = short, 2 = long) */
449          16,                    /* bitsize */
450          TRUE,                  /* pc_relative */
451          0,                     /* bitpos */
452          complain_overflow_signed, /* complain_on_overflow */
453          ppc64_elf_branch_reloc, /* special_function */
454          "R_PPC64_REL14",       /* name */
455          FALSE,                 /* partial_inplace */
456          0,                     /* src_mask */
457          0x0000fffc,            /* dst_mask */
458          TRUE),                 /* pcrel_offset */
459
460   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
461      the branch is expected to be taken.  The lower two bits must be
462      zero.  */
463   HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
464          0,                     /* rightshift */
465          2,                     /* size (0 = byte, 1 = short, 2 = long) */
466          16,                    /* bitsize */
467          TRUE,                  /* pc_relative */
468          0,                     /* bitpos */
469          complain_overflow_signed, /* complain_on_overflow */
470          ppc64_elf_brtaken_reloc, /* special_function */
471          "R_PPC64_REL14_BRTAKEN", /* name */
472          FALSE,                 /* partial_inplace */
473          0,                     /* src_mask */
474          0x0000fffc,            /* dst_mask */
475          TRUE),                 /* pcrel_offset */
476
477   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
478      the branch is not expected to be taken.  The lower two bits must
479      be zero.  */
480   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
481          0,                     /* rightshift */
482          2,                     /* size (0 = byte, 1 = short, 2 = long) */
483          16,                    /* bitsize */
484          TRUE,                  /* pc_relative */
485          0,                     /* bitpos */
486          complain_overflow_signed, /* complain_on_overflow */
487          ppc64_elf_brtaken_reloc, /* special_function */
488          "R_PPC64_REL14_BRNTAKEN",/* name */
489          FALSE,                 /* partial_inplace */
490          0,                     /* src_mask */
491          0x0000fffc,            /* dst_mask */
492          TRUE),                 /* pcrel_offset */
493
494   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
495      symbol.  */
496   HOWTO (R_PPC64_GOT16,         /* type */
497          0,                     /* rightshift */
498          1,                     /* size (0 = byte, 1 = short, 2 = long) */
499          16,                    /* bitsize */
500          FALSE,                 /* pc_relative */
501          0,                     /* bitpos */
502          complain_overflow_signed, /* complain_on_overflow */
503          ppc64_elf_unhandled_reloc, /* special_function */
504          "R_PPC64_GOT16",       /* name */
505          FALSE,                 /* partial_inplace */
506          0,                     /* src_mask */
507          0xffff,                /* dst_mask */
508          FALSE),                /* pcrel_offset */
509
510   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
511      the symbol.  */
512   HOWTO (R_PPC64_GOT16_LO,      /* type */
513          0,                     /* rightshift */
514          1,                     /* size (0 = byte, 1 = short, 2 = long) */
515          16,                    /* bitsize */
516          FALSE,                 /* pc_relative */
517          0,                     /* bitpos */
518          complain_overflow_dont, /* complain_on_overflow */
519          ppc64_elf_unhandled_reloc, /* special_function */
520          "R_PPC64_GOT16_LO",    /* name */
521          FALSE,                 /* partial_inplace */
522          0,                     /* src_mask */
523          0xffff,                /* dst_mask */
524          FALSE),                /* pcrel_offset */
525
526   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
527      the symbol.  */
528   HOWTO (R_PPC64_GOT16_HI,      /* type */
529          16,                    /* rightshift */
530          1,                     /* size (0 = byte, 1 = short, 2 = long) */
531          16,                    /* bitsize */
532          FALSE,                 /* pc_relative */
533          0,                     /* bitpos */
534          complain_overflow_signed,/* complain_on_overflow */
535          ppc64_elf_unhandled_reloc, /* special_function */
536          "R_PPC64_GOT16_HI",    /* name */
537          FALSE,                 /* partial_inplace */
538          0,                     /* src_mask */
539          0xffff,                /* dst_mask */
540          FALSE),                /* pcrel_offset */
541
542   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
543      the symbol.  */
544   HOWTO (R_PPC64_GOT16_HA,      /* type */
545          16,                    /* rightshift */
546          1,                     /* size (0 = byte, 1 = short, 2 = long) */
547          16,                    /* bitsize */
548          FALSE,                 /* pc_relative */
549          0,                     /* bitpos */
550          complain_overflow_signed,/* complain_on_overflow */
551          ppc64_elf_unhandled_reloc, /* special_function */
552          "R_PPC64_GOT16_HA",    /* name */
553          FALSE,                 /* partial_inplace */
554          0,                     /* src_mask */
555          0xffff,                /* dst_mask */
556          FALSE),                /* pcrel_offset */
557
558   /* This is used only by the dynamic linker.  The symbol should exist
559      both in the object being run and in some shared library.  The
560      dynamic linker copies the data addressed by the symbol from the
561      shared library into the object, because the object being
562      run has to have the data at some particular address.  */
563   HOWTO (R_PPC64_COPY,          /* type */
564          0,                     /* rightshift */
565          0,                     /* this one is variable size */
566          0,                     /* bitsize */
567          FALSE,                 /* pc_relative */
568          0,                     /* bitpos */
569          complain_overflow_dont, /* complain_on_overflow */
570          ppc64_elf_unhandled_reloc, /* special_function */
571          "R_PPC64_COPY",        /* name */
572          FALSE,                 /* partial_inplace */
573          0,                     /* src_mask */
574          0,                     /* dst_mask */
575          FALSE),                /* pcrel_offset */
576
577   /* Like R_PPC64_ADDR64, but used when setting global offset table
578      entries.  */
579   HOWTO (R_PPC64_GLOB_DAT,      /* type */
580          0,                     /* rightshift */
581          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
582          64,                    /* bitsize */
583          FALSE,                 /* pc_relative */
584          0,                     /* bitpos */
585          complain_overflow_dont, /* complain_on_overflow */
586          ppc64_elf_unhandled_reloc,  /* special_function */
587          "R_PPC64_GLOB_DAT",    /* name */
588          FALSE,                 /* partial_inplace */
589          0,                     /* src_mask */
590          ONES (64),             /* dst_mask */
591          FALSE),                /* pcrel_offset */
592
593   /* Created by the link editor.  Marks a procedure linkage table
594      entry for a symbol.  */
595   HOWTO (R_PPC64_JMP_SLOT,      /* type */
596          0,                     /* rightshift */
597          0,                     /* size (0 = byte, 1 = short, 2 = long) */
598          0,                     /* bitsize */
599          FALSE,                 /* pc_relative */
600          0,                     /* bitpos */
601          complain_overflow_dont, /* complain_on_overflow */
602          ppc64_elf_unhandled_reloc, /* special_function */
603          "R_PPC64_JMP_SLOT",    /* name */
604          FALSE,                 /* partial_inplace */
605          0,                     /* src_mask */
606          0,                     /* dst_mask */
607          FALSE),                /* pcrel_offset */
608
609   /* Used only by the dynamic linker.  When the object is run, this
610      doubleword64 is set to the load address of the object, plus the
611      addend.  */
612   HOWTO (R_PPC64_RELATIVE,      /* type */
613          0,                     /* rightshift */
614          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
615          64,                    /* bitsize */
616          FALSE,                 /* pc_relative */
617          0,                     /* bitpos */
618          complain_overflow_dont, /* complain_on_overflow */
619          bfd_elf_generic_reloc, /* special_function */
620          "R_PPC64_RELATIVE",    /* name */
621          FALSE,                 /* partial_inplace */
622          0,                     /* src_mask */
623          ONES (64),             /* dst_mask */
624          FALSE),                /* pcrel_offset */
625
626   /* Like R_PPC64_ADDR32, but may be unaligned.  */
627   HOWTO (R_PPC64_UADDR32,       /* type */
628          0,                     /* rightshift */
629          2,                     /* size (0 = byte, 1 = short, 2 = long) */
630          32,                    /* bitsize */
631          FALSE,                 /* pc_relative */
632          0,                     /* bitpos */
633          complain_overflow_bitfield, /* complain_on_overflow */
634          bfd_elf_generic_reloc, /* special_function */
635          "R_PPC64_UADDR32",     /* name */
636          FALSE,                 /* partial_inplace */
637          0,                     /* src_mask */
638          0xffffffff,            /* dst_mask */
639          FALSE),                /* pcrel_offset */
640
641   /* Like R_PPC64_ADDR16, but may be unaligned.  */
642   HOWTO (R_PPC64_UADDR16,       /* type */
643          0,                     /* rightshift */
644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
645          16,                    /* bitsize */
646          FALSE,                 /* pc_relative */
647          0,                     /* bitpos */
648          complain_overflow_bitfield, /* complain_on_overflow */
649          bfd_elf_generic_reloc, /* special_function */
650          "R_PPC64_UADDR16",     /* name */
651          FALSE,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0xffff,                /* dst_mask */
654          FALSE),                /* pcrel_offset */
655
656   /* 32-bit PC relative.  */
657   HOWTO (R_PPC64_REL32,         /* type */
658          0,                     /* rightshift */
659          2,                     /* size (0 = byte, 1 = short, 2 = long) */
660          32,                    /* bitsize */
661          TRUE,                  /* pc_relative */
662          0,                     /* bitpos */
663          complain_overflow_signed, /* complain_on_overflow */
664          bfd_elf_generic_reloc, /* special_function */
665          "R_PPC64_REL32",       /* name */
666          FALSE,                 /* partial_inplace */
667          0,                     /* src_mask */
668          0xffffffff,            /* dst_mask */
669          TRUE),                 /* pcrel_offset */
670
671   /* 32-bit relocation to the symbol's procedure linkage table.  */
672   HOWTO (R_PPC64_PLT32,         /* type */
673          0,                     /* rightshift */
674          2,                     /* size (0 = byte, 1 = short, 2 = long) */
675          32,                    /* bitsize */
676          FALSE,                 /* pc_relative */
677          0,                     /* bitpos */
678          complain_overflow_bitfield, /* complain_on_overflow */
679          ppc64_elf_unhandled_reloc, /* special_function */
680          "R_PPC64_PLT32",       /* name */
681          FALSE,                 /* partial_inplace */
682          0,                     /* src_mask */
683          0xffffffff,            /* dst_mask */
684          FALSE),                /* pcrel_offset */
685
686   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
687      FIXME: R_PPC64_PLTREL32 not supported.  */
688   HOWTO (R_PPC64_PLTREL32,      /* type */
689          0,                     /* rightshift */
690          2,                     /* size (0 = byte, 1 = short, 2 = long) */
691          32,                    /* bitsize */
692          TRUE,                  /* pc_relative */
693          0,                     /* bitpos */
694          complain_overflow_signed, /* complain_on_overflow */
695          ppc64_elf_unhandled_reloc, /* special_function */
696          "R_PPC64_PLTREL32",    /* name */
697          FALSE,                 /* partial_inplace */
698          0,                     /* src_mask */
699          0xffffffff,            /* dst_mask */
700          TRUE),                 /* pcrel_offset */
701
702   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
703      the symbol.  */
704   HOWTO (R_PPC64_PLT16_LO,      /* type */
705          0,                     /* rightshift */
706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
707          16,                    /* bitsize */
708          FALSE,                 /* pc_relative */
709          0,                     /* bitpos */
710          complain_overflow_dont, /* complain_on_overflow */
711          ppc64_elf_unhandled_reloc, /* special_function */
712          "R_PPC64_PLT16_LO",    /* name */
713          FALSE,                 /* partial_inplace */
714          0,                     /* src_mask */
715          0xffff,                /* dst_mask */
716          FALSE),                /* pcrel_offset */
717
718   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
719      the symbol.  */
720   HOWTO (R_PPC64_PLT16_HI,      /* type */
721          16,                    /* rightshift */
722          1,                     /* size (0 = byte, 1 = short, 2 = long) */
723          16,                    /* bitsize */
724          FALSE,                 /* pc_relative */
725          0,                     /* bitpos */
726          complain_overflow_signed, /* complain_on_overflow */
727          ppc64_elf_unhandled_reloc, /* special_function */
728          "R_PPC64_PLT16_HI",    /* name */
729          FALSE,                 /* partial_inplace */
730          0,                     /* src_mask */
731          0xffff,                /* dst_mask */
732          FALSE),                /* pcrel_offset */
733
734   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
735      the symbol.  */
736   HOWTO (R_PPC64_PLT16_HA,      /* type */
737          16,                    /* rightshift */
738          1,                     /* size (0 = byte, 1 = short, 2 = long) */
739          16,                    /* bitsize */
740          FALSE,                 /* pc_relative */
741          0,                     /* bitpos */
742          complain_overflow_signed, /* complain_on_overflow */
743          ppc64_elf_unhandled_reloc, /* special_function */
744          "R_PPC64_PLT16_HA",    /* name */
745          FALSE,                 /* partial_inplace */
746          0,                     /* src_mask */
747          0xffff,                /* dst_mask */
748          FALSE),                /* pcrel_offset */
749
750   /* 16-bit section relative relocation.  */
751   HOWTO (R_PPC64_SECTOFF,       /* type */
752          0,                     /* rightshift */
753          1,                     /* size (0 = byte, 1 = short, 2 = long) */
754          16,                    /* bitsize */
755          FALSE,                 /* pc_relative */
756          0,                     /* bitpos */
757          complain_overflow_signed, /* complain_on_overflow */
758          ppc64_elf_sectoff_reloc, /* special_function */
759          "R_PPC64_SECTOFF",     /* name */
760          FALSE,                 /* partial_inplace */
761          0,                     /* src_mask */
762          0xffff,                /* dst_mask */
763          FALSE),                /* pcrel_offset */
764
765   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
766   HOWTO (R_PPC64_SECTOFF_LO,    /* type */
767          0,                     /* rightshift */
768          1,                     /* size (0 = byte, 1 = short, 2 = long) */
769          16,                    /* bitsize */
770          FALSE,                 /* pc_relative */
771          0,                     /* bitpos */
772          complain_overflow_dont, /* complain_on_overflow */
773          ppc64_elf_sectoff_reloc, /* special_function */
774          "R_PPC64_SECTOFF_LO",  /* name */
775          FALSE,                 /* partial_inplace */
776          0,                     /* src_mask */
777          0xffff,                /* dst_mask */
778          FALSE),                /* pcrel_offset */
779
780   /* 16-bit upper half section relative relocation.  */
781   HOWTO (R_PPC64_SECTOFF_HI,    /* type */
782          16,                    /* rightshift */
783          1,                     /* size (0 = byte, 1 = short, 2 = long) */
784          16,                    /* bitsize */
785          FALSE,                 /* pc_relative */
786          0,                     /* bitpos */
787          complain_overflow_signed, /* complain_on_overflow */
788          ppc64_elf_sectoff_reloc, /* special_function */
789          "R_PPC64_SECTOFF_HI",  /* name */
790          FALSE,                 /* partial_inplace */
791          0,                     /* src_mask */
792          0xffff,                /* dst_mask */
793          FALSE),                /* pcrel_offset */
794
795   /* 16-bit upper half adjusted section relative relocation.  */
796   HOWTO (R_PPC64_SECTOFF_HA,    /* type */
797          16,                    /* rightshift */
798          1,                     /* size (0 = byte, 1 = short, 2 = long) */
799          16,                    /* bitsize */
800          FALSE,                 /* pc_relative */
801          0,                     /* bitpos */
802          complain_overflow_signed, /* complain_on_overflow */
803          ppc64_elf_sectoff_ha_reloc, /* special_function */
804          "R_PPC64_SECTOFF_HA",  /* name */
805          FALSE,                 /* partial_inplace */
806          0,                     /* src_mask */
807          0xffff,                /* dst_mask */
808          FALSE),                /* pcrel_offset */
809
810   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
811   HOWTO (R_PPC64_REL30,         /* type */
812          2,                     /* rightshift */
813          2,                     /* size (0 = byte, 1 = short, 2 = long) */
814          30,                    /* bitsize */
815          TRUE,                  /* pc_relative */
816          0,                     /* bitpos */
817          complain_overflow_dont, /* complain_on_overflow */
818          bfd_elf_generic_reloc, /* special_function */
819          "R_PPC64_REL30",       /* name */
820          FALSE,                 /* partial_inplace */
821          0,                     /* src_mask */
822          0xfffffffc,            /* dst_mask */
823          TRUE),                 /* pcrel_offset */
824
825   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
826
827   /* A standard 64-bit relocation.  */
828   HOWTO (R_PPC64_ADDR64,        /* type */
829          0,                     /* rightshift */
830          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
831          64,                    /* bitsize */
832          FALSE,                 /* pc_relative */
833          0,                     /* bitpos */
834          complain_overflow_dont, /* complain_on_overflow */
835          bfd_elf_generic_reloc, /* special_function */
836          "R_PPC64_ADDR64",      /* name */
837          FALSE,                 /* partial_inplace */
838          0,                     /* src_mask */
839          ONES (64),             /* dst_mask */
840          FALSE),                /* pcrel_offset */
841
842   /* The bits 32-47 of an address.  */
843   HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
844          32,                    /* rightshift */
845          1,                     /* size (0 = byte, 1 = short, 2 = long) */
846          16,                    /* bitsize */
847          FALSE,                 /* pc_relative */
848          0,                     /* bitpos */
849          complain_overflow_dont, /* complain_on_overflow */
850          bfd_elf_generic_reloc, /* special_function */
851          "R_PPC64_ADDR16_HIGHER", /* name */
852          FALSE,                 /* partial_inplace */
853          0,                     /* src_mask */
854          0xffff,                /* dst_mask */
855          FALSE),                /* pcrel_offset */
856
857   /* The bits 32-47 of an address, plus 1 if the contents of the low
858      16 bits, treated as a signed number, is negative.  */
859   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
860          32,                    /* rightshift */
861          1,                     /* size (0 = byte, 1 = short, 2 = long) */
862          16,                    /* bitsize */
863          FALSE,                 /* pc_relative */
864          0,                     /* bitpos */
865          complain_overflow_dont, /* complain_on_overflow */
866          ppc64_elf_ha_reloc,    /* special_function */
867          "R_PPC64_ADDR16_HIGHERA", /* name */
868          FALSE,                 /* partial_inplace */
869          0,                     /* src_mask */
870          0xffff,                /* dst_mask */
871          FALSE),                /* pcrel_offset */
872
873   /* The bits 48-63 of an address.  */
874   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
875          48,                    /* rightshift */
876          1,                     /* size (0 = byte, 1 = short, 2 = long) */
877          16,                    /* bitsize */
878          FALSE,                 /* pc_relative */
879          0,                     /* bitpos */
880          complain_overflow_dont, /* complain_on_overflow */
881          bfd_elf_generic_reloc, /* special_function */
882          "R_PPC64_ADDR16_HIGHEST", /* name */
883          FALSE,                 /* partial_inplace */
884          0,                     /* src_mask */
885          0xffff,                /* dst_mask */
886          FALSE),                /* pcrel_offset */
887
888   /* The bits 48-63 of an address, plus 1 if the contents of the low
889      16 bits, treated as a signed number, is negative.  */
890   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
891          48,                    /* rightshift */
892          1,                     /* size (0 = byte, 1 = short, 2 = long) */
893          16,                    /* bitsize */
894          FALSE,                 /* pc_relative */
895          0,                     /* bitpos */
896          complain_overflow_dont, /* complain_on_overflow */
897          ppc64_elf_ha_reloc,    /* special_function */
898          "R_PPC64_ADDR16_HIGHESTA", /* name */
899          FALSE,                 /* partial_inplace */
900          0,                     /* src_mask */
901          0xffff,                /* dst_mask */
902          FALSE),                /* pcrel_offset */
903
904   /* Like ADDR64, but may be unaligned.  */
905   HOWTO (R_PPC64_UADDR64,       /* type */
906          0,                     /* rightshift */
907          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
908          64,                    /* bitsize */
909          FALSE,                 /* pc_relative */
910          0,                     /* bitpos */
911          complain_overflow_dont, /* complain_on_overflow */
912          bfd_elf_generic_reloc, /* special_function */
913          "R_PPC64_UADDR64",     /* name */
914          FALSE,                 /* partial_inplace */
915          0,                     /* src_mask */
916          ONES (64),             /* dst_mask */
917          FALSE),                /* pcrel_offset */
918
919   /* 64-bit relative relocation.  */
920   HOWTO (R_PPC64_REL64,         /* type */
921          0,                     /* rightshift */
922          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
923          64,                    /* bitsize */
924          TRUE,                  /* pc_relative */
925          0,                     /* bitpos */
926          complain_overflow_dont, /* complain_on_overflow */
927          bfd_elf_generic_reloc, /* special_function */
928          "R_PPC64_REL64",       /* name */
929          FALSE,                 /* partial_inplace */
930          0,                     /* src_mask */
931          ONES (64),             /* dst_mask */
932          TRUE),                 /* pcrel_offset */
933
934   /* 64-bit relocation to the symbol's procedure linkage table.  */
935   HOWTO (R_PPC64_PLT64,         /* type */
936          0,                     /* rightshift */
937          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
938          64,                    /* bitsize */
939          FALSE,                 /* pc_relative */
940          0,                     /* bitpos */
941          complain_overflow_dont, /* complain_on_overflow */
942          ppc64_elf_unhandled_reloc, /* special_function */
943          "R_PPC64_PLT64",       /* name */
944          FALSE,                 /* partial_inplace */
945          0,                     /* src_mask */
946          ONES (64),             /* dst_mask */
947          FALSE),                /* pcrel_offset */
948
949   /* 64-bit PC relative relocation to the symbol's procedure linkage
950      table.  */
951   /* FIXME: R_PPC64_PLTREL64 not supported.  */
952   HOWTO (R_PPC64_PLTREL64,      /* type */
953          0,                     /* rightshift */
954          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
955          64,                    /* bitsize */
956          TRUE,                  /* pc_relative */
957          0,                     /* bitpos */
958          complain_overflow_dont, /* complain_on_overflow */
959          ppc64_elf_unhandled_reloc, /* special_function */
960          "R_PPC64_PLTREL64",    /* name */
961          FALSE,                 /* partial_inplace */
962          0,                     /* src_mask */
963          ONES (64),             /* dst_mask */
964          TRUE),                 /* pcrel_offset */
965
966   /* 16 bit TOC-relative relocation.  */
967
968   /* R_PPC64_TOC16        47       half16*      S + A - .TOC.  */
969   HOWTO (R_PPC64_TOC16,         /* type */
970          0,                     /* rightshift */
971          1,                     /* size (0 = byte, 1 = short, 2 = long) */
972          16,                    /* bitsize */
973          FALSE,                 /* pc_relative */
974          0,                     /* bitpos */
975          complain_overflow_signed, /* complain_on_overflow */
976          ppc64_elf_toc_reloc,   /* special_function */
977          "R_PPC64_TOC16",       /* name */
978          FALSE,                 /* partial_inplace */
979          0,                     /* src_mask */
980          0xffff,                /* dst_mask */
981          FALSE),                /* pcrel_offset */
982
983   /* 16 bit TOC-relative relocation without overflow.  */
984
985   /* R_PPC64_TOC16_LO     48       half16        #lo (S + A - .TOC.)  */
986   HOWTO (R_PPC64_TOC16_LO,      /* type */
987          0,                     /* rightshift */
988          1,                     /* size (0 = byte, 1 = short, 2 = long) */
989          16,                    /* bitsize */
990          FALSE,                 /* pc_relative */
991          0,                     /* bitpos */
992          complain_overflow_dont, /* complain_on_overflow */
993          ppc64_elf_toc_reloc,   /* special_function */
994          "R_PPC64_TOC16_LO",    /* name */
995          FALSE,                 /* partial_inplace */
996          0,                     /* src_mask */
997          0xffff,                /* dst_mask */
998          FALSE),                /* pcrel_offset */
999
1000   /* 16 bit TOC-relative relocation, high 16 bits.  */
1001
1002   /* R_PPC64_TOC16_HI     49       half16        #hi (S + A - .TOC.)  */
1003   HOWTO (R_PPC64_TOC16_HI,      /* type */
1004          16,                    /* rightshift */
1005          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1006          16,                    /* bitsize */
1007          FALSE,                 /* pc_relative */
1008          0,                     /* bitpos */
1009          complain_overflow_signed, /* complain_on_overflow */
1010          ppc64_elf_toc_reloc,   /* special_function */
1011          "R_PPC64_TOC16_HI",    /* name */
1012          FALSE,                 /* partial_inplace */
1013          0,                     /* src_mask */
1014          0xffff,                /* dst_mask */
1015          FALSE),                /* pcrel_offset */
1016
1017   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1018      contents of the low 16 bits, treated as a signed number, is
1019      negative.  */
1020
1021   /* R_PPC64_TOC16_HA     50       half16        #ha (S + A - .TOC.)  */
1022   HOWTO (R_PPC64_TOC16_HA,      /* type */
1023          16,                    /* rightshift */
1024          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1025          16,                    /* bitsize */
1026          FALSE,                 /* pc_relative */
1027          0,                     /* bitpos */
1028          complain_overflow_signed, /* complain_on_overflow */
1029          ppc64_elf_toc_ha_reloc, /* special_function */
1030          "R_PPC64_TOC16_HA",    /* name */
1031          FALSE,                 /* partial_inplace */
1032          0,                     /* src_mask */
1033          0xffff,                /* dst_mask */
1034          FALSE),                /* pcrel_offset */
1035
1036   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
1037
1038   /* R_PPC64_TOC                  51       doubleword64  .TOC.  */
1039   HOWTO (R_PPC64_TOC,           /* type */
1040          0,                     /* rightshift */
1041          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1042          64,                    /* bitsize */
1043          FALSE,                 /* pc_relative */
1044          0,                     /* bitpos */
1045          complain_overflow_dont, /* complain_on_overflow */
1046          ppc64_elf_toc64_reloc, /* special_function */
1047          "R_PPC64_TOC",         /* name */
1048          FALSE,                 /* partial_inplace */
1049          0,                     /* src_mask */
1050          ONES (64),             /* dst_mask */
1051          FALSE),                /* pcrel_offset */
1052
1053   /* Like R_PPC64_GOT16, but also informs the link editor that the
1054      value to relocate may (!) refer to a PLT entry which the link
1055      editor (a) may replace with the symbol value.  If the link editor
1056      is unable to fully resolve the symbol, it may (b) create a PLT
1057      entry and store the address to the new PLT entry in the GOT.
1058      This permits lazy resolution of function symbols at run time.
1059      The link editor may also skip all of this and just (c) emit a
1060      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
1061   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
1062     HOWTO (R_PPC64_PLTGOT16,    /* type */
1063          0,                     /* rightshift */
1064          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1065          16,                    /* bitsize */
1066          FALSE,                 /* pc_relative */
1067          0,                     /* bitpos */
1068          complain_overflow_signed, /* complain_on_overflow */
1069          ppc64_elf_unhandled_reloc, /* special_function */
1070          "R_PPC64_PLTGOT16",    /* name */
1071          FALSE,                 /* partial_inplace */
1072          0,                     /* src_mask */
1073          0xffff,                /* dst_mask */
1074          FALSE),                /* pcrel_offset */
1075
1076   /* Like R_PPC64_PLTGOT16, but without overflow.  */
1077   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1078   HOWTO (R_PPC64_PLTGOT16_LO,   /* type */
1079          0,                     /* rightshift */
1080          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1081          16,                    /* bitsize */
1082          FALSE,                 /* pc_relative */
1083          0,                     /* bitpos */
1084          complain_overflow_dont, /* complain_on_overflow */
1085          ppc64_elf_unhandled_reloc, /* special_function */
1086          "R_PPC64_PLTGOT16_LO", /* name */
1087          FALSE,                 /* partial_inplace */
1088          0,                     /* src_mask */
1089          0xffff,                /* dst_mask */
1090          FALSE),                /* pcrel_offset */
1091
1092   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
1093   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
1094   HOWTO (R_PPC64_PLTGOT16_HI,   /* type */
1095          16,                    /* rightshift */
1096          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1097          16,                    /* bitsize */
1098          FALSE,                 /* pc_relative */
1099          0,                     /* bitpos */
1100          complain_overflow_signed, /* complain_on_overflow */
1101          ppc64_elf_unhandled_reloc, /* special_function */
1102          "R_PPC64_PLTGOT16_HI", /* name */
1103          FALSE,                 /* partial_inplace */
1104          0,                     /* src_mask */
1105          0xffff,                /* dst_mask */
1106          FALSE),                /* pcrel_offset */
1107
1108   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1109      1 if the contents of the low 16 bits, treated as a signed number,
1110      is negative.  */
1111   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
1112   HOWTO (R_PPC64_PLTGOT16_HA,   /* type */
1113          16,                    /* rightshift */
1114          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1115          16,                    /* bitsize */
1116          FALSE,                 /* pc_relative */
1117          0,                     /* bitpos */
1118          complain_overflow_signed, /* complain_on_overflow */
1119          ppc64_elf_unhandled_reloc, /* special_function */
1120          "R_PPC64_PLTGOT16_HA", /* name */
1121          FALSE,                 /* partial_inplace */
1122          0,                     /* src_mask */
1123          0xffff,                /* dst_mask */
1124          FALSE),                /* pcrel_offset */
1125
1126   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
1127   HOWTO (R_PPC64_ADDR16_DS,     /* type */
1128          0,                     /* rightshift */
1129          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1130          16,                    /* bitsize */
1131          FALSE,                 /* pc_relative */
1132          0,                     /* bitpos */
1133          complain_overflow_signed, /* complain_on_overflow */
1134          bfd_elf_generic_reloc, /* special_function */
1135          "R_PPC64_ADDR16_DS",   /* name */
1136          FALSE,                 /* partial_inplace */
1137          0,                     /* src_mask */
1138          0xfffc,                /* dst_mask */
1139          FALSE),                /* pcrel_offset */
1140
1141   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
1142   HOWTO (R_PPC64_ADDR16_LO_DS,  /* type */
1143          0,                     /* rightshift */
1144          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1145          16,                    /* bitsize */
1146          FALSE,                 /* pc_relative */
1147          0,                     /* bitpos */
1148          complain_overflow_dont,/* complain_on_overflow */
1149          bfd_elf_generic_reloc, /* special_function */
1150          "R_PPC64_ADDR16_LO_DS",/* name */
1151          FALSE,                 /* partial_inplace */
1152          0,                     /* src_mask */
1153          0xfffc,                /* dst_mask */
1154          FALSE),                /* pcrel_offset */
1155
1156   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
1157   HOWTO (R_PPC64_GOT16_DS,      /* type */
1158          0,                     /* rightshift */
1159          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1160          16,                    /* bitsize */
1161          FALSE,                 /* pc_relative */
1162          0,                     /* bitpos */
1163          complain_overflow_signed, /* complain_on_overflow */
1164          ppc64_elf_unhandled_reloc, /* special_function */
1165          "R_PPC64_GOT16_DS",    /* name */
1166          FALSE,                 /* partial_inplace */
1167          0,                     /* src_mask */
1168          0xfffc,                /* dst_mask */
1169          FALSE),                /* pcrel_offset */
1170
1171   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
1172   HOWTO (R_PPC64_GOT16_LO_DS,   /* type */
1173          0,                     /* rightshift */
1174          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1175          16,                    /* bitsize */
1176          FALSE,                 /* pc_relative */
1177          0,                     /* bitpos */
1178          complain_overflow_dont, /* complain_on_overflow */
1179          ppc64_elf_unhandled_reloc, /* special_function */
1180          "R_PPC64_GOT16_LO_DS", /* name */
1181          FALSE,                 /* partial_inplace */
1182          0,                     /* src_mask */
1183          0xfffc,                /* dst_mask */
1184          FALSE),                /* pcrel_offset */
1185
1186   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
1187   HOWTO (R_PPC64_PLT16_LO_DS,   /* type */
1188          0,                     /* rightshift */
1189          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1190          16,                    /* bitsize */
1191          FALSE,                 /* pc_relative */
1192          0,                     /* bitpos */
1193          complain_overflow_dont, /* complain_on_overflow */
1194          ppc64_elf_unhandled_reloc, /* special_function */
1195          "R_PPC64_PLT16_LO_DS", /* name */
1196          FALSE,                 /* partial_inplace */
1197          0,                     /* src_mask */
1198          0xfffc,                /* dst_mask */
1199          FALSE),                /* pcrel_offset */
1200
1201   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
1202   HOWTO (R_PPC64_SECTOFF_DS,    /* type */
1203          0,                     /* rightshift */
1204          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1205          16,                    /* bitsize */
1206          FALSE,                 /* pc_relative */
1207          0,                     /* bitpos */
1208          complain_overflow_signed, /* complain_on_overflow */
1209          ppc64_elf_sectoff_reloc, /* special_function */
1210          "R_PPC64_SECTOFF_DS",  /* name */
1211          FALSE,                 /* partial_inplace */
1212          0,                     /* src_mask */
1213          0xfffc,                /* dst_mask */
1214          FALSE),                /* pcrel_offset */
1215
1216   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
1217   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1218          0,                     /* rightshift */
1219          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1220          16,                    /* bitsize */
1221          FALSE,                 /* pc_relative */
1222          0,                     /* bitpos */
1223          complain_overflow_dont, /* complain_on_overflow */
1224          ppc64_elf_sectoff_reloc, /* special_function */
1225          "R_PPC64_SECTOFF_LO_DS",/* name */
1226          FALSE,                 /* partial_inplace */
1227          0,                     /* src_mask */
1228          0xfffc,                /* dst_mask */
1229          FALSE),                /* pcrel_offset */
1230
1231   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
1232   HOWTO (R_PPC64_TOC16_DS,      /* type */
1233          0,                     /* rightshift */
1234          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1235          16,                    /* bitsize */
1236          FALSE,                 /* pc_relative */
1237          0,                     /* bitpos */
1238          complain_overflow_signed, /* complain_on_overflow */
1239          ppc64_elf_toc_reloc,   /* special_function */
1240          "R_PPC64_TOC16_DS",    /* name */
1241          FALSE,                 /* partial_inplace */
1242          0,                     /* src_mask */
1243          0xfffc,                /* dst_mask */
1244          FALSE),                /* pcrel_offset */
1245
1246   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
1247   HOWTO (R_PPC64_TOC16_LO_DS,   /* type */
1248          0,                     /* rightshift */
1249          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1250          16,                    /* bitsize */
1251          FALSE,                 /* pc_relative */
1252          0,                     /* bitpos */
1253          complain_overflow_dont, /* complain_on_overflow */
1254          ppc64_elf_toc_reloc,   /* special_function */
1255          "R_PPC64_TOC16_LO_DS", /* name */
1256          FALSE,                 /* partial_inplace */
1257          0,                     /* src_mask */
1258          0xfffc,                /* dst_mask */
1259          FALSE),                /* pcrel_offset */
1260
1261   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
1262   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
1263   HOWTO (R_PPC64_PLTGOT16_DS,   /* type */
1264          0,                     /* rightshift */
1265          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1266          16,                    /* bitsize */
1267          FALSE,                 /* pc_relative */
1268          0,                     /* bitpos */
1269          complain_overflow_signed, /* complain_on_overflow */
1270          ppc64_elf_unhandled_reloc, /* special_function */
1271          "R_PPC64_PLTGOT16_DS", /* name */
1272          FALSE,                 /* partial_inplace */
1273          0,                     /* src_mask */
1274          0xfffc,                /* dst_mask */
1275          FALSE),                /* pcrel_offset */
1276
1277   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
1278   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
1279   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1280          0,                     /* rightshift */
1281          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1282          16,                    /* bitsize */
1283          FALSE,                 /* pc_relative */
1284          0,                     /* bitpos */
1285          complain_overflow_dont, /* complain_on_overflow */
1286          ppc64_elf_unhandled_reloc, /* special_function */
1287          "R_PPC64_PLTGOT16_LO_DS",/* name */
1288          FALSE,                 /* partial_inplace */
1289          0,                     /* src_mask */
1290          0xfffc,                /* dst_mask */
1291          FALSE),                /* pcrel_offset */
1292
1293   /* Marker relocs for TLS.  */
1294   HOWTO (R_PPC64_TLS,
1295          0,                     /* rightshift */
1296          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1297          32,                    /* bitsize */
1298          FALSE,                 /* pc_relative */
1299          0,                     /* bitpos */
1300          complain_overflow_dont, /* complain_on_overflow */
1301          bfd_elf_generic_reloc, /* special_function */
1302          "R_PPC64_TLS",         /* name */
1303          FALSE,                 /* partial_inplace */
1304          0,                     /* src_mask */
1305          0,                     /* dst_mask */
1306          FALSE),                /* pcrel_offset */
1307
1308   HOWTO (R_PPC64_TLSGD,
1309          0,                     /* rightshift */
1310          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1311          32,                    /* bitsize */
1312          FALSE,                 /* pc_relative */
1313          0,                     /* bitpos */
1314          complain_overflow_dont, /* complain_on_overflow */
1315          bfd_elf_generic_reloc, /* special_function */
1316          "R_PPC64_TLSGD",       /* name */
1317          FALSE,                 /* partial_inplace */
1318          0,                     /* src_mask */
1319          0,                     /* dst_mask */
1320          FALSE),                /* pcrel_offset */
1321
1322   HOWTO (R_PPC64_TLSLD,
1323          0,                     /* rightshift */
1324          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1325          32,                    /* bitsize */
1326          FALSE,                 /* pc_relative */
1327          0,                     /* bitpos */
1328          complain_overflow_dont, /* complain_on_overflow */
1329          bfd_elf_generic_reloc, /* special_function */
1330          "R_PPC64_TLSLD",       /* name */
1331          FALSE,                 /* partial_inplace */
1332          0,                     /* src_mask */
1333          0,                     /* dst_mask */
1334          FALSE),                /* pcrel_offset */
1335
1336   HOWTO (R_PPC64_TOCSAVE,
1337          0,                     /* rightshift */
1338          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1339          32,                    /* bitsize */
1340          FALSE,                 /* pc_relative */
1341          0,                     /* bitpos */
1342          complain_overflow_dont, /* complain_on_overflow */
1343          bfd_elf_generic_reloc, /* special_function */
1344          "R_PPC64_TOCSAVE",     /* name */
1345          FALSE,                 /* partial_inplace */
1346          0,                     /* src_mask */
1347          0,                     /* dst_mask */
1348          FALSE),                /* pcrel_offset */
1349
1350   /* Computes the load module index of the load module that contains the
1351      definition of its TLS sym.  */
1352   HOWTO (R_PPC64_DTPMOD64,
1353          0,                     /* rightshift */
1354          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1355          64,                    /* bitsize */
1356          FALSE,                 /* pc_relative */
1357          0,                     /* bitpos */
1358          complain_overflow_dont, /* complain_on_overflow */
1359          ppc64_elf_unhandled_reloc, /* special_function */
1360          "R_PPC64_DTPMOD64",    /* name */
1361          FALSE,                 /* partial_inplace */
1362          0,                     /* src_mask */
1363          ONES (64),             /* dst_mask */
1364          FALSE),                /* pcrel_offset */
1365
1366   /* Computes a dtv-relative displacement, the difference between the value
1367      of sym+add and the base address of the thread-local storage block that
1368      contains the definition of sym, minus 0x8000.  */
1369   HOWTO (R_PPC64_DTPREL64,
1370          0,                     /* rightshift */
1371          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1372          64,                    /* bitsize */
1373          FALSE,                 /* pc_relative */
1374          0,                     /* bitpos */
1375          complain_overflow_dont, /* complain_on_overflow */
1376          ppc64_elf_unhandled_reloc, /* special_function */
1377          "R_PPC64_DTPREL64",    /* name */
1378          FALSE,                 /* partial_inplace */
1379          0,                     /* src_mask */
1380          ONES (64),             /* dst_mask */
1381          FALSE),                /* pcrel_offset */
1382
1383   /* A 16 bit dtprel reloc.  */
1384   HOWTO (R_PPC64_DTPREL16,
1385          0,                     /* rightshift */
1386          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1387          16,                    /* bitsize */
1388          FALSE,                 /* pc_relative */
1389          0,                     /* bitpos */
1390          complain_overflow_signed, /* complain_on_overflow */
1391          ppc64_elf_unhandled_reloc, /* special_function */
1392          "R_PPC64_DTPREL16",    /* name */
1393          FALSE,                 /* partial_inplace */
1394          0,                     /* src_mask */
1395          0xffff,                /* dst_mask */
1396          FALSE),                /* pcrel_offset */
1397
1398   /* Like DTPREL16, but no overflow.  */
1399   HOWTO (R_PPC64_DTPREL16_LO,
1400          0,                     /* rightshift */
1401          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1402          16,                    /* bitsize */
1403          FALSE,                 /* pc_relative */
1404          0,                     /* bitpos */
1405          complain_overflow_dont, /* complain_on_overflow */
1406          ppc64_elf_unhandled_reloc, /* special_function */
1407          "R_PPC64_DTPREL16_LO", /* name */
1408          FALSE,                 /* partial_inplace */
1409          0,                     /* src_mask */
1410          0xffff,                /* dst_mask */
1411          FALSE),                /* pcrel_offset */
1412
1413   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1414   HOWTO (R_PPC64_DTPREL16_HI,
1415          16,                    /* rightshift */
1416          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1417          16,                    /* bitsize */
1418          FALSE,                 /* pc_relative */
1419          0,                     /* bitpos */
1420          complain_overflow_signed, /* complain_on_overflow */
1421          ppc64_elf_unhandled_reloc, /* special_function */
1422          "R_PPC64_DTPREL16_HI", /* name */
1423          FALSE,                 /* partial_inplace */
1424          0,                     /* src_mask */
1425          0xffff,                /* dst_mask */
1426          FALSE),                /* pcrel_offset */
1427
1428   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1429   HOWTO (R_PPC64_DTPREL16_HA,
1430          16,                    /* rightshift */
1431          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1432          16,                    /* bitsize */
1433          FALSE,                 /* pc_relative */
1434          0,                     /* bitpos */
1435          complain_overflow_signed, /* complain_on_overflow */
1436          ppc64_elf_unhandled_reloc, /* special_function */
1437          "R_PPC64_DTPREL16_HA", /* name */
1438          FALSE,                 /* partial_inplace */
1439          0,                     /* src_mask */
1440          0xffff,                /* dst_mask */
1441          FALSE),                /* pcrel_offset */
1442
1443   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
1444   HOWTO (R_PPC64_DTPREL16_HIGHER,
1445          32,                    /* rightshift */
1446          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1447          16,                    /* bitsize */
1448          FALSE,                 /* pc_relative */
1449          0,                     /* bitpos */
1450          complain_overflow_dont, /* complain_on_overflow */
1451          ppc64_elf_unhandled_reloc, /* special_function */
1452          "R_PPC64_DTPREL16_HIGHER", /* name */
1453          FALSE,                 /* partial_inplace */
1454          0,                     /* src_mask */
1455          0xffff,                /* dst_mask */
1456          FALSE),                /* pcrel_offset */
1457
1458   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
1459   HOWTO (R_PPC64_DTPREL16_HIGHERA,
1460          32,                    /* rightshift */
1461          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1462          16,                    /* bitsize */
1463          FALSE,                 /* pc_relative */
1464          0,                     /* bitpos */
1465          complain_overflow_dont, /* complain_on_overflow */
1466          ppc64_elf_unhandled_reloc, /* special_function */
1467          "R_PPC64_DTPREL16_HIGHERA", /* name */
1468          FALSE,                 /* partial_inplace */
1469          0,                     /* src_mask */
1470          0xffff,                /* dst_mask */
1471          FALSE),                /* pcrel_offset */
1472
1473   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
1474   HOWTO (R_PPC64_DTPREL16_HIGHEST,
1475          48,                    /* rightshift */
1476          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1477          16,                    /* bitsize */
1478          FALSE,                 /* pc_relative */
1479          0,                     /* bitpos */
1480          complain_overflow_dont, /* complain_on_overflow */
1481          ppc64_elf_unhandled_reloc, /* special_function */
1482          "R_PPC64_DTPREL16_HIGHEST", /* name */
1483          FALSE,                 /* partial_inplace */
1484          0,                     /* src_mask */
1485          0xffff,                /* dst_mask */
1486          FALSE),                /* pcrel_offset */
1487
1488   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
1489   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1490          48,                    /* rightshift */
1491          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1492          16,                    /* bitsize */
1493          FALSE,                 /* pc_relative */
1494          0,                     /* bitpos */
1495          complain_overflow_dont, /* complain_on_overflow */
1496          ppc64_elf_unhandled_reloc, /* special_function */
1497          "R_PPC64_DTPREL16_HIGHESTA", /* name */
1498          FALSE,                 /* partial_inplace */
1499          0,                     /* src_mask */
1500          0xffff,                /* dst_mask */
1501          FALSE),                /* pcrel_offset */
1502
1503   /* Like DTPREL16, but for insns with a DS field.  */
1504   HOWTO (R_PPC64_DTPREL16_DS,
1505          0,                     /* rightshift */
1506          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1507          16,                    /* bitsize */
1508          FALSE,                 /* pc_relative */
1509          0,                     /* bitpos */
1510          complain_overflow_signed, /* complain_on_overflow */
1511          ppc64_elf_unhandled_reloc, /* special_function */
1512          "R_PPC64_DTPREL16_DS", /* name */
1513          FALSE,                 /* partial_inplace */
1514          0,                     /* src_mask */
1515          0xfffc,                /* dst_mask */
1516          FALSE),                /* pcrel_offset */
1517
1518   /* Like DTPREL16_DS, but no overflow.  */
1519   HOWTO (R_PPC64_DTPREL16_LO_DS,
1520          0,                     /* rightshift */
1521          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1522          16,                    /* bitsize */
1523          FALSE,                 /* pc_relative */
1524          0,                     /* bitpos */
1525          complain_overflow_dont, /* complain_on_overflow */
1526          ppc64_elf_unhandled_reloc, /* special_function */
1527          "R_PPC64_DTPREL16_LO_DS", /* name */
1528          FALSE,                 /* partial_inplace */
1529          0,                     /* src_mask */
1530          0xfffc,                /* dst_mask */
1531          FALSE),                /* pcrel_offset */
1532
1533   /* Computes a tp-relative displacement, the difference between the value of
1534      sym+add and the value of the thread pointer (r13).  */
1535   HOWTO (R_PPC64_TPREL64,
1536          0,                     /* rightshift */
1537          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1538          64,                    /* bitsize */
1539          FALSE,                 /* pc_relative */
1540          0,                     /* bitpos */
1541          complain_overflow_dont, /* complain_on_overflow */
1542          ppc64_elf_unhandled_reloc, /* special_function */
1543          "R_PPC64_TPREL64",     /* name */
1544          FALSE,                 /* partial_inplace */
1545          0,                     /* src_mask */
1546          ONES (64),             /* dst_mask */
1547          FALSE),                /* pcrel_offset */
1548
1549   /* A 16 bit tprel reloc.  */
1550   HOWTO (R_PPC64_TPREL16,
1551          0,                     /* rightshift */
1552          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1553          16,                    /* bitsize */
1554          FALSE,                 /* pc_relative */
1555          0,                     /* bitpos */
1556          complain_overflow_signed, /* complain_on_overflow */
1557          ppc64_elf_unhandled_reloc, /* special_function */
1558          "R_PPC64_TPREL16",     /* name */
1559          FALSE,                 /* partial_inplace */
1560          0,                     /* src_mask */
1561          0xffff,                /* dst_mask */
1562          FALSE),                /* pcrel_offset */
1563
1564   /* Like TPREL16, but no overflow.  */
1565   HOWTO (R_PPC64_TPREL16_LO,
1566          0,                     /* rightshift */
1567          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1568          16,                    /* bitsize */
1569          FALSE,                 /* pc_relative */
1570          0,                     /* bitpos */
1571          complain_overflow_dont, /* complain_on_overflow */
1572          ppc64_elf_unhandled_reloc, /* special_function */
1573          "R_PPC64_TPREL16_LO",  /* name */
1574          FALSE,                 /* partial_inplace */
1575          0,                     /* src_mask */
1576          0xffff,                /* dst_mask */
1577          FALSE),                /* pcrel_offset */
1578
1579   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1580   HOWTO (R_PPC64_TPREL16_HI,
1581          16,                    /* rightshift */
1582          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1583          16,                    /* bitsize */
1584          FALSE,                 /* pc_relative */
1585          0,                     /* bitpos */
1586          complain_overflow_signed, /* complain_on_overflow */
1587          ppc64_elf_unhandled_reloc, /* special_function */
1588          "R_PPC64_TPREL16_HI",  /* name */
1589          FALSE,                 /* partial_inplace */
1590          0,                     /* src_mask */
1591          0xffff,                /* dst_mask */
1592          FALSE),                /* pcrel_offset */
1593
1594   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1595   HOWTO (R_PPC64_TPREL16_HA,
1596          16,                    /* rightshift */
1597          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1598          16,                    /* bitsize */
1599          FALSE,                 /* pc_relative */
1600          0,                     /* bitpos */
1601          complain_overflow_signed, /* complain_on_overflow */
1602          ppc64_elf_unhandled_reloc, /* special_function */
1603          "R_PPC64_TPREL16_HA",  /* name */
1604          FALSE,                 /* partial_inplace */
1605          0,                     /* src_mask */
1606          0xffff,                /* dst_mask */
1607          FALSE),                /* pcrel_offset */
1608
1609   /* Like TPREL16_HI, but next higher group of 16 bits.  */
1610   HOWTO (R_PPC64_TPREL16_HIGHER,
1611          32,                    /* rightshift */
1612          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1613          16,                    /* bitsize */
1614          FALSE,                 /* pc_relative */
1615          0,                     /* bitpos */
1616          complain_overflow_dont, /* complain_on_overflow */
1617          ppc64_elf_unhandled_reloc, /* special_function */
1618          "R_PPC64_TPREL16_HIGHER",      /* name */
1619          FALSE,                 /* partial_inplace */
1620          0,                     /* src_mask */
1621          0xffff,                /* dst_mask */
1622          FALSE),                /* pcrel_offset */
1623
1624   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
1625   HOWTO (R_PPC64_TPREL16_HIGHERA,
1626          32,                    /* rightshift */
1627          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1628          16,                    /* bitsize */
1629          FALSE,                 /* pc_relative */
1630          0,                     /* bitpos */
1631          complain_overflow_dont, /* complain_on_overflow */
1632          ppc64_elf_unhandled_reloc, /* special_function */
1633          "R_PPC64_TPREL16_HIGHERA", /* name */
1634          FALSE,                 /* partial_inplace */
1635          0,                     /* src_mask */
1636          0xffff,                /* dst_mask */
1637          FALSE),                /* pcrel_offset */
1638
1639   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
1640   HOWTO (R_PPC64_TPREL16_HIGHEST,
1641          48,                    /* rightshift */
1642          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1643          16,                    /* bitsize */
1644          FALSE,                 /* pc_relative */
1645          0,                     /* bitpos */
1646          complain_overflow_dont, /* complain_on_overflow */
1647          ppc64_elf_unhandled_reloc, /* special_function */
1648          "R_PPC64_TPREL16_HIGHEST", /* name */
1649          FALSE,                 /* partial_inplace */
1650          0,                     /* src_mask */
1651          0xffff,                /* dst_mask */
1652          FALSE),                /* pcrel_offset */
1653
1654   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
1655   HOWTO (R_PPC64_TPREL16_HIGHESTA,
1656          48,                    /* rightshift */
1657          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1658          16,                    /* bitsize */
1659          FALSE,                 /* pc_relative */
1660          0,                     /* bitpos */
1661          complain_overflow_dont, /* complain_on_overflow */
1662          ppc64_elf_unhandled_reloc, /* special_function */
1663          "R_PPC64_TPREL16_HIGHESTA", /* name */
1664          FALSE,                 /* partial_inplace */
1665          0,                     /* src_mask */
1666          0xffff,                /* dst_mask */
1667          FALSE),                /* pcrel_offset */
1668
1669   /* Like TPREL16, but for insns with a DS field.  */
1670   HOWTO (R_PPC64_TPREL16_DS,
1671          0,                     /* rightshift */
1672          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1673          16,                    /* bitsize */
1674          FALSE,                 /* pc_relative */
1675          0,                     /* bitpos */
1676          complain_overflow_signed, /* complain_on_overflow */
1677          ppc64_elf_unhandled_reloc, /* special_function */
1678          "R_PPC64_TPREL16_DS",  /* name */
1679          FALSE,                 /* partial_inplace */
1680          0,                     /* src_mask */
1681          0xfffc,                /* dst_mask */
1682          FALSE),                /* pcrel_offset */
1683
1684   /* Like TPREL16_DS, but no overflow.  */
1685   HOWTO (R_PPC64_TPREL16_LO_DS,
1686          0,                     /* rightshift */
1687          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1688          16,                    /* bitsize */
1689          FALSE,                 /* pc_relative */
1690          0,                     /* bitpos */
1691          complain_overflow_dont, /* complain_on_overflow */
1692          ppc64_elf_unhandled_reloc, /* special_function */
1693          "R_PPC64_TPREL16_LO_DS", /* name */
1694          FALSE,                 /* partial_inplace */
1695          0,                     /* src_mask */
1696          0xfffc,                /* dst_mask */
1697          FALSE),                /* pcrel_offset */
1698
1699   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1700      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1701      to the first entry relative to the TOC base (r2).  */
1702   HOWTO (R_PPC64_GOT_TLSGD16,
1703          0,                     /* rightshift */
1704          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1705          16,                    /* bitsize */
1706          FALSE,                 /* pc_relative */
1707          0,                     /* bitpos */
1708          complain_overflow_signed, /* complain_on_overflow */
1709          ppc64_elf_unhandled_reloc, /* special_function */
1710          "R_PPC64_GOT_TLSGD16", /* name */
1711          FALSE,                 /* partial_inplace */
1712          0,                     /* src_mask */
1713          0xffff,                /* dst_mask */
1714          FALSE),                /* pcrel_offset */
1715
1716   /* Like GOT_TLSGD16, but no overflow.  */
1717   HOWTO (R_PPC64_GOT_TLSGD16_LO,
1718          0,                     /* rightshift */
1719          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1720          16,                    /* bitsize */
1721          FALSE,                 /* pc_relative */
1722          0,                     /* bitpos */
1723          complain_overflow_dont, /* complain_on_overflow */
1724          ppc64_elf_unhandled_reloc, /* special_function */
1725          "R_PPC64_GOT_TLSGD16_LO", /* name */
1726          FALSE,                 /* partial_inplace */
1727          0,                     /* src_mask */
1728          0xffff,                /* dst_mask */
1729          FALSE),                /* pcrel_offset */
1730
1731   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1732   HOWTO (R_PPC64_GOT_TLSGD16_HI,
1733          16,                    /* rightshift */
1734          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1735          16,                    /* bitsize */
1736          FALSE,                 /* pc_relative */
1737          0,                     /* bitpos */
1738          complain_overflow_signed, /* complain_on_overflow */
1739          ppc64_elf_unhandled_reloc, /* special_function */
1740          "R_PPC64_GOT_TLSGD16_HI", /* name */
1741          FALSE,                 /* partial_inplace */
1742          0,                     /* src_mask */
1743          0xffff,                /* dst_mask */
1744          FALSE),                /* pcrel_offset */
1745
1746   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1747   HOWTO (R_PPC64_GOT_TLSGD16_HA,
1748          16,                    /* rightshift */
1749          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1750          16,                    /* bitsize */
1751          FALSE,                 /* pc_relative */
1752          0,                     /* bitpos */
1753          complain_overflow_signed, /* complain_on_overflow */
1754          ppc64_elf_unhandled_reloc, /* special_function */
1755          "R_PPC64_GOT_TLSGD16_HA", /* name */
1756          FALSE,                 /* partial_inplace */
1757          0,                     /* src_mask */
1758          0xffff,                /* dst_mask */
1759          FALSE),                /* pcrel_offset */
1760
1761   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1762      with values (sym+add)@dtpmod and zero, and computes the offset to the
1763      first entry relative to the TOC base (r2).  */
1764   HOWTO (R_PPC64_GOT_TLSLD16,
1765          0,                     /* rightshift */
1766          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1767          16,                    /* bitsize */
1768          FALSE,                 /* pc_relative */
1769          0,                     /* bitpos */
1770          complain_overflow_signed, /* complain_on_overflow */
1771          ppc64_elf_unhandled_reloc, /* special_function */
1772          "R_PPC64_GOT_TLSLD16", /* name */
1773          FALSE,                 /* partial_inplace */
1774          0,                     /* src_mask */
1775          0xffff,                /* dst_mask */
1776          FALSE),                /* pcrel_offset */
1777
1778   /* Like GOT_TLSLD16, but no overflow.  */
1779   HOWTO (R_PPC64_GOT_TLSLD16_LO,
1780          0,                     /* rightshift */
1781          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1782          16,                    /* bitsize */
1783          FALSE,                 /* pc_relative */
1784          0,                     /* bitpos */
1785          complain_overflow_dont, /* complain_on_overflow */
1786          ppc64_elf_unhandled_reloc, /* special_function */
1787          "R_PPC64_GOT_TLSLD16_LO", /* name */
1788          FALSE,                 /* partial_inplace */
1789          0,                     /* src_mask */
1790          0xffff,                /* dst_mask */
1791          FALSE),                /* pcrel_offset */
1792
1793   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1794   HOWTO (R_PPC64_GOT_TLSLD16_HI,
1795          16,                    /* rightshift */
1796          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1797          16,                    /* bitsize */
1798          FALSE,                 /* pc_relative */
1799          0,                     /* bitpos */
1800          complain_overflow_signed, /* complain_on_overflow */
1801          ppc64_elf_unhandled_reloc, /* special_function */
1802          "R_PPC64_GOT_TLSLD16_HI", /* name */
1803          FALSE,                 /* partial_inplace */
1804          0,                     /* src_mask */
1805          0xffff,                /* dst_mask */
1806          FALSE),                /* pcrel_offset */
1807
1808   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1809   HOWTO (R_PPC64_GOT_TLSLD16_HA,
1810          16,                    /* rightshift */
1811          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1812          16,                    /* bitsize */
1813          FALSE,                 /* pc_relative */
1814          0,                     /* bitpos */
1815          complain_overflow_signed, /* complain_on_overflow */
1816          ppc64_elf_unhandled_reloc, /* special_function */
1817          "R_PPC64_GOT_TLSLD16_HA", /* name */
1818          FALSE,                 /* partial_inplace */
1819          0,                     /* src_mask */
1820          0xffff,                /* dst_mask */
1821          FALSE),                /* pcrel_offset */
1822
1823   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1824      the offset to the entry relative to the TOC base (r2).  */
1825   HOWTO (R_PPC64_GOT_DTPREL16_DS,
1826          0,                     /* rightshift */
1827          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1828          16,                    /* bitsize */
1829          FALSE,                 /* pc_relative */
1830          0,                     /* bitpos */
1831          complain_overflow_signed, /* complain_on_overflow */
1832          ppc64_elf_unhandled_reloc, /* special_function */
1833          "R_PPC64_GOT_DTPREL16_DS", /* name */
1834          FALSE,                 /* partial_inplace */
1835          0,                     /* src_mask */
1836          0xfffc,                /* dst_mask */
1837          FALSE),                /* pcrel_offset */
1838
1839   /* Like GOT_DTPREL16_DS, but no overflow.  */
1840   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1841          0,                     /* rightshift */
1842          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1843          16,                    /* bitsize */
1844          FALSE,                 /* pc_relative */
1845          0,                     /* bitpos */
1846          complain_overflow_dont, /* complain_on_overflow */
1847          ppc64_elf_unhandled_reloc, /* special_function */
1848          "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1849          FALSE,                 /* partial_inplace */
1850          0,                     /* src_mask */
1851          0xfffc,                /* dst_mask */
1852          FALSE),                /* pcrel_offset */
1853
1854   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
1855   HOWTO (R_PPC64_GOT_DTPREL16_HI,
1856          16,                    /* rightshift */
1857          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1858          16,                    /* bitsize */
1859          FALSE,                 /* pc_relative */
1860          0,                     /* bitpos */
1861          complain_overflow_signed, /* complain_on_overflow */
1862          ppc64_elf_unhandled_reloc, /* special_function */
1863          "R_PPC64_GOT_DTPREL16_HI", /* name */
1864          FALSE,                 /* partial_inplace */
1865          0,                     /* src_mask */
1866          0xffff,                /* dst_mask */
1867          FALSE),                /* pcrel_offset */
1868
1869   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1870   HOWTO (R_PPC64_GOT_DTPREL16_HA,
1871          16,                    /* rightshift */
1872          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1873          16,                    /* bitsize */
1874          FALSE,                 /* pc_relative */
1875          0,                     /* bitpos */
1876          complain_overflow_signed, /* complain_on_overflow */
1877          ppc64_elf_unhandled_reloc, /* special_function */
1878          "R_PPC64_GOT_DTPREL16_HA", /* name */
1879          FALSE,                 /* partial_inplace */
1880          0,                     /* src_mask */
1881          0xffff,                /* dst_mask */
1882          FALSE),                /* pcrel_offset */
1883
1884   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1885      offset to the entry relative to the TOC base (r2).  */
1886   HOWTO (R_PPC64_GOT_TPREL16_DS,
1887          0,                     /* rightshift */
1888          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1889          16,                    /* bitsize */
1890          FALSE,                 /* pc_relative */
1891          0,                     /* bitpos */
1892          complain_overflow_signed, /* complain_on_overflow */
1893          ppc64_elf_unhandled_reloc, /* special_function */
1894          "R_PPC64_GOT_TPREL16_DS", /* name */
1895          FALSE,                 /* partial_inplace */
1896          0,                     /* src_mask */
1897          0xfffc,                /* dst_mask */
1898          FALSE),                /* pcrel_offset */
1899
1900   /* Like GOT_TPREL16_DS, but no overflow.  */
1901   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1902          0,                     /* rightshift */
1903          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1904          16,                    /* bitsize */
1905          FALSE,                 /* pc_relative */
1906          0,                     /* bitpos */
1907          complain_overflow_dont, /* complain_on_overflow */
1908          ppc64_elf_unhandled_reloc, /* special_function */
1909          "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1910          FALSE,                 /* partial_inplace */
1911          0,                     /* src_mask */
1912          0xfffc,                /* dst_mask */
1913          FALSE),                /* pcrel_offset */
1914
1915   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
1916   HOWTO (R_PPC64_GOT_TPREL16_HI,
1917          16,                    /* rightshift */
1918          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1919          16,                    /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_signed, /* complain_on_overflow */
1923          ppc64_elf_unhandled_reloc, /* special_function */
1924          "R_PPC64_GOT_TPREL16_HI", /* name */
1925          FALSE,                 /* partial_inplace */
1926          0,                     /* src_mask */
1927          0xffff,                /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1931   HOWTO (R_PPC64_GOT_TPREL16_HA,
1932          16,                    /* rightshift */
1933          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1934          16,                    /* bitsize */
1935          FALSE,                 /* pc_relative */
1936          0,                     /* bitpos */
1937          complain_overflow_signed, /* complain_on_overflow */
1938          ppc64_elf_unhandled_reloc, /* special_function */
1939          "R_PPC64_GOT_TPREL16_HA", /* name */
1940          FALSE,                 /* partial_inplace */
1941          0,                     /* src_mask */
1942          0xffff,                /* dst_mask */
1943          FALSE),                /* pcrel_offset */
1944
1945   HOWTO (R_PPC64_JMP_IREL,      /* type */
1946          0,                     /* rightshift */
1947          0,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1948          0,                     /* bitsize */
1949          FALSE,                 /* pc_relative */
1950          0,                     /* bitpos */
1951          complain_overflow_dont, /* complain_on_overflow */
1952          ppc64_elf_unhandled_reloc, /* special_function */
1953          "R_PPC64_JMP_IREL",    /* name */
1954          FALSE,                 /* partial_inplace */
1955          0,                     /* src_mask */
1956          0,                     /* dst_mask */
1957          FALSE),                /* pcrel_offset */
1958
1959   HOWTO (R_PPC64_IRELATIVE,     /* type */
1960          0,                     /* rightshift */
1961          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1962          64,                    /* bitsize */
1963          FALSE,                 /* pc_relative */
1964          0,                     /* bitpos */
1965          complain_overflow_dont, /* complain_on_overflow */
1966          bfd_elf_generic_reloc, /* special_function */
1967          "R_PPC64_IRELATIVE",   /* name */
1968          FALSE,                 /* partial_inplace */
1969          0,                     /* src_mask */
1970          ONES (64),             /* dst_mask */
1971          FALSE),                /* pcrel_offset */
1972
1973   /* A 16 bit relative relocation.  */
1974   HOWTO (R_PPC64_REL16,         /* type */
1975          0,                     /* rightshift */
1976          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1977          16,                    /* bitsize */
1978          TRUE,                  /* pc_relative */
1979          0,                     /* bitpos */
1980          complain_overflow_signed, /* complain_on_overflow */
1981          bfd_elf_generic_reloc, /* special_function */
1982          "R_PPC64_REL16",       /* name */
1983          FALSE,                 /* partial_inplace */
1984          0,                     /* src_mask */
1985          0xffff,                /* dst_mask */
1986          TRUE),                 /* pcrel_offset */
1987
1988   /* A 16 bit relative relocation without overflow.  */
1989   HOWTO (R_PPC64_REL16_LO,      /* type */
1990          0,                     /* rightshift */
1991          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1992          16,                    /* bitsize */
1993          TRUE,                  /* pc_relative */
1994          0,                     /* bitpos */
1995          complain_overflow_dont,/* complain_on_overflow */
1996          bfd_elf_generic_reloc, /* special_function */
1997          "R_PPC64_REL16_LO",    /* name */
1998          FALSE,                 /* partial_inplace */
1999          0,                     /* src_mask */
2000          0xffff,                /* dst_mask */
2001          TRUE),                 /* pcrel_offset */
2002
2003   /* The high order 16 bits of a relative address.  */
2004   HOWTO (R_PPC64_REL16_HI,      /* type */
2005          16,                    /* rightshift */
2006          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2007          16,                    /* bitsize */
2008          TRUE,                  /* pc_relative */
2009          0,                     /* bitpos */
2010          complain_overflow_signed, /* complain_on_overflow */
2011          bfd_elf_generic_reloc, /* special_function */
2012          "R_PPC64_REL16_HI",    /* name */
2013          FALSE,                 /* partial_inplace */
2014          0,                     /* src_mask */
2015          0xffff,                /* dst_mask */
2016          TRUE),                 /* pcrel_offset */
2017
2018   /* The high order 16 bits of a relative address, plus 1 if the contents of
2019      the low 16 bits, treated as a signed number, is negative.  */
2020   HOWTO (R_PPC64_REL16_HA,      /* type */
2021          16,                    /* rightshift */
2022          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2023          16,                    /* bitsize */
2024          TRUE,                  /* pc_relative */
2025          0,                     /* bitpos */
2026          complain_overflow_signed, /* complain_on_overflow */
2027          ppc64_elf_ha_reloc,    /* special_function */
2028          "R_PPC64_REL16_HA",    /* name */
2029          FALSE,                 /* partial_inplace */
2030          0,                     /* src_mask */
2031          0xffff,                /* dst_mask */
2032          TRUE),                 /* pcrel_offset */
2033
2034   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
2035   HOWTO (R_PPC64_REL16DX_HA,    /* type */
2036          16,                    /* rightshift */
2037          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2038          16,                    /* bitsize */
2039          TRUE,                  /* pc_relative */
2040          0,                     /* bitpos */
2041          complain_overflow_signed, /* complain_on_overflow */
2042          ppc64_elf_ha_reloc,    /* special_function */
2043          "R_PPC64_REL16DX_HA",  /* name */
2044          FALSE,                 /* partial_inplace */
2045          0,                     /* src_mask */
2046          0x1fffc1,              /* dst_mask */
2047          TRUE),                 /* pcrel_offset */
2048
2049   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
2050   HOWTO (R_PPC64_16DX_HA,       /* type */
2051          16,                    /* rightshift */
2052          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2053          16,                    /* bitsize */
2054          FALSE,                 /* pc_relative */
2055          0,                     /* bitpos */
2056          complain_overflow_signed, /* complain_on_overflow */
2057          ppc64_elf_ha_reloc,    /* special_function */
2058          "R_PPC64_16DX_HA",     /* name */
2059          FALSE,                 /* partial_inplace */
2060          0,                     /* src_mask */
2061          0x1fffc1,              /* dst_mask */
2062          FALSE),                /* pcrel_offset */
2063
2064   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
2065   HOWTO (R_PPC64_ADDR16_HIGH,   /* type */
2066          16,                    /* rightshift */
2067          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2068          16,                    /* bitsize */
2069          FALSE,                 /* pc_relative */
2070          0,                     /* bitpos */
2071          complain_overflow_dont, /* complain_on_overflow */
2072          bfd_elf_generic_reloc, /* special_function */
2073          "R_PPC64_ADDR16_HIGH", /* name */
2074          FALSE,                 /* partial_inplace */
2075          0,                     /* src_mask */
2076          0xffff,                /* dst_mask */
2077          FALSE),                /* pcrel_offset */
2078
2079   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
2080   HOWTO (R_PPC64_ADDR16_HIGHA,  /* type */
2081          16,                    /* rightshift */
2082          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2083          16,                    /* bitsize */
2084          FALSE,                 /* pc_relative */
2085          0,                     /* bitpos */
2086          complain_overflow_dont, /* complain_on_overflow */
2087          ppc64_elf_ha_reloc,    /* special_function */
2088          "R_PPC64_ADDR16_HIGHA",        /* name */
2089          FALSE,                 /* partial_inplace */
2090          0,                     /* src_mask */
2091          0xffff,                /* dst_mask */
2092          FALSE),                /* pcrel_offset */
2093
2094   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
2095   HOWTO (R_PPC64_DTPREL16_HIGH,
2096          16,                    /* rightshift */
2097          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2098          16,                    /* bitsize */
2099          FALSE,                 /* pc_relative */
2100          0,                     /* bitpos */
2101          complain_overflow_dont, /* complain_on_overflow */
2102          ppc64_elf_unhandled_reloc, /* special_function */
2103          "R_PPC64_DTPREL16_HIGH", /* name */
2104          FALSE,                 /* partial_inplace */
2105          0,                     /* src_mask */
2106          0xffff,                /* dst_mask */
2107          FALSE),                /* pcrel_offset */
2108
2109   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
2110   HOWTO (R_PPC64_DTPREL16_HIGHA,
2111          16,                    /* rightshift */
2112          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2113          16,                    /* bitsize */
2114          FALSE,                 /* pc_relative */
2115          0,                     /* bitpos */
2116          complain_overflow_dont, /* complain_on_overflow */
2117          ppc64_elf_unhandled_reloc, /* special_function */
2118          "R_PPC64_DTPREL16_HIGHA", /* name */
2119          FALSE,                 /* partial_inplace */
2120          0,                     /* src_mask */
2121          0xffff,                /* dst_mask */
2122          FALSE),                /* pcrel_offset */
2123
2124   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
2125   HOWTO (R_PPC64_TPREL16_HIGH,
2126          16,                    /* rightshift */
2127          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2128          16,                    /* bitsize */
2129          FALSE,                 /* pc_relative */
2130          0,                     /* bitpos */
2131          complain_overflow_dont, /* complain_on_overflow */
2132          ppc64_elf_unhandled_reloc, /* special_function */
2133          "R_PPC64_TPREL16_HIGH",        /* name */
2134          FALSE,                 /* partial_inplace */
2135          0,                     /* src_mask */
2136          0xffff,                /* dst_mask */
2137          FALSE),                /* pcrel_offset */
2138
2139   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
2140   HOWTO (R_PPC64_TPREL16_HIGHA,
2141          16,                    /* rightshift */
2142          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2143          16,                    /* bitsize */
2144          FALSE,                 /* pc_relative */
2145          0,                     /* bitpos */
2146          complain_overflow_dont, /* complain_on_overflow */
2147          ppc64_elf_unhandled_reloc, /* special_function */
2148          "R_PPC64_TPREL16_HIGHA",       /* name */
2149          FALSE,                 /* partial_inplace */
2150          0,                     /* src_mask */
2151          0xffff,                /* dst_mask */
2152          FALSE),                /* pcrel_offset */
2153
2154   /* Marker reloc on ELFv2 large-model function entry.  */
2155   HOWTO (R_PPC64_ENTRY,
2156          0,                     /* rightshift */
2157          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2158          32,                    /* bitsize */
2159          FALSE,                 /* pc_relative */
2160          0,                     /* bitpos */
2161          complain_overflow_dont, /* complain_on_overflow */
2162          bfd_elf_generic_reloc, /* special_function */
2163          "R_PPC64_ENTRY",       /* name */
2164          FALSE,                 /* partial_inplace */
2165          0,                     /* src_mask */
2166          0,                     /* dst_mask */
2167          FALSE),                /* pcrel_offset */
2168
2169   /* Like ADDR64, but use local entry point of function.  */
2170   HOWTO (R_PPC64_ADDR64_LOCAL,  /* type */
2171          0,                     /* rightshift */
2172          4,                     /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2173          64,                    /* bitsize */
2174          FALSE,                 /* pc_relative */
2175          0,                     /* bitpos */
2176          complain_overflow_dont, /* complain_on_overflow */
2177          bfd_elf_generic_reloc, /* special_function */
2178          "R_PPC64_ADDR64_LOCAL", /* name */
2179          FALSE,                 /* partial_inplace */
2180          0,                     /* src_mask */
2181          ONES (64),             /* dst_mask */
2182          FALSE),                /* pcrel_offset */
2183
2184   /* GNU extension to record C++ vtable hierarchy.  */
2185   HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2186          0,                     /* rightshift */
2187          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2188          0,                     /* bitsize */
2189          FALSE,                 /* pc_relative */
2190          0,                     /* bitpos */
2191          complain_overflow_dont, /* complain_on_overflow */
2192          NULL,                  /* special_function */
2193          "R_PPC64_GNU_VTINHERIT", /* name */
2194          FALSE,                 /* partial_inplace */
2195          0,                     /* src_mask */
2196          0,                     /* dst_mask */
2197          FALSE),                /* pcrel_offset */
2198
2199   /* GNU extension to record C++ vtable member usage.  */
2200   HOWTO (R_PPC64_GNU_VTENTRY,   /* type */
2201          0,                     /* rightshift */
2202          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2203          0,                     /* bitsize */
2204          FALSE,                 /* pc_relative */
2205          0,                     /* bitpos */
2206          complain_overflow_dont, /* complain_on_overflow */
2207          NULL,                  /* special_function */
2208          "R_PPC64_GNU_VTENTRY", /* name */
2209          FALSE,                 /* partial_inplace */
2210          0,                     /* src_mask */
2211          0,                     /* dst_mask */
2212          FALSE),                /* pcrel_offset */
2213 };
2214
2215 \f
2216 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2217    be done.  */
2218
2219 static void
2220 ppc_howto_init (void)
2221 {
2222   unsigned int i, type;
2223
2224   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2225     {
2226       type = ppc64_elf_howto_raw[i].type;
2227       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2228       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2229     }
2230 }
2231
2232 static reloc_howto_type *
2233 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2234                              bfd_reloc_code_real_type code)
2235 {
2236   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2237
2238   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2239     /* Initialize howto table if needed.  */
2240     ppc_howto_init ();
2241
2242   switch (code)
2243     {
2244     default:
2245       return NULL;
2246
2247     case BFD_RELOC_NONE:                        r = R_PPC64_NONE;
2248       break;
2249     case BFD_RELOC_32:                          r = R_PPC64_ADDR32;
2250       break;
2251     case BFD_RELOC_PPC_BA26:                    r = R_PPC64_ADDR24;
2252       break;
2253     case BFD_RELOC_16:                          r = R_PPC64_ADDR16;
2254       break;
2255     case BFD_RELOC_LO16:                        r = R_PPC64_ADDR16_LO;
2256       break;
2257     case BFD_RELOC_HI16:                        r = R_PPC64_ADDR16_HI;
2258       break;
2259     case BFD_RELOC_PPC64_ADDR16_HIGH:           r = R_PPC64_ADDR16_HIGH;
2260       break;
2261     case BFD_RELOC_HI16_S:                      r = R_PPC64_ADDR16_HA;
2262       break;
2263     case BFD_RELOC_PPC64_ADDR16_HIGHA:          r = R_PPC64_ADDR16_HIGHA;
2264       break;
2265     case BFD_RELOC_PPC_BA16:                    r = R_PPC64_ADDR14;
2266       break;
2267     case BFD_RELOC_PPC_BA16_BRTAKEN:            r = R_PPC64_ADDR14_BRTAKEN;
2268       break;
2269     case BFD_RELOC_PPC_BA16_BRNTAKEN:           r = R_PPC64_ADDR14_BRNTAKEN;
2270       break;
2271     case BFD_RELOC_PPC_B26:                     r = R_PPC64_REL24;
2272       break;
2273     case BFD_RELOC_PPC_B16:                     r = R_PPC64_REL14;
2274       break;
2275     case BFD_RELOC_PPC_B16_BRTAKEN:             r = R_PPC64_REL14_BRTAKEN;
2276       break;
2277     case BFD_RELOC_PPC_B16_BRNTAKEN:            r = R_PPC64_REL14_BRNTAKEN;
2278       break;
2279     case BFD_RELOC_16_GOTOFF:                   r = R_PPC64_GOT16;
2280       break;
2281     case BFD_RELOC_LO16_GOTOFF:                 r = R_PPC64_GOT16_LO;
2282       break;
2283     case BFD_RELOC_HI16_GOTOFF:                 r = R_PPC64_GOT16_HI;
2284       break;
2285     case BFD_RELOC_HI16_S_GOTOFF:               r = R_PPC64_GOT16_HA;
2286       break;
2287     case BFD_RELOC_PPC_COPY:                    r = R_PPC64_COPY;
2288       break;
2289     case BFD_RELOC_PPC_GLOB_DAT:                r = R_PPC64_GLOB_DAT;
2290       break;
2291     case BFD_RELOC_32_PCREL:                    r = R_PPC64_REL32;
2292       break;
2293     case BFD_RELOC_32_PLTOFF:                   r = R_PPC64_PLT32;
2294       break;
2295     case BFD_RELOC_32_PLT_PCREL:                r = R_PPC64_PLTREL32;
2296       break;
2297     case BFD_RELOC_LO16_PLTOFF:                 r = R_PPC64_PLT16_LO;
2298       break;
2299     case BFD_RELOC_HI16_PLTOFF:                 r = R_PPC64_PLT16_HI;
2300       break;
2301     case BFD_RELOC_HI16_S_PLTOFF:               r = R_PPC64_PLT16_HA;
2302       break;
2303     case BFD_RELOC_16_BASEREL:                  r = R_PPC64_SECTOFF;
2304       break;
2305     case BFD_RELOC_LO16_BASEREL:                r = R_PPC64_SECTOFF_LO;
2306       break;
2307     case BFD_RELOC_HI16_BASEREL:                r = R_PPC64_SECTOFF_HI;
2308       break;
2309     case BFD_RELOC_HI16_S_BASEREL:              r = R_PPC64_SECTOFF_HA;
2310       break;
2311     case BFD_RELOC_CTOR:                        r = R_PPC64_ADDR64;
2312       break;
2313     case BFD_RELOC_64:                          r = R_PPC64_ADDR64;
2314       break;
2315     case BFD_RELOC_PPC64_HIGHER:                r = R_PPC64_ADDR16_HIGHER;
2316       break;
2317     case BFD_RELOC_PPC64_HIGHER_S:              r = R_PPC64_ADDR16_HIGHERA;
2318       break;
2319     case BFD_RELOC_PPC64_HIGHEST:               r = R_PPC64_ADDR16_HIGHEST;
2320       break;
2321     case BFD_RELOC_PPC64_HIGHEST_S:             r = R_PPC64_ADDR16_HIGHESTA;
2322       break;
2323     case BFD_RELOC_64_PCREL:                    r = R_PPC64_REL64;
2324       break;
2325     case BFD_RELOC_64_PLTOFF:                   r = R_PPC64_PLT64;
2326       break;
2327     case BFD_RELOC_64_PLT_PCREL:                r = R_PPC64_PLTREL64;
2328       break;
2329     case BFD_RELOC_PPC_TOC16:                   r = R_PPC64_TOC16;
2330       break;
2331     case BFD_RELOC_PPC64_TOC16_LO:              r = R_PPC64_TOC16_LO;
2332       break;
2333     case BFD_RELOC_PPC64_TOC16_HI:              r = R_PPC64_TOC16_HI;
2334       break;
2335     case BFD_RELOC_PPC64_TOC16_HA:              r = R_PPC64_TOC16_HA;
2336       break;
2337     case BFD_RELOC_PPC64_TOC:                   r = R_PPC64_TOC;
2338       break;
2339     case BFD_RELOC_PPC64_PLTGOT16:              r = R_PPC64_PLTGOT16;
2340       break;
2341     case BFD_RELOC_PPC64_PLTGOT16_LO:           r = R_PPC64_PLTGOT16_LO;
2342       break;
2343     case BFD_RELOC_PPC64_PLTGOT16_HI:           r = R_PPC64_PLTGOT16_HI;
2344       break;
2345     case BFD_RELOC_PPC64_PLTGOT16_HA:           r = R_PPC64_PLTGOT16_HA;
2346       break;
2347     case BFD_RELOC_PPC64_ADDR16_DS:             r = R_PPC64_ADDR16_DS;
2348       break;
2349     case BFD_RELOC_PPC64_ADDR16_LO_DS:          r = R_PPC64_ADDR16_LO_DS;
2350       break;
2351     case BFD_RELOC_PPC64_GOT16_DS:              r = R_PPC64_GOT16_DS;
2352       break;
2353     case BFD_RELOC_PPC64_GOT16_LO_DS:           r = R_PPC64_GOT16_LO_DS;
2354       break;
2355     case BFD_RELOC_PPC64_PLT16_LO_DS:           r = R_PPC64_PLT16_LO_DS;
2356       break;
2357     case BFD_RELOC_PPC64_SECTOFF_DS:            r = R_PPC64_SECTOFF_DS;
2358       break;
2359     case BFD_RELOC_PPC64_SECTOFF_LO_DS:         r = R_PPC64_SECTOFF_LO_DS;
2360       break;
2361     case BFD_RELOC_PPC64_TOC16_DS:              r = R_PPC64_TOC16_DS;
2362       break;
2363     case BFD_RELOC_PPC64_TOC16_LO_DS:           r = R_PPC64_TOC16_LO_DS;
2364       break;
2365     case BFD_RELOC_PPC64_PLTGOT16_DS:           r = R_PPC64_PLTGOT16_DS;
2366       break;
2367     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:        r = R_PPC64_PLTGOT16_LO_DS;
2368       break;
2369     case BFD_RELOC_PPC_TLS:                     r = R_PPC64_TLS;
2370       break;
2371     case BFD_RELOC_PPC_TLSGD:                   r = R_PPC64_TLSGD;
2372       break;
2373     case BFD_RELOC_PPC_TLSLD:                   r = R_PPC64_TLSLD;
2374       break;
2375     case BFD_RELOC_PPC_DTPMOD:                  r = R_PPC64_DTPMOD64;
2376       break;
2377     case BFD_RELOC_PPC_TPREL16:                 r = R_PPC64_TPREL16;
2378       break;
2379     case BFD_RELOC_PPC_TPREL16_LO:              r = R_PPC64_TPREL16_LO;
2380       break;
2381     case BFD_RELOC_PPC_TPREL16_HI:              r = R_PPC64_TPREL16_HI;
2382       break;
2383     case BFD_RELOC_PPC64_TPREL16_HIGH:          r = R_PPC64_TPREL16_HIGH;
2384       break;
2385     case BFD_RELOC_PPC_TPREL16_HA:              r = R_PPC64_TPREL16_HA;
2386       break;
2387     case BFD_RELOC_PPC64_TPREL16_HIGHA:         r = R_PPC64_TPREL16_HIGHA;
2388       break;
2389     case BFD_RELOC_PPC_TPREL:                   r = R_PPC64_TPREL64;
2390       break;
2391     case BFD_RELOC_PPC_DTPREL16:                r = R_PPC64_DTPREL16;
2392       break;
2393     case BFD_RELOC_PPC_DTPREL16_LO:             r = R_PPC64_DTPREL16_LO;
2394       break;
2395     case BFD_RELOC_PPC_DTPREL16_HI:             r = R_PPC64_DTPREL16_HI;
2396       break;
2397     case BFD_RELOC_PPC64_DTPREL16_HIGH:         r = R_PPC64_DTPREL16_HIGH;
2398       break;
2399     case BFD_RELOC_PPC_DTPREL16_HA:             r = R_PPC64_DTPREL16_HA;
2400       break;
2401     case BFD_RELOC_PPC64_DTPREL16_HIGHA:        r = R_PPC64_DTPREL16_HIGHA;
2402       break;
2403     case BFD_RELOC_PPC_DTPREL:                  r = R_PPC64_DTPREL64;
2404       break;
2405     case BFD_RELOC_PPC_GOT_TLSGD16:             r = R_PPC64_GOT_TLSGD16;
2406       break;
2407     case BFD_RELOC_PPC_GOT_TLSGD16_LO:          r = R_PPC64_GOT_TLSGD16_LO;
2408       break;
2409     case BFD_RELOC_PPC_GOT_TLSGD16_HI:          r = R_PPC64_GOT_TLSGD16_HI;
2410       break;
2411     case BFD_RELOC_PPC_GOT_TLSGD16_HA:          r = R_PPC64_GOT_TLSGD16_HA;
2412       break;
2413     case BFD_RELOC_PPC_GOT_TLSLD16:             r = R_PPC64_GOT_TLSLD16;
2414       break;
2415     case BFD_RELOC_PPC_GOT_TLSLD16_LO:          r = R_PPC64_GOT_TLSLD16_LO;
2416       break;
2417     case BFD_RELOC_PPC_GOT_TLSLD16_HI:          r = R_PPC64_GOT_TLSLD16_HI;
2418       break;
2419     case BFD_RELOC_PPC_GOT_TLSLD16_HA:          r = R_PPC64_GOT_TLSLD16_HA;
2420       break;
2421     case BFD_RELOC_PPC_GOT_TPREL16:             r = R_PPC64_GOT_TPREL16_DS;
2422       break;
2423     case BFD_RELOC_PPC_GOT_TPREL16_LO:          r = R_PPC64_GOT_TPREL16_LO_DS;
2424       break;
2425     case BFD_RELOC_PPC_GOT_TPREL16_HI:          r = R_PPC64_GOT_TPREL16_HI;
2426       break;
2427     case BFD_RELOC_PPC_GOT_TPREL16_HA:          r = R_PPC64_GOT_TPREL16_HA;
2428       break;
2429     case BFD_RELOC_PPC_GOT_DTPREL16:            r = R_PPC64_GOT_DTPREL16_DS;
2430       break;
2431     case BFD_RELOC_PPC_GOT_DTPREL16_LO:         r = R_PPC64_GOT_DTPREL16_LO_DS;
2432       break;
2433     case BFD_RELOC_PPC_GOT_DTPREL16_HI:         r = R_PPC64_GOT_DTPREL16_HI;
2434       break;
2435     case BFD_RELOC_PPC_GOT_DTPREL16_HA:         r = R_PPC64_GOT_DTPREL16_HA;
2436       break;
2437     case BFD_RELOC_PPC64_TPREL16_DS:            r = R_PPC64_TPREL16_DS;
2438       break;
2439     case BFD_RELOC_PPC64_TPREL16_LO_DS:         r = R_PPC64_TPREL16_LO_DS;
2440       break;
2441     case BFD_RELOC_PPC64_TPREL16_HIGHER:        r = R_PPC64_TPREL16_HIGHER;
2442       break;
2443     case BFD_RELOC_PPC64_TPREL16_HIGHERA:       r = R_PPC64_TPREL16_HIGHERA;
2444       break;
2445     case BFD_RELOC_PPC64_TPREL16_HIGHEST:       r = R_PPC64_TPREL16_HIGHEST;
2446       break;
2447     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:      r = R_PPC64_TPREL16_HIGHESTA;
2448       break;
2449     case BFD_RELOC_PPC64_DTPREL16_DS:           r = R_PPC64_DTPREL16_DS;
2450       break;
2451     case BFD_RELOC_PPC64_DTPREL16_LO_DS:        r = R_PPC64_DTPREL16_LO_DS;
2452       break;
2453     case BFD_RELOC_PPC64_DTPREL16_HIGHER:       r = R_PPC64_DTPREL16_HIGHER;
2454       break;
2455     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:      r = R_PPC64_DTPREL16_HIGHERA;
2456       break;
2457     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:      r = R_PPC64_DTPREL16_HIGHEST;
2458       break;
2459     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:     r = R_PPC64_DTPREL16_HIGHESTA;
2460       break;
2461     case BFD_RELOC_16_PCREL:                    r = R_PPC64_REL16;
2462       break;
2463     case BFD_RELOC_LO16_PCREL:                  r = R_PPC64_REL16_LO;
2464       break;
2465     case BFD_RELOC_HI16_PCREL:                  r = R_PPC64_REL16_HI;
2466       break;
2467     case BFD_RELOC_HI16_S_PCREL:                r = R_PPC64_REL16_HA;
2468       break;
2469     case BFD_RELOC_PPC_16DX_HA:                 r = R_PPC64_16DX_HA;
2470       break;
2471     case BFD_RELOC_PPC_REL16DX_HA:              r = R_PPC64_REL16DX_HA;
2472       break;
2473     case BFD_RELOC_PPC64_ENTRY:                 r = R_PPC64_ENTRY;
2474       break;
2475     case BFD_RELOC_PPC64_ADDR64_LOCAL:          r = R_PPC64_ADDR64_LOCAL;
2476       break;
2477     case BFD_RELOC_VTABLE_INHERIT:              r = R_PPC64_GNU_VTINHERIT;
2478       break;
2479     case BFD_RELOC_VTABLE_ENTRY:                r = R_PPC64_GNU_VTENTRY;
2480       break;
2481     }
2482
2483   return ppc64_elf_howto_table[r];
2484 };
2485
2486 static reloc_howto_type *
2487 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2488                              const char *r_name)
2489 {
2490   unsigned int i;
2491
2492   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2493     if (ppc64_elf_howto_raw[i].name != NULL
2494         && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2495       return &ppc64_elf_howto_raw[i];
2496
2497   return NULL;
2498 }
2499
2500 /* Set the howto pointer for a PowerPC ELF reloc.  */
2501
2502 static void
2503 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2504                          Elf_Internal_Rela *dst)
2505 {
2506   unsigned int type;
2507
2508   /* Initialize howto table if needed.  */
2509   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2510     ppc_howto_init ();
2511
2512   type = ELF64_R_TYPE (dst->r_info);
2513   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2514     {
2515       /* xgettext:c-format */
2516       _bfd_error_handler (_("%B: invalid relocation type %d"),
2517                           abfd, (int) type);
2518       type = R_PPC64_NONE;
2519     }
2520   cache_ptr->howto = ppc64_elf_howto_table[type];
2521 }
2522
2523 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
2524
2525 static bfd_reloc_status_type
2526 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2527                     void *data, asection *input_section,
2528                     bfd *output_bfd, char **error_message)
2529 {
2530   enum elf_ppc64_reloc_type r_type;
2531   long insn;
2532   bfd_size_type octets;
2533   bfd_vma value;
2534
2535   /* If this is a relocatable link (output_bfd test tells us), just
2536      call the generic function.  Any adjustment will be done at final
2537      link time.  */
2538   if (output_bfd != NULL)
2539     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2540                                   input_section, output_bfd, error_message);
2541
2542   /* Adjust the addend for sign extension of the low 16 bits.
2543      We won't actually be using the low 16 bits, so trashing them
2544      doesn't matter.  */
2545   reloc_entry->addend += 0x8000;
2546   r_type = reloc_entry->howto->type;
2547   if (r_type != R_PPC64_REL16DX_HA)
2548     return bfd_reloc_continue;
2549
2550   value = 0;
2551   if (!bfd_is_com_section (symbol->section))
2552     value = symbol->value;
2553   value += (reloc_entry->addend
2554             + symbol->section->output_offset
2555             + symbol->section->output_section->vma);
2556   value -= (reloc_entry->address
2557             + input_section->output_offset
2558             + input_section->output_section->vma);
2559   value = (bfd_signed_vma) value >> 16;
2560
2561   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2562   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2563   insn &= ~0x1fffc1;
2564   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2565   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2566   if (value + 0x8000 > 0xffff)
2567     return bfd_reloc_overflow;
2568   return bfd_reloc_ok;
2569 }
2570
2571 static bfd_reloc_status_type
2572 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2573                         void *data, asection *input_section,
2574                         bfd *output_bfd, char **error_message)
2575 {
2576   if (output_bfd != NULL)
2577     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2578                                   input_section, output_bfd, error_message);
2579
2580   if (strcmp (symbol->section->name, ".opd") == 0
2581       && (symbol->section->owner->flags & DYNAMIC) == 0)
2582     {
2583       bfd_vma dest = opd_entry_value (symbol->section,
2584                                       symbol->value + reloc_entry->addend,
2585                                       NULL, NULL, FALSE);
2586       if (dest != (bfd_vma) -1)
2587         reloc_entry->addend = dest - (symbol->value
2588                                       + symbol->section->output_section->vma
2589                                       + symbol->section->output_offset);
2590     }
2591   else
2592     {
2593       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2594
2595       if (symbol->section->owner != abfd
2596           && symbol->section->owner != NULL
2597           && abiversion (symbol->section->owner) >= 2)
2598         {
2599           unsigned int i;
2600
2601           for (i = 0; i < symbol->section->owner->symcount; ++i)
2602             {
2603               asymbol *symdef = symbol->section->owner->outsymbols[i];
2604
2605               if (strcmp (symdef->name, symbol->name) == 0)
2606                 {
2607                   elfsym = (elf_symbol_type *) symdef;
2608                   break;
2609                 }
2610             }
2611         }
2612       reloc_entry->addend
2613         += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2614     }
2615   return bfd_reloc_continue;
2616 }
2617
2618 static bfd_reloc_status_type
2619 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2620                          void *data, asection *input_section,
2621                          bfd *output_bfd, char **error_message)
2622 {
2623   long insn;
2624   enum elf_ppc64_reloc_type r_type;
2625   bfd_size_type octets;
2626   /* Assume 'at' branch hints.  */
2627   bfd_boolean is_isa_v2 = TRUE;
2628
2629   /* If this is a relocatable link (output_bfd test tells us), just
2630      call the generic function.  Any adjustment will be done at final
2631      link time.  */
2632   if (output_bfd != NULL)
2633     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2634                                   input_section, output_bfd, error_message);
2635
2636   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2637   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2638   insn &= ~(0x01 << 21);
2639   r_type = reloc_entry->howto->type;
2640   if (r_type == R_PPC64_ADDR14_BRTAKEN
2641       || r_type == R_PPC64_REL14_BRTAKEN)
2642     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
2643
2644   if (is_isa_v2)
2645     {
2646       /* Set 'a' bit.  This is 0b00010 in BO field for branch
2647          on CR(BI) insns (BO == 001at or 011at), and 0b01000
2648          for branch on CTR insns (BO == 1a00t or 1a01t).  */
2649       if ((insn & (0x14 << 21)) == (0x04 << 21))
2650         insn |= 0x02 << 21;
2651       else if ((insn & (0x14 << 21)) == (0x10 << 21))
2652         insn |= 0x08 << 21;
2653       else
2654         goto out;
2655     }
2656   else
2657     {
2658       bfd_vma target = 0;
2659       bfd_vma from;
2660
2661       if (!bfd_is_com_section (symbol->section))
2662         target = symbol->value;
2663       target += symbol->section->output_section->vma;
2664       target += symbol->section->output_offset;
2665       target += reloc_entry->addend;
2666
2667       from = (reloc_entry->address
2668               + input_section->output_offset
2669               + input_section->output_section->vma);
2670
2671       /* Invert 'y' bit if not the default.  */
2672       if ((bfd_signed_vma) (target - from) < 0)
2673         insn ^= 0x01 << 21;
2674     }
2675   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2676  out:
2677   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2678                                  input_section, output_bfd, error_message);
2679 }
2680
2681 static bfd_reloc_status_type
2682 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2683                          void *data, asection *input_section,
2684                          bfd *output_bfd, char **error_message)
2685 {
2686   /* If this is a relocatable link (output_bfd test tells us), just
2687      call the generic function.  Any adjustment will be done at final
2688      link time.  */
2689   if (output_bfd != NULL)
2690     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2691                                   input_section, output_bfd, error_message);
2692
2693   /* Subtract the symbol section base address.  */
2694   reloc_entry->addend -= symbol->section->output_section->vma;
2695   return bfd_reloc_continue;
2696 }
2697
2698 static bfd_reloc_status_type
2699 ppc64_elf_sectoff_ha_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
2713   /* Adjust the addend for sign extension of the low 16 bits.  */
2714   reloc_entry->addend += 0x8000;
2715   return bfd_reloc_continue;
2716 }
2717
2718 static bfd_reloc_status_type
2719 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2720                      void *data, asection *input_section,
2721                      bfd *output_bfd, char **error_message)
2722 {
2723   bfd_vma TOCstart;
2724
2725   /* If this is a relocatable link (output_bfd test tells us), just
2726      call the generic function.  Any adjustment will be done at final
2727      link time.  */
2728   if (output_bfd != NULL)
2729     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2730                                   input_section, output_bfd, error_message);
2731
2732   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2733   if (TOCstart == 0)
2734     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2735
2736   /* Subtract the TOC base address.  */
2737   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2738   return bfd_reloc_continue;
2739 }
2740
2741 static bfd_reloc_status_type
2742 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2743                         void *data, asection *input_section,
2744                         bfd *output_bfd, char **error_message)
2745 {
2746   bfd_vma TOCstart;
2747
2748   /* If this is a relocatable link (output_bfd test tells us), just
2749      call the generic function.  Any adjustment will be done at final
2750      link time.  */
2751   if (output_bfd != NULL)
2752     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2753                                   input_section, output_bfd, error_message);
2754
2755   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2756   if (TOCstart == 0)
2757     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2758
2759   /* Subtract the TOC base address.  */
2760   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2761
2762   /* Adjust the addend for sign extension of the low 16 bits.  */
2763   reloc_entry->addend += 0x8000;
2764   return bfd_reloc_continue;
2765 }
2766
2767 static bfd_reloc_status_type
2768 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2769                        void *data, asection *input_section,
2770                        bfd *output_bfd, char **error_message)
2771 {
2772   bfd_vma TOCstart;
2773   bfd_size_type octets;
2774
2775   /* If this is a relocatable link (output_bfd test tells us), just
2776      call the generic function.  Any adjustment will be done at final
2777      link time.  */
2778   if (output_bfd != NULL)
2779     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2780                                   input_section, output_bfd, error_message);
2781
2782   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2783   if (TOCstart == 0)
2784     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2785
2786   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2787   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2788   return bfd_reloc_ok;
2789 }
2790
2791 static bfd_reloc_status_type
2792 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2793                            void *data, asection *input_section,
2794                            bfd *output_bfd, char **error_message)
2795 {
2796   /* If this is a relocatable link (output_bfd test tells us), just
2797      call the generic function.  Any adjustment will be done at final
2798      link time.  */
2799   if (output_bfd != NULL)
2800     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2801                                   input_section, output_bfd, error_message);
2802
2803   if (error_message != NULL)
2804     {
2805       static char buf[60];
2806       sprintf (buf, "generic linker can't handle %s",
2807                reloc_entry->howto->name);
2808       *error_message = buf;
2809     }
2810   return bfd_reloc_dangerous;
2811 }
2812
2813 /* Track GOT entries needed for a given symbol.  We might need more
2814    than one got entry per symbol.  */
2815 struct got_entry
2816 {
2817   struct got_entry *next;
2818
2819   /* The symbol addend that we'll be placing in the GOT.  */
2820   bfd_vma addend;
2821
2822   /* Unlike other ELF targets, we use separate GOT entries for the same
2823      symbol referenced from different input files.  This is to support
2824      automatic multiple TOC/GOT sections, where the TOC base can vary
2825      from one input file to another.  After partitioning into TOC groups
2826      we merge entries within the group.
2827
2828      Point to the BFD owning this GOT entry.  */
2829   bfd *owner;
2830
2831   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2832      TLS_TPREL or TLS_DTPREL for tls entries.  */
2833   unsigned char tls_type;
2834
2835   /* Non-zero if got.ent points to real entry.  */
2836   unsigned char is_indirect;
2837
2838   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
2839   union
2840     {
2841       bfd_signed_vma refcount;
2842       bfd_vma offset;
2843       struct got_entry *ent;
2844     } got;
2845 };
2846
2847 /* The same for PLT.  */
2848 struct plt_entry
2849 {
2850   struct plt_entry *next;
2851
2852   bfd_vma addend;
2853
2854   union
2855     {
2856       bfd_signed_vma refcount;
2857       bfd_vma offset;
2858     } plt;
2859 };
2860
2861 struct ppc64_elf_obj_tdata
2862 {
2863   struct elf_obj_tdata elf;
2864
2865   /* Shortcuts to dynamic linker sections.  */
2866   asection *got;
2867   asection *relgot;
2868
2869   /* Used during garbage collection.  We attach global symbols defined
2870      on removed .opd entries to this section so that the sym is removed.  */
2871   asection *deleted_section;
2872
2873   /* TLS local dynamic got entry handling.  Support for multiple GOT
2874      sections means we potentially need one of these for each input bfd.  */
2875   struct got_entry tlsld_got;
2876
2877   union {
2878     /* A copy of relocs before they are modified for --emit-relocs.  */
2879     Elf_Internal_Rela *relocs;
2880
2881     /* Section contents.  */
2882     bfd_byte *contents;
2883   } opd;
2884
2885   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2886      the reloc to be in the range -32768 to 32767.  */
2887   unsigned int has_small_toc_reloc : 1;
2888
2889   /* Set if toc/got ha relocs detected not using r2, or lo reloc
2890      instruction not one we handle.  */
2891   unsigned int unexpected_toc_insn : 1;
2892 };
2893
2894 #define ppc64_elf_tdata(bfd) \
2895   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2896
2897 #define ppc64_tlsld_got(bfd) \
2898   (&ppc64_elf_tdata (bfd)->tlsld_got)
2899
2900 #define is_ppc64_elf(bfd) \
2901   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2902    && elf_object_id (bfd) == PPC64_ELF_DATA)
2903
2904 /* Override the generic function because we store some extras.  */
2905
2906 static bfd_boolean
2907 ppc64_elf_mkobject (bfd *abfd)
2908 {
2909   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2910                                   PPC64_ELF_DATA);
2911 }
2912
2913 /* Fix bad default arch selected for a 64 bit input bfd when the
2914    default is 32 bit.  Also select arch based on apuinfo.  */
2915
2916 static bfd_boolean
2917 ppc64_elf_object_p (bfd *abfd)
2918 {
2919   if (!abfd->arch_info->the_default)
2920     return TRUE;
2921
2922   if (abfd->arch_info->bits_per_word == 32)
2923     {
2924       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2925
2926       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2927         {
2928           /* Relies on arch after 32 bit default being 64 bit default.  */
2929           abfd->arch_info = abfd->arch_info->next;
2930           BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2931         }
2932     }
2933   return _bfd_elf_ppc_set_arch (abfd);
2934 }
2935
2936 /* Support for core dump NOTE sections.  */
2937
2938 static bfd_boolean
2939 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2940 {
2941   size_t offset, size;
2942
2943   if (note->descsz != 504)
2944     return FALSE;
2945
2946   /* pr_cursig */
2947   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2948
2949   /* pr_pid */
2950   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2951
2952   /* pr_reg */
2953   offset = 112;
2954   size = 384;
2955
2956   /* Make a ".reg/999" section.  */
2957   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2958                                           size, note->descpos + offset);
2959 }
2960
2961 static bfd_boolean
2962 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2963 {
2964   if (note->descsz != 136)
2965     return FALSE;
2966
2967   elf_tdata (abfd)->core->pid
2968     = bfd_get_32 (abfd, note->descdata + 24);
2969   elf_tdata (abfd)->core->program
2970     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2971   elf_tdata (abfd)->core->command
2972     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2973
2974   return TRUE;
2975 }
2976
2977 static char *
2978 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2979                            ...)
2980 {
2981   switch (note_type)
2982     {
2983     default:
2984       return NULL;
2985
2986     case NT_PRPSINFO:
2987       {
2988         char data[136];
2989         va_list ap;
2990
2991         va_start (ap, note_type);
2992         memset (data, 0, sizeof (data));
2993         strncpy (data + 40, va_arg (ap, const char *), 16);
2994         strncpy (data + 56, va_arg (ap, const char *), 80);
2995         va_end (ap);
2996         return elfcore_write_note (abfd, buf, bufsiz,
2997                                    "CORE", note_type, data, sizeof (data));
2998       }
2999
3000     case NT_PRSTATUS:
3001       {
3002         char data[504];
3003         va_list ap;
3004         long pid;
3005         int cursig;
3006         const void *greg;
3007
3008         va_start (ap, note_type);
3009         memset (data, 0, 112);
3010         pid = va_arg (ap, long);
3011         bfd_put_32 (abfd, pid, data + 32);
3012         cursig = va_arg (ap, int);
3013         bfd_put_16 (abfd, cursig, data + 12);
3014         greg = va_arg (ap, const void *);
3015         memcpy (data + 112, greg, 384);
3016         memset (data + 496, 0, 8);
3017         va_end (ap);
3018         return elfcore_write_note (abfd, buf, bufsiz,
3019                                    "CORE", note_type, data, sizeof (data));
3020       }
3021     }
3022 }
3023
3024 /* Add extra PPC sections.  */
3025
3026 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3027 {
3028   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
3029   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3030   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3031   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3033   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3034   { NULL,                     0,  0, 0,            0 }
3035 };
3036
3037 enum _ppc64_sec_type {
3038   sec_normal = 0,
3039   sec_opd = 1,
3040   sec_toc = 2
3041 };
3042
3043 struct _ppc64_elf_section_data
3044 {
3045   struct bfd_elf_section_data elf;
3046
3047   union
3048   {
3049     /* An array with one entry for each opd function descriptor,
3050        and some spares since opd entries may be either 16 or 24 bytes.  */
3051 #define OPD_NDX(OFF) ((OFF) >> 4)
3052     struct _opd_sec_data
3053     {
3054       /* Points to the function code section for local opd entries.  */
3055       asection **func_sec;
3056
3057       /* After editing .opd, adjust references to opd local syms.  */
3058       long *adjust;
3059     } opd;
3060
3061     /* An array for toc sections, indexed by offset/8.  */
3062     struct _toc_sec_data
3063     {
3064       /* Specifies the relocation symbol index used at a given toc offset.  */
3065       unsigned *symndx;
3066
3067       /* And the relocation addend.  */
3068       bfd_vma *add;
3069     } toc;
3070   } u;
3071
3072   enum _ppc64_sec_type sec_type:2;
3073
3074   /* Flag set when small branches are detected.  Used to
3075      select suitable defaults for the stub group size.  */
3076   unsigned int has_14bit_branch:1;
3077 };
3078
3079 #define ppc64_elf_section_data(sec) \
3080   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3081
3082 static bfd_boolean
3083 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3084 {
3085   if (!sec->used_by_bfd)
3086     {
3087       struct _ppc64_elf_section_data *sdata;
3088       bfd_size_type amt = sizeof (*sdata);
3089
3090       sdata = bfd_zalloc (abfd, amt);
3091       if (sdata == NULL)
3092         return FALSE;
3093       sec->used_by_bfd = sdata;
3094     }
3095
3096   return _bfd_elf_new_section_hook (abfd, sec);
3097 }
3098
3099 static struct _opd_sec_data *
3100 get_opd_info (asection * sec)
3101 {
3102   if (sec != NULL
3103       && ppc64_elf_section_data (sec) != NULL
3104       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3105     return &ppc64_elf_section_data (sec)->u.opd;
3106   return NULL;
3107 }
3108 \f
3109 /* Parameters for the qsort hook.  */
3110 static bfd_boolean synthetic_relocatable;
3111 static asection *synthetic_opd;
3112
3113 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
3114
3115 static int
3116 compare_symbols (const void *ap, const void *bp)
3117 {
3118   const asymbol *a = * (const asymbol **) ap;
3119   const asymbol *b = * (const asymbol **) bp;
3120
3121   /* Section symbols first.  */
3122   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3123     return -1;
3124   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3125     return 1;
3126
3127   /* then .opd symbols.  */
3128   if (synthetic_opd != NULL)
3129     {
3130       if (strcmp (a->section->name, ".opd") == 0
3131           && strcmp (b->section->name, ".opd") != 0)
3132         return -1;
3133       if (strcmp (a->section->name, ".opd") != 0
3134           && strcmp (b->section->name, ".opd") == 0)
3135         return 1;
3136     }
3137
3138   /* then other code symbols.  */
3139   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3140       == (SEC_CODE | SEC_ALLOC)
3141       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3142          != (SEC_CODE | SEC_ALLOC))
3143     return -1;
3144
3145   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3146       != (SEC_CODE | SEC_ALLOC)
3147       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3148          == (SEC_CODE | SEC_ALLOC))
3149     return 1;
3150
3151   if (synthetic_relocatable)
3152     {
3153       if (a->section->id < b->section->id)
3154         return -1;
3155
3156       if (a->section->id > b->section->id)
3157         return 1;
3158     }
3159
3160   if (a->value + a->section->vma < b->value + b->section->vma)
3161     return -1;
3162
3163   if (a->value + a->section->vma > b->value + b->section->vma)
3164     return 1;
3165
3166   /* For syms with the same value, prefer strong dynamic global function
3167      syms over other syms.  */
3168   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3169     return -1;
3170
3171   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3172     return 1;
3173
3174   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3175     return -1;
3176
3177   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3178     return 1;
3179
3180   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3181     return -1;
3182
3183   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3184     return 1;
3185
3186   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3187     return -1;
3188
3189   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3190     return 1;
3191
3192   return a > b;
3193 }
3194
3195 /* Search SYMS for a symbol of the given VALUE.  */
3196
3197 static asymbol *
3198 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3199 {
3200   long mid;
3201
3202   if (id == (unsigned) -1)
3203     {
3204       while (lo < hi)
3205         {
3206           mid = (lo + hi) >> 1;
3207           if (syms[mid]->value + syms[mid]->section->vma < value)
3208             lo = mid + 1;
3209           else if (syms[mid]->value + syms[mid]->section->vma > value)
3210             hi = mid;
3211           else
3212             return syms[mid];
3213         }
3214     }
3215   else
3216     {
3217       while (lo < hi)
3218         {
3219           mid = (lo + hi) >> 1;
3220           if (syms[mid]->section->id < id)
3221             lo = mid + 1;
3222           else if (syms[mid]->section->id > id)
3223             hi = mid;
3224           else if (syms[mid]->value < value)
3225             lo = mid + 1;
3226           else if (syms[mid]->value > value)
3227             hi = mid;
3228           else
3229             return syms[mid];
3230         }
3231     }
3232   return NULL;
3233 }
3234
3235 static bfd_boolean
3236 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3237 {
3238   bfd_vma vma = *(bfd_vma *) ptr;
3239   return ((section->flags & SEC_ALLOC) != 0
3240           && section->vma <= vma
3241           && vma < section->vma + section->size);
3242 }
3243
3244 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3245    entry syms.  Also generate @plt symbols for the glink branch table.
3246    Returns count of synthetic symbols in RET or -1 on error.  */
3247
3248 static long
3249 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3250                                 long static_count, asymbol **static_syms,
3251                                 long dyn_count, asymbol **dyn_syms,
3252                                 asymbol **ret)
3253 {
3254   asymbol *s;
3255   long i;
3256   long count;
3257   char *names;
3258   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3259   asection *opd = NULL;
3260   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3261   asymbol **syms;
3262   int abi = abiversion (abfd);
3263
3264   *ret = NULL;
3265
3266   if (abi < 2)
3267     {
3268       opd = bfd_get_section_by_name (abfd, ".opd");
3269       if (opd == NULL && abi == 1)
3270         return 0;
3271     }
3272
3273   syms = NULL;
3274   codesecsym = 0;
3275   codesecsymend = 0;
3276   secsymend = 0;
3277   opdsymend = 0;
3278   symcount = 0;
3279   if (opd != NULL)
3280     {
3281       symcount = static_count;
3282       if (!relocatable)
3283         symcount += dyn_count;
3284       if (symcount == 0)
3285         return 0;
3286
3287       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3288       if (syms == NULL)
3289         return -1;
3290
3291       if (!relocatable && static_count != 0 && dyn_count != 0)
3292         {
3293           /* Use both symbol tables.  */
3294           memcpy (syms, static_syms, static_count * sizeof (*syms));
3295           memcpy (syms + static_count, dyn_syms,
3296                   (dyn_count + 1) * sizeof (*syms));
3297         }
3298       else if (!relocatable && static_count == 0)
3299         memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3300       else
3301         memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3302
3303       synthetic_relocatable = relocatable;
3304       synthetic_opd = opd;
3305       qsort (syms, symcount, sizeof (*syms), compare_symbols);
3306
3307       if (!relocatable && symcount > 1)
3308         {
3309           long j;
3310           /* Trim duplicate syms, since we may have merged the normal and
3311              dynamic symbols.  Actually, we only care about syms that have
3312              different values, so trim any with the same value.  */
3313           for (i = 1, j = 1; i < symcount; ++i)
3314             if (syms[i - 1]->value + syms[i - 1]->section->vma
3315                 != syms[i]->value + syms[i]->section->vma)
3316               syms[j++] = syms[i];
3317           symcount = j;
3318         }
3319
3320       i = 0;
3321       /* Note that here and in compare_symbols we can't compare opd and
3322          sym->section directly.  With separate debug info files, the
3323          symbols will be extracted from the debug file while abfd passed
3324          to this function is the real binary.  */
3325       if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3326         ++i;
3327       codesecsym = i;
3328
3329       for (; i < symcount; ++i)
3330         if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3331                                          | SEC_THREAD_LOCAL))
3332              != (SEC_CODE | SEC_ALLOC))
3333             || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3334           break;
3335       codesecsymend = i;
3336
3337       for (; i < symcount; ++i)
3338         if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3339           break;
3340       secsymend = i;
3341
3342       for (; i < symcount; ++i)
3343         if (strcmp (syms[i]->section->name, ".opd") != 0)
3344           break;
3345       opdsymend = i;
3346
3347       for (; i < symcount; ++i)
3348         if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3349             != (SEC_CODE | SEC_ALLOC))
3350           break;
3351       symcount = i;
3352     }
3353   count = 0;
3354
3355   if (relocatable)
3356     {
3357       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3358       arelent *r;
3359       size_t size;
3360       long relcount;
3361
3362       if (opdsymend == secsymend)
3363         goto done;
3364
3365       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3366       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3367       if (relcount == 0)
3368         goto done;
3369
3370       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3371         {
3372           count = -1;
3373           goto done;
3374         }
3375
3376       size = 0;
3377       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3378         {
3379           asymbol *sym;
3380
3381           while (r < opd->relocation + relcount
3382                  && r->address < syms[i]->value + opd->vma)
3383             ++r;
3384
3385           if (r == opd->relocation + relcount)
3386             break;
3387
3388           if (r->address != syms[i]->value + opd->vma)
3389             continue;
3390
3391           if (r->howto->type != R_PPC64_ADDR64)
3392             continue;
3393
3394           sym = *r->sym_ptr_ptr;
3395           if (!sym_exists_at (syms, opdsymend, symcount,
3396                               sym->section->id, sym->value + r->addend))
3397             {
3398               ++count;
3399               size += sizeof (asymbol);
3400               size += strlen (syms[i]->name) + 2;
3401             }
3402         }
3403
3404       if (size == 0)
3405         goto done;
3406       s = *ret = bfd_malloc (size);
3407       if (s == NULL)
3408         {
3409           count = -1;
3410           goto done;
3411         }
3412
3413       names = (char *) (s + count);
3414
3415       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3416         {
3417           asymbol *sym;
3418
3419           while (r < opd->relocation + relcount
3420                  && r->address < syms[i]->value + opd->vma)
3421             ++r;
3422
3423           if (r == opd->relocation + relcount)
3424             break;
3425
3426           if (r->address != syms[i]->value + opd->vma)
3427             continue;
3428
3429           if (r->howto->type != R_PPC64_ADDR64)
3430             continue;
3431
3432           sym = *r->sym_ptr_ptr;
3433           if (!sym_exists_at (syms, opdsymend, symcount,
3434                               sym->section->id, sym->value + r->addend))
3435             {
3436               size_t len;
3437
3438               *s = *syms[i];
3439               s->flags |= BSF_SYNTHETIC;
3440               s->section = sym->section;
3441               s->value = sym->value + r->addend;
3442               s->name = names;
3443               *names++ = '.';
3444               len = strlen (syms[i]->name);
3445               memcpy (names, syms[i]->name, len + 1);
3446               names += len + 1;
3447               /* Have udata.p point back to the original symbol this
3448                  synthetic symbol was derived from.  */
3449               s->udata.p = syms[i];
3450               s++;
3451             }
3452         }
3453     }
3454   else
3455     {
3456       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3457       bfd_byte *contents = NULL;
3458       size_t size;
3459       long plt_count = 0;
3460       bfd_vma glink_vma = 0, resolv_vma = 0;
3461       asection *dynamic, *glink = NULL, *relplt = NULL;
3462       arelent *p;
3463
3464       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3465         {
3466         free_contents_and_exit_err:
3467           count = -1;
3468         free_contents_and_exit:
3469           if (contents)
3470             free (contents);
3471           goto done;
3472         }
3473
3474       size = 0;
3475       for (i = secsymend; i < opdsymend; ++i)
3476         {
3477           bfd_vma ent;
3478
3479           /* Ignore bogus symbols.  */
3480           if (syms[i]->value > opd->size - 8)
3481             continue;
3482
3483           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3484           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3485             {
3486               ++count;
3487               size += sizeof (asymbol);
3488               size += strlen (syms[i]->name) + 2;
3489             }
3490         }
3491
3492       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
3493       if (dyn_count != 0
3494           && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3495         {
3496           bfd_byte *dynbuf, *extdyn, *extdynend;
3497           size_t extdynsize;
3498           void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3499
3500           if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3501             goto free_contents_and_exit_err;
3502
3503           extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3504           swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3505
3506           extdyn = dynbuf;
3507           extdynend = extdyn + dynamic->size;
3508           for (; extdyn < extdynend; extdyn += extdynsize)
3509             {
3510               Elf_Internal_Dyn dyn;
3511               (*swap_dyn_in) (abfd, extdyn, &dyn);
3512
3513               if (dyn.d_tag == DT_NULL)
3514                 break;
3515
3516               if (dyn.d_tag == DT_PPC64_GLINK)
3517                 {
3518                   /* The first glink stub starts at offset 32; see
3519                      comment in ppc64_elf_finish_dynamic_sections. */
3520                   glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3521                   /* The .glink section usually does not survive the final
3522                      link; search for the section (usually .text) where the
3523                      glink stubs now reside.  */
3524                   glink = bfd_sections_find_if (abfd, section_covers_vma,
3525                                                 &glink_vma);
3526                   break;
3527                 }
3528             }
3529
3530           free (dynbuf);
3531         }
3532
3533       if (glink != NULL)
3534         {
3535           /* Determine __glink trampoline by reading the relative branch
3536              from the first glink stub.  */
3537           bfd_byte buf[4];
3538           unsigned int off = 0;
3539
3540           while (bfd_get_section_contents (abfd, glink, buf,
3541                                            glink_vma + off - glink->vma, 4))
3542             {
3543               unsigned int insn = bfd_get_32 (abfd, buf);
3544               insn ^= B_DOT;
3545               if ((insn & ~0x3fffffc) == 0)
3546                 {
3547                   resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3548                   break;
3549                 }
3550               off += 4;
3551               if (off > 4)
3552                 break;
3553             }
3554
3555           if (resolv_vma)
3556             size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3557
3558           relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3559           if (relplt != NULL)
3560             {
3561               slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3562               if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3563                 goto free_contents_and_exit_err;
3564
3565               plt_count = relplt->size / sizeof (Elf64_External_Rela);
3566               size += plt_count * sizeof (asymbol);
3567
3568               p = relplt->relocation;
3569               for (i = 0; i < plt_count; i++, p++)
3570                 {
3571                   size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3572                   if (p->addend != 0)
3573                     size += sizeof ("+0x") - 1 + 16;
3574                 }
3575             }
3576         }
3577
3578       if (size == 0)
3579         goto free_contents_and_exit;
3580       s = *ret = bfd_malloc (size);
3581       if (s == NULL)
3582         goto free_contents_and_exit_err;
3583
3584       names = (char *) (s + count + plt_count + (resolv_vma != 0));
3585
3586       for (i = secsymend; i < opdsymend; ++i)
3587         {
3588           bfd_vma ent;
3589
3590           if (syms[i]->value > opd->size - 8)
3591             continue;
3592
3593           ent = bfd_get_64 (abfd, contents + syms[i]->value);
3594           if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3595             {
3596               long lo, hi;
3597               size_t len;
3598               asection *sec = abfd->sections;
3599
3600               *s = *syms[i];
3601               lo = codesecsym;
3602               hi = codesecsymend;
3603               while (lo < hi)
3604                 {
3605                   long mid = (lo + hi) >> 1;
3606                   if (syms[mid]->section->vma < ent)
3607                     lo = mid + 1;
3608                   else if (syms[mid]->section->vma > ent)
3609                     hi = mid;
3610                   else
3611                     {
3612                       sec = syms[mid]->section;
3613                       break;
3614                     }
3615                 }
3616
3617               if (lo >= hi && lo > codesecsym)
3618                 sec = syms[lo - 1]->section;
3619
3620               for (; sec != NULL; sec = sec->next)
3621                 {
3622                   if (sec->vma > ent)
3623                     break;
3624                   /* SEC_LOAD may not be set if SEC is from a separate debug
3625                      info file.  */
3626                   if ((sec->flags & SEC_ALLOC) == 0)
3627                     break;
3628                   if ((sec->flags & SEC_CODE) != 0)
3629                     s->section = sec;
3630                 }
3631               s->flags |= BSF_SYNTHETIC;
3632               s->value = ent - s->section->vma;
3633               s->name = names;
3634               *names++ = '.';
3635               len = strlen (syms[i]->name);
3636               memcpy (names, syms[i]->name, len + 1);
3637               names += len + 1;
3638               /* Have udata.p point back to the original symbol this
3639                  synthetic symbol was derived from.  */
3640               s->udata.p = syms[i];
3641               s++;
3642             }
3643         }
3644       free (contents);
3645
3646       if (glink != NULL && relplt != NULL)
3647         {
3648           if (resolv_vma)
3649             {
3650               /* Add a symbol for the main glink trampoline.  */
3651               memset (s, 0, sizeof *s);
3652               s->the_bfd = abfd;
3653               s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3654               s->section = glink;
3655               s->value = resolv_vma - glink->vma;
3656               s->name = names;
3657               memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3658               names += sizeof ("__glink_PLTresolve");
3659               s++;
3660               count++;
3661             }
3662
3663           /* FIXME: It would be very much nicer to put sym@plt on the
3664              stub rather than on the glink branch table entry.  The
3665              objdump disassembler would then use a sensible symbol
3666              name on plt calls.  The difficulty in doing so is
3667              a) finding the stubs, and,
3668              b) matching stubs against plt entries, and,
3669              c) there can be multiple stubs for a given plt entry.
3670
3671              Solving (a) could be done by code scanning, but older
3672              ppc64 binaries used different stubs to current code.
3673              (b) is the tricky one since you need to known the toc
3674              pointer for at least one function that uses a pic stub to
3675              be able to calculate the plt address referenced.
3676              (c) means gdb would need to set multiple breakpoints (or
3677              find the glink branch itself) when setting breakpoints
3678              for pending shared library loads.  */
3679           p = relplt->relocation;
3680           for (i = 0; i < plt_count; i++, p++)
3681             {
3682               size_t len;
3683
3684               *s = **p->sym_ptr_ptr;
3685               /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
3686                  we are defining a symbol, ensure one of them is set.  */
3687               if ((s->flags & BSF_LOCAL) == 0)
3688                 s->flags |= BSF_GLOBAL;
3689               s->flags |= BSF_SYNTHETIC;
3690               s->section = glink;
3691               s->value = glink_vma - glink->vma;
3692               s->name = names;
3693               s->udata.p = NULL;
3694               len = strlen ((*p->sym_ptr_ptr)->name);
3695               memcpy (names, (*p->sym_ptr_ptr)->name, len);
3696               names += len;
3697               if (p->addend != 0)
3698                 {
3699                   memcpy (names, "+0x", sizeof ("+0x") - 1);
3700                   names += sizeof ("+0x") - 1;
3701                   bfd_sprintf_vma (abfd, names, p->addend);
3702                   names += strlen (names);
3703                 }
3704               memcpy (names, "@plt", sizeof ("@plt"));
3705               names += sizeof ("@plt");
3706               s++;
3707               if (abi < 2)
3708                 {
3709                   glink_vma += 8;
3710                   if (i >= 0x8000)
3711                     glink_vma += 4;
3712                 }
3713               else
3714                 glink_vma += 4;
3715             }
3716           count += plt_count;
3717         }
3718     }
3719
3720  done:
3721   free (syms);
3722   return count;
3723 }
3724 \f
3725 /* The following functions are specific to the ELF linker, while
3726    functions above are used generally.  Those named ppc64_elf_* are
3727    called by the main ELF linker code.  They appear in this file more
3728    or less in the order in which they are called.  eg.
3729    ppc64_elf_check_relocs is called early in the link process,
3730    ppc64_elf_finish_dynamic_sections is one of the last functions
3731    called.
3732
3733    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3734    functions have both a function code symbol and a function descriptor
3735    symbol.  A call to foo in a relocatable object file looks like:
3736
3737    .            .text
3738    .    x:
3739    .            bl      .foo
3740    .            nop
3741
3742    The function definition in another object file might be:
3743
3744    .            .section .opd
3745    .    foo:    .quad   .foo
3746    .            .quad   .TOC.@tocbase
3747    .            .quad   0
3748    .
3749    .            .text
3750    .    .foo:   blr
3751
3752    When the linker resolves the call during a static link, the branch
3753    unsurprisingly just goes to .foo and the .opd information is unused.
3754    If the function definition is in a shared library, things are a little
3755    different:  The call goes via a plt call stub, the opd information gets
3756    copied to the plt, and the linker patches the nop.
3757
3758    .    x:
3759    .            bl      .foo_stub
3760    .            ld      2,40(1)
3761    .
3762    .
3763    .    .foo_stub:
3764    .            std     2,40(1)                 # in practice, the call stub
3765    .            addis   11,2,Lfoo@toc@ha        # is slightly optimized, but
3766    .            addi    11,11,Lfoo@toc@l        # this is the general idea
3767    .            ld      12,0(11)
3768    .            ld      2,8(11)
3769    .            mtctr   12
3770    .            ld      11,16(11)
3771    .            bctr
3772    .
3773    .            .section .plt
3774    .    Lfoo:   reloc (R_PPC64_JMP_SLOT, foo)
3775
3776    The "reloc ()" notation is supposed to indicate that the linker emits
3777    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
3778    copying.
3779
3780    What are the difficulties here?  Well, firstly, the relocations
3781    examined by the linker in check_relocs are against the function code
3782    sym .foo, while the dynamic relocation in the plt is emitted against
3783    the function descriptor symbol, foo.  Somewhere along the line, we need
3784    to carefully copy dynamic link information from one symbol to the other.
3785    Secondly, the generic part of the elf linker will make .foo a dynamic
3786    symbol as is normal for most other backends.  We need foo dynamic
3787    instead, at least for an application final link.  However, when
3788    creating a shared library containing foo, we need to have both symbols
3789    dynamic so that references to .foo are satisfied during the early
3790    stages of linking.  Otherwise the linker might decide to pull in a
3791    definition from some other object, eg. a static library.
3792
3793    Update: As of August 2004, we support a new convention.  Function
3794    calls may use the function descriptor symbol, ie. "bl foo".  This
3795    behaves exactly as "bl .foo".  */
3796
3797 /* Of those relocs that might be copied as dynamic relocs, this
3798    function selects those that must be copied when linking a shared
3799    library or PIE, even when the symbol is local.  */
3800
3801 static int
3802 must_be_dyn_reloc (struct bfd_link_info *info,
3803                    enum elf_ppc64_reloc_type r_type)
3804 {
3805   switch (r_type)
3806     {
3807     default:
3808       /* Only relative relocs can be resolved when the object load
3809          address isn't fixed.  DTPREL64 is excluded because the
3810          dynamic linker needs to differentiate global dynamic from
3811          local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
3812       return 1;
3813
3814     case R_PPC64_REL32:
3815     case R_PPC64_REL64:
3816     case R_PPC64_REL30:
3817       return 0;
3818
3819     case R_PPC64_TPREL16:
3820     case R_PPC64_TPREL16_LO:
3821     case R_PPC64_TPREL16_HI:
3822     case R_PPC64_TPREL16_HA:
3823     case R_PPC64_TPREL16_DS:
3824     case R_PPC64_TPREL16_LO_DS:
3825     case R_PPC64_TPREL16_HIGH:
3826     case R_PPC64_TPREL16_HIGHA:
3827     case R_PPC64_TPREL16_HIGHER:
3828     case R_PPC64_TPREL16_HIGHERA:
3829     case R_PPC64_TPREL16_HIGHEST:
3830     case R_PPC64_TPREL16_HIGHESTA:
3831     case R_PPC64_TPREL64:
3832       /* These relocations are relative but in a shared library the
3833          linker doesn't know the thread pointer base.  */
3834       return bfd_link_dll (info);
3835     }
3836 }
3837
3838 /* Whether an undefined weak symbol should resolve to its link-time
3839    value, even in PIC or PIE objects.  */
3840 #define UNDEFWEAK_NO_DYNAMIC_RELOC(INFO, H)             \
3841   ((H)->root.type == bfd_link_hash_undefweak            \
3842    && (ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT    \
3843        || (INFO)->dynamic_undefined_weak == 0))
3844
3845 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3846    copying dynamic variables from a shared lib into an app's dynbss
3847    section, and instead use a dynamic relocation to point into the
3848    shared lib.  With code that gcc generates, it's vital that this be
3849    enabled;  In the PowerPC64 ABI, the address of a function is actually
3850    the address of a function descriptor, which resides in the .opd
3851    section.  gcc uses the descriptor directly rather than going via the
3852    GOT as some other ABI's do, which means that initialized function
3853    pointers must reference the descriptor.  Thus, a function pointer
3854    initialized to the address of a function in a shared library will
3855    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
3856    redefines the function descriptor symbol to point to the copy.  This
3857    presents a problem as a plt entry for that function is also
3858    initialized from the function descriptor symbol and the copy reloc
3859    may not be initialized first.  */
3860 #define ELIMINATE_COPY_RELOCS 1
3861
3862 /* Section name for stubs is the associated section name plus this
3863    string.  */
3864 #define STUB_SUFFIX ".stub"
3865
3866 /* Linker stubs.
3867    ppc_stub_long_branch:
3868    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3869    destination, but a 24 bit branch in a stub section will reach.
3870    .    b       dest
3871
3872    ppc_stub_plt_branch:
3873    Similar to the above, but a 24 bit branch in the stub section won't
3874    reach its destination.
3875    .    addis   %r11,%r2,xxx@toc@ha
3876    .    ld      %r12,xxx@toc@l(%r11)
3877    .    mtctr   %r12
3878    .    bctr
3879
3880    ppc_stub_plt_call:
3881    Used to call a function in a shared library.  If it so happens that
3882    the plt entry referenced crosses a 64k boundary, then an extra
3883    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3884    .    std     %r2,40(%r1)
3885    .    addis   %r11,%r2,xxx@toc@ha
3886    .    ld      %r12,xxx+0@toc@l(%r11)
3887    .    mtctr   %r12
3888    .    ld      %r2,xxx+8@toc@l(%r11)
3889    .    ld      %r11,xxx+16@toc@l(%r11)
3890    .    bctr
3891
3892    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3893    code to adjust the value and save r2 to support multiple toc sections.
3894    A ppc_stub_long_branch with an r2 offset looks like:
3895    .    std     %r2,40(%r1)
3896    .    addis   %r2,%r2,off@ha
3897    .    addi    %r2,%r2,off@l
3898    .    b       dest
3899
3900    A ppc_stub_plt_branch with an r2 offset looks like:
3901    .    std     %r2,40(%r1)
3902    .    addis   %r11,%r2,xxx@toc@ha
3903    .    ld      %r12,xxx@toc@l(%r11)
3904    .    addis   %r2,%r2,off@ha
3905    .    addi    %r2,%r2,off@l
3906    .    mtctr   %r12
3907    .    bctr
3908
3909    In cases where the "addis" instruction would add zero, the "addis" is
3910    omitted and following instructions modified slightly in some cases.
3911 */
3912
3913 enum ppc_stub_type {
3914   ppc_stub_none,
3915   ppc_stub_long_branch,
3916   ppc_stub_long_branch_r2off,
3917   ppc_stub_plt_branch,
3918   ppc_stub_plt_branch_r2off,
3919   ppc_stub_plt_call,
3920   ppc_stub_plt_call_r2save,
3921   ppc_stub_global_entry,
3922   ppc_stub_save_res
3923 };
3924
3925 /* Information on stub grouping.  */
3926 struct map_stub
3927 {
3928   /* The stub section.  */
3929   asection *stub_sec;
3930   /* This is the section to which stubs in the group will be attached.  */
3931   asection *link_sec;
3932   /* Next group.  */
3933   struct map_stub *next;
3934   /* Whether to emit a copy of register save/restore functions in this
3935      group.  */
3936   int needs_save_res;
3937   /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3938      or -1u if no such stub with bctrl exists.  */
3939   unsigned int tls_get_addr_opt_bctrl;
3940 };
3941
3942 struct ppc_stub_hash_entry {
3943
3944   /* Base hash table entry structure.  */
3945   struct bfd_hash_entry root;
3946
3947   enum ppc_stub_type stub_type;
3948
3949   /* Group information.  */
3950   struct map_stub *group;
3951
3952   /* Offset within stub_sec of the beginning of this stub.  */
3953   bfd_vma stub_offset;
3954
3955   /* Given the symbol's value and its section we can determine its final
3956      value when building the stubs (so the stub knows where to jump.  */
3957   bfd_vma target_value;
3958   asection *target_section;
3959
3960   /* The symbol table entry, if any, that this was derived from.  */
3961   struct ppc_link_hash_entry *h;
3962   struct plt_entry *plt_ent;
3963
3964   /* Symbol st_other.  */
3965   unsigned char other;
3966 };
3967
3968 struct ppc_branch_hash_entry {
3969
3970   /* Base hash table entry structure.  */
3971   struct bfd_hash_entry root;
3972
3973   /* Offset within branch lookup table.  */
3974   unsigned int offset;
3975
3976   /* Generation marker.  */
3977   unsigned int iter;
3978 };
3979
3980 /* Used to track dynamic relocations for local symbols.  */
3981 struct ppc_dyn_relocs
3982 {
3983   struct ppc_dyn_relocs *next;
3984
3985   /* The input section of the reloc.  */
3986   asection *sec;
3987
3988   /* Total number of relocs copied for the input section.  */
3989   unsigned int count : 31;
3990
3991   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
3992   unsigned int ifunc : 1;
3993 };
3994
3995 struct ppc_link_hash_entry
3996 {
3997   struct elf_link_hash_entry elf;
3998
3999   union {
4000     /* A pointer to the most recently used stub hash entry against this
4001        symbol.  */
4002     struct ppc_stub_hash_entry *stub_cache;
4003
4004     /* A pointer to the next symbol starting with a '.'  */
4005     struct ppc_link_hash_entry *next_dot_sym;
4006   } u;
4007
4008   /* Track dynamic relocs copied for this symbol.  */
4009   struct elf_dyn_relocs *dyn_relocs;
4010
4011   /* Chain of aliases referring to a weakdef.  */
4012   struct ppc_link_hash_entry *weakref;
4013
4014   /* Link between function code and descriptor symbols.  */
4015   struct ppc_link_hash_entry *oh;
4016
4017   /* Flag function code and descriptor symbols.  */
4018   unsigned int is_func:1;
4019   unsigned int is_func_descriptor:1;
4020   unsigned int fake:1;
4021
4022   /* Whether global opd/toc sym has been adjusted or not.
4023      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4024      should be set for all globals defined in any opd/toc section.  */
4025   unsigned int adjust_done:1;
4026
4027   /* Set if this is an out-of-line register save/restore function,
4028      with non-standard calling convention.  */
4029   unsigned int save_res:1;
4030
4031   /* Set if a duplicate symbol with non-zero localentry is detected,
4032      even when the duplicate symbol does not provide a definition.  */
4033   unsigned int non_zero_localentry:1;
4034
4035   /* Contexts in which symbol is used in the GOT (or TOC).
4036      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4037      corresponding relocs are encountered during check_relocs.
4038      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4039      indicate the corresponding GOT entry type is not needed.
4040      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4041      a TPREL one.  We use a separate flag rather than setting TPREL
4042      just for convenience in distinguishing the two cases.  */
4043 #define TLS_GD           1      /* GD reloc. */
4044 #define TLS_LD           2      /* LD reloc. */
4045 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
4046 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
4047 #define TLS_TLS         16      /* Any TLS reloc.  */
4048 #define TLS_EXPLICIT    32      /* Marks TOC section TLS relocs. */
4049 #define TLS_TPRELGD     64      /* TPREL reloc resulting from GD->IE. */
4050 #define PLT_IFUNC      128      /* STT_GNU_IFUNC.  */
4051   unsigned char tls_mask;
4052 };
4053
4054 /* ppc64 ELF linker hash table.  */
4055
4056 struct ppc_link_hash_table
4057 {
4058   struct elf_link_hash_table elf;
4059
4060   /* The stub hash table.  */
4061   struct bfd_hash_table stub_hash_table;
4062
4063   /* Another hash table for plt_branch stubs.  */
4064   struct bfd_hash_table branch_hash_table;
4065
4066   /* Hash table for function prologue tocsave.  */
4067   htab_t tocsave_htab;
4068
4069   /* Various options and other info passed from the linker.  */
4070   struct ppc64_elf_params *params;
4071
4072   /* The size of sec_info below.  */
4073   unsigned int sec_info_arr_size;
4074
4075   /* Per-section array of extra section info.  Done this way rather
4076      than as part of ppc64_elf_section_data so we have the info for
4077      non-ppc64 sections.  */
4078   struct
4079   {
4080     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
4081     bfd_vma toc_off;
4082
4083     union
4084     {
4085       /* The section group that this section belongs to.  */
4086       struct map_stub *group;
4087       /* A temp section list pointer.  */
4088       asection *list;
4089     } u;
4090   } *sec_info;
4091
4092   /* Linked list of groups.  */
4093   struct map_stub *group;
4094
4095   /* Temp used when calculating TOC pointers.  */
4096   bfd_vma toc_curr;
4097   bfd *toc_bfd;
4098   asection *toc_first_sec;
4099
4100   /* Used when adding symbols.  */
4101   struct ppc_link_hash_entry *dot_syms;
4102
4103   /* Shortcuts to get to dynamic linker sections.  */
4104   asection *glink;
4105   asection *sfpr;
4106   asection *brlt;
4107   asection *relbrlt;
4108   asection *glink_eh_frame;
4109
4110   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
4111   struct ppc_link_hash_entry *tls_get_addr;
4112   struct ppc_link_hash_entry *tls_get_addr_fd;
4113
4114   /* The size of reliplt used by got entry relocs.  */
4115   bfd_size_type got_reli_size;
4116
4117   /* Statistics.  */
4118   unsigned long stub_count[ppc_stub_global_entry];
4119
4120   /* Number of stubs against global syms.  */
4121   unsigned long stub_globals;
4122
4123   /* Set if we're linking code with function descriptors.  */
4124   unsigned int opd_abi:1;
4125
4126   /* Support for multiple toc sections.  */
4127   unsigned int do_multi_toc:1;
4128   unsigned int multi_toc_needed:1;
4129   unsigned int second_toc_pass:1;
4130   unsigned int do_toc_opt:1;
4131
4132   /* Set if tls optimization is enabled.  */
4133   unsigned int do_tls_opt:1;
4134
4135   /* Set on error.  */
4136   unsigned int stub_error:1;
4137
4138   /* Whether func_desc_adjust needs to be run over symbols.  */
4139   unsigned int need_func_desc_adj:1;
4140
4141   /* Whether there exist local gnu indirect function resolvers,
4142      referenced by dynamic relocations.  */
4143   unsigned int local_ifunc_resolver:1;
4144   unsigned int maybe_local_ifunc_resolver:1;
4145
4146   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
4147   unsigned int has_plt_localentry0:1;
4148
4149   /* Incremented every time we size stubs.  */
4150   unsigned int stub_iteration;
4151
4152   /* Small local sym cache.  */
4153   struct sym_cache sym_cache;
4154 };
4155
4156 /* Rename some of the generic section flags to better document how they
4157    are used here.  */
4158
4159 /* Nonzero if this section has TLS related relocations.  */
4160 #define has_tls_reloc sec_flg0
4161
4162 /* Nonzero if this section has a call to __tls_get_addr.  */
4163 #define has_tls_get_addr_call sec_flg1
4164
4165 /* Nonzero if this section has any toc or got relocs.  */
4166 #define has_toc_reloc sec_flg2
4167
4168 /* Nonzero if this section has a call to another section that uses
4169    the toc or got.  */
4170 #define makes_toc_func_call sec_flg3
4171
4172 /* Recursion protection when determining above flag.  */
4173 #define call_check_in_progress sec_flg4
4174 #define call_check_done sec_flg5
4175
4176 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
4177
4178 #define ppc_hash_table(p) \
4179   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4180   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4181
4182 #define ppc_stub_hash_lookup(table, string, create, copy) \
4183   ((struct ppc_stub_hash_entry *) \
4184    bfd_hash_lookup ((table), (string), (create), (copy)))
4185
4186 #define ppc_branch_hash_lookup(table, string, create, copy) \
4187   ((struct ppc_branch_hash_entry *) \
4188    bfd_hash_lookup ((table), (string), (create), (copy)))
4189
4190 /* Create an entry in the stub hash table.  */
4191
4192 static struct bfd_hash_entry *
4193 stub_hash_newfunc (struct bfd_hash_entry *entry,
4194                    struct bfd_hash_table *table,
4195                    const char *string)
4196 {
4197   /* Allocate the structure if it has not already been allocated by a
4198      subclass.  */
4199   if (entry == NULL)
4200     {
4201       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4202       if (entry == NULL)
4203         return entry;
4204     }
4205
4206   /* Call the allocation method of the superclass.  */
4207   entry = bfd_hash_newfunc (entry, table, string);
4208   if (entry != NULL)
4209     {
4210       struct ppc_stub_hash_entry *eh;
4211
4212       /* Initialize the local fields.  */
4213       eh = (struct ppc_stub_hash_entry *) entry;
4214       eh->stub_type = ppc_stub_none;
4215       eh->group = NULL;
4216       eh->stub_offset = 0;
4217       eh->target_value = 0;
4218       eh->target_section = NULL;
4219       eh->h = NULL;
4220       eh->plt_ent = NULL;
4221       eh->other = 0;
4222     }
4223
4224   return entry;
4225 }
4226
4227 /* Create an entry in the branch hash table.  */
4228
4229 static struct bfd_hash_entry *
4230 branch_hash_newfunc (struct bfd_hash_entry *entry,
4231                      struct bfd_hash_table *table,
4232                      const char *string)
4233 {
4234   /* Allocate the structure if it has not already been allocated by a
4235      subclass.  */
4236   if (entry == NULL)
4237     {
4238       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4239       if (entry == NULL)
4240         return entry;
4241     }
4242
4243   /* Call the allocation method of the superclass.  */
4244   entry = bfd_hash_newfunc (entry, table, string);
4245   if (entry != NULL)
4246     {
4247       struct ppc_branch_hash_entry *eh;
4248
4249       /* Initialize the local fields.  */
4250       eh = (struct ppc_branch_hash_entry *) entry;
4251       eh->offset = 0;
4252       eh->iter = 0;
4253     }
4254
4255   return entry;
4256 }
4257
4258 /* Create an entry in a ppc64 ELF linker hash table.  */
4259
4260 static struct bfd_hash_entry *
4261 link_hash_newfunc (struct bfd_hash_entry *entry,
4262                    struct bfd_hash_table *table,
4263                    const char *string)
4264 {
4265   /* Allocate the structure if it has not already been allocated by a
4266      subclass.  */
4267   if (entry == NULL)
4268     {
4269       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4270       if (entry == NULL)
4271         return entry;
4272     }
4273
4274   /* Call the allocation method of the superclass.  */
4275   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4276   if (entry != NULL)
4277     {
4278       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4279
4280       memset (&eh->u.stub_cache, 0,
4281               (sizeof (struct ppc_link_hash_entry)
4282                - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4283
4284       /* When making function calls, old ABI code references function entry
4285          points (dot symbols), while new ABI code references the function
4286          descriptor symbol.  We need to make any combination of reference and
4287          definition work together, without breaking archive linking.
4288
4289          For a defined function "foo" and an undefined call to "bar":
4290          An old object defines "foo" and ".foo", references ".bar" (possibly
4291          "bar" too).
4292          A new object defines "foo" and references "bar".
4293
4294          A new object thus has no problem with its undefined symbols being
4295          satisfied by definitions in an old object.  On the other hand, the
4296          old object won't have ".bar" satisfied by a new object.
4297
4298          Keep a list of newly added dot-symbols.  */
4299
4300       if (string[0] == '.')
4301         {
4302           struct ppc_link_hash_table *htab;
4303
4304           htab = (struct ppc_link_hash_table *) table;
4305           eh->u.next_dot_sym = htab->dot_syms;
4306           htab->dot_syms = eh;
4307         }
4308     }
4309
4310   return entry;
4311 }
4312
4313 struct tocsave_entry {
4314   asection *sec;
4315   bfd_vma offset;
4316 };
4317
4318 static hashval_t
4319 tocsave_htab_hash (const void *p)
4320 {
4321   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4322   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4323 }
4324
4325 static int
4326 tocsave_htab_eq (const void *p1, const void *p2)
4327 {
4328   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4329   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4330   return e1->sec == e2->sec && e1->offset == e2->offset;
4331 }
4332
4333 /* Destroy a ppc64 ELF linker hash table.  */
4334
4335 static void
4336 ppc64_elf_link_hash_table_free (bfd *obfd)
4337 {
4338   struct ppc_link_hash_table *htab;
4339
4340   htab = (struct ppc_link_hash_table *) obfd->link.hash;
4341   if (htab->tocsave_htab)
4342     htab_delete (htab->tocsave_htab);
4343   bfd_hash_table_free (&htab->branch_hash_table);
4344   bfd_hash_table_free (&htab->stub_hash_table);
4345   _bfd_elf_link_hash_table_free (obfd);
4346 }
4347
4348 /* Create a ppc64 ELF linker hash table.  */
4349
4350 static struct bfd_link_hash_table *
4351 ppc64_elf_link_hash_table_create (bfd *abfd)
4352 {
4353   struct ppc_link_hash_table *htab;
4354   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4355
4356   htab = bfd_zmalloc (amt);
4357   if (htab == NULL)
4358     return NULL;
4359
4360   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4361                                       sizeof (struct ppc_link_hash_entry),
4362                                       PPC64_ELF_DATA))
4363     {
4364       free (htab);
4365       return NULL;
4366     }
4367
4368   /* Init the stub hash table too.  */
4369   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4370                             sizeof (struct ppc_stub_hash_entry)))
4371     {
4372       _bfd_elf_link_hash_table_free (abfd);
4373       return NULL;
4374     }
4375
4376   /* And the branch hash table.  */
4377   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4378                             sizeof (struct ppc_branch_hash_entry)))
4379     {
4380       bfd_hash_table_free (&htab->stub_hash_table);
4381       _bfd_elf_link_hash_table_free (abfd);
4382       return NULL;
4383     }
4384
4385   htab->tocsave_htab = htab_try_create (1024,
4386                                         tocsave_htab_hash,
4387                                         tocsave_htab_eq,
4388                                         NULL);
4389   if (htab->tocsave_htab == NULL)
4390     {
4391       ppc64_elf_link_hash_table_free (abfd);
4392       return NULL;
4393     }
4394   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4395
4396   /* Initializing two fields of the union is just cosmetic.  We really
4397      only care about glist, but when compiled on a 32-bit host the
4398      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
4399      debugger inspection of these fields look nicer.  */
4400   htab->elf.init_got_refcount.refcount = 0;
4401   htab->elf.init_got_refcount.glist = NULL;
4402   htab->elf.init_plt_refcount.refcount = 0;
4403   htab->elf.init_plt_refcount.glist = NULL;
4404   htab->elf.init_got_offset.offset = 0;
4405   htab->elf.init_got_offset.glist = NULL;
4406   htab->elf.init_plt_offset.offset = 0;
4407   htab->elf.init_plt_offset.glist = NULL;
4408
4409   return &htab->elf.root;
4410 }
4411
4412 /* Create sections for linker generated code.  */
4413
4414 static bfd_boolean
4415 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4416 {
4417   struct ppc_link_hash_table *htab;
4418   flagword flags;
4419
4420   htab = ppc_hash_table (info);
4421
4422   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4423            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4424   if (htab->params->save_restore_funcs)
4425     {
4426       /* Create .sfpr for code to save and restore fp regs.  */
4427       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4428                                                        flags);
4429       if (htab->sfpr == NULL
4430           || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4431         return FALSE;
4432     }
4433
4434   if (bfd_link_relocatable (info))
4435     return TRUE;
4436
4437   /* Create .glink for lazy dynamic linking support.  */
4438   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4439                                                     flags);
4440   if (htab->glink == NULL
4441       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4442     return FALSE;
4443
4444   if (!info->no_ld_generated_unwind_info)
4445     {
4446       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4447                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4448       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4449                                                                  ".eh_frame",
4450                                                                  flags);
4451       if (htab->glink_eh_frame == NULL
4452           || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4453         return FALSE;
4454     }
4455
4456   flags = SEC_ALLOC | SEC_LINKER_CREATED;
4457   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4458   if (htab->elf.iplt == NULL
4459       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4460     return FALSE;
4461
4462   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4463            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4464   htab->elf.irelplt
4465     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4466   if (htab->elf.irelplt == NULL
4467       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4468     return FALSE;
4469
4470   /* Create branch lookup table for plt_branch stubs.  */
4471   flags = (SEC_ALLOC | SEC_LOAD
4472            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4473   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4474                                                    flags);
4475   if (htab->brlt == NULL
4476       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4477     return FALSE;
4478
4479   if (!bfd_link_pic (info))
4480     return TRUE;
4481
4482   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4483            | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4484   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4485                                                       ".rela.branch_lt",
4486                                                       flags);
4487   if (htab->relbrlt == NULL
4488       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4489     return FALSE;
4490
4491   return TRUE;
4492 }
4493
4494 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
4495
4496 bfd_boolean
4497 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4498                          struct ppc64_elf_params *params)
4499 {
4500   struct ppc_link_hash_table *htab;
4501
4502   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4503
4504 /* Always hook our dynamic sections into the first bfd, which is the
4505    linker created stub bfd.  This ensures that the GOT header is at
4506    the start of the output TOC section.  */
4507   htab = ppc_hash_table (info);
4508   htab->elf.dynobj = params->stub_bfd;
4509   htab->params = params;
4510
4511   return create_linkage_sections (htab->elf.dynobj, info);
4512 }
4513
4514 /* Build a name for an entry in the stub hash table.  */
4515
4516 static char *
4517 ppc_stub_name (const asection *input_section,
4518                const asection *sym_sec,
4519                const struct ppc_link_hash_entry *h,
4520                const Elf_Internal_Rela *rel)
4521 {
4522   char *stub_name;
4523   ssize_t len;
4524
4525   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4526      offsets from a sym as a branch target?  In fact, we could
4527      probably assume the addend is always zero.  */
4528   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4529
4530   if (h)
4531     {
4532       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4533       stub_name = bfd_malloc (len);
4534       if (stub_name == NULL)
4535         return stub_name;
4536
4537       len = sprintf (stub_name, "%08x.%s+%x",
4538                      input_section->id & 0xffffffff,
4539                      h->elf.root.root.string,
4540                      (int) rel->r_addend & 0xffffffff);
4541     }
4542   else
4543     {
4544       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4545       stub_name = bfd_malloc (len);
4546       if (stub_name == NULL)
4547         return stub_name;
4548
4549       len = sprintf (stub_name, "%08x.%x:%x+%x",
4550                      input_section->id & 0xffffffff,
4551                      sym_sec->id & 0xffffffff,
4552                      (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4553                      (int) rel->r_addend & 0xffffffff);
4554     }
4555   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4556     stub_name[len - 2] = 0;
4557   return stub_name;
4558 }
4559
4560 /* Look up an entry in the stub hash.  Stub entries are cached because
4561    creating the stub name takes a bit of time.  */
4562
4563 static struct ppc_stub_hash_entry *
4564 ppc_get_stub_entry (const asection *input_section,
4565                     const asection *sym_sec,
4566                     struct ppc_link_hash_entry *h,
4567                     const Elf_Internal_Rela *rel,
4568                     struct ppc_link_hash_table *htab)
4569 {
4570   struct ppc_stub_hash_entry *stub_entry;
4571   struct map_stub *group;
4572
4573   /* If this input section is part of a group of sections sharing one
4574      stub section, then use the id of the first section in the group.
4575      Stub names need to include a section id, as there may well be
4576      more than one stub used to reach say, printf, and we need to
4577      distinguish between them.  */
4578   group = htab->sec_info[input_section->id].u.group;
4579   if (group == NULL)
4580     return NULL;
4581
4582   if (h != NULL && h->u.stub_cache != NULL
4583       && h->u.stub_cache->h == h
4584       && h->u.stub_cache->group == group)
4585     {
4586       stub_entry = h->u.stub_cache;
4587     }
4588   else
4589     {
4590       char *stub_name;
4591
4592       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4593       if (stub_name == NULL)
4594         return NULL;
4595
4596       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4597                                          stub_name, FALSE, FALSE);
4598       if (h != NULL)
4599         h->u.stub_cache = stub_entry;
4600
4601       free (stub_name);
4602     }
4603
4604   return stub_entry;
4605 }
4606
4607 /* Add a new stub entry to the stub hash.  Not all fields of the new
4608    stub entry are initialised.  */
4609
4610 static struct ppc_stub_hash_entry *
4611 ppc_add_stub (const char *stub_name,
4612               asection *section,
4613               struct bfd_link_info *info)
4614 {
4615   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4616   struct map_stub *group;
4617   asection *link_sec;
4618   asection *stub_sec;
4619   struct ppc_stub_hash_entry *stub_entry;
4620
4621   group = htab->sec_info[section->id].u.group;
4622   link_sec = group->link_sec;
4623   stub_sec = group->stub_sec;
4624   if (stub_sec == NULL)
4625     {
4626       size_t namelen;
4627       bfd_size_type len;
4628       char *s_name;
4629
4630       namelen = strlen (link_sec->name);
4631       len = namelen + sizeof (STUB_SUFFIX);
4632       s_name = bfd_alloc (htab->params->stub_bfd, len);
4633       if (s_name == NULL)
4634         return NULL;
4635
4636       memcpy (s_name, link_sec->name, namelen);
4637       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4638       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4639       if (stub_sec == NULL)
4640         return NULL;
4641       group->stub_sec = stub_sec;
4642     }
4643
4644   /* Enter this entry into the linker stub hash table.  */
4645   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4646                                      TRUE, FALSE);
4647   if (stub_entry == NULL)
4648     {
4649       /* xgettext:c-format */
4650       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4651                               section->owner, stub_name);
4652       return NULL;
4653     }
4654
4655   stub_entry->group = group;
4656   stub_entry->stub_offset = 0;
4657   return stub_entry;
4658 }
4659
4660 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4661    not already done.  */
4662
4663 static bfd_boolean
4664 create_got_section (bfd *abfd, struct bfd_link_info *info)
4665 {
4666   asection *got, *relgot;
4667   flagword flags;
4668   struct ppc_link_hash_table *htab = ppc_hash_table (info);
4669
4670   if (!is_ppc64_elf (abfd))
4671     return FALSE;
4672   if (htab == NULL)
4673     return FALSE;
4674
4675   if (!htab->elf.sgot
4676       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4677     return FALSE;
4678
4679   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4680            | SEC_LINKER_CREATED);
4681
4682   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4683   if (!got
4684       || !bfd_set_section_alignment (abfd, got, 3))
4685     return FALSE;
4686
4687   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4688                                                flags | SEC_READONLY);
4689   if (!relgot
4690       || ! bfd_set_section_alignment (abfd, relgot, 3))
4691     return FALSE;
4692
4693   ppc64_elf_tdata (abfd)->got = got;
4694   ppc64_elf_tdata (abfd)->relgot = relgot;
4695   return TRUE;
4696 }
4697
4698 /* Follow indirect and warning symbol links.  */
4699
4700 static inline struct bfd_link_hash_entry *
4701 follow_link (struct bfd_link_hash_entry *h)
4702 {
4703   while (h->type == bfd_link_hash_indirect
4704          || h->type == bfd_link_hash_warning)
4705     h = h->u.i.link;
4706   return h;
4707 }
4708
4709 static inline struct elf_link_hash_entry *
4710 elf_follow_link (struct elf_link_hash_entry *h)
4711 {
4712   return (struct elf_link_hash_entry *) follow_link (&h->root);
4713 }
4714
4715 static inline struct ppc_link_hash_entry *
4716 ppc_follow_link (struct ppc_link_hash_entry *h)
4717 {
4718   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4719 }
4720
4721 /* Merge PLT info on FROM with that on TO.  */
4722
4723 static void
4724 move_plt_plist (struct ppc_link_hash_entry *from,
4725                 struct ppc_link_hash_entry *to)
4726 {
4727   if (from->elf.plt.plist != NULL)
4728     {
4729       if (to->elf.plt.plist != NULL)
4730         {
4731           struct plt_entry **entp;
4732           struct plt_entry *ent;
4733
4734           for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4735             {
4736               struct plt_entry *dent;
4737
4738               for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4739                 if (dent->addend == ent->addend)
4740                   {
4741                     dent->plt.refcount += ent->plt.refcount;
4742                     *entp = ent->next;
4743                     break;
4744                   }
4745               if (dent == NULL)
4746                 entp = &ent->next;
4747             }
4748           *entp = to->elf.plt.plist;
4749         }
4750
4751       to->elf.plt.plist = from->elf.plt.plist;
4752       from->elf.plt.plist = NULL;
4753     }
4754 }
4755
4756 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
4757
4758 static void
4759 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4760                                 struct elf_link_hash_entry *dir,
4761                                 struct elf_link_hash_entry *ind)
4762 {
4763   struct ppc_link_hash_entry *edir, *eind;
4764
4765   edir = (struct ppc_link_hash_entry *) dir;
4766   eind = (struct ppc_link_hash_entry *) ind;
4767
4768   edir->is_func |= eind->is_func;
4769   edir->is_func_descriptor |= eind->is_func_descriptor;
4770   edir->tls_mask |= eind->tls_mask;
4771   if (eind->oh != NULL)
4772     edir->oh = ppc_follow_link (eind->oh);
4773
4774   /* If called to transfer flags for a weakdef during processing
4775      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4776      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
4777   if (!(ELIMINATE_COPY_RELOCS
4778         && eind->elf.root.type != bfd_link_hash_indirect
4779         && edir->elf.dynamic_adjusted))
4780     edir->elf.non_got_ref |= eind->elf.non_got_ref;
4781
4782   if (edir->elf.versioned != versioned_hidden)
4783     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4784   edir->elf.ref_regular |= eind->elf.ref_regular;
4785   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4786   edir->elf.needs_plt |= eind->elf.needs_plt;
4787   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4788
4789   /* If we were called to copy over info for a weak sym, don't copy
4790      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
4791      in order to simplify readonly_dynrelocs and save a field in the
4792      symbol hash entry, but that means dyn_relocs can't be used in any
4793      tests about a specific symbol, or affect other symbol flags which
4794      are then tested.
4795      Chain weakdefs so we can get from the weakdef back to an alias.
4796      The list is circular so that we don't need to use u.weakdef as
4797      well as this list to look at all aliases.  */
4798   if (eind->elf.root.type != bfd_link_hash_indirect)
4799     {
4800       struct ppc_link_hash_entry *cur, *add, *next;
4801
4802       add = eind;
4803       do
4804         {
4805           cur = edir->weakref;
4806           if (cur != NULL)
4807             {
4808               do
4809                 {
4810                   /* We can be called twice for the same symbols.
4811                      Don't make multiple loops.  */
4812                   if (cur == add)
4813                     return;
4814                   cur = cur->weakref;
4815                 } while (cur != edir);
4816             }
4817           next = add->weakref;
4818           if (cur != add)
4819             {
4820               add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4821               edir->weakref = add;
4822             }
4823           add = next;
4824         } while (add != NULL && add != eind);
4825       return;
4826     }
4827
4828   /* Copy over any dynamic relocs we may have on the indirect sym.  */
4829   if (eind->dyn_relocs != NULL)
4830     {
4831       if (edir->dyn_relocs != NULL)
4832         {
4833           struct elf_dyn_relocs **pp;
4834           struct elf_dyn_relocs *p;
4835
4836           /* Add reloc counts against the indirect sym to the direct sym
4837              list.  Merge any entries against the same section.  */
4838           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4839             {
4840               struct elf_dyn_relocs *q;
4841
4842               for (q = edir->dyn_relocs; q != NULL; q = q->next)
4843                 if (q->sec == p->sec)
4844                   {
4845                     q->pc_count += p->pc_count;
4846                     q->count += p->count;
4847                     *pp = p->next;
4848                     break;
4849                   }
4850               if (q == NULL)
4851                 pp = &p->next;
4852             }
4853           *pp = edir->dyn_relocs;
4854         }
4855
4856       edir->dyn_relocs = eind->dyn_relocs;
4857       eind->dyn_relocs = NULL;
4858     }
4859
4860   /* Copy over got entries that we may have already seen to the
4861      symbol which just became indirect.  */
4862   if (eind->elf.got.glist != NULL)
4863     {
4864       if (edir->elf.got.glist != NULL)
4865         {
4866           struct got_entry **entp;
4867           struct got_entry *ent;
4868
4869           for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4870             {
4871               struct got_entry *dent;
4872
4873               for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4874                 if (dent->addend == ent->addend
4875                     && dent->owner == ent->owner
4876                     && dent->tls_type == ent->tls_type)
4877                   {
4878                     dent->got.refcount += ent->got.refcount;
4879                     *entp = ent->next;
4880                     break;
4881                   }
4882               if (dent == NULL)
4883                 entp = &ent->next;
4884             }
4885           *entp = edir->elf.got.glist;
4886         }
4887
4888       edir->elf.got.glist = eind->elf.got.glist;
4889       eind->elf.got.glist = NULL;
4890     }
4891
4892   /* And plt entries.  */
4893   move_plt_plist (eind, edir);
4894
4895   if (eind->elf.dynindx != -1)
4896     {
4897       if (edir->elf.dynindx != -1)
4898         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4899                                 edir->elf.dynstr_index);
4900       edir->elf.dynindx = eind->elf.dynindx;
4901       edir->elf.dynstr_index = eind->elf.dynstr_index;
4902       eind->elf.dynindx = -1;
4903       eind->elf.dynstr_index = 0;
4904     }
4905 }
4906
4907 /* Find the function descriptor hash entry from the given function code
4908    hash entry FH.  Link the entries via their OH fields.  */
4909
4910 static struct ppc_link_hash_entry *
4911 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4912 {
4913   struct ppc_link_hash_entry *fdh = fh->oh;
4914
4915   if (fdh == NULL)
4916     {
4917       const char *fd_name = fh->elf.root.root.string + 1;
4918
4919       fdh = (struct ppc_link_hash_entry *)
4920         elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4921       if (fdh == NULL)
4922         return fdh;
4923
4924       fdh->is_func_descriptor = 1;
4925       fdh->oh = fh;
4926       fh->is_func = 1;
4927       fh->oh = fdh;
4928     }
4929
4930   fdh = ppc_follow_link (fdh);
4931   fdh->is_func_descriptor = 1;
4932   fdh->oh = fh;
4933   return fdh;
4934 }
4935
4936 /* Make a fake function descriptor sym for the undefined code sym FH.  */
4937
4938 static struct ppc_link_hash_entry *
4939 make_fdh (struct bfd_link_info *info,
4940           struct ppc_link_hash_entry *fh)
4941 {
4942   bfd *abfd = fh->elf.root.u.undef.abfd;
4943   struct bfd_link_hash_entry *bh = NULL;
4944   struct ppc_link_hash_entry *fdh;
4945   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4946                     ? BSF_WEAK
4947                     : BSF_GLOBAL);
4948
4949   if (!_bfd_generic_link_add_one_symbol (info, abfd,
4950                                          fh->elf.root.root.string + 1,
4951                                          flags, bfd_und_section_ptr, 0,
4952                                          NULL, FALSE, FALSE, &bh))
4953     return NULL;
4954
4955   fdh = (struct ppc_link_hash_entry *) bh;
4956   fdh->elf.non_elf = 0;
4957   fdh->fake = 1;
4958   fdh->is_func_descriptor = 1;
4959   fdh->oh = fh;
4960   fh->is_func = 1;
4961   fh->oh = fdh;
4962   return fdh;
4963 }
4964
4965 /* Fix function descriptor symbols defined in .opd sections to be
4966    function type.  */
4967
4968 static bfd_boolean
4969 ppc64_elf_add_symbol_hook (bfd *ibfd,
4970                            struct bfd_link_info *info,
4971                            Elf_Internal_Sym *isym,
4972                            const char **name,
4973                            flagword *flags ATTRIBUTE_UNUSED,
4974                            asection **sec,
4975                            bfd_vma *value)
4976 {
4977   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4978       && (ibfd->flags & DYNAMIC) == 0
4979       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4980     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4981
4982   if (*sec != NULL
4983       && strcmp ((*sec)->name, ".opd") == 0)
4984     {
4985       asection *code_sec;
4986
4987       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4988             || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4989         isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4990
4991       /* If the symbol is a function defined in .opd, and the function
4992          code is in a discarded group, let it appear to be undefined.  */
4993       if (!bfd_link_relocatable (info)
4994           && (*sec)->reloc_count != 0
4995           && opd_entry_value (*sec, *value, &code_sec, NULL,
4996                               FALSE) != (bfd_vma) -1
4997           && discarded_section (code_sec))
4998         {
4999           *sec = bfd_und_section_ptr;
5000           isym->st_shndx = SHN_UNDEF;
5001         }
5002     }
5003   else if (*sec != NULL
5004            && strcmp ((*sec)->name, ".toc") == 0
5005            && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5006     {
5007       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5008       if (htab != NULL)
5009         htab->params->object_in_toc = 1;
5010     }
5011
5012   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5013     {
5014       if (abiversion (ibfd) == 0)
5015         set_abiversion (ibfd, 2);
5016       else if (abiversion (ibfd) == 1)
5017         {
5018           info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
5019                                     " for ABI version 1\n"), name);
5020           bfd_set_error (bfd_error_bad_value);
5021           return FALSE;
5022         }
5023     }
5024
5025   return TRUE;
5026 }
5027
5028 /* Merge non-visibility st_other attributes: local entry point.  */
5029
5030 static void
5031 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5032                                   const Elf_Internal_Sym *isym,
5033                                   bfd_boolean definition,
5034                                   bfd_boolean dynamic)
5035 {
5036   if (definition && (!dynamic || !h->def_regular))
5037     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5038                 | ELF_ST_VISIBILITY (h->other));
5039 }
5040
5041 /* Hook called on merging a symbol.  We use this to clear "fake" since
5042    we now have a real symbol.  */
5043
5044 static bfd_boolean
5045 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5046                         const Elf_Internal_Sym *isym,
5047                         asection **psec ATTRIBUTE_UNUSED,
5048                         bfd_boolean newdef ATTRIBUTE_UNUSED,
5049                         bfd_boolean olddef ATTRIBUTE_UNUSED,
5050                         bfd *oldbfd ATTRIBUTE_UNUSED,
5051                         const asection *oldsec ATTRIBUTE_UNUSED)
5052 {
5053   ((struct ppc_link_hash_entry *) h)->fake = 0;
5054   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5055     ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5056   return TRUE;
5057 }
5058
5059 /* This function makes an old ABI object reference to ".bar" cause the
5060    inclusion of a new ABI object archive that defines "bar".
5061    NAME is a symbol defined in an archive.  Return a symbol in the hash
5062    table that might be satisfied by the archive symbols.  */
5063
5064 static struct elf_link_hash_entry *
5065 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5066                                  struct bfd_link_info *info,
5067                                  const char *name)
5068 {
5069   struct elf_link_hash_entry *h;
5070   char *dot_name;
5071   size_t len;
5072
5073   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5074   if (h != NULL
5075       /* Don't return this sym if it is a fake function descriptor
5076          created by add_symbol_adjust.  */
5077       && !((struct ppc_link_hash_entry *) h)->fake)
5078     return h;
5079
5080   if (name[0] == '.')
5081     return h;
5082
5083   len = strlen (name);
5084   dot_name = bfd_alloc (abfd, len + 2);
5085   if (dot_name == NULL)
5086     return (struct elf_link_hash_entry *) 0 - 1;
5087   dot_name[0] = '.';
5088   memcpy (dot_name + 1, name, len + 1);
5089   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5090   bfd_release (abfd, dot_name);
5091   return h;
5092 }
5093
5094 /* This function satisfies all old ABI object references to ".bar" if a
5095    new ABI object defines "bar".  Well, at least, undefined dot symbols
5096    are made weak.  This stops later archive searches from including an
5097    object if we already have a function descriptor definition.  It also
5098    prevents the linker complaining about undefined symbols.
5099    We also check and correct mismatched symbol visibility here.  The
5100    most restrictive visibility of the function descriptor and the
5101    function entry symbol is used.  */
5102
5103 static bfd_boolean
5104 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5105 {
5106   struct ppc_link_hash_table *htab;
5107   struct ppc_link_hash_entry *fdh;
5108
5109   if (eh->elf.root.type == bfd_link_hash_warning)
5110     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5111
5112   if (eh->elf.root.type == bfd_link_hash_indirect)
5113     return TRUE;
5114
5115   if (eh->elf.root.root.string[0] != '.')
5116     abort ();
5117
5118   htab = ppc_hash_table (info);
5119   if (htab == NULL)
5120     return FALSE;
5121
5122   fdh = lookup_fdh (eh, htab);
5123   if (fdh == NULL
5124       && !bfd_link_relocatable (info)
5125       && (eh->elf.root.type == bfd_link_hash_undefined
5126           || eh->elf.root.type == bfd_link_hash_undefweak)
5127       && eh->elf.ref_regular)
5128     {
5129       /* Make an undefined function descriptor sym, in order to
5130          pull in an --as-needed shared lib.  Archives are handled
5131          elsewhere.  */
5132       fdh = make_fdh (info, eh);
5133       if (fdh == NULL)
5134         return FALSE;
5135     }
5136
5137   if (fdh != NULL)
5138     {
5139       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5140       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5141
5142       /* Make both descriptor and entry symbol have the most
5143          constraining visibility of either symbol.  */
5144       if (entry_vis < descr_vis)
5145         fdh->elf.other += entry_vis - descr_vis;
5146       else if (entry_vis > descr_vis)
5147         eh->elf.other += descr_vis - entry_vis;
5148
5149       /* Propagate reference flags from entry symbol to function
5150          descriptor symbol.  */
5151       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5152       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5153       fdh->elf.ref_regular |= eh->elf.ref_regular;
5154       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5155
5156       if (!fdh->elf.forced_local
5157           && fdh->elf.dynindx == -1
5158           && fdh->elf.versioned != versioned_hidden
5159           && (bfd_link_dll (info)
5160               || fdh->elf.def_dynamic
5161               || fdh->elf.ref_dynamic)
5162           && (eh->elf.ref_regular
5163               || eh->elf.def_regular))
5164         {
5165           if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5166             return FALSE;
5167         }
5168     }
5169
5170   return TRUE;
5171 }
5172
5173 /* Set up opd section info and abiversion for IBFD, and process list
5174    of dot-symbols we made in link_hash_newfunc.  */
5175
5176 static bfd_boolean
5177 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5178 {
5179   struct ppc_link_hash_table *htab;
5180   struct ppc_link_hash_entry **p, *eh;
5181   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5182
5183   if (opd != NULL && opd->size != 0)
5184     {
5185       if (abiversion (ibfd) == 0)
5186         set_abiversion (ibfd, 1);
5187       else if (abiversion (ibfd) >= 2)
5188         {
5189           /* xgettext:c-format */
5190           info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5191                                     " version %d\n"),
5192                                   ibfd, abiversion (ibfd));
5193           bfd_set_error (bfd_error_bad_value);
5194           return FALSE;
5195         }
5196
5197       if ((ibfd->flags & DYNAMIC) == 0
5198           && (opd->flags & SEC_RELOC) != 0
5199           && opd->reloc_count != 0
5200           && !bfd_is_abs_section (opd->output_section))
5201         {
5202           /* Garbage collection needs some extra help with .opd sections.
5203              We don't want to necessarily keep everything referenced by
5204              relocs in .opd, as that would keep all functions.  Instead,
5205              if we reference an .opd symbol (a function descriptor), we
5206              want to keep the function code symbol's section.  This is
5207              easy for global symbols, but for local syms we need to keep
5208              information about the associated function section.  */
5209           bfd_size_type amt;
5210           asection **opd_sym_map;
5211
5212           amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5213           opd_sym_map = bfd_zalloc (ibfd, amt);
5214           if (opd_sym_map == NULL)
5215             return FALSE;
5216           ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5217           BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5218           ppc64_elf_section_data (opd)->sec_type = sec_opd;
5219         }
5220     }
5221
5222   if (!is_ppc64_elf (info->output_bfd))
5223     return TRUE;
5224   htab = ppc_hash_table (info);
5225   if (htab == NULL)
5226     return FALSE;
5227
5228   /* For input files without an explicit abiversion in e_flags
5229      we should have flagged any with symbol st_other bits set
5230      as ELFv1 and above flagged those with .opd as ELFv2.
5231      Set the output abiversion if not yet set, and for any input
5232      still ambiguous, take its abiversion from the output.
5233      Differences in ABI are reported later.  */
5234   if (abiversion (info->output_bfd) == 0)
5235     set_abiversion (info->output_bfd, abiversion (ibfd));
5236   else if (abiversion (ibfd) == 0)
5237     set_abiversion (ibfd, abiversion (info->output_bfd));
5238
5239   p = &htab->dot_syms;
5240   while ((eh = *p) != NULL)
5241     {
5242       *p = NULL;
5243       if (&eh->elf == htab->elf.hgot)
5244         ;
5245       else if (htab->elf.hgot == NULL
5246                && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5247         htab->elf.hgot = &eh->elf;
5248       else if (abiversion (ibfd) <= 1)
5249         {
5250           htab->need_func_desc_adj = 1;
5251           if (!add_symbol_adjust (eh, info))
5252             return FALSE;
5253         }
5254       p = &eh->u.next_dot_sym;
5255     }
5256   return TRUE;
5257 }
5258
5259 /* Undo hash table changes when an --as-needed input file is determined
5260    not to be needed.  */
5261
5262 static bfd_boolean
5263 ppc64_elf_notice_as_needed (bfd *ibfd,
5264                             struct bfd_link_info *info,
5265                             enum notice_asneeded_action act)
5266 {
5267   if (act == notice_not_needed)
5268     {
5269       struct ppc_link_hash_table *htab = ppc_hash_table (info);
5270
5271       if (htab == NULL)
5272         return FALSE;
5273
5274       htab->dot_syms = NULL;
5275     }
5276   return _bfd_elf_notice_as_needed (ibfd, info, act);
5277 }
5278
5279 /* If --just-symbols against a final linked binary, then assume we need
5280    toc adjusting stubs when calling functions defined there.  */
5281
5282 static void
5283 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5284 {
5285   if ((sec->flags & SEC_CODE) != 0
5286       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5287       && is_ppc64_elf (sec->owner))
5288     {
5289       if (abiversion (sec->owner) >= 2
5290           || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5291         sec->has_toc_reloc = 1;
5292     }
5293   _bfd_elf_link_just_syms (sec, info);
5294 }
5295
5296 static struct plt_entry **
5297 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5298                        unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5299 {
5300   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5301   struct plt_entry **local_plt;
5302   unsigned char *local_got_tls_masks;
5303
5304   if (local_got_ents == NULL)
5305     {
5306       bfd_size_type size = symtab_hdr->sh_info;
5307
5308       size *= (sizeof (*local_got_ents)
5309                + sizeof (*local_plt)
5310                + sizeof (*local_got_tls_masks));
5311       local_got_ents = bfd_zalloc (abfd, size);
5312       if (local_got_ents == NULL)
5313         return NULL;
5314       elf_local_got_ents (abfd) = local_got_ents;
5315     }
5316
5317   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5318     {
5319       struct got_entry *ent;
5320
5321       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5322         if (ent->addend == r_addend
5323             && ent->owner == abfd
5324             && ent->tls_type == tls_type)
5325           break;
5326       if (ent == NULL)
5327         {
5328           bfd_size_type amt = sizeof (*ent);
5329           ent = bfd_alloc (abfd, amt);
5330           if (ent == NULL)
5331             return FALSE;
5332           ent->next = local_got_ents[r_symndx];
5333           ent->addend = r_addend;
5334           ent->owner = abfd;
5335           ent->tls_type = tls_type;
5336           ent->is_indirect = FALSE;
5337           ent->got.refcount = 0;
5338           local_got_ents[r_symndx] = ent;
5339         }
5340       ent->got.refcount += 1;
5341     }
5342
5343   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5344   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5345   local_got_tls_masks[r_symndx] |= tls_type;
5346
5347   return local_plt + r_symndx;
5348 }
5349
5350 static bfd_boolean
5351 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5352 {
5353   struct plt_entry *ent;
5354
5355   for (ent = *plist; ent != NULL; ent = ent->next)
5356     if (ent->addend == addend)
5357       break;
5358   if (ent == NULL)
5359     {
5360       bfd_size_type amt = sizeof (*ent);
5361       ent = bfd_alloc (abfd, amt);
5362       if (ent == NULL)
5363         return FALSE;
5364       ent->next = *plist;
5365       ent->addend = addend;
5366       ent->plt.refcount = 0;
5367       *plist = ent;
5368     }
5369   ent->plt.refcount += 1;
5370   return TRUE;
5371 }
5372
5373 static bfd_boolean
5374 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5375 {
5376   return (r_type == R_PPC64_REL24
5377           || r_type == R_PPC64_REL14
5378           || r_type == R_PPC64_REL14_BRTAKEN
5379           || r_type == R_PPC64_REL14_BRNTAKEN
5380           || r_type == R_PPC64_ADDR24
5381           || r_type == R_PPC64_ADDR14
5382           || r_type == R_PPC64_ADDR14_BRTAKEN
5383           || r_type == R_PPC64_ADDR14_BRNTAKEN);
5384 }
5385
5386 /* Look through the relocs for a section during the first phase, and
5387    calculate needed space in the global offset table, procedure
5388    linkage table, and dynamic reloc sections.  */
5389
5390 static bfd_boolean
5391 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5392                         asection *sec, const Elf_Internal_Rela *relocs)
5393 {
5394   struct ppc_link_hash_table *htab;
5395   Elf_Internal_Shdr *symtab_hdr;
5396   struct elf_link_hash_entry **sym_hashes;
5397   const Elf_Internal_Rela *rel;
5398   const Elf_Internal_Rela *rel_end;
5399   asection *sreloc;
5400   asection **opd_sym_map;
5401   struct elf_link_hash_entry *tga, *dottga;
5402
5403   if (bfd_link_relocatable (info))
5404     return TRUE;
5405
5406   /* Don't do anything special with non-loaded, non-alloced sections.
5407      In particular, any relocs in such sections should not affect GOT
5408      and PLT reference counting (ie. we don't allow them to create GOT
5409      or PLT entries), there's no possibility or desire to optimize TLS
5410      relocs, and there's not much point in propagating relocs to shared
5411      libs that the dynamic linker won't relocate.  */
5412   if ((sec->flags & SEC_ALLOC) == 0)
5413     return TRUE;
5414
5415   BFD_ASSERT (is_ppc64_elf (abfd));
5416
5417   htab = ppc_hash_table (info);
5418   if (htab == NULL)
5419     return FALSE;
5420
5421   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5422                               FALSE, FALSE, TRUE);
5423   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5424                                  FALSE, FALSE, TRUE);
5425   symtab_hdr = &elf_symtab_hdr (abfd);
5426   sym_hashes = elf_sym_hashes (abfd);
5427   sreloc = NULL;
5428   opd_sym_map = NULL;
5429   if (ppc64_elf_section_data (sec) != NULL
5430       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5431     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5432
5433   rel_end = relocs + sec->reloc_count;
5434   for (rel = relocs; rel < rel_end; rel++)
5435     {
5436       unsigned long r_symndx;
5437       struct elf_link_hash_entry *h;
5438       enum elf_ppc64_reloc_type r_type;
5439       int tls_type;
5440       struct _ppc64_elf_section_data *ppc64_sec;
5441       struct plt_entry **ifunc, **plt_list;
5442
5443       r_symndx = ELF64_R_SYM (rel->r_info);
5444       if (r_symndx < symtab_hdr->sh_info)
5445         h = NULL;
5446       else
5447         {
5448           struct ppc_link_hash_entry *eh;
5449
5450           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5451           h = elf_follow_link (h);
5452           eh = (struct ppc_link_hash_entry *) h;
5453
5454           /* PR15323, ref flags aren't set for references in the same
5455              object.  */
5456           h->root.non_ir_ref_regular = 1;
5457           if (eh->is_func && eh->oh != NULL)
5458             eh->oh->elf.root.non_ir_ref_regular = 1;
5459
5460           if (h == htab->elf.hgot)
5461             sec->has_toc_reloc = 1;
5462         }
5463
5464       tls_type = 0;
5465       ifunc = NULL;
5466       if (h != NULL)
5467         {
5468           if (h->type == STT_GNU_IFUNC)
5469             {
5470               h->needs_plt = 1;
5471               ifunc = &h->plt.plist;
5472             }
5473         }
5474       else
5475         {
5476           Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5477                                                           abfd, r_symndx);
5478           if (isym == NULL)
5479             return FALSE;
5480
5481           if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5482             {
5483               ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5484                                              rel->r_addend, PLT_IFUNC);
5485               if (ifunc == NULL)
5486                 return FALSE;
5487             }
5488         }
5489
5490       r_type = ELF64_R_TYPE (rel->r_info);
5491       switch (r_type)
5492         {
5493         case R_PPC64_TLSGD:
5494         case R_PPC64_TLSLD:
5495           /* These special tls relocs tie a call to __tls_get_addr with
5496              its parameter symbol.  */
5497           break;
5498
5499         case R_PPC64_GOT_TLSLD16:
5500         case R_PPC64_GOT_TLSLD16_LO:
5501         case R_PPC64_GOT_TLSLD16_HI:
5502         case R_PPC64_GOT_TLSLD16_HA:
5503           tls_type = TLS_TLS | TLS_LD;
5504           goto dogottls;
5505
5506         case R_PPC64_GOT_TLSGD16:
5507         case R_PPC64_GOT_TLSGD16_LO:
5508         case R_PPC64_GOT_TLSGD16_HI:
5509         case R_PPC64_GOT_TLSGD16_HA:
5510           tls_type = TLS_TLS | TLS_GD;
5511           goto dogottls;
5512
5513         case R_PPC64_GOT_TPREL16_DS:
5514         case R_PPC64_GOT_TPREL16_LO_DS:
5515         case R_PPC64_GOT_TPREL16_HI:
5516         case R_PPC64_GOT_TPREL16_HA:
5517           if (bfd_link_dll (info))
5518             info->flags |= DF_STATIC_TLS;
5519           tls_type = TLS_TLS | TLS_TPREL;
5520           goto dogottls;
5521
5522         case R_PPC64_GOT_DTPREL16_DS:
5523         case R_PPC64_GOT_DTPREL16_LO_DS:
5524         case R_PPC64_GOT_DTPREL16_HI:
5525         case R_PPC64_GOT_DTPREL16_HA:
5526           tls_type = TLS_TLS | TLS_DTPREL;
5527         dogottls:
5528           sec->has_tls_reloc = 1;
5529           /* Fall through */
5530
5531         case R_PPC64_GOT16:
5532         case R_PPC64_GOT16_DS:
5533         case R_PPC64_GOT16_HA:
5534         case R_PPC64_GOT16_HI:
5535         case R_PPC64_GOT16_LO:
5536         case R_PPC64_GOT16_LO_DS:
5537           /* This symbol requires a global offset table entry.  */
5538           sec->has_toc_reloc = 1;
5539           if (r_type == R_PPC64_GOT_TLSLD16
5540               || r_type == R_PPC64_GOT_TLSGD16
5541               || r_type == R_PPC64_GOT_TPREL16_DS
5542               || r_type == R_PPC64_GOT_DTPREL16_DS
5543               || r_type == R_PPC64_GOT16
5544               || r_type == R_PPC64_GOT16_DS)
5545             {
5546               htab->do_multi_toc = 1;
5547               ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5548             }
5549
5550           if (ppc64_elf_tdata (abfd)->got == NULL
5551               && !create_got_section (abfd, info))
5552             return FALSE;
5553
5554           if (h != NULL)
5555             {
5556               struct ppc_link_hash_entry *eh;
5557               struct got_entry *ent;
5558
5559               eh = (struct ppc_link_hash_entry *) h;
5560               for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5561                 if (ent->addend == rel->r_addend
5562                     && ent->owner == abfd
5563                     && ent->tls_type == tls_type)
5564                   break;
5565               if (ent == NULL)
5566                 {
5567                   bfd_size_type amt = sizeof (*ent);
5568                   ent = bfd_alloc (abfd, amt);
5569                   if (ent == NULL)
5570                     return FALSE;
5571                   ent->next = eh->elf.got.glist;
5572                   ent->addend = rel->r_addend;
5573                   ent->owner = abfd;
5574                   ent->tls_type = tls_type;
5575                   ent->is_indirect = FALSE;
5576                   ent->got.refcount = 0;
5577                   eh->elf.got.glist = ent;
5578                 }
5579               ent->got.refcount += 1;
5580               eh->tls_mask |= tls_type;
5581             }
5582           else
5583             /* This is a global offset table entry for a local symbol.  */
5584             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5585                                         rel->r_addend, tls_type))
5586               return FALSE;
5587
5588           /* We may also need a plt entry if the symbol turns out to be
5589              an ifunc.  */
5590           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5591             {
5592               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5593                 return FALSE;
5594             }
5595           break;
5596
5597         case R_PPC64_PLT16_HA:
5598         case R_PPC64_PLT16_HI:
5599         case R_PPC64_PLT16_LO:
5600         case R_PPC64_PLT32:
5601         case R_PPC64_PLT64:
5602           /* This symbol requires a procedure linkage table entry.  */
5603           plt_list = ifunc;
5604           if (h != NULL)
5605             {
5606               h->needs_plt = 1;
5607               if (h->root.root.string[0] == '.'
5608                   && h->root.root.string[1] != '\0')
5609                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5610               plt_list = &h->plt.plist;
5611             }
5612           if (plt_list == NULL)
5613             {
5614               /* It does not make sense to have a procedure linkage
5615                  table entry for a non-ifunc local symbol.  */
5616               info->callbacks->einfo
5617                 /* xgettext:c-format */
5618                 (_("%H: %s reloc against local symbol\n"),
5619                  abfd, sec, rel->r_offset,
5620                  ppc64_elf_howto_table[r_type]->name);
5621               bfd_set_error (bfd_error_bad_value);
5622               return FALSE;
5623             }
5624           if (!update_plt_info (abfd, plt_list, rel->r_addend))
5625             return FALSE;
5626           break;
5627
5628           /* The following relocations don't need to propagate the
5629              relocation if linking a shared object since they are
5630              section relative.  */
5631         case R_PPC64_SECTOFF:
5632         case R_PPC64_SECTOFF_LO:
5633         case R_PPC64_SECTOFF_HI:
5634         case R_PPC64_SECTOFF_HA:
5635         case R_PPC64_SECTOFF_DS:
5636         case R_PPC64_SECTOFF_LO_DS:
5637         case R_PPC64_DTPREL16:
5638         case R_PPC64_DTPREL16_LO:
5639         case R_PPC64_DTPREL16_HI:
5640         case R_PPC64_DTPREL16_HA:
5641         case R_PPC64_DTPREL16_DS:
5642         case R_PPC64_DTPREL16_LO_DS:
5643         case R_PPC64_DTPREL16_HIGH:
5644         case R_PPC64_DTPREL16_HIGHA:
5645         case R_PPC64_DTPREL16_HIGHER:
5646         case R_PPC64_DTPREL16_HIGHERA:
5647         case R_PPC64_DTPREL16_HIGHEST:
5648         case R_PPC64_DTPREL16_HIGHESTA:
5649           break;
5650
5651           /* Nor do these.  */
5652         case R_PPC64_REL16:
5653         case R_PPC64_REL16_LO:
5654         case R_PPC64_REL16_HI:
5655         case R_PPC64_REL16_HA:
5656         case R_PPC64_REL16DX_HA:
5657           break;
5658
5659           /* Not supported as a dynamic relocation.  */
5660         case R_PPC64_ADDR64_LOCAL:
5661           if (bfd_link_pic (info))
5662             {
5663               if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5664                 ppc_howto_init ();
5665               /* xgettext:c-format */
5666               info->callbacks->einfo (_("%H: %s reloc unsupported "
5667                                         "in shared libraries and PIEs.\n"),
5668                                       abfd, sec, rel->r_offset,
5669                                       ppc64_elf_howto_table[r_type]->name);
5670               bfd_set_error (bfd_error_bad_value);
5671               return FALSE;
5672             }
5673           break;
5674
5675         case R_PPC64_TOC16:
5676         case R_PPC64_TOC16_DS:
5677           htab->do_multi_toc = 1;
5678           ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5679           /* Fall through.  */
5680         case R_PPC64_TOC16_LO:
5681         case R_PPC64_TOC16_HI:
5682         case R_PPC64_TOC16_HA:
5683         case R_PPC64_TOC16_LO_DS:
5684           sec->has_toc_reloc = 1;
5685           break;
5686
5687           /* Marker reloc.  */
5688         case R_PPC64_ENTRY:
5689           break;
5690
5691           /* This relocation describes the C++ object vtable hierarchy.
5692              Reconstruct it for later use during GC.  */
5693         case R_PPC64_GNU_VTINHERIT:
5694           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5695             return FALSE;
5696           break;
5697
5698           /* This relocation describes which C++ vtable entries are actually
5699              used.  Record for later use during GC.  */
5700         case R_PPC64_GNU_VTENTRY:
5701           BFD_ASSERT (h != NULL);
5702           if (h != NULL
5703               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5704             return FALSE;
5705           break;
5706
5707         case R_PPC64_REL14:
5708         case R_PPC64_REL14_BRTAKEN:
5709         case R_PPC64_REL14_BRNTAKEN:
5710           {
5711             asection *dest = NULL;
5712
5713             /* Heuristic: If jumping outside our section, chances are
5714                we are going to need a stub.  */
5715             if (h != NULL)
5716               {
5717                 /* If the sym is weak it may be overridden later, so
5718                    don't assume we know where a weak sym lives.  */
5719                 if (h->root.type == bfd_link_hash_defined)
5720                   dest = h->root.u.def.section;
5721               }
5722             else
5723               {
5724                 Elf_Internal_Sym *isym;
5725
5726                 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5727                                               abfd, r_symndx);
5728                 if (isym == NULL)
5729                   return FALSE;
5730
5731                 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5732               }
5733
5734             if (dest != sec)
5735               ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5736           }
5737           /* Fall through.  */
5738
5739         case R_PPC64_REL24:
5740           plt_list = ifunc;
5741           if (h != NULL)
5742             {
5743               h->needs_plt = 1;
5744               if (h->root.root.string[0] == '.'
5745                   && h->root.root.string[1] != '\0')
5746                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5747
5748               if (h == tga || h == dottga)
5749                 {
5750                   sec->has_tls_reloc = 1;
5751                   if (rel != relocs
5752                       && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5753                           || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5754                     /* We have a new-style __tls_get_addr call with
5755                        a marker reloc.  */
5756                     ;
5757                   else
5758                     /* Mark this section as having an old-style call.  */
5759                     sec->has_tls_get_addr_call = 1;
5760                 }
5761               plt_list = &h->plt.plist;
5762             }
5763
5764           /* We may need a .plt entry if the function this reloc
5765              refers to is in a shared lib.  */
5766           if (plt_list
5767               && !update_plt_info (abfd, plt_list, rel->r_addend))
5768             return FALSE;
5769           break;
5770
5771         case R_PPC64_ADDR14:
5772         case R_PPC64_ADDR14_BRNTAKEN:
5773         case R_PPC64_ADDR14_BRTAKEN:
5774         case R_PPC64_ADDR24:
5775           goto dodyn;
5776
5777         case R_PPC64_TPREL64:
5778           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5779           if (bfd_link_dll (info))
5780             info->flags |= DF_STATIC_TLS;
5781           goto dotlstoc;
5782
5783         case R_PPC64_DTPMOD64:
5784           if (rel + 1 < rel_end
5785               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5786               && rel[1].r_offset == rel->r_offset + 8)
5787             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5788           else
5789             tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5790           goto dotlstoc;
5791
5792         case R_PPC64_DTPREL64:
5793           tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5794           if (rel != relocs
5795               && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5796               && rel[-1].r_offset == rel->r_offset - 8)
5797             /* This is the second reloc of a dtpmod, dtprel pair.
5798                Don't mark with TLS_DTPREL.  */
5799             goto dodyn;
5800
5801         dotlstoc:
5802           sec->has_tls_reloc = 1;
5803           if (h != NULL)
5804             {
5805               struct ppc_link_hash_entry *eh;
5806               eh = (struct ppc_link_hash_entry *) h;
5807               eh->tls_mask |= tls_type;
5808             }
5809           else
5810             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5811                                         rel->r_addend, tls_type))
5812               return FALSE;
5813
5814           ppc64_sec = ppc64_elf_section_data (sec);
5815           if (ppc64_sec->sec_type != sec_toc)
5816             {
5817               bfd_size_type amt;
5818
5819               /* One extra to simplify get_tls_mask.  */
5820               amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5821               ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5822               if (ppc64_sec->u.toc.symndx == NULL)
5823                 return FALSE;
5824               amt = sec->size * sizeof (bfd_vma) / 8;
5825               ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5826               if (ppc64_sec->u.toc.add == NULL)
5827                 return FALSE;
5828               BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5829               ppc64_sec->sec_type = sec_toc;
5830             }
5831           BFD_ASSERT (rel->r_offset % 8 == 0);
5832           ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5833           ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5834
5835           /* Mark the second slot of a GD or LD entry.
5836              -1 to indicate GD and -2 to indicate LD.  */
5837           if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5838             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5839           else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5840             ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5841           goto dodyn;
5842
5843         case R_PPC64_TPREL16:
5844         case R_PPC64_TPREL16_LO:
5845         case R_PPC64_TPREL16_HI:
5846         case R_PPC64_TPREL16_HA:
5847         case R_PPC64_TPREL16_DS:
5848         case R_PPC64_TPREL16_LO_DS:
5849         case R_PPC64_TPREL16_HIGH:
5850         case R_PPC64_TPREL16_HIGHA:
5851         case R_PPC64_TPREL16_HIGHER:
5852         case R_PPC64_TPREL16_HIGHERA:
5853         case R_PPC64_TPREL16_HIGHEST:
5854         case R_PPC64_TPREL16_HIGHESTA:
5855           if (bfd_link_dll (info))
5856             info->flags |= DF_STATIC_TLS;
5857           goto dodyn;
5858
5859         case R_PPC64_ADDR64:
5860           if (opd_sym_map != NULL
5861               && rel + 1 < rel_end
5862               && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5863             {
5864               if (h != NULL)
5865                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5866               else
5867                 {
5868                   asection *s;
5869                   Elf_Internal_Sym *isym;
5870
5871                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5872                                                 abfd, r_symndx);
5873                   if (isym == NULL)
5874                     return FALSE;
5875
5876                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5877                   if (s != NULL && s != sec)
5878                     opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5879                 }
5880             }
5881           /* Fall through.  */
5882
5883         case R_PPC64_ADDR16:
5884         case R_PPC64_ADDR16_DS:
5885         case R_PPC64_ADDR16_HA:
5886         case R_PPC64_ADDR16_HI:
5887         case R_PPC64_ADDR16_HIGH:
5888         case R_PPC64_ADDR16_HIGHA:
5889         case R_PPC64_ADDR16_HIGHER:
5890         case R_PPC64_ADDR16_HIGHERA:
5891         case R_PPC64_ADDR16_HIGHEST:
5892         case R_PPC64_ADDR16_HIGHESTA:
5893         case R_PPC64_ADDR16_LO:
5894         case R_PPC64_ADDR16_LO_DS:
5895           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5896               && rel->r_addend == 0)
5897             {
5898               /* We may need a .plt entry if this reloc refers to a
5899                  function in a shared lib.  */
5900               if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5901                 return FALSE;
5902               h->pointer_equality_needed = 1;
5903             }
5904           /* Fall through.  */
5905
5906         case R_PPC64_REL30:
5907         case R_PPC64_REL32:
5908         case R_PPC64_REL64:
5909         case R_PPC64_ADDR32:
5910         case R_PPC64_UADDR16:
5911         case R_PPC64_UADDR32:
5912         case R_PPC64_UADDR64:
5913         case R_PPC64_TOC:
5914           if (h != NULL && !bfd_link_pic (info))
5915             /* We may need a copy reloc.  */
5916             h->non_got_ref = 1;
5917
5918           /* Don't propagate .opd relocs.  */
5919           if (NO_OPD_RELOCS && opd_sym_map != NULL)
5920             break;
5921
5922           /* If we are creating a shared library, and this is a reloc
5923              against a global symbol, or a non PC relative reloc
5924              against a local symbol, then we need to copy the reloc
5925              into the shared library.  However, if we are linking with
5926              -Bsymbolic, we do not need to copy a reloc against a
5927              global symbol which is defined in an object we are
5928              including in the link (i.e., DEF_REGULAR is set).  At
5929              this point we have not seen all the input files, so it is
5930              possible that DEF_REGULAR is not set now but will be set
5931              later (it is never cleared).  In case of a weak definition,
5932              DEF_REGULAR may be cleared later by a strong definition in
5933              a shared library.  We account for that possibility below by
5934              storing information in the dyn_relocs field of the hash
5935              table entry.  A similar situation occurs when creating
5936              shared libraries and symbol visibility changes render the
5937              symbol local.
5938
5939              If on the other hand, we are creating an executable, we
5940              may need to keep relocations for symbols satisfied by a
5941              dynamic library if we manage to avoid copy relocs for the
5942              symbol.  */
5943         dodyn:
5944           if ((bfd_link_pic (info)
5945                && (must_be_dyn_reloc (info, r_type)
5946                    || (h != NULL
5947                        && (!SYMBOLIC_BIND (info, h)
5948                            || h->root.type == bfd_link_hash_defweak
5949                            || !h->def_regular))))
5950               || (ELIMINATE_COPY_RELOCS
5951                   && !bfd_link_pic (info)
5952                   && h != NULL
5953                   && (h->root.type == bfd_link_hash_defweak
5954                       || !h->def_regular))
5955               || (!bfd_link_pic (info)
5956                   && ifunc != NULL))
5957             {
5958               /* We must copy these reloc types into the output file.
5959                  Create a reloc section in dynobj and make room for
5960                  this reloc.  */
5961               if (sreloc == NULL)
5962                 {
5963                   sreloc = _bfd_elf_make_dynamic_reloc_section
5964                     (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5965
5966                   if (sreloc == NULL)
5967                     return FALSE;
5968                 }
5969
5970               /* If this is a global symbol, we count the number of
5971                  relocations we need for this symbol.  */
5972               if (h != NULL)
5973                 {
5974                   struct elf_dyn_relocs *p;
5975                   struct elf_dyn_relocs **head;
5976
5977                   head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5978                   p = *head;
5979                   if (p == NULL || p->sec != sec)
5980                     {
5981                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5982                       if (p == NULL)
5983                         return FALSE;
5984                       p->next = *head;
5985                       *head = p;
5986                       p->sec = sec;
5987                       p->count = 0;
5988                       p->pc_count = 0;
5989                     }
5990                   p->count += 1;
5991                   if (!must_be_dyn_reloc (info, r_type))
5992                     p->pc_count += 1;
5993                 }
5994               else
5995                 {
5996                   /* Track dynamic relocs needed for local syms too.
5997                      We really need local syms available to do this
5998                      easily.  Oh well.  */
5999                   struct ppc_dyn_relocs *p;
6000                   struct ppc_dyn_relocs **head;
6001                   bfd_boolean is_ifunc;
6002                   asection *s;
6003                   void *vpp;
6004                   Elf_Internal_Sym *isym;
6005
6006                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6007                                                 abfd, r_symndx);
6008                   if (isym == NULL)
6009                     return FALSE;
6010
6011                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6012                   if (s == NULL)
6013                     s = sec;
6014
6015                   vpp = &elf_section_data (s)->local_dynrel;
6016                   head = (struct ppc_dyn_relocs **) vpp;
6017                   is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6018                   p = *head;
6019                   if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6020                     p = p->next;
6021                   if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6022                     {
6023                       p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6024                       if (p == NULL)
6025                         return FALSE;
6026                       p->next = *head;
6027                       *head = p;
6028                       p->sec = sec;
6029                       p->ifunc = is_ifunc;
6030                       p->count = 0;
6031                     }
6032                   p->count += 1;
6033                 }
6034             }
6035           break;
6036
6037         default:
6038           break;
6039         }
6040     }
6041
6042   return TRUE;
6043 }
6044
6045 /* Merge backend specific data from an object file to the output
6046    object file when linking.  */
6047
6048 static bfd_boolean
6049 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6050 {
6051   bfd *obfd = info->output_bfd;
6052   unsigned long iflags, oflags;
6053
6054   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6055     return TRUE;
6056
6057   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6058     return TRUE;
6059
6060   if (!_bfd_generic_verify_endian_match (ibfd, info))
6061     return FALSE;
6062
6063   iflags = elf_elfheader (ibfd)->e_flags;
6064   oflags = elf_elfheader (obfd)->e_flags;
6065
6066   if (iflags & ~EF_PPC64_ABI)
6067     {
6068       _bfd_error_handler
6069         /* xgettext:c-format */
6070         (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6071       bfd_set_error (bfd_error_bad_value);
6072       return FALSE;
6073     }
6074   else if (iflags != oflags && iflags != 0)
6075     {
6076       _bfd_error_handler
6077         /* xgettext:c-format */
6078         (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6079          ibfd, iflags, oflags);
6080       bfd_set_error (bfd_error_bad_value);
6081       return FALSE;
6082     }
6083
6084   _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6085
6086   /* Merge Tag_compatibility attributes and any common GNU ones.  */
6087   _bfd_elf_merge_object_attributes (ibfd, info);
6088
6089   return TRUE;
6090 }
6091
6092 static bfd_boolean
6093 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6094 {
6095   /* Print normal ELF private data.  */
6096   _bfd_elf_print_private_bfd_data (abfd, ptr);
6097
6098   if (elf_elfheader (abfd)->e_flags != 0)
6099     {
6100       FILE *file = ptr;
6101
6102       fprintf (file, _("private flags = 0x%lx:"),
6103                elf_elfheader (abfd)->e_flags);
6104
6105       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6106         fprintf (file, _(" [abiv%ld]"),
6107                  elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6108       fputc ('\n', file);
6109     }
6110
6111   return TRUE;
6112 }
6113
6114 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
6115    of the code entry point, and its section, which must be in the same
6116    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
6117
6118 static bfd_vma
6119 opd_entry_value (asection *opd_sec,
6120                  bfd_vma offset,
6121                  asection **code_sec,
6122                  bfd_vma *code_off,
6123                  bfd_boolean in_code_sec)
6124 {
6125   bfd *opd_bfd = opd_sec->owner;
6126   Elf_Internal_Rela *relocs;
6127   Elf_Internal_Rela *lo, *hi, *look;
6128   bfd_vma val;
6129
6130   /* No relocs implies we are linking a --just-symbols object, or looking
6131      at a final linked executable with addr2line or somesuch.  */
6132   if (opd_sec->reloc_count == 0)
6133     {
6134       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6135
6136       if (contents == NULL)
6137         {
6138           if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6139             return (bfd_vma) -1;
6140           ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6141         }
6142
6143       /* PR 17512: file: 64b9dfbb.  */
6144       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6145         return (bfd_vma) -1;
6146
6147       val = bfd_get_64 (opd_bfd, contents + offset);
6148       if (code_sec != NULL)
6149         {
6150           asection *sec, *likely = NULL;
6151
6152           if (in_code_sec)
6153             {
6154               sec = *code_sec;
6155               if (sec->vma <= val
6156                   && val < sec->vma + sec->size)
6157                 likely = sec;
6158               else
6159                 val = -1;
6160             }
6161           else
6162             for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6163               if (sec->vma <= val
6164                   && (sec->flags & SEC_LOAD) != 0
6165                   && (sec->flags & SEC_ALLOC) != 0)
6166                 likely = sec;
6167           if (likely != NULL)
6168             {
6169               *code_sec = likely;
6170               if (code_off != NULL)
6171                 *code_off = val - likely->vma;
6172             }
6173         }
6174       return val;
6175     }
6176
6177   BFD_ASSERT (is_ppc64_elf (opd_bfd));
6178
6179   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6180   if (relocs == NULL)
6181     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6182   /* PR 17512: file: df8e1fd6.  */
6183   if (relocs == NULL)
6184     return (bfd_vma) -1;
6185
6186   /* Go find the opd reloc at the sym address.  */
6187   lo = relocs;
6188   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6189   val = (bfd_vma) -1;
6190   while (lo < hi)
6191     {
6192       look = lo + (hi - lo) / 2;
6193       if (look->r_offset < offset)
6194         lo = look + 1;
6195       else if (look->r_offset > offset)
6196         hi = look;
6197       else
6198         {
6199           Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6200
6201           if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6202               && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6203             {
6204               unsigned long symndx = ELF64_R_SYM (look->r_info);
6205               asection *sec = NULL;
6206
6207               if (symndx >= symtab_hdr->sh_info
6208                   && elf_sym_hashes (opd_bfd) != NULL)
6209                 {
6210                   struct elf_link_hash_entry **sym_hashes;
6211                   struct elf_link_hash_entry *rh;
6212
6213                   sym_hashes = elf_sym_hashes (opd_bfd);
6214                   rh = sym_hashes[symndx - symtab_hdr->sh_info];
6215                   if (rh != NULL)
6216                     {
6217                       rh = elf_follow_link (rh);
6218                       if (rh->root.type != bfd_link_hash_defined
6219                           && rh->root.type != bfd_link_hash_defweak)
6220                         break;
6221                       if (rh->root.u.def.section->owner == opd_bfd)
6222                         {
6223                           val = rh->root.u.def.value;
6224                           sec = rh->root.u.def.section;
6225                         }
6226                     }
6227                 }
6228
6229               if (sec == NULL)
6230                 {
6231                   Elf_Internal_Sym *sym;
6232
6233                   if (symndx < symtab_hdr->sh_info)
6234                     {
6235                       sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6236                       if (sym == NULL)
6237                         {
6238                           size_t symcnt = symtab_hdr->sh_info;
6239                           sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6240                                                       symcnt, 0,
6241                                                       NULL, NULL, NULL);
6242                           if (sym == NULL)
6243                             break;
6244                           symtab_hdr->contents = (bfd_byte *) sym;
6245                         }
6246                       sym += symndx;
6247                     }
6248                   else
6249                     {
6250                       sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6251                                                   1, symndx,
6252                                                   NULL, NULL, NULL);
6253                       if (sym == NULL)
6254                         break;
6255                     }
6256                   sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6257                   if (sec == NULL)
6258                     break;
6259                   BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6260                   val = sym->st_value;
6261                 }
6262
6263               val += look->r_addend;
6264               if (code_off != NULL)
6265                 *code_off = val;
6266               if (code_sec != NULL)
6267                 {
6268                   if (in_code_sec && *code_sec != sec)
6269                     return -1;
6270                   else
6271                     *code_sec = sec;
6272                 }
6273               if (sec->output_section != NULL)
6274                 val += sec->output_section->vma + sec->output_offset;
6275             }
6276           break;
6277         }
6278     }
6279
6280   return val;
6281 }
6282
6283 /* If the ELF symbol SYM might be a function in SEC, return the
6284    function size and set *CODE_OFF to the function's entry point,
6285    otherwise return zero.  */
6286
6287 static bfd_size_type
6288 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6289                               bfd_vma *code_off)
6290 {
6291   bfd_size_type size;
6292
6293   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6294                      | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6295     return 0;
6296
6297   size = 0;
6298   if (!(sym->flags & BSF_SYNTHETIC))
6299     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6300
6301   if (strcmp (sym->section->name, ".opd") == 0)
6302     {
6303       struct _opd_sec_data *opd = get_opd_info (sym->section);
6304       bfd_vma symval = sym->value;
6305
6306       if (opd != NULL
6307           && opd->adjust != NULL
6308           && elf_section_data (sym->section)->relocs != NULL)
6309         {
6310           /* opd_entry_value will use cached relocs that have been
6311              adjusted, but with raw symbols.  That means both local
6312              and global symbols need adjusting.  */
6313           long adjust = opd->adjust[OPD_NDX (symval)];
6314           if (adjust == -1)
6315             return 0;
6316           symval += adjust;
6317         }
6318
6319       if (opd_entry_value (sym->section, symval,
6320                            &sec, code_off, TRUE) == (bfd_vma) -1)
6321         return 0;
6322       /* An old ABI binary with dot-syms has a size of 24 on the .opd
6323          symbol.  This size has nothing to do with the code size of the
6324          function, which is what we're supposed to return, but the
6325          code size isn't available without looking up the dot-sym.
6326          However, doing that would be a waste of time particularly
6327          since elf_find_function will look at the dot-sym anyway.
6328          Now, elf_find_function will keep the largest size of any
6329          function sym found at the code address of interest, so return
6330          1 here to avoid it incorrectly caching a larger function size
6331          for a small function.  This does mean we return the wrong
6332          size for a new-ABI function of size 24, but all that does is
6333          disable caching for such functions.  */
6334       if (size == 24)
6335         size = 1;
6336     }
6337   else
6338     {
6339       if (sym->section != sec)
6340         return 0;
6341       *code_off = sym->value;
6342     }
6343   if (size == 0)
6344     size = 1;
6345   return size;
6346 }
6347
6348 /* Return true if symbol is a strong function defined in an ELFv2
6349    object with st_other localentry bits of zero, ie. its local entry
6350    point coincides with its global entry point.  */
6351
6352 static bfd_boolean
6353 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6354 {
6355   return (h != NULL
6356           && h->type == STT_FUNC
6357           && h->root.type == bfd_link_hash_defined
6358           && (STO_PPC64_LOCAL_MASK & h->other) == 0
6359           && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6360           && is_ppc64_elf (h->root.u.def.section->owner)
6361           && abiversion (h->root.u.def.section->owner) >= 2);
6362 }
6363
6364 /* Return true if symbol is defined in a regular object file.  */
6365
6366 static bfd_boolean
6367 is_static_defined (struct elf_link_hash_entry *h)
6368 {
6369   return ((h->root.type == bfd_link_hash_defined
6370            || h->root.type == bfd_link_hash_defweak)
6371           && h->root.u.def.section != NULL
6372           && h->root.u.def.section->output_section != NULL);
6373 }
6374
6375 /* If FDH is a function descriptor symbol, return the associated code
6376    entry symbol if it is defined.  Return NULL otherwise.  */
6377
6378 static struct ppc_link_hash_entry *
6379 defined_code_entry (struct ppc_link_hash_entry *fdh)
6380 {
6381   if (fdh->is_func_descriptor)
6382     {
6383       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6384       if (fh->elf.root.type == bfd_link_hash_defined
6385           || fh->elf.root.type == bfd_link_hash_defweak)
6386         return fh;
6387     }
6388   return NULL;
6389 }
6390
6391 /* If FH is a function code entry symbol, return the associated
6392    function descriptor symbol if it is defined.  Return NULL otherwise.  */
6393
6394 static struct ppc_link_hash_entry *
6395 defined_func_desc (struct ppc_link_hash_entry *fh)
6396 {
6397   if (fh->oh != NULL
6398       && fh->oh->is_func_descriptor)
6399     {
6400       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6401       if (fdh->elf.root.type == bfd_link_hash_defined
6402           || fdh->elf.root.type == bfd_link_hash_defweak)
6403         return fdh;
6404     }
6405   return NULL;
6406 }
6407
6408 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6409
6410 /* Garbage collect sections, after first dealing with dot-symbols.  */
6411
6412 static bfd_boolean
6413 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6414 {
6415   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6416
6417   if (htab != NULL && htab->need_func_desc_adj)
6418     {
6419       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6420       htab->need_func_desc_adj = 0;
6421     }
6422   return bfd_elf_gc_sections (abfd, info);
6423 }
6424
6425 /* Mark all our entry sym sections, both opd and code section.  */
6426
6427 static void
6428 ppc64_elf_gc_keep (struct bfd_link_info *info)
6429 {
6430   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6431   struct bfd_sym_chain *sym;
6432
6433   if (htab == NULL)
6434     return;
6435
6436   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6437     {
6438       struct ppc_link_hash_entry *eh, *fh;
6439       asection *sec;
6440
6441       eh = (struct ppc_link_hash_entry *)
6442         elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6443       if (eh == NULL)
6444         continue;
6445       if (eh->elf.root.type != bfd_link_hash_defined
6446           && eh->elf.root.type != bfd_link_hash_defweak)
6447         continue;
6448
6449       fh = defined_code_entry (eh);
6450       if (fh != NULL)
6451         {
6452           sec = fh->elf.root.u.def.section;
6453           sec->flags |= SEC_KEEP;
6454         }
6455       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6456                && opd_entry_value (eh->elf.root.u.def.section,
6457                                    eh->elf.root.u.def.value,
6458                                    &sec, NULL, FALSE) != (bfd_vma) -1)
6459         sec->flags |= SEC_KEEP;
6460
6461       sec = eh->elf.root.u.def.section;
6462       sec->flags |= SEC_KEEP;
6463     }
6464 }
6465
6466 /* Mark sections containing dynamically referenced symbols.  When
6467    building shared libraries, we must assume that any visible symbol is
6468    referenced.  */
6469
6470 static bfd_boolean
6471 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6472 {
6473   struct bfd_link_info *info = (struct bfd_link_info *) inf;
6474   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6475   struct ppc_link_hash_entry *fdh;
6476   struct bfd_elf_dynamic_list *d = info->dynamic_list;
6477
6478   /* Dynamic linking info is on the func descriptor sym.  */
6479   fdh = defined_func_desc (eh);
6480   if (fdh != NULL)
6481     eh = fdh;
6482
6483   if ((eh->elf.root.type == bfd_link_hash_defined
6484        || eh->elf.root.type == bfd_link_hash_defweak)
6485       && (eh->elf.ref_dynamic
6486           || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6487               && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6488               && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6489               && (!bfd_link_executable (info)
6490                   || info->gc_keep_exported
6491                   || info->export_dynamic
6492                   || (eh->elf.dynamic
6493                       && d != NULL
6494                       && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6495               && (eh->elf.versioned >= versioned
6496                   || !bfd_hide_sym_by_version (info->version_info,
6497                                                eh->elf.root.root.string)))))
6498     {
6499       asection *code_sec;
6500       struct ppc_link_hash_entry *fh;
6501
6502       eh->elf.root.u.def.section->flags |= SEC_KEEP;
6503
6504       /* Function descriptor syms cause the associated
6505          function code sym section to be marked.  */
6506       fh = defined_code_entry (eh);
6507       if (fh != NULL)
6508         {
6509           code_sec = fh->elf.root.u.def.section;
6510           code_sec->flags |= SEC_KEEP;
6511         }
6512       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6513                && opd_entry_value (eh->elf.root.u.def.section,
6514                                    eh->elf.root.u.def.value,
6515                                    &code_sec, NULL, FALSE) != (bfd_vma) -1)
6516         code_sec->flags |= SEC_KEEP;
6517     }
6518
6519   return TRUE;
6520 }
6521
6522 /* Return the section that should be marked against GC for a given
6523    relocation.  */
6524
6525 static asection *
6526 ppc64_elf_gc_mark_hook (asection *sec,
6527                         struct bfd_link_info *info,
6528                         Elf_Internal_Rela *rel,
6529                         struct elf_link_hash_entry *h,
6530                         Elf_Internal_Sym *sym)
6531 {
6532   asection *rsec;
6533
6534   /* Syms return NULL if we're marking .opd, so we avoid marking all
6535      function sections, as all functions are referenced in .opd.  */
6536   rsec = NULL;
6537   if (get_opd_info (sec) != NULL)
6538     return rsec;
6539
6540   if (h != NULL)
6541     {
6542       enum elf_ppc64_reloc_type r_type;
6543       struct ppc_link_hash_entry *eh, *fh, *fdh;
6544
6545       r_type = ELF64_R_TYPE (rel->r_info);
6546       switch (r_type)
6547         {
6548         case R_PPC64_GNU_VTINHERIT:
6549         case R_PPC64_GNU_VTENTRY:
6550           break;
6551
6552         default:
6553           switch (h->root.type)
6554             {
6555             case bfd_link_hash_defined:
6556             case bfd_link_hash_defweak:
6557               eh = (struct ppc_link_hash_entry *) h;
6558               fdh = defined_func_desc (eh);
6559               if (fdh != NULL)
6560                 {
6561                   /* -mcall-aixdesc code references the dot-symbol on
6562                      a call reloc.  Mark the function descriptor too
6563                      against garbage collection.  */
6564                   fdh->elf.mark = 1;
6565                   if (fdh->elf.u.weakdef != NULL)
6566                     fdh->elf.u.weakdef->mark = 1;
6567                   eh = fdh;
6568                 }
6569
6570               /* Function descriptor syms cause the associated
6571                  function code sym section to be marked.  */
6572               fh = defined_code_entry (eh);
6573               if (fh != NULL)
6574                 {
6575                   /* They also mark their opd section.  */
6576                   eh->elf.root.u.def.section->gc_mark = 1;
6577
6578                   rsec = fh->elf.root.u.def.section;
6579                 }
6580               else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6581                        && opd_entry_value (eh->elf.root.u.def.section,
6582                                            eh->elf.root.u.def.value,
6583                                            &rsec, NULL, FALSE) != (bfd_vma) -1)
6584                 eh->elf.root.u.def.section->gc_mark = 1;
6585               else
6586                 rsec = h->root.u.def.section;
6587               break;
6588
6589             case bfd_link_hash_common:
6590               rsec = h->root.u.c.p->section;
6591               break;
6592
6593             default:
6594               return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6595             }
6596         }
6597     }
6598   else
6599     {
6600       struct _opd_sec_data *opd;
6601
6602       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6603       opd = get_opd_info (rsec);
6604       if (opd != NULL && opd->func_sec != NULL)
6605         {
6606           rsec->gc_mark = 1;
6607
6608           rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6609         }
6610     }
6611
6612   return rsec;
6613 }
6614
6615 /* Update the .got, .plt. and dynamic reloc reference counts for the
6616    section being removed.  */
6617
6618 static bfd_boolean
6619 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6620                          asection *sec, const Elf_Internal_Rela *relocs)
6621 {
6622   struct ppc_link_hash_table *htab;
6623   Elf_Internal_Shdr *symtab_hdr;
6624   struct elf_link_hash_entry **sym_hashes;
6625   struct got_entry **local_got_ents;
6626   const Elf_Internal_Rela *rel, *relend;
6627
6628   if (bfd_link_relocatable (info))
6629     return TRUE;
6630
6631   if ((sec->flags & SEC_ALLOC) == 0)
6632     return TRUE;
6633
6634   elf_section_data (sec)->local_dynrel = NULL;
6635
6636   htab = ppc_hash_table (info);
6637   if (htab == NULL)
6638     return FALSE;
6639
6640   symtab_hdr = &elf_symtab_hdr (abfd);
6641   sym_hashes = elf_sym_hashes (abfd);
6642   local_got_ents = elf_local_got_ents (abfd);
6643
6644   relend = relocs + sec->reloc_count;
6645   for (rel = relocs; rel < relend; rel++)
6646     {
6647       unsigned long r_symndx;
6648       enum elf_ppc64_reloc_type r_type;
6649       struct elf_link_hash_entry *h = NULL;
6650       struct plt_entry **plt_list = NULL;
6651       unsigned char tls_type = 0;
6652
6653       r_symndx = ELF64_R_SYM (rel->r_info);
6654       r_type = ELF64_R_TYPE (rel->r_info);
6655       if (r_symndx >= symtab_hdr->sh_info)
6656         {
6657           struct ppc_link_hash_entry *eh;
6658           struct elf_dyn_relocs **pp;
6659           struct elf_dyn_relocs *p;
6660
6661           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6662           h = elf_follow_link (h);
6663           eh = (struct ppc_link_hash_entry *) h;
6664
6665           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6666             if (p->sec == sec)
6667               {
6668                 /* Everything must go for SEC.  */
6669                 *pp = p->next;
6670                 break;
6671               }
6672         }
6673
6674       switch (r_type)
6675         {
6676         case R_PPC64_GOT_TLSLD16:
6677         case R_PPC64_GOT_TLSLD16_LO:
6678         case R_PPC64_GOT_TLSLD16_HI:
6679         case R_PPC64_GOT_TLSLD16_HA:
6680           tls_type = TLS_TLS | TLS_LD;
6681           goto dogot;
6682
6683         case R_PPC64_GOT_TLSGD16:
6684         case R_PPC64_GOT_TLSGD16_LO:
6685         case R_PPC64_GOT_TLSGD16_HI:
6686         case R_PPC64_GOT_TLSGD16_HA:
6687           tls_type = TLS_TLS | TLS_GD;
6688           goto dogot;
6689
6690         case R_PPC64_GOT_TPREL16_DS:
6691         case R_PPC64_GOT_TPREL16_LO_DS:
6692         case R_PPC64_GOT_TPREL16_HI:
6693         case R_PPC64_GOT_TPREL16_HA:
6694           tls_type = TLS_TLS | TLS_TPREL;
6695           goto dogot;
6696
6697         case R_PPC64_GOT_DTPREL16_DS:
6698         case R_PPC64_GOT_DTPREL16_LO_DS:
6699         case R_PPC64_GOT_DTPREL16_HI:
6700         case R_PPC64_GOT_DTPREL16_HA:
6701           tls_type = TLS_TLS | TLS_DTPREL;
6702           goto dogot;
6703
6704         case R_PPC64_GOT16:
6705         case R_PPC64_GOT16_DS:
6706         case R_PPC64_GOT16_HA:
6707         case R_PPC64_GOT16_HI:
6708         case R_PPC64_GOT16_LO:
6709         case R_PPC64_GOT16_LO_DS:
6710         dogot:
6711           {
6712             struct got_entry *ent;
6713
6714             if (h != NULL)
6715               ent = h->got.glist;
6716             else
6717               ent = local_got_ents[r_symndx];
6718
6719             for (; ent != NULL; ent = ent->next)
6720               if (ent->addend == rel->r_addend
6721                   && ent->owner == abfd
6722                   && ent->tls_type == tls_type)
6723                 break;
6724             if (ent == NULL)
6725               abort ();
6726             if (ent->got.refcount > 0)
6727               ent->got.refcount -= 1;
6728           }
6729           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
6730             plt_list = &h->plt.plist;
6731           break;
6732
6733         case R_PPC64_PLT16_HA:
6734         case R_PPC64_PLT16_HI:
6735         case R_PPC64_PLT16_LO:
6736         case R_PPC64_PLT32:
6737         case R_PPC64_PLT64:
6738         case R_PPC64_REL14:
6739         case R_PPC64_REL14_BRNTAKEN:
6740         case R_PPC64_REL14_BRTAKEN:
6741         case R_PPC64_REL24:
6742           if (h != NULL)
6743             plt_list = &h->plt.plist;
6744           else if (local_got_ents != NULL)
6745             {
6746               struct plt_entry **local_plt = (struct plt_entry **)
6747                 (local_got_ents + symtab_hdr->sh_info);
6748               unsigned char *local_got_tls_masks = (unsigned char *)
6749                 (local_plt + symtab_hdr->sh_info);
6750               if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6751                 plt_list = local_plt + r_symndx;
6752             }
6753           break;
6754
6755         case R_PPC64_ADDR64:
6756         case R_PPC64_ADDR16:
6757         case R_PPC64_ADDR16_DS:
6758         case R_PPC64_ADDR16_HA:
6759         case R_PPC64_ADDR16_HI:
6760         case R_PPC64_ADDR16_HIGH:
6761         case R_PPC64_ADDR16_HIGHA:
6762         case R_PPC64_ADDR16_HIGHER:
6763         case R_PPC64_ADDR16_HIGHERA:
6764         case R_PPC64_ADDR16_HIGHEST:
6765         case R_PPC64_ADDR16_HIGHESTA:
6766         case R_PPC64_ADDR16_LO:
6767         case R_PPC64_ADDR16_LO_DS:
6768           if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
6769               && rel->r_addend == 0)
6770             plt_list = &h->plt.plist;
6771           break;
6772
6773         default:
6774           break;
6775         }
6776       if (plt_list != NULL)
6777         {
6778           struct plt_entry *ent;
6779
6780           for (ent = *plt_list; ent != NULL; ent = ent->next)
6781             if (ent->addend == rel->r_addend)
6782               break;
6783           if (ent != NULL && ent->plt.refcount > 0)
6784             ent->plt.refcount -= 1;
6785         }
6786     }
6787   return TRUE;
6788 }
6789
6790 /* The maximum size of .sfpr.  */
6791 #define SFPR_MAX (218*4)
6792
6793 struct sfpr_def_parms
6794 {
6795   const char name[12];
6796   unsigned char lo, hi;
6797   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6798   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6799 };
6800
6801 /* Auto-generate _save*, _rest* functions in .sfpr.
6802    If STUB_SEC is non-null, define alias symbols in STUB_SEC
6803    instead.  */
6804
6805 static bfd_boolean
6806 sfpr_define (struct bfd_link_info *info,
6807              const struct sfpr_def_parms *parm,
6808              asection *stub_sec)
6809 {
6810   struct ppc_link_hash_table *htab = ppc_hash_table (info);
6811   unsigned int i;
6812   size_t len = strlen (parm->name);
6813   bfd_boolean writing = FALSE;
6814   char sym[16];
6815
6816   if (htab == NULL)
6817     return FALSE;
6818
6819   memcpy (sym, parm->name, len);
6820   sym[len + 2] = 0;
6821
6822   for (i = parm->lo; i <= parm->hi; i++)
6823     {
6824       struct ppc_link_hash_entry *h;
6825
6826       sym[len + 0] = i / 10 + '0';
6827       sym[len + 1] = i % 10 + '0';
6828       h = (struct ppc_link_hash_entry *)
6829         elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6830       if (stub_sec != NULL)
6831         {
6832           if (h != NULL
6833               && h->elf.root.type == bfd_link_hash_defined
6834               && h->elf.root.u.def.section == htab->sfpr)
6835             {
6836               struct elf_link_hash_entry *s;
6837               char buf[32];
6838               sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6839               s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6840               if (s == NULL)
6841                 return FALSE;
6842               if (s->root.type == bfd_link_hash_new
6843                   || (s->root.type = bfd_link_hash_defined
6844                       && s->root.u.def.section == stub_sec))
6845                 {
6846                   s->root.type = bfd_link_hash_defined;
6847                   s->root.u.def.section = stub_sec;
6848                   s->root.u.def.value = (stub_sec->size
6849                                          + h->elf.root.u.def.value);
6850                   s->ref_regular = 1;
6851                   s->def_regular = 1;
6852                   s->ref_regular_nonweak = 1;
6853                   s->forced_local = 1;
6854                   s->non_elf = 0;
6855                   s->root.linker_def = 1;
6856                 }
6857             }
6858           continue;
6859         }
6860       if (h != NULL)
6861         {
6862           h->save_res = 1;
6863           if (!h->elf.def_regular)
6864             {
6865               h->elf.root.type = bfd_link_hash_defined;
6866               h->elf.root.u.def.section = htab->sfpr;
6867               h->elf.root.u.def.value = htab->sfpr->size;
6868               h->elf.type = STT_FUNC;
6869               h->elf.def_regular = 1;
6870               h->elf.non_elf = 0;
6871               _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6872               writing = TRUE;
6873               if (htab->sfpr->contents == NULL)
6874                 {
6875                   htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6876                   if (htab->sfpr->contents == NULL)
6877                     return FALSE;
6878                 }
6879             }
6880         }
6881       if (writing)
6882         {
6883           bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6884           if (i != parm->hi)
6885             p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6886           else
6887             p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6888           htab->sfpr->size = p - htab->sfpr->contents;
6889         }
6890     }
6891
6892   return TRUE;
6893 }
6894
6895 static bfd_byte *
6896 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6897 {
6898   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6899   return p + 4;
6900 }
6901
6902 static bfd_byte *
6903 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6904 {
6905   p = savegpr0 (abfd, p, r);
6906   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6907   p = p + 4;
6908   bfd_put_32 (abfd, BLR, p);
6909   return p + 4;
6910 }
6911
6912 static bfd_byte *
6913 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6914 {
6915   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6916   return p + 4;
6917 }
6918
6919 static bfd_byte *
6920 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6921 {
6922   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6923   p = p + 4;
6924   p = restgpr0 (abfd, p, r);
6925   bfd_put_32 (abfd, MTLR_R0, p);
6926   p = p + 4;
6927   if (r == 29)
6928     {
6929       p = restgpr0 (abfd, p, 30);
6930       p = restgpr0 (abfd, p, 31);
6931     }
6932   bfd_put_32 (abfd, BLR, p);
6933   return p + 4;
6934 }
6935
6936 static bfd_byte *
6937 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6938 {
6939   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6940   return p + 4;
6941 }
6942
6943 static bfd_byte *
6944 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6945 {
6946   p = savegpr1 (abfd, p, r);
6947   bfd_put_32 (abfd, BLR, p);
6948   return p + 4;
6949 }
6950
6951 static bfd_byte *
6952 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6953 {
6954   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6955   return p + 4;
6956 }
6957
6958 static bfd_byte *
6959 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6960 {
6961   p = restgpr1 (abfd, p, r);
6962   bfd_put_32 (abfd, BLR, p);
6963   return p + 4;
6964 }
6965
6966 static bfd_byte *
6967 savefpr (bfd *abfd, bfd_byte *p, int r)
6968 {
6969   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6970   return p + 4;
6971 }
6972
6973 static bfd_byte *
6974 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6975 {
6976   p = savefpr (abfd, p, r);
6977   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6978   p = p + 4;
6979   bfd_put_32 (abfd, BLR, p);
6980   return p + 4;
6981 }
6982
6983 static bfd_byte *
6984 restfpr (bfd *abfd, bfd_byte *p, int r)
6985 {
6986   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6987   return p + 4;
6988 }
6989
6990 static bfd_byte *
6991 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6992 {
6993   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6994   p = p + 4;
6995   p = restfpr (abfd, p, r);
6996   bfd_put_32 (abfd, MTLR_R0, p);
6997   p = p + 4;
6998   if (r == 29)
6999     {
7000       p = restfpr (abfd, p, 30);
7001       p = restfpr (abfd, p, 31);
7002     }
7003   bfd_put_32 (abfd, BLR, p);
7004   return p + 4;
7005 }
7006
7007 static bfd_byte *
7008 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7009 {
7010   p = savefpr (abfd, p, r);
7011   bfd_put_32 (abfd, BLR, p);
7012   return p + 4;
7013 }
7014
7015 static bfd_byte *
7016 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7017 {
7018   p = restfpr (abfd, p, r);
7019   bfd_put_32 (abfd, BLR, p);
7020   return p + 4;
7021 }
7022
7023 static bfd_byte *
7024 savevr (bfd *abfd, bfd_byte *p, int r)
7025 {
7026   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7027   p = p + 4;
7028   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7029   return p + 4;
7030 }
7031
7032 static bfd_byte *
7033 savevr_tail (bfd *abfd, bfd_byte *p, int r)
7034 {
7035   p = savevr (abfd, p, r);
7036   bfd_put_32 (abfd, BLR, p);
7037   return p + 4;
7038 }
7039
7040 static bfd_byte *
7041 restvr (bfd *abfd, bfd_byte *p, int r)
7042 {
7043   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7044   p = p + 4;
7045   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7046   return p + 4;
7047 }
7048
7049 static bfd_byte *
7050 restvr_tail (bfd *abfd, bfd_byte *p, int r)
7051 {
7052   p = restvr (abfd, p, r);
7053   bfd_put_32 (abfd, BLR, p);
7054   return p + 4;
7055 }
7056
7057 /* Called via elf_link_hash_traverse to transfer dynamic linking
7058    information on function code symbol entries to their corresponding
7059    function descriptor symbol entries.  */
7060
7061 static bfd_boolean
7062 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
7063 {
7064   struct bfd_link_info *info;
7065   struct ppc_link_hash_table *htab;
7066   struct ppc_link_hash_entry *fh;
7067   struct ppc_link_hash_entry *fdh;
7068   bfd_boolean force_local;
7069
7070   fh = (struct ppc_link_hash_entry *) h;
7071   if (fh->elf.root.type == bfd_link_hash_indirect)
7072     return TRUE;
7073
7074   if (!fh->is_func)
7075     return TRUE;
7076
7077   if (fh->elf.root.root.string[0] != '.'
7078       || fh->elf.root.root.string[1] == '\0')
7079     return TRUE;
7080
7081   info = inf;
7082   htab = ppc_hash_table (info);
7083   if (htab == NULL)
7084     return FALSE;
7085
7086   /* Find the corresponding function descriptor symbol.  */
7087   fdh = lookup_fdh (fh, htab);
7088
7089   /* Resolve undefined references to dot-symbols as the value
7090      in the function descriptor, if we have one in a regular object.
7091      This is to satisfy cases like ".quad .foo".  Calls to functions
7092      in dynamic objects are handled elsewhere.  */
7093   if ((fh->elf.root.type == bfd_link_hash_undefined
7094        || fh->elf.root.type == bfd_link_hash_undefweak)
7095       && (fdh->elf.root.type == bfd_link_hash_defined
7096           || fdh->elf.root.type == bfd_link_hash_defweak)
7097       && get_opd_info (fdh->elf.root.u.def.section) != NULL
7098       && opd_entry_value (fdh->elf.root.u.def.section,
7099                           fdh->elf.root.u.def.value,
7100                           &fh->elf.root.u.def.section,
7101                           &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7102     {
7103       fh->elf.root.type = fdh->elf.root.type;
7104       fh->elf.forced_local = 1;
7105       fh->elf.def_regular = fdh->elf.def_regular;
7106       fh->elf.def_dynamic = fdh->elf.def_dynamic;
7107     }
7108
7109   if (!fh->elf.dynamic)
7110     {
7111       struct plt_entry *ent;
7112
7113       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7114         if (ent->plt.refcount > 0)
7115           break;
7116       if (ent == NULL)
7117         return TRUE;
7118     }
7119
7120   /* Create a descriptor as undefined if necessary.  */
7121   if (fdh == NULL
7122       && !bfd_link_executable (info)
7123       && (fh->elf.root.type == bfd_link_hash_undefined
7124           || fh->elf.root.type == bfd_link_hash_undefweak))
7125     {
7126       fdh = make_fdh (info, fh);
7127       if (fdh == NULL)
7128         return FALSE;
7129     }
7130
7131   /* We can't support overriding of symbols on a fake descriptor.  */
7132   if (fdh != NULL
7133       && fdh->fake
7134       && (fh->elf.root.type == bfd_link_hash_defined
7135           || fh->elf.root.type == bfd_link_hash_defweak))
7136     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7137
7138   /* Transfer dynamic linking information to the function descriptor.  */
7139   if (fdh != NULL)
7140     {
7141       fdh->elf.ref_regular |= fh->elf.ref_regular;
7142       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7143       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7144       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7145       fdh->elf.dynamic |= fh->elf.dynamic;
7146       fdh->elf.needs_plt |= (fh->elf.needs_plt
7147                              || fh->elf.type == STT_FUNC
7148                              || fh->elf.type == STT_GNU_IFUNC);
7149       move_plt_plist (fh, fdh);
7150
7151       if (!fdh->elf.forced_local
7152           && fh->elf.dynindx != -1)
7153         if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7154           return FALSE;
7155     }
7156
7157   /* Now that the info is on the function descriptor, clear the
7158      function code sym info.  Any function code syms for which we
7159      don't have a definition in a regular file, we force local.
7160      This prevents a shared library from exporting syms that have
7161      been imported from another library.  Function code syms that
7162      are really in the library we must leave global to prevent the
7163      linker dragging in a definition from a static library.  */
7164   force_local = (!fh->elf.def_regular
7165                  || fdh == NULL
7166                  || !fdh->elf.def_regular
7167                  || fdh->elf.forced_local);
7168   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7169
7170   return TRUE;
7171 }
7172
7173 static const struct sfpr_def_parms save_res_funcs[] =
7174   {
7175     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7176     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7177     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7178     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7179     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7180     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7181     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7182     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7183     { "._savef", 14, 31, savefpr, savefpr1_tail },
7184     { "._restf", 14, 31, restfpr, restfpr1_tail },
7185     { "_savevr_", 20, 31, savevr, savevr_tail },
7186     { "_restvr_", 20, 31, restvr, restvr_tail }
7187   };
7188
7189 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
7190    this hook to a) provide some gcc support functions, and b) transfer
7191    dynamic linking information gathered so far on function code symbol
7192    entries, to their corresponding function descriptor symbol entries.  */
7193
7194 static bfd_boolean
7195 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7196                             struct bfd_link_info *info)
7197 {
7198   struct ppc_link_hash_table *htab;
7199
7200   htab = ppc_hash_table (info);
7201   if (htab == NULL)
7202     return FALSE;
7203
7204   /* Provide any missing _save* and _rest* functions.  */
7205   if (htab->sfpr != NULL)
7206     {
7207       unsigned int i;
7208
7209       htab->sfpr->size = 0;
7210       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7211         if (!sfpr_define (info, &save_res_funcs[i], NULL))
7212           return FALSE;
7213       if (htab->sfpr->size == 0)
7214         htab->sfpr->flags |= SEC_EXCLUDE;
7215     }
7216
7217   if (bfd_link_relocatable (info))
7218     return TRUE;
7219
7220   if (htab->elf.hgot != NULL)
7221     {
7222       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7223       /* Make .TOC. defined so as to prevent it being made dynamic.
7224          The wrong value here is fixed later in ppc64_elf_set_toc.  */
7225       if (!htab->elf.hgot->def_regular
7226           || htab->elf.hgot->root.type != bfd_link_hash_defined)
7227         {
7228           htab->elf.hgot->root.type = bfd_link_hash_defined;
7229           htab->elf.hgot->root.u.def.value = 0;
7230           htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7231           htab->elf.hgot->def_regular = 1;
7232           htab->elf.hgot->root.linker_def = 1;
7233         }
7234       htab->elf.hgot->type = STT_OBJECT;
7235       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7236                                | STV_HIDDEN);
7237     }
7238
7239   if (htab->need_func_desc_adj)
7240     {
7241       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7242       htab->need_func_desc_adj = 0;
7243     }
7244
7245   return TRUE;
7246 }
7247
7248 /* Return true if we have dynamic relocs against H that apply to
7249    read-only sections.  */
7250
7251 static bfd_boolean
7252 readonly_dynrelocs (struct elf_link_hash_entry *h)
7253 {
7254   struct ppc_link_hash_entry *eh;
7255   struct elf_dyn_relocs *p;
7256
7257   eh = (struct ppc_link_hash_entry *) h;
7258   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7259     {
7260       asection *s = p->sec->output_section;
7261
7262       if (s != NULL && (s->flags & SEC_READONLY) != 0)
7263         return TRUE;
7264     }
7265   return FALSE;
7266 }
7267
7268 /* Return true if we have dynamic relocs against H or any of its weak
7269    aliases, that apply to read-only sections.  */
7270
7271 static bfd_boolean
7272 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7273 {
7274   struct ppc_link_hash_entry *eh;
7275
7276   eh = (struct ppc_link_hash_entry *) h;
7277   do
7278     {
7279       if (readonly_dynrelocs (&eh->elf))
7280         return TRUE;
7281       eh = eh->weakref;
7282     } while (eh != NULL && &eh->elf != h);
7283
7284   return FALSE;
7285 }
7286
7287 /* Return whether EH has pc-relative dynamic relocs.  */
7288
7289 static bfd_boolean
7290 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7291 {
7292   struct elf_dyn_relocs *p;
7293
7294   for (p = eh->dyn_relocs; p != NULL; p = p->next)
7295     if (p->pc_count != 0)
7296       return TRUE;
7297   return FALSE;
7298 }
7299
7300 /* Return true if a global entry stub will be created for H.  Valid
7301    for ELFv2 before plt entries have been allocated.  */
7302
7303 static bfd_boolean
7304 global_entry_stub (struct elf_link_hash_entry *h)
7305 {
7306   struct plt_entry *pent;
7307
7308   if (!h->pointer_equality_needed
7309       || h->def_regular)
7310     return FALSE;
7311
7312   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7313     if (pent->plt.refcount > 0
7314         && pent->addend == 0)
7315       return TRUE;
7316
7317   return FALSE;
7318 }
7319
7320 /* Adjust a symbol defined by a dynamic object and referenced by a
7321    regular object.  The current definition is in some section of the
7322    dynamic object, but we're not including those sections.  We have to
7323    change the definition to something the rest of the link can
7324    understand.  */
7325
7326 static bfd_boolean
7327 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7328                                  struct elf_link_hash_entry *h)
7329 {
7330   struct ppc_link_hash_table *htab;
7331   asection *s, *srel;
7332
7333   htab = ppc_hash_table (info);
7334   if (htab == NULL)
7335     return FALSE;
7336
7337   /* Deal with function syms.  */
7338   if (h->type == STT_FUNC
7339       || h->type == STT_GNU_IFUNC
7340       || h->needs_plt)
7341     {
7342       /* Clear procedure linkage table information for any symbol that
7343          won't need a .plt entry.  */
7344       struct plt_entry *ent;
7345       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7346         if (ent->plt.refcount > 0)
7347           break;
7348       if (ent == NULL
7349           || (h->type != STT_GNU_IFUNC
7350               && (SYMBOL_CALLS_LOCAL (info, h)
7351                   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
7352           || ((struct ppc_link_hash_entry *) h)->save_res)
7353         {
7354           h->plt.plist = NULL;
7355           h->needs_plt = 0;
7356           h->pointer_equality_needed = 0;
7357         }
7358       else if (abiversion (info->output_bfd) >= 2)
7359         {
7360           /* Taking a function's address in a read/write section
7361              doesn't require us to define the function symbol in the
7362              executable on a global entry stub.  A dynamic reloc can
7363              be used instead.  The reason we prefer a few more dynamic
7364              relocs is that calling via a global entry stub costs a
7365              few more instructions, and pointer_equality_needed causes
7366              extra work in ld.so when resolving these symbols.  */
7367           if (global_entry_stub (h)
7368               && !alias_readonly_dynrelocs (h))
7369             {
7370               h->pointer_equality_needed = 0;
7371               /* After adjust_dynamic_symbol, non_got_ref set in
7372                  the non-pic case means that dyn_relocs for this
7373                  symbol should be discarded.  */
7374               h->non_got_ref = 0;
7375             }
7376
7377           /* If making a plt entry, then we don't need copy relocs.  */
7378           return TRUE;
7379         }
7380     }
7381   else
7382     h->plt.plist = NULL;
7383
7384   /* If this is a weak symbol, and there is a real definition, the
7385      processor independent code will have arranged for us to see the
7386      real definition first, and we can just use the same value.  */
7387   if (h->u.weakdef != NULL)
7388     {
7389       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7390                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
7391       h->root.u.def.section = h->u.weakdef->root.u.def.section;
7392       h->root.u.def.value = h->u.weakdef->root.u.def.value;
7393       if (ELIMINATE_COPY_RELOCS)
7394         h->non_got_ref = h->u.weakdef->non_got_ref;
7395       return TRUE;
7396     }
7397
7398   /* If we are creating a shared library, we must presume that the
7399      only references to the symbol are via the global offset table.
7400      For such cases we need not do anything here; the relocations will
7401      be handled correctly by relocate_section.  */
7402   if (bfd_link_pic (info))
7403     return TRUE;
7404
7405   /* If there are no references to this symbol that do not use the
7406      GOT, we don't need to generate a copy reloc.  */
7407   if (!h->non_got_ref)
7408     return TRUE;
7409
7410   /* Don't generate a copy reloc for symbols defined in the executable.  */
7411   if (!h->def_dynamic || !h->ref_regular || h->def_regular
7412
7413       /* If -z nocopyreloc was given, don't generate them either.  */
7414       || info->nocopyreloc
7415
7416       /* If we didn't find any dynamic relocs in read-only sections, then
7417          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
7418       || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7419
7420       /* Protected variables do not work with .dynbss.  The copy in
7421          .dynbss won't be used by the shared library with the protected
7422          definition for the variable.  Text relocations are preferable
7423          to an incorrect program.  */
7424       || h->protected_def)
7425     {
7426       h->non_got_ref = 0;
7427       return TRUE;
7428     }
7429
7430   if (h->plt.plist != NULL)
7431     {
7432       /* We should never get here, but unfortunately there are versions
7433          of gcc out there that improperly (for this ABI) put initialized
7434          function pointers, vtable refs and suchlike in read-only
7435          sections.  Allow them to proceed, but warn that this might
7436          break at runtime.  */
7437       info->callbacks->einfo
7438         (_("%P: copy reloc against `%T' requires lazy plt linking; "
7439            "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7440          h->root.root.string);
7441     }
7442
7443   /* This is a reference to a symbol defined by a dynamic object which
7444      is not a function.  */
7445
7446   /* We must allocate the symbol in our .dynbss section, which will
7447      become part of the .bss section of the executable.  There will be
7448      an entry for this symbol in the .dynsym section.  The dynamic
7449      object will contain position independent code, so all references
7450      from the dynamic object to this symbol will go through the global
7451      offset table.  The dynamic linker will use the .dynsym entry to
7452      determine the address it must put in the global offset table, so
7453      both the dynamic object and the regular object will refer to the
7454      same memory location for the variable.  */
7455
7456   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7457      to copy the initial value out of the dynamic object and into the
7458      runtime process image.  We need to remember the offset into the
7459      .rela.bss section we are going to use.  */
7460   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7461     {
7462       s = htab->elf.sdynrelro;
7463       srel = htab->elf.sreldynrelro;
7464     }
7465   else
7466     {
7467       s = htab->elf.sdynbss;
7468       srel = htab->elf.srelbss;
7469     }
7470   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7471     {
7472       srel->size += sizeof (Elf64_External_Rela);
7473       h->needs_copy = 1;
7474     }
7475
7476   return _bfd_elf_adjust_dynamic_copy (info, h, s);
7477 }
7478
7479 /* If given a function descriptor symbol, hide both the function code
7480    sym and the descriptor.  */
7481 static void
7482 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7483                        struct elf_link_hash_entry *h,
7484                        bfd_boolean force_local)
7485 {
7486   struct ppc_link_hash_entry *eh;
7487   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7488
7489   eh = (struct ppc_link_hash_entry *) h;
7490   if (eh->is_func_descriptor)
7491     {
7492       struct ppc_link_hash_entry *fh = eh->oh;
7493
7494       if (fh == NULL)
7495         {
7496           const char *p, *q;
7497           struct elf_link_hash_table *htab = elf_hash_table (info);
7498           char save;
7499
7500           /* We aren't supposed to use alloca in BFD because on
7501              systems which do not have alloca the version in libiberty
7502              calls xmalloc, which might cause the program to crash
7503              when it runs out of memory.  This function doesn't have a
7504              return status, so there's no way to gracefully return an
7505              error.  So cheat.  We know that string[-1] can be safely
7506              accessed;  It's either a string in an ELF string table,
7507              or allocated in an objalloc structure.  */
7508
7509           p = eh->elf.root.root.string - 1;
7510           save = *p;
7511           *(char *) p = '.';
7512           fh = (struct ppc_link_hash_entry *)
7513             elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7514           *(char *) p = save;
7515
7516           /* Unfortunately, if it so happens that the string we were
7517              looking for was allocated immediately before this string,
7518              then we overwrote the string terminator.  That's the only
7519              reason the lookup should fail.  */
7520           if (fh == NULL)
7521             {
7522               q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7523               while (q >= eh->elf.root.root.string && *q == *p)
7524                 --q, --p;
7525               if (q < eh->elf.root.root.string && *p == '.')
7526                 fh = (struct ppc_link_hash_entry *)
7527                   elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7528             }
7529           if (fh != NULL)
7530             {
7531               eh->oh = fh;
7532               fh->oh = eh;
7533             }
7534         }
7535       if (fh != NULL)
7536         _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7537     }
7538 }
7539
7540 static bfd_boolean
7541 get_sym_h (struct elf_link_hash_entry **hp,
7542            Elf_Internal_Sym **symp,
7543            asection **symsecp,
7544            unsigned char **tls_maskp,
7545            Elf_Internal_Sym **locsymsp,
7546            unsigned long r_symndx,
7547            bfd *ibfd)
7548 {
7549   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7550
7551   if (r_symndx >= symtab_hdr->sh_info)
7552     {
7553       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7554       struct elf_link_hash_entry *h;
7555
7556       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7557       h = elf_follow_link (h);
7558
7559       if (hp != NULL)
7560         *hp = h;
7561
7562       if (symp != NULL)
7563         *symp = NULL;
7564
7565       if (symsecp != NULL)
7566         {
7567           asection *symsec = NULL;
7568           if (h->root.type == bfd_link_hash_defined
7569               || h->root.type == bfd_link_hash_defweak)
7570             symsec = h->root.u.def.section;
7571           *symsecp = symsec;
7572         }
7573
7574       if (tls_maskp != NULL)
7575         {
7576           struct ppc_link_hash_entry *eh;
7577
7578           eh = (struct ppc_link_hash_entry *) h;
7579           *tls_maskp = &eh->tls_mask;
7580         }
7581     }
7582   else
7583     {
7584       Elf_Internal_Sym *sym;
7585       Elf_Internal_Sym *locsyms = *locsymsp;
7586
7587       if (locsyms == NULL)
7588         {
7589           locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7590           if (locsyms == NULL)
7591             locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7592                                             symtab_hdr->sh_info,
7593                                             0, NULL, NULL, NULL);
7594           if (locsyms == NULL)
7595             return FALSE;
7596           *locsymsp = locsyms;
7597         }
7598       sym = locsyms + r_symndx;
7599
7600       if (hp != NULL)
7601         *hp = NULL;
7602
7603       if (symp != NULL)
7604         *symp = sym;
7605
7606       if (symsecp != NULL)
7607         *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7608
7609       if (tls_maskp != NULL)
7610         {
7611           struct got_entry **lgot_ents;
7612           unsigned char *tls_mask;
7613
7614           tls_mask = NULL;
7615           lgot_ents = elf_local_got_ents (ibfd);
7616           if (lgot_ents != NULL)
7617             {
7618               struct plt_entry **local_plt = (struct plt_entry **)
7619                 (lgot_ents + symtab_hdr->sh_info);
7620               unsigned char *lgot_masks = (unsigned char *)
7621                 (local_plt + symtab_hdr->sh_info);
7622               tls_mask = &lgot_masks[r_symndx];
7623             }
7624           *tls_maskp = tls_mask;
7625         }
7626     }
7627   return TRUE;
7628 }
7629
7630 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
7631    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7632    type suitable for optimization, and 1 otherwise.  */
7633
7634 static int
7635 get_tls_mask (unsigned char **tls_maskp,
7636               unsigned long *toc_symndx,
7637               bfd_vma *toc_addend,
7638               Elf_Internal_Sym **locsymsp,
7639               const Elf_Internal_Rela *rel,
7640               bfd *ibfd)
7641 {
7642   unsigned long r_symndx;
7643   int next_r;
7644   struct elf_link_hash_entry *h;
7645   Elf_Internal_Sym *sym;
7646   asection *sec;
7647   bfd_vma off;
7648
7649   r_symndx = ELF64_R_SYM (rel->r_info);
7650   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7651     return 0;
7652
7653   if ((*tls_maskp != NULL && **tls_maskp != 0)
7654       || sec == NULL
7655       || ppc64_elf_section_data (sec) == NULL
7656       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7657     return 1;
7658
7659   /* Look inside a TOC section too.  */
7660   if (h != NULL)
7661     {
7662       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7663       off = h->root.u.def.value;
7664     }
7665   else
7666     off = sym->st_value;
7667   off += rel->r_addend;
7668   BFD_ASSERT (off % 8 == 0);
7669   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7670   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7671   if (toc_symndx != NULL)
7672     *toc_symndx = r_symndx;
7673   if (toc_addend != NULL)
7674     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7675   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7676     return 0;
7677   if ((h == NULL || is_static_defined (h))
7678       && (next_r == -1 || next_r == -2))
7679     return 1 - next_r;
7680   return 1;
7681 }
7682
7683 /* Find (or create) an entry in the tocsave hash table.  */
7684
7685 static struct tocsave_entry *
7686 tocsave_find (struct ppc_link_hash_table *htab,
7687               enum insert_option insert,
7688               Elf_Internal_Sym **local_syms,
7689               const Elf_Internal_Rela *irela,
7690               bfd *ibfd)
7691 {
7692   unsigned long r_indx;
7693   struct elf_link_hash_entry *h;
7694   Elf_Internal_Sym *sym;
7695   struct tocsave_entry ent, *p;
7696   hashval_t hash;
7697   struct tocsave_entry **slot;
7698
7699   r_indx = ELF64_R_SYM (irela->r_info);
7700   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7701     return NULL;
7702   if (ent.sec == NULL || ent.sec->output_section == NULL)
7703     {
7704       _bfd_error_handler
7705         (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7706       return NULL;
7707     }
7708
7709   if (h != NULL)
7710     ent.offset = h->root.u.def.value;
7711   else
7712     ent.offset = sym->st_value;
7713   ent.offset += irela->r_addend;
7714
7715   hash = tocsave_htab_hash (&ent);
7716   slot = ((struct tocsave_entry **)
7717           htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7718   if (slot == NULL)
7719     return NULL;
7720
7721   if (*slot == NULL)
7722     {
7723       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7724       if (p == NULL)
7725         return NULL;
7726       *p = ent;
7727       *slot = p;
7728     }
7729   return *slot;
7730 }
7731
7732 /* Adjust all global syms defined in opd sections.  In gcc generated
7733    code for the old ABI, these will already have been done.  */
7734
7735 static bfd_boolean
7736 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7737 {
7738   struct ppc_link_hash_entry *eh;
7739   asection *sym_sec;
7740   struct _opd_sec_data *opd;
7741
7742   if (h->root.type == bfd_link_hash_indirect)
7743     return TRUE;
7744
7745   if (h->root.type != bfd_link_hash_defined
7746       && h->root.type != bfd_link_hash_defweak)
7747     return TRUE;
7748
7749   eh = (struct ppc_link_hash_entry *) h;
7750   if (eh->adjust_done)
7751     return TRUE;
7752
7753   sym_sec = eh->elf.root.u.def.section;
7754   opd = get_opd_info (sym_sec);
7755   if (opd != NULL && opd->adjust != NULL)
7756     {
7757       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7758       if (adjust == -1)
7759         {
7760           /* This entry has been deleted.  */
7761           asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7762           if (dsec == NULL)
7763             {
7764               for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7765                 if (discarded_section (dsec))
7766                   {
7767                     ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7768                     break;
7769                   }
7770             }
7771           eh->elf.root.u.def.value = 0;
7772           eh->elf.root.u.def.section = dsec;
7773         }
7774       else
7775         eh->elf.root.u.def.value += adjust;
7776       eh->adjust_done = 1;
7777     }
7778   return TRUE;
7779 }
7780
7781 /* Handles decrementing dynamic reloc counts for the reloc specified by
7782    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
7783    have already been determined.  */
7784
7785 static bfd_boolean
7786 dec_dynrel_count (bfd_vma r_info,
7787                   asection *sec,
7788                   struct bfd_link_info *info,
7789                   Elf_Internal_Sym **local_syms,
7790                   struct elf_link_hash_entry *h,
7791                   Elf_Internal_Sym *sym)
7792 {
7793   enum elf_ppc64_reloc_type r_type;
7794   asection *sym_sec = NULL;
7795
7796   /* Can this reloc be dynamic?  This switch, and later tests here
7797      should be kept in sync with the code in check_relocs.  */
7798   r_type = ELF64_R_TYPE (r_info);
7799   switch (r_type)
7800     {
7801     default:
7802       return TRUE;
7803
7804     case R_PPC64_TPREL16:
7805     case R_PPC64_TPREL16_LO:
7806     case R_PPC64_TPREL16_HI:
7807     case R_PPC64_TPREL16_HA:
7808     case R_PPC64_TPREL16_DS:
7809     case R_PPC64_TPREL16_LO_DS:
7810     case R_PPC64_TPREL16_HIGH:
7811     case R_PPC64_TPREL16_HIGHA:
7812     case R_PPC64_TPREL16_HIGHER:
7813     case R_PPC64_TPREL16_HIGHERA:
7814     case R_PPC64_TPREL16_HIGHEST:
7815     case R_PPC64_TPREL16_HIGHESTA:
7816     case R_PPC64_TPREL64:
7817     case R_PPC64_DTPMOD64:
7818     case R_PPC64_DTPREL64:
7819     case R_PPC64_ADDR64:
7820     case R_PPC64_REL30:
7821     case R_PPC64_REL32:
7822     case R_PPC64_REL64:
7823     case R_PPC64_ADDR14:
7824     case R_PPC64_ADDR14_BRNTAKEN:
7825     case R_PPC64_ADDR14_BRTAKEN:
7826     case R_PPC64_ADDR16:
7827     case R_PPC64_ADDR16_DS:
7828     case R_PPC64_ADDR16_HA:
7829     case R_PPC64_ADDR16_HI:
7830     case R_PPC64_ADDR16_HIGH:
7831     case R_PPC64_ADDR16_HIGHA:
7832     case R_PPC64_ADDR16_HIGHER:
7833     case R_PPC64_ADDR16_HIGHERA:
7834     case R_PPC64_ADDR16_HIGHEST:
7835     case R_PPC64_ADDR16_HIGHESTA:
7836     case R_PPC64_ADDR16_LO:
7837     case R_PPC64_ADDR16_LO_DS:
7838     case R_PPC64_ADDR24:
7839     case R_PPC64_ADDR32:
7840     case R_PPC64_UADDR16:
7841     case R_PPC64_UADDR32:
7842     case R_PPC64_UADDR64:
7843     case R_PPC64_TOC:
7844       break;
7845     }
7846
7847   if (local_syms != NULL)
7848     {
7849       unsigned long r_symndx;
7850       bfd *ibfd = sec->owner;
7851
7852       r_symndx = ELF64_R_SYM (r_info);
7853       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7854         return FALSE;
7855     }
7856
7857   if ((bfd_link_pic (info)
7858        && (must_be_dyn_reloc (info, r_type)
7859            || (h != NULL
7860                && (!SYMBOLIC_BIND (info, h)
7861                    || h->root.type == bfd_link_hash_defweak
7862                    || !h->def_regular))))
7863       || (ELIMINATE_COPY_RELOCS
7864           && !bfd_link_pic (info)
7865           && h != NULL
7866           && (h->root.type == bfd_link_hash_defweak
7867               || !h->def_regular)))
7868     ;
7869   else
7870     return TRUE;
7871
7872   if (h != NULL)
7873     {
7874       struct elf_dyn_relocs *p;
7875       struct elf_dyn_relocs **pp;
7876       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7877
7878       /* elf_gc_sweep may have already removed all dyn relocs associated
7879          with local syms for a given section.  Also, symbol flags are
7880          changed by elf_gc_sweep_symbol, confusing the test above.  Don't
7881          report a dynreloc miscount.  */
7882       if (*pp == NULL && info->gc_sections)
7883         return TRUE;
7884
7885       while ((p = *pp) != NULL)
7886         {
7887           if (p->sec == sec)
7888             {
7889               if (!must_be_dyn_reloc (info, r_type))
7890                 p->pc_count -= 1;
7891               p->count -= 1;
7892               if (p->count == 0)
7893                 *pp = p->next;
7894               return TRUE;
7895             }
7896           pp = &p->next;
7897         }
7898     }
7899   else
7900     {
7901       struct ppc_dyn_relocs *p;
7902       struct ppc_dyn_relocs **pp;
7903       void *vpp;
7904       bfd_boolean is_ifunc;
7905
7906       if (local_syms == NULL)
7907         sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7908       if (sym_sec == NULL)
7909         sym_sec = sec;
7910
7911       vpp = &elf_section_data (sym_sec)->local_dynrel;
7912       pp = (struct ppc_dyn_relocs **) vpp;
7913
7914       if (*pp == NULL && info->gc_sections)
7915         return TRUE;
7916
7917       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7918       while ((p = *pp) != NULL)
7919         {
7920           if (p->sec == sec && p->ifunc == is_ifunc)
7921             {
7922               p->count -= 1;
7923               if (p->count == 0)
7924                 *pp = p->next;
7925               return TRUE;
7926             }
7927           pp = &p->next;
7928         }
7929     }
7930
7931   /* xgettext:c-format */
7932   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7933                           sec->owner, sec);
7934   bfd_set_error (bfd_error_bad_value);
7935   return FALSE;
7936 }
7937
7938 /* Remove unused Official Procedure Descriptor entries.  Currently we
7939    only remove those associated with functions in discarded link-once
7940    sections, or weakly defined functions that have been overridden.  It
7941    would be possible to remove many more entries for statically linked
7942    applications.  */
7943
7944 bfd_boolean
7945 ppc64_elf_edit_opd (struct bfd_link_info *info)
7946 {
7947   bfd *ibfd;
7948   bfd_boolean some_edited = FALSE;
7949   asection *need_pad = NULL;
7950   struct ppc_link_hash_table *htab;
7951
7952   htab = ppc_hash_table (info);
7953   if (htab == NULL)
7954     return FALSE;
7955
7956   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7957     {
7958       asection *sec;
7959       Elf_Internal_Rela *relstart, *rel, *relend;
7960       Elf_Internal_Shdr *symtab_hdr;
7961       Elf_Internal_Sym *local_syms;
7962       struct _opd_sec_data *opd;
7963       bfd_boolean need_edit, add_aux_fields, broken;
7964       bfd_size_type cnt_16b = 0;
7965
7966       if (!is_ppc64_elf (ibfd))
7967         continue;
7968
7969       sec = bfd_get_section_by_name (ibfd, ".opd");
7970       if (sec == NULL || sec->size == 0)
7971         continue;
7972
7973       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7974         continue;
7975
7976       if (sec->output_section == bfd_abs_section_ptr)
7977         continue;
7978
7979       /* Look through the section relocs.  */
7980       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7981         continue;
7982
7983       local_syms = NULL;
7984       symtab_hdr = &elf_symtab_hdr (ibfd);
7985
7986       /* Read the relocations.  */
7987       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7988                                             info->keep_memory);
7989       if (relstart == NULL)
7990         return FALSE;
7991
7992       /* First run through the relocs to check they are sane, and to
7993          determine whether we need to edit this opd section.  */
7994       need_edit = FALSE;
7995       broken = FALSE;
7996       need_pad = sec;
7997       relend = relstart + sec->reloc_count;
7998       for (rel = relstart; rel < relend; )
7999         {
8000           enum elf_ppc64_reloc_type r_type;
8001           unsigned long r_symndx;
8002           asection *sym_sec;
8003           struct elf_link_hash_entry *h;
8004           Elf_Internal_Sym *sym;
8005           bfd_vma offset;
8006
8007           /* .opd contains an array of 16 or 24 byte entries.  We're
8008              only interested in the reloc pointing to a function entry
8009              point.  */
8010           offset = rel->r_offset;
8011           if (rel + 1 == relend
8012               || rel[1].r_offset != offset + 8)
8013             {
8014               /* If someone messes with .opd alignment then after a
8015                  "ld -r" we might have padding in the middle of .opd.
8016                  Also, there's nothing to prevent someone putting
8017                  something silly in .opd with the assembler.  No .opd
8018                  optimization for them!  */
8019             broken_opd:
8020               _bfd_error_handler
8021                 (_("%B: .opd is not a regular array of opd entries"), ibfd);
8022               broken = TRUE;
8023               break;
8024             }
8025
8026           if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8027               || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8028             {
8029               _bfd_error_handler
8030                 /* xgettext:c-format */
8031                 (_("%B: unexpected reloc type %u in .opd section"),
8032                  ibfd, r_type);
8033               broken = TRUE;
8034               break;
8035             }
8036
8037           r_symndx = ELF64_R_SYM (rel->r_info);
8038           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8039                           r_symndx, ibfd))
8040             goto error_ret;
8041
8042           if (sym_sec == NULL || sym_sec->owner == NULL)
8043             {
8044               const char *sym_name;
8045               if (h != NULL)
8046                 sym_name = h->root.root.string;
8047               else
8048                 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8049                                              sym_sec);
8050
8051               _bfd_error_handler
8052                 /* xgettext:c-format */
8053                 (_("%B: undefined sym `%s' in .opd section"),
8054                  ibfd, sym_name);
8055               broken = TRUE;
8056               break;
8057             }
8058
8059           /* opd entries are always for functions defined in the
8060              current input bfd.  If the symbol isn't defined in the
8061              input bfd, then we won't be using the function in this
8062              bfd;  It must be defined in a linkonce section in another
8063              bfd, or is weak.  It's also possible that we are
8064              discarding the function due to a linker script /DISCARD/,
8065              which we test for via the output_section.  */
8066           if (sym_sec->owner != ibfd
8067               || sym_sec->output_section == bfd_abs_section_ptr)
8068             need_edit = TRUE;
8069
8070           rel += 2;
8071           if (rel + 1 == relend
8072               || (rel + 2 < relend
8073                   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8074             ++rel;
8075
8076           if (rel == relend)
8077             {
8078               if (sec->size == offset + 24)
8079                 {
8080                   need_pad = NULL;
8081                   break;
8082                 }
8083               if (sec->size == offset + 16)
8084                 {
8085                   cnt_16b++;
8086                   break;
8087                 }
8088               goto broken_opd;
8089             }
8090           else if (rel + 1 < relend
8091                    && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8092                    && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8093             {
8094               if (rel[0].r_offset == offset + 16)
8095                 cnt_16b++;
8096               else if (rel[0].r_offset != offset + 24)
8097                 goto broken_opd;
8098             }
8099           else
8100             goto broken_opd;
8101         }
8102
8103       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8104
8105       if (!broken && (need_edit || add_aux_fields))
8106         {
8107           Elf_Internal_Rela *write_rel;
8108           Elf_Internal_Shdr *rel_hdr;
8109           bfd_byte *rptr, *wptr;
8110           bfd_byte *new_contents;
8111           bfd_size_type amt;
8112
8113           new_contents = NULL;
8114           amt = OPD_NDX (sec->size) * sizeof (long);
8115           opd = &ppc64_elf_section_data (sec)->u.opd;
8116           opd->adjust = bfd_zalloc (sec->owner, amt);
8117           if (opd->adjust == NULL)
8118             return FALSE;
8119           ppc64_elf_section_data (sec)->sec_type = sec_opd;
8120
8121           /* This seems a waste of time as input .opd sections are all
8122              zeros as generated by gcc, but I suppose there's no reason
8123              this will always be so.  We might start putting something in
8124              the third word of .opd entries.  */
8125           if ((sec->flags & SEC_IN_MEMORY) == 0)
8126             {
8127               bfd_byte *loc;
8128               if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8129                 {
8130                   if (loc != NULL)
8131                     free (loc);
8132                 error_ret:
8133                   if (local_syms != NULL
8134                       && symtab_hdr->contents != (unsigned char *) local_syms)
8135                     free (local_syms);
8136                   if (elf_section_data (sec)->relocs != relstart)
8137                     free (relstart);
8138                   return FALSE;
8139                 }
8140               sec->contents = loc;
8141               sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8142             }
8143
8144           elf_section_data (sec)->relocs = relstart;
8145
8146           new_contents = sec->contents;
8147           if (add_aux_fields)
8148             {
8149               new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8150               if (new_contents == NULL)
8151                 return FALSE;
8152               need_pad = NULL;
8153             }
8154           wptr = new_contents;
8155           rptr = sec->contents;
8156           write_rel = relstart;
8157           for (rel = relstart; rel < relend; )
8158             {
8159               unsigned long r_symndx;
8160               asection *sym_sec;
8161               struct elf_link_hash_entry *h;
8162               struct ppc_link_hash_entry *fdh = NULL;
8163               Elf_Internal_Sym *sym;
8164               long opd_ent_size;
8165               Elf_Internal_Rela *next_rel;
8166               bfd_boolean skip;
8167
8168               r_symndx = ELF64_R_SYM (rel->r_info);
8169               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8170                               r_symndx, ibfd))
8171                 goto error_ret;
8172
8173               next_rel = rel + 2;
8174               if (next_rel + 1 == relend
8175                   || (next_rel + 2 < relend
8176                       && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8177                 ++next_rel;
8178
8179               /* See if the .opd entry is full 24 byte or
8180                  16 byte (with fd_aux entry overlapped with next
8181                  fd_func).  */
8182               opd_ent_size = 24;
8183               if (next_rel == relend)
8184                 {
8185                   if (sec->size == rel->r_offset + 16)
8186                     opd_ent_size = 16;
8187                 }
8188               else if (next_rel->r_offset == rel->r_offset + 16)
8189                 opd_ent_size = 16;
8190
8191               if (h != NULL
8192                   && h->root.root.string[0] == '.')
8193                 {
8194                   fdh = ((struct ppc_link_hash_entry *) h)->oh;
8195                   if (fdh != NULL)
8196                     {
8197                       fdh = ppc_follow_link (fdh);
8198                       if (fdh->elf.root.type != bfd_link_hash_defined
8199                           && fdh->elf.root.type != bfd_link_hash_defweak)
8200                         fdh = NULL;
8201                     }
8202                 }
8203
8204               skip = (sym_sec->owner != ibfd
8205                       || sym_sec->output_section == bfd_abs_section_ptr);
8206               if (skip)
8207                 {
8208                   if (fdh != NULL && sym_sec->owner == ibfd)
8209                     {
8210                       /* Arrange for the function descriptor sym
8211                          to be dropped.  */
8212                       fdh->elf.root.u.def.value = 0;
8213                       fdh->elf.root.u.def.section = sym_sec;
8214                     }
8215                   opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8216
8217                   if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8218                     rel = next_rel;
8219                   else
8220                     while (1)
8221                       {
8222                         if (!dec_dynrel_count (rel->r_info, sec, info,
8223                                                NULL, h, sym))
8224                           goto error_ret;
8225
8226                         if (++rel == next_rel)
8227                           break;
8228
8229                         r_symndx = ELF64_R_SYM (rel->r_info);
8230                         if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8231                                         r_symndx, ibfd))
8232                           goto error_ret;
8233                       }
8234                 }
8235               else
8236                 {
8237                   /* We'll be keeping this opd entry.  */
8238                   long adjust;
8239
8240                   if (fdh != NULL)
8241                     {
8242                       /* Redefine the function descriptor symbol to
8243                          this location in the opd section.  It is
8244                          necessary to update the value here rather
8245                          than using an array of adjustments as we do
8246                          for local symbols, because various places
8247                          in the generic ELF code use the value
8248                          stored in u.def.value.  */
8249                       fdh->elf.root.u.def.value = wptr - new_contents;
8250                       fdh->adjust_done = 1;
8251                     }
8252
8253                   /* Local syms are a bit tricky.  We could
8254                      tweak them as they can be cached, but
8255                      we'd need to look through the local syms
8256                      for the function descriptor sym which we
8257                      don't have at the moment.  So keep an
8258                      array of adjustments.  */
8259                   adjust = (wptr - new_contents) - (rptr - sec->contents);
8260                   opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8261
8262                   if (wptr != rptr)
8263                     memcpy (wptr, rptr, opd_ent_size);
8264                   wptr += opd_ent_size;
8265                   if (add_aux_fields && opd_ent_size == 16)
8266                     {
8267                       memset (wptr, '\0', 8);
8268                       wptr += 8;
8269                     }
8270
8271                   /* We need to adjust any reloc offsets to point to the
8272                      new opd entries.  */
8273                   for ( ; rel != next_rel; ++rel)
8274                     {
8275                       rel->r_offset += adjust;
8276                       if (write_rel != rel)
8277                         memcpy (write_rel, rel, sizeof (*rel));
8278                       ++write_rel;
8279                     }
8280                 }
8281
8282               rptr += opd_ent_size;
8283             }
8284
8285           sec->size = wptr - new_contents;
8286           sec->reloc_count = write_rel - relstart;
8287           if (add_aux_fields)
8288             {
8289               free (sec->contents);
8290               sec->contents = new_contents;
8291             }
8292
8293           /* Fudge the header size too, as this is used later in
8294              elf_bfd_final_link if we are emitting relocs.  */
8295           rel_hdr = _bfd_elf_single_rel_hdr (sec);
8296           rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8297           some_edited = TRUE;
8298         }
8299       else if (elf_section_data (sec)->relocs != relstart)
8300         free (relstart);
8301
8302       if (local_syms != NULL
8303           && symtab_hdr->contents != (unsigned char *) local_syms)
8304         {
8305           if (!info->keep_memory)
8306             free (local_syms);
8307           else
8308             symtab_hdr->contents = (unsigned char *) local_syms;
8309         }
8310     }
8311
8312   if (some_edited)
8313     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8314
8315   /* If we are doing a final link and the last .opd entry is just 16 byte
8316      long, add a 8 byte padding after it.  */
8317   if (need_pad != NULL && !bfd_link_relocatable (info))
8318     {
8319       bfd_byte *p;
8320
8321       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8322         {
8323           BFD_ASSERT (need_pad->size > 0);
8324
8325           p = bfd_malloc (need_pad->size + 8);
8326           if (p == NULL)
8327             return FALSE;
8328
8329           if (! bfd_get_section_contents (need_pad->owner, need_pad,
8330                                           p, 0, need_pad->size))
8331             return FALSE;
8332
8333           need_pad->contents = p;
8334           need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8335         }
8336       else
8337         {
8338           p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8339           if (p == NULL)
8340             return FALSE;
8341
8342           need_pad->contents = p;
8343         }
8344
8345       memset (need_pad->contents + need_pad->size, 0, 8);
8346       need_pad->size += 8;
8347     }
8348
8349   return TRUE;
8350 }
8351
8352 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
8353
8354 asection *
8355 ppc64_elf_tls_setup (struct bfd_link_info *info)
8356 {
8357   struct ppc_link_hash_table *htab;
8358
8359   htab = ppc_hash_table (info);
8360   if (htab == NULL)
8361     return NULL;
8362
8363   if (abiversion (info->output_bfd) == 1)
8364     htab->opd_abi = 1;
8365
8366   if (htab->params->no_multi_toc)
8367     htab->do_multi_toc = 0;
8368   else if (!htab->do_multi_toc)
8369     htab->params->no_multi_toc = 1;
8370
8371   /* Default to --no-plt-localentry, as this option can cause problems
8372      with symbol interposition.  For example, glibc libpthread.so and
8373      libc.so duplicate many pthread symbols, with a fallback
8374      implementation in libc.so.  In some cases the fallback does more
8375      work than the pthread implementation.  __pthread_condattr_destroy
8376      is one such symbol: the libpthread.so implementation is
8377      localentry:0 while the libc.so implementation is localentry:8.
8378      An app that "cleverly" uses dlopen to only load necessary
8379      libraries at runtime may omit loading libpthread.so when not
8380      running multi-threaded, which then results in the libc.so
8381      fallback symbols being used and ld.so complaining.  Now there
8382      are workarounds in ld (see non_zero_localentry) to detect the
8383      pthread situation, but that may not be the only case where
8384      --plt-localentry can cause trouble.  */
8385   if (htab->params->plt_localentry0 < 0)
8386     htab->params->plt_localentry0 = 0;
8387   if (htab->params->plt_localentry0
8388       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8389                                FALSE, FALSE, FALSE) == NULL)
8390     info->callbacks->einfo
8391       (_("%P: warning: --plt-localentry is especially dangerous without "
8392          "ld.so support to detect ABI violations.\n"));
8393
8394   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8395                         elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8396                                               FALSE, FALSE, TRUE));
8397   /* Move dynamic linking info to the function descriptor sym.  */
8398   if (htab->tls_get_addr != NULL)
8399     func_desc_adjust (&htab->tls_get_addr->elf, info);
8400   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8401                            elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8402                                                  FALSE, FALSE, TRUE));
8403   if (htab->params->tls_get_addr_opt)
8404     {
8405       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8406
8407       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8408                                   FALSE, FALSE, TRUE);
8409       if (opt != NULL)
8410         func_desc_adjust (opt, info);
8411       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8412                                      FALSE, FALSE, TRUE);
8413       if (opt_fd != NULL
8414           && (opt_fd->root.type == bfd_link_hash_defined
8415               || opt_fd->root.type == bfd_link_hash_defweak))
8416         {
8417           /* If glibc supports an optimized __tls_get_addr call stub,
8418              signalled by the presence of __tls_get_addr_opt, and we'll
8419              be calling __tls_get_addr via a plt call stub, then
8420              make __tls_get_addr point to __tls_get_addr_opt.  */
8421           tga_fd = &htab->tls_get_addr_fd->elf;
8422           if (htab->elf.dynamic_sections_created
8423               && tga_fd != NULL
8424               && (tga_fd->type == STT_FUNC
8425                   || tga_fd->needs_plt)
8426               && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8427                    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8428             {
8429               struct plt_entry *ent;
8430
8431               for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8432                 if (ent->plt.refcount > 0)
8433                   break;
8434               if (ent != NULL)
8435                 {
8436                   tga_fd->root.type = bfd_link_hash_indirect;
8437                   tga_fd->root.u.i.link = &opt_fd->root;
8438                   ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8439                   opt_fd->mark = 1;
8440                   if (opt_fd->dynindx != -1)
8441                     {
8442                       /* Use __tls_get_addr_opt in dynamic relocations.  */
8443                       opt_fd->dynindx = -1;
8444                       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8445                                               opt_fd->dynstr_index);
8446                       if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8447                         return NULL;
8448                     }
8449                   htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8450                   tga = &htab->tls_get_addr->elf;
8451                   if (opt != NULL && tga != NULL)
8452                     {
8453                       tga->root.type = bfd_link_hash_indirect;
8454                       tga->root.u.i.link = &opt->root;
8455                       ppc64_elf_copy_indirect_symbol (info, opt, tga);
8456                       opt->mark = 1;
8457                       _bfd_elf_link_hash_hide_symbol (info, opt,
8458                                                       tga->forced_local);
8459                       htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8460                     }
8461                   htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8462                   htab->tls_get_addr_fd->is_func_descriptor = 1;
8463                   if (htab->tls_get_addr != NULL)
8464                     {
8465                       htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8466                       htab->tls_get_addr->is_func = 1;
8467                     }
8468                 }
8469             }
8470         }
8471       else if (htab->params->tls_get_addr_opt < 0)
8472         htab->params->tls_get_addr_opt = 0;
8473     }
8474   return _bfd_elf_tls_setup (info->output_bfd, info);
8475 }
8476
8477 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8478    HASH1 or HASH2.  */
8479
8480 static bfd_boolean
8481 branch_reloc_hash_match (const bfd *ibfd,
8482                          const Elf_Internal_Rela *rel,
8483                          const struct ppc_link_hash_entry *hash1,
8484                          const struct ppc_link_hash_entry *hash2)
8485 {
8486   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8487   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8488   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8489
8490   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8491     {
8492       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8493       struct elf_link_hash_entry *h;
8494
8495       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8496       h = elf_follow_link (h);
8497       if (h == &hash1->elf || h == &hash2->elf)
8498         return TRUE;
8499     }
8500   return FALSE;
8501 }
8502
8503 /* Run through all the TLS relocs looking for optimization
8504    opportunities.  The linker has been hacked (see ppc64elf.em) to do
8505    a preliminary section layout so that we know the TLS segment
8506    offsets.  We can't optimize earlier because some optimizations need
8507    to know the tp offset, and we need to optimize before allocating
8508    dynamic relocations.  */
8509
8510 bfd_boolean
8511 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8512 {
8513   bfd *ibfd;
8514   asection *sec;
8515   struct ppc_link_hash_table *htab;
8516   unsigned char *toc_ref;
8517   int pass;
8518
8519   if (!bfd_link_executable (info))
8520     return TRUE;
8521
8522   htab = ppc_hash_table (info);
8523   if (htab == NULL)
8524     return FALSE;
8525
8526   /* Make two passes over the relocs.  On the first pass, mark toc
8527      entries involved with tls relocs, and check that tls relocs
8528      involved in setting up a tls_get_addr call are indeed followed by
8529      such a call.  If they are not, we can't do any tls optimization.
8530      On the second pass twiddle tls_mask flags to notify
8531      relocate_section that optimization can be done, and adjust got
8532      and plt refcounts.  */
8533   toc_ref = NULL;
8534   for (pass = 0; pass < 2; ++pass)
8535     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8536       {
8537         Elf_Internal_Sym *locsyms = NULL;
8538         asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8539
8540         for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8541           if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8542             {
8543               Elf_Internal_Rela *relstart, *rel, *relend;
8544               bfd_boolean found_tls_get_addr_arg = 0;
8545
8546               /* Read the relocations.  */
8547               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8548                                                     info->keep_memory);
8549               if (relstart == NULL)
8550                 {
8551                   free (toc_ref);
8552                   return FALSE;
8553                 }
8554
8555               relend = relstart + sec->reloc_count;
8556               for (rel = relstart; rel < relend; rel++)
8557                 {
8558                   enum elf_ppc64_reloc_type r_type;
8559                   unsigned long r_symndx;
8560                   struct elf_link_hash_entry *h;
8561                   Elf_Internal_Sym *sym;
8562                   asection *sym_sec;
8563                   unsigned char *tls_mask;
8564                   unsigned char tls_set, tls_clear, tls_type = 0;
8565                   bfd_vma value;
8566                   bfd_boolean ok_tprel, is_local;
8567                   long toc_ref_index = 0;
8568                   int expecting_tls_get_addr = 0;
8569                   bfd_boolean ret = FALSE;
8570
8571                   r_symndx = ELF64_R_SYM (rel->r_info);
8572                   if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8573                                   r_symndx, ibfd))
8574                     {
8575                     err_free_rel:
8576                       if (elf_section_data (sec)->relocs != relstart)
8577                         free (relstart);
8578                       if (toc_ref != NULL)
8579                         free (toc_ref);
8580                       if (locsyms != NULL
8581                           && (elf_symtab_hdr (ibfd).contents
8582                               != (unsigned char *) locsyms))
8583                         free (locsyms);
8584                       return ret;
8585                     }
8586
8587                   if (h != NULL)
8588                     {
8589                       if (h->root.type == bfd_link_hash_defined
8590                           || h->root.type == bfd_link_hash_defweak)
8591                         value = h->root.u.def.value;
8592                       else if (h->root.type == bfd_link_hash_undefweak)
8593                         value = 0;
8594                       else
8595                         {
8596                           found_tls_get_addr_arg = 0;
8597                           continue;
8598                         }
8599                     }
8600                   else
8601                     /* Symbols referenced by TLS relocs must be of type
8602                        STT_TLS.  So no need for .opd local sym adjust.  */
8603                     value = sym->st_value;
8604
8605                   ok_tprel = FALSE;
8606                   is_local = FALSE;
8607                   if (h == NULL
8608                       || !h->def_dynamic)
8609                     {
8610                       is_local = TRUE;
8611                       if (h != NULL
8612                           && h->root.type == bfd_link_hash_undefweak)
8613                         ok_tprel = TRUE;
8614                       else if (sym_sec != NULL
8615                                && sym_sec->output_section != NULL)
8616                         {
8617                           value += sym_sec->output_offset;
8618                           value += sym_sec->output_section->vma;
8619                           value -= htab->elf.tls_sec->vma;
8620                           ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8621                                       < (bfd_vma) 1 << 32);
8622                         }
8623                     }
8624
8625                   r_type = ELF64_R_TYPE (rel->r_info);
8626                   /* If this section has old-style __tls_get_addr calls
8627                      without marker relocs, then check that each
8628                      __tls_get_addr call reloc is preceded by a reloc
8629                      that conceivably belongs to the __tls_get_addr arg
8630                      setup insn.  If we don't find matching arg setup
8631                      relocs, don't do any tls optimization.  */
8632                   if (pass == 0
8633                       && sec->has_tls_get_addr_call
8634                       && h != NULL
8635                       && (h == &htab->tls_get_addr->elf
8636                           || h == &htab->tls_get_addr_fd->elf)
8637                       && !found_tls_get_addr_arg
8638                       && is_branch_reloc (r_type))
8639                     {
8640                       info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8641                                                 "TLS optimization disabled\n"),
8642                                               ibfd, sec, rel->r_offset);
8643                       ret = TRUE;
8644                       goto err_free_rel;
8645                     }
8646
8647                   found_tls_get_addr_arg = 0;
8648                   switch (r_type)
8649                     {
8650                     case R_PPC64_GOT_TLSLD16:
8651                     case R_PPC64_GOT_TLSLD16_LO:
8652                       expecting_tls_get_addr = 1;
8653                       found_tls_get_addr_arg = 1;
8654                       /* Fall through.  */
8655
8656                     case R_PPC64_GOT_TLSLD16_HI:
8657                     case R_PPC64_GOT_TLSLD16_HA:
8658                       /* These relocs should never be against a symbol
8659                          defined in a shared lib.  Leave them alone if
8660                          that turns out to be the case.  */
8661                       if (!is_local)
8662                         continue;
8663
8664                       /* LD -> LE */
8665                       tls_set = 0;
8666                       tls_clear = TLS_LD;
8667                       tls_type = TLS_TLS | TLS_LD;
8668                       break;
8669
8670                     case R_PPC64_GOT_TLSGD16:
8671                     case R_PPC64_GOT_TLSGD16_LO:
8672                       expecting_tls_get_addr = 1;
8673                       found_tls_get_addr_arg = 1;
8674                       /* Fall through. */
8675
8676                     case R_PPC64_GOT_TLSGD16_HI:
8677                     case R_PPC64_GOT_TLSGD16_HA:
8678                       if (ok_tprel)
8679                         /* GD -> LE */
8680                         tls_set = 0;
8681                       else
8682                         /* GD -> IE */
8683                         tls_set = TLS_TLS | TLS_TPRELGD;
8684                       tls_clear = TLS_GD;
8685                       tls_type = TLS_TLS | TLS_GD;
8686                       break;
8687
8688                     case R_PPC64_GOT_TPREL16_DS:
8689                     case R_PPC64_GOT_TPREL16_LO_DS:
8690                     case R_PPC64_GOT_TPREL16_HI:
8691                     case R_PPC64_GOT_TPREL16_HA:
8692                       if (ok_tprel)
8693                         {
8694                           /* IE -> LE */
8695                           tls_set = 0;
8696                           tls_clear = TLS_TPREL;
8697                           tls_type = TLS_TLS | TLS_TPREL;
8698                           break;
8699                         }
8700                       continue;
8701
8702                     case R_PPC64_TLSGD:
8703                     case R_PPC64_TLSLD:
8704                       found_tls_get_addr_arg = 1;
8705                       /* Fall through.  */
8706
8707                     case R_PPC64_TLS:
8708                     case R_PPC64_TOC16:
8709                     case R_PPC64_TOC16_LO:
8710                       if (sym_sec == NULL || sym_sec != toc)
8711                         continue;
8712
8713                       /* Mark this toc entry as referenced by a TLS
8714                          code sequence.  We can do that now in the
8715                          case of R_PPC64_TLS, and after checking for
8716                          tls_get_addr for the TOC16 relocs.  */
8717                       if (toc_ref == NULL)
8718                         toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8719                       if (toc_ref == NULL)
8720                         goto err_free_rel;
8721
8722                       if (h != NULL)
8723                         value = h->root.u.def.value;
8724                       else
8725                         value = sym->st_value;
8726                       value += rel->r_addend;
8727                       if (value % 8 != 0)
8728                         continue;
8729                       BFD_ASSERT (value < toc->size
8730                                   && toc->output_offset % 8 == 0);
8731                       toc_ref_index = (value + toc->output_offset) / 8;
8732                       if (r_type == R_PPC64_TLS
8733                           || r_type == R_PPC64_TLSGD
8734                           || r_type == R_PPC64_TLSLD)
8735                         {
8736                           toc_ref[toc_ref_index] = 1;
8737                           continue;
8738                         }
8739
8740                       if (pass != 0 && toc_ref[toc_ref_index] == 0)
8741                         continue;
8742
8743                       tls_set = 0;
8744                       tls_clear = 0;
8745                       expecting_tls_get_addr = 2;
8746                       break;
8747
8748                     case R_PPC64_TPREL64:
8749                       if (pass == 0
8750                           || sec != toc
8751                           || toc_ref == NULL
8752                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8753                         continue;
8754                       if (ok_tprel)
8755                         {
8756                           /* IE -> LE */
8757                           tls_set = TLS_EXPLICIT;
8758                           tls_clear = TLS_TPREL;
8759                           break;
8760                         }
8761                       continue;
8762
8763                     case R_PPC64_DTPMOD64:
8764                       if (pass == 0
8765                           || sec != toc
8766                           || toc_ref == NULL
8767                           || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8768                         continue;
8769                       if (rel + 1 < relend
8770                           && (rel[1].r_info
8771                               == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8772                           && rel[1].r_offset == rel->r_offset + 8)
8773                         {
8774                           if (ok_tprel)
8775                             /* GD -> LE */
8776                             tls_set = TLS_EXPLICIT | TLS_GD;
8777                           else
8778                             /* GD -> IE */
8779                             tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8780                           tls_clear = TLS_GD;
8781                         }
8782                       else
8783                         {
8784                           if (!is_local)
8785                             continue;
8786
8787                           /* LD -> LE */
8788                           tls_set = TLS_EXPLICIT;
8789                           tls_clear = TLS_LD;
8790                         }
8791                       break;
8792
8793                     default:
8794                       continue;
8795                     }
8796
8797                   if (pass == 0)
8798                     {
8799                       if (!expecting_tls_get_addr
8800                           || !sec->has_tls_get_addr_call)
8801                         continue;
8802
8803                       if (rel + 1 < relend
8804                           && branch_reloc_hash_match (ibfd, rel + 1,
8805                                                       htab->tls_get_addr,
8806                                                       htab->tls_get_addr_fd))
8807                         {
8808                           if (expecting_tls_get_addr == 2)
8809                             {
8810                               /* Check for toc tls entries.  */
8811                               unsigned char *toc_tls;
8812                               int retval;
8813
8814                               retval = get_tls_mask (&toc_tls, NULL, NULL,
8815                                                      &locsyms,
8816                                                      rel, ibfd);
8817                               if (retval == 0)
8818                                 goto err_free_rel;
8819                               if (toc_tls != NULL)
8820                                 {
8821                                   if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8822                                     found_tls_get_addr_arg = 1;
8823                                   if (retval > 1)
8824                                     toc_ref[toc_ref_index] = 1;
8825                                 }
8826                             }
8827                           continue;
8828                         }
8829
8830                       if (expecting_tls_get_addr != 1)
8831                         continue;
8832
8833                       /* Uh oh, we didn't find the expected call.  We
8834                          could just mark this symbol to exclude it
8835                          from tls optimization but it's safer to skip
8836                          the entire optimization.  */
8837                       /* xgettext:c-format */
8838                       info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8839                                                 "TLS optimization disabled\n"),
8840                                               ibfd, sec, rel->r_offset);
8841                       ret = TRUE;
8842                       goto err_free_rel;
8843                     }
8844
8845                   if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8846                     {
8847                       struct plt_entry *ent;
8848                       for (ent = htab->tls_get_addr->elf.plt.plist;
8849                            ent != NULL;
8850                            ent = ent->next)
8851                         if (ent->addend == 0)
8852                           {
8853                             if (ent->plt.refcount > 0)
8854                               {
8855                                 ent->plt.refcount -= 1;
8856                                 expecting_tls_get_addr = 0;
8857                               }
8858                             break;
8859                           }
8860                     }
8861
8862                   if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8863                     {
8864                       struct plt_entry *ent;
8865                       for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8866                            ent != NULL;
8867                            ent = ent->next)
8868                         if (ent->addend == 0)
8869                           {
8870                             if (ent->plt.refcount > 0)
8871                               ent->plt.refcount -= 1;
8872                             break;
8873                           }
8874                     }
8875
8876                   if (tls_clear == 0)
8877                     continue;
8878
8879                   if ((tls_set & TLS_EXPLICIT) == 0)
8880                     {
8881                       struct got_entry *ent;
8882
8883                       /* Adjust got entry for this reloc.  */
8884                       if (h != NULL)
8885                         ent = h->got.glist;
8886                       else
8887                         ent = elf_local_got_ents (ibfd)[r_symndx];
8888
8889                       for (; ent != NULL; ent = ent->next)
8890                         if (ent->addend == rel->r_addend
8891                             && ent->owner == ibfd
8892                             && ent->tls_type == tls_type)
8893                           break;
8894                       if (ent == NULL)
8895                         abort ();
8896
8897                       if (tls_set == 0)
8898                         {
8899                           /* We managed to get rid of a got entry.  */
8900                           if (ent->got.refcount > 0)
8901                             ent->got.refcount -= 1;
8902                         }
8903                     }
8904                   else
8905                     {
8906                       /* If we got rid of a DTPMOD/DTPREL reloc pair then
8907                          we'll lose one or two dyn relocs.  */
8908                       if (!dec_dynrel_count (rel->r_info, sec, info,
8909                                              NULL, h, sym))
8910                         return FALSE;
8911
8912                       if (tls_set == (TLS_EXPLICIT | TLS_GD))
8913                         {
8914                           if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8915                                                  NULL, h, sym))
8916                             return FALSE;
8917                         }
8918                     }
8919
8920                   *tls_mask |= tls_set;
8921                   *tls_mask &= ~tls_clear;
8922                 }
8923
8924               if (elf_section_data (sec)->relocs != relstart)
8925                 free (relstart);
8926             }
8927
8928         if (locsyms != NULL
8929             && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8930           {
8931             if (!info->keep_memory)
8932               free (locsyms);
8933             else
8934               elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8935           }
8936       }
8937
8938   if (toc_ref != NULL)
8939     free (toc_ref);
8940   htab->do_tls_opt = 1;
8941   return TRUE;
8942 }
8943
8944 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8945    the values of any global symbols in a toc section that has been
8946    edited.  Globals in toc sections should be a rarity, so this function
8947    sets a flag if any are found in toc sections other than the one just
8948    edited, so that further hash table traversals can be avoided.  */
8949
8950 struct adjust_toc_info
8951 {
8952   asection *toc;
8953   unsigned long *skip;
8954   bfd_boolean global_toc_syms;
8955 };
8956
8957 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8958
8959 static bfd_boolean
8960 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8961 {
8962   struct ppc_link_hash_entry *eh;
8963   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8964   unsigned long i;
8965
8966   if (h->root.type != bfd_link_hash_defined
8967       && h->root.type != bfd_link_hash_defweak)
8968     return TRUE;
8969
8970   eh = (struct ppc_link_hash_entry *) h;
8971   if (eh->adjust_done)
8972     return TRUE;
8973
8974   if (eh->elf.root.u.def.section == toc_inf->toc)
8975     {
8976       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8977         i = toc_inf->toc->rawsize >> 3;
8978       else
8979         i = eh->elf.root.u.def.value >> 3;
8980
8981       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8982         {
8983           _bfd_error_handler
8984             (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8985           do
8986             ++i;
8987           while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8988           eh->elf.root.u.def.value = (bfd_vma) i << 3;
8989         }
8990
8991       eh->elf.root.u.def.value -= toc_inf->skip[i];
8992       eh->adjust_done = 1;
8993     }
8994   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8995     toc_inf->global_toc_syms = TRUE;
8996
8997   return TRUE;
8998 }
8999
9000 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9001    on a _LO variety toc/got reloc.  */
9002
9003 static bfd_boolean
9004 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9005 {
9006   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9007           || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9008           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9009           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9010           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9011           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9012           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9013           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9014           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9015           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9016           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9017           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9018           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9019           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9020           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9021           || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9022           || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9023               /* Exclude lfqu by testing reloc.  If relocs are ever
9024                  defined for the reduced D field in psq_lu then those
9025                  will need testing too.  */
9026               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9027           || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9028               && (insn & 1) == 0)
9029           || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9030           || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9031               /* Exclude stfqu.  psq_stu as above for psq_lu.  */
9032               && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9033           || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9034               && (insn & 1) == 0));
9035 }
9036
9037 /* Examine all relocs referencing .toc sections in order to remove
9038    unused .toc entries.  */
9039
9040 bfd_boolean
9041 ppc64_elf_edit_toc (struct bfd_link_info *info)
9042 {
9043   bfd *ibfd;
9044   struct adjust_toc_info toc_inf;
9045   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9046
9047   htab->do_toc_opt = 1;
9048   toc_inf.global_toc_syms = TRUE;
9049   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9050     {
9051       asection *toc, *sec;
9052       Elf_Internal_Shdr *symtab_hdr;
9053       Elf_Internal_Sym *local_syms;
9054       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9055       unsigned long *skip, *drop;
9056       unsigned char *used;
9057       unsigned char *keep, last, some_unused;
9058
9059       if (!is_ppc64_elf (ibfd))
9060         continue;
9061
9062       toc = bfd_get_section_by_name (ibfd, ".toc");
9063       if (toc == NULL
9064           || toc->size == 0
9065           || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9066           || discarded_section (toc))
9067         continue;
9068
9069       toc_relocs = NULL;
9070       local_syms = NULL;
9071       symtab_hdr = &elf_symtab_hdr (ibfd);
9072
9073       /* Look at sections dropped from the final link.  */
9074       skip = NULL;
9075       relstart = NULL;
9076       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9077         {
9078           if (sec->reloc_count == 0
9079               || !discarded_section (sec)
9080               || get_opd_info (sec)
9081               || (sec->flags & SEC_ALLOC) == 0
9082               || (sec->flags & SEC_DEBUGGING) != 0)
9083             continue;
9084
9085           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9086           if (relstart == NULL)
9087             goto error_ret;
9088
9089           /* Run through the relocs to see which toc entries might be
9090              unused.  */
9091           for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9092             {
9093               enum elf_ppc64_reloc_type r_type;
9094               unsigned long r_symndx;
9095               asection *sym_sec;
9096               struct elf_link_hash_entry *h;
9097               Elf_Internal_Sym *sym;
9098               bfd_vma val;
9099
9100               r_type = ELF64_R_TYPE (rel->r_info);
9101               switch (r_type)
9102                 {
9103                 default:
9104                   continue;
9105
9106                 case R_PPC64_TOC16:
9107                 case R_PPC64_TOC16_LO:
9108                 case R_PPC64_TOC16_HI:
9109                 case R_PPC64_TOC16_HA:
9110                 case R_PPC64_TOC16_DS:
9111                 case R_PPC64_TOC16_LO_DS:
9112                   break;
9113                 }
9114
9115               r_symndx = ELF64_R_SYM (rel->r_info);
9116               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9117                               r_symndx, ibfd))
9118                 goto error_ret;
9119
9120               if (sym_sec != toc)
9121                 continue;
9122
9123               if (h != NULL)
9124                 val = h->root.u.def.value;
9125               else
9126                 val = sym->st_value;
9127               val += rel->r_addend;
9128
9129               if (val >= toc->size)
9130                 continue;
9131
9132               /* Anything in the toc ought to be aligned to 8 bytes.
9133                  If not, don't mark as unused.  */
9134               if (val & 7)
9135                 continue;
9136
9137               if (skip == NULL)
9138                 {
9139                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9140                   if (skip == NULL)
9141                     goto error_ret;
9142                 }
9143
9144               skip[val >> 3] = ref_from_discarded;
9145             }
9146
9147           if (elf_section_data (sec)->relocs != relstart)
9148             free (relstart);
9149         }
9150
9151       /* For largetoc loads of address constants, we can convert
9152          .  addis rx,2,addr@got@ha
9153          .  ld ry,addr@got@l(rx)
9154          to
9155          .  addis rx,2,addr@toc@ha
9156          .  addi ry,rx,addr@toc@l
9157          when addr is within 2G of the toc pointer.  This then means
9158          that the word storing "addr" in the toc is no longer needed.  */
9159
9160       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9161           && toc->output_section->rawsize < (bfd_vma) 1 << 31
9162           && toc->reloc_count != 0)
9163         {
9164           /* Read toc relocs.  */
9165           toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9166                                                   info->keep_memory);
9167           if (toc_relocs == NULL)
9168             goto error_ret;
9169
9170           for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9171             {
9172               enum elf_ppc64_reloc_type r_type;
9173               unsigned long r_symndx;
9174               asection *sym_sec;
9175               struct elf_link_hash_entry *h;
9176               Elf_Internal_Sym *sym;
9177               bfd_vma val, addr;
9178
9179               r_type = ELF64_R_TYPE (rel->r_info);
9180               if (r_type != R_PPC64_ADDR64)
9181                 continue;
9182
9183               r_symndx = ELF64_R_SYM (rel->r_info);
9184               if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9185                               r_symndx, ibfd))
9186                 goto error_ret;
9187
9188               if (sym_sec == NULL
9189                   || sym_sec->output_section == NULL
9190                   || discarded_section (sym_sec))
9191                 continue;
9192
9193               if (!SYMBOL_REFERENCES_LOCAL (info, h))
9194                 continue;
9195
9196               if (h != NULL)
9197                 {
9198                   if (h->type == STT_GNU_IFUNC)
9199                     continue;
9200                   val = h->root.u.def.value;
9201                 }
9202               else
9203                 {
9204                   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9205                     continue;
9206                   val = sym->st_value;
9207                 }
9208               val += rel->r_addend;
9209               val += sym_sec->output_section->vma + sym_sec->output_offset;
9210
9211               /* We don't yet know the exact toc pointer value, but we
9212                  know it will be somewhere in the toc section.  Don't
9213                  optimize if the difference from any possible toc
9214                  pointer is outside [ff..f80008000, 7fff7fff].  */
9215               addr = toc->output_section->vma + TOC_BASE_OFF;
9216               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9217                 continue;
9218
9219               addr = toc->output_section->vma + toc->output_section->rawsize;
9220               if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9221                 continue;
9222
9223               if (skip == NULL)
9224                 {
9225                   skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9226                   if (skip == NULL)
9227                     goto error_ret;
9228                 }
9229
9230               skip[rel->r_offset >> 3]
9231                 |= can_optimize | ((rel - toc_relocs) << 2);
9232             }
9233         }
9234
9235       if (skip == NULL)
9236         continue;
9237
9238       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9239       if (used == NULL)
9240         {
9241         error_ret:
9242           if (local_syms != NULL
9243               && symtab_hdr->contents != (unsigned char *) local_syms)
9244             free (local_syms);
9245           if (sec != NULL
9246               && relstart != NULL
9247               && elf_section_data (sec)->relocs != relstart)
9248             free (relstart);
9249           if (toc_relocs != NULL
9250               && elf_section_data (toc)->relocs != toc_relocs)
9251             free (toc_relocs);
9252           if (skip != NULL)
9253             free (skip);
9254           return FALSE;
9255         }
9256
9257       /* Now check all kept sections that might reference the toc.
9258          Check the toc itself last.  */
9259       for (sec = (ibfd->sections == toc && toc->next ? toc->next
9260                   : ibfd->sections);
9261            sec != NULL;
9262            sec = (sec == toc ? NULL
9263                   : sec->next == NULL ? toc
9264                   : sec->next == toc && toc->next ? toc->next
9265                   : sec->next))
9266         {
9267           int repeat;
9268
9269           if (sec->reloc_count == 0
9270               || discarded_section (sec)
9271               || get_opd_info (sec)
9272               || (sec->flags & SEC_ALLOC) == 0
9273               || (sec->flags & SEC_DEBUGGING) != 0)
9274             continue;
9275
9276           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9277                                                 info->keep_memory);
9278           if (relstart == NULL)
9279             {
9280               free (used);
9281               goto error_ret;
9282             }
9283
9284           /* Mark toc entries referenced as used.  */
9285           do
9286             {
9287               repeat = 0;
9288               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9289                 {
9290                   enum elf_ppc64_reloc_type r_type;
9291                   unsigned long r_symndx;
9292                   asection *sym_sec;
9293                   struct elf_link_hash_entry *h;
9294                   Elf_Internal_Sym *sym;
9295                   bfd_vma val;
9296                   enum {no_check, check_lo, check_ha} insn_check;
9297
9298                   r_type = ELF64_R_TYPE (rel->r_info);
9299                   switch (r_type)
9300                     {
9301                     default:
9302                       insn_check = no_check;
9303                       break;
9304
9305                     case R_PPC64_GOT_TLSLD16_HA:
9306                     case R_PPC64_GOT_TLSGD16_HA:
9307                     case R_PPC64_GOT_TPREL16_HA:
9308                     case R_PPC64_GOT_DTPREL16_HA:
9309                     case R_PPC64_GOT16_HA:
9310                     case R_PPC64_TOC16_HA:
9311                       insn_check = check_ha;
9312                       break;
9313
9314                     case R_PPC64_GOT_TLSLD16_LO:
9315                     case R_PPC64_GOT_TLSGD16_LO:
9316                     case R_PPC64_GOT_TPREL16_LO_DS:
9317                     case R_PPC64_GOT_DTPREL16_LO_DS:
9318                     case R_PPC64_GOT16_LO:
9319                     case R_PPC64_GOT16_LO_DS:
9320                     case R_PPC64_TOC16_LO:
9321                     case R_PPC64_TOC16_LO_DS:
9322                       insn_check = check_lo;
9323                       break;
9324                     }
9325
9326                   if (insn_check != no_check)
9327                     {
9328                       bfd_vma off = rel->r_offset & ~3;
9329                       unsigned char buf[4];
9330                       unsigned int insn;
9331
9332                       if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9333                         {
9334                           free (used);
9335                           goto error_ret;
9336                         }
9337                       insn = bfd_get_32 (ibfd, buf);
9338                       if (insn_check == check_lo
9339                           ? !ok_lo_toc_insn (insn, r_type)
9340                           : ((insn & ((0x3f << 26) | 0x1f << 16))
9341                              != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9342                         {
9343                           char str[12];
9344
9345                           ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9346                           sprintf (str, "%#08x", insn);
9347                           info->callbacks->einfo
9348                             /* xgettext:c-format */
9349                             (_("%H: toc optimization is not supported for"
9350                                " %s instruction.\n"),
9351                              ibfd, sec, rel->r_offset & ~3, str);
9352                         }
9353                     }
9354
9355                   switch (r_type)
9356                     {
9357                     case R_PPC64_TOC16:
9358                     case R_PPC64_TOC16_LO:
9359                     case R_PPC64_TOC16_HI:
9360                     case R_PPC64_TOC16_HA:
9361                     case R_PPC64_TOC16_DS:
9362                     case R_PPC64_TOC16_LO_DS:
9363                       /* In case we're taking addresses of toc entries.  */
9364                     case R_PPC64_ADDR64:
9365                       break;
9366
9367                     default:
9368                       continue;
9369                     }
9370
9371                   r_symndx = ELF64_R_SYM (rel->r_info);
9372                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9373                                   r_symndx, ibfd))
9374                     {
9375                       free (used);
9376                       goto error_ret;
9377                     }
9378
9379                   if (sym_sec != toc)
9380                     continue;
9381
9382                   if (h != NULL)
9383                     val = h->root.u.def.value;
9384                   else
9385                     val = sym->st_value;
9386                   val += rel->r_addend;
9387
9388                   if (val >= toc->size)
9389                     continue;
9390
9391                   if ((skip[val >> 3] & can_optimize) != 0)
9392                     {
9393                       bfd_vma off;
9394                       unsigned char opc;
9395
9396                       switch (r_type)
9397                         {
9398                         case R_PPC64_TOC16_HA:
9399                           break;
9400
9401                         case R_PPC64_TOC16_LO_DS:
9402                           off = rel->r_offset;
9403                           off += (bfd_big_endian (ibfd) ? -2 : 3);
9404                           if (!bfd_get_section_contents (ibfd, sec, &opc,
9405                                                          off, 1))
9406                             {
9407                               free (used);
9408                               goto error_ret;
9409                             }
9410                           if ((opc & (0x3f << 2)) == (58u << 2))
9411                             break;
9412                           /* Fall through.  */
9413
9414                         default:
9415                           /* Wrong sort of reloc, or not a ld.  We may
9416                              as well clear ref_from_discarded too.  */
9417                           skip[val >> 3] = 0;
9418                         }
9419                     }
9420
9421                   if (sec != toc)
9422                     used[val >> 3] = 1;
9423                   /* For the toc section, we only mark as used if this
9424                      entry itself isn't unused.  */
9425                   else if ((used[rel->r_offset >> 3]
9426                             || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9427                            && !used[val >> 3])
9428                     {
9429                       /* Do all the relocs again, to catch reference
9430                          chains.  */
9431                       repeat = 1;
9432                       used[val >> 3] = 1;
9433                     }
9434                 }
9435             }
9436           while (repeat);
9437
9438           if (elf_section_data (sec)->relocs != relstart)
9439             free (relstart);
9440         }
9441
9442       /* Merge the used and skip arrays.  Assume that TOC
9443          doublewords not appearing as either used or unused belong
9444          to an entry more than one doubleword in size.  */
9445       for (drop = skip, keep = used, last = 0, some_unused = 0;
9446            drop < skip + (toc->size + 7) / 8;
9447            ++drop, ++keep)
9448         {
9449           if (*keep)
9450             {
9451               *drop &= ~ref_from_discarded;
9452               if ((*drop & can_optimize) != 0)
9453                 some_unused = 1;
9454               last = 0;
9455             }
9456           else if ((*drop & ref_from_discarded) != 0)
9457             {
9458               some_unused = 1;
9459               last = ref_from_discarded;
9460             }
9461           else
9462             *drop = last;
9463         }
9464
9465       free (used);
9466
9467       if (some_unused)
9468         {
9469           bfd_byte *contents, *src;
9470           unsigned long off;
9471           Elf_Internal_Sym *sym;
9472           bfd_boolean local_toc_syms = FALSE;
9473
9474           /* Shuffle the toc contents, and at the same time convert the
9475              skip array from booleans into offsets.  */
9476           if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9477             goto error_ret;
9478
9479           elf_section_data (toc)->this_hdr.contents = contents;
9480
9481           for (src = contents, off = 0, drop = skip;
9482                src < contents + toc->size;
9483                src += 8, ++drop)
9484             {
9485               if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9486                 off += 8;
9487               else if (off != 0)
9488                 {
9489                   *drop = off;
9490                   memcpy (src - off, src, 8);
9491                 }
9492             }
9493           *drop = off;
9494           toc->rawsize = toc->size;
9495           toc->size = src - contents - off;
9496
9497           /* Adjust addends for relocs against the toc section sym,
9498              and optimize any accesses we can.  */
9499           for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9500             {
9501               if (sec->reloc_count == 0
9502                   || discarded_section (sec))
9503                 continue;
9504
9505               relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9506                                                     info->keep_memory);
9507               if (relstart == NULL)
9508                 goto error_ret;
9509
9510               for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9511                 {
9512                   enum elf_ppc64_reloc_type r_type;
9513                   unsigned long r_symndx;
9514                   asection *sym_sec;
9515                   struct elf_link_hash_entry *h;
9516                   bfd_vma val;
9517
9518                   r_type = ELF64_R_TYPE (rel->r_info);
9519                   switch (r_type)
9520                     {
9521                     default:
9522                       continue;
9523
9524                     case R_PPC64_TOC16:
9525                     case R_PPC64_TOC16_LO:
9526                     case R_PPC64_TOC16_HI:
9527                     case R_PPC64_TOC16_HA:
9528                     case R_PPC64_TOC16_DS:
9529                     case R_PPC64_TOC16_LO_DS:
9530                     case R_PPC64_ADDR64:
9531                       break;
9532                     }
9533
9534                   r_symndx = ELF64_R_SYM (rel->r_info);
9535                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9536                                   r_symndx, ibfd))
9537                     goto error_ret;
9538
9539                   if (sym_sec != toc)
9540                     continue;
9541
9542                   if (h != NULL)
9543                     val = h->root.u.def.value;
9544                   else
9545                     {
9546                       val = sym->st_value;
9547                       if (val != 0)
9548                         local_toc_syms = TRUE;
9549                     }
9550
9551                   val += rel->r_addend;
9552
9553                   if (val > toc->rawsize)
9554                     val = toc->rawsize;
9555                   else if ((skip[val >> 3] & ref_from_discarded) != 0)
9556                     continue;
9557                   else if ((skip[val >> 3] & can_optimize) != 0)
9558                     {
9559                       Elf_Internal_Rela *tocrel
9560                         = toc_relocs + (skip[val >> 3] >> 2);
9561                       unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9562
9563                       switch (r_type)
9564                         {
9565                         case R_PPC64_TOC16_HA:
9566                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9567                           break;
9568
9569                         case R_PPC64_TOC16_LO_DS:
9570                           rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9571                           break;
9572
9573                         default:
9574                           if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9575                             ppc_howto_init ();
9576                           info->callbacks->einfo
9577                             /* xgettext:c-format */
9578                             (_("%H: %s references "
9579                                "optimized away TOC entry\n"),
9580                              ibfd, sec, rel->r_offset,
9581                              ppc64_elf_howto_table[r_type]->name);
9582                           bfd_set_error (bfd_error_bad_value);
9583                           goto error_ret;
9584                         }
9585                       rel->r_addend = tocrel->r_addend;
9586                       elf_section_data (sec)->relocs = relstart;
9587                       continue;
9588                     }
9589
9590                   if (h != NULL || sym->st_value != 0)
9591                     continue;
9592
9593                   rel->r_addend -= skip[val >> 3];
9594                   elf_section_data (sec)->relocs = relstart;
9595                 }
9596
9597               if (elf_section_data (sec)->relocs != relstart)
9598                 free (relstart);
9599             }
9600
9601           /* We shouldn't have local or global symbols defined in the TOC,
9602              but handle them anyway.  */
9603           if (local_syms != NULL)
9604             for (sym = local_syms;
9605                  sym < local_syms + symtab_hdr->sh_info;
9606                  ++sym)
9607               if (sym->st_value != 0
9608                   && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9609                 {
9610                   unsigned long i;
9611
9612                   if (sym->st_value > toc->rawsize)
9613                     i = toc->rawsize >> 3;
9614                   else
9615                     i = sym->st_value >> 3;
9616
9617                   if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9618                     {
9619                       if (local_toc_syms)
9620                         _bfd_error_handler
9621                           (_("%s defined on removed toc entry"),
9622                            bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9623                       do
9624                         ++i;
9625                       while ((skip[i] & (ref_from_discarded | can_optimize)));
9626                       sym->st_value = (bfd_vma) i << 3;
9627                     }
9628
9629                   sym->st_value -= skip[i];
9630                   symtab_hdr->contents = (unsigned char *) local_syms;
9631                 }
9632
9633           /* Adjust any global syms defined in this toc input section.  */
9634           if (toc_inf.global_toc_syms)
9635             {
9636               toc_inf.toc = toc;
9637               toc_inf.skip = skip;
9638               toc_inf.global_toc_syms = FALSE;
9639               elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9640                                       &toc_inf);
9641             }
9642
9643           if (toc->reloc_count != 0)
9644             {
9645               Elf_Internal_Shdr *rel_hdr;
9646               Elf_Internal_Rela *wrel;
9647               bfd_size_type sz;
9648
9649               /* Remove unused toc relocs, and adjust those we keep.  */
9650               if (toc_relocs == NULL)
9651                 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9652                                                         info->keep_memory);
9653               if (toc_relocs == NULL)
9654                 goto error_ret;
9655
9656               wrel = toc_relocs;
9657               for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9658                 if ((skip[rel->r_offset >> 3]
9659                      & (ref_from_discarded | can_optimize)) == 0)
9660                   {
9661                     wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9662                     wrel->r_info = rel->r_info;
9663                     wrel->r_addend = rel->r_addend;
9664                     ++wrel;
9665                   }
9666                 else if (!dec_dynrel_count (rel->r_info, toc, info,
9667                                             &local_syms, NULL, NULL))
9668                   goto error_ret;
9669
9670               elf_section_data (toc)->relocs = toc_relocs;
9671               toc->reloc_count = wrel - toc_relocs;
9672               rel_hdr = _bfd_elf_single_rel_hdr (toc);
9673               sz = rel_hdr->sh_entsize;
9674               rel_hdr->sh_size = toc->reloc_count * sz;
9675             }
9676         }
9677       else if (toc_relocs != NULL
9678                && elf_section_data (toc)->relocs != toc_relocs)
9679         free (toc_relocs);
9680
9681       if (local_syms != NULL
9682           && symtab_hdr->contents != (unsigned char *) local_syms)
9683         {
9684           if (!info->keep_memory)
9685             free (local_syms);
9686           else
9687             symtab_hdr->contents = (unsigned char *) local_syms;
9688         }
9689       free (skip);
9690     }
9691
9692   return TRUE;
9693 }
9694
9695 /* Return true iff input section I references the TOC using
9696    instructions limited to +/-32k offsets.  */
9697
9698 bfd_boolean
9699 ppc64_elf_has_small_toc_reloc (asection *i)
9700 {
9701   return (is_ppc64_elf (i->owner)
9702           && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9703 }
9704
9705 /* Allocate space for one GOT entry.  */
9706
9707 static void
9708 allocate_got (struct elf_link_hash_entry *h,
9709               struct bfd_link_info *info,
9710               struct got_entry *gent)
9711 {
9712   struct ppc_link_hash_table *htab = ppc_hash_table (info);
9713   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9714   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9715                  ? 16 : 8);
9716   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9717                   ? 2 : 1) * sizeof (Elf64_External_Rela);
9718   asection *got = ppc64_elf_tdata (gent->owner)->got;
9719
9720   gent->got.offset = got->size;
9721   got->size += entsize;
9722
9723   if (h->type == STT_GNU_IFUNC)
9724     {
9725       htab->elf.irelplt->size += rentsize;
9726       htab->got_reli_size += rentsize;
9727     }
9728   else if ((bfd_link_pic (info)
9729             || (htab->elf.dynamic_sections_created
9730                 && h->dynindx != -1
9731                 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9732            && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9733     {
9734       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9735       relgot->size += rentsize;
9736     }
9737 }
9738
9739 /* This function merges got entries in the same toc group.  */
9740
9741 static void
9742 merge_got_entries (struct got_entry **pent)
9743 {
9744   struct got_entry *ent, *ent2;
9745
9746   for (ent = *pent; ent != NULL; ent = ent->next)
9747     if (!ent->is_indirect)
9748       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9749         if (!ent2->is_indirect
9750             && ent2->addend == ent->addend
9751             && ent2->tls_type == ent->tls_type
9752             && elf_gp (ent2->owner) == elf_gp (ent->owner))
9753           {
9754             ent2->is_indirect = TRUE;
9755             ent2->got.ent = ent;
9756           }
9757 }
9758
9759 /* If H is undefined, make it dynamic if that makes sense.  */
9760
9761 static bfd_boolean
9762 ensure_undef_dynamic (struct bfd_link_info *info,
9763                       struct elf_link_hash_entry *h)
9764 {
9765   struct elf_link_hash_table *htab = elf_hash_table (info);
9766
9767   if (htab->dynamic_sections_created
9768       && ((info->dynamic_undefined_weak != 0
9769            && h->root.type == bfd_link_hash_undefweak)
9770           || h->root.type == bfd_link_hash_undefined)
9771       && h->dynindx == -1
9772       && !h->forced_local
9773       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9774     return bfd_elf_link_record_dynamic_symbol (info, h);
9775   return TRUE;
9776 }
9777
9778 /* Allocate space in .plt, .got and associated reloc sections for
9779    dynamic relocs.  */
9780
9781 static bfd_boolean
9782 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9783 {
9784   struct bfd_link_info *info;
9785   struct ppc_link_hash_table *htab;
9786   asection *s;
9787   struct ppc_link_hash_entry *eh;
9788   struct got_entry **pgent, *gent;
9789
9790   if (h->root.type == bfd_link_hash_indirect)
9791     return TRUE;
9792
9793   info = (struct bfd_link_info *) inf;
9794   htab = ppc_hash_table (info);
9795   if (htab == NULL)
9796     return FALSE;
9797
9798   eh = (struct ppc_link_hash_entry *) h;
9799   /* Run through the TLS GD got entries first if we're changing them
9800      to TPREL.  */
9801   if ((eh->tls_mask & TLS_TPRELGD) != 0)
9802     for (gent = h->got.glist; gent != NULL; gent = gent->next)
9803       if (gent->got.refcount > 0
9804           && (gent->tls_type & TLS_GD) != 0)
9805         {
9806           /* This was a GD entry that has been converted to TPREL.  If
9807              there happens to be a TPREL entry we can use that one.  */
9808           struct got_entry *ent;
9809           for (ent = h->got.glist; ent != NULL; ent = ent->next)
9810             if (ent->got.refcount > 0
9811                 && (ent->tls_type & TLS_TPREL) != 0
9812                 && ent->addend == gent->addend
9813                 && ent->owner == gent->owner)
9814               {
9815                 gent->got.refcount = 0;
9816                 break;
9817               }
9818
9819           /* If not, then we'll be using our own TPREL entry.  */
9820           if (gent->got.refcount != 0)
9821             gent->tls_type = TLS_TLS | TLS_TPREL;
9822         }
9823
9824   /* Remove any list entry that won't generate a word in the GOT before
9825      we call merge_got_entries.  Otherwise we risk merging to empty
9826      entries.  */
9827   pgent = &h->got.glist;
9828   while ((gent = *pgent) != NULL)
9829     if (gent->got.refcount > 0)
9830       {
9831         if ((gent->tls_type & TLS_LD) != 0
9832             && !h->def_dynamic)
9833           {
9834             ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9835             *pgent = gent->next;
9836           }
9837         else
9838           pgent = &gent->next;
9839       }
9840     else
9841       *pgent = gent->next;
9842
9843   if (!htab->do_multi_toc)
9844     merge_got_entries (&h->got.glist);
9845
9846   for (gent = h->got.glist; gent != NULL; gent = gent->next)
9847     if (!gent->is_indirect)
9848       {
9849         /* Make sure this symbol is output as a dynamic symbol.  */
9850         if (!ensure_undef_dynamic (info, h))
9851           return FALSE;
9852
9853         if (!is_ppc64_elf (gent->owner))
9854           abort ();
9855
9856         allocate_got (h, info, gent);
9857       }
9858
9859   /* If no dynamic sections we can't have dynamic relocs, except for
9860      IFUNCs which are handled even in static executables.  */
9861   if (!htab->elf.dynamic_sections_created
9862       && h->type != STT_GNU_IFUNC)
9863     eh->dyn_relocs = NULL;
9864
9865   /* Also discard relocs on undefined weak syms with non-default
9866      visibility, or when dynamic_undefined_weak says so.  */
9867   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9868     eh->dyn_relocs = NULL;
9869
9870   if (eh->dyn_relocs != NULL)
9871     {
9872       struct elf_dyn_relocs *p, **pp;
9873
9874       /* In the shared -Bsymbolic case, discard space allocated for
9875          dynamic pc-relative relocs against symbols which turn out to
9876          be defined in regular objects.  For the normal shared case,
9877          discard space for relocs that have become local due to symbol
9878          visibility changes.  */
9879
9880       if (bfd_link_pic (info))
9881         {
9882           /* Relocs that use pc_count are those that appear on a call
9883              insn, or certain REL relocs (see must_be_dyn_reloc) that
9884              can be generated via assembly.  We want calls to
9885              protected symbols to resolve directly to the function
9886              rather than going via the plt.  If people want function
9887              pointer comparisons to work as expected then they should
9888              avoid writing weird assembly.  */
9889           if (SYMBOL_CALLS_LOCAL (info, h))
9890             {
9891               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9892                 {
9893                   p->count -= p->pc_count;
9894                   p->pc_count = 0;
9895                   if (p->count == 0)
9896                     *pp = p->next;
9897                   else
9898                     pp = &p->next;
9899                 }
9900             }
9901
9902           if (eh->dyn_relocs != NULL)
9903             {
9904               /* Make sure this symbol is output as a dynamic symbol.  */
9905               if (!ensure_undef_dynamic (info, h))
9906                 return FALSE;
9907             }
9908         }
9909       else if (h->type == STT_GNU_IFUNC)
9910         {
9911           /* A plt entry is always created when making direct calls to
9912              an ifunc, even when building a static executable, but
9913              that doesn't cover all cases.  We may have only an ifunc
9914              initialised function pointer for a given ifunc symbol.
9915
9916              For ELFv2, dynamic relocations are not required when
9917              generating a global entry PLT stub.  */
9918           if (abiversion (info->output_bfd) >= 2)
9919             {
9920               if (global_entry_stub (h))
9921                 eh->dyn_relocs = NULL;
9922             }
9923
9924           /* For ELFv1 we have function descriptors.  Descriptors need
9925              to be treated like PLT entries and thus have dynamic
9926              relocations.  One exception is when the function
9927              descriptor is copied into .dynbss (which should only
9928              happen with ancient versions of gcc).  */
9929           else if (h->needs_copy)
9930             eh->dyn_relocs = NULL;
9931         }
9932       else if (ELIMINATE_COPY_RELOCS)
9933         {
9934           /* For the non-pic case, discard space for relocs against
9935              symbols which turn out to need copy relocs or are not
9936              dynamic.  */
9937           if (!h->non_got_ref
9938               && !h->def_regular)
9939             {
9940               /* Make sure this symbol is output as a dynamic symbol.  */
9941               if (!ensure_undef_dynamic (info, h))
9942                 return FALSE;
9943
9944               if (h->dynindx == -1)
9945                 eh->dyn_relocs = NULL;
9946             }
9947           else
9948             eh->dyn_relocs = NULL;
9949         }
9950
9951       /* Finally, allocate space.  */
9952       for (p = eh->dyn_relocs; p != NULL; p = p->next)
9953         {
9954           asection *sreloc = elf_section_data (p->sec)->sreloc;
9955           if (eh->elf.type == STT_GNU_IFUNC)
9956             sreloc = htab->elf.irelplt;
9957           sreloc->size += p->count * sizeof (Elf64_External_Rela);
9958         }
9959     }
9960
9961   if ((htab->elf.dynamic_sections_created
9962        && h->dynindx != -1)
9963       || h->type == STT_GNU_IFUNC)
9964     {
9965       struct plt_entry *pent;
9966       bfd_boolean doneone = FALSE;
9967       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9968         if (pent->plt.refcount > 0)
9969           {
9970             if (!htab->elf.dynamic_sections_created
9971                 || h->dynindx == -1)
9972               {
9973                 s = htab->elf.iplt;
9974                 pent->plt.offset = s->size;
9975                 s->size += PLT_ENTRY_SIZE (htab);
9976                 s = htab->elf.irelplt;
9977               }
9978             else
9979               {
9980                 /* If this is the first .plt entry, make room for the special
9981                    first entry.  */
9982                 s = htab->elf.splt;
9983                 if (s->size == 0)
9984                   s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9985
9986                 pent->plt.offset = s->size;
9987
9988                 /* Make room for this entry.  */
9989                 s->size += PLT_ENTRY_SIZE (htab);
9990
9991                 /* Make room for the .glink code.  */
9992                 s = htab->glink;
9993                 if (s->size == 0)
9994                   s->size += GLINK_CALL_STUB_SIZE;
9995                 if (htab->opd_abi)
9996                   {
9997                     /* We need bigger stubs past index 32767.  */
9998                     if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9999                       s->size += 4;
10000                     s->size += 2*4;
10001                   }
10002                 else
10003                   s->size += 4;
10004
10005                 /* We also need to make an entry in the .rela.plt section.  */
10006                 s = htab->elf.srelplt;
10007               }
10008             s->size += sizeof (Elf64_External_Rela);
10009             doneone = TRUE;
10010           }
10011         else
10012           pent->plt.offset = (bfd_vma) -1;
10013       if (!doneone)
10014         {
10015           h->plt.plist = NULL;
10016           h->needs_plt = 0;
10017         }
10018     }
10019   else
10020     {
10021       h->plt.plist = NULL;
10022       h->needs_plt = 0;
10023     }
10024
10025   return TRUE;
10026 }
10027
10028 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10029    to set up space for global entry stubs.  These are put in glink,
10030    after the branch table.  */
10031
10032 static bfd_boolean
10033 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10034 {
10035   struct bfd_link_info *info;
10036   struct ppc_link_hash_table *htab;
10037   struct plt_entry *pent;
10038   asection *s;
10039
10040   if (h->root.type == bfd_link_hash_indirect)
10041     return TRUE;
10042
10043   if (!h->pointer_equality_needed)
10044     return TRUE;
10045
10046   if (h->def_regular)
10047     return TRUE;
10048
10049   info = inf;
10050   htab = ppc_hash_table (info);
10051   if (htab == NULL)
10052     return FALSE;
10053
10054   s = htab->glink;
10055   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10056     if (pent->plt.offset != (bfd_vma) -1
10057         && pent->addend == 0)
10058       {
10059         /* For ELFv2, if this symbol is not defined in a regular file
10060            and we are not generating a shared library or pie, then we
10061            need to define the symbol in the executable on a call stub.
10062            This is to avoid text relocations.  */
10063         s->size = (s->size + 15) & -16;
10064         h->root.type = bfd_link_hash_defined;
10065         h->root.u.def.section = s;
10066         h->root.u.def.value = s->size;
10067         s->size += 16;
10068         break;
10069       }
10070   return TRUE;
10071 }
10072
10073 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10074    read-only sections.  */
10075
10076 static bfd_boolean
10077 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
10078 {
10079   if (h->root.type == bfd_link_hash_indirect)
10080     return TRUE;
10081
10082   if (readonly_dynrelocs (h))
10083     {
10084       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
10085
10086       /* Not an error, just cut short the traversal.  */
10087       return FALSE;
10088     }
10089   return TRUE;
10090 }
10091
10092 /* Set the sizes of the dynamic sections.  */
10093
10094 static bfd_boolean
10095 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10096                                  struct bfd_link_info *info)
10097 {
10098   struct ppc_link_hash_table *htab;
10099   bfd *dynobj;
10100   asection *s;
10101   bfd_boolean relocs;
10102   bfd *ibfd;
10103   struct got_entry *first_tlsld;
10104
10105   htab = ppc_hash_table (info);
10106   if (htab == NULL)
10107     return FALSE;
10108
10109   dynobj = htab->elf.dynobj;
10110   if (dynobj == NULL)
10111     abort ();
10112
10113   if (htab->elf.dynamic_sections_created)
10114     {
10115       /* Set the contents of the .interp section to the interpreter.  */
10116       if (bfd_link_executable (info) && !info->nointerp)
10117         {
10118           s = bfd_get_linker_section (dynobj, ".interp");
10119           if (s == NULL)
10120             abort ();
10121           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10122           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10123         }
10124     }
10125
10126   /* Set up .got offsets for local syms, and space for local dynamic
10127      relocs.  */
10128   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10129     {
10130       struct got_entry **lgot_ents;
10131       struct got_entry **end_lgot_ents;
10132       struct plt_entry **local_plt;
10133       struct plt_entry **end_local_plt;
10134       unsigned char *lgot_masks;
10135       bfd_size_type locsymcount;
10136       Elf_Internal_Shdr *symtab_hdr;
10137
10138       if (!is_ppc64_elf (ibfd))
10139         continue;
10140
10141       for (s = ibfd->sections; s != NULL; s = s->next)
10142         {
10143           struct ppc_dyn_relocs *p;
10144
10145           for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10146             {
10147               if (!bfd_is_abs_section (p->sec)
10148                   && bfd_is_abs_section (p->sec->output_section))
10149                 {
10150                   /* Input section has been discarded, either because
10151                      it is a copy of a linkonce section or due to
10152                      linker script /DISCARD/, so we'll be discarding
10153                      the relocs too.  */
10154                 }
10155               else if (p->count != 0)
10156                 {
10157                   asection *srel = elf_section_data (p->sec)->sreloc;
10158                   if (p->ifunc)
10159                     srel = htab->elf.irelplt;
10160                   srel->size += p->count * sizeof (Elf64_External_Rela);
10161                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10162                     info->flags |= DF_TEXTREL;
10163                 }
10164             }
10165         }
10166
10167       lgot_ents = elf_local_got_ents (ibfd);
10168       if (!lgot_ents)
10169         continue;
10170
10171       symtab_hdr = &elf_symtab_hdr (ibfd);
10172       locsymcount = symtab_hdr->sh_info;
10173       end_lgot_ents = lgot_ents + locsymcount;
10174       local_plt = (struct plt_entry **) end_lgot_ents;
10175       end_local_plt = local_plt + locsymcount;
10176       lgot_masks = (unsigned char *) end_local_plt;
10177       s = ppc64_elf_tdata (ibfd)->got;
10178       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10179         {
10180           struct got_entry **pent, *ent;
10181
10182           pent = lgot_ents;
10183           while ((ent = *pent) != NULL)
10184             if (ent->got.refcount > 0)
10185               {
10186                 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10187                   {
10188                     ppc64_tlsld_got (ibfd)->got.refcount += 1;
10189                     *pent = ent->next;
10190                   }
10191                 else
10192                   {
10193                     unsigned int ent_size = 8;
10194                     unsigned int rel_size = sizeof (Elf64_External_Rela);
10195
10196                     ent->got.offset = s->size;
10197                     if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10198                       {
10199                         ent_size *= 2;
10200                         rel_size *= 2;
10201                       }
10202                     s->size += ent_size;
10203                     if ((*lgot_masks & PLT_IFUNC) != 0)
10204                       {
10205                         htab->elf.irelplt->size += rel_size;
10206                         htab->got_reli_size += rel_size;
10207                       }
10208                     else if (bfd_link_pic (info))
10209                       {
10210                         asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10211                         srel->size += rel_size;
10212                       }
10213                     pent = &ent->next;
10214                   }
10215               }
10216             else
10217               *pent = ent->next;
10218         }
10219
10220       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
10221       for (; local_plt < end_local_plt; ++local_plt)
10222         {
10223           struct plt_entry *ent;
10224
10225           for (ent = *local_plt; ent != NULL; ent = ent->next)
10226             if (ent->plt.refcount > 0)
10227               {
10228                 s = htab->elf.iplt;
10229                 ent->plt.offset = s->size;
10230                 s->size += PLT_ENTRY_SIZE (htab);
10231
10232                 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10233               }
10234             else
10235               ent->plt.offset = (bfd_vma) -1;
10236         }
10237     }
10238
10239   /* Allocate global sym .plt and .got entries, and space for global
10240      sym dynamic relocs.  */
10241   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10242   /* Stash the end of glink branch table.  */
10243   if (htab->glink != NULL)
10244     htab->glink->rawsize = htab->glink->size;
10245
10246   if (!htab->opd_abi && !bfd_link_pic (info))
10247     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10248
10249   first_tlsld = NULL;
10250   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10251     {
10252       struct got_entry *ent;
10253
10254       if (!is_ppc64_elf (ibfd))
10255         continue;
10256
10257       ent = ppc64_tlsld_got (ibfd);
10258       if (ent->got.refcount > 0)
10259         {
10260           if (!htab->do_multi_toc && first_tlsld != NULL)
10261             {
10262               ent->is_indirect = TRUE;
10263               ent->got.ent = first_tlsld;
10264             }
10265           else
10266             {
10267               if (first_tlsld == NULL)
10268                 first_tlsld = ent;
10269               s = ppc64_elf_tdata (ibfd)->got;
10270               ent->got.offset = s->size;
10271               ent->owner = ibfd;
10272               s->size += 16;
10273               if (bfd_link_pic (info))
10274                 {
10275                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10276                   srel->size += sizeof (Elf64_External_Rela);
10277                 }
10278             }
10279         }
10280       else
10281         ent->got.offset = (bfd_vma) -1;
10282     }
10283
10284   /* We now have determined the sizes of the various dynamic sections.
10285      Allocate memory for them.  */
10286   relocs = FALSE;
10287   for (s = dynobj->sections; s != NULL; s = s->next)
10288     {
10289       if ((s->flags & SEC_LINKER_CREATED) == 0)
10290         continue;
10291
10292       if (s == htab->brlt || s == htab->relbrlt)
10293         /* These haven't been allocated yet;  don't strip.  */
10294         continue;
10295       else if (s == htab->elf.sgot
10296                || s == htab->elf.splt
10297                || s == htab->elf.iplt
10298                || s == htab->glink
10299                || s == htab->elf.sdynbss
10300                || s == htab->elf.sdynrelro)
10301         {
10302           /* Strip this section if we don't need it; see the
10303              comment below.  */
10304         }
10305       else if (s == htab->glink_eh_frame)
10306         {
10307           if (!bfd_is_abs_section (s->output_section))
10308             /* Not sized yet.  */
10309             continue;
10310         }
10311       else if (CONST_STRNEQ (s->name, ".rela"))
10312         {
10313           if (s->size != 0)
10314             {
10315               if (s != htab->elf.srelplt)
10316                 relocs = TRUE;
10317
10318               /* We use the reloc_count field as a counter if we need
10319                  to copy relocs into the output file.  */
10320               s->reloc_count = 0;
10321             }
10322         }
10323       else
10324         {
10325           /* It's not one of our sections, so don't allocate space.  */
10326           continue;
10327         }
10328
10329       if (s->size == 0)
10330         {
10331           /* If we don't need this section, strip it from the
10332              output file.  This is mostly to handle .rela.bss and
10333              .rela.plt.  We must create both sections in
10334              create_dynamic_sections, because they must be created
10335              before the linker maps input sections to output
10336              sections.  The linker does that before
10337              adjust_dynamic_symbol is called, and it is that
10338              function which decides whether anything needs to go
10339              into these sections.  */
10340           s->flags |= SEC_EXCLUDE;
10341           continue;
10342         }
10343
10344       if ((s->flags & SEC_HAS_CONTENTS) == 0)
10345         continue;
10346
10347       /* Allocate memory for the section contents.  We use bfd_zalloc
10348          here in case unused entries are not reclaimed before the
10349          section's contents are written out.  This should not happen,
10350          but this way if it does we get a R_PPC64_NONE reloc in .rela
10351          sections instead of garbage.
10352          We also rely on the section contents being zero when writing
10353          the GOT and .dynrelro.  */
10354       s->contents = bfd_zalloc (dynobj, s->size);
10355       if (s->contents == NULL)
10356         return FALSE;
10357     }
10358
10359   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10360     {
10361       if (!is_ppc64_elf (ibfd))
10362         continue;
10363
10364       s = ppc64_elf_tdata (ibfd)->got;
10365       if (s != NULL && s != htab->elf.sgot)
10366         {
10367           if (s->size == 0)
10368             s->flags |= SEC_EXCLUDE;
10369           else
10370             {
10371               s->contents = bfd_zalloc (ibfd, s->size);
10372               if (s->contents == NULL)
10373                 return FALSE;
10374             }
10375         }
10376       s = ppc64_elf_tdata (ibfd)->relgot;
10377       if (s != NULL)
10378         {
10379           if (s->size == 0)
10380             s->flags |= SEC_EXCLUDE;
10381           else
10382             {
10383               s->contents = bfd_zalloc (ibfd, s->size);
10384               if (s->contents == NULL)
10385                 return FALSE;
10386               relocs = TRUE;
10387               s->reloc_count = 0;
10388             }
10389         }
10390     }
10391
10392   if (htab->elf.dynamic_sections_created)
10393     {
10394       bfd_boolean tls_opt;
10395
10396       /* Add some entries to the .dynamic section.  We fill in the
10397          values later, in ppc64_elf_finish_dynamic_sections, but we
10398          must add the entries now so that we get the correct size for
10399          the .dynamic section.  The DT_DEBUG entry is filled in by the
10400          dynamic linker and used by the debugger.  */
10401 #define add_dynamic_entry(TAG, VAL) \
10402   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10403
10404       if (bfd_link_executable (info))
10405         {
10406           if (!add_dynamic_entry (DT_DEBUG, 0))
10407             return FALSE;
10408         }
10409
10410       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10411         {
10412           if (!add_dynamic_entry (DT_PLTGOT, 0)
10413               || !add_dynamic_entry (DT_PLTRELSZ, 0)
10414               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10415               || !add_dynamic_entry (DT_JMPREL, 0)
10416               || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10417             return FALSE;
10418         }
10419
10420       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10421         {
10422           if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10423               || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10424             return FALSE;
10425         }
10426
10427       tls_opt = (htab->params->tls_get_addr_opt
10428                  && htab->tls_get_addr_fd != NULL
10429                  && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10430       if (tls_opt || !htab->opd_abi)
10431         {
10432           if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10433             return FALSE;
10434         }
10435
10436       if (relocs)
10437         {
10438           if (!add_dynamic_entry (DT_RELA, 0)
10439               || !add_dynamic_entry (DT_RELASZ, 0)
10440               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10441             return FALSE;
10442
10443           /* If any dynamic relocs apply to a read-only section,
10444              then we need a DT_TEXTREL entry.  */
10445           if ((info->flags & DF_TEXTREL) == 0)
10446             elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10447
10448           if ((info->flags & DF_TEXTREL) != 0)
10449             {
10450               if (!add_dynamic_entry (DT_TEXTREL, 0))
10451                 return FALSE;
10452             }
10453         }
10454     }
10455 #undef add_dynamic_entry
10456
10457   return TRUE;
10458 }
10459
10460 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
10461
10462 static bfd_boolean
10463 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10464 {
10465   if (h->plt.plist != NULL
10466       && !h->def_regular
10467       && !h->pointer_equality_needed)
10468     return FALSE;
10469
10470   return _bfd_elf_hash_symbol (h);
10471 }
10472
10473 /* Determine the type of stub needed, if any, for a call.  */
10474
10475 static inline enum ppc_stub_type
10476 ppc_type_of_stub (asection *input_sec,
10477                   const Elf_Internal_Rela *rel,
10478                   struct ppc_link_hash_entry **hash,
10479                   struct plt_entry **plt_ent,
10480                   bfd_vma destination,
10481                   unsigned long local_off)
10482 {
10483   struct ppc_link_hash_entry *h = *hash;
10484   bfd_vma location;
10485   bfd_vma branch_offset;
10486   bfd_vma max_branch_offset;
10487   enum elf_ppc64_reloc_type r_type;
10488
10489   if (h != NULL)
10490     {
10491       struct plt_entry *ent;
10492       struct ppc_link_hash_entry *fdh = h;
10493       if (h->oh != NULL
10494           && h->oh->is_func_descriptor)
10495         {
10496           fdh = ppc_follow_link (h->oh);
10497           *hash = fdh;
10498         }
10499
10500       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10501         if (ent->addend == rel->r_addend
10502             && ent->plt.offset != (bfd_vma) -1)
10503           {
10504             *plt_ent = ent;
10505             return ppc_stub_plt_call;
10506           }
10507
10508       /* Here, we know we don't have a plt entry.  If we don't have a
10509          either a defined function descriptor or a defined entry symbol
10510          in a regular object file, then it is pointless trying to make
10511          any other type of stub.  */
10512       if (!is_static_defined (&fdh->elf)
10513           && !is_static_defined (&h->elf))
10514         return ppc_stub_none;
10515     }
10516   else if (elf_local_got_ents (input_sec->owner) != NULL)
10517     {
10518       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10519       struct plt_entry **local_plt = (struct plt_entry **)
10520         elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10521       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10522
10523       if (local_plt[r_symndx] != NULL)
10524         {
10525           struct plt_entry *ent;
10526
10527           for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10528             if (ent->addend == rel->r_addend
10529                 && ent->plt.offset != (bfd_vma) -1)
10530               {
10531                 *plt_ent = ent;
10532                 return ppc_stub_plt_call;
10533               }
10534         }
10535     }
10536
10537   /* Determine where the call point is.  */
10538   location = (input_sec->output_offset
10539               + input_sec->output_section->vma
10540               + rel->r_offset);
10541
10542   branch_offset = destination - location;
10543   r_type = ELF64_R_TYPE (rel->r_info);
10544
10545   /* Determine if a long branch stub is needed.  */
10546   max_branch_offset = 1 << 25;
10547   if (r_type != R_PPC64_REL24)
10548     max_branch_offset = 1 << 15;
10549
10550   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10551     /* We need a stub.  Figure out whether a long_branch or plt_branch
10552        is needed later.  */
10553     return ppc_stub_long_branch;
10554
10555   return ppc_stub_none;
10556 }
10557
10558 /* With power7 weakly ordered memory model, it is possible for ld.so
10559    to update a plt entry in one thread and have another thread see a
10560    stale zero toc entry.  To avoid this we need some sort of acquire
10561    barrier in the call stub.  One solution is to make the load of the
10562    toc word seem to appear to depend on the load of the function entry
10563    word.  Another solution is to test for r2 being zero, and branch to
10564    the appropriate glink entry if so.
10565
10566    .    fake dep barrier        compare
10567    .    ld 12,xxx(2)            ld 12,xxx(2)
10568    .    mtctr 12                mtctr 12
10569    .    xor 11,12,12            ld 2,xxx+8(2)
10570    .    add 2,2,11              cmpldi 2,0
10571    .    ld 2,xxx+8(2)           bnectr+
10572    .    bctr                    b <glink_entry>
10573
10574    The solution involving the compare turns out to be faster, so
10575    that's what we use unless the branch won't reach.  */
10576
10577 #define ALWAYS_USE_FAKE_DEP 0
10578 #define ALWAYS_EMIT_R2SAVE 0
10579
10580 #define PPC_LO(v) ((v) & 0xffff)
10581 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10582 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10583
10584 static inline unsigned int
10585 plt_stub_size (struct ppc_link_hash_table *htab,
10586                struct ppc_stub_hash_entry *stub_entry,
10587                bfd_vma off)
10588 {
10589   unsigned size = 12;
10590
10591   if (ALWAYS_EMIT_R2SAVE
10592       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10593     size += 4;
10594   if (PPC_HA (off) != 0)
10595     size += 4;
10596   if (htab->opd_abi)
10597     {
10598       size += 4;
10599       if (htab->params->plt_static_chain)
10600         size += 4;
10601       if (htab->params->plt_thread_safe
10602           && htab->elf.dynamic_sections_created
10603           && stub_entry->h != NULL
10604           && stub_entry->h->elf.dynindx != -1)
10605         size += 8;
10606       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10607         size += 4;
10608     }
10609   if (stub_entry->h != NULL
10610       && (stub_entry->h == htab->tls_get_addr_fd
10611           || stub_entry->h == htab->tls_get_addr)
10612       && htab->params->tls_get_addr_opt)
10613     {
10614       size += 7 * 4;
10615       if (ALWAYS_EMIT_R2SAVE
10616           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10617         size += 6 * 4;
10618     }
10619   return size;
10620 }
10621
10622 /* Depending on the sign of plt_stub_align:
10623    If positive, return the padding to align to a 2**plt_stub_align
10624    boundary.
10625    If negative, if this stub would cross fewer 2**plt_stub_align
10626    boundaries if we align, then return the padding needed to do so.  */
10627
10628 static inline unsigned int
10629 plt_stub_pad (struct ppc_link_hash_table *htab,
10630               struct ppc_stub_hash_entry *stub_entry,
10631               bfd_vma plt_off)
10632 {
10633   int stub_align;
10634   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10635   bfd_vma stub_off = stub_entry->group->stub_sec->size;
10636
10637   if (htab->params->plt_stub_align >= 0)
10638     {
10639       stub_align = 1 << htab->params->plt_stub_align;
10640       if ((stub_off & (stub_align - 1)) != 0)
10641         return stub_align - (stub_off & (stub_align - 1));
10642       return 0;
10643     }
10644
10645   stub_align = 1 << -htab->params->plt_stub_align;
10646   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10647       > ((stub_size - 1) & -stub_align))
10648     return stub_align - (stub_off & (stub_align - 1));
10649   return 0;
10650 }
10651
10652 /* Build a .plt call stub.  */
10653
10654 static inline bfd_byte *
10655 build_plt_stub (struct ppc_link_hash_table *htab,
10656                 struct ppc_stub_hash_entry *stub_entry,
10657                 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10658 {
10659   bfd *obfd = htab->params->stub_bfd;
10660   bfd_boolean plt_load_toc = htab->opd_abi;
10661   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10662   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10663                                  && htab->elf.dynamic_sections_created
10664                                  && stub_entry->h != NULL
10665                                  && stub_entry->h->elf.dynindx != -1);
10666   bfd_boolean use_fake_dep = plt_thread_safe;
10667   bfd_vma cmp_branch_off = 0;
10668
10669   if (!ALWAYS_USE_FAKE_DEP
10670       && plt_load_toc
10671       && plt_thread_safe
10672       && !((stub_entry->h == htab->tls_get_addr_fd
10673             || stub_entry->h == htab->tls_get_addr)
10674            && htab->params->tls_get_addr_opt))
10675     {
10676       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10677       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10678                           / PLT_ENTRY_SIZE (htab));
10679       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10680       bfd_vma to, from;
10681
10682       if (pltindex > 32768)
10683         glinkoff += (pltindex - 32768) * 4;
10684       to = (glinkoff
10685             + htab->glink->output_offset
10686             + htab->glink->output_section->vma);
10687       from = (p - stub_entry->group->stub_sec->contents
10688               + 4 * (ALWAYS_EMIT_R2SAVE
10689                      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10690               + 4 * (PPC_HA (offset) != 0)
10691               + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10692                      != PPC_HA (offset))
10693               + 4 * (plt_static_chain != 0)
10694               + 20
10695               + stub_entry->group->stub_sec->output_offset
10696               + stub_entry->group->stub_sec->output_section->vma);
10697       cmp_branch_off = to - from;
10698       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10699     }
10700
10701   if (PPC_HA (offset) != 0)
10702     {
10703       if (r != NULL)
10704         {
10705           if (ALWAYS_EMIT_R2SAVE
10706               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10707             r[0].r_offset += 4;
10708           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10709           r[1].r_offset = r[0].r_offset + 4;
10710           r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10711           r[1].r_addend = r[0].r_addend;
10712           if (plt_load_toc)
10713             {
10714               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10715                 {
10716                   r[2].r_offset = r[1].r_offset + 4;
10717                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10718                   r[2].r_addend = r[0].r_addend;
10719                 }
10720               else
10721                 {
10722                   r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10723                   r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10724                   r[2].r_addend = r[0].r_addend + 8;
10725                   if (plt_static_chain)
10726                     {
10727                       r[3].r_offset = r[2].r_offset + 4;
10728                       r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10729                       r[3].r_addend = r[0].r_addend + 16;
10730                     }
10731                 }
10732             }
10733         }
10734       if (ALWAYS_EMIT_R2SAVE
10735           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10736         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10737       if (plt_load_toc)
10738         {
10739           bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10740           bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),  p += 4;
10741         }
10742       else
10743         {
10744           bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10745           bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),  p += 4;
10746         }
10747       if (plt_load_toc
10748           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10749         {
10750           bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10751           offset = 0;
10752         }
10753       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10754       if (plt_load_toc)
10755         {
10756           if (use_fake_dep)
10757             {
10758               bfd_put_32 (obfd, XOR_R2_R12_R12, p),             p += 4;
10759               bfd_put_32 (obfd, ADD_R11_R11_R2, p),             p += 4;
10760             }
10761           bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10762           if (plt_static_chain)
10763             bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10764         }
10765     }
10766   else
10767     {
10768       if (r != NULL)
10769         {
10770           if (ALWAYS_EMIT_R2SAVE
10771               || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10772             r[0].r_offset += 4;
10773           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10774           if (plt_load_toc)
10775             {
10776               if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10777                 {
10778                   r[1].r_offset = r[0].r_offset + 4;
10779                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10780                   r[1].r_addend = r[0].r_addend;
10781                 }
10782               else
10783                 {
10784                   r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10785                   r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10786                   r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10787                   if (plt_static_chain)
10788                     {
10789                       r[2].r_offset = r[1].r_offset + 4;
10790                       r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10791                       r[2].r_addend = r[0].r_addend + 8;
10792                     }
10793                 }
10794             }
10795         }
10796       if (ALWAYS_EMIT_R2SAVE
10797           || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10798         bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),      p += 4;
10799       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),       p += 4;
10800       if (plt_load_toc
10801           && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10802         {
10803           bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),   p += 4;
10804           offset = 0;
10805         }
10806       bfd_put_32 (obfd, MTCTR_R12, p),                          p += 4;
10807       if (plt_load_toc)
10808         {
10809           if (use_fake_dep)
10810             {
10811               bfd_put_32 (obfd, XOR_R11_R12_R12, p),            p += 4;
10812               bfd_put_32 (obfd, ADD_R2_R2_R11, p),              p += 4;
10813             }
10814           if (plt_static_chain)
10815             bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10816           bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10817         }
10818     }
10819   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10820     {
10821       bfd_put_32 (obfd, CMPLDI_R2_0, p),                        p += 4;
10822       bfd_put_32 (obfd, BNECTR_P4, p),                          p += 4;
10823       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10824     }
10825   else
10826     bfd_put_32 (obfd, BCTR, p),                                 p += 4;
10827   return p;
10828 }
10829
10830 /* Build a special .plt call stub for __tls_get_addr.  */
10831
10832 #define LD_R11_0R3      0xe9630000
10833 #define LD_R12_0R3      0xe9830000
10834 #define MR_R0_R3        0x7c601b78
10835 #define CMPDI_R11_0     0x2c2b0000
10836 #define ADD_R3_R12_R13  0x7c6c6a14
10837 #define BEQLR           0x4d820020
10838 #define MR_R3_R0        0x7c030378
10839 #define STD_R11_0R1     0xf9610000
10840 #define BCTRL           0x4e800421
10841 #define LD_R11_0R1      0xe9610000
10842 #define MTLR_R11        0x7d6803a6
10843
10844 static inline bfd_byte *
10845 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10846                          struct ppc_stub_hash_entry *stub_entry,
10847                          bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10848 {
10849   bfd *obfd = htab->params->stub_bfd;
10850
10851   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),         p += 4;
10852   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),         p += 4;
10853   bfd_put_32 (obfd, MR_R0_R3, p),               p += 4;
10854   bfd_put_32 (obfd, CMPDI_R11_0, p),            p += 4;
10855   bfd_put_32 (obfd, ADD_R3_R12_R13, p),         p += 4;
10856   bfd_put_32 (obfd, BEQLR, p),                  p += 4;
10857   bfd_put_32 (obfd, MR_R3_R0, p),               p += 4;
10858   if (r != NULL)
10859     r[0].r_offset += 7 * 4;
10860   if (!ALWAYS_EMIT_R2SAVE
10861       && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10862     return build_plt_stub (htab, stub_entry, p, offset, r);
10863
10864   bfd_put_32 (obfd, MFLR_R11, p),               p += 4;
10865   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10866
10867   if (r != NULL)
10868     r[0].r_offset += 2 * 4;
10869   p = build_plt_stub (htab, stub_entry, p, offset, r);
10870   bfd_put_32 (obfd, BCTRL, p - 4);
10871
10872   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),     p += 4;
10873   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10874   bfd_put_32 (obfd, MTLR_R11, p),               p += 4;
10875   bfd_put_32 (obfd, BLR, p),                    p += 4;
10876
10877   return p;
10878 }
10879
10880 static Elf_Internal_Rela *
10881 get_relocs (asection *sec, int count)
10882 {
10883   Elf_Internal_Rela *relocs;
10884   struct bfd_elf_section_data *elfsec_data;
10885
10886   elfsec_data = elf_section_data (sec);
10887   relocs = elfsec_data->relocs;
10888   if (relocs == NULL)
10889     {
10890       bfd_size_type relsize;
10891       relsize = sec->reloc_count * sizeof (*relocs);
10892       relocs = bfd_alloc (sec->owner, relsize);
10893       if (relocs == NULL)
10894         return NULL;
10895       elfsec_data->relocs = relocs;
10896       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10897                                           sizeof (Elf_Internal_Shdr));
10898       if (elfsec_data->rela.hdr == NULL)
10899         return NULL;
10900       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10901                                         * sizeof (Elf64_External_Rela));
10902       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10903       sec->reloc_count = 0;
10904     }
10905   relocs += sec->reloc_count;
10906   sec->reloc_count += count;
10907   return relocs;
10908 }
10909
10910 static bfd_vma
10911 get_r2off (struct bfd_link_info *info,
10912            struct ppc_stub_hash_entry *stub_entry)
10913 {
10914   struct ppc_link_hash_table *htab = ppc_hash_table (info);
10915   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10916
10917   if (r2off == 0)
10918     {
10919       /* Support linking -R objects.  Get the toc pointer from the
10920          opd entry.  */
10921       char buf[8];
10922       if (!htab->opd_abi)
10923         return r2off;
10924       asection *opd = stub_entry->h->elf.root.u.def.section;
10925       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10926
10927       if (strcmp (opd->name, ".opd") != 0
10928           || opd->reloc_count != 0)
10929         {
10930           info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10931                                   stub_entry->h->elf.root.root.string);
10932           bfd_set_error (bfd_error_bad_value);
10933           return (bfd_vma) -1;
10934         }
10935       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10936         return (bfd_vma) -1;
10937       r2off = bfd_get_64 (opd->owner, buf);
10938       r2off -= elf_gp (info->output_bfd);
10939     }
10940   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10941   return r2off;
10942 }
10943
10944 static bfd_boolean
10945 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10946 {
10947   struct ppc_stub_hash_entry *stub_entry;
10948   struct ppc_branch_hash_entry *br_entry;
10949   struct bfd_link_info *info;
10950   struct ppc_link_hash_table *htab;
10951   bfd_byte *loc;
10952   bfd_byte *p;
10953   bfd_vma dest, off;
10954   int size;
10955   Elf_Internal_Rela *r;
10956   asection *plt;
10957
10958   /* Massage our args to the form they really have.  */
10959   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10960   info = in_arg;
10961
10962   htab = ppc_hash_table (info);
10963   if (htab == NULL)
10964     return FALSE;
10965
10966   /* Make a note of the offset within the stubs for this entry.  */
10967   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10968   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10969
10970   htab->stub_count[stub_entry->stub_type - 1] += 1;
10971   switch (stub_entry->stub_type)
10972     {
10973     case ppc_stub_long_branch:
10974     case ppc_stub_long_branch_r2off:
10975       /* Branches are relative.  This is where we are going to.  */
10976       dest = (stub_entry->target_value
10977               + stub_entry->target_section->output_offset
10978               + stub_entry->target_section->output_section->vma);
10979       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10980       off = dest;
10981
10982       /* And this is where we are coming from.  */
10983       off -= (stub_entry->stub_offset
10984               + stub_entry->group->stub_sec->output_offset
10985               + stub_entry->group->stub_sec->output_section->vma);
10986
10987       size = 4;
10988       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10989         {
10990           bfd_vma r2off = get_r2off (info, stub_entry);
10991
10992           if (r2off == (bfd_vma) -1)
10993             {
10994               htab->stub_error = TRUE;
10995               return FALSE;
10996             }
10997           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10998           loc += 4;
10999           size = 8;
11000           if (PPC_HA (r2off) != 0)
11001             {
11002               bfd_put_32 (htab->params->stub_bfd,
11003                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
11004               loc += 4;
11005               size += 4;
11006             }
11007           if (PPC_LO (r2off) != 0)
11008             {
11009               bfd_put_32 (htab->params->stub_bfd,
11010                           ADDI_R2_R2 | PPC_LO (r2off), loc);
11011               loc += 4;
11012               size += 4;
11013             }
11014           off -= size - 4;
11015         }
11016       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
11017
11018       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11019         {
11020           info->callbacks->einfo
11021             (_("%P: long branch stub `%s' offset overflow\n"),
11022              stub_entry->root.string);
11023           htab->stub_error = TRUE;
11024           return FALSE;
11025         }
11026
11027       if (info->emitrelocations)
11028         {
11029           r = get_relocs (stub_entry->group->stub_sec, 1);
11030           if (r == NULL)
11031             return FALSE;
11032           r->r_offset = loc - stub_entry->group->stub_sec->contents;
11033           r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11034           r->r_addend = dest;
11035           if (stub_entry->h != NULL)
11036             {
11037               struct elf_link_hash_entry **hashes;
11038               unsigned long symndx;
11039               struct ppc_link_hash_entry *h;
11040
11041               hashes = elf_sym_hashes (htab->params->stub_bfd);
11042               if (hashes == NULL)
11043                 {
11044                   bfd_size_type hsize;
11045
11046                   hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11047                   hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11048                   if (hashes == NULL)
11049                     return FALSE;
11050                   elf_sym_hashes (htab->params->stub_bfd) = hashes;
11051                   htab->stub_globals = 1;
11052                 }
11053               symndx = htab->stub_globals++;
11054               h = stub_entry->h;
11055               hashes[symndx] = &h->elf;
11056               r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11057               if (h->oh != NULL && h->oh->is_func)
11058                 h = ppc_follow_link (h->oh);
11059               if (h->elf.root.u.def.section != stub_entry->target_section)
11060                 /* H is an opd symbol.  The addend must be zero.  */
11061                 r->r_addend = 0;
11062               else
11063                 {
11064                   off = (h->elf.root.u.def.value
11065                          + h->elf.root.u.def.section->output_offset
11066                          + h->elf.root.u.def.section->output_section->vma);
11067                   r->r_addend -= off;
11068                 }
11069             }
11070         }
11071       break;
11072
11073     case ppc_stub_plt_branch:
11074     case ppc_stub_plt_branch_r2off:
11075       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11076                                          stub_entry->root.string + 9,
11077                                          FALSE, FALSE);
11078       if (br_entry == NULL)
11079         {
11080           info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
11081                                   stub_entry->root.string);
11082           htab->stub_error = TRUE;
11083           return FALSE;
11084         }
11085
11086       dest = (stub_entry->target_value
11087               + stub_entry->target_section->output_offset
11088               + stub_entry->target_section->output_section->vma);
11089       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11090         dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11091
11092       bfd_put_64 (htab->brlt->owner, dest,
11093                   htab->brlt->contents + br_entry->offset);
11094
11095       if (br_entry->iter == htab->stub_iteration)
11096         {
11097           br_entry->iter = 0;
11098
11099           if (htab->relbrlt != NULL)
11100             {
11101               /* Create a reloc for the branch lookup table entry.  */
11102               Elf_Internal_Rela rela;
11103               bfd_byte *rl;
11104
11105               rela.r_offset = (br_entry->offset
11106                                + htab->brlt->output_offset
11107                                + htab->brlt->output_section->vma);
11108               rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11109               rela.r_addend = dest;
11110
11111               rl = htab->relbrlt->contents;
11112               rl += (htab->relbrlt->reloc_count++
11113                      * sizeof (Elf64_External_Rela));
11114               bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11115             }
11116           else if (info->emitrelocations)
11117             {
11118               r = get_relocs (htab->brlt, 1);
11119               if (r == NULL)
11120                 return FALSE;
11121               /* brlt, being SEC_LINKER_CREATED does not go through the
11122                  normal reloc processing.  Symbols and offsets are not
11123                  translated from input file to output file form, so
11124                  set up the offset per the output file.  */
11125               r->r_offset = (br_entry->offset
11126                              + htab->brlt->output_offset
11127                              + htab->brlt->output_section->vma);
11128               r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11129               r->r_addend = dest;
11130             }
11131         }
11132
11133       dest = (br_entry->offset
11134               + htab->brlt->output_offset
11135               + htab->brlt->output_section->vma);
11136
11137       off = (dest
11138              - elf_gp (htab->brlt->output_section->owner)
11139              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11140
11141       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11142         {
11143           info->callbacks->einfo
11144             (_("%P: linkage table error against `%T'\n"),
11145              stub_entry->root.string);
11146           bfd_set_error (bfd_error_bad_value);
11147           htab->stub_error = TRUE;
11148           return FALSE;
11149         }
11150
11151       if (info->emitrelocations)
11152         {
11153           r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11154           if (r == NULL)
11155             return FALSE;
11156           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11157           if (bfd_big_endian (info->output_bfd))
11158             r[0].r_offset += 2;
11159           if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11160             r[0].r_offset += 4;
11161           r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11162           r[0].r_addend = dest;
11163           if (PPC_HA (off) != 0)
11164             {
11165               r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11166               r[1].r_offset = r[0].r_offset + 4;
11167               r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11168               r[1].r_addend = r[0].r_addend;
11169             }
11170         }
11171
11172       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11173         {
11174           if (PPC_HA (off) != 0)
11175             {
11176               size = 16;
11177               bfd_put_32 (htab->params->stub_bfd,
11178                           ADDIS_R12_R2 | PPC_HA (off), loc);
11179               loc += 4;
11180               bfd_put_32 (htab->params->stub_bfd,
11181                           LD_R12_0R12 | PPC_LO (off), loc);
11182             }
11183           else
11184             {
11185               size = 12;
11186               bfd_put_32 (htab->params->stub_bfd,
11187                           LD_R12_0R2 | PPC_LO (off), loc);
11188             }
11189         }
11190       else
11191         {
11192           bfd_vma r2off = get_r2off (info, stub_entry);
11193
11194           if (r2off == (bfd_vma) -1)
11195             {
11196               htab->stub_error = TRUE;
11197               return FALSE;
11198             }
11199
11200           bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11201           loc += 4;
11202           size = 16;
11203           if (PPC_HA (off) != 0)
11204             {
11205               size += 4;
11206               bfd_put_32 (htab->params->stub_bfd,
11207                           ADDIS_R12_R2 | PPC_HA (off), loc);
11208               loc += 4;
11209               bfd_put_32 (htab->params->stub_bfd,
11210                           LD_R12_0R12 | PPC_LO (off), loc);
11211             }
11212           else
11213             bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11214
11215           if (PPC_HA (r2off) != 0)
11216             {
11217               size += 4;
11218               loc += 4;
11219               bfd_put_32 (htab->params->stub_bfd,
11220                           ADDIS_R2_R2 | PPC_HA (r2off), loc);
11221             }
11222           if (PPC_LO (r2off) != 0)
11223             {
11224               size += 4;
11225               loc += 4;
11226               bfd_put_32 (htab->params->stub_bfd,
11227                           ADDI_R2_R2 | PPC_LO (r2off), loc);
11228             }
11229         }
11230       loc += 4;
11231       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11232       loc += 4;
11233       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11234       break;
11235
11236     case ppc_stub_plt_call:
11237     case ppc_stub_plt_call_r2save:
11238       if (stub_entry->h != NULL
11239           && stub_entry->h->is_func_descriptor
11240           && stub_entry->h->oh != NULL)
11241         {
11242           struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11243
11244           /* If the old-ABI "dot-symbol" is undefined make it weak so
11245              we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
11246           if (fh->elf.root.type == bfd_link_hash_undefined
11247               && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11248                   || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11249             fh->elf.root.type = bfd_link_hash_undefweak;
11250         }
11251
11252       /* Now build the stub.  */
11253       dest = stub_entry->plt_ent->plt.offset & ~1;
11254       if (dest >= (bfd_vma) -2)
11255         abort ();
11256
11257       plt = htab->elf.splt;
11258       if (!htab->elf.dynamic_sections_created
11259           || stub_entry->h == NULL
11260           || stub_entry->h->elf.dynindx == -1)
11261         plt = htab->elf.iplt;
11262
11263       dest += plt->output_offset + plt->output_section->vma;
11264
11265       if (stub_entry->h == NULL
11266           && (stub_entry->plt_ent->plt.offset & 1) == 0)
11267         {
11268           Elf_Internal_Rela rela;
11269           bfd_byte *rl;
11270
11271           rela.r_offset = dest;
11272           if (htab->opd_abi)
11273             rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11274           else
11275             rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11276           rela.r_addend = (stub_entry->target_value
11277                            + stub_entry->target_section->output_offset
11278                            + stub_entry->target_section->output_section->vma);
11279
11280           rl = (htab->elf.irelplt->contents
11281                 + (htab->elf.irelplt->reloc_count++
11282                    * sizeof (Elf64_External_Rela)));
11283           bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11284           stub_entry->plt_ent->plt.offset |= 1;
11285           htab->local_ifunc_resolver = 1;
11286         }
11287
11288       off = (dest
11289              - elf_gp (plt->output_section->owner)
11290              - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11291
11292       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11293         {
11294           info->callbacks->einfo
11295             /* xgettext:c-format */
11296             (_("%P: linkage table error against `%T'\n"),
11297              stub_entry->h != NULL
11298              ? stub_entry->h->elf.root.root.string
11299              : "<local sym>");
11300           bfd_set_error (bfd_error_bad_value);
11301           htab->stub_error = TRUE;
11302           return FALSE;
11303         }
11304
11305       if (htab->params->plt_stub_align != 0)
11306         {
11307           unsigned pad = plt_stub_pad (htab, stub_entry, off);
11308
11309           stub_entry->group->stub_sec->size += pad;
11310           stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11311           loc += pad;
11312         }
11313
11314       r = NULL;
11315       if (info->emitrelocations)
11316         {
11317           r = get_relocs (stub_entry->group->stub_sec,
11318                           ((PPC_HA (off) != 0)
11319                            + (htab->opd_abi
11320                               ? 2 + (htab->params->plt_static_chain
11321                                      && PPC_HA (off + 16) == PPC_HA (off))
11322                               : 1)));
11323           if (r == NULL)
11324             return FALSE;
11325           r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11326           if (bfd_big_endian (info->output_bfd))
11327             r[0].r_offset += 2;
11328           r[0].r_addend = dest;
11329         }
11330       if (stub_entry->h != NULL
11331           && (stub_entry->h == htab->tls_get_addr_fd
11332               || stub_entry->h == htab->tls_get_addr)
11333           && htab->params->tls_get_addr_opt)
11334         p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11335       else
11336         p = build_plt_stub (htab, stub_entry, loc, off, r);
11337       size = p - loc;
11338       break;
11339
11340     case ppc_stub_save_res:
11341       return TRUE;
11342
11343     default:
11344       BFD_FAIL ();
11345       return FALSE;
11346     }
11347
11348   stub_entry->group->stub_sec->size += size;
11349
11350   if (htab->params->emit_stub_syms)
11351     {
11352       struct elf_link_hash_entry *h;
11353       size_t len1, len2;
11354       char *name;
11355       const char *const stub_str[] = { "long_branch",
11356                                        "long_branch_r2off",
11357                                        "plt_branch",
11358                                        "plt_branch_r2off",
11359                                        "plt_call",
11360                                        "plt_call" };
11361
11362       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11363       len2 = strlen (stub_entry->root.string);
11364       name = bfd_malloc (len1 + len2 + 2);
11365       if (name == NULL)
11366         return FALSE;
11367       memcpy (name, stub_entry->root.string, 9);
11368       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11369       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11370       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11371       if (h == NULL)
11372         return FALSE;
11373       if (h->root.type == bfd_link_hash_new)
11374         {
11375           h->root.type = bfd_link_hash_defined;
11376           h->root.u.def.section = stub_entry->group->stub_sec;
11377           h->root.u.def.value = stub_entry->stub_offset;
11378           h->ref_regular = 1;
11379           h->def_regular = 1;
11380           h->ref_regular_nonweak = 1;
11381           h->forced_local = 1;
11382           h->non_elf = 0;
11383           h->root.linker_def = 1;
11384         }
11385     }
11386
11387   return TRUE;
11388 }
11389
11390 /* As above, but don't actually build the stub.  Just bump offset so
11391    we know stub section sizes, and select plt_branch stubs where
11392    long_branch stubs won't do.  */
11393
11394 static bfd_boolean
11395 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11396 {
11397   struct ppc_stub_hash_entry *stub_entry;
11398   struct bfd_link_info *info;
11399   struct ppc_link_hash_table *htab;
11400   bfd_vma off;
11401   int size;
11402
11403   /* Massage our args to the form they really have.  */
11404   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11405   info = in_arg;
11406
11407   htab = ppc_hash_table (info);
11408   if (htab == NULL)
11409     return FALSE;
11410
11411   if (stub_entry->h != NULL
11412       && stub_entry->h->save_res
11413       && stub_entry->h->elf.root.type == bfd_link_hash_defined
11414       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11415     {
11416       /* Don't make stubs to out-of-line register save/restore
11417          functions.  Instead, emit copies of the functions.  */
11418       stub_entry->group->needs_save_res = 1;
11419       stub_entry->stub_type = ppc_stub_save_res;
11420       return TRUE;
11421     }
11422
11423   if (stub_entry->stub_type == ppc_stub_plt_call
11424       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11425     {
11426       asection *plt;
11427       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11428       if (off >= (bfd_vma) -2)
11429         abort ();
11430       plt = htab->elf.splt;
11431       if (!htab->elf.dynamic_sections_created
11432           || stub_entry->h == NULL
11433           || stub_entry->h->elf.dynindx == -1)
11434         plt = htab->elf.iplt;
11435       off += (plt->output_offset
11436               + plt->output_section->vma
11437               - elf_gp (plt->output_section->owner)
11438               - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11439
11440       size = plt_stub_size (htab, stub_entry, off);
11441       if (stub_entry->h != NULL
11442           && (stub_entry->h == htab->tls_get_addr_fd
11443               || stub_entry->h == htab->tls_get_addr)
11444           && htab->params->tls_get_addr_opt
11445           && (ALWAYS_EMIT_R2SAVE
11446               || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11447         stub_entry->group->tls_get_addr_opt_bctrl
11448           = stub_entry->group->stub_sec->size + size - 5 * 4;
11449
11450       if (htab->params->plt_stub_align)
11451         size += plt_stub_pad (htab, stub_entry, off);
11452       if (info->emitrelocations)
11453         {
11454           stub_entry->group->stub_sec->reloc_count
11455             += ((PPC_HA (off) != 0)
11456                 + (htab->opd_abi
11457                    ? 2 + (htab->params->plt_static_chain
11458                           && PPC_HA (off + 16) == PPC_HA (off))
11459                    : 1));
11460           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11461         }
11462     }
11463   else
11464     {
11465       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11466          variants.  */
11467       bfd_vma r2off = 0;
11468       bfd_vma local_off = 0;
11469
11470       off = (stub_entry->target_value
11471              + stub_entry->target_section->output_offset
11472              + stub_entry->target_section->output_section->vma);
11473       off -= (stub_entry->group->stub_sec->size
11474               + stub_entry->group->stub_sec->output_offset
11475               + stub_entry->group->stub_sec->output_section->vma);
11476
11477       /* Reset the stub type from the plt variant in case we now
11478          can reach with a shorter stub.  */
11479       if (stub_entry->stub_type >= ppc_stub_plt_branch)
11480         stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11481
11482       size = 4;
11483       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11484         {
11485           r2off = get_r2off (info, stub_entry);
11486           if (r2off == (bfd_vma) -1)
11487             {
11488               htab->stub_error = TRUE;
11489               return FALSE;
11490             }
11491           size = 8;
11492           if (PPC_HA (r2off) != 0)
11493             size += 4;
11494           if (PPC_LO (r2off) != 0)
11495             size += 4;
11496           off -= size - 4;
11497         }
11498
11499       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11500
11501       /* If the branch offset if too big, use a ppc_stub_plt_branch.
11502          Do the same for -R objects without function descriptors.  */
11503       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11504           || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11505               && r2off == 0
11506               && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11507         {
11508           struct ppc_branch_hash_entry *br_entry;
11509
11510           br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11511                                              stub_entry->root.string + 9,
11512                                              TRUE, FALSE);
11513           if (br_entry == NULL)
11514             {
11515               info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11516                                       stub_entry->root.string);
11517               htab->stub_error = TRUE;
11518               return FALSE;
11519             }
11520
11521           if (br_entry->iter != htab->stub_iteration)
11522             {
11523               br_entry->iter = htab->stub_iteration;
11524               br_entry->offset = htab->brlt->size;
11525               htab->brlt->size += 8;
11526
11527               if (htab->relbrlt != NULL)
11528                 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11529               else if (info->emitrelocations)
11530                 {
11531                   htab->brlt->reloc_count += 1;
11532                   htab->brlt->flags |= SEC_RELOC;
11533                 }
11534             }
11535
11536           stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11537           off = (br_entry->offset
11538                  + htab->brlt->output_offset
11539                  + htab->brlt->output_section->vma
11540                  - elf_gp (htab->brlt->output_section->owner)
11541                  - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11542
11543           if (info->emitrelocations)
11544             {
11545               stub_entry->group->stub_sec->reloc_count
11546                 += 1 + (PPC_HA (off) != 0);
11547               stub_entry->group->stub_sec->flags |= SEC_RELOC;
11548             }
11549
11550           if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11551             {
11552               size = 12;
11553               if (PPC_HA (off) != 0)
11554                 size = 16;
11555             }
11556           else
11557             {
11558               size = 16;
11559               if (PPC_HA (off) != 0)
11560                 size += 4;
11561
11562               if (PPC_HA (r2off) != 0)
11563                 size += 4;
11564               if (PPC_LO (r2off) != 0)
11565                 size += 4;
11566             }
11567         }
11568       else if (info->emitrelocations)
11569         {
11570           stub_entry->group->stub_sec->reloc_count += 1;
11571           stub_entry->group->stub_sec->flags |= SEC_RELOC;
11572         }
11573     }
11574
11575   stub_entry->group->stub_sec->size += size;
11576   return TRUE;
11577 }
11578
11579 /* Set up various things so that we can make a list of input sections
11580    for each output section included in the link.  Returns -1 on error,
11581    0 when no stubs will be needed, and 1 on success.  */
11582
11583 int
11584 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11585 {
11586   unsigned int id;
11587   bfd_size_type amt;
11588   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11589
11590   if (htab == NULL)
11591     return -1;
11592
11593   htab->sec_info_arr_size = bfd_get_next_section_id ();
11594   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11595   htab->sec_info = bfd_zmalloc (amt);
11596   if (htab->sec_info == NULL)
11597     return -1;
11598
11599   /* Set toc_off for com, und, abs and ind sections.  */
11600   for (id = 0; id < 3; id++)
11601     htab->sec_info[id].toc_off = TOC_BASE_OFF;
11602
11603   return 1;
11604 }
11605
11606 /* Set up for first pass at multitoc partitioning.  */
11607
11608 void
11609 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11610 {
11611   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11612
11613   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11614   htab->toc_bfd = NULL;
11615   htab->toc_first_sec = NULL;
11616 }
11617
11618 /* The linker repeatedly calls this function for each TOC input section
11619    and linker generated GOT section.  Group input bfds such that the toc
11620    within a group is less than 64k in size.  */
11621
11622 bfd_boolean
11623 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11624 {
11625   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11626   bfd_vma addr, off, limit;
11627
11628   if (htab == NULL)
11629     return FALSE;
11630
11631   if (!htab->second_toc_pass)
11632     {
11633       /* Keep track of the first .toc or .got section for this input bfd.  */
11634       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11635
11636       if (new_bfd)
11637         {
11638           htab->toc_bfd = isec->owner;
11639           htab->toc_first_sec = isec;
11640         }
11641
11642       addr = isec->output_offset + isec->output_section->vma;
11643       off = addr - htab->toc_curr;
11644       limit = 0x80008000;
11645       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11646         limit = 0x10000;
11647       if (off + isec->size > limit)
11648         {
11649           addr = (htab->toc_first_sec->output_offset
11650                   + htab->toc_first_sec->output_section->vma);
11651           htab->toc_curr = addr;
11652           htab->toc_curr &= -TOC_BASE_ALIGN;
11653         }
11654
11655       /* toc_curr is the base address of this toc group.  Set elf_gp
11656          for the input section to be the offset relative to the
11657          output toc base plus 0x8000.  Making the input elf_gp an
11658          offset allows us to move the toc as a whole without
11659          recalculating input elf_gp.  */
11660       off = htab->toc_curr - elf_gp (isec->output_section->owner);
11661       off += TOC_BASE_OFF;
11662
11663       /* Die if someone uses a linker script that doesn't keep input
11664          file .toc and .got together.  */
11665       if (new_bfd
11666           && elf_gp (isec->owner) != 0
11667           && elf_gp (isec->owner) != off)
11668         return FALSE;
11669
11670       elf_gp (isec->owner) = off;
11671       return TRUE;
11672     }
11673
11674   /* During the second pass toc_first_sec points to the start of
11675      a toc group, and toc_curr is used to track the old elf_gp.
11676      We use toc_bfd to ensure we only look at each bfd once.  */
11677   if (htab->toc_bfd == isec->owner)
11678     return TRUE;
11679   htab->toc_bfd = isec->owner;
11680
11681   if (htab->toc_first_sec == NULL
11682       || htab->toc_curr != elf_gp (isec->owner))
11683     {
11684       htab->toc_curr = elf_gp (isec->owner);
11685       htab->toc_first_sec = isec;
11686     }
11687   addr = (htab->toc_first_sec->output_offset
11688           + htab->toc_first_sec->output_section->vma);
11689   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11690   elf_gp (isec->owner) = off;
11691
11692   return TRUE;
11693 }
11694
11695 /* Called via elf_link_hash_traverse to merge GOT entries for global
11696    symbol H.  */
11697
11698 static bfd_boolean
11699 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11700 {
11701   if (h->root.type == bfd_link_hash_indirect)
11702     return TRUE;
11703
11704   merge_got_entries (&h->got.glist);
11705
11706   return TRUE;
11707 }
11708
11709 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11710    symbol H.  */
11711
11712 static bfd_boolean
11713 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11714 {
11715   struct got_entry *gent;
11716
11717   if (h->root.type == bfd_link_hash_indirect)
11718     return TRUE;
11719
11720   for (gent = h->got.glist; gent != NULL; gent = gent->next)
11721     if (!gent->is_indirect)
11722       allocate_got (h, (struct bfd_link_info *) inf, gent);
11723   return TRUE;
11724 }
11725
11726 /* Called on the first multitoc pass after the last call to
11727    ppc64_elf_next_toc_section.  This function removes duplicate GOT
11728    entries.  */
11729
11730 bfd_boolean
11731 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11732 {
11733   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11734   struct bfd *ibfd, *ibfd2;
11735   bfd_boolean done_something;
11736
11737   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11738
11739   if (!htab->do_multi_toc)
11740     return FALSE;
11741
11742   /* Merge global sym got entries within a toc group.  */
11743   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11744
11745   /* And tlsld_got.  */
11746   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11747     {
11748       struct got_entry *ent, *ent2;
11749
11750       if (!is_ppc64_elf (ibfd))
11751         continue;
11752
11753       ent = ppc64_tlsld_got (ibfd);
11754       if (!ent->is_indirect
11755           && ent->got.offset != (bfd_vma) -1)
11756         {
11757           for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11758             {
11759               if (!is_ppc64_elf (ibfd2))
11760                 continue;
11761
11762               ent2 = ppc64_tlsld_got (ibfd2);
11763               if (!ent2->is_indirect
11764                   && ent2->got.offset != (bfd_vma) -1
11765                   && elf_gp (ibfd2) == elf_gp (ibfd))
11766                 {
11767                   ent2->is_indirect = TRUE;
11768                   ent2->got.ent = ent;
11769                 }
11770             }
11771         }
11772     }
11773
11774   /* Zap sizes of got sections.  */
11775   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11776   htab->elf.irelplt->size -= htab->got_reli_size;
11777   htab->got_reli_size = 0;
11778
11779   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11780     {
11781       asection *got, *relgot;
11782
11783       if (!is_ppc64_elf (ibfd))
11784         continue;
11785
11786       got = ppc64_elf_tdata (ibfd)->got;
11787       if (got != NULL)
11788         {
11789           got->rawsize = got->size;
11790           got->size = 0;
11791           relgot = ppc64_elf_tdata (ibfd)->relgot;
11792           relgot->rawsize = relgot->size;
11793           relgot->size = 0;
11794         }
11795     }
11796
11797   /* Now reallocate the got, local syms first.  We don't need to
11798      allocate section contents again since we never increase size.  */
11799   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11800     {
11801       struct got_entry **lgot_ents;
11802       struct got_entry **end_lgot_ents;
11803       struct plt_entry **local_plt;
11804       struct plt_entry **end_local_plt;
11805       unsigned char *lgot_masks;
11806       bfd_size_type locsymcount;
11807       Elf_Internal_Shdr *symtab_hdr;
11808       asection *s;
11809
11810       if (!is_ppc64_elf (ibfd))
11811         continue;
11812
11813       lgot_ents = elf_local_got_ents (ibfd);
11814       if (!lgot_ents)
11815         continue;
11816
11817       symtab_hdr = &elf_symtab_hdr (ibfd);
11818       locsymcount = symtab_hdr->sh_info;
11819       end_lgot_ents = lgot_ents + locsymcount;
11820       local_plt = (struct plt_entry **) end_lgot_ents;
11821       end_local_plt = local_plt + locsymcount;
11822       lgot_masks = (unsigned char *) end_local_plt;
11823       s = ppc64_elf_tdata (ibfd)->got;
11824       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11825         {
11826           struct got_entry *ent;
11827
11828           for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11829             {
11830               unsigned int ent_size = 8;
11831               unsigned int rel_size = sizeof (Elf64_External_Rela);
11832
11833               ent->got.offset = s->size;
11834               if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11835                 {
11836                   ent_size *= 2;
11837                   rel_size *= 2;
11838                 }
11839               s->size += ent_size;
11840               if ((*lgot_masks & PLT_IFUNC) != 0)
11841                 {
11842                   htab->elf.irelplt->size += rel_size;
11843                   htab->got_reli_size += rel_size;
11844                 }
11845               else if (bfd_link_pic (info))
11846                 {
11847                   asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11848                   srel->size += rel_size;
11849                 }
11850             }
11851         }
11852     }
11853
11854   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11855
11856   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11857     {
11858       struct got_entry *ent;
11859
11860       if (!is_ppc64_elf (ibfd))
11861         continue;
11862
11863       ent = ppc64_tlsld_got (ibfd);
11864       if (!ent->is_indirect
11865           && ent->got.offset != (bfd_vma) -1)
11866         {
11867           asection *s = ppc64_elf_tdata (ibfd)->got;
11868           ent->got.offset = s->size;
11869           s->size += 16;
11870           if (bfd_link_pic (info))
11871             {
11872               asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11873               srel->size += sizeof (Elf64_External_Rela);
11874             }
11875         }
11876     }
11877
11878   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11879   if (!done_something)
11880     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11881       {
11882         asection *got;
11883
11884         if (!is_ppc64_elf (ibfd))
11885           continue;
11886
11887         got = ppc64_elf_tdata (ibfd)->got;
11888         if (got != NULL)
11889           {
11890             done_something = got->rawsize != got->size;
11891             if (done_something)
11892               break;
11893           }
11894       }
11895
11896   if (done_something)
11897     (*htab->params->layout_sections_again) ();
11898
11899   /* Set up for second pass over toc sections to recalculate elf_gp
11900      on input sections.  */
11901   htab->toc_bfd = NULL;
11902   htab->toc_first_sec = NULL;
11903   htab->second_toc_pass = TRUE;
11904   return done_something;
11905 }
11906
11907 /* Called after second pass of multitoc partitioning.  */
11908
11909 void
11910 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11911 {
11912   struct ppc_link_hash_table *htab = ppc_hash_table (info);
11913
11914   /* After the second pass, toc_curr tracks the TOC offset used
11915      for code sections below in ppc64_elf_next_input_section.  */
11916   htab->toc_curr = TOC_BASE_OFF;
11917 }
11918
11919 /* No toc references were found in ISEC.  If the code in ISEC makes no
11920    calls, then there's no need to use toc adjusting stubs when branching
11921    into ISEC.  Actually, indirect calls from ISEC are OK as they will
11922    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
11923    needed, and 2 if a cyclical call-graph was found but no other reason
11924    for a stub was detected.  If called from the top level, a return of
11925    2 means the same as a return of 0.  */
11926
11927 static int
11928 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11929 {
11930   int ret;
11931
11932   /* Mark this section as checked.  */
11933   isec->call_check_done = 1;
11934
11935   /* We know none of our code bearing sections will need toc stubs.  */
11936   if ((isec->flags & SEC_LINKER_CREATED) != 0)
11937     return 0;
11938
11939   if (isec->size == 0)
11940     return 0;
11941
11942   if (isec->output_section == NULL)
11943     return 0;
11944
11945   ret = 0;
11946   if (isec->reloc_count != 0)
11947     {
11948       Elf_Internal_Rela *relstart, *rel;
11949       Elf_Internal_Sym *local_syms;
11950       struct ppc_link_hash_table *htab;
11951
11952       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11953                                             info->keep_memory);
11954       if (relstart == NULL)
11955         return -1;
11956
11957       /* Look for branches to outside of this section.  */
11958       local_syms = NULL;
11959       htab = ppc_hash_table (info);
11960       if (htab == NULL)
11961         return -1;
11962
11963       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11964         {
11965           enum elf_ppc64_reloc_type r_type;
11966           unsigned long r_symndx;
11967           struct elf_link_hash_entry *h;
11968           struct ppc_link_hash_entry *eh;
11969           Elf_Internal_Sym *sym;
11970           asection *sym_sec;
11971           struct _opd_sec_data *opd;
11972           bfd_vma sym_value;
11973           bfd_vma dest;
11974
11975           r_type = ELF64_R_TYPE (rel->r_info);
11976           if (r_type != R_PPC64_REL24
11977               && r_type != R_PPC64_REL14
11978               && r_type != R_PPC64_REL14_BRTAKEN
11979               && r_type != R_PPC64_REL14_BRNTAKEN)
11980             continue;
11981
11982           r_symndx = ELF64_R_SYM (rel->r_info);
11983           if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11984                           isec->owner))
11985             {
11986               ret = -1;
11987               break;
11988             }
11989
11990           /* Calls to dynamic lib functions go through a plt call stub
11991              that uses r2.  */
11992           eh = (struct ppc_link_hash_entry *) h;
11993           if (eh != NULL
11994               && (eh->elf.plt.plist != NULL
11995                   || (eh->oh != NULL
11996                       && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11997             {
11998               ret = 1;
11999               break;
12000             }
12001
12002           if (sym_sec == NULL)
12003             /* Ignore other undefined symbols.  */
12004             continue;
12005
12006           /* Assume branches to other sections not included in the
12007              link need stubs too, to cover -R and absolute syms.  */
12008           if (sym_sec->output_section == NULL)
12009             {
12010               ret = 1;
12011               break;
12012             }
12013
12014           if (h == NULL)
12015             sym_value = sym->st_value;
12016           else
12017             {
12018               if (h->root.type != bfd_link_hash_defined
12019                   && h->root.type != bfd_link_hash_defweak)
12020                 abort ();
12021               sym_value = h->root.u.def.value;
12022             }
12023           sym_value += rel->r_addend;
12024
12025           /* If this branch reloc uses an opd sym, find the code section.  */
12026           opd = get_opd_info (sym_sec);
12027           if (opd != NULL)
12028             {
12029               if (h == NULL && opd->adjust != NULL)
12030                 {
12031                   long adjust;
12032
12033                   adjust = opd->adjust[OPD_NDX (sym_value)];
12034                   if (adjust == -1)
12035                     /* Assume deleted functions won't ever be called.  */
12036                     continue;
12037                   sym_value += adjust;
12038                 }
12039
12040               dest = opd_entry_value (sym_sec, sym_value,
12041                                       &sym_sec, NULL, FALSE);
12042               if (dest == (bfd_vma) -1)
12043                 continue;
12044             }
12045           else
12046             dest = (sym_value
12047                     + sym_sec->output_offset
12048                     + sym_sec->output_section->vma);
12049
12050           /* Ignore branch to self.  */
12051           if (sym_sec == isec)
12052             continue;
12053
12054           /* If the called function uses the toc, we need a stub.  */
12055           if (sym_sec->has_toc_reloc
12056               || sym_sec->makes_toc_func_call)
12057             {
12058               ret = 1;
12059               break;
12060             }
12061
12062           /* Assume any branch that needs a long branch stub might in fact
12063              need a plt_branch stub.  A plt_branch stub uses r2.  */
12064           else if (dest - (isec->output_offset
12065                            + isec->output_section->vma
12066                            + rel->r_offset) + (1 << 25)
12067                    >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12068                                                              ? h->other
12069                                                              : sym->st_other))
12070             {
12071               ret = 1;
12072               break;
12073             }
12074
12075           /* If calling back to a section in the process of being
12076              tested, we can't say for sure that no toc adjusting stubs
12077              are needed, so don't return zero.  */
12078           else if (sym_sec->call_check_in_progress)
12079             ret = 2;
12080
12081           /* Branches to another section that itself doesn't have any TOC
12082              references are OK.  Recursively call ourselves to check.  */
12083           else if (!sym_sec->call_check_done)
12084             {
12085               int recur;
12086
12087               /* Mark current section as indeterminate, so that other
12088                  sections that call back to current won't be marked as
12089                  known.  */
12090               isec->call_check_in_progress = 1;
12091               recur = toc_adjusting_stub_needed (info, sym_sec);
12092               isec->call_check_in_progress = 0;
12093
12094               if (recur != 0)
12095                 {
12096                   ret = recur;
12097                   if (recur != 2)
12098                     break;
12099                 }
12100             }
12101         }
12102
12103       if (local_syms != NULL
12104           && (elf_symtab_hdr (isec->owner).contents
12105               != (unsigned char *) local_syms))
12106         free (local_syms);
12107       if (elf_section_data (isec)->relocs != relstart)
12108         free (relstart);
12109     }
12110
12111   if ((ret & 1) == 0
12112       && isec->map_head.s != NULL
12113       && (strcmp (isec->output_section->name, ".init") == 0
12114           || strcmp (isec->output_section->name, ".fini") == 0))
12115     {
12116       if (isec->map_head.s->has_toc_reloc
12117           || isec->map_head.s->makes_toc_func_call)
12118         ret = 1;
12119       else if (!isec->map_head.s->call_check_done)
12120         {
12121           int recur;
12122           isec->call_check_in_progress = 1;
12123           recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12124           isec->call_check_in_progress = 0;
12125           if (recur != 0)
12126             ret = recur;
12127         }
12128     }
12129
12130   if (ret == 1)
12131     isec->makes_toc_func_call = 1;
12132
12133   return ret;
12134 }
12135
12136 /* The linker repeatedly calls this function for each input section,
12137    in the order that input sections are linked into output sections.
12138    Build lists of input sections to determine groupings between which
12139    we may insert linker stubs.  */
12140
12141 bfd_boolean
12142 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12143 {
12144   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12145
12146   if (htab == NULL)
12147     return FALSE;
12148
12149   if ((isec->output_section->flags & SEC_CODE) != 0
12150       && isec->output_section->id < htab->sec_info_arr_size)
12151     {
12152       /* This happens to make the list in reverse order,
12153          which is what we want.  */
12154       htab->sec_info[isec->id].u.list
12155         = htab->sec_info[isec->output_section->id].u.list;
12156       htab->sec_info[isec->output_section->id].u.list = isec;
12157     }
12158
12159   if (htab->multi_toc_needed)
12160     {
12161       /* Analyse sections that aren't already flagged as needing a
12162          valid toc pointer.  Exclude .fixup for the linux kernel.
12163          .fixup contains branches, but only back to the function that
12164          hit an exception.  */
12165       if (!(isec->has_toc_reloc
12166             || (isec->flags & SEC_CODE) == 0
12167             || strcmp (isec->name, ".fixup") == 0
12168             || isec->call_check_done))
12169         {
12170           if (toc_adjusting_stub_needed (info, isec) < 0)
12171             return FALSE;
12172         }
12173       /* Make all sections use the TOC assigned for this object file.
12174          This will be wrong for pasted sections;  We fix that in
12175          check_pasted_section().  */
12176       if (elf_gp (isec->owner) != 0)
12177         htab->toc_curr = elf_gp (isec->owner);
12178     }
12179
12180   htab->sec_info[isec->id].toc_off = htab->toc_curr;
12181   return TRUE;
12182 }
12183
12184 /* Check that all .init and .fini sections use the same toc, if they
12185    have toc relocs.  */
12186
12187 static bfd_boolean
12188 check_pasted_section (struct bfd_link_info *info, const char *name)
12189 {
12190   asection *o = bfd_get_section_by_name (info->output_bfd, name);
12191
12192   if (o != NULL)
12193     {
12194       struct ppc_link_hash_table *htab = ppc_hash_table (info);
12195       bfd_vma toc_off = 0;
12196       asection *i;
12197
12198       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12199         if (i->has_toc_reloc)
12200           {
12201             if (toc_off == 0)
12202               toc_off = htab->sec_info[i->id].toc_off;
12203             else if (toc_off != htab->sec_info[i->id].toc_off)
12204               return FALSE;
12205           }
12206
12207       if (toc_off == 0)
12208         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12209           if (i->makes_toc_func_call)
12210             {
12211               toc_off = htab->sec_info[i->id].toc_off;
12212               break;
12213             }
12214
12215       /* Make sure the whole pasted function uses the same toc offset.  */
12216       if (toc_off != 0)
12217         for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12218           htab->sec_info[i->id].toc_off = toc_off;
12219     }
12220   return TRUE;
12221 }
12222
12223 bfd_boolean
12224 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12225 {
12226   return (check_pasted_section (info, ".init")
12227           & check_pasted_section (info, ".fini"));
12228 }
12229
12230 /* See whether we can group stub sections together.  Grouping stub
12231    sections may result in fewer stubs.  More importantly, we need to
12232    put all .init* and .fini* stubs at the beginning of the .init or
12233    .fini output sections respectively, because glibc splits the
12234    _init and _fini functions into multiple parts.  Putting a stub in
12235    the middle of a function is not a good idea.  */
12236
12237 static bfd_boolean
12238 group_sections (struct bfd_link_info *info,
12239                 bfd_size_type stub_group_size,
12240                 bfd_boolean stubs_always_before_branch)
12241 {
12242   struct ppc_link_hash_table *htab;
12243   asection *osec;
12244   bfd_boolean suppress_size_errors;
12245
12246   htab = ppc_hash_table (info);
12247   if (htab == NULL)
12248     return FALSE;
12249
12250   suppress_size_errors = FALSE;
12251   if (stub_group_size == 1)
12252     {
12253       /* Default values.  */
12254       if (stubs_always_before_branch)
12255         stub_group_size = 0x1e00000;
12256       else
12257         stub_group_size = 0x1c00000;
12258       suppress_size_errors = TRUE;
12259     }
12260
12261   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12262     {
12263       asection *tail;
12264
12265       if (osec->id >= htab->sec_info_arr_size)
12266         continue;
12267
12268       tail = htab->sec_info[osec->id].u.list;
12269       while (tail != NULL)
12270         {
12271           asection *curr;
12272           asection *prev;
12273           bfd_size_type total;
12274           bfd_boolean big_sec;
12275           bfd_vma curr_toc;
12276           struct map_stub *group;
12277           bfd_size_type group_size;
12278
12279           curr = tail;
12280           total = tail->size;
12281           group_size = (ppc64_elf_section_data (tail) != NULL
12282                         && ppc64_elf_section_data (tail)->has_14bit_branch
12283                         ? stub_group_size >> 10 : stub_group_size);
12284
12285           big_sec = total > group_size;
12286           if (big_sec && !suppress_size_errors)
12287             /* xgettext:c-format */
12288             _bfd_error_handler (_("%B section %A exceeds stub group size"),
12289                                 tail->owner, tail);
12290           curr_toc = htab->sec_info[tail->id].toc_off;
12291
12292           while ((prev = htab->sec_info[curr->id].u.list) != NULL
12293                  && ((total += curr->output_offset - prev->output_offset)
12294                      < (ppc64_elf_section_data (prev) != NULL
12295                         && ppc64_elf_section_data (prev)->has_14bit_branch
12296                         ? (group_size = stub_group_size >> 10) : group_size))
12297                  && htab->sec_info[prev->id].toc_off == curr_toc)
12298             curr = prev;
12299
12300           /* OK, the size from the start of CURR to the end is less
12301              than group_size and thus can be handled by one stub
12302              section.  (or the tail section is itself larger than
12303              group_size, in which case we may be toast.)  We should
12304              really be keeping track of the total size of stubs added
12305              here, as stubs contribute to the final output section
12306              size.  That's a little tricky, and this way will only
12307              break if stubs added make the total size more than 2^25,
12308              ie. for the default stub_group_size, if stubs total more
12309              than 2097152 bytes, or nearly 75000 plt call stubs.  */
12310           group = bfd_alloc (curr->owner, sizeof (*group));
12311           if (group == NULL)
12312             return FALSE;
12313           group->link_sec = curr;
12314           group->stub_sec = NULL;
12315           group->needs_save_res = 0;
12316           group->tls_get_addr_opt_bctrl = -1u;
12317           group->next = htab->group;
12318           htab->group = group;
12319           do
12320             {
12321               prev = htab->sec_info[tail->id].u.list;
12322               /* Set up this stub group.  */
12323               htab->sec_info[tail->id].u.group = group;
12324             }
12325           while (tail != curr && (tail = prev) != NULL);
12326
12327           /* But wait, there's more!  Input sections up to group_size
12328              bytes before the stub section can be handled by it too.
12329              Don't do this if we have a really large section after the
12330              stubs, as adding more stubs increases the chance that
12331              branches may not reach into the stub section.  */
12332           if (!stubs_always_before_branch && !big_sec)
12333             {
12334               total = 0;
12335               while (prev != NULL
12336                      && ((total += tail->output_offset - prev->output_offset)
12337                          < (ppc64_elf_section_data (prev) != NULL
12338                             && ppc64_elf_section_data (prev)->has_14bit_branch
12339                             ? (group_size = stub_group_size >> 10) : group_size))
12340                      && htab->sec_info[prev->id].toc_off == curr_toc)
12341                 {
12342                   tail = prev;
12343                   prev = htab->sec_info[tail->id].u.list;
12344                   htab->sec_info[tail->id].u.group = group;
12345                 }
12346             }
12347           tail = prev;
12348         }
12349     }
12350   return TRUE;
12351 }
12352
12353 static const unsigned char glink_eh_frame_cie[] =
12354 {
12355   0, 0, 0, 16,                          /* length.  */
12356   0, 0, 0, 0,                           /* id.  */
12357   1,                                    /* CIE version.  */
12358   'z', 'R', 0,                          /* Augmentation string.  */
12359   4,                                    /* Code alignment.  */
12360   0x78,                                 /* Data alignment.  */
12361   65,                                   /* RA reg.  */
12362   1,                                    /* Augmentation size.  */
12363   DW_EH_PE_pcrel | DW_EH_PE_sdata4,     /* FDE encoding.  */
12364   DW_CFA_def_cfa, 1, 0                  /* def_cfa: r1 offset 0.  */
12365 };
12366
12367 static size_t
12368 stub_eh_frame_size (struct map_stub *group, size_t align)
12369 {
12370   size_t this_size = 17;
12371   if (group->tls_get_addr_opt_bctrl != -1u)
12372     {
12373       unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12374       if (to_bctrl < 64)
12375         this_size += 1;
12376       else if (to_bctrl < 256)
12377         this_size += 2;
12378       else if (to_bctrl < 65536)
12379         this_size += 3;
12380       else
12381         this_size += 5;
12382       this_size += 6;
12383     }
12384   this_size = (this_size + align - 1) & -align;
12385   return this_size;
12386 }
12387
12388 /* Stripping output sections is normally done before dynamic section
12389    symbols have been allocated.  This function is called later, and
12390    handles cases like htab->brlt which is mapped to its own output
12391    section.  */
12392
12393 static void
12394 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12395 {
12396   if (isec->size == 0
12397       && isec->output_section->size == 0
12398       && !(isec->output_section->flags & SEC_KEEP)
12399       && !bfd_section_removed_from_list (info->output_bfd,
12400                                          isec->output_section)
12401       && elf_section_data (isec->output_section)->dynindx == 0)
12402     {
12403       isec->output_section->flags |= SEC_EXCLUDE;
12404       bfd_section_list_remove (info->output_bfd, isec->output_section);
12405       info->output_bfd->section_count--;
12406     }
12407 }
12408
12409 /* Determine and set the size of the stub section for a final link.
12410
12411    The basic idea here is to examine all the relocations looking for
12412    PC-relative calls to a target that is unreachable with a "bl"
12413    instruction.  */
12414
12415 bfd_boolean
12416 ppc64_elf_size_stubs (struct bfd_link_info *info)
12417 {
12418   bfd_size_type stub_group_size;
12419   bfd_boolean stubs_always_before_branch;
12420   struct ppc_link_hash_table *htab = ppc_hash_table (info);
12421
12422   if (htab == NULL)
12423     return FALSE;
12424
12425   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12426     htab->params->plt_thread_safe = 1;
12427   if (!htab->opd_abi)
12428     htab->params->plt_thread_safe = 0;
12429   else if (htab->params->plt_thread_safe == -1)
12430     {
12431       static const char *const thread_starter[] =
12432         {
12433           "pthread_create",
12434           /* libstdc++ */
12435           "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12436           /* librt */
12437           "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12438           "mq_notify", "create_timer",
12439           /* libanl */
12440           "getaddrinfo_a",
12441           /* libgomp */
12442           "GOMP_parallel",
12443           "GOMP_parallel_start",
12444           "GOMP_parallel_loop_static",
12445           "GOMP_parallel_loop_static_start",
12446           "GOMP_parallel_loop_dynamic",
12447           "GOMP_parallel_loop_dynamic_start",
12448           "GOMP_parallel_loop_guided",
12449           "GOMP_parallel_loop_guided_start",
12450           "GOMP_parallel_loop_runtime",
12451           "GOMP_parallel_loop_runtime_start",
12452           "GOMP_parallel_sections",
12453           "GOMP_parallel_sections_start",
12454           /* libgo */
12455           "__go_go",
12456         };
12457       unsigned i;
12458
12459       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12460         {
12461           struct elf_link_hash_entry *h;
12462           h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12463                                     FALSE, FALSE, TRUE);
12464           htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12465           if (htab->params->plt_thread_safe)
12466             break;
12467         }
12468     }
12469   stubs_always_before_branch = htab->params->group_size < 0;
12470   if (htab->params->group_size < 0)
12471     stub_group_size = -htab->params->group_size;
12472   else
12473     stub_group_size = htab->params->group_size;
12474
12475   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12476     return FALSE;
12477
12478 #define STUB_SHRINK_ITER 20
12479   /* Loop until no stubs added.  After iteration 20 of this loop we may
12480      exit on a stub section shrinking.  This is to break out of a
12481      pathological case where adding stubs on one iteration decreases
12482      section gaps (perhaps due to alignment), which then requires
12483      fewer or smaller stubs on the next iteration.  */
12484
12485   while (1)
12486     {
12487       bfd *input_bfd;
12488       unsigned int bfd_indx;
12489       struct map_stub *group;
12490
12491       htab->stub_iteration += 1;
12492
12493       for (input_bfd = info->input_bfds, bfd_indx = 0;
12494            input_bfd != NULL;
12495            input_bfd = input_bfd->link.next, bfd_indx++)
12496         {
12497           Elf_Internal_Shdr *symtab_hdr;
12498           asection *section;
12499           Elf_Internal_Sym *local_syms = NULL;
12500
12501           if (!is_ppc64_elf (input_bfd))
12502             continue;
12503
12504           /* We'll need the symbol table in a second.  */
12505           symtab_hdr = &elf_symtab_hdr (input_bfd);
12506           if (symtab_hdr->sh_info == 0)
12507             continue;
12508
12509           /* Walk over each section attached to the input bfd.  */
12510           for (section = input_bfd->sections;
12511                section != NULL;
12512                section = section->next)
12513             {
12514               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12515
12516               /* If there aren't any relocs, then there's nothing more
12517                  to do.  */
12518               if ((section->flags & SEC_RELOC) == 0
12519                   || (section->flags & SEC_ALLOC) == 0
12520                   || (section->flags & SEC_LOAD) == 0
12521                   || (section->flags & SEC_CODE) == 0
12522                   || section->reloc_count == 0)
12523                 continue;
12524
12525               /* If this section is a link-once section that will be
12526                  discarded, then don't create any stubs.  */
12527               if (section->output_section == NULL
12528                   || section->output_section->owner != info->output_bfd)
12529                 continue;
12530
12531               /* Get the relocs.  */
12532               internal_relocs
12533                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12534                                              info->keep_memory);
12535               if (internal_relocs == NULL)
12536                 goto error_ret_free_local;
12537
12538               /* Now examine each relocation.  */
12539               irela = internal_relocs;
12540               irelaend = irela + section->reloc_count;
12541               for (; irela < irelaend; irela++)
12542                 {
12543                   enum elf_ppc64_reloc_type r_type;
12544                   unsigned int r_indx;
12545                   enum ppc_stub_type stub_type;
12546                   struct ppc_stub_hash_entry *stub_entry;
12547                   asection *sym_sec, *code_sec;
12548                   bfd_vma sym_value, code_value;
12549                   bfd_vma destination;
12550                   unsigned long local_off;
12551                   bfd_boolean ok_dest;
12552                   struct ppc_link_hash_entry *hash;
12553                   struct ppc_link_hash_entry *fdh;
12554                   struct elf_link_hash_entry *h;
12555                   Elf_Internal_Sym *sym;
12556                   char *stub_name;
12557                   const asection *id_sec;
12558                   struct _opd_sec_data *opd;
12559                   struct plt_entry *plt_ent;
12560
12561                   r_type = ELF64_R_TYPE (irela->r_info);
12562                   r_indx = ELF64_R_SYM (irela->r_info);
12563
12564                   if (r_type >= R_PPC64_max)
12565                     {
12566                       bfd_set_error (bfd_error_bad_value);
12567                       goto error_ret_free_internal;
12568                     }
12569
12570                   /* Only look for stubs on branch instructions.  */
12571                   if (r_type != R_PPC64_REL24
12572                       && r_type != R_PPC64_REL14
12573                       && r_type != R_PPC64_REL14_BRTAKEN
12574                       && r_type != R_PPC64_REL14_BRNTAKEN)
12575                     continue;
12576
12577                   /* Now determine the call target, its name, value,
12578                      section.  */
12579                   if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12580                                   r_indx, input_bfd))
12581                     goto error_ret_free_internal;
12582                   hash = (struct ppc_link_hash_entry *) h;
12583
12584                   ok_dest = FALSE;
12585                   fdh = NULL;
12586                   sym_value = 0;
12587                   if (hash == NULL)
12588                     {
12589                       sym_value = sym->st_value;
12590                       if (sym_sec != NULL
12591                           && sym_sec->output_section != NULL)
12592                         ok_dest = TRUE;
12593                     }
12594                   else if (hash->elf.root.type == bfd_link_hash_defined
12595                            || hash->elf.root.type == bfd_link_hash_defweak)
12596                     {
12597                       sym_value = hash->elf.root.u.def.value;
12598                       if (sym_sec->output_section != NULL)
12599                         ok_dest = TRUE;
12600                     }
12601                   else if (hash->elf.root.type == bfd_link_hash_undefweak
12602                            || hash->elf.root.type == bfd_link_hash_undefined)
12603                     {
12604                       /* Recognise an old ABI func code entry sym, and
12605                          use the func descriptor sym instead if it is
12606                          defined.  */
12607                       if (hash->elf.root.root.string[0] == '.'
12608                           && hash->oh != NULL)
12609                         {
12610                           fdh = ppc_follow_link (hash->oh);
12611                           if (fdh->elf.root.type == bfd_link_hash_defined
12612                               || fdh->elf.root.type == bfd_link_hash_defweak)
12613                             {
12614                               sym_sec = fdh->elf.root.u.def.section;
12615                               sym_value = fdh->elf.root.u.def.value;
12616                               if (sym_sec->output_section != NULL)
12617                                 ok_dest = TRUE;
12618                             }
12619                           else
12620                             fdh = NULL;
12621                         }
12622                     }
12623                   else
12624                     {
12625                       bfd_set_error (bfd_error_bad_value);
12626                       goto error_ret_free_internal;
12627                     }
12628
12629                   destination = 0;
12630                   local_off = 0;
12631                   if (ok_dest)
12632                     {
12633                       sym_value += irela->r_addend;
12634                       destination = (sym_value
12635                                      + sym_sec->output_offset
12636                                      + sym_sec->output_section->vma);
12637                       local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12638                                                             ? hash->elf.other
12639                                                             : sym->st_other);
12640                     }
12641
12642                   code_sec = sym_sec;
12643                   code_value = sym_value;
12644                   opd = get_opd_info (sym_sec);
12645                   if (opd != NULL)
12646                     {
12647                       bfd_vma dest;
12648
12649                       if (hash == NULL && opd->adjust != NULL)
12650                         {
12651                           long adjust = opd->adjust[OPD_NDX (sym_value)];
12652                           if (adjust == -1)
12653                             continue;
12654                           code_value += adjust;
12655                           sym_value += adjust;
12656                         }
12657                       dest = opd_entry_value (sym_sec, sym_value,
12658                                               &code_sec, &code_value, FALSE);
12659                       if (dest != (bfd_vma) -1)
12660                         {
12661                           destination = dest;
12662                           if (fdh != NULL)
12663                             {
12664                               /* Fixup old ABI sym to point at code
12665                                  entry.  */
12666                               hash->elf.root.type = bfd_link_hash_defweak;
12667                               hash->elf.root.u.def.section = code_sec;
12668                               hash->elf.root.u.def.value = code_value;
12669                             }
12670                         }
12671                     }
12672
12673                   /* Determine what (if any) linker stub is needed.  */
12674                   plt_ent = NULL;
12675                   stub_type = ppc_type_of_stub (section, irela, &hash,
12676                                                 &plt_ent, destination,
12677                                                 local_off);
12678
12679                   if (stub_type != ppc_stub_plt_call)
12680                     {
12681                       /* Check whether we need a TOC adjusting stub.
12682                          Since the linker pastes together pieces from
12683                          different object files when creating the
12684                          _init and _fini functions, it may be that a
12685                          call to what looks like a local sym is in
12686                          fact a call needing a TOC adjustment.  */
12687                       if (code_sec != NULL
12688                           && code_sec->output_section != NULL
12689                           && (htab->sec_info[code_sec->id].toc_off
12690                               != htab->sec_info[section->id].toc_off)
12691                           && (code_sec->has_toc_reloc
12692                               || code_sec->makes_toc_func_call))
12693                         stub_type = ppc_stub_long_branch_r2off;
12694                     }
12695
12696                   if (stub_type == ppc_stub_none)
12697                     continue;
12698
12699                   /* __tls_get_addr calls might be eliminated.  */
12700                   if (stub_type != ppc_stub_plt_call
12701                       && hash != NULL
12702                       && (hash == htab->tls_get_addr
12703                           || hash == htab->tls_get_addr_fd)
12704                       && section->has_tls_reloc
12705                       && irela != internal_relocs)
12706                     {
12707                       /* Get tls info.  */
12708                       unsigned char *tls_mask;
12709
12710                       if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12711                                          irela - 1, input_bfd))
12712                         goto error_ret_free_internal;
12713                       if (*tls_mask != 0)
12714                         continue;
12715                     }
12716
12717                   if (stub_type == ppc_stub_plt_call)
12718                     {
12719                       if (!htab->opd_abi
12720                           && htab->params->plt_localentry0 != 0
12721                           && is_elfv2_localentry0 (&hash->elf))
12722                         htab->has_plt_localentry0 = 1;
12723                       else if (irela + 1 < irelaend
12724                                && irela[1].r_offset == irela->r_offset + 4
12725                                && (ELF64_R_TYPE (irela[1].r_info)
12726                                    == R_PPC64_TOCSAVE))
12727                         {
12728                           if (!tocsave_find (htab, INSERT,
12729                                              &local_syms, irela + 1, input_bfd))
12730                             goto error_ret_free_internal;
12731                         }
12732                       else
12733                         stub_type = ppc_stub_plt_call_r2save;
12734                     }
12735
12736                   /* Support for grouping stub sections.  */
12737                   id_sec = htab->sec_info[section->id].u.group->link_sec;
12738
12739                   /* Get the name of this stub.  */
12740                   stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12741                   if (!stub_name)
12742                     goto error_ret_free_internal;
12743
12744                   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12745                                                      stub_name, FALSE, FALSE);
12746                   if (stub_entry != NULL)
12747                     {
12748                       /* The proper stub has already been created.  */
12749                       free (stub_name);
12750                       if (stub_type == ppc_stub_plt_call_r2save)
12751                         stub_entry->stub_type = stub_type;
12752                       continue;
12753                     }
12754
12755                   stub_entry = ppc_add_stub (stub_name, section, info);
12756                   if (stub_entry == NULL)
12757                     {
12758                       free (stub_name);
12759                     error_ret_free_internal:
12760                       if (elf_section_data (section)->relocs == NULL)
12761                         free (internal_relocs);
12762                     error_ret_free_local:
12763                       if (local_syms != NULL
12764                           && (symtab_hdr->contents
12765                               != (unsigned char *) local_syms))
12766                         free (local_syms);
12767                       return FALSE;
12768                     }
12769
12770                   stub_entry->stub_type = stub_type;
12771                   if (stub_type != ppc_stub_plt_call
12772                       && stub_type != ppc_stub_plt_call_r2save)
12773                     {
12774                       stub_entry->target_value = code_value;
12775                       stub_entry->target_section = code_sec;
12776                     }
12777                   else
12778                     {
12779                       stub_entry->target_value = sym_value;
12780                       stub_entry->target_section = sym_sec;
12781                     }
12782                   stub_entry->h = hash;
12783                   stub_entry->plt_ent = plt_ent;
12784                   stub_entry->other = hash ? hash->elf.other : sym->st_other;
12785
12786                   if (stub_entry->h != NULL)
12787                     htab->stub_globals += 1;
12788                 }
12789
12790               /* We're done with the internal relocs, free them.  */
12791               if (elf_section_data (section)->relocs != internal_relocs)
12792                 free (internal_relocs);
12793             }
12794
12795           if (local_syms != NULL
12796               && symtab_hdr->contents != (unsigned char *) local_syms)
12797             {
12798               if (!info->keep_memory)
12799                 free (local_syms);
12800               else
12801                 symtab_hdr->contents = (unsigned char *) local_syms;
12802             }
12803         }
12804
12805       /* We may have added some stubs.  Find out the new size of the
12806          stub sections.  */
12807       for (group = htab->group; group != NULL; group = group->next)
12808         if (group->stub_sec != NULL)
12809           {
12810             asection *stub_sec = group->stub_sec;
12811
12812             if (htab->stub_iteration <= STUB_SHRINK_ITER
12813                 || stub_sec->rawsize < stub_sec->size)
12814               /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
12815               stub_sec->rawsize = stub_sec->size;
12816             stub_sec->size = 0;
12817             stub_sec->reloc_count = 0;
12818             stub_sec->flags &= ~SEC_RELOC;
12819           }
12820
12821       htab->brlt->size = 0;
12822       htab->brlt->reloc_count = 0;
12823       htab->brlt->flags &= ~SEC_RELOC;
12824       if (htab->relbrlt != NULL)
12825         htab->relbrlt->size = 0;
12826
12827       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12828
12829       for (group = htab->group; group != NULL; group = group->next)
12830         if (group->needs_save_res)
12831           group->stub_sec->size += htab->sfpr->size;
12832
12833       if (info->emitrelocations
12834           && htab->glink != NULL && htab->glink->size != 0)
12835         {
12836           htab->glink->reloc_count = 1;
12837           htab->glink->flags |= SEC_RELOC;
12838         }
12839
12840       if (htab->glink_eh_frame != NULL
12841           && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12842           && htab->glink_eh_frame->output_section->size != 0)
12843         {
12844           size_t size = 0, align = 4;
12845
12846           for (group = htab->group; group != NULL; group = group->next)
12847             if (group->stub_sec != NULL)
12848               size += stub_eh_frame_size (group, align);
12849           if (htab->glink != NULL && htab->glink->size != 0)
12850             size += (24 + align - 1) & -align;
12851           if (size != 0)
12852             size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12853           align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12854           size = (size + align - 1) & -align;
12855           htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12856           htab->glink_eh_frame->size = size;
12857         }
12858
12859       if (htab->params->plt_stub_align != 0)
12860         for (group = htab->group; group != NULL; group = group->next)
12861           if (group->stub_sec != NULL)
12862             group->stub_sec->size = ((group->stub_sec->size
12863                                       + (1 << htab->params->plt_stub_align) - 1)
12864                                      & -(1 << htab->params->plt_stub_align));
12865
12866       for (group = htab->group; group != NULL; group = group->next)
12867         if (group->stub_sec != NULL
12868             && group->stub_sec->rawsize != group->stub_sec->size
12869             && (htab->stub_iteration <= STUB_SHRINK_ITER
12870                 || group->stub_sec->rawsize < group->stub_sec->size))
12871           break;
12872
12873       if (group == NULL
12874           && (htab->glink_eh_frame == NULL
12875               || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12876         break;
12877
12878       /* Ask the linker to do its stuff.  */
12879       (*htab->params->layout_sections_again) ();
12880     }
12881
12882   if (htab->glink_eh_frame != NULL
12883       && htab->glink_eh_frame->size != 0)
12884     {
12885       bfd_vma val;
12886       bfd_byte *p, *last_fde;
12887       size_t last_fde_len, size, align, pad;
12888       struct map_stub *group;
12889
12890       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12891       if (p == NULL)
12892         return FALSE;
12893       htab->glink_eh_frame->contents = p;
12894       last_fde = p;
12895       align = 4;
12896
12897       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12898       /* CIE length (rewrite in case little-endian).  */
12899       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12900       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12901       p += last_fde_len + 4;
12902
12903       for (group = htab->group; group != NULL; group = group->next)
12904         if (group->stub_sec != NULL)
12905           {
12906             last_fde = p;
12907             last_fde_len = stub_eh_frame_size (group, align) - 4;
12908             /* FDE length.  */
12909             bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12910             p += 4;
12911             /* CIE pointer.  */
12912             val = p - htab->glink_eh_frame->contents;
12913             bfd_put_32 (htab->elf.dynobj, val, p);
12914             p += 4;
12915             /* Offset to stub section, written later.  */
12916             p += 4;
12917             /* stub section size.  */
12918             bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12919             p += 4;
12920             /* Augmentation.  */
12921             p += 1;
12922             if (group->tls_get_addr_opt_bctrl != -1u)
12923               {
12924                 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12925
12926                 /* This FDE needs more than just the default.
12927                    Describe __tls_get_addr_opt stub LR.  */
12928                 if (to_bctrl < 64)
12929                   *p++ = DW_CFA_advance_loc + to_bctrl;
12930                 else if (to_bctrl < 256)
12931                   {
12932                     *p++ = DW_CFA_advance_loc1;
12933                     *p++ = to_bctrl;
12934                   }
12935                 else if (to_bctrl < 65536)
12936                   {
12937                     *p++ = DW_CFA_advance_loc2;
12938                     bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12939                     p += 2;
12940                   }
12941                 else
12942                   {
12943                     *p++ = DW_CFA_advance_loc4;
12944                     bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12945                     p += 4;
12946                   }
12947                 *p++ = DW_CFA_offset_extended_sf;
12948                 *p++ = 65;
12949                 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12950                 *p++ = DW_CFA_advance_loc + 4;
12951                 *p++ = DW_CFA_restore_extended;
12952                 *p++ = 65;
12953               }
12954             /* Pad.  */
12955             p = last_fde + last_fde_len + 4;
12956           }
12957       if (htab->glink != NULL && htab->glink->size != 0)
12958         {
12959           last_fde = p;
12960           last_fde_len = ((24 + align - 1) & -align) - 4;
12961           /* FDE length.  */
12962           bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12963           p += 4;
12964           /* CIE pointer.  */
12965           val = p - htab->glink_eh_frame->contents;
12966           bfd_put_32 (htab->elf.dynobj, val, p);
12967           p += 4;
12968           /* Offset to .glink, written later.  */
12969           p += 4;
12970           /* .glink size.  */
12971           bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12972           p += 4;
12973           /* Augmentation.  */
12974           p += 1;
12975
12976           *p++ = DW_CFA_advance_loc + 1;
12977           *p++ = DW_CFA_register;
12978           *p++ = 65;
12979           *p++ = htab->opd_abi ? 12 : 0;
12980           *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12981           *p++ = DW_CFA_restore_extended;
12982           *p++ = 65;
12983           p += ((24 + align - 1) & -align) - 24;
12984         }
12985       /* Subsume any padding into the last FDE if user .eh_frame
12986          sections are aligned more than glink_eh_frame.  Otherwise any
12987          zero padding will be seen as a terminator.  */
12988       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12989       size = p - htab->glink_eh_frame->contents;
12990       pad = ((size + align - 1) & -align) - size;
12991       htab->glink_eh_frame->size = size + pad;
12992       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12993     }
12994
12995   maybe_strip_output (info, htab->brlt);
12996   if (htab->glink_eh_frame != NULL)
12997     maybe_strip_output (info, htab->glink_eh_frame);
12998
12999   return TRUE;
13000 }
13001
13002 /* Called after we have determined section placement.  If sections
13003    move, we'll be called again.  Provide a value for TOCstart.  */
13004
13005 bfd_vma
13006 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13007 {
13008   asection *s;
13009   bfd_vma TOCstart, adjust;
13010
13011   if (info != NULL)
13012     {
13013       struct elf_link_hash_entry *h;
13014       struct elf_link_hash_table *htab = elf_hash_table (info);
13015
13016       if (is_elf_hash_table (htab)
13017           && htab->hgot != NULL)
13018         h = htab->hgot;
13019       else
13020         {
13021           h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13022           if (is_elf_hash_table (htab))
13023             htab->hgot = h;
13024         }
13025       if (h != NULL
13026           && h->root.type == bfd_link_hash_defined
13027           && !h->root.linker_def
13028           && (!is_elf_hash_table (htab)
13029               || h->def_regular))
13030         {
13031           TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13032                       + h->root.u.def.section->output_offset
13033                       + h->root.u.def.section->output_section->vma);
13034           _bfd_set_gp_value (obfd, TOCstart);
13035           return TOCstart;
13036         }
13037     }
13038
13039   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13040      order.  The TOC starts where the first of these sections starts.  */
13041   s = bfd_get_section_by_name (obfd, ".got");
13042   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13043     s = bfd_get_section_by_name (obfd, ".toc");
13044   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13045     s = bfd_get_section_by_name (obfd, ".tocbss");
13046   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13047     s = bfd_get_section_by_name (obfd, ".plt");
13048   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13049     {
13050       /* This may happen for
13051          o  references to TOC base (SYM@toc / TOC[tc0]) without a
13052          .toc directive
13053          o  bad linker script
13054          o --gc-sections and empty TOC sections
13055
13056          FIXME: Warn user?  */
13057
13058       /* Look for a likely section.  We probably won't even be
13059          using TOCstart.  */
13060       for (s = obfd->sections; s != NULL; s = s->next)
13061         if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13062                          | SEC_EXCLUDE))
13063             == (SEC_ALLOC | SEC_SMALL_DATA))
13064           break;
13065       if (s == NULL)
13066         for (s = obfd->sections; s != NULL; s = s->next)
13067           if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13068               == (SEC_ALLOC | SEC_SMALL_DATA))
13069             break;
13070       if (s == NULL)
13071         for (s = obfd->sections; s != NULL; s = s->next)
13072           if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13073               == SEC_ALLOC)
13074             break;
13075       if (s == NULL)
13076         for (s = obfd->sections; s != NULL; s = s->next)
13077           if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13078             break;
13079     }
13080
13081   TOCstart = 0;
13082   if (s != NULL)
13083     TOCstart = s->output_section->vma + s->output_offset;
13084
13085   /* Force alignment.  */
13086   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13087   TOCstart -= adjust;
13088   _bfd_set_gp_value (obfd, TOCstart);
13089
13090   if (info != NULL && s != NULL)
13091     {
13092       struct ppc_link_hash_table *htab = ppc_hash_table (info);
13093
13094       if (htab != NULL)
13095         {
13096           if (htab->elf.hgot != NULL)
13097             {
13098               htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13099               htab->elf.hgot->root.u.def.section = s;
13100             }
13101         }
13102       else
13103         {
13104           struct bfd_link_hash_entry *bh = NULL;
13105           _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13106                                             s, TOC_BASE_OFF - adjust,
13107                                             NULL, FALSE, FALSE, &bh);
13108         }
13109     }
13110   return TOCstart;
13111 }
13112
13113 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13114    write out any global entry stubs.  */
13115
13116 static bfd_boolean
13117 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
13118 {
13119   struct bfd_link_info *info;
13120   struct ppc_link_hash_table *htab;
13121   struct plt_entry *pent;
13122   asection *s;
13123
13124   if (h->root.type == bfd_link_hash_indirect)
13125     return TRUE;
13126
13127   if (!h->pointer_equality_needed)
13128     return TRUE;
13129
13130   if (h->def_regular)
13131     return TRUE;
13132
13133   info = inf;
13134   htab = ppc_hash_table (info);
13135   if (htab == NULL)
13136     return FALSE;
13137
13138   s = htab->glink;
13139   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13140     if (pent->plt.offset != (bfd_vma) -1
13141         && pent->addend == 0)
13142       {
13143         bfd_byte *p;
13144         asection *plt;
13145         bfd_vma off;
13146
13147         p = s->contents + h->root.u.def.value;
13148         plt = htab->elf.splt;
13149         if (!htab->elf.dynamic_sections_created
13150             || h->dynindx == -1)
13151           plt = htab->elf.iplt;
13152         off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13153         off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13154
13155         if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13156           {
13157             info->callbacks->einfo
13158               (_("%P: linkage table error against `%T'\n"),
13159                h->root.root.string);
13160             bfd_set_error (bfd_error_bad_value);
13161             htab->stub_error = TRUE;
13162           }
13163
13164         htab->stub_count[ppc_stub_global_entry - 1] += 1;
13165         if (htab->params->emit_stub_syms)
13166           {
13167             size_t len = strlen (h->root.root.string);
13168             char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13169
13170             if (name == NULL)
13171               return FALSE;
13172
13173             sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13174             h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13175             if (h == NULL)
13176               return FALSE;
13177             if (h->root.type == bfd_link_hash_new)
13178               {
13179                 h->root.type = bfd_link_hash_defined;
13180                 h->root.u.def.section = s;
13181                 h->root.u.def.value = p - s->contents;
13182                 h->ref_regular = 1;
13183                 h->def_regular = 1;
13184                 h->ref_regular_nonweak = 1;
13185                 h->forced_local = 1;
13186                 h->non_elf = 0;
13187                 h->root.linker_def = 1;
13188               }
13189           }
13190
13191         if (PPC_HA (off) != 0)
13192           {
13193             bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13194             p += 4;
13195           }
13196         bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13197         p += 4;
13198         bfd_put_32 (s->owner, MTCTR_R12, p);
13199         p += 4;
13200         bfd_put_32 (s->owner, BCTR, p);
13201         break;
13202       }
13203   return TRUE;
13204 }
13205
13206 /* Build all the stubs associated with the current output file.
13207    The stubs are kept in a hash table attached to the main linker
13208    hash table.  This function is called via gldelf64ppc_finish.  */
13209
13210 bfd_boolean
13211 ppc64_elf_build_stubs (struct bfd_link_info *info,
13212                        char **stats)
13213 {
13214   struct ppc_link_hash_table *htab = ppc_hash_table (info);
13215   struct map_stub *group;
13216   asection *stub_sec;
13217   bfd_byte *p;
13218   int stub_sec_count = 0;
13219
13220   if (htab == NULL)
13221     return FALSE;
13222
13223   /* Allocate memory to hold the linker stubs.  */
13224   for (group = htab->group; group != NULL; group = group->next)
13225     if ((stub_sec = group->stub_sec) != NULL
13226         && stub_sec->size != 0)
13227       {
13228         stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13229         if (stub_sec->contents == NULL)
13230           return FALSE;
13231         stub_sec->size = 0;
13232       }
13233
13234   if (htab->glink != NULL && htab->glink->size != 0)
13235     {
13236       unsigned int indx;
13237       bfd_vma plt0;
13238
13239       /* Build the .glink plt call stub.  */
13240       if (htab->params->emit_stub_syms)
13241         {
13242           struct elf_link_hash_entry *h;
13243           h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13244                                     TRUE, FALSE, FALSE);
13245           if (h == NULL)
13246             return FALSE;
13247           if (h->root.type == bfd_link_hash_new)
13248             {
13249               h->root.type = bfd_link_hash_defined;
13250               h->root.u.def.section = htab->glink;
13251               h->root.u.def.value = 8;
13252               h->ref_regular = 1;
13253               h->def_regular = 1;
13254               h->ref_regular_nonweak = 1;
13255               h->forced_local = 1;
13256               h->non_elf = 0;
13257               h->root.linker_def = 1;
13258             }
13259         }
13260       plt0 = (htab->elf.splt->output_section->vma
13261               + htab->elf.splt->output_offset
13262               - 16);
13263       if (info->emitrelocations)
13264         {
13265           Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13266           if (r == NULL)
13267             return FALSE;
13268           r->r_offset = (htab->glink->output_offset
13269                          + htab->glink->output_section->vma);
13270           r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13271           r->r_addend = plt0;
13272         }
13273       p = htab->glink->contents;
13274       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13275       bfd_put_64 (htab->glink->owner, plt0, p);
13276       p += 8;
13277       if (htab->opd_abi)
13278         {
13279           bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13280           p += 4;
13281           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13282           p += 4;
13283           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13284           p += 4;
13285           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13286           p += 4;
13287           bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13288           p += 4;
13289           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13290           p += 4;
13291           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13292           p += 4;
13293           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13294           p += 4;
13295           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13296           p += 4;
13297           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13298           p += 4;
13299         }
13300       else
13301         {
13302           bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13303           p += 4;
13304           bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13305           p += 4;
13306           bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13307           p += 4;
13308           bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13309           p += 4;
13310           bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13311           p += 4;
13312           bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13313           p += 4;
13314           bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13315           p += 4;
13316           bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13317           p += 4;
13318           bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13319           p += 4;
13320           bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13321           p += 4;
13322           bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13323           p += 4;
13324           bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13325           p += 4;
13326           bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13327           p += 4;
13328         }
13329       bfd_put_32 (htab->glink->owner, BCTR, p);
13330       p += 4;
13331       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13332         {
13333           bfd_put_32 (htab->glink->owner, NOP, p);
13334           p += 4;
13335         }
13336
13337       /* Build the .glink lazy link call stubs.  */
13338       indx = 0;
13339       while (p < htab->glink->contents + htab->glink->rawsize)
13340         {
13341           if (htab->opd_abi)
13342             {
13343               if (indx < 0x8000)
13344                 {
13345                   bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13346                   p += 4;
13347                 }
13348               else
13349                 {
13350                   bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13351                   p += 4;
13352                   bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13353                               p);
13354                   p += 4;
13355                 }
13356             }
13357           bfd_put_32 (htab->glink->owner,
13358                       B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13359           indx++;
13360           p += 4;
13361         }
13362
13363       /* Build .glink global entry stubs.  */
13364       if (htab->glink->size > htab->glink->rawsize)
13365         elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13366     }
13367
13368   if (htab->brlt != NULL && htab->brlt->size != 0)
13369     {
13370       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13371                                          htab->brlt->size);
13372       if (htab->brlt->contents == NULL)
13373         return FALSE;
13374     }
13375   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13376     {
13377       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13378                                             htab->relbrlt->size);
13379       if (htab->relbrlt->contents == NULL)
13380         return FALSE;
13381     }
13382
13383   /* Build the stubs as directed by the stub hash table.  */
13384   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13385
13386   for (group = htab->group; group != NULL; group = group->next)
13387     if (group->needs_save_res)
13388       {
13389         stub_sec = group->stub_sec;
13390         memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13391                 htab->sfpr->size);
13392         if (htab->params->emit_stub_syms)
13393           {
13394             unsigned int i;
13395
13396             for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13397               if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13398                 return FALSE;
13399           }
13400         stub_sec->size += htab->sfpr->size;
13401       }
13402
13403   if (htab->relbrlt != NULL)
13404     htab->relbrlt->reloc_count = 0;
13405
13406   if (htab->params->plt_stub_align != 0)
13407     for (group = htab->group; group != NULL; group = group->next)
13408       if ((stub_sec = group->stub_sec) != NULL)
13409         stub_sec->size = ((stub_sec->size
13410                            + (1 << htab->params->plt_stub_align) - 1)
13411                           & -(1 << htab->params->plt_stub_align));
13412
13413   for (group = htab->group; group != NULL; group = group->next)
13414     if ((stub_sec = group->stub_sec) != NULL)
13415       {
13416         stub_sec_count += 1;
13417         if (stub_sec->rawsize != stub_sec->size
13418             && (htab->stub_iteration <= STUB_SHRINK_ITER
13419                 || stub_sec->rawsize < stub_sec->size))
13420           break;
13421       }
13422
13423   if (group != NULL)
13424     {
13425       htab->stub_error = TRUE;
13426       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13427     }
13428
13429   if (htab->stub_error)
13430     return FALSE;
13431
13432   if (stats != NULL)
13433     {
13434       *stats = bfd_malloc (500);
13435       if (*stats == NULL)
13436         return FALSE;
13437
13438       sprintf (*stats, _("linker stubs in %u group%s\n"
13439                          "  branch       %lu\n"
13440                          "  toc adjust   %lu\n"
13441                          "  long branch  %lu\n"
13442                          "  long toc adj %lu\n"
13443                          "  plt call     %lu\n"
13444                          "  plt call toc %lu\n"
13445                          "  global entry %lu"),
13446                stub_sec_count,
13447                stub_sec_count == 1 ? "" : "s",
13448                htab->stub_count[ppc_stub_long_branch - 1],
13449                htab->stub_count[ppc_stub_long_branch_r2off - 1],
13450                htab->stub_count[ppc_stub_plt_branch - 1],
13451                htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13452                htab->stub_count[ppc_stub_plt_call - 1],
13453                htab->stub_count[ppc_stub_plt_call_r2save - 1],
13454                htab->stub_count[ppc_stub_global_entry - 1]);
13455     }
13456   return TRUE;
13457 }
13458
13459 /* What to do when ld finds relocations against symbols defined in
13460    discarded sections.  */
13461
13462 static unsigned int
13463 ppc64_elf_action_discarded (asection *sec)
13464 {
13465   if (strcmp (".opd", sec->name) == 0)
13466     return 0;
13467
13468   if (strcmp (".toc", sec->name) == 0)
13469     return 0;
13470
13471   if (strcmp (".toc1", sec->name) == 0)
13472     return 0;
13473
13474   return _bfd_elf_default_action_discarded (sec);
13475 }
13476
13477 /* The RELOCATE_SECTION function is called by the ELF backend linker
13478    to handle the relocations for a section.
13479
13480    The relocs are always passed as Rela structures; if the section
13481    actually uses Rel structures, the r_addend field will always be
13482    zero.
13483
13484    This function is responsible for adjust the section contents as
13485    necessary, and (if using Rela relocs and generating a
13486    relocatable output file) adjusting the reloc addend as
13487    necessary.
13488
13489    This function does not have to worry about setting the reloc
13490    address or the reloc symbol index.
13491
13492    LOCAL_SYMS is a pointer to the swapped in local symbols.
13493
13494    LOCAL_SECTIONS is an array giving the section in the input file
13495    corresponding to the st_shndx field of each local symbol.
13496
13497    The global hash table entry for the global symbols can be found
13498    via elf_sym_hashes (input_bfd).
13499
13500    When generating relocatable output, this function must handle
13501    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
13502    going to be the section symbol corresponding to the output
13503    section, which means that the addend must be adjusted
13504    accordingly.  */
13505
13506 static bfd_boolean
13507 ppc64_elf_relocate_section (bfd *output_bfd,
13508                             struct bfd_link_info *info,
13509                             bfd *input_bfd,
13510                             asection *input_section,
13511                             bfd_byte *contents,
13512                             Elf_Internal_Rela *relocs,
13513                             Elf_Internal_Sym *local_syms,
13514                             asection **local_sections)
13515 {
13516   struct ppc_link_hash_table *htab;
13517   Elf_Internal_Shdr *symtab_hdr;
13518   struct elf_link_hash_entry **sym_hashes;
13519   Elf_Internal_Rela *rel;
13520   Elf_Internal_Rela *wrel;
13521   Elf_Internal_Rela *relend;
13522   Elf_Internal_Rela outrel;
13523   bfd_byte *loc;
13524   struct got_entry **local_got_ents;
13525   bfd_vma TOCstart;
13526   bfd_boolean ret = TRUE;
13527   bfd_boolean is_opd;
13528   /* Assume 'at' branch hints.  */
13529   bfd_boolean is_isa_v2 = TRUE;
13530   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13531
13532   /* Initialize howto table if needed.  */
13533   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13534     ppc_howto_init ();
13535
13536   htab = ppc_hash_table (info);
13537   if (htab == NULL)
13538     return FALSE;
13539
13540   /* Don't relocate stub sections.  */
13541   if (input_section->owner == htab->params->stub_bfd)
13542     return TRUE;
13543
13544   BFD_ASSERT (is_ppc64_elf (input_bfd));
13545
13546   local_got_ents = elf_local_got_ents (input_bfd);
13547   TOCstart = elf_gp (output_bfd);
13548   symtab_hdr = &elf_symtab_hdr (input_bfd);
13549   sym_hashes = elf_sym_hashes (input_bfd);
13550   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13551
13552   rel = wrel = relocs;
13553   relend = relocs + input_section->reloc_count;
13554   for (; rel < relend; wrel++, rel++)
13555     {
13556       enum elf_ppc64_reloc_type r_type;
13557       bfd_vma addend;
13558       bfd_reloc_status_type r;
13559       Elf_Internal_Sym *sym;
13560       asection *sec;
13561       struct elf_link_hash_entry *h_elf;
13562       struct ppc_link_hash_entry *h;
13563       struct ppc_link_hash_entry *fdh;
13564       const char *sym_name;
13565       unsigned long r_symndx, toc_symndx;
13566       bfd_vma toc_addend;
13567       unsigned char tls_mask, tls_gd, tls_type;
13568       unsigned char sym_type;
13569       bfd_vma relocation;
13570       bfd_boolean unresolved_reloc;
13571       bfd_boolean warned;
13572       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13573       unsigned int insn;
13574       unsigned int mask;
13575       struct ppc_stub_hash_entry *stub_entry;
13576       bfd_vma max_br_offset;
13577       bfd_vma from;
13578       Elf_Internal_Rela orig_rel;
13579       reloc_howto_type *howto;
13580       struct reloc_howto_struct alt_howto;
13581
13582     again:
13583       orig_rel = *rel;
13584
13585       r_type = ELF64_R_TYPE (rel->r_info);
13586       r_symndx = ELF64_R_SYM (rel->r_info);
13587
13588       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13589          symbol of the previous ADDR64 reloc.  The symbol gives us the
13590          proper TOC base to use.  */
13591       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13592           && wrel != relocs
13593           && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13594           && is_opd)
13595         r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13596
13597       sym = NULL;
13598       sec = NULL;
13599       h_elf = NULL;
13600       sym_name = NULL;
13601       unresolved_reloc = FALSE;
13602       warned = FALSE;
13603
13604       if (r_symndx < symtab_hdr->sh_info)
13605         {
13606           /* It's a local symbol.  */
13607           struct _opd_sec_data *opd;
13608
13609           sym = local_syms + r_symndx;
13610           sec = local_sections[r_symndx];
13611           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13612           sym_type = ELF64_ST_TYPE (sym->st_info);
13613           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13614           opd = get_opd_info (sec);
13615           if (opd != NULL && opd->adjust != NULL)
13616             {
13617               long adjust = opd->adjust[OPD_NDX (sym->st_value
13618                                                  + rel->r_addend)];
13619               if (adjust == -1)
13620                 relocation = 0;
13621               else
13622                 {
13623                   /* If this is a relocation against the opd section sym
13624                      and we have edited .opd, adjust the reloc addend so
13625                      that ld -r and ld --emit-relocs output is correct.
13626                      If it is a reloc against some other .opd symbol,
13627                      then the symbol value will be adjusted later.  */
13628                   if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13629                     rel->r_addend += adjust;
13630                   else
13631                     relocation += adjust;
13632                 }
13633             }
13634         }
13635       else
13636         {
13637           bfd_boolean ignored;
13638
13639           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13640                                    r_symndx, symtab_hdr, sym_hashes,
13641                                    h_elf, sec, relocation,
13642                                    unresolved_reloc, warned, ignored);
13643           sym_name = h_elf->root.root.string;
13644           sym_type = h_elf->type;
13645           if (sec != NULL
13646               && sec->owner == output_bfd
13647               && strcmp (sec->name, ".opd") == 0)
13648             {
13649               /* This is a symbol defined in a linker script.  All
13650                  such are defined in output sections, even those
13651                  defined by simple assignment from a symbol defined in
13652                  an input section.  Transfer the symbol to an
13653                  appropriate input .opd section, so that a branch to
13654                  this symbol will be mapped to the location specified
13655                  by the opd entry.  */
13656               struct bfd_link_order *lo;
13657               for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13658                 if (lo->type == bfd_indirect_link_order)
13659                   {
13660                     asection *isec = lo->u.indirect.section;
13661                     if (h_elf->root.u.def.value >= isec->output_offset
13662                         && h_elf->root.u.def.value < (isec->output_offset
13663                                                       + isec->size))
13664                       {
13665                         h_elf->root.u.def.value -= isec->output_offset;
13666                         h_elf->root.u.def.section = isec;
13667                         sec = isec;
13668                         break;
13669                       }
13670                   }
13671             }
13672         }
13673       h = (struct ppc_link_hash_entry *) h_elf;
13674
13675       if (sec != NULL && discarded_section (sec))
13676         {
13677           _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13678                                input_bfd, input_section,
13679                                contents + rel->r_offset);
13680           wrel->r_offset = rel->r_offset;
13681           wrel->r_info = 0;
13682           wrel->r_addend = 0;
13683
13684           /* For ld -r, remove relocations in debug sections against
13685              symbols defined in discarded sections.  Not done for
13686              non-debug to preserve relocs in .eh_frame which the
13687              eh_frame editing code expects to be present.  */
13688           if (bfd_link_relocatable (info)
13689               && (input_section->flags & SEC_DEBUGGING))
13690             wrel--;
13691
13692           continue;
13693         }
13694
13695       if (bfd_link_relocatable (info))
13696         goto copy_reloc;
13697
13698       if (h != NULL && &h->elf == htab->elf.hgot)
13699         {
13700           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13701           sec = bfd_abs_section_ptr;
13702           unresolved_reloc = FALSE;
13703         }
13704
13705       /* TLS optimizations.  Replace instruction sequences and relocs
13706          based on information we collected in tls_optimize.  We edit
13707          RELOCS so that --emit-relocs will output something sensible
13708          for the final instruction stream.  */
13709       tls_mask = 0;
13710       tls_gd = 0;
13711       toc_symndx = 0;
13712       if (h != NULL)
13713         tls_mask = h->tls_mask;
13714       else if (local_got_ents != NULL)
13715         {
13716           struct plt_entry **local_plt = (struct plt_entry **)
13717             (local_got_ents + symtab_hdr->sh_info);
13718           unsigned char *lgot_masks = (unsigned char *)
13719             (local_plt + symtab_hdr->sh_info);
13720           tls_mask = lgot_masks[r_symndx];
13721         }
13722       if (tls_mask == 0
13723           && (r_type == R_PPC64_TLS
13724               || r_type == R_PPC64_TLSGD
13725               || r_type == R_PPC64_TLSLD))
13726         {
13727           /* Check for toc tls entries.  */
13728           unsigned char *toc_tls;
13729
13730           if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13731                              &local_syms, rel, input_bfd))
13732             return FALSE;
13733
13734           if (toc_tls)
13735             tls_mask = *toc_tls;
13736         }
13737
13738       /* Check that tls relocs are used with tls syms, and non-tls
13739          relocs are used with non-tls syms.  */
13740       if (r_symndx != STN_UNDEF
13741           && r_type != R_PPC64_NONE
13742           && (h == NULL
13743               || h->elf.root.type == bfd_link_hash_defined
13744               || h->elf.root.type == bfd_link_hash_defweak)
13745           && (IS_PPC64_TLS_RELOC (r_type)
13746               != (sym_type == STT_TLS
13747                   || (sym_type == STT_SECTION
13748                       && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13749         {
13750           if (tls_mask != 0
13751               && (r_type == R_PPC64_TLS
13752                   || r_type == R_PPC64_TLSGD
13753                   || r_type == R_PPC64_TLSLD))
13754             /* R_PPC64_TLS is OK against a symbol in the TOC.  */
13755             ;
13756           else
13757             info->callbacks->einfo
13758               (!IS_PPC64_TLS_RELOC (r_type)
13759                /* xgettext:c-format */
13760                ? _("%H: %s used with TLS symbol `%T'\n")
13761                /* xgettext:c-format */
13762                : _("%H: %s used with non-TLS symbol `%T'\n"),
13763                input_bfd, input_section, rel->r_offset,
13764                ppc64_elf_howto_table[r_type]->name,
13765                sym_name);
13766         }
13767
13768       /* Ensure reloc mapping code below stays sane.  */
13769       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13770           || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13771           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
13772           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13773           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13774           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13775           || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
13776           || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13777           || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13778           || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13779         abort ();
13780
13781       switch (r_type)
13782         {
13783         default:
13784           break;
13785
13786         case R_PPC64_LO_DS_OPT:
13787           insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13788           if ((insn & (0x3f << 26)) != 58u << 26)
13789             abort ();
13790           insn += (14u << 26) - (58u << 26);
13791           bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13792           r_type = R_PPC64_TOC16_LO;
13793           rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13794           break;
13795
13796         case R_PPC64_TOC16:
13797         case R_PPC64_TOC16_LO:
13798         case R_PPC64_TOC16_DS:
13799         case R_PPC64_TOC16_LO_DS:
13800           {
13801             /* Check for toc tls entries.  */
13802             unsigned char *toc_tls;
13803             int retval;
13804
13805             retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13806                                    &local_syms, rel, input_bfd);
13807             if (retval == 0)
13808               return FALSE;
13809
13810             if (toc_tls)
13811               {
13812                 tls_mask = *toc_tls;
13813                 if (r_type == R_PPC64_TOC16_DS
13814                     || r_type == R_PPC64_TOC16_LO_DS)
13815                   {
13816                     if (tls_mask != 0
13817                         && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13818                       goto toctprel;
13819                   }
13820                 else
13821                   {
13822                     /* If we found a GD reloc pair, then we might be
13823                        doing a GD->IE transition.  */
13824                     if (retval == 2)
13825                       {
13826                         tls_gd = TLS_TPRELGD;
13827                         if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13828                           goto tls_ldgd_opt;
13829                       }
13830                     else if (retval == 3)
13831                       {
13832                         if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13833                           goto tls_ldgd_opt;
13834                       }
13835                   }
13836               }
13837           }
13838           break;
13839
13840         case R_PPC64_GOT_TPREL16_HI:
13841         case R_PPC64_GOT_TPREL16_HA:
13842           if (tls_mask != 0
13843               && (tls_mask & TLS_TPREL) == 0)
13844             {
13845               rel->r_offset -= d_offset;
13846               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13847               r_type = R_PPC64_NONE;
13848               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13849             }
13850           break;
13851
13852         case R_PPC64_GOT_TPREL16_DS:
13853         case R_PPC64_GOT_TPREL16_LO_DS:
13854           if (tls_mask != 0
13855               && (tls_mask & TLS_TPREL) == 0)
13856             {
13857             toctprel:
13858               insn = bfd_get_32 (input_bfd,
13859                                  contents + rel->r_offset - d_offset);
13860               insn &= 31 << 21;
13861               insn |= 0x3c0d0000;       /* addis 0,13,0 */
13862               bfd_put_32 (input_bfd, insn,
13863                           contents + rel->r_offset - d_offset);
13864               r_type = R_PPC64_TPREL16_HA;
13865               if (toc_symndx != 0)
13866                 {
13867                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13868                   rel->r_addend = toc_addend;
13869                   /* We changed the symbol.  Start over in order to
13870                      get h, sym, sec etc. right.  */
13871                   goto again;
13872                 }
13873               else
13874                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13875             }
13876           break;
13877
13878         case R_PPC64_TLS:
13879           if (tls_mask != 0
13880               && (tls_mask & TLS_TPREL) == 0)
13881             {
13882               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13883               insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13884               if (insn == 0)
13885                 abort ();
13886               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13887               /* Was PPC64_TLS which sits on insn boundary, now
13888                  PPC64_TPREL16_LO which is at low-order half-word.  */
13889               rel->r_offset += d_offset;
13890               r_type = R_PPC64_TPREL16_LO;
13891               if (toc_symndx != 0)
13892                 {
13893                   rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13894                   rel->r_addend = toc_addend;
13895                   /* We changed the symbol.  Start over in order to
13896                      get h, sym, sec etc. right.  */
13897                   goto again;
13898                 }
13899               else
13900                 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13901             }
13902           break;
13903
13904         case R_PPC64_GOT_TLSGD16_HI:
13905         case R_PPC64_GOT_TLSGD16_HA:
13906           tls_gd = TLS_TPRELGD;
13907           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13908             goto tls_gdld_hi;
13909           break;
13910
13911         case R_PPC64_GOT_TLSLD16_HI:
13912         case R_PPC64_GOT_TLSLD16_HA:
13913           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13914             {
13915             tls_gdld_hi:
13916               if ((tls_mask & tls_gd) != 0)
13917                 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13918                           + R_PPC64_GOT_TPREL16_DS);
13919               else
13920                 {
13921                   rel->r_offset -= d_offset;
13922                   bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13923                   r_type = R_PPC64_NONE;
13924                 }
13925               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13926             }
13927           break;
13928
13929         case R_PPC64_GOT_TLSGD16:
13930         case R_PPC64_GOT_TLSGD16_LO:
13931           tls_gd = TLS_TPRELGD;
13932           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13933             goto tls_ldgd_opt;
13934           break;
13935
13936         case R_PPC64_GOT_TLSLD16:
13937         case R_PPC64_GOT_TLSLD16_LO:
13938           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13939             {
13940               unsigned int insn1, insn2;
13941               bfd_vma offset;
13942
13943             tls_ldgd_opt:
13944               offset = (bfd_vma) -1;
13945               /* If not using the newer R_PPC64_TLSGD/LD to mark
13946                  __tls_get_addr calls, we must trust that the call
13947                  stays with its arg setup insns, ie. that the next
13948                  reloc is the __tls_get_addr call associated with
13949                  the current reloc.  Edit both insns.  */
13950               if (input_section->has_tls_get_addr_call
13951                   && rel + 1 < relend
13952                   && branch_reloc_hash_match (input_bfd, rel + 1,
13953                                               htab->tls_get_addr,
13954                                               htab->tls_get_addr_fd))
13955                 offset = rel[1].r_offset;
13956               /* We read the low GOT_TLS (or TOC16) insn because we
13957                  need to keep the destination reg.  It may be
13958                  something other than the usual r3, and moved to r3
13959                  before the call by intervening code.  */
13960               insn1 = bfd_get_32 (input_bfd,
13961                                   contents + rel->r_offset - d_offset);
13962               if ((tls_mask & tls_gd) != 0)
13963                 {
13964                   /* IE */
13965                   insn1 &= (0x1f << 21) | (0x1f << 16);
13966                   insn1 |= 58 << 26;    /* ld */
13967                   insn2 = 0x7c636a14;   /* add 3,3,13 */
13968                   if (offset != (bfd_vma) -1)
13969                     rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13970                   if ((tls_mask & TLS_EXPLICIT) == 0)
13971                     r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13972                               + R_PPC64_GOT_TPREL16_DS);
13973                   else
13974                     r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13975                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13976                 }
13977               else
13978                 {
13979                   /* LE */
13980                   insn1 &= 0x1f << 21;
13981                   insn1 |= 0x3c0d0000;  /* addis r,13,0 */
13982                   insn2 = 0x38630000;   /* addi 3,3,0 */
13983                   if (tls_gd == 0)
13984                     {
13985                       /* Was an LD reloc.  */
13986                       if (toc_symndx)
13987                         sec = local_sections[toc_symndx];
13988                       for (r_symndx = 0;
13989                            r_symndx < symtab_hdr->sh_info;
13990                            r_symndx++)
13991                         if (local_sections[r_symndx] == sec)
13992                           break;
13993                       if (r_symndx >= symtab_hdr->sh_info)
13994                         r_symndx = STN_UNDEF;
13995                       rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13996                       if (r_symndx != STN_UNDEF)
13997                         rel->r_addend -= (local_syms[r_symndx].st_value
13998                                           + sec->output_offset
13999                                           + sec->output_section->vma);
14000                     }
14001                   else if (toc_symndx != 0)
14002                     {
14003                       r_symndx = toc_symndx;
14004                       rel->r_addend = toc_addend;
14005                     }
14006                   r_type = R_PPC64_TPREL16_HA;
14007                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14008                   if (offset != (bfd_vma) -1)
14009                     {
14010                       rel[1].r_info = ELF64_R_INFO (r_symndx,
14011                                                     R_PPC64_TPREL16_LO);
14012                       rel[1].r_offset = offset + d_offset;
14013                       rel[1].r_addend = rel->r_addend;
14014                     }
14015                 }
14016               bfd_put_32 (input_bfd, insn1,
14017                           contents + rel->r_offset - d_offset);
14018               if (offset != (bfd_vma) -1)
14019                 bfd_put_32 (input_bfd, insn2, contents + offset);
14020               if ((tls_mask & tls_gd) == 0
14021                   && (tls_gd == 0 || toc_symndx != 0))
14022                 {
14023                   /* We changed the symbol.  Start over in order
14024                      to get h, sym, sec etc. right.  */
14025                   goto again;
14026                 }
14027             }
14028           break;
14029
14030         case R_PPC64_TLSGD:
14031           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
14032             {
14033               unsigned int insn2;
14034               bfd_vma offset = rel->r_offset;
14035
14036               if ((tls_mask & TLS_TPRELGD) != 0)
14037                 {
14038                   /* IE */
14039                   r_type = R_PPC64_NONE;
14040                   insn2 = 0x7c636a14;   /* add 3,3,13 */
14041                 }
14042               else
14043                 {
14044                   /* LE */
14045                   if (toc_symndx != 0)
14046                     {
14047                       r_symndx = toc_symndx;
14048                       rel->r_addend = toc_addend;
14049                     }
14050                   r_type = R_PPC64_TPREL16_LO;
14051                   rel->r_offset = offset + d_offset;
14052                   insn2 = 0x38630000;   /* addi 3,3,0 */
14053                 }
14054               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14055               /* Zap the reloc on the _tls_get_addr call too.  */
14056               BFD_ASSERT (offset == rel[1].r_offset);
14057               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14058               bfd_put_32 (input_bfd, insn2, contents + offset);
14059               if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14060                 goto again;
14061             }
14062           break;
14063
14064         case R_PPC64_TLSLD:
14065           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
14066             {
14067               unsigned int insn2;
14068               bfd_vma offset = rel->r_offset;
14069
14070               if (toc_symndx)
14071                 sec = local_sections[toc_symndx];
14072               for (r_symndx = 0;
14073                    r_symndx < symtab_hdr->sh_info;
14074                    r_symndx++)
14075                 if (local_sections[r_symndx] == sec)
14076                   break;
14077               if (r_symndx >= symtab_hdr->sh_info)
14078                 r_symndx = STN_UNDEF;
14079               rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14080               if (r_symndx != STN_UNDEF)
14081                 rel->r_addend -= (local_syms[r_symndx].st_value
14082                                   + sec->output_offset
14083                                   + sec->output_section->vma);
14084
14085               r_type = R_PPC64_TPREL16_LO;
14086               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14087               rel->r_offset = offset + d_offset;
14088               /* Zap the reloc on the _tls_get_addr call too.  */
14089               BFD_ASSERT (offset == rel[1].r_offset);
14090               rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14091               insn2 = 0x38630000;       /* addi 3,3,0 */
14092               bfd_put_32 (input_bfd, insn2, contents + offset);
14093               goto again;
14094             }
14095           break;
14096
14097         case R_PPC64_DTPMOD64:
14098           if (rel + 1 < relend
14099               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14100               && rel[1].r_offset == rel->r_offset + 8)
14101             {
14102               if ((tls_mask & TLS_GD) == 0)
14103                 {
14104                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14105                   if ((tls_mask & TLS_TPRELGD) != 0)
14106                     r_type = R_PPC64_TPREL64;
14107                   else
14108                     {
14109                       bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14110                       r_type = R_PPC64_NONE;
14111                     }
14112                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14113                 }
14114             }
14115           else
14116             {
14117               if ((tls_mask & TLS_LD) == 0)
14118                 {
14119                   bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14120                   r_type = R_PPC64_NONE;
14121                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14122                 }
14123             }
14124           break;
14125
14126         case R_PPC64_TPREL64:
14127           if ((tls_mask & TLS_TPREL) == 0)
14128             {
14129               r_type = R_PPC64_NONE;
14130               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14131             }
14132           break;
14133
14134         case R_PPC64_ENTRY:
14135           relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14136           if (!bfd_link_pic (info)
14137               && !info->traditional_format
14138               && relocation + 0x80008000 <= 0xffffffff)
14139             {
14140               unsigned int insn1, insn2;
14141
14142               insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14143               insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14144               if ((insn1 & ~0xfffc) == LD_R2_0R12
14145                   && insn2 == ADD_R2_R2_R12)
14146                 {
14147                   bfd_put_32 (input_bfd,
14148                               LIS_R2 + PPC_HA (relocation),
14149                               contents + rel->r_offset);
14150                   bfd_put_32 (input_bfd,
14151                               ADDI_R2_R2 + PPC_LO (relocation),
14152                               contents + rel->r_offset + 4);
14153                 }
14154             }
14155           else
14156             {
14157               relocation -= (rel->r_offset
14158                              + input_section->output_offset
14159                              + input_section->output_section->vma);
14160               if (relocation + 0x80008000 <= 0xffffffff)
14161                 {
14162                   unsigned int insn1, insn2;
14163
14164                   insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14165                   insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14166                   if ((insn1 & ~0xfffc) == LD_R2_0R12
14167                       && insn2 == ADD_R2_R2_R12)
14168                     {
14169                       bfd_put_32 (input_bfd,
14170                                   ADDIS_R2_R12 + PPC_HA (relocation),
14171                                   contents + rel->r_offset);
14172                       bfd_put_32 (input_bfd,
14173                                   ADDI_R2_R2 + PPC_LO (relocation),
14174                                   contents + rel->r_offset + 4);
14175                     }
14176                 }
14177             }
14178           break;
14179
14180         case R_PPC64_REL16_HA:
14181           /* If we are generating a non-PIC executable, edit
14182              .  0:      addis 2,12,.TOC.-0b@ha
14183              .          addi 2,2,.TOC.-0b@l
14184              used by ELFv2 global entry points to set up r2, to
14185              .          lis 2,.TOC.@ha
14186              .          addi 2,2,.TOC.@l
14187              if .TOC. is in range.  */
14188           if (!bfd_link_pic (info)
14189               && !info->traditional_format
14190               && !htab->opd_abi
14191               && rel->r_addend == d_offset
14192               && h != NULL && &h->elf == htab->elf.hgot
14193               && rel + 1 < relend
14194               && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14195               && rel[1].r_offset == rel->r_offset + 4
14196               && rel[1].r_addend == rel->r_addend + 4
14197               && relocation + 0x80008000 <= 0xffffffff)
14198             {
14199               unsigned int insn1, insn2;
14200               bfd_vma offset = rel->r_offset - d_offset;
14201               insn1 = bfd_get_32 (input_bfd, contents + offset);
14202               insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14203               if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14204                   && (insn2 & 0xffff0000) == ADDI_R2_R2)
14205                 {
14206                   r_type = R_PPC64_ADDR16_HA;
14207                   rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14208                   rel->r_addend -= d_offset;
14209                   rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14210                   rel[1].r_addend -= d_offset + 4;
14211                   bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14212                 }
14213             }
14214           break;
14215         }
14216
14217       /* Handle other relocations that tweak non-addend part of insn.  */
14218       insn = 0;
14219       max_br_offset = 1 << 25;
14220       addend = rel->r_addend;
14221       reloc_dest = DEST_NORMAL;
14222       switch (r_type)
14223         {
14224         default:
14225           break;
14226
14227         case R_PPC64_TOCSAVE:
14228           if (relocation + addend == (rel->r_offset
14229                                       + input_section->output_offset
14230                                       + input_section->output_section->vma)
14231               && tocsave_find (htab, NO_INSERT,
14232                                &local_syms, rel, input_bfd))
14233             {
14234               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14235               if (insn == NOP
14236                   || insn == CROR_151515 || insn == CROR_313131)
14237                 bfd_put_32 (input_bfd,
14238                             STD_R2_0R1 + STK_TOC (htab),
14239                             contents + rel->r_offset);
14240             }
14241           break;
14242
14243           /* Branch taken prediction relocations.  */
14244         case R_PPC64_ADDR14_BRTAKEN:
14245         case R_PPC64_REL14_BRTAKEN:
14246           insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
14247           /* Fall through.  */
14248
14249           /* Branch not taken prediction relocations.  */
14250         case R_PPC64_ADDR14_BRNTAKEN:
14251         case R_PPC64_REL14_BRNTAKEN:
14252           insn |= bfd_get_32 (input_bfd,
14253                               contents + rel->r_offset) & ~(0x01 << 21);
14254           /* Fall through.  */
14255
14256         case R_PPC64_REL14:
14257           max_br_offset = 1 << 15;
14258           /* Fall through.  */
14259
14260         case R_PPC64_REL24:
14261           /* Calls to functions with a different TOC, such as calls to
14262              shared objects, need to alter the TOC pointer.  This is
14263              done using a linkage stub.  A REL24 branching to these
14264              linkage stubs needs to be followed by a nop, as the nop
14265              will be replaced with an instruction to restore the TOC
14266              base pointer.  */
14267           fdh = h;
14268           if (h != NULL
14269               && h->oh != NULL
14270               && h->oh->is_func_descriptor)
14271             fdh = ppc_follow_link (h->oh);
14272           stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14273                                            htab);
14274           if (stub_entry != NULL
14275               && (stub_entry->stub_type == ppc_stub_plt_call
14276                   || stub_entry->stub_type == ppc_stub_plt_call_r2save
14277                   || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14278                   || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14279             {
14280               bfd_boolean can_plt_call = FALSE;
14281
14282               if (stub_entry->stub_type == ppc_stub_plt_call
14283                   && !htab->opd_abi
14284                   && htab->params->plt_localentry0 != 0
14285                   && is_elfv2_localentry0 (&h->elf))
14286                 {
14287                   /* The function doesn't use or change r2.  */
14288                   can_plt_call = TRUE;
14289                 }
14290
14291               /* All of these stubs may modify r2, so there must be a
14292                  branch and link followed by a nop.  The nop is
14293                  replaced by an insn to restore r2.  */
14294               else if (rel->r_offset + 8 <= input_section->size)
14295                 {
14296                   unsigned long br;
14297
14298                   br = bfd_get_32 (input_bfd,
14299                                    contents + rel->r_offset);
14300                   if ((br & 1) != 0)
14301                     {
14302                       unsigned long nop;
14303
14304                       nop = bfd_get_32 (input_bfd,
14305                                         contents + rel->r_offset + 4);
14306                       if (nop == NOP
14307                           || nop == CROR_151515 || nop == CROR_313131)
14308                         {
14309                           if (h != NULL
14310                               && (h == htab->tls_get_addr_fd
14311                                   || h == htab->tls_get_addr)
14312                               && htab->params->tls_get_addr_opt)
14313                             {
14314                               /* Special stub used, leave nop alone.  */
14315                             }
14316                           else
14317                             bfd_put_32 (input_bfd,
14318                                         LD_R2_0R1 + STK_TOC (htab),
14319                                         contents + rel->r_offset + 4);
14320                           can_plt_call = TRUE;
14321                         }
14322                     }
14323                 }
14324
14325               if (!can_plt_call && h != NULL)
14326                 {
14327                   const char *name = h->elf.root.root.string;
14328
14329                   if (*name == '.')
14330                     ++name;
14331
14332                   if (strncmp (name, "__libc_start_main", 17) == 0
14333                       && (name[17] == 0 || name[17] == '@'))
14334                     {
14335                       /* Allow crt1 branch to go via a toc adjusting
14336                          stub.  Other calls that never return could do
14337                          the same, if we could detect such.  */
14338                       can_plt_call = TRUE;
14339                     }
14340                 }
14341
14342               if (!can_plt_call)
14343                 {
14344                   /* g++ as of 20130507 emits self-calls without a
14345                      following nop.  This is arguably wrong since we
14346                      have conflicting information.  On the one hand a
14347                      global symbol and on the other a local call
14348                      sequence, but don't error for this special case.
14349                      It isn't possible to cheaply verify we have
14350                      exactly such a call.  Allow all calls to the same
14351                      section.  */
14352                   asection *code_sec = sec;
14353
14354                   if (get_opd_info (sec) != NULL)
14355                     {
14356                       bfd_vma off = (relocation + addend
14357                                      - sec->output_section->vma
14358                                      - sec->output_offset);
14359
14360                       opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14361                     }
14362                   if (code_sec == input_section)
14363                     can_plt_call = TRUE;
14364                 }
14365
14366               if (!can_plt_call)
14367                 {
14368                   if (stub_entry->stub_type == ppc_stub_plt_call
14369                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14370                     info->callbacks->einfo
14371                       /* xgettext:c-format */
14372                       (_("%H: call to `%T' lacks nop, can't restore toc; "
14373                          "recompile with -fPIC\n"),
14374                        input_bfd, input_section, rel->r_offset, sym_name);
14375                   else
14376                     info->callbacks->einfo
14377                       /* xgettext:c-format */
14378                       (_("%H: call to `%T' lacks nop, can't restore toc; "
14379                          "(-mcmodel=small toc adjust stub)\n"),
14380                        input_bfd, input_section, rel->r_offset, sym_name);
14381
14382                   bfd_set_error (bfd_error_bad_value);
14383                   ret = FALSE;
14384                 }
14385
14386               if (can_plt_call
14387                   && (stub_entry->stub_type == ppc_stub_plt_call
14388                       || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14389                 unresolved_reloc = FALSE;
14390             }
14391
14392           if ((stub_entry == NULL
14393                || stub_entry->stub_type == ppc_stub_long_branch
14394                || stub_entry->stub_type == ppc_stub_plt_branch)
14395               && get_opd_info (sec) != NULL)
14396             {
14397               /* The branch destination is the value of the opd entry. */
14398               bfd_vma off = (relocation + addend
14399                              - sec->output_section->vma
14400                              - sec->output_offset);
14401               bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14402               if (dest != (bfd_vma) -1)
14403                 {
14404                   relocation = dest;
14405                   addend = 0;
14406                   reloc_dest = DEST_OPD;
14407                 }
14408             }
14409
14410           /* If the branch is out of reach we ought to have a long
14411              branch stub.  */
14412           from = (rel->r_offset
14413                   + input_section->output_offset
14414                   + input_section->output_section->vma);
14415
14416           relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14417                                                   ? fdh->elf.other
14418                                                   : sym->st_other);
14419
14420           if (stub_entry != NULL
14421               && (stub_entry->stub_type == ppc_stub_long_branch
14422                   || stub_entry->stub_type == ppc_stub_plt_branch)
14423               && (r_type == R_PPC64_ADDR14_BRTAKEN
14424                   || r_type == R_PPC64_ADDR14_BRNTAKEN
14425                   || (relocation + addend - from + max_br_offset
14426                       < 2 * max_br_offset)))
14427             /* Don't use the stub if this branch is in range.  */
14428             stub_entry = NULL;
14429
14430           if (stub_entry != NULL)
14431             {
14432               /* Munge up the value and addend so that we call the stub
14433                  rather than the procedure directly.  */
14434               asection *stub_sec = stub_entry->group->stub_sec;
14435
14436               if (stub_entry->stub_type == ppc_stub_save_res)
14437                 relocation += (stub_sec->output_offset
14438                                + stub_sec->output_section->vma
14439                                + stub_sec->size - htab->sfpr->size
14440                                - htab->sfpr->output_offset
14441                                - htab->sfpr->output_section->vma);
14442               else
14443                 relocation = (stub_entry->stub_offset
14444                               + stub_sec->output_offset
14445                               + stub_sec->output_section->vma);
14446               addend = 0;
14447               reloc_dest = DEST_STUB;
14448
14449               if ((stub_entry->stub_type == ppc_stub_plt_call
14450                    || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14451                   && (ALWAYS_EMIT_R2SAVE
14452                       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14453                   && rel + 1 < relend
14454                   && rel[1].r_offset == rel->r_offset + 4
14455                   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14456                 relocation += 4;
14457             }
14458
14459           if (insn != 0)
14460             {
14461               if (is_isa_v2)
14462                 {
14463                   /* Set 'a' bit.  This is 0b00010 in BO field for branch
14464                      on CR(BI) insns (BO == 001at or 011at), and 0b01000
14465                      for branch on CTR insns (BO == 1a00t or 1a01t).  */
14466                   if ((insn & (0x14 << 21)) == (0x04 << 21))
14467                     insn |= 0x02 << 21;
14468                   else if ((insn & (0x14 << 21)) == (0x10 << 21))
14469                     insn |= 0x08 << 21;
14470                   else
14471                     break;
14472                 }
14473               else
14474                 {
14475                   /* Invert 'y' bit if not the default.  */
14476                   if ((bfd_signed_vma) (relocation + addend - from) < 0)
14477                     insn ^= 0x01 << 21;
14478                 }
14479
14480               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14481             }
14482
14483           /* NOP out calls to undefined weak functions.
14484              We can thus call a weak function without first
14485              checking whether the function is defined.  */
14486           else if (h != NULL
14487                    && h->elf.root.type == bfd_link_hash_undefweak
14488                    && h->elf.dynindx == -1
14489                    && r_type == R_PPC64_REL24
14490                    && relocation == 0
14491                    && addend == 0)
14492             {
14493               bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14494               goto copy_reloc;
14495             }
14496           break;
14497         }
14498
14499       /* Set `addend'.  */
14500       tls_type = 0;
14501       switch (r_type)
14502         {
14503         default:
14504           info->callbacks->einfo
14505             /* xgettext:c-format */
14506             (_("%P: %B: unknown relocation type %d for `%T'\n"),
14507              input_bfd, (int) r_type, sym_name);
14508
14509           bfd_set_error (bfd_error_bad_value);
14510           ret = FALSE;
14511           goto copy_reloc;
14512
14513         case R_PPC64_NONE:
14514         case R_PPC64_TLS:
14515         case R_PPC64_TLSGD:
14516         case R_PPC64_TLSLD:
14517         case R_PPC64_TOCSAVE:
14518         case R_PPC64_GNU_VTINHERIT:
14519         case R_PPC64_GNU_VTENTRY:
14520         case R_PPC64_ENTRY:
14521           goto copy_reloc;
14522
14523           /* GOT16 relocations.  Like an ADDR16 using the symbol's
14524              address in the GOT as relocation value instead of the
14525              symbol's value itself.  Also, create a GOT entry for the
14526              symbol and put the symbol value there.  */
14527         case R_PPC64_GOT_TLSGD16:
14528         case R_PPC64_GOT_TLSGD16_LO:
14529         case R_PPC64_GOT_TLSGD16_HI:
14530         case R_PPC64_GOT_TLSGD16_HA:
14531           tls_type = TLS_TLS | TLS_GD;
14532           goto dogot;
14533
14534         case R_PPC64_GOT_TLSLD16:
14535         case R_PPC64_GOT_TLSLD16_LO:
14536         case R_PPC64_GOT_TLSLD16_HI:
14537         case R_PPC64_GOT_TLSLD16_HA:
14538           tls_type = TLS_TLS | TLS_LD;
14539           goto dogot;
14540
14541         case R_PPC64_GOT_TPREL16_DS:
14542         case R_PPC64_GOT_TPREL16_LO_DS:
14543         case R_PPC64_GOT_TPREL16_HI:
14544         case R_PPC64_GOT_TPREL16_HA:
14545           tls_type = TLS_TLS | TLS_TPREL;
14546           goto dogot;
14547
14548         case R_PPC64_GOT_DTPREL16_DS:
14549         case R_PPC64_GOT_DTPREL16_LO_DS:
14550         case R_PPC64_GOT_DTPREL16_HI:
14551         case R_PPC64_GOT_DTPREL16_HA:
14552           tls_type = TLS_TLS | TLS_DTPREL;
14553           goto dogot;
14554
14555         case R_PPC64_GOT16:
14556         case R_PPC64_GOT16_LO:
14557         case R_PPC64_GOT16_HI:
14558         case R_PPC64_GOT16_HA:
14559         case R_PPC64_GOT16_DS:
14560         case R_PPC64_GOT16_LO_DS:
14561         dogot:
14562           {
14563             /* Relocation is to the entry for this symbol in the global
14564                offset table.  */
14565             asection *got;
14566             bfd_vma *offp;
14567             bfd_vma off;
14568             unsigned long indx = 0;
14569             struct got_entry *ent;
14570
14571             if (tls_type == (TLS_TLS | TLS_LD)
14572                 && (h == NULL
14573                     || !h->elf.def_dynamic))
14574               ent = ppc64_tlsld_got (input_bfd);
14575             else
14576               {
14577                 if (h != NULL)
14578                   {
14579                     if (!htab->elf.dynamic_sections_created
14580                         || h->elf.dynindx == -1
14581                         || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14582                         || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14583                       /* This is actually a static link, or it is a
14584                          -Bsymbolic link and the symbol is defined
14585                          locally, or the symbol was forced to be local
14586                          because of a version file.  */
14587                       ;
14588                     else
14589                       {
14590                         indx = h->elf.dynindx;
14591                         unresolved_reloc = FALSE;
14592                       }
14593                     ent = h->elf.got.glist;
14594                   }
14595                 else
14596                   {
14597                     if (local_got_ents == NULL)
14598                       abort ();
14599                     ent = local_got_ents[r_symndx];
14600                   }
14601
14602                 for (; ent != NULL; ent = ent->next)
14603                   if (ent->addend == orig_rel.r_addend
14604                       && ent->owner == input_bfd
14605                       && ent->tls_type == tls_type)
14606                     break;
14607               }
14608
14609             if (ent == NULL)
14610               abort ();
14611             if (ent->is_indirect)
14612               ent = ent->got.ent;
14613             offp = &ent->got.offset;
14614             got = ppc64_elf_tdata (ent->owner)->got;
14615             if (got == NULL)
14616               abort ();
14617
14618             /* The offset must always be a multiple of 8.  We use the
14619                least significant bit to record whether we have already
14620                processed this entry.  */
14621             off = *offp;
14622             if ((off & 1) != 0)
14623               off &= ~1;
14624             else
14625               {
14626                 /* Generate relocs for the dynamic linker, except in
14627                    the case of TLSLD where we'll use one entry per
14628                    module.  */
14629                 asection *relgot;
14630                 bfd_boolean ifunc;
14631
14632                 *offp = off | 1;
14633                 relgot = NULL;
14634                 ifunc = (h != NULL
14635                          ? h->elf.type == STT_GNU_IFUNC
14636                          : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14637                 if (ifunc)
14638                   {
14639                     relgot = htab->elf.irelplt;
14640                     if (indx == 0)
14641                       htab->local_ifunc_resolver = 1;
14642                     else if (is_static_defined (&h->elf))
14643                       htab->maybe_local_ifunc_resolver = 1;
14644                   }
14645                 else if (indx != 0
14646                          || (bfd_link_pic (info)
14647                              && (h == NULL
14648                                  || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14649                                  || (tls_type == (TLS_TLS | TLS_LD)
14650                                      && !h->elf.def_dynamic))))
14651                   relgot = ppc64_elf_tdata (ent->owner)->relgot;
14652                 if (relgot != NULL)
14653                   {
14654                     outrel.r_offset = (got->output_section->vma
14655                                        + got->output_offset
14656                                        + off);
14657                     outrel.r_addend = addend;
14658                     if (tls_type & (TLS_LD | TLS_GD))
14659                       {
14660                         outrel.r_addend = 0;
14661                         outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14662                         if (tls_type == (TLS_TLS | TLS_GD))
14663                           {
14664                             loc = relgot->contents;
14665                             loc += (relgot->reloc_count++
14666                                     * sizeof (Elf64_External_Rela));
14667                             bfd_elf64_swap_reloca_out (output_bfd,
14668                                                        &outrel, loc);
14669                             outrel.r_offset += 8;
14670                             outrel.r_addend = addend;
14671                             outrel.r_info
14672                               = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14673                           }
14674                       }
14675                     else if (tls_type == (TLS_TLS | TLS_DTPREL))
14676                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14677                     else if (tls_type == (TLS_TLS | TLS_TPREL))
14678                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14679                     else if (indx != 0)
14680                       outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14681                     else
14682                       {
14683                         if (ifunc)
14684                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14685                         else
14686                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14687
14688                         /* Write the .got section contents for the sake
14689                            of prelink.  */
14690                         loc = got->contents + off;
14691                         bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14692                                     loc);
14693                       }
14694
14695                     if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14696                       {
14697                         outrel.r_addend += relocation;
14698                         if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14699                           {
14700                             if (htab->elf.tls_sec == NULL)
14701                               outrel.r_addend = 0;
14702                             else
14703                               outrel.r_addend -= htab->elf.tls_sec->vma;
14704                           }
14705                       }
14706                     loc = relgot->contents;
14707                     loc += (relgot->reloc_count++
14708                             * sizeof (Elf64_External_Rela));
14709                     bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14710                   }
14711
14712                 /* Init the .got section contents here if we're not
14713                    emitting a reloc.  */
14714                 else
14715                   {
14716                     relocation += addend;
14717                     if (tls_type != 0)
14718                       {
14719                         if (htab->elf.tls_sec == NULL)
14720                           relocation = 0;
14721                         else
14722                           {
14723                             if (tls_type & TLS_LD)
14724                               relocation = 0;
14725                             else
14726                               relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14727                             if (tls_type & TLS_TPREL)
14728                               relocation += DTP_OFFSET - TP_OFFSET;
14729                           }
14730
14731                         if (tls_type & (TLS_GD | TLS_LD))
14732                           {
14733                             bfd_put_64 (output_bfd, relocation,
14734                                         got->contents + off + 8);
14735                             relocation = 1;
14736                           }
14737                       }
14738                     bfd_put_64 (output_bfd, relocation,
14739                                 got->contents + off);
14740                   }
14741               }
14742
14743             if (off >= (bfd_vma) -2)
14744               abort ();
14745
14746             relocation = got->output_section->vma + got->output_offset + off;
14747             addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14748           }
14749           break;
14750
14751         case R_PPC64_PLT16_HA:
14752         case R_PPC64_PLT16_HI:
14753         case R_PPC64_PLT16_LO:
14754         case R_PPC64_PLT32:
14755         case R_PPC64_PLT64:
14756           /* Relocation is to the entry for this symbol in the
14757              procedure linkage table.  */
14758           {
14759             struct plt_entry **plt_list = NULL;
14760             if (h != NULL)
14761               plt_list = &h->elf.plt.plist;
14762             else if (local_got_ents != NULL)
14763               {
14764                 struct plt_entry **local_plt = (struct plt_entry **)
14765                   (local_got_ents + symtab_hdr->sh_info);
14766                 unsigned char *local_got_tls_masks = (unsigned char *)
14767                   (local_plt + symtab_hdr->sh_info);
14768                 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14769                   plt_list = local_plt + r_symndx;
14770               }
14771             if (plt_list)
14772               {
14773                 struct plt_entry *ent;
14774
14775                 for (ent = *plt_list; ent != NULL; ent = ent->next)
14776                   if (ent->plt.offset != (bfd_vma) -1
14777                       && ent->addend == orig_rel.r_addend)
14778                     {
14779                       asection *plt;
14780
14781                       plt = htab->elf.splt;
14782                       if (!htab->elf.dynamic_sections_created
14783                           || h == NULL
14784                           || h->elf.dynindx == -1)
14785                         plt = htab->elf.iplt;
14786                       relocation = (plt->output_section->vma
14787                                     + plt->output_offset
14788                                     + ent->plt.offset);
14789                       addend = 0;
14790                       unresolved_reloc = FALSE;
14791                       break;
14792                     }
14793               }
14794           }
14795           break;
14796
14797         case R_PPC64_TOC:
14798           /* Relocation value is TOC base.  */
14799           relocation = TOCstart;
14800           if (r_symndx == STN_UNDEF)
14801             relocation += htab->sec_info[input_section->id].toc_off;
14802           else if (unresolved_reloc)
14803             ;
14804           else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14805             relocation += htab->sec_info[sec->id].toc_off;
14806           else
14807             unresolved_reloc = TRUE;
14808           goto dodyn;
14809
14810           /* TOC16 relocs.  We want the offset relative to the TOC base,
14811              which is the address of the start of the TOC plus 0x8000.
14812              The TOC consists of sections .got, .toc, .tocbss, and .plt,
14813              in this order.  */
14814         case R_PPC64_TOC16:
14815         case R_PPC64_TOC16_LO:
14816         case R_PPC64_TOC16_HI:
14817         case R_PPC64_TOC16_DS:
14818         case R_PPC64_TOC16_LO_DS:
14819         case R_PPC64_TOC16_HA:
14820           addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14821           break;
14822
14823           /* Relocate against the beginning of the section.  */
14824         case R_PPC64_SECTOFF:
14825         case R_PPC64_SECTOFF_LO:
14826         case R_PPC64_SECTOFF_HI:
14827         case R_PPC64_SECTOFF_DS:
14828         case R_PPC64_SECTOFF_LO_DS:
14829         case R_PPC64_SECTOFF_HA:
14830           if (sec != NULL)
14831             addend -= sec->output_section->vma;
14832           break;
14833
14834         case R_PPC64_REL16:
14835         case R_PPC64_REL16_LO:
14836         case R_PPC64_REL16_HI:
14837         case R_PPC64_REL16_HA:
14838         case R_PPC64_REL16DX_HA:
14839           break;
14840
14841         case R_PPC64_REL14:
14842         case R_PPC64_REL14_BRNTAKEN:
14843         case R_PPC64_REL14_BRTAKEN:
14844         case R_PPC64_REL24:
14845           break;
14846
14847         case R_PPC64_TPREL16:
14848         case R_PPC64_TPREL16_LO:
14849         case R_PPC64_TPREL16_HI:
14850         case R_PPC64_TPREL16_HA:
14851         case R_PPC64_TPREL16_DS:
14852         case R_PPC64_TPREL16_LO_DS:
14853         case R_PPC64_TPREL16_HIGH:
14854         case R_PPC64_TPREL16_HIGHA:
14855         case R_PPC64_TPREL16_HIGHER:
14856         case R_PPC64_TPREL16_HIGHERA:
14857         case R_PPC64_TPREL16_HIGHEST:
14858         case R_PPC64_TPREL16_HIGHESTA:
14859           if (h != NULL
14860               && h->elf.root.type == bfd_link_hash_undefweak
14861               && h->elf.dynindx == -1)
14862             {
14863               /* Make this relocation against an undefined weak symbol
14864                  resolve to zero.  This is really just a tweak, since
14865                  code using weak externs ought to check that they are
14866                  defined before using them.  */
14867               bfd_byte *p = contents + rel->r_offset - d_offset;
14868
14869               insn = bfd_get_32 (input_bfd, p);
14870               insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14871               if (insn != 0)
14872                 bfd_put_32 (input_bfd, insn, p);
14873               break;
14874             }
14875           if (htab->elf.tls_sec != NULL)
14876             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14877           /* The TPREL16 relocs shouldn't really be used in shared
14878              libs or with non-local symbols as that will result in
14879              DT_TEXTREL being set, but support them anyway.  */
14880           goto dodyn;
14881
14882         case R_PPC64_DTPREL16:
14883         case R_PPC64_DTPREL16_LO:
14884         case R_PPC64_DTPREL16_HI:
14885         case R_PPC64_DTPREL16_HA:
14886         case R_PPC64_DTPREL16_DS:
14887         case R_PPC64_DTPREL16_LO_DS:
14888         case R_PPC64_DTPREL16_HIGH:
14889         case R_PPC64_DTPREL16_HIGHA:
14890         case R_PPC64_DTPREL16_HIGHER:
14891         case R_PPC64_DTPREL16_HIGHERA:
14892         case R_PPC64_DTPREL16_HIGHEST:
14893         case R_PPC64_DTPREL16_HIGHESTA:
14894           if (htab->elf.tls_sec != NULL)
14895             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14896           break;
14897
14898         case R_PPC64_ADDR64_LOCAL:
14899           addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14900                                               ? h->elf.other
14901                                               : sym->st_other);
14902           break;
14903
14904         case R_PPC64_DTPMOD64:
14905           relocation = 1;
14906           addend = 0;
14907           goto dodyn;
14908
14909         case R_PPC64_TPREL64:
14910           if (htab->elf.tls_sec != NULL)
14911             addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14912           goto dodyn;
14913
14914         case R_PPC64_DTPREL64:
14915           if (htab->elf.tls_sec != NULL)
14916             addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14917           /* Fall through.  */
14918
14919           /* Relocations that may need to be propagated if this is a
14920              dynamic object.  */
14921         case R_PPC64_REL30:
14922         case R_PPC64_REL32:
14923         case R_PPC64_REL64:
14924         case R_PPC64_ADDR14:
14925         case R_PPC64_ADDR14_BRNTAKEN:
14926         case R_PPC64_ADDR14_BRTAKEN:
14927         case R_PPC64_ADDR16:
14928         case R_PPC64_ADDR16_DS:
14929         case R_PPC64_ADDR16_HA:
14930         case R_PPC64_ADDR16_HI:
14931         case R_PPC64_ADDR16_HIGH:
14932         case R_PPC64_ADDR16_HIGHA:
14933         case R_PPC64_ADDR16_HIGHER:
14934         case R_PPC64_ADDR16_HIGHERA:
14935         case R_PPC64_ADDR16_HIGHEST:
14936         case R_PPC64_ADDR16_HIGHESTA:
14937         case R_PPC64_ADDR16_LO:
14938         case R_PPC64_ADDR16_LO_DS:
14939         case R_PPC64_ADDR24:
14940         case R_PPC64_ADDR32:
14941         case R_PPC64_ADDR64:
14942         case R_PPC64_UADDR16:
14943         case R_PPC64_UADDR32:
14944         case R_PPC64_UADDR64:
14945         dodyn:
14946           if ((input_section->flags & SEC_ALLOC) == 0)
14947             break;
14948
14949           if (NO_OPD_RELOCS && is_opd)
14950             break;
14951
14952           if (bfd_link_pic (info)
14953               ? ((h == NULL
14954                   || h->dyn_relocs != NULL)
14955                  && ((h != NULL && pc_dynrelocs (h))
14956                      || must_be_dyn_reloc (info, r_type)))
14957               : (h != NULL
14958                  ? h->dyn_relocs != NULL
14959                  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14960             {
14961               bfd_boolean skip, relocate;
14962               asection *sreloc;
14963               bfd_vma out_off;
14964               long indx = 0;
14965
14966               /* When generating a dynamic object, these relocations
14967                  are copied into the output file to be resolved at run
14968                  time.  */
14969
14970               skip = FALSE;
14971               relocate = FALSE;
14972
14973               out_off = _bfd_elf_section_offset (output_bfd, info,
14974                                                  input_section, rel->r_offset);
14975               if (out_off == (bfd_vma) -1)
14976                 skip = TRUE;
14977               else if (out_off == (bfd_vma) -2)
14978                 skip = TRUE, relocate = TRUE;
14979               out_off += (input_section->output_section->vma
14980                           + input_section->output_offset);
14981               outrel.r_offset = out_off;
14982               outrel.r_addend = rel->r_addend;
14983
14984               /* Optimize unaligned reloc use.  */
14985               if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14986                   || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14987                 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14988               else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14989                        || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14990                 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14991               else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14992                        || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14993                 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14994
14995               if (skip)
14996                 memset (&outrel, 0, sizeof outrel);
14997               else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14998                        && !is_opd
14999                        && r_type != R_PPC64_TOC)
15000                 {
15001                   indx = h->elf.dynindx;
15002                   BFD_ASSERT (indx != -1);
15003                   outrel.r_info = ELF64_R_INFO (indx, r_type);
15004                 }
15005               else
15006                 {
15007                   /* This symbol is local, or marked to become local,
15008                      or this is an opd section reloc which must point
15009                      at a local function.  */
15010                   outrel.r_addend += relocation;
15011                   if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15012                     {
15013                       if (is_opd && h != NULL)
15014                         {
15015                           /* Lie about opd entries.  This case occurs
15016                              when building shared libraries and we
15017                              reference a function in another shared
15018                              lib.  The same thing happens for a weak
15019                              definition in an application that's
15020                              overridden by a strong definition in a
15021                              shared lib.  (I believe this is a generic
15022                              bug in binutils handling of weak syms.)
15023                              In these cases we won't use the opd
15024                              entry in this lib.  */
15025                           unresolved_reloc = FALSE;
15026                         }
15027                       if (!is_opd
15028                           && r_type == R_PPC64_ADDR64
15029                           && (h != NULL
15030                               ? h->elf.type == STT_GNU_IFUNC
15031                               : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15032                         outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15033                       else
15034                         {
15035                           outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15036
15037                           /* We need to relocate .opd contents for ld.so.
15038                              Prelink also wants simple and consistent rules
15039                              for relocs.  This make all RELATIVE relocs have
15040                              *r_offset equal to r_addend.  */
15041                           relocate = TRUE;
15042                         }
15043                     }
15044                   else
15045                     {
15046                       if (h != NULL
15047                           ? h->elf.type == STT_GNU_IFUNC
15048                           : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15049                         {
15050                           info->callbacks->einfo
15051                             /* xgettext:c-format */
15052                             (_("%H: %s for indirect "
15053                                "function `%T' unsupported\n"),
15054                              input_bfd, input_section, rel->r_offset,
15055                              ppc64_elf_howto_table[r_type]->name,
15056                              sym_name);
15057                           ret = FALSE;
15058                         }
15059                       else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15060                         ;
15061                       else if (sec == NULL || sec->owner == NULL)
15062                         {
15063                           bfd_set_error (bfd_error_bad_value);
15064                           return FALSE;
15065                         }
15066                       else
15067                         {
15068                           asection *osec;
15069
15070                           osec = sec->output_section;
15071                           indx = elf_section_data (osec)->dynindx;
15072
15073                           if (indx == 0)
15074                             {
15075                               if ((osec->flags & SEC_READONLY) == 0
15076                                   && htab->elf.data_index_section != NULL)
15077                                 osec = htab->elf.data_index_section;
15078                               else
15079                                 osec = htab->elf.text_index_section;
15080                               indx = elf_section_data (osec)->dynindx;
15081                             }
15082                           BFD_ASSERT (indx != 0);
15083
15084                           /* We are turning this relocation into one
15085                              against a section symbol, so subtract out
15086                              the output section's address but not the
15087                              offset of the input section in the output
15088                              section.  */
15089                           outrel.r_addend -= osec->vma;
15090                         }
15091
15092                       outrel.r_info = ELF64_R_INFO (indx, r_type);
15093                     }
15094                 }
15095
15096               sreloc = elf_section_data (input_section)->sreloc;
15097               if (h != NULL
15098                   ? h->elf.type == STT_GNU_IFUNC
15099                   : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15100                 {
15101                   sreloc = htab->elf.irelplt;
15102                   if (indx == 0)
15103                     htab->local_ifunc_resolver = 1;
15104                   else if (is_static_defined (&h->elf))
15105                     htab->maybe_local_ifunc_resolver = 1;
15106                 }
15107               if (sreloc == NULL)
15108                 abort ();
15109
15110               if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15111                   >= sreloc->size)
15112                 abort ();
15113               loc = sreloc->contents;
15114               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15115               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15116
15117               /* If this reloc is against an external symbol, it will
15118                  be computed at runtime, so there's no need to do
15119                  anything now.  However, for the sake of prelink ensure
15120                  that the section contents are a known value.  */
15121               if (! relocate)
15122                 {
15123                   unresolved_reloc = FALSE;
15124                   /* The value chosen here is quite arbitrary as ld.so
15125                      ignores section contents except for the special
15126                      case of .opd where the contents might be accessed
15127                      before relocation.  Choose zero, as that won't
15128                      cause reloc overflow.  */
15129                   relocation = 0;
15130                   addend = 0;
15131                   /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15132                      to improve backward compatibility with older
15133                      versions of ld.  */
15134                   if (r_type == R_PPC64_ADDR64)
15135                     addend = outrel.r_addend;
15136                   /* Adjust pc_relative relocs to have zero in *r_offset.  */
15137                   else if (ppc64_elf_howto_table[r_type]->pc_relative)
15138                     addend = outrel.r_offset;
15139                 }
15140             }
15141           break;
15142
15143         case R_PPC64_COPY:
15144         case R_PPC64_GLOB_DAT:
15145         case R_PPC64_JMP_SLOT:
15146         case R_PPC64_JMP_IREL:
15147         case R_PPC64_RELATIVE:
15148           /* We shouldn't ever see these dynamic relocs in relocatable
15149              files.  */
15150           /* Fall through.  */
15151
15152         case R_PPC64_PLTGOT16:
15153         case R_PPC64_PLTGOT16_DS:
15154         case R_PPC64_PLTGOT16_HA:
15155         case R_PPC64_PLTGOT16_HI:
15156         case R_PPC64_PLTGOT16_LO:
15157         case R_PPC64_PLTGOT16_LO_DS:
15158         case R_PPC64_PLTREL32:
15159         case R_PPC64_PLTREL64:
15160           /* These ones haven't been implemented yet.  */
15161
15162           info->callbacks->einfo
15163             /* xgettext:c-format */
15164             (_("%P: %B: %s is not supported for `%T'\n"),
15165              input_bfd,
15166              ppc64_elf_howto_table[r_type]->name, sym_name);
15167
15168           bfd_set_error (bfd_error_invalid_operation);
15169           ret = FALSE;
15170           goto copy_reloc;
15171         }
15172
15173       /* Multi-instruction sequences that access the TOC can be
15174          optimized, eg. addis ra,r2,0; addi rb,ra,x;
15175          to             nop;           addi rb,r2,x;  */
15176       howto = ppc64_elf_howto_table[(int) r_type];
15177       switch (r_type)
15178         {
15179         default:
15180           break;
15181
15182         case R_PPC64_GOT_TLSLD16_HI:
15183         case R_PPC64_GOT_TLSGD16_HI:
15184         case R_PPC64_GOT_TPREL16_HI:
15185         case R_PPC64_GOT_DTPREL16_HI:
15186         case R_PPC64_GOT16_HI:
15187         case R_PPC64_TOC16_HI:
15188           /* These relocs would only be useful if building up an
15189              offset to later add to r2, perhaps in an indexed
15190              addressing mode instruction.  Don't try to optimize.
15191              Unfortunately, the possibility of someone building up an
15192              offset like this or even with the HA relocs, means that
15193              we need to check the high insn when optimizing the low
15194              insn.  */
15195           break;
15196
15197         case R_PPC64_GOT_TLSLD16_HA:
15198         case R_PPC64_GOT_TLSGD16_HA:
15199         case R_PPC64_GOT_TPREL16_HA:
15200         case R_PPC64_GOT_DTPREL16_HA:
15201         case R_PPC64_GOT16_HA:
15202         case R_PPC64_TOC16_HA:
15203           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15204               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15205             {
15206               bfd_byte *p = contents + (rel->r_offset & ~3);
15207               bfd_put_32 (input_bfd, NOP, p);
15208             }
15209           break;
15210
15211         case R_PPC64_GOT_TLSLD16_LO:
15212         case R_PPC64_GOT_TLSGD16_LO:
15213         case R_PPC64_GOT_TPREL16_LO_DS:
15214         case R_PPC64_GOT_DTPREL16_LO_DS:
15215         case R_PPC64_GOT16_LO:
15216         case R_PPC64_GOT16_LO_DS:
15217         case R_PPC64_TOC16_LO:
15218         case R_PPC64_TOC16_LO_DS:
15219           if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15220               && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15221             {
15222               bfd_byte *p = contents + (rel->r_offset & ~3);
15223               insn = bfd_get_32 (input_bfd, p);
15224               if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15225                 {
15226                   /* Transform addic to addi when we change reg.  */
15227                   insn &= ~((0x3f << 26) | (0x1f << 16));
15228                   insn |= (14u << 26) | (2 << 16);
15229                 }
15230               else
15231                 {
15232                   insn &= ~(0x1f << 16);
15233                   insn |= 2 << 16;
15234                 }
15235               bfd_put_32 (input_bfd, insn, p);
15236             }
15237           break;
15238
15239         case R_PPC64_TPREL16_HA:
15240           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15241             {
15242               bfd_byte *p = contents + (rel->r_offset & ~3);
15243               insn = bfd_get_32 (input_bfd, p);
15244               if ((insn & ((0x3f << 26) | 0x1f << 16))
15245                   != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15246                 /* xgettext:c-format */
15247                 info->callbacks->minfo
15248                   (_("%H: warning: %s unexpected insn %#x.\n"),
15249                    input_bfd, input_section, rel->r_offset, howto->name, insn);
15250               else
15251                 bfd_put_32 (input_bfd, NOP, p);
15252             }
15253           break;
15254
15255         case R_PPC64_TPREL16_LO:
15256         case R_PPC64_TPREL16_LO_DS:
15257           if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15258             {
15259               bfd_byte *p = contents + (rel->r_offset & ~3);
15260               insn = bfd_get_32 (input_bfd, p);
15261               insn &= ~(0x1f << 16);
15262               insn |= 13 << 16;
15263               bfd_put_32 (input_bfd, insn, p);
15264             }
15265           break;
15266         }
15267
15268       /* Do any further special processing.  */
15269       switch (r_type)
15270         {
15271         default:
15272           break;
15273
15274         case R_PPC64_REL16_HA:
15275         case R_PPC64_REL16DX_HA:
15276         case R_PPC64_ADDR16_HA:
15277         case R_PPC64_ADDR16_HIGHA:
15278         case R_PPC64_ADDR16_HIGHERA:
15279         case R_PPC64_ADDR16_HIGHESTA:
15280         case R_PPC64_TOC16_HA:
15281         case R_PPC64_SECTOFF_HA:
15282         case R_PPC64_TPREL16_HA:
15283         case R_PPC64_TPREL16_HIGHA:
15284         case R_PPC64_TPREL16_HIGHERA:
15285         case R_PPC64_TPREL16_HIGHESTA:
15286         case R_PPC64_DTPREL16_HA:
15287         case R_PPC64_DTPREL16_HIGHA:
15288         case R_PPC64_DTPREL16_HIGHERA:
15289         case R_PPC64_DTPREL16_HIGHESTA:
15290           /* It's just possible that this symbol is a weak symbol
15291              that's not actually defined anywhere. In that case,
15292              'sec' would be NULL, and we should leave the symbol
15293              alone (it will be set to zero elsewhere in the link).  */
15294           if (sec == NULL)
15295             break;
15296           /* Fall through.  */
15297
15298         case R_PPC64_GOT16_HA:
15299         case R_PPC64_PLTGOT16_HA:
15300         case R_PPC64_PLT16_HA:
15301         case R_PPC64_GOT_TLSGD16_HA:
15302         case R_PPC64_GOT_TLSLD16_HA:
15303         case R_PPC64_GOT_TPREL16_HA:
15304         case R_PPC64_GOT_DTPREL16_HA:
15305           /* Add 0x10000 if sign bit in 0:15 is set.
15306              Bits 0:15 are not used.  */
15307           addend += 0x8000;
15308           break;
15309
15310         case R_PPC64_ADDR16_DS:
15311         case R_PPC64_ADDR16_LO_DS:
15312         case R_PPC64_GOT16_DS:
15313         case R_PPC64_GOT16_LO_DS:
15314         case R_PPC64_PLT16_LO_DS:
15315         case R_PPC64_SECTOFF_DS:
15316         case R_PPC64_SECTOFF_LO_DS:
15317         case R_PPC64_TOC16_DS:
15318         case R_PPC64_TOC16_LO_DS:
15319         case R_PPC64_PLTGOT16_DS:
15320         case R_PPC64_PLTGOT16_LO_DS:
15321         case R_PPC64_GOT_TPREL16_DS:
15322         case R_PPC64_GOT_TPREL16_LO_DS:
15323         case R_PPC64_GOT_DTPREL16_DS:
15324         case R_PPC64_GOT_DTPREL16_LO_DS:
15325         case R_PPC64_TPREL16_DS:
15326         case R_PPC64_TPREL16_LO_DS:
15327         case R_PPC64_DTPREL16_DS:
15328         case R_PPC64_DTPREL16_LO_DS:
15329           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15330           mask = 3;
15331           /* If this reloc is against an lq, lxv, or stxv insn, then
15332              the value must be a multiple of 16.  This is somewhat of
15333              a hack, but the "correct" way to do this by defining _DQ
15334              forms of all the _DS relocs bloats all reloc switches in
15335              this file.  It doesn't make much sense to use these
15336              relocs in data, so testing the insn should be safe.  */
15337           if ((insn & (0x3f << 26)) == (56u << 26)
15338               || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15339             mask = 15;
15340           relocation += addend;
15341           addend = insn & (mask ^ 3);
15342           if ((relocation & mask) != 0)
15343             {
15344               relocation ^= relocation & mask;
15345               info->callbacks->einfo
15346                 /* xgettext:c-format */
15347                 (_("%H: error: %s not a multiple of %u\n"),
15348                  input_bfd, input_section, rel->r_offset,
15349                  howto->name,
15350                  mask + 1);
15351               bfd_set_error (bfd_error_bad_value);
15352               ret = FALSE;
15353               goto copy_reloc;
15354             }
15355           break;
15356         }
15357
15358       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15359          because such sections are not SEC_ALLOC and thus ld.so will
15360          not process them.  */
15361       if (unresolved_reloc
15362           && !((input_section->flags & SEC_DEBUGGING) != 0
15363                && h->elf.def_dynamic)
15364           && _bfd_elf_section_offset (output_bfd, info, input_section,
15365                                       rel->r_offset) != (bfd_vma) -1)
15366         {
15367           info->callbacks->einfo
15368             /* xgettext:c-format */
15369             (_("%H: unresolvable %s against `%T'\n"),
15370              input_bfd, input_section, rel->r_offset,
15371              howto->name,
15372              h->elf.root.root.string);
15373           ret = FALSE;
15374         }
15375
15376       /* 16-bit fields in insns mostly have signed values, but a
15377          few insns have 16-bit unsigned values.  Really, we should
15378          have different reloc types.  */
15379       if (howto->complain_on_overflow != complain_overflow_dont
15380           && howto->dst_mask == 0xffff
15381           && (input_section->flags & SEC_CODE) != 0)
15382         {
15383           enum complain_overflow complain = complain_overflow_signed;
15384
15385           insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15386           if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15387             complain = complain_overflow_bitfield;
15388           else if (howto->rightshift == 0
15389                    ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15390                       || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15391                       || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15392                    : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15393                       || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15394                       || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15395             complain = complain_overflow_unsigned;
15396           if (howto->complain_on_overflow != complain)
15397             {
15398               alt_howto = *howto;
15399               alt_howto.complain_on_overflow = complain;
15400               howto = &alt_howto;
15401             }
15402         }
15403
15404       if (r_type == R_PPC64_REL16DX_HA)
15405         {
15406           /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
15407           if (rel->r_offset + 4 > input_section->size)
15408             r = bfd_reloc_outofrange;
15409           else
15410             {
15411               relocation += addend;
15412               relocation -= (rel->r_offset
15413                              + input_section->output_offset
15414                              + input_section->output_section->vma);
15415               relocation = (bfd_signed_vma) relocation >> 16;
15416               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15417               insn &= ~0x1fffc1;
15418               insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15419               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15420               r = bfd_reloc_ok;
15421               if (relocation + 0x8000 > 0xffff)
15422                 r = bfd_reloc_overflow;
15423             }
15424         }
15425       else
15426         r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15427                                       rel->r_offset, relocation, addend);
15428
15429       if (r != bfd_reloc_ok)
15430         {
15431           char *more_info = NULL;
15432           const char *reloc_name = howto->name;
15433
15434           if (reloc_dest != DEST_NORMAL)
15435             {
15436               more_info = bfd_malloc (strlen (reloc_name) + 8);
15437               if (more_info != NULL)
15438                 {
15439                   strcpy (more_info, reloc_name);
15440                   strcat (more_info, (reloc_dest == DEST_OPD
15441                                       ? " (OPD)" : " (stub)"));
15442                   reloc_name = more_info;
15443                 }
15444             }
15445
15446           if (r == bfd_reloc_overflow)
15447             {
15448               /* On code like "if (foo) foo();" don't report overflow
15449                  on a branch to zero when foo is undefined.  */
15450               if (!warned
15451                   && (reloc_dest == DEST_STUB
15452                       || !(h != NULL
15453                            && (h->elf.root.type == bfd_link_hash_undefweak
15454                                || h->elf.root.type == bfd_link_hash_undefined)
15455                            && is_branch_reloc (r_type))))
15456                 info->callbacks->reloc_overflow (info, &h->elf.root,
15457                                                  sym_name, reloc_name,
15458                                                  orig_rel.r_addend,
15459                                                  input_bfd, input_section,
15460                                                  rel->r_offset);
15461             }
15462           else
15463             {
15464               info->callbacks->einfo
15465                 /* xgettext:c-format */
15466                 (_("%H: %s against `%T': error %d\n"),
15467                  input_bfd, input_section, rel->r_offset,
15468                  reloc_name, sym_name, (int) r);
15469               ret = FALSE;
15470             }
15471           if (more_info != NULL)
15472             free (more_info);
15473         }
15474     copy_reloc:
15475       if (wrel != rel)
15476         *wrel = *rel;
15477     }
15478
15479   if (wrel != rel)
15480     {
15481       Elf_Internal_Shdr *rel_hdr;
15482       size_t deleted = rel - wrel;
15483
15484       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15485       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15486       if (rel_hdr->sh_size == 0)
15487         {
15488           /* It is too late to remove an empty reloc section.  Leave
15489              one NONE reloc.
15490              ??? What is wrong with an empty section???  */
15491           rel_hdr->sh_size = rel_hdr->sh_entsize;
15492           deleted -= 1;
15493         }
15494       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15495       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15496       input_section->reloc_count -= deleted;
15497     }
15498
15499   /* If we're emitting relocations, then shortly after this function
15500      returns, reloc offsets and addends for this section will be
15501      adjusted.  Worse, reloc symbol indices will be for the output
15502      file rather than the input.  Save a copy of the relocs for
15503      opd_entry_value.  */
15504   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15505     {
15506       bfd_size_type amt;
15507       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15508       rel = bfd_alloc (input_bfd, amt);
15509       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15510       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15511       if (rel == NULL)
15512         return FALSE;
15513       memcpy (rel, relocs, amt);
15514     }
15515   return ret;
15516 }
15517
15518 /* Adjust the value of any local symbols in opd sections.  */
15519
15520 static int
15521 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15522                               const char *name ATTRIBUTE_UNUSED,
15523                               Elf_Internal_Sym *elfsym,
15524                               asection *input_sec,
15525                               struct elf_link_hash_entry *h)
15526 {
15527   struct _opd_sec_data *opd;
15528   long adjust;
15529   bfd_vma value;
15530
15531   if (h != NULL)
15532     return 1;
15533
15534   opd = get_opd_info (input_sec);
15535   if (opd == NULL || opd->adjust == NULL)
15536     return 1;
15537
15538   value = elfsym->st_value - input_sec->output_offset;
15539   if (!bfd_link_relocatable (info))
15540     value -= input_sec->output_section->vma;
15541
15542   adjust = opd->adjust[OPD_NDX (value)];
15543   if (adjust == -1)
15544     return 2;
15545
15546   elfsym->st_value += adjust;
15547   return 1;
15548 }
15549
15550 /* Finish up dynamic symbol handling.  We set the contents of various
15551    dynamic sections here.  */
15552
15553 static bfd_boolean
15554 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15555                                  struct bfd_link_info *info,
15556                                  struct elf_link_hash_entry *h,
15557                                  Elf_Internal_Sym *sym)
15558 {
15559   struct ppc_link_hash_table *htab;
15560   struct plt_entry *ent;
15561   Elf_Internal_Rela rela;
15562   bfd_byte *loc;
15563
15564   htab = ppc_hash_table (info);
15565   if (htab == NULL)
15566     return FALSE;
15567
15568   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15569     if (ent->plt.offset != (bfd_vma) -1)
15570       {
15571         /* This symbol has an entry in the procedure linkage
15572            table.  Set it up.  */
15573         if (!htab->elf.dynamic_sections_created
15574             || h->dynindx == -1)
15575           {
15576             BFD_ASSERT (h->type == STT_GNU_IFUNC
15577                         && h->def_regular
15578                         && (h->root.type == bfd_link_hash_defined
15579                             || h->root.type == bfd_link_hash_defweak));
15580             rela.r_offset = (htab->elf.iplt->output_section->vma
15581                              + htab->elf.iplt->output_offset
15582                              + ent->plt.offset);
15583             if (htab->opd_abi)
15584               rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15585             else
15586               rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15587             rela.r_addend = (h->root.u.def.value
15588                              + h->root.u.def.section->output_offset
15589                              + h->root.u.def.section->output_section->vma
15590                              + ent->addend);
15591             loc = (htab->elf.irelplt->contents
15592                    + (htab->elf.irelplt->reloc_count++
15593                       * sizeof (Elf64_External_Rela)));
15594             htab->local_ifunc_resolver = 1;
15595           }
15596         else
15597           {
15598             rela.r_offset = (htab->elf.splt->output_section->vma
15599                              + htab->elf.splt->output_offset
15600                              + ent->plt.offset);
15601             rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15602             rela.r_addend = ent->addend;
15603             loc = (htab->elf.srelplt->contents
15604                    + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15605                       / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15606             if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15607               htab->maybe_local_ifunc_resolver = 1;
15608           }
15609         bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15610
15611         if (!htab->opd_abi)
15612           {
15613             if (!h->def_regular)
15614               {
15615                 /* Mark the symbol as undefined, rather than as
15616                    defined in glink.  Leave the value if there were
15617                    any relocations where pointer equality matters
15618                    (this is a clue for the dynamic linker, to make
15619                    function pointer comparisons work between an
15620                    application and shared library), otherwise set it
15621                    to zero.  */
15622                 sym->st_shndx = SHN_UNDEF;
15623                 if (!h->pointer_equality_needed)
15624                   sym->st_value = 0;
15625                 else if (!h->ref_regular_nonweak)
15626                   {
15627                     /* This breaks function pointer comparisons, but
15628                        that is better than breaking tests for a NULL
15629                        function pointer.  */
15630                     sym->st_value = 0;
15631                   }
15632               }
15633           }
15634       }
15635
15636   if (h->needs_copy)
15637     {
15638       /* This symbol needs a copy reloc.  Set it up.  */
15639       asection *srel;
15640
15641       if (h->dynindx == -1
15642           || (h->root.type != bfd_link_hash_defined
15643               && h->root.type != bfd_link_hash_defweak)
15644           || htab->elf.srelbss == NULL
15645           || htab->elf.sreldynrelro == NULL)
15646         abort ();
15647
15648       rela.r_offset = (h->root.u.def.value
15649                        + h->root.u.def.section->output_section->vma
15650                        + h->root.u.def.section->output_offset);
15651       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15652       rela.r_addend = 0;
15653       if (h->root.u.def.section == htab->elf.sdynrelro)
15654         srel = htab->elf.sreldynrelro;
15655       else
15656         srel = htab->elf.srelbss;
15657       loc = srel->contents;
15658       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15659       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15660     }
15661
15662   return TRUE;
15663 }
15664
15665 /* Used to decide how to sort relocs in an optimal manner for the
15666    dynamic linker, before writing them out.  */
15667
15668 static enum elf_reloc_type_class
15669 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15670                             const asection *rel_sec,
15671                             const Elf_Internal_Rela *rela)
15672 {
15673   enum elf_ppc64_reloc_type r_type;
15674   struct ppc_link_hash_table *htab = ppc_hash_table (info);
15675
15676   if (rel_sec == htab->elf.irelplt)
15677     return reloc_class_ifunc;
15678
15679   r_type = ELF64_R_TYPE (rela->r_info);
15680   switch (r_type)
15681     {
15682     case R_PPC64_RELATIVE:
15683       return reloc_class_relative;
15684     case R_PPC64_JMP_SLOT:
15685       return reloc_class_plt;
15686     case R_PPC64_COPY:
15687       return reloc_class_copy;
15688     default:
15689       return reloc_class_normal;
15690     }
15691 }
15692
15693 /* Finish up the dynamic sections.  */
15694
15695 static bfd_boolean
15696 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15697                                    struct bfd_link_info *info)
15698 {
15699   struct ppc_link_hash_table *htab;
15700   bfd *dynobj;
15701   asection *sdyn;
15702
15703   htab = ppc_hash_table (info);
15704   if (htab == NULL)
15705     return FALSE;
15706
15707   dynobj = htab->elf.dynobj;
15708   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15709
15710   if (htab->elf.dynamic_sections_created)
15711     {
15712       Elf64_External_Dyn *dyncon, *dynconend;
15713
15714       if (sdyn == NULL || htab->elf.sgot == NULL)
15715         abort ();
15716
15717       dyncon = (Elf64_External_Dyn *) sdyn->contents;
15718       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15719       for (; dyncon < dynconend; dyncon++)
15720         {
15721           Elf_Internal_Dyn dyn;
15722           asection *s;
15723
15724           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15725
15726           switch (dyn.d_tag)
15727             {
15728             default:
15729               continue;
15730
15731             case DT_PPC64_GLINK:
15732               s = htab->glink;
15733               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15734               /* We stupidly defined DT_PPC64_GLINK to be the start
15735                  of glink rather than the first entry point, which is
15736                  what ld.so needs, and now have a bigger stub to
15737                  support automatic multiple TOCs.  */
15738               dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15739               break;
15740
15741             case DT_PPC64_OPD:
15742               s = bfd_get_section_by_name (output_bfd, ".opd");
15743               if (s == NULL)
15744                 continue;
15745               dyn.d_un.d_ptr = s->vma;
15746               break;
15747
15748             case DT_PPC64_OPT:
15749               if (htab->do_multi_toc && htab->multi_toc_needed)
15750                 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15751               if (htab->has_plt_localentry0)
15752                 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15753               break;
15754
15755             case DT_PPC64_OPDSZ:
15756               s = bfd_get_section_by_name (output_bfd, ".opd");
15757               if (s == NULL)
15758                 continue;
15759               dyn.d_un.d_val = s->size;
15760               break;
15761
15762             case DT_PLTGOT:
15763               s = htab->elf.splt;
15764               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15765               break;
15766
15767             case DT_JMPREL:
15768               s = htab->elf.srelplt;
15769               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15770               break;
15771
15772             case DT_PLTRELSZ:
15773               dyn.d_un.d_val = htab->elf.srelplt->size;
15774               break;
15775
15776             case DT_TEXTREL:
15777               if (htab->local_ifunc_resolver)
15778                 info->callbacks->einfo
15779                   (_("%X%P: text relocations and GNU indirect "
15780                      "functions will result in a segfault at runtime\n"));
15781               else if (htab->maybe_local_ifunc_resolver)
15782                 info->callbacks->einfo
15783                   (_("%P: warning: text relocations and GNU indirect "
15784                      "functions may result in a segfault at runtime\n"));
15785               continue;
15786             }
15787
15788           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15789         }
15790     }
15791
15792   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15793       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15794     {
15795       /* Fill in the first entry in the global offset table.
15796          We use it to hold the link-time TOCbase.  */
15797       bfd_put_64 (output_bfd,
15798                   elf_gp (output_bfd) + TOC_BASE_OFF,
15799                   htab->elf.sgot->contents);
15800
15801       /* Set .got entry size.  */
15802       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15803     }
15804
15805   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15806       && htab->elf.splt->output_section != bfd_abs_section_ptr)
15807     {
15808       /* Set .plt entry size.  */
15809       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15810         = PLT_ENTRY_SIZE (htab);
15811     }
15812
15813   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15814      brlt ourselves if emitrelocations.  */
15815   if (htab->brlt != NULL
15816       && htab->brlt->reloc_count != 0
15817       && !_bfd_elf_link_output_relocs (output_bfd,
15818                                        htab->brlt,
15819                                        elf_section_data (htab->brlt)->rela.hdr,
15820                                        elf_section_data (htab->brlt)->relocs,
15821                                        NULL))
15822     return FALSE;
15823
15824   if (htab->glink != NULL
15825       && htab->glink->reloc_count != 0
15826       && !_bfd_elf_link_output_relocs (output_bfd,
15827                                        htab->glink,
15828                                        elf_section_data (htab->glink)->rela.hdr,
15829                                        elf_section_data (htab->glink)->relocs,
15830                                        NULL))
15831     return FALSE;
15832
15833   if (htab->glink_eh_frame != NULL
15834       && htab->glink_eh_frame->size != 0)
15835     {
15836       bfd_vma val;
15837       bfd_byte *p;
15838       struct map_stub *group;
15839       size_t align = 4;
15840
15841       p = htab->glink_eh_frame->contents;
15842       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15843
15844       for (group = htab->group; group != NULL; group = group->next)
15845         if (group->stub_sec != NULL)
15846           {
15847             /* Offset to stub section.  */
15848             val = (group->stub_sec->output_section->vma
15849                    + group->stub_sec->output_offset);
15850             val -= (htab->glink_eh_frame->output_section->vma
15851                     + htab->glink_eh_frame->output_offset
15852                     + (p + 8 - htab->glink_eh_frame->contents));
15853             if (val + 0x80000000 > 0xffffffff)
15854               {
15855                 info->callbacks->einfo
15856                   (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15857                    group->stub_sec->name);
15858                 return FALSE;
15859               }
15860             bfd_put_32 (dynobj, val, p + 8);
15861             p += stub_eh_frame_size (group, align);
15862           }
15863       if (htab->glink != NULL && htab->glink->size != 0)
15864         {
15865           /* Offset to .glink.  */
15866           val = (htab->glink->output_section->vma
15867                  + htab->glink->output_offset
15868                  + 8);
15869           val -= (htab->glink_eh_frame->output_section->vma
15870                   + htab->glink_eh_frame->output_offset
15871                   + (p + 8 - htab->glink_eh_frame->contents));
15872           if (val + 0x80000000 > 0xffffffff)
15873             {
15874               info->callbacks->einfo
15875                 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15876                  htab->glink->name);
15877               return FALSE;
15878             }
15879           bfd_put_32 (dynobj, val, p + 8);
15880           p += (24 + align - 1) & -align;
15881         }
15882
15883       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15884           && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15885                                                htab->glink_eh_frame,
15886                                                htab->glink_eh_frame->contents))
15887         return FALSE;
15888     }
15889
15890   /* We need to handle writing out multiple GOT sections ourselves,
15891      since we didn't add them to DYNOBJ.  We know dynobj is the first
15892      bfd.  */
15893   while ((dynobj = dynobj->link.next) != NULL)
15894     {
15895       asection *s;
15896
15897       if (!is_ppc64_elf (dynobj))
15898         continue;
15899
15900       s = ppc64_elf_tdata (dynobj)->got;
15901       if (s != NULL
15902           && s->size != 0
15903           && s->output_section != bfd_abs_section_ptr
15904           && !bfd_set_section_contents (output_bfd, s->output_section,
15905                                         s->contents, s->output_offset,
15906                                         s->size))
15907         return FALSE;
15908       s = ppc64_elf_tdata (dynobj)->relgot;
15909       if (s != NULL
15910           && s->size != 0
15911           && s->output_section != bfd_abs_section_ptr
15912           && !bfd_set_section_contents (output_bfd, s->output_section,
15913                                         s->contents, s->output_offset,
15914                                         s->size))
15915         return FALSE;
15916     }
15917
15918   return TRUE;
15919 }
15920
15921 #include "elf64-target.h"
15922
15923 /* FreeBSD support */
15924
15925 #undef  TARGET_LITTLE_SYM
15926 #undef  TARGET_LITTLE_NAME
15927
15928 #undef  TARGET_BIG_SYM
15929 #define TARGET_BIG_SYM  powerpc_elf64_fbsd_vec
15930 #undef  TARGET_BIG_NAME
15931 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15932
15933 #undef  ELF_OSABI
15934 #define ELF_OSABI       ELFOSABI_FREEBSD
15935
15936 #undef  elf64_bed
15937 #define elf64_bed       elf64_powerpc_fbsd_bed
15938
15939 #include "elf64-target.h"